-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.py
More file actions
executable file
·332 lines (279 loc) · 8.58 KB
/
assembler.py
File metadata and controls
executable file
·332 lines (279 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/python
import getopt, sys, re, os
import struct
iset = {} # instruction set
labels = {} # label table
alias = {'sp':'r11', 'ba':'r12', 'fl':'r13', 'c1':'r14', 'c2':'r15'}
magicNumber = 0xDEADC0DE
baseAddress = stackAddress = stackSize = heapAddress = heapSize = 0x0
header = False
doAssemble = False
binaryFile = asciiFile = symbolFile = debugFile = None
def error(msg):
print >> sys.stderr, msg
sys.exit(1)
def debug(msg):
print >> sys.stderr, msg
def output(msg, trailer = ' '):
# ASCII Binary STDOUT
sys.stdout.write(msg + trailer)
# Binary File
if binaryFile is not None:
n = int(msg, 2)
if len(msg) == 8:
binaryFile.write( struct.pack('<B', n) )
if len(msg) == 16:
binaryFile.write( struct.pack('<H', n) )
if len(msg) == 32:
binaryFile.write( struct.pack('<I', n) )
# ASCII Binary File
if asciiFile is not None:
asciiFile.write(msg + trailer)
def exportSymbol(label):
if symbolFile is not None:
symbolFile.write(".%s 0x%X\n" % (label, labels[label]))
def exportDebug(lineNum, progOffset):
if debugFile is not None:
debugFile.write("%d 0x%X\n" % (lineNum, progOffset - baseAddress))
def parseISA():
parse = 0
header = open("isa.h", "r")
for line in header:
if parse == 0:
if "Instruction Op Codes" in line:
parse = 1;
continue
if "End Instruction Op Codes" in line:
parse = 0;
break;
code, octothorpe, comment = line.partition('//')
code = code.strip()
comment = comment.strip()
if code == "":
continue
i = code.split()
o = comment.split()
#debug(i, o)
op = { 'opcode': "", 'operands': 0, 'var': 0, 'opr0': "", 'opr1': "", 'opr2': "" }
op['opcode'] = i[2][2:]
if len(o) > 0:
op['var'] = int(o[0], 10)
if len(o) > 1:
op['operands'] = len(o) - 1
op['opr0'] = ''.join(re.search('\[(.*?)\]', o[1]).group(1).split(','))
if len(o) > 2:
op['opr1'] = ''.join(re.search('\[(.*?)\]', o[2]).group(1).split(','))
if len(o) > 3:
op['opr2'] = ''.join(re.search('\[(.*?)\]', o[3]).group(1).split(','))
#debug(op)
iset[i[1]] = op
#for i in iset:
# debug(i, iset[i])
def loadLabels(source):
lineNum = 0
progOffset = baseAddress
for line in source:
export = False
lineNum += 1
code, octothorpe, comment = line.partition('#')
code, semicolon, comment = code.partition(';')
code = code.strip()
if code == "":
continue
tokens = code.split()
if tokens[0] == 'export':
export = True
tokens = tokens[1:]
if tokens[0][0] == '.':
if tokens[0][1:] in labels:
error('line '+str(line)+': A label named '+tokens[0][1:]+' already exists.')
if len(tokens) > 2:
error('line '+str(line)+': Invalid label syntax, too many tokens.')
elif len(tokens) > 1:
labels[tokens[0][1:]] = int(tokens[1], 0)
else:
labels[tokens[0][1:]] = progOffset
if export:
exportSymbol(tokens[0][1:])
#debug("%d:%s -> %X" % (lineNum, tokens[0][1:], labels[tokens[0][1:]]))
elif tokens[0] == 'w':
progOffset += 4
elif tokens[0] == 'b':
progOffset += 1
else:
exportDebug(lineNum, progOffset);
progOffset += 8
def parseOperand(opr, modes, line, bits):
modebit = 0
absolute = 0
if opr[0] == '@':
if '@' not in modes:
error('line '+str(line)+': Operand does not support absolute addressing '+str(opr))
absolute = 1
opr = opr[1:] #strip @ symbol
if opr in alias:
opr = alias[opr]
if opr[0] == '.':
label, plus, number = opr.partition('+')
if label[1:] not in labels:
error('line '+str(line)+': Can\'t find label \''+label+'\'')
offset = 0
if plus:
offset = int(number, 0)
opr = str(labels[label[1:]] + offset)
if opr[0] == 'r':
if 'r' not in modes:
error('line '+str(line)+': Operand does not support register direct mode.')
if 'r' in modes and 'c' in modes:
modebit = 1
return format(int(opr[1:]), bits), modebit, absolute
else:
if 'c' not in modes:
error('line '+str(line)+': Operand does not support immediate mode '+str(opr))
if opr[0] == '\'' and opr[2] == '\'':
opr = str(ord(opr[1]))
return format(int(opr, 0), bits), modebit, absolute
def assemble(source):
loadLabels(source)
source.seek(0)
# executable binary header
if header is True:
output(format(magicNumber, '032b'))
output(format(baseAddress, '032b'), '\n')
output(format(stackAddress, '032b'))
output(format(stackSize, '032b'), '\n')
output(format(heapAddress, '032b'))
output(format(heapSize, '032b'), '\n')
lineNum = 0
for line in source:
lineNum += 1
code, octothorpe, comment = line.partition('#')
code, semicolon, comment = code.partition(';')
code = code.strip()
if code == "":
continue
tokens = code.split()
i = tokens[0]
args = tokens[1:]
#print i, args
if i[0] == '.' or i == 'export':
continue # skip label definitions
if i == 'w':
opr0, m0, a0 = parseOperand(args[0], 'c', lineNum, '032b')
output(opr0, '\n')
continue;
if i == 'b':
opr0, m0, a0 = parseOperand(args[0], 'c', lineNum, '08b')
output(opr0, '\n')
continue;
if i not in iset:
error('line '+str(lineNum)+': Unrecognized assembly instruction: '+i)
sys.exit(1)
op = iset[i]
opcode = op['opcode']
opr0 = opr1 = opr2 = None
m0 = m1 = m2 = 0
a0 = a1 = a2 = 0
if int(op['operands']) != len(args):
error('line '+str(lineNum)+': Expected '+str(op['operands'])+' operands but found '+str(len(args)))
sys.exit(1)
#debug(op)
#debug(args)
if len(args) > 0:
opr0, m0, a0 = parseOperand(args[0], op['opr0'], lineNum, '032b' if op['var'] == 0 else '08b')
if len(args) > 1:
opr1, m1, a1 = parseOperand(args[1], op['opr1'], lineNum, '032b' if op['var'] == 1 else '08b')
if len(args) > 2:
opr2, m2, a2 = parseOperand(args[2], op['opr2'], lineNum, '032b' if op['var'] == 2 else '08b')
mode = m0+m1+m2
if mode > 1:
error('line '+str(lineNum)+': Only one operand can be variable mode:'+str(m0)+str(m1)+str(m2))
sys.exit(1)
absolute = a0+a1+a2
if absolute > 1:
error('line '+str(lineNum)+': Only one operand can use absolute addressing:'+str(a0)+str(a1)+str(a2))
sys.exit(1)
mode = (mode | (absolute << 1))
mode = format(int(mode), '08b')
# Only one operand is variable and which one differs based on the opcode.
# Makes this assembler code look ugly, but lets us have compact binaries.
if op['var'] == 0:
packInstruction(opcode, mode, opr1, opr2, opr0)
if op['var'] == 1:
packInstruction(opcode, mode, opr0, opr2, opr1)
if op['var'] == 2:
packInstruction(opcode, mode, opr0, opr1, opr2)
def packInstruction(opcode, mode, opr0, opr1, opr2):
if opr0 is None:
opr0 = format(0, '08b')
if opr1 is None:
opr1 = format(0, '08b')
if opr2 is None:
opr2 = format(0, '032b')
output(opcode)
output(mode)
output(opr0)
output(opr1)
output(opr2, '\n')
options = [
('h','help','This help.'),
('a:','assemble=','Translate assembly language into machine code.'),
('d:','disassemble=','Translate machine code into assembly language.'),
('b','binary','Output machine code to binary file.'),
('t','text','Output machine code to ascii binary file.'),
('e','symbols','Output exported symbols to ABI file.'),
('g','debug','Output debug info to file.'),
('s:','base-address','Base address where the binary will start in memory.')]
shortOpt = "".join([opt[0] for opt in options])
longOpt = [opt[1] for opt in options]
def usage():
pad = str(len(max(longOpt, key=len)))
fmt = ' -%s, --%-'+pad+'s : %s'
debug('Usage: '+sys.argv[0]+' [options]\n')
for opt in options:
debug(fmt%(opt[0][0], opt[1], opt[2]))
sys.exit(1)
def main():
global binaryFile, asciiFile, symbolFile, debugFile
global baseAddress
base = None
try:
opts, args = getopt.getopt(sys.argv[1:], shortOpt, longOpt)
except getopt.GetoptError as err:
debug(str(err))
usage()
parseISA()
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-a', '--assemble'):
base = os.path.splitext(a)[0]
source = open(a, "r")
doAssemble = True
elif o in ('-d', '--disassemble'):
rom = open(a, "r");
elif o in ('-b', '--binary'):
binaryFile = open(base + '.bin', "wb")
elif o in ('-t', '--text'):
asciiFile = open(base + '.rom', "w")
elif o in ('-e', '--symbols'):
symbolFile = open(base + '.sym', "w")
elif o in ('-g', '--debug'):
debugFile = open(base + '.debug', "w")
elif o in ('-s', '--base-address'):
baseAddress = int(a, 0)
#TODO: finish implementing header info
elif o in ('-s', '--stack-address'):
stackAddress = int(a, 0)
elif o in ('-z', '--stack-size'):
stackSize = int(a, 0)
elif o in ('-p', '--heap-address'):
heapAddress = int(a, 0)
elif o in ('-z', '--heap-size'):
heapSize = int(a, 0)
else:
assert False, 'Unhandled option: ' + str(o)
if doAssemble:
assemble(source)
if __name__ == "__main__":
main()