Пример #1
0
    def _get_main(self):
        from rpython.jit.tl.tl import interp_without_call
        from rpython.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
Пример #2
0
    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)
Пример #3
0
 def test_new_obj(self):
     from rpython.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']
Пример #4
0
 def test_obj_equality(self):
     from rpython.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
Пример #5
0
 def compile(self, filename):
     from rpython.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
Пример #6
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
Пример #7
0
 def test_getattr(self):
     from rpython.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
Пример #8
0
 def test_call_without_return_value(self):
     from rpython.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
Пример #9
0
 def test_setattr(self):
     from rpython.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
Пример #10
0
    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
Пример #11
0
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)]
Пример #12
0
def test_serialization():
    from rpython.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
Пример #13
0
 def test_obj_truth(self):
     from rpython.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
Пример #14
0
 def test_method(self):
     from rpython.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
Пример #15
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)
Пример #16
0
    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
Пример #17
0
    def test_tl_call(self, listops=True, policy=None):
        from rpython.jit.tl.tl import interp
        from rpython.jit.tl.tlopcode import compile

        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], enable_opts='',
                               listops=listops, backendopt=True, policy=policy)
        assert res == 0