-
Notifications
You must be signed in to change notification settings - Fork 8
/
bytecode.py
514 lines (408 loc) · 14.8 KB
/
bytecode.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
import copy
from util import signext, assert_signexted
class UnknownCommand(Exception):
"""A byte sequence was attempted to parse that has no byte code command associated"""
class ByteCodeCommand(object):
# defaul values
nargs = 0
length = property(lambda self: self.nargs + 1)
commandmask = 0xff
@classmethod
def from_bin(cls, data, offset):
assert cls.check_match(data[offset])
instance = ByteCodeCommand()
instance.__class__ = cls
instance.set_from_data(data, offset)
instance._check()
return instance
@classmethod
def check_match(cls, command):
if not hasattr(cls, 'command'):
# happens for command groups like BinaryOperator
return False
return (command & cls.commandmask) == cls.command
def __repr__(self):
# generic initialize-by-__dict__
return '%s(%s)'%(type(self).__name__, ', '.join('%s=%r'%(k, v) for (k, v) in vars(self).items()))
############ defaults for trivial cases ############
def to_bin(self):
assert self.commandmask == 0xff
assert self.nargs == 0
return [self.command]
def _check(self):
pass
def set_from_data(self, data, offset):
assert self.commandmask == 0xff
assert self.nargs == 0
pass
def generalize(self, own_position):
"""Return a copy of self, possibly modified to a more general
command that has the same semantics but not necessarily a
defined length"""
return copy.copy(self)
class VariableLengthCommand(ByteCodeCommand):
"""Command that has different length versions depending on the exact
payload"""
def _get_real_command(self):
"""Return a proper ByteCodeCommand, use current .nargs for determining
the length requirements."""
raise NotImplementedError
nargs = None
def to_bin(self):
real = self._get_real_command() # if the worst case was wrong, this will raise an error e.g. from assert_signexted
ret = real.to_bin()
if len(ret) != self.length:
raise Exception("_get_real_command didn't respect nargs")
return ret
class SFACommand(ByteCodeCommand):
def __init__(self, sfa):
self.sfa = sfa
def set_from_data(self, data, offset):
self.sfa = signext(data[offset], 0x3f)
def _check(self):
assert_signexted(self.sfa, 0x3f)
def to_bin(self):
self._check()
return [self.command | (self.sfa & 0x3f)]
class PushLocal(SFACommand):
command = 0x00
commandmask = 0xc0
class PopLocal(SFACommand):
command = 0x40
commandmask = 0xc0
class UnaryOperator(ByteCodeCommand):
pass
class BitwiseNot(UnaryOperator): command = 0x8c
class ArithmeticInvert(UnaryOperator): command = 0x8d
class LogicNot(UnaryOperator): command = 0x8e
class BinaryOperator(ByteCodeCommand):
pass
class Add(BinaryOperator): command = 0x80
class Sub(BinaryOperator): command = 0x81
class Mul(BinaryOperator): command = 0x82
class Div(BinaryOperator): command = 0x83
class Mod(BinaryOperator): command = 0x84
class ShiftLeft(BinaryOperator): command = 0x85
class ShiftRight(BinaryOperator): command = 0x86
class BitwiseAnd(BinaryOperator): command = 0x87
class BitwiseOr(BinaryOperator): command = 0x88
class BitwiseXor(BinaryOperator): command = 0x89
class LogicAnd(BinaryOperator): command = 0x8a
class LogicOr(BinaryOperator): command = 0x8b
class PushConstant(ByteCodeCommand):
def generalize(self, own_position):
return PushConstantV(value=self.value)
class PushConstantV(VariableLengthCommand):
def __init__(self, value):
self.value = value
nargs = 2 # maximum
def _get_real_command(self):
if self.nargs == 2:
return Push16(value=self.value)
elif self.nargs == 1:
if self.value < 128:
return PushS8(value=self.value)
else:
return PushU8(value=self.value)
else:
return PushImmediate(value=self.value)
def prebake(self):
if -4 <= self.value < 4:
self.nargs = 0
elif -128 <= self.value < 256:
self.nargs = 1
elif 0 <= self.value < 0x10000:
self.nargs = 2
else:
raise ValueError("Integer overflow")
class PushImmediate(PushConstant):
command = 0x90
commandmask = 0xf8
def __init__(self, value):
self.value = value
def set_from_data(self, data, offset):
self.value = signext(data[offset], 0x07)
def _check(self):
assert_signexted(self.value, 0x07)
def to_bin(self):
self._check()
return [self.command | (self.value & 0x07)]
class PushData(PushConstant):
pass
class PushU8(PushData):
command = 0x98
nargs = 1
def __init__(self, value=None):
self.value = value
def set_from_data(self, data, offset):
self.value = data[offset+1]
def _check(self):
assert self.value in xrange(256)
def to_bin(self):
self._check()
return [self.command, self.value]
class PushS8(PushData):
command = 0x99
nargs = 1
def __init__(self, value):
self.value = value
def set_from_data(self, data, offset):
self.value = signext(data[offset+1], 0xff)
def _check(self):
assert_signexted(self.value, 0xff)
def to_bin(self):
self._check()
return [self.command, self.value%256]
class Push16(PushData):
command = 0x9a
nargs = 2
def __init__(self, value):
self.value = value
def set_from_data(self, data, offset):
self.value = signext((data[offset+1]<<8)+data[offset+2], 0xffff)
def _check(self):
assert_signexted(self.value, 0xffff)
def to_bin(self):
self._check()
return [self.command, ((self.value & 0xffff) >> 8)%256, (self.value & 0xff)%256]
class Return(ByteCodeCommand): command = 0x9b
class Return0(Return): command = 0x9c
class DropValue(ByteCodeCommand): command = 0x9d
class CallAddress(ByteCodeCommand): command = 0x9e
class JumpToAddress(ByteCodeCommand): command = 0x9f
class VariableAddressCommand(VariableLengthCommand):
def __init__(self, address):
self.address = address
nargs = 2 # worst case
def prebake(self):
if -128 <= self.reladdr < 128:
self.nargs = 1
else:
self.nargs = 2
def _get_real_command(self):
if self.nargs == 1:
return self.shortcommand(reladdr=self.reladdr)
else:
return self.longcommand(reladdr=self.reladdr)
class RelativeAddressCommand(ByteCodeCommand):
def __init__(self, reladdr):
self.reladdr = reladdr
def generalize(self, own_position):
return self.generalized(address=own_position + self.reladdr)
class RelativeAddressCommand1(RelativeAddressCommand):
nargs = 1
def set_from_data(self, data, offset):
self.reladdr = signext(data[offset+1], 0xff)
def _check(self):
assert_signexted(self.reladdr, 0xff)
def to_bin(self):
self._check()
return [self.command, self.reladdr % 256]
class RelativeAddressCommand2(RelativeAddressCommand):
nargs = 2
def set_from_data(self, data, offset):
self.reladdr = signext((data[offset+1]<<8)+data[offset+2], 0xffff)
def _check(self):
assert_signexted(self.reladdr, 0xffff)
def to_bin(self):
self._check()
return [self.command, ((self.reladdr & 0xffff) >> 8)%256, (self.reladdr & 0xff)%256]
class JumpCommand(object):
pass
class UnconditionalJumpCommand(JumpCommand):
pass
class JumpV(UnconditionalJumpCommand, VariableAddressCommand):
pass
class JumpRel1(UnconditionalJumpCommand, RelativeAddressCommand1):
command = 0xa0
generalized = JumpV
class JumpRel2(UnconditionalJumpCommand, RelativeAddressCommand2):
command = 0xa1
generalized = JumpV
JumpV.shortcommand, JumpV.longcommand = JumpRel1, JumpRel2
class CallCommand(object):
pass
class CallV(CallCommand, VariableAddressCommand):
pass
class CallRel1(CallCommand, RelativeAddressCommand1):
command = 0xa2
generalized = CallV
class CallRel2(CallCommand, RelativeAddressCommand2):
command = 0xa3
generalized = CallV
CallV.shortcommand, CallV.longcommand = CallRel1, CallRel2
class JumpIfCommand(JumpCommand):
pass
class JumpVIf(JumpIfCommand, VariableAddressCommand):
pass
class JumpRel1If(JumpIfCommand, RelativeAddressCommand1):
command = 0xa4
generalized = JumpVIf
class JumpRel2If(JumpIfCommand, RelativeAddressCommand2):
command = 0xa5
generalized = JumpVIf
JumpVIf.shortcommand, JumpVIf.longcommand = JumpRel1If, JumpRel2If
class JumpIfNotCommand(JumpCommand):
pass
class JumpVIfNot(JumpIfNotCommand, VariableAddressCommand):
pass
class JumpRel1IfNot(JumpIfNotCommand, RelativeAddressCommand1):
command = 0xa6
generalized = JumpVIfNot
class JumpRel2IfNot(JumpIfNotCommand, RelativeAddressCommand2):
command = 0xa7
generalized = JumpVIfNot
JumpVIfNot.shortcommand, JumpVIfNot.longcommand = JumpRel1IfNot, JumpRel2IfNot
class CompareLT(BinaryOperator): command = 0xa8
class CompareLE(BinaryOperator): command = 0xa9
class CompareEE(BinaryOperator): command = 0xaa
class CompareNE(BinaryOperator): command = 0xab
class CompareGE(BinaryOperator): command = 0xac
class CompareGT(BinaryOperator): command = 0xad
class StackPointer(ByteCodeCommand):
command = 0xae
class StackFramePointer(ByteCodeCommand):
command = 0xaf
class CallUserFunction(ByteCodeCommand):
command = 0xb0
commandmask = 0xf0
def __init__(self, funcid):
self.funcid = funcid
def set_from_data(self, data, offset):
self.funcid = data[offset] & 0x0f
def _check(self):
assert self.funcid in xrange(16)
def to_bin(self):
self._check()
return [self.command | (self.funcid & 0x0f)]
class GlobalAccess(ByteCodeCommand):
commandmask = 0xf8
nargs = 2 # or 1 or 0, will be overwritten by instances which know their exact mode
M2NARGSPOP = {0: (1, False), 1: (2, False), 2: (0, True), 3: (1, True), 4: (2, True)}
NARGSPOP2M = dict((v, k) for (k, v) in M2NARGSPOP.items())
def __init__(self, nargs, popoffset, address=None):
self.popoffset = popoffset
self.nargs = nargs
self.address = address # may be None if nargs == 0, in which case popoffset has to be true
def set_from_data(self, data, offset):
m = data[offset] & 0x07
self.nargs, self.popoffset = self.M2NARGSPOP[m]
if self.nargs == 0:
self.address = None
elif self.nargs == 1:
self.address = data[offset+1]
elif self.nargs == 2:
self.address = (data[offset+1] << 8) | data[offset+2]
def _check(self):
if self.nargs == 0:
assert self.address == None
assert self.popoffset
elif self.nargs == 1:
assert self.address in xrange(1<<8)
elif self.nargs == 2:
assert self.address in xrange(1<<16)
@classmethod
def check_match(cls, command):
return super(GlobalAccess, cls).check_match(command) and command & 0x7 in (0, 1, 2, 3, 4)
def to_bin(self):
self._check()
if self.nargs == 0:
return [self.command | self.NARGSPOP2M[self.nargs, self.popoffset]]
elif self.nargs == 1:
return [self.command | self.NARGSPOP2M[self.nargs, self.popoffset], self.address]
elif self.nargs == 2:
return [self.command | self.NARGSPOP2M[self.nargs, self.popoffset], self.address>>8, self.address%256]
class GlobalLoad(GlobalAccess): pass
class GlobalStore(GlobalAccess): pass
class GlobalU8(GlobalAccess): pass
class GlobalS8(GlobalAccess): pass
class Global16(GlobalAccess): pass
class GlobalLoadU8(GlobalLoad, GlobalU8): command = 0xc0
class GlobalStoreU8(GlobalStore, GlobalU8): command = 0xc8
class GlobalLoadS8(GlobalLoad, GlobalS8): command = 0xd0
class GlobalStoreS8(GlobalStore, GlobalS8): command = 0xd8
class GlobalLoad16(GlobalLoad, Global16): command = 0xe0
class GlobalStore16(GlobalStore, Global16): command = 0xe8
class StackAccess(ByteCodeCommand):
commandmask = 0xc7
def __init__(self, k):
self.k = k
def set_from_data(self, data, offset):
self.k = (data[offset] & 0x38) >> 3
def _check(self):
assert self.k in xrange(6)
@classmethod
def check_match(cls, command):
return super(StackAccess, cls).check_match(command) and command & 0x7 in (5, 6) and ((command & 0x38) >> 3) in xrange(6)
def to_bin(self):
self._check()
return [self.command | (self.k & 0x07) << 3]
class Bury(StackAccess): command = 0xc5
class Dig(StackAccess): command = 0xc6
class StackShoveling(ByteCodeCommand):
commandmask = 0xf8
def __init__(self, n):
self.n = n
def set_from_data(self, data, offset):
self.n = data[offset] & 0x07
def _check(self):
assert self.n in range(8)
def to_bin(self):
self._check()
return [self.command | (self.n & 0x07)]
class PushZeros(StackShoveling):
command = 0xf0
class PopMany(StackShoveling):
command = 0xf8
class Label(ByteCodeCommand):
"""Like a byte code, but results in null-length bytecode and can be used
for jump calculations"""
__instancecounter = 0
def __init__(self, descr=None, id=None, export=None):
if descr is not None:
self.descr = descr # for debugging purposes
type(self).__instancecounter += 1
self.id = id or "label%d"%self.__instancecounter
self.export = export
def to_bin(self):
return []
nargs = None
length = 0
def get_ref(self):
return self.LabelRef(self)
class LabelRef(object):
"""Reference to a label"""
def __init__(self, ref):
self.ref = ref
def __repr__(self):
# this is not how they are constructed, but they can't be constructed directly anyway
return "LabelRef(%r)"%self.ref.id
def interpret(commandbuffer, index):
command = commandbuffer[index]
candidates = []
for c in globals().values():
if type(c) == type and issubclass(c, ByteCodeCommand):
if c.check_match(command):
candidates.append(c)
if not candidates:
raise UnknownCommand(command)
if len(candidates) > 1:
raise Exception("Multiple matches for command %x"%command)
(commandclass, ) = candidates
return commandclass.from_bin(commandbuffer, index)
def test():
for c in range(256):
commandbuffer = [c, 233, 253]
try:
command = interpret(commandbuffer, 0)
except UnknownCommand:
print "Command %02x is unknown"%c
continue
r = repr(command)
print r
assert repr(eval(r)) == r
b = command.to_bin()
assert repr(interpret(b, 0)) == r
assert b == commandbuffer[:command.length]
if __name__ == "__main__":
test()