paramgr.pas 19 KB

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