Repository navigation
Expand file tree
/
Copy pathx86_printer.py
More file actions
64 lines (49 loc) · 1.91 KB
/
Copy pathx86_printer.py
File metadata and controls
64 lines (49 loc) · 1.91 KB
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
from utils import dedent, indent, indent_stmt, label_name
# Prints the assembly syntax tree (asm_ast.py) as x86-64 in AT&T syntax.
# Each method prints the node class it is named after; see asm_node.
class X86Printer:
def X86Program(self, p):
result = ''
if isinstance(p.body, dict):
for (l,ss) in p.body.items():
if l == 'main':
result += '.globl ' + label_name('main') + '\n'
result += '.align 16\n'
result += label_name(l) + ':\n'
indent()
result += '\n'.join([str(s) for s in ss]) + '\n'
result += '\n'
dedent()
else:
result += '.globl ' + label_name('main') + '\n' + \
label_name('main') + ':\n'
indent()
result += '\n'.join([str(s) for s in p.body])
result += '\n'
dedent()
result += '\n'
return result
def Instr(self, i):
return indent_stmt() + i.instr + ' ' + ', '.join(str(a) for a in i.args)
def Callq(self, i):
return indent_stmt() + 'callq' + ' ' + label_name(i.func)
def IndirectCallq(self, i):
return indent_stmt() + 'callq' + ' *' + str(i.func)
def JumpIf(self, i):
return indent_stmt() + 'j' + i.cc + ' ' + label_name(i.label)
def Jump(self, i):
return indent_stmt() + 'jmp ' + label_name(i.label)
def IndirectJump(self, i):
return indent_stmt() + 'jmp *' + str(i.target)
def TailJump(self, i):
return indent_stmt() + 'tailjmp ' + str(i.func)
def Variable(self, a):
return a.id
def Immediate(self, a):
return '$' + str(a.value)
def Reg(self, a): # also prints ByteReg
return '%' + a.id
def Deref(self, a):
return str(a.offset) + '(%' + a.reg + ')'
def Global(self, a):
return label_name(a.name) + "(%rip)"