pub struct Function { /* private fields */ }
Implementations§
Source§impl Function
impl Function
Sourcepub fn compile(&self)
pub fn compile(&self)
Examples found in repository?
More examples
examples/mult.rs (line 18)
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 29)
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 24)
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(¬_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 15)
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 35)
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}
Additional examples can be found in:
Sourcepub fn alloca(&self, size: c_long) -> Value
pub fn alloca(&self, size: c_long) -> Value
Examples found in repository?
examples/load_and_store.rs (line 12)
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}
More examples
examples/branch_with_result.rs (line 12)
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}
Sourcepub fn dump(&self) -> Result<String, Error>
pub fn dump(&self) -> Result<String, Error>
Examples found in repository?
examples/load_and_store.rs (line 28)
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}
More examples
examples/branch_with_result.rs (line 33)
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}
examples/abis.rs (line 48)
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}
Sourcepub fn to_closure<T>(&self) -> T
pub fn to_closure<T>(&self) -> T
Examples found in repository?
More examples
examples/mult.rs (line 21)
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 32)
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 26)
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(¬_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 28)
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 40)
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}
Additional examples can be found in:
pub fn insn_call_native( &self, native_func: *mut c_void, params: Vec<Value>, return_type: Option<JitType>, ) -> Value
Sourcepub fn arg(&self, idx: i32) -> Result<Value, Exception>
pub fn arg(&self, idx: i32) -> Result<Value, Exception>
Examples found in repository?
examples/mult.rs (line 12)
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
examples/load_and_store.rs (line 11)
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 16)
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(¬_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 11)
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 14)
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}
examples/abis.rs (line 11)
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}
Sourcepub fn insn_mult(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_mult(&mut self, left: &Value, right: &Value) -> Value
Examples found in repository?
examples/mult.rs (line 15)
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
examples/function_calls.rs (line 13)
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}
Sourcepub fn insn_add(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_add(&mut self, left: &Value, right: &Value) -> Value
Examples found in repository?
examples/mult.rs (line 16)
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
examples/load_and_store.rs (line 24)
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/function_calls.rs (line 21)
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 25)
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}
examples/abis.rs (line 13)
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}
pub fn insn_pow(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_rem(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_div(&mut self, left: &Value, right: &Value) -> Value
Sourcepub fn insn_sub(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_sub(&mut self, left: &Value, right: &Value) -> Value
Examples found in repository?
examples/branch_with_result.rs (line 21)
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}
Sourcepub fn insn_eq(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_eq(&mut self, left: &Value, right: &Value) -> Value
Examples found in repository?
examples/branching.rs (line 19)
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(¬_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}
More examples
examples/branch_with_result.rs (line 16)
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}
pub fn insn_and(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_or(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_xor(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_le(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_lt(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_ge(&mut self, left: &Value, right: &Value) -> Value
Sourcepub fn insn_gt(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_gt(&mut self, left: &Value, right: &Value) -> Value
Examples found in repository?
examples/abis.rs (line 34)
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}
pub fn insn_ne(&mut self, left: &Value, right: &Value) -> Value
pub fn insn_acos(&mut self, value: &Value) -> Value
pub fn insn_asin(&mut self, value: &Value) -> Value
pub fn insn_atan(&mut self, value: &Value) -> Value
pub fn insn_cos(&mut self, value: &Value) -> Value
pub fn insn_cosh(&mut self, value: &Value) -> Value
pub fn insn_exp(&mut self, value: &Value) -> Value
pub fn insn_log(&mut self, value: &Value) -> Value
pub fn insn_log10(&mut self, value: &Value) -> Value
pub fn insn_sin(&mut self, value: &Value) -> Value
pub fn insn_sinh(&mut self, value: &Value) -> Value
pub fn insn_sqrt(&mut self, value: &Value) -> Value
pub fn insn_tan(&mut self, value: &Value) -> Value
pub fn insn_tanh(&mut self, value: &Value) -> Value
Sourcepub fn insn_return(&mut self, value: &Value)
pub fn insn_return(&mut self, value: &Value)
Examples found in repository?
More examples
examples/mult.rs (line 17)
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 27)
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 21)
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(¬_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 14)
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 31)
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}
Additional examples can be found in:
pub fn insn_not(&mut self, value: &Value) -> Value
Sourcepub fn insn_branch(&self, label: &mut Label)
pub fn insn_branch(&self, label: &mut Label)
Examples found in repository?
examples/branch_with_result.rs (line 23)
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}
More examples
examples/abis.rs (line 43)
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}
Sourcepub fn insn_branch_if(&self, value: &Value, label: &mut Label)
pub fn insn_branch_if(&self, value: &Value, label: &mut Label)
Examples found in repository?
examples/branching.rs (line 20)
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(¬_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}
More examples
examples/branch_with_result.rs (line 20)
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}
examples/abis.rs (line 35)
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}
pub fn insn_branch_if_not(&self, value: &Value, label: &mut Label)
Sourcepub fn insn_load(&mut self, ptr: &Value) -> Value
pub fn insn_load(&mut self, ptr: &Value) -> Value
Examples found in repository?
examples/load_and_store.rs (line 21)
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}
More examples
examples/branch_with_result.rs (line 28)
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}
examples/abis.rs (line 37)
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}
Sourcepub fn insn_store(&mut self, ptr: &Value, value: &Value)
pub fn insn_store(&mut self, ptr: &Value, value: &Value)
Examples found in repository?
examples/load_and_store.rs (line 18)
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}
More examples
examples/branch_with_result.rs (line 22)
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}
examples/abis.rs (line 28)
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}
Sourcepub fn insn_call(&mut self, function: &Function, args: Vec<Value>) -> Value
pub fn insn_call(&mut self, function: &Function, args: Vec<Value>) -> Value
Examples found in repository?
examples/function_calls.rs (line 22)
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}
More examples
examples/abis.rs (line 41)
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}
pub fn insn_load_relative( &mut self, base_ptr: &Value, offset_bytes: c_long, typ: &JitType, ) -> Value
Sourcepub fn create_value_int(&mut self) -> Value
pub fn create_value_int(&mut self) -> Value
Examples found in repository?
examples/abis.rs (line 25)
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}
pub fn create_value_void_ptr(&mut self) -> Value
pub fn create_value_float32(&mut self) -> Value
Sourcepub fn create_value_float64(&mut self) -> Value
pub fn create_value_float64(&mut self) -> Value
Examples found in repository?
examples/abis.rs (line 26)
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}
Sourcepub fn insn_label(&self, label: &mut Label)
pub fn insn_label(&self, label: &mut Label)
Examples found in repository?
examples/branching.rs (line 22)
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(¬_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}
More examples
examples/branch_with_result.rs (line 24)
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}
examples/abis.rs (line 32)
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}
pub fn insn_ceil(&self, value: &Value) -> Value
pub fn insn_floor(&self, value: &Value) -> Value
pub fn insn_rint(&self, value: &Value) -> Value
pub fn insn_round(&self, value: &Value) -> Value
pub fn insn_trunc(&self, value: &Value) -> Value
pub fn insn_load_elem_address( &self, base_addr: &Value, index: &Value, elem_type: &JitType, ) -> Value
pub fn create_float32_constant(&mut self, constant_value: c_float) -> Value
Sourcepub fn create_float64_constant(&mut self, constant_value: c_double) -> Value
pub fn create_float64_constant(&mut self, constant_value: c_double) -> Value
Examples found in repository?
More examples
examples/load_and_store.rs (line 16)
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 13)
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(¬_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 27)
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}
pub fn create_long_constant(&mut self, constant_value: c_long) -> Value
Sourcepub fn create_sbyte_constant(&mut self, constant_value: c_char) -> Value
pub fn create_sbyte_constant(&mut self, constant_value: c_char) -> Value
Examples found in repository?
examples/load_and_store.rs (line 15)
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}
pub fn create_ubyte_constant(&mut self, constant_value: c_uchar) -> Value
pub fn create_short_constant(&mut self, constant_value: c_long) -> Value
pub fn create_ushort_constant(&mut self, constant_value: c_ulong) -> Value
Sourcepub fn create_int_constant(&mut self, constant_value: c_int) -> Value
pub fn create_int_constant(&mut self, constant_value: c_int) -> Value
Examples found in repository?
examples/function_calls.rs (line 12)
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}
More examples
examples/branch_with_result.rs (line 29)
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}
examples/abis.rs (line 22)
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}
pub fn create_ulong_constant(&mut self, constant_value: c_ulong) -> Value
pub fn create_uint_constant(&mut self, constant_value: c_uint) -> Value
pub fn create_nint_constant(&mut self, constant_value: c_int) -> Value
pub fn create_nuint_constant(&mut self, constant_value: c_int) -> Value
pub fn create_void_ptr_constant(&mut self, constant_value: *mut c_void) -> Value
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Function
impl RefUnwindSafe for Function
impl !Send for Function
impl !Sync for Function
impl Unpin for Function
impl UnwindSafe for Function
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more