def rewrite_externs(func, env): """Rewrite external symbol references Base on flypy.compiler.lower.constants.rewrite_constants """ if env['flypy.state.opaque']: return target = env['flypy.target'] # For each operation for op in func.ops: # Only for call operation if op.opcode == 'call': # For each constant constants = ir.collect_constants(op) new_constants = [] for c in constants: extern = c.const if extern_support.is_extern_symbol(extern): # Make a declare-only function argtypes = extern.type.argtypes restype = extern.type.restype if target == "cpu": # Install external symbol for CPU target extern.install() functype = ptypes.Function(lltype(restype), [lltype(t) for t in argtypes], extern.type.varargs) # Note: Global value should really be part inserted into # a module. But there are no module support at this point. replacment = ir.GlobalValue(extern.name, functype, external=True) else: # No change replacment = c # Add replacement new_constants.append(replacment) # Replace ir.substitute_args(op, constants, new_constants)
def rewrite_externs(func, env): """Rewrite external symbol references Base on flypy.compiler.lower.constants.rewrite_constants """ if env["flypy.state.opaque"]: return target = env["flypy.target"] # For each operation for op in func.ops: # Only for call operation if op.opcode == "call": # For each constant constants = ir.collect_constants(op) new_constants = [] for c in constants: extern = c.const if extern_support.is_extern_symbol(extern): # Make a declare-only function argtypes = extern.type.argtypes restype = extern.type.restype if target == "cpu": # Install external symbol for CPU target extern.install() functype = ptypes.Function(lltype(restype), [lltype(t) for t in argtypes], extern.type.varargs) # Note: Global value should really be part inserted into # a module. But there are no module support at this point. replacment = ir.GlobalValue(extern.name, functype, external=True) else: # No change replacment = c # Add replacement new_constants.append(replacment) # Replace ir.substitute_args(op, constants, new_constants)
def ptrcast(builder, argtypes, value, type): valtype, typetype = argtypes # e.g. `int, Type[double]` type = typetype.parameters[0] result = builder.ptrcast(lltype(type), value) builder.ret(result)
def convert_impl(builder, argtypes, value, type): valtype, typetype = argtypes # e.g. `int, Type[double]` type = typetype.parameters[0] result = builder.convert(lltype(type), value) builder.ret(result)
def result_type(argtypes): return lltype(argtypes[1].parameters[0])
def restype_undef(argtypes): type = argtypes[0] if type.impl == Type: type = type.parameters[0] (restype,) = type return lltype(restype)
def __nonzero__(self): return True @staticmethod def toobject(obj, type): return type.parameters[0] @jit('Constructor[a]') class Constructor(object): layout = [] #('ctor', 'a')] @cjit('Constructor[a] -> Type[b] -> c', opaque=True, infer_restype=index_type) def __getitem__(self, item): raise NotImplementedError @cjit('a -> bool') def __nonzero__(self): return True #===------------------------------------------------------------------=== # Low-level Implementation #===------------------------------------------------------------------=== def firstarg(builder, argtypes, *args): builder.ret(args[0]) add_impl_cls(Constructor, "__getitem__", firstarg, restype_func=lambda argtypes: lltype(argtypes[1]))
def impl(builder, argtypes, *args): [ty] = argtypes lty = lltype(ty) return builder.ret(builder.call_math(lty, mathname, list(args)))
def restype_undef(argtypes): type = argtypes[0] if type.impl == Type: type = type.parameters[0] (restype, ) = type return lltype(restype)