-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
103 lines (85 loc) · 3.41 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
from stack_instruction import *
from rlsra import *
from lsra import *
from ir import *
from interpreter import Interpreter
n = 10
ins: list[StackInstruction] = [
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.StLocal, [0]),
StackInstruction(StackInstructionKind.Push, [1]),
StackInstruction(StackInstructionKind.StLocal, [1]),
StackInstruction(StackInstructionKind.Push, [n]),
StackInstruction(StackInstructionKind.StLocal, [3]),
# 6:
StackInstruction(StackInstructionKind.LdLocal, [3]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Eq, []),
StackInstruction(StackInstructionKind.Branch, [23, 10]),
# 10:
StackInstruction(StackInstructionKind.LdLocal, [0]),
StackInstruction(StackInstructionKind.LdLocal, [1]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.StLocal, [2]),
StackInstruction(StackInstructionKind.LdLocal, [1]),
StackInstruction(StackInstructionKind.StLocal, [0]),
StackInstruction(StackInstructionKind.LdLocal, [2]),
StackInstruction(StackInstructionKind.StLocal, [1]),
StackInstruction(StackInstructionKind.LdLocal, [3]),
StackInstruction(StackInstructionKind.Push, [1]),
StackInstruction(StackInstructionKind.Sub, []),
StackInstruction(StackInstructionKind.StLocal, [3]),
StackInstruction(StackInstructionKind.Jmp, [6]),
# 23:
StackInstruction(StackInstructionKind.LdLocal, [0]),
StackInstruction(StackInstructionKind.Ret, []),
]
"""
ins: list[StackInstruction] = [
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.StLocal, [0]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.StLocal, [1]),
StackInstruction(StackInstructionKind.LdLocal, [0]),
StackInstruction(StackInstructionKind.LdLocal, [0]),
StackInstruction(StackInstructionKind.LdLocal, [1]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.LdLocal, [1]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Ret, [])
]
"""
"""
ins: list[StackInstruction] = [
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Push, [0]),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Add, []),
StackInstruction(StackInstructionKind.Ret, [])
]
"""
fn = StackFunction(local_vars=5, instructions=ins)
ir = import_to_ir(fn)
num_regs = 2
try:
Lsra(num_regs=num_regs).do_linear_scan(ir)
except Exception as e:
ir.dump()
raise e
print("IR dump =======")
ir.dump()
i = Interpreter(num_regs=num_regs, ir=ir)
print("Result:", i.run())
print("Spill count:", i.spill_count)
print("Restore count:", i.restore_count)
print("Move count:", i.move_count)