Пример #1
0
    def ctype(cls, ty):
        restype = ctype(ty.restype)
        argtypes = [ctype(argtype) for argtype in ty.argtypes]

        if byref(ty.restype):
            argtypes.append(ctypes.POINTER(restype))
            restype = None  # void

        return ctypes.POINTER(ctypes.PYFUNCTYPE(restype, *argtypes))
Пример #2
0
    def ctype(cls, ty):
        restype = ctype(ty.restype)
        argtypes = [ctype(argtype) for argtype in ty.argtypes]

        if byref(ty.restype):
            argtypes.append(ctypes.POINTER(restype))
            restype = None # void

        return ctypes.POINTER(ctypes.PYFUNCTYPE(restype, *argtypes))
Пример #3
0
def representation_type(ty):
    """
    Get the low-level representation type for a high-level (user-defined) type.

    Returns
    =======
    The pykit type for the object layout.
    """

    from flypy.lib import vectorobject
    from flypy.lib import arrayobject
    from flypy.runtime.obj import pointerobject

    # NOTE: special cases should be kept to an absolute minimum here. They
    #       should probably be introduced only if ctypes cannot represent the
    #       type
    if ty.impl == vectorobject.Vector:
        base, count = ty.parameters
        return ptypes.Vector(representation_type(base), count)
    elif ty.impl == pointerobject.Pointer:
        # type pointed to may not be supported by ctypes
        (base, ) = ty.parameters
        if base.impl == vectorobject.Vector:
            return ptypes.Pointer(representation_type(base))

    cty = conversion.ctype(ty)
    result_type = ctypes_support.from_ctypes_type(cty)

    # struct uses pointer
    if result_type.is_struct:
        result_type = ptypes.Pointer(result_type)

    return result_type
Пример #4
0
def representation_type(ty):
    """
    Get the low-level representation type for a high-level (user-defined) type.

    Returns
    =======
    The pykit type for the object layout.
    """

    from flypy.lib import vectorobject
    from flypy.lib import arrayobject
    from flypy.runtime.obj import pointerobject

    # NOTE: special cases should be kept to an absolute minimum here. They
    #       should probably be introduced only if ctypes cannot represent the
    #       type
    if ty.impl == vectorobject.Vector:
        base, count = ty.parameters
        return ptypes.Vector(representation_type(base), count)
    elif ty.impl == pointerobject.Pointer:
        # type pointed to may not be supported by ctypes
        (base,) = ty.parameters
        if base.impl == vectorobject.Vector:
            return ptypes.Pointer(representation_type(base))

    cty = conversion.ctype(ty)
    result_type = ctypes_support.from_ctypes_type(cty)

    # struct uses pointer
    if result_type.is_struct:
        result_type = ptypes.Pointer(result_type)

    return result_type
Пример #5
0
    def __call__(self, *args, **kwargs):
        from flypy.representation import byref, stack_allocate
        from flypy.conversion import (toctypes, fromctypes, toobject,
                                      fromobject, ctype)
        #from flypy.support.ctypes_support import CTypesStruct
        #from flypy.types import Function

        # Keep this alive for the duration of the call
        keepalive = list(args) + list(kwargs.values())

        # Order arguments
        args = flatargs(self.py_func, args, kwargs)

        # Translate
        cfunc, restype = self.translate([typeof(x) for x in args.flat])

        # Construct flypy values
        argtypes = [typeof(x) for x in args]
        arg_objs = list(starmap(fromobject, zip(args, argtypes)))

        # Map flypy values to a ctypes representation
        args = []
        for arg, argtype in zip(arg_objs, argtypes):
            c_arg = toctypes(arg, argtype, keepalive)
            if byref(argtype) and stack_allocate(argtype):
                c_arg = ctypes.pointer(c_arg)
            args.append(c_arg)

        # We need this cast since the ctypes function constructed from LLVM
        # IR has different structs (which are structurally equivalent)
        c_restype = ctype(restype)
        if byref(restype):
            c_result = c_restype()  # dummy result value
            args.append(ctypes.pointer(c_result))
            c_restype = None  # void

        c_signature = ctypes.PYFUNCTYPE(c_restype,
                                        *[type(arg) for arg in args])
        cfunc = ctypes.cast(cfunc, c_signature)

        # Handle calling convention
        if byref(restype):
            cfunc(*args)
        else:
            c_result = cfunc(*args)

        # Map ctypes result back to a python value
        result = fromctypes(c_result, restype)
        result_obj = toobject(result, restype)

        return result_obj
Пример #6
0
def make_ctypes_ptr(ptr, type):
    from flypy.support.cffi_support import is_cffi, ffi
    from flypy.support.ctypes_support import is_ctypes_pointer_type

    cty = ctype(type)

    if is_cffi(ptr):
        addr = ffi.cast('uintptr_t', ptr)
        ctypes_ptr = ctypes.c_void_p(int(addr))
        ptr = ctypes.cast(ctypes_ptr, cty)
    else:
        ptr = ctypes.cast(ptr, cty)

    return ptr
Пример #7
0
def make_ctypes_ptr(ptr, type):
    from flypy.support.cffi_support import is_cffi, ffi
    from flypy.support.ctypes_support import is_ctypes_pointer_type

    cty = ctype(type)

    if is_cffi(ptr):
        addr = ffi.cast('uintptr_t', ptr)
        ctypes_ptr = ctypes.c_void_p(int(addr))
        ptr = ctypes.cast(ctypes_ptr, cty)
    else:
        ptr = ctypes.cast(ptr, cty)

    return ptr
Пример #8
0
    def __call__(self, *args, **kwargs):
        from flypy.representation import byref, stack_allocate
        from flypy.conversion import (
            toctypes, fromctypes, toobject, fromobject, ctype)
        #from flypy.support.ctypes_support import CTypesStruct
        #from flypy.types import Function

        # Keep this alive for the duration of the call
        keepalive = list(args) + list(kwargs.values())

        # Order arguments
        args = flatargs(self.py_func, args, kwargs)

        # Translate
        cfunc, restype = self.translate([typeof(x) for x in args.flat])

        # Construct flypy values
        argtypes = [typeof(x) for x in args]
        arg_objs = list(starmap(fromobject, zip(args, argtypes)))

        # Map flypy values to a ctypes representation
        args = []
        for arg, argtype in zip(arg_objs, argtypes):
            c_arg = toctypes(arg, argtype, keepalive)
            if byref(argtype) and stack_allocate(argtype):
                c_arg = ctypes.pointer(c_arg)
            args.append(c_arg)

        # We need this cast since the ctypes function constructed from LLVM
        # IR has different structs (which are structurally equivalent)
        c_restype = ctype(restype)
        if byref(restype):
            c_result = c_restype() # dummy result value
            args.append(ctypes.pointer(c_result))
            c_restype = None # void

        c_signature = ctypes.PYFUNCTYPE(c_restype, *[type(arg) for arg in args])
        cfunc = ctypes.cast(cfunc, c_signature)

        # Handle calling convention
        if byref(restype):
            cfunc(*args)
        else:
            c_result = cfunc(*args)

        # Map ctypes result back to a python value
        result = fromctypes(c_result, restype)
        result_obj = toobject(result, restype)

        return result_obj
Пример #9
0
 def ctype(cls, ty):
     [base] = ty.parameters
     return ctypes.POINTER(ctype(base))
Пример #10
0
 def fromctypes(cls, val, ty):
     if isinstance(val, (int, long)):
         cty = ctype(ty)
         return cty(val)
     return val
Пример #11
0
 def ctype(cls, ty):
     restype = ctype(ty.parameters[-1])
     argtypes = [ctype(argtype) for argtype in ty.parameters[:-1]]
     #return ctypes.CFUNCTYPE(restype, *argtypes)
     return ctypes.POINTER(ctypes.CFUNCTYPE(restype, *argtypes))
Пример #12
0
 def fromctypes(cls, val, ty):
     if isinstance(val, ctypes.Array):
         cty = ctype(ty)
         return cty(*val)
     return val
Пример #13
0
 def ctype(cls, ty):
     cty = conversion.ctype(ty.parameters[0])
     # Get the base type if a pointer
     if hasattr(cty, '_type_'):
         return cty._type_
     return cty
Пример #14
0
 def fromctypes(cls, val, ty):
     if isinstance(val, (int, long)):
         cty = ctype(ty)
         return cty(val)
     return val
Пример #15
0
def make_ctypes_array(arr, type):
    cty = ctype(type)
    arr = ctypes.cast(arr, cty)
    return arr
Пример #16
0
 def ctype(cls, ty):
     base, count = ty.parameters
     return ctype(base) * count
Пример #17
0
 def ctype(cls, ty):
     restype = ctype(ty.parameters[-1])
     argtypes = [ctype(argtype) for argtype in ty.parameters[:-1]]
     #return ctypes.CFUNCTYPE(restype, *argtypes)
     return ctypes.POINTER(ctypes.CFUNCTYPE(restype, *argtypes))
Пример #18
0
def newarray(basetype, size):
    arr = (ctype(basetype) * size)()
    return Array(arr)
Пример #19
0
 def ctype(cls, ty):
     base, count = ty.parameters
     return ctype(base) * count
Пример #20
0
 def ctype(cls, ty):
     cty = conversion.ctype(ty.parameters[0])
     # Get the base type if a pointer
     if hasattr(cty, '_type_'):
         return cty._type_
     return cty
Пример #21
0
 def ctype(cls, ty):
     [base] = ty.parameters
     return ctypes.POINTER(ctype(base))