This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·87 lines (66 loc) · 1.88 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
#!/usr/bin/env python3
"""The main function of the compiler, AKA the compiler driver"""
import lexer
import parser
from support import *
from datalayout import *
from cfg import *
from regalloc import *
from codegen import *
# reducing headcaches while debugging
import colored_traceback
colored_traceback.add_hook()
def compile_program(text):
lex = lexer.Lexer(text)
pars = parser.Parser(lex)
res = pars.program()
print("\n", res, "\n")
res.navigate(print_stat_list)
# we want a return here because we're not
# ready for the next part
# return 0
node_list = get_node_list(res)
for n in node_list:
print(type(n), id(n), "->", type(n.parent), id(n.parent))
print("\nTotal nodes in IR:", len(node_list), "\n")
res.navigate(lowering)
node_list = get_node_list(res)
print("\n", res, "\n")
for n in node_list:
print(type(n), id(n))
try:
n.flatten()
except Exception:
pass
# res.navigate(flattening)
print("\n", res, "\n")
print_dotty(res, "log.dot")
print("\n\nDATALAYOUT\n\n")
perform_data_layout(res)
print("\n", res, "\n")
cfg = CFG(res)
cfg.liveness()
cfg.print_liveness()
cfg.print_cfg_to_dot("cfg.dot")
print("\n\nREGALLOC\n\n")
ra = LinearScanRegisterAllocator(cfg, 11)
reg_alloc = ra()
print(reg_alloc)
print("\n\nCODEGEN\n\n")
code = generate_code(res, reg_alloc)
print(code)
return code
def driver_main():
from lexer import __test_program
test_program = __test_program
import sys
print(sys.argv)
if len(sys.argv) >= 2:
with open(sys.argv[1], "r") as inf:
test_program = inf.read()
code = compile_program(test_program)
if len(sys.argv) > 2:
with open(sys.argv[-1], "w") as outf:
outf.write(code)
if __name__ == "__main__":
driver_main()