Ejemplo n.º 1
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.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     del cls._import
Ejemplo n.º 2
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.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
     del cls._import
Ejemplo n.º 3
0
Archivo: types.py Proyecto: njues/Sypy
 def _read(self, storage, width, i, offset):
     if we_are_translated():
         res = libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
                                     width, storage, i, offset)
     else:
         res = libffi.array_getitem_T(self.T, width, storage, i, offset)
     return byteswap(res)
Ejemplo n.º 4
0
 def _read(self, storage, width, i, offset):
     if we_are_translated():
         res = libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
                                     width, storage, i, offset)
     else:
         res = libffi.array_getitem_T(self.T, width, storage, i, offset)
     return byteswap(res)
Ejemplo n.º 5
0
 def _write(self, storage, width, i, offset, value):
     #value = byteswap(value) XXX
     if we_are_translated():
         libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
                              width, storage, i, offset, value)
     else:
         libffi.array_setitem_T(self.T, width, storage, i, offset, value)
Ejemplo n.º 6
0
Archivo: types.py Proyecto: njues/Sypy
 def _write(self, storage, width, i, offset, value):
     #value = byteswap(value) XXX
     if we_are_translated():
         libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
                              width, storage, i, offset, value)
     else:
         libffi.array_setitem_T(self.T, width, storage, i, offset, value)
Ejemplo n.º 7
0
    def define_callback_with_collect(cls):
        from pypy.rlib.clibffi import ffi_type_pointer, cast_type_to_ffitype,\
             CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint
        from pypy.rpython.lltypesystem import rffi, ll2ctypes
        import gc
        ffi_size_t = cast_type_to_ffitype(rffi.SIZE_T)

        from pypy.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.LONGP, p_a1)[0]
            a2 = rffi.cast(rffi.LONGP, 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(rffi.LONG)
            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(rffi.LONG)))
            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
Ejemplo n.º 8
0
    def define_callback_with_collect(cls):
        from pypy.rlib.clibffi import ffi_type_pointer, cast_type_to_ffitype,\
             CDLL, ffi_type_void, CallbackFuncPtr, ffi_type_sint
        from pypy.rpython.lltypesystem import rffi, ll2ctypes
        import gc
        ffi_size_t = cast_type_to_ffitype(rffi.SIZE_T)

        from pypy.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.LONGP, p_a1)[0]
            a2 = rffi.cast(rffi.LONGP, 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(rffi.LONG)
            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(rffi.LONG)))
            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
Ejemplo n.º 9
0
 def read(self, storage, width, i, offset):
     return self.box(libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
         width, storage, i, offset
     ))
Ejemplo n.º 10
0
Archivo: types.py Proyecto: njues/Sypy
def _setup():
    # compute alignment
    for tp in globals().values():
        if isinstance(tp, type) and hasattr(tp, 'T'):
            tp.alignment = clibffi.cast_type_to_ffitype(tp.T).c_alignment
Ejemplo n.º 11
0
 def store(self, storage, width, i, offset, box):
     value = self.unbox(box)
     libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
         width, storage, i, offset, value
     )
Ejemplo n.º 12
0
 def fill(self, storage, width, box, start, stop, offset):
     value = self.unbox(box)
     for i in xrange(start, stop):
         libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T),
             width, storage, i, offset, value
         )
Ejemplo n.º 13
0
 def read_bool(self, storage, width, i, offset):
     return bool(self.for_computation(
         libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T),
                              width, storage, i, offset)))
Ejemplo n.º 14
0
 def store(self, storage, width, i, offset, box):
     value = self.unbox(box)
     libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width,
                          storage, i, offset, value)
Ejemplo n.º 15
0
 def fill(self, storage, width, box, start, stop, offset):
     value = self.unbox(box)
     for i in xrange(start, stop):
         libffi.array_setitem(clibffi.cast_type_to_ffitype(self.T), width,
                              storage, i, offset, value)
Ejemplo n.º 16
0
def _setup():
    # compute alignment
    for tp in globals().values():
        if isinstance(tp, type) and hasattr(tp, 'T'):
            tp.alignment = clibffi.cast_type_to_ffitype(tp.T).c_alignment
Ejemplo n.º 17
0
 def read(self, storage, width, i, offset):
     return self.box(
         libffi.array_getitem(clibffi.cast_type_to_ffitype(self.T), width,
                              storage, i, offset))