| 1 |
'''Toy Language with Cons Cells''' |
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import py |
| 5 |
from rpython.rlib.objectmodel import specialize, we_are_translated |
| 6 |
from rpython.jit.tl.tlopcode import * |
| 7 |
from rpython.jit.tl import tlopcode |
| 8 |
from rpython.rlib.jit import JitDriver, elidable |
| 9 |
from rpython.rlib.rarithmetic import is_valid_int |
| 10 |
|
| 11 |
|
| 12 |
class Obj(object): |
| 13 |
|
| 14 |
def t(self): raise TypeError |
| 15 |
|
| 16 |
def int_o(self): raise TypeError |
| 17 |
def to_string(self): raise TypeError |
| 18 |
|
| 19 |
def add(self, other): raise TypeError |
| 20 |
def sub(self, other): raise TypeError |
| 21 |
def mul(self, other): raise TypeError |
| 22 |
def div(self, other): raise TypeError |
| 23 |
|
| 24 |
def eq(self, other): raise TypeError |
| 25 |
def lt(self, other): raise TypeError |
| 26 |
|
| 27 |
def car(self): raise TypeError |
| 28 |
def cdr(self): raise TypeError |
| 29 |
|
| 30 |
def _concat(self, other): raise TypeError |
| 31 |
|
| 32 |
|
| 33 |
def getattr(self, name): raise TypeError |
| 34 |
def setattr(self, name, value): raise TypeError |
| 35 |
def send(self, name): raise TypeError |
| 36 |
|
| 37 |
|
| 38 |
class ClassDescr(object): |
| 39 |
|
| 40 |
def __init__(self, attributes, methods): |
| 41 |
self.attributes = attributes |
| 42 |
self.methods = methods |
| 43 |
|
| 44 |
def __eq__(self, other): |
| 45 |
"NOT_RPYTHON" |
| 46 |
return self.__dict__ == other.__dict__ |
| 47 |
|
| 48 |
class ConstantPool(object): |
| 49 |
|
| 50 |
def __init__(self): |
| 51 |
self.classdescrs = [] |
| 52 |
self.strings = [] |
| 53 |
|
| 54 |
def add_classdescr(self, attributes, methods): |
| 55 |
idx = len(self.classdescrs) |
| 56 |
descr = ClassDescr(attributes, methods) |
| 57 |
self.classdescrs.append(descr) |
| 58 |
return idx |
| 59 |
|
| 60 |
def add_string(self, s): |
| 61 |
try: |
| 62 |
return self.strings.index(s) |
| 63 |
except ValueError: |
| 64 |
idx = len(self.strings) |
| 65 |
self.strings.append(s) |
| 66 |
return idx |
| 67 |
|
| 68 |
def __eq__(self, other): |
| 69 |
"NOT_RPYTHON" |
| 70 |
return self.__dict__ == other.__dict__ |
| 71 |
|
| 72 |
class Class(object): |
| 73 |
|
| 74 |
classes = [] |
| 75 |
|
| 76 |
@elidable |
| 77 |
def get(key): |
| 78 |
for descr, cls in Class.classes: |
| 79 |
if key.attributes == descr.attributes and\ |
| 80 |
key.methods == descr.methods: |
| 81 |
return cls |
| 82 |
result = Class(key) |
| 83 |
Class.classes.append((key, result)) |
| 84 |
return result |
| 85 |
get = staticmethod(get) |
| 86 |
|
| 87 |
def __init__(self, descr): |
| 88 |
attributes = {} |
| 89 |
for name in descr.attributes: |
| 90 |
attributes[name] = len(attributes) |
| 91 |
self.attributes = attributes |
| 92 |
self.methods = {} |
| 93 |
for methname, pc in descr.methods: |
| 94 |
self.methods[methname] = pc |
| 95 |
|
| 96 |
class InstanceObj(Obj): |
| 97 |
|
| 98 |
def __init__(self, cls): |
| 99 |
self.cls = cls |
| 100 |
self.values = [nil] * len(cls.attributes) |
| 101 |
|
| 102 |
def getclass(self): |
| 103 |
return self.cls |
| 104 |
|
| 105 |
def to_string(self): |
| 106 |
return '<Object>' |
| 107 |
|
| 108 |
def t(self): |
| 109 |
return True |
| 110 |
|
| 111 |
def eq(self, other): |
| 112 |
return self is other |
| 113 |
|
| 114 |
def getattr(self, name): |
| 115 |
i = self.getclass().attributes[name] |
| 116 |
return self.values[i] |
| 117 |
|
| 118 |
def setattr(self, name, value): |
| 119 |
i = self.getclass().attributes[name] |
| 120 |
self.values[i] = value |
| 121 |
return value |
| 122 |
|
| 123 |
def send(self, name): |
| 124 |
return self.getclass().methods[name] |
| 125 |
|
| 126 |
|
| 127 |
class IntObj(Obj): |
| 128 |
|
| 129 |
def __init__(self, value): |
| 130 |
self.value = value |
| 131 |
|
| 132 |
def t(self): |
| 133 |
return bool(self.value) |
| 134 |
|
| 135 |
def int_o(self): |
| 136 |
return self.value |
| 137 |
|
| 138 |
def to_string(self): |
| 139 |
return str(self.value) |
| 140 |
|
| 141 |
def add(self, other): return IntObj(self.value + other.int_o()) |
| 142 |
def sub(self, other): return IntObj(self.value - other.int_o()) |
| 143 |
def mul(self, other): return IntObj(self.value * other.int_o()) |
| 144 |
def div(self, other): return IntObj(self.value // other.int_o()) |
| 145 |
|
| 146 |
def eq(self, other): |
| 147 |
return isinstance(other, IntObj) and self.value == other.value |
| 148 |
|
| 149 |
def lt(self, other): return self.value < other.int_o() |
| 150 |
|
| 151 |
zero = IntObj(0) |
| 152 |
|
| 153 |
class LispObj(Obj): |
| 154 |
|
| 155 |
def div(self, n): |
| 156 |
n = n.int_o() |
| 157 |
if n < 0: |
| 158 |
raise IndexError |
| 159 |
return self._nth(n) |
| 160 |
|
| 161 |
def add(self, other): |
| 162 |
if not isinstance(other, LispObj): |
| 163 |
raise TypeError |
| 164 |
return self._concat(other) |
| 165 |
|
| 166 |
class NilObj(LispObj): |
| 167 |
|
| 168 |
def to_string(self): |
| 169 |
return 'nil' |
| 170 |
|
| 171 |
def t(self): |
| 172 |
return False |
| 173 |
|
| 174 |
def eq(self, other): |
| 175 |
return self is other |
| 176 |
|
| 177 |
def _concat(self, other): |
| 178 |
return other |
| 179 |
|
| 180 |
def _nth(self, n): |
| 181 |
raise IndexError |
| 182 |
|
| 183 |
nil = NilObj() |
| 184 |
|
| 185 |
class ConsObj(LispObj): |
| 186 |
def __init__(self, car, cdr): |
| 187 |
self._car = car |
| 188 |
self._cdr = cdr |
| 189 |
|
| 190 |
def to_string(self): |
| 191 |
return '<ConsObj>' |
| 192 |
|
| 193 |
def t(self): |
| 194 |
return True |
| 195 |
|
| 196 |
def eq(self, other): |
| 197 |
return (isinstance(other, ConsObj) and |
| 198 |
self._car.eq(other._car) and self._cdr.eq(other._cdr)) |
| 199 |
|
| 200 |
def car(self): |
| 201 |
return self._car |
| 202 |
|
| 203 |
def cdr(self): |
| 204 |
return self._cdr |
| 205 |
|
| 206 |
def _concat(self, other): |
| 207 |
return ConsObj(self._car, self._cdr._concat(other)) |
| 208 |
|
| 209 |
def _nth(self, n): |
| 210 |
if n == 0: |
| 211 |
return self._car |
| 212 |
else: |
| 213 |
return self._cdr._nth(n-1) |
| 214 |
|
| 215 |
def char2int(c): |
| 216 |
t = ord(c) |
| 217 |
if t & 128: |
| 218 |
t = -(-ord(c) & 0xff) |
| 219 |
return t |
| 220 |
|
| 221 |
class Frame(object): |
| 222 |
|
| 223 |
def __init__(self, args, pc): |
| 224 |
assert is_valid_int(pc) |
| 225 |
self.args = args |
| 226 |
self.pc = pc |
| 227 |
self.stack = [] |
| 228 |
|
| 229 |
|
| 230 |
def make_interp(supports_call, jitted=True): |
| 231 |
myjitdriver = JitDriver(greens = ['pc', 'code'], |
| 232 |
reds = ['frame', 'pool']) |
| 233 |
|
| 234 |
def interp(code='', pc=0, inputarg=0, pool=None): |
| 235 |
if not isinstance(code,str): |
| 236 |
raise TypeError("code '%s' should be a string" % str(code)) |
| 237 |
|
| 238 |
if pool is None: |
| 239 |
pool = ConstantPool() |
| 240 |
args = [IntObj(inputarg)] |
| 241 |
return interp_eval(code, pc, args, pool).int_o() |
| 242 |
|
| 243 |
def interp_eval(code, pc, args, pool): |
| 244 |
assert is_valid_int(pc) |
| 245 |
frame = Frame(args, pc) |
| 246 |
pc = frame.pc |
| 247 |
|
| 248 |
while pc < len(code): |
| 249 |
if jitted: |
| 250 |
myjitdriver.jit_merge_point(frame=frame, |
| 251 |
code=code, pc=pc, pool=pool) |
| 252 |
opcode = ord(code[pc]) |
| 253 |
pc += 1 |
| 254 |
stack = frame.stack |
| 255 |
|
| 256 |
if opcode == NOP: |
| 257 |
pass |
| 258 |
|
| 259 |
elif opcode == NIL: |
| 260 |
stack.append(nil) |
| 261 |
|
| 262 |
elif opcode == CONS: |
| 263 |
car, cdr = stack.pop(), stack.pop() |
| 264 |
stack.append(ConsObj(car, cdr)) |
| 265 |
|
| 266 |
elif opcode == CAR: |
| 267 |
stack.append(stack.pop().car()) |
| 268 |
|
| 269 |
elif opcode == CDR: |
| 270 |
stack.append(stack.pop().cdr()) |
| 271 |
|
| 272 |
elif opcode == PUSH: |
| 273 |
stack.append(IntObj(char2int(code[pc]))) |
| 274 |
pc += 1 |
| 275 |
|
| 276 |
elif opcode == POP: |
| 277 |
stack.pop() |
| 278 |
|
| 279 |
elif opcode == SWAP: |
| 280 |
a, b = stack.pop(), stack.pop() |
| 281 |
stack.append(a) |
| 282 |
stack.append(b) |
| 283 |
|
| 284 |
elif opcode == ROLL: |
| 285 |
r = char2int(code[pc]) |
| 286 |
if r < -1: |
| 287 |
i = len(stack) + r |
| 288 |
if i < 0: |
| 289 |
raise IndexError |
| 290 |
stack.insert( i, stack.pop() ) |
| 291 |
elif r > 1: |
| 292 |
i = len(stack) - r |
| 293 |
if i < 0: |
| 294 |
raise IndexError |
| 295 |
stack.append(stack.pop(i)) |
| 296 |
|
| 297 |
pc += 1 |
| 298 |
|
| 299 |
elif opcode == PICK: |
| 300 |
stack.append( stack[-1 - char2int(code[pc])] ) |
| 301 |
pc += 1 |
| 302 |
|
| 303 |
elif opcode == PUT: |
| 304 |
stack[-1 - char2int(code[pc])] = stack.pop() |
| 305 |
pc += 1 |
| 306 |
|
| 307 |
elif opcode == ADD: |
| 308 |
a, b = stack.pop(), stack.pop() |
| 309 |
stack.append(b.add(a)) |
| 310 |
|
| 311 |
elif opcode == SUB: |
| 312 |
a, b = stack.pop(), stack.pop() |
| 313 |
stack.append(b.sub(a)) |
| 314 |
|
| 315 |
elif opcode == MUL: |
| 316 |
a, b = stack.pop(), stack.pop() |
| 317 |
stack.append(b.mul(a)) |
| 318 |
|
| 319 |
elif opcode == DIV: |
| 320 |
a, b = stack.pop(), stack.pop() |
| 321 |
stack.append(b.div(a)) |
| 322 |
|
| 323 |
elif opcode == EQ: |
| 324 |
a, b = stack.pop(), stack.pop() |
| 325 |
stack.append(IntObj(b.eq(a))) |
| 326 |
|
| 327 |
elif opcode == NE: |
| 328 |
a, b = stack.pop(), stack.pop() |
| 329 |
stack.append(IntObj(not b.eq(a))) |
| 330 |
|
| 331 |
elif opcode == LT: |
| 332 |
a, b = stack.pop(), stack.pop() |
| 333 |
stack.append(IntObj(b.lt(a))) |
| 334 |
|
| 335 |
elif opcode == LE: |
| 336 |
a, b = stack.pop(), stack.pop() |
| 337 |
stack.append(IntObj(not a.lt(b))) |
| 338 |
|
| 339 |
elif opcode == GT: |
| 340 |
a, b = stack.pop(), stack.pop() |
| 341 |
stack.append(IntObj(a.lt(b))) |
| 342 |
|
| 343 |
elif opcode == GE: |
| 344 |
a, b = stack.pop(), stack.pop() |
| 345 |
stack.append(IntObj(not b.lt(a))) |
| 346 |
|
| 347 |
elif opcode == BR: |
| 348 |
old_pc = pc |
| 349 |
pc += char2int(code[pc]) |
| 350 |
pc += 1 |
| 351 |
if jitted and old_pc > pc: |
| 352 |
myjitdriver.can_enter_jit(code=code, pc=pc, frame=frame, |
| 353 |
pool=pool) |
| 354 |
|
| 355 |
elif opcode == BR_COND: |
| 356 |
cond = stack.pop() |
| 357 |
if cond.t(): |
| 358 |
old_pc = pc |
| 359 |
pc += char2int(code[pc]) + 1 |
| 360 |
if jitted and old_pc > pc: |
| 361 |
myjitdriver.can_enter_jit(code=code, pc=pc, frame=frame, |
| 362 |
pool=pool) |
| 363 |
else: |
| 364 |
pc += 1 |
| 365 |
|
| 366 |
elif opcode == BR_COND_STK: |
| 367 |
offset = stack.pop().int_o() |
| 368 |
if stack.pop().t(): |
| 369 |
old_pc = pc |
| 370 |
pc += offset |
| 371 |
if jitted and old_pc > pc: |
| 372 |
myjitdriver.can_enter_jit(code=code, pc=pc, frame=frame, |
| 373 |
pool=pool) |
| 374 |
|
| 375 |
|
| 376 |
elif supports_call and opcode == CALL: |
| 377 |
offset = char2int(code[pc]) |
| 378 |
pc += 1 |
| 379 |
res = interp_eval(code, pc + offset, [zero], pool) |
| 380 |
if res: |
| 381 |
stack.append(res) |
| 382 |
|
| 383 |
elif opcode == RETURN: |
| 384 |
break |
| 385 |
|
| 386 |
elif opcode == PUSHARG: |
| 387 |
stack.append(frame.args[0]) |
| 388 |
|
| 389 |
elif opcode == PUSHARGN: |
| 390 |
idx = char2int(code[pc]) |
| 391 |
pc += 1 |
| 392 |
stack.append(frame.args[idx]) |
| 393 |
|
| 394 |
elif opcode == NEW: |
| 395 |
idx = char2int(code[pc]) |
| 396 |
pc += 1 |
| 397 |
descr = pool.classdescrs[idx] |
| 398 |
cls = Class.get(descr) |
| 399 |
stack.append(InstanceObj(cls)) |
| 400 |
|
| 401 |
elif opcode == GETATTR: |
| 402 |
idx = char2int(code[pc]) |
| 403 |
pc += 1 |
| 404 |
name = pool.strings[idx] |
| 405 |
a = stack.pop() |
| 406 |
stack.append(a.getattr(name)) |
| 407 |
|
| 408 |
elif opcode == SETATTR: |
| 409 |
idx = char2int(code[pc]) |
| 410 |
pc += 1 |
| 411 |
name = pool.strings[idx] |
| 412 |
a, b = stack.pop(), stack.pop() |
| 413 |
b.setattr(name, a) |
| 414 |
|
| 415 |
elif supports_call and opcode == SEND: |
| 416 |
idx = char2int(code[pc]) |
| 417 |
pc += 1 |
| 418 |
num_args = char2int(code[pc]) |
| 419 |
pc += 1 |
| 420 |
num_args += 1 |
| 421 |
name = pool.strings[idx] |
| 422 |
meth_args = [None] * num_args |
| 423 |
while num_args > 0: |
| 424 |
num_args -= 1 |
| 425 |
meth_args[num_args] = stack.pop() |
| 426 |
a = meth_args[0] |
| 427 |
meth_pc = a.send(name) |
| 428 |
res = interp_eval(code, meth_pc, meth_args, pool) |
| 429 |
if res: |
| 430 |
stack.append(res) |
| 431 |
|
| 432 |
elif opcode == PRINT: |
| 433 |
if not we_are_translated(): |
| 434 |
a = stack.pop() |
| 435 |
print(a.to_string()) |
| 436 |
|
| 437 |
elif opcode == DUMP: |
| 438 |
if not we_are_translated(): |
| 439 |
parts = [] |
| 440 |
for obj in stack: |
| 441 |
parts.append(obj.to_string()) |
| 442 |
print('[%s]' % ', '.join(parts)) |
| 443 |
|
| 444 |
else: |
| 445 |
raise RuntimeError("unknown opcode: " + str(opcode)) |
| 446 |
|
| 447 |
if frame.stack: |
| 448 |
return frame.stack[-1] |
| 449 |
else: |
| 450 |
return None |
| 451 |
|
| 452 |
return interp, interp_eval |
| 453 |
|
| 454 |
|
| 455 |
interp , interp_eval = make_interp(supports_call = True) |
| 456 |
interp_without_call, interp_eval_without_call = make_interp(supports_call = False) |
| 457 |
interp_nonjit , interp_eval_nonjit = make_interp(supports_call = True, jitted=False) |
| 458 |
|
| 459 |
def non_constant(bytecode, pool): |
| 460 |
from rpython.rlib.nonconst import NonConstant |
| 461 |
if NonConstant(False): |
| 462 |
pool = ConstantPool() |
| 463 |
pool.add_string("foo") |
| 464 |
pool.add_string("bazz") |
| 465 |
pool.add_classdescr(["a", "bc"], [("foo", 3), ("y", 5)]) |
| 466 |
return "123", pool |
| 467 |
else: |
| 468 |
return bytecode, pool |
| 469 |
|
| 470 |
if __name__ == '__main__': |
| 471 |
import sys |
| 472 |
from rpython.jit.tl.test.test_tl import FACTORIAL_SOURCE |
| 473 |
if len(sys.argv) == 1: |
| 474 |
src = FACTORIAL_SOURCE |
| 475 |
elif len(sys.argv) == 2: |
| 476 |
src = file(sys.argv[1]).read() |
| 477 |
else: |
| 478 |
print('Usage: python tlc.py [sourcefile]', file=sys.stderr) |
| 479 |
sys.exit(2) |
| 480 |
|
| 481 |
pool = ConstantPool() |
| 482 |
bytecode = compile(src, pool) |
| 483 |
sys.stdout.write(serialize_program(bytecode, pool)) |