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
use super::{Operand, Reset};
use crate::{
Engine,
engine::{
BlockType,
translator::func::{Pos, labels::LabelRef},
},
ir,
ir::BoundedSlotSpan,
};
use alloc::vec::{Drain, Vec};
#[cfg(doc)]
use crate::ir::Op;
/// The height of the operand stack upon entering a [`ControlFrame`].
#[derive(Debug, Copy, Clone)]
pub struct StackHeight(u16);
impl From<StackHeight> for usize {
fn from(height: StackHeight) -> Self {
usize::from(height.0)
}
}
impl From<usize> for StackHeight {
fn from(height: usize) -> Self {
let Ok(height) = u16::try_from(height) else {
panic!("out of bounds stack height: {height}")
};
Self(height)
}
}
/// The Wasm control stack.
#[derive(Debug, Default)]
pub struct ControlStack {
/// The stack of control frames.
frames: Vec<ControlFrame>,
/// The current top-most [`Op::ConsumeFuel`] on the stack.
///
/// # Note
///
/// This is meant as cache and optimization to quickly query the top-most
/// fuel consumption instruction since this information is accessed commonly.
///
/// [`Op`]: crate::ir::Op
consume_fuel_instr: Option<Pos<ir::BlockFuel>>,
/// Special operand stack to memorize operands for `else` control frames.
else_operands: ElseOperands,
/// This is `true` if an `if` with else providers was just popped from the stack.
///
/// # Note
///
/// This means that its associated `else` operands need to be taken care of by
/// either pushing back an `else` control frame or by manually popping them off
/// the control stack.
orphaned_else_operands: bool,
}
/// Duplicated operands for Wasm `else` control frames.
#[derive(Debug, Default)]
pub struct ElseOperands {
/// The end indices of each `else` operands.
ends: Vec<usize>,
/// All operands of all allocated `else` control frames.
operands: Vec<Operand>,
}
impl Reset for ElseOperands {
fn reset(&mut self) {
self.ends.clear();
self.operands.clear();
}
}
impl ElseOperands {
/// Pushes operands for a new Wasm `else` control frame.
pub fn push(&mut self, operands: impl IntoIterator<Item = Operand>) {
self.operands.extend(operands);
let end = self.operands.len();
self.ends.push(end);
}
/// Pops the top-most Wasm `else` operands from `self` and returns them.
pub fn pop(&mut self) -> Option<Drain<'_, Operand>> {
let end = self.ends.pop()?;
let start = self.ends.last().copied().unwrap_or(0);
Some(self.operands.drain(start..end))
}
}
impl Reset for ControlStack {
fn reset(&mut self) {
self.frames.clear();
self.else_operands.reset();
self.orphaned_else_operands = false;
}
}
impl ControlStack {
/// Returns the current [`Op::ConsumeFuel`] if fuel metering is enabled.
///
/// Returns `None` otherwise.
#[inline]
pub fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
debug_assert!(!self.is_empty());
self.consume_fuel_instr
}
/// Returns `true` if `self` is empty.
pub fn is_empty(&self) -> bool {
self.height() == 0
}
/// Returns the height of the [`ControlStack`].
pub fn height(&self) -> usize {
self.frames.len()
}
/// Pushes a new unreachable Wasm control frame onto the [`ControlStack`].
pub fn push_unreachable(&mut self, kind: ControlFrameKind) {
debug_assert!(!self.orphaned_else_operands);
self.frames.push(ControlFrame::from(kind))
}
/// Pushes a new Wasm `block` onto the [`ControlStack`].
pub fn push_block(
&mut self,
ty: BlockType,
height: usize,
branch_slots: BoundedSlotSpan,
label: LabelRef,
consume_fuel: Option<Pos<ir::BlockFuel>>,
) {
debug_assert!(!self.orphaned_else_operands);
self.frames.push(ControlFrame::from(BlockControlFrame {
ty,
height: StackHeight::from(height),
branch_slots,
is_branched_to: false,
consume_fuel,
label,
}));
self.consume_fuel_instr = consume_fuel;
}
/// Pushes a new Wasm `loop` onto the [`ControlStack`].
pub fn push_loop(
&mut self,
ty: BlockType,
height: usize,
branch_slots: BoundedSlotSpan,
label: LabelRef,
consume_fuel: Option<Pos<ir::BlockFuel>>,
) {
debug_assert!(!self.orphaned_else_operands);
self.frames.push(ControlFrame::from(LoopControlFrame {
ty,
height: StackHeight::from(height),
branch_slots,
is_branched_to: false,
consume_fuel,
label,
}));
self.consume_fuel_instr = consume_fuel;
}
/// Pushes a new Wasm `if` onto the [`ControlStack`].
#[expect(clippy::too_many_arguments)]
pub fn push_if(
&mut self,
ty: BlockType,
height: usize,
branch_slots: BoundedSlotSpan,
label: LabelRef,
consume_fuel: Option<Pos<ir::BlockFuel>>,
reachability: IfReachability,
else_operands: impl IntoIterator<Item = Operand>,
) {
debug_assert!(!self.orphaned_else_operands);
self.frames.push(ControlFrame::from(IfControlFrame {
ty,
height: StackHeight::from(height),
branch_slots,
is_branched_to: false,
consume_fuel,
label,
reachability,
}));
if matches!(reachability, IfReachability::Both { .. }) {
self.else_operands.push(else_operands);
}
self.consume_fuel_instr = consume_fuel;
}
/// Pushes a new Wasm `else` onto the [`ControlStack`].
///
/// Returns iterator yielding the memorized `else` operands.
pub fn push_else(
&mut self,
if_frame: IfControlFrame,
consume_fuel: Option<Pos<ir::BlockFuel>>,
is_end_of_then_reachable: bool,
) {
debug_assert!(!self.orphaned_else_operands);
let ty = if_frame.ty();
let height = if_frame.height();
let branch_slots = if_frame.branch_slots;
let label = if_frame.label();
let is_branched_to = if_frame.is_branched_to();
let reachability = match if_frame.reachability {
IfReachability::Both { .. } => ElseReachability::Both {
is_end_of_then_reachable,
},
IfReachability::OnlyThen => ElseReachability::OnlyThen {
is_end_of_then_reachable,
},
IfReachability::OnlyElse => ElseReachability::OnlyElse,
};
self.frames.push(ControlFrame::from(ElseControlFrame {
ty,
height: StackHeight::from(height),
branch_slots,
is_branched_to,
consume_fuel,
label,
reachability,
}));
self.consume_fuel_instr = consume_fuel;
}
/// Pops the top-most [`ControlFrame`] and returns it if any.
pub fn pop(&mut self) -> Option<ControlFrame> {
debug_assert!(!self.orphaned_else_operands);
let frame = self.frames.pop()?;
if !matches!(frame, ControlFrame::Block(_) | ControlFrame::Unreachable(_)) {
// Need to replace the cached top-most `consume_fuel_instr`.
self.consume_fuel_instr = self.get(0).consume_fuel_instr();
}
self.orphaned_else_operands = match &frame {
ControlFrame::If(frame) => {
matches!(frame.reachability, IfReachability::Both { .. })
}
_ => false,
};
Some(frame)
}
/// Pops the top-most `else` operands from the control stack.
///
/// # Panics (Debug)
///
/// If the `else` operands are not in orphaned state.
pub fn pop_else_operands(&mut self) -> Drain<'_, Operand> {
debug_assert!(self.orphaned_else_operands);
let Some(else_operands) = self.else_operands.pop() else {
panic!("missing `else` operands")
};
self.orphaned_else_operands = false;
else_operands
}
/// Returns a shared reference to the [`ControlFrame`] at `depth` if any.
pub fn get(&self, depth: usize) -> &ControlFrame {
let height = self.height();
self.frames.iter().rev().nth(depth).unwrap_or_else(|| {
panic!(
"out of bounds control frame at depth (={depth}) for stack of height (={height})"
)
})
}
/// Returns an exclusive reference to the [`ControlFrame`] at `depth` if any.
pub fn get_mut(&mut self, depth: usize) -> ControlFrameMut<'_> {
let height = self.height();
self.frames
.iter_mut()
.rev()
.nth(depth)
.map(ControlFrameMut)
.unwrap_or_else(|| {
panic!(
"out of bounds control frame at depth (={depth}) for stack of height (={height})"
)
})
}
}
/// An exclusive reference to a [`ControlFrame`].
#[derive(Debug)]
pub struct ControlFrameMut<'a>(&'a mut ControlFrame);
impl<'a> ControlFrameBase for ControlFrameMut<'a> {
fn ty(&self) -> BlockType {
self.0.ty()
}
fn height(&self) -> usize {
self.0.height()
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
self.0.branch_slots()
}
fn label(&self) -> LabelRef {
self.0.label()
}
fn is_branched_to(&self) -> bool {
self.0.is_branched_to()
}
fn branch_to(&mut self) {
self.0.branch_to()
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
self.0.len_branch_params(engine)
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
self.0.consume_fuel_instr()
}
}
/// An acquired branch target.
#[derive(Debug)]
pub enum AcquiredTarget<'stack> {
/// The branch targets the function enclosing `block` and therefore is a `return`.
Return(ControlFrameMut<'stack>),
/// The branch targets a regular [`ControlFrame`].
Branch(ControlFrameMut<'stack>),
}
impl<'stack> AcquiredTarget<'stack> {
/// Returns an exclusive reference to the [`ControlFrame`] of the [`AcquiredTarget`].
pub fn control_frame(self) -> ControlFrameMut<'stack> {
match self {
Self::Return(frame) => frame,
Self::Branch(frame) => frame,
}
}
}
impl ControlStack {
/// Acquires the target [`ControlFrame`] at the given relative `depth`.
pub fn acquire_target(&mut self, depth: usize) -> AcquiredTarget<'_> {
let is_root = self.is_root(depth);
let frame = self.get_mut(depth);
if is_root {
AcquiredTarget::Return(frame)
} else {
AcquiredTarget::Branch(frame)
}
}
/// Returns `true` if `depth` points to the first control flow frame.
fn is_root(&self, depth: usize) -> bool {
if self.frames.is_empty() {
return false;
}
depth == self.height() - 1
}
}
/// A Wasm control frame.
#[derive(Debug)]
pub enum ControlFrame {
/// A Wasm `block` control frame.
Block(BlockControlFrame),
/// A Wasm `loop` control frame.
Loop(LoopControlFrame),
/// A Wasm `if` control frame.
If(IfControlFrame),
/// A Wasm `else` control frame.
Else(ElseControlFrame),
/// A generic unreachable control frame.
Unreachable(ControlFrameKind),
}
impl From<BlockControlFrame> for ControlFrame {
fn from(frame: BlockControlFrame) -> Self {
Self::Block(frame)
}
}
impl From<LoopControlFrame> for ControlFrame {
fn from(frame: LoopControlFrame) -> Self {
Self::Loop(frame)
}
}
impl From<IfControlFrame> for ControlFrame {
fn from(frame: IfControlFrame) -> Self {
Self::If(frame)
}
}
impl From<ElseControlFrame> for ControlFrame {
fn from(frame: ElseControlFrame) -> Self {
Self::Else(frame)
}
}
impl From<ControlFrameKind> for ControlFrame {
fn from(frame: ControlFrameKind) -> Self {
Self::Unreachable(frame)
}
}
/// Trait implemented by control frame types that share a common API.
pub trait ControlFrameBase {
/// Returns the [`BlockType`] of the control frame.
fn ty(&self) -> BlockType;
/// Returns the height of the control frame.
fn height(&self) -> usize;
/// Returns the branch slots of the control frame as [`BoundedSlotSpan`].
///
/// Returns `None` if no values need to be copied for branching.
fn branch_slots(&self) -> Option<BoundedSlotSpan>;
/// Returns the branch label of `self`.
fn label(&self) -> LabelRef;
/// Returns `true` if there exists a branch to `self.`
fn is_branched_to(&self) -> bool;
/// Makes `self` aware that there is a branch to it.
fn branch_to(&mut self);
/// Returns the number of operands required for branching to `self`.
// TODO: remove in favor of `branch_slots`
fn len_branch_params(&self, engine: &Engine) -> u16;
/// Returns a reference to the [`Op::ConsumeFuel`] of `self`.
///
/// Returns `None` if fuel metering is disabled.
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>>;
}
impl ControlFrameBase for ControlFrame {
fn ty(&self) -> BlockType {
match self {
ControlFrame::Block(frame) => frame.ty(),
ControlFrame::Loop(frame) => frame.ty(),
ControlFrame::If(frame) => frame.ty(),
ControlFrame::Else(frame) => frame.ty(),
ControlFrame::Unreachable(_) => {
panic!("invalid query for unreachable control frame: `ControlFrameBase::ty`")
}
}
}
fn height(&self) -> usize {
match self {
ControlFrame::Block(frame) => frame.height(),
ControlFrame::Loop(frame) => frame.height(),
ControlFrame::If(frame) => frame.height(),
ControlFrame::Else(frame) => frame.height(),
ControlFrame::Unreachable(_) => {
panic!("invalid query for unreachable control frame: `ControlFrameBase::height`")
}
}
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
match self {
ControlFrame::Block(frame) => frame.branch_slots(),
ControlFrame::Loop(frame) => frame.branch_slots(),
ControlFrame::If(frame) => frame.branch_slots(),
ControlFrame::Else(frame) => frame.branch_slots(),
ControlFrame::Unreachable(_) => {
panic!(
"invalid query for unreachable control frame: `ControlFrameBase::branch_slots`"
)
}
}
}
fn label(&self) -> LabelRef {
match self {
ControlFrame::Block(frame) => frame.label(),
ControlFrame::Loop(frame) => frame.label(),
ControlFrame::If(frame) => frame.label(),
ControlFrame::Else(frame) => frame.label(),
ControlFrame::Unreachable(_) => {
panic!("invalid query for unreachable control frame: `ControlFrame::label`")
}
}
}
fn is_branched_to(&self) -> bool {
match self {
ControlFrame::Block(frame) => frame.is_branched_to(),
ControlFrame::Loop(frame) => frame.is_branched_to(),
ControlFrame::If(frame) => frame.is_branched_to(),
ControlFrame::Else(frame) => frame.is_branched_to(),
ControlFrame::Unreachable(_) => {
panic!(
"invalid query for unreachable control frame: `ControlFrame::is_branched_to`"
)
}
}
}
fn branch_to(&mut self) {
match self {
ControlFrame::Block(frame) => frame.branch_to(),
ControlFrame::Loop(frame) => frame.branch_to(),
ControlFrame::If(frame) => frame.branch_to(),
ControlFrame::Else(frame) => frame.branch_to(),
ControlFrame::Unreachable(_) => {
panic!("invalid query for unreachable control frame: `ControlFrame::branch_to`")
}
}
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
match self {
ControlFrame::Block(frame) => frame.len_branch_params(engine),
ControlFrame::Loop(frame) => frame.len_branch_params(engine),
ControlFrame::If(frame) => frame.len_branch_params(engine),
ControlFrame::Else(frame) => frame.len_branch_params(engine),
ControlFrame::Unreachable(_) => {
panic!(
"invalid query for unreachable control frame: `ControlFrame::len_branch_params`"
)
}
}
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
match self {
ControlFrame::Block(frame) => frame.consume_fuel_instr(),
ControlFrame::Loop(frame) => frame.consume_fuel_instr(),
ControlFrame::If(frame) => frame.consume_fuel_instr(),
ControlFrame::Else(frame) => frame.consume_fuel_instr(),
ControlFrame::Unreachable(_) => {
panic!(
"invalid query for unreachable control frame: `ControlFrame::consume_fuel_instr`"
)
}
}
}
}
/// A Wasm `block` control frame.
#[derive(Debug)]
pub struct BlockControlFrame {
/// The block type of the [`BlockControlFrame`].
ty: BlockType,
/// The value stack height upon entering the [`BlockControlFrame`].
height: StackHeight,
/// The [`BoundedSlotSpan`] referring to the results of the [`BlockControlFrame`].
branch_slots: BoundedSlotSpan,
/// This is `true` if there is at least one branch to this [`BlockControlFrame`].
is_branched_to: bool,
/// The [`BlockControlFrame`]'s [`Op::ConsumeFuel`] if fuel metering is enabled.
///
/// # Note
///
/// This is `Some` if fuel metering is enabled and `None` otherwise.
consume_fuel: Option<Pos<ir::BlockFuel>>,
/// The label used to branch to the [`BlockControlFrame`].
label: LabelRef,
}
impl ControlFrameBase for BlockControlFrame {
fn ty(&self) -> BlockType {
self.ty
}
fn height(&self) -> usize {
self.height.into()
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
if self.branch_slots.is_empty() {
return None;
}
Some(self.branch_slots)
}
fn label(&self) -> LabelRef {
self.label
}
fn is_branched_to(&self) -> bool {
self.is_branched_to
}
fn branch_to(&mut self) {
self.is_branched_to = true;
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
self.ty.len_results(engine)
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
self.consume_fuel
}
}
/// A Wasm `loop` control frame.
#[derive(Debug)]
pub struct LoopControlFrame {
/// The block type of the [`LoopControlFrame`].
ty: BlockType,
/// The value stack height upon entering the [`LoopControlFrame`].
height: StackHeight,
/// The [`BoundedSlotSpan`] referring to the results of the [`LoopControlFrame`].
branch_slots: BoundedSlotSpan,
/// This is `true` if there is at least one branch to this [`LoopControlFrame`].
is_branched_to: bool,
/// The [`LoopControlFrame`]'s [`Op::ConsumeFuel`] if fuel metering is enabled.
///
/// # Note
///
/// This is `Some` if fuel metering is enabled and `None` otherwise.
consume_fuel: Option<Pos<ir::BlockFuel>>,
/// The label used to branch to the [`LoopControlFrame`].
label: LabelRef,
}
impl ControlFrameBase for LoopControlFrame {
fn ty(&self) -> BlockType {
self.ty
}
fn height(&self) -> usize {
self.height.into()
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
if self.branch_slots.is_empty() {
return None;
}
Some(self.branch_slots)
}
fn label(&self) -> LabelRef {
self.label
}
fn is_branched_to(&self) -> bool {
self.is_branched_to
}
fn branch_to(&mut self) {
self.is_branched_to = true;
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
self.ty.len_params(engine)
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
self.consume_fuel
}
}
/// A Wasm `if` control frame including its `then` part.
#[derive(Debug)]
pub struct IfControlFrame {
/// The block type of the [`IfControlFrame`].
ty: BlockType,
/// The value stack height upon entering the [`IfControlFrame`].
height: StackHeight,
/// The [`BoundedSlotSpan`] referring to the results of the [`IfControlFrame`].
branch_slots: BoundedSlotSpan,
/// This is `true` if there is at least one branch to this [`IfControlFrame`].
is_branched_to: bool,
/// The [`IfControlFrame`]'s [`Op::ConsumeFuel`] if fuel metering is enabled.
///
/// # Note
///
/// This is `Some` if fuel metering is enabled and `None` otherwise.
consume_fuel: Option<Pos<ir::BlockFuel>>,
/// The label used to branch to the [`IfControlFrame`].
label: LabelRef,
/// The reachability of the `then` and `else` blocks.
reachability: IfReachability,
}
impl IfControlFrame {
/// Returns the [`IfReachability`] of the [`IfControlFrame`].
pub fn reachability(&self) -> IfReachability {
self.reachability
}
/// Returns `true` if the `else` branch is reachable.
///
/// # Note
///
/// The `else` branch is unreachable if the `if` condition is a constant `true` value.
pub fn is_else_reachable(&self) -> bool {
match self.reachability {
IfReachability::Both { .. } | IfReachability::OnlyElse => true,
IfReachability::OnlyThen => false,
}
}
}
impl ControlFrameBase for IfControlFrame {
fn ty(&self) -> BlockType {
self.ty
}
fn height(&self) -> usize {
self.height.into()
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
if self.branch_slots.is_empty() {
return None;
}
Some(self.branch_slots)
}
fn label(&self) -> LabelRef {
self.label
}
fn is_branched_to(&self) -> bool {
self.is_branched_to
}
fn branch_to(&mut self) {
self.is_branched_to = true;
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
self.ty.len_results(engine)
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
self.consume_fuel
}
}
/// The reachability of the `if` control flow frame.
#[derive(Debug, Copy, Clone)]
pub enum IfReachability {
/// Both, `then` and `else` blocks of the `if` are reachable.
///
/// # Note
///
/// This variant does not mean that necessarily both `then` and `else`
/// blocks do exist and are non-empty. The `then` block might still be
/// empty and the `then` block might still be missing.
Both { else_label: LabelRef },
/// Only the `then` block of the `if` is reachable.
///
/// # Note
///
/// This case happens only in case the `if` has a `true` constant condition.
OnlyThen,
/// Only the `else` block of the `if` is reachable.
///
/// # Note
///
/// This case happens only in case the `if` has a `false` constant condition.
OnlyElse,
}
/// A Wasm `else` control frame part of Wasm `if`.
#[derive(Debug)]
pub struct ElseControlFrame {
/// The block type of the [`ElseControlFrame`].
ty: BlockType,
/// The value stack height upon entering the [`ElseControlFrame`].
height: StackHeight,
/// The [`BoundedSlotSpan`] referring to the results of the [`ElseControlFrame`].
branch_slots: BoundedSlotSpan,
/// This is `true` if there is at least one branch to this [`ElseControlFrame`].
is_branched_to: bool,
/// The [`LoopControlFrame`]'s [`Op::ConsumeFuel`] if fuel metering is enabled.
///
/// # Note
///
/// This is `Some` if fuel metering is enabled and `None` otherwise.
consume_fuel: Option<Pos<ir::BlockFuel>>,
/// The label used to branch to the [`ElseControlFrame`].
label: LabelRef,
/// The reachability of the `then` and `else` blocks.
reachability: ElseReachability,
}
/// The reachability of the `else` control flow frame.
#[derive(Debug, Copy, Clone)]
pub enum ElseReachability {
/// Both, `then` and `else` blocks of the `if` are reachable.
///
/// # Note
///
/// This variant does not mean that necessarily both `then` and `else`
/// blocks do exist and are non-empty. The `then` block might still be
/// empty and the `then` block might still be missing.
Both {
/// Is `true` if code is reachable when entering the `else` block.
///
/// # Note
///
/// This means that the end of the `then` block was reachable.
is_end_of_then_reachable: bool,
},
/// Only the `then` block of the `if` is reachable.
///
/// # Note
///
/// This case happens only in case the `if` has a `true` constant condition.
OnlyThen {
/// Is `true` if code is reachable when entering the `else` block.
///
/// # Note
///
/// This means that the end of the `then` block was reachable.
is_end_of_then_reachable: bool,
},
/// Only the `else` block of the `if` is reachable.
///
/// # Note
///
/// This case happens only in case the `if` has a `false` constant condition.
OnlyElse,
}
impl ElseControlFrame {
/// Returns the [`ElseReachability`] of the [`ElseReachability`].
pub fn reachability(&self) -> ElseReachability {
self.reachability
}
/// Returns `true` if the end of the `then` branch is reachable.
pub fn is_end_of_then_reachable(&self) -> bool {
match self.reachability {
ElseReachability::Both {
is_end_of_then_reachable,
}
| ElseReachability::OnlyThen {
is_end_of_then_reachable,
} => is_end_of_then_reachable,
ElseReachability::OnlyElse => false,
}
}
}
impl ControlFrameBase for ElseControlFrame {
fn ty(&self) -> BlockType {
self.ty
}
fn height(&self) -> usize {
self.height.into()
}
fn branch_slots(&self) -> Option<BoundedSlotSpan> {
if self.branch_slots.is_empty() {
return None;
}
Some(self.branch_slots)
}
fn label(&self) -> LabelRef {
self.label
}
fn is_branched_to(&self) -> bool {
self.is_branched_to
}
fn branch_to(&mut self) {
self.is_branched_to = true;
}
fn len_branch_params(&self, engine: &Engine) -> u16 {
// TODO: remove in favor of `branch_slots`
self.ty.len_results(engine)
}
fn consume_fuel_instr(&self) -> Option<Pos<ir::BlockFuel>> {
self.consume_fuel
}
}
/// The kind of a Wasm control frame.
#[derive(Debug, Copy, Clone)]
pub enum ControlFrameKind {
/// An Wasm `block` control frame.
Block,
/// An Wasm `loop` control frame.
Loop,
/// An Wasm `if` control frame.
If,
/// An Wasm `else` control frame.
Else,
}