Commits

Maciej Fijalkowski  committed cb06fc4

hack differently - explicit getfield on vrefs (that does not necesarilly
force them)

  • Participants
  • Parent commits 86d4d71
  • Branches vref-copy

Comments (0)

Files changed (5)

File pypy/rlib/_jit_vref.py

     def getattr(self, s_attr):
         if s_attr.const == 'virtual':
             return annmodel.s_Bool
-        elif s_attr.const == 'dereference_or_copy':
-            return self.s_instance
-        else:
-            raise AssertionError("Unknown attribute %s" % s_attr.const)
+        return annmodel.SomeObject.getattr(self, s_attr)
+
+    def method_getfield(self, s_name):
+        return self.s_instance.getattr(s_name)
 
     def rtyper_makerepr(self, rtyper):
         if rtyper.type_system.name == 'lltypesystem':
         v = hop.inputarg(self, arg=0)
         if s_attr.const == 'virtual':
             return hop.genop('jit_is_virtual', [v], resulttype = lltype.Bool)
-        elif s_attr.const == 'dereference_or_copy':
-            v_result = hop.genop('jit_dereference_or_copy', [v],
-                                 resulttype = OBJECTPTR)
-            return hop.genop('cast_pointer', [v_result],
-                             resulttype = hop.r_result)
-        else:
-            raise AssertionError("Unknown attribute %s" % s_attr.const)
+        return Repr.rtype_getattr(self, hop)
+
+    def rtype_method_getfield(self, hop):
+        attr = hop.args_s[1].const
+        hop.exception_cannot_occur()
+        v = hop.inputarg(self, arg=0)
+        c_name = hop.inputconst(lltype.Void, attr)
+        r_arg = hop.rtyper.getrepr(hop.args_s[0].s_instance)
+        v2 = hop.genop('cast_pointer', [v], resulttype=r_arg)
+        return hop.genop('jit_vref_getfield', [v2, c_name],
+                         resulttype = hop.r_result)
 
 from pypy.rpython.ootypesystem.rclass import OBJECT
 

File pypy/rlib/jit.py

         be forced by the '()' operator."""
         return self._state == 'non-forced'
 
-    @property
-    def dereference_or_copy(self):
-        """ Get a forced version, but without forcing the original virtual.
-        Useful for things like profilers where we want the object, but
-        we don't care if modifications will be reflected in the underlaying
-        JIT code.
+    def getfield(self, fieldname):
+        """ Get a field, either by reading a field directly if the reference
+        is not virtual at all, or will fish it from the resume data. If
+        the field is itself virtual, you'll receive a null pointer.
         """
-        # note that this always returns the original object and never
-        # a copy when untranslated
-        return self._x
+        return getattr(self._x, fieldname)
 
     def _finish(self):
         if self._state == 'non-forced':

File pypy/rlib/test/test__jit_vref.py

         x = self.interpret(f, [])
         assert x is False
 
-    def test_rtype_dereference_or_copy(self):
+    def test_rtype_getfield(self):
+        class X(object):
+            def __init__(self, x):
+                self.x = x
+        
         def f():
-            vref = virtual_ref(X())
-            return vref.dereference_or_copy.x
+            vref = virtual_ref(X(1))
+            return vref.getfield('x')
         x = self.interpret(f, [])
-        assert x == 3
+        assert x == 1
 
 class TestLLtype(BaseTestVRef, LLRtypeMixin):
     OBJECTTYPE = OBJECTPTR
     def castable(self, TO, var):
         return ootype.isSubclass(lltype.typeOf(var), TO)
 
-    def test_rtype_dereference_or_copy(self):
+    def test_rtype_getfield(self):
         py.test.skip("not supported")

File pypy/rpython/lltypesystem/lloperation.py

     'jit_force_virtualizable':LLOp(canrun=True),
     'jit_force_virtual':    LLOp(canrun=True),
     'jit_is_virtual':       LLOp(canrun=True),
-    'jit_dereference_or_copy': LLOp(canrun=True),
     'jit_force_quasi_immutable': LLOp(canrun=True),
     'jit_record_known_class'  : LLOp(canrun=True),
+    'jit_vref_getfield' : LLOp(canrun=True),
     'get_exception_addr':   LLOp(),
     'get_exc_value_addr':   LLOp(),
     'do_malloc_fixedsize_clear':LLOp(canmallocgc=True),

File pypy/rpython/lltypesystem/opimpl.py

 def op_jit_force_virtual(x):
     return x
 
+def op_jit_vref_getfield(x, field):
+    return getattr(x, 'inst_' + field)
+
 def op_jit_is_virtual(x):
     return False
 
-def op_jit_dereference_or_copy(x):
-    return x
-
 def op_jit_force_quasi_immutable(*args):
     pass