浏览代码

+ added "list" parameter to thlcgjvm.inc/decstack() methods, and
in case of -ar add to the assembler output the height of the
evaluation stack every time it's increased or decreased (to
more easily track missing/wrong inc/decstack() operations)

git-svn-id: branches/jvmbackend@18335 -

Jonas Maebe 14 年之前
父节点
当前提交
e699eb3cc5
共有 4 个文件被更改,包括 47 次插入39 次删除
  1. 36 28
      compiler/jvm/hlcgcpu.pas
  2. 4 4
      compiler/jvm/njvmadd.pas
  3. 5 5
      compiler/jvm/njvmcal.pas
  4. 2 2
      compiler/jvm/njvmmat.pas

+ 36 - 28
compiler/jvm/hlcgcpu.pas

@@ -43,8 +43,8 @@ uses
      public
      public
       constructor create;
       constructor create;
 
 
-      procedure incstack(slots: longint);
-      procedure decstack(slots: longint);
+      procedure incstack(list : TAsmList;slots: longint);
+      procedure decstack(list : TAsmList;slots: longint);
 
 
       procedure a_call_name(list : TAsmList;pd : tprocdef;const s : string; weak: boolean);override;
       procedure a_call_name(list : TAsmList;pd : tprocdef;const s : string; weak: boolean);override;
       procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : string);override;
       procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : string);override;
@@ -159,7 +159,7 @@ uses
 implementation
 implementation
 
 
   uses
   uses
-    verbose,cutils,
+    verbose,cutils,globals,
     defutil,
     defutil,
     aasmtai,aasmcpu,
     aasmtai,aasmcpu,
     symconst,
     symconst,
@@ -179,18 +179,26 @@ implementation
       fmaxevalstackheight:=0;
       fmaxevalstackheight:=0;
     end;
     end;
 
 
-  procedure thlcgjvm.incstack(slots: longint);
+  procedure thlcgjvm.incstack(list: TasmList;slots: longint);
     begin
     begin
+      if slots=0 then
+        exit;
       inc(fevalstackheight,slots);
       inc(fevalstackheight,slots);
       if (fevalstackheight>fmaxevalstackheight) then
       if (fevalstackheight>fmaxevalstackheight) then
         fmaxevalstackheight:=fevalstackheight;
         fmaxevalstackheight:=fevalstackheight;
+      if cs_asm_regalloc in current_settings.globalswitches then
+        list.concat(tai_comment.Create(strpnew('allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
     end;
     end;
 
 
-  procedure thlcgjvm.decstack(slots: longint);
+  procedure thlcgjvm.decstack(list: TAsmList;slots: longint);
     begin
     begin
+      if slots=0 then
+        exit;
       dec(fevalstackheight,slots);
       dec(fevalstackheight,slots);
       if (fevalstackheight<0) then
       if (fevalstackheight<0) then
         internalerror(2010120501);
         internalerror(2010120501);
+      if cs_asm_regalloc in current_settings.globalswitches then
+        list.concat(tai_comment.Create(strpnew('    freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
     end;
     end;
 
 
   procedure thlcgjvm.a_call_name(list: TAsmList; pd: tprocdef; const s: string; weak: boolean);
   procedure thlcgjvm.a_call_name(list: TAsmList; pd: tprocdef; const s: string; weak: boolean);
@@ -239,7 +247,7 @@ implementation
                     else
                     else
                       list.concat(taicpu.op_const(a_ldc2_w,a));
                       list.concat(taicpu.op_const(a_ldc2_w,a));
                   end;
                   end;
-                  incstack(1);
+                  incstack(list,1);
                 end;
                 end;
               else
               else
                 internalerror(2010110702);
                 internalerror(2010110702);
@@ -254,7 +262,7 @@ implementation
         else
         else
           internalerror(2010110703);
           internalerror(2010110703);
       end;
       end;
-      incstack(1);
+      incstack(list,1);
     end;
     end;
 
 
     procedure thlcgjvm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
     procedure thlcgjvm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
@@ -285,7 +293,7 @@ implementation
               list.concat(taicpu.op_none(a_fconst_2))
               list.concat(taicpu.op_none(a_fconst_2))
             else
             else
               list.concat(taicpu.op_single(a_ldc,a));
               list.concat(taicpu.op_single(a_ldc,a));
-            incstack(1);
+            incstack(list,1);
           end;
           end;
         s64real:
         s64real:
           begin
           begin
@@ -295,7 +303,7 @@ implementation
               list.concat(taicpu.op_none(a_dconst_1))
               list.concat(taicpu.op_none(a_dconst_1))
             else
             else
               list.concat(taicpu.op_double(a_ldc2_w,a));
               list.concat(taicpu.op_double(a_ldc2_w,a));
-            incstack(2);
+            incstack(list,2);
           end
           end
         else
         else
           internalerror(2011010501);
           internalerror(2011010501);
@@ -332,7 +340,7 @@ implementation
             list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
             list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
             maybe_adjust_op_result(list,op,size);
             maybe_adjust_op_result(list,op,size);
             if op<>OP_NEG then
             if op<>OP_NEG then
-              decstack(1);
+              decstack(list,1);
           end;
           end;
         OS_64,OS_S64:
         OS_64,OS_S64:
           begin
           begin
@@ -349,7 +357,7 @@ implementation
               internalerror(2010120533);
               internalerror(2010120533);
             list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
             list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
             if op<>OP_NEG then
             if op<>OP_NEG then
-            decstack(2);
+            decstack(list,2);
           end;
           end;
         else
         else
           internalerror(2010120531);
           internalerror(2010120531);
@@ -357,7 +365,7 @@ implementation
       if trunc32 then
       if trunc32 then
         begin
         begin
           list.concat(taicpu.op_none(a_l2i));
           list.concat(taicpu.op_none(a_l2i));
-          decstack(1);
+          decstack(list,1);
         end;
         end;
     end;
     end;
 
 
@@ -437,14 +445,14 @@ implementation
                 OS_S32,OS_32:
                 OS_S32,OS_32:
                   begin
                   begin
                     list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
                     list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
-                    decstack(2);
+                    decstack(list,2);
                   end;
                   end;
                 OS_64,OS_S64:
                 OS_64,OS_S64:
                   begin
                   begin
                     list.concat(taicpu.op_none(a_lcmp));
                     list.concat(taicpu.op_none(a_lcmp));
-                    decstack(3);
+                    decstack(list,3);
                     list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
                     list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
-                    decstack(1);
+                    decstack(list,1);
                   end;
                   end;
                 else
                 else
                   internalerror(2010120538);
                   internalerror(2010120538);
@@ -460,7 +468,7 @@ implementation
                 else
                 else
                   internalerror(2010120537);
                   internalerror(2010120537);
               end;
               end;
-              decstack(2);
+              decstack(list,2);
             end;
             end;
           else
           else
             internalerror(2010120538);
             internalerror(2010120538);
@@ -550,7 +558,7 @@ implementation
                   if dup then
                   if dup then
                     begin
                     begin
                       list.concat(taicpu.op_none(a_dup));
                       list.concat(taicpu.op_none(a_dup));
-                      incstack(1);
+                      incstack(list,1);
                     end;
                     end;
                   { field name/type encoded in symbol, no index/offset }
                   { field name/type encoded in symbol, no index/offset }
                   if not assigned(ref.symbol) or
                   if not assigned(ref.symbol) or
@@ -608,7 +616,7 @@ implementation
           if dup then
           if dup then
             begin
             begin
               list.concat(taicpu.op_none(a_dup2));
               list.concat(taicpu.op_none(a_dup2));
-              incstack(2);
+              incstack(list,2);
             end;
             end;
           result:=2;
           result:=2;
         end;
         end;
@@ -627,7 +635,7 @@ implementation
       extra_slots:=prepare_stack_for_ref(list,ref,false);
       extra_slots:=prepare_stack_for_ref(list,ref,false);
       a_load_const_stack(list,tosize,a,def2regtyp(tosize));
       a_load_const_stack(list,tosize,a,def2regtyp(tosize));
       a_load_stack_ref(list,tosize,ref,extra_slots);
       a_load_stack_ref(list,tosize,ref,extra_slots);
-      decstack(extra_slots);
+      decstack(list,extra_slots);
     end;
     end;
 
 
   procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
   procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
@@ -637,7 +645,7 @@ implementation
       extra_slots:=prepare_stack_for_ref(list,ref,false);
       extra_slots:=prepare_stack_for_ref(list,ref,false);
       a_load_reg_stack(list,fromsize,register);
       a_load_reg_stack(list,fromsize,register);
       a_load_stack_ref(list,tosize,ref,extra_slots);
       a_load_stack_ref(list,tosize,ref,extra_slots);
-      decstack(extra_slots);
+      decstack(list,extra_slots);
     end;
     end;
 
 
   procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
   procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
@@ -884,7 +892,7 @@ implementation
     begin
     begin
       opc:=loadstoreopc(size,false,false,finishandval);
       opc:=loadstoreopc(size,false,false,finishandval);
       list.concat(taicpu.op_reg(opc,reg));
       list.concat(taicpu.op_reg(opc,reg));
-      decstack(1+ord(size.size>4));
+      decstack(list,1+ord(size.size>4));
     end;
     end;
 
 
   procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
   procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
@@ -900,7 +908,7 @@ implementation
         list.concat(taicpu.op_ref(opc,ref))
         list.concat(taicpu.op_ref(opc,ref))
       else
       else
         list.concat(taicpu.op_none(opc));
         list.concat(taicpu.op_none(opc));
-      decstack(1+ord(size.size>4)+extra_slots);
+      decstack(list,1+ord(size.size>4)+extra_slots);
     end;
     end;
 
 
   procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
   procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
@@ -912,7 +920,7 @@ implementation
       list.concat(taicpu.op_reg(opc,reg));
       list.concat(taicpu.op_reg(opc,reg));
       if finishandval<>-1 then
       if finishandval<>-1 then
         a_op_const_stack(list,OP_AND,size,finishandval);
         a_op_const_stack(list,OP_AND,size,finishandval);
-      incstack(1+ord(size.size>4));
+      incstack(list,1+ord(size.size>4));
     end;
     end;
 
 
   procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
   procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
@@ -930,7 +938,7 @@ implementation
         list.concat(taicpu.op_none(opc));
         list.concat(taicpu.op_none(opc));
       if finishandval<>-1 then
       if finishandval<>-1 then
         a_op_const_stack(list,OP_AND,size,finishandval);
         a_op_const_stack(list,OP_AND,size,finishandval);
-      incstack(1+ord(size.size>4)-extra_slots);
+      incstack(list,1+ord(size.size>4)-extra_slots);
     end;
     end;
 
 
   function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
   function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
@@ -1074,14 +1082,14 @@ implementation
             begin
             begin
               { truncate }
               { truncate }
               list.concat(taicpu.op_none(a_l2i));
               list.concat(taicpu.op_none(a_l2i));
-              decstack(1);
+              decstack(list,1);
             end;
             end;
         end
         end
       else if tosize in [OS_S64,OS_64] then
       else if tosize in [OS_S64,OS_64] then
         begin
         begin
           { extend }
           { extend }
           list.concat(taicpu.op_none(a_i2l));
           list.concat(taicpu.op_none(a_i2l));
-          incstack(1);
+          incstack(list,1);
           { if it was an unsigned 32 bit value, remove sign extension }
           { if it was an unsigned 32 bit value, remove sign extension }
           if fromsize=OS_32 then
           if fromsize=OS_32 then
             a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
             a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
@@ -1117,13 +1125,13 @@ implementation
          (tosize=OS_F64) then
          (tosize=OS_F64) then
         begin
         begin
           list.concat(taicpu.op_none(a_f2d));
           list.concat(taicpu.op_none(a_f2d));
-          incstack(1);
+          incstack(list,1);
         end
         end
       else if (fromsize=OS_F64) and
       else if (fromsize=OS_F64) and
               (tosize=OS_F32) then
               (tosize=OS_F32) then
         begin
         begin
           list.concat(taicpu.op_none(a_d2f));
           list.concat(taicpu.op_none(a_d2f));
-          decstack(1);
+          decstack(list,1);
         end;
         end;
     end;
     end;
 
 

+ 4 - 4
compiler/jvm/njvmadd.pas

@@ -159,7 +159,7 @@ interface
         end;
         end;
 
 
         current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
         current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
-        thlcgjvm(hlcg).decstack(1+ord(location.size=OS_F64));
+        thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1+ord(location.size=OS_F64));
         { could be optimized in the future by keeping the results on the stack,
         { could be optimized in the future by keeping the results on the stack,
           if we add code to swap the operands when necessary (a_swap for
           if we add code to swap the operands when necessary (a_swap for
           singles, store/load/load for doubles since there is no swap for
           singles, store/load/load for doubles since there is no swap for
@@ -193,10 +193,10 @@ interface
         else
         else
           op:=a_fcmpl;
           op:=a_fcmpl;
         current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
         current_asmdata.CurrAsmList.concat(taicpu.op_none(op));
-        thlcgjvm(hlcg).decstack((1+ord(left.location.size=OS_F64))*2-1);
+        thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,(1+ord(left.location.size=OS_F64))*2-1);
 
 
         current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpnode2signedtopcmp],current_procinfo.CurrTrueLabel));
         current_asmdata.CurrAsmList.concat(taicpu.op_sym(opcmp2if[cmpnode2signedtopcmp],current_procinfo.CurrTrueLabel));
-        thlcgjvm(hlcg).decstack(1);
+        thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
         hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
         hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
       end;
       end;
 
 
@@ -230,7 +230,7 @@ interface
               thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
               thlcgjvm(hlcg).a_op_reg_stack(current_asmdata.CurrAsmList,OP_NOT,left.resultdef,NR_NO);
               thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,OP_AND,right.resultdef,right.location);
               thlcgjvm(hlcg).a_op_loc_stack(current_asmdata.CurrAsmList,OP_AND,right.resultdef,right.location);
               current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ifeq,current_procinfo.CurrTrueLabel));
               current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ifeq,current_procinfo.CurrTrueLabel));
-              thlcgjvm(hlcg).decstack(1);
+              thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
               hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
               hlcg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
             end;
             end;
           else
           else

+ 5 - 5
compiler/jvm/njvmcal.pas

@@ -73,7 +73,7 @@ implementation
           self pointer on the evaluation stack for use as function result
           self pointer on the evaluation stack for use as function result
           after the constructor has run }
           after the constructor has run }
         current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
         current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
-        thlcgjvm(hlcg).incstack(2);
+        thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
       end;
       end;
 
 
 
 
@@ -95,12 +95,12 @@ implementation
           1..4:
           1..4:
             begin
             begin
               current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop));
               current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop));
-              thlcgjvm(hlcg).decstack(1);
+              thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
             end;
             end;
           8:
           8:
             begin
             begin
               current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop2));
               current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop2));
-              thlcgjvm(hlcg).decstack(2);
+              thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
             end
             end
           else
           else
             internalerror(2011010305);
             internalerror(2011010305);
@@ -125,9 +125,9 @@ implementation
         { remove parameters from internal evaluation stack counter (in case of
         { remove parameters from internal evaluation stack counter (in case of
           e.g. no parameters and a result, it can also increase) }
           e.g. no parameters and a result, it can also increase) }
         if totalremovesize>0 then
         if totalremovesize>0 then
-          thlcgjvm(hlcg).decstack(totalremovesize shr 2)
+          thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,totalremovesize shr 2)
         else if totalremovesize<0 then
         else if totalremovesize<0 then
-          thlcgjvm(hlcg).incstack((-totalremovesize) shr 2);
+          thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,(-totalremovesize) shr 2);
       end;
       end;
 
 
 
 

+ 2 - 2
compiler/jvm/njvmmat.pas

@@ -112,12 +112,12 @@ implementation
                (torddef(resultdef).ordtype=s64bit) then
                (torddef(resultdef).ordtype=s64bit) then
               begin
               begin
                 current_asmdata.CurrAsmList.concat(taicpu.op_none(a_lrem));
                 current_asmdata.CurrAsmList.concat(taicpu.op_none(a_lrem));
-                thlcgjvm(hlcg).decstack(2);
+                thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
               end
               end
             else
             else
               begin
               begin
                 current_asmdata.CurrAsmList.concat(taicpu.op_none(a_irem));
                 current_asmdata.CurrAsmList.concat(taicpu.op_none(a_irem));
-                thlcgjvm(hlcg).decstack(1);
+                thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
               end;
               end;
             if isu32int then
             if isu32int then
               thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,OS_S64,OS_32,false);
               thlcgjvm(hlcg).resize_stack_int_val(current_asmdata.CurrAsmList,OS_S64,OS_32,false);