Struct Context

Source
pub struct Context { /* private fields */ }

Implementations§

Source§

impl Context

Source

pub fn new() -> Context

Examples found in repository?
examples/mult2.rs (line 4)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![]).unwrap();
7    let zero = func.create_float64_constant(0.0);
8    func.insn_return(&zero);
9    func.compile();
10    context.build_end();
11    let function: fn() -> f64  = func.to_closure();
12    println!("{}", function());
13}
More examples
Hide additional examples
examples/mult.rs (line 4)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let int_type = Context::int_type();
9    let params = vec![int_type, int_type, int_type];
10    let mut func = context.function(Abi::Cdecl, int_type, params).unwrap();
11
12    let x = func.arg(0).unwrap();
13    let y = func.arg(1).unwrap();
14    let z = func.arg(2).unwrap();
15    let temp1 = func.insn_mult(&x, &y);
16    let temp2 = func.insn_add(&temp1, &z);
17    func.insn_return(&temp2);
18    func.compile();
19    context.build_end();
20
21    let result: extern "C" fn(i32,i32,i32) -> i32 = func.to_closure();
22    println!("3*5+2 = {}", result(3,5,2))
23}
examples/load_and_store.rs (line 4)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7    let float_type = Context::sbyte_type();
8    let params = vec![float_type];
9    let mut func = context.function(Abi::Cdecl, float_type, params).unwrap();
10
11    let x = func.arg(0).unwrap();
12    let ptr1 = func.alloca(1);
13    let ptr2 = func.alloca(8);
14
15    let tag = func.create_sbyte_constant(111);
16    let value = func.create_float64_constant(111.0);
17
18    func.insn_store(&ptr1, &tag);
19    func.insn_store(&ptr2, &value);
20
21    let res1 = func.insn_load(&ptr1);
22    let res2 = func.insn_load(&ptr2);
23
24    let res= func.insn_add(&res1, &res2);
25
26
27    func.insn_return(&res);
28    println!("{}", func.dump().unwrap());
29    func.compile();
30    context.build_end();
31
32    let result: extern "C" fn(f64) -> u8 = func.to_closure();
33    println!("{}", result(0.23))
34}
examples/branching.rs (line 4)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let float_type = Context::float64_type();
9    let mut func = context.function(Abi::Cdecl, float_type, vec![float_type]).unwrap();
10
11    // Return 1 if arg0 == 4
12    // else return 0
13    let is_four_result = func.create_float64_constant(1.0);
14    let not_four_result = func.create_float64_constant(0.0);
15
16    let x = func.arg(0).unwrap();
17    let four = func.create_float64_constant(4.0);
18    let mut label = Label::new();
19    let eq_to_4 = func.insn_eq(&x, &four);
20    func.insn_branch_if(&eq_to_4, &mut label);
21    func.insn_return(&not_four_result);
22    func.insn_label(&mut label);
23    func.insn_return(&is_four_result);
24    func.compile();
25    context.build_end();
26    let result: extern "C" fn(f64) -> f64 = func.to_closure();
27    println!("{}", result(4.0));
28}
examples/function_calls.rs (line 5)
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}
examples/branch_with_result.rs (line 4)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let i32_type = Context::int_type();
9    let mut func = context.function(Abi::Cdecl, i32_type, vec![i32_type, i32_type]).unwrap();
10
11
12    let result = func.alloca(4);
13
14    let a = func.arg(0).unwrap();
15    let b = func.arg(1).unwrap();
16    let a_eq_b = func.insn_eq(&a, &b);
17    let mut done_label = Label::new();
18    let mut eq_label = Label::new();
19
20    func.insn_branch_if(&a_eq_b, &mut eq_label);
21    let a_minus_b = func.insn_sub(&a, &b);
22    func.insn_store(&result, &a_minus_b);
23    func.insn_branch(&mut done_label);
24    func.insn_label(&mut eq_label);
25    let a_plus_b = func.insn_add(&a, &b);
26    func.insn_store(&result, &a_plus_b);
27    func.insn_label(&mut done_label);
28    let ret = func.insn_load(&result);
29    let hundred = func.create_int_constant(100);
30    let ret = func.insn_add(&ret, &hundred);
31    func.insn_return(&ret);
32
33    println!("{}", func.dump().unwrap());
34
35    func.compile();
36    println!("{}", func.dump().unwrap());
37    context.build_end();
38
39
40    let result: extern "C" fn(i32, i32) -> i32 = func.to_closure();
41    println!("{} == 102", result(1,1));
42    println!("{} == 101", result(3,2));
43}
Source

pub fn build_start(&self)

Examples found in repository?
examples/mult2.rs (line 5)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![]).unwrap();
7    let zero = func.create_float64_constant(0.0);
8    func.insn_return(&zero);
9    func.compile();
10    context.build_end();
11    let function: fn() -> f64  = func.to_closure();
12    println!("{}", function());
13}
More examples
Hide additional examples
examples/mult.rs (line 5)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let int_type = Context::int_type();
9    let params = vec![int_type, int_type, int_type];
10    let mut func = context.function(Abi::Cdecl, int_type, params).unwrap();
11
12    let x = func.arg(0).unwrap();
13    let y = func.arg(1).unwrap();
14    let z = func.arg(2).unwrap();
15    let temp1 = func.insn_mult(&x, &y);
16    let temp2 = func.insn_add(&temp1, &z);
17    func.insn_return(&temp2);
18    func.compile();
19    context.build_end();
20
21    let result: extern "C" fn(i32,i32,i32) -> i32 = func.to_closure();
22    println!("3*5+2 = {}", result(3,5,2))
23}
examples/load_and_store.rs (line 5)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7    let float_type = Context::sbyte_type();
8    let params = vec![float_type];
9    let mut func = context.function(Abi::Cdecl, float_type, params).unwrap();
10
11    let x = func.arg(0).unwrap();
12    let ptr1 = func.alloca(1);
13    let ptr2 = func.alloca(8);
14
15    let tag = func.create_sbyte_constant(111);
16    let value = func.create_float64_constant(111.0);
17
18    func.insn_store(&ptr1, &tag);
19    func.insn_store(&ptr2, &value);
20
21    let res1 = func.insn_load(&ptr1);
22    let res2 = func.insn_load(&ptr2);
23
24    let res= func.insn_add(&res1, &res2);
25
26
27    func.insn_return(&res);
28    println!("{}", func.dump().unwrap());
29    func.compile();
30    context.build_end();
31
32    let result: extern "C" fn(f64) -> u8 = func.to_closure();
33    println!("{}", result(0.23))
34}
examples/branching.rs (line 5)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let float_type = Context::float64_type();
9    let mut func = context.function(Abi::Cdecl, float_type, vec![float_type]).unwrap();
10
11    // Return 1 if arg0 == 4
12    // else return 0
13    let is_four_result = func.create_float64_constant(1.0);
14    let not_four_result = func.create_float64_constant(0.0);
15
16    let x = func.arg(0).unwrap();
17    let four = func.create_float64_constant(4.0);
18    let mut label = Label::new();
19    let eq_to_4 = func.insn_eq(&x, &four);
20    func.insn_branch_if(&eq_to_4, &mut label);
21    func.insn_return(&not_four_result);
22    func.insn_label(&mut label);
23    func.insn_return(&is_four_result);
24    func.compile();
25    context.build_end();
26    let result: extern "C" fn(f64) -> f64 = func.to_closure();
27    println!("{}", result(4.0));
28}
examples/function_calls.rs (line 6)
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}
examples/branch_with_result.rs (line 5)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let i32_type = Context::int_type();
9    let mut func = context.function(Abi::Cdecl, i32_type, vec![i32_type, i32_type]).unwrap();
10
11
12    let result = func.alloca(4);
13
14    let a = func.arg(0).unwrap();
15    let b = func.arg(1).unwrap();
16    let a_eq_b = func.insn_eq(&a, &b);
17    let mut done_label = Label::new();
18    let mut eq_label = Label::new();
19
20    func.insn_branch_if(&a_eq_b, &mut eq_label);
21    let a_minus_b = func.insn_sub(&a, &b);
22    func.insn_store(&result, &a_minus_b);
23    func.insn_branch(&mut done_label);
24    func.insn_label(&mut eq_label);
25    let a_plus_b = func.insn_add(&a, &b);
26    func.insn_store(&result, &a_plus_b);
27    func.insn_label(&mut done_label);
28    let ret = func.insn_load(&result);
29    let hundred = func.create_int_constant(100);
30    let ret = func.insn_add(&ret, &hundred);
31    func.insn_return(&ret);
32
33    println!("{}", func.dump().unwrap());
34
35    func.compile();
36    println!("{}", func.dump().unwrap());
37    context.build_end();
38
39
40    let result: extern "C" fn(i32, i32) -> i32 = func.to_closure();
41    println!("{} == 102", result(1,1));
42    println!("{} == 101", result(3,2));
43}
Source

pub fn build_end(&self)

Examples found in repository?
examples/mult2.rs (line 10)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![]).unwrap();
7    let zero = func.create_float64_constant(0.0);
8    func.insn_return(&zero);
9    func.compile();
10    context.build_end();
11    let function: fn() -> f64  = func.to_closure();
12    println!("{}", function());
13}
More examples
Hide additional examples
examples/mult.rs (line 19)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let int_type = Context::int_type();
9    let params = vec![int_type, int_type, int_type];
10    let mut func = context.function(Abi::Cdecl, int_type, params).unwrap();
11
12    let x = func.arg(0).unwrap();
13    let y = func.arg(1).unwrap();
14    let z = func.arg(2).unwrap();
15    let temp1 = func.insn_mult(&x, &y);
16    let temp2 = func.insn_add(&temp1, &z);
17    func.insn_return(&temp2);
18    func.compile();
19    context.build_end();
20
21    let result: extern "C" fn(i32,i32,i32) -> i32 = func.to_closure();
22    println!("3*5+2 = {}", result(3,5,2))
23}
examples/load_and_store.rs (line 30)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7    let float_type = Context::sbyte_type();
8    let params = vec![float_type];
9    let mut func = context.function(Abi::Cdecl, float_type, params).unwrap();
10
11    let x = func.arg(0).unwrap();
12    let ptr1 = func.alloca(1);
13    let ptr2 = func.alloca(8);
14
15    let tag = func.create_sbyte_constant(111);
16    let value = func.create_float64_constant(111.0);
17
18    func.insn_store(&ptr1, &tag);
19    func.insn_store(&ptr2, &value);
20
21    let res1 = func.insn_load(&ptr1);
22    let res2 = func.insn_load(&ptr2);
23
24    let res= func.insn_add(&res1, &res2);
25
26
27    func.insn_return(&res);
28    println!("{}", func.dump().unwrap());
29    func.compile();
30    context.build_end();
31
32    let result: extern "C" fn(f64) -> u8 = func.to_closure();
33    println!("{}", result(0.23))
34}
examples/branching.rs (line 25)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let float_type = Context::float64_type();
9    let mut func = context.function(Abi::Cdecl, float_type, vec![float_type]).unwrap();
10
11    // Return 1 if arg0 == 4
12    // else return 0
13    let is_four_result = func.create_float64_constant(1.0);
14    let not_four_result = func.create_float64_constant(0.0);
15
16    let x = func.arg(0).unwrap();
17    let four = func.create_float64_constant(4.0);
18    let mut label = Label::new();
19    let eq_to_4 = func.insn_eq(&x, &four);
20    func.insn_branch_if(&eq_to_4, &mut label);
21    func.insn_return(&not_four_result);
22    func.insn_label(&mut label);
23    func.insn_return(&is_four_result);
24    func.compile();
25    context.build_end();
26    let result: extern "C" fn(f64) -> f64 = func.to_closure();
27    println!("{}", result(4.0));
28}
examples/function_calls.rs (line 26)
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}
examples/branch_with_result.rs (line 37)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let i32_type = Context::int_type();
9    let mut func = context.function(Abi::Cdecl, i32_type, vec![i32_type, i32_type]).unwrap();
10
11
12    let result = func.alloca(4);
13
14    let a = func.arg(0).unwrap();
15    let b = func.arg(1).unwrap();
16    let a_eq_b = func.insn_eq(&a, &b);
17    let mut done_label = Label::new();
18    let mut eq_label = Label::new();
19
20    func.insn_branch_if(&a_eq_b, &mut eq_label);
21    let a_minus_b = func.insn_sub(&a, &b);
22    func.insn_store(&result, &a_minus_b);
23    func.insn_branch(&mut done_label);
24    func.insn_label(&mut eq_label);
25    let a_plus_b = func.insn_add(&a, &b);
26    func.insn_store(&result, &a_plus_b);
27    func.insn_label(&mut done_label);
28    let ret = func.insn_load(&result);
29    let hundred = func.create_int_constant(100);
30    let ret = func.insn_add(&ret, &hundred);
31    func.insn_return(&ret);
32
33    println!("{}", func.dump().unwrap());
34
35    func.compile();
36    println!("{}", func.dump().unwrap());
37    context.build_end();
38
39
40    let result: extern "C" fn(i32, i32) -> i32 = func.to_closure();
41    println!("{} == 102", result(1,1));
42    println!("{} == 101", result(3,2));
43}
Source

pub fn function( &mut self, abi: Abi, return_type: JitType, params: Vec<JitType>, ) -> Result<Function, Exception>

let mut context = Context::new(); let params = vec![JitType::Int] let function = context.function(Abi::Cdecl, JitType::Int, params.as_slice());

Examples found in repository?
examples/mult2.rs (line 6)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![]).unwrap();
7    let zero = func.create_float64_constant(0.0);
8    func.insn_return(&zero);
9    func.compile();
10    context.build_end();
11    let function: fn() -> f64  = func.to_closure();
12    println!("{}", function());
13}
More examples
Hide additional examples
examples/mult.rs (line 10)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let int_type = Context::int_type();
9    let params = vec![int_type, int_type, int_type];
10    let mut func = context.function(Abi::Cdecl, int_type, params).unwrap();
11
12    let x = func.arg(0).unwrap();
13    let y = func.arg(1).unwrap();
14    let z = func.arg(2).unwrap();
15    let temp1 = func.insn_mult(&x, &y);
16    let temp2 = func.insn_add(&temp1, &z);
17    func.insn_return(&temp2);
18    func.compile();
19    context.build_end();
20
21    let result: extern "C" fn(i32,i32,i32) -> i32 = func.to_closure();
22    println!("3*5+2 = {}", result(3,5,2))
23}
examples/load_and_store.rs (line 9)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7    let float_type = Context::sbyte_type();
8    let params = vec![float_type];
9    let mut func = context.function(Abi::Cdecl, float_type, params).unwrap();
10
11    let x = func.arg(0).unwrap();
12    let ptr1 = func.alloca(1);
13    let ptr2 = func.alloca(8);
14
15    let tag = func.create_sbyte_constant(111);
16    let value = func.create_float64_constant(111.0);
17
18    func.insn_store(&ptr1, &tag);
19    func.insn_store(&ptr2, &value);
20
21    let res1 = func.insn_load(&ptr1);
22    let res2 = func.insn_load(&ptr2);
23
24    let res= func.insn_add(&res1, &res2);
25
26
27    func.insn_return(&res);
28    println!("{}", func.dump().unwrap());
29    func.compile();
30    context.build_end();
31
32    let result: extern "C" fn(f64) -> u8 = func.to_closure();
33    println!("{}", result(0.23))
34}
examples/branching.rs (line 9)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let float_type = Context::float64_type();
9    let mut func = context.function(Abi::Cdecl, float_type, vec![float_type]).unwrap();
10
11    // Return 1 if arg0 == 4
12    // else return 0
13    let is_four_result = func.create_float64_constant(1.0);
14    let not_four_result = func.create_float64_constant(0.0);
15
16    let x = func.arg(0).unwrap();
17    let four = func.create_float64_constant(4.0);
18    let mut label = Label::new();
19    let eq_to_4 = func.insn_eq(&x, &four);
20    func.insn_branch_if(&eq_to_4, &mut label);
21    func.insn_return(&not_four_result);
22    func.insn_label(&mut label);
23    func.insn_return(&is_four_result);
24    func.compile();
25    context.build_end();
26    let result: extern "C" fn(f64) -> f64 = func.to_closure();
27    println!("{}", result(4.0));
28}
examples/function_calls.rs (line 10)
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}
examples/branch_with_result.rs (line 9)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let i32_type = Context::int_type();
9    let mut func = context.function(Abi::Cdecl, i32_type, vec![i32_type, i32_type]).unwrap();
10
11
12    let result = func.alloca(4);
13
14    let a = func.arg(0).unwrap();
15    let b = func.arg(1).unwrap();
16    let a_eq_b = func.insn_eq(&a, &b);
17    let mut done_label = Label::new();
18    let mut eq_label = Label::new();
19
20    func.insn_branch_if(&a_eq_b, &mut eq_label);
21    let a_minus_b = func.insn_sub(&a, &b);
22    func.insn_store(&result, &a_minus_b);
23    func.insn_branch(&mut done_label);
24    func.insn_label(&mut eq_label);
25    let a_plus_b = func.insn_add(&a, &b);
26    func.insn_store(&result, &a_plus_b);
27    func.insn_label(&mut done_label);
28    let ret = func.insn_load(&result);
29    let hundred = func.create_int_constant(100);
30    let ret = func.insn_add(&ret, &hundred);
31    func.insn_return(&ret);
32
33    println!("{}", func.dump().unwrap());
34
35    func.compile();
36    println!("{}", func.dump().unwrap());
37    context.build_end();
38
39
40    let result: extern "C" fn(i32, i32) -> i32 = func.to_closure();
41    println!("{} == 102", result(1,1));
42    println!("{} == 101", result(3,2));
43}
Source

pub fn int_type() -> JitType

Examples found in repository?
examples/mult.rs (line 8)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let int_type = Context::int_type();
9    let params = vec![int_type, int_type, int_type];
10    let mut func = context.function(Abi::Cdecl, int_type, params).unwrap();
11
12    let x = func.arg(0).unwrap();
13    let y = func.arg(1).unwrap();
14    let z = func.arg(2).unwrap();
15    let temp1 = func.insn_mult(&x, &y);
16    let temp2 = func.insn_add(&temp1, &z);
17    func.insn_return(&temp2);
18    func.compile();
19    context.build_end();
20
21    let result: extern "C" fn(i32,i32,i32) -> i32 = func.to_closure();
22    println!("3*5+2 = {}", result(3,5,2))
23}
More examples
Hide additional examples
examples/function_calls.rs (line 9)
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}
examples/branch_with_result.rs (line 8)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let i32_type = Context::int_type();
9    let mut func = context.function(Abi::Cdecl, i32_type, vec![i32_type, i32_type]).unwrap();
10
11
12    let result = func.alloca(4);
13
14    let a = func.arg(0).unwrap();
15    let b = func.arg(1).unwrap();
16    let a_eq_b = func.insn_eq(&a, &b);
17    let mut done_label = Label::new();
18    let mut eq_label = Label::new();
19
20    func.insn_branch_if(&a_eq_b, &mut eq_label);
21    let a_minus_b = func.insn_sub(&a, &b);
22    func.insn_store(&result, &a_minus_b);
23    func.insn_branch(&mut done_label);
24    func.insn_label(&mut eq_label);
25    let a_plus_b = func.insn_add(&a, &b);
26    func.insn_store(&result, &a_plus_b);
27    func.insn_label(&mut done_label);
28    let ret = func.insn_load(&result);
29    let hundred = func.create_int_constant(100);
30    let ret = func.insn_add(&ret, &hundred);
31    func.insn_return(&ret);
32
33    println!("{}", func.dump().unwrap());
34
35    func.compile();
36    println!("{}", func.dump().unwrap());
37    context.build_end();
38
39
40    let result: extern "C" fn(i32, i32) -> i32 = func.to_closure();
41    println!("{} == 102", result(1,1));
42    println!("{} == 101", result(3,2));
43}
Source

pub fn long_type() -> JitType

Source

pub fn ulong_type() -> JitType

Source

pub fn float32_type() -> JitType

Source

pub fn float64_type() -> JitType

Examples found in repository?
examples/mult2.rs (line 6)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![]).unwrap();
7    let zero = func.create_float64_constant(0.0);
8    func.insn_return(&zero);
9    func.compile();
10    context.build_end();
11    let function: fn() -> f64  = func.to_closure();
12    println!("{}", function());
13}
More examples
Hide additional examples
examples/branching.rs (line 8)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7
8    let float_type = Context::float64_type();
9    let mut func = context.function(Abi::Cdecl, float_type, vec![float_type]).unwrap();
10
11    // Return 1 if arg0 == 4
12    // else return 0
13    let is_four_result = func.create_float64_constant(1.0);
14    let not_four_result = func.create_float64_constant(0.0);
15
16    let x = func.arg(0).unwrap();
17    let four = func.create_float64_constant(4.0);
18    let mut label = Label::new();
19    let eq_to_4 = func.insn_eq(&x, &four);
20    func.insn_branch_if(&eq_to_4, &mut label);
21    func.insn_return(&not_four_result);
22    func.insn_label(&mut label);
23    func.insn_return(&is_four_result);
24    func.compile();
25    context.build_end();
26    let result: extern "C" fn(f64) -> f64 = func.to_closure();
27    println!("{}", result(4.0));
28}
examples/abis.rs (line 10)
4fn main() {
5    let mut context = Context::new();
6    context.build_start();
7
8    // INNER FUNC
9    // Tried messing with this ABI but it really made no difference *shrug*
10    let mut inner_func = context.function(Abi::VarArg   , Context::float64_type(), vec![Context::float64_type(), Context::float64_type()]).unwrap();
11    let arg = inner_func.arg(0).unwrap();
12    let arg2 = inner_func.arg(1).unwrap();
13    let arg_plus_arg = inner_func.insn_add(&arg, &arg2);
14    inner_func.insn_return(&arg_plus_arg);
15    inner_func.compile();
16
17    // DONE
18
19
20    let mut func = context.function(Abi::Cdecl, Context::float64_type(), vec![Context::float64_type()]).unwrap();
21    let arg = func.arg(0).unwrap();
22    let zero = func.create_int_constant(0);
23    let one = func.create_int_constant(1);
24    let hundred_k = func.create_int_constant(50_000_000);
25    let i = func.create_value_int();
26    let f = func.create_value_float64();
27    let zero_f = func.create_float64_constant(0.0);
28    func.insn_store(&i, &zero);
29    func.insn_store(&f, &zero_f);
30    let mut done_lbl = Label::new();
31    let mut test_lbl = Label::new();
32    func.insn_label(&mut test_lbl);
33
34    let is_greater = func.insn_gt(&i, &hundred_k);
35    func.insn_branch_if(&is_greater, &mut done_lbl);
36
37    let load_i = func.insn_load(&i);
38    let i_plus_1 = func.insn_add(&load_i, &one);
39    func.insn_store(&i, &i_plus_1);
40
41    let res = func.insn_call(&inner_func, vec![f.clone(), arg.clone()]);
42    func.insn_store(&f, &res);
43    func.insn_branch(&mut test_lbl);
44
45    func.insn_label(&mut done_lbl);
46
47    func.insn_return(&f);
48    println!("func: {}", func.dump().unwrap());
49    func.compile();
50    println!("func: {}", func.dump().unwrap());
51
52
53    context.build_end();
54
55
56
57    let func: extern "C" fn(f64) -> f64 =  func.to_closure();
58    let now = Instant::now();
59    println!("{}", func(1.1));
60    println!("Elapsed: {}ms", now.elapsed().as_millis());
61}
Source

pub fn sbyte_type() -> JitType

Examples found in repository?
examples/load_and_store.rs (line 7)
3fn main() {
4    let mut context = Context::new();
5    context.build_start();
6
7    let float_type = Context::sbyte_type();
8    let params = vec![float_type];
9    let mut func = context.function(Abi::Cdecl, float_type, params).unwrap();
10
11    let x = func.arg(0).unwrap();
12    let ptr1 = func.alloca(1);
13    let ptr2 = func.alloca(8);
14
15    let tag = func.create_sbyte_constant(111);
16    let value = func.create_float64_constant(111.0);
17
18    func.insn_store(&ptr1, &tag);
19    func.insn_store(&ptr2, &value);
20
21    let res1 = func.insn_load(&ptr1);
22    let res2 = func.insn_load(&ptr2);
23
24    let res= func.insn_add(&res1, &res2);
25
26
27    func.insn_return(&res);
28    println!("{}", func.dump().unwrap());
29    func.compile();
30    context.build_end();
31
32    let result: extern "C" fn(f64) -> u8 = func.to_closure();
33    println!("{}", result(0.23))
34}
Source

pub fn ubyte_type() -> JitType

Source

pub fn void_ptr_type() -> JitType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.