-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2l_internals.py
528 lines (430 loc) · 13.9 KB
/
p2l_internals.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
from collections import namedtuple
from instructions import *
from p2l_labels import *
from p2l_config import P2LConfig
def assert_type(obj, class_type):
if isinstance(obj, class_type):
return
assert False, 'Type mismatch, expected {}, got {}'.format(
class_type,
str(obj))
def assert_not_type(obj, class_type):
if not isinstance(obj, class_type):
return
# print obj
assert False
# data to store function definition in compiler
class FunctionImpl:
def __init__(self, name, byte_code):
self.name = name
# required paramter
self.arity = None
self.dummy_arity = 0
self.byte_code = byte_code
# class
ArrayAllocation = namedtuple(
'ArrayAllocation',
['offset', 'size'],
verbose = False
)
# class to represent
# all names in current scope
# (this includes usual variables and arrays)
class ScopeVariables:
def __init__(self):
self.vars = {}
self.arrays = {}
# last free index for allocation
self.free_index = P2LConfig.NUM_REGISTERS
# counter for number of local variables allocated
self.dummy_slots = 0
def declare_variable(self, var_name, is_local):
# TODO: check conflicts
self.vars[var_name] = self.free_index
self.free_index += 1
if is_local:
self.dummy_slots += 1
def declare_array(self, array_name, size):
allocation = ArrayAllocation(
offset=self.free_index,
size=size
)
self.arrays[array_name] = allocation
self.free_index += size
self.dummy_slots += size
def array_offset(self, array_name):
allocation = self.arrays[array_name]
return allocation.offset
def is_declared(self, var_name):
return var_name in self.vars
def total_slots_allocated(self):
return self.free_index
def dummy_slots_allocated(self):
return self.dummy_slots
def produce_get_for_array(self, array_name, index):
allocation = self.arrays[array_name]
absolute_index = allocation.offset + index
byte_code = ByteCode()
byte_code.append( LoadEnv(0, absolute_index) )
def produce_get_byte_code(self, var_name):
# returns byte code needed to
# 1) take value of @var_name
# 2) push onto data stack
if var_name not in self.vars:
# @var_name is likely global
return None
index = self.vars[var_name]
byte_code = ByteCode()
byte_code.append( LoadEnv(0, index) )
return byte_code
def produce_assign_byte_code(self, var_name):
# returns byte code needed to
# 1) pop value from data stack
# 2) save this value in @var_name
index = self.vars[var_name]
# save the value to current frame
byte_code = ByteCode()
byte_code.append( StoreToEnv(0, index) )
return byte_code
class CompilationError(Exception):
def __init__(self, message):
Exception.__init__(self, message)
# byte code represents a sequence of instructions
# for vm to executre
class ByteCode:
def __init__(self):
self.output = []
@staticmethod
def from_list(output):
byte_code = ByteCode()
byte_code.output = output
return byte_code
def append(self, op):
assert_not_type(op, ByteCode)
self.output.append(op)
def append_byte_code(self, byte_code):
assert_type(byte_code, ByteCode)
self.output += byte_code.output
def show(self):
print 'ByteCode output of size {}:'.format(len(self.output))
for op in self.output:
print op, ' |||| ', op.to_source()
def show_without_source(self):
print 'ByteCode output of size {}:'.format(len(self.output))
for index, op in enumerate(self.output):
print '{}: \t {}'.format(index, op)
def dump_without_source(self):
result = ['ByteCode output of size {}:'.format(len(self.output))]
for index, op in enumerate(self.output):
# {:d} -- means aligning integers
result.append( '{:d}: {}'.format(index, op) )
return '\n'.join(result)
def to_source(self):
result = ''
for op in self.output:
result += op.to_source()
result += '\n'
return result
class EmptyByteCode(ByteCode):
pass
# instructions of this type will be replaced with the byte code,
# returned by function @expand
class IExpandable:
def expand(self):
assert False
def to_source(self):
raise CompilationError(
'{} wasnt expanded'.format(self.__class__)
)
# internal compiler instructions
# that are used during code generation
class ApplyFunction(IExpandable):
def __init__(self, name, arity, dummy_arity):
self.name = name
self.arity = arity
self.dummy_arity = dummy_arity
def __repr__(self):
return 'ApplyFunction "{}" (arity = {}, dummy = {})'.format(
self.name,
self.arity,
self.dummy_arity
)
def expand(self):
output = []
# add dummy variables to stack
for i in xrange(self.dummy_arity):
output.append( LoadConstant(0) )
# count size of frame to be allocated
num_variables = P2LConfig.NUM_REGISTERS
num_variables += self.arity
num_variables += self.dummy_arity
output += [
LoadFunctionLabel(self.name),
CallFunction(num_variables)
]
result = ByteCode.from_list(output)
# print result
return result
# executing of expansion of this byte code have following effect:
# stack --> get(x)
# Load(n, x)
class LoadEnvGeneric(IExpandable):
def __init__(self, n):
if n != 0:
raise CompilationError(
'LoadEnvGeneric is only supported from 0 frame')
self.n = n
def __repr__(self):
return 'LoadToEnvGeneric({})'.format(
self.n,
)
def expand(self):
x_register = LapshaLoadImplementation.X_REGISTER
output = [
StoreToEnv(self.n, x_register),
UnconditionalJumpLabelSEL(LAPSHA_LOAD_LABEL)
]
byte_code = ByteCode()
byte_code.output = output
return byte_code
# expects x in 0 register
class LapshaLoadSlow(IExpandable):
# register where "x" value is stored
X_REGISTER = 0
def expand(self):
# construct body
output = [Label(LAPSHA_LOAD_LABEL)]
for i in xrange(P2LConfig.MAX_LAPSHA_INDEX):
if_true = LABEL_PROVIDER.next_any_label('true')
if_false = LABEL_PROVIDER.next_any_label('false')
value_to_compare = i
body = [
LoadEnv(0, self.X_REGISTER),
LoadConstant(value_to_compare),
CompareEqual(),
IfBranchLabel(if_true, if_false),
Label(if_true),
LoadEnv(0, value_to_compare),
ReturnFromBranch(),
Label(if_false)
]
output += body
byte_code = ByteCode()
byte_code.output = output
return byte_code
# expects x in 0 register
class LapshaLoadImplementation(IExpandable):
# register where "x" value is stored
X_REGISTER = 0
def dp(self, length, add):
assert length > 0
if length == 1:
# x == add
output = [
LoadEnv(0, add),
ReturnFromBranch(),
]
return output
if_true = LABEL_PROVIDER.next_any_label('true')
if_false = LABEL_PROVIDER.next_any_label('false')
mid = length / 2
if_output = self.dp(mid, add + mid)
else_output = self.dp(mid, add)
output = [
LoadEnv(0, self.X_REGISTER),
LoadConstant(add + mid),
CompareGreaterOrEqual(),
IfBranchLabel(if_true, if_false),
Label(if_true)
] + if_output + [
Label(if_false)
] + else_output
return output
def expand(self):
# construct body
output = [Label(LAPSHA_LOAD_LABEL)]
output += self.dp(P2LConfig.MAX_LAPSHA_INDEX, 0)
byte_code = ByteCode()
byte_code.output = output
return byte_code
# executing of expansion of this byte code have following effect:
# [x, y]
# stack --> get(y)
# stack --> get(x)
# frame[n][x] = y
class SaveToEnvGeneric(IExpandable):
def __init__(self, n):
if n != 0:
raise CompilationError(
'LoadSaveGeneric is only supported from 0 frame')
self.n = n
def __repr__(self):
return 'SaveToEnvGeneric({})'.format(
self.n,
)
def expand(self):
x_register = LapshaSaveImplementation.X_REGISTER
y_register = LapshaSaveImplementation.Y_REGISTER
output = [
StoreToEnv(self.n, y_register),
StoreToEnv(self.n, x_register),
UnconditionalJumpLabelSEL(LAPSHA_SAVE_LABEL)
]
byte_code = ByteCode()
byte_code.output = output
return byte_code
# NOTE: there is only small difference
# with Load
# expects x in 0 register
# expects y in 1 register
class LapshaSaveSlow(IExpandable):
# register where "x" value is stored
X_REGISTER = 0
Y_REGISTER = 1
def expand(self):
# construct body
output = [Label(LAPSHA_SAVE_LABEL)]
for i in xrange(P2LConfig.MAX_LAPSHA_INDEX):
if_true = LABEL_PROVIDER.next_any_label('true')
if_false = LABEL_PROVIDER.next_any_label('false')
value_to_compare = i
body = [
LoadEnv(0, self.X_REGISTER), # frame[x] --> stack
LoadConstant(value_to_compare),
CompareEqual(),
IfBranchLabel(if_true, if_false),
Label(if_true), # x == value
# seems like: the only difference is here
LoadEnv(0, self.Y_REGISTER), # frame[y] --> stack
SaveToEnv(0, value_to_compare), # stack --> frame[x]
# ^^^^
ReturnFromBranch(),
Label(if_false)
]
output += body
byte_code = ByteCode()
byte_code.output = output
return byte_code
# NOTE: there is only small difference
# with Load
# expects x in 0 register
# expects y in 1 register
class LapshaSaveImplementation(IExpandable):
# register where "x" value is stored
X_REGISTER = 0
Y_REGISTER = 1
def dp(self, length, add):
assert length > 0
if length == 1:
# x == add
output = [
# LoadConstant(add),
# DebugPrint(),
LoadEnv(0, self.Y_REGISTER), # frame[y] --> stack
SaveToEnv(0, add), # stack --> frame[x]
ReturnFromBranch(),
]
return output
if_true = LABEL_PROVIDER.next_any_label('true')
if_false = LABEL_PROVIDER.next_any_label('false')
mid = length / 2
if_output = self.dp(mid, add + mid)
else_output = self.dp(mid, add)
output = [
# LoadConstant(add),
# LoadConstant(add + length),
# AllocateCons(),
# DebugPrint(),
LoadEnv(0, self.X_REGISTER),
LoadConstant(add + mid),
CompareGreaterOrEqual(),
IfBranchLabel(if_true, if_false),
Label(if_true),
] + if_output + [
Label(if_false)
] + else_output
return output
def expand(self):
# construct body
output = [Label(LAPSHA_SAVE_LABEL)]
# output += [
# LoadConstant(777),
# LoadEnv(0, 0),
# AllocateCons(),
# DebugPrint()
# ]
output += self.dp(P2LConfig.MAX_LAPSHA_INDEX, 0)
byte_code = ByteCode()
byte_code.output = output
return byte_code
# TSEL - based
class UnconditionalJumpLabel(IExpandable):
def __init__(self, label):
self.label = label
def expand(self):
byte_code = ByteCode()
byte_code.output = [
LoadConstant(0),
JumpLabel(self.label)
]
return byte_code
#SEL - based
class UnconditionalJumpLabelSEL(IExpandable):
def __init__(self, label):
self.label = label
def expand(self):
byte_code = ByteCode()
byte_code.output = [
LoadConstant(0),
JumpLabelSEL(self.label)
]
return byte_code
class Label:
def __init__(self, label):
self.label = label
def __repr__(self):
return '{}: '.format(self.label)
class WithLabelAbstractOp:
def __init__(self, label):
self.label = label
def __repr__(self):
return '{} ({})'.format(self.__class__, self.label)
# this method is used to replace label based jumps
# with their absoulte values
@staticmethod
def transform(address):
assert False
class WithLabelAbstractOp2:
def __init__(self, label1, label2):
self.label1 = label1
self.label2 = label2
def __repr__(self):
return '{} ({}, {})'.format(
self.__class__,
self.label1,
self.label2
)
# always should be put
# with LoadConstant
class JumpLabel(WithLabelAbstractOp):
# virtual
@staticmethod
def transform(address):
return TailCallBranch(address, address)
# SEL based
class JumpLabelSEL(WithLabelAbstractOp):
# virtual
@staticmethod
def transform(address):
return ConditionalBranch(address, address)
class LoadFunctionLabel(WithLabelAbstractOp):
# virtual
@staticmethod
def transform(address):
return LoadFunction(address)
# TSEL based
class IfBranchLabel(WithLabelAbstractOp2):
# virtual
@staticmethod
def transform(address1, address2):
return TailCallBranch(address1, address2)