瀏覽代碼

* fixed stackalloc to not allocate localst.datasize twice
* order of stackalloc code fixed for implicit init/final

peter 22 年之前
父節點
當前提交
d113495cf1
共有 6 個文件被更改,包括 152 次插入114 次删除
  1. 14 3
      compiler/cgbase.pas
  2. 84 75
      compiler/ncgutil.pas
  3. 18 2
      compiler/pmodules.pas
  4. 25 17
      compiler/psub.pas
  5. 6 2
      compiler/sparc/cpupi.pas
  6. 5 15
      compiler/tgobj.pas

+ 14 - 3
compiler/cgbase.pas

@@ -198,7 +198,7 @@ unit cgbase;
 implementation
 implementation
 
 
      uses
      uses
-        systems,
+        cutils,systems,
         cresstr,
         cresstr,
         tgobj,rgobj,
         tgobj,rgobj,
         defutil,
         defutil,
@@ -361,8 +361,15 @@ implementation
 
 
 
 
     function tprocinfo.calc_stackframe_size:longint;
     function tprocinfo.calc_stackframe_size:longint;
+      var
+        _align : longint;
       begin
       begin
-        result:=procdef.localst.datasize+tg.gettempsize;
+        { align to 4 bytes at least
+          otherwise all those subl $2,%esp are meaningless PM }
+        _align:=target_info.alignment.localalignmin;
+        if _align<4 then
+          _align:=4;
+        result:=Align(tg.direction*tg.lasttemp,_align);
       end;
       end;
 
 
 
 
@@ -567,7 +574,11 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.58  2003-08-11 21:18:20  peter
+  Revision 1.59  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.58  2003/08/11 21:18:20  peter
     * start of sparc support for newra
     * start of sparc support for newra
 
 
   Revision 1.57  2003/07/06 17:58:22  peter
   Revision 1.57  2003/07/06 17:58:22  peter

+ 84 - 75
compiler/ncgutil.pas

@@ -67,8 +67,9 @@ interface
     procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
     procedure gen_initialize_code(list:TAAsmoutput;inlined:boolean);
     procedure gen_finalize_code(list : TAAsmoutput;inlined:boolean);
     procedure gen_finalize_code(list : TAAsmoutput;inlined:boolean);
 
 
-    procedure gen_entry_code(list:TAAsmoutput;inlined:boolean);
+    procedure gen_proc_symbol(list:Taasmoutput);
     procedure gen_stackalloc_code(list:Taasmoutput);
     procedure gen_stackalloc_code(list:Taasmoutput);
+    procedure gen_entry_code(list:TAAsmoutput;inlined:boolean);
     procedure gen_exit_code(list : TAAsmoutput;inlined,usesacc,usesacchi,usesfpu:boolean);
     procedure gen_exit_code(list : TAAsmoutput;inlined,usesacc,usesacchi,usesfpu:boolean);
 
 
 (*
 (*
@@ -1625,6 +1626,79 @@ implementation
                                 Entry/Exit
                                 Entry/Exit
 ****************************************************************************}
 ****************************************************************************}
 
 
+    procedure gen_proc_symbol(list:Taasmoutput);
+      var
+        hs : string;
+      begin
+        { add symbol entry point as well as debug information                 }
+        { will be inserted in front of the rest of this list.                 }
+        { Insert alignment and assembler names }
+        { Align, gprof uses 16 byte granularity }
+        if (cs_profile in aktmoduleswitches) then
+          list.concat(Tai_align.create(16))
+        else
+          list.concat(Tai_align.create(aktalignment.procalign));
+
+{$ifdef GDB}
+        if (cs_debuginfo in aktmoduleswitches) then
+          begin
+            if (po_public in current_procinfo.procdef.procoptions) then
+              Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
+            current_procinfo.procdef.concatstabto(list);
+            Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
+          end;
+{$endif GDB}
+
+        repeat
+          hs:=current_procinfo.procdef.aliasnames.getfirst;
+          if hs='' then
+            break;
+{$ifdef GDB}
+          if (cs_debuginfo in aktmoduleswitches) and
+             target_info.use_function_relative_addresses then
+          list.concat(Tai_stab_function_name.create(strpnew(hs)));
+{$endif GDB}
+          if (cs_profile in aktmoduleswitches) or
+             (po_public in current_procinfo.procdef.procoptions) then
+            list.concat(Tai_symbol.createname_global(hs,0))
+          else
+            list.concat(Tai_symbol.createname(hs,0));
+        until false;
+      end;
+
+
+    procedure gen_stackalloc_code(list:Taasmoutput);
+      var
+        stackframe : longint;
+      begin
+        { Calculate size of stackframe }
+        stackframe:=current_procinfo.calc_stackframe_size;
+
+{$ifndef powerpc}
+        { at least for the ppc this applies always, so this code isn't usable (FK) }
+        { omit stack frame ? }
+        if (current_procinfo.framepointer.number=NR_STACK_POINTER_REG) then
+          begin
+            CGmessage(cg_d_stackframe_omited);
+            if stackframe<>0 then
+              cg.g_stackpointer_alloc(list,stackframe);
+          end
+        else
+{$endif powerpc}
+          begin
+            if (po_interrupt in current_procinfo.procdef.procoptions) then
+              cg.g_interrupt_stackframe_entry(list);
+
+            cg.g_stackframe_entry(list,stackframe);
+
+            {Never call stack checking before the standard system unit
+             has been initialized.}
+             if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
+               cg.g_stackcheck(list,stackframe);
+          end;
+      end;
+
+
     procedure gen_entry_code(list:TAAsmoutput;inlined:boolean);
     procedure gen_entry_code(list:TAAsmoutput;inlined:boolean);
       var
       var
         href : treference;
         href : treference;
@@ -1638,7 +1712,6 @@ implementation
           gdb stabs information is generated AFTER the rest of this
           gdb stabs information is generated AFTER the rest of this
           code, since temp. allocation might occur before - carl
           code, since temp. allocation might occur before - carl
         }
         }
-
         if assigned(current_procinfo.procdef.parast) and
         if assigned(current_procinfo.procdef.parast) and
            not (po_assembler in current_procinfo.procdef.procoptions) then
            not (po_assembler in current_procinfo.procdef.procoptions) then
           begin
           begin
@@ -1714,77 +1787,7 @@ implementation
       end;
       end;
 
 
 
 
-    procedure gen_stackalloc_code(list:Taasmoutput);
-      var
-        hs : string;
-        stackframe : longint;
-      begin
-        {************************* Stack allocation **************************}
-        { and symbol entry point as well as debug information                 }
-        { will be inserted in front of the rest of this list.                 }
-        { Insert alignment and assembler names }
-        { Align, gprof uses 16 byte granularity }
-        if (cs_profile in aktmoduleswitches) then
-          list.concat(Tai_align.create(16))
-        else
-          list.concat(Tai_align.create(aktalignment.procalign));
-
-{$ifdef GDB}
-        if (cs_debuginfo in aktmoduleswitches) then
-          begin
-            if (po_public in current_procinfo.procdef.procoptions) then
-              Tprocsym(current_procinfo.procdef.procsym).is_global:=true;
-            current_procinfo.procdef.concatstabto(list);
-            Tprocsym(current_procinfo.procdef.procsym).isstabwritten:=true;
-          end;
-{$endif GDB}
-
-        repeat
-          hs:=current_procinfo.procdef.aliasnames.getfirst;
-          if hs='' then
-            break;
-{$ifdef GDB}
-          if (cs_debuginfo in aktmoduleswitches) and
-             target_info.use_function_relative_addresses then
-          list.concat(Tai_stab_function_name.create(strpnew(hs)));
-{$endif GDB}
-          if (cs_profile in aktmoduleswitches) or
-             (po_public in current_procinfo.procdef.procoptions) then
-            list.concat(Tai_symbol.createname_global(hs,0))
-          else
-            list.concat(Tai_symbol.createname(hs,0));
-        until false;
-
-        { Calculate size of stackframe }
-        stackframe:=current_procinfo.calc_stackframe_size;
-
-{$ifndef powerpc}
-        { at least for the ppc this applies always, so this code isn't usable (FK) }
-        { omit stack frame ? }
-        if (current_procinfo.framepointer.number=NR_STACK_POINTER_REG) then
-          begin
-            CGmessage(cg_d_stackframe_omited);
-            if stackframe<>0 then
-              cg.g_stackpointer_alloc(list,stackframe);
-          end
-        else
-{$endif powerpc}
-          begin
-            if (po_interrupt in current_procinfo.procdef.procoptions) then
-              cg.g_interrupt_stackframe_entry(list);
-
-            cg.g_stackframe_entry(list,stackframe);
-
-            {Never call stack checking before the standard system unit
-             has been initialized.}
-             if (cs_check_stack in aktlocalswitches) and (current_procinfo.procdef.proctypeoption<>potype_proginit) then
-               cg.g_stackcheck(list,stackframe);
-          end;
-      end;
-
-
     procedure gen_exit_code(list : TAAsmoutput;inlined,usesacc,usesacchi,usesfpu:boolean);
     procedure gen_exit_code(list : TAAsmoutput;inlined,usesacc,usesacchi,usesfpu:boolean);
-
       var
       var
 {$ifdef GDB}
 {$ifdef GDB}
         stabsendlabel : tasmlabel;
         stabsendlabel : tasmlabel;
@@ -1792,6 +1795,7 @@ implementation
         p : pchar;
         p : pchar;
 {$endif GDB}
 {$endif GDB}
         rsp : Tregister;
         rsp : Tregister;
+        stacksize,
         retsize : longint;
         retsize : longint;
       begin
       begin
 
 
@@ -1830,8 +1834,9 @@ implementation
          begin
          begin
            if (current_procinfo.framepointer.number=NR_STACK_POINTER_REG) then
            if (current_procinfo.framepointer.number=NR_STACK_POINTER_REG) then
             begin
             begin
-              if (tg.gettempsize<>0) then
-                cg.a_op_const_reg(list,OP_ADD,OS_32,tg.gettempsize,current_procinfo.framepointer);
+              stacksize:=current_procinfo.calc_stackframe_size;
+              if (stacksize<>0) then
+                cg.a_op_const_reg(list,OP_ADD,OS_32,stacksize,current_procinfo.framepointer);
             end
             end
            else
            else
             cg.g_restore_frame_pointer(list);
             cg.g_restore_frame_pointer(list);
@@ -2072,7 +2077,11 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.135  2003-08-17 16:59:20  jonas
+  Revision 1.136  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.135  2003/08/17 16:59:20  jonas
     * fixed regvars so they work with newra (at least for ppc)
     * fixed regvars so they work with newra (at least for ppc)
     * fixed some volatile register bugs
     * fixed some volatile register bugs
     + -dnotranslation option for -dnewra, which causes the registers not to
     + -dnotranslation option for -dnewra, which causes the registers not to

+ 18 - 2
compiler/pmodules.pas

@@ -752,7 +752,10 @@ implementation
         usesacc,
         usesacc,
         usesfpu,
         usesfpu,
         usesacchi : boolean;
         usesacchi : boolean;
+        headertai : tai;
+        templist  : taasmoutput;
       begin
       begin
+        templist:=Taasmoutput.create;
         { update module flags }
         { update module flags }
         current_module.flags:=current_module.flags or flag;
         current_module.flags:=current_module.flags or flag;
         { create procdef }
         { create procdef }
@@ -771,7 +774,11 @@ implementation
             internalerror(200304253);
             internalerror(200304253);
         end;
         end;
         include(current_procinfo.flags,pi_do_call);
         include(current_procinfo.flags,pi_do_call);
-        gen_stackalloc_code(list);
+        { generate symbol and save end of header position }
+        gen_proc_symbol(templist);
+        headertai:=tai(templist.last);
+        list.concatlist(templist);
+        { generate procedure 'body' }
         gen_entry_code(list,false);
         gen_entry_code(list,false);
         gen_initialize_code(list,false);
         gen_initialize_code(list,false);
         gen_finalize_code(list,false);
         gen_finalize_code(list,false);
@@ -779,9 +786,14 @@ implementation
         usesfpu:=false;
         usesfpu:=false;
         usesacchi:=false;
         usesacchi:=false;
         gen_load_return_value(list,usesacc,usesacchi,usesfpu);
         gen_load_return_value(list,usesacc,usesacchi,usesfpu);
+        { Add stack allocation code after header }
+        gen_stackalloc_code(templist);
+        list.insertlistafter(headertai,templist);
+        { Add exit code at the end }
         gen_exit_code(list,false,usesacc,usesacchi,usesfpu);
         gen_exit_code(list,false,usesacc,usesacchi,usesfpu);
 {        list.convert_registers;}
 {        list.convert_registers;}
         release_main_proc(pd);
         release_main_proc(pd);
+        templist.free;
       end;
       end;
 
 
 
 
@@ -1453,7 +1465,11 @@ implementation
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.117  2003-08-20 09:07:00  daniel
+  Revision 1.118  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.117  2003/08/20 09:07:00  daniel
     * New register coding now mandatory, some more convert_registers calls
     * New register coding now mandatory, some more convert_registers calls
       removed.
       removed.
 
 

+ 25 - 17
compiler/psub.pas

@@ -571,8 +571,8 @@ implementation
         oldprocinfo : tprocinfo;
         oldprocinfo : tprocinfo;
         oldaktmaxfpuregisters : longint;
         oldaktmaxfpuregisters : longint;
         oldfilepos : tfileposinfo;
         oldfilepos : tfileposinfo;
-        templist, templist2,
-        stackalloccode : Taasmoutput;
+        templist : Taasmoutput;
+        headertai : tai;
         usesacc,
         usesacc,
         usesfpu,
         usesfpu,
         usesacchi      : boolean;
         usesacchi      : boolean;
@@ -594,7 +594,6 @@ implementation
         aktbreaklabel:=nil;
         aktbreaklabel:=nil;
         aktcontinuelabel:=nil;
         aktcontinuelabel:=nil;
         templist:=Taasmoutput.create;
         templist:=Taasmoutput.create;
-        templist2:=Taasmoutput.create;
 
 
         { add parast/localst to symtablestack }
         { add parast/localst to symtablestack }
         add_to_symtablestack;
         add_to_symtablestack;
@@ -639,7 +638,7 @@ implementation
         aktfilepos:=exitpos;
         aktfilepos:=exitpos;
         aktlocalswitches:=exitswitches;
         aktlocalswitches:=exitswitches;
         gen_finalize_code(templist,false);
         gen_finalize_code(templist,false);
-        { the finalcode must be added if the was no position available,
+        { the finalcode must be concatted if there was no position available,
           using insertlistafter will result in an insert at the start
           using insertlistafter will result in an insert at the start
           when currentai=nil }
           when currentai=nil }
         if assigned(tasmnode(finalasmnode).currenttai) then
         if assigned(tasmnode(finalasmnode).currenttai) then
@@ -659,17 +658,26 @@ implementation
         {   regvars again) (JM)                                            }
         {   regvars again) (JM)                                            }
         free_regvars(aktproccode);
         free_regvars(aktproccode);
 {$endif newra}
 {$endif newra}
-        { handle return value, this is not done for assembler routines when
-          they didn't reference the result variable }
+
+        { add code that will load the return value, this is not done
+          for assembler routines when they didn't reference the result
+          variable }
         usesacc:=false;
         usesacc:=false;
         usesfpu:=false;
         usesfpu:=false;
         usesacchi:=false;
         usesacchi:=false;
-        gen_load_return_value(templist2,usesacc,usesacchi,usesfpu);
+        gen_load_return_value(templist,usesacc,usesacchi,usesfpu);
+        aktproccode.concatlist(templist);
 
 
+        { generate symbol and save end of header position }
+        gen_proc_symbol(templist);
+        headertai:=tai(templist.last);
+        { add entry code after header }
         gen_entry_code(templist,false);
         gen_entry_code(templist,false);
+        { insert symbol and entry code }
         aktproccode.insertlist(templist);
         aktproccode.insertlist(templist);
 
 
-        aktproccode.concatlist(templist2);
+        { The procedure body is finished, we can now
+          allocate the registers }
 {$ifdef newra}
 {$ifdef newra}
 {$ifdef ra_debug2}
 {$ifdef ra_debug2}
         rg.writegraph;
         rg.writegraph;
@@ -698,14 +706,11 @@ implementation
 {$ifdef newra}
 {$ifdef newra}
         translate_regvars(aktproccode,rg.colour);
         translate_regvars(aktproccode,rg.colour);
 {$endif newra}
 {$endif newra}
-        stackalloccode:=Taasmoutput.create;
-        gen_stackalloc_code(stackalloccode);
-{        stackalloccode.convert_registers;}
-        aktproccode.insertlist(stackalloccode);
-        stackalloccode.free;
-
+        { Add stack allocation code after header }
+        gen_stackalloc_code(templist);
+        aktproccode.insertlistafter(headertai,templist);
+        { Add exit code at the end }
         gen_exit_code(templist,false,usesacc,usesacchi,usesfpu);
         gen_exit_code(templist,false,usesacc,usesacchi,usesfpu);
-{        templist.convert_registers;}
         aktproccode.concatlist(templist);
         aktproccode.concatlist(templist);
 
 
         { now all the registers used are known }
         { now all the registers used are known }
@@ -741,7 +746,6 @@ implementation
 
 
         { restore }
         { restore }
         templist.free;
         templist.free;
-        templist2.free;
         aktmaxfpuregisters:=oldaktmaxfpuregisters;
         aktmaxfpuregisters:=oldaktmaxfpuregisters;
         aktfilepos:=oldfilepos;
         aktfilepos:=oldfilepos;
         current_procinfo:=oldprocinfo;
         current_procinfo:=oldprocinfo;
@@ -1302,7 +1306,11 @@ begin
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.137  2003-08-20 15:50:35  peter
+  Revision 1.138  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.137  2003/08/20 15:50:35  peter
     * define NOOPT until optimizer is fixed
     * define NOOPT until optimizer is fixed
 
 
   Revision 1.136  2003/08/20 09:07:00  daniel
   Revision 1.136  2003/08/20 09:07:00  daniel

+ 6 - 2
compiler/sparc/cpupi.pas

@@ -77,7 +77,7 @@ implementation
             <register window save area for calling>
             <register window save area for calling>
           %sp
           %sp
         }
         }
-        result:=procdef.localst.datasize+tg.gettempsize+savearea;
+        result:=Align(lasttemp+savearea,4);
       end;
       end;
 
 
 
 
@@ -86,7 +86,11 @@ begin
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.18  2003-07-06 17:58:22  peter
+  Revision 1.19  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.18  2003/07/06 17:58:22  peter
     * framepointer fixes for sparc
     * framepointer fixes for sparc
     * parent framepointer code more generic
     * parent framepointer code more generic
 
 

+ 5 - 15
compiler/tgobj.pas

@@ -86,7 +86,6 @@ unit tgobj;
              @param(l start offset where temps will start in stack)
              @param(l start offset where temps will start in stack)
           }
           }
           procedure setfirsttemp(l : longint);
           procedure setfirsttemp(l : longint);
-          function gettempsize : longint;
 
 
           procedure gettemp(list: taasmoutput; size : longint;temptype:ttemptype;var ref : treference);
           procedure gettemp(list: taasmoutput; size : longint;temptype:ttemptype;var ref : treference);
           procedure ungettemp(list: taasmoutput; const ref : treference);
           procedure ungettemp(list: taasmoutput; const ref : treference);
@@ -202,19 +201,6 @@ unit tgobj;
       end;
       end;
 
 
 
 
-    function ttgobj.gettempsize : longint;
-      var
-        _align : longint;
-      begin
-        { align to 4 bytes at least
-          otherwise all those subl $2,%esp are meaningless PM }
-        _align:=target_info.alignment.localalignmin;
-        if _align<4 then
-          _align:=4;
-        gettempsize:=Align(direction*lasttemp,_align);
-      end;
-
-
     function ttgobj.AllocTemp(list: taasmoutput; size : longint; temptype : ttemptype) : longint;
     function ttgobj.AllocTemp(list: taasmoutput; size : longint; temptype : ttemptype) : longint;
       var
       var
          tl,
          tl,
@@ -551,7 +537,11 @@ finalization
 end.
 end.
 {
 {
   $Log$
   $Log$
-  Revision 1.36  2003-07-06 17:58:22  peter
+  Revision 1.37  2003-08-20 17:48:49  peter
+    * fixed stackalloc to not allocate localst.datasize twice
+    * order of stackalloc code fixed for implicit init/final
+
+  Revision 1.36  2003/07/06 17:58:22  peter
     * framepointer fixes for sparc
     * framepointer fixes for sparc
     * parent framepointer code more generic
     * parent framepointer code more generic