Path: blob/main/cranelift/jit/examples/jit-minimal.rs
1692 views
use codegen::ir::UserFuncName;1use cranelift::prelude::*;2use cranelift_jit::{JITBuilder, JITModule};3use cranelift_module::{Linkage, Module, default_libcall_names};4use std::mem;56fn main() {7let mut flag_builder = settings::builder();8flag_builder.set("use_colocated_libcalls", "false").unwrap();9// FIXME set back to true once the x64 backend supports it.10flag_builder.set("is_pic", "false").unwrap();11let isa_builder = cranelift_native::builder().unwrap_or_else(|msg| {12panic!("host machine is not supported: {msg}");13});14let isa = isa_builder15.finish(settings::Flags::new(flag_builder))16.unwrap();17let mut module = JITModule::new(JITBuilder::with_isa(isa, default_libcall_names()));1819let mut ctx = module.make_context();20let mut func_ctx = FunctionBuilderContext::new();2122let mut sig_a = module.make_signature();23sig_a.params.push(AbiParam::new(types::I32));24sig_a.returns.push(AbiParam::new(types::I32));2526let mut sig_b = module.make_signature();27sig_b.returns.push(AbiParam::new(types::I32));2829let func_a = module30.declare_function("a", Linkage::Local, &sig_a)31.unwrap();32let func_b = module33.declare_function("b", Linkage::Local, &sig_b)34.unwrap();3536ctx.func.signature = sig_a;37ctx.func.name = UserFuncName::user(0, func_a.as_u32());3839{40let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);41let block = bcx.create_block();4243bcx.switch_to_block(block);44bcx.append_block_params_for_function_params(block);45let param = bcx.block_params(block)[0];46let cst = bcx.ins().iconst(types::I32, 37);47let add = bcx.ins().iadd(cst, param);48bcx.ins().return_(&[add]);49bcx.seal_all_blocks();50bcx.finalize();51}52module.define_function(func_a, &mut ctx).unwrap();53module.clear_context(&mut ctx);5455ctx.func.signature = sig_b;56ctx.func.name = UserFuncName::user(0, func_b.as_u32());5758{59let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);60let block = bcx.create_block();6162bcx.switch_to_block(block);63let local_func = module.declare_func_in_func(func_a, &mut bcx.func);64let arg = bcx.ins().iconst(types::I32, 5);65let call = bcx.ins().call(local_func, &[arg]);66let value = {67let results = bcx.inst_results(call);68assert_eq!(results.len(), 1);69results[0]70};71bcx.ins().return_(&[value]);72bcx.seal_all_blocks();73bcx.finalize();74}75module.define_function(func_b, &mut ctx).unwrap();76module.clear_context(&mut ctx);7778// Perform linking.79module.finalize_definitions().unwrap();8081// Get a raw pointer to the generated code.82let code_b = module.get_finalized_function(func_b);8384// Cast it to a rust function pointer type.85let ptr_b = unsafe { mem::transmute::<_, extern "C" fn() -> u32>(code_b) };8687// Call it!88let res = ptr_b();8990assert_eq!(res, 42);91}929394