/
main.py
450 lines (333 loc) · 12.6 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# python expression compiler
# by Samuel Michael Squire
# https://github.com/samsquire
# https://samsquire.com/
#
# Zero Clause BSD licenced
# Copyright (c) 2023 Samuel Michael Squire sam@samsquire.com
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import sys
from pprint import pprint
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
class Ast:
def __init__(self):
self.constant = None
self.reference = None
self.symbol = "?"
def load_variable(self):
if type(self.left) == LiteralNumber:
self.source_left = "${}".format(self.left.value)
self.source_right = "%{}".format(self.use_register)
elif type(self.left) == Reference:
lhs_position = self.left.source
print("movl -{}(%rbp), %edi".format(lhs_position))
self.source_left = "%edi"
else:
self.source_left = "%{}".format(self.left.use_register)
if type(self.right) == LiteralNumber:
print("movl ${}, %{}".format(self.right.value, self.use_register))
self.source_right = "%{}".format(self.use_register)
elif type(self.right) == Reference:
rhs_position = "{}".format(self.right.source)
print("movl -{}(%rbp), %{}".format(rhs_position, self.use_register))
self.source_right = "%{}".format(self.use_register)
else:
# print("movl %{}, %{} ".format(self.right.use_register, self.use_register))
self.source_right = "%{}".format(self.right.use_register)
def resolvereferences(normalised):
variables = {}
for operation in normalised:
if type(operation) == Assign:
variables[operation.name] = operation
if type(operation) == Reference:
operation.use_register = variables[operation.name].use_register
operation.source = variables[operation.name].source
def assignrealregisters(assignments, ranges, realregisters):
scopes = []
previousassignments = list(realregisters)
template_variables = {}
for item in ranges:
template_variables[item[0]] = None
previous_template_variables = dict(template_variables)
for instruction in range(0, len(assignments)):
for item in ranges:
if instruction >= item[1] and instruction <= item[2]:
eprint("instruction {} {} falls in range of {}".format(
instruction, assignments[instruction][1], item[0]))
if previous_template_variables[item[0]] == None and (
item[3].reference_type == "register" or item[4] == "assignment"):
# this variable is unassigned
chosen_register = previousassignments.pop()
previous_template_variables[item[0]] = chosen_register
item[3].use_register = chosen_register
eprint("{} is not assigned, assigning {}".format(
item[0], chosen_register))
elif previous_template_variables[
item[0]] != None and instruction == item[2]:
# we are at end of range, free up register
free_register = previous_template_variables[item[0]]
previousassignments.append(free_register)
eprint("{} is now free".format(free_register))
# instruction falls in range of variable ranges[0]
def assignregisters(normalised):
assignments = []
counter = 0
for item in normalised:
if type(item) == Assign:
reference_type = "assignment"
item.reference_type = "assignment"
assignments.append(
(item.name, "=", item.value, item.value, "not assignable",
reference_type, item, reference_type, "", item, item, "assignment"))
if type(item) == Reference:
item.reference = item.name
item.reference_type = "variable"
if type(item) == LiteralNumber:
item.reference = item.value
item.reference_type = "constant"
if type(item) == Println:
reference = "t{}".format(counter)
reference_type = "unit"
item.reference_type = reference_type
assignments.append(
(reference, item.symbol, item.template, item.data.reference,
"not assignable", reference_type, item, reference_type,
reference_type, item, item, "unit"))
counter = counter + 1
if type(item) in [Add, Mul]:
reference = "t{}".format(counter)
item.reference = reference
reference_type = "register"
item.reference_type = reference_type
assignments.append(
(reference, item.symbol, item.left.reference, item.right.reference,
"assignable", reference_type, item, item.left.reference_type,
item.right.reference_type, item.left, item.right, "reference"))
counter = counter + 1
for item in assignments:
eprint(("{} <- {} {} {}".format(item[0], item[2], item[1], item[3])))
return assignments
def liveranges(assignments):
# find variables used
variables = {}
for assignment in assignments:
if assignment[2] not in variables:
variables[assignment[2]] = (assignment[2], assignment[9], assignment[11])
if assignment[3] not in variables:
variables[assignment[3]] = (assignment[3], assignment[10],
assignment[11])
if assignment[0] not in variables:
variables[assignment[0]] = (assignment[0], assignment[6], assignment[11])
ranges = []
for variable_data in variables.values():
variable = variable_data[0]
search_target = variable
start_position_a = -1
end_position_a = -1
for position, search1 in enumerate(assignments):
if search_target == search1[2] or search1[0] == search_target:
start_position_a = position
break
if search_target == search1[3] or search_target == search1[0]:
start_position_a = position
break
for position in range(start_position_a, len(assignments)):
search_target = variable
if search_target == assignments[position][2]:
end_position_a = position
if search_target == assignments[position][3]:
end_position_a = position
eprint("Variable {} appears {}-{} {}".format(variable, start_position_a,
end_position_a,
variable_data))
ranges.append((variable, start_position_a, end_position_a,
variable_data[1], variable_data[2]))
return ranges
class Reference(Ast):
def findconstant(self):
return []
def normalise(self, normalised):
normalised.append(self)
def __init__(self, name):
self.name = name
def codegen(self, target, method):
pass
def assignlocalvariables(self, method):
pass
class Assign(Ast):
def findconstant(self):
return []
def normalise(self, normalised):
normalised.append(self)
def __init__(self, name, value):
self.name = name
self.value = value
self.symbol = "="
def assignlocalvariables(self, method):
method.stack -= 4
self.source = method.stack
eprint("Assigning local variable {} to stack position {}".format(
self.name, self.source))
def codegen(self, target, method):
print("movl ${}, -{}(%rbp)".format(self.value.value, self.source))
print("movl -{}(%rbp), %{} ".format(self.source, self.use_register))
class LiteralNumber(Ast):
def __repr__(self):
return "int({})".format(self.value)
def findconstant(self):
self.constant = Constant(self.value, "long")
return [self.constant]
def normalise(self, normalised):
normalised.append(self)
def __init__(self, value):
self.value = value
def codegen(self, target, method):
pass
def assignlocalvariables(self, method):
pass
class Mul(Ast):
def __init__(self, left, right):
self.left = left
self.right = right
self.symbol = "*"
def assignlocalvariables(self, method):
self.left.assignlocalvariables(method)
self.right.assignlocalvariables(method)
def normalise(self, normalised):
self.left.normalise(normalised)
self.right.normalise(normalised)
normalised.append(self)
def findconstant(self):
constants = []
left_constants = []
right_constants = []
left_constants.extend(self.left.findconstant())
right_constants.extend(self.right.findconstant())
constants.extend(left_constants)
constants.extend(right_constants)
return constants
def codegen(self, target, method):
self.left.codegen("edx", method)
self.right.codegen("eax", method)
self.load_variable()
print("imul {}, {}".format(self.source_left, self.source_right))
class Add(Ast):
def __init__(self, left, right):
self.left = left
self.right = right
self.symbol = "+"
def normalise(self, normalised):
self.left.normalise(normalised)
self.right.normalise(normalised)
normalised.append(self)
def findconstant(self):
constants = []
left_constants = []
right_constants = []
left_constants.extend(self.left.findconstant())
right_constants.extend(self.right.findconstant())
constants.extend(left_constants)
constants.extend(right_constants)
return constants
def codegen(self, target, method):
self.left.codegen("edx", method)
self.right.codegen("eax", method)
self.load_variable()
print("addl {}, {}".format(self.source_left, self.source_right))
def assignlocalvariables(self, method):
self.left.assignlocalvariables(method)
self.right.assignlocalvariables(method)
class Constant(Ast):
counter = 0
def __init__(self, value, type):
self.name = "{}{}".format("CONSTANT", Constant.counter)
self.value = value
self.type = type
Constant.counter = Constant.counter + 1
self.symbol = "CONSTANT"
def codegen(self, target, method):
pass
def constantgen(self):
print(".{}:".format(self.name))
if self.type == "string":
print(" .{} \"{}\"".format(self.type, self.value))
else:
print(" .{} {}".format(self.type, self.value))
class Println(Ast):
def normalise(self, normalised):
self.data.normalise(normalised)
normalised.append(self)
def assignlocalvariables(self, method):
self.data.assignlocalvariables(method)
def findconstant(self):
self.constant_template = Constant(self.template, "string")
constants = [self.constant_template]
constants.extend(self.data.findconstant())
return constants
def __init__(self, template, data):
self.data = data
self.template = template
self.symbol = "func println"
def codegen(self, target, method):
self.data.codegen("", method)
if type(self.data) == LiteralNumber:
print("movl ${}, %esi".format(self.data.value))
else:
print("movl %{}, %esi".format(self.data.use_register))
print("leaq .{}(%rip), %rax ".format(self.constant_template.name))
print("movq %rax, %rdi")
print("call printf@PLT")
print("movl $0, %eax")
class Main(Ast):
def __init__(self, program):
self.program = program
self.stack = 16
def findconstant(self):
constants = []
for line in self.program:
constants.extend(line.findconstant())
return constants
def codegen(self, target, method):
print(".globl main")
print(".align 4")
for ast in self.program:
constants = ast.findconstant()
for constant in constants:
constant.constantgen()
normalised = []
for ast in self.program:
ast.normalise(normalised)
ast.assignlocalvariables(self)
assignments = assignregisters(normalised)
ranges = liveranges(assignments)
realregisters = assignrealregisters(assignments, ranges,
["eax", "ebx", "ecx", "edx"])
resolvereferences(normalised)
eprint(normalised)
print("main:")
print("pushq %rbp")
print("movq %rsp, %rbp")
print("subq $32, %rsp")
print("movq %rdi, -24(%rbp)")
print("movl %esi, -28(%rbp)")
for ast in self.program:
ast.codegen(target, self)
print("leave")
print("ret")
program = Main([
Assign("a", LiteralNumber(5)),
Assign("b", LiteralNumber(6)),
Println(
"Value is %d",
Mul(Add(Reference("a"), LiteralNumber(7)),
Add(LiteralNumber(8), Reference("b")))),
Println("Hello world %d", LiteralNumber(10))
])
program.findconstant()
program.codegen("eax", program)
a = 5
b = 6
eprint((a + 7) * (8 + b))
# print(0x3f - 0x1f)