cpupara.pas 17 KB

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