rune_ssa/
term.rs

1use crate::{Assign, BlockId};
2use std::fmt;
3
4/// An operation that terminates a block.
5pub enum Term {
6    /// Default termination. The procedure will panic.
7    Panic,
8    /// Conditionally jump to the given block if the given condition is true.
9    JumpIf {
10        /// The condition of the jump.
11        condition: Assign,
12        /// Where to jump if the condition is true.
13        then_block: BlockId,
14        /// Where to jump if the condition is false.
15        else_block: BlockId,
16    },
17    /// Unconditionally jump to the given block.
18    Jump {
19        /// Block to jump to.
20        block: BlockId,
21    },
22    /// Return from the current procedure with the given value.
23    Return {
24        /// The value to return.
25        var: Assign,
26    },
27}
28
29impl Term {
30    /// Dump the block terminator diagnostically.
31    pub(crate) fn dump(&self) -> TermDump<'_> {
32        TermDump { term: self }
33    }
34}
35
36pub(crate) struct TermDump<'a> {
37    term: &'a Term,
38}
39
40impl fmt::Display for TermDump<'_> {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self.term {
43            Term::Panic => {
44                write!(f, "panic")?;
45            }
46            Term::JumpIf {
47                condition,
48                then_block,
49                else_block,
50            } => {
51                write!(f, "jump-if {}, {}, {}", condition, then_block, else_block)?;
52            }
53            Term::Jump { block } => {
54                write!(f, "jump {}", block)?;
55            }
56            Term::Return { var } => {
57                write!(f, "return {}", var)?;
58            }
59        }
60
61        Ok(())
62    }
63}