cpupara.pas 20 KB

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