-
Notifications
You must be signed in to change notification settings - Fork 1
/
icg.py
261 lines (203 loc) · 7.9 KB
/
icg.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
from tabulate import tabulate
from constructs.ast import *
from constructs.symbol_table import SymbolInfo, SymbolTable
class Quad:
dest = None
op1 = None
op2 = None
operator = None
def __init__(self, dest, op1, op2, operator):
self.dest = dest
self.op1 = op1
self.op2 = op2
self.operator = operator
def print_info(self):
print("{} = {} {} {}".format(self.dest, self.op1, self.operator, self.op2))
class IntermediateCode:
def __init__(self):
self.code_list: List[Quad] = []
self.temp_var_count = 0
self.label_count = 0
self.loop_stack = []
def get_new_temp_var(self):
self.temp_var_count += 1
return "t" + str(self.temp_var_count)
def get_new_label(self):
self.label_count += 1
return "LABEL_" + str(self.label_count)
def add_to_list(self, code: Quad):
self.code_list.append(code)
def print_three_address_code(self):
for i in self.code_list:
dest = i.dest
op1 = i.op1
operator = i.operator
op2 = i.op2
if isinstance(dest, SymbolInfo):
dest = dest.name
if isinstance(op1, SymbolInfo):
op1 = op1.name
if isinstance(op2, SymbolInfo):
op2 = op2.name
if operator == '=':
print("{} = {}".format(dest, op1))
elif operator == 'Label: ':
print("{}{}".format(operator, dest))
elif operator == 'if':
print("{} {} goto {}".format(operator, op1, dest))
elif operator == 'ifFalse':
print("{} goto {}".format(operator, dest))
elif operator == 'print':
print("call print")
elif operator == 'push':
print("push {}".format(dest))
elif operator == 'use':
print("call use")
elif operator == 'call_return':
print("{} = call {}".format(dest, op1))
elif operator == 'goto':
print("goto {}".format(dest))
elif operator == '++':
op = op1
if(op1 is None):
op = op2
print("{} = {} + 1".format(dest, op))
else:
print("{} = {} {} {}".format(dest, op1, operator, op2))
def __str__(self):
return str(
tabulate(
[
[
quad.dest if not isinstance(quad.dest, SymbolInfo) else quad.dest.name,
quad.operator if not isinstance(quad.operator, SymbolInfo) else quad.operator.name,
quad.op1 if not isinstance(quad.op1, SymbolInfo) else quad.op1.name,
quad.op2 if not isinstance(quad.op2, SymbolInfo) else quad.op2.name, ]
for quad in self.code_list
],
headers=[
"Destination",
"Operator",
"Operand 1",
"Operand 2",
],
tablefmt="fancy_grid",
)
)
def recursive_icg(node, ic: IntermediateCode, symtab: SymbolTable):
if isinstance(node, Until):
l = ic.get_new_label()
ic.add_to_list(Quad(l, None, None, "Label: "))
true_l = ic.get_new_label()
false_l = ic.get_new_label()
condition = node.data
condition_res = recursive_icg(condition, ic, symtab)
ic.add_to_list(Quad(true_l, condition_res.name, None, "if"))
ic.add_to_list(Quad(false_l, None, None, "ifFalse"))
ic.add_to_list(Quad(true_l, None, None, "Label: "))
symtab.enter_scope()
body = node.children
for i in body:
recursive_icg(i, ic, symtab)
symtab.leave_scope()
node.children = []
ic.add_to_list(Quad(l, None, None, "goto"))
ic.add_to_list(Quad(false_l, None, None, "Label: "))
elif isinstance(node, Foreach):
lab = ic.get_new_label()
ic.add_to_list(Quad(lab, None, None, "Label: "))
true_l = ic.get_new_label()
false_l = ic.get_new_label()
iterator = node.data
# i = 0
t_i = ic.get_new_temp_var()
symtab.add_if_not_exists(t_i)
i = symtab.get_symbol(t_i)
i.value = 0
ic.add_to_list(Quad(i.name, 0, None, '='))
# l = range(iterator)
t_l = ic.get_new_temp_var()
symtab.add_if_not_exists(t_l)
l = symtab.get_symbol(t_l)
ic.add_to_list(Quad(iterator, None, None, "push"))
ic.add_to_list(Quad(l.name, "len", None, "call_return"))
# i < l
t_r = ic.get_new_temp_var()
symtab.add_if_not_exists(t_r)
r = symtab.get_symbol(t_l)
ic.add_to_list(Quad(r.name, i.name, l.name, '<'))
# checking condition
# res = recursive_icg(iterator, ic, symtab)
ic.add_to_list(Quad(true_l, r.name, None, "if"))
ic.add_to_list(Quad(false_l, None, None, "ifFalse"))
ic.add_to_list(Quad(true_l, None, None, "Label: "))
# body
symtab.enter_scope()
body = node.children
ic.loop_stack.append((i, iterator))
for ibrr in body:
recursive_icg(ibrr, ic, symtab)
symtab.leave_scope()
# i++
ic.add_to_list(Quad(i.name, i.name, None, '++'))
node.children = []
ic.add_to_list(Quad(lab, None, None, "goto"))
ic.add_to_list(Quad(false_l, None, None, "Label: "))
new_children = []
for child in node.children:
new_children.append(recursive_icg(child, ic, symtab))
new_children.reverse()
return_val = None
if isinstance(node, (BinOP, RelationalExpr, LogicalExprBin)):
if node.operator == '=' and new_children != []:
ic.add_to_list(Quad(new_children[1], new_children[0], None, node.operator))
return_val = new_children[1]
elif node.operator != '=':
temp = ic.get_new_temp_var()
symtab.add_if_not_exists(temp)
temp_symbol = symtab.get_symbol(temp)
temp_symbol.value = node
if (len(new_children) < 2):
new_children.append(None)
ic.add_to_list(Quad(temp_symbol, new_children[0], new_children[1], node.operator))
return_val = temp_symbol
elif isinstance(node, Use):
ic.add_to_list(Quad(node.data, None, None, "push"))
ic.add_to_list(Quad(None, None, None, "use"))
elif isinstance(node, Literal):
return_val = node
if (node.type == "variable" and node.value == "$_"):
if (ic.loop_stack == []):
print("ERROR: CANNOT USE $_ as variable")
else:
t = ic.get_new_temp_var()
symtab.add_if_not_exists(t)
temp = symtab.get_symbol(t)
temp.value = ic.loop_stack[-1][1] + '[' + ic.loop_stack[-1][0].name + ']'
ic.add_to_list(Quad(temp.name, temp.value, None, '='))
return_val = temp
elif isinstance(node, List):
return_val = new_children
elif isinstance(node, Array):
data = node.data
name = node.name
if isinstance(data, List):
data = data.children
if node.name is None:
name = name + "[" + str(int(node.index)) + "]"
ic.add_to_list(Quad(name, data, None, '='))
elif isinstance(node, Print):
if (node.children is not None):
t = new_children[0]
for i in t:
ic.add_to_list(Quad(i, None, None, "push"))
ic.add_to_list(Quad(None, None, None, "print"))
else:
ic.add_to_list(Quad(node.data, None, None, "print"))
else:
return_val = node
return return_val
def intermediate_codegen(ast, symtab):
ic = IntermediateCode()
recursive_icg(ast, ic, symtab)
return ic