예제 #1
0
def rewrite_ops(func, env=None):
    """
    Rewrite unary/binary operations to special methods:

        pycall(operator.add, a, b) -> call(getfield(a, __add__), [a, b])
    """
    for op in func.ops:
        if op.opcode == 'call' and isinstance(op.args[0], Const):
            f, args = op.args
            f = f.const
            if not hashable(f):
                continue

            try:
                methname = lookup_special(f)
            except KeyError:
                if f in special_runtime:
                    methname = special_runtime[f]
                else:
                    continue

            self = args[0]
            args = args[1:]
            m = newop('getfield', [self, methname])
            call = newop('call', [m, args])
            op.replace_uses(call)
            op.replace([m, call])
예제 #2
0
파일: pretty.py 프로젝트: YusufCakan/pykit
def ftype(val, seen=None):
    from pykit import types

    if not isinstance(val, types.Type):
        return str(val)

    if seen is None:
        seen = set()
    if id(val) in seen:
        return '...'

    seen.add(id(val))

    if hashable(val) and val in types.type2name:
        result = types.type2name[val]
    elif val.is_struct:
        args = ", ".join('%s:%s' % (name, ftype(ty, seen))
                         for name, ty in zip(val.names, val.types))
        result = '{ %s }' % args
    elif val.is_pointer:
        result ="%s*" % (ftype(val.base, seen),)
    else:
        result = repr(val)

    seen.remove(id(val))
    return result
예제 #3
0
def from_ctypes_value(ctypes_value):
    """
    Convert a ctypes value to a pykit constant
    """
    if not is_ctypes_value(ctypes_value):
        assert isinstance(ctypes_value, (int, long, float))
        return Const(ctypes_value)

    #if is_ctypes_function(ctypes_value):
    #    restype = from_ctypes_type(ctypes_value.restype)
    #    argtypes = [from_ctypes_type(argty) for argty in ctypes_value.argtypes]
    #    ptr = ptrval(ctypes_value)
    #    func_type = types.Function(restype, argtypes, varargs=False)
        #func_type_p = types.Pointer(func_type)
        #return Pointer(ptr, func_type_p)

    ctype = type(ctypes_value)

    if hashable(ctype) and ctype in ctypes_map:
        return Const(ctypes_value.value, from_ctypes_type(ctype))
    elif is_ctypes_struct_type(ctype):
        names = [name for name, _ in ctype._fields_]
        values = [from_ctypes_value(getattr(ctypes_value, n)) for n in names]
        if not names:
            names, values = ('dummy',), (Const(0, types.Int8))
        return Struct(names, values, from_ctypes_type(ctype))
    else:
        assert is_ctypes_pointer_type(ctype), ctype
        return Pointer(ctypes.cast(ctypes_value, ctypes.c_void_p).value,
                       from_ctypes_type(ctype))
예제 #4
0
def rewrite_ops(func, env=None):
    """
    Rewrite unary/binary operations to special methods:

        pycall(operator.add, a, b) -> call(getfield(a, __add__), [a, b])
    """
    for op in func.ops:
        if op.opcode == 'call' and isinstance(op.args[0], Const):
            f, args = op.args
            f = f.const
            if not hashable(f):
                continue

            try:
                methname = lookup_special(f)
            except KeyError:
                if f in special_runtime:
                    methname = special_runtime[f]
                else:
                    continue

            self = args[0]
            args = args[1:]
            m = newop('getfield', [self, methname])
            call = newop('call', [m, args])
            op.replace_uses(call)
            op.replace([m, call])
예제 #5
0
def from_ctypes_type(cty, ctypes_value=None):
    """
    Convert a ctypes type to a flypy type.

    Supported are structs, unit types (int/float)
    """
    if hashable(cty) and cty in ctypes_map:
        return ctypes_map[cty]
    elif cty is ctypes.c_void_p or cty is ctypes.py_object:
        return coretypes.Pointer[coretypes.void]
    elif is_ctypes_array_type(cty):
        return arrayobject.Array[from_ctypes_type(cty._type_), cty._length_]
    elif is_ctypes_pointer_type(cty):
        return coretypes.Pointer[from_ctypes_type(cty._type_)]
    elif is_ctypes_struct_type(cty):
        fields = [(name, from_ctypes_type(field_type))
                      for name, field_type in cty._fields_]
        fields = fields or [('dummy', coretypes.int8)]
        return coretypes.struct_(fields)
    elif is_ctypes_function_type(cty):
        # from_ctypes_type(cty._restype_) # <- this value is arbitrary,
        # it's always a c_int
        restype = from_ctypes_type(ctypes_value.restype)
        argtypes = tuple(from_ctypes_type(argty) for argty in ctypes_value.argtypes)
        return coretypes.ForeignFunction[argtypes + (restype,)]
    else:
        raise NotImplementedError(cty)
예제 #6
0
def from_ctypes_type(cty, ctypes_value=None):
    """
    Convert a ctypes type to a flypy type.

    Supported are structs, unit types (int/float)
    """
    if hashable(cty) and cty in ctypes_map:
        return ctypes_map[cty]
    elif cty is ctypes.c_void_p or cty is ctypes.py_object:
        return coretypes.Pointer[coretypes.void]
    elif is_ctypes_array_type(cty):
        return arrayobject.Array[from_ctypes_type(cty._type_), cty._length_]
    elif is_ctypes_pointer_type(cty):
        return coretypes.Pointer[from_ctypes_type(cty._type_)]
    elif is_ctypes_struct_type(cty):
        fields = [(name, from_ctypes_type(field_type))
                  for name, field_type in cty._fields_]
        fields = fields or [('dummy', coretypes.int8)]
        return coretypes.struct_(fields)
    elif is_ctypes_function_type(cty):
        # from_ctypes_type(cty._restype_) # <- this value is arbitrary,
        # it's always a c_int
        restype = from_ctypes_type(ctypes_value.restype)
        argtypes = tuple(
            from_ctypes_type(argty) for argty in ctypes_value.argtypes)
        return coretypes.ForeignFunction[argtypes + (restype, )]
    else:
        raise NotImplementedError(cty)
예제 #7
0
def from_ctypes_value(ctypes_value):
    """
    Convert a ctypes value to a pykit constant
    """
    if not is_ctypes_value(ctypes_value):
        assert isinstance(ctypes_value, (int, long, float))
        return Const(ctypes_value)

    #if is_ctypes_function(ctypes_value):
    #    restype = from_ctypes_type(ctypes_value.restype)
    #    argtypes = [from_ctypes_type(argty) for argty in ctypes_value.argtypes]
    #    ptr = ptrval(ctypes_value)
    #    func_type = types.Function(restype, argtypes, varargs=False)
    #func_type_p = types.Pointer(func_type)
    #return Pointer(ptr, func_type_p)

    ctype = type(ctypes_value)

    if hashable(ctype) and ctype in ctypes_map:
        return Const(ctypes_value.value, from_ctypes_type(ctype))
    elif is_ctypes_struct_type(ctype):
        names = [name for name, _ in ctype._fields_]
        values = [from_ctypes_value(getattr(ctypes_value, n)) for n in names]
        if not names:
            names, values = ('dummy', ), (Const(0, types.Int8))
        return Struct(names, values, from_ctypes_type(ctype))
    else:
        assert is_ctypes_pointer_type(ctype), ctype
        return Pointer(
            ctypes.cast(ctypes_value, ctypes.c_void_p).value,
            from_ctypes_type(ctype))
예제 #8
0
def from_ctypes_type(ctypes_type, memo=None):
    """
    Convert a ctypes type to a pykit type.

    Supported are structs, unit types (int/float)
    """
    if memo is None:
        memo = {}
    if hashable(ctypes_type) and ctypes_type in memo:
        return memo[ctypes_type]

    if hashable(ctypes_type) and ctypes_type in ctypes_map:
        result = ctypes_map[ctypes_type]
    elif ctypes_type is ctypes.c_void_p:
        result = types.Pointer(types.Void)
    elif is_ctypes_array_type(ctypes_type):
        result = types.Array(from_ctypes_type(ctypes_type._type_, memo),
                             ctypes_type._length_)
    elif is_ctypes_pointer_type(ctypes_type):
        result = types.Pointer(from_ctypes_type(ctypes_type._type_, memo))
    elif is_ctypes_struct_type(ctypes_type):
        # pre-order caching for recursive data structures
        f_names = []
        f_types = []
        result = types.Struct(f_names, f_types)
        memo[ctypes_type] = result

        fields = [(name, from_ctypes_type(field_type, memo))
                  for name, field_type in ctypes_type._fields_]
        fieldnames, fieldtypes = zip(*fields) or (('dummy', ), (types.Int8, ))

        f_names.extend(fieldnames)
        f_types.extend(fieldtypes)
    elif is_ctypes_function_type(ctypes_type):
        c_restype = from_ctypes_type(ctypes_type._restype_, memo)
        c_argtypes = [
            from_ctypes_type(argty, memo) for argty in ctypes_type._argtypes_
        ]
        result = types.Function(c_restype, c_argtypes, False)
    else:
        raise NotImplementedError(ctypes_type)

    memo[ctypes_type] = result
    return result
예제 #9
0
def from_ctypes_type(ctypes_type, memo=None):
    """
    Convert a ctypes type to a pykit type.

    Supported are structs, unit types (int/float)
    """
    if memo is None:
        memo = {}
    if hashable(ctypes_type) and ctypes_type in memo:
        return memo[ctypes_type]

    if hashable(ctypes_type) and ctypes_type in ctypes_map:
        result = ctypes_map[ctypes_type]
    elif ctypes_type is ctypes.c_void_p:
        result = types.Pointer(types.Void)
    elif is_ctypes_array_type(ctypes_type):
        result = types.Array(from_ctypes_type(ctypes_type._type_, memo),
                             ctypes_type._length_)
    elif is_ctypes_pointer_type(ctypes_type):
        result = types.Pointer(from_ctypes_type(ctypes_type._type_, memo))
    elif is_ctypes_struct_type(ctypes_type):
        # pre-order caching for recursive data structures
        f_names = []
        f_types = []
        result = types.Struct(f_names, f_types)
        memo[ctypes_type] = result

        fields = [(name, from_ctypes_type(field_type, memo))
                      for name, field_type in ctypes_type._fields_]
        fieldnames, fieldtypes = zip(*fields) or (('dummy',), (types.Int8,))

        f_names.extend(fieldnames)
        f_types.extend(fieldtypes)
    elif is_ctypes_function_type(ctypes_type):
        c_restype = from_ctypes_type(ctypes_type._restype_, memo)
        c_argtypes = [from_ctypes_type(argty, memo)
                          for argty in ctypes_type._argtypes_]
        result = types.Function(c_restype, c_argtypes, False)
    else:
        raise NotImplementedError(ctypes_type)

    memo[ctypes_type] = result
    return result
예제 #10
0
def llvm_type(type, memo=None):
    if memo is None:
        memo = {}
    if hashable(type) and type in memo:
        return memo[type]

    ty = type.__class__
    if ty == Boolean:
        result = Type.int(1)
    elif ty == Integral:
        result = Type.int(type.bits)
    elif type == Float32:
        result = Type.float()
    elif type == Float64:
        result = Type.double()
    elif ty == Array:
        result = Type.array(llvm_type(type.base, memo), type.count)
    elif ty == Vector:
        result = Type.vector(llvm_type(type.base, memo), type.count)
    elif ty == Struct:
        result = handle_struct(type, memo)
    elif ty == Pointer:
        if type.base.is_void:
            return Type.pointer(Type.int(8))
        result = Type.pointer(llvm_type(type.base, memo))
    elif ty == Function:
        result = Type.function(
            llvm_type(type.restype, memo),
            [llvm_type(argtype, memo) for argtype in type.argtypes],
            var_arg=type.varargs)
    elif ty == VoidT:
        result = Type.void()
    else:
        raise TypeError("Cannot convert type %s" % (type,))

    memo[type] = result
    return result
예제 #11
0
파일: typing.py 프로젝트: flypy/flypy
 def lookup_overlay(self, pyfunc):
     if not hashable(pyfunc):
         return None
     return self.overlays.get(pyfunc)
예제 #12
0
파일: pretty.py 프로젝트: pombredanne/pykit
def ftype(val):
    from pykit import types
    if hashable(val) and val in types.type2name:
        return types.type2name[val]
    return str(val)
예제 #13
0
파일: types.py 프로젝트: YusufCakan/pykit
}

def typeof(value):
    """Python value -> type"""
    return typing_defaults[type(value)]

# ______________________________________________________________________
# Convert

conversion_map = invert(typing_defaults)
conversion_map.update(dict.fromkeys(int_set, int))
conversion_map.update(dict.fromkeys(float_set, float))
# conversion_map.update(dict.fromkeys(complex_set, complex))

def convert(value, dst_type):
    """(python value, type) -> converted python value"""
    if dst_type.is_typedef:
        dst_type = dst_type.type
    converter = conversion_map[dst_type]
    return converter(value)

# ______________________________________________________________________

type2name = dict((v, n) for n, v in globals().items() if hashable(v))
typename = type2name.__getitem__

def resolve_typedef(type):
    while type.is_typedef:
        type = type.type
    return type
예제 #14
0
파일: pretty.py 프로젝트: inaimathi/pykit
def ftype(val):
    from pykit import types
    if hashable(val) and val in types.type2name:
        return types.type2name[val]
    return str(val)
예제 #15
0
파일: typing.py 프로젝트: pombreda/flypy
 def lookup_overlay(self, pyfunc):
     if not hashable(pyfunc):
         return None
     return self.overlays.get(pyfunc)