cpupara.pas 17 KB

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