-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx86_ast.py
217 lines (196 loc) · 6.41 KB
/
x86_ast.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
from typing import List
from utils import label_name, indent, dedent, indent_stmt
class X86Program:
__match_args__ = ("body",)
def __init__(self, body):
self.body = body
def __str__(self):
result = ''
if type(self.body) == dict:
for (l,ss) in self.body.items():
if l == label_name('main'):
result += '\t.globl ' + label_name('main') + '\n'
result += '\t.align 16\n'
result += l + ':\n'
indent()
result += ''.join([str(s) for s in ss]) + '\n'
dedent()
else:
result += '\t.globl ' + label_name('main') + '\n' + \
label_name('main') + ':\n'
indent()
result += ''.join([str(s) for s in self.body])
dedent()
result += '\n'
return result
def __repr__(self):
return 'X86Program(' + repr(self.body) + ')'
class X86ProgramDefs:
__match_args__ = ("defs",)
def __init__(self, defs):
self.defs = defs
def __str__(self):
return '\n'.join([str(d) for d in self.defs])
def __repr__(self):
return 'X86ProgramDefs(' + repr(self.defs) + ')'
class instr: ...
class arg: ...
class location(arg): ...
class Instr(instr):
instr: str
args: List[arg]
__match_args__ = ("instr", "args")
def __init__(self, instr, args):
self.instr = instr
self.args = args
def source(self):
return self.args[0]
def target(self):
return self.args[-1]
def __str__(self):
return indent_stmt() + self.instr + ' ' + ', '.join(str(a) for a in self.args) + '\n'
def __repr__(self):
return 'Instr(' + repr(self.instr) + ', ' + repr(self.args) + ')'
class Callq(instr):
__match_args__ = ("func", "num_args")
def __init__(self, func, num_args):
self.func = func
self.num_args = num_args
def __str__(self):
return indent_stmt() + 'callq' + ' ' + str(self.func) + '\n'
def __repr__(self):
return 'Callq(' + repr(self.func) + ', ' + repr(self.num_args) + ')'
class IndirectCallq(instr):
__match_args__ = ("func", "num_args")
def __init__(self, func, num_args):
self.func = func
self.num_args = num_args
def __str__(self):
return indent_stmt() + 'callq' + ' *' + str(self.func) + '\n'
def __repr__(self):
return 'IndirectCallq(' + repr(self.func) + ', ' + repr(self.num_args) + ')'
class JumpIf(instr):
cc: str
label: str
__match_args__ = ("cc", "label")
def __init__(self, cc, label):
self.cc = cc
self.label = label
def __str__(self):
# breakpoint()
return indent_stmt() + 'j' + self.cc + ' ' + self.label + '\n'
def __repr__(self):
return 'JumpIf(' + repr(self.cc) + ', ' + repr(self.label) + ')'
class Jump(instr):
label: str
__match_args__ = ("label",)
def __init__(self, label):
self.label = label
def __str__(self):
return indent_stmt() + 'jmp ' + self.label + '\n'
def __repr__(self):
return 'Jump(' + repr(self.label) + ')'
class IndirectJump(instr):
__match_args__ = ("target",)
def __init__(self, target):
self.target = target
def __str__(self):
return indent_stmt() + 'jmp *' + str(self.target) + '\n'
def __repr__(self):
return 'IndirectJump(' + repr(self.target) + ')'
class TailJump(instr):
__match_args__ = ("func","arity")
def __init__(self, func, arity):
self.func = func
self.arity = arity
# def __str__(self):
# return indent_stmt() + 'tailjmp ' + str(self.func) + '\n'
def __str__(self):
return indent_stmt() + 'jmp *' + str(self.func) + '\n'
def __repr__(self):
return 'TailJump(' + repr(self.func) + ',' + repr(self.arity) + ')'
class Variable(location):
__match_args__ = ("id",)
def __init__(self, id):
self.id = id
def __str__(self):
return self.id
def __repr__(self):
return 'Variable(' + repr(self.id) + ')'
def __eq__(self, other):
if isinstance(other, Variable):
return self.id == other.id
else:
return False
def __hash__(self):
return hash(self.id)
class Immediate(arg):
__match_args__ = ("value",)
def __init__(self, value):
self.value = value
def __str__(self):
return '$' + str(self.value)
def __repr__(self):
return 'Immediate(' + repr(self.value) + ')'
def __eq__(self, other):
if isinstance(other, Immediate):
return self.value == other.value
else:
return False
def __hash__(self):
return hash(self.value)
class Reg(location):
__match_args__ = ("id",)
def __init__(self, id):
self.id = id
def __str__(self):
return '%' + self.id
def __repr__(self):
return 'Reg(' + repr(self.id) + ')'
def __eq__(self, other):
if isinstance(other, Reg):
return self.id == other.id
else:
return False
def __hash__(self):
return hash(self.id)
class ByteReg(arg):
__match_args__ = ("id",)
def __init__(self, id):
self.id = id
def __str__(self):
return '%' + self.id
def __repr__(self):
return 'ByteReg(' + repr(self.id) + ')'
def __eq__(self, other):
if isinstance(other, ByteReg):
return self.id == other.id
else:
return False
def __hash__(self):
return hash(self.id)
class Deref(arg):
__match_args__ = ("reg", "offset")
def __init__(self, reg, offset):
self.reg = reg
self.offset = offset
def __str__(self):
return str(self.offset) + '(%' + self.reg + ')'
def __repr__(self):
return 'Deref(' + repr(self.reg) + ', ' + repr(self.offset) + ')'
def __eq__(self, other):
if isinstance(other, Deref):
return self.reg == other.reg and self.offset == other.offset
else:
return False
def __hash__(self):
return hash((self.reg, self.offset))
from sys import platform
class Global(arg):
__match_args__ = ("name",)
def __init__(self, name):
self.name = name
def __str__(self):
return ('_' if platform == "darwin" else '') + str(self.name) + "(%rip)"
def __repr__(self):
return 'Global(' + repr(self.name) + ')'