-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexprinterp.py
312 lines (248 loc) · 9.55 KB
/
exprinterp.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# exprinterp.py
'''
Project 4 (Part 2) : Write an Interpreter
==========================================
Once you've got your compiler emitting intermediate code, you should
be able to write a simple interpreter that runs the code. This
can be useful for prototyping the execution environment, testing,
and other tasks involving the generated code.
Your task is simple, extend the Interpreter class below so that it
can run the code you generated in part 1. The comments and docstrings
in the class describe it in further details.
When your done, you should be able to run simple programs by
typing:
bash % python exprinterp.py someprogram.e
'''
import exprblock
class Interpreter(object):
'''
Runs an interpreter on the SSA intermediate code generated for
your compiler. The implementation idea is as follows. Given
a sequence of instruction tuples such as:
code = [
('literal_int', 1, '_int_1'),
('literal_int', 2, '_int_2'),
('add_int', '_int_1', '_int_2, '_int_3')
('print_int', '_int_3')
...
]
The class executes methods self.run_opcode(args). For example:
self.run_literal_int(1, '_int_1')
self.run_literal_int(2, '_int_2')
self.run_add_int('_int_1', '_int_2', '_int_3')
self.run_print_int('_int_3')
To store the values of variables created in the intermediate
language, simply use a dictionary.
For external function declarations, allow specific Python modules
(e.g., math, os, etc.) to be registered with the interpreter.
We don't have namespaces in the source language so this is going
to be a bit of sick hack.
'''
def __init__(self,name="module"):
# Dictionary of currently defined variables
self.vars = {}
# List of Python modules to search for external decls
external_libs = [ 'math', 'os' ]
self.external_libs = [ __import__(name) for name in external_libs ]
def run(self, ircode):
'''
Run intermediate code in the interpreter. ircode is a list
of instruction tuples. Each instruction (opcode, *args) is
dispatched to a method self.run_opcode(*args)
'''
self.pc = 0
while True:
try:
op = ircode[self.pc]
except IndexError:
if self.pc > len(ircode):
print "Wrong PC %d - terminating" % self.pc
return
self.pc += 1
opcode = op[0]
if hasattr(self, "run_"+opcode):
getattr(self, "run_"+opcode)(*op[1:])
else:
print "Warning: No run_"+opcode+"() method"
# YOU MUST IMPLEMENT: Methods for different opcodes. A few sample
# opcodes are shown below to get you started.
def run_jump(self, label):
self.pc = label
def run_cbranch(self, cond, if_label, else_label):
if self.vars[cond]:
self.pc = if_label
else:
self.pc = else_label
def run_literal_int(self, value, target):
'''
Create a literal integer value
'''
self.vars[target] = value
def run_add_int(self, left, right, target):
'''
Add two integer varibles
'''
self.vars[target] = self.vars[left] + self.vars[right]
def run_print_int(self, source):
'''
Output an integer value.
'''
print self.vars[source]
run_literal_float = run_literal_int
run_literal_string = run_literal_int
def run_alloc_int(self, name):
self.vars[name] = 0
def run_alloc_float(self, name):
self.vars[name] = 0.0
def run_alloc_string(self, name):
self.vars[name] = ''
def run_store_int(self, source, target):
self.vars[target] = self.vars[source]
run_store_float = run_store_int
run_store_string = run_store_int
def run_load_int(self, name, target):
self.vars[target] = self.vars[name]
run_load_float = run_load_int
run_load_string = run_load_int
run_add_float = run_add_int
run_add_string = run_add_int
def run_sub_int(self, left, right, target):
self.vars[target] = self.vars[left] - self.vars[right]
run_sub_float = run_sub_int
def run_mul_int(self, left, right, target):
self.vars[target] = self.vars[left] * self.vars[right]
run_mul_float = run_mul_int
def run_div_int(self, left, right, target):
self.vars[target] = self.vars[left] // self.vars[right]
def run_div_float(self, left, right, target):
self.vars[target] = self.vars[left] / self.vars[right]
def run_uadd_int(self, source, target):
self.vars[target] = self.vars[source]
run_uadd_float = run_uadd_int
def run_usub_int(self, source, target):
self.vars[target] = -self.vars[source]
run_usub_float = run_usub_int
def run_cmp_int(self, op, left, right, target):
compare = cmp(self.vars[left], self.vars[right])
if op == 'lt':
result = bool(compare < 0)
elif op == 'le':
result = bool(compare <= 0)
elif op == 'eq':
result = bool(compare == 0)
elif op == 'ne':
result = bool(compare != 0)
elif op == 'ge':
result = bool(compare >= 0)
elif op == 'gt':
result = bool(compare > 0)
elif op == 'land':
result = self.vars[left] and self.vars[right]
elif op == 'lor':
result = self.vars[left] or self.vars[right]
self.vars[target] = result
run_cmp_float = run_cmp_int
run_cmp_bool = run_cmp_int
run_print_float = run_print_int
run_print_string = run_print_int
def run_extern_func(self, name, rettypename, *parmtypenames):
'''
Scan the list of external modules for a matching function name.
Place a reference to the external function in the dict of vars.
'''
for module in self.external_libs:
func = getattr(module, name, None)
if func:
self.vars[name] = func
break
else:
raise RuntimeError("No extern function %s found" % name)
def run_call_func(self, funcname, *args):
'''
Call a previously declared external function.
'''
target = args[-1]
func = self.vars.get(funcname)
argvals = [self.vars[name] for name in args[:-1]]
self.vars[target] = func(*argvals)
class BlockLinker(exprblock.BlockVisitor):
def __init__(self):
self.code = []
self.code_map = {}
self.jump_list = []
def rec_jump(self):
self.jump_list.append(len(self.code))
def rec_block(self, block):
self.code_map[block] = len(self.code)
def patch_jumps(self):
for n in self.jump_list:
self.code[n] = tuple(self.code_map.get(e,e) for e in self.code[n])
def visit_BasicBlock(self, block):
self.rec_block(block)
self.code.extend(block.instructions)
if block.next_block:
self.rec_jump()
self.code.append(('jump', block.next_block))
def visit_IfBlock(self,block):
self.rec_block(block)
self.code.extend(block.instructions)
# then branch
self.rec_jump()
self.code.append(('cbranch', block.test,
block.if_branch,
block.else_branch if block.else_branch else block.next_block))
self.visit(block.if_branch)
self.rec_jump()
self.code.append(('jump', block.next_block))
# else branch
if block.else_branch:
self.visit(block.else_branch)
self.rec_jump()
self.code.append(('jump', block.next_block))
def visit_WhileBlock(self, block):
self.rec_block(block)
self.code.extend(block.instructions)
self.visit(block.test)
self.rec_jump()
self.code.append(('cbranch', block.test,
block.body, block.next_block))
self.visit(block.body)
self.rec_jump()
self.code.append(('jump', block))
def get_options():
parser = optparse.OptionParser()
parser.add_option("-c", "--show-instructions",
action="store_true", dest="show_code", default=False,
help="show linearized 3A instructions")
options, args = parser.parse_args(sys.argv[1:])
return options, args
# ----------------------------------------------------------------------
# DO NOT MODIFY ANYTHING BELOW
# ----------------------------------------------------------------------
if __name__ == '__main__':
import exprlex
import exprparse
import exprcheck
import exprcode
import sys
import optparse
from errors import subscribe_errors, errors_reported
options, args = get_options()
lexer = exprlex.make_lexer()
parser = exprparse.make_parser()
with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
program = parser.parse(open(sys.argv[1]).read())
# Check the program
exprcheck.check_program(program)
# If no errors occurred, generate code
if not errors_reported():
code = exprcode.generate_code(program)
linker = BlockLinker()
linker.visit(code.start_block)
linker.patch_jumps()
if options.show_code:
for n, inst in enumerate(linker.code):
print n,":", inst
print "GIVES"
interpreter = Interpreter()
interpreter.run(linker.code)