-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_dlang_vm.py
executable file
·498 lines (430 loc) · 14.4 KB
/
meta_dlang_vm.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python2
# Copyright 2022 Federico Stazi. Subject to the MIT license.
"""DLANG byte-code interpreter"""
import os
import sys
import time
# RPython setup
from rpython.rlib.jit import JitDriver
JIT_DRIVER = JitDriver(
greens=['cp', 'verbosity', 'code'],
reds=['start_time', 'vm', 'stdin', 'cp_from_label'])
def jitpolicy(_):
"""The policy for JIT compilation"""
from rpython.jit.codewriter.policy import JitPolicy
return JitPolicy()
class DlangRuntimeError(Exception):
"""Error thrown when operations fail at run-time"""
pass
class Tag(object):
"""The item tags"""
Unit = 1
Bool = 2
Int = 3
HeapIndex = 4
HeapRef = 5
CodeIndex = 6
ReturnAddress = 7
FramePointer = 8
PairHeader = 9
InlHeader = 10
InrHeader = 11
ClosureHeader = 12
Unknown = 0
@staticmethod
def to_str(tag):
"""Convert a tag to its string representation"""
tag_str = "Unknown"
if tag == Tag.Unit:
tag_str = "Unit"
elif tag == Tag.Bool:
tag_str = "Bool"
elif tag == Tag.Int:
tag_str = "Int"
elif tag == Tag.HeapIndex:
tag_str = "HeapIndex"
elif tag == Tag.HeapRef:
tag_str = "HeapRef"
elif tag == Tag.CodeIndex:
tag_str = "CodeIndex"
elif tag == Tag.ReturnAddress:
tag_str = "ReturnAddress"
elif tag == Tag.FramePointer:
tag_str = "FramePointer"
elif tag == Tag.PairHeader:
tag_str = "PairHeader"
elif tag == Tag.InlHeader:
tag_str = "InlHeader"
elif tag == Tag.InrHeader:
tag_str = "InrHeader"
elif tag == Tag.ClosureHeader:
tag_str = "ClosureHeader"
return tag_str
class Item(object):
"""The virtual machine items"""
def __init__(self, tag=Tag.Unknown, val=0, ptr=None, bdy=[]):
self.tag = tag
self.val = val
self.ptr = ptr
self.bdy = bdy
@staticmethod
def to_str(item):
"""Convert an item to its string representation"""
if item.tag in [Tag.HeapIndex, Tag.HeapRef]:
return "(" + Tag.to_str(item.tag) + ", " + Item.to_str(item.ptr) + ")"
elif item.tag in [Tag.PairHeader, Tag.InlHeader,
Tag.InrHeader, Tag.ClosureHeader]:
return ("(" + Tag.to_str(item.tag) + ", [" +
", ".join([Item.to_str(x) for x in item.bdy]) + "])")
elif item.tag in [Tag.Unknown]:
return "(?)"
return "(" + Tag.to_str(item.tag) + ", " + str(item.val) + ")"
@staticmethod
def equal(item_a, item_b):
"""Checks it two items are equal"""
return (item_a.tag == item_b.tag and
item_a.val == item_b.val and
item_a.ptr is item_b.ptr and
(item_a.bdy is item_b.bdy or
(item_a.bdy == [] and item_b.bdy == [])))
class VirtualMachine(object):
"""Class representing the virtual machine state"""
def __init__(self):
self.stack = []
self.cp = 0
self.fp = 0
self.status = 0
def print_result(self):
"""Return the string representation of the virtual machine"""
return self._print_result(self.stack[-1])
def _print_result(self, res):
"""Return the final result of the computation in the virtual machine"""
res_str = "ERROR"
if res.tag == Tag.Unit:
res_str = "()"
if res.tag == Tag.Int:
res_str = str(res.val)
if res.tag == Tag.Bool and res.val == 0:
res_str = "false"
if res.tag == Tag.Bool and res.val == 1:
res_str = "true"
if res.tag == Tag.HeapRef:
res_str = "reference"
if res.tag == Tag.HeapIndex:
if res.ptr.tag == Tag.PairHeader:
str_a = self._print_result(res.ptr.bdy[0])
str_b = self._print_result(res.ptr.bdy[1])
if str_a != "ERROR" and str_b != "ERROR":
res_str = "(" + str_a + ", " + str_b + ")"
return res_str
def to_int(value):
"""Convert a value to its 32 bits representation"""
return (value & 0xffffffff) - ((value & 0x80000000) << 1)
def interpret(code, verbosity):
"""Interpret the DLANG code"""
# Prepare the Virtual Machine
vm = VirtualMachine()
vm.stack.append(Item(Tag.FramePointer, val=0))
vm.stack.append(Item(Tag.ReturnAddress, val=0))
# Tokenize code
code = [line.split(" ") for line in code.split("\n")]
# Locate the labels
cp_from_label = {}
for cp in range(len(code)):
if code[cp][0] == "LABEL" or code[cp][0] == "FUNCTION":
cp_from_label[code[cp][1]] = cp
# Get a pointer to stdin
stdin = os.fdopen(0)
# Get the start time
start_time = int(1000000000 * time.time())
while vm.status == 0:
if verbosity == "debug":
print "cp = " + str(vm.cp)
print "stack = [" + "\n ".join([Item.to_str(x) for x in vm.stack]) + "]"
JIT_DRIVER.jit_merge_point(cp=vm.cp, verbosity=verbosity, code=code, vm=vm,
stdin=stdin, start_time=start_time,
cp_from_label=cp_from_label)
try:
if code[vm.cp][0] == "UNARY":
a = vm.stack.pop()
if code[vm.cp][1] == "NOT" and a.tag == Tag.Bool:
vm.stack.append(Item(Tag.Bool, val=1 - a.val))
elif code[vm.cp][1] == "NEG" and a.tag == Tag.Int:
vm.stack.append(Item(Tag.Int, val=to_int(-a.val)))
elif code[vm.cp][1] == "READ" and a.tag == Tag.Unit:
vm.stack.append(Item(Tag.Int, val=int(stdin.readline()[:-1])))
else:
raise DlangRuntimeError
vm.cp += 1
continue
if code[vm.cp][0] == "OPER":
b = vm.stack.pop()
a = vm.stack.pop()
if code[vm.cp][1] == "AND" and a.tag == Tag.Bool and b.tag == Tag.Bool:
vm.stack.append(Item(Tag.Bool, val=1 if a.val and b.val else 0))
elif code[vm.cp][1] == "OR" and a.tag == Tag.Bool and b.tag == Tag.Bool:
vm.stack.append(Item(Tag.Bool, val=1 if a.val or b.val else 0))
elif code[vm.cp][1] == "EQ":
vm.stack.append(Item(Tag.Bool, val=1 if Item.equal(a, b) else 0))
elif code[vm.cp][1] == "LT" and a.tag == Tag.Int and b.tag == Tag.Int:
vm.stack.append(Item(Tag.Bool, val=1 if a.val < b.val else 0))
elif code[vm.cp][1] == "ADD" and a.tag == Tag.Int and b.tag == Tag.Int:
vm.stack.append(Item(Tag.Int, val=to_int(a.val + b.val)))
elif code[vm.cp][1] == "SUB" and a.tag == Tag.Int and b.tag == Tag.Int:
vm.stack.append(Item(Tag.Int, val=to_int(a.val - b.val)))
elif code[vm.cp][1] == "MUL" and a.tag == Tag.Int and b.tag == Tag.Int:
vm.stack.append(Item(Tag.Int, val=to_int(a.val * b.val)))
elif code[vm.cp][1] == "DIV" and a.tag == Tag.Int and b.tag == Tag.Int:
if not b.val:
raise DlangRuntimeError
vm.stack.append(Item(Tag.Int, val=a.val / b.val))
else:
raise DlangRuntimeError
vm.cp += 1
continue
if code[vm.cp][0] == "MK_PAIR":
b = vm.stack.pop()
a = vm.stack.pop()
pair = Item(Tag.PairHeader, bdy=[a, b])
vm.stack.append(Item(Tag.HeapIndex, ptr=pair))
vm.cp += 1
continue
if code[vm.cp][0] == "FST":
ptr = vm.stack.pop()
if ptr.tag != Tag.HeapIndex:
raise DlangRuntimeError
pair = ptr.ptr
if pair.tag != Tag.PairHeader:
raise DlangRuntimeError
vm.stack.append(pair.bdy[0])
vm.cp += 1
continue
if code[vm.cp][0] == "SND":
ptr = vm.stack.pop()
if ptr.tag != Tag.HeapIndex:
raise DlangRuntimeError
pair = ptr.ptr
if pair.tag != Tag.PairHeader:
raise DlangRuntimeError
vm.stack.append(pair.bdy[1])
vm.cp += 1
continue
if code[vm.cp][0] == "MK_INL":
inl = Item(Tag.InlHeader, bdy=[vm.stack.pop()])
vm.stack.append(Item(Tag.HeapIndex, ptr=inl))
vm.cp += 1
continue
if code[vm.cp][0] == "MK_INR":
inr = Item(Tag.InrHeader, bdy=[vm.stack.pop()])
vm.stack.append(Item(Tag.HeapIndex, ptr=inr))
vm.cp += 1
continue
if code[vm.cp][0] == "PUSH":
tag = code[vm.cp][1]
if tag == "STACK_UNIT":
vm.stack.append(Item(Tag.Unit))
elif tag == "STACK_BOOL":
val = 0
if code[vm.cp][2] == "true":
val = 1
if code[vm.cp][2] == "false":
val = 0
vm.stack.append(Item(Tag.Bool, val=val))
elif tag == "STACK_INT":
vm.stack.append(Item(Tag.Int, val=int(code[vm.cp][2])))
vm.cp += 1
continue
if code[vm.cp][0] == "APPLY":
vm.stack.append(Item(Tag.FramePointer, val=vm.fp))
vm.stack.append(Item(Tag.ReturnAddress, val=vm.cp + 1))
ptr = vm.stack[-3]
if ptr.tag != Tag.HeapIndex:
raise DlangRuntimeError
closure = ptr.ptr
if closure.tag != Tag.ClosureHeader:
raise DlangRuntimeError
cp = closure.bdy[0]
if cp.tag != Tag.CodeIndex:
raise DlangRuntimeError
old_cp = vm.cp
vm.cp = cp.val
vm.fp = len(vm.stack) - 2
if old_cp < vm.cp:
JIT_DRIVER.can_enter_jit(
cp=vm.cp, verbosity=verbosity, code=code,
cp_from_label=cp_from_label, vm=vm, stdin=stdin,
start_time=start_time)
continue
if code[vm.cp][0] == "LOOKUP":
if code[vm.cp][1] == "STACK_LOCATION":
vm.stack.append(vm.stack[vm.fp + int(code[vm.cp][2])])
elif code[vm.cp][1] == "HEAP_LOCATION":
ptr = vm.stack[vm.fp - 1]
if ptr.tag != Tag.HeapIndex:
raise DlangRuntimeError
vm.stack.append(ptr.ptr.bdy[int(code[vm.cp][2])])
vm.cp += 1
continue
if code[vm.cp][0] == "RETURN":
item = vm.stack.pop()
cp = vm.stack[vm.fp + 1]
if cp.tag != Tag.ReturnAddress:
raise DlangRuntimeError
old_cp = vm.cp
vm.cp = cp.val
fp = vm.stack[vm.fp]
if fp.tag != Tag.FramePointer:
raise DlangRuntimeError
if vm.fp - 2 < 0:
raise DlangRuntimeError
vm.stack = vm.stack[:max(0, vm.fp - 2)]
vm.stack.append(item)
vm.fp = fp.val
if old_cp < vm.cp:
JIT_DRIVER.can_enter_jit(
cp=vm.cp, verbosity=verbosity, code=code,
cp_from_label=cp_from_label, vm=vm, stdin=stdin,
start_time=start_time)
continue
if code[vm.cp][0] == "MK_CLOSURE":
size = int(code[vm.cp][2])
closure = Item(Tag.ClosureHeader, bdy=[])
closure.bdy.append(Item(Tag.CodeIndex,
val=cp_from_label[code[vm.cp][1]]))
for _ in range(size):
closure.bdy.append(vm.stack.pop())
vm.stack.append(Item(Tag.HeapIndex, ptr=closure))
vm.cp += 1
continue
if code[vm.cp][0] == "SWAP":
a = vm.stack.pop()
b = vm.stack.pop()
vm.stack.append(a)
vm.stack.append(b)
vm.cp += 1
continue
if code[vm.cp][0] == "POP":
vm.stack.pop()
vm.cp += 1
continue
if code[vm.cp][0] == "LABEL":
vm.cp += 1
continue
if code[vm.cp][0] == "FUNCTION":
vm.cp += 1
continue
if code[vm.cp][0] == "DEREF":
item = vm.stack.pop()
if item.tag != Tag.HeapRef:
raise DlangRuntimeError
vm.stack.append(item.ptr)
vm.cp += 1
continue
if code[vm.cp][0] == "MK_REF":
vm.stack.append(Item(Tag.HeapRef, ptr=vm.stack.pop()))
vm.cp += 1
continue
if code[vm.cp][0] == "ASSIGN":
item = vm.stack.pop()
ptr = vm.stack.pop()
if ptr.tag != Tag.HeapRef:
raise DlangRuntimeError
ptr.ptr = item
vm.stack.append(Item(Tag.Unit))
vm.cp += 1
continue
if code[vm.cp][0] == "HALT":
vm.status = 1
continue
if code[vm.cp][0] == "GOTO":
old_cp = vm.cp
vm.cp = cp_from_label[code[vm.cp][1]]
if old_cp < vm.cp:
JIT_DRIVER.can_enter_jit(
cp=vm.cp, verbosity=verbosity, code=code,
cp_from_label=cp_from_label, vm=vm, stdin=stdin,
start_time=start_time)
continue
if code[vm.cp][0] == "TEST":
old_cp = vm.cp
item = vm.stack.pop()
if item.tag != Tag.Bool:
raise DlangRuntimeError
if item.val:
vm.cp += 1
else:
vm.cp = cp_from_label[code[vm.cp][1]]
if old_cp < vm.cp:
JIT_DRIVER.can_enter_jit(
cp=vm.cp, verbosity=verbosity, code=code,
cp_from_label=cp_from_label, vm=vm, stdin=stdin,
start_time=start_time)
continue
if code[vm.cp][0] == "CASE":
old_cp = vm.cp
ptr = vm.stack.pop()
if ptr.tag != Tag.HeapIndex:
raise DlangRuntimeError
item = ptr.ptr
if item.tag == Tag.InlHeader:
vm.stack.append(item.bdy[0])
vm.cp += 1
elif item.tag == Tag.InrHeader:
vm.stack.append(item.bdy[0])
vm.cp = cp_from_label[code[vm.cp][1]]
else:
raise DlangRuntimeError
if old_cp < vm.cp:
JIT_DRIVER.can_enter_jit(
cp=vm.cp, verbosity=verbosity, code=code,
cp_from_label=cp_from_label, vm=vm, stdin=stdin,
start_time=start_time)
continue
raise Exception
except DlangRuntimeError:
if verbosity != "quiet":
print "Runtime error at cp = " + str(vm.cp)
return
if verbosity != "output" and verbosity != "quiet":
duration = str(int(1000000000 * time.time()) - start_time + 1000000000000)
print (duration[1:4] + " s " +
duration[4:7] + " ms " +
duration[7:10] + " ms " +
duration[10:13] + " us")
if verbosity != "time" and verbosity != "quiet":
print vm.print_result()
def entry_point(argv):
"""Entry point for the Python interpreter"""
args = {}
args[""] = ""
i = 1
while i < len(argv):
if argv[i][:2] == "--":
if i + 1 == len(argv):
print "Missing argument for " + args[argv[i]]
return 1
args[argv[i]] = argv[i+1]
i += 2
else:
args[""] = argv[i]
i += 1
filename = args[""]
verbosity = args["--verbosity"] if "--verbosity" in args else "output"
# Read the input code
code = ""
fp = os.open(filename, os.O_RDONLY, 0777)
while True:
read = os.read(fp, 4096)
if not read:
break
code += read
os.close(fp)
# Run the interpreter
interpret(code, verbosity)
return 0
def target(*_):
"""Entry point for RPython"""
return entry_point, None
# Main for Python
if __name__ == "__main__":
entry_point(sys.argv)