[pypy-svn] r36555 - in pypy/dist/pypy/rpython: . test

fijal at codespeak.net fijal at codespeak.net
Fri Jan 12 12:53:22 CET 2007


Author: fijal
Date: Fri Jan 12 12:53:03 2007
New Revision: 36555

Modified:
   pypy/dist/pypy/rpython/extfunc.py
   pypy/dist/pypy/rpython/test/test_extfunc.py
Log:
(arigo, cfbolz, fijal) - Add possibility to create different implementations for different typesystems.


Modified: pypy/dist/pypy/rpython/extfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunc.py	(original)
+++ pypy/dist/pypy/rpython/extfunc.py	Fri Jan 12 12:53:03 2007
@@ -1,6 +1,7 @@
 
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem.lltype import typeOf
+from pypy.objspace.flow.model import Constant
 
 class ExtFuncEntry(ExtRegistryEntry):
     def compute_result_annotation(self, *args_s):
@@ -8,6 +9,16 @@
                "Argument number mismatch"
         for arg, expected in zip(args_s, self.signature_args):
             assert expected.contains(arg)
+
+        for type_system in ['lltype', 'ootype']:
+            impl = getattr(self, type_system + 'impl', None)
+            if impl:
+                key = impl.im_func
+                pbc = self.bookkeeper.immutablevalue(impl.im_func)
+                s_result = self.bookkeeper.emulate_pbc_call(key, pbc,
+                       self.signature_args)
+                assert self.signature_result.contains(s_result)
+        
         return self.signature_result
 
     def specialize_call(self, hop):
@@ -17,10 +28,17 @@
         r_result = rtyper.getrepr(self.signature_result)
         ll_result = r_result.lowleveltype
         name = getattr(self, 'name', None) or self.instance.__name__
-        obj = rtyper.type_system.getexternalcallable(args_ll, ll_result, name,
-                                      _entry=self, _callable=self.instance)
-        vlist = [hop.inputconst(typeOf(obj), obj)] + hop.inputargs(*args_r)
-        hop.exception_is_here()
-        return hop.genop('direct_call', vlist, r_result)
-
-#def register_
+        method_name = rtyper.type_system.name[:2] + 'typeimpl'
+        impl = getattr(self, method_name, None)
+        if impl:
+            hop2 = hop.copy()
+            v = Constant(impl.im_func)
+            bookkeeper = rtyper.annotator.bookkeeper
+            hop2.v_s_insertfirstarg(v, bookkeeper.immutablevalue(impl.im_func))
+            return hop2.dispatch()
+        else:
+            obj = rtyper.type_system.getexternalcallable(args_ll, ll_result,
+                                 name, _entry=self, _callable=self.instance)
+            vlist = [hop.inputconst(typeOf(obj), obj)] + hop.inputargs(*args_r)
+            hop.exception_is_here()
+            return hop.genop('direct_call', vlist, r_result)

Modified: pypy/dist/pypy/rpython/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_extfunc.py	(original)
+++ pypy/dist/pypy/rpython/test/test_extfunc.py	Fri Jan 12 12:53:03 2007
@@ -31,4 +31,21 @@
     res = interpret(f, [])
     assert res == 42
 
-    
+def c(y, x):
+    yyy
+
+class CTestFuncEntry(ExtFuncEntry):
+    _about_ = c
+    name = 'ccc'
+    signature_args = [annmodel.SomeInteger()] * 2
+    signature_result = annmodel.SomeInteger()
+
+    def lltypeimpl(y, x):
+        return y + x
+
+def test_interp_c():
+    def f():
+        return c(3, 4)
+
+    res = interpret(f, [])
+    assert res == 7



More information about the Pypy-commit mailing list