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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::fmt::Write;

use orzir_core::{ArenaPtr, Context, Dialect, Op, OpMetadata, Parse, Successor, Value};
use orzir_macros::{ControlFlow, DataFlow, Op, Parse, Print, RegionInterface, Verify};

use crate::verifiers::{control_flow::*, *};

/// The jump operation.
///
/// This represents an unconditional jump to another block with some arguments.
#[derive(Op, DataFlow, RegionInterface, ControlFlow, Parse, Print, Verify)]
#[mnemonic = "cf.jump"]
#[verifiers(NumResults<0>, VariadicOperands, NumRegions<0>, NumSuccessors<1>, IsTerminator)]
#[format(pattern = "{succ}", kind = "op", num_results = 0)]
pub struct JumoOp {
    #[metadata]
    metadata: OpMetadata,
    /// The successor of this jump operation.
    #[successor(0)]
    succ: Successor,
}

/// The branch operation.
///
/// This represents a conditional branch to two different blocks.
#[derive(Op, DataFlow, RegionInterface, ControlFlow, Parse, Print, Verify)]
#[mnemonic = "cf.branch"]
#[verifiers(NumResults<0>, NumOperands<1>, NumRegions<0>, NumSuccessors<2>, IsTerminator)]
#[format(
    kind = "op",
    pattern = "{cond}, {then_succ}, {else_succ}",
    num_results = 0
)]
pub struct BranchOp {
    #[metadata]
    metadata: OpMetadata,
    /// The condition of the branch.
    #[operand(0)]
    cond: ArenaPtr<Value>,
    /// The successor of the branch if the condition is true.
    #[successor(0)]
    then_succ: Successor,
    /// The successor of the branch if the condition is false.
    #[successor(1)]
    else_succ: Successor,
}

/// Register the control flow dialect.
pub fn register(ctx: &mut Context) {
    let dialect = Dialect::new("cf".into());
    ctx.register_dialect(dialect);

    JumoOp::register(ctx, JumoOp::parse);
    BranchOp::register(ctx, BranchOp::parse);
}

#[cfg(test)]
mod tests {
    use orzir_core::{
        Context,
        OpObj,
        Parse,
        ParseState,
        Print,
        PrintState,
        RegionInterface,
        TokenStream,
    };

    use crate::dialects::std::{
        arith,
        builtin::{self, ModuleOp},
        cf,
        func,
        register_std_dialects,
    };

    #[test]
    fn test_0() {
        let src = r#"
        module {
            func.func @foo : fn() -> (int<32>, float) {
            ^entry(%x : float, %y: int<32>):
                // nothing here
                %0 = arith.iconst true : int<1>
                %1 = arith.iconst false : int<1>
                %2 = arith.iadd %0, %1 : int<1>

                %aaaa = arith.iconst -0x123i32 : int<32>

                %b = arith.iconst 0b101i32 : int<32>
                %c = arith.iconst 0o123i32 : int<32>
                %d = arith.iconst 123i32 : int<32>

                cf.jump ^entry(%x, %y)
            }
        }
        "#;

        let stream = TokenStream::new(src);
        let mut state = ParseState::new(stream);
        let mut ctx = Context::default();

        register_std_dialects(&mut ctx);

        let op = OpObj::parse(&mut ctx, &mut state).unwrap();
        let mut state = PrintState::new("    ");
        op.deref(&ctx.ops).as_ref().verify(&ctx).unwrap();
        op.deref(&ctx.ops).print(&ctx, &mut state).unwrap();
        println!("{}", state.buffer);

        let module_op = op.deref(&ctx.ops).as_a::<ModuleOp>().unwrap();
        assert!(module_op
            .get_region(0)
            .unwrap()
            .deref(&ctx.regions)
            .lookup_symbol(&ctx, "foo")
            .is_some());
    }

    #[test]
    fn test_1() {
        let src = r#"
        module {
            func.func @foo : fn () -> int<32> {
            ^entry:
                %a = arith.iconst 123i32 : int<32>
                %b = arith.iconst 456i32 : int<32>

                %cond = arith.iconst true : int<32>

                cf.branch %cond, ^then(%a), ^else(%b)

            ^then(%x: int<32>):
                cf.jump ^return

            ^else(%y: int<32>):
                cf.jump ^return

            ^return:
                func.return %a
            }
        }
        "#;

        let stream = TokenStream::new(src);
        let mut state = ParseState::new(stream);
        let mut ctx = Context::default();

        builtin::register(&mut ctx);
        func::register(&mut ctx);
        arith::register(&mut ctx);
        cf::register(&mut ctx);

        let op = OpObj::parse(&mut ctx, &mut state).unwrap();
        let mut state = PrintState::new("    ");
        op.deref(&ctx.ops).print(&ctx, &mut state).unwrap();
        println!("{}", state.buffer);

        let module_op = op.deref(&ctx.ops).as_a::<ModuleOp>().unwrap();
        assert!(module_op
            .get_region(0)
            .unwrap()
            .deref(&ctx.regions)
            .lookup_symbol(&ctx, "foo")
            .is_some());
    }
}