-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquad.py
143 lines (108 loc) · 4.33 KB
/
quad.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
from ir import *
from codegen import Value
class Quad:
class Error(Exception):
pass
@classmethod
def map_instruction(cls, instr):
assert len(instr) >= 1
opcode, *rest = instr
if len(rest) >= 3 and isinstance(rest[2], Value):
arg2 = rest[2]
a2 = arg2.name
if len(rest) >= 2 and isinstance(rest[1], Value):
arg1 = rest[1]
a1 = arg1.name
if len(rest) >= 1 and isinstance(rest[0], Value):
result = rest[0]
dst = result.name
instr_type_class = (arg1 if issubclass(opcode, Compare) else result).type_class
if instr_type_class is Integer:
prefix = 'I'
elif instr_type_class is Float:
prefix = 'R'
if opcode is Input:
instrs = [f'{prefix}INP {dst}']
elif opcode is Output:
instrs = [f'{prefix}PRT {dst}']
elif opcode is StaticCast:
if result.type_class is Integer:
instrs = [f'RTOI {dst} {a1}']
else:
instrs = [f'ITOR {dst} {a1}']
elif opcode is UnaryAdd:
instrs = [f'{prefix}ADD {dst} 0 {a1}']
elif opcode is Negate:
instrs = [f'{prefix}SUB {dst} 0 {a1}']
elif opcode is Not:
instrs = [f'{prefix}EQL {dst} {a1} 0']
elif opcode is Assign:
instrs = [f'{prefix}ASN {dst} {a1}']
elif opcode is Or:
instrs = []
if isinstance(arg1, int) or isinstance(arg1, float):
normalized_arg1 = int(arg1 == 0)
else:
normalized_arg1 = f'_{a1}'
instrs.append(f'{prefix}EQL {normalized_arg1} {a1} 0')
if isinstance(arg2, int) or isinstance(arg2, float):
normalized_arg2 = int(arg2 == 0)
else:
normalized_arg2 = f'_{a2}'
instrs.append(f'{prefix}EQL {normalized_arg2} {a2} 0')
instrs.append(f'{prefix}ADD {dst} {normalized_arg1} {normalized_arg2}')
return instrs
elif opcode is And:
instrs = []
if isinstance(arg1, int) or isinstance(arg1, float):
normalized_arg1 = int(arg1 == 0)
else:
normalized_arg1 = f'_{a1}'
instrs.append(f'{prefix}EQL {normalized_arg1} {a1} 0')
if isinstance(arg2, int) or isinstance(arg2, float):
normalized_arg2 = int(arg2 == 0)
else:
normalized_arg2 = f'_{a2}'
instrs.append(f'{prefix}EQL {normalized_arg2} {a2} 0')
instrs.append(f'{prefix}MLT {dst} {normalized_arg1} {normalized_arg2}')
return instrs
elif opcode is Equal:
instrs = [f'{prefix}EQL {dst} {a1} {a2}']
elif opcode is NotEqual:
instrs = [f'{prefix}NQL {dst} {a1} {a2}']
elif opcode is Less:
instrs = [f'{prefix}LSS {dst} {a1} {a2}']
elif opcode is Greater:
instrs = [f'{prefix}GRT {dst} {a1} {a2}']
elif opcode is LessOrEqual:
temp_dst = f'_{dst}'
return [
f'{prefix}EQL {dst} {a1} {a2}',
f'{prefix}LSS {temp_dst} {a1} {a2}',
f'{prefix}ADD {dst} {dst} {temp_dst}']
elif opcode is GreaterOrEqual:
temp_dst = f'_{dst}'
return [
f'{prefix}EQL {dst} {a1} {a2}',
f'{prefix}GRT {temp_dst} {a1} {a2}',
f'{prefix}ADD {dst} {dst} {temp_dst}']
elif opcode is Add:
instrs = [f'{prefix}ADD {dst} {a1} {a2}']
elif opcode is Sub:
instrs = [f'{prefix}SUB {dst} {a1} {a2}']
elif opcode is Mul:
instrs = [f'{prefix}MLT {dst} {a1} {a2}']
elif opcode is Div:
instrs = [f'{prefix}DIV {dst} {a1} {a2}']
elif opcode is Jump:
_, label = rest
instrs = [f'JUMP <{label}>']
elif opcode is CondBr:
_, test_result, true_label, false_label = rest
instrs = [f'JMPZ <{false_label}> {test_result.name}',
f'JUMP <{true_label}>']
elif opcode is Halt:
instrs = ['HALT']
else:
raise cls.Error(f'{cls.__name__} back-end does not support {opcode}')
return instrs