cpupara.pas 18 KB

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