paramgr.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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;
  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 in [arraydef,recorddef]) or
  111. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_shortstring,st_longstring])) or
  112. ((def.deftype=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  113. ((def.deftype=objectdef) and is_object(def)) or
  114. (def.deftype=variantdef) or
  115. ((def.deftype=setdef) and (tsetdef(def).settype<>smallset));
  116. end;
  117. function tparamanager.push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  118. begin
  119. push_high_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and
  120. (
  121. is_open_array(def) or
  122. is_open_string(def) or
  123. is_array_of_const(def)
  124. );
  125. end;
  126. { true if a parameter is too large to copy and only the address is pushed }
  127. function tparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  128. begin
  129. result:=false;
  130. { var,out always require address }
  131. if varspez in [vs_var,vs_out] then
  132. begin
  133. result:=true;
  134. exit;
  135. end;
  136. { Only vs_const, vs_value here }
  137. case def.deftype of
  138. variantdef,
  139. formaldef :
  140. result:=true;
  141. recorddef :
  142. result:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (def.size>sizeof(aint));
  143. arraydef :
  144. begin
  145. if (calloption in [pocall_cdecl,pocall_cppdecl]) then
  146. begin
  147. { array of const values are pushed on the stack }
  148. result:=not is_array_of_const(def);
  149. end
  150. else
  151. begin
  152. result:=(
  153. (tarraydef(def).highrange>=tarraydef(def).lowrange) and
  154. (def.size>sizeof(aint))
  155. ) or
  156. is_open_array(def) or
  157. is_array_of_const(def) or
  158. is_array_constructor(def);
  159. end;
  160. end;
  161. objectdef :
  162. result:=is_object(def);
  163. stringdef :
  164. result:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  165. procvardef :
  166. result:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (po_methodpointer in tprocvardef(def).procoptions);
  167. setdef :
  168. result:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tsetdef(def).settype<>smallset);
  169. end;
  170. end;
  171. { return the size of a push }
  172. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  173. begin
  174. push_size:=-1;
  175. case varspez of
  176. vs_out,
  177. vs_var :
  178. push_size:=sizeof(aint);
  179. vs_value,
  180. vs_const :
  181. begin
  182. if push_addr_param(varspez,def,calloption) then
  183. push_size:=sizeof(aint)
  184. else
  185. begin
  186. { special array are normally pushed by addr, only for
  187. cdecl array of const it comes here and the pushsize
  188. is unknown }
  189. if is_array_of_const(def) then
  190. push_size:=0
  191. else
  192. push_size:=def.size;
  193. end;
  194. end;
  195. end;
  196. end;
  197. function tparamanager.get_para_align(calloption : tproccalloption):byte;
  198. begin
  199. result:=std_param_align;
  200. end;
  201. function tparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  202. begin
  203. result:=[];
  204. end;
  205. function tparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  206. begin
  207. result:=[];
  208. end;
  209. function tparamanager.get_volatile_registers_flags(calloption : tproccalloption):tcpuregisterset;
  210. begin
  211. result:=[];
  212. end;
  213. function tparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  214. begin
  215. result:=[];
  216. end;
  217. procedure tparamanager.allocparaloc(list: taasmoutput; const cgpara: TCGPara);
  218. var
  219. paraloc : pcgparalocation;
  220. begin
  221. paraloc:=cgpara.location;
  222. while assigned(paraloc) do
  223. begin
  224. case paraloc^.loc of
  225. LOC_REGISTER,
  226. LOC_CREGISTER:
  227. begin
  228. if getsupreg(paraloc^.register)<first_int_imreg then
  229. cg.getcpuregister(list,paraloc^.register);
  230. end;
  231. LOC_FPUREGISTER,
  232. LOC_CFPUREGISTER:
  233. begin
  234. if getsupreg(paraloc^.register)<first_fpu_imreg then
  235. cg.getcpuregister(list,paraloc^.register);
  236. end;
  237. LOC_MMREGISTER,
  238. LOC_CMMREGISTER :
  239. begin
  240. if getsupreg(paraloc^.register)<first_mm_imreg then
  241. cg.getcpuregister(list,paraloc^.register);
  242. end;
  243. end;
  244. paraloc:=paraloc^.next;
  245. end;
  246. end;
  247. procedure tparamanager.freeparaloc(list: taasmoutput; const cgpara: TCGPara);
  248. var
  249. paraloc : Pcgparalocation;
  250. {$ifdef cputargethasfixedstack}
  251. href : treference;
  252. {$endif cputargethasfixedstack}
  253. begin
  254. paraloc:=cgpara.location;
  255. while assigned(paraloc) do
  256. begin
  257. case paraloc^.loc of
  258. LOC_VOID:
  259. ;
  260. LOC_REGISTER,
  261. LOC_CREGISTER:
  262. begin
  263. if getsupreg(paraloc^.register)<first_int_imreg then
  264. cg.ungetcpuregister(list,paraloc^.register);
  265. end;
  266. LOC_FPUREGISTER,
  267. LOC_CFPUREGISTER:
  268. begin
  269. if getsupreg(paraloc^.register)<first_fpu_imreg then
  270. cg.ungetcpuregister(list,paraloc^.register);
  271. end;
  272. LOC_MMREGISTER,
  273. LOC_CMMREGISTER :
  274. begin
  275. if getsupreg(paraloc^.register)<first_mm_imreg then
  276. cg.ungetcpuregister(list,paraloc^.register);
  277. end;
  278. LOC_REFERENCE,
  279. LOC_CREFERENCE :
  280. begin
  281. {$ifdef cputargethasfixedstack}
  282. { don't use reference_reset_base, because that will depend on cgobj }
  283. fillchar(href,sizeof(href),0);
  284. href.base:=paraloc^.reference.index;
  285. href.offset:=paraloc^.reference.offset;
  286. tg.ungettemp(list,href);
  287. {$endif cputargethasfixedstack}
  288. end;
  289. else
  290. internalerror(2004110212);
  291. end;
  292. paraloc:=paraloc^.next;
  293. end;
  294. end;
  295. procedure tparamanager.createtempparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  296. var
  297. href : treference;
  298. len : aint;
  299. paraloc,
  300. newparaloc : pcgparalocation;
  301. begin
  302. cgpara.reset;
  303. cgpara.size:=parasym.paraloc[callerside].size;
  304. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  305. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  306. {$ifdef powerpc}
  307. cgpara.composite:=parasym.paraloc[callerside].composite;
  308. {$endif powerpc}
  309. paraloc:=parasym.paraloc[callerside].location;
  310. while assigned(paraloc) do
  311. begin
  312. if paraloc^.size=OS_NO then
  313. len:=push_size(parasym.varspez,parasym.vartype.def,calloption)
  314. else
  315. len:=tcgsize2size[paraloc^.size];
  316. newparaloc:=cgpara.add_location;
  317. newparaloc^.size:=paraloc^.size;
  318. {$warning maybe release this optimization for all targets?}
  319. {$ifdef sparc}
  320. { Does it fit a register? }
  321. if len<=sizeof(aint) then
  322. newparaloc^.loc:=LOC_REGISTER
  323. else
  324. {$endif sparc}
  325. newparaloc^.loc:=paraloc^.loc;
  326. case newparaloc^.loc of
  327. LOC_REGISTER :
  328. newparaloc^.register:=cg.getintregister(list,paraloc^.size);
  329. LOC_FPUREGISTER :
  330. newparaloc^.register:=cg.getfpuregister(list,paraloc^.size);
  331. LOC_MMREGISTER :
  332. newparaloc^.register:=cg.getmmregister(list,paraloc^.size);
  333. LOC_REFERENCE :
  334. begin
  335. tg.gettemp(list,len,tt_persistent,href);
  336. newparaloc^.reference.index:=href.base;
  337. newparaloc^.reference.offset:=href.offset;
  338. end;
  339. end;
  340. paraloc:=paraloc^.next;
  341. end;
  342. end;
  343. procedure tparamanager.duplicateparaloc(list: taasmoutput;calloption : tproccalloption;parasym : tparavarsym;var cgpara:TCGPara);
  344. var
  345. paraloc,
  346. newparaloc : pcgparalocation;
  347. begin
  348. cgpara.reset;
  349. cgpara.size:=parasym.paraloc[callerside].size;
  350. cgpara.intsize:=parasym.paraloc[callerside].intsize;
  351. cgpara.alignment:=parasym.paraloc[callerside].alignment;
  352. {$ifdef powerpc}
  353. cgpara.composite:=parasym.paraloc[callerside].composite;
  354. {$endif powerpc}
  355. paraloc:=parasym.paraloc[callerside].location;
  356. while assigned(paraloc) do
  357. begin
  358. newparaloc:=cgpara.add_location;
  359. move(paraloc^,newparaloc^,sizeof(newparaloc^));
  360. newparaloc^.next:=nil;
  361. paraloc:=paraloc^.next;
  362. end;
  363. end;
  364. function tparamanager.create_inline_paraloc_info(p : tabstractprocdef):longint;
  365. begin
  366. { We need to return the size allocated }
  367. create_paraloc_info(p,callerside);
  368. result:=create_paraloc_info(p,calleeside);
  369. end;
  370. initialization
  371. ;
  372. finalization
  373. paramanager.free;
  374. end.
  375. {
  376. $Log$
  377. Revision 1.85 2005-01-20 17:47:01 peter
  378. * remove copy_value_on_stack and a_param_copy_ref
  379. Revision 1.84 2005/01/18 22:19:20 peter
  380. * multiple location support for i386 a_param_ref
  381. * remove a_param_copy_ref for i386
  382. Revision 1.83 2005/01/10 21:50:05 jonas
  383. + support for passing records in registers under darwin
  384. * tcgpara now also has an intsize field, which contains the size in
  385. bytes of the whole parameter
  386. Revision 1.82 2004/11/21 17:17:03 florian
  387. * changed funcret location back to tlocation
  388. Revision 1.81 2004/11/15 23:35:31 peter
  389. * tparaitem removed, use tparavarsym instead
  390. * parameter order is now calculated from paranr value in tparavarsym
  391. Revision 1.80 2004/10/31 21:45:03 peter
  392. * generic tlocation
  393. * move tlocation to cgutils
  394. Revision 1.79 2004/09/25 14:23:54 peter
  395. * ungetregister is now only used for cpuregisters, renamed to
  396. ungetcpuregister
  397. * renamed (get|unget)explicitregister(s) to ..cpuregister
  398. * removed location-release/reference_release
  399. Revision 1.78 2004/09/21 17:25:12 peter
  400. * paraloc branch merged
  401. Revision 1.77.4.1 2004/08/31 20:43:06 peter
  402. * paraloc patch
  403. Revision 1.77 2004/07/09 23:41:04 jonas
  404. * support register parameters for inlined procedures + some inline
  405. cleanups
  406. Revision 1.76 2004/06/20 08:55:30 florian
  407. * logs truncated
  408. Revision 1.75 2004/06/16 20:07:09 florian
  409. * dwarf branch merged
  410. Revision 1.74 2004/04/18 15:22:24 florian
  411. + location support for arguments, currently PowerPC/MorphOS only
  412. Revision 1.73.2.5 2004/05/03 20:18:52 peter
  413. * fixes for tprintf
  414. Revision 1.73.2.4 2004/05/02 20:20:59 florian
  415. * started to fix callee side result value handling
  416. Revision 1.73.2.3 2004/05/02 12:45:32 peter
  417. * enabled cpuhasfixedstack for x86-64 again
  418. * fixed size of temp allocation for parameters
  419. }