-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions.py
411 lines (329 loc) · 7.89 KB
/
instructions.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
from vmdata import *
# exception to raise when vm should terminate
class StopExecution(Exception):
pass
# Instructions:
class AbstractOp:
# default value
arity = 0
def __init__(self, *args):
assert len(args) == self.arity
self.args = args
# store args also as string for easier output
self.str_args = map(str, args)
# dump as a instruction on lambda man language
def to_source(self):
return ' '.join([self.name] + self.str_args)
def __repr__(self):
return '{}({})'.format(self.__class__, ', '.join(self.str_args))
# push to data stack integer N
class LoadConstant(AbstractOp):
name = 'LDC'
arity = 1
def mutate(self, vm):
vm.data_stack.append(VmInteger(self.args[0]))
vm.counter += 1
# push to data stack
# from n-th frame i-th value
#
# frame[n][i] --> stack
class LoadEnv(AbstractOp):
name = 'LD'
arity = 2
def mutate(self, vm):
n, i = self.args
# go n frames up
frame = go_to_nth_parent(vm.current_frame, n)
assert frame.tag != Tag.DUMMY_FRAME
# push the i-th elemt
value = frame.values[i]
vm.data_stack.append(value)
vm.counter += 1
FrameToStack = LoadEnv
# save top value from data stack
# to n-th frame as i-th value
#
# stack --> frame[n][i]
class StoreToEnv(AbstractOp):
name = 'ST'
arity = 2
def mutate(self, vm):
# go n frames up
n, i = self.args
frame = go_to_nth_parent(vm.current_frame, n)
assert frame.tag != Tag.DUMMY_FRAME
value = vm.data_stack.pop()
frame.values[i] = value
vm.counter += 1
# synonim:
SaveToEnv = StoreToEnv
class IntOperation(AbstractOp):
def mutate(self, vm):
# self.func should be reloaded
self.mutate_arithmetic(vm, self.func)
@staticmethod
def mutate_arithmetic(vm, func):
v2 = vm.data_stack.pop()
v1 = vm.data_stack.pop()
assert (v1.tag == Tag.INTEGER)
assert (v2.tag == Tag.INTEGER)
c = func(v1.n, v2.n)
vm.data_stack.append(VmInteger(c))
vm.counter += 1
class IntAddition(IntOperation):
name = 'ADD'
@staticmethod
def func(x, y):
return x + y
class IntSubtraction(IntOperation):
name = 'SUB'
@staticmethod
def func(x, y):
return x - y
class IntMultiplication(IntOperation):
name = 'MUL'
@staticmethod
def func(x, y):
return x * y
class IntDivision(IntOperation):
name = 'DIV'
@staticmethod
def func(x, y):
return x / y
class CompareEqual(IntOperation):
name = 'CEQ'
@staticmethod
def func(x, y):
if x == y:
return 1
else:
return 0
class CompareGreater(IntOperation):
name = 'CGT'
@staticmethod
def func(x, y):
if x > y:
return 1
else:
return 0
class CompareGreaterOrEqual(IntOperation):
name = 'CGTE'
@staticmethod
def func(x, y):
if x >= y:
return 1
else:
return 0
class IsInteger(AbstractOp):
name = 'ATOM'
def mutate(self, vm):
x = vm.data_stack.pop()
if x.tag == Tag.INTEGER:
y = 1
else:
y = 0
vm.data_stack.append(VmInteger(y))
vm.counter += 1
class AllocateCons(AbstractOp):
name = 'CONS'
def mutate(self, vm):
y = vm.data_stack.pop()
x = vm.data_stack.pop()
z = VmCons(x, y)
vm.data_stack.append(z)
vm.counter += 1
class ExtractFirst(AbstractOp):
name = 'CAR'
def mutate(self, vm):
x = vm.data_stack.pop()
assert (x.tag == Tag.CONS)
y = x.first
vm.data_stack.append(y)
vm.counter += 1
class ExtractSecond(AbstractOp):
name = 'CDR'
def mutate(self, vm):
x = vm.data_stack.pop()
assert (x.tag == Tag.CONS)
y = x.second
vm.data_stack.append(y)
vm.counter += 1
# jump to instruction based on
# top value form data_stack
class ConditionalBranch(AbstractOp):
name = 'SEL'
arity = 2
def mutate(self, vm):
x = vm.data_stack.pop()
go_true, go_false = self.args
assert (x.tag == Tag.INTEGER)
vm.control_stack.append(JoinAddress(vm.counter + 1))
if x == 0:
vm.counter = go_false
else:
vm.counter = go_true
class ReturnFromBranch(AbstractOp):
name = 'JOIN'
def mutate(self, vm):
x = vm.control_stack.pop()
assert (x.tag == Tag.JOIN)
vm.counter = x.value
class TailCallBranch(AbstractOp):
name = 'TSEL'
arity = 2
def mutate(self, vm):
go_true, go_false = self.args
x = vm.data_stack.pop()
assert (x.tag == Tag.INTEGER)
if x.n == 0:
vm.counter = go_false
else:
vm.counter = go_true
class LoadFunction(AbstractOp):
name = 'LDF'
arity = 1
def mutate(self, vm):
# address from which to load function
(address, ) = self.args
closure = VmClosure(address, vm.current_frame)
vm.data_stack.append(closure)
vm.counter += 1
class CallFunction(AbstractOp):
name = 'AP'
arity = 1
def mutate(self, vm):
# n -- number of arguments
(n, ) = self.args
closure = vm.data_stack.pop()
assert closure.tag == Tag.CLOSURE
address = closure.address
frame = closure.frame
# create new frame for the call
new_frame = EnvFrame(n)
new_frame.parent = frame
# copy n values from stack into frame
new_frame.copy_from_stack(vm, n)
# save frame pointer
vm.control_stack.append(vm.current_frame)
# save return address
vm.control_stack.append(ReturnAddress(vm.counter + 1))
# establish new env
vm.current_frame = new_frame
# and jump to address
vm.counter = address
class ReturnFromFunction(AbstractOp):
name = 'RTN'
def mutate(self, vm):
# pop return address
address = vm.control_stack.pop()
if address.tag == Tag.STOP:
raise StopExecution()
assert (address.tag == Tag.RETURN)
# pop frame pointer
frame = vm.control_stack.pop()
# restore env
vm.current_frame = frame
# jump to return address
vm.counter = address.value
# create dummy frame of size n
# and go to it
class EmptyEnv(AbstractOp):
name ='DUM'
arity = 1
def mutate(self, vm):
# n -- size of a frame to allocate
(n, ) = self.args
frame = EnvFrame(n)
frame.parent = vm.current_frame
frame.tag = Tag.DUMMY_FRAME
vm.current_frame = frame
vm.counter += 1
class RecursiveCallFunction(AbstractOp):
name = 'RAP'
arity = 1
def mutate(self, vm):
# n -- number of arguments to copy
(n, ) = self.args
closure = vm.data_stack.pop()
assert closure.tag == Tag.CLOSURE
address = closure.address
frame = closure.frame
assert vm.current_frame.tag == Tag.DUMMY_FRAME
assert frame.get_size() == n
frame.copy_from_stack(vm, n)
next_frame = vm.current_frame.parent
# save frame pointer and return address
vm.control_stack.append(next_frame)
vm.control_stack.append(ReturnAddress(vm.counter + 1))
vm.current_frame = frame
# make the frame normal
vm.current_frame.tag = Tag.GOOD_FRAME
vm.counter = address
# is not supported by ABI
class Stop(AbstractOp):
name = 'STOP'
class TailCallFunction(AbstractOp):
name = 'TAP'
arity = 1
def mutate(self, vm):
# n -- number of arguments to copy
(n, ) = self.args
closure = vm.data_stack.pop()
assert (closure.tag == Tag.CLOSURE)
address = closure.address
frame = closure.frame
new_frame = EnvFrame(n)
new_frame.parent = vm.current_frame
frame.copy_from_stack(vm, n)
vm.current_frame = new_frame
vm.counter = address
class RecursiveTailCallFunction(AbstractOp):
name = 'TRAP'
arity = 1
def mutate(self, vm):
# n -- number of arguments to copy
(n, ) = self.args
closure = vm.data_stack.pop()
assert (closure.tag == Tag.CLOSURE)
address = closure.address
frame = closure.frame
assert vm.current_frame.tag == Tag.DUMMY_FRAME
assert vm.current_frame.get_size() == n
frame.copy_from_stack(vm, n)
vm.current_frame = frame
vm.counter = address
class DebugPrint(AbstractOp):
name = 'DBUG'
def mutate(self, vm):
value = vm.data_stack.pop()
print 'DBUG: {} ({})'.format(value, value.__class__)
vm.trace_output.append( value )
vm.counter += 1
ALL_INSTRUCTIONS = [
LoadConstant,
LoadEnv,
IntAddition,
IntSubtraction,
IntMultiplication,
IntDivision,
CompareEqual,
CompareGreater,
CompareGreaterOrEqual,
IsInteger,
AllocateCons,
ExtractFirst,
ExtractSecond,
ConditionalBranch,
ReturnFromBranch,
LoadFunction,
CallFunction,
ReturnFromFunction,
EmptyEnv,
RecursiveCallFunction,
Stop,
TailCallBranch,
TailCallFunction,
RecursiveTailCallFunction,
StoreToEnv,
DebugPrint,
]