[pypy-svn] r64032 - in pypy/branch/pyjitpl5-simplify/pypy/jit/backend: minimal minimal/test test

arigo at codespeak.net arigo at codespeak.net
Mon Apr 13 19:08:39 CEST 2009


Author: arigo
Date: Mon Apr 13 19:08:39 2009
New Revision: 64032

Added:
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/   (props changed)
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/__init__.py   (contents, props changed)
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/runner.py   (contents, props changed)
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/   (props changed)
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/__init__.py   (contents, props changed)
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/test_runner.py
      - copied, changed from r64026, pypy/branch/pyjitpl5-simplify/pypy/jit/backend/x86/test/test_runner.py
Modified:
   pypy/branch/pyjitpl5-simplify/pypy/jit/backend/test/runner.py
Log:
Start another backend, the "minimal" backend, meant to be fully
translated but just working by interpretation.


Added: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/__init__.py
==============================================================================

Added: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/runner.py
==============================================================================
--- (empty file)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/runner.py	Mon Apr 13 19:08:39 2009
@@ -0,0 +1,326 @@
+import py
+from pypy.rlib.objectmodel import specialize
+from pypy.rpython.lltypesystem import lltype, llmemory, rffi, rstr, rclass
+from pypy.jit.metainterp.history import AbstractDescr, Box, BoxInt, BoxPtr
+from pypy.jit.metainterp import executor
+from pypy.jit.metainterp.resoperation import rop
+
+
+class CPU(object):
+
+    def __init__(self, rtyper, stats, translate_support_code=False,
+                 mixlevelann=None):
+        self.rtyper = rtyper
+        self.stats = stats
+        self.translate_support_code = translate_support_code
+        self.setup()
+
+    def setup(self):
+        if self.rtyper is not None:   # normal case
+            bk = self.rtyper.annotator.bookkeeper
+            clsdef = bk.getuniqueclassdef(OverflowError)
+            ovferror_repr = rclass.getclassrepr(self.rtyper, clsdef)
+            ll_inst = self.rtyper.exceptiondata.get_standard_ll_exc_instance(
+                self.rtyper, clsdef)
+        else:
+            # for tests, a random emulated ll_inst will do
+            ll_inst = lltype.malloc(rclass.OBJECT)
+            ll_inst.typeptr = lltype.malloc(rclass.OBJECT_VTABLE,
+                                            immortal=True)
+        self._ovf_error_vtable = ll_inst.typeptr
+        self._ovf_error_inst   = ll_inst
+
+    def compile_operations(self, loop):
+        pass
+
+    def execute_operations(self, loop, valueboxes):
+        self.clear_exception()
+        self._guard_failed = False
+        while True:
+            env = {}
+            assert len(valueboxes) == len(loop.inputargs)
+            for i in range(len(valueboxes)):
+                env[loop.inputargs[i]] = valueboxes[i]
+            operations = loop.operations
+            i = 0
+            #
+            while True:
+                assert i < len(operations), ("reached the end without "
+                                             "seeing a final op")
+                op = operations[i]
+                i += 1
+                argboxes = []
+                for box in op.args:
+                    if isinstance(box, Box):
+                        box = env[box]
+                    argboxes.append(box)
+                if op.is_final():
+                    break
+                if op.is_guard():
+                    try:
+                        resbox = self.execute_guard(op.opnum, argboxes)
+                    except GuardFailed:
+                        self._guard_failed = True
+                        operations = op.suboperations
+                        i = 0
+                        continue
+                else:
+                    resbox = executor._execute_nonspec(self, op.opnum,
+                                                       argboxes,
+                                                       op.descr)
+                if op.result is not None:
+                    assert resbox is not None
+                    env[op.result] = resbox
+                else:
+                    assert resbox is None
+            #
+            if op.opnum == rop.JUMP:
+                loop = op.jump_target
+                valueboxes = argboxes
+                continue
+            if op.opnum == rop.FAIL:
+                break
+            raise 0, "bad opnum"
+        #
+        for i in range(len(op.args)):
+            box = op.args[i]
+            if isinstance(box, BoxInt):
+                value = env[box].getint()
+                box.changevalue_int(value)
+            elif isinstance(box, BoxPtr):
+                value = env[box].getptr_base()
+                box.changevalue_ptr(value)
+        return op
+
+    def execute_guard(self, opnum, argboxes):
+        if opnum == rop.GUARD_TRUE:
+            value = argboxes[0].getint()
+            if not value:
+                raise GuardFailed
+        elif opnum == rop.GUARD_FALSE:
+            value = argboxes[0].getint()
+            if value:
+                raise GuardFailed
+        elif opnum == rop.GUARD_CLASS:
+            value = argboxes[0].getptr(rclass.OBJECTPTR)
+            expected_class = self.cast_int_to_ptr(rclass.CLASSTYPE,
+                                                  argboxes[1].getint())
+            if value.typeptr != expected_class:
+                raise GuardFailed
+        elif opnum == rop.GUARD_VALUE:
+            value = argboxes[0].getint()
+            expected_value = argboxes[1].getint()
+            if value != expected_value:
+                raise GuardFailed
+        elif opnum == rop.GUARD_NONVIRTUALIZED:
+            pass    # XXX
+        elif opnum == rop.GUARD_NO_EXCEPTION:
+            if self.current_exception:
+                raise GuardFailed
+        elif opnum == rop.GUARD_EXCEPTION:
+            expected_exception = argboxes[0].getptr(rclass.CLASSTYPE)
+            assert expected_exception
+            exc = self.current_exception
+            if exc and rclass.ll_issubclass(exc, expected_exception):
+                raise GuardFailed
+        else:
+            assert 0, "unknown guard op"
+
+    # ----------
+
+    def sizeof(self, TYPE):
+        def alloc():
+            p = lltype.malloc(TYPE)
+            return lltype.cast_opaque_ptr(llmemory.GCREF, p)
+        return SizeDescr(alloc)
+
+    def calldescrof(self, ARGS, RESULT):
+        dict2 = base_dict.copy()
+        args = []
+        for i, ARG in enumerate(ARGS):
+            args.append(make_reader(ARG, 'args[%d]' % i, dict2))
+        dict = {'args': ', '.join(args),
+                'result': make_writer(RESULT, 'res', dict2)}
+        dict2.update({'rffi': rffi,
+                      'FUNC': lltype.Ptr(lltype.FuncType(ARGS, RESULT)),
+                      'length': len(ARGS),
+                      })
+        exec py.code.Source("""
+            def call(function, args):
+                assert len(args) == length
+                function = rffi.cast(FUNC, function)
+                res = function(%(args)s)
+                return %(result)s
+        """ % dict).compile() in dict2
+        return CallDescr(dict2['call'])
+
+    # ----------
+
+    def do_new(self, args, sizedescr):
+        assert isinstance(sizedescr, SizeDescr)
+        p = sizedescr.alloc()
+        return BoxPtr(p)
+
+    do_new_with_vtable = do_new
+
+    def do_getfield_gc(self, args, fielddescr):
+        assert isinstance(fielddescr, FieldDescr)
+        gcref = args[0].getptr_base()
+        return fielddescr.getfield(gcref)
+
+    do_getfield_raw = do_getfield_gc
+
+    def do_setfield_gc(self, args, fielddescr):
+        assert isinstance(fielddescr, FieldDescr)
+        gcref = args[0].getptr_base()
+        fielddescr.setfield(gcref, args[1])
+
+    do_setfield_raw = do_setfield_gc
+
+    def do_new_array(self, args, arraydescr):
+        assert isinstance(arraydescr, ArrayDescr)
+        p = arraydescr.new(args[0].getint())
+        return BoxPtr(p)
+
+    def do_arraylen_gc(self, args, arraydescr):
+        assert isinstance(arraydescr, ArrayDescr)
+        gcref = args[0].getptr_base()
+        return BoxInt(arraydescr.length(gcref))
+
+    do_arraylen_raw = do_arraylen_gc
+
+    def do_getarrayitem_gc(self, args, arraydescr):
+        assert isinstance(arraydescr, ArrayDescr)
+        index = args[1].getint()
+        gcref = args[0].getptr_base()
+        return arraydescr.getarrayitem(gcref, index)
+    do_getarrayitem_raw = do_getarrayitem_gc
+
+    def do_setarrayitem_gc(self, args, arraydescr):
+        assert isinstance(arraydescr, ArrayDescr)
+        index = args[1].getint()
+        gcref = args[0].getptr_base()
+        arraydescr.setarrayitem(gcref, index, args[2])
+
+    do_setarrayitem_raw = do_setarrayitem_gc
+
+    def do_newstr(self, args, descr=None):
+        p = rstr.mallocstr(args[0].getint())
+        return BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, p))
+
+    def do_newunicode(self, args, descr=None):
+        p = rstr.mallocunicode(args[0].getint())
+        return BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, p))
+
+    def do_strlen(self, args, descr=None):
+        str = args[0].getptr(rstr.STR)
+        return BoxInt(len(str.chars))
+
+    def do_unicodelen(self, args, descr=None):
+        unicode = args[0].getptr(rstr.UNICODE)
+        return BoxInt(len(unicode.chars))
+
+    def do_strgetitem(self, args, descr=None):
+        str = args[0].getptr(rstr.STR)
+        i = args[1].getint()
+        return BoxInt(ord(str.chars[i]))
+
+    def do_unicodegetitem(self, args, descr=None):
+        unicode = args[0].getptr(rstr.UNICODE)
+        i = args[1].getint()
+        return BoxInt(ord(unicode.chars[i]))
+
+    def do_strsetitem(self, args, descr=None):
+        str = args[0].getptr(rstr.STR)
+        i = args[1].getint()
+        str.chars[i] = chr(args[2].getint())
+
+    def do_unicodesetitem(self, args, descr=None):
+        unicode = args[0].getptr(rstr.UNICODE)
+        i = args[1].getint()
+        unicode.chars[i] = unichr(args[2].getint())
+
+    def do_cast_int_to_ptr(self, args, descr=None):
+        return BoxPtr(self.cast_int_to_gcref(args[0].getint()))
+
+    def do_cast_ptr_to_int(self, args, descr=None):
+        return BoxInt(self.cast_gcref_to_int(args[0].getptr_base()))
+
+    def do_call(self, args, calldescr):
+        return calldescr.call(args[0].getint(), args[1:])
+
+    # ----------
+
+    def clear_exception(self):
+        self.current_exception = lltype.nullptr(rclass.OBJECT_VTABLE)
+        self.current_exc_inst = lltype.nullptr(rclass.OBJECT)
+
+    def set_overflow_error(self):
+        self.current_exception = self._ovf_error_vtable
+        self.current_exc_inst = self._ovf_error_inst
+
+    def guard_failed(self):
+        return self._guard_failed
+
+    # ----------
+
+    def cast_gcref_to_int(self, x):
+        return rffi.cast(lltype.Signed, x)
+
+    def cast_int_to_gcref(self, x):
+        return rffi.cast(llmemory.GCREF, x)
+
+    def cast_adr_to_int(self, x):
+        return rffi.cast(lltype.Signed, x)
+
+    @specialize.arg(1)
+    def cast_int_to_ptr(self, TYPE, x):
+        return rffi.cast(TYPE, x)
+
+
+class SizeDescr(AbstractDescr):
+    def __init__(self, alloc):
+        self.alloc = alloc
+
+class CallDescr(AbstractDescr):
+    def __init__(self, call):
+        self.call = call
+
+# ____________________________________________________________
+
+
+def _name(dict, obj):
+    name = '_n%d' % len(dict)
+    dict[name] = obj
+    return name
+
+def make_reader(TYPE, boxstr, dict):
+    if TYPE is lltype.Void:
+        return "None"
+    elif isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'gc':
+        return "%s.getptr(%s)" % (boxstr, _name(dict, TYPE))
+    else:
+        return "cast_primitive(%s, %s.getint())" % (_name(dict, TYPE), boxstr)
+
+def make_writer(TYPE, str, dict):
+    if TYPE is lltype.Void:
+        return "None"
+    elif isinstance(TYPE, lltype.Ptr) and TYPE.TO._gckind == 'gc':
+        return "BoxPtr(cast_opaque_ptr(GCREF, %s))" % (str,)
+    else:
+        return "BoxInt(cast_primitive(Signed, %s))" % (str,)
+
+base_dict = {
+    'cast_primitive': lltype.cast_primitive,
+    'cast_opaque_ptr': lltype.cast_opaque_ptr,
+    'GCREF': llmemory.GCREF,
+    'Signed': lltype.Signed,
+    'BoxInt': BoxInt,
+    'BoxPtr': BoxPtr,
+    }
+
+class GuardFailed(Exception):
+    pass
+
+import pypy.jit.metainterp.executor
+pypy.jit.metainterp.executor.make_execute_list(CPU)

Added: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/__init__.py
==============================================================================

Copied: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/test_runner.py (from r64026, pypy/branch/pyjitpl5-simplify/pypy/jit/backend/x86/test/test_runner.py)
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/backend/x86/test/test_runner.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/backend/minimal/test/test_runner.py	Mon Apr 13 19:08:39 2009
@@ -1,523 +1,16 @@
 import py
-from pypy.rpython.lltypesystem import lltype, llmemory, rffi, rstr, rclass
-from pypy.jit.metainterp.history import ResOperation, TreeLoop
-from pypy.jit.metainterp.history import (BoxInt, BoxPtr, ConstInt, ConstPtr,
-                                         Box)
-from pypy.jit.backend.x86.runner import CPU
-from pypy.jit.backend.x86.regalloc import WORD
-from pypy.jit.backend.x86 import symbolic
-from pypy.jit.metainterp.resoperation import rop
-from pypy.jit.metainterp.executor import execute
-from pypy.jit.backend.test.runner import BaseBackendTest, U, S
-import ctypes
-import sys
+from pypy.jit.backend.minimal.runner import CPU
+from pypy.jit.backend.test.runner import BaseBackendTest
 
 class FakeStats(object):
     pass
 
-class FakeMetaInterp(object):
-    pass
-
 # ____________________________________________________________
 
-class TestX86(BaseBackendTest):
+class TestMinimal(BaseBackendTest):
 
     # for the individual tests see
     # ====> ../../test/runner.py
     
     def setup_class(cls):
         cls.cpu = CPU(rtyper=None, stats=FakeStats())
-        cls.cpu.set_meta_interp(FakeMetaInterp())
-
-    def test_int_binary_ops(self):
-        for op, args, res in [
-            (rop.INT_SUB, [BoxInt(42), BoxInt(40)], 2),
-            (rop.INT_SUB, [BoxInt(42), ConstInt(40)], 2),
-            (rop.INT_SUB, [ConstInt(42), BoxInt(40)], 2),
-            (rop.INT_ADD, [ConstInt(-3), ConstInt(-5)], -8),
-            ]:
-            assert self.execute_operation(op, args, 'int').value == res
-
-    def test_int_unary_ops(self):
-        for op, args, res in [
-            (rop.INT_NEG, [BoxInt(42)], -42),
-            ]:
-            assert self.execute_operation(op, args, 'int').value == res
-
-    def test_int_comp_ops(self):
-        for op, args, res in [
-            (rop.INT_LT, [BoxInt(40), BoxInt(39)], 0),
-            (rop.INT_LT, [BoxInt(40), ConstInt(41)], 1),
-            (rop.INT_LT, [ConstInt(41), BoxInt(40)], 0),
-            (rop.INT_LE, [ConstInt(42), BoxInt(42)], 1),
-            (rop.INT_GT, [BoxInt(40), ConstInt(-100)], 1),
-            ]:
-            assert self.execute_operation(op, args, 'int').value == res
-
-    def test_execute_ptr_operation(self):
-        cpu = self.cpu
-        u = lltype.malloc(U)
-        u_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, u))
-        ofs = cpu.fielddescrof(S, 'value')
-        assert self.execute_operation(rop.SETFIELD_GC,
-                                      [u_box, BoxInt(3)],
-                                     'void', ofs) == None
-        assert u.parent.parent.value == 3
-        u.parent.parent.value += 100
-        assert (self.execute_operation(rop.GETFIELD_GC, [u_box], 'int', ofs)
-                .value == 103)
-
-    def test_execute_operations_in_env(self):
-        cpu = self.cpu
-        cpu.set_meta_interp(FakeMetaInterp())
-        x = BoxInt(123)
-        y = BoxInt(456)
-        z = BoxInt(579)
-        t = BoxInt(455)
-        u = BoxInt(0)    # False
-        operations = [
-            ResOperation(rop.INT_ADD, [x, y], z),
-            ResOperation(rop.INT_SUB, [y, ConstInt(1)], t),
-            ResOperation(rop.INT_EQ, [t, ConstInt(0)], u),
-            ResOperation(rop.GUARD_FALSE, [u], None),
-            ResOperation(rop.JUMP, [z, t], None),
-            ]
-        loop = TreeLoop('loop')
-        loop.operations = operations
-        loop.inputargs = [x, y]
-        operations[-1].jump_target = loop
-        operations[-2].suboperations = [ResOperation(rop.FAIL, [t, z], None)]
-        cpu.compile_operations(loop)
-        res = self.cpu.execute_operations(loop, [BoxInt(0), BoxInt(10)])
-        assert [arg.value for arg in res.args] == [0, 55]
-
-    def test_misc_int_ops(self):
-        for op, args, res in [
-            (rop.INT_MOD, [BoxInt(7), BoxInt(3)], 1),
-            (rop.INT_MOD, [ConstInt(0), BoxInt(7)], 0),
-            (rop.INT_MOD, [BoxInt(13), ConstInt(5)], 3),
-            (rop.INT_MOD, [ConstInt(33), ConstInt(10)], 3),
-            (rop.INT_FLOORDIV, [BoxInt(13), BoxInt(3)], 4),
-            (rop.INT_FLOORDIV, [BoxInt(42), ConstInt(10)], 4),
-            (rop.INT_FLOORDIV, [ConstInt(42), BoxInt(10)], 4),
-            (rop.INT_RSHIFT, [ConstInt(3), BoxInt(4)], 3>>4),
-            (rop.INT_RSHIFT, [BoxInt(3), ConstInt(10)], 3>>10),
-            #(rop.INT_LSHIFT, [BoxInt(3), BoxInt(1)], 3<<1),
-            ]:
-            assert self.execute_operation(op, args, 'int').value == res
-
-    def test_same_as(self):
-        py.test.skip("rewrite")
-        u = lltype.malloc(U)
-        uadr = lltype.cast_opaque_ptr(llmemory.GCREF, u)
-        for op, args, tp, res in [
-            ('same_as', [BoxInt(7)], 'int', 7),
-            ('same_as', [ConstInt(7)], 'int', 7),
-            ('same_as', [BoxPtr(uadr)], 'ptr', uadr),
-            ('same_as', [ConstPtr(uadr)], 'ptr', uadr),
-            ]:
-            assert self.execute_operation(op, args, tp).value == res
-
-    def test_allocations(self):
-        from pypy.rpython.lltypesystem import rstr
-        
-        allocs = [None]
-        all = []
-        def f(size):
-            allocs.insert(0, size)
-            buf = ctypes.create_string_buffer(size)
-            all.append(buf)
-            return ctypes.cast(buf, ctypes.c_void_p).value
-        func = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)(f)
-        addr = ctypes.cast(func, ctypes.c_void_p).value
-        
-        try:
-            saved_addr = self.cpu.assembler.malloc_func_addr
-            self.cpu.assembler.malloc_func_addr = addr
-            ofs = symbolic.get_field_token(rstr.STR, 'chars', False)[0]
-            
-            res = self.execute_operation(rop.NEWSTR, [ConstInt(7)], 'ptr')
-            assert allocs[0] == 7 + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
-            assert resbuf[ofs/WORD] == 7
-            
-            # ------------------------------------------------------------
-
-            res = self.execute_operation(rop.NEWSTR, [BoxInt(7)], 'ptr')
-            assert allocs[0] == 7 + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
-            assert resbuf[ofs/WORD] == 7
-
-            # ------------------------------------------------------------
-
-            TP = lltype.GcArray(lltype.Signed)
-            ofs = symbolic.get_field_token(TP, 'length', False)[0]
-            descr = self.cpu.arraydescrof(TP)
-
-            res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
-                                             'ptr', descr)
-            assert allocs[0] == 10*WORD + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
-            assert resbuf[ofs/WORD] == 10
-
-            # ------------------------------------------------------------
-
-            res = self.execute_operation(rop.NEW_ARRAY, [BoxInt(10)],
-                                             'ptr', descr)
-            assert allocs[0] == 10*WORD + ofs + WORD
-            resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
-            assert resbuf[ofs/WORD] == 10
-            
-        finally:
-            self.cpu.assembler.malloc_func_addr = saved_addr
-
-    def test_stringitems(self):
-        from pypy.rpython.lltypesystem.rstr import STR
-        ofs = symbolic.get_field_token(STR, 'chars', False)[0]
-        ofs_items = symbolic.get_field_token(STR.chars, 'items', False)[0]
-
-        res = self.execute_operation(rop.NEWSTR, [ConstInt(10)], 'ptr')
-        self.execute_operation(rop.STRSETITEM, [res, ConstInt(2), ConstInt(ord('d'))], 'void')
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_char))
-        assert resbuf[ofs + ofs_items + 2] == 'd'
-        self.execute_operation(rop.STRSETITEM, [res, BoxInt(2), ConstInt(ord('z'))], 'void')
-        assert resbuf[ofs + ofs_items + 2] == 'z'
-        r = self.execute_operation(rop.STRGETITEM, [res, BoxInt(2)], 'int')
-        assert r.value == ord('z')
-
-    def test_arrayitems(self):
-        TP = lltype.GcArray(lltype.Signed)
-        ofs = symbolic.get_field_token(TP, 'length', False)[0]
-        itemsofs = symbolic.get_field_token(TP, 'items', False)[0]
-        descr = self.cpu.arraydescrof(TP)
-        res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
-                                     'ptr', descr)
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_int))
-        assert resbuf[ofs/WORD] == 10
-        self.execute_operation(rop.SETARRAYITEM_GC, [res,
-                                                     ConstInt(2), BoxInt(38)],
-                               'void', descr)
-        assert resbuf[itemsofs/WORD + 2] == 38
-        
-        self.execute_operation(rop.SETARRAYITEM_GC, [res,
-                                                     BoxInt(3), BoxInt(42)],
-                               'void', descr)
-        assert resbuf[itemsofs/WORD + 3] == 42
-
-        r = self.execute_operation(rop.GETARRAYITEM_GC, [res, ConstInt(2)],
-                                   'int', descr)
-        assert r.value == 38
-        r = self.execute_operation(rop.GETARRAYITEM_GC, [res.constbox(),
-                                                         BoxInt(2)],
-                                   'int', descr)
-        assert r.value == 38
-        r = self.execute_operation(rop.GETARRAYITEM_GC, [res.constbox(),
-                                                         ConstInt(2)],
-                                   'int', descr)
-        assert r.value == 38
-        r = self.execute_operation(rop.GETARRAYITEM_GC, [res,
-                                                         BoxInt(2)],
-                                   'int', descr)
-        assert r.value == 38
-        
-        r = self.execute_operation(rop.GETARRAYITEM_GC, [res, BoxInt(3)],
-                                   'int', descr)
-        assert r.value == 42
-
-    def test_arrayitems_not_int(self):
-        TP = lltype.GcArray(lltype.Char)
-        ofs = symbolic.get_field_token(TP, 'length', False)[0]
-        itemsofs = symbolic.get_field_token(TP, 'items', False)[0]
-        descr = self.cpu.arraydescrof(TP)
-        res = self.execute_operation(rop.NEW_ARRAY, [ConstInt(10)],
-                                     'ptr', descr)
-        resbuf = ctypes.cast(res.value.intval, ctypes.POINTER(ctypes.c_char))
-        assert resbuf[ofs] == chr(10)
-        for i in range(10):
-            self.execute_operation(rop.SETARRAYITEM_GC, [res,
-                                                   ConstInt(i), BoxInt(i)],
-                                   'void', descr)
-        for i in range(10):
-            assert resbuf[itemsofs + i] == chr(i)
-        for i in range(10):
-            r = self.execute_operation(rop.GETARRAYITEM_GC, [res,
-                                                             ConstInt(i)],
-                                         'int', descr)
-            assert r.value == i
-
-    def test_getfield_setfield(self):
-        TP = lltype.GcStruct('x', ('s', lltype.Signed),
-                             ('f', lltype.Float),
-                             ('u', rffi.USHORT),
-                             ('c1', lltype.Char),
-                             ('c2', lltype.Char),
-                             ('c3', lltype.Char))
-        res = self.execute_operation(rop.NEW, [],
-                                     'ptr', self.cpu.sizeof(TP))
-        ofs_s = self.cpu.fielddescrof(TP, 's')
-        ofs_f = self.cpu.fielddescrof(TP, 'f')
-        ofs_u = self.cpu.fielddescrof(TP, 'u')
-        ofsc1 = self.cpu.fielddescrof(TP, 'c1')
-        ofsc2 = self.cpu.fielddescrof(TP, 'c2')
-        ofsc3 = self.cpu.fielddescrof(TP, 'c3')
-        self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void',
-                               ofs_s)
-        # XXX ConstFloat
-        #self.execute_operation(rop.SETFIELD_GC, [res, ofs_f, 1e100], 'void')
-        # XXX we don't support shorts (at all)
-        #self.execute_operation(rop.SETFIELD_GC, [res, ofs_u, ConstInt(5)], 'void')
-        s = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofs_s)
-        assert s.value == 3
-        self.execute_operation(rop.SETFIELD_GC, [res, BoxInt(3)], 'void',
-                               ofs_s)
-        s = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofs_s)
-        assert s.value == 3
-        #u = self.execute_operation(rop.GETFIELD_GC, [res, ofs_u], 'int')
-        #assert u.value == 5
-        self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(1)], 'void',
-                               ofsc1)
-        self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(2)], 'void',
-                               ofsc2)
-        self.execute_operation(rop.SETFIELD_GC, [res, ConstInt(3)], 'void',
-                               ofsc3)
-        c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc1)
-        assert c.value == 1
-        c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc2)
-        assert c.value == 2
-        c = self.execute_operation(rop.GETFIELD_GC, [res], 'int', ofsc3)
-        assert c.value == 3
-
-    def test_uint_ops(self):
-        from pypy.rlib.rarithmetic import r_uint, intmask
-
-        arg0 = BoxInt(intmask(r_uint(sys.maxint + 3)))
-        arg1 = BoxInt(intmask(r_uint(4)))
-        res = self.execute_operation(rop.UINT_ADD, [arg0, arg1], 'int')
-        assert res.value == intmask(r_uint(sys.maxint + 3) + r_uint(4))
-
-        arg0 = BoxInt(intmask(sys.maxint + 10))
-        arg1 = BoxInt(10)
-        res = self.execute_operation(rop.UINT_MUL, [arg0, arg1], 'int')
-        assert res.value == intmask((sys.maxint + 10) * 10)
-
-        arg0 = BoxInt(intmask(r_uint(sys.maxint + 3)))
-        arg1 = BoxInt(intmask(r_uint(4)))
-
-        res = self.execute_operation(rop.UINT_GT, [arg0, arg1], 'int')
-        assert res.value == 1
-
-    def test_do_operations(self):
-        cpu = self.cpu
-        #
-        A = lltype.GcArray(lltype.Char)
-        descr_A = cpu.arraydescrof(A)
-        a = lltype.malloc(A, 5)
-        x = cpu.do_arraylen_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, a))],
-            descr_A)
-        assert x.value == 5
-        #
-        a[2] = 'Y'
-        x = cpu.do_getarrayitem_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, a)), BoxInt(2)],
-            descr_A)
-        assert x.value == ord('Y')
-        #
-        B = lltype.GcArray(lltype.Ptr(A))
-        descr_B = cpu.arraydescrof(B)
-        b = lltype.malloc(B, 4)
-        b[3] = a
-        x = cpu.do_getarrayitem_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, b)), BoxInt(3)],
-            descr_B)
-        assert isinstance(x, BoxPtr)
-        assert x.getptr(lltype.Ptr(A)) == a
-        #
-        s = rstr.mallocstr(6)
-        x = cpu.do_strlen(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s))])
-        assert x.value == 6
-        #
-        s.chars[3] = 'X'
-        x = cpu.do_strgetitem(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s)), BoxInt(3)])
-        assert x.value == ord('X')
-        #
-        S = lltype.GcStruct('S', ('x', lltype.Char), ('y', lltype.Ptr(A)))
-        descrfld_x = cpu.fielddescrof(S, 'x')
-        s = lltype.malloc(S)
-        s.x = 'Z'
-        x = cpu.do_getfield_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s))],
-            descrfld_x)
-        assert x.value == ord('Z')
-        #
-        cpu.do_setfield_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s)),
-             BoxInt(ord('4'))],
-            descrfld_x)
-        assert s.x == '4'
-        #
-        descrfld_y = cpu.fielddescrof(S, 'y')
-        s.y = a
-        x = cpu.do_getfield_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s))],
-            descrfld_y)
-        assert isinstance(x, BoxPtr)
-        assert x.getptr(lltype.Ptr(A)) == a
-        #
-        s.y = lltype.nullptr(A)
-        cpu.do_setfield_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s)), x],
-            descrfld_y)
-        assert s.y == a
-        #
-        RS = lltype.Struct('S', ('x', lltype.Char), ('y', lltype.Ptr(A)))
-        descrfld_rx = cpu.fielddescrof(RS, 'x')
-        rs = lltype.malloc(RS, immortal=True)
-        rs.x = '?'
-        x = cpu.do_getfield_raw(
-            [BoxInt(cpu.cast_adr_to_int(llmemory.cast_ptr_to_adr(rs)))],
-            descrfld_rx)
-        assert x.value == ord('?')
-        #
-        cpu.do_setfield_raw(
-            [BoxInt(cpu.cast_adr_to_int(llmemory.cast_ptr_to_adr(rs))),
-             BoxInt(ord('!'))],
-            descrfld_rx)
-        assert rs.x == '!'
-        #
-        descrfld_ry = cpu.fielddescrof(RS, 'y')
-        rs.y = a
-        x = cpu.do_getfield_raw(
-            [BoxInt(cpu.cast_adr_to_int(llmemory.cast_ptr_to_adr(rs)))],
-            descrfld_ry)
-        assert isinstance(x, BoxPtr)
-        assert x.getptr(lltype.Ptr(A)) == a
-        #
-        rs.y = lltype.nullptr(A)
-        cpu.do_setfield_raw(
-            [BoxInt(cpu.cast_adr_to_int(llmemory.cast_ptr_to_adr(rs))), x],
-            descrfld_ry)
-        assert rs.y == a
-        #
-        descrsize = cpu.sizeof(S)
-        x = cpu.do_new([], descrsize)
-        assert isinstance(x, BoxPtr)
-        x.getptr(lltype.Ptr(S))
-        #
-        descrsize2 = cpu.sizeof(rclass.OBJECT)
-        vtable2 = lltype.malloc(rclass.OBJECT_VTABLE, immortal=True)
-        x = cpu.do_new_with_vtable(
-            [BoxInt(cpu.cast_adr_to_int(llmemory.cast_ptr_to_adr(vtable2)))],
-            descrsize2)
-        assert isinstance(x, BoxPtr)
-        # well...
-        #assert x.getptr(rclass.OBJECTPTR).typeptr == vtable2
-        #
-        arraydescr = cpu.arraydescrof(A)
-        x = cpu.do_new_array([BoxInt(7)], arraydescr)
-        assert isinstance(x, BoxPtr)
-        assert len(x.getptr(lltype.Ptr(A))) == 7
-        #
-        cpu.do_setarrayitem_gc(
-            [x, BoxInt(5), BoxInt(ord('*'))], descr_A)
-        assert x.getptr(lltype.Ptr(A))[5] == '*'
-        #
-        cpu.do_setarrayitem_gc(
-            [BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, b)),
-             BoxInt(1), x],
-            descr_B)
-        assert b[1] == x.getptr(lltype.Ptr(A))
-        #
-        x = cpu.do_newstr([BoxInt(5)])
-        assert isinstance(x, BoxPtr)
-        assert len(x.getptr(lltype.Ptr(rstr.STR)).chars) == 5
-        #
-        cpu.do_strsetitem([x, BoxInt(4), BoxInt(ord('/'))])
-        assert x.getptr(lltype.Ptr(rstr.STR)).chars[4] == '/'
-
-    def test_oononnull_with_guard(self):
-        p = lltype.cast_opaque_ptr(llmemory.GCREF,
-                                   lltype.malloc(lltype.GcStruct('x')))
-        p = BoxPtr(p)
-        f = BoxInt()
-        ops = [
-            ResOperation(rop.OONONNULL, [p], f),
-            ResOperation(rop.GUARD_TRUE, [f], None),
-            ResOperation(rop.FAIL, [ConstInt(0)], None),
-            ]
-        ops[1].suboperations = [ResOperation(rop.FAIL, [ConstInt(1)], None)]
-        loop = TreeLoop('name')
-        loop.operations = ops
-        loop.inputargs = [p]
-        self.cpu.compile_operations(loop)
-        op = self.cpu.execute_operations(loop, [p])
-        assert op.args[0].value == 0
-
-    def test_stuff_followed_by_guard(self):
-        boxes = [(BoxInt(1), BoxInt(0)),
-                 (BoxInt(0), BoxInt(1)),
-                 (BoxInt(1), BoxInt(1)),
-                 (BoxInt(-1), BoxInt(1)),
-                 (BoxInt(1), BoxInt(-1)),
-                 (ConstInt(1), BoxInt(0)),
-                 (ConstInt(0), BoxInt(1)),
-                 (ConstInt(1), BoxInt(1)),
-                 (ConstInt(-1), BoxInt(1)),
-                 (ConstInt(1), BoxInt(-1)),
-                 (BoxInt(1), ConstInt(0)),
-                 (BoxInt(0), ConstInt(1)),
-                 (BoxInt(1), ConstInt(1)),
-                 (BoxInt(-1), ConstInt(1)),
-                 (BoxInt(1), ConstInt(-1))]
-        guards = [rop.GUARD_FALSE, rop.GUARD_TRUE]
-        all = [rop.INT_EQ, rop.INT_NE, rop.INT_LE, rop.INT_LT, rop.INT_GT,
-               rop.INT_GE, rop.UINT_GT, rop.UINT_LT, rop.UINT_LE, rop.UINT_GE]
-        for a, b in boxes:
-            for guard in guards:
-                for op in all:
-                    res = BoxInt()
-                    ops = [
-                        ResOperation(op, [a, b], res),
-                        ResOperation(guard, [res], None),
-                        ResOperation(rop.FAIL, [ConstInt(0)], None),
-                        ]
-                    ops[1].suboperations = [ResOperation(rop.FAIL, [ConstInt(1)], None)]
-                    loop = TreeLoop('name')
-                    loop.operations = ops
-                    loop.inputargs = [i for i in (a, b) if isinstance(i, Box)]
-                    self.cpu.compile_operations(loop)
-                    r = self.cpu.execute_operations(loop, loop.inputargs)
-                    if guard == rop.GUARD_FALSE:
-                        assert r.args[0].value == execute(self.cpu, op, (a, b)).value
-                    else:
-                        assert r.args[0].value != execute(self.cpu, op, (a, b)).value
-
-    def test_overflow_mc(self):
-        from pypy.jit.backend.x86.assembler import MachineCodeBlockWrapper
-
-        orig_size = MachineCodeBlockWrapper.MC_SIZE
-        MachineCodeBlockWrapper.MC_SIZE = 1024
-        old_mc = self.cpu.assembler.mc
-        old_mc2 = self.cpu.assembler.mc2
-        self.cpu.assembler.mc = None
-        try:
-            ops = []
-            base_v = BoxInt()
-            v = base_v
-            for i in range(1024):
-                next_v = BoxInt()
-                ops.append(ResOperation(rop.INT_ADD, [v, ConstInt(1)], next_v))
-                v = next_v
-            ops.append(ResOperation(rop.FAIL, [v], None))
-            loop = TreeLoop('name')
-            loop.operations = ops
-            loop.inputargs = [base_v]
-            self.cpu.compile_operations(loop)
-            op = self.cpu.execute_operations(loop, [base_v])
-            assert op.args[0].value == 1024
-        finally:
-            MachineCodeBlockWrapper.MC_SIZE = orig_size
-            self.cpu.assembler.mc = old_mc
-            self.cpu.assembler.mc2 = old_mc2

Modified: pypy/branch/pyjitpl5-simplify/pypy/jit/backend/test/runner.py
==============================================================================
--- pypy/branch/pyjitpl5-simplify/pypy/jit/backend/test/runner.py	(original)
+++ pypy/branch/pyjitpl5-simplify/pypy/jit/backend/test/runner.py	Mon Apr 13 19:08:39 2009
@@ -3,17 +3,16 @@
 from pypy.jit.metainterp.history import (BoxInt, Box, BoxPtr, TreeLoop,
                                          ConstInt, ConstPtr)
 from pypy.jit.metainterp.resoperation import ResOperation, rop
-from pypy.rpython.lltypesystem import lltype, llmemory, rstr, rffi
+from pypy.rpython.lltypesystem import lltype, llmemory, rstr, rffi, rclass
 from pypy.jit.metainterp.executor import execute
 from pypy.rlib.rarithmetic import r_uint, intmask
 
-MY_VTABLE = lltype.Struct('my_vtable')    # for tests only
+MY_VTABLE = rclass.OBJECT_VTABLE    # for tests only
 
 S = lltype.GcForwardReference()
-S.become(lltype.GcStruct('S', ('typeptr', lltype.Ptr(MY_VTABLE)),
+S.become(lltype.GcStruct('S', ('parent', rclass.OBJECT),
                               ('value', lltype.Signed),
-                              ('next', lltype.Ptr(S)),
-                         hints = {'typeptr': True}))
+                              ('next', lltype.Ptr(S))))
 T = lltype.GcStruct('T', ('parent', S),
                          ('next', lltype.Ptr(S)))
 U = lltype.GcStruct('U', ('parent', T),
@@ -21,7 +20,7 @@
 
 class Runner(object):
         
-    def execute_operation(self, opname, valueboxes, result_type, descr=0):
+    def execute_operation(self, opname, valueboxes, result_type, descr=None):
         loop = self.get_compiled_single_operation(opname, result_type,
                                                   valueboxes, descr)
         boxes = [box for box in valueboxes if isinstance(box, Box)]
@@ -269,7 +268,7 @@
             assert not self.cpu.guard_failed()
             
         t = lltype.malloc(T)
-        t.parent.typeptr = vtable_for_T
+        t.parent.parent.typeptr = vtable_for_T
         t_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, t))
         T_box = ConstInt(cpu.cast_adr_to_int(vtable_for_T_addr))
         null_box = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, lltype.nullptr(T)))
@@ -286,11 +285,11 @@
         cpu = self.cpu
         cpu._cache_gcstruct2vtable = {T: vtable_for_T, U: vtable_for_U}
         t = lltype.malloc(T)
-        t.parent.typeptr = vtable_for_T
+        t.parent.parent.typeptr = vtable_for_T
         t_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, t))
         T_box = ConstInt(self.cpu.cast_adr_to_int(vtable_for_T_addr))
         u = lltype.malloc(U)
-        u.parent.parent.typeptr = vtable_for_U
+        u.parent.parent.parent.typeptr = vtable_for_U
         u_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, u))
         U_box = ConstInt(self.cpu.cast_adr_to_int(vtable_for_U_addr))
         null_box = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, lltype.nullptr(T)))



More information about the Pypy-commit mailing list