@@ -2,10 +2,9 @@ use core::ops::{BitAnd, BitOr, BitXor, Neg};
22
33use super :: { DefaultRuntime , Stack } ;
44use crate :: {
5- get_label_args,
65 log:: debug,
76 runtime:: { BlockType , LabelFrame } ,
8- CallFrame , Error , ModuleInstance , Result , Store ,
7+ CallFrame , Error , LabelArgs , ModuleInstance , Result , Store ,
98} ;
109use alloc:: vec:: Vec ;
1110use log:: info;
@@ -132,45 +131,51 @@ fn exec_one(
132131 }
133132
134133 If ( args, else_offset, end_offset) => {
135- let end_instr_ptr = cf. instr_ptr + * end_offset;
136-
137- info ! (
138- "if it's true, we'll jump to the next instruction (@{})" ,
139- cf. instr_ptr + 1
140- ) ;
141-
142- if let Some ( else_offset) = else_offset {
143- info ! (
144- "else: {:?} (@{})" ,
145- instrs[ cf. instr_ptr + else_offset] ,
146- cf. instr_ptr + else_offset
147- ) ;
148- } ;
149-
150- info ! ( "end: {:?} (@{})" , instrs[ end_instr_ptr] , end_instr_ptr) ;
151-
152- if stack. values . pop_t :: < i32 > ( ) ? != 0 {
134+ if stack. values . pop_t :: < i32 > ( ) ? == 0 {
135+ if let Some ( else_offset) = else_offset {
136+ log:: info!( "entering else at {}" , cf. instr_ptr + * else_offset) ;
137+ cf. enter_label (
138+ LabelFrame {
139+ instr_ptr : cf. instr_ptr + * else_offset,
140+ end_instr_ptr : cf. instr_ptr + * end_offset,
141+ stack_ptr : stack. values . len ( ) , // - params,
142+ args : crate :: LabelArgs :: new ( * args, module) ?,
143+ ty : BlockType :: Else ,
144+ } ,
145+ & mut stack. values ,
146+ ) ;
147+ cf. instr_ptr += * else_offset;
148+ } else {
149+ log:: info!( "skipping if" ) ;
150+ cf. instr_ptr += * end_offset
151+ }
152+ } else {
153+ log:: info!( "entering then" ) ;
153154 cf. enter_label (
154155 LabelFrame {
155156 instr_ptr : cf. instr_ptr ,
156157 end_instr_ptr : cf. instr_ptr + * end_offset,
157158 stack_ptr : stack. values . len ( ) , // - params,
158- args : get_label_args ( * args, module) ?,
159+ args : LabelArgs :: new ( * args, module) ?,
159160 ty : BlockType :: If ,
160161 } ,
161162 & mut stack. values ,
162163 )
163164 }
164165 }
165166
167+ // Else(_end_offset) => {
168+ // // end the if block
169+ // cf.break_to(0, &mut stack.values)?;
170+ // }
166171 Loop ( args, end_offset) => {
167172 // let params = stack.values.pop_block_params(*args, &module)?;
168173 cf. enter_label (
169174 LabelFrame {
170175 instr_ptr : cf. instr_ptr ,
171176 end_instr_ptr : cf. instr_ptr + * end_offset,
172177 stack_ptr : stack. values . len ( ) , // - params,
173- args : get_label_args ( * args, module) ?,
178+ args : LabelArgs :: new ( * args, module) ?,
174179 ty : BlockType :: Loop ,
175180 } ,
176181 & mut stack. values ,
@@ -183,7 +188,7 @@ fn exec_one(
183188 instr_ptr : cf. instr_ptr ,
184189 end_instr_ptr : cf. instr_ptr + * end_offset,
185190 stack_ptr : stack. values . len ( ) , //- params,
186- args : get_label_args ( * args, module) ?,
191+ args : LabelArgs :: new ( * args, module) ?,
187192 ty : BlockType :: Block ,
188193 } ,
189194 & mut stack. values ,
@@ -210,7 +215,7 @@ fn exec_one(
210215 BrIf ( v) => {
211216 if stack. values . pop_t :: < i32 > ( ) ? > 0 {
212217 cf. break_to ( * v, & mut stack. values ) ?
213- } ;
218+ }
214219 }
215220
216221 Return => match stack. call_stack . is_empty ( ) {
@@ -235,21 +240,22 @@ fn exec_one(
235240 }
236241 }
237242
238- EndBlockFrame => {
239- let blocks = & mut cf. labels ;
240-
241- // remove the label from the label stack
242- let Some ( block) = blocks. pop ( ) else {
243- panic ! ( "end: no label to end, this should have been validated by the parser" ) ;
243+ Else ( end_offset) => {
244+ let Some ( block) = cf. labels . pop ( ) else {
245+ panic ! ( "else: no label to end, this should have been validated by the parser" ) ;
244246 } ;
245247
246248 let res_count = block. args . results ;
247- info ! ( "we want to keep {} values on the stack" , res_count) ;
248- info ! ( "current block stack ptr: {}" , block . stack_ptr ) ;
249- info ! ( "stack: {:?}" , stack . values ) ;
249+ stack. values . truncate_keep ( block . stack_ptr , res_count) ;
250+ cf . instr_ptr += * end_offset ;
251+ }
250252
251- // trim the lable's stack from the stack
252- stack. values . truncate_keep ( block. stack_ptr , res_count)
253+ EndBlockFrame => {
254+ // remove the label from the label stack
255+ let Some ( block) = cf. labels . pop ( ) else {
256+ panic ! ( "end: no label to end, this should have been validated by the parser" ) ;
257+ } ;
258+ stack. values . truncate_keep ( block. stack_ptr , block. args . results )
253259 }
254260
255261 LocalGet ( local_index) => stack. values . push ( cf. get_local ( * local_index as usize ) ) ,
0 commit comments