xir_cranelift/
lib.rs

1//! Cranelift intermediate representation for XIR.
2
3#![feature(allocator_api)]
4
5mod operation;
6mod r#type;
7
8pub use operation::*;
9pub use r#type::*;
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    use xir::{Attribute, Block, BlockArgument, Context, Span};
15
16    #[test]
17    fn add_integers() {
18        let context = Context::new();
19        let span = Span::new(&context, "", 0, 0);
20        let r#type = i64_type(span);
21        let mut block = Block::new(&context, [BlockArgument::new(r#type, span)]);
22        let argument = block.arguments()[0].into();
23
24        block
25            .operations_mut()
26            .push_back(iconst(&context, r#type, Attribute::I64(42), span));
27        let value = block.operations().back().unwrap().value(&context, 0);
28
29        block
30            .operations_mut()
31            .push_back(iadd(&context, value, argument, r#type, span));
32    }
33
34    #[test]
35    fn load_integer() {
36        let context = Context::new();
37        let span = Span::new(&context, "", 0, 0);
38        let integer_type = i64_type(span);
39        let pointer_type = super::pointer_type(span);
40        let mut block = Block::new(&context, [BlockArgument::new(pointer_type, span)]);
41        let pointer = block.arguments()[0].into();
42
43        block
44            .operations_mut()
45            .push_back(load(&context, integer_type, pointer, span));
46    }
47
48    #[test]
49    fn store_integer() {
50        let context = Context::new();
51        let span = Span::new(&context, "", 0, 0);
52        let integer_type = i64_type(span);
53        let pointer_type = super::pointer_type(span);
54        let mut block = Block::new(
55            &context,
56            [
57                BlockArgument::new(integer_type, span),
58                BlockArgument::new(pointer_type, span),
59            ],
60        );
61        let value = block.arguments()[0].into();
62        let pointer = block.arguments()[1].into();
63
64        block
65            .operations_mut()
66            .push_back(store(&context, value, pointer, span));
67    }
68}