cpupara.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. PowerPC specific calling conventions
  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. { PowerPC specific calling conventions are handled by this unit
  19. }
  20. unit cpupara;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. cpubase,
  26. symconst,symbase,symtype,symdef,paramgr;
  27. type
  28. tppcparamanager = class(tparamanager)
  29. function push_addr_param(def : tdef;calloption : tproccalloption) : boolean;override;
  30. function getintparaloc(nr : longint) : tparalocation;override;
  31. procedure create_param_loc_info(p : tabstractprocdef);override;
  32. function getfuncretparaloc(p : tabstractprocdef) : tparalocation;override;
  33. end;
  34. implementation
  35. uses
  36. verbose,systems,
  37. cpuinfo,cginfo,cgbase,
  38. defutil;
  39. function tppcparamanager.getintparaloc(nr : longint) : tparalocation;
  40. begin
  41. fillchar(result,sizeof(tparalocation),0);
  42. if nr<1 then
  43. internalerror(2002070801)
  44. else if nr<=8 then
  45. begin
  46. result.loc:=LOC_REGISTER;
  47. result.register.enum:=R_INTREGISTER;
  48. result.register.number:=NR_R2+nr*(NR_R1-NR_R0);
  49. end
  50. else
  51. begin
  52. result.loc:=LOC_REFERENCE;
  53. result.reference.index.enum:=R_INTREGISTER;
  54. result.reference.index.number:=NR_STACK_POINTER_REG;
  55. result.reference.offset:=(nr-8)*4;
  56. end;
  57. result.size := OS_INT;
  58. end;
  59. function getparaloc(p : tdef) : tcgloc;
  60. begin
  61. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  62. if push_addr_param for the def is true
  63. }
  64. case p.deftype of
  65. orddef:
  66. getparaloc:=LOC_REGISTER;
  67. floatdef:
  68. getparaloc:=LOC_FPUREGISTER;
  69. enumdef:
  70. getparaloc:=LOC_REGISTER;
  71. pointerdef:
  72. getparaloc:=LOC_REGISTER;
  73. formaldef:
  74. getparaloc:=LOC_REGISTER;
  75. classrefdef:
  76. getparaloc:=LOC_REGISTER;
  77. recorddef:
  78. getparaloc:=LOC_REFERENCE;
  79. objectdef:
  80. if is_object(p) then
  81. getparaloc:=LOC_REFERENCE
  82. else
  83. getparaloc:=LOC_REGISTER;
  84. stringdef:
  85. if is_shortstring(p) or is_longstring(p) then
  86. getparaloc:=LOC_REFERENCE
  87. else
  88. getparaloc:=LOC_REGISTER;
  89. procvardef:
  90. if (po_methodpointer in tprocvardef(p).procoptions) then
  91. getparaloc:=LOC_REFERENCE
  92. else
  93. getparaloc:=LOC_REGISTER;
  94. filedef:
  95. getparaloc:=LOC_REGISTER;
  96. arraydef:
  97. getparaloc:=LOC_REFERENCE;
  98. setdef:
  99. if is_smallset(p) then
  100. getparaloc:=LOC_REGISTER
  101. else
  102. getparaloc:=LOC_REFERENCE;
  103. variantdef:
  104. getparaloc:=LOC_REFERENCE;
  105. { avoid problems with errornous definitions }
  106. errordef:
  107. getparaloc:=LOC_REGISTER;
  108. else
  109. internalerror(2002071001);
  110. end;
  111. end;
  112. function tppcparamanager.push_addr_param(def : tdef;calloption : tproccalloption) : boolean;
  113. begin
  114. case def.deftype of
  115. recorddef:
  116. push_addr_param:=true;
  117. arraydef:
  118. push_addr_param:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  119. is_open_array(def) or
  120. is_array_of_const(def) or
  121. is_array_constructor(def);
  122. setdef :
  123. push_addr_param:=(tsetdef(def).settype<>smallset);
  124. stringdef :
  125. push_addr_param:=tstringdef(def).string_typ in [st_shortstring,st_longstring];
  126. procvardef :
  127. push_addr_param:=po_methodpointer in tprocvardef(def).procoptions;
  128. else
  129. push_addr_param:=inherited push_addr_param(def,calloption);
  130. end;
  131. end;
  132. procedure tppcparamanager.create_param_loc_info(p : tabstractprocdef);
  133. var
  134. nextintreg,nextfloatreg,nextmmreg : tregister;
  135. paradef : tdef;
  136. stack_offset : aword;
  137. hp : tparaitem;
  138. loc : tcgloc;
  139. is_64bit: boolean;
  140. procedure assignintreg;
  141. begin
  142. if nextintreg.number<=NR_R10 then
  143. begin
  144. hp.paraloc.loc:=LOC_REGISTER;
  145. hp.paraloc.register:=nextintreg;
  146. inc(nextintreg.number,NR_R1-NR_R0);
  147. if target_info.abi=abi_powerpc_aix then
  148. inc(stack_offset,4);
  149. end
  150. else
  151. begin
  152. hp.paraloc.loc:=LOC_REFERENCE;
  153. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  154. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  155. hp.paraloc.reference.offset:=stack_offset;
  156. inc(stack_offset,4);
  157. end;
  158. end;
  159. begin
  160. nextintreg.enum:=R_INTREGISTER;
  161. nextintreg.number:=NR_R3;
  162. nextfloatreg.enum:=R_F1;
  163. nextmmreg.enum:=R_M1;
  164. case target_info.abi of
  165. abi_powerpc_aix:
  166. stack_offset:=24;
  167. abi_powerpc_sysv:
  168. stack_offset:=8;
  169. else
  170. internalerror(2003051901);
  171. end;
  172. { frame pointer for nested procedures? }
  173. { inc(nextintreg); }
  174. { constructor? }
  175. { destructor? }
  176. hp:=tparaitem(p.para.first);
  177. while assigned(hp) do
  178. begin
  179. if (hp.paratyp in [vs_var,vs_out]) then
  180. paradef := voidpointertype.def
  181. else
  182. paradef := hp.paratype.def;
  183. loc:=getparaloc(paradef);
  184. hp.paraloc.sp_fixup:=0;
  185. case loc of
  186. LOC_REGISTER:
  187. begin
  188. hp.paraloc.size := def_cgsize(paradef);
  189. { for things like formaldef }
  190. if hp.paraloc.size = OS_NO then
  191. hp.paraloc.size := OS_ADDR;
  192. is_64bit := hp.paraloc.size in [OS_64,OS_S64];
  193. if nextintreg.number<=(NR_R10-ord(is_64bit)*(NR_R1-NR_R0)) then
  194. begin
  195. hp.paraloc.loc:=LOC_REGISTER;
  196. if is_64bit then
  197. begin
  198. if odd((nextintreg.number-NR_R3) shr 8) and (target_info.abi=abi_powerpc_sysv) Then
  199. inc(nextintreg.number,NR_R1-NR_R0);
  200. hp.paraloc.registerhigh:=nextintreg;
  201. inc(nextintreg.number,NR_R1-NR_R0);
  202. if target_info.abi=abi_powerpc_aix then
  203. inc(stack_offset,4);
  204. end;
  205. hp.paraloc.registerlow:=nextintreg;
  206. inc(nextintreg.number,NR_R1-NR_R0);
  207. if target_info.abi=abi_powerpc_aix then
  208. inc(stack_offset,4);
  209. end
  210. else
  211. begin
  212. nextintreg.number := NR_R11;
  213. hp.paraloc.loc:=LOC_REFERENCE;
  214. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  215. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  216. hp.paraloc.reference.offset:=stack_offset;
  217. if not is_64bit then
  218. inc(stack_offset,4)
  219. else
  220. inc(stack_offset,8);
  221. end;
  222. end;
  223. LOC_FPUREGISTER:
  224. begin
  225. hp.paraloc.size:=def_cgsize(paradef);
  226. if nextfloatreg.enum<=R_F10 then
  227. begin
  228. hp.paraloc.loc:=LOC_FPUREGISTER;
  229. hp.paraloc.register:=nextfloatreg;
  230. inc(nextfloatreg.enum);
  231. if target_info.abi=abi_powerpc_aix then
  232. inc(stack_offset,8);
  233. end
  234. else
  235. begin
  236. {!!!!!!!}
  237. hp.paraloc.size:=def_cgsize(paradef);
  238. internalerror(2002071004);
  239. end;
  240. end;
  241. LOC_REFERENCE:
  242. begin
  243. hp.paraloc.size:=OS_ADDR;
  244. if push_addr_param(paradef,p.proccalloption) or
  245. is_open_array(paradef) or
  246. is_array_of_const(paradef) then
  247. assignintreg
  248. else
  249. begin
  250. hp.paraloc.loc:=LOC_REFERENCE;
  251. hp.paraloc.reference.index.enum:=R_INTREGISTER;
  252. hp.paraloc.reference.index.number:=NR_STACK_POINTER_REG;
  253. hp.paraloc.reference.offset:=stack_offset;
  254. inc(stack_offset,hp.paratype.def.size);
  255. end;
  256. end;
  257. else
  258. internalerror(2002071002);
  259. end;
  260. hp:=tparaitem(hp.next);
  261. end;
  262. end;
  263. function tppcparamanager.getfuncretparaloc(p : tabstractprocdef) : tparalocation;
  264. begin
  265. case p.rettype.def.deftype of
  266. orddef,
  267. enumdef:
  268. begin
  269. getfuncretparaloc.loc:=LOC_REGISTER;
  270. getfuncretparaloc.register.enum:=R_INTREGISTER;
  271. getfuncretparaloc.register.number:=NR_R3;
  272. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  273. if getfuncretparaloc.size in [OS_S64,OS_64] then
  274. begin
  275. getfuncretparaloc.registerhigh.enum:=R_INTREGISTER;
  276. getfuncretparaloc.registerhigh.number:=NR_R3;
  277. getfuncretparaloc.register.number:=NR_R4;
  278. end;
  279. end;
  280. floatdef:
  281. begin
  282. getfuncretparaloc.loc:=LOC_FPUREGISTER;
  283. getfuncretparaloc.register.enum:=R_F1;
  284. getfuncretparaloc.size:=def_cgsize(p.rettype.def);
  285. end;
  286. { smallsets are OS_INT in R3, others are OS_ADDR in R3 -> the same }
  287. { ugly, I know :) (JM) }
  288. setdef,
  289. variantdef,
  290. pointerdef,
  291. formaldef,
  292. classrefdef,
  293. recorddef,
  294. objectdef,
  295. stringdef,
  296. procvardef,
  297. filedef,
  298. arraydef,
  299. errordef:
  300. begin
  301. getfuncretparaloc.loc:=LOC_REGISTER;
  302. getfuncretparaloc.register.enum:=R_INTREGISTER;
  303. getfuncretparaloc.register.number:=NR_R3;
  304. getfuncretparaloc.size:=OS_ADDR;
  305. end;
  306. else
  307. internalerror(2002090903);
  308. end;
  309. end;
  310. begin
  311. paramanager:=tppcparamanager.create;
  312. end.
  313. {
  314. $Log$
  315. Revision 1.34 2003-05-30 23:45:49 marco
  316. * register skipping (aligning) for int64 parameters, sys V abi only.
  317. Revision 1.33 2003/05/30 22:54:19 marco
  318. * getfuncretparaloc now uses r3 for highdword and r4 for lo. Doesn't work tho
  319. Revision 1.32 2003/05/30 22:35:03 marco
  320. * committed fix that swaps int64 parameters hi and lo.
  321. Revision 1.31 2003/05/24 11:48:40 jonas
  322. * added some missing paralocation size settings
  323. Revision 1.30 2003/05/19 12:15:28 florian
  324. * fixed calling sequence for subroutines using the aix abi
  325. Revision 1.29 2003/05/12 20:14:47 florian
  326. * fixed parameter passing by value of large sets, strings and method pointers
  327. Revision 1.28 2003/05/11 23:19:32 florian
  328. * fixed passing of small const arrays and const records, they are always passed by reference
  329. Revision 1.27 2003/04/26 11:30:59 florian
  330. * fixed the powerpc to work with the new function result handling
  331. Revision 1.26 2003/04/23 12:35:35 florian
  332. * fixed several issues with powerpc
  333. + applied a patch from Jonas for nested function calls (PowerPC only)
  334. * ...
  335. Revision 1.25 2003/04/17 18:52:35 jonas
  336. * process para's from first to last instead of the other way round
  337. Revision 1.24 2003/04/16 07:55:07 jonas
  338. * fixed paralocation for integer var/out parameters
  339. Revision 1.23 2003/03/11 21:46:24 jonas
  340. * lots of new regallocator fixes, both in generic and ppc-specific code
  341. (ppc compiler still can't compile the linux system unit though)
  342. Revision 1.22 2003/01/09 22:00:53 florian
  343. * fixed some PowerPC issues
  344. Revision 1.21 2003/01/09 20:41:10 florian
  345. * fixed broken PowerPC compiler
  346. Revision 1.20 2003/01/09 11:22:14 olle
  347. * made powerpc compiler compile after Daniels Tregister modification
  348. Revision 1.19 2003/01/08 18:43:58 daniel
  349. * Tregister changed into a record
  350. Revision 1.18 2002/12/15 19:22:01 florian
  351. * fixed some crashes and a rte 201
  352. Revision 1.17 2002/11/25 17:43:27 peter
  353. * splitted defbase in defutil,symutil,defcmp
  354. * merged isconvertable and is_equal into compare_defs(_ext)
  355. * made operator search faster by walking the list only once
  356. Revision 1.16 2002/11/18 17:32:01 peter
  357. * pass proccalloption to ret_in_xxx and push_xxx functions
  358. Revision 1.15 2002/10/02 13:33:36 jonas
  359. + set, variant support in getfuncretparaloc
  360. Revision 1.14 2002/09/28 21:27:16 florian
  361. + getparaloc supports now sets and variants
  362. Revision 1.13 2002/09/10 21:28:05 jonas
  363. * int64 paras are now handled correctly (until the registers are used up
  364. anyway :)
  365. * the return location is now initialized correctly
  366. * fixed bug where ret_in_reg() was called for the procdefinition instead
  367. of for the result of the procedure
  368. Revision 1.12 2002/09/09 09:11:37 florian
  369. - removed passes_parameters_in_reg
  370. Revision 1.11 2002/09/07 17:54:59 florian
  371. * first part of PowerPC fixes
  372. Revision 1.10 2002/09/01 21:04:49 florian
  373. * several powerpc related stuff fixed
  374. Revision 1.9 2002/08/31 12:43:31 florian
  375. * ppc compilation fixed
  376. Revision 1.8 2002/08/18 10:42:38 florian
  377. * remaining assembler writer bugs fixed, the errors in the
  378. system unit are inline assembler problems
  379. Revision 1.7 2002/08/17 22:09:47 florian
  380. * result type handling in tcgcal.pass_2 overhauled
  381. * better tnode.dowrite
  382. * some ppc stuff fixed
  383. Revision 1.6 2002/08/13 21:40:58 florian
  384. * more fixes for ppc calling conventions
  385. Revision 1.5 2002/07/30 20:50:44 florian
  386. * the code generator knows now if parameters are in registers
  387. Revision 1.4 2002/07/28 20:45:22 florian
  388. + added direct assembler reader for PowerPC
  389. Revision 1.3 2002/07/26 22:22:10 florian
  390. * several PowerPC related fixes to get forward with system unit compilation
  391. Revision 1.2 2002/07/11 14:41:34 florian
  392. * start of the new generic parameter handling
  393. Revision 1.1 2002/07/07 09:44:32 florian
  394. * powerpc target fixed, very simple units can be compiled
  395. }