def test_dylib_symbols(self): llvm.add_symbol("__xyzzy", 1234) llvm.add_symbol("__xyzzy", 5678) addr = llvm.address_of_symbol("__xyzzy") self.assertEqual(addr, 5678) addr = llvm.address_of_symbol("__foobar") self.assertIs(addr, None)
def printf(builder, fmt, *args, override_debug=False): if "print_values" not in debug_env and not override_debug: return #FIXME: Fix builtin printf and use that instead of this try: import llvmlite.binding as llvm libc = util.find_library("c") llvm.load_library_permanently(libc) # Address will be none if the symbol is not found printf_address = llvm.address_of_symbol("printf") except Exception as e: return # Direct pointer constants don't work printf_ty = ir.FunctionType(ir.IntType(32), [ir.IntType(8).as_pointer()], var_arg=True) printf = builder.inttoptr( ir.IntType(64)(printf_address), printf_ty.as_pointer()) ir_module = builder.function.module fmt += "\0" int8 = ir.IntType(8) fmt_data = bytearray(fmt.encode("utf8")) fmt_ty = ir.ArrayType(int8, len(fmt_data)) global_fmt = ir.GlobalVariable(ir_module, fmt_ty, name="printf_fmt_" + str(len(ir_module.globals))) global_fmt.linkage = "internal" global_fmt.global_constant = True global_fmt.initializer = fmt_ty(fmt_data) fmt_ptr = builder.gep(global_fmt, [ir.IntType(32)(0), ir.IntType(32)(0)]) builder.call(printf, [fmt_ptr] + list(args))
def _generate_cpu_printf_wrapper(module): printf_ty = ir.FunctionType(ir.IntType(32), [ir.IntType(8).as_pointer()], var_arg=True) function = ir.Function(module, printf_ty, name=_BUILTIN_PREFIX + "printf") function.attributes.add('alwaysinline') block = function.append_basic_block(name="entry") builder = ir.IRBuilder(block) builder.debug_metadata = LLVMBuilderContext.get_debug_location( function, None) try: import llvmlite.binding as llvm libc = ctypes.util.find_library("c") llvm.load_library_permanently(libc) # Address will be none if the symbol is not found printf_address = llvm.address_of_symbol("printf") except: printf_address = None if printf_address is not None: # Direct pointer constants don't work printf = builder.inttoptr( pnlvm.ir.IntType(64)(printf_address), printf_ty.as_pointer()) builder.ret(builder.call(printf, function.args)) else: builder.ret(ir.IntType(32)(-1))
def remove_native_function(self, func): """ Remove internal references to nonpython mode function *func*. KeyError is raised if the function isn't known to us. """ name, ptr = self.native_funcs.pop(func) # If the symbol wasn't redefined, NULL it out. # (otherwise, it means the corresponding Python function was # re-compiled, and the new target is still alive) if ll.address_of_symbol(name) == ptr: ll.add_symbol(name, 0)
def get_func(self, name, *types): '''Return a function address''' func_typ = ct.CFUNCTYPE(*types) func_ptr = self.engine.get_function_address(name) if not func_ptr: func_ptr = llvm.address_of_symbol(name) if not func_ptr: Q.abort('Unable to find address of {}', name) return func_typ(func_ptr)
def _add_missing_symbol(symbol, addr): """Add missing symbol into LLVM internal symtab """ if not ll.address_of_symbol(symbol): ll.add_symbol(symbol, addr)
def dylib_address_of_symbol(name): llvm.address_of_symbol(name)