cpupara.pas 17 KB

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