paramgr.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. Generic calling convention handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# Parameter passing manager. Used to manage how
  19. parameters are passed to routines.
  20. }
  21. unit paramgr;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. cclasses,globtype,
  26. cpubase,cgbase,
  27. parabase,
  28. aasmtai,
  29. symconst,symtype,symsym,symdef;
  30. type
  31. {# This class defines some methods to take care of routine
  32. parameters. It should be overriden for each new processor
  33. }
  34. tparamanager = class
  35. { true if the location in paraloc can be reused as localloc }
  36. function param_use_paraloc(const cgpara:tcgpara):boolean;virtual;
  37. {# Returns true if the return value is actually a parameter
  38. pointer.
  39. }
  40. function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  41. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;
  42. { Returns true if a parameter is too large to copy and only
  43. the address is pushed
  44. }
  45. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;virtual;abstract;
  46. { return the size of a push }
  47. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  48. {# Returns a structure giving the information on
  49. the storage of the parameter (which must be
  50. an integer parameter). This is only used when calling
  51. internal routines directly, where all parameters must
  52. be 4-byte values.
  53. In case the location is a register, this register is allocated.
  54. Call freeintparaloc() after the call to free the locations again.
  55. Default implementation: don't do anything at all (in case you don't
  56. use register parameter passing)
  57. @param(list Current assembler list)
  58. @param(nr Parameter number of routine, starting from 1)
  59. }
  60. function get_para_align(calloption : tproccalloption):byte;virtual;
  61. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;virtual;
  62. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;virtual;
  63. function get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;virtual;
  64. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;virtual;
  65. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);virtual;abstract;
  66. {# allocate a parameter location created with create_paraloc_info
  67. @param(list Current assembler list)
  68. @param(loc Parameter location)
  69. }
  70. procedure allocparaloc(list: taasmoutput; const cgpara: TCGPara); virtual;
  71. {# free a parameter location allocated with alloccgpara
  72. @param(list Current assembler list)
  73. @param(loc Parameter location)
  74. }
  75. procedure freeparaloc(list: taasmoutput; const cgpara: TCGPara); virtual;
  76. { This is used to populate the location information on all parameters
  77. for the routine as seen in either the caller or the callee. It returns
  78. the size allocated on the stack
  79. }
  80. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;virtual;abstract;
  81. { This is used to populate the location information on all parameters
  82. for the routine when it is being inlined. It returns
  83. the size allocated on the stack
  84. }
  85. function create_inline_paraloc_info(p : tabstractprocdef):longint;virtual;
  86. { This is used to populate the location information on all parameters
  87. for the routine that are passed as varargs. It returns
  88. the size allocated on the stack (including the normal parameters)
  89. }
  90. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;virtual;abstract;
  91. procedure createtempparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);virtual;
  92. procedure duplicateparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  93. function parseparaloc(parasym : tparavarsym;const s : string) : boolean;virtual;abstract;
  94. end;
  95. var
  96. paramanager : tparamanager;
  97. implementation
  98. uses
  99. systems,
  100. cgobj,tgobj,cgutils,
  101. defutil,verbose;
  102. { true if the location in paraloc can be reused as localloc }
  103. function tparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  104. begin
  105. result:=false;
  106. end;
  107. { true if uses a parameter as return value }
  108. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  109. begin
  110. ret_in_param:=((def.deftype=arraydef) and not(is_dynamic_array(def))) or
  111. (def.deftype=recorddef) or
  112. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_shortstring,st_longstring])) or
  113. ((def.deftype=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  114. ((def.deftype=objectdef) and is_object(def)) or
  115. (def.deftype=variantdef) or
  116. ((def.deftype=setdef) and (tsetdef(def).settype<>smallset));
  117. end;
  118. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  119. begin
  120. push_high_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and
  121. (
  122. is_open_array(def) or
  123. is_open_string(def) or
  124. is_array_of_const(def)
  125. );
  126. end;
  127. { return the size of a push }
  128. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  129. begin
  130. push_size:=-1;
  131. case varspez of
  132. vs_out,
  133. vs_var :
  134. push_size:=sizeof(aint);
  135. vs_value,
  136. vs_const :
  137. begin
  138. if push_addr_param(varspez,def,calloption) then
  139. push_size:=sizeof(aint)
  140. else
  141. begin
  142. { special array are normally pushed by addr, only for
  143. cdecl array of const it comes here and the pushsize
  144. is unknown }
  145. if is_array_of_const(def) then
  146. push_size:=0
  147. else
  148. push_size:=def.size;
  149. end;
  150. end;
  151. end;
  152. end;
  153. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  154. begin
  155. result:=std_param_align;
  156. end;
  157. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  158. begin
  159. result:=[];
  160. end;
  161. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  162. begin
  163. result:=[];
  164. end;
  165. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  166. begin
  167. result:=[];
  168. end;
  169. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  170. begin
  171. result:=[];
  172. end;
  173. procedure tparamanager.allocparaloc(list: taasmoutput; const cgpara: TCGPara);
  174. var
  175. paraloc : pcgparalocation;
  176. begin
  177. paraloc:=cgpara.location;
  178. while assigned(paraloc) do
  179. begin
  180. case paraloc^.loc of
  181. LOC_REGISTER,
  182. LOC_CREGISTER:
  183. begin
  184. if getsupreg(paraloc^.register)<first_int_imreg then
  185. cg.getcpuregister(list,paraloc^.register);
  186. end;
  187. LOC_FPUREGISTER,
  188. LOC_CFPUREGISTER:
  189. begin
  190. if getsupreg(paraloc^.register)<first_fpu_imreg then
  191. cg.getcpuregister(list,paraloc^.register);
  192. end;
  193. LOC_MMREGISTER,
  194. LOC_CMMREGISTER :
  195. begin
  196. if getsupreg(paraloc^.register)<first_mm_imreg then
  197. cg.getcpuregister(list,paraloc^.register);
  198. end;
  199. end;
  200. paraloc:=paraloc^.next;
  201. end;
  202. end;
  203. procedure tparamanager.freeparaloc(list: taasmoutput; const cgpara: TCGPara);
  204. var
  205. paraloc : Pcgparalocation;
  206. {$ifdef cputargethasfixedstack}
  207. href : treference;
  208. {$endif cputargethasfixedstack}
  209. begin
  210. paraloc:=cgpara.location;
  211. while assigned(paraloc) do
  212. begin
  213. case paraloc^.loc of
  214. LOC_VOID:
  215. ;
  216. LOC_REGISTER,
  217. LOC_CREGISTER:
  218. begin
  219. if getsupreg(paraloc^.register)<first_int_imreg then
  220. cg.ungetcpuregister(list,paraloc^.register);
  221. end;
  222. LOC_FPUREGISTER,
  223. LOC_CFPUREGISTER:
  224. begin
  225. if getsupreg(paraloc^.register)<first_fpu_imreg then
  226. cg.ungetcpuregister(list,paraloc^.register);
  227. end;
  228. LOC_MMREGISTER,
  229. LOC_CMMREGISTER :
  230. begin
  231. if getsupreg(paraloc^.register)<first_mm_imreg then
  232. cg.ungetcpuregister(list,paraloc^.register);
  233. end;
  234. LOC_REFERENCE,
  235. LOC_CREFERENCE :
  236. begin
  237. {$ifdef cputargethasfixedstack}
  238. { don't use reference_reset_base, because that will depend on cgobj }
  239. fillchar(href,sizeof(href),0);
  240. href.base:=paraloc^.reference.index;
  241. href.offset:=paraloc^.reference.offset;
  242. tg.ungettemp(list,href);
  243. {$endif cputargethasfixedstack}
  244. end;
  245. else
  246. internalerror(2004110212);
  247. end;
  248. paraloc:=paraloc^.next;
  249. end;
  250. end;
  251. procedure tparamanager.createtempparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  252. var
  253. href : treference;
  254. len : aint;
  255. paraloc,
  256. newparaloc : pcgparalocation;
  257. begin
  258. cgpara.reset;
  259. cgpara.size:=parasym.paraloc[callerside].size;
  260. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  261. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  262. {$ifdef powerpc}
  263. cgpara.composite:=parasym.paraloc[callerside].composite;
  264. {$endif powerpc}
  265. paraloc:=parasym.paraloc[callerside].location;
  266. while assigned(paraloc) do
  267. begin
  268. if paraloc^.size=OS_NO then
  269. len:=push_size(parasym.varspez,parasym.vartype.def,calloption)
  270. else
  271. len:=tcgsize2size[paraloc^.size];
  272. newparaloc:=cgpara.add_location;
  273. newparaloc^.size:=paraloc^.size;
  274. {$warning maybe release this optimization for all targets?}
  275. {$ifdef sparc}
  276. { Does it fit a register? }
  277. if len<=sizeof(aint) then
  278. newparaloc^.loc:=LOC_REGISTER
  279. else
  280. {$endif sparc}
  281. newparaloc^.loc:=paraloc^.loc;
  282. case newparaloc^.loc of
  283. LOC_REGISTER :
  284. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  285. LOC_FPUREGISTER :
  286. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  287. LOC_MMREGISTER :
  288. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  289. LOC_REFERENCE :
  290. begin
  291. tg.gettemp(list,len,tt_persistent,href);
  292. newparaloc^.reference.index:=href.base;
  293. newparaloc^.reference.offset:=href.offset;
  294. end;
  295. end;
  296. paraloc:=paraloc^.next;
  297. end;
  298. end;
  299. procedure tparamanager.duplicateparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  300. var
  301. paraloc,
  302. newparaloc : pcgparalocation;
  303. begin
  304. cgpara.reset;
  305. cgpara.size:=parasym.paraloc[callerside].size;
  306. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  307. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  308. {$ifdef powerpc}
  309. cgpara.composite:=parasym.paraloc[callerside].composite;
  310. {$endif powerpc}
  311. paraloc:=parasym.paraloc[callerside].location;
  312. while assigned(paraloc) do
  313. begin
  314. newparaloc:=cgpara.add_location;
  315. move(paraloc^,newparaloc^,sizeof(newparaloc^));
  316. newparaloc^.next:=nil;
  317. paraloc:=paraloc^.next;
  318. end;
  319. end;
  320. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  321. begin
  322. { We need to return the size allocated }
  323. create_paraloc_info(p,callerside);
  324. result:=create_paraloc_info(p,calleeside);
  325. end;
  326. initialization
  327. ;
  328. finalization
  329. paramanager.free;
  330. end.
  331. {
  332. $Log$
  333. Revision 1.88 2005-02-14 17:13:07 peter
  334. * truncate log
  335. Revision 1.87 2005/02/08 16:40:16 florian
  336. * dyn. arrays are returned in registers
  337. Revision 1.86 2005/02/03 20:04:49 peter
  338. * push_addr_param must be defined per target
  339. Revision 1.85 2005/01/20 17:47:01 peter
  340. * remove copy_value_on_stack and a_param_copy_ref
  341. Revision 1.84 2005/01/18 22:19:20 peter
  342. * multiple location support for i386 a_param_ref
  343. * remove a_param_copy_ref for i386
  344. Revision 1.83 2005/01/10 21:50:05 jonas
  345. + support for passing records in registers under darwin
  346. * tcgpara now also has an intsize field, which contains the size in
  347. bytes of the whole parameter
  348. }