Commits

Maciej Fijalkowski  committed 0235d65 Draft

start hacking on vref-getfield

  • Participants
  • Parent commits cb06fc4
  • Branches vref-copy

Comments (0)

Files changed (5)

File pypy/jit/metainterp/test/test_virtualref.py

         res = self.meta_interp(f, [10])
         assert res == 0
 
+    def test_vref_getfield(self):
+        driver = JitDriver(greens = [], reds = ['n', 's'])
+
+        class X(object):
+            def __init__(self, x):
+                self.x = x
+
+        @dont_look_inside
+        def residual(vref):
+            return vref.getfield('x')
+
+        def f(n):
+            s = 0
+            while n > 0:
+                driver.jit_merge_point(n=n, s=s)
+                x = X(1)
+                v = virtual_ref(x)
+                s += residual(v)
+                virtual_ref_finish(v, x)
+                n -= 1
+            return s
+
+        res = self.meta_interp(f, [10])
+        assert res == 10
 
 class TestLLtype(VRefTests, LLJitMixin):
     pass

File pypy/jit/metainterp/virtualref.py

     def _freeze_(self):
         return True
 
+    def _find_type_of_virtualref(self):
+        # XXX limitation is that we can only have one type
+        for graph in graphs:
+            for block in graph.iterblocks():
+                for op in block.operations:
+
     def replace_force_virtual_with_call(self, graphs):
         # similar to rvirtualizable2.replace_force_virtualizable_with_call().
         c_force_virtual_ptr = None
         c_is_virtual_ptr = None
+        c_getfield_ptrs = {} # fieldname -> function
         force_virtual_count = 0
+        self._find_type_of_virtualref()
         for graph in graphs:
             for block in graph.iterblocks():
                 for op in block.operations:
                         #
                         op.opname = 'direct_call'
                         op.args = [c_is_virtual_ptr, op.args[0]]
+                    if op.opname == 'jit_vref_getfield':
+                        # get a function for each field
+                        key = op.args[1].value
+                        c_func = c_getfield_ptrs.get(key, None)
+                        if c_func is None:
+                            c_func = self.get_vref_getfield_fnptr(key,
+                                                        op.result.concretetype)
+                            c_getfield_ptrs[key] = c_func
+                        op.opname = 'direct_call'
+                        op.args = [c_func, op.args[0]]
         #
         if c_force_virtual_ptr is not None:
             log("replaced %d 'jit_force_virtual' with %r" % (force_virtual_count,
             force_virtual_if_necessary)
         return inputconst(lltype.typeOf(funcptr), funcptr)
 
+    def get_vref_getfield_fnptr(self, name, RES_TP):
+        def read_virtual_field(inst):
+            if inst.typeptr != self.jit_virtual_ref_vtable:
+                lltype.cast_ptr(
+                xxx
+            xxx
+        FUNC = lltype.FuncType([rclass.OBJECTPTR], RES_TP)
+        funcptr = self.warmrunnerdesc.helper_func(
+            lltype.Ptr(FUNC),
+            read_virtual_field)
+        return inputconst(lltype.typeOf(funcptr), funcptr)
+
     def get_is_virtual_fnptr(self):
         #
         def is_virtual(inst):

File pypy/rlib/_jit_vref.py

 
     def specialize_call(self, hop):
         r_generic_object = getinstancerepr(hop.rtyper, None)
+        hop.genop('jit_record_vref', [hop.args_v[0]], resulttype=lltype.Void)
         [v] = hop.inputargs(r_generic_object)   # might generate a cast_pointer
         hop.exception_cannot_occur()
         return v
         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],
+        return hop.genop('jit_vref_getfield', [v, c_name],
                          resulttype = hop.r_result)
 
 from pypy.rpython.ootypesystem.rclass import OBJECT

File pypy/rpython/lltypesystem/lloperation.py

     'jit_force_quasi_immutable': LLOp(canrun=True),
     'jit_record_known_class'  : LLOp(canrun=True),
     'jit_vref_getfield' : LLOp(canrun=True),
+    'jit_record_vref': 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

     return x
 
 def op_jit_vref_getfield(x, field):
-    return getattr(x, 'inst_' + field)
+    # XXX is this even a correct hack?
+    return getattr(x._obj._parentstructure(), 'inst_' + field)
 
 def op_jit_is_virtual(x):
     return False