Browse Source

+ check whether all br instructions, generated by goto point to a pascal goto label

Nikolay Nikolov 1 year ago
parent
commit
3defa34470
3 changed files with 35 additions and 3 deletions
  1. 2 0
      compiler/wasm32/aasmcpu.pas
  2. 28 2
      compiler/wasm32/cpupi.pas
  3. 5 1
      compiler/wasm32/hlcgcpu.pas

+ 2 - 0
compiler/wasm32/aasmcpu.pas

@@ -56,6 +56,8 @@ uses
       { taicpu }
       { taicpu }
 
 
       taicpu = class(tai_cpu_abstract_sym)
       taicpu = class(tai_cpu_abstract_sym)
+         is_br_generated_by_goto: boolean;
+
          constructor op_none(op : tasmop);
          constructor op_none(op : tasmop);
 
 
          constructor op_reg(op : tasmop;_op1 : tregister);
          constructor op_reg(op : tasmop;_op1 : tregister);

+ 28 - 2
compiler/wasm32/cpupi.pas

@@ -59,7 +59,7 @@ interface
       procedure postprocess_code; override;
       procedure postprocess_code; override;
       procedure set_first_temp_offset;override;
       procedure set_first_temp_offset;override;
       procedure add_goto_target(l : tasmlabel);
       procedure add_goto_target(l : tasmlabel);
-      function is_goto_target(l : tasmlabel): Boolean;
+      function is_goto_target(l : tasmsymbol): Boolean;
     end;
     end;
 
 
 implementation
 implementation
@@ -918,10 +918,36 @@ implementation
               end;
               end;
           end;
           end;
 
 
+        procedure check_goto_br_instructions(list: TAsmList);
+          var
+            hp: tai;
+          begin
+            hp:=tai(list.first);
+            while assigned(hp) do
+              begin
+                if (hp.typ=ait_instruction) and (taicpu(hp).is_br_generated_by_goto) then
+                  begin
+                    if (taicpu(hp).opcode<>a_br) or
+                       (taicpu(hp).ops<>1) or
+                       (taicpu(hp).oper[0]^.typ<>top_ref) or
+                       (taicpu(hp).oper[0]^.ref^.offset<>0) or
+                       (taicpu(hp).oper[0]^.ref^.base<>NR_NO) or
+                       (taicpu(hp).oper[0]^.ref^.index<>NR_NO) or
+                       (taicpu(hp).oper[0]^.ref^.symbol=nil) then
+                      internalerror(2023102203);
+                    if not is_goto_target(taicpu(hp).oper[0]^.ref^.symbol) then
+                      internalerror(2023102204);
+                  end;
+                hp:=tai(hp.next);
+              end;
+          end;
+
       var
       var
         localslist: TAsmList;
         localslist: TAsmList;
         labels_resolved: Boolean;
         labels_resolved: Boolean;
       begin
       begin
+        check_goto_br_instructions(aktproccode);
+
         localslist:=prepare_locals;
         localslist:=prepare_locals;
 
 
         replace_local_frame_pointer(aktproccode);
         replace_local_frame_pointer(aktproccode);
@@ -963,7 +989,7 @@ implementation
         FGotoTargets.Add(l.Name,l);
         FGotoTargets.Add(l.Name,l);
       end;
       end;
 
 
-    function tcpuprocinfo.is_goto_target(l: tasmlabel): Boolean;
+    function tcpuprocinfo.is_goto_target(l: tasmsymbol): Boolean;
       begin
       begin
         result:=FGotoTargets.FindIndexOf(l.Name)<>-1;
         result:=FGotoTargets.FindIndexOf(l.Name)<>-1;
       end;
       end;

+ 5 - 1
compiler/wasm32/hlcgcpu.pas

@@ -1934,8 +1934,12 @@ implementation
     end;
     end;
 
 
   procedure thlcgwasm.a_jmp_always_pascal_goto(list: TAsmList; l: tasmlabel);
   procedure thlcgwasm.a_jmp_always_pascal_goto(list: TAsmList; l: tasmlabel);
+    var
+      br_ins: taicpu;
     begin
     begin
-      list.concat(taicpu.op_sym(a_br,l));
+      br_ins:=taicpu.op_sym(a_br,l);
+      br_ins.is_br_generated_by_goto:=true;
+      list.concat(br_ins);
     end;
     end;
 
 
   procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
   procedure thlcgwasm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);