cpupara.pas 18 KB

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