[][src]Struct inkwell::execution_engine::ExecutionEngine

pub struct ExecutionEngine { /* fields omitted */ }

A reference-counted wrapper around LLVM's execution engine.

Note

Cloning this object is essentially just a case of copying a couple pointers and incrementing one or two atomics, so this should be quite cheap to create copies. The underlying LLVM object will be automatically deallocated when there are no more references to it.

Methods

impl ExecutionEngine[src]

This function probably doesn't need to be called, but is here due to linking(?) requirements. Bad things happen if we don't provide it.

This function probably doesn't need to be called, but is here due to linking(?) requirements. Bad things happen if we don't provide it.

pub fn add_global_mapping(&self, value: &dyn AnyValue, addr: usize)[src]

Maps the specified value to an address.

Example

use inkwell::targets::{InitializationConfig, Target};
use inkwell::context::Context;
use inkwell::OptimizationLevel;

Target::initialize_native(&InitializationConfig::default()).unwrap();

extern fn sumf(a: f64, b: f64) -> f64 {
    a + b
}

let context = Context::create();
let module = context.create_module("test");
let builder = context.create_builder();

let ft = context.f64_type();
let fnt = ft.fn_type(&[], false);

let f = module.add_function("test_fn", fnt, None);
let b = context.append_basic_block(&f, "entry");

builder.position_at_end(&b);

let extf = module.add_function("sumf", ft.fn_type(&[ft.into(), ft.into()], false), None);

let argf = ft.const_float(64.);
let call_site_value = builder.build_call(extf, &[argf.into(), argf.into()], "retv");
let retv = call_site_value.try_as_basic_value().left().unwrap().into_float_value();

builder.build_return(Some(&retv));

let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();
ee.add_global_mapping(&extf, sumf as usize);

let result = unsafe { ee.run_function(&f, &[]) }.as_float(&ft);

assert_eq!(result, 128.);

pub fn add_module(&self, module: &Module) -> Result<(), ()>[src]

Adds a module to an ExecutionEngine.

The method will be Ok(()) if the module does not belong to an ExecutionEngine already and Err(()) otherwise.

use inkwell::targets::{InitializationConfig, Target};
use inkwell::context::Context;
use inkwell::OptimizationLevel;

Target::initialize_native(&InitializationConfig::default()).unwrap();

let context = Context::create();
let module = context.create_module("test");
let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

assert!(ee.add_module(&module).is_err());

pub fn remove_module(&self, module: &Module) -> Result<(), RemoveModuleError>[src]

pub unsafe fn get_function<F>(
    &self,
    fn_name: &str
) -> Result<JitFunction<F>, FunctionLookupError> where
    F: UnsafeFunctionPointer
[src]

Try to load a function from the execution engine.

If a target hasn't already been initialized, spurious "function not found" errors may be encountered.

The UnsafeFunctionPointer trait is designed so only unsafe extern "C" functions can be retrieved via the get_function() method. If you get funny type errors then it's probably because you have specified the wrong calling convention or forgotten to specify the retrieved function as unsafe.

Examples

let context = Context::create();
let module = context.create_module("test");
let builder = context.create_builder();

// Set up the function signature
let double = context.f64_type();
let sig = double.fn_type(&[], false);

// Add the function to our module
let f = module.add_function("test_fn", sig, None);
let b = context.append_basic_block(&f, "entry");
builder.position_at_end(&b);

// Insert a return statement
let ret = double.const_float(64.0);
builder.build_return(Some(&ret));

// create the JIT engine
let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

// fetch our JIT'd function and execute it
unsafe {
    let test_fn = ee.get_function::<unsafe extern "C" fn() -> f64>("test_fn").unwrap();
    let return_value = test_fn.call();
    assert_eq!(return_value, 64.0);
}

Safety

It is the caller's responsibility to ensure they call the function with the correct signature and calling convention.

The JitFunction wrapper ensures a function won't accidentally outlive the execution engine it came from, but adding functions after calling this method may invalidate the function pointer.

pub fn get_target_data(&self) -> &TargetData[src]

pub fn get_function_value(
    &self,
    fn_name: &str
) -> Result<FunctionValue, FunctionLookupError>
[src]

pub unsafe fn run_function(
    &self,
    function: &FunctionValue,
    args: &[&GenericValue]
) -> GenericValue
[src]

pub unsafe fn run_function_as_main(
    &self,
    function: &FunctionValue,
    args: &[&str]
) -> c_int
[src]

pub fn free_fn_machine_code(&self, function: &FunctionValue)[src]

pub fn run_static_constructors(&self)[src]

pub fn run_static_destructors(&self)[src]

Trait Implementations

impl Eq for ExecutionEngine[src]

impl Drop for ExecutionEngine[src]

impl Clone for ExecutionEngine[src]

impl PartialEq<ExecutionEngine> for ExecutionEngine[src]

impl Debug for ExecutionEngine[src]

Auto Trait Implementations

impl !Sync for ExecutionEngine

impl !Send for ExecutionEngine

impl Unpin for ExecutionEngine

impl UnwindSafe for ExecutionEngine

impl !RefUnwindSafe for ExecutionEngine

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]