Skip to main content

miden_assembly_syntax/ast/
op.rs

1use core::fmt;
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use super::{Block, Immediate, Instruction};
6
7/// Represents the Miden Assembly instruction set syntax
8///
9/// This is separate from [Instruction] in order to distinguish control flow instructions and
10/// instructions with block regions from the rest.
11#[derive(Clone)]
12#[repr(u8)]
13pub enum Op {
14    /// Represents a conditional branch
15    ///
16    /// Can be either `if`..`end`, or `if`..`else`..`end`.
17    If {
18        span: SourceSpan,
19        /// This block is always present and non-empty
20        then_blk: Block,
21        /// This block will be empty if no `else` branch was present
22        else_blk: Block,
23    } = 0,
24    /// Represents a condition-controlled loop
25    While { span: SourceSpan, body: Block } = 1,
26    /// Represents a counter-controlled loop.
27    ///
28    /// NOTE: The iteration count must be known at compile-time, so this is _not_ used for general
29    /// `for`-style loops where the iteration count is dynamic.
30    /// Also, it may be either a literal value or constant name (e.g. `add.1` or `add.CONST`)
31    Repeat {
32        span: SourceSpan,
33        count: Immediate<u32>,
34        body: Block,
35    } = 2,
36    /// A primitive operation, e.g. `add`
37    Inst(Span<Instruction>) = 3,
38}
39
40impl crate::prettier::PrettyPrint for Op {
41    fn render(&self) -> crate::prettier::Document {
42        use crate::prettier::*;
43
44        match self {
45            Self::If { then_blk, else_blk, .. } => {
46                text("if.true") + then_blk.render() + text("else") + else_blk.render() + text("end")
47            },
48            Self::While { body, .. } => text("while.true") + body.render() + text("end"),
49            Self::Repeat { count, body, .. } => {
50                display(format!("repeat.{count}")) + body.render() + text("end")
51            },
52            Self::Inst(inst) => inst.render(),
53        }
54    }
55}
56
57impl fmt::Debug for Op {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            Self::If { then_blk, else_blk, .. } => {
61                f.debug_struct("If").field("then", then_blk).field("else", else_blk).finish()
62            },
63            Self::While { body, .. } => f.debug_tuple("While").field(body).finish(),
64            Self::Repeat { count, body, .. } => {
65                f.debug_struct("Repeat").field("count", count).field("body", body).finish()
66            },
67            Self::Inst(inst) => fmt::Debug::fmt(&**inst, f),
68        }
69    }
70}
71
72impl Eq for Op {}
73
74impl PartialEq for Op {
75    fn eq(&self, other: &Self) -> bool {
76        match (self, other) {
77            (
78                Self::If { then_blk: lt, else_blk: le, .. },
79                Self::If { then_blk: rt, else_blk: re, .. },
80            ) => lt == rt && le == re,
81            (Self::While { body: lbody, .. }, Self::While { body: rbody, .. }) => lbody == rbody,
82            (
83                Self::Repeat { count: lcount, body: lbody, .. },
84                Self::Repeat { count: rcount, body: rbody, .. },
85            ) => lcount == rcount && lbody == rbody,
86            (Self::Inst(l), Self::Inst(r)) => l == r,
87            _ => false,
88        }
89    }
90}
91
92impl Spanned for Op {
93    fn span(&self) -> SourceSpan {
94        match self {
95            Self::If { span, .. } | Self::While { span, .. } | Self::Repeat { span, .. } => *span,
96            Self::Inst(spanned) => spanned.span(),
97        }
98    }
99}