-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_bril.py
More file actions
223 lines (207 loc) · 7.02 KB
/
Copy pathto_bril.py
File metadata and controls
223 lines (207 loc) · 7.02 KB
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
from weakref import ref
class to_bril(object):
def __init__(self, tree, holes):
self.tree = tree
self.holes = holes
self.instrs = []
self.idx = 0
self.args = []
self.bril_prog = dict()
self.convert()
def convert(self):
self.visit(self.tree, 'v0')
self.post_process()
arg_list = list()
for arg in self.args:
arg_list.append({
'name': arg,
'type': 'int'
})
self.bril_prog['functions'] = [
{
'name': 'main',
'args': arg_list,
'instrs': self.instrs
}
]
def unique_name(self):
name = "v" + str(self.idx)
self.idx += 1
return name
def visit(self, tree, root_var):
if tree.data == 'add':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'add',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'sub':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'sub',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'shl':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'shl',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'shr':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'shr',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'mul':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'mul',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'div':
lhs = self.unique_name()
rhs = self.unique_name()
instr = {
'dest': root_var,
'op': 'div',
'type': 'int',
'args': [lhs, rhs]
}
self.instrs.append(instr)
self.visit(tree.children[0], lhs)
self.visit(tree.children[1], rhs)
elif tree.data == 'if':
cond = self.unique_name()
true = self.unique_name()
false = self.unique_name()
instr = {
'dest': root_var,
'op': 'if',
'type': 'int',
'args': [cond, true, false]
}
self.instrs.append(instr)
self.visit(tree.children[0], cond)
self.visit(tree.children[1], true)
self.visit(tree.children[2], false)
elif tree.data == 'num':
instr = {
'dest': root_var,
'op': 'const',
'type': 'int',
'value': int(tree.children[0])
}
self.instrs.append(instr)
elif tree.data == 'var':
var_name = str(tree.children[0])
if var_name.startswith('h') and var_name in self.holes:
instr = {
'dest': root_var,
'op': 'const',
'type': 'int',
'value': str(self.holes.get(var_name, var_name))
}
self.instrs.append(instr)
else:
instr = {
'dest': root_var,
'op': 'var',
'type': 'int',
'value': str(tree.children[0])
}
self.instrs.append(instr)
def post_process(self):
"""
- Reverse the order of instruction
- Remove all constant if operations
- Inline all var operations
"""
self.instrs.reverse()
# Remove all constant if operations
# Also add var to args
new_instrs = []
const_map = dict()
var_map = dict() # var -> var, act as Context
for instr in self.instrs:
if instr['op'] == 'const':
const_map[instr['dest']] = instr['value']
new_instrs.append(instr)
elif instr['op'] == 'if':
cond = instr['args'][0]
true = instr['args'][1]
false = instr['args'][2]
if cond in const_map:
if const_map[cond] == "0":
if false in var_map:
var_map[instr['dest']] = var_map[false]
else:
var_map[instr['dest']] = false
else:
if true in var_map:
var_map[instr['dest']] = var_map[true]
else:
var_map[instr['dest']] = true
elif instr['op'] == 'var':
var_map[instr['dest']] = instr['value']
if instr['value'] not in self.args and not instr['value'].startswith('h'):
self.args.append(instr['value'])
else:
new_instrs.append(instr)
self.instrs = new_instrs
# Fold id operations and inline var operations
for instr in self.instrs:
if 'args' in instr:
for idx, arg in enumerate(instr['args']):
if arg in var_map:
instr['args'][idx] = var_map[arg]
# add print result instr
self.instrs.append({
'op': 'print',
'args': ['v0']
})
# Simple DSE
referenced_vars = set()
for instr in self.instrs:
if 'args' in instr:
for arg in instr['args']:
referenced_vars.add(arg)
new_instrs = []
for instr in self.instrs:
if 'dest' in instr and instr['dest'] in referenced_vars:
new_instrs.append(instr)
elif 'op' in instr and instr['op'] == 'print':
new_instrs.append(instr)
self.instrs = new_instrs
# prog = dict()
# print(prog)