Skip to content

Commit 6e0c79f

Browse files
committed
add InstrEncoder::try_replace_instr method
1 parent c3e522d commit 6e0c79f

File tree

1 file changed

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

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ impl InstrEncoder {
107107
Ok(instr)
108108
}
109109

110+
/// Replaces `instr` with `new_instr` in `self`.
111+
///
112+
/// - Returns `Ok(true)` if replacement was successful.
113+
/// - Returns `Ok(false)` if replacement was unsuccessful.
114+
///
115+
/// # Panics (Debug)
116+
///
117+
/// If `instr` or `new_instr` are [`Instruction`] parameters.
118+
pub fn try_replace_instr(
119+
&mut self,
120+
instr: Instr,
121+
new_instr: Instruction,
122+
) -> Result<bool, Error> {
123+
debug_assert!(
124+
!new_instr.is_instruction_parameter(),
125+
"parameter: {new_instr:?}"
126+
);
127+
let Some(last_instr) = self.last_instr else {
128+
return Ok(false);
129+
};
130+
let replace = self.get_mut(instr);
131+
debug_assert!(!replace.is_instruction_parameter(), "parameter: {instr:?}");
132+
if instr != last_instr {
133+
return Ok(false);
134+
}
135+
*replace = new_instr;
136+
Ok(true)
137+
}
138+
110139
/// Pushes an [`Instruction`] parameter to the [`InstrEncoder`].
111140
///
112141
/// The parameter is associated to the last pushed [`Instruction`].

0 commit comments

Comments
 (0)