[pypy-svn] r52214 - in pypy/branch/jit-refactoring/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Thu Mar 6 15:28:49 CET 2008


Author: arigo
Date: Thu Mar  6 15:28:48 2008
New Revision: 52214

Modified:
   pypy/branch/jit-refactoring/pypy/rpython/annlowlevel.py
   pypy/branch/jit-refactoring/pypy/rpython/test/test_llann.py
Log:
Oh well.  Very ad hoc function to support the JIT...


Modified: pypy/branch/jit-refactoring/pypy/rpython/annlowlevel.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/rpython/annlowlevel.py	(original)
+++ pypy/branch/jit-refactoring/pypy/rpython/annlowlevel.py	Thu Mar  6 15:28:48 2008
@@ -367,6 +367,56 @@
 
 # ____________________________________________________________
 
+def llstructofhelpers(P, initdata):
+    """Ad hoc.  Returns an immortal structure of type P.TO whose fields
+    are low-level function pointers, initialized according to the
+    constant dict 'initdata'."""
+    # implementation for the purpose of direct running only
+    # XXX this is a workaround for the fact that prebuilt llhelpers cannot
+    # be translated
+    # XXX this is all happy hacking :-/
+    result = lltype.malloc(P.TO, immortal=True, zero=True)
+    for name, callable in initdata.items():
+        F = eval('P.TO.%s' % name)
+        fnptr = llhelper(F, callable)
+        exec 'result.%s = fnptr' % name
+    return result
+
+class LLStructOfHelpersEntry(extregistry.ExtRegistryEntry):
+    _about_ = llstructofhelpers
+
+    def compute_result_annotation(self, s_P, s_initdata):
+        assert s_P.is_constant()
+        assert s_initdata.is_constant()
+        P = s_P.const
+        initdata = s_initdata.const
+        for name, callable in initdata.items():
+            F = eval('P.TO.%s' % name)
+            s_callable = self.bookkeeper.immutablevalue(callable)
+            args_s = [annmodel.lltype_to_annotation(T) for T in F.TO.ARGS]
+            key = (name, llhelper, s_callable.const)
+            s_res = self.bookkeeper.emulate_pbc_call(name, s_callable, args_s)
+            assert annmodel.lltype_to_annotation(F.TO.RESULT).contains(s_res)
+        return annmodel.SomePtr(P)
+
+    def specialize_call(self, hop):
+        bk = hop.rtyper.annotator.bookkeeper
+        P = hop.args_s[0].const
+        initdata = hop.args_s[1].const
+        result = lltype.malloc(P.TO, immortal=True, zero=True)
+        for name, callable in initdata.items():
+            F = eval('P.TO.%s' % name)
+            s_callable = bk.immutablevalue(callable)
+            r_callable = hop.rtyper.getrepr(s_callable)
+            c_fnptr = r_callable.get_unique_llfn()
+            assert isinstance(c_fnptr, Constant)
+            fnptr = c_fnptr.value
+            exec 'result.%s = fnptr' % name
+        hop.exception_cannot_occur()
+        return hop.inputconst(P, result)
+
+# ____________________________________________________________
+
 def hlstr(ll_s):
     if hasattr(ll_s, 'items'):
         return ''.join(items)

Modified: pypy/branch/jit-refactoring/pypy/rpython/test/test_llann.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/rpython/test/test_llann.py	(original)
+++ pypy/branch/jit-refactoring/pypy/rpython/test/test_llann.py	Thu Mar  6 15:28:48 2008
@@ -8,6 +8,7 @@
 from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
 from pypy.rpython.annlowlevel import PseudoHighLevelCallable
 from pypy.rpython.annlowlevel import llhelper, cast_instance_to_base_ptr
+from pypy.rpython.annlowlevel import llstructofhelpers
 from pypy.rpython.annlowlevel import base_ptr_lltype
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.rpython.test.test_llinterp import interpret
@@ -478,6 +479,30 @@
     assert res == 99
 
 
+def test_llstructofhelpers():
+    F = Ptr(FuncType([], Signed))
+    S1 = Struct('S1', ('f1', F))
+    S2 = Struct('S2', ('parent', S1), ('f2', F))
+
+    def myf1():
+        return 401 + we_are_translated()
+
+    def myf2():
+        return 602 + we_are_translated()
+
+    initdata = {'parent.f1': myf1,
+                'f2': myf2}
+
+    def h():
+        s2 = llstructofhelpers(Ptr(S2), initdata)
+        assert typeOf(s2) == Ptr(S2)
+        return s2.parent.f1() + s2.f2()
+
+    assert h() == 401 + 602
+    res = interpret(h, [])
+    assert res == 401 + 602 + 2
+
+
 def test_cast_instance_to_base_ptr():
     class A:
         def __init__(self, x, y):



More information about the Pypy-commit mailing list