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
|
names = {}
def opcode(n, opcode_name):
global opcode_names
names[opcode_name] = globals()[opcode_name] = n
# basic tl opcodes:
opcode(1, "NOP")
opcode(2, "PUSH") #1 operand
opcode(3, "POP")
opcode(4, "SWAP")
opcode(5, "ROLL")
opcode(6, "PICK") #1 operand (DUP = PICK,0)
opcode(7, "PUT") #1 operand
opcode(8, "ADD")
opcode(9, "SUB")
opcode(10, "MUL")
opcode(11, "DIV")
opcode(12, "EQ")
opcode(13, "NE")
opcode(14, "LT")
opcode(15, "LE")
opcode(16, "GT")
opcode(17, "GE")
opcode(18, "BR_COND") #1 operand offset
opcode(19, "BR_COND_STK") # no operand, takes [condition, offset] from the stack
opcode(20, "CALL") #1 operand offset
opcode(21, "RETURN")
opcode(22, "PUSHARG")
opcode(23, "INVALID")
# tl with cons cells and boxed values opcodes
opcode(24, "NIL")
opcode(25, "CONS")
opcode(26, "CAR")
opcode(27, "CDR")
# object oriented features of tlc
opcode(28, "NEW")
opcode(29, "GETATTR")
opcode(30, "SETATTR")
opcode(31, "SEND")
opcode(32, "PUSHARGN")
opcode(33, "PRINT")
opcode(34, "DUMP")
opcode(35, "BR")
del opcode
def compile(code='', pool=None):
bytecode = []
labels = {} #[key] = pc
label_usage = [] #(name, pc)
method_usage = [] #[methods]
for s in code.split('\n'):
for comment in '; # //'.split():
s = s.split(comment, 1)[0]
s = s.strip()
if not s:
continue
t = s.split()
if t[0].endswith(':'):
assert ',' not in t[0]
labels[ t[0][:-1] ] = len(bytecode)
continue
bytecode.append(names[t[0]])
if len(t) > 1:
arg = t[1]
try:
bytecode.append( int(arg) )
except ValueError:
if t[0] == 'NEW':
# it's a class descr
items = arg.split(',')
items = [x.strip() for x in items if x]
attributes = []
methods = []
for item in items:
if '=' in item:
methname, label = item.split('=')
methods.append((methname, label))
else:
attributes.append(item)
assert pool is not None
idx = pool.add_classdescr(attributes, methods)
method_usage.append(methods)
bytecode.append(idx)
elif t[0] in ('GETATTR', 'SETATTR'):
# it's a string
idx = pool.add_string(arg)
bytecode.append(idx)
elif t[0] == 'SEND':
# 'methodname/num_args'
methname, num_args = arg.split('/')
idx = pool.add_string(methname)
bytecode.append(idx)
bytecode.append(int(num_args))
else:
# it's a label
label_usage.append( (arg, len(bytecode)) )
bytecode.append( 0 )
for label, pc in label_usage:
offset = labels[label] - pc - 1
assert -128 <= offset <= 127
bytecode[pc] = offset
for methods in method_usage:
for i, (methname, label) in enumerate(methods):
pc = labels[label]
methods[i] = (methname, pc)
return ''.join([chr(i & 0xff) for i in bytecode])
def decode_descr(encdescr):
from rpython.jit.tl.tlc import ClassDescr
items = encdescr.split(',')
attributes = []
methods = []
for item in items:
if '=' in item:
methname, pc = item.split('=')
methods.append((methname, int(pc)))
else:
attributes.append(item)
return ClassDescr(attributes, methods)
def decode_pool(encpool):
"""
encpool is encoded in this way:
attr1,attr2,foo=3|attr1,bar=5|...
attr1,attr2,foo,bar,hello,world,...
"""
from rpython.jit.tl.tlc import ConstantPool
if encpool == '':
return None
lines = encpool.split('\n')
assert len(lines) == 2
encdescrs = lines[0].split('|')
classdescrs = [decode_descr(enc) for enc in encdescrs]
strings = lines[1].split(',')
pool = ConstantPool()
pool.classdescrs = classdescrs
pool.strings = strings
return pool
def serialize_descr(descr):
parts = []
parts += descr.attributes
parts += ['%s=%s' % item for item in descr.methods]
return ','.join(parts)
def serialize_pool(pool):
if pool is None:
return ''
encdescrs = '|'.join([serialize_descr(descr) for descr in pool.classdescrs])
encstrings = ','.join(pool.strings)
return '%s\n%s' % (encdescrs, encstrings)
def serialize_program(bytecode, pool):
poolcode = serialize_pool(pool)
return '%s\n%s' % (poolcode, bytecode)
def decode_program(s):
idx1 = s.find('\n')
assert idx1 >= 0
idx2 = s.find('\n', idx1+1)
assert idx2 >= 0
poolcode = s[:idx2]
bytecode = s[idx2+1:] # remove the 2nd newline
pool = decode_pool(poolcode)
return bytecode, pool
|