@@ -21,6 +21,7 @@ use self::{
21
21
ElseReachability ,
22
22
IfControlFrame ,
23
23
IfReachability ,
24
+ ImmediateOperand ,
24
25
LocalIdx ,
25
26
LoopControlFrame ,
26
27
Operand ,
@@ -755,6 +756,46 @@ impl FuncTranslator {
755
756
}
756
757
}
757
758
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
+
758
799
/// Translates a commutative binary Wasm operator to Wasmi bytecode.
759
800
fn translate_binary_commutative < T , R > (
760
801
& mut self ,
@@ -769,11 +810,7 @@ impl FuncTranslator {
769
810
bail_unreachable ! ( self ) ;
770
811
match self . stack . pop2 ( ) {
771
812
( 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)
777
814
}
778
815
( val, Operand :: Immediate ( imm) ) | ( Operand :: Immediate ( imm) , val) => {
779
816
let lhs = self . layout . operand_to_reg ( val) ?;
@@ -818,17 +855,7 @@ impl FuncTranslator {
818
855
bail_unreachable ! ( self ) ;
819
856
match self . stack . pop2 ( ) {
820
857
( 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)
832
859
}
833
860
( lhs, Operand :: Immediate ( rhs) ) => {
834
861
let lhs = self . layout . operand_to_reg ( lhs) ?;
0 commit comments