Skip to content

Commit e48480c

Browse files
committed
implement translate_end_else
1 parent 4d11544 commit e48480c

File tree

1 file changed

+29
-6
lines changed
  • crates/wasmi/src/engine/translator/func2

1 file changed

+29
-6
lines changed

crates/wasmi/src/engine/translator/func2/mod.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use self::{
1818
ControlFrameBase,
1919
ControlFrameKind,
2020
ElseControlFrame,
21+
ElseReachability,
2122
IfControlFrame,
2223
IfReachability,
2324
LocalIdx,
@@ -482,6 +483,34 @@ impl FuncTranslator {
482483
Ok(())
483484
}
484485

486+
/// Translates the end of a Wasm `else` control frame.
487+
fn translate_end_else(&mut self, frame: ElseControlFrame) -> Result<(), Error> {
488+
debug_assert!(!self.stack.is_control_empty());
489+
let reachability = frame.reachability();
490+
if matches!(
491+
reachability,
492+
ElseReachability::OnlyThen | ElseReachability::OnlyElse
493+
) {
494+
return self.translate_end_if_or_else_only(frame, reachability);
495+
}
496+
let end_of_then_reachable = frame.is_end_of_then_reachable();
497+
let end_of_else_reachable = self.reachable;
498+
let reachable = match (end_of_then_reachable, end_of_else_reachable) {
499+
(false, false) => frame.is_branched_to(),
500+
_ => true,
501+
};
502+
if end_of_else_reachable {
503+
let len_values = frame.len_branch_params(&self.engine);
504+
let consume_fuel_instr: Option<Instr> = frame.consume_fuel_instr();
505+
self.copy_branch_params(usize::from(len_values), consume_fuel_instr)?;
506+
}
507+
self.labels
508+
.pin_label(frame.label(), self.instrs.next_instr())
509+
.unwrap();
510+
self.reachable = reachable;
511+
Ok(())
512+
}
513+
485514
/// Translates the end of a Wasm `else` control frame where only one branch is known to be reachable.
486515
fn translate_end_if_or_else_only(
487516
&mut self,
@@ -505,12 +534,6 @@ impl FuncTranslator {
505534
Ok(())
506535
}
507536

508-
/// Translates the end of a Wasm `else` control frame.
509-
fn translate_end_else(&mut self, _frame: ElseControlFrame) -> Result<(), Error> {
510-
debug_assert!(!self.stack.is_control_empty());
511-
todo!()
512-
}
513-
514537
/// Translates the end of an unreachable Wasm control frame.
515538
fn translate_end_unreachable(&mut self, _frame: ControlFrameKind) -> Result<(), Error> {
516539
debug_assert!(!self.stack.is_control_empty());

0 commit comments

Comments
 (0)