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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use std::fmt::Write;

use thiserror::Error;

use super::{layout::OpList, parse::ParseState, symbol::NameAllocDuplicatedErr, value::Value};
use crate::{
    core::parse::{ParseErrorKind, TokenKind},
    delimiter,
    parse_error,
    support::{
        error::{PrintResult, VerifyResult},
        storage::ArenaPtr,
    },
    token_wildcard,
    verify_error,
    Context,
    OpObj,
    Parse,
    ParseResult,
    Print,
    PrintState,
    Region,
    RunVerifiers,
    TyObj,
    Verify,
};

/// The block in the region.
pub struct Block {
    /// Self ptr.
    self_ptr: ArenaPtr<Self>,
    /// Arguments of the block.
    ///
    /// There is no phi node in the OrzIR, instead, the block arguments are used
    /// to deal with the incoming values from the predecessors and keep the IR
    /// in the SSA form.
    args: Vec<ArenaPtr<Value>>,
    /// If this block is an entry block.
    ///
    /// An entry block can be omitted if it has no arguments. The entry block
    /// must be the first block in the layout.
    is_entry: bool,
    /// The parent region.
    parent_region: ArenaPtr<Region>,
    /// The layout of the block.
    ///
    /// The layout is the list of operations in the block, indicating the print
    /// order of the operations.
    layout: OpList,
}

impl RunVerifiers for Block {
    fn run_verifiers(&self, _ctx: &Context) -> VerifyResult<()> { Ok(()) }
}

/// The error when the entry block is not the first block in the layout.
#[derive(Debug, Error)]
#[error("The entry block must be the first block in the layout.")]
pub struct InvalidEntryBlockError;

impl Verify for Block {
    fn verify(&self, ctx: &Context) -> VerifyResult<()> {
        if self.is_entry {
            let parent_region = self.parent_region.deref(&ctx.regions);
            if parent_region.layout().front().unwrap() != self.self_ptr {
                return verify_error!(InvalidEntryBlockError).into();
            }
        }

        for arg in &self.args {
            arg.deref(&ctx.values).verify(ctx)?;
        }
        for op in self.layout().iter() {
            op.deref(&ctx.ops).as_ref().verify(ctx)?;
        }
        Ok(())
    }
}

impl Block {
    /// Create a new block.
    pub fn new(
        ctx: &mut Context,
        is_entry: bool,
        parent_region: ArenaPtr<Region>,
        name: Option<String>,
    ) -> ArenaPtr<Block> {
        let self_ptr = if let Some(name) = name {
            let self_ptr = parent_region
                .deref(&ctx.regions)
                .block_names
                .borrow()
                .get_by_name(&name)
                .unwrap_or_else(|| ctx.blocks.reserve());
            parent_region
                .deref(&ctx.regions)
                .block_names
                .borrow_mut()
                .set(self_ptr, name)
                .unwrap();
            self_ptr
        } else {
            ctx.blocks.reserve()
        };
        let instance = Self {
            self_ptr,
            args: Vec::new(),
            is_entry,
            parent_region,
            layout: OpList::default(),
        };
        ctx.blocks.fill(self_ptr, instance);
        self_ptr
    }

    /// Get the layout of operations in the block.
    pub fn layout(&self) -> &OpList { &self.layout }

    /// Get the mutable layout of operations in the block.
    pub fn layout_mut(&mut self) -> &mut OpList { &mut self.layout }

    /// Get the name of the block.
    ///
    /// This will allocate a new name if the block does not have one.
    pub fn name(&self, ctx: &Context) -> String {
        let region = self.parent_region.deref(&ctx.regions);
        let name = region.block_names.borrow_mut().get(self.self_ptr);
        name
    }

    /// Set the name of the block.
    pub fn set_name(&self, ctx: &Context, name: String) -> Result<(), NameAllocDuplicatedErr> {
        let region = self.parent_region.deref(&ctx.regions);
        region.block_names.borrow_mut().set(self.self_ptr, name)
    }

    /// Set the arguments of the block by the given index.
    pub fn set_arg(&mut self, index: usize, arg: ArenaPtr<Value>) -> Option<ArenaPtr<Value>> {
        if index > self.args.len() {
            panic!("index out of range when setting block argument.");
        }
        if index == self.args.len() {
            self.args.push(arg);
            return None;
        }

        let old = std::mem::replace(&mut self.args[index], arg);
        Some(old)
    }

    /// Get the number of arguments of the block.
    pub fn num_args(&self) -> usize { self.args.len() }

    /// Get the arguments of the block.
    pub fn args(&self) -> &[ArenaPtr<Value>] { &self.args }

    /// Test if the block is an entry block.
    pub fn is_entry(&self) -> bool { self.is_entry }

    /// Reserve a unknown block with a name, if the name is already used, return
    /// the block.
    pub(crate) fn reserve_with_name(
        ctx: &mut Context,
        name: String,
        region: ArenaPtr<Region>,
    ) -> ArenaPtr<Block> {
        let region = region.deref(&ctx.regions);
        let self_ptr = region
            .block_names
            .borrow()
            .get_by_name(&name)
            .unwrap_or_else(|| ctx.blocks.reserve());
        region.block_names.borrow_mut().set(self_ptr, name).unwrap();
        self_ptr
    }

    /// Get the parent region of the block.
    pub fn parent_region(&self) -> ArenaPtr<Region> { self.parent_region }
}

impl Parse for Block {
    type Item = ArenaPtr<Block>;

    /// Parsing a block.
    ///
    /// A block starts with a block label `^...`, and an optional list of block
    /// arguments. A block ends with the `}` token, which indicates the end of a
    /// region, or another block label.
    fn parse(ctx: &mut Context, state: &mut ParseState) -> ParseResult<Self::Item> {
        let token = state.stream.peek()?;
        let block = match &token.kind {
            TokenKind::BlockLabel(_) => {
                let token = state.stream.consume()?;
                if let TokenKind::BlockLabel(label) = token.kind {
                    Block::new(ctx, false, state.curr_region(), Some(label))
                } else {
                    unreachable!()
                }
            }
            _ => Block::new(ctx, true, state.curr_region(), None),
        };

        // parse the block arguments.
        let is_entry = block.deref(&ctx.blocks).is_entry();
        if !is_entry {
            let token = state.stream.consume()?;
            match token.kind {
                TokenKind::Char('(') => {
                    let mut cnt = 0;
                    // parse the arguments.
                    loop {
                        let token = state.stream.peek()?;
                        match &token.kind {
                            TokenKind::Char(')') => {
                                state.stream.consume()?;
                                break;
                            }
                            TokenKind::ValueName(name) => {
                                let name = name.clone();
                                let _arg = Value::parse(ctx, state)?;

                                state.stream.expect(delimiter!(':'))?;
                                let ty = TyObj::parse(ctx, state)?;

                                let arg =
                                    Value::new_block_argument(ctx, ty, block, cnt, Some(name));
                                block.deref_mut(&mut ctx.blocks).set_arg(cnt, arg);

                                cnt += 1;

                                if state.stream.consume_if(TokenKind::Char(','))?.is_none() {
                                    // end of the arguments.
                                    state.stream.expect(delimiter!(')'))?;
                                    break;
                                }
                            }
                            _ => {
                                return parse_error!(
                                    token.span,
                                    ParseErrorKind::InvalidToken(
                                        vec![delimiter!(')'), token_wildcard!("%...")].into(),
                                        token.kind.clone()
                                    )
                                )
                                .into();
                            }
                        }
                    }
                    state.stream.expect(delimiter!(':'))?;
                }
                TokenKind::Char(':') => {
                    // just exit.
                }
                _ => {
                    return parse_error!(
                        token.span,
                        ParseErrorKind::InvalidToken(
                            vec![delimiter!('('), delimiter!(':')].into(),
                            token.kind
                        )
                    )
                    .into();
                }
            }
        }

        state.enter_op_from(block);

        // parse the operations.
        loop {
            let token = state.stream.peek()?;
            match token.kind {
                TokenKind::ValueName(_) | TokenKind::Tokenized(_) => {
                    // parse an operation
                    let op = OpObj::parse(ctx, state)?;
                    block
                        .deref_mut(&mut ctx.blocks)
                        .layout_mut()
                        .append(op)
                        .expect("should be able to append an operation when parsing.")
                }
                TokenKind::BlockLabel(_) | TokenKind::Char('}') => {
                    // end of the block
                    break;
                }
                _ => {
                    return parse_error!(
                        token.span,
                        ParseErrorKind::InvalidToken(
                            vec![
                                delimiter!('}'),
                                token_wildcard!("%..."),
                                token_wildcard!("^..."),
                                token_wildcard!("...")
                            ]
                            .into(),
                            token.kind.clone()
                        )
                    )
                    .into();
                }
            }
        }

        state.exit_op();

        Ok(block)
    }
}

impl Print for Block {
    fn print(&self, ctx: &Context, state: &mut PrintState) -> PrintResult<()> {
        if !self.is_entry() {
            state.write_indent()?;
            write!(state.buffer, "^{}", self.name(ctx))?;
            if self.args.is_empty() {
                write!(state.buffer, ":")?;
            } else {
                write!(state.buffer, "(")?;
                for (i, arg) in self.args.iter().enumerate() {
                    let arg = arg.deref(&ctx.values);
                    arg.print(ctx, state)?;
                    write!(state.buffer, ": ")?;
                    let ty = arg.ty(ctx);
                    ty.deref(&ctx.tys).print(ctx, state)?;
                    if i != self.args.len() - 1 {
                        write!(state.buffer, ", ")?;
                    }
                }
                write!(state.buffer, "):")?;
            }
            writeln!(state.buffer)?;
        }

        state.indent();
        for op in self.layout().iter() {
            state.write_indent()?;
            op.deref(&ctx.ops).print(ctx, state)?;
            writeln!(state.buffer)?;
        }
        state.dedent();

        Ok(())
    }
}