11use super :: { DefaultRuntime , Stack } ;
22use crate :: {
33 log:: debug,
4- runtime:: { BlockFrame , BlockFrameInner , RawWasmValue } ,
4+ runtime:: { BlockType , LabelFrame , RawWasmValue } ,
55 CallFrame , Error , ModuleInstance , Result , Store ,
66} ;
77use alloc:: vec:: Vec ;
8+ use log:: info;
89use tinywasm_types:: { BlockArgs , Instruction } ;
910
1011mod macros;
@@ -64,6 +65,9 @@ enum ExecResult {
6465 Trap ( crate :: Trap ) ,
6566}
6667
68+ /// Run a single step of the interpreter
69+ /// A seperate function is used so later, we can more easily implement
70+ /// a step-by-step debugger (using generators once they're stable?)
6771#[ inline]
6872fn exec_one (
6973 cf : & mut CallFrame ,
@@ -74,6 +78,9 @@ fn exec_one(
7478 module : & ModuleInstance ,
7579) -> Result < ExecResult > {
7680 use tinywasm_types:: Instruction :: * ;
81+
82+ info ! ( "ptr: {} instr: {:?}" , cf. instr_ptr, instr) ;
83+
7784 match instr {
7885 Nop => { /* do nothing */ }
7986 Unreachable => return Ok ( ExecResult :: Trap ( crate :: Trap :: Unreachable ) ) , // we don't need to include the call frame here because it's already on the stack
@@ -117,23 +124,23 @@ fn exec_one(
117124 }
118125
119126 Loop ( args, end_offset) => {
120- cf. block_frames . push ( BlockFrame {
127+ cf. labels . push ( LabelFrame {
121128 instr_ptr : cf. instr_ptr ,
122129 end_instr_ptr : cf. instr_ptr + * end_offset,
123130 stack_ptr : stack. values . len ( ) ,
124131 args : * args,
125- block : BlockFrameInner :: Loop ,
132+ ty : BlockType :: Loop ,
126133 } ) ;
127134 stack. values . block_args ( * args) ?;
128135 }
129136
130137 Block ( args, end_offset) => {
131- cf. block_frames . push ( BlockFrame {
138+ cf. labels . push ( LabelFrame {
132139 instr_ptr : cf. instr_ptr ,
133140 end_instr_ptr : cf. instr_ptr + * end_offset,
134141 stack_ptr : stack. values . len ( ) ,
135142 args : * args,
136- block : BlockFrameInner :: Block ,
143+ ty : BlockType :: Block ,
137144 } ) ;
138145 stack. values . block_args ( * args) ?;
139146 }
@@ -163,7 +170,7 @@ fn exec_one(
163170 }
164171
165172 EndFunc => {
166- if cf. block_frames . len ( ) > 0 {
173+ if cf. labels . len ( ) > 0 {
167174 panic ! ( "endfunc: block frames not empty, this should have been validated by the parser" ) ;
168175 }
169176
@@ -178,44 +185,24 @@ fn exec_one(
178185 }
179186
180187 EndBlockFrame => {
181- let blocks = & mut cf. block_frames ;
188+ let blocks = & mut cf. labels ;
189+
190+ // remove the label from the label stack
182191 let Some ( block) = blocks. pop ( ) else {
183- panic ! ( "end: no block to end, this should have been validated by the parser" ) ;
192+ panic ! ( "end: no label to end, this should have been validated by the parser" ) ;
184193 } ;
185194
186- debug ! ( "end, blocks: {:?}" , blocks) ;
187- debug ! ( " instr_ptr: {}" , cf. instr_ptr) ;
188-
189195 let res: & [ RawWasmValue ] = match block. args {
190196 BlockArgs :: Empty => & [ ] ,
191197 BlockArgs :: Type ( _t) => todo ! ( ) ,
192198 BlockArgs :: FuncType ( _t) => todo ! ( ) ,
193199 } ;
194200
195- match block. block {
196- BlockFrameInner :: Loop => {
197- debug ! ( "end(loop): continue loop" ) ;
201+ // trim the lable's stack from the stack
202+ stack. values . trim ( block. stack_ptr ) ;
198203
199- // remove the loop values from the stack
200- stack. values . trim ( block. stack_ptr ) ;
201-
202- // set the instruction pointer to the start of the loop
203- cf. instr_ptr = block. instr_ptr ;
204-
205- // push the loop back onto the stack
206- blocks. push ( block) ;
207- }
208- BlockFrameInner :: Block => {
209- // remove the block values from the stack
210- stack. values . trim ( block. stack_ptr ) ;
211-
212- // push the block result values to the stack
213- stack. values . extend ( res. iter ( ) . copied ( ) ) ;
214- }
215- _ => {
216- panic ! ( "end: unimplemented block type end: {:?}" , block. block) ;
217- }
218- }
204+ // push the block result values to the stack
205+ stack. values . extend ( res. iter ( ) . copied ( ) ) ;
219206 }
220207
221208 LocalGet ( local_index) => {
@@ -244,17 +231,16 @@ fn exec_one(
244231 F32Sub => sub_instr ! ( f32 , stack) ,
245232 F64Sub => sub_instr ! ( f64 , stack) ,
246233
247- I32LtS => {
248- let [ b, a] = stack. values . pop_n_const :: < 2 > ( ) ?;
249- let a: i32 = a. into ( ) ;
250- let b: i32 = b. into ( ) ;
251- stack. values . push ( ( ( a < b) as i32 ) . into ( ) ) ;
252- debug ! ( "i32.lt_s: {} < {} = {}" , a, b, a < b) ;
253- }
234+ I32LtS => lts_instr ! ( i32 , stack) ,
254235 I64LtS => lts_instr ! ( i64 , stack) ,
255236 F32Lt => lts_instr ! ( f32 , stack) ,
256237 F64Lt => lts_instr ! ( f64 , stack) ,
257238
239+ I32GeS => ges_instr ! ( i32 , stack) ,
240+ I64GeS => ges_instr ! ( i64 , stack) ,
241+ F32Ge => ges_instr ! ( f32 , stack) ,
242+ F64Ge => ges_instr ! ( f64 , stack) ,
243+
258244 I32DivS => div_instr ! ( i32 , stack) ,
259245 I64DivS => div_instr ! ( i64 , stack) ,
260246 F32Div => div_instr ! ( f32 , stack) ,
0 commit comments