예제 #1
0
 def __call__(self, *args):
     print("*", args)
     builtin_map = {int: "i", bytes: "P"}
     argspec = ""
     callargs = []
     for a in args:
         if isinstance(a, _SimpleCData):
             callargs.append(a.value)
             argspec += a._type_
         else:
             callargs.append(a)
             argspec += builtin_map[type(a)]
     f = ffi.func("i", self.addr, argspec)
     return f(*callargs)
예제 #2
0
def import_llvm_bc(path):
    my_path = path + ".bc"
    if not os.path.isfile(my_path):
        return _old_hook(path) if _old_hook else None

    with open(my_path, "rb") as f:
        bc = f.read()

    buf = LLVMCreateMemoryBufferWithMemoryRangeCopy(bc, len(bc), my_path)
    #print(buf)

    llmod = LLVMParseBitcode2(buf)
    #print("llmod:", llmod)

    LLVMDumpModule(llmod)
    print()

    engine = compile_mod(llmod)

    pymod = imp.new_module("__llvm__")

    f = LLVMGetFirstFunction(llmod)
    while f:
        f_name = LLVMGetValueName(f)
        num_params = LLVMCountParams(f)
        #print(f_name, num_params)
        for i in range(num_params):
            param = LLVMGetParam(f, i)
            #print(" ", LLVMGetValueName(param), LLVMPrintTypeToString(LLVMTypeOf(param)), LLVMTypeOf(param) == i32_t)
            assert LLVMTypeOf(param) == i32_t

        addr = LLVMGetFunctionAddress(engine, f_name)
        #print("func addr:", f_name, addr)
        fun_obj = ffi.func("i", addr, "i" * num_params)
        setattr(pymod, f_name, fun_obj)

        f = LLVMGetNextFunction(f)

    return pymod
예제 #3
0
import ffi

show = ffi.func("v", "native_module_show", "s")
add = ffi.func("i", "native_module_add", "ii")
예제 #4
0
target_mach = LLVMCreateTargetMachine(target, def_triple, "", "",
                                      LLVMCodeGenLevelDefault,
                                      LLVMRelocDefault,
                                      LLVMCodeModelJITDefault)

orc = LLVMOrcCreateInstance(target_mach)


def orc_sym_resolver(name, ctx):
    print("orc_sym_resolver")
    addr = by_ref("Q")
    err = LLVMOrcGetSymbolAddress(orc, addr, name)
    return addr[0]


orc_sym_resolver_cb = ffi.callback("Q", orc_sym_resolver, "sp")

shmod = LLVMOrcMakeSharedModule(mod)
#print("mod", mod, "shmod", shmod)

orc_mod_ref = by_ref("P")
LLVMOrcAddLazilyCompiledIR(orc, orc_mod_ref, shmod, orc_sym_resolver_cb, None)

addr_ref = by_ref("Q")
err = LLVMOrcGetSymbolAddress(orc, addr_ref, "sum")
addr = addr_ref[0]
#print(err, addr)

f = ffi.func("i", addr, "ii")
print("result", f(5, 10))
import ffi

print = ffi.func("v", "ef_print_env", "v")
set = ffi.func("i", "ef_set_env", "ss")
get = ffi.func("s", "ef_get_env", "s")
remove = ffi.func("i", "ef_del_env", "s")