1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::{
    ir::{operation, Location, NamedAttribute, Operation, Type, Value},
    Context,
};

macro_rules! impl_arith_binary_op {
    ($name:ident, $op:literal) => {
        pub fn $name<'c>(
            lhs: Value<'c>,
            rhs: Value<'c>,
            result: Type<'c>,
            location: Location<'c>,
        ) -> Operation<'c> {
            operation::Builder::new(concat!("arith.", $op), location)
                .add_operands(&[lhs, rhs])
                .add_results(&[result])
                .build()
        }
    };
}

impl_arith_binary_op!(addi, "addi");
impl_arith_binary_op!(subi, "subi");
impl_arith_binary_op!(muli, "muli");
impl_arith_binary_op!(divui, "divui");
impl_arith_binary_op!(remui, "remui");
impl_arith_binary_op!(shrsi, "shrsi");
impl_arith_binary_op!(shrui, "shrui");
impl_arith_binary_op!(andi, "andi");
impl_arith_binary_op!(ori, "ori");
impl_arith_binary_op!(xori, "xori");

/// arith.constant
pub fn r#const<'c>(
    context: &'c Context,
    val: &str,
    result: Type<'c>,
    location: Location<'c>,
) -> Operation<'c> {
    operation::Builder::new("arith.constant", location)
        .add_results(&[result])
        .add_attributes(&[NamedAttribute::new_parsed(
            context,
            "value",
            &format!("{val} : {}", result),
        )
        .unwrap()])
        .build()
}