{ 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,aasmdata,aasmcpu, node, symsym, cpubase, cgbase, tgobj; {$ifdef OLDREGVARS} procedure assign_regvars(p: tnode); procedure load_regvars(asml: TAsmList; p: tnode); procedure cleanup_regvars(asml: TAsmList); procedure store_regvar(asml: TAsmList; reg: tregister); procedure load_regvar(asml: TAsmList; vsym: tvarsym); procedure load_regvar_reg(asml: TAsmList; reg: tregister); procedure load_all_regvars(asml: TAsmList); procedure free_regvars(list: TAsmList); { procedure translate_regvars(list: TAsmList); } {$endif OLDREGVARS} {$ifdef i386} (* procedure sync_regvars_other(list1, list2: TAsmList; const regvarsloaded1, regvarsloaded2: regvarother_booleanarray); procedure sync_regvars_int(list1, list2: TAsmList; 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 ((tvarsym(p).varregable <> vr_none) or ((tvarsym(p).varspez in [vs_var,vs_const,vs_out,vs_constref]) and paramanager.push_addr_param(tvarsym(p).varspez,tvarsym(p).vardef,current_procinfo.procdef.proccalloption))) and not tvarsym(p).vardef.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 (tvarsym(p).varregable <> vr_none) 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_opt_regvar in current_settings.optimizerswitches) and { we have to store regvars back to memory in this case (the nested } { procedures can access the variables of the parent) } (not current_procinfo.has_nestedprocs) and not(pi_has_assembler_block 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]).vardef,current_procinfo.procdef.proccalloption) then siz:=OS_32 else if (tvarsym(regvarinfo^.regvars[i]).vardef.typ in [orddef,enumdef]) and (tvarsym(regvarinfo^.regvars[i]).vardef.size=1) then siz:=OS_8 else if (tvarsym(regvarinfo^.regvars[i]).vardef.typ in [orddef,enumdef]) and (tvarsym(regvarinfo^.regvars[i]).vardef.size=2) then siz:=OS_16 else siz:=OS_32; { allocate a register for this regvar } tvarsym(regvarinfo^.regvars[i]).localloc.register:=cg.getintregister(current_asmdata.CurrAsmList,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)