2
0

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