function_calls/
function_calls.rs

1use gnu_libjit_sys::jit_function_create;
2use gnu_libjit::{Abi, Context};
3
4fn main() {
5    let mut context = Context::new();
6    context.build_start();
7
8    // This function multiplies int by 2
9    let int_type = Context::int_type();
10    let mut func_mult_by_2 = context.function(Abi::Cdecl, int_type, vec![int_type]).unwrap();
11    let x = func_mult_by_2.arg(0).unwrap();
12    let const_1 = func_mult_by_2.create_int_constant(2);
13    let temp1 = func_mult_by_2.insn_mult(&x, &const_1);
14    func_mult_by_2.insn_return(&temp1);
15    func_mult_by_2.compile();
16
17    // This main function has 1 arg, it adds 5 to the arg and calls func_mult_by_2 and returns that value.
18    let mut func = context.function(Abi::Cdecl, int_type, vec![int_type]).unwrap();
19    let arg0 = func.arg(0).unwrap();
20    let five = func.create_int_constant(5);
21    let arg0_plus_5 = func.insn_add(&arg0, &five);
22    let res = func.insn_call(&func_mult_by_2, vec![arg0_plus_5]);
23    func.insn_return(&res);
24    func.compile();
25
26    context.build_end();
27
28    let result: extern "C" fn(i32) -> i32 = func.to_closure();
29    println!("(100+5)*2 == {}", result(100))
30}