paramgr.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. symtype,symdef;
  27. type
  28. {# This class defines some methods to take care of routine
  29. parameters. It should be overriden for each new processor
  30. }
  31. tparamanager = class
  32. {# Returns true if the return value can be put in accumulator }
  33. function ret_in_acc(def : tdef) : boolean;virtual;
  34. {# Returns true if the return value is put in a register
  35. Either a floating point register, or a general purpose
  36. register.
  37. }
  38. function ret_in_reg(def : tdef) : boolean;virtual;
  39. {# Returns true if the return value is actually a parameter
  40. pointer.
  41. }
  42. function ret_in_param(def : tdef) : boolean;virtual;
  43. function push_high_param(def : tdef) : boolean;virtual;
  44. {# Returns true if a parameter is too large to copy and only
  45. the address is pushed
  46. }
  47. function push_addr_param(def : tdef;is_cdecl:boolean) : boolean;virtual;
  48. {# Returns a structure giving the information on
  49. the storage of the parameter (which must be
  50. an integer parameter)
  51. @param(nr Parameter number of routine, starting from 1)
  52. }
  53. function getintparaloc(nr : longint) : tparalocation;virtual;abstract;
  54. procedure create_param_loc_info(p : tabstractprocdef);virtual;abstract;
  55. {
  56. Returns the location where the invisible parameter for structured
  57. function results will be passed.
  58. }
  59. function getfuncretparaloc(p : tabstractprocdef) : tparalocation;virtual;
  60. { Returns the self pointer location for the given tabstractprocdef,
  61. when the stack frame is already created. This is used by the code
  62. generating the wrappers for implemented interfaces.
  63. }
  64. function getselflocation(p : tabstractprocdef) : tparalocation;virtual;abstract;
  65. {
  66. Returns the location of the result if the result is in
  67. a register, the register(s) return depend on the type of
  68. the result.
  69. @param(def The definition of the result type of the function)
  70. }
  71. function getfuncresultloc(def : tdef): tparalocation; virtual;
  72. end;
  73. procedure setparalocs(p : tprocdef);
  74. function getfuncretusedregisters(def : tdef): tregisterset;
  75. var
  76. paralocdummy : tparalocation;
  77. paramanager : tparamanager;
  78. implementation
  79. uses
  80. cpuinfo,globals,globtype,
  81. symconst,symbase,symsym,
  82. rgobj,
  83. defbase,cgbase,cginfo,verbose;
  84. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  85. function tparamanager.ret_in_acc(def : tdef) : boolean;
  86. begin
  87. ret_in_acc:=(def.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  88. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_ansistring,st_widestring])) or
  89. ((def.deftype=procvardef) and not(po_methodpointer in tprocvardef(def).procoptions)) or
  90. ((def.deftype=objectdef) and not is_object(def)) or
  91. ((def.deftype=setdef) and (tsetdef(def).settype=smallset));
  92. end;
  93. function tparamanager.ret_in_reg(def : tdef) : boolean;
  94. begin
  95. ret_in_reg:=ret_in_acc(def) or (def.deftype=floatdef);
  96. end;
  97. { true if uses a parameter as return value }
  98. function tparamanager.ret_in_param(def : tdef) : boolean;
  99. begin
  100. ret_in_param:=(def.deftype in [arraydef,recorddef]) or
  101. ((def.deftype=stringdef) and (tstringdef(def).string_typ in [st_shortstring,st_longstring])) or
  102. ((def.deftype=procvardef) and (po_methodpointer in tprocvardef(def).procoptions)) or
  103. ((def.deftype=objectdef) and is_object(def)) or
  104. (def.deftype=variantdef) or
  105. ((def.deftype=setdef) and (tsetdef(def).settype<>smallset));
  106. end;
  107. function tparamanager.push_high_param(def : tdef) : boolean;
  108. begin
  109. push_high_param:=is_open_array(def) or
  110. is_open_string(def) or
  111. is_array_of_const(def);
  112. end;
  113. { true if a parameter is too large to copy and only the address is pushed }
  114. function tparamanager.push_addr_param(def : tdef;is_cdecl:boolean) : boolean;
  115. begin
  116. push_addr_param:=false;
  117. if never_copy_const_param then
  118. push_addr_param:=true
  119. else
  120. begin
  121. case def.deftype of
  122. variantdef,
  123. formaldef :
  124. push_addr_param:=true;
  125. recorddef :
  126. push_addr_param:=(not is_cdecl) and (def.size>pointer_size);
  127. arraydef :
  128. push_addr_param:=(
  129. (tarraydef(def).highrange>=tarraydef(def).lowrange) and
  130. (def.size>pointer_size) and
  131. (not is_cdecl)
  132. ) or
  133. is_open_array(def) or
  134. is_array_of_const(def) or
  135. is_array_constructor(def);
  136. objectdef :
  137. push_addr_param:=is_object(def);
  138. stringdef :
  139. push_addr_param:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  140. procvardef :
  141. push_addr_param:=(not is_cdecl) and (po_methodpointer in tprocvardef(def).procoptions);
  142. setdef :
  143. push_addr_param:=(not is_cdecl) and (tsetdef(def).settype<>smallset);
  144. end;
  145. end;
  146. end;
  147. function tparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  148. begin
  149. result.loc:=LOC_REFERENCE;
  150. result.size:=OS_ADDR;
  151. result.sp_fixup:=pointer_size;
  152. result.reference.index:=stack_pointer_reg;
  153. result.reference.offset:=0;
  154. end;
  155. function tparamanager.getfuncresultloc(def : tdef): tparalocation;
  156. begin
  157. fillchar(result,sizeof(tparalocation),0);
  158. if is_void(def) then exit;
  159. result.size := def_cgsize(def);
  160. case def.deftype of
  161. orddef,
  162. enumdef :
  163. begin
  164. result.loc := LOC_REGISTER;
  165. if result.size in [OS_64,OS_S64] then
  166. begin
  167. result.register64.reghi:=accumulatorhigh;
  168. result.register64.reglo:=accumulator;
  169. end
  170. else
  171. result.register:=accumulator;
  172. end;
  173. floatdef :
  174. begin
  175. result.loc := LOC_FPUREGISTER;
  176. if cs_fp_emulation in aktmoduleswitches then
  177. result.register := accumulator
  178. else
  179. result.register := FPU_RESULT_REG;
  180. end;
  181. else
  182. begin
  183. if ret_in_reg(def) then
  184. begin
  185. result.loc := LOC_REGISTER;
  186. result.register := accumulator;
  187. end
  188. else
  189. begin
  190. result.loc := LOC_REFERENCE;
  191. internalerror(2002081602);
  192. (*
  193. {$ifdef EXTDEBUG}
  194. { it is impossible to have the
  195. return value with an index register
  196. and a symbol!
  197. }
  198. if (ref.index <> R_NO) or (assigned(ref.symbol)) then
  199. internalerror(2002081602);
  200. {$endif}
  201. result.reference.index := ref.base;
  202. result.reference.offset := ref.offset;
  203. *)
  204. end;
  205. end;
  206. end;
  207. end;
  208. function getfuncretusedregisters(def : tdef): tregisterset;
  209. var
  210. paramloc : tparalocation;
  211. regset : tregisterset;
  212. begin
  213. regset:=[];
  214. getfuncretusedregisters:=[];
  215. { if nothing is returned in registers,
  216. its useless to continue on in this
  217. routine
  218. }
  219. if not paramanager.ret_in_reg(def) then
  220. exit;
  221. paramloc := paramanager.getfuncresultloc(def);
  222. case paramloc.loc of
  223. LOC_FPUREGISTER,
  224. LOC_CFPUREGISTER,
  225. LOC_MMREGISTER,
  226. LOC_CMMREGISTER,
  227. LOC_REGISTER,LOC_CREGISTER :
  228. begin
  229. regset := regset + [paramloc.register];
  230. if ((paramloc.size in [OS_S64,OS_64]) and
  231. (sizeof(aword) < 8))
  232. then
  233. begin
  234. regset := regset + [paramloc.registerhigh];
  235. end;
  236. end;
  237. else
  238. internalerror(20020816);
  239. end;
  240. getfuncretusedregisters:=regset;
  241. end;
  242. procedure setparalocs(p : tprocdef);
  243. var
  244. hp : tparaitem;
  245. begin
  246. hp:=tparaitem(p.para.first);
  247. while assigned(hp) do
  248. begin
  249. if (hp.paraloc.loc in [LOC_REGISTER,LOC_FPUREGISTER,
  250. LOC_MMREGISTER]) and
  251. (
  252. (vo_regable in tvarsym(hp.parasym).varoptions) or
  253. (vo_fpuregable in tvarsym(hp.parasym).varoptions) or
  254. paramanager.push_addr_param(hp.paratype.def,p.proccalloption in [pocall_cdecl,pocall_cppdecl]) or
  255. (hp.paratyp in [vs_var,vs_out])
  256. ) then
  257. begin
  258. case hp.paraloc.loc of
  259. LOC_REGISTER:
  260. hp.paraloc.loc := LOC_CREGISTER;
  261. LOC_FPUREGISTER:
  262. hp.paraloc.loc := LOC_CFPUREGISTER;
  263. {$ifdef SUPPORT_MMX}
  264. LOC_MMREGISTER:
  265. hp.paraloc.loc := LOC_CMMREGISTER;
  266. {$endif}
  267. end;
  268. tvarsym(hp.parasym).paraitem:=hp;
  269. end;
  270. hp:=tparaitem(hp.next);
  271. end;
  272. end;
  273. finalization
  274. paramanager.free;
  275. end.
  276. {
  277. $Log$
  278. Revision 1.17 2002-09-07 19:40:39 florian
  279. * tvarsym.paraitem is set now
  280. Revision 1.16 2002/09/01 21:04:48 florian
  281. * several powerpc related stuff fixed
  282. Revision 1.15 2002/08/25 19:25:19 peter
  283. * sym.insert_in_data removed
  284. * symtable.insertvardata/insertconstdata added
  285. * removed insert_in_data call from symtable.insert, it needs to be
  286. called separatly. This allows to deref the address calculation
  287. * procedures now calculate the parast addresses after the procedure
  288. directives are parsed. This fixes the cdecl parast problem
  289. * push_addr_param has an extra argument that specifies if cdecl is used
  290. or not
  291. Revision 1.14 2002/08/17 22:09:47 florian
  292. * result type handling in tcgcal.pass_2 overhauled
  293. * better tnode.dowrite
  294. * some ppc stuff fixed
  295. Revision 1.13 2002/08/17 09:23:38 florian
  296. * first part of procinfo rewrite
  297. Revision 1.12 2002/08/16 14:24:58 carl
  298. * issameref() to test if two references are the same (then emit no opcodes)
  299. + ret_in_reg to replace ret_in_acc
  300. (fix some register allocation bugs at the same time)
  301. + save_std_register now has an extra parameter which is the
  302. usedinproc registers
  303. Revision 1.11 2002/08/12 15:08:40 carl
  304. + stab register indexes for powerpc (moved from gdb to cpubase)
  305. + tprocessor enumeration moved to cpuinfo
  306. + linker in target_info is now a class
  307. * many many updates for m68k (will soon start to compile)
  308. - removed some ifdef or correct them for correct cpu
  309. Revision 1.10 2002/08/10 17:15:20 jonas
  310. * register parameters are now LOC_CREGISTER instead of LOC_REGISTER
  311. Revision 1.9 2002/08/09 07:33:02 florian
  312. * a couple of interface related fixes
  313. Revision 1.8 2002/08/06 20:55:21 florian
  314. * first part of ppc calling conventions fix
  315. Revision 1.7 2002/08/05 18:27:48 carl
  316. + more more more documentation
  317. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  318. Revision 1.6 2002/07/30 20:50:43 florian
  319. * the code generator knows now if parameters are in registers
  320. Revision 1.5 2002/07/26 21:15:39 florian
  321. * rewrote the system handling
  322. Revision 1.4 2002/07/20 11:57:55 florian
  323. * types.pas renamed to defbase.pas because D6 contains a types
  324. unit so this would conflicts if D6 programms are compiled
  325. + Willamette/SSE2 instructions to assembler added
  326. Revision 1.3 2002/07/13 19:38:43 florian
  327. * some more generic calling stuff fixed
  328. Revision 1.2 2002/07/13 07:17:15 jonas
  329. * fixed memory leak reported by Sergey Korshunoff
  330. Revision 1.1 2002/07/11 14:41:28 florian
  331. * start of the new generic parameter handling
  332. }