@@ -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:: Instruction ;
6+ use tinywasm_types:: { BlockArgsPacked , Instruction } ;
77use wasmparser:: { FuncValidator , FunctionBody , VisitOperator , WasmModuleResources } ;
88
99struct ValidateThenVisit < ' a , T , U > ( T , & ' a mut U ) ;
@@ -74,12 +74,14 @@ macro_rules! define_primitive_operands {
7474}
7575
7676macro_rules! define_mem_operands {
77- ( $( $name: ident, $instr: expr ) ,* ) => {
77+ ( $( $name: ident, $instr: ident ) ,* ) => {
7878 $(
7979 fn $name( & mut self , mem_arg: wasmparser:: MemArg ) -> Self :: Output {
80- self . instructions. push( $instr(
81- convert_memarg( mem_arg)
82- ) ) ;
80+ let arg = convert_memarg( mem_arg) ;
81+ self . instructions. push( Instruction :: $instr {
82+ offset: arg. offset,
83+ mem_addr: arg. mem_addr,
84+ } ) ;
8385 Ok ( ( ) )
8486 }
8587 ) *
@@ -149,29 +151,29 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
149151 }
150152
151153 define_mem_operands ! {
152- visit_i32_load, Instruction :: I32Load ,
153- visit_i64_load, Instruction :: I64Load ,
154- visit_f32_load, Instruction :: F32Load ,
155- visit_f64_load, Instruction :: F64Load ,
156- visit_i32_load8_s, Instruction :: I32Load8S ,
157- visit_i32_load8_u, Instruction :: I32Load8U ,
158- visit_i32_load16_s, Instruction :: I32Load16S ,
159- visit_i32_load16_u, Instruction :: I32Load16U ,
160- visit_i64_load8_s, Instruction :: I64Load8S ,
161- visit_i64_load8_u, Instruction :: I64Load8U ,
162- visit_i64_load16_s, Instruction :: I64Load16S ,
163- visit_i64_load16_u, Instruction :: I64Load16U ,
164- visit_i64_load32_s, Instruction :: I64Load32S ,
165- visit_i64_load32_u, Instruction :: I64Load32U ,
166- visit_i32_store, Instruction :: I32Store ,
167- visit_i64_store, Instruction :: I64Store ,
168- visit_f32_store, Instruction :: F32Store ,
169- visit_f64_store, Instruction :: F64Store ,
170- visit_i32_store8, Instruction :: I32Store8 ,
171- visit_i32_store16, Instruction :: I32Store16 ,
172- visit_i64_store8, Instruction :: I64Store8 ,
173- visit_i64_store16, Instruction :: I64Store16 ,
174- visit_i64_store32, Instruction :: I64Store32
154+ visit_i32_load, I32Load ,
155+ visit_i64_load, I64Load ,
156+ visit_f32_load, F32Load ,
157+ visit_f64_load, F64Load ,
158+ visit_i32_load8_s, I32Load8S ,
159+ visit_i32_load8_u, I32Load8U ,
160+ visit_i32_load16_s, I32Load16S ,
161+ visit_i32_load16_u, I32Load16U ,
162+ visit_i64_load8_s, I64Load8S ,
163+ visit_i64_load8_u, I64Load8U ,
164+ visit_i64_load16_s, I64Load16S ,
165+ visit_i64_load16_u, I64Load16U ,
166+ visit_i64_load32_s, I64Load32S ,
167+ visit_i64_load32_u, I64Load32U ,
168+ visit_i32_store, I32Store ,
169+ visit_i64_store, I64Store ,
170+ visit_f32_store, F32Store ,
171+ visit_f64_store, F64Store ,
172+ visit_i32_store8, I32Store8 ,
173+ visit_i32_store16, I32Store16 ,
174+ visit_i64_store8, I64Store8 ,
175+ visit_i64_store16, I64Store16 ,
176+ visit_i64_store32, I64Store32
175177 }
176178
177179 define_operands ! {
@@ -327,7 +329,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
327329 match instruction {
328330 Instruction :: LocalGet ( a) => * instruction = Instruction :: LocalGet2 ( * a, idx) ,
329331 Instruction :: LocalGet2 ( a, b) => * instruction = Instruction :: LocalGet3 ( * a, * b, idx) ,
330- Instruction :: LocalGet3 ( a, b, c) => * instruction = Instruction :: LocalGet4 ( * a, * b, * c, idx) ,
332+ // Instruction::LocalGet3(a, b, c) => *instruction = Instruction::LocalGet4(*a, *b, *c, idx),
331333 Instruction :: LocalTee ( a) => * instruction = Instruction :: LocalTeeGet ( * a, idx) ,
332334 _ => return self . visit ( Instruction :: LocalGet ( idx) ) ,
333335 } ;
@@ -396,7 +398,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
396398
397399 fn visit_if ( & mut self , ty : wasmparser:: BlockType ) -> Self :: Output {
398400 self . label_ptrs . push ( self . instructions . len ( ) ) ;
399- self . visit ( Instruction :: If ( convert_blocktype ( ty) , None , 0 ) )
401+ self . visit ( Instruction :: If ( BlockArgsPacked :: new ( convert_blocktype ( ty) ) , 0 , 0 ) )
400402 }
401403
402404 fn visit_else ( & mut self ) -> Self :: Output {
@@ -414,7 +416,9 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
414416
415417 match self . instructions [ label_pointer] {
416418 Instruction :: Else ( ref mut else_instr_end_offset) => {
417- * else_instr_end_offset = ( current_instr_ptr - label_pointer as usize ) as u32 ;
419+ * else_instr_end_offset = ( current_instr_ptr - label_pointer as usize )
420+ . try_into ( )
421+ . expect ( "else_instr_end_offset is too large, tinywasm does not support if blocks that large" ) ;
418422
419423 #[ cold]
420424 fn error ( ) -> crate :: ParseError {
@@ -431,13 +435,20 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder {
431435 return Err ( error ( ) ) ;
432436 } ;
433437
434- * else_offset = Some ( ( label_pointer - if_label_pointer) as u32 ) ;
435- * end_offset = ( current_instr_ptr - if_label_pointer) as u32 ;
438+ * else_offset = ( label_pointer - if_label_pointer)
439+ . try_into ( )
440+ . expect ( "else_instr_end_offset is too large, tinywasm does not support blocks that large" ) ;
441+
442+ * end_offset = ( current_instr_ptr - if_label_pointer)
443+ . try_into ( )
444+ . expect ( "else_instr_end_offset is too large, tinywasm does not support blocks that large" ) ;
436445 }
437446 Instruction :: Block ( _, ref mut end_offset)
438447 | Instruction :: Loop ( _, ref mut end_offset)
439448 | Instruction :: If ( _, _, ref mut end_offset) => {
440- * end_offset = ( current_instr_ptr - label_pointer) as u32 ;
449+ * end_offset = ( current_instr_ptr - label_pointer)
450+ . try_into ( )
451+ . expect ( "else_instr_end_offset is too large, tinywasm does not support blocks that large" ) ;
441452 }
442453 _ => {
443454 return Err ( crate :: ParseError :: UnsupportedOperator (
0 commit comments