[pypy-svn] r77555 - in pypy/branch/jitffi/pypy: jit/metainterp/test rlib rlib/test

antocuni at codespeak.net antocuni at codespeak.net
Mon Oct 4 10:10:37 CEST 2010


Author: antocuni
Date: Mon Oct  4 10:10:35 2010
New Revision: 77555

Added:
   pypy/branch/jitffi/pypy/rlib/test/test_libffi.py   (contents, props changed)
Modified:
   pypy/branch/jitffi/pypy/jit/metainterp/test/test_direct_call.py
   pypy/branch/jitffi/pypy/rlib/libffi.py
Log:
add a more user-friendly interface to build argchains. Moreover, checkin test_libffi, which I forgot to svn add earlier :-(


Modified: pypy/branch/jitffi/pypy/jit/metainterp/test/test_direct_call.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/metainterp/test/test_direct_call.py	(original)
+++ pypy/branch/jitffi/pypy/jit/metainterp/test/test_direct_call.py	Mon Oct  4 10:10:35 2010
@@ -2,7 +2,7 @@
 import py
 from pypy.rlib.jit import JitDriver, hint
 from pypy.jit.metainterp.test.test_basic import LLJitMixin
-from pypy.rlib.libffi import CDLL, ffi_type_sint, IntArg, Func
+from pypy.rlib.libffi import CDLL, ffi_type_sint, ArgChain, Func
 from pypy.tool.udir import udir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.platform import platform
@@ -34,10 +34,9 @@
                 driver.jit_merge_point(n=n, func=func)
                 driver.can_enter_jit(n=n, func=func)
                 func = hint(func, promote=True)
-                arg0 = IntArg(n)
-                arg1 = IntArg(1)
-                arg0.next = arg1
-                n = func.call(arg0, lltype.Signed)
+                argchain = ArgChain()
+                argchain.int(n).int(1)
+                n = func.call(argchain, lltype.Signed)
             return n
             
         res = self.meta_interp(f, [0])

Modified: pypy/branch/jitffi/pypy/rlib/libffi.py
==============================================================================
--- pypy/branch/jitffi/pypy/rlib/libffi.py	(original)
+++ pypy/branch/jitffi/pypy/rlib/libffi.py	Mon Oct  4 10:10:35 2010
@@ -18,6 +18,28 @@
 
 # ----------------------------------------------------------------------
 
+class ArgChain(object):
+    first = None
+    last = None
+    numargs = 0
+
+    def int(self, intval):
+        self._append(IntArg(intval))
+        return self
+
+    def float(self, floatval):
+        self._append(FloatArg(floatval))
+        return self
+
+    def _append(self, arg):
+        if self.first is None:
+            self.first = self.last = arg
+        else:
+            self.last.next = arg
+            self.last = arg
+        self.numargs += 1
+    
+
 class AbstractArg(object):
     next = None
 
@@ -116,7 +138,7 @@
         # assuming that archain is completely virtual.
         ll_args = self._prepare()
         i = 0
-        arg = argchain
+        arg = argchain.first
         while arg:
             arg.push(self, ll_args, i)
             i += 1

Added: pypy/branch/jitffi/pypy/rlib/test/test_libffi.py
==============================================================================
--- (empty file)
+++ pypy/branch/jitffi/pypy/rlib/test/test_libffi.py	Mon Oct  4 10:10:35 2010
@@ -0,0 +1,57 @@
+import py
+import sys
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.rpython.lltypesystem.ll2ctypes import ALLOCATED
+from pypy.rlib.test.test_clibffi import BaseFfiTest, get_libm_name
+from pypy.rlib.libffi import CDLL, Func, get_libc_name, ArgChain
+from pypy.rlib.libffi import ffi_type_double, ffi_type_void
+
+
+class TestLibffi(BaseFfiTest):
+    """
+    Test the new JIT-friendly interface to libffi
+    """
+
+    def get_libc(self):
+        return CDLL(get_libc_name())
+    
+    def get_libm(self):
+        return CDLL(get_libm_name(sys.platform))
+
+    def test_argchain(self):
+        chain = ArgChain()
+        assert chain.numargs == 0
+        chain2 = chain.int(42)
+        assert chain2 is chain
+        assert chain.numargs == 1
+        intarg = chain.first
+        assert chain.last is intarg
+        assert intarg.intval == 42
+        chain.float(123.45)
+        assert chain.numargs == 2
+        assert chain.first is intarg
+        assert intarg.next is chain.last
+        floatarg = intarg.next
+        assert floatarg.floatval == 123.45
+
+    def test_library_open(self):
+        lib = self.get_libc()
+        del lib
+        assert not ALLOCATED
+
+    def test_library_get_func(self):
+        lib = self.get_libc()
+        ptr = lib.getpointer('fopen', [], ffi_type_void)
+        py.test.raises(KeyError, lib.getpointer, 'xxxxxxxxxxxxxxx', [], ffi_type_void)
+        del ptr
+        del lib
+        assert not ALLOCATED
+
+    def test_call_argchain(self):
+        libm = self.get_libm()
+        pow = libm.getpointer('pow', [ffi_type_double, ffi_type_double],
+                              ffi_type_double)
+        argchain = ArgChain()
+        argchain.float(2.0).float(3.0)
+        res = pow.call(argchain, rffi.DOUBLE)
+        assert res == 8.0



More information about the Pypy-commit mailing list