forked from IUCompilerCourse/python-student-support-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx86_ast.py
146 lines (112 loc) · 3.34 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
from __future__ import annotations
import ast
from dataclasses import dataclass
from typing import Iterable
from utils import dedent, indent, indent_stmt, label_name
@dataclass
class X86Program:
body: dict[str, list[instr]] | list[instr]
def __str__(self):
result = ''
if isinstance(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
@dataclass
class X86ProgramDefs:
defs: list[ast.FunctionDef]
def __str__(self):
return "\n".join([str(d) for d in self.defs])
class instr: ...
class arg: ...
class location(arg): ...
@dataclass(frozen=True, eq=False)
class Instr(instr):
instr: str
args: tuple[arg, ...]
def __init__(self, name: str, args: Iterable[arg]):
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
object.__setattr__(self, 'instr', name)
object.__setattr__(self, 'args', tuple(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'
@dataclass(frozen=True, eq=False)
class Callq(instr):
func: str
num_args: int
def __str__(self):
return indent_stmt() + 'callq' + ' ' + self.func + '\n'
@dataclass(frozen=True, eq=False)
class IndirectCallq(instr):
func: arg
num_args: int
def __str__(self):
return indent_stmt() + 'callq' + ' *' + str(self.func) + '\n'
@dataclass(frozen=True, eq=False)
class JumpIf(instr):
cc: str
label: str
def __str__(self):
return indent_stmt() + 'j' + self.cc + ' ' + self.label + '\n'
@dataclass(frozen=True, eq=False)
class Jump(instr):
label: str
def __str__(self):
return indent_stmt() + 'jmp ' + self.label + '\n'
@dataclass(frozen=True, eq=False)
class IndirectJump(instr):
target: location
def __str__(self):
return indent_stmt() + 'jmp *' + str(self.target) + '\n'
@dataclass(frozen=True, eq=False)
class TailJump(instr):
func: arg
arity: int
def __str__(self):
return indent_stmt() + 'tailjmp ' + str(self.func) + '\n'
@dataclass(frozen=True)
class Variable(location):
id: str
def __str__(self):
return self.id
@dataclass(frozen=True)
class Immediate(arg):
value: int
def __str__(self):
return '$' + str(self.value)
@dataclass(frozen=True)
class Reg(location):
id: str
def __str__(self):
return '%' + self.id
@dataclass(frozen=True)
class ByteReg(Reg):
pass
@dataclass(frozen=True)
class Deref(arg):
reg: str
offset: int
def __str__(self):
return str(self.offset) + '(%' + self.reg + ')'
@dataclass(frozen=True)
class Global(arg):
name: str
def __str__(self):
return self.name + "(%rip)"