@@ -3,7 +3,7 @@ use crate::{conversion::convert_blocktype, Result};
33use crate :: conversion:: { convert_heaptype, convert_memarg, convert_valtype} ;
44use alloc:: string:: ToString ;
55use alloc:: { boxed:: Box , format, vec:: Vec } ;
6- use tinywasm_types:: { BlockArgsPacked , Instruction } ;
6+ use tinywasm_types:: Instruction ;
77use wasmparser:: { FuncValidator , FunctionBody , VisitOperator , WasmModuleResources } ;
88
99struct ValidateThenVisit < ' a , T , U > ( T , & ' a mut U ) ;
@@ -65,16 +65,14 @@ macro_rules! define_primitive_operands {
6565 ( $( $name: ident, $instr: expr, $ty: ty) ,* ) => {
6666 $(
6767 fn $name( & mut self , arg: $ty) -> Self :: Output {
68- self . instructions. push( $instr( arg) ) ;
69- Ok ( ( ) )
68+ Ok ( self . instructions. push( $instr( arg) ) )
7069 }
7170 ) *
7271 } ;
7372 ( $( $name: ident, $instr: expr, $ty: ty, $ty2: ty) ,* ) => {
7473 $(
7574 fn $name( & mut self , arg: $ty, arg2: $ty) -> Self :: Output {
76- self . instructions. push( $instr( arg, arg2) ) ;
77- Ok ( ( ) )
75+ Ok ( self . instructions. push( $instr( arg, arg2) ) )
7876 }
7977 ) *
8078 } ;
@@ -112,8 +110,7 @@ impl FunctionBuilder {
112110
113111 #[ inline]
114112 fn visit ( & mut self , op : Instruction ) -> Result < ( ) > {
115- self . instructions . push ( op) ;
116- Ok ( ( ) )
113+ Ok ( self . instructions . push ( op) )
117114 }
118115}
119116
@@ -162,7 +159,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
162159 visit_i64_load16_u, I64Load16U ,
163160 visit_i64_load32_s, I64Load32S ,
164161 visit_i64_load32_u, I64Load32U ,
165- // visit_i32_store, I32Store,
162+ // visit_i32_store, I32Store, custom implementation
166163 visit_i64_store, I64Store ,
167164 visit_f32_store, F32Store ,
168165 visit_f64_store, F64Store ,
@@ -325,15 +322,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
325322 let arg = convert_memarg ( memarg) ;
326323 let i32store = Instruction :: I32Store { offset : arg. offset , mem_addr : arg. mem_addr } ;
327324
328- if self . instructions . len ( ) < 3 {
329- return self . visit ( i32store) ;
330- }
331-
332- #[ cold]
333- fn cold ( ) { }
334-
335- if arg. mem_addr > 0xFF || arg. offset > 0xFFFF_FFFF {
336- cold ( ) ;
325+ if self . instructions . len ( ) < 3 || arg. mem_addr > 0xFF || arg. offset > 0xFFFF_FFFF {
337326 return self . visit ( i32store) ;
338327 }
339328
@@ -353,31 +342,22 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
353342 }
354343
355344 fn visit_local_get ( & mut self , idx : u32 ) -> Self :: Output {
356- if let Some ( instruction) = self . instructions . last_mut ( ) {
357- match instruction {
358- Instruction :: LocalGet ( a) => * instruction = Instruction :: LocalGet2 ( * a, idx) ,
359- Instruction :: LocalGet2 ( a, b) => * instruction = Instruction :: LocalGet3 ( * a, * b, idx) ,
360- Instruction :: LocalTee ( a) => * instruction = Instruction :: LocalTeeGet ( * a, idx) ,
361- _ => return self . visit ( Instruction :: LocalGet ( idx) ) ,
362- } ;
363- Ok ( ( ) )
364- } else {
365- self . visit ( Instruction :: LocalGet ( idx) )
366- }
345+ let Some ( instruction) = self . instructions . last_mut ( ) else {
346+ return self . visit ( Instruction :: LocalGet ( idx) ) ;
347+ } ;
348+
349+ match instruction {
350+ Instruction :: LocalGet ( a) => * instruction = Instruction :: LocalGet2 ( * a, idx) ,
351+ Instruction :: LocalGet2 ( a, b) => * instruction = Instruction :: LocalGet3 ( * a, * b, idx) ,
352+ Instruction :: LocalTee ( a) => * instruction = Instruction :: LocalTeeGet ( * a, idx) ,
353+ _ => return self . visit ( Instruction :: LocalGet ( idx) ) ,
354+ } ;
355+
356+ Ok ( ( ) )
367357 }
368358
369359 fn visit_local_set ( & mut self , idx : u32 ) -> Self :: Output {
370360 self . visit ( Instruction :: LocalSet ( idx) )
371- // if let Some(instruction) = self.instructions.last_mut() {
372- // match instruction {
373- // // Needs more testing, seems to make performance worse
374- // // Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx),
375- // _ => return self.visit(Instruction::LocalSet(idx)),
376- // };
377- // // Ok(())
378- // } else {
379- // self.visit(Instruction::LocalSet(idx))
380- // }
381361 }
382362
383363 fn visit_local_tee ( & mut self , idx : u32 ) -> Self :: Output {
@@ -426,7 +406,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
426406
427407 fn visit_if ( & mut self , ty : wasmparser:: BlockType ) -> Self :: Output {
428408 self . label_ptrs . push ( self . instructions . len ( ) ) ;
429- self . visit ( Instruction :: If ( BlockArgsPacked :: new ( convert_blocktype ( ty) ) , 0 , 0 ) )
409+ self . visit ( Instruction :: If ( convert_blocktype ( ty) . into ( ) , 0 , 0 ) )
430410 }
431411
432412 fn visit_else ( & mut self ) -> Self :: Output {
0 commit comments