-
Notifications
You must be signed in to change notification settings - Fork 0
/
remcf.py
65 lines (44 loc) · 1.33 KB
/
remcf.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
import x86ir as x86
import constants as C
def remove_ctrl_flow(stmts):
new_stmts = []
for s in stmts:
if isinstance(s, (x86.If, x86.While)):
rem_func = remove_if if isinstance(s, x86.If) else remove_while
new_stmts.extend(rem_func(s))
else:
new_stmts.append(s)
return new_stmts
def remove_if(s):
lbl_else, lbl_end = _free_if_labels()
stmts = [
x86.Cmp(C.ConstInt(0), s.test),
x86.Je(lbl_else)
]
stmts += remove_ctrl_flow(s.body)
stmts += [
x86.Jmp(lbl_end),
x86.Label(lbl_else)
]
stmts += remove_ctrl_flow(s.orelse)
stmts.append(x86.Label(lbl_end))
return stmts
def remove_while(s):
lbl_start, lbl_end = _free_while_labels()
return (
[x86.Label(lbl_start)]
+ remove_ctrl_flow(s.tasm)
+ [x86.Cmp("$0", s.test), x86.Je(lbl_end)]
+ remove_ctrl_flow(s.body)
+ [x86.Jmp(lbl_start), x86.Label(lbl_end)]
)
def _free_if_labels():
_free_if_labels.ctr += 1
suffix = '_label_{}'.format(_free_if_labels.ctr)
return 'else' + suffix, 'fi' + suffix
_free_if_labels.ctr = 0
def _free_while_labels():
_free_while_labels.ctr += 1
suffix = '_label_{}'.format(_free_while_labels.ctr)
return 'while' + suffix, 'end' + suffix
_free_while_labels.ctr = 0