paramgr.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. cpubase,
  26. aasmtai,
  27. globtype,
  28. symconst,symtype,symdef;
  29. type
  30. {# This class defines some methods to take care of routine
  31. parameters. It should be overriden for each new processor
  32. }
  33. tparamanager = class
  34. {# Returns true if the return value is actually a parameter
  35. pointer.
  36. }
  37. function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  38. function push_high_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  39. { Returns true if a parameter is too large to copy and only
  40. the address is pushed
  41. }
  42. function push_addr_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  43. { return the size of a push }
  44. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  45. { Returns true if a parameter needs to be copied on the stack, this
  46. is required for cdecl procedures
  47. }
  48. function copy_value_on_stack(def : tdef;calloption : tproccalloption) : boolean;virtual;
  49. {# Returns a structure giving the information on
  50. the storage of the parameter (which must be
  51. an integer parameter). This is only used when calling
  52. internal routines directly, where all parameters must
  53. be 4-byte values.
  54. In case the location is a register, this register is allocated.
  55. Call freeintparaloc() after the call to free the locations again.
  56. Default implementation: don't do anything at all (in case you don't
  57. use register parameter passing)
  58. @param(list Current assembler list)
  59. @param(nr Parameter number of routine, starting from 1)
  60. }
  61. function getintparaloc(list: taasmoutput; nr : longint) : tparalocation;virtual;abstract;
  62. {# frees a parameter location allocated with getintparaloc
  63. @param(list Current assembler list)
  64. @param(nr Parameter number of routine, starting from 1)
  65. }
  66. procedure freeintparaloc(list: taasmoutput; nr : longint); virtual; abstract;
  67. {# allocate a parameter location created with create_paraloc_info
  68. @param(list Current assembler list)
  69. @param(loc Parameter location)
  70. }
  71. procedure allocparaloc(list: taasmoutput; const loc: tparalocation); virtual;
  72. {# free a parameter location allocated with allocparaloc
  73. @param(list Current assembler list)
  74. @param(loc Parameter location)
  75. }
  76. procedure freeparaloc(list: taasmoutput; const loc: tparalocation); virtual;
  77. {# This is used to populate the location information on all parameters
  78. for the routine as seen in either the caller or the callee. This is used for normal call resolution.
  79. }
  80. procedure create_paraloc_info(p : tabstractprocdef; side: tcallercallee);virtual;abstract;
  81. { Returns the self pointer location for the given tabstractprocdef,
  82. when the stack frame is already created. This is used by the code
  83. generating the wrappers for implemented interfaces.
  84. }
  85. function getselflocation(p : tabstractprocdef) : tparalocation;virtual;abstract;
  86. { Return the location of the low and high part of a 64bit parameter }
  87. procedure splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);virtual;
  88. procedure alloctempregs(list: taasmoutput;var locpara:tparalocation);virtual;
  89. end;
  90. var
  91. paralocdummy : tparalocation;
  92. paramanager : tparamanager;
  93. implementation
  94. uses
  95. cpuinfo,globals,systems,
  96. symbase,symsym,
  97. rgobj,
  98. defutil,cgbase,cginfo,verbose;
  99. { true if uses a parameter as return value }
  100. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  101. begin
  102. ret_in_param:=(def.deftype in [arraydef,recorddef]) or
  103. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_shortstring,st_longstring])) or
  104. ((def.deftype=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  105. ((def.deftype=objectdef) and is_object(def)) or
  106. (def.deftype=variantdef) or
  107. ((def.deftype=setdef) and (tsetdef(def).settype<>smallset));
  108. end;
  109. function tparamanager.push_high_param(def : tdef;calloption : tproccalloption) : boolean;
  110. begin
  111. push_high_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and
  112. (
  113. is_open_array(def) or
  114. is_open_string(def) or
  115. is_array_of_const(def)
  116. );
  117. end;
  118. { true if a parameter is too large to copy and only the address is pushed }
  119. function tparamanager.push_addr_param(def : tdef;calloption : tproccalloption) : boolean;
  120. begin
  121. push_addr_param:=false;
  122. case def.deftype of
  123. variantdef,
  124. formaldef :
  125. push_addr_param:=true;
  126. recorddef :
  127. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (def.size>pointer_size);
  128. arraydef :
  129. begin
  130. if (calloption in [pocall_cdecl,pocall_cppdecl]) then
  131. begin
  132. { array of const values are pushed on the stack }
  133. push_addr_param:=not is_array_of_const(def);
  134. end
  135. else
  136. begin
  137. push_addr_param:=(
  138. (tarraydef(def).highrange>=tarraydef(def).lowrange) and
  139. (def.size>pointer_size)
  140. ) or
  141. is_open_array(def) or
  142. is_array_of_const(def) or
  143. is_array_constructor(def);
  144. end;
  145. end;
  146. objectdef :
  147. push_addr_param:=is_object(def);
  148. stringdef :
  149. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  150. procvardef :
  151. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (po_methodpointer in tprocvardef(def).procoptions);
  152. setdef :
  153. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tsetdef(def).settype<>smallset);
  154. end;
  155. end;
  156. { true if a parameter is too large to push and needs a concatcopy to get the value on the stack }
  157. function tparamanager.copy_value_on_stack(def : tdef;calloption : tproccalloption) : boolean;
  158. begin
  159. copy_value_on_stack:=false;
  160. { this is only for cdecl procedures }
  161. if not(calloption in [pocall_cdecl,pocall_cppdecl]) then
  162. exit;
  163. case def.deftype of
  164. variantdef,
  165. formaldef :
  166. copy_value_on_stack:=true;
  167. recorddef :
  168. copy_value_on_stack:=(def.size>pointer_size);
  169. arraydef :
  170. copy_value_on_stack:=(tarraydef(def).highrange>=tarraydef(def).lowrange) and
  171. (def.size>pointer_size);
  172. objectdef :
  173. copy_value_on_stack:=is_object(def);
  174. stringdef :
  175. copy_value_on_stack:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  176. procvardef :
  177. copy_value_on_stack:=(po_methodpointer in tprocvardef(def).procoptions);
  178. setdef :
  179. copy_value_on_stack:=(tsetdef(def).settype<>smallset);
  180. end;
  181. end;
  182. { return the size of a push }
  183. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  184. begin
  185. push_size:=-1;
  186. case varspez of
  187. vs_out,
  188. vs_var :
  189. push_size:=pointer_size;
  190. vs_value,
  191. vs_const :
  192. begin
  193. if push_addr_param(def,calloption) then
  194. push_size:=pointer_size
  195. else
  196. begin
  197. { special array are normally pushed by addr, only for
  198. cdecl array of const it comes here and the pushsize
  199. is unknown }
  200. if is_array_of_const(def) then
  201. push_size:=0
  202. else
  203. push_size:=def.size;
  204. end;
  205. end;
  206. end;
  207. end;
  208. procedure tparamanager.allocparaloc(list: taasmoutput; const loc: tparalocation);
  209. begin
  210. case loc.loc of
  211. LOC_REGISTER, LOC_CREGISTER:
  212. begin
  213. {$ifndef cpu64bit}
  214. if (loc.size in [OS_64,OS_S64,OS_F64]) then
  215. begin
  216. rg.getexplicitregisterint(list,loc.registerhigh);
  217. rg.getexplicitregisterint(list,loc.registerlow);
  218. end
  219. else
  220. {$endif cpu64bit}
  221. rg.getexplicitregisterint(list,loc.register);
  222. end;
  223. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  224. rg.getexplicitregisterfpu(list,loc.register);
  225. LOC_REFERENCE,LOC_CREFERENCE:
  226. { do nothing by default, most of the time it's the framepointer }
  227. else
  228. internalerror(200306091);
  229. end;
  230. end;
  231. procedure tparamanager.freeparaloc(list: taasmoutput; const loc: tparalocation);
  232. begin
  233. case loc.loc of
  234. LOC_REGISTER, LOC_CREGISTER:
  235. begin
  236. {$ifndef cpu64bit}
  237. if (loc.size in [OS_64,OS_S64,OS_F64]) then
  238. begin
  239. rg.ungetregisterint(list,loc.registerhigh);
  240. rg.ungetregisterint(list,loc.registerlow);
  241. end
  242. else
  243. {$endif cpu64bit}
  244. rg.ungetregisterint(list,loc.register);
  245. end;
  246. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  247. rg.ungetregisterfpu(list,loc.register,loc.size);
  248. LOC_REFERENCE,LOC_CREFERENCE:
  249. { do nothing by default, most of the time it's the framepointer }
  250. else
  251. internalerror(200306091);
  252. end;
  253. end;
  254. procedure tparamanager.splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);
  255. begin
  256. if not(locpara.size in [OS_64,OS_S64]) then
  257. internalerror(200307023);
  258. lochipara:=locpara;
  259. loclopara:=locpara;
  260. if locpara.size=OS_S64 then
  261. lochipara.size:=OS_S32
  262. else
  263. lochipara.size:=OS_32;
  264. loclopara.size:=OS_32;
  265. case locpara.loc of
  266. LOC_REGISTER,LOC_CREGISTER:
  267. begin
  268. loclopara.register:=locpara.registerlow;
  269. lochipara.register:=locpara.registerhigh;
  270. end;
  271. LOC_REFERENCE:
  272. begin
  273. if target_info.endian=endian_big then
  274. inc(loclopara.reference.offset,4)
  275. else
  276. inc(lochipara.reference.offset,4);
  277. end;
  278. else
  279. internalerror(200307024);
  280. end;
  281. end;
  282. procedure tparamanager.alloctempregs(list: taasmoutput;var locpara:tparalocation);
  283. begin
  284. if locpara.loc<>LOC_REGISTER then
  285. internalerror(200308123);
  286. {$ifndef cpu64bit}
  287. if locpara.size in [OS_64,OS_S64] then
  288. begin
  289. locpara.registerlow:=rg.getregisterint(list,OS_32);
  290. locpara.registerhigh:=rg.getregisterint(list,OS_32);
  291. end
  292. else
  293. {$endif cpu64bit}
  294. locpara.register:=rg.getregisterint(list,locpara.size);
  295. end;
  296. initialization
  297. ;
  298. finalization
  299. paramanager.free;
  300. end.
  301. {
  302. $Log$
  303. Revision 1.52 2003-09-04 15:39:58 peter
  304. * released useparatemp
  305. Revision 1.51 2003/09/03 15:55:01 peter
  306. * NEWRA branch merged
  307. Revision 1.50.2.1 2003/08/29 17:28:59 peter
  308. * next batch of updates
  309. Revision 1.50 2003/08/11 21:18:20 peter
  310. * start of sparc support for newra
  311. Revision 1.49 2003/07/08 21:24:59 peter
  312. * sparc fixes
  313. Revision 1.48 2003/07/05 20:11:41 jonas
  314. * create_paraloc_info() is now called separately for the caller and
  315. callee info
  316. * fixed ppc cycle
  317. Revision 1.47 2003/07/02 22:18:04 peter
  318. * paraloc splitted in callerparaloc,calleeparaloc
  319. * sparc calling convention updates
  320. Revision 1.46 2003/06/17 16:32:03 peter
  321. * allocpara/freepara 64bit support
  322. Revision 1.45 2003/06/13 21:19:30 peter
  323. * current_procdef removed, use current_procinfo.procdef instead
  324. Revision 1.44 2003/06/12 21:11:10 peter
  325. * ungetregisterfpu gets size parameter
  326. Revision 1.43 2003/06/09 14:54:26 jonas
  327. * (de)allocation of registers for parameters is now performed properly
  328. (and checked on the ppc)
  329. - removed obsolete allocation of all parameter registers at the start
  330. of a procedure (and deallocation at the end)
  331. Revision 1.42 2003/06/08 10:54:41 jonas
  332. - disabled changing of LOC_*REGISTER to LOC_C*REGISTER in setparalocs,
  333. this is not necessary anymore (doesn't do anything anymore actually,
  334. except making sure the interface crc changes)
  335. Revision 1.41 2003/06/07 18:57:04 jonas
  336. + added freeintparaloc
  337. * ppc get/freeintparaloc now check whether the parameter regs are
  338. properly allocated/deallocated (and get an extra list para)
  339. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  340. * fixed lot of missing pi_do_call's
  341. Revision 1.40 2003/05/31 15:05:28 peter
  342. * FUNCTION_RESULT64_LOW/HIGH_REG added for int64 results
  343. Revision 1.39 2003/05/30 23:57:08 peter
  344. * more sparc cleanup
  345. * accumulator removed, splitted in function_return_reg (called) and
  346. function_result_reg (caller)
  347. Revision 1.38 2003/05/13 15:16:13 peter
  348. * removed ret_in_acc, it's the reverse of ret_in_param
  349. * fixed ret_in_param for win32 cdecl array
  350. Revision 1.37 2003/04/30 22:15:59 florian
  351. * some 64 bit adaptions in ncgadd
  352. * x86-64 now uses ncgadd
  353. * tparamanager.ret_in_acc doesn't return true anymore for a void-def
  354. Revision 1.36 2003/04/27 11:21:33 peter
  355. * aktprocdef renamed to current_procinfo.procdef
  356. * procinfo renamed to current_procinfo
  357. * procinfo will now be stored in current_module so it can be
  358. cleaned up properly
  359. * gen_main_procsym changed to create_main_proc and release_main_proc
  360. to also generate a tprocinfo structure
  361. * fixed unit implicit initfinal
  362. Revision 1.35 2003/04/27 07:29:50 peter
  363. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  364. a new procdef declaration
  365. * aktprocsym removed
  366. * lexlevel removed, use symtable.symtablelevel instead
  367. * implicit init/final code uses the normal genentry/genexit
  368. * funcret state checking updated for new funcret handling
  369. Revision 1.34 2003/04/23 13:15:04 peter
  370. * fix push_high_param for cdecl
  371. Revision 1.33 2003/04/23 10:14:30 peter
  372. * cdecl array of const has no addr push
  373. Revision 1.32 2003/04/22 13:47:08 peter
  374. * fixed C style array of const
  375. * fixed C array passing
  376. * fixed left to right with high parameters
  377. Revision 1.31 2003/02/02 19:25:54 carl
  378. * Several bugfixes for m68k target (register alloc., opcode emission)
  379. + VIS target
  380. + Generic add more complete (still not verified)
  381. Revision 1.30 2003/01/08 18:43:56 daniel
  382. * Tregister changed into a record
  383. Revision 1.29 2002/12/23 20:58:03 peter
  384. * remove unused global var
  385. Revision 1.28 2002/12/17 22:19:33 peter
  386. * fixed pushing of records>8 bytes with stdcall
  387. * simplified hightree loading
  388. Revision 1.27 2002/12/06 16:56:58 peter
  389. * only compile cs_fp_emulation support when cpufpuemu is defined
  390. * define cpufpuemu for m68k only
  391. Revision 1.26 2002/11/27 20:04:09 peter
  392. * tvarsym.get_push_size replaced by paramanager.push_size
  393. Revision 1.25 2002/11/27 02:33:19 peter
  394. * copy_value_on_stack method added for cdecl record passing
  395. Revision 1.24 2002/11/25 17:43:21 peter
  396. * splitted defbase in defutil,symutil,defcmp
  397. * merged isconvertable and is_equal into compare_defs(_ext)
  398. * made operator search faster by walking the list only once
  399. Revision 1.23 2002/11/18 17:31:58 peter
  400. * pass proccalloption to ret_in_xxx and push_xxx functions
  401. Revision 1.22 2002/11/16 18:00:04 peter
  402. * only push small arrays on the stack for win32
  403. Revision 1.21 2002/10/05 12:43:25 carl
  404. * fixes for Delphi 6 compilation
  405. (warning : Some features do not work under Delphi)
  406. Revision 1.20 2002/09/30 07:07:25 florian
  407. * fixes to common code to get the alpha compiler compiled applied
  408. Revision 1.19 2002/09/30 07:00:47 florian
  409. * fixes to common code to get the alpha compiler compiled applied
  410. Revision 1.18 2002/09/09 09:10:51 florian
  411. + added generic tparamanager.getframepointerloc
  412. Revision 1.17 2002/09/07 19:40:39 florian
  413. * tvarsym.paraitem is set now
  414. Revision 1.16 2002/09/01 21:04:48 florian
  415. * several powerpc related stuff fixed
  416. Revision 1.15 2002/08/25 19:25:19 peter
  417. * sym.insert_in_data removed
  418. * symtable.insertvardata/insertconstdata added
  419. * removed insert_in_data call from symtable.insert, it needs to be
  420. called separatly. This allows to deref the address calculation
  421. * procedures now calculate the parast addresses after the procedure
  422. directives are parsed. This fixes the cdecl parast problem
  423. * push_addr_param has an extra argument that specifies if cdecl is used
  424. or not
  425. Revision 1.14 2002/08/17 22:09:47 florian
  426. * result type handling in tcgcal.pass_2 overhauled
  427. * better tnode.dowrite
  428. * some ppc stuff fixed
  429. Revision 1.13 2002/08/17 09:23:38 florian
  430. * first part of procinfo rewrite
  431. Revision 1.12 2002/08/16 14:24:58 carl
  432. * issameref() to test if two references are the same (then emit no opcodes)
  433. + ret_in_reg to replace ret_in_acc
  434. (fix some register allocation bugs at the same time)
  435. + save_std_register now has an extra parameter which is the
  436. usedinproc registers
  437. Revision 1.11 2002/08/12 15:08:40 carl
  438. + stab register indexes for powerpc (moved from gdb to cpubase)
  439. + tprocessor enumeration moved to cpuinfo
  440. + linker in target_info is now a class
  441. * many many updates for m68k (will soon start to compile)
  442. - removed some ifdef or correct them for correct cpu
  443. Revision 1.10 2002/08/10 17:15:20 jonas
  444. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  445. Revision 1.9 2002/08/09 07:33:02 florian
  446. * a couple of interface related fixes
  447. Revision 1.8 2002/08/06 20:55:21 florian
  448. * first part of ppc calling conventions fix
  449. Revision 1.7 2002/08/05 18:27:48 carl
  450. + more more more documentation
  451. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  452. Revision 1.6 2002/07/30 20:50:43 florian
  453. * the code generator knows now if parameters are in registers
  454. Revision 1.5 2002/07/26 21:15:39 florian
  455. * rewrote the system handling
  456. Revision 1.4 2002/07/20 11:57:55 florian
  457. * types.pas renamed to defbase.pas because D6 contains a types
  458. unit so this would conflicts if D6 programms are compiled
  459. + Willamette/SSE2 instructions to assembler added
  460. Revision 1.3 2002/07/13 19:38:43 florian
  461. * some more generic calling stuff fixed
  462. Revision 1.2 2002/07/13 07:17:15 jonas
  463. * fixed memory leak reported by Sergey Korshunoff
  464. Revision 1.1 2002/07/11 14:41:28 florian
  465. * start of the new generic parameter handling
  466. }