Skip to main content

miden_assembly_syntax/ast/instruction/
mod.rs

1pub mod advice;
2mod debug_var;
3mod inline_call;
4mod print;
5
6use alloc::vec::Vec;
7use core::ops::Range;
8
9pub use self::{
10    advice::SystemEventNode,
11    debug_var::{
12        DebugFrameBase, DebugLocationExpression, DebugLocationExpressionError,
13        DebugLocationExpressionOp, DebugVarInfo, DebugVarLocation,
14    },
15    inline_call::DebugInlineCallInfo,
16};
17use crate::{
18    Felt,
19    ast::{InvocationTarget, immediate::*},
20    parser::{PushValue, WordValue},
21};
22
23// INSTRUCTION
24// ================================================================================================
25
26/// Represents the set of primitive instructions in Miden Assembly syntax.
27///
28/// NOTE: For control flow instructions, see [crate::ast::Op].
29#[derive(Clone, PartialEq, Eq, Debug)]
30pub enum Instruction {
31    Nop,
32    Assert,
33    AssertWithError(ErrorMsg),
34    AssertEq,
35    AssertEqWithError(ErrorMsg),
36    AssertEqw,
37    AssertEqwWithError(ErrorMsg),
38    Assertz,
39    AssertzWithError(ErrorMsg),
40    Add,
41    AddImm(ImmFelt),
42    Sub,
43    SubImm(ImmFelt),
44    Mul,
45    MulImm(ImmFelt),
46    Div,
47    DivImm(ImmFelt),
48    Neg,
49    ILog2,
50    Inv,
51    Incr,
52    Pow2,
53    Exp,
54    ExpImm(ImmFelt),
55    ExpBitLength(u8),
56    Not,
57    And,
58    Or,
59    Xor,
60    Eq,
61    EqImm(ImmFelt),
62    Neq,
63    NeqImm(ImmFelt),
64    Eqw,
65    Lt,
66    LtImm(ImmFelt),
67    Lte,
68    LteImm(ImmFelt),
69    Gt,
70    GtImm(ImmFelt),
71    Gte,
72    GteImm(ImmFelt),
73    IsOdd,
74
75    // ----- ext2 operations ---------------------------------------------------------------------
76    Ext2Add,
77    Ext2Sub,
78    Ext2Mul,
79    Ext2Div,
80    Ext2Neg,
81    Ext2Inv,
82
83    // ----- u32 manipulation --------------------------------------------------------------------
84    U32Test,
85    U32TestW,
86    U32Assert,
87    U32AssertWithError(ErrorMsg),
88    U32Assert2,
89    U32Assert2WithError(ErrorMsg),
90    U32AssertW,
91    U32AssertWWithError(ErrorMsg),
92    U32Split,
93    U32Cast,
94    U32WrappingAdd,
95    U32WrappingAddImm(ImmU32),
96    U32OverflowingAdd,
97    U32OverflowingAddImm(ImmU32),
98    U32WideningAdd,
99    U32WideningAddImm(ImmU32),
100    U32OverflowingAdd3,
101    U32WideningAdd3,
102    U32WrappingAdd3,
103    U32WrappingSub,
104    U32WrappingSubImm(ImmU32),
105    U32OverflowingSub,
106    U32OverflowingSubImm(ImmU32),
107    U32WrappingMul,
108    U32WrappingMulImm(ImmU32),
109    U32WideningMul,
110    U32WideningMulImm(ImmU32),
111    U32WideningMadd,
112    U32WrappingMadd,
113    U32Div,
114    U32DivImm(ImmU32),
115    U32Mod,
116    U32ModImm(ImmU32),
117    U32DivMod,
118    U32DivModImm(ImmU32),
119    U32And,
120    U32Or,
121    U32Xor,
122    U32Not,
123    U32Shr,
124    U32ShrImm(ImmU8),
125    U32Shl,
126    U32ShlImm(ImmU8),
127    U32Rotr,
128    U32RotrImm(ImmU8),
129    U32Rotl,
130    U32RotlImm(ImmU8),
131    U32Popcnt,
132    U32Ctz,
133    U32Clz,
134    U32Clo,
135    U32Cto,
136    U32Lt,
137    U32Lte,
138    U32Gt,
139    U32Gte,
140    U32Min,
141    U32Max,
142
143    // ----- stack manipulation ------------------------------------------------------------------
144    Drop,
145    DropW,
146    PadW,
147    Dup0,
148    Dup1,
149    Dup2,
150    Dup3,
151    Dup4,
152    Dup5,
153    Dup6,
154    Dup7,
155    Dup8,
156    Dup9,
157    Dup10,
158    Dup11,
159    Dup12,
160    Dup13,
161    Dup14,
162    Dup15,
163    DupW0,
164    DupW1,
165    DupW2,
166    DupW3,
167    Swap1,
168    Swap2,
169    Swap3,
170    Swap4,
171    Swap5,
172    Swap6,
173    Swap7,
174    Swap8,
175    Swap9,
176    Swap10,
177    Swap11,
178    Swap12,
179    Swap13,
180    Swap14,
181    Swap15,
182    SwapW1,
183    SwapW2,
184    SwapW3,
185    SwapDw,
186    MovUp2,
187    MovUp3,
188    MovUp4,
189    MovUp5,
190    MovUp6,
191    MovUp7,
192    MovUp8,
193    MovUp9,
194    MovUp10,
195    MovUp11,
196    MovUp12,
197    MovUp13,
198    MovUp14,
199    MovUp15,
200    MovUpW2,
201    MovUpW3,
202    MovDn2,
203    MovDn3,
204    MovDn4,
205    MovDn5,
206    MovDn6,
207    MovDn7,
208    MovDn8,
209    MovDn9,
210    MovDn10,
211    MovDn11,
212    MovDn12,
213    MovDn13,
214    MovDn14,
215    MovDn15,
216    MovDnW2,
217    MovDnW3,
218    Reversew,
219    Reversedw,
220    CSwap,
221    CSwapW,
222    CDrop,
223    CDropW,
224
225    // ----- input / output operations -----------------------------------------------------------
226    Push(Immediate<PushValue>),
227    PushSlice(Immediate<WordValue>, Range<usize>),
228    PushFeltList(Vec<Felt>),
229    Locaddr(ImmU16),
230    Sdepth,
231    Caller,
232    Clk,
233
234    MemLoad,
235    MemLoadImm(ImmU32),
236    MemLoadWBe,
237    MemLoadWBeImm(ImmU32),
238    MemLoadWLe,
239    MemLoadWLeImm(ImmU32),
240    LocLoad(ImmU16),
241    LocLoadWBe(ImmU16),
242    LocLoadWLe(ImmU16),
243
244    MemStore,
245    MemStoreImm(ImmU32),
246    MemStoreWBe,
247    MemStoreWBeImm(ImmU32),
248    MemStoreWLe,
249    MemStoreWLeImm(ImmU32),
250    LocStore(ImmU16),
251    LocStoreWBe(ImmU16),
252    LocStoreWLe(ImmU16),
253
254    MemStream,
255    AdvPipe,
256
257    AdvPush,
258    AdvPushW,
259    AdvLoadW,
260
261    SysEvent(SystemEventNode),
262
263    // ----- cryptographic operations ------------------------------------------------------------
264    Hash,
265    HMerge,
266    HPerm,
267    MTreeGet,
268    MTreeSet,
269    MTreeMerge,
270    MTreeVerify,
271    MTreeVerifyWithError(ErrorMsg),
272    CryptoStream,
273
274    // ----- STARK proof verification ------------------------------------------------------------
275    FriExt2Fold4,
276    HornerBase,
277    HornerExt,
278    EvalCircuit,
279    LogDeferred,
280
281    // ----- exec / call -------------------------------------------------------------------------
282    Exec(InvocationTarget),
283    Call(InvocationTarget),
284    SysCall(InvocationTarget),
285    DynExec,
286    DynCall,
287    ProcRef(InvocationTarget),
288
289    // ----- debug decorators --------------------------------------------------------------------
290    /// Records a source variable location at this point in the generated operation stream.
291    DebugVar(DebugVarInfo),
292    /// Appends one source function to the inline call chain active for subsequent operations.
293    ///
294    /// Frames are emitted from innermost to outermost. This compiler-internal marker has no VM
295    /// execution semantics; the assembler stores the active chain on each generated source-node
296    /// occurrence until [`DebugInlineCallClear`](Self::DebugInlineCallClear) is encountered.
297    DebugInlineCall(DebugInlineCallInfo),
298    /// Clears the inline call chain for subsequent generated operations and control nodes.
299    ///
300    /// This marks the point at which source attribution returns to the containing non-inlined
301    /// function. It is compiler-internal and does not emit a VM operation.
302    DebugInlineCallClear,
303
304    // ----- event decorators --------------------------------------------------------------------
305    Emit,
306    EmitImm(EventImmediate),
307
308    // ----- traces (read-only events) -----------------------------------------------------------
309    Trace,
310    TraceImm(EventImmediate),
311}
312
313impl Instruction {
314    /// Returns true if this instruction has a textual representation in Miden Assembly.
315    ///
316    /// Some instructions (like [`DebugVar`](Self::DebugVar)) are compiler-internal and have
317    /// no surface syntax. They should be skipped during pretty-printing.
318    pub const fn has_textual_representation(&self) -> bool {
319        !matches!(self, Self::DebugVar(_) | Self::DebugInlineCall(_) | Self::DebugInlineCallClear)
320    }
321}
322
323impl core::fmt::Display for Instruction {
324    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
325        use crate::prettier::PrettyPrint;
326
327        self.pretty_print(f)
328    }
329}