{ Copyright (c) 2002 by Florian Klaempfl Generic calling convention handling 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. **************************************************************************** } {# Parameter passing manager. Used to manage how parameters are passed to routines. } unit paramgr; {$i fpcdefs.inc} interface uses cclasses,globtype, cpubase,cgbase,cgutils, parabase, aasmtai,aasmdata, symconst,symtype,symsym,symdef; type {# This class defines some methods to take care of routine parameters. It should be overridden for each new processor } { tparamanager } tparamanager = class { true if the location in paraloc can be reused as localloc } function param_use_paraloc(const cgpara:tcgpara):boolean;virtual; {# Returns true if the return value is actually a parameter pointer. } function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;virtual; function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual; { Returns true if a parameter is too large to copy and only the address is pushed } function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;abstract; { returns true if a parameter must be handled via copy-out (construct a reference, copy the parameter's value there in case of copy-in/out, pass the reference) } function push_copyout_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual; { return the size of a push } function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;virtual; {# Returns a structure giving the information on the storage of the parameter (which must be an integer parameter). This is only used when calling internal routines directly, where all parameters must be 4-byte values. In case the location is a register, this register is allocated. Call freeintparaloc() after the call to free the locations again. Default implementation: don't do anything at all (in case you don't use register parameter passing) @param(list Current assembler list) @param(nr Parameter number of routine, starting from 1) } function get_para_align(calloption : tproccalloption):byte;virtual; function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual; function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual; function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual; function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual; procedure getintparaloc(calloption : tproccalloption; nr : longint; def: tdef; var cgpara : tcgpara);virtual;abstract; {# allocate an individual pcgparalocation that's part of a tcgpara @param(list Current assembler list) @param(loc Parameter location element) } procedure allocparaloc(list: TAsmList; const paraloc: pcgparalocation); {# allocate a parameter location created with create_paraloc_info @param(list Current assembler list) @param(loc Parameter location) } procedure alloccgpara(list: TAsmList; const cgpara: TCGPara); virtual; {# free a parameter location allocated with alloccgpara @param(list Current assembler list) @param(loc Parameter location) } procedure freecgpara(list: TAsmList; const cgpara: TCGPara); virtual; { This is used to populate the location information on all parameters for the routine as seen in either the caller or the callee. It returns the size allocated on the stack } function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract; { Returns the location of the function result if p had def as function result instead of its actual result. Used if the compiler forces the function result to something different than the real result. } function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;virtual;abstract; procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee); { This is used to populate the location information on all parameters for the routine when it is being inlined. It returns the size allocated on the stack } function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual; { This is used to populate the location information on all parameters for the routine that are passed as varargs. It returns the size allocated on the stack (including the normal parameters) } function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract; function is_stack_paraloc(paraloc: pcgparalocation): boolean;virtual; procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);virtual; procedure duplicatecgparaloc(const orgparaloc: pcgparalocation; intonewparaloc: pcgparalocation); procedure duplicateparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara); function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual; function parsefuncretloc(p : tabstractprocdef; const s : string) : boolean;virtual; { allocate room for parameters on the stack in the entry code? } function use_fixed_stack: boolean; { whether stack pointer can be changed in the middle of procedure } function use_stackalloc: boolean; strict protected { common part of get_funcretloc; returns true if retloc is completely initialized afterwards } function set_common_funcretloc_info(p : tabstractprocdef; forcetempdef: tdef; out retcgsize: tcgsize; out retloc: tcgpara): boolean; end; var paramanager : tparamanager; implementation uses systems, cgobj,tgobj, defutil,verbose; { true if the location in paraloc can be reused as localloc } function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean; begin result:=false; end; { true if uses a parameter as return value } function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean; begin if (tf_safecall_exceptions in target_info.flags) and (calloption=pocall_safecall) then begin result:=true; exit; end; ret_in_param:=((def.typ=arraydef) and not(is_dynamic_array(def))) or (def.typ=recorddef) or (def.typ=stringdef) or ((def.typ=procvardef) and not tprocvardef(def).is_addressonly) or { interfaces are also passed by reference to be compatible with delphi and COM } ((def.typ=objectdef) and (is_object(def) or is_interface(def) or is_dispinterface(def))) or (def.typ=variantdef) or ((def.typ=setdef) and not is_smallset(def)); end; function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean; begin push_high_param:=not(calloption in cdecl_pocalls) and ( is_open_array(def) or is_open_string(def) or is_array_of_const(def) ); end; function tparamanager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean; begin push_copyout_param:=false; end; { return the size of a push } function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint; begin push_size:=-1; case varspez of vs_constref, vs_out, vs_var : push_size:=sizeof(pint); vs_value, vs_const : begin if push_addr_param(varspez,def,calloption) then push_size:=sizeof(pint) else begin { special array are normally pushed by addr, only for cdecl array of const it comes here and the pushsize is unknown } if is_array_of_const(def) then push_size:=0 else push_size:=def.size; end; end; end; end; function tparamanager.get_para_align(calloption : tproccalloption):byte; begin result:=std_param_align; end; function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset; begin result:=[]; end; function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset; begin result:=[]; end; function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset; begin result:=[]; end; function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset; begin result:=[]; end; {$if first_mm_imreg = 0} {$WARN 4044 OFF} { Comparison might be always false ... } {$endif} procedure tparamanager.allocparaloc(list: TAsmList; const paraloc: pcgparalocation); begin case paraloc^.loc of LOC_REGISTER, LOC_CREGISTER: begin if getsupreg(paraloc^.register)