Skip to content

Commit da2a397

Browse files
committed
add tranlate_binary_consteval* helpers
1 parent 1a5727f commit da2a397

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use self::{
2121
ElseReachability,
2222
IfControlFrame,
2323
IfReachability,
24+
ImmediateOperand,
2425
LocalIdx,
2526
LoopControlFrame,
2627
Operand,
@@ -755,6 +756,46 @@ impl FuncTranslator {
755756
}
756757
}
757758

759+
/// Evaluates `consteval(lhs, rhs)` and pushed either its result or tranlates a `trap`.
760+
fn translate_binary_consteval_fallible<T, R>(
761+
&mut self,
762+
lhs: ImmediateOperand,
763+
rhs: ImmediateOperand,
764+
consteval: impl FnOnce(T, T) -> Result<R, TrapCode>,
765+
) -> Result<(), Error>
766+
where
767+
T: From<TypedVal>,
768+
R: Into<TypedVal>,
769+
{
770+
let lhs: T = lhs.val().into();
771+
let rhs: T = rhs.val().into();
772+
match consteval(lhs, rhs) {
773+
Ok(value) => {
774+
self.stack.push_immediate(value)?;
775+
}
776+
Err(trap) => {
777+
self.translate_trap(trap)?;
778+
}
779+
}
780+
Ok(())
781+
}
782+
783+
/// Evaluates `consteval(lhs, rhs)` and pushed either its result or tranlates a `trap`.
784+
fn translate_binary_consteval<T, R>(
785+
&mut self,
786+
lhs: ImmediateOperand,
787+
rhs: ImmediateOperand,
788+
consteval: fn(T, T) -> R,
789+
) -> Result<(), Error>
790+
where
791+
T: From<TypedVal>,
792+
R: Into<TypedVal>,
793+
{
794+
self.translate_binary_consteval_fallible::<T, R>(lhs, rhs, |lhs, rhs| {
795+
Ok(consteval(lhs, rhs))
796+
})
797+
}
798+
758799
/// Translates a commutative binary Wasm operator to Wasmi bytecode.
759800
fn translate_binary_commutative<T, R>(
760801
&mut self,
@@ -769,11 +810,7 @@ impl FuncTranslator {
769810
bail_unreachable!(self);
770811
match self.stack.pop2() {
771812
(Operand::Immediate(lhs), Operand::Immediate(rhs)) => {
772-
let lhs = lhs.val().into();
773-
let rhs = rhs.val().into();
774-
let value = consteval(lhs, rhs);
775-
self.stack.push_immediate(value)?;
776-
Ok(())
813+
self.translate_binary_consteval::<T, R>(lhs, rhs, consteval)
777814
}
778815
(val, Operand::Immediate(imm)) | (Operand::Immediate(imm), val) => {
779816
let lhs = self.layout.operand_to_reg(val)?;
@@ -818,17 +855,7 @@ impl FuncTranslator {
818855
bail_unreachable!(self);
819856
match self.stack.pop2() {
820857
(Operand::Immediate(lhs), Operand::Immediate(rhs)) => {
821-
let lhs = lhs.val().into();
822-
let rhs = rhs.val().into();
823-
match consteval(lhs, rhs) {
824-
Ok(value) => {
825-
self.stack.push_immediate(value)?;
826-
}
827-
Err(trap) => {
828-
self.translate_trap(trap)?;
829-
}
830-
}
831-
Ok(())
858+
self.translate_binary_consteval_fallible::<T, T>(lhs, rhs, consteval)
832859
}
833860
(lhs, Operand::Immediate(rhs)) => {
834861
let lhs = self.layout.operand_to_reg(lhs)?;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use self::{
2222
LoopControlFrame,
2323
},
2424
locals::LocalIdx,
25-
operand::{Operand, TempOperand},
25+
operand::{ImmediateOperand, Operand, TempOperand},
2626
operands::{OperandIdx, PreservedLocalsIter},
2727
};
2828
use super::{Reset, ReusableAllocations};

0 commit comments

Comments
 (0)