cpupara.pas 18 KB

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