forked from pfalcon/ScratchABlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
575 lines (501 loc) · 18.3 KB
/
parser.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import sys
import string
from itertools import permutations
from graph import Graph
from core import *
import yaml
class ParseError(Exception):
pass
class UndefinedLabel(ParseError):
pass
class Lexer:
def __init__(self, l):
self.l = l
self.ws()
def peek(self):
if not self.l:
return self.l
return self.l[0]
def eol(self):
return not self.l
def is_punct(self, c):
return c in "&|^-+*/%=<>"
def match(self, tok):
if not self.l.startswith(tok):
return False
is_word = tok[0] in string.ascii_letters
rest = self.l[len(tok):]
if rest:
if is_word and rest[0] not in string.whitespace:
return False
elif self.is_punct(tok[0]) and self.is_punct(rest[0]):
return False
self.l = rest
self.ws()
return True
def match_till(self, tok):
res = ""
while self.l and not self.l.startswith(tok):
res += self.l[0]
self.l = self.l[1:]
return res
def match_bracket(self, open_b, close_b):
self.expect(open_b)
level = 1
res = ""
while self.l and level:
if self.match(open_b):
level += 1
elif self.match(close_b):
level -= 1
else:
res += self.l[0]
self.l = self.l[1:]
return res
def expect(self, tok, msg=None):
if not self.match(tok):
if msg is None:
msg = "Expected: '%s'" % tok
raise ParseError(msg, self.l)
def ws(self):
while self.l and self.l[0] == " ":
self.l = self.l[1:]
def rest(self):
return self.l.rstrip()
def word(self):
w = ""
while self.l and self.l[0] in (string.ascii_letters + string.digits + "_.$"):
w += self.l[0]
self.l = self.l[1:]
return w
def isident(self):
return self.l[0] in string.ascii_letters + "_."
def isdigit(self):
return self.l[0] in string.digits
def ident(self):
assert self.isident(), repr(self.l)
w = self.word()
self.ws()
return w
def num(self):
assert self.isdigit(), repr(self.l)
base = 10
if self.l.startswith("0x"):
base = 16
w = self.word()
self.ws()
return int(w, 0), base
def skip_comment(self):
if self.match("/*"):
self.match_till("*/")
self.expect("*/")
class Parser:
def __init__(self, fname):
self.fname = fname
self.expect_line_addr = None
self.cfg = None
self.labels = {}
self.curline = -1
self.script = []
self.pass_no = None
self.lex = None
def error(self, msg):
print("%s:%d: %s" % (self.fname, self.curline, msg))
sys.exit(1)
def parse_cond(self, lex):
self.lex = lex
lex.expect("(")
e = self.parse_expr()
lex.expect(")")
return COND(e)
# Get line from a file, expanding any "macro-like" features,
# like "if (cond) $r0 = $r1"
def get_expand_line(self, f):
while True:
l = f.readline()
if not l:
return [(None, None)]
self.curline += 1
l = l.rstrip()
if not l:
continue
if l[0] == "#":
if self.pass_no == 1:
l = l[1:]
if l.startswith("xform: "):
self.script.append(l.split(None, 1))
elif l.startswith("xform_bblock: "):
self.script.append(l.split(None, 1))
elif l.startswith("xform_inst: "):
self.script.append(l.split(None, 1))
elif l.startswith("script: "):
self.script.append(l.split(None, 1))
elif l.startswith("struct: "):
import progdb
data = yaml.load(l.split(None, 1)[1])
progdb.set_struct_types(data)
elif l.startswith("struct_addr: "):
import progdb
data = yaml.load(l.split(None, 1)[1])
data = {(start, start + max(progdb.get_struct_types()[name].keys()) + 4): name for start, name in data.items()}
progdb.set_struct_instances(data)
continue
if self.expect_line_addr is None:
self.expect_line_addr = self.detect_addr_field(l)
if self.expect_line_addr:
addr, l = l.split(" ", 1)
else:
addr = str(self.curline)
l = l.lstrip()
if l.startswith("if "):
# May need to expand "if macro"
lex = Lexer(l)
lex.expect("if")
c = self.parse_cond(lex)
rest = lex.rest()
if not lex.match("goto"):
# Uncomment to treat as literal line for roundtrip testing
#return [(addr, "\x01" + l)]
out = [
(addr, "if %s goto %s.1.cond" % (str(c.neg()), addr)),
(addr + ".0", rest),
(addr + ".1", addr + ".1.cond:"),
]
return out
return [(addr, l)]
def parse_labels(self):
self.pass_no = 1
self.curline = 0
with open(self.fname) as f:
l = ""
while l is not None:
for addr, l in self.get_expand_line(f):
if l is None:
break
if l[-1] == ":":
self.labels[l[:-1]] = addr
def parse_reg(self, lex):
lex.expect("$")
return REG(lex.ident())
def parse_arglist(self):
args = []
self.lex.expect("(")
while not self.lex.match(")"):
comm = ""
if self.lex.match("/*"):
comm = "/*" + self.lex.match_till("*/") + "*/"
self.lex.expect("*/")
a = self.parse_expr()
assert a, (repr(a), repr(self.lex.l))
a.comment = comm
args.append(a)
if self.lex.match(","):
pass
return args
def parse_num(self):
if self.lex.isdigit():
return VALUE(*self.lex.num())
def parse_primary(self):
if self.lex.match("*"):
self.lex.expect("(", "Dereference requires explicit pointer cast")
type = self.lex.ident()
self.lex.expect("*")
self.lex.expect(")")
e = self.parse_primary()
return MEM(type, e)
elif self.lex.match("("):
e = self.parse_expr()
self.lex.expect(")")
if is_addr(e):
# If there was identifier, check it for being a type name
if e.addr in ("i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64"):
tp = TYPE(e.addr)
e = self.parse_primary()
return EXPR("CAST", [tp, e])
return e
elif self.lex.peek() == "$":
return self.parse_reg(self.lex)
elif self.lex.isdigit():
return VALUE(*self.lex.num())
elif self.lex.match("-"):
e = self.parse_primary()
if is_value(e):
return VALUE(-e.val, e.base)
return EXPR("NEG", [e])
elif self.lex.match("!"):
e = self.parse_primary()
return EXPR("!", [e])
elif self.lex.isident():
id = self.lex.ident()
if self.lex.peek() == "(":
args = self.parse_arglist()
return EXPR("SFUNC", [SFUNC(id)] + args)
else:
return ADDR(self.labels.get(id, id))
else:
self.error("Cannot parse")
def parse_level(self, operators, next_level):
e = next_level()
match = True
while match:
match = False
for op in operators:
if self.lex.match(op):
e2 = next_level()
e = EXPR(op, [e, e2])
match = True
break
return e
def parse_mul(self):
return self.parse_level(("*", "/", "%"), self.parse_primary)
def parse_add(self):
return self.parse_level(("+", "-"), self.parse_mul)
def parse_shift(self):
return self.parse_level(("<<", ">>"), self.parse_add)
def parse_lt_gt(self):
return self.parse_level(("<", "<=", ">=", ">"), self.parse_shift)
def parse_eq(self):
return self.parse_level(("==", "!="), self.parse_lt_gt)
def parse_band(self):
return self.parse_level(("&",), self.parse_eq)
def parse_bxor(self):
return self.parse_level(("^",), self.parse_band)
def parse_bor(self):
return self.parse_level(("|",), self.parse_bxor)
def parse_land(self):
return self.parse_level(("&&",), self.parse_bor)
def parse_lor(self):
return self.parse_level(("||",), self.parse_land)
def parse_expr(self):
return self.parse_lor()
# If there's numeric value, treat it as address. Otherwise, parse expression.
def parse_local_addr_expr(self):
if self.lex.isdigit():
w = self.lex.word()
return ADDR(self.labels.get(w, w))
return self.parse_expr()
def get_label(self, label):
try:
return self.labels[label]
except KeyError:
raise UndefinedLabel(label)
def label_from_addr(self, addr):
labels = list(filter(lambda x: x[1] == addr, self.labels.items()))
if not labels:
return addr
return labels[0][0]
def parse_inst(self, l):
lex = self.lex = Lexer(l)
if lex.peek() == ".":
# Asm directive, ignore
return None
if lex.match("db") or lex.match("unk"):
arg = self.parse_num()
if arg is None:
arg = STR(lex.l)
return Inst(None, "SFUNC", [EXPR("SFUNC", [SFUNC("litbyte"), arg])])
if lex.match("goto"):
return Inst(None, "goto", [self.parse_local_addr_expr()])
if lex.match("call"):
return Inst(None, "call", [self.parse_expr()])
if lex.match("if"):
args = []
while True:
c = self.parse_cond(lex)
lex.expect("goto")
label = lex.match_till(",").strip()
addr = ADDR(self.get_label(label))
args.extend([c, addr])
if not lex.match(","):
break
return Inst(None, "if", args)
if lex.match("return"):
args = []
while True:
lex.skip_comment()
if lex.eol():
break
a = self.parse_expr()
args.append(a)
if not lex.eol():
lex.expect(",")
return Inst(None, "return", args)
dest = self.parse_expr()
if not dest:
return Inst(None, "LIT", [l])
if is_expr(dest) and isinstance(dest.args[0], SFUNC) and lex.eol():
return Inst(None, "SFUNC", [dest])
def make_assign_inst(dest, op, args):
#return Inst(dest, op, args)
return Inst(dest, "=", [EXPR(op, args)])
lex.ws()
if lex.match("&="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "&", [dest, src])
elif lex.match("|="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "|", [dest, src])
elif lex.match("^="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "^", [dest, src])
elif lex.match("+="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "+", [dest, src])
elif lex.match("-="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "-", [dest, src])
elif lex.match("*="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "*", [dest, src])
elif lex.match("/="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "/", [dest, src])
elif lex.match("%="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, "%", [dest, src])
elif lex.match(">>="):
lex.ws()
src = self.parse_expr()
return make_assign_inst(dest, ">>", [dest, src])
elif lex.match("<<="):
src = self.parse_expr()
return make_assign_inst(dest, "<<", [dest, src])
elif lex.match("="):
lex.ws()
src = self.parse_expr()
assert src, repr(lex.l)
if isinstance(src, SFUNC):
args = self.parse_arglist()
return Inst(dest, "SFUNC", [src] + args)
lex.ws()
if lex.eol():
return Inst(dest, "=", [src])
else:
assert 0, (lex.l, self.curline)
else:
assert False, (repr(lex.l), self.curline)
def detect_addr_field(self, l):
# Autodetect whether there's address as first field
# of line or not.
self.expect_line_addr = False
if l[0].isdigit():
# Can be either label or address
if l[-1] == ":":
if " " in l:
return True
else:
return True
return False
def _parse_bblocks(self, f):
self.cfg = Graph()
# TODO: not stored in props to avoid modifying tests
self.cfg.filename = self.fname
self.cfg.props["name"] = None
self.cfg.props["trailing_jumps"] = True
block = None
last_block = None
l = ""
while l is not None:
for addr, l in self.get_expand_line(f):
if l is None:
break
if self.should_stop(addr, l):
return
#print(addr, l)
l = l.split(";", 1)[0]
l = l.strip()
if not l:
continue
if l[-1] == ":":
# label
if self.cfg.props["name"] is None:
# First label is function name
if block is None and self.cfg.is_empty():
self.cfg.props["name"] = l[:-1]
self.cfg.props["addr"] = addr
if block is not None:
last_block = block
block = BBlock(addr)
block.cfg = self.cfg
block.label = l[:-1]
self.cfg.add_node(addr, val=block)
continue
elif block is None:
block = BBlock(addr)
block.cfg = self.cfg
self.cfg.add_node(addr, val=block)
if last_block is not None:
self.cfg.add_edge(last_block.addr, block.addr)
#last_block.end.append(addr)
last_block = None
if l[0] == "\x01":
inst = Inst(None, "LIT", [l[1:]], addr=addr)
block.add(inst)
continue
inst = self.parse_inst(l)
if not inst:
continue
inst.addr = addr
if inst.op in ("goto", "if", "call"):
if inst.op != "if":
pairs = [None, inst.args[0]]
else:
pairs = inst.args
# Add out edge to each successor block, which can be
# many for generalized "if".
for i in range(0, len(pairs), 2):
cond = pairs[i]
addr = pairs[i + 1]
# Skip adding bblocks for indirect jumps
if isinstance(addr, ADDR):
addr = addr.addr
if addr not in self.cfg:
self.cfg.add_node(addr)
self.cfg.add_edge(block.addr, addr, cond=cond)
if cond or inst.op == "call":
last_block = block
else:
last_block = None
block.add(inst)
block = None
elif inst.op == "return":
block.add(inst)
block = None
last_block = None
else:
block.add(inst)
if last_block:
print("Warning: function %s was not properly terminated" % self.cfg.props["name"])
# block may be None e.g. if last instruction was call,
# and function abruptly ended without return.
if block:
self.cfg.add_edge(last_block.addr, block.addr)
def parse_bblocks(self, f):
self.pass_no = 2
try:
self._parse_bblocks(f)
except ParseError as e:
print("Error: %d: %r" % (self.curline + 1, e))
raise
sys.exit(1)
# External labels may produce empty CFG nodes during parsing.
# Make a pass over graph and remove such.
for node, info in list(self.cfg.nodes_props()):
if not info:
self.cfg.remove_node(node)
def parse(self):
self.parse_labels()
self.curline = 0
with open(self.fname) as f:
self.parse_bblocks(f)
return self.cfg
def should_stop(self, addr, l):
return False