[pypy-svn] r77573 - in pypy/branch/jitffi/pypy: jit/backend/llsupport jit/backend/llsupport/test jit/backend/test jit/metainterp/optimizeopt jit/metainterp/test rlib rlib/test

antocuni at codespeak.net antocuni at codespeak.net
Mon Oct 4 15:52:29 CEST 2010


Author: antocuni
Date: Mon Oct  4 15:52:28 2010
New Revision: 77573

Modified:
   pypy/branch/jitffi/pypy/jit/backend/llsupport/ffisupport.py
   pypy/branch/jitffi/pypy/jit/backend/llsupport/test/test_ffisupport.py
   pypy/branch/jitffi/pypy/jit/backend/test/runner_test.py
   pypy/branch/jitffi/pypy/jit/metainterp/optimizeopt/fficall.py
   pypy/branch/jitffi/pypy/jit/metainterp/test/test_fficall.py
   pypy/branch/jitffi/pypy/jit/metainterp/test/test_optimizeopt.py
   pypy/branch/jitffi/pypy/rlib/libffi.py
   pypy/branch/jitffi/pypy/rlib/test/test_libffi.py
Log:
introduce a new namespace to contain all the ffi types



Modified: pypy/branch/jitffi/pypy/jit/backend/llsupport/ffisupport.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/backend/llsupport/ffisupport.py	(original)
+++ pypy/branch/jitffi/pypy/jit/backend/llsupport/ffisupport.py	Mon Oct  4 15:52:28 2010
@@ -4,7 +4,7 @@
 
 def get_call_descr_dynamic(ffi_args, ffi_result, extrainfo=None):
     """Get a call descr: the types of result and args are represented by
-    rlib.libffi.ffi_type_*"""
+    rlib.libffi.types.*"""
     try:
         reskind = get_ffi_type_kind(ffi_result)
         argkinds = [get_ffi_type_kind(arg) for arg in ffi_args]
@@ -23,41 +23,41 @@
 
 
 # XXX: maybe we can turn this into a dictionary, but we need to do it at
-# runtime as libffi.ffi_type_* are pointers
+# runtime as libffi.types.* pointers
 def get_ffi_type_kind(ffi_type):
-    from pypy.rlib import libffi
-    if ffi_type is libffi.ffi_type_void:
+    from pypy.rlib.libffi import types
+    if ffi_type is types.void:
         return history.VOID
-    elif ffi_type is libffi.ffi_type_pointer:
+    elif ffi_type is types.pointer:
         return history.REF
-    elif ffi_type is libffi.ffi_type_double:
+    elif ffi_type is types.double:
         return history.FLOAT
-    elif ffi_type is libffi.ffi_type_uchar:
+    elif ffi_type is types.uchar:
         return history.INT
-    elif ffi_type is libffi.ffi_type_uint8:
+    elif ffi_type is types.uint8:
         return history.INT
-    elif ffi_type is libffi.ffi_type_schar:
+    elif ffi_type is types.schar:
         return history.INT
-    elif ffi_type is libffi.ffi_type_sint8:
+    elif ffi_type is types.sint8:
         return history.INT
-    elif ffi_type is libffi.ffi_type_uint16:
+    elif ffi_type is types.uint16:
         return history.INT
-    elif ffi_type is libffi.ffi_type_ushort:
+    elif ffi_type is types.ushort:
         return history.INT
-    elif ffi_type is libffi.ffi_type_sint16:
+    elif ffi_type is types.sint16:
         return history.INT
-    elif ffi_type is libffi.ffi_type_sshort:
+    elif ffi_type is types.sshort:
         return history.INT
-    elif ffi_type is libffi.ffi_type_uint:
+    elif ffi_type is types.uint:
         return history.INT
-    elif ffi_type is libffi.ffi_type_uint32:
+    elif ffi_type is types.uint32:
         return history.INT
-    elif ffi_type is libffi.ffi_type_sint:
+    elif ffi_type is types.sint:
         return history.INT
-    elif ffi_type is libffi.ffi_type_sint32:
+    elif ffi_type is types.sint32:
         return history.INT
-    ## elif ffi_type is libffi.ffi_type_uint64:
+    ## elif ffi_type is types.uint64:
     ##     return history.INT
-    ## elif ffi_type is libffi.ffi_type_sint64:
+    ## elif ffi_type is types.sint64:
     ##     return history.INT
     raise KeyError

Modified: pypy/branch/jitffi/pypy/jit/backend/llsupport/test/test_ffisupport.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/backend/llsupport/test/test_ffisupport.py	(original)
+++ pypy/branch/jitffi/pypy/jit/backend/llsupport/test/test_ffisupport.py	Mon Oct  4 15:52:28 2010
@@ -1,18 +1,18 @@
-from pypy.rlib import libffi
+from pypy.rlib.libffi import types
 from pypy.jit.backend.llsupport.ffisupport import get_call_descr_dynamic, \
     VoidCallDescr, DynamicIntCallDescr
     
 def test_call_descr_dynamic():
 
-    args = [libffi.ffi_type_sint, libffi.ffi_type_double, libffi.ffi_type_pointer]
-    descr = get_call_descr_dynamic(args, libffi.ffi_type_void)
+    args = [types.sint, types.double, types.pointer]
+    descr = get_call_descr_dynamic(args, types.void)
     assert isinstance(descr, VoidCallDescr)
     assert descr.arg_classes == 'ifr'
 
-    descr = get_call_descr_dynamic([], libffi.ffi_type_sint8)
+    descr = get_call_descr_dynamic([], types.sint8)
     assert isinstance(descr, DynamicIntCallDescr)
     assert descr.get_result_size(False) == 1
 
-    descr = get_call_descr_dynamic([], libffi.ffi_type_float)
+    descr = get_call_descr_dynamic([], types.float)
     assert descr is None # single floats are not supported so far
     

Modified: pypy/branch/jitffi/pypy/jit/backend/test/runner_test.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/backend/test/runner_test.py	(original)
+++ pypy/branch/jitffi/pypy/jit/backend/test/runner_test.py	Mon Oct  4 15:52:28 2010
@@ -421,7 +421,7 @@
             assert x == 3.5 - 42
 
     def test_call(self):
-        from pypy.rlib.libffi import ffi_type_sint, ffi_type_uchar, ffi_type_sint16
+        from pypy.rlib.libffi import types
 
         def func_int(a, b):
             return a + b
@@ -429,9 +429,9 @@
             return chr(ord(c) + ord(c1))
 
         functions = [
-            (func_int, lltype.Signed, ffi_type_sint, 655360),
-            (func_int, rffi.SHORT, ffi_type_sint16, 1213),
-            (func_char, lltype.Char, ffi_type_uchar, 12)
+            (func_int, lltype.Signed, types.sint, 655360),
+            (func_int, rffi.SHORT, types.sint16, 1213),
+            (func_char, lltype.Char, types.uchar, 12)
             ]
 
         for func, TP, ffi_type, num in functions:

Modified: pypy/branch/jitffi/pypy/jit/metainterp/optimizeopt/fficall.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/metainterp/optimizeopt/fficall.py	(original)
+++ pypy/branch/jitffi/pypy/jit/metainterp/optimizeopt/fficall.py	Mon Oct  4 15:52:28 2010
@@ -15,7 +15,7 @@
     def _get_signature(self, funcval):
         """
         given the funcval, return a tuple (argtypes, restype), where the
-        actuall types are libffi.ffi_type_*
+        actuall types are libffi.types.*
 
         The implementation is tricky because we have three possible cases:
 

Modified: pypy/branch/jitffi/pypy/jit/metainterp/test/test_fficall.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/metainterp/test/test_fficall.py	(original)
+++ pypy/branch/jitffi/pypy/jit/metainterp/test/test_fficall.py	Mon Oct  4 15:52:28 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, ffi_type_double, ffi_type_uchar, ArgChain, Func
+from pypy.rlib.libffi import CDLL, types, ArgChain, Func
 from pypy.tool.udir import udir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.platform import platform
@@ -40,8 +40,8 @@
 
         def f(n):
             cdll = CDLL(self.lib_name)
-            func = cdll.getpointer('sum_xy', [ffi_type_sint, ffi_type_double],
-                                   ffi_type_sint)
+            func = cdll.getpointer('sum_xy', [types.sint, types.double],
+                                   types.sint)
             while n < 10:
                 driver.jit_merge_point(n=n, func=func)
                 driver.can_enter_jit(n=n, func=func)
@@ -66,7 +66,7 @@
 
         def f(n):
             cdll = CDLL(self.lib_name)
-            func = cdll.getpointer('abs', [ffi_type_double], ffi_type_double)
+            func = cdll.getpointer('abs', [types.double], types.double)
             res = 0.0
             while n < 10:
                 driver.jit_merge_point(n=n, func=func, res=res)
@@ -87,8 +87,8 @@
 
         def f(n):
             cdll = CDLL(self.lib_name)
-            func = cdll.getpointer('cast_to_uchar', [ffi_type_sint],
-                                   ffi_type_uchar)
+            func = cdll.getpointer('cast_to_uchar', [types.sint],
+                                   types.uchar)
             res = 0
             while n < 10:
                 driver.jit_merge_point(n=n, func=func, res=res)

Modified: pypy/branch/jitffi/pypy/jit/metainterp/test/test_optimizeopt.py
==============================================================================
--- pypy/branch/jitffi/pypy/jit/metainterp/test/test_optimizeopt.py	(original)
+++ pypy/branch/jitffi/pypy/jit/metainterp/test/test_optimizeopt.py	Mon Oct  4 15:52:28 2010
@@ -3900,7 +3900,7 @@
 
 # ------------------------------------------------ 
 from pypy.rpython.lltypesystem import llmemory
-from pypy.rlib.libffi import Func, ffi_type_sint, ffi_type_double
+from pypy.rlib.libffi import Func, types
 from pypy.jit.metainterp.history import AbstractDescr
 
 class MyCallDescr(AbstractDescr):
@@ -3928,7 +3928,7 @@
     def _identityhash(self):
         return id(self)
 
-class TestFfiCall(OptimizeOptTest, LLtypeMixin):
+class TestFfiCall(BaseTestOptimizeOpt, LLtypeMixin):
 
     class namespace:
         cpu = LLtypeMixin.cpu
@@ -3936,8 +3936,8 @@
         int_float__int = MyCallDescr('if', 'i')
         funcptr = FakeLLObject()
         func = FakeLLObject(_fake_class=Func,
-                            argtypes=[ffi_type_sint, ffi_type_double],
-                            restype=ffi_type_sint)
+                            argtypes=[types.sint, types.double],
+                            restype=types.sint)
 
     namespace = namespace.__dict__
 

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 15:52:28 2010
@@ -7,13 +7,31 @@
     push_arg_as_ffiptr, c_ffi_call
 from pypy.rlib.rdynload import dlopen, dlclose, dlsym, dlsym_byordinal
 
-def import_types():
-    g = globals()
-    for key, value in clibffi.__dict__.iteritems():
-        if key.startswith('ffi_type_'):
-            g[key] = value
-import_types()
-del import_types
+class types(object):
+    """
+    This namespace contains the primitive types you can use to declare the
+    signatures of the ffi functions.
+
+    In general, the name of the types are closely related to the ones of the
+    C-level ffi_type_*: e.g, instead of ffi_type_sint you should use
+    libffi.types.sint.
+
+    However, you should not rely on a perfect correspondence: in particular,
+    the exact meaning of ffi_type_{slong,ulong} changes a lot between libffi
+    versions, so types.slong could be different than ffi_type_slong.
+    """
+
+    @classmethod
+    def _import(cls):
+        prefix = 'ffi_type_'
+        for key, value in clibffi.__dict__.iteritems():
+            if key.startswith(prefix):
+                name = key[len(prefix):]
+                setattr(cls, name, value)
+        cls.slong = clibffi.cast_type_to_ffitype(rffi.LONG)
+        cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG)
+
+types._import()
 
 def _fits_into_long(TYPE):
     if not isinstance(TYPE, lltype.Number):
@@ -23,7 +41,7 @@
     sz = rffi.sizeof(TYPE)
     return sz <= rffi.sizeof(rffi.LONG)
 
-# ----------------------------------------------------------------------
+# ======================================================================
 
 class ArgChain(object):
     first = None
@@ -71,6 +89,9 @@
         func._push_float(self.floatval, ll_args, i)
 
 
+# ======================================================================
+
+
 class Func(AbstractFuncPtr):
 
     _immutable_fields_ = ['funcsym', 'argtypes', 'restype']
@@ -168,7 +189,7 @@
     def _do_call(self, funcsym, ll_args, RESULT):
         # XXX: check len(args)?
         ll_result = lltype.nullptr(rffi.CCHARP.TO)
-        if self.restype != ffi_type_void:
+        if self.restype != types.void:
             ll_result = lltype.malloc(rffi.CCHARP.TO,
                                       intmask(self.restype.c_size),
                                       flavor='raw')
@@ -192,8 +213,8 @@
         lltype.free(ll_args, flavor='raw')
 
 
-# ----------------------------------------------------------------------
-    
+# ======================================================================
+
 
 # XXX: it partially duplicate the code in clibffi.py
 class CDLL(object):

Modified: pypy/branch/jitffi/pypy/rlib/test/test_libffi.py
==============================================================================
--- pypy/branch/jitffi/pypy/rlib/test/test_libffi.py	(original)
+++ pypy/branch/jitffi/pypy/rlib/test/test_libffi.py	Mon Oct  4 15:52:28 2010
@@ -3,8 +3,7 @@
 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
+from pypy.rlib.libffi import CDLL, Func, get_libc_name, ArgChain, types
 
 
 class TestLibffi(BaseFfiTest):
@@ -41,16 +40,16 @@
 
     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)
+        ptr = lib.getpointer('fopen', [], types.void)
+        py.test.raises(KeyError, lib.getpointer, 'xxxxxxxxxxxxxxx', [], types.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)
+        pow = libm.getpointer('pow', [types.double, types.double],
+                              types.double)
         argchain = ArgChain()
         argchain.float(2.0).float(3.0)
         res = pow.call(argchain, rffi.DOUBLE)



More information about the Pypy-commit mailing list