Struct Label

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

Implementations§

Source§

impl Label

Source

pub fn new() -> Label

Examples found in repository?
examples/branching.rs (line 18)
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}
More examples
Hide additional examples
examples/branch_with_result.rs (line 17)
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 30)
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}

Auto Trait Implementations§

§

impl Freeze for Label

§

impl RefUnwindSafe for Label

§

impl Send for Label

§

impl Sync for Label

§

impl Unpin for Label

§

impl UnwindSafe for Label

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.