Repository navigation
Expand file tree
/
Copy pathx86_generator.py
More file actions
211 lines (187 loc) · 6.43 KB
/
Copy pathx86_generator.py
File metadata and controls
211 lines (187 loc) · 6.43 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
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
# x86_generator.py
def generate_x86(tac, variables=None):
registers = ['eax', 'ebx', 'ecx', 'edx']
reg_map = {}
mem_map = {}
# FIX: Start with the known variables from semantic analysis (if provided)
data_section = set(variables) if variables else set()
asm = []
def get_operand(op):
if isinstance(op, int):
return f"{op}"
if op in reg_map:
return reg_map[op]
elif op in mem_map:
return f"[{mem_map[op]}]"
else:
return f"[{op}]"
def allocate(op):
if op is None or isinstance(op, int):
return
if op.startswith('t'):
for r in registers:
if r not in reg_map.values():
reg_map[op] = r
return
if not (isinstance(op, str) and op.startswith('L')):
mem_map[op] = op
data_section.add(op) # Ensure it's in the data section
for quad in tac:
op, a1, a2, res = quad
if op not in ('label', 'goto'):
allocate(a1)
allocate(a2)
if op not in ('label', 'goto', 'ifnz'):
allocate(res)
asm.append("section .data")
asm.append(" newline db 10")
asm.append(" buf times 12 db 0")
asm.append(" true_str db 'True', 10, 0")
asm.append(" false_str db 'False', 10, 0")
# Declare all variables, sorted for consistency
for var in sorted(data_section):
# Make sure we don't declare temporary registers or labels as memory
if not var.startswith('t') and not var.startswith('L'):
asm.append(f" {var} dd 0")
asm.append("\nsection .text")
asm.append(" global _start")
asm.append("_start:")
for quad in tac:
op, a1, a2, res = quad
if op == '=':
src = '1' if a1 == 'True' else '0' if a1 == 'False' else get_operand(a1)
dst = get_operand(res)
asm.append(f" mov eax, {src}")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op in ('+', '-', '*', '//'):
left = get_operand(a1)
right = get_operand(a2)
dst = get_operand(res)
asm.append(f" mov eax, {left}")
if op == '+':
asm.append(f" add eax, {right}")
elif op == '-':
asm.append(f" sub eax, {right}")
elif op == '*':
asm.append(f" imul eax, {right}")
elif op == '//':
asm.append(" cdq")
asm.append(f" mov ebx, {right}")
asm.append(" idiv ebx")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op in ('==', '!=', '>', '<', '>=', '<='):
left = get_operand(a1)
right = get_operand(a2)
dst = get_operand(res)
asm.append(f" mov eax, {left}")
asm.append(f" cmp eax, {right}")
set_instr = {
'==': 'sete', '!=': 'setne', '>': 'setg', '<': 'setl', '>=': 'setge', '<=': 'setle'
}[op]
asm.append(f" {set_instr} al")
asm.append(f" movzx eax, al")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op == 'and':
left = get_operand(a1)
right = get_operand(a2)
dst = get_operand(res)
asm.append(f" mov eax, {left}")
asm.append(f" and eax, {right}")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op == 'or':
left = get_operand(a1)
right = get_operand(a2)
dst = get_operand(res)
asm.append(f" mov eax, {left}")
asm.append(f" or eax, {right}")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op == 'not':
src = get_operand(a1)
dst = get_operand(res)
asm.append(f" mov eax, {src}")
# Bitwise NOT on 0 gives -1 (true), on 1 gives -2 (still truthy).
# Since Python's `not` yields True/False (1/0), normalize:
asm.append(" cmp eax, 0")
asm.append(" sete al")
asm.append(" movzx eax, al")
if dst != 'eax':
asm.append(f" mov {dst}, eax")
elif op == 'ifnz':
cond = get_operand(a1)
label = res
asm.append(f" cmp {cond}, 0")
asm.append(f" jne {label}")
elif op == 'goto':
asm.append(f" jmp {res}")
elif op == 'label':
asm.append(f"{res}:")
elif op == 'print':
if a1 == 'True':
asm += [
" mov eax, 4",
" mov ebx, 1",
" mov ecx, true_str",
" mov edx, 5",
" int 0x80"
]
elif a1 == 'False':
asm += [
" mov eax, 4",
" mov ebx, 1",
" mov ecx, false_str",
" mov edx, 6",
" int 0x80"
]
else:
val = get_operand(a1)
asm.append(f" mov eax, {val}")
asm.append(f" call print_int")
asm += [
" mov eax, 1",
" xor ebx, ebx",
" int 0x80"
]
asm += [
"\nprint_int:",
" mov ecx, buf + 11",
" mov byte [ecx], 0",
" dec ecx",
" cmp eax, 0",
" jge .convert",
" neg eax",
" mov bl, '-'",
" mov [ecx], bl",
" dec ecx",
".convert:",
" xor edx, edx",
" mov ebx, 10",
".next_digit:",
" xor edx, edx",
" div ebx",
" add dl, '0'",
" mov [ecx], dl",
" dec ecx",
" test eax, eax",
" jnz .next_digit",
" inc ecx",
" mov eax, 4",
" mov ebx, 1",
" mov edx, buf + 12",
" sub edx, ecx",
" int 0x80",
" mov eax, 4",
" mov ebx, 1",
" mov ecx, newline",
" mov edx, 1",
" int 0x80",
" ret"
]
return '\n'.join(asm)
def save_asm(filename, asm_code):
with open(filename, 'w') as f:
f.write(asm_code)