def _get_main(self): from pypy.jit.tl.tl import interp_without_call from pypy.jit.tl.tlopcode import compile code = compile(''' PUSH 1 # accumulator PUSH 7 # N start: PICK 0 PUSH 1 LE BR_COND exit SWAP PICK 1 MUL SWAP PUSH 1 SUB PUSH 1 BR_COND start exit: POP RETURN ''') code2 = compile(''' PUSHARG start: PUSH 1 SUB PICK 0 PUSH 1 LE BR_COND exit PUSH 1 BR_COND start exit: RETURN ''') codes = [code, code2] def main(n, inputarg): code = codes[n] return interp_without_call(code, inputarg=inputarg) return main
def test_new_obj(self): from pypy.jit.tl.tlc import interp_eval, InstanceObj, nil pool = ConstantPool() bytecode = compile(""" NEW foo,bar """, pool) obj = interp_eval(bytecode, 0, [nil], pool) assert isinstance(obj, InstanceObj) assert len(obj.values) == 2 assert sorted(obj.cls.attributes.keys()) == ['bar', 'foo']
def test_concat_errors(self): bytecode = compile(""" NIL PUSH 4 ADD """) py.test.raises(TypeError, self.interp, bytecode, 0, 0) bytecode = compile(""" PUSH 4 NIL ADD """) py.test.raises(TypeError, self.interp, bytecode, 0, 0) bytecode = compile(""" NIL PUSH 1 CONS PUSH 4 ADD """) py.test.raises(TypeError, self.interp, bytecode, 0, 0) bytecode = compile(""" PUSH 4 NIL PUSH 1 CONS ADD """) py.test.raises(TypeError, self.interp, bytecode, 0, 0) bytecode = compile(""" PUSH 2 PUSH 1 CONS NIL ADD """) py.test.raises(TypeError, self.interp, bytecode, 0, 0)
def test_obj_equality(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" NEW foo,bar NEW foo,bar EQ """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 0
def compile(self, filename): from pypy.jit.tl.tlc import interp_eval, IntObj pool = ConstantPool() path = py.path.local(__file__).join(filename) src = path.read() bytecode = compile(src, pool) def fn(n): obj = IntObj(n) res = interp_eval(bytecode, 0, [obj], pool) return res.int_o() return fn
def test_obj_equality(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ NEW foo,bar NEW foo,bar EQ """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 0
def test_unconditional_branch(self): bytecode = compile(""" main: BR target PUSH 123 RETURN target: PUSH 42 RETURN """) res = self.interp(bytecode, 0, 0) assert res == 42
def test_setattr(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" NEW foo,bar PICK 0 PUSH 42 SETATTR foo """, pool) obj = interp_eval(bytecode, 0, [nil], pool) assert obj.values[0].int_o() == 42 assert obj.values[1] is nil
def test_getattr(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" NEW foo,bar PICK 0 PUSH 42 SETATTR bar GETATTR bar """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_call_without_return_value(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" CALL foo PUSH 42 RETURN foo: RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_call_without_return_value(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ CALL foo PUSH 42 RETURN foo: RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_getattr(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ NEW foo,bar PICK 0 PUSH 42 SETATTR bar GETATTR bar """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_basic_cons_cell(self): bytecode = compile(""" NIL PUSHARG CONS PUSH 1 CONS CDR CAR """) res = self.interp(bytecode, 0, 42) assert res == 42
def test_setattr(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ NEW foo,bar PICK 0 PUSH 42 SETATTR foo """, pool) obj = interp_eval(bytecode, 0, [nil], pool) assert obj.values[0].int_o() == 42 assert obj.values[1] is nil
def test_constant_pool(): pool = ConstantPool() bytecode = compile(""" NEW foo,bar,meth=f f: RETURN """, pool) expected = test_tl.list2bytecode([NEW, 0, RETURN]) assert expected == bytecode assert len(pool.classdescrs) == 1 descr = pool.classdescrs[0] assert descr.attributes == ['foo', 'bar'] assert descr.methods == [('meth', 2)]
def test_serialization(): from pypy.jit.tl.tlopcode import serialize_program, decode_program pool = ConstantPool() bytecode = compile(""" NEW foo,bar,meth=f SETATTR foobar f: RETURN """, pool) s = serialize_program(bytecode, pool) bytecode2, pool2 = decode_program(s) assert bytecode == bytecode2 assert pool == pool2
def test_serialization(): from pypy.jit.tl.tlopcode import serialize_program, decode_program pool = ConstantPool() bytecode = compile( """ NEW foo,bar,meth=f SETATTR foobar f: RETURN """, pool) s = serialize_program(bytecode, pool) bytecode2, pool2 = decode_program(s) assert bytecode == bytecode2 assert pool == pool2
def test_constant_pool(): pool = ConstantPool() bytecode = compile( """ NEW foo,bar,meth=f f: RETURN """, pool) expected = test_tl.list2bytecode([NEW, 0, RETURN]) assert expected == bytecode assert len(pool.classdescrs) == 1 descr = pool.classdescrs[0] assert descr.attributes == ['foo', 'bar'] assert descr.methods == [('meth', 2)]
def test_obj_truth(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" NEW foo,bar BR_COND true PUSH 12 PUSH 1 BR_COND exit true: PUSH 42 exit: RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_method(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile(""" NEW foo,meth=meth PICK 0 PUSH 42 SETATTR foo SEND meth/0 RETURN meth: PUSHARG GETATTR foo RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_obj_truth(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ NEW foo,bar BR_COND true PUSH 12 PUSH 1 BR_COND exit true: PUSH 42 exit: RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_method(self): from pypy.jit.tl.tlc import interp_eval, nil pool = ConstantPool() bytecode = compile( """ NEW foo,meth=meth PICK 0 PUSH 42 SETATTR foo SEND meth/0 RETURN meth: PUSHARG GETATTR foo RETURN """, pool) res = interp_eval(bytecode, 0, [nil], pool) assert res.int_o() == 42
def test_tl_call(self, listops=True, policy=None): from pypy.jit.tl.tl import interp from pypy.jit.tl.tlopcode import compile from pypy.jit.metainterp import simple_optimize code = compile(''' PUSHARG start: PUSH 1 SUB PICK 0 PUSH 0 CALL func POP GT BR_COND start exit: RETURN func: PUSH 0 inside: PUSH 1 ADD PICK 0 PUSH 5 LE BR_COND inside PUSH 1 RETURN ''') assert interp(code, inputarg=100) == 0 codes = [code, ''] def main(num, arg): return interp(codes[num], inputarg=arg) res = self.meta_interp(main, [0, 20], optimizer=OPTIMIZER_SIMPLE, listops=listops, backendopt=True, policy=policy) assert res == 0
def test_nth(self): bytecode = compile(""" NIL PUSH 4 CONS PUSH 2 CONS PUSH 1 CONS PUSHARG DIV """) res = self.interp(bytecode, 0, 0) assert res == 1 res = self.interp(bytecode, 0, 1) assert res == 2 res = self.interp(bytecode, 0, 2) assert res == 4 py.test.raises(IndexError, self.interp, bytecode, 0, 3)
def test_concat(self): bytecode = compile(""" NIL PUSH 4 CONS PUSH 2 CONS NIL PUSH 5 CONS PUSH 3 CONS PUSH 1 CONS ADD PUSHARG DIV """) for i, n in enumerate([2, 4, 1, 3, 5]): res = self.interp(bytecode, 0, i) assert res == n