def add_impl(opaque_func, name, implementation, restype=None, restype_func=None): """ Assign an implementation to an `opaque` function. Sets up a pykit function and calls `implementation` to produce the function body. """ def impl(py_func, argtypes): # TODO: do this better from flypy.compiler import representation_type ll_argtypes = [representation_type(x) for x in argtypes] argnames = list(string.ascii_letters[:len(argtypes)]) # Determine return type if restype_func: result_type = restype_func(argtypes) else: result_type = restype or ll_argtypes[0] type = ptypes.Function(result_type, tuple(ll_argtypes), False) func = ir.Function(name, argnames, type) func.new_block("entry") b = ir.Builder(func) b.position_at_beginning(func.startblock) implementation(b, argtypes, *func.args) return func opaque.implement_opaque(opaque_func, impl)
# Low-level implementations #===------------------------------------------------------------------=== ## typeof() def make_typeof(py_func, argtypes): [type] = argtypes @jit def typeof_impl(obj): return type env = fresh_env(typeof_impl, tuple(argtypes), "cpu") func, env = phase.ll_lower(typeof_impl, env) return func opaque.implement_opaque(typeof, make_typeof) ## addressof() #def impl_addressof(builder, argtypes, obj): # builder.ret(builder.addressof(obj)) # #lowlevel_impls.add_impl(addressof, "addressof", impl_addressof) ## Overlays # make flypy.typeof() available in flypy code overlay(flypy.typeof, typeof)
#===------------------------------------------------------------------=== # Low-level implementations #===------------------------------------------------------------------=== ## typeof() def make_typeof(py_func, argtypes): [type] = argtypes @jit def typeof_impl(obj): return type env = fresh_env(typeof_impl, tuple(argtypes), "cpu") func, env = phase.ll_lower(typeof_impl, env) return func opaque.implement_opaque(typeof, make_typeof) ## addressof() #def impl_addressof(builder, argtypes, obj): # builder.ret(builder.addressof(obj)) # #lowlevel_impls.add_impl(addressof, "addressof", impl_addressof) ## Overlays # make flypy.typeof() available in flypy code overlay(flypy.typeof, typeof)