|
1 | 1 | use llvm_sys::core::LLVMDisposeMessage;
|
2 |
| -use llvm_sys::execution_engine::{LLVMGetExecutionEngineTargetData, LLVMExecutionEngineRef, LLVMRunFunction, LLVMRunFunctionAsMain, LLVMDisposeExecutionEngine, LLVMGetFunctionAddress, LLVMAddModule, LLVMFindFunction, LLVMLinkInMCJIT, LLVMLinkInInterpreter, LLVMRemoveModule, LLVMGenericValueRef, LLVMFreeMachineCodeForFunction, LLVMGetPointerToGlobal}; |
| 2 | +use llvm_sys::execution_engine::{LLVMGetExecutionEngineTargetData, LLVMExecutionEngineRef, LLVMRunFunction, LLVMRunFunctionAsMain, LLVMDisposeExecutionEngine, LLVMGetFunctionAddress, LLVMAddModule, LLVMFindFunction, LLVMLinkInMCJIT, LLVMLinkInInterpreter, LLVMRemoveModule, LLVMGenericValueRef, LLVMFreeMachineCodeForFunction, LLVMGetPointerToGlobal, LLVMAddGlobalMapping}; |
3 | 3 |
|
4 | 4 | use module::Module;
|
5 | 5 | use targets::TargetData;
|
6 |
| -use values::{AsValueRef, FunctionValue, GenericValue, GlobalValue}; |
| 6 | +use values::{AnyValue, AsValueRef, FunctionValue, GenericValue, GlobalValue}; |
7 | 7 |
|
8 | 8 | use std::ffi::{CStr, CString};
|
9 | 9 | use std::mem::{forget, uninitialized, zeroed};
|
@@ -51,11 +51,50 @@ impl ExecutionEngine {
|
51 | 51 | }
|
52 | 52 | }
|
53 | 53 |
|
54 |
| - // pub fn add_global_mapping(&mut self, global: &AnyValue, addr: *const ()) { |
55 |
| - // unsafe { |
56 |
| - // LLVMAddGlobalMapping(self.execution_engine, global.as_value_ref(), addr as *mut ::libc::c_void) |
57 |
| - // } |
58 |
| - // } |
| 54 | + /// Maps the specified value to an address. |
| 55 | + /// |
| 56 | + /// # Example |
| 57 | + /// ```no_run |
| 58 | + /// use inkwell::targets::{InitializationConfig, Target}; |
| 59 | + /// use inkwell::context::Context; |
| 60 | + /// use inkwell::OptimizationLevel; |
| 61 | + /// |
| 62 | + /// Target::initialize_native(&InitializationConfig::default()).unwrap(); |
| 63 | + /// |
| 64 | + /// extern fn sumf(a: f64, b: f64) -> f64 { |
| 65 | + /// a + b |
| 66 | + /// } |
| 67 | + /// |
| 68 | + /// let context = Context::create(); |
| 69 | + /// let module = context.create_module("test"); |
| 70 | + /// let builder = context.create_builder(); |
| 71 | + /// |
| 72 | + /// let ft = context.f64_type(); |
| 73 | + /// let fnt = ft.fn_type(&[], false); |
| 74 | + /// |
| 75 | + /// let f = module.add_function("test_fn", &fnt, None); |
| 76 | + /// let b = context.append_basic_block(&f, "entry"); |
| 77 | + /// builder.position_at_end(&b); |
| 78 | + /// |
| 79 | + /// let extf = module.add_function("sumf", &ft.fn_type(&[ &ft, &ft ], false), None); |
| 80 | + /// |
| 81 | + /// let argf = ft.const_float(64.); |
| 82 | + /// let retv = builder.build_call(&extf, &[ &argf, &argf ], "retv", false).left().unwrap().into_float_value(); |
| 83 | + /// |
| 84 | + /// builder.build_return(Some(&retv)); |
| 85 | + /// |
| 86 | + /// let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); |
| 87 | + /// ee.add_global_mapping(&extf, sumf as usize); |
| 88 | + /// |
| 89 | + /// let result = unsafe { ee.run_function(&f, &[]) }.as_float(&ft); |
| 90 | + /// |
| 91 | + /// assert_eq!(result, 128.); |
| 92 | + /// ``` |
| 93 | + pub fn add_global_mapping(&mut self, value: &AnyValue, addr: usize) { |
| 94 | + unsafe { |
| 95 | + LLVMAddGlobalMapping(self.execution_engine, value.as_value_ref(), addr as *mut _) |
| 96 | + } |
| 97 | + } |
59 | 98 |
|
60 | 99 | // TODOC: EE must *own* modules and deal out references
|
61 | 100 | pub fn add_module(&mut self, module: Module) -> &Module {
|
|
0 commit comments