/
vm.py
236 lines (194 loc) · 7.07 KB
/
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
from const import *
from runtime import LinkList, build_list, Closure
class VMException(Exception):
pass
class Frame(object):
def __init__(self, proto, upvars, args):
self.proto = proto
self.insts = proto.insts
self.args = args
self.upvars = upvars
self.to_be_forked = False
self.localvars = [None] * proto.maxLocalvars
argn = len(args)
argc = proto.argc
if proto.isVararg:
if argn >= argc:
self.localvars[:argc] = args[:argc]
self.varargs = build_list(args[argc:])
else:
raise VMException("Expect equal or more than %s arguments, got %s"
% (argc, argn))
else:
if argn == argc:
self.localvars[:argc] = args[:]
else:
raise VMException("Expect %s arguments, got %s" % (argc, argn))
self.stack = []
def close_upvars(frame, scope_index):
for upvar in frame.upvars:
if upvar.frame is frame and upvar.scope_index == index:
upvar.close()
class VM(object):
def __init__(self, consts, env, entry=None, debug=False):
self.consts = consts
self.env = env
if not entry:
entry = consts[-1]
self.frames = [Frame(entry, [], [])]
self.debug = debug
def inst2str(self, inst):
operator, operand = inst
op = VMOps[operator]
if operator == OpBinOp:
operand = BinOps[operand][0]
return "%s %s" % (op, operand)
elif operator == OpUnOp:
operand = UnOps[operand][0]
return "%s %s" % (op, operand)
elif operator == OpLoadConst:
c = self.consts[operand]
return "%s %s # %s" % (op, operand, c)
elif operator == OpLoadGlobal:
c = self.consts[operand]
return "%s %s # %s" % (op, operand, c)
else:
return "%s %s" % (op, operand)
def turn_debug(self, flag):
self.debug = flag
def start(self):
env = self.env
consts = self.consts
frames = self.frames
frame = frames.pop()
insts = frame.insts
pc = 0
while True:
try:
operator, operand = insts[pc]
if self.debug:
print self.inst2str(insts[pc])
except IndexError:
return
if operator == OpLoadLocal:
assert operand >= 0
var = frame.localvars[operand]
assert var is not None
frame.stack.append(var)
elif operator == OpSetLocal:
assert operand >= 0
a = frame.stack.pop()
frame.localvars[operand] = a
elif operator == OpLoadGlobal:
a = consts[operand]
frame.stack.append(env[a])
elif operator == OpSetGlobal:
b = frame.stack.pop()
s = consts[operand]
env[s] = b
elif operator == OpLoadUpvar:
assert operand >= 0
var = frame.upvars[operand].get()
frame.stack.append(var)
elif operator == OpSetUpvar:
assert operand >= 0
a = frame.stack.pop()
frame.upvars[operand].set(a)
elif operator == OpLoadVarg:
frame.stack.append(frame.varargs)
elif operator == OpLoadConst:
var = consts[operand]
frame.stack.append(var)
elif operator == OpBinOp:
b = frame.stack.pop()
a = frame.stack.pop()
try:
frame.stack.append(BinOps[operand][1](a, b))
except KeyError:
raise VMException("Unknown BinOp '%s' " % operand)
elif operator == OpUnOp:
a = frame.stack.pop()
try:
symbol, unop = UnOps[operand]
frame.stack.append(unop(a))
except KeyError:
raise VMException("Unknown UnOp '%s' " % operand)
elif operator == OpCall:
assert operand >= 0
if operand == 0:
args = []
else:
args = frame.stack[-operand:]
frame.stack = frame.stack[:-operand]
closure = frame.stack.pop()
if isinstance(closure, Closure):
proto = closure.proto
upvars = closure.upvars
new_frame = Frame(proto, upvars, args)
frame.saved_pc = pc
frames.append(frame)
frame = new_frame
insts = frame.insts
pc = 0
continue
else:
ret = closure(*args)
frame.stack.extend(ret or [])
elif operator == OpTailCall:
if operand == 0:
args = []
else:
args = frame.stack[-operand:]
frame.stack = frame.stack[:-operand]
closure = frame.stack.pop()
if isinstance(closure, Closure):
proto = closure.proto
upvars = closure.upvars
frame = Frame(proto, upvars, args)
insts = frame.insts
pc = 0
continue
else:
ret = closure(*args)
frame.stack.extend(ret or [])
elif operator == OpRet:
rets = frame.stack[-operand:]
close_upvars(frame, 0)
frame = frames.pop()
pc = frame.saved_pc
insts = frame.insts
frame.stack.extend(rets)
elif operator == OpJump:
pc += operand
continue
elif operator == OpTest:
"""
(if (= x 2) (a) (b))
# In the end , only the value of whole "if"
# expression is on the top of the stack.
Load x
LoadConst 2
EQ
TEST 3
LOAD A
JUMP 2
LOAD B
"""
isTrue = frame.stack.pop()
if not isTrue:
pc += operand
continue
elif operator == OpPop:
frame.stack = frame.stack[:-operand]
elif operator == OpCloseUpvar:
scope_index = operand
close_upvars(frame, scope_index)
elif operator == OpBuildClosure:
proto = consts[operand]
closure = Closure(proto, frame)
frame.stack.append(closure)
elif operator == OpHalt:
return
else:
raise VMException("Unknown VM instruction: %s" % operator)
pc += 1