wasmi 2.0.0-beta.3

WebAssembly interpreter
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
use super::{ImmediateOperand, LocalIdx, LocalOperand, LocalsHead, Operand, Reset, TempOperand};
use crate::{
    Error,
    ValType,
    core::{RawVal, TypedRawVal},
    engine::{TranslationError, translator::utils::required_cells_for_ty},
    ir::{Slot, SlotSpan},
};
use alloc::vec::Vec;
use core::{num::NonZero, slice};

/// A [`StackOperand`] or [`Operand`] position on the [`OperandStack`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct StackPos(NonZero<usize>);

impl From<StackPos> for usize {
    fn from(value: StackPos) -> Self {
        value.0.get().wrapping_sub(1)
    }
}

impl From<usize> for StackPos {
    fn from(value: usize) -> Self {
        let Some(operand_idx) = NonZero::new(value.wrapping_add(1)) else {
            panic!("out of bounds `StackPos`: {value}")
        };
        Self(operand_idx)
    }
}

/// An [`Operand`] on the [`OperandStack`].
///
/// This is the internal version of [`Operand`] with information that shall remain
/// hidden to the outside.
#[derive(Debug, Copy, Clone)]
pub enum StackOperand {
    /// A local variable.
    Local {
        /// The temporary stack offset of the operand.
        temp_slots: SlotSpan,
        /// The type of the local operand.
        ///
        /// This does not have to be the type of the associated local but
        /// might be a type overwrite. This is useful for Wasm `reinterpret`
        /// operators with local operand inputs.
        ty: ValType,
        /// The index of the local variable.
        local_index: LocalIdx,
        /// The previous [`StackOperand::Local`] on the [`OperandStack`].
        prev_local: Option<StackPos>,
        /// The next [`StackOperand::Local`] on the [`OperandStack`].
        next_local: Option<StackPos>,
    },
    /// A temporary value on the [`OperandStack`].
    Temp {
        /// The temporary stack offset of the operand.
        temp_slots: SlotSpan,
        /// The type of the temporary operand.
        ty: ValType,
        /// This is `true` if the [`StackOperand::Temp`] is also stored in a register.
        in_reg: bool,
    },
    /// An immediate value on the [`OperandStack`].
    Immediate {
        /// The temporary stack offset of the operand.
        temp_slots: SlotSpan,
        /// The type of the immediate operand.
        ty: ValType,
        /// The value of the immediate operand.
        val: RawVal,
    },
}

impl StackOperand {
    /// Returns the [`ValType`] of the [`StackOperand`].
    pub fn ty(&self) -> ValType {
        match self {
            | Self::Temp { ty, .. } | Self::Immediate { ty, .. } | Self::Local { ty, .. } => *ty,
        }
    }

    /// Returns the temporary [`SlotSpan`] of the [`StackOperand`].
    pub fn temp_slots(&self) -> SlotSpan {
        match self {
            | Self::Temp { temp_slots, .. }
            | Self::Immediate { temp_slots, .. }
            | Self::Local { temp_slots, .. } => *temp_slots,
        }
    }
}

/// Whether pushing an operand should also allocate a register for it.
#[derive(Debug, Copy, Clone)]
pub enum Allocation {
    /// No register is being allocated for the operand.
    None,
    /// A register is being allocated for the operand.
    Reg,
}

impl Allocation {
    /// Returns `true` if `self` is [`Self::Reg`].
    pub fn is_reg(&self) -> bool {
        matches!(self, Self::Reg)
    }
}

/// The Wasm operand (or value) stack.
#[derive(Debug, Default)]
pub struct OperandStack {
    /// The current set of operands on the [`OperandStack`].
    operands: Vec<StackOperand>,
    /// Stores the first occurrences of every local variable on the [`OperandStack`] if any.
    local_heads: LocalsHead,
    /// The current number of local operands on the `operands` stack.
    ///
    /// This field is required to optimize [`OperandStack::preserve_all_locals`].
    len_locals: usize,
    /// The total number of stack slots used for locals.
    local_slots: u16,
    /// The current top-most temporary stack offset.
    ///
    /// # Note
    ///
    /// - This is used and advanced for the next operand pushed to the stack.
    /// - Upon popping an operand this offset is decreased.
    temp_offset: u16,
    /// The maximum recorded temporary stack offset.
    max_offset: u16,
    /// Information about the registers that are in use.
    regs: RegisterMap,
}

/// The state of all registers and their live links.
#[derive(Debug, Default, Copy, Clone)]
pub struct RegisterMap {
    /// The link of the general-purpose register (GPR) if any.
    ireg: Option<RegisterLink>,
    /// The link of the `f32` register if any.
    freg32: Option<RegisterLink>,
    /// The link of the `f64` register if any.
    freg64: Option<RegisterLink>,
}

impl Reset for RegisterMap {
    fn reset(&mut self) {
        self.ireg = None;
        self.freg32 = None;
        self.freg64 = None;
    }
}

impl RegisterMap {
    /// Returns a `&mut` to the [`RegisterLink`] of the register for type `ty`.
    fn get_mut(&mut self, ty: ValType) -> &mut Option<RegisterLink> {
        match ty {
            | ValType::I32 | ValType::FuncRef | ValType::ExternRef | ValType::I64 => &mut self.ireg,
            | ValType::F32 => &mut self.freg32,
            | ValType::F64 => &mut self.freg64,
            | ValType::V128 => unreachable!(),
        }
    }

    /// Allocates `link` to the register for type `ty`.
    ///
    /// Returns the old link of the register for type `ty` if any.
    pub fn alloc(&mut self, ty: ValType, link: RegisterLink) -> Option<RegisterLink> {
        self.get_mut(ty).replace(link)
    }

    /// Deallocates any link from the register for type `ty`.
    ///
    /// Returns the deallocated link of the register for type `ty` if any.
    pub fn dealloc(&mut self, ty: ValType) -> Option<RegisterLink> {
        self.get_mut(ty).take()
    }

    /// Deallocates any temporary link from the register for type `ty`.
    ///
    /// Returns the deallocated link of the register for type `ty` if any.
    pub fn dealloc_temp(&mut self, ty: ValType, skip_threshold: usize) -> Option<StackPos> {
        let link = self.get_mut(ty);
        let Some(RegisterLink::Temp(pos)) = link else {
            return None;
        };
        if usize::from(*pos) >= skip_threshold {
            return None;
        }
        let pos = *pos;
        link.take();
        Some(pos)
    }

    /// Returns the link of the register for type `ty` if any.
    pub fn get(&self, ty: ValType) -> Option<RegisterLink> {
        match ty {
            | ValType::I32 | ValType::FuncRef | ValType::ExternRef | ValType::I64 => self.ireg,
            | ValType::F32 => self.freg32,
            | ValType::F64 => self.freg64,
            | ValType::V128 => None,
        }
    }

    /// Returns `true` if the local at `local_index` is known to be stored in the register of type `ty`.
    pub fn is_local_in_reg(&self, ty: ValType, local_index: LocalIdx) -> bool {
        let Some(RegisterLink::Local(index)) = self.get(ty) else {
            return false;
        };
        index == local_index
    }

    /// Returns `true` if the local at `local_index` is known to be stored in any of the registers.
    pub fn is_local_in_any_reg(&self, local_index: LocalIdx) -> bool {
        self.is_local_in_reg(ValType::I64, local_index)
            || self.is_local_in_reg(ValType::F32, local_index)
            || self.is_local_in_reg(ValType::F64, local_index)
    }

    /// Enhance the `operand` with `in_reg` information if it is a local operand.
    pub fn wrap_operand(&self, pos: StackPos, operand: StackOperand) -> Operand {
        let in_reg = match operand {
            StackOperand::Local {
                ty, local_index, ..
            } => self.is_local_in_reg(ty, local_index),
            _ => false,
        };
        Operand::new(pos, operand, in_reg)
    }

    /// Discards any local register links from `self`.
    pub fn discard_local_regs(&mut self) {
        fn is_local(link: &mut RegisterLink) -> bool {
            matches!(link, RegisterLink::Local(_))
        }
        self.ireg.take_if(is_local);
        self.freg32.take_if(is_local);
        self.freg64.take_if(is_local);
    }
}

/// The current linkage of a register.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RegisterLink {
    /// The register is linked to a temporary on the stack.
    Temp(StackPos),
    /// The register is linked to a local variable.
    Local(LocalIdx),
}

impl Reset for OperandStack {
    fn reset(&mut self) {
        self.operands.clear();
        self.local_heads.reset();
        self.len_locals = 0;
        self.local_slots = 0;
        self.temp_offset = 0;
        self.max_offset = 0;
        self.regs.reset();
    }
}

impl OperandStack {
    /// Slot `amount` local variables.
    ///
    /// # Errors
    ///
    /// If too many local variables are being registered.
    pub fn register_locals(&mut self, amount: usize, ty: ValType) -> Result<(), Error> {
        self.local_heads.register(amount)?;
        let cells_per_item = required_cells_for_ty(ty);
        let Ok(amount) = u16::try_from(amount) else {
            return Err(Error::from(TranslationError::TooManyLocalVariables));
        };
        let required_cells = amount
            .checked_mul(cells_per_item)
            .ok_or_else(|| Error::from(TranslationError::AllocatedTooManySlots))?;
        self.push_local_slots(required_cells)?;
        Ok(())
    }

    /// Registers the local at `local_index` for register of type `ty`.
    pub fn register_local_for_reg(
        &mut self,
        ty: ValType,
        local_index: LocalIdx,
    ) -> Result<(), Error> {
        if matches!(ty, ValType::V128) {
            // Cannot register `v128` operand for a register.
            return Ok(());
        }
        self.regs.alloc(ty, RegisterLink::Local(local_index));
        Ok(())
    }

    /// Deallocates the local at `local_index` for register of type `ty`.
    pub fn dealloc_local_for_reg(
        &mut self,
        ty: ValType,
        local_index: LocalIdx,
    ) -> Result<(), Error> {
        if matches!(ty, ValType::V128) {
            // Cannot register `v128` operand for a register.
            return Ok(());
        }
        if self.regs.is_local_in_any_reg(local_index) {
            self.regs.dealloc(ty);
        }
        Ok(())
    }

    /// Deallocates the register of the `ty` from `self`.
    ///
    /// Returns `Some` if a copy operator is required to reflect the changes.
    ///
    /// # Note
    ///
    /// If the register operand is a register-backed local it is turned into a normal local operand
    /// and `None` is returned as no copy operator is required.
    pub fn dealloc_reg(&mut self, ty: ValType) -> Option<TempOperand> {
        let link = self.regs.dealloc(ty)?;
        let RegisterLink::Temp(pos) = link else {
            return None;
        };
        let operand = self.get_at_mut(pos);
        let StackOperand::Temp {
            temp_slots,
            ty,
            in_reg,
        } = operand
        else {
            unreachable!()
        };
        let returned = TempOperand::new(*temp_slots, *ty, pos, *in_reg);
        *in_reg = false;
        Some(returned)
    }

    /// Returns the current [`RegisterMap`] state.
    pub fn get_registers(&self) -> RegisterMap {
        self.regs
    }

    /// Restores the state of registers of `self`.
    pub fn set_registers(&mut self, registers: RegisterMap) {
        self.regs = registers;
    }

    /// Returns the total number of stack slots used for locals.
    pub fn get_local_slots(&self) -> u16 {
        self.local_slots
    }

    /// Pushes the total number of local slots by `delta`.
    ///
    /// # Errors
    ///
    /// Returns an error if too many local slots are in use.
    fn push_local_slots(&mut self, delta: u16) -> Result<(), Error> {
        debug_assert_eq!(self.local_slots, self.max_offset);
        let Some(new_value) = self.local_slots.checked_add(delta) else {
            return Err(Error::from(TranslationError::AllocatedTooManySlots));
        };
        self.local_slots = new_value;
        self.temp_offset = new_value;
        self.max_offset = self.max_offset.max(self.temp_offset);
        Ok(())
    }

    /// Pushes the offset for temporary operands by `delta`.
    ///
    /// Returns the temporary offset before this operation.
    ///
    /// # Errors
    ///
    /// Returns an error if the new temporary offset is out of bounds.
    fn push_temp_offset(&mut self, delta: u16) -> Result<SlotSpan, Error> {
        let old_offset = self.temp_offset;
        let Some(new_value) = old_offset.checked_add(delta) else {
            return Err(Error::from(TranslationError::AllocatedTooManySlots));
        };
        self.temp_offset = new_value;
        self.max_offset = self.max_offset.max(self.temp_offset);
        Ok(SlotSpan::new(Slot::from(old_offset)))
    }

    /// Pops the offset for temporary operands by `delta`.
    ///
    /// # Panics
    ///
    /// If the temporary offset would drop below zero.
    fn pop_temp_offset(&mut self, delta: usize) -> Result<(), Error> {
        let Ok(delta) = u16::try_from(delta) else {
            return Err(Error::from(TranslationError::AllocatedTooManySlots));
        };
        self.temp_offset = self.temp_offset.checked_sub(delta).unwrap_or_else(|| {
            panic!(
                "underflow in `pop_temp_offset`: temp_offset = {}, delta = {delta}",
                self.temp_offset
            )
        });
        Ok(())
    }

    /// Returns the current height of `self`
    ///
    /// # Note
    ///
    /// The height is equal to the number of [`StackOperand`]s on `self`.
    pub fn height(&self) -> usize {
        self.operands.len()
    }

    /// Returns the temporary [`Slot`] allocated for the next pushed operand.
    pub fn next_temp_slots(&self) -> SlotSpan {
        SlotSpan::new(Slot::from(self.temp_offset))
    }

    /// Returns the maximum stack offset of `self`.
    ///
    /// # Note
    ///
    /// This value is equal to the maximum number of cells a function requires to operate.
    pub fn max_stack_offset(&self) -> usize {
        usize::from(self.max_offset)
    }

    /// Returns the [`StackPos`] of the next pushed operand.
    fn next_stack_pos(&self) -> StackPos {
        StackPos::from(self.operands.len())
    }

    /// Returns the [`StackPos`] of the operand at `depth`.
    fn depth_to_stack_pos(&self, depth: usize) -> StackPos {
        StackPos::from(self.height() - depth - 1)
    }

    /// Pushes the [`Operand`] back to the [`OperandStack`].
    ///
    /// Returns the new [`StackPos`].
    ///
    /// # Errors
    ///
    /// - If too many operands have been pushed onto the [`OperandStack`].
    /// - If the local with `local_idx` does not exist.
    #[inline]
    pub fn push_operand(&mut self, operand: Operand) -> Result<Operand, Error> {
        match operand {
            Operand::Local(op) => self
                .push_local(op.local_index(), op.ty())
                .map(Operand::from),
            Operand::Temp(op) => self.push_temp(op.ty(), op.alloc()).map(Operand::from),
            Operand::Immediate(op) => self.push_immediate(op.val()).map(Operand::from),
        }
    }

    /// Pushes a local variable with index `local_idx` to the [`OperandStack`].
    ///
    /// # Errors
    ///
    /// - If too many operands have been pushed onto the [`OperandStack`].
    /// - If the local with `local_idx` does not exist.
    #[inline]
    pub fn push_local(
        &mut self,
        local_index: LocalIdx,
        ty: ValType,
    ) -> Result<LocalOperand, Error> {
        let stack_pos = self.next_stack_pos();
        let next_local = self.local_heads.replace_first(local_index, Some(stack_pos));
        if let Some(next_local) = next_local {
            self.update_prev_local(next_local, Some(stack_pos));
        }
        let temp_slots = self.push_temp_offset(required_cells_for_ty(ty))?;
        self.operands.push(StackOperand::Local {
            temp_slots,
            ty,
            local_index,
            prev_local: None,
            next_local,
        });
        self.len_locals += 1;
        let in_reg = self.regs.is_local_in_reg(ty, local_index);
        Ok(LocalOperand::new(temp_slots, ty, local_index, in_reg))
    }

    /// Pushes a temporary with type `ty` on the [`OperandStack`].
    ///
    /// # Errors
    ///
    /// If too many operands have been pushed onto the [`OperandStack`].
    #[inline]
    pub fn push_temp(&mut self, ty: ValType, alloc: Allocation) -> Result<TempOperand, Error> {
        let stack_pos = self.next_stack_pos();
        if alloc.is_reg() {
            self.alloc_reg(RegisterLink::Temp(stack_pos), ty);
        }
        let temp_slots = self.push_temp_offset(required_cells_for_ty(ty))?;
        let in_reg = alloc.is_reg();
        self.operands.push(StackOperand::Temp {
            temp_slots,
            ty,
            in_reg,
        });
        Ok(TempOperand::new(temp_slots, ty, stack_pos, in_reg))
    }

    /// Pushes an immediate `value` on the [`OperandStack`].
    ///
    /// # Errors
    ///
    /// If too many operands have been pushed onto the [`OperandStack`].
    #[inline]
    pub fn push_immediate(
        &mut self,
        value: impl Into<TypedRawVal>,
    ) -> Result<ImmediateOperand, Error> {
        let value = value.into();
        let ty = value.ty();
        let val = value.raw();
        let temp_slots = self.push_temp_offset(required_cells_for_ty(ty))?;
        self.operands.push(StackOperand::Immediate {
            temp_slots,
            ty,
            val,
        });
        Ok(ImmediateOperand::new(temp_slots, ty, val))
    }

    /// Returns an iterator that yields the last `n` [`Operand`]s.
    ///
    /// # Panics
    ///
    /// If `n` is out of bounds for `self`.
    pub fn peek(&self, n: usize) -> PeekedOperands<'_> {
        let len_operands = self.operands.len();
        let first_index = len_operands - n;
        let Some(operands) = self.operands.get(first_index..) else {
            return PeekedOperands::empty();
        };
        PeekedOperands {
            stack_pos: first_index,
            operands: operands.iter(),
            regs: self.regs,
        }
    }

    /// Pops the top-most [`StackOperand`] from `self` if any.
    ///
    /// # Panics
    ///
    /// If `self` is empty.
    #[inline]
    pub fn pop(&mut self) -> Operand {
        let Some(operand) = self.operands.pop() else {
            panic!("tried to pop operand from empty stack");
        };
        self.pop_temp_offset(usize::from(required_cells_for_ty(operand.ty())))
            .unwrap_or_else(|error| panic!("failed to pop temporary offset: {error}"));
        let stack_pos = self.next_stack_pos();
        self.try_unlink_local(operand);
        if let Some(reg_pos) = self.try_unlink_reg(operand) {
            debug_assert_eq!(stack_pos, reg_pos);
        }
        self.regs.wrap_operand(stack_pos, operand)
    }

    /// Returns the [`Operand`] at `depth`.
    ///
    /// A `depth` of 1 returns the last [`Operand`] on `self`.
    ///
    /// # Panics
    ///
    /// If `depth` is out of bounds for `self`.
    #[inline]
    pub fn get(&self, depth: usize) -> Operand {
        let stack_pos = self.depth_to_stack_pos(depth);
        let operand = self.get_at(stack_pos);
        self.regs.wrap_operand(stack_pos, operand)
    }

    /// Returns the [`StackOperand`] at `index`.
    ///
    /// # Panics
    ///
    /// If `index` is out of bounds for `self`.
    #[inline]
    fn get_at(&self, pos: StackPos) -> StackOperand {
        self.operands[usize::from(pos)]
    }

    /// Returns the [`StackOperand`] at `index`.
    ///
    /// # Panics
    ///
    /// If `index` is out of bounds for `self`.
    #[inline]
    fn get_at_mut(&mut self, pos: StackPos) -> &mut StackOperand {
        &mut self.operands[usize::from(pos)]
    }

    /// Converts and returns the [`Operand`] at `depth` into a [`Operand::Temp`].
    ///
    /// # Note
    ///
    /// - Returns the [`Operand`] at `depth` before being converted to an [`Operand::Temp`].
    /// - [`Operand::Temp`] will have their optional `instr` set to `None`.
    ///
    /// # Panics
    ///
    /// If `depth` is out of bounds for the [`OperandStack`] of operands.
    #[must_use]
    pub fn operand_to_temp(&mut self, depth: usize) -> Operand {
        let stack_pos = self.depth_to_stack_pos(depth);
        let operand = self.operand_to_temp_at(stack_pos);
        self.regs.wrap_operand(stack_pos, operand)
    }

    /// Converts and returns the [`StackOperand`] at `index` into a [`StackOperand::Temp`].
    ///
    /// # Note
    ///
    /// - Returns the [`Operand`] at `index` before being converted to an [`Operand::Temp`].
    /// - [`Operand::Temp`] will have their optional `instr` set to `None`.
    ///
    /// # Panics
    ///
    /// If `index` is out of bounds for `self`.
    #[must_use]
    fn operand_to_temp_at(&mut self, index: StackPos) -> StackOperand {
        let operand = self.get_at(index);
        let temp_slots = operand.temp_slots();
        let ty = operand.ty();
        self.try_unlink_local(operand);
        self.try_unlink_reg(operand);
        self.operands[usize::from(index)] = StackOperand::Temp {
            temp_slots,
            ty,
            in_reg: false,
        };
        operand
    }

    fn preserve_local_at(&mut self, pos: StackPos) -> StackOperand {
        let operand = self.get_at(pos);
        let StackOperand::Local {
            temp_slots,
            ty,
            local_index,
            prev_local,
            next_local,
        } = operand
        else {
            unreachable!()
        };
        self.unlink_local(local_index, prev_local, next_local);
        self.operands[usize::from(pos)] = StackOperand::Temp {
            in_reg: false,
            temp_slots,
            ty,
        };
        operand
    }

    /// Preserve all locals on the [`OperandStack`] that refer to `local_index`.
    ///
    /// This is done by converting those locals to [`StackOperand::Temp`] and yielding them.
    ///
    /// # Note
    ///
    /// The users must fully consume all items yielded by the returned iterator in order
    /// for the local preservation to take full effect.
    ///
    /// # Panics
    ///
    /// If the local at `local_index` is out of bounds.
    #[must_use]
    pub fn preserve_locals(&mut self, local_index: LocalIdx) -> PreservedLocalsIter<'_> {
        let stack_pos = self.local_heads.replace_first(local_index, None);
        let in_reg = self.regs.is_local_in_any_reg(local_index);
        PreservedLocalsIter {
            stack: self,
            stack_pos,
            in_reg,
        }
    }

    /// Preserve all locals on the [`OperandStack`].
    ///
    /// This is done by converting those locals to [`StackOperand::Temp`] and yielding them.
    ///
    /// # Note
    ///
    /// The users must fully consume all items yielded by the returned iterator in order
    /// for the local preservation to take full effect.
    #[must_use]
    pub fn preserve_all_locals(&mut self, skip: usize) -> PreservedAllLocalsIter<'_> {
        PreservedAllLocalsIter::new(self, skip)
    }
}

#[derive(Copy, Clone)]
pub struct PreservedRegs {
    pub ireg: Option<Slot>,
    pub freg32: Option<Slot>,
    pub freg64: Option<Slot>,
}

impl OperandStack {
    /// Discards any local register links from `self`.
    pub fn discard_local_regs(&mut self) {
        self.regs.discard_local_regs()
    }

    /// Preserve all register operands on the [`OperandStack`].
    ///
    /// This is done by converting those operands to [`StackOperand::Temp`] and
    /// returning their associated [`Slot`] in order to emit copy operators by
    /// the caller.
    #[must_use]
    pub fn preserve_all_regs(&mut self) -> PreservedRegs {
        fn preserve_reg(this: &mut OperandStack, link: RegisterLink) -> Option<Slot> {
            let RegisterLink::Temp(stack_pos) = link else {
                return None;
            };
            Some(this.operand_to_temp_at(stack_pos).temp_slots().head())
        }
        let reg_tys = [ValType::I64, ValType::F32, ValType::F64];
        let [ireg, freg32, freg64] = reg_tys.map(|ty| {
            self.regs
                .dealloc(ty)
                .and_then(|reg| preserve_reg(self, reg))
        });
        PreservedRegs {
            ireg,
            freg32,
            freg64,
        }
    }

    /// Preserve all temporary register operands on the [`OperandStack`].
    ///
    /// This is done by converting those operands to [`StackOperand::Temp`] and
    /// returning their associated [`Slot`] in order to emit copy operators by
    /// the caller.
    #[must_use]
    pub fn preserve_all_temp_regs(&mut self, skip: usize) -> PreservedRegs {
        let skip_threshold = self.operands.len() - skip;
        let reg_tys = [ValType::I64, ValType::F32, ValType::F64];
        let [ireg, freg32, freg64] = reg_tys.map(|ty| {
            self.regs
                .dealloc_temp(ty, skip_threshold)
                .map(|pos| self.operand_to_temp_at(pos).temp_slots().head())
        });
        PreservedRegs {
            ireg,
            freg32,
            freg64,
        }
    }

    /// Unlinks the [`StackOperand::Local`] `operand` at `index` from `self`.
    ///
    /// Does nothing if `operand` is not a [`StackOperand::Local`].
    #[inline]
    fn try_unlink_local(&mut self, operand: StackOperand) {
        let StackOperand::Local {
            local_index,
            prev_local,
            next_local,
            ..
        } = operand
        else {
            return;
        };
        self.unlink_local(local_index, prev_local, next_local);
    }

    /// Links the operand at `pos` to the register associated to `ty`.
    ///
    /// # Panics (Debug)
    ///
    /// If the register with `ty` is already occupied.
    fn alloc_reg(&mut self, link: RegisterLink, ty: ValType) {
        let prev_link = self.regs.alloc(ty, link);
        debug_assert!(
            prev_link.is_none(),
            "a register operand already exists on the stack",
        );
    }

    /// Deallocates the `operand`'s register if any.
    ///
    /// Does nothing if `operand` has no allocated register.
    #[inline]
    fn try_unlink_reg(&mut self, operand: StackOperand) -> Option<StackPos> {
        let ty = match operand {
            StackOperand::Temp {
                in_reg: true, ty, ..
            } => ty,
            _ => return None,
        };
        match self.regs.dealloc(ty)? {
            RegisterLink::Temp(stack_pos) => Some(stack_pos),
            RegisterLink::Local(_) => None,
        }
    }

    /// Unlinks the [`StackOperand::Local`] `operand` identified by the parameters from `self`.
    fn unlink_local(
        &mut self,
        local_index: LocalIdx,
        prev_local: Option<StackPos>,
        next_local: Option<StackPos>,
    ) {
        if let Some(prev_local) = prev_local {
            self.update_next_local(prev_local, next_local);
        } else {
            self.local_heads.replace_first(local_index, next_local);
        }
        if let Some(next_local) = next_local {
            self.update_prev_local(next_local, prev_local);
        }
        self.len_locals -= 1;
    }

    /// Updates the `prev_local` of the [`StackOperand::Local`] at `local_index` to `prev_index`.
    ///
    /// # Panics
    ///
    /// - If `local_index` does not refer to a [`StackOperand::Local`].
    /// - If `local_index` is out of bounds of the operand stack.
    fn update_prev_local(&mut self, local_pos: StackPos, prev_pos: Option<StackPos>) {
        match self.operands.get_mut(usize::from(local_pos)) {
            Some(StackOperand::Local { prev_local, .. }) => {
                *prev_local = prev_pos;
            }
            entry => panic!("expected `StackOperand::Local` but found: {entry:?}"),
        }
    }

    /// Updates the `next_local` of the [`StackOperand::Local`] at `local_index` to `prev_index`.
    ///
    /// # Panics
    ///
    /// - If `local_index` does not refer to a [`StackOperand::Local`].
    /// - If `local_index` is out of bounds of the operand stack.
    fn update_next_local(&mut self, local_pos: StackPos, next_pos: Option<StackPos>) {
        match self.operands.get_mut(usize::from(local_pos)) {
            Some(StackOperand::Local { next_local, .. }) => {
                *next_local = next_pos;
            }
            entry => panic!("expected `StackOperand::Local` but found: {entry:?}"),
        }
    }
}

/// Iterator yielding preserved local indices while preserving them.
///
/// # Note
///
/// This intentionally iterates backwards from the last pushed stack operand to the first one.
/// Together with the remaining number of local operands on the stack this achieves armortized
/// constant O(1) preservation for all locals via [`OperandStack::preserve_all_locals`].
///
/// The reason for this is that a single call to [`OperandStack::preserve_all_locals`] has the
/// effect that there are no more local operands on the stack. New locals are always pushed to the
/// top of the stack. A single Wasm `local.get` operation (or similar) may only push a single local
/// operand on the stack. This iterator yields once there are no more local operands and since
/// it iterates from the back (top-most) operand it will find the newly inserted locals in
/// armortized constant O(1) time.
#[derive(Debug)]
pub struct PreservedAllLocalsIter<'stack> {
    /// The underlying operand stack.
    stack: &'stack mut OperandStack,
    /// The current operand stack position of the next preserved local if any.
    stack_pos: usize,
    /// The number of operands to be skipped before preservation.
    skip: usize,
    /// The number of remaining locals to be found.
    remaining_locals: usize,
}

impl<'stack> PreservedAllLocalsIter<'stack> {
    /// Creates a new [`PreservedAllLocalsIter`] for `stack` skipping the top-most `skip` operands.
    pub fn new(stack: &'stack mut OperandStack, skip: usize) -> Self {
        let stack_pos = stack.operands.len();
        let remaining_locals = stack.len_locals;
        Self {
            stack,
            stack_pos,
            skip,
            remaining_locals,
        }
    }

    /// Returns `true` if there are remaining local operands on the stack.
    fn has_remaining_locals(&self) -> bool {
        self.remaining_locals != 0
    }

    /// Returns the index of the next local operand on the stack if any.
    ///
    /// Returns `None` if there are no more local operands on the stack.
    fn find_next_local(&mut self) -> Option<usize> {
        let mut stack_pos = self.stack_pos;
        loop {
            if !self.has_remaining_locals() {
                return None;
            }
            stack_pos -= 1;
            let opd = self.stack.operands.get(stack_pos)?;
            let StackOperand::Local { .. } = opd else {
                continue;
            };
            self.remaining_locals -= 1;
            if self.skip > 0 {
                self.skip -= 1;
                continue;
            }
            return Some(stack_pos);
        }
    }
}

impl Iterator for PreservedAllLocalsIter<'_> {
    type Item = LocalOperand;

    fn next(&mut self) -> Option<Self::Item> {
        self.stack_pos = self.find_next_local()?;
        let stack_pos = StackPos::from(self.stack_pos);
        let operand = self.stack.preserve_local_at(stack_pos);
        let StackOperand::Local {
            temp_slots,
            ty,
            local_index,
            ..
        } = operand
        else {
            unreachable!("expected `StackOperand::Local` but found: {operand:?}")
        };
        let in_reg = self.stack.regs.is_local_in_reg(ty, local_index);
        Some(LocalOperand::new(temp_slots, ty, local_index, in_reg))
    }
}

/// Iterator yielding preserved local indices while preserving them.
#[derive(Debug)]
pub struct PreservedLocalsIter<'stack> {
    /// The underlying operand stack.
    stack: &'stack mut OperandStack,
    /// The current operand stack position of the next preserved local if any.
    stack_pos: Option<StackPos>,
    /// This is `true` if the preserved local is stored in a register.
    ///
    /// This must be the same for all local operands on the stack that
    /// refer to the same underlying local variable.
    in_reg: bool,
}

impl Iterator for PreservedLocalsIter<'_> {
    type Item = LocalOperand;

    fn next(&mut self) -> Option<Self::Item> {
        let stack_pos = self.stack_pos?;
        let operand = self.stack.preserve_local_at(stack_pos);
        let StackOperand::Local {
            temp_slots,
            ty,
            local_index,
            next_local,
            ..
        } = operand
        else {
            unreachable!("expected `StackOperand::Local` but found: {operand:?}")
        };
        self.stack_pos = next_local;
        Some(LocalOperand::new(temp_slots, ty, local_index, self.in_reg))
    }
}

/// Iterator yielding peeked stack operators.
#[derive(Debug)]
pub struct PeekedOperands<'stack> {
    /// The stack position of the next yielded operand.
    stack_pos: usize,
    /// The iterator of peeked stack operands.
    operands: slice::Iter<'stack, StackOperand>,
    /// Information about the registers that are in use.
    regs: RegisterMap,
}

impl<'stack> PeekedOperands<'stack> {
    /// Creates a [`PeekedOperands`] iterator that yields no operands.
    pub fn empty() -> Self {
        Self {
            stack_pos: 0,
            operands: [].iter(),
            regs: RegisterMap::default(),
        }
    }
}

impl Iterator for PeekedOperands<'_> {
    type Item = Operand;

    fn next(&mut self) -> Option<Self::Item> {
        let operand = self.operands.next().copied()?;
        let stack_pos = StackPos::from(self.stack_pos);
        self.stack_pos += 1;
        let wrapped = self.regs.wrap_operand(stack_pos, operand);
        Some(wrapped)
    }
}

impl ExactSizeIterator for PeekedOperands<'_> {
    fn len(&self) -> usize {
        self.operands.len()
    }
}