paramgr.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. globtype,
  27. symconst,symtype,symdef;
  28. type
  29. {# This class defines some methods to take care of routine
  30. parameters. It should be overriden for each new processor
  31. }
  32. tparamanager = class
  33. {# Returns true if the return value can be put in accumulator }
  34. function ret_in_acc(def : tdef;calloption : tproccalloption) : boolean;virtual;
  35. {# Returns true if the return value is put in a register
  36. Either a floating point register, or a general purpose
  37. register.
  38. }
  39. function ret_in_reg(def : tdef;calloption : tproccalloption) : boolean;virtual;
  40. {# Returns true if the return value is actually a parameter
  41. pointer.
  42. }
  43. function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  44. function push_high_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  45. { Returns true if a parameter is too large to copy and only
  46. the address is pushed
  47. }
  48. function push_addr_param(def : tdef;calloption : tproccalloption) : boolean;virtual;
  49. { return the size of a push }
  50. function push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  51. { Returns true if a parameter needs to be copied on the stack, this
  52. is required for cdecl procedures
  53. }
  54. function copy_value_on_stack(def : tdef;calloption : tproccalloption) : boolean;
  55. {# Returns a structure giving the information on
  56. the storage of the parameter (which must be
  57. an integer parameter). This is only used when calling
  58. internal routines directly, where all parameters must
  59. be 4-byte values.
  60. @param(nr Parameter number of routine, starting from 1)
  61. }
  62. function getintparaloc(nr : longint) : tparalocation;virtual;abstract;
  63. {# This is used to populate the location information on all parameters
  64. for the routine. This is used for normal call resolution.
  65. }
  66. procedure create_param_loc_info(p : tabstractprocdef);virtual;abstract;
  67. {
  68. Returns the location where the invisible parameter for structured
  69. function results will be passed.
  70. }
  71. function getfuncretparaloc(p : tabstractprocdef) : tparalocation;virtual;
  72. {
  73. Returns the location where the invisible parameter for nested
  74. subroutines is passed.
  75. }
  76. function getframepointerloc(p : tabstractprocdef) : tparalocation;virtual;
  77. { Returns the self pointer location for the given tabstractprocdef,
  78. when the stack frame is already created. This is used by the code
  79. generating the wrappers for implemented interfaces.
  80. }
  81. function getselflocation(p : tabstractprocdef) : tparalocation;virtual;abstract;
  82. {
  83. Returns the location of the result if the result is in
  84. a register, the register(s) return depend on the type of
  85. the result.
  86. @param(def The definition of the result type of the function)
  87. }
  88. function getfuncresultloc(def : tdef;calloption:tproccalloption): tparalocation; virtual;
  89. end;
  90. procedure setparalocs(p : tprocdef);
  91. function getfuncretusedregisters(def : tdef;calloption:tproccalloption): tregisterset;
  92. var
  93. paralocdummy : tparalocation;
  94. paramanager : tparamanager;
  95. implementation
  96. uses
  97. cpuinfo,globals,systems,
  98. symbase,symsym,
  99. rgobj,
  100. defutil,cgbase,cginfo,verbose;
  101. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  102. function tparamanager.ret_in_acc(def : tdef;calloption : tproccalloption) : boolean;
  103. begin
  104. ret_in_acc:=(def.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  105. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_ansistring,st_widestring])) or
  106. ((def.deftype=procvardef) and not(po_methodpointer in tprocvardef(def).procoptions)) or
  107. ((def.deftype=objectdef) and not is_object(def)) or
  108. ((def.deftype=setdef) and (tsetdef(def).settype=smallset));
  109. end;
  110. function tparamanager.ret_in_reg(def : tdef;calloption : tproccalloption) : boolean;
  111. begin
  112. ret_in_reg:=ret_in_acc(def,calloption) or (def.deftype=floatdef);
  113. end;
  114. { true if uses a parameter as return value }
  115. function tparamanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  116. begin
  117. ret_in_param:=(def.deftype in [arraydef,recorddef]) or
  118. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_shortstring,st_longstring])) or
  119. ((def.deftype=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  120. ((def.deftype=objectdef) and is_object(def)) or
  121. (def.deftype=variantdef) or
  122. ((def.deftype=setdef) and (tsetdef(def).settype<>smallset));
  123. end;
  124. function tparamanager.push_high_param(def : tdef;calloption : tproccalloption) : boolean;
  125. begin
  126. push_high_param:=is_open_array(def) or
  127. is_open_string(def) or
  128. (is_array_of_const(def) and
  129. not(calloption in [pocall_cdecl,pocall_cppdecl]));
  130. end;
  131. { true if a parameter is too large to copy and only the address is pushed }
  132. function tparamanager.push_addr_param(def : tdef;calloption : tproccalloption) : boolean;
  133. begin
  134. push_addr_param:=false;
  135. case def.deftype of
  136. variantdef,
  137. formaldef :
  138. push_addr_param:=true;
  139. recorddef :
  140. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (def.size>pointer_size);
  141. arraydef :
  142. push_addr_param:=(calloption in [pocall_cdecl,pocall_cppdecl]) or
  143. (
  144. (tarraydef(def).highrange>=tarraydef(def).lowrange) and
  145. (def.size>pointer_size)
  146. ) or
  147. is_open_array(def) or
  148. is_array_of_const(def) or
  149. is_array_constructor(def);
  150. objectdef :
  151. push_addr_param:=is_object(def);
  152. stringdef :
  153. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  154. procvardef :
  155. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (po_methodpointer in tprocvardef(def).procoptions);
  156. setdef :
  157. push_addr_param:=not(calloption in [pocall_cdecl,pocall_cppdecl]) and (tsetdef(def).settype<>smallset);
  158. end;
  159. end;
  160. { true if a parameter is too large to push and needs a concatcopy to get the value on the stack }
  161. function tparamanager.copy_value_on_stack(def : tdef;calloption : tproccalloption) : boolean;
  162. begin
  163. copy_value_on_stack:=false;
  164. { this is only for cdecl procedures }
  165. if not(calloption in [pocall_cdecl,pocall_cppdecl]) then
  166. exit;
  167. case def.deftype of
  168. variantdef,
  169. formaldef :
  170. copy_value_on_stack:=true;
  171. recorddef :
  172. copy_value_on_stack:=(def.size>pointer_size);
  173. arraydef :
  174. copy_value_on_stack:=(tarraydef(def).highrange>=tarraydef(def).lowrange) and
  175. (def.size>pointer_size);
  176. objectdef :
  177. copy_value_on_stack:=is_object(def);
  178. stringdef :
  179. copy_value_on_stack:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  180. procvardef :
  181. copy_value_on_stack:=(po_methodpointer in tprocvardef(def).procoptions);
  182. setdef :
  183. copy_value_on_stack:=(tsetdef(def).settype<>smallset);
  184. end;
  185. end;
  186. { return the size of a push }
  187. function tparamanager.push_size(varspez:tvarspez;def : tdef;calloption : tproccalloption) : longint;
  188. begin
  189. push_size:=-1;
  190. case varspez of
  191. vs_out,
  192. vs_var :
  193. push_size:=pointer_size;
  194. vs_value,
  195. vs_const :
  196. begin
  197. if push_addr_param(def,calloption) then
  198. push_size:=pointer_size
  199. else
  200. begin
  201. { special array are normally pushed by addr, only for
  202. cdecl array of const it comes here and the pushsize
  203. is unknown }
  204. if is_array_of_const(def) then
  205. push_size:=0
  206. else
  207. push_size:=def.size;
  208. end;
  209. end;
  210. end;
  211. end;
  212. function tparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  213. begin
  214. result.loc:=LOC_REFERENCE;
  215. result.size:=OS_ADDR;
  216. result.sp_fixup:=pointer_size;
  217. result.reference.index.enum:=stack_pointer_reg;
  218. result.reference.offset:=0;
  219. end;
  220. function tparamanager.getframepointerloc(p : tabstractprocdef) : tparalocation;
  221. begin
  222. result.loc:=LOC_REFERENCE;
  223. result.size:=OS_ADDR;
  224. result.sp_fixup:=pointer_size;
  225. result.reference.index.enum:=stack_pointer_reg;
  226. result.reference.offset:=0;
  227. end;
  228. function tparamanager.getfuncresultloc(def : tdef;calloption:tproccalloption): tparalocation;
  229. begin
  230. fillchar(result,sizeof(tparalocation),0);
  231. if is_void(def) then exit;
  232. result.size := def_cgsize(def);
  233. case def.deftype of
  234. orddef,
  235. enumdef :
  236. begin
  237. result.loc := LOC_REGISTER;
  238. {$ifndef cpu64bit}
  239. if result.size in [OS_64,OS_S64] then
  240. begin
  241. result.register64.reghi.enum:=accumulatorhigh;
  242. result.register64.reglo.enum:=accumulator;
  243. end
  244. else
  245. {$endif cpu64bit}
  246. result.register.enum:=accumulator;
  247. end;
  248. floatdef :
  249. begin
  250. result.loc := LOC_FPUREGISTER;
  251. {$ifdef cpufpemu}
  252. if cs_fp_emulation in aktmoduleswitches then
  253. result.register.enum := accumulator
  254. else
  255. {$endif cpufpemu}
  256. result.register.enum := FPU_RESULT_REG;
  257. end;
  258. else
  259. begin
  260. if ret_in_reg(def,calloption) then
  261. begin
  262. result.loc := LOC_REGISTER;
  263. result.register.enum := accumulator;
  264. end
  265. else
  266. begin
  267. result.loc := LOC_REFERENCE;
  268. internalerror(2002081602);
  269. (*
  270. {$ifdef EXTDEBUG}
  271. { it is impossible to have the
  272. return value with an index register
  273. and a symbol!
  274. }
  275. if (ref.index <> R_NO) or (assigned(ref.symbol)) then
  276. internalerror(2002081602);
  277. {$endif}
  278. result.reference.index := ref.base;
  279. result.reference.offset := ref.offset;
  280. *)
  281. end;
  282. end;
  283. end;
  284. end;
  285. function getfuncretusedregisters(def : tdef;calloption:tproccalloption): tregisterset;
  286. var
  287. paramloc : tparalocation;
  288. regset : tregisterset;
  289. begin
  290. regset:=[];
  291. getfuncretusedregisters:=[];
  292. { if nothing is returned in registers,
  293. its useless to continue on in this
  294. routine
  295. }
  296. if not paramanager.ret_in_reg(def,calloption) then
  297. exit;
  298. paramloc := paramanager.getfuncresultloc(def,calloption);
  299. case paramloc.loc of
  300. LOC_FPUREGISTER,
  301. LOC_CFPUREGISTER,
  302. LOC_MMREGISTER,
  303. LOC_CMMREGISTER,
  304. LOC_REGISTER,LOC_CREGISTER :
  305. begin
  306. regset := regset + [paramloc.register.enum];
  307. if ((paramloc.size in [OS_S64,OS_64]) and
  308. (sizeof(aword) < 8))
  309. then
  310. begin
  311. regset := regset + [paramloc.registerhigh.enum];
  312. end;
  313. end;
  314. else
  315. internalerror(20020816);
  316. end;
  317. getfuncretusedregisters:=regset;
  318. end;
  319. procedure setparalocs(p : tprocdef);
  320. var
  321. hp : tparaitem;
  322. begin
  323. hp:=tparaitem(p.para.first);
  324. while assigned(hp) do
  325. begin
  326. if (hp.paraloc.loc in [LOC_REGISTER,LOC_FPUREGISTER,
  327. LOC_MMREGISTER]) and
  328. (
  329. (vo_regable in tvarsym(hp.parasym).varoptions) or
  330. (vo_fpuregable in tvarsym(hp.parasym).varoptions) or
  331. paramanager.push_addr_param(hp.paratype.def,p.proccalloption) or
  332. (hp.paratyp in [vs_var,vs_out])
  333. ) then
  334. begin
  335. case hp.paraloc.loc of
  336. LOC_REGISTER:
  337. hp.paraloc.loc := LOC_CREGISTER;
  338. LOC_FPUREGISTER:
  339. hp.paraloc.loc := LOC_CFPUREGISTER;
  340. {$ifdef SUPPORT_MMX}
  341. LOC_MMREGISTER:
  342. hp.paraloc.loc := LOC_CMMREGISTER;
  343. {$endif}
  344. end;
  345. tvarsym(hp.parasym).paraitem:=hp;
  346. end;
  347. hp:=tparaitem(hp.next);
  348. end;
  349. end;
  350. initialization
  351. ;
  352. finalization
  353. paramanager.free;
  354. end.
  355. {
  356. $Log$
  357. Revision 1.32 2003-04-22 13:47:08 peter
  358. * fixed C style array of const
  359. * fixed C array passing
  360. * fixed left to right with high parameters
  361. Revision 1.31 2003/02/02 19:25:54 carl
  362. * Several bugfixes for m68k target (register alloc., opcode emission)
  363. + VIS target
  364. + Generic add more complete (still not verified)
  365. Revision 1.30 2003/01/08 18:43:56 daniel
  366. * Tregister changed into a record
  367. Revision 1.29 2002/12/23 20:58:03 peter
  368. * remove unused global var
  369. Revision 1.28 2002/12/17 22:19:33 peter
  370. * fixed pushing of records>8 bytes with stdcall
  371. * simplified hightree loading
  372. Revision 1.27 2002/12/06 16:56:58 peter
  373. * only compile cs_fp_emulation support when cpufpuemu is defined
  374. * define cpufpuemu for m68k only
  375. Revision 1.26 2002/11/27 20:04:09 peter
  376. * tvarsym.get_push_size replaced by paramanager.push_size
  377. Revision 1.25 2002/11/27 02:33:19 peter
  378. * copy_value_on_stack method added for cdecl record passing
  379. Revision 1.24 2002/11/25 17:43:21 peter
  380. * splitted defbase in defutil,symutil,defcmp
  381. * merged isconvertable and is_equal into compare_defs(_ext)
  382. * made operator search faster by walking the list only once
  383. Revision 1.23 2002/11/18 17:31:58 peter
  384. * pass proccalloption to ret_in_xxx and push_xxx functions
  385. Revision 1.22 2002/11/16 18:00:04 peter
  386. * only push small arrays on the stack for win32
  387. Revision 1.21 2002/10/05 12:43:25 carl
  388. * fixes for Delphi 6 compilation
  389. (warning : Some features do not work under Delphi)
  390. Revision 1.20 2002/09/30 07:07:25 florian
  391. * fixes to common code to get the alpha compiler compiled applied
  392. Revision 1.19 2002/09/30 07:00:47 florian
  393. * fixes to common code to get the alpha compiler compiled applied
  394. Revision 1.18 2002/09/09 09:10:51 florian
  395. + added generic tparamanager.getframepointerloc
  396. Revision 1.17 2002/09/07 19:40:39 florian
  397. * tvarsym.paraitem is set now
  398. Revision 1.16 2002/09/01 21:04:48 florian
  399. * several powerpc related stuff fixed
  400. Revision 1.15 2002/08/25 19:25:19 peter
  401. * sym.insert_in_data removed
  402. * symtable.insertvardata/insertconstdata added
  403. * removed insert_in_data call from symtable.insert, it needs to be
  404. called separatly. This allows to deref the address calculation
  405. * procedures now calculate the parast addresses after the procedure
  406. directives are parsed. This fixes the cdecl parast problem
  407. * push_addr_param has an extra argument that specifies if cdecl is used
  408. or not
  409. Revision 1.14 2002/08/17 22:09:47 florian
  410. * result type handling in tcgcal.pass_2 overhauled
  411. * better tnode.dowrite
  412. * some ppc stuff fixed
  413. Revision 1.13 2002/08/17 09:23:38 florian
  414. * first part of procinfo rewrite
  415. Revision 1.12 2002/08/16 14:24:58 carl
  416. * issameref() to test if two references are the same (then emit no opcodes)
  417. + ret_in_reg to replace ret_in_acc
  418. (fix some register allocation bugs at the same time)
  419. + save_std_register now has an extra parameter which is the
  420. usedinproc registers
  421. Revision 1.11 2002/08/12 15:08:40 carl
  422. + stab register indexes for powerpc (moved from gdb to cpubase)
  423. + tprocessor enumeration moved to cpuinfo
  424. + linker in target_info is now a class
  425. * many many updates for m68k (will soon start to compile)
  426. - removed some ifdef or correct them for correct cpu
  427. Revision 1.10 2002/08/10 17:15:20 jonas
  428. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  429. Revision 1.9 2002/08/09 07:33:02 florian
  430. * a couple of interface related fixes
  431. Revision 1.8 2002/08/06 20:55:21 florian
  432. * first part of ppc calling conventions fix
  433. Revision 1.7 2002/08/05 18:27:48 carl
  434. + more more more documentation
  435. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  436. Revision 1.6 2002/07/30 20:50:43 florian
  437. * the code generator knows now if parameters are in registers
  438. Revision 1.5 2002/07/26 21:15:39 florian
  439. * rewrote the system handling
  440. Revision 1.4 2002/07/20 11:57:55 florian
  441. * types.pas renamed to defbase.pas because D6 contains a types
  442. unit so this would conflicts if D6 programms are compiled
  443. + Willamette/SSE2 instructions to assembler added
  444. Revision 1.3 2002/07/13 19:38:43 florian
  445. * some more generic calling stuff fixed
  446. Revision 1.2 2002/07/13 07:17:15 jonas
  447. * fixed memory leak reported by Sergey Korshunoff
  448. Revision 1.1 2002/07/11 14:41:28 florian
  449. * start of the new generic parameter handling
  450. }