Browse Source

* fixed the break and continue br depth calculation

git-svn-id: branches/wasm@48032 -
nickysn 4 years ago
parent
commit
aa102564c5
2 changed files with 18 additions and 7 deletions
  1. 3 2
      compiler/wasm32/hlcgcpu.pas
  2. 15 5
      compiler/wasm32/nwasmflw.pas

+ 3 - 2
compiler/wasm32/hlcgcpu.pas

@@ -46,6 +46,7 @@ uses
       loopContBr: integer; // the value is different depending of the condition test
       loopContBr: integer; // the value is different depending of the condition test
                            // if it's in the beggning the jump should be done to the loop (1)
                            // if it's in the beggning the jump should be done to the loop (1)
                            // if the condition at the end, the jump should done to the end of block (0)
                            // if the condition at the end, the jump should done to the end of block (0)
+      loopBreakBr: integer;
       fntypelookup : TWasmProcTypeLookup;
       fntypelookup : TWasmProcTypeLookup;
 
 
       constructor create;
       constructor create;
@@ -1394,9 +1395,9 @@ implementation
       //list.concat(taicpu.op_const(a_i32_const, 0));
       //list.concat(taicpu.op_const(a_i32_const, 0));
       if l = current_procinfo.CurrBreakLabel then begin
       if l = current_procinfo.CurrBreakLabel then begin
         // todo: this should be moved to node generator pass2
         // todo: this should be moved to node generator pass2
-        list.concat(taicpu.op_const(a_br,2+br_blocks))
+        list.concat(taicpu.op_const(a_br,br_blocks-loopBreakBr))
       end else if l = current_procinfo.CurrContinueLabel then begin
       end else if l = current_procinfo.CurrContinueLabel then begin
-        list.concat(taicpu.op_const(a_br,loopContBr+br_blocks))
+        list.concat(taicpu.op_const(a_br,br_blocks-loopContBr))
       end else begin
       end else begin
         Internalerror(2019091806); // unexpected jump
         Internalerror(2019091806); // unexpected jump
       end;
       end;

+ 15 - 5
compiler/wasm32/nwasmflw.pas

@@ -81,7 +81,8 @@ var
    oldclabel,oldblabel : tasmlabel;
    oldclabel,oldblabel : tasmlabel;
    truelabel,falselabel : tasmlabel;
    truelabel,falselabel : tasmlabel;
    oldflowcontrol : tflowcontrol;
    oldflowcontrol : tflowcontrol;
-   oldloopbroffset: Integer;
+   oldloopcontbroffset: Integer;
+   oldloopbreakbroffset: Integer;
 begin
 begin
   location_reset(location,LOC_VOID,OS_NO);
   location_reset(location,LOC_VOID,OS_NO);
 
 
@@ -91,7 +92,8 @@ begin
 
 
   oldflowcontrol:=flowcontrol;
   oldflowcontrol:=flowcontrol;
 
 
-  oldloopbroffset:=thlcgwasm(hlcg).loopContBr;
+  oldloopcontbroffset:=thlcgwasm(hlcg).loopContBr;
+  oldloopbreakbroffset:=thlcgwasm(hlcg).loopBreakBr;
   oldclabel:=current_procinfo.CurrContinueLabel;
   oldclabel:=current_procinfo.CurrContinueLabel;
   oldblabel:=current_procinfo.CurrBreakLabel;
   oldblabel:=current_procinfo.CurrBreakLabel;
 
 
@@ -99,16 +101,20 @@ begin
   exclude(flowcontrol,fc_unwind_loop);
   exclude(flowcontrol,fc_unwind_loop);
 
 
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
+  thlcgwasm(hlcg).incblock;
+  thlcgwasm(hlcg).loopBreakBr:=thlcgwasm(hlcg).br_blocks;
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_loop));
+  thlcgwasm(hlcg).incblock;
 
 
   if lnf_testatbegin in loopflags then
   if lnf_testatbegin in loopflags then
   begin
   begin
     pass_generate_code_condition;
     pass_generate_code_condition;
-    thlcgwasm(hlcg).loopContBr:=1;
+    thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks;
   end else
   end else
-    thlcgwasm(hlcg).loopContBr:=0;
+    thlcgwasm(hlcg).loopContBr:=thlcgwasm(hlcg).br_blocks+1;
 
 
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_block));
+  thlcgwasm(hlcg).incblock;
 
 
   current_procinfo.CurrContinueLabel:=lcont;
   current_procinfo.CurrContinueLabel:=lcont;
   current_procinfo.CurrBreakLabel:=lbreak;
   current_procinfo.CurrBreakLabel:=lbreak;
@@ -119,17 +125,21 @@ begin
     current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
     current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,1) ); // jump back to the external loop
 
 
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
+  thlcgwasm(hlcg).decblock;
   if not (lnf_testatbegin in loopflags) then begin
   if not (lnf_testatbegin in loopflags) then begin
     pass_generate_code_condition;
     pass_generate_code_condition;
   end;
   end;
   current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
   current_asmdata.CurrAsmList.concat(taicpu.op_const(a_br,0) ); // jump back to loop
 
 
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_loop));
+  thlcgwasm(hlcg).decblock;
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
   current_asmdata.CurrAsmList.concat(taicpu.op_none(a_end_block));
+  thlcgwasm(hlcg).decblock;
 
 
   current_procinfo.CurrContinueLabel:=oldclabel;
   current_procinfo.CurrContinueLabel:=oldclabel;
   current_procinfo.CurrBreakLabel:=oldblabel;
   current_procinfo.CurrBreakLabel:=oldblabel;
-  thlcgwasm(hlcg).loopContBr:=oldloopbroffset;
+  thlcgwasm(hlcg).loopContBr:=oldloopcontbroffset;
+  thlcgwasm(hlcg).loopBreakBr:=oldloopbreakbroffset;
 
 
   { a break/continue in a while/repeat block can't be seen outside }
   { a break/continue in a while/repeat block can't be seen outside }
   flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);
   flowcontrol:=oldflowcontrol+(flowcontrol-[fc_break,fc_continue,fc_inflowcontrol]);