{ $Id$ Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe This unit handles register variable allocation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **************************************************************************** } unit regvars; {$i fpcdefs.inc} interface uses aasmbase,aasmtai,aasmcpu, node, symsym, cpubase, cgbase, tgobj; {$ifdef OLDREGVARS} procedure assign_regvars(p: tnode); procedure load_regvars(asml: TAAsmoutput; p: tnode); procedure cleanup_regvars(asml: TAAsmoutput); procedure store_regvar(asml: TAAsmoutput; reg: tregister); procedure load_regvar(asml: TAAsmoutput; vsym: tvarsym); procedure load_regvar_reg(asml: TAAsmoutput; reg: tregister); procedure load_all_regvars(asml: TAAsmoutput); procedure free_regvars(list: taasmoutput); { procedure translate_regvars(list: taasmoutput); } {$endif OLDREGVARS} {$ifdef i386} (* procedure sync_regvars_other(list1, list2: taasmoutput; const regvarsloaded1, regvarsloaded2: regvarother_booleanarray); procedure sync_regvars_int(list1, list2: taasmoutput; const regvarsloaded1, regvarsloaded2: Tsuperregisterset); *) {$endif i386} implementation uses globtype,systems,comphook, cutils,cclasses,verbose,globals, psub, symconst,symbase,symtype,symdef,paramgr,defutil, cpuinfo,cgobj,procinfo; {$ifdef OLDREGVARS} procedure searchregvars(p : tnamedindexitem;arg:pointer); var i,j,k : longint; parasym : boolean; begin parasym:=pboolean(arg)^; if (tsym(p).typ=varsym) and ((vo_regable in tvarsym(p).varoptions) or ((tvarsym(p).varspez in [vs_var,vs_const,vs_out]) and paramanager.push_addr_param(tvarsym(p).varspez,tvarsym(p).vartype.def,current_procinfo.procdef.proccalloption))) and not tvarsym(p).vartype.def.needs_inittable then begin j:=tvarsym(p).refs; { walk through all momentary register variables } for i:=1 to maxvarregs do begin with pregvarinfo(current_procinfo.procdef.regvarinfo)^ do if ((regvars[i]=nil) or (j>regvars_refs[i])) and (j>0) then begin for k:=maxvarregs-1 downto i do begin regvars[k+1]:=regvars[k]; regvars_para[k+1]:=regvars_para[k]; regvars_refs[k+1]:=regvars_refs[k]; end; { calc the new refs tvarsym(p).refs:=j; } regvars[i]:=tsym(p); regvars_para[i]:=parasym; regvars_refs[i]:=j; break; end; end; end; end; procedure searchfpuregvars(p : tnamedindexitem;arg:pointer); var i,j,k : longint; parasym : boolean; begin parasym:=pboolean(arg)^; if (tsym(p).typ=varsym) and (vo_fpuregable in tvarsym(p).varoptions) then begin j:=tvarsym(p).refs; { parameter get a less value } { walk through all momentary register variables } for i:=1 to maxfpuvarregs do begin with pregvarinfo(current_procinfo.procdef.regvarinfo)^ do if ((fpuregvars[i]=nil) or (j>fpuregvars_refs[i])) and (j>0) then begin for k:=maxfpuvarregs-1 downto i do begin fpuregvars[k+1]:=fpuregvars[k]; fpuregvars_para[k+1]:=fpuregvars_para[k]; fpuregvars_refs[k+1]:=fpuregvars_refs[k]; end; { calc the new refs tvarsym(p).refs:=j; } fpuregvars[i]:=tsym(p); fpuregvars_para[i]:=parasym; fpuregvars_refs[i]:=j; break; end; end; end; end; procedure assign_regvars(p: tnode); { register variables } var {$ifndef i386} hp: tparaitem; {$endif i386} regvarinfo: pregvarinfo; i: longint; parasym : boolean; siz : tcgsize; begin { max. optimizations } { only if no asm is used } { and no try statement } if (cs_regvars in aktglobalswitches) and {$ifndef i386} { we have to store regvars back to memory in this case! } (tcgprocinfo(current_procinfo).nestedprocs.count = 0) and {$endif i386} not(pi_uses_asm in current_procinfo.flags) and not(pi_uses_exceptions in current_procinfo.flags) then begin new(regvarinfo); fillchar(regvarinfo^,sizeof(regvarinfo^),0); current_procinfo.procdef.regvarinfo := regvarinfo; if (p.registersint 100)) then begin { register is no longer available for } { expressions } { search the register which is the most } { unused } { call by reference/const ? } if paramanager.push_addr_param(tvarsym(regvarinfo^.regvars[i]).varspez,tvarsym(regvarinfo^.regvars[i]).vartype.def,current_procinfo.procdef.proccalloption) then siz:=OS_32 else if (tvarsym(regvarinfo^.regvars[i]).vartype.def.deftype in [orddef,enumdef]) and (tvarsym(regvarinfo^.regvars[i]).vartype.def.size=1) then siz:=OS_8 else if (tvarsym(regvarinfo^.regvars[i]).vartype.def.deftype in [orddef,enumdef]) and (tvarsym(regvarinfo^.regvars[i]).vartype.def.size=2) then siz:=OS_16 else siz:=OS_32; { allocate a register for this regvar } tvarsym(regvarinfo^.regvars[i]).localloc.register:=cg.getintregister(exprasmlist,siz); tvarsym(regvarinfo^.regvars[i]).localloc.loc:=LOC_REGISTER; { and make sure it can't be freed } { rg.makeregvarint(getsupreg(regvarinfo^.regvars[i].localloc.register));} end else begin regvarinfo^.regvars[i] := nil; regvarinfo^.regvars_para[i] := false; end; end; end; if ((p.registersfpu+1) fpcdefs.inc to avoid conflicts if compiling by hand + try to fix temp allocation (still in ifdef) + generic constructor calls + start of tassembler / tmodulebase class cleanup Revision 1.30 2002/05/12 16:53:10 peter * moved entry and exitcode to ncgutil and cgobj * foreach gets extra argument for passing local data to the iterator function * -CR checks also class typecasts at runtime by changing them into as * fixed compiler to cycle with the -CR option * fixed stabs with elf writer, finally the global variables can be watched * removed a lot of routines from cga unit and replaced them by calls to cgobj * u32bit-s32bit updates for and,or,xor nodes. When one element is u32bit then the other is typecasted also to u32bit without giving a rangecheck warning/error. * fixed pascal calling method with reversing also the high tree in the parast, detected by tcalcst3 test Revision 1.29 2002/04/21 15:23:34 carl + changeregsize -> makeregsize Revision 1.28 2002/04/19 15:46:03 peter * mangledname rewrite, tprocdef.mangledname is now created dynamicly in most cases and not written to the ppu * add mangeledname_prefix() routine to generate the prefix of manglednames depending on the current procedure, object and module * removed static procprefix since the mangledname is now build only on demand from tprocdef.mangledname Revision 1.27 2002/04/15 19:44:19 peter * fixed stackcheck that would be called recursively when a stack error was found * generic changeregsize(reg,size) for i386 register resizing * removed some more routines from cga unit * fixed returnvalue handling * fixed default stacksize of linux and go32v2, 8kb was a bit small :-) Revision 1.26 2002/04/15 19:04:04 carl + reg2str -> std_reg2str() Revision 1.25 2002/04/06 18:13:01 jonas * several powerpc-related additions and fixes Revision 1.24 2002/04/02 17:11:29 peter * tlocation,treference update * LOC_CONSTANT added for better constant handling * secondadd splitted in multiple routines * location_force_reg added for loading a location to a register of a specified size * secondassignment parses now first the right and then the left node (this is compatible with Kylix). This saves a lot of push/pop especially with string operations * adapted some routines to use the new cg methods Revision 1.23 2002/03/31 20:26:36 jonas + a_loadfpu_* and a_loadmm_* methods in tcg * register allocation is now handled by a class and is mostly processor independent (+rgobj.pas and i386/rgcpu.pas) * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas) * some small improvements and fixes to the optimizer * some register allocation fixes * some fpuvaroffset fixes in the unary minus node * push/popusedregisters is now called rg.save/restoreusedregisters and (for i386) uses temps instead of push/pop's when using -Op3 (that code is also better optimizable) * fixed and optimized register saving/restoring for new/dispose nodes * LOC_FPU locations now also require their "register" field to be set to R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only) - list field removed of the tnode class because it's not used currently and can cause hard-to-find bugs }