forked from mountainstorm/Subleq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubleq-asm.py
executable file
·513 lines (419 loc) · 11.7 KB
/
subleq-asm.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/python
# coding: latin-1
#
# subleq.py
# subleq
#
# Created by R J Cooper on 10/09/2011.
# Copyright 2011 Mountainstorm. All rights reserved.
#
# subleq assembler
#
import ply.lex as lex
import ply.yacc as yacc
import os
import os.path
import types
import struct
import sys
import subprocess
import pickle
class Parser:
tokens = ()
precedence = ()
def __init__(self, **kw):
self.debug = kw.get('debug', 0)
self.names = { }
try:
modname = os.path.split(os.path.splitext(__file__)[0])[1] + "_" + self.__class__.__name__
except:
modname = "parser"+"_"+self.__class__.__name__
self.debugfile = modname + ".dbg"
self.tabmodule = modname + "_" + "parsetab"
#print self.debugfile, self.tabmodule
# Build the lexer and parser
lex.lex(module=self, debug=self.debug)
yacc.yacc(module=self,
debug=self.debug,
debugfile=self.debugfile,
tabmodule=self.tabmodule)
def run(self, data):
yacc.parse(data)
class Subleq(Parser):
def __init__(self, pack):
Parser.__init__(self)
self.debug = False
self.pack = pack # the struct.pack string used for a word
self.code = ""
self.linking = None
self.debugInfo = None
def run(self, data):
self.code = "" # the code stream
self.instStack = []
self.linking = {
"labels": {}, # dict of label names against their offsets
"literals": {}, # dict of literal values and the offsets to point to them
"fixups": {}, # dict of offsets (to fixup) against the label it should point to
"offsets": {}, # dict of offsets against the label/literal we want the offset of
"derefs": {} # dict of offsets against the label we want the value at
}
self.dbg = {
"labels": {}, # dict of label names against their offsets
"literals": {}, # dict of literal against their offsets
"offsets" : {}, # dict of .offset strings against their offsets
"inst" : {}, # dict of offsets against {inst:"", children:{}}
}
Parser.run(self, data)
self.link()
def link(self):
self.addAlign(4) # align the literals - not needed but good practice
# store of all literal names aagainst their locations
literals = {}
# output up the literals and fix them up
for l in self.linking["literals"]:
offsets = self.linking["literals"][l]
for o in offsets:
self.patch(o, len(self.code))
literals[l] = len(self.code)
self.code += struct.pack(self.pack, l)
# all the labels exist now; so process all the fixups to point to their labels
for f in self.linking["fixups"]:
label = self.linking["fixups"][f]
if label in self.linking["labels"]:
self.patch(f, self.linking["labels"][label])
else:
print "Error; label %s not defined" % self.linking["fixups"][f]
# sort out all the address
for o in self.linking["offsets"]:
label = self.linking["offsets"][o]
# a literal or unmodified label
try:
num = int(label)
offset = self.getLiteralOffset(num, literals)
self.dbg["offsets"][offset] = ".offset " + label
except:
offset = 0
if label in self.linking["labels"]:
num = self.linking["labels"][label]
offset = self.getLiteralOffset(num, literals)
self.dbg["offsets"][".offset " + label] = offset
else:
print "Error; label %s not defined" % label
self.patch(o, offset)
# sort out all the derefs
for d in self.linking["derefs"]:
label = self.linking["derefs"][d]
offset = 0
if label in self.linking["labels"]:
num = self.linking["labels"][label]
offset = (num * -1)
else:
print "Error; label %s not defined" % label
self.patch(d, offset)
# put labels into dbg
self.dbg["labels"] = self.linking["labels"]
self.dbg["literals"] = literals
def getLiteralOffset(self, num, literals):
# find this num in the literals table
if num in literals:
offset = literals[num]
else:
offset = len(self.code)
literals[num] = offset # add our offset literal into dbg
self.code += struct.pack(self.pack, num)
return offset
def patch(self, offset, value):
pre = self.code[:offset]
post = self.code[offset + self.getWordSize():]
self.code = pre + struct.pack(self.pack, value) + post
def call(self, inst, *args):
inst = inst.lower()
p = [None, inst]
for i in xrange(0, len(args)):
if type(args[i]) is not types.ListType:
p.append([args[i]])
else:
p.append(args[i])
if i != (len(args) - 1):
p.append(",")
method = getattr(self, "p_%s" % inst)
method(p)
def addLabel(self, label):
if label not in self.linking["labels"]:
self.linking["labels"][label] = len(self.code)
else:
print "Error; label already defined"
def addLiteral(self, literal):
if literal in self.linking["literals"]:
self.linking["literals"][literal].append(len(self.code))
else:
self.linking["literals"][literal] = [len(self.code)]
def addFixup(self, label):
self.linking["fixups"][len(self.code)] = label
def addOffset(self, label):
self.linking["offsets"][len(self.code)] = label
def addDeref(self, label):
self.linking["derefs"][len(self.code)] = label
def addAlign(self, value):
if len(self.code) % value:
pad = self.getWordSize() - (len(self.code) % self.getWordSize())
self.code += struct.pack("%ux" % pad)
def beginCallDbg(self, p):
inst = "%s" % p[1]
if len(p) >= 3:
inst += " %s" % self.argToStr(p[2])
if len(p) >= 5:
inst += ", %s" % self.argToStr(p[4])
if len(p) >= 7:
inst += ", %s" % self.argToStr(p[6])
call = {"inst": inst, "offset": len(self.code), "children":{}}
if len(self.instStack) > 0:
self.instStack[-1]["children"][len(self.code)] = call
self.instStack.append(call)
def argToStr(self, arg):
if type(arg) == types.ListType:
if len(arg) == 1:
arg = arg[0]
elif len(arg) == 2:
arg = arg[0] + " " + arg[1]
elif len(arg) == 3:
arg = arg[0] + arg[1] + arg[2]
return arg
def endCallDbg(self):
call = self.instStack.pop()
call["length"] = len(self.code) - call["offset"]
if len(self.instStack) == 0:
self.dbg["inst"][call["offset"]] = call
def packWord(self, value):
return struct.pack(self.pack, value)
def addSubleqArg(self, arg):
if arg[0] == ".offset":
self.addOffset(arg[1])
elif arg[0] == "[":
self.addDeref(arg[1])
else:
# a number or unmodified label
try:
self.addLiteral(int(arg[0])) # add a const and a fixup to it
except:
self.addFixup(arg[0]) # fixup to label
self.code += self.packWord(0)
def getSubleqSize(self):
return (3 * self.getWordSize())
def getWordSize(self):
return struct.calcsize(self.pack)
def dump(self, start = 0):
# debug dump the program
for i in xrange(start, len(self.code), 4):
a, = struct.unpack("<I", self.code[i:i+4])
comment = ""
if i in self.dbg["comments"]:
comment = self.dbg["comments"][i]
print "0x%08x : 0x%08x%s" % (i, a, comment)
#
# Token definition
#
instructions = (
'SUBLEQ',
'JMP', 'SUB', 'ADD', 'MOV', 'PUSH',
'CALLC', 'HALT',
)
tokens = instructions + (
'COMMA', 'COLON', 'LBRACE', 'RBRACE',
'OFFSET',
'ASCII', 'INT', 'FILL', 'ALIGN',
'IDENTIFIER', 'NUMBER', 'STRING',
)
# Token regex's
t_COMMA = r','
t_COLON = r':'
t_LBRACE = r'\['
t_RBRACE = r'\]'
t_OFFSET = r'\.offset'
t_ASCII = r'\.ascii'
t_INT = r'\.int'
t_FILL = r'\.fill'
t_ALIGN = r'\.align'
def t_IDENTIFIER(self, t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
val = t.value.upper()
if val in self.instructions:
t.type = val
else:
try:
self.t_NUMBER(t)
except:
pass
return t
def t_NUMBER(self, t):
r'[-+]?\d+|[a-fA-F0-9]+h|[01]+b'
if t.value[-1] == 'b':
t.value = int(t.value[:-1], 2)
elif t.value[-1] == 'h':
t.value = int(t.value[:-1], 16)
t.type = "NUMBER"
else:
t.value = int(t.value)
return t
def t_STRING(self, t):
r'"([^"]*)"'
t.value = t.value[1:-1]
return t
t_ignore = ' \t'
def t_COMMENT(self, t):
r'[;#][^\n]*' # ignore both comments and # (preprocessor) lines
pass
def t_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(self, t):
#raise SyntaxError("Unknown symbol %r" % (t.value[0],))
print "Skipping", repr(t.value[0])
t.lexer.skip(1)
#
# Statements
#
def p_module(self, p):
'''module : statement-list'''
pass
def p_statement_list(self, p):
'''statement-list : statement
| statement statement-list'''
pass
def p_statement(self, p):
'''statement : label
| int
| ascii
| fill
| align
| subleq
| jmp
| sub
| add
| mov
| push
| callc
| halt'''
pass
def p_label(self, p):
'''label : IDENTIFIER COLON'''
self.addLabel(p[1])
def p_int(self, p):
'''int : INT intargs'''
self.code += p[2]
def p_intargs(self, p):
'''intargs : NUMBER
| NUMBER COMMA intargs'''
p[0] = self.packWord(p[1])
if len(p) == 4:
p[0] += p[3]
def p_ascii(self, p):
'''ascii : ASCII STRING'''
self.code += struct.pack("%us" % len(p[2]), p[2])
self.addAlign(self.getWordSize())
def p_fill(self, p):
'''fill : FILL NUMBER'''
for i in xrange(0, p[2]):
self.code += self.packWord(0)
def p_align(self, p):
'''align : ALIGN NUMBER'''
self.addAlign(p[2])
def p_subleq(self, p):
'''subleq : SUBLEQ arg COMMA arg
| SUBLEQ arg COMMA arg COMMA IDENTIFIER'''
self.beginCallDbg(p)
self.addSubleqArg(p[2])
self.addSubleqArg(p[4])
if len(p) <= 5:
# jmp to next instruction
self.code += self.packWord(len(self.code) + self.getWordSize())
else:
self.addFixup(p[6][0])
self.code += self.packWord(0)
self.endCallDbg()
def p_jmp(self, p):
'''jmp : JMP IDENTIFIER'''
self.beginCallDbg(p)
self.call("subleq", "Z", "Z", p[2])
self.endCallDbg()
def p_sub(self, p):
'''sub : SUB arg COMMA arg'''
self.beginCallDbg(p)
self.call("subleq", p[2], p[4])
self.endCallDbg()
def p_add(self, p):
'''add : ADD arg COMMA arg'''
self.beginCallDbg(p)
self.call("subleq", p[2], "Z")
self.call("subleq", "Z", p[4])
self.call("subleq", "Z", "Z")
self.endCallDbg()
def p_mov(self, p):
'''mov : MOV arg COMMA arg'''
self.beginCallDbg(p)
self.call("sub", p[4], p[4])
self.call("add", p[2], p[4])
self.endCallDbg()
def p_push(self, p):
'''push : PUSH arg'''
self.beginCallDbg(p)
self.call("sub", 4, "sp")
self.call("mov", p[2], ["[", "sp", "]"])
self.endCallDbg()
def p_callc(self, p):
'''callc : CALLC'''
self.beginCallDbg(p)
self.call("add", "bar", "sp")
self.beginCallDbg(["trap", "trap", -2])
self.addSubleqArg(["Z"])
self.addSubleqArg(["sp"])
self.code += self.packWord(-2)
self.endCallDbg()
self.call("sub", "bar", "sp")
self.endCallDbg()
def p_arg(self, p):
'''arg : NUMBER
| IDENTIFIER
| OFFSET NUMBER
| OFFSET IDENTIFIER
| LBRACE IDENTIFIER RBRACE'''
p[0] = p[1:]
def p_halt(self, p):
'''halt : HALT'''
self.beginCallDbg(p)
self.addSubleqArg(["Z"])
self.addSubleqArg(["Z"])
self.code += self.packWord(-1)
self.endCallDbg()
def p_error(self, p):
val = ""
line = -1
if p:
val = p.value
line = p.lexer.lineno
print "Syntax error at '%s' line: %u" % (val, line)
if __name__ == '__main__':
argc = len(sys.argv)
if argc == 2 or argc == 3:
input = sys.argv[1]
output = os.path.splitext(input)[0] + ".bin"
if argc == 3:
output = sys.argv[2]
dbgFile = os.path.splitext(output)[0] + ".dbg"
data = subprocess.Popen(["cpp", input], stdout=subprocess.PIPE).communicate()[0]
subleq = Subleq("<i")
subleq.run(data)
f = file(output, "wb+")
if f:
f.write(subleq.code)
f.close()
f = file(dbgFile, "wb+")
if f:
pickle.dump(subleq.dbg, f)
f.close()
else:
print "Unable to create debug info file: %s" % (dbgFile)
else:
print "Unable to create output file: %s" % (output)
else:
print "Usage: subleq.py <input.slq> [<output.bin>]"