midenc-codegen-masm 0.10.0

Miden Assembly backend for the Miden compiler
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
use miden_assembly_syntax::parser::WordValue;
use midenc_dialect_hir::assertions;
use midenc_hir::{
    ArrayType, Felt, Immediate, SourceSpan, Type,
    dialects::builtin::attributes::{ArgumentExtension, Signature},
};

use super::{OpEmitter, int64, masm};
use crate::Event;

impl OpEmitter<'_> {
    /// Push the caller procedure hash as a word.
    pub fn caller(&mut self, span: SourceSpan) {
        self.emit(masm::Instruction::Caller, span);
        self.push(Type::from(ArrayType::new(Type::Felt, 4)));
    }

    /// Push the current VM clock cycle.
    pub fn clk(&mut self, span: SourceSpan) {
        self.emit(masm::Instruction::Clk, span);
        self.push(Type::Felt);
    }

    /// Format a diagnostic message for a HIR assertion code when one is available.
    fn assertion_message(
        code: Option<u32>,
        message: Option<&str>,
        default: impl Into<String>,
    ) -> String {
        if let Some(message) = message.filter(|message| !message.is_empty()) {
            return message.to_owned();
        }

        let default = default.into();
        match code.filter(|code| *code != 0) {
            Some(assertions::ASSERT_FAILED_ALIGNMENT) => {
                "pointer address does not meet minimum alignment for the type".into()
            }
            Some(code) => format!("{default} (assertion code 0x{code:08x})"),
            None => default,
        }
    }

    /// Assert that an integer value on the stack has the value 1
    ///
    /// This operation consumes the input value.
    pub fn assert(&mut self, code: Option<u32>, message: Option<&str>, span: SourceSpan) {
        let arg = self.stack.pop().expect("operand stack is empty");
        let ty = arg.ty().clone();
        let message =
            Self::assertion_message(code, message, format!("expected {ty} value to equal 1"));
        match ty {
            Type::Felt
            | Type::U32
            | Type::I32
            | Type::U16
            | Type::I16
            | Type::U8
            | Type::I8
            | Type::I1 => {
                self.emit(Self::assert_with_message_inst(message, span), span);
            }
            Type::I128 | Type::U128 => {
                self.emit_all(
                    [
                        masm::Instruction::Push(masm::Immediate::Value(masm::Span::new(
                            span,
                            WordValue([Felt::ZERO, Felt::ZERO, Felt::ZERO, Felt::ONE]).into(),
                        ))),
                        Self::assert_eqw_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            Type::U64 | Type::I64 => {
                self.emit_all(
                    [
                        Self::assertz_with_message_inst(message.clone(), span),
                        Self::assert_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            ty if !ty.is_integer() => {
                panic!("invalid argument to assert: expected integer, got {ty}")
            }
            ty => unimplemented!("support for assert on {ty} is not implemented"),
        }
    }

    /// Assert that an integer value on the stack has the value 0
    ///
    /// This operation consumes the input value.
    pub fn assertz(&mut self, code: Option<u32>, message: Option<&str>, span: SourceSpan) {
        let arg = self.stack.pop().expect("operand stack is empty");
        let ty = arg.ty().clone();
        let message =
            Self::assertion_message(code, message, format!("expected {ty} value to equal 0"));
        match ty {
            Type::Felt
            | Type::U32
            | Type::I32
            | Type::U16
            | Type::I16
            | Type::U8
            | Type::I8
            | Type::I1 => {
                self.emit(Self::assertz_with_message_inst(message, span), span);
            }
            Type::U64 | Type::I64 => {
                self.emit_all(
                    [
                        Self::assertz_with_message_inst(message.clone(), span),
                        Self::assertz_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            Type::U128 | Type::I128 => {
                self.emit_all(
                    [
                        masm::Instruction::Push(masm::Immediate::Value(masm::Span::new(
                            span,
                            WordValue([Felt::ZERO; 4]).into(),
                        ))),
                        Self::assert_eqw_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            ty if !ty.is_integer() => {
                panic!("invalid argument to assertz: expected integer, got {ty}")
            }
            ty => unimplemented!("support for assertz on {ty} is not implemented"),
        }
    }

    /// Assert that the top two integer values on the stack have the same value
    ///
    /// This operation consumes the input values.
    pub fn assert_eq(&mut self, code: Option<u32>, message: Option<&str>, span: SourceSpan) {
        let rhs = self.pop().expect("operand stack is empty");
        let lhs = self.pop().expect("operand stack is empty");
        let ty = lhs.ty().clone();
        assert_eq!(ty, rhs.ty(), "expected assert_eq operands to have the same type");
        let message =
            Self::assertion_message(code, message, format!("expected {ty} values to be equal"));
        match ty {
            Type::Felt
            | Type::U32
            | Type::I32
            | Type::U16
            | Type::I16
            | Type::U8
            | Type::I8
            | Type::I1 => {
                self.emit(Self::assert_eq_with_message_inst(message, span), span);
            }
            Type::U128 | Type::I128 => {
                self.emit(Self::assert_eqw_with_message_inst(message, span), span)
            }
            Type::U64 | Type::I64 => {
                self.emit_all(
                    [
                        // compare the hi bits
                        masm::Instruction::MovUp2,
                        Self::assert_eq_with_message_inst(message.clone(), span),
                        // compare the low bits
                        Self::assert_eq_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            ty if !ty.is_integer() => {
                panic!("invalid argument to assert_eq: expected integer, got {ty}")
            }
            ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
        }
    }

    /// Emit code to assert that an integer value on the stack has the same value
    /// as the provided immediate.
    ///
    /// This operation consumes the input value.
    #[allow(unused)]
    pub fn assert_eq_imm(&mut self, imm: Immediate, span: SourceSpan) {
        let lhs = self.pop().expect("operand stack is empty");
        let ty = lhs.ty().clone();
        let message = format!("expected {ty} value to equal {imm}");
        assert_eq!(ty, imm.ty(), "expected assert_eq_imm operands to have the same type");
        match ty {
            Type::Felt
            | Type::U32
            | Type::I32
            | Type::U16
            | Type::I16
            | Type::U8
            | Type::I8
            | Type::I1 => {
                self.emit_all(
                    [
                        masm::Instruction::EqImm(imm.as_felt().unwrap().into()),
                        Self::assert_with_message_inst(message, span),
                    ],
                    span,
                );
            }
            Type::I128 | Type::U128 => {
                self.push_immediate(imm, span);
                self.emit(Self::assert_eqw_with_message_inst(message, span), span)
            }
            Type::I64 | Type::U64 => {
                let imm = match imm {
                    Immediate::I64(i) => i as u64,
                    Immediate::U64(i) => i,
                    _ => unreachable!(),
                };
                let (hi, lo) = int64::to_raw_parts(imm);
                self.emit_all(
                    [
                        masm::Instruction::EqImm(Felt::new_unchecked(hi as u64).into()),
                        Self::assert_with_message_inst(message.clone(), span),
                        masm::Instruction::EqImm(Felt::new_unchecked(lo as u64).into()),
                        Self::assert_with_message_inst(message, span),
                    ],
                    span,
                )
            }
            ty if !ty.is_integer() => {
                panic!("invalid argument to assert_eq: expected integer, got {ty}")
            }
            ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
        }
    }

    /// Emit code to select between two values of the same type, based on a boolean condition.
    ///
    /// The semantics of this instruction are basically the same as Miden's `cdrop` instruction,
    /// but with support for selecting between any of the representable integer/pointer types as
    /// values. Given three values on the operand stack (in order of appearance), `c`, `b`, and
    /// `a`:
    ///
    /// * Pop `c` from the stack. This value must be an i1/boolean, or execution will trap.
    /// * Pop `b` and `a` from the stack, and push back `b` if `c` is true, or `a` if `c` is false.
    ///
    /// This operation will assert that the selected value is a valid value for the given type.
    pub fn select(&mut self, span: SourceSpan) {
        let c = self.stack.pop().expect("operand stack is empty");
        let b = self.stack.pop().expect("operand stack is empty");
        let a = self.stack.pop().expect("operand stack is empty");
        assert_eq!(c.ty(), Type::I1, "expected selector operand to be an i1");
        let ty = a.ty();
        assert_eq!(ty, b.ty(), "expected selections to be of the same type");
        match &ty {
            Type::Felt
            | Type::U32
            | Type::I32
            | Type::U16
            | Type::I16
            | Type::U8
            | Type::I8
            | Type::I1 => self.emit(masm::Instruction::CDrop, span),
            Type::I128 | Type::U128 => self.emit(masm::Instruction::CDropW, span),
            Type::I64 | Type::U64 => {
                // Perform two conditional drops, one for each 32-bit limb
                // corresponding to the value which is being selected
                self.emit_all(
                    [
                        // stack starts as [c, b_hi, b_lo, a_hi, a_lo]
                        masm::Instruction::Dup0, // [c, c, b_hi, b_lo, a_hi, a_lo]
                        masm::Instruction::MovDn5, // [c, b_hi, b_lo, a_hi, a_lo, c]
                        masm::Instruction::MovUp3, // [a_hi, c, b_hi, b_lo, a_lo, c]
                        masm::Instruction::MovUp2, // [b_hi, a_hi, c, b_lo, a_lo, c]
                        masm::Instruction::MovUp5, // [c, b_hi, a_hi, c, b_lo, a_lo]
                        masm::Instruction::CDrop, // [d_hi, c, b_lo, a_lo]
                        masm::Instruction::MovDn3, // [c, b_lo, a_lo, d_hi]
                        masm::Instruction::CDrop, // [d_lo, d_hi]
                        masm::Instruction::Swap1, // [d_hi, d_lo]
                    ],
                    span,
                );
            }
            ty if !ty.is_integer() => {
                panic!("invalid argument to assert_eq: expected integer, got {ty}")
            }
            ty => unimplemented!("support for assert_eq on {ty} is not implemented"),
        }
        self.push(ty);
    }

    /// Emit a `println` trace that can be handled by the debug executor.
    pub fn println(&mut self, span: SourceSpan) {
        // Don't `pop` operands as the debug executor reads them from the stack to handle printing.
        let ptr = &self.stack[0];
        let len = &self.stack[1];

        assert_eq!(
            ptr.ty(),
            Type::from(midenc_hir::PointerType::new(Type::U8)),
            "expected println pointer operand to be a ptr<u8>"
        );
        assert_eq!(len.ty(), Type::U32, "expected println length operand to be a u32");

        self.emit(masm::Instruction::EmitImm(Event::PrintLn.as_event_id().as_felt().into()), span);

        // Clean up the stack after the debug executor handled printing.
        self.dropn(2, span);
    }

    /// Execute the given procedure.
    ///
    /// A function called using this operation is invoked in the same memory context as the caller.
    pub fn exec(
        &mut self,
        callee: masm::InvocationTarget,
        signature: &Signature,
        span: SourceSpan,
    ) {
        self.process_call_signature(&callee, signature, span);

        self.emit(
            masm::Instruction::EmitImm(Event::FrameStart.as_event_id().as_felt().into()),
            span,
        );
        self.emit(masm::Instruction::Exec(callee), span);
        self.emit(masm::Instruction::EmitImm(Event::FrameEnd.as_event_id().as_felt().into()), span);
    }

    /// Execute the procedure whose MAST root is stored in slot `index` (stack top) of a function
    /// table with `num_slots` slots based at `base_elem_addr` (a word-aligned element address).
    ///
    /// Traps with an assertion failure if `index >= num_slots`, or if the slot's signature tag
    /// differs from `type_tag` — which also covers null slots, whose tag is the reserved 0. The
    /// callee is invoked in the same memory context as the caller (`dynexec`).
    ///
    /// Expects `[index, args...]` on the operand stack, with the index on top. The index is
    /// rewritten in place to the slot's element address, which `dynexec` pops before
    /// transferring control, so the callee observes `[args...]` in normal argument order.
    pub fn exec_indirect(
        &mut self,
        num_slots: u32,
        base_elem_addr: u32,
        type_tag: u32,
        signature: &Signature,
        span: SourceSpan,
    ) {
        // Consume the index operand; all further effects on it are transient
        let index = self.stack.pop().expect("operand stack is empty");
        assert_eq!(index.ty(), Type::U32, "expected u32 table index for exec_indirect");

        // Bounds check: [index, ..] -> [index < num_slots, index, ..] -> [index, ..]
        self.emit(masm::Instruction::Dup0, span);
        self.emit_push(num_slots, span);
        self.emit(masm::Instruction::U32Lt, span);
        self.emit(
            Self::assert_with_message_inst(
                "indirect call: function table index out of bounds",
                span,
            ),
            span,
        );

        // Rewrite the index to the slot's element address: base_elem_addr + index * slot size.
        // The felt arithmetic cannot overflow: index < num_slots, and the linker guarantees
        // that the whole table fits in the 32-bit address space.
        self.emit(
            masm::Instruction::MulImm(
                Felt::new_unchecked(crate::linker::FunctionTableLayout::SLOT_SIZE_ELEMENTS as u64)
                    .into(),
            ),
            span,
        );
        self.emit(
            masm::Instruction::AddImm(Felt::new_unchecked(base_elem_addr as u64).into()),
            span,
        );

        // Signature check: the tag stored next to the slot's digest must equal the tag the call
        // site expects. A null slot keeps the zero tag that memory is initialized with, so it
        // can never match and traps here too.
        // [slot_addr, ..] -> [tag_addr, slot_addr, ..] -> [tag, slot_addr, ..] -> [slot_addr, ..]
        self.emit(masm::Instruction::Dup0, span);
        self.emit(
            masm::Instruction::AddImm(
                Felt::new_unchecked(
                    crate::linker::FunctionTableLayout::TYPE_TAG_OFFSET_ELEMENTS as u64,
                )
                .into(),
            ),
            span,
        );
        self.emit(masm::Instruction::MemLoad, span);
        self.emit_push(type_tag, span);
        self.emit(
            Self::assert_eq_with_message_inst(
                "indirect call: callee signature mismatch or null function reference",
                span,
            ),
            span,
        );

        // Consume the arguments and produce the results on the emulated stack. Signatures for
        // indirect calls never carry argument-extension attributes, so argument types must match
        // the parameter types exactly. NOTE: this deliberately does not reuse
        // `process_call_signature`: its zext/sext paths emit instructions that operate on the
        // physical stack top, which at this point holds the transient slot address.
        for (i, param) in signature.params.iter().enumerate() {
            assert!(
                matches!(param.extension(), ArgumentExtension::None),
                "invalid exec_indirect: argument extension is not supported for parameter at \
                 index {i}"
            );
            let arg = self.stack.pop().expect("operand stack is empty");
            assert_eq!(
                arg.ty(),
                param.ty,
                "invalid exec_indirect: invalid argument type for parameter at index {i}"
            );
        }
        for result in signature.results.iter().rev() {
            self.push(result.ty.clone());
        }

        // `dynexec` pops the element address and reads the callee MAST root word at it
        self.emit(
            masm::Instruction::EmitImm(Event::FrameStart.as_event_id().as_felt().into()),
            span,
        );
        self.emit(masm::Instruction::DynExec, span);
        self.emit(masm::Instruction::EmitImm(Event::FrameEnd.as_event_id().as_felt().into()), span);
    }

    /// Push the MAST root digest of `callee` onto the operand stack as one word.
    ///
    /// This emits a `procref` instruction; the assembler computes the digest at assembly time
    /// and pushes it with `root[0]` on top.
    pub fn procedure_root(&mut self, callee: masm::InvocationTarget, span: SourceSpan) {
        for _ in 0..midenc_dialect_hir::ProcedureRoot::DIGEST_FELTS {
            self.push(Type::Felt);
        }
        self.emit(masm::Instruction::ProcRef(callee), span);
    }

    /// Execute the given procedure in a new context.
    ///
    /// A function called using this operation is invoked in a new memory context.
    pub fn call(
        &mut self,
        callee: masm::InvocationTarget,
        signature: &Signature,
        span: SourceSpan,
    ) {
        self.process_call_signature(&callee, signature, span);

        self.emit(
            masm::Instruction::EmitImm(Event::FrameStart.as_event_id().as_felt().into()),
            span,
        );
        self.emit(masm::Instruction::Call(callee), span);
        self.emit(masm::Instruction::EmitImm(Event::FrameEnd.as_event_id().as_felt().into()), span);
    }

    /// Execute the given kernel procedure as a syscall.
    pub fn syscall(
        &mut self,
        callee: masm::InvocationTarget,
        signature: &Signature,
        span: SourceSpan,
    ) {
        self.process_call_signature(&callee, signature, span);

        self.emit(
            masm::Instruction::EmitImm(Event::FrameStart.as_event_id().as_felt().into()),
            span,
        );
        self.emit(masm::Instruction::SysCall(callee), span);
        self.emit(masm::Instruction::EmitImm(Event::FrameEnd.as_event_id().as_felt().into()), span);
    }

    fn process_call_signature(
        &mut self,
        callee: &masm::InvocationTarget,
        signature: &Signature,
        span: SourceSpan,
    ) {
        for i in 0..signature.arity() {
            let param = &signature.params[i];
            let arg = self.stack.pop().expect("operand stack is empty");
            let ty = arg.ty();
            // Validate the purpose matches
            if param.is_sret_param() {
                assert_eq!(
                    i, 0,
                    "invalid function signature: sret parameters must be the first parameter, and \
                     only one sret parameter is allowed"
                );
                assert_eq!(
                    signature.results.len(),
                    0,
                    "invalid function signature: a function with sret parameters cannot also have \
                     results"
                );
                assert!(
                    ty.is_pointer(),
                    "invalid exec to {callee}: invalid argument for sret parameter, expected {}, \
                     got {ty}",
                    param.ty
                );
            }
            // Validate that the argument type is valid for the parameter ABI
            match param.extension() {
                // Types must match exactly
                ArgumentExtension::None => {
                    assert_eq!(
                        ty, param.ty,
                        "invalid call to {callee}: invalid argument type for parameter at index \
                         {i}"
                    );
                }
                // Caller can provide a smaller type which will be zero-extended to the expected
                // type
                //
                // However, the argument must be an unsigned integer, and of smaller or equal size
                // in order for the types to differ
                ArgumentExtension::Zext if ty != param.ty => {
                    assert!(
                        param.ty.is_unsigned_integer(),
                        "invalid function signature: zero-extension is only valid for unsigned \
                         integer types"
                    );
                    assert!(
                        ty.is_unsigned_integer(),
                        "invalid call to {callee}: invalid argument type for parameter at index \
                         {i}, expected unsigned integer type, got {ty}"
                    );
                    let expected_size = param.ty.size_in_bits();
                    let provided_size = param.ty.size_in_bits();
                    assert!(
                        provided_size <= expected_size,
                        "invalid call to {callee}: invalid argument type for parameter at index \
                         {i}, expected integer width to be <= {expected_size} bits"
                    );
                    // Zero-extend this argument
                    self.stack.push(arg);
                    self.zext(&param.ty, span);
                    self.stack.drop();
                }
                // Caller can provide a smaller type which will be sign-extended to the expected
                // type
                //
                // However, the argument must be an integer which can fit in the range of the
                // expected type
                ArgumentExtension::Sext if ty != param.ty => {
                    assert!(
                        param.ty.is_signed_integer(),
                        "invalid function signature: sign-extension is only valid for signed \
                         integer types"
                    );
                    assert!(
                        ty.is_integer(),
                        "invalid call to {callee}: invalid argument type for parameter at index \
                         {i}, expected integer type, got {ty}"
                    );
                    let expected_size = param.ty.size_in_bits();
                    let provided_size = param.ty.size_in_bits();
                    if ty.is_unsigned_integer() {
                        assert!(
                            provided_size < expected_size,
                            "invalid call to {callee}: invalid argument type for parameter at \
                             index {i}, expected unsigned integer width to be < {expected_size} \
                             bits"
                        );
                    } else {
                        assert!(
                            provided_size <= expected_size,
                            "invalid call to {callee}: invalid argument type for parameter at \
                             index {i}, expected integer width to be <= {expected_size} bits"
                        );
                    }
                    // Push the operand back on the stack for `sext`
                    self.stack.push(arg);
                    self.sext(&param.ty, span);
                    self.stack.drop();
                }
                ArgumentExtension::Zext | ArgumentExtension::Sext => (),
            }
        }

        for result in signature.results.iter().rev() {
            self.push(result.ty.clone());
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::{collections::BTreeSet, rc::Rc};

    use midenc_hir::{ArrayType, Context};

    use super::*;
    use crate::{OperandStack, masm::Op};

    #[test]
    fn caller_emits_vm_instruction_and_pushes_word() {
        let mut block = Vec::default();
        let context = Rc::new(Context::default());
        let mut stack = OperandStack::new(context);
        let mut invoked = BTreeSet::default();
        let mut emitter = OpEmitter::new(&mut invoked, &mut block, &mut stack);

        let span = SourceSpan::default();
        emitter.caller(span);

        assert_eq!(emitter.stack_len(), 1);
        assert_eq!(emitter.stack()[0], Type::from(ArrayType::new(Type::Felt, 4)));
        assert_eq!(&block[0], &Op::Inst(masm::Span::new(span, masm::Instruction::Caller)));
    }

    /// Pin the exact instruction sequence and stack effect of an indirect call: the bounds
    /// check, the in-place index-to-address rewrite, the signature-tag check, and the
    /// frame-traced `dynexec`.
    #[test]
    fn exec_indirect_emits_bounds_check_tag_check_and_dynexec() {
        use midenc_hir::{CallConv, Felt};

        use crate::linker::FunctionTableLayout;

        let mut block = Vec::default();
        let context = Rc::new(Context::default());
        let mut stack = OperandStack::new(context.clone());
        let mut invoked = BTreeSet::default();
        let mut emitter = OpEmitter::new(&mut invoked, &mut block, &mut stack);

        let signature =
            Signature::with_convention(&context, CallConv::C, [Type::I32, Type::I32], [Type::I32]);

        // The scheduled operand order is [index, args...], index on top
        emitter.push(Type::I32);
        emitter.push(Type::I32);
        emitter.push(Type::U32);

        let span = SourceSpan::default();
        let num_slots = 5u32;
        let base_elem_addr = 294912u32;
        let type_tag = 3u32;
        emitter.exec_indirect(num_slots, base_elem_addr, type_tag, &signature, span);

        // The emulated stack holds exactly the call result
        assert_eq!(emitter.stack_len(), 1);
        assert_eq!(emitter.stack()[0], Type::I32);

        let insts = block
            .iter()
            .map(|op| match op {
                Op::Inst(inst) => inst.clone().into_inner(),
                op => panic!("unexpected non-instruction op: {op:?}"),
            })
            .collect::<Vec<_>>();
        assert_eq!(insts.len(), 14);
        // Bounds check: duplicate the index and assert it is in bounds
        assert_eq!(insts[0], masm::Instruction::Dup0);
        assert!(
            matches!(&insts[1], masm::Instruction::Push(masm::Immediate::Value(value)) if *value.inner() == num_slots.into()),
            "expected push of the slot count, got {:?}",
            insts[1]
        );
        assert_eq!(insts[2], masm::Instruction::U32Lt);
        assert!(
            matches!(&insts[3], masm::Instruction::AssertWithError(masm::Immediate::Value(msg)) if msg.inner().contains("function table index out of bounds")),
            "expected bounds-check assertion, got {:?}",
            insts[3]
        );
        // Rewrite the index to the slot's element address
        assert!(
            matches!(&insts[4], masm::Instruction::MulImm(masm::Immediate::Value(value)) if *value.inner() == Felt::new_unchecked(FunctionTableLayout::SLOT_SIZE_ELEMENTS as u64)),
            "expected multiply by the slot size, got {:?}",
            insts[4]
        );
        assert!(
            matches!(&insts[5], masm::Instruction::AddImm(masm::Immediate::Value(value)) if *value.inner() == Felt::new_unchecked(base_elem_addr as u64)),
            "expected add of the table base address, got {:?}",
            insts[5]
        );
        // Signature check: load the slot's tag and assert it matches the expected tag
        assert_eq!(insts[6], masm::Instruction::Dup0);
        assert!(
            matches!(&insts[7], masm::Instruction::AddImm(masm::Immediate::Value(value)) if *value.inner() == Felt::new_unchecked(FunctionTableLayout::TYPE_TAG_OFFSET_ELEMENTS as u64)),
            "expected add of the tag offset, got {:?}",
            insts[7]
        );
        assert_eq!(insts[8], masm::Instruction::MemLoad);
        assert!(
            matches!(&insts[9], masm::Instruction::Push(masm::Immediate::Value(value)) if *value.inner() == type_tag.into()),
            "expected push of the expected signature tag, got {:?}",
            insts[9]
        );
        assert!(
            matches!(&insts[10], masm::Instruction::AssertEqWithError(masm::Immediate::Value(msg)) if msg.inner().contains("callee signature mismatch")),
            "expected signature-check assertion, got {:?}",
            insts[10]
        );
        // Frame-traced dynexec, which itself pops the slot address
        assert!(matches!(&insts[11], masm::Instruction::EmitImm(_)));
        assert_eq!(insts[12], masm::Instruction::DynExec);
        assert!(matches!(&insts[13], masm::Instruction::EmitImm(_)));
    }

    #[test]
    fn clk_emits_vm_instruction_and_pushes_felt() {
        let mut block = Vec::default();
        let context = Rc::new(Context::default());
        let mut stack = OperandStack::new(context);
        let mut invoked = BTreeSet::default();
        let mut emitter = OpEmitter::new(&mut invoked, &mut block, &mut stack);

        let span = SourceSpan::default();
        emitter.clk(span);

        assert_eq!(emitter.stack_len(), 1);
        assert_eq!(emitter.stack()[0], Type::Felt);
        assert_eq!(&block[0], &Op::Inst(masm::Span::new(span, masm::Instruction::Clk)));
    }
}