Skip to content

Commit 4d2d316

Browse files
committed
encode staged in OpEncoder::pin_label
1 parent d544dac commit 4d2d316

File tree

3 files changed

+10
-30
lines changed

3 files changed

+10
-30
lines changed

crates/wasmi/src/engine/translator/func/encoder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,11 @@ impl OpEncoder {
262262
/// # Panics
263263
///
264264
/// If there is a staged [`Op`].
265-
pub fn pin_label(&mut self, lref: LabelRef) {
266-
assert!(self.staged.is_none());
265+
pub fn pin_label(&mut self, lref: LabelRef) -> Result<(), Error> {
266+
self.try_encode_staged()?;
267267
let next_pos = Pos::from(self.ops.next_pos());
268268
self.labels.pin_label(lref, next_pos);
269+
Ok(())
269270
}
270271

271272
/// Pins the [`Label`] at `lref` to the current encoded bytestream position if unpinned.

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

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,6 @@ impl FuncTranslator {
694694
!can_avoid_copies
695695
}
696696

697-
/// Pins the `label` to the next [`Pos<Op>`].
698-
fn pin_label(&mut self, label: LabelRef) {
699-
self.instrs.pin_label(label);
700-
}
701-
702697
/// Convert the [`Operand`] at `depth` into an [`Operand::Temp`] by copying if necessary.
703698
///
704699
/// # Note
@@ -1005,15 +1000,11 @@ impl FuncTranslator {
10051000
}
10061001
self.push_frame_results(&frame)?;
10071002
}
1008-
self.instrs.pin_label(frame.label());
1003+
self.instrs.pin_label(frame.label())?;
10091004
self.reachable |= frame.is_branched_to();
10101005
if self.reachable && self.stack.is_control_empty() {
10111006
self.encode_return(consume_fuel_instr)?;
10121007
}
1013-
if frame.is_branched_to() {
1014-
// No need to reset `last_instr` if there was no branch to the end of a Wasm `block`.
1015-
self.instrs.try_encode_staged()?;
1016-
}
10171008
Ok(())
10181009
}
10191010

@@ -1062,10 +1053,8 @@ impl FuncTranslator {
10621053
self.copy_branch_params(&frame, consume_fuel_instr)?;
10631054
}
10641055
self.push_frame_results(&frame)?;
1065-
self.instrs.pin_label(frame.label());
1056+
self.instrs.pin_label(frame.label())?;
10661057
self.reachable = true;
1067-
// Need to reset `last_instr` since end of `if` is a control flow boundary.
1068-
self.instrs.try_encode_staged()?;
10691058
Ok(())
10701059
}
10711060

@@ -1094,10 +1083,8 @@ impl FuncTranslator {
10941083
self.copy_branch_params(&frame, consume_fuel_instr)?;
10951084
}
10961085
self.push_frame_results(&frame)?;
1097-
self.instrs.pin_label(frame.label());
1086+
self.instrs.pin_label(frame.label())?;
10981087
self.reachable = reachable;
1099-
// Need to reset `last_instr` since end of `else` is a control flow boundary.
1100-
self.instrs.try_encode_staged()?;
11011088
Ok(())
11021089
}
11031090

@@ -1114,13 +1101,8 @@ impl FuncTranslator {
11141101
}
11151102
self.push_frame_results(&frame)?;
11161103
}
1117-
self.instrs.pin_label(frame.label());
1104+
self.instrs.pin_label(frame.label())?;
11181105
self.reachable = end_is_reachable || frame.is_branched_to();
1119-
if frame.is_branched_to() {
1120-
// No need to reset `last_instr` if there was no branch to the
1121-
// end of a Wasm `if` where only `then` or `else` is reachable.
1122-
self.instrs.try_encode_staged()?;
1123-
}
11241106
Ok(())
11251107
}
11261108

crates/wasmi/src/engine/translator/func/visit.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,10 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
123123
let continue_label = self.instrs.new_label();
124124
let consume_fuel = self.stack.consume_fuel_instr();
125125
self.move_operands_to_temp(usize::from(len_params), consume_fuel)?;
126-
self.pin_label(continue_label);
126+
self.instrs.pin_label(continue_label)?;
127127
let consume_fuel = self.instrs.encode_consume_fuel()?;
128128
self.stack
129129
.push_loop(block_ty, continue_label, consume_fuel)?;
130-
// Need to reset `last_instr` because a loop header is a control flow boundary.
131-
self.instrs.try_encode_staged()?;
132130
Ok(())
133131
}
134132

@@ -191,8 +189,7 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
191189
self.encode_br(frame.label())?;
192190
}
193191
// Start of `else` block:
194-
self.instrs.try_encode_staged()?;
195-
self.instrs.pin_label(else_label);
192+
self.instrs.pin_label(else_label)?;
196193
}
197194
let consume_fuel_instr = self.instrs.encode_consume_fuel()?;
198195
self.reachable = frame.is_else_reachable();
@@ -273,7 +270,7 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
273270
self.encode_copies(branch_results, len_branch_params, consume_fuel_instr)?;
274271
}
275272
self.encode_br(label)?;
276-
self.instrs.pin_label(skip_label);
273+
self.instrs.pin_label(skip_label)?;
277274
Ok(())
278275
}
279276

0 commit comments

Comments
 (0)