cpupara.pas 19 KB

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