def test_jit_ffi_call():
    cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw')
    cd.abi = clibffi.FFI_DEFAULT_ABI
    cd.nargs = 1
    cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
    atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw')
    atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
    cd.atypes = atypes
    cd.exchange_size = 64    # 64 bytes of exchange data
    cd.exchange_result = 24
    cd.exchange_args[0] = 16
    #
    jit_ffi_prep_cif(cd)
    #
    assert rffi.sizeof(rffi.DOUBLE) == 8
    exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw')
    exb[2] = 1.23
    jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb))
    res = exb[3]
    lltype.free(exb, flavor='raw')
    #
    lltype.free(atypes, flavor='raw')
    lltype.free(cd, flavor='raw')
    #
    assert res == math.sin(1.23)
Exemple #2
0
def test_jit_ffi_call():
    cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw')
    cd.abi = clibffi.FFI_DEFAULT_ABI
    cd.nargs = 1
    cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
    atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw')
    atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
    cd.atypes = atypes
    cd.exchange_size = 64  # 64 bytes of exchange data
    cd.exchange_result = 24
    cd.exchange_args[0] = 16
    #
    jit_ffi_prep_cif(cd)
    #
    assert rffi.sizeof(rffi.DOUBLE) == 8
    exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw')
    exb[2] = 1.23
    jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb))
    res = exb[3]
    lltype.free(exb, flavor='raw')
    #
    lltype.free(atypes, flavor='raw')
    lltype.free(cd, flavor='raw')
    #
    assert res == math.sin(1.23)
Exemple #3
0
    def test_jit_ffi_vref(self):
        py.test.skip("unsupported so far")
        from rpython.rlib import clibffi
        from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call

        math_sin = intmask(
            ctypes.cast(ctypes.CDLL(None).sin, ctypes.c_void_p).value)
        math_sin = rffi.cast(rffi.VOIDP, math_sin)

        cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw')
        cd.abi = clibffi.FFI_DEFAULT_ABI
        cd.nargs = 1
        cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
        atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw')
        atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
        cd.atypes = atypes
        cd.exchange_size = 64  # 64 bytes of exchange data
        cd.exchange_result = 24
        cd.exchange_result_libffi = 24
        cd.exchange_args[0] = 16

        def f():
            #
            jit_ffi_prep_cif(cd)
            #
            assert rffi.sizeof(rffi.DOUBLE) == 8
            exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw')
            exb[2] = 1.23
            jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb))
            res = exb[3]
            lltype.free(exb, flavor='raw')
            #
            return res
            #

        res = self.interp_operations(f, [])
        lltype.free(cd, flavor='raw')
        assert res == math.sin(1.23)

        lltype.free(atypes, flavor='raw')
Exemple #4
0
    def test_jit_ffi_vref(self):
        py.test.skip("unsupported so far")
        from rpython.rlib import clibffi
        from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call

        math_sin = intmask(ctypes.cast(ctypes.CDLL(None).sin,
                                       ctypes.c_void_p).value)
        math_sin = rffi.cast(rffi.VOIDP, math_sin)

        cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw')
        cd.abi = clibffi.FFI_DEFAULT_ABI
        cd.nargs = 1
        cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
        atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw')
        atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
        cd.atypes = atypes
        cd.exchange_size = 64    # 64 bytes of exchange data
        cd.exchange_result = 24
        cd.exchange_result_libffi = 24
        cd.exchange_args[0] = 16

        def f():
            #
            jit_ffi_prep_cif(cd)
            #
            assert rffi.sizeof(rffi.DOUBLE) == 8
            exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw')
            exb[2] = 1.23
            jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb))
            res = exb[3]
            lltype.free(exb, flavor='raw')
            #
            return res
            #
        res = self.interp_operations(f, [])
        lltype.free(cd, flavor='raw')
        assert res == math.sin(1.23)

        lltype.free(atypes, flavor='raw')
Exemple #5
0
 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)
     cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
     cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
     cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED)
     cls.unsigned = clibffi.cast_type_to_ffitype(rffi.UNSIGNED)
     cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     del cls._import
Exemple #6
0
 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)
     cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
     cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
     cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED)
     cls.unsigned = clibffi.cast_type_to_ffitype(rffi.UNSIGNED)
     cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     # XXX long double support: clibffi.ffi_type_longdouble, but then
     # XXX fix the whole rest of this file to add a case for long double
     del cls._import
Exemple #7
0
    def define_callback_with_collect(cls):
        from rpython.rlib.clibffi import ffi_type_pointer, cast_type_to_ffitype,\
             CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint
        ffi_size_t = cast_type_to_ffitype(rffi.SIZE_T)

        from rpython.rlib.clibffi import get_libc_name

        def callback(ll_args, ll_res, stuff):
            gc.collect()
            p_a1 = rffi.cast(rffi.VOIDPP, ll_args[0])[0]
            p_a2 = rffi.cast(rffi.VOIDPP, ll_args[1])[0]
            a1 = rffi.cast(rffi.SIGNEDP, p_a1)[0]
            a2 = rffi.cast(rffi.SIGNEDP, p_a2)[0]
            res = rffi.cast(rffi.INTP, ll_res)
            if a1 > a2:
                res[0] = rffi.cast(rffi.INT, 1)
            else:
                res[0] = rffi.cast(rffi.INT, -1)

        def f():
            libc = CDLL(get_libc_name())
            qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_size_t,
                                              ffi_size_t, ffi_type_pointer],
                                    ffi_type_void)

            ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer],
                                  ffi_type_sint, callback)

            TP = rffi.CArray(lltype.Signed)
            to_sort = lltype.malloc(TP, 4, flavor='raw')
            to_sort[0] = 4
            to_sort[1] = 3
            to_sort[2] = 1
            to_sort[3] = 2
            qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort))
            qsort.push_arg(rffi.cast(rffi.SIZE_T, 4))
            qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(lltype.Signed)))
            qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure))
            qsort.call(lltype.Void)
            result = [to_sort[i] for i in range(4)] == [1, 2, 3, 4]
            lltype.free(to_sort, flavor='raw')
            keepalive_until_here(ptr)
            return int(result)

        return f
Exemple #8
0
 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)
     cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
     cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
     cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED)
     cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     del cls._import
Exemple #9
0
 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)
     cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
     cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
     cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED)
     cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     # XXX long double support: clibffi.ffi_type_longdouble, but then
     # XXX fix the whole rest of this file to add a case for long double
     del cls._import
Exemple #10
0
 def ffi_type(self):
     return clibffi.cast_type_to_ffitype(rffi.DOUBLE)
Exemple #11
0
        def ffi_size(self):
            return rffi.sizeof(llt)

        def ffi_type(self):
            return ctype

    return GenericCInt()


from rpython.rlib.rarithmetic import build_int
for x in [8, 16, 32, 64]:
    for s in [True, False]:
        nm = "C" + ("" if s else "U") + "Int" + str(x)
        int_tp = lltype.build_number(None, build_int(nm, s, x))
        ctype = clibffi.cast_type_to_ffitype(int_tp)
        make_itype(unicode("pixie.stdlib." + nm), ctype, int_tp)


class Token(py_object):
    """ Tokens are returned by ffi_set_value and are called when ffi is ready to clean up resources
    """
    def finalize_token(self):
        pass


class CInt(CType):
    def __init__(self):
        CType.__init__(self, u"pixie.stdlib.CInt")

    def ffi_get_value(self, ptr):
Exemple #12
0
 def cast_to_ffitype(self):
     for rtype in unsigned_types:
         if self.size == rffi.sizeof(rtype):
             return clibffi.cast_type_to_ffitype(rtype)
     else:
         assert False, "undefined ffi type"
Exemple #13
0
 def ffi_type(self):
     return clibffi.cast_type_to_ffitype(rffi.FLOAT)
Exemple #14
0
from topaz.coerce import Coerce

# XXX maybe move to rlib/jit_libffi
from pypy.module._cffi_backend import misc

_native_types = [
    ('VOID',       clibffi.ffi_type_void,                     lltype.Void,     []),
    ('INT8',       clibffi.ffi_type_sint8,                    rffi.CHAR,       ['CHAR', 'SCHAR']),
    ('UINT8',      clibffi.ffi_type_uint8,                    rffi.UCHAR,      ['UCHAR']),
    ('INT16',      clibffi.ffi_type_sint16,                   rffi.SHORT,      ['SHORT', 'SSHORT']),
    ('UINT16',     clibffi.ffi_type_uint16,                   rffi.USHORT,     ['USHORT']),
    ('INT32',      clibffi.ffi_type_sint32,                   rffi.INT,        ['INT', 'SINT']),
    ('UINT32',     clibffi.ffi_type_uint32,                   rffi.UINT,       ['UINT']),
    ('INT64',      clibffi.ffi_type_sint64,                   rffi.LONGLONG,   ['LONG_LONG', 'SLONG_LONG']),
    ('UINT64',     clibffi.ffi_type_uint64,                   rffi.ULONGLONG,  ['ULONG_LONG']),
    ('LONG',       clibffi.cast_type_to_ffitype(rffi.LONG),   rffi.LONG,       ['SLONG']),
    ('ULONG',      clibffi.cast_type_to_ffitype(rffi.ULONG),  rffi.ULONG,      []),
    ('FLOAT32',    clibffi.ffi_type_float,                    rffi.FLOAT,      ['FLOAT']),
    ('FLOAT64',    clibffi.ffi_type_double,                   rffi.DOUBLE,     ['DOUBLE']),
    ('LONGDOUBLE', clibffi.ffi_type_longdouble,               rffi.LONGDOUBLE, []),
    ('POINTER',    clibffi.ffi_type_pointer,                  rffi.VOIDP,      []),
    ('CALLBACK',   clibffi.ffi_type_pointer,                  rffi.VOIDP,      []),
    ('FUNCTION',   clibffi.ffi_type_pointer,                  rffi.VOIDP,      []),
    ('BUFFER_IN',),
    ('BUFFER_OUT',),
    ('BUFFER_INOUT',),
    ('CHAR_ARRAY',),
    ('BOOL',       clibffi.cast_type_to_ffitype(lltype.Bool), lltype.Bool,     []),
    ('STRING',     clibffi.ffi_type_pointer,                  rffi.CCHARP,     []),
    ('VARARGS',    clibffi.ffi_type_void,                     rffi.CHAR,       []),
    ('NATIVE_VARARGS',),
Exemple #15
0
 def cast_to_ffitype(self):
     for rtype in unsigned_types:
         if self.size == rffi.sizeof(rtype):
             return clibffi.cast_type_to_ffitype(rtype)
     else:
         assert False, "undefined ffi type"
Exemple #16
0
 def ffi_type(self):
     return clibffi.cast_type_to_ffitype(rffi.FLOAT)
Exemple #17
0
            casted[0] = rffi.cast(llt, val.int_val())

        def ffi_size(self):
            return rffi.sizeof(llt)

        def ffi_type(self):
            return ctype

    return GenericCInt()

from rpython.rlib.rarithmetic import build_int
for x in [8, 16, 32, 64]:
    for s in [True, False]:
        nm = "C" + ("" if s else "U") + "Int" + str(x)
        int_tp = lltype.build_number(None, build_int(nm, s, x))
        ctype = clibffi.cast_type_to_ffitype(int_tp)
        make_itype(unicode("pixie.stdlib." + nm), ctype, int_tp)







class Token(py_object):
    """ Tokens are returned by ffi_set_value and are called when ffi is ready to clean up resources
    """
    def finalize_token(self):
        pass

Exemple #18
0
def get_clibffi_type(arg):
    if arg == Integer._type:
        return clibffi.cast_type_to_ffitype(rffi.LONG)
    if arg == String._type:
        return clibffi.ffi_type_pointer
    assert False
Exemple #19
0
 def cast_to_ffitype(self):
     for rtype in signed_types:
         if self.size == rffi.sizeof(rtype):
             return clibffi.cast_type_to_ffitype(rtype)
     else:
         raise unwind(LTypeError(u"undefined ffi type: %s" % self.repr()))
Exemple #20
0
 def cast_to_ffitype(self):
     for rtype in signed_types:
         if self.size == rffi.sizeof(rtype):
             return clibffi.cast_type_to_ffitype(rtype)
     else:
         raise unwind(LTypeError(u"undefined ffi type: %s" % self.repr()))
Exemple #21
0
 def ffi_type(self):
     return clibffi.cast_type_to_ffitype(rffi.DOUBLE)
Exemple #22
0
from topaz.coerce import Coerce

from topaz.modules.ffi import misc

_native_types = [
    ('VOID', clibffi.ffi_type_void, lltype.Void, []),
    ('INT8', clibffi.ffi_type_sint8, rffi.CHAR, ['CHAR', 'SCHAR']),
    ('UINT8', clibffi.ffi_type_uint8, rffi.UCHAR, ['UCHAR']),
    ('INT16', clibffi.ffi_type_sint16, rffi.SHORT, ['SHORT', 'SSHORT']),
    ('UINT16', clibffi.ffi_type_uint16, rffi.USHORT, ['USHORT']),
    ('INT32', clibffi.ffi_type_sint32, rffi.INT, ['INT', 'SINT']),
    ('UINT32', clibffi.ffi_type_uint32, rffi.UINT, ['UINT']),
    ('INT64', clibffi.ffi_type_sint64, rffi.LONGLONG,
     ['LONG_LONG', 'SLONG_LONG']),
    ('UINT64', clibffi.ffi_type_uint64, rffi.ULONGLONG, ['ULONG_LONG']),
    ('LONG', clibffi.cast_type_to_ffitype(rffi.LONG), rffi.LONG, ['SLONG']),
    ('ULONG', clibffi.cast_type_to_ffitype(rffi.ULONG), rffi.ULONG, []),
    ('FLOAT32', clibffi.ffi_type_float, rffi.FLOAT, ['FLOAT']),
    ('FLOAT64', clibffi.ffi_type_double, rffi.DOUBLE, ['DOUBLE']),
    ('LONGDOUBLE', clibffi.ffi_type_longdouble, rffi.LONGDOUBLE, []),
    ('POINTER', clibffi.ffi_type_pointer, rffi.VOIDP, []),
    ('CALLBACK', clibffi.ffi_type_pointer, rffi.VOIDP, []),
    ('FUNCTION', clibffi.ffi_type_pointer, rffi.VOIDP, []),
    ('BUFFER_IN', ),
    ('BUFFER_OUT', ),
    ('BUFFER_INOUT', ),
    ('CHAR_ARRAY', ),
    ('BOOL', clibffi.cast_type_to_ffitype(lltype.Bool), lltype.Bool, []),
    ('STRING', clibffi.ffi_type_pointer, rffi.CCHARP, []),
    ('VARARGS', clibffi.ffi_type_void, rffi.CHAR, []),
    ('NATIVE_VARARGS', ),