Skip to main content

compile

Function compile 

Source
pub fn compile(func: &Function) -> Result<Compiled, JitError>
Expand description

Compiles a function with a fresh host engine, for the one-off case.

This is the shortcut for compiling a single function where holding onto a Jit does not pay off. It builds an engine, compiles, and returns the runnable Compiled. When compiling several functions, build one Jit with Jit::new and reuse it — that creates the host code generator once instead of per call.

§Errors

The same as Jit::new followed by Jit::compile: the host being unsupported, the function being invalid or using an unsupported feature, a code-generation failure, or executable memory being unavailable.

§Examples

use jit_lang::compile;
use ir_lang::{Builder, BinOp, Type};

// fn square(x: int) -> int { x * x }
let mut b = Builder::new("square", &[Type::Int], Type::Int);
let x = b.block_params(b.entry())[0];
let sq = b.bin(BinOp::Mul, x, x);
b.ret(Some(sq));

let square = compile(&b.finish()).expect("square is well-formed");

// SAFETY: the signature is `fn(int) -> int`, and `square` outlives the call.
let square_fn: extern "C" fn(i64) -> i64 = unsafe { square.entry() };
assert_eq!(square_fn(7), 49);