{ Copyright (c) 1998-2002 by Florian Klaempfl and David Zhang Calling conventions for the MIPSEL 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 cpupara; {$i fpcdefs.inc} interface uses globtype, cclasses, aasmtai, cpubase,cpuinfo, symconst,symbase,symsym,symtype,symdef,paramgr,parabase,cgbase,cgutils; const MIPS_MAX_OFFSET = 20; { The value below is OK for O32 and N32 calling conventions } MIPS_MAX_REGISTERS_USED_IN_CALL = 6; { All ABI seem to start with $4 i.e. $a0 } MIPS_FIRST_REGISTER_USED_IN_CALL = RS_R4; { O32 ABI uses $a0 to $a3, i.e R4 to R7 } MIPS_LAST_REGISTER_USED_IN_CALL_ABI_O32 = RS_R7; { N32 ABI uses also R8 and R9 } MIPS_LAST_REGISTER_USED_IN_CALL_ABI_N32 = RS_R9; { The calculation below is based on the assumption that all registers used for ABI calls are ordered and follow each other } MIPS_NB_REGISTERS_USED_IN_CALL_O32 = MIPS_LAST_REGISTER_USED_IN_CALL_ABI_O32 - MIPS_FIRST_REGISTER_USED_IN_CALL + 1; MIPS_NB_REGISTERS_USED_IN_CALL_N32 = MIPS_LAST_REGISTER_USED_IN_CALL_ABI_N32 - MIPS_FIRST_REGISTER_USED_IN_CALL + 1; { Set O32 ABI as default } const mips_nb_used_registers : longint = MIPS_NB_REGISTERS_USED_IN_CALL_O32; { Might need to be changed if we support N64 ABI later } mips_sizeof_register_param : longint = 4; type tparasupregs = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of tsuperregister; tparasupregsused = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of boolean; tparasupregsize = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of tcgsize; tparasuprename = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of shortstring; tparasupregsoffset = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of longint; const parasupregs : tparasupregs = (RS_R4, RS_R5, RS_R6, RS_R7, RS_R8, RS_R9); type TMIPSParaManager=class(TParaManager) function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override; function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override; function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override; {Returns a structure giving the information on the storage of the parameter (which must be an integer parameter) @param(nr Parameter number of routine, starting from 1)} procedure getintparaloc(calloption : tproccalloption; nr : longint; def : tdef; var cgpara : tcgpara);override; function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override; function create_varargs_paraloc_info(p : TAbstractProcDef; varargspara:tvarargsparalist):longint;override; function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override; private intparareg, intparasize : longint; can_use_float : boolean; procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist); end; implementation uses cutils,verbose,systems, defutil, cpupi, procinfo, cgobj; function TMIPSParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet; begin { O32 ABI values } result:=[RS_R1..RS_R15,RS_R24..RS_R25,RS_R31]; end; function TMIPSParaManager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet; begin { O32 ABI values } result:=[RS_F0..RS_F19]; end; procedure TMIPSParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint; def : tdef; var cgpara : tcgpara); var paraloc : pcgparalocation; begin if nr<1 then InternalError(2002100806); cgpara.reset; cgpara.size:=def_cgsize(def); cgpara.intsize:=tcgsize2size[cgpara.size]; cgpara.alignment:=std_param_align; cgpara.def:=def; paraloc:=cgpara.add_location; with paraloc^ do begin { MIPS: ABI dependent number of first parameters are passed into registers } dec(nr); if nr=tarraydef(def).lowrange) or is_open_array(def) or is_array_of_const(def) or is_array_constructor(def);} variantdef, formaldef : result:=true; objectdef : result:=is_object(def); stringdef : result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]); procvardef : result:=not tprocvardef(def).is_addressonly; setdef : result:=not(is_smallset(def)); end; end; function TMIPSParaManager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara; var paraloc : pcgparalocation; retcgsize : tcgsize; retdef : tdef; begin if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then begin { Return is passed as var parameter, in this case we use the first register R4 for it } if assigned(forcetempdef) then retdef:=forcetempdef else retdef:=p.returndef; if ret_in_param(retdef,p.proccalloption) then begin if intparareg=0 then inc(intparareg); if side=calleeside then begin result.reset; paraloc:=result.add_location; paraloc^.loc:=LOC_REFERENCE; paraloc^.reference.index:=NR_STACK_POINTER_REG; { return is at offset zero } paraloc^.reference.offset:=0; paraloc^.size:=retcgsize; { Reserve first register for ret_in_param } if assigned(current_procinfo) then begin TMIPSProcInfo(current_procinfo).register_used[0]:=true; TMIPSProcInfo(current_procinfo).register_size[0]:=retcgsize; TMIPSProcInfo(current_procinfo).register_name[0]:='ret_in_param_result'; TMIPSProcInfo(current_procinfo).register_offset[0]:=0; end; end else begin getIntParaLoc(p.proccalloption,1,retdef,result); end; // This is now done in set_common_funcretloc_info already // result.def:=getpointerdef(result.def); end; exit; end; paraloc:=result.add_location; { Return in FPU register? } if result.def.typ=floatdef then begin paraloc^.loc:=LOC_FPUREGISTER; paraloc^.register:=NR_FPU_RESULT_REG; if retcgsize=OS_F64 then setsubreg(paraloc^.register,R_SUBFD); paraloc^.size:=retcgsize; end else { Return in register } begin {$ifndef cpu64bitalu} if retcgsize in [OS_64,OS_S64] then begin { low } paraloc^.loc:=LOC_REGISTER; if side=callerside then paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG else paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG; paraloc^.size:=OS_32; { high } paraloc:=result.add_location; paraloc^.loc:=LOC_REGISTER; if side=callerside then paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG else paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG; paraloc^.size:=OS_32; end else {$endif cpu64bitalu} begin paraloc^.loc:=LOC_REGISTER; paraloc^.size:=retcgsize; if side=callerside then paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize)) else paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize)); end; end end; procedure TMIPSParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist); var paraloc : pcgparalocation; i : integer; hp : tparavarsym; paracgsize : tcgsize; paralen : longint; paradef : tdef; fpparareg : integer; reg : tsuperregister; alignment : longint; tmp : longint; begin fpparareg := 0; for i:=0 to paras.count-1 do begin hp:=tparavarsym(paras[i]); paradef := hp.vardef; { currently only support C-style array of const } if (p.proccalloption in cstylearrayofconst) and is_array_of_const(paradef) then begin paraloc:=hp.paraloc[side].add_location; { hack: the paraloc must be valid, but is not actually used } paraloc^.loc:=LOC_REGISTER; paraloc^.register:=NR_R0; paraloc^.size:=OS_ADDR; break; end; if push_addr_param(hp.varspez,paradef,p.proccalloption) then begin paracgsize := OS_ADDR; paralen := tcgsize2size[paracgsize]; paradef := getpointerdef(paradef); end else begin paracgsize := def_cgsize(paradef); { for things like formaldef } if (paracgsize=OS_NO) and (paradef.typ <> recorddef) then begin paracgsize:=OS_ADDR; paradef:=voidpointertype; end; if not is_special_array(paradef) then paralen := paradef.size else paralen := tcgsize2size[paracgsize]; end; if (paracgsize in [OS_64, OS_S64, OS_F64]) or (hp.vardef.alignment = 8) then alignment := 8 else alignment := 4; hp.paraloc[side].reset; hp.paraloc[side].Alignment:=alignment; if paracgsize=OS_NO then begin paracgsize:=OS_32; paralen:=align(paralen,4); end else paralen:=tcgsize2size[paracgsize]; hp.paraloc[side].intsize:=paralen; hp.paraloc[side].size:=paracgsize; hp.paraloc[side].def:=paradef; { check the alignment, mips O32ABI require a nature alignment } tmp := align(intparasize, alignment) - intparasize; while tmp > 0 do begin inc(intparareg); inc(intparasize,4); dec(tmp,4); end; { any non-float args will disable the use the floating regs } { up to two fp args } if (not(paracgsize in [OS_F32, OS_F64])) or (fpparareg = 2) then can_use_float := false; while paralen>0 do begin paraloc:=hp.paraloc[side].add_location; { We can allocate at maximum 32 bits per register } if (paracgsize in [OS_64,OS_S64]) or ((paracgsize in [OS_F32,OS_F64]) and not(can_use_float)) then paraloc^.size:=OS_32 else paraloc^.size:=paracgsize; { ret in param? } if vo_is_funcret in hp.varoptions then begin { This should be the first parameter } if (side=calleeside) and assigned(current_procinfo) then begin TMIPSProcInfo(current_procinfo).register_used[0]:=true; TMIPSProcInfo(current_procinfo).register_name[0]:='result'; TMIPSProcInfo(current_procinfo).register_size[0]:=paracgsize; TMIPSProcInfo(current_procinfo).register_offset[0]:=0; end; //if (intparareg<>1) then // Comment(V_Warning,'intparareg should be one for funcret in TMipsParaManager.create_paraloc_info_intern'); if side=callerside then begin paraloc^.loc:=LOC_REGISTER; paraloc^.register:=newreg(R_INTREGISTER,parasupregs[0],R_SUBWHOLE); end else begin paraloc^.loc:=LOC_REFERENCE; if (po_nostackframe in p.procoptions) then paraloc^.reference.index := NR_STACK_POINTER_REG else begin paraloc^.reference.index := NR_FRAME_POINTER_REG; if assigned(current_procinfo) then TMIPSProcinfo(current_procinfo).needs_frame_pointer := true; end; paraloc^.reference.offset:=0; end; inc(intparasize,align(tcgsize2size[paraloc^.size],sizeof(aint))); end { In case of po_delphi_nested_cc, the parent frame pointer is always passed on the stack. } else if (intparareg p) then // begin // TMIPSProcinfo(current_procinfo).allocate_push_parasize(intparasize); // end; end; function TMIPSParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint; begin intparareg:=0; intparasize:=0; can_use_float := true; { Create Function result paraloc } create_funcretloc_info(p,callerside); { calculate the registers for the normal parameters } create_paraloc_info_intern(p,callerside,p.paras); { append the varargs } can_use_float := false; { restore correct intparasize value } if intparareg < 4 then intparasize:=intparareg * 4; create_paraloc_info_intern(p,callerside,varargspara); { We need to return the size allocated on the stack } result:=intparasize; end; function TMIPSParaManager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint; begin intparareg:=0; intparasize:=0; can_use_float := true; { Create Function result paraloc } create_funcretloc_info(p,side); create_paraloc_info_intern(p,side,p.paras); { We need to return the size allocated on the stack } result:=intparasize; end; begin ParaManager:=TMIPSParaManager.create; end.