Author: Richard Plangger <planri...@gmail.com>
Branch: s390x-backend
Changeset: r82095:d40d932f8349
Date: 2016-02-05 22:37 +0100
http://bitbucket.org/pypy/pypy/changeset/d40d932f8349/

Log:    ffi call fixed in deprecated api that is still used (fix before that
        was not sufficient), fixed legacy tests test_libffi

diff --git a/pypy/module/_rawffi/interp_rawffi.py 
b/pypy/module/_rawffi/interp_rawffi.py
--- a/pypy/module/_rawffi/interp_rawffi.py
+++ b/pypy/module/_rawffi/interp_rawffi.py
@@ -4,7 +4,6 @@
 from pypy.interpreter.gateway import interp2app, unwrap_spec
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
 
-from rpython.jit.backend.llsupport.symbolic import WORD
 from rpython.rlib.clibffi import *
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rtyper.tool import rffi_platform
@@ -447,6 +446,9 @@
         self.ptr = ptr
         self.argshapes = argshapes
         self.resshape = resshape
+        self.narrow_integer = False
+        if resshape is not None:
+            self.narrow_integer = resshape.itemcode.lower() in ('c','h','i')
 
     def getbuffer(self, space):
         return space.wrap(rffi.cast(lltype.Unsigned, self.ptr.funcsym))
@@ -506,9 +508,9 @@
                 result = self.resshape.allocate(space, 1, autofree=True)
                 # adjust_return_size() was used here on result.ll_buffer
                 self.ptr.call(args_ll, result.ll_buffer)
-                if BIGENDIAN and result.shape.itemcode in 
('c','h','i','C','H','I'):
+                if BIGENDIAN and self.narrow_integer:
                     # we get a 8 byte value in big endian
-                    n = WORD - result.shape.size
+                    n = rffi.sizeof(lltype.Signed) - result.shape.size
                     result.buffer_advance(n)
 
                 return space.wrap(result)
diff --git a/rpython/rlib/clibffi.py b/rpython/rlib/clibffi.py
--- a/rpython/rlib/clibffi.py
+++ b/rpython/rlib/clibffi.py
@@ -594,10 +594,10 @@
                                             intmask(argtypes[i].c_size),
                                             flavor='raw')
         if restype != ffi_type_void:
-            size = adjust_return_size(intmask(restype.c_size))
+            self.restype_size = intmask(restype.c_size)
+            size = adjust_return_size(self.restype_size)
             self.ll_result = lltype.malloc(rffi.VOIDP.TO, size,
                                            flavor='raw')
-            self.restype_size = intmask(restype.c_size)
         else:
             self.restype_size = -1
 
@@ -637,7 +637,7 @@
         if RES_TP is not lltype.Void:
             TP = lltype.Ptr(rffi.CArray(RES_TP))
             ptr = self.ll_result
-            if _BIG_ENDIAN and self.restype_size != -1:
+            if _BIG_ENDIAN and RES_TP in TYPE_MAP_INT:
                 # we get a 8 byte value in big endian
                 n = rffi.sizeof(lltype.Signed) - self.restype_size
                 ptr = rffi.ptradd(ptr, n)
diff --git a/rpython/rlib/libffi.py b/rpython/rlib/libffi.py
--- a/rpython/rlib/libffi.py
+++ b/rpython/rlib/libffi.py
@@ -4,6 +4,7 @@
 from __future__ import with_statement
 
 from rpython.rtyper.lltypesystem import rffi, lltype
+from rpython.rlib.unroll import unrolling_iterable
 from rpython.rlib.objectmodel import specialize, enforceargs
 from rpython.rlib.rarithmetic import intmask, r_uint, r_singlefloat, r_longlong
 from rpython.rlib import jit
@@ -15,6 +16,9 @@
 from rpython.rlib.rdynload import DLLHANDLE
 
 import os
+import sys
+
+_BIG_ENDIAN = sys.byteorder == 'big'
 
 class types(object):
     """
@@ -211,6 +215,8 @@
 
 # ======================================================================
 
+NARROW_INTEGER_TYPES = unrolling_iterable([rffi.CHAR,
+    rffi.UCHAR, rffi.SHORT, rffi.USHORT, rffi.INT, rffi.UINT])
 
 class Func(AbstractFuncPtr):
 
@@ -263,7 +269,12 @@
             res = self._do_call_raw(self.funcsym, ll_args)
         elif _fits_into_signed(RESULT):
             assert not types.is_struct(self.restype)
-            res = self._do_call_int(self.funcsym, ll_args)
+            for res in NARROW_INTEGER_TYPES:
+                if RESULT is res:
+                    res = self._do_call_int(self.funcsym, ll_args, rffi.CHAR)
+                    break
+            else:
+                res = self._do_call_int(self.funcsym, ll_args, rffi.SIGNED)
         elif RESULT is rffi.DOUBLE:
             return self._do_call_float(self.funcsym, ll_args)
         elif RESULT is rffi.FLOAT:
@@ -325,8 +336,9 @@
 
     #@jit.oopspec('libffi_call_int(self, funcsym, ll_args)')
     @jit.dont_look_inside
-    def _do_call_int(self, funcsym, ll_args):
-        return self._do_call(funcsym, ll_args, rffi.INT)
+    @specialize.arg(3)
+    def _do_call_int(self, funcsym, ll_args, TP):
+        return self._do_call(funcsym, ll_args, TP)
 
     #@jit.oopspec('libffi_call_float(self, funcsym, ll_args)')
     @jit.dont_look_inside
@@ -368,10 +380,10 @@
     @specialize.arg(3)
     def _do_call(self, funcsym, ll_args, RESULT):
         # XXX: check len(args)?
-        ll_result = lltype.nullptr(rffi.CCHARP.TO)
+        ll_result = lltype.nullptr(rffi.VOIDP.TO)
         if self.restype != types.void:
             size = adjust_return_size(intmask(self.restype.c_size))
-            ll_result = lltype.malloc(rffi.CCHARP.TO, size,
+            ll_result = lltype.malloc(rffi.VOIDP.TO, size,
                                       flavor='raw')
         ffires = c_ffi_call(self.ll_cif,
                             self.funcsym,
@@ -379,14 +391,20 @@
                             rffi.cast(rffi.VOIDPP, ll_args))
         if RESULT is not lltype.Void:
             TP = lltype.Ptr(rffi.CArray(RESULT))
-            buf = rffi.cast(TP, ll_result)
             if types.is_struct(self.restype):
                 assert RESULT == rffi.SIGNED
                 # for structs, we directly return the buffer and transfer the
                 # ownership
+                buf = rffi.cast(TP, ll_result)
                 res = rffi.cast(RESULT, buf)
             else:
-                res = buf[0]
+                if _BIG_ENDIAN and types.getkind(self.restype) in ('i','u'):
+                    ptr = ll_result
+                    n = rffi.sizeof(lltype.Signed) - self.restype.c_size
+                    ptr = rffi.ptradd(ptr, n)
+                    res = rffi.cast(TP, ptr)[0]
+                else:
+                    res = rffi.cast(TP, ll_result)[0]
         else:
             res = None
         self._free_buffers(ll_result, ll_args)
diff --git a/rpython/rlib/test/test_libffi.py b/rpython/rlib/test/test_libffi.py
--- a/rpython/rlib/test/test_libffi.py
+++ b/rpython/rlib/test/test_libffi.py
@@ -274,7 +274,7 @@
         """
         libfoo = self.get_libfoo()
         func = (libfoo, 'diff_xy', [types.sint, types.signed], types.sint)
-        res = self.call(func, [50, 8], lltype.Signed)
+        res = self.call(func, [50, 8], rffi.INT)
         assert res == 42
 
     def test_simple(self):
@@ -287,7 +287,7 @@
         """
         libfoo = self.get_libfoo()
         func = (libfoo, 'sum_xy', [types.sint, types.double], types.sint)
-        res = self.call(func, [38, 4.2], lltype.Signed, jitif=["floats"])
+        res = self.call(func, [38, 4.2], rffi.INT, jitif=["floats"])
         assert res == 42
 
     def test_float_result(self):
@@ -319,7 +319,7 @@
         """
         libfoo = self.get_libfoo()
         func = (libfoo, 'many_args', [types.uchar, types.sint], types.sint)
-        res = self.call(func, [chr(20), 22], rffi.SIGNED)
+        res = self.call(func, [chr(20), 22], rffi.INT)
         assert res == 42
 
     def test_char_args(self):
@@ -418,12 +418,12 @@
         set_dummy = (libfoo, 'set_dummy', [types.sint], types.void)
         get_dummy = (libfoo, 'get_dummy', [], types.sint)
         #
-        initval = self.call(get_dummy, [], rffi.SIGNED)
+        initval = self.call(get_dummy, [], rffi.INT)
         #
         res = self.call(set_dummy, [initval+1], lltype.Void)
         assert res is None
         #
-        res = self.call(get_dummy, [], rffi.SIGNED)
+        res = self.call(get_dummy, [], rffi.INT)
         assert res == initval+1
 
     def test_single_float_args(self):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to