[pypy-svn] r65877 - in pypy/branch/pyjitpl5/pypy/rpython: . lltypesystem ootypesystem test

arigo at codespeak.net arigo at codespeak.net
Tue Jun 23 13:09:29 CEST 2009


Author: arigo
Date: Tue Jun 23 13:09:27 2009
New Revision: 65877

Modified:
   pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rvirtualizable2.py
   pypy/branch/pyjitpl5/pypy/rpython/ootypesystem/rvirtualizable2.py
   pypy/branch/pyjitpl5/pypy/rpython/rvirtualizable2.py
   pypy/branch/pyjitpl5/pypy/rpython/test/test_rvirtualizable2.py
Log:
Simplify the rtyper attaching attributes to virtualizables.
For now, just attaches 'virtualizable2_accessor' as a hint.


Modified: pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rvirtualizable2.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rvirtualizable2.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/lltypesystem/rvirtualizable2.py	Tue Jun 23 13:09:27 2009
@@ -34,37 +34,34 @@
 class Virtualizable2InstanceRepr(AbstractVirtualizable2InstanceRepr, InstanceRepr):
 
     VirtualizableAccessor = VirtualizableAccessor
-    op_getfield = 'getfield'
-    op_setfield = 'setfield'
+##    op_getfield = 'getfield'
+##    op_setfield = 'setfield'
 
-    def _setup_instance_repr(self):
-        llfields = []
-        if self.top_of_virtualizable_hierarchy:
-            llfields.append(('vable_base', llmemory.Address))
-            llfields.append(('vable_rti', VABLERTIPTR))
-        InstanceRepr._setup_repr(self, llfields,
-                                 hints = {'virtualizable2': True,
-                                          'virtuals' : self.virtuals},
-                                 adtmeths = {'access': self.accessor})
+##    def _setup_instance_repr(self):
+##        llfields = []
+##        if self.top_of_virtualizable_hierarchy:
+##            llfields.append(('vable_base', llmemory.Address))
+##            llfields.append(('vable_rti', VABLERTIPTR))
+##        InstanceRepr._setup_repr(self, llfields,
+##                                 hints = {'virtualizable2': True,
+##                                          'virtuals' : self.virtuals},
+##                                 adtmeths = {'access': self.accessor})
 
-    def gencast(self, llops, vinst):
-        return llops.genop('cast_pointer', [vinst], resulttype=self)
-
-    def get_mangled_fields(self):
-        return [mangled_name for _, (mangled_name, _) in self.fields.items()]
+##    def gencast(self, llops, vinst):
+##        return llops.genop('cast_pointer', [vinst], resulttype=self)
 
     def get_field(self, attr):
         return self.fields[attr]
 
-    def is_in_fields(self, attr):
-        return attr in self.fields
+##    def is_in_fields(self, attr):
+##        return attr in self.fields
 
-    def set_vable(self, llops, vinst, force_cast=False):
-        if self.top_of_virtualizable_hierarchy:
-            if force_cast:
-                vinst = llops.genop('cast_pointer', [vinst], resulttype=self)
-            cname = inputconst(lltype.Void, 'vable_rti')
-            vvalue = inputconst(VABLERTIPTR, lltype.nullptr(VABLERTIPTR.TO))
-            llops.genop('setfield', [vinst, cname, vvalue])
-        else:
-            self.rbase.set_vable(llops, vinst, force_cast=True)
+##    def set_vable(self, llops, vinst, force_cast=False):
+##        if self.top_of_virtualizable_hierarchy:
+##            if force_cast:
+##                vinst = llops.genop('cast_pointer', [vinst], resulttype=self)
+##            cname = inputconst(lltype.Void, 'vable_rti')
+##            vvalue = inputconst(VABLERTIPTR, lltype.nullptr(VABLERTIPTR.TO))
+##            llops.genop('setfield', [vinst, cname, vvalue])
+##        else:
+##            self.rbase.set_vable(llops, vinst, force_cast=True)

Modified: pypy/branch/pyjitpl5/pypy/rpython/ootypesystem/rvirtualizable2.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/ootypesystem/rvirtualizable2.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/ootypesystem/rvirtualizable2.py	Tue Jun 23 13:09:27 2009
@@ -7,36 +7,34 @@
 
 class VirtualizableAccessor(AbstractVirtualizableAccessor):
 
-    def initialize(self, TYPE, redirected_fields, PARENT=None):
-        pass # TODO
+    def initialize(self, TYPE, redirected_fields):
+        self.TYPE = TYPE
+        self.redirected_fields = redirected_fields
 
-    def prepare_getsets(self):
-        self.getsets = {} # TODO
+##    def prepare_getsets(self):
+##        self.getsets = {} # TODO
 
 
 class Virtualizable2InstanceRepr(AbstractVirtualizable2InstanceRepr, InstanceRepr):
 
     VirtualizableAccessor = VirtualizableAccessor
-    op_getfield = 'oogetfield'
-    op_setfield = 'oosetfield'
+##    op_getfield = 'oogetfield'
+##    op_setfield = 'oosetfield'
 
-    def _setup_instance_repr(self):
-        InstanceRepr._setup_repr(self, hints = {'virtualizable2': True,
-                                                'virtuals' : self.virtuals})
+##    def _setup_instance_repr(self):
+##        InstanceRepr._setup_repr(self, hints = {'virtualizable2': True,
+##                                                'virtuals' : self.virtuals})
 
-    def gencast(self, llops, vinst):
-        raise NotImplementedError
-
-    def get_mangled_fields(self):
-        return self.allfields.keys()
+##    def gencast(self, llops, vinst):
+##        raise NotImplementedError
 
     def get_field(self, attr):
         mangled = mangle(attr, self.rtyper.getconfig())
         return mangled, self.allfields[mangled]
 
-    def is_in_fields(self, attr):
-        mangled = mangle(attr, self.rtyper.getconfig())
-        return mangled in self.allfields
+##    def is_in_fields(self, attr):
+##        mangled = mangle(attr, self.rtyper.getconfig())
+##        return mangled in self.allfields
 
-    def set_vable(self, llops, vinst, force_cast=False):
-        pass # TODO
+##    def set_vable(self, llops, vinst, force_cast=False):
+##        pass # TODO

Modified: pypy/branch/pyjitpl5/pypy/rpython/rvirtualizable2.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/rvirtualizable2.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/rvirtualizable2.py	Tue Jun 23 13:09:27 2009
@@ -6,29 +6,23 @@
 
 class AbstractVirtualizableAccessor(object):
 
-    def initialize(self, TYPE, redirected_fields, PARENT=None):
+    def initialize(self, TYPE, redirected_fields):
         self.TYPE = TYPE
         self.redirected_fields = redirected_fields
-        self.subaccessors = []
-        if PARENT is None:
-            self.parent = None
-        else:
-            self.parent = PARENT.access
-            self.parent.subaccessors.append(self)
 
     def __repr__(self):
         return '<VirtualizableAccessor for %s>' % getattr(self, 'TYPE', '?')
 
-    def __getattr__(self, name):
-        if name.startswith('getset') and 'getsets' not in self.__dict__:
-            self.prepare_getsets()
-            return getattr(self, name)
-        else:
-            raise AttributeError("%s object has no attribute %r" % (
-                self.__class__.__name__, name))
+##    def __getattr__(self, name):
+##        if name.startswith('getset') and 'getsets' not in self.__dict__:
+##            self.prepare_getsets()
+##            return getattr(self, name)
+##        else:
+##            raise AttributeError("%s object has no attribute %r" % (
+##                self.__class__.__name__, name))
 
-    def prepare_getsets(self):
-        raise NotImplementedError
+##    def prepare_getsets(self):
+##        raise NotImplementedError
 
     def _freeze_(self):
         return True
@@ -37,8 +31,8 @@
 class AbstractVirtualizable2InstanceRepr(AbstractInstanceRepr):
 
     VirtualizableAccessor = AbstractVirtualizableAccessor
-    op_getfield = None
-    op_setfield = None
+##    op_getfield = None
+##    op_setfield = None
 
     def _super(self):
         return super(AbstractVirtualizable2InstanceRepr, self)
@@ -50,73 +44,71 @@
             basedesc = classdesc.basedesc
             assert basedesc is None or basedesc.lookup('_virtualizable2_') is None
             self.top_of_virtualizable_hierarchy = True
+            self.accessor = self.VirtualizableAccessor()
         else:
             self.top_of_virtualizable_hierarchy = False
-        try:
-            self.virtuals = tuple(classdesc.classdict['_always_virtual_'].value)
-        except KeyError:
-            self.virtuals = ()
-        self.accessor = self.VirtualizableAccessor()
-
-    def _setup_instance_repr(self):
-        raise NotImplementedError
 
-    def gencast(self, llops, vinst):
-        raise NotImplementedError
+##    def _setup_instance_repr(self):
+##        raise NotImplementedError
 
-    def set_vable(self, llops, vinst, force_cast=False):
-        raise NotImplementedError
+##    def gencast(self, llops, vinst):
+##        raise NotImplementedError
 
-    def get_mangled_fields(self):
-        raise NotImplementedError
+##    def set_vable(self, llops, vinst, force_cast=False):
+##        raise NotImplementedError
 
     def get_field(self, attr):
         raise NotImplementedError
 
-    def is_in_fields(self, attr):
-        raise NotImplementedError
+##    def is_in_fields(self, attr):
+##        raise NotImplementedError
 
     def _setup_repr(self):
-        self._setup_instance_repr()
-        my_redirected_fields = []
-        for mangled_name in self.get_mangled_fields():
-            my_redirected_fields.append(mangled_name)
-        self.my_redirected_fields = dict.fromkeys(my_redirected_fields)    
         if self.top_of_virtualizable_hierarchy:
+            hints = {'virtualizable2_accessor': self.accessor}
+            self._super()._setup_repr(hints = hints)
+            my_redirected_fields = []
+            c_vfields = self.classdef.classdesc.classdict['_virtualizable2_']
+            for name in c_vfields.value:
+                if name.endswith('[*]'):
+                    name = name[:-3]
+                    suffix = '[*]'
+                else:
+                    suffix = ''
+                mangled_name, r = self.get_field(name)
+                my_redirected_fields.append(mangled_name + suffix)
             self.accessor.initialize(self.object_type, my_redirected_fields)
         else:
-            BASE = self.rtyper.type_system.derefType(self.rbase.lowleveltype)
-            self.accessor.initialize(self.object_type, my_redirected_fields,
-                                     BASE)
-
-    def new_instance(self, llops, classcallhop=None):
-        vptr = self._super().new_instance(llops, classcallhop)
-        self.set_vable(llops, vptr)
-        return vptr
-
-    def getfield(self, vinst, attr, llops, force_cast=False, flags={}):
-        """Read the given attribute (or __class__ for the type) of 'vinst'."""
-        if not flags.get('access_directly') and self.is_in_fields(attr):
-            mangled_name, r = self.get_field(attr)
-            if mangled_name in self.my_redirected_fields:
-                if force_cast:
-                    vinst = self.gencast(llops, vinst)
-                c_name = inputconst(lltype.Void, mangled_name)
-                llops.genop('promote_virtualizable', [vinst, c_name])
-                return llops.genop(self.op_getfield, [vinst, c_name],
-                                   resulttype=r)
-        return self._super().getfield(vinst, attr, llops, force_cast)
-
-    def setfield(self, vinst, attr, vvalue, llops, force_cast=False,
-                 flags={}):
-        """Write the given attribute (or __class__ for the type) of 'vinst'."""
-        if not flags.get('access_directly') and self.is_in_fields(attr):
-            mangled_name, r = self.get_field(attr)
-            if mangled_name in self.my_redirected_fields:
-                if force_cast:
-                    vinst = self.gencast(llops, vinst)
-                c_name = inputconst(lltype.Void, mangled_name)
-                llops.genop('promote_virtualizable', [vinst, c_name])
-                llops.genop(self.op_setfield, [vinst, c_name, vvalue])
-                return
-        self._super().setfield(vinst, attr, vvalue, llops, force_cast)
+            self._super()._setup_repr()
+
+##    def new_instance(self, llops, classcallhop=None):
+##        vptr = self._super().new_instance(llops, classcallhop)
+##        self.set_vable(llops, vptr)
+##        return vptr
+
+##    def getfield(self, vinst, attr, llops, force_cast=False, flags={}):
+##        """Read the given attribute (or __class__ for the type) of 'vinst'."""
+##        if not flags.get('access_directly') and self.is_in_fields(attr):
+##            mangled_name, r = self.get_field(attr)
+##            if mangled_name in self.my_redirected_fields:
+##                if force_cast:
+##                    vinst = self.gencast(llops, vinst)
+##                c_name = inputconst(lltype.Void, mangled_name)
+##                llops.genop('promote_virtualizable', [vinst, c_name])
+##                return llops.genop(self.op_getfield, [vinst, c_name],
+##                                   resulttype=r)
+##        return self._super().getfield(vinst, attr, llops, force_cast)
+
+##    def setfield(self, vinst, attr, vvalue, llops, force_cast=False,
+##                 flags={}):
+##        """Write the given attribute (or __class__ for the type) of 'vinst'."""
+##        if not flags.get('access_directly') and self.is_in_fields(attr):
+##            mangled_name, r = self.get_field(attr)
+##            if mangled_name in self.my_redirected_fields:
+##                if force_cast:
+##                    vinst = self.gencast(llops, vinst)
+##                c_name = inputconst(lltype.Void, mangled_name)
+##                llops.genop('promote_virtualizable', [vinst, c_name])
+##                llops.genop(self.op_setfield, [vinst, c_name, vvalue])
+##                return
+##        self._super().setfield(vinst, attr, vvalue, llops, force_cast)

Modified: pypy/branch/pyjitpl5/pypy/rpython/test/test_rvirtualizable2.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/rpython/test/test_rvirtualizable2.py	(original)
+++ pypy/branch/pyjitpl5/pypy/rpython/test/test_rvirtualizable2.py	Tue Jun 23 13:09:27 2009
@@ -4,13 +4,14 @@
 
 
 class V(object):
-    _virtualizable2_ = True
+    _virtualizable2_ = ['v']
 
     def __init__(self, v):
         self.v = v
 
 class BaseTest(BaseRtypingTest):
     def test_generate_promote_virtualizable(self):
+        py.test.skip("later")
         def fn(n):
             vinst = V(n)
             return vinst.v
@@ -23,10 +24,44 @@
         assert op_promote.opname == 'promote_virtualizable'
         assert op_promote.args[0] is v_inst
         TYPE = self.gettype(v_inst)
-        assert TYPE._hints['virtualizable2']
+        assert TYPE._hints['virtualizable2'] == True
+
+    def test_accessor(self):
+        class Base(object):
+            pass
+        class V(Base):
+            _virtualizable2_ = ['v1', 'v2[*]']
+        class W(V):
+            pass
+        #
+        def fn1(n):
+            Base().base1 = 42
+            V().v1 = 43
+            V().v2 = ['x', 'y']
+            W().w1 = 44
+            return V()
+        _, _, graph = self.gengraph(fn1, [int])
+        v_inst = graph.getreturnvar()
+        TYPE = self.gettype(v_inst)
+        accessor = TYPE._hints['virtualizable2_accessor']
+        assert accessor.TYPE == TYPE
+        assert accessor.redirected_fields == [self.prefix + 'v1',
+                                              self.prefix + 'v2[*]']
+        #
+        def fn2(n):
+            Base().base1 = 42
+            V().v1 = 43
+            V().v2 = ['x', 'y']
+            W().w1 = 44
+            return W()
+        _, _, graph = self.gengraph(fn2, [int])
+        w_inst = graph.getreturnvar()
+        TYPE = self.gettype(w_inst)
+        assert 'virtualizable2_accessor' not in TYPE._hints
 
 
 class TestLLtype(LLRtypeMixin, BaseTest):
+    prefix = 'inst_'
 
     def gettype(self, v):
         return v.concretetype.TO
@@ -39,9 +74,11 @@
         assert res.item1 == 42
         res = lltype.normalizeptr(res.item0)
         assert res.inst_v == 42
+        py.test.skip("later")
         assert not res.vable_rti
 
 class TestOOtype(OORtypeMixin, BaseTest):
+    prefix = 'o'
 
     def gettype(self, v):
         return v.concretetype



More information about the Pypy-commit mailing list