[pypy-svn] r62302 - in pypy/branch/pyjitpl5/pypy/jit: backend/llgraph metainterp metainterp/test

arigo at codespeak.net arigo at codespeak.net
Sun Mar 1 17:39:25 CET 2009


Author: arigo
Date: Sun Mar  1 17:39:23 2009
New Revision: 62302

Modified:
   pypy/branch/pyjitpl5/pypy/jit/backend/llgraph/llimpl.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/pyjitpl.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/resoperation.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_basic.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_slist.py
Log:
Restart implementing list support.  For now only fixed-sized lists,
implemented directly as array operations.


Modified: pypy/branch/pyjitpl5/pypy/jit/backend/llgraph/llimpl.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/backend/llgraph/llimpl.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/backend/llgraph/llimpl.py	Sun Mar  1 17:39:23 2009
@@ -195,7 +195,7 @@
     if tp == 'int':
         return str(x)
     elif tp == 'void':
-        return ''
+        return '---'
     elif tp == 'ptr':
         if not x:
             return '(* None)'

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/codewriter.py	Sun Mar  1 17:39:23 2009
@@ -15,27 +15,6 @@
 MAX_MAKE_NEW_VARS = 16
 
 
-##class BuiltinDescr(history.AbstractValue):
-##    pass
-
-##class ListDescr(BuiltinDescr):
-##    def __init__(self, getfunc, setfunc, malloc_func, append_func,
-##                 pop_func, insert_func, len_func, nonzero_func, tp):
-##        self.setfunc      = setfunc
-##        self.getfunc      = getfunc
-##        self.malloc_func  = malloc_func
-##        self.append_func  = append_func
-##        self.insert_func  = insert_func
-##        self.pop_func     = pop_func
-##        self.len_func     = len_func
-##        self.nonzero_func = nonzero_func
-##        self.tp           = tp
-    
-##    def equals(self, other):
-##        if isinstance(other, ListDescr):
-##            return True
-##        return False
-
 class JitCode(history.AbstractValue):
     def __init__(self, name):
         self.name = name
@@ -99,7 +78,6 @@
         self.rtyper = metainterp.cpu.rtyper
         self.cpu = metainterp.cpu
         self.policy = policy
-        self.list_cache = {}
 
     def make_portal_bytecode(self, graph):
         log.info("making JitCodes...")
@@ -133,81 +111,52 @@
                                   IndirectCallset(self, graphs)
         return result
 
-    def get_list_desc(self, LIST):
-        try:
-            return self.all_listdescs[LIST]
-        except KeyError:
-            import vlist
-            listdesc = vlist.ListClassDesc(self, LIST)
-            self.all_listdescs[LIST] = listdesc
-            return listdesc
-
-    def register_list_op(self, llfunc, oopspec_name, LIST):
-        metainterp = self.metainterp
-        if llfunc._obj not in metainterp.builtins_seen:
-            metainterp.builtins_seen[llfunc._obj] = True
-            key = llmemory.cast_ptr_to_adr(llfunc)
-            value = (oopspec_name.replace('.', '_'), self.get_list_desc(LIST))
-            metainterp.builtins_keys.append(key)
-            metainterp.builtins_values.append(value)
-
 
-    def list_descr_for_tp(self, TP):
+    if 0:        # disabled
+      def fixed_list_descr_for_tp(self, TP):
         try:
-            return self.list_cache[TP.TO]
+            return self.fixed_list_cache[TP.TO]
         except KeyError:
-            if isinstance(TP.TO, lltype.GcStruct):
-                OF = TP.TO.items.TO.OF
-            else:
-                OF = TP.TO.OF
+            OF = TP.TO.OF
             rtyper = self.rtyper
-            args = [TP, lltype.Signed, OF]
             setfunc, _ = support.builtin_func_for_spec(rtyper, 'list.setitem',
-                                                       args, lltype.Void)
+                                                       [TP, lltype.Signed, OF],
+                                                       lltype.Void)
             getfunc, _ = support.builtin_func_for_spec(rtyper, 'list.getitem',
-                                                       args[:-1], OF)
+                                                       [TP, lltype.Signed], OF)
             malloc_func, _ = support.builtin_func_for_spec(rtyper, 'newlist',
-                                                           [lltype.Signed], TP)
+                                                           [lltype.Signed, OF],
+                                                           TP)
             len_func, _ = support.builtin_func_for_spec(rtyper, 'list.len',
                                                         [TP], lltype.Signed)
-            nonzero_func, _ = support.builtin_func_for_spec(rtyper,
-                                                            'list.nonzero',
-                                                            [TP], lltype.Bool)
-
-            if isinstance(TP.TO, lltype.GcStruct):
-                append_func, _ = support.builtin_func_for_spec(rtyper,
-                                                               'list.append',
-                                                        [TP, OF], lltype.Void)
-                pop_func, _ = support.builtin_func_for_spec(rtyper, 'list.pop',
-                                                            [TP], OF)
-                insert_func, _ = support.builtin_func_for_spec(rtyper,
-                      'list.insert', [TP, lltype.Signed, OF], lltype.Void)
-            if isinstance(OF, lltype.Number):
-                tp = "int"
-            else:
-                tp = "ptr"
-            if isinstance(TP.TO, lltype.GcStruct):
-                ld = ListDescr(history.ConstAddr(getfunc.value, self.cpu),
-                               history.ConstAddr(setfunc.value, self.cpu),
-                               history.ConstAddr(malloc_func.value, self.cpu),
-                               history.ConstAddr(append_func.value, self.cpu),
-                               history.ConstAddr(pop_func.value, self.cpu),
-                               history.ConstAddr(insert_func.value, self.cpu),
-                               history.ConstAddr(len_func.value, self.cpu),
-                               history.ConstAddr(nonzero_func.value, self.cpu),
-                               tp)
-            else:
-                ld = ListDescr(history.ConstAddr(getfunc.value, self.cpu),
-                               history.ConstAddr(setfunc.value, self.cpu),
-                               history.ConstAddr(malloc_func.value, self.cpu),
-                               None, None, None,
-                               history.ConstAddr(len_func.value, self.cpu),
-                               history.ConstAddr(nonzero_func.value, self.cpu),
-                               tp)
-            self.list_cache[TP.TO] = ld
+##            if isinstance(TP.TO, lltype.GcStruct):
+##                append_func, _ = support.builtin_func_for_spec(rtyper,
+##                                                               'list.append',
+##                                                        [TP, OF], lltype.Void)
+##                pop_func, _ = support.builtin_func_for_spec(rtyper, 'list.pop',
+##                                                            [TP], OF)
+##                insert_func, _ = support.builtin_func_for_spec(rtyper,
+##                      'list.insert', [TP, lltype.Signed, OF], lltype.Void)
+            tp = getkind(OF)
+##            if isinstance(TP.TO, lltype.GcStruct):
+##                ld = ListDescr(history.ConstAddr(getfunc.value, self.cpu),
+##                               history.ConstAddr(setfunc.value, self.cpu),
+##                               history.ConstAddr(malloc_func.value, self.cpu),
+##                               history.ConstAddr(append_func.value, self.cpu),
+##                               history.ConstAddr(pop_func.value, self.cpu),
+##                               history.ConstAddr(insert_func.value, self.cpu),
+##                               history.ConstAddr(len_func.value, self.cpu),
+##                               history.ConstAddr(nonzero_func.value, self.cpu),
+##                               tp)
+##            else:
+            ld = FixedListDescr(history.ConstAddr(getfunc.value, self.cpu),
+                                history.ConstAddr(setfunc.value, self.cpu),
+                                history.ConstAddr(malloc_func.value, self.cpu),
+                                history.ConstAddr(len_func.value, self.cpu),
+                                tp)
+            self.fixed_list_cache[TP.TO] = ld
             return ld
 
-    
 
 class BytecodeMaker(object):
     debug = True
@@ -722,7 +671,9 @@
         c_func, TP = support.builtin_func_for_spec(self.codewriter.rtyper,
                                                    oopspec_name, ll_args,
                                                    op.result.concretetype)
-##        if oopspec_name.startswith('list') or oopspec_name == 'newlist':
+        if self.codewriter.metainterp.options.listops:
+            if self.handle_list_call(op, oopspec_name, args, TP):
+                return
 ##            if oopspec_name.startswith('list.getitem'):
 ##                opname = oopspec_name[len('list.'):]
 ##            elif oopspec_name.startswith('list.setitem'):
@@ -757,6 +708,57 @@
         self.emit_varargs([c_func] + args)
         self.register_var(op.result)
 
+    def handle_list_call(self, op, oopspec_name, args, TP):
+        if not (oopspec_name.startswith('list.') or oopspec_name == 'newlist'):
+            return False
+        if hasattr(TP, '_ll_resize'):
+            return False
+        # non-resizable lists: they are just arrays
+        ARRAY = TP.TO
+        assert isinstance(ARRAY, lltype.GcArray)
+        arraydescr = self.cpu.arraydescrof(ARRAY)
+        #
+        if oopspec_name == 'newlist':
+            # normalize number of arguments
+            if len(args) < 1:
+                args.append(Constant(0, lltype.Signed))
+            if len(args) > 1:
+                v_default = args[1]
+                if (not isinstance(v_default, Constant) or
+                    v_default.value != TP.TO.OF._defl()):
+                    return False     # variable or non-null initial value
+            self.emit('new_array')
+            self.emit(self.const_position(arraydescr))
+            self.emit(self.var_position(args[0]))
+            self.register_var(op.result)
+            return True
+        #
+        if (oopspec_name == 'list.getitem' or
+            oopspec_name == 'list.getitem_foldable'): # <- XXX do better here
+            # XXX check if index < 0, and check IndexError, both only if needed
+            self.emit('getarrayitem_gc')
+            self.emit(self.var_position(args[0]))
+            self.emit(self.const_position(arraydescr))
+            self.emit(self.var_position(args[1]))
+            self.register_var(op.result)
+            return True
+        #
+        if oopspec_name == 'list.setitem':
+            # XXX check if index < 0, and check IndexError, both only if needed
+            self.emit('setarrayitem_gc')
+            self.emit(self.var_position(args[0]))
+            self.emit(self.const_position(arraydescr))
+            self.emit(self.var_position(args[1]))
+            self.emit(self.var_position(args[2]))
+            self.register_var(op.result)
+            return True
+        #
+        if (oopspec_name == 'list.len' or
+            oopspec_name == 'list.len_foldable'):
+            xxx  # ... 'arraylen_gc'
+        #
+        return False
+
     def serialize_op_indirect_call(self, op):
         self.minimize_variables()
         targets = self.codewriter.policy.graphs_from(op)

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/history.py	Sun Mar  1 17:39:23 2009
@@ -465,7 +465,8 @@
 # ----------------------------------------------------------------
 
 class Options:
-    def __init__(self, specialize=True):
+    def __init__(self, specialize=True, listops=False):
         self.specialize = specialize
+        self.listops = listops
     def _freeze_(self):
         return True

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/pyjitpl.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/pyjitpl.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/pyjitpl.py	Sun Mar  1 17:39:23 2009
@@ -76,10 +76,6 @@
                     assert isinstance(indirectcallset,
                                       codewriter.IndirectCallset)
                     args += (indirectcallset, )
-                elif argspec == "builtin":
-                    builtin = self.load_const_arg()
-                    assert isinstance(builtin, codewriter.BuiltinDescr)
-                    args += (builtin, )
                 elif argspec == "virtualizabledesc":
                     from virtualizable import VirtualizableDesc
                     virtualizabledesc = self.load_const_arg()
@@ -424,59 +420,56 @@
         return self.execute_with_exc(rop.CALL_VOID, varargs, 'void')
 
 
-    @arguments("builtin", "varargs")
-    def opimpl_getitem(self, descr, varargs):
-        args = [descr.getfunc] + varargs
-        return self.execute_with_exc('getitem', args, descr.tp)
-
-    @arguments("builtin", "varargs")
-    def opimpl_setitem(self, descr, varargs):
-        args = [descr.setfunc] + varargs
-        return self.execute_with_exc('setitem', args, 'void')
-
-    @arguments("builtin", "varargs")
-    def opimpl_getitem_foldable(self, descr, varargs):
-        args = [descr.getfunc] + varargs
-        return self.execute_with_exc('getitem', args, descr.tp, True)
-
-    @arguments("builtin", "varargs")
-    def opimpl_setitem_foldable(self, descr, varargs):
-        args = [descr.setfunc] + varargs
-        return self.execute_with_exc('setitem', args, 'void', True)
-
-    @arguments("builtin", "varargs")
-    def opimpl_newlist(self, descr, varargs):
-        if len(varargs) == 2:
-            mf = descr.malloc_3_func
-        else:
-            mf = descr.malloc_2_func
-        args = [mf] + varargs
-        return self.execute_with_exc('newlist', args, 'ptr')
-
-    @arguments("builtin", "varargs")
-    def opimpl_append(self, descr, varargs):
-        args = [descr.append_func] + varargs
-        return self.execute_with_exc('append', args, 'void')
-
-    @arguments("builtin", "varargs")
-    def opimpl_insert(self, descr, varargs):
-        args = [descr.insert_func] + varargs
-        return self.execute_with_exc('insert', args, 'void')
-
-    @arguments("builtin", "varargs")
-    def opimpl_pop(self, descr, varargs):
-        args = [descr.pop_func] + varargs
-        return self.execute_with_exc('pop', args, descr.tp)
-
-    @arguments("builtin", "varargs")
-    def opimpl_len(self, descr, varargs):
-        args = [descr.len_func] + varargs
-        return self.execute_with_exc('len', args, 'int')
-
-    @arguments("builtin", "varargs")
-    def opimpl_listnonzero(self, descr, varargs):
-        args = [descr.nonzero_func] + varargs
-        return self.execute_with_exc('listnonzero', args, 'int')
+##    @arguments("fixedlist", "box", "box")
+##    def opimpl_list_getitem(self, descr, listbox, indexbox):
+##        args = [descr.getfunc, listbox, indexbox]
+##        return self.execute_with_exc(rop.LIST_GETITEM, args, descr.tp)
+
+##    @arguments("fixedlist", "box", "box", "box")
+##    def opimpl_list_setitem(self, descr, listbox, indexbox, newitembox):
+##        args = [descr.setfunc, listbox, indexbox, newitembox]
+##        return self.execute_with_exc(rop.LIST_SETITEM, args, 'void')
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_getitem_foldable(self, descr, varargs):
+##        args = [descr.getfunc] + varargs
+##        return self.execute_with_exc('getitem', args, descr.tp, True)
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_setitem_foldable(self, descr, varargs):
+##        args = [descr.setfunc] + varargs
+##        return self.execute_with_exc('setitem', args, 'void', True)
+
+##    @arguments("fixedlist", "box", "box")
+##    def opimpl_newlist(self, descr, countbox, defaultbox):
+##        args = [descr.malloc_func, countbox, defaultbox]
+##        return self.execute_with_exc(rop.NEWLIST, args, 'ptr')
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_append(self, descr, varargs):
+##        args = [descr.append_func] + varargs
+##        return self.execute_with_exc('append', args, 'void')
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_insert(self, descr, varargs):
+##        args = [descr.insert_func] + varargs
+##        return self.execute_with_exc('insert', args, 'void')
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_pop(self, descr, varargs):
+##        args = [descr.pop_func] + varargs
+##        return self.execute_with_exc('pop', args, descr.tp)
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_len(self, descr, varargs):
+##        args = [descr.len_func] + varargs
+##        return self.execute_with_exc('len', args, 'int')
+
+##    @arguments("builtin", "varargs")
+##    def opimpl_listnonzero(self, descr, varargs):
+##        args = [descr.nonzero_func] + varargs
+##        return self.execute_with_exc('listnonzero', args, 'int')
+
 
     @arguments("orgpc", "indirectcallset", "box", "varargs")
     def opimpl_indirect_call(self, pc, indirectcallset, box, varargs):
@@ -721,12 +714,6 @@
 
         self.opcode_implementations = []
         self.opname_to_index = {}
-
-        # helpers to eventually build the dictionary "self.builtins":
-        self.builtins_keys = []
-        self.builtins_values = []
-        self.builtins_seen = {}
-
         self.class_sizes = populate_type_cache(graphs, self.cpu)
 
         self._virtualizabledescs = {}
@@ -980,26 +967,6 @@
                         frame.exception_target))
         return key
 
-    def make_builtin_dictionary(self):
-        # In case this is translated, the following runs at run-time.
-        # It's not possible to make a dictionary with keys that are function
-        # pointers at translation-time, as the actual address of each
-        # function could change from run to run.
-        if we_are_translated():
-            self.builtins = {}
-            for i in range(len(self.builtins_keys)):
-                self.builtins[self.builtins_keys[i]] = self.builtins_values[i]
-
-    def builtins_get(self, addr):
-        assert lltype.typeOf(addr) == llmemory.Address
-        if we_are_translated():
-            return self.builtins.get(addr, (None, None))
-        else:
-            for i in range(len(self.builtins_keys)):
-                if self.builtins_keys[i] == addr:
-                    return self.builtins_values[i]
-            return (None, None)
-
     # ____________________________________________________________
     # construction-time interface
 

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/resoperation.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/resoperation.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/resoperation.py	Sun Mar  1 17:39:23 2009
@@ -126,6 +126,7 @@
     OOIS                   = 72
     OOISNOT                = 73
     #
+    ARRAYLEN_GC            = 77
     STRLEN                 = 78
     STRGETITEM             = 79
     _ALWAYS_PURE_LAST = 79  # ----- end of always_pure operations -----

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_basic.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_basic.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_basic.py	Sun Mar  1 17:39:23 2009
@@ -17,7 +17,8 @@
     stats = history.Stats()
     cpu = CPUClass(rtyper, stats, False)
     graph = rtyper.annotator.translator.graphs[0]
-    metainterp = pyjitpl.OOMetaInterp(graph, [], cpu, stats, False)
+    opt = history.Options(specialize=False, listops=False)
+    metainterp = pyjitpl.OOMetaInterp(graph, [], cpu, stats, opt)
     metainterp.num_green_args = 0
     return metainterp, rtyper
 

Modified: pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_slist.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_slist.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_slist.py	Sun Mar  1 17:39:23 2009
@@ -46,7 +46,7 @@
                 lst[0] += 2
                 n -= 1
             return lst[0]
-        res = self.meta_interp(f, [21])
+        res = self.meta_interp(f, [21], listops=True)
         assert res == 42
         # no more list operations in the loop
         py.test.skip("not a ModifiedList yet")



More information about the Pypy-commit mailing list