-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.py
466 lines (396 loc) · 14.2 KB
/
machine.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from __future__ import print_function
"""machine.py – a bytecode interpreter to experiment with RPython
This code was part of an attempt to wrap my head around the RPython tool chain. This
code takes as input something resembling assembly language (see mandelbrot.mach as
an example), compiles it to byte code and then executes it. If run on the standard
CPython interpreter, it is quite slow. Performance with PyPy is much better. If this
program is translated with RPython --jit, performance is much, much better.
This has only really been tested by running mandelbrot.mach, so it probably has a bunch of
buggy edge cases that haven't been exercised. To run mandelbrot.mach using python, for
example, use:
$ python machine.py mandelbrot.mach
However, be prepared to wait a long time in this case. The commands used to translate the
code using RPython are given below.
The code below would make more sense broken into several files, but I've kept it as one
for ease of download, etc. The sections are:
* Syntax Tree
* Parser and Compiler
* Operators
* Main Loop
* Launching Code / RPython Stuff
The second section is not very relevant to the RPython aspect of the code, but the first
section will tell you something about how I had to set up the data structures to make this
work with RPython. The critical restriction is that a variable may only hold a single
type at a given location in the code. This leads to variable in the code getting boxed,
so that objects, which can hold more than one type as values internally get passed around
instead of the raw values.
Some useful links for writing an interpreter in PyPy.
Start Here: http://morepypy.blogspot.com/2011/04/tutorial-writing-interpreter-with-pypy.html
More Info on RPython: http://rpython.readthedocs.org/en/improve-docs/rpython.html
Helpful hints on optimization and such: https://bitly.com/bundles/cfbolz/
These are the commands that I used to translate this. You would likely need to adjust
the paths. The jit option can be skipped; translation is faster, but performance suffers.
export PYPY_LOCALBASE=~/Code/PyPy/pypy-2.3.1-src
pypy-2.3.1-osx64/bin/pypy pypy-2.3.1-src/rpython/bin/rpython --opt=jit machine.py
"""
import os
import sys
import math
sys.path.append("~/Code/PyPy/pypy-2.3.1-src")
from rpython.rlib.jit import JitDriver, purefunction, hint
stdin_fd = sys.stdin.fileno()
stdout_fd = sys.stdout.fileno()
stderr_fd = sys.stderr.fileno()
#-------- Syntax Tree --------
class MValue(object):
_immutable_fields_ = ["value"]
def as_text(self):
return "<VALUE>"
class MSymbol(MValue):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = self.name = value
def as_text(self):
return self.value
class MLineNo(MValue):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = int(value)
def as_text(self):
return str(self.value)
class MCode(MValue):
pass
class MLineNo(MCode):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = int(value)
def as_text(self):
return str(self.value)
class MMemLoc(MCode):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = int(value)
def as_text(self):
return str(self.value)
class MCmd(MCode):
_immutable_fields_ = ["value"]
count = 0
def __init__(self, name, size):
self.name = name
self.size = size
self.value = self.count
self.__class__.count += 1
def as_text(self):
return self.name
class MOpLoc(MCode):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = int(value)
def as_text(self):
return str(self.value)
class MLiteral(MValue):
pass
class MString(MLiteral):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = value
def as_text(self):
return self.value
class MInt(MLiteral):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = int(value)
def as_text(self):
return str(self.value)
class MFloat(MLiteral):
_immutable_fields_ = ["value"]
def __init__(self, value):
self.value = float(value)
def as_text(self):
return str(self.value)
cmd_map = {
"set":MCmd("set", 3),
"exec_1" : MCmd("exec_1", 4),
"exec_2": MCmd("exec_2", 5),
"branchif": MCmd("branchif", 3),
"jump": MCmd("jump", 2),
"display": MCmd("display", 2),
"end": MCmd("end", 1)
}
for k, v in cmd_map.items():
globals()["C_" + k.upper()] = v.value
#-------- Parser and Compiler--------
def atomize(token):
"Numbers become numbers; every other token is a symbol."
try:
return MInt(int(token))
except ValueError:
pass
try:
return MFloat(float(token))
except ValueError:
pass
n = len(token) - 2
if n >= 0 and token.startswith('"') and token.endswith('"'):
return MString(token[1:1+n])
return MSymbol(token)
def split(text):
raw_split = [x.strip() for x in text.split(' ')]
return [x for x in raw_split if x]
def preparse(text):
lines = []
for l in text.split('\n'):
l = l.strip()
if not l or l.startswith('#'):
continue
lines.append([atomize(x) for x in split(l)])
return lines
def extract_labels(lines):
labels = {}
program = []
for x in lines:
if x[0].name == 'label':
labels[x[1].name] = len(program)
elif x[0].name == 'exec':
if len(x) == 4:
program.append(MSymbol('exec_1'))
elif len(x) == 5:
program.append(MSymbol('exec_2'))
else:
raise ValueError('wrong number of args to exec')
program.extend(x[1:])
else:
program.extend(x)
return program, labels
def replace_labels(program, labels):
pc = 0
while pc < len(program):
name = program[pc].name
if name == 'branchif':
program[pc+2] = MLineNo(labels[program[pc+2].name])
elif name == "jump":
program[pc+1] = MLineNo(labels[program[pc+1].name])
size = cmd_map[name].size
assert isinstance(size, int)
pc += size
def intern_ops(program):
pc = 0
while pc < len(program):
name = program[pc].name
assert isinstance(name, str)
cmd = cmd_map[name]
program[pc] = cmd
size = cmd.size
assert isinstance(size, int)
pc += cmd.size
def parse(text):
lines = preparse(text)
program, labels = extract_labels(lines)
replace_labels(program, labels)
intern_ops(program)
return program
def attach_to_memory(program):
# Note that this works on program in place, copy first
OP_EXEC_1 = cmd_map['exec_1']
OP_EXEC_2 = cmd_map['exec_2']
pc = 0
memory = []
map = {}
while pc < len(program):
op = program[pc]
n = op.size
look_at = range(pc+1, pc+n)
if op in (OP_EXEC_1, OP_EXEC_2):
i = look_at[1]
del look_at[1]
arg = program[i]
assert isinstance(arg, MSymbol)
if op is OP_EXEC_1:
program[i] = MOpLoc(monop_map[arg.value])
else:
program[i] = MOpLoc(binop_map[arg.value])
for i in look_at:
arg = program[i]
if isinstance(arg, MSymbol):
if arg.name not in map:
map[arg.name] = MMemLoc(len(memory))
memory.append(None)
program[i] = map[arg.name]
elif isinstance(arg, MLiteral):
program[i] = MMemLoc(len(memory))
memory.append(arg)
pass
else:
assert isinstance(arg, MLineNo)
pc += n
return memory
def compile(program):
program = program[:]
mem = attach_to_memory(program)
code = []
for x in program:
assert isinstance(x, MCode)
code.append(x.value)
return code, mem
#-------- Operators --------
def both_ints(a, b):
return isinstance(a, MInt) and isinstance(b, MInt)
def int_value(x):
if isinstance(x, MInt):
return x.value
if isinstance(x, MFloat):
return int(x.value)
raise RuntimeError("illegal value to int_value")
def float_value(x):
if isinstance(x, MInt):
return float(x.value)
if isinstance(x, MFloat):
return x.value
raise RuntimeError("illegal value to int_value")
def o_lt(a, b):
return MInt(int_value(a) < int_value(b)) if both_ints(a,b) else MInt(float_value(a) < float_value(b))
def o_ge(a, b):
return MInt(int_value(a) >= int_value(b)) if both_ints(a,b) else MInt(float_value(a) >= float_value(b))
def o_gt(a, b):
return MInt(int_value(a) > int_value(b)) if both_ints(a,b) else MInt(float_value(a) > float_value(b))
def o_sub(a, b):
return MInt(int_value(a) - int_value(b)) if both_ints(a,b) else MFloat(float_value(a) - float_value(b))
def o_mul(a, b):
return MInt(int_value(a) * int_value(b)) if both_ints(a,b) else MFloat(float_value(a) * float_value(b))
def o_add(a, b):
return MInt(int_value(a) + int_value(b)) if both_ints(a,b) else MFloat(float_value(a) + float_value(b))
def o_div(a, b):
return MInt(int_value(a) / int_value(b)) if both_ints(a,b) else MFloat(float_value(a) / float_value(b))
def o_hypot(a, b):
return MFloat(math.hypot(float_value(a), float_value(b)))
def o_float(a):
return MFloat(float_value(a))
def o_int(a):
return MInt(int_value(a))
_monops = [('float', o_float),
('int', o_int)]
monop_map = dict((x[0], i) for (i, x) in enumerate(_monops))
for k, v in monop_map.items():
globals()["O_" + k.upper()] = v
_binops = [('lt', o_lt),
('ge' , o_ge),
('gt' , o_gt),
('sub' , o_sub),
('mul' , o_mul),
('div' , o_div),
('add' , o_add),
('hypot', o_hypot)]
binop_map = dict((x[0], i) for (i, x) in enumerate(_binops))
for k, v in binop_map.items():
globals()["O_" + k.upper()] = v
# -------- Main Loop --------
@purefunction
def opcode(code, pc):
return code[pc]
def execute(program):
code, mem = compile(program)
pc = 0
try:
while pc < len(program):
jitdriver.jit_merge_point(pc=pc, mem=mem, code=code, program=program)
op = opcode(code, pc)
if op == C_SET:
a = opcode(code, pc+1)
b = opcode(code, pc+2)
mem[a] = mem[b]
pc += 3
elif op == C_EXEC_1:
symbol = opcode(code, pc+1)
op = opcode(code, pc+2)
a = mem[opcode(code, pc+3)]
if op == O_FLOAT:
mem[symbol] = o_float(a)
elif op == O_INT:
mem[symbol] = o_int(a)
else:
raise ValueError("illegal unary op: %s" % op)
pc += 4
elif op == C_EXEC_2:
symbol = opcode(code, pc+1)
op = opcode(code, pc+2)
a = mem[opcode(code, pc+3)]
b = mem[opcode(code, pc+4)]
if op == O_LT:
mem[symbol] = o_lt(a, b)
elif op == O_GE:
mem[symbol] = o_ge(a, b)
elif op == O_GT:
mem[symbol] = o_gt(a, b)
elif op == O_SUB:
mem[symbol] = o_sub(a, b)
elif op == O_MUL:
mem[symbol] = o_mul(a, b)
elif op == O_DIV:
mem[symbol] = o_div(a, b)
elif op == O_ADD:
mem[symbol] = o_add(a, b)
elif op == O_HYPOT:
mem[symbol] = o_hypot(a, b)
else:
raise ValueError("illegal binary op: %s" % op)
pc += 5
elif op == C_BRANCHIF:
x = opcode(code, pc+1)
loc = opcode(code, pc+2)
if int_value(mem[x]):
pc = loc
else:
pc += 3
elif op == C_JUMP:
loc = opcode(code, pc+1)
pc = loc
elif op == C_DISPLAY:
arg = opcode(code, pc+1)
os.write(stdout_fd, '%s\n' % mem[arg].as_text())
pc += 2
elif op == C_END:
pc = len(code)
else:
raise ValueError("unknown command")#: %s(%s) after %s(%s)" % (op.as_text(), pc))
return 0
except:
os.write(stderr_fd, "ERROR at PC: %s\n" % pc)
return 1
#-------- Launching Code / RPython Stuff
# For RPython, we use the default policy.
def jitpolicy(driver):
from rpython.jit.codewriter.policy import JitPolicy
return JitPolicy()
# This is for logging, which can be helpful for optimizing.
def get_location(pc, code, program):
if pc < 0:
return "<illegal PC>"
cmd = program[pc]
return " ".join([x.as_text() for x in program[pc:pc+cmd.size]])
return "PC: %s" % pc
# This tells RPython what stays constant in the main loop (pc, code, program) and what is
# expected to change (mem). These are mapped to the actual variables inside the main loop
# with the jit_merge_point function.
jitdriver = JitDriver(greens=['pc', 'code', 'program'],
reds=['mem'],
get_printable_location=get_location)
def main(argv):
if len(argv) != 2:
os.write(stderr_fd, 'Usage %s FILENAME\n' % argv[0])
return 1
else:
fp = os.open(argv[1], os.O_RDONLY, 0777)
program_text = ""
while True:
read = os.read(fp, 4096)
if len(read) == 0:
break
program_text += read
program = parse(program_text)
return_code = execute(program)
return return_code
# This tells RPython what to use and entry point for compiling.
def target(*args):
return main, None
if __name__ == "__main__":
main(sys.argv)