| 1 |
names = {} |
| 2 |
|
| 3 |
def opcode(n, opcode_name): |
| 4 |
global opcode_names |
| 5 |
names[opcode_name] = globals()[opcode_name] = n |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
opcode(1, "NOP") |
| 10 |
opcode(2, "PUSH") |
| 11 |
opcode(3, "POP") |
| 12 |
opcode(4, "SWAP") |
| 13 |
opcode(5, "ROLL") |
| 14 |
|
| 15 |
opcode(6, "PICK") |
| 16 |
opcode(7, "PUT") |
| 17 |
|
| 18 |
opcode(8, "ADD") |
| 19 |
opcode(9, "SUB") |
| 20 |
opcode(10, "MUL") |
| 21 |
opcode(11, "DIV") |
| 22 |
|
| 23 |
opcode(12, "EQ") |
| 24 |
opcode(13, "NE") |
| 25 |
opcode(14, "LT") |
| 26 |
opcode(15, "LE") |
| 27 |
opcode(16, "GT") |
| 28 |
opcode(17, "GE") |
| 29 |
|
| 30 |
opcode(18, "BR_COND") |
| 31 |
opcode(19, "BR_COND_STK") |
| 32 |
|
| 33 |
opcode(20, "CALL") |
| 34 |
opcode(21, "RETURN") |
| 35 |
|
| 36 |
opcode(22, "PUSHARG") |
| 37 |
|
| 38 |
opcode(23, "INVALID") |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
opcode(24, "NIL") |
| 43 |
opcode(25, "CONS") |
| 44 |
opcode(26, "CAR") |
| 45 |
opcode(27, "CDR") |
| 46 |
|
| 47 |
|
| 48 |
opcode(28, "NEW") |
| 49 |
opcode(29, "GETATTR") |
| 50 |
opcode(30, "SETATTR") |
| 51 |
opcode(31, "SEND") |
| 52 |
opcode(32, "PUSHARGN") |
| 53 |
|
| 54 |
opcode(33, "PRINT") |
| 55 |
opcode(34, "DUMP") |
| 56 |
opcode(35, "BR") |
| 57 |
|
| 58 |
del opcode |
| 59 |
|
| 60 |
|
| 61 |
def compile(code='', pool=None): |
| 62 |
bytecode = [] |
| 63 |
labels = {} |
| 64 |
label_usage = [] |
| 65 |
method_usage = [] |
| 66 |
for s in code.split('\n'): |
| 67 |
for comment in '; # //'.split(): |
| 68 |
s = s.split(comment, 1)[0] |
| 69 |
s = s.strip() |
| 70 |
if not s: |
| 71 |
continue |
| 72 |
t = s.split() |
| 73 |
if t[0].endswith(':'): |
| 74 |
assert ',' not in t[0] |
| 75 |
labels[ t[0][:-1] ] = len(bytecode) |
| 76 |
continue |
| 77 |
bytecode.append(names[t[0]]) |
| 78 |
if len(t) > 1: |
| 79 |
arg = t[1] |
| 80 |
try: |
| 81 |
bytecode.append( int(arg) ) |
| 82 |
except ValueError: |
| 83 |
if t[0] == 'NEW': |
| 84 |
|
| 85 |
items = arg.split(',') |
| 86 |
items = [x.strip() for x in items if x] |
| 87 |
attributes = [] |
| 88 |
methods = [] |
| 89 |
for item in items: |
| 90 |
if '=' in item: |
| 91 |
methname, label = item.split('=') |
| 92 |
methods.append((methname, label)) |
| 93 |
else: |
| 94 |
attributes.append(item) |
| 95 |
assert pool is not None |
| 96 |
idx = pool.add_classdescr(attributes, methods) |
| 97 |
method_usage.append(methods) |
| 98 |
bytecode.append(idx) |
| 99 |
elif t[0] in ('GETATTR', 'SETATTR'): |
| 100 |
|
| 101 |
idx = pool.add_string(arg) |
| 102 |
bytecode.append(idx) |
| 103 |
elif t[0] == 'SEND': |
| 104 |
|
| 105 |
methname, num_args = arg.split('/') |
| 106 |
idx = pool.add_string(methname) |
| 107 |
bytecode.append(idx) |
| 108 |
bytecode.append(int(num_args)) |
| 109 |
else: |
| 110 |
|
| 111 |
label_usage.append( (arg, len(bytecode)) ) |
| 112 |
bytecode.append( 0 ) |
| 113 |
for label, pc in label_usage: |
| 114 |
offset = labels[label] - pc - 1 |
| 115 |
assert -128 <= offset <= 127 |
| 116 |
bytecode[pc] = offset |
| 117 |
for methods in method_usage: |
| 118 |
for i, (methname, label) in enumerate(methods): |
| 119 |
pc = labels[label] |
| 120 |
methods[i] = (methname, pc) |
| 121 |
return ''.join([chr(i & 0xff) for i in bytecode]) |
| 122 |
|
| 123 |
|
| 124 |
def decode_descr(encdescr): |
| 125 |
from rpython.jit.tl.tlc import ClassDescr |
| 126 |
items = encdescr.split(',') |
| 127 |
attributes = [] |
| 128 |
methods = [] |
| 129 |
for item in items: |
| 130 |
if '=' in item: |
| 131 |
methname, pc = item.split('=') |
| 132 |
methods.append((methname, int(pc))) |
| 133 |
else: |
| 134 |
attributes.append(item) |
| 135 |
return ClassDescr(attributes, methods) |
| 136 |
|
| 137 |
def decode_pool(encpool): |
| 138 |
""" |
| 139 |
encpool is encoded in this way: |
| 140 |
|
| 141 |
attr1,attr2,foo=3|attr1,bar=5|... |
| 142 |
attr1,attr2,foo,bar,hello,world,... |
| 143 |
""" |
| 144 |
from rpython.jit.tl.tlc import ConstantPool |
| 145 |
if encpool == '': |
| 146 |
return None |
| 147 |
lines = encpool.split('\n') |
| 148 |
assert len(lines) == 2 |
| 149 |
encdescrs = lines[0].split('|') |
| 150 |
classdescrs = [decode_descr(enc) for enc in encdescrs] |
| 151 |
strings = lines[1].split(',') |
| 152 |
pool = ConstantPool() |
| 153 |
pool.classdescrs = classdescrs |
| 154 |
pool.strings = strings |
| 155 |
return pool |
| 156 |
|
| 157 |
def serialize_descr(descr): |
| 158 |
parts = [] |
| 159 |
parts += descr.attributes |
| 160 |
parts += ['%s=%s' % item for item in descr.methods] |
| 161 |
return ','.join(parts) |
| 162 |
|
| 163 |
def serialize_pool(pool): |
| 164 |
if pool is None: |
| 165 |
return '' |
| 166 |
encdescrs = '|'.join([serialize_descr(descr) for descr in pool.classdescrs]) |
| 167 |
encstrings = ','.join(pool.strings) |
| 168 |
return '%s\n%s' % (encdescrs, encstrings) |
| 169 |
|
| 170 |
def serialize_program(bytecode, pool): |
| 171 |
poolcode = serialize_pool(pool) |
| 172 |
return '%s\n%s' % (poolcode, bytecode) |
| 173 |
|
| 174 |
def decode_program(s): |
| 175 |
idx1 = s.find('\n') |
| 176 |
assert idx1 >= 0 |
| 177 |
idx2 = s.find('\n', idx1+1) |
| 178 |
assert idx2 >= 0 |
| 179 |
poolcode = s[:idx2] |
| 180 |
bytecode = s[idx2+1:] |
| 181 |
pool = decode_pool(poolcode) |
| 182 |
return bytecode, pool |