-
Notifications
You must be signed in to change notification settings - Fork 0
/
x86ir.py
158 lines (116 loc) · 4.01 KB
/
x86ir.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
import textwrap
from collections import namedtuple
import extendedast as ext
class X86Instruction:
def __init__(
self, *args, op=None, reg_modifier=None,
read_args=None, written_args=None
):
self.op = op
self.reg_mod = reg_modifier
self.args = list(args)
self.rargs = set(read_args or [])
self.wargs = set(written_args or [])
def read_args(self):
return self.get_vars(self.rargs)
def written_args(self):
return self.get_vars(self.wargs)
def get_vars(self, indices):
return set(
self.args[i] for i in indices if isinstance(self.args[i], ext.Name)
)
def _reg_modded_args(self):
return self.args if not self.reg_mod else [
ext.Reg(self.reg_mod(a.id)) if isinstance(a, ext.Reg) else a
for a in self.args
]
def __str__(self):
args = self._reg_modded_args()
return " {} {}".format(self.op, ', '.join(str(a) for a in args))
def __repr__(self):
args = self._reg_modded_args()
return "{} {}".format(self.op, ' '.join(repr(a) for a in args))
@classmethod
def copy(cls, obj):
return obj.__class__(*obj.args)
def X86Op(op, reg_modifier=None, read_args=None, written_args=None):
class Op(X86Instruction):
def __init__(self, *args):
super().__init__(
*args, op=op, reg_modifier=reg_modifier,
read_args=read_args, written_args=written_args
)
return Op
If = namedtuple('If', ['test', 'body', 'orelse'])
While = namedtuple('While', ['tasm', 'test', 'body'])
class Directive(namedtuple('Directive', ['name', 'args'])):
def __str__(self):
return ".{} {}".format(self.name, ', '.join(map(str, self.args)))
class Label(namedtuple('Label', ['name'])):
def __str__(self):
return "{}:".format(self.name)
Mov = X86Op('movl', read_args=[0], written_args=[1])
Add = X86Op('addl', read_args=[0, 1], written_args=[1])
Sub = X86Op('subl', read_args=[0, 1], written_args=[1])
Neg = X86Op('negl', read_args=[0], written_args=[0])
Sal = X86Op('sall', read_args=[0, 1], written_args=[1])
Sar = X86Op('sarl', read_args=[0, 1], written_args=[1])
And = X86Op('andl', read_args=[0, 1], written_args=[1])
Or = X86Op('orl', read_args=[0, 1], written_args=[1])
Xor = X86Op('xorl', read_args=[0, 1], written_args=[1])
Cmp = X86Op('cmpl', read_args=[0, 1])
Jmp = X86Op('jmp', read_args=[0])
Je = X86Op('je', read_args=[0])
def CmpResult(cmp):
CmpResult.comparators.append(X86Op(
'set' + cmp,
reg_modifier=(lambda r: r[1] + 'l'),
written_args=[0]
))
return CmpResult.comparators[-1]
CmpResult.comparators = []
Sete = CmpResult('e')
Setne = CmpResult('ne')
Setl = CmpResult('l')
Setnl = CmpResult('nl')
Setg = CmpResult('g')
Setng = CmpResult('gl')
Call = X86Op('call', read_args=[0])
class CallPtr(Call):
def __str__(self):
return " {} *{}".format(self.op, self._reg_modded_args()[0])
def __repr__(self):
return "{} *{}".format(self.op, self._reg_modded_args()[0])
Push = X86Op('pushl', read_args=[0])
Pop = X86Op('popl', written_args=[0])
Leave = X86Op('leave')
Ret = X86Op('ret')
def dump(statements):
text = ""
for s in statements:
if isinstance(s, If):
text += textwrap.dedent("""
if {}:
{}
else:
{}
""").strip().format(
s.test,
textwrap.indent(dump(s.body), ' ').rstrip(),
textwrap.indent(dump(s.orelse), ' ')
)
elif isinstance(s, While):
text += textwrap.dedent("""
while:
{}
test {}
do:
{}
""").strip().format(
textwrap.indent(dump(s.tasm), ' ').rstrip(),
s.test,
textwrap.indent(dump(s.body), ' ')
)
else:
text += "{}\n".format(repr(s))
return text