cpupara.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. PowerPC64 specific calling conventions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cpupara;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. aasmtai,
  23. cpubase,
  24. symconst, symtype, symdef, symsym,
  25. paramgr, parabase, cgbase;
  26. type
  27. tppcparamanager = class(tparamanager)
  28. function get_volatile_registers_int(calloption: tproccalloption):
  29. tcpuregisterset; override;
  30. function get_volatile_registers_fpu(calloption: tproccalloption):
  31. tcpuregisterset; override;
  32. function push_addr_param(varspez: tvarspez; def: tdef; calloption:
  33. tproccalloption): boolean; override;
  34. procedure getintparaloc(calloption: tproccalloption; nr: longint; var
  35. cgpara: TCGPara); override;
  36. function create_paraloc_info(p: tabstractprocdef; side: tcallercallee):
  37. longint; override;
  38. function create_varargs_paraloc_info(p: tabstractprocdef; varargspara:
  39. tvarargsparalist): longint; override;
  40. procedure create_funcretloc_info(p: tabstractprocdef; side: tcallercallee);
  41. private
  42. procedure init_values(var curintreg, curfloatreg, curmmreg: tsuperregister;
  43. var cur_stack_offset: aword);
  44. function create_paraloc_info_intern(p: tabstractprocdef; side:
  45. tcallercallee; paras: tparalist;
  46. var curintreg, curfloatreg, curmmreg: tsuperregister; var
  47. cur_stack_offset: aword): longint;
  48. function parseparaloc(p: tparavarsym; const s: string): boolean; override;
  49. end;
  50. implementation
  51. uses
  52. verbose, systems,
  53. defutil,
  54. cgutils;
  55. function tppcparamanager.get_volatile_registers_int(calloption:
  56. tproccalloption): tcpuregisterset;
  57. begin
  58. result := [RS_R3..RS_R12];
  59. end;
  60. function tppcparamanager.get_volatile_registers_fpu(calloption:
  61. tproccalloption): tcpuregisterset;
  62. begin
  63. result := [RS_F0..RS_F13];
  64. end;
  65. procedure tppcparamanager.getintparaloc(calloption: tproccalloption; nr:
  66. longint; var cgpara: TCGPara);
  67. var
  68. paraloc: pcgparalocation;
  69. begin
  70. cgpara.reset;
  71. cgpara.size := OS_INT;
  72. cgpara.intsize := tcgsize2size[OS_INT];
  73. cgpara.alignment := get_para_align(calloption);
  74. paraloc := cgpara.add_location;
  75. with paraloc^ do begin
  76. size := OS_INT;
  77. if (nr <= 8) then begin
  78. if nr = 0 then
  79. internalerror(200309271);
  80. loc := LOC_REGISTER;
  81. register := newreg(R_INTREGISTER, RS_R2 + nr, R_SUBWHOLE);
  82. end else begin
  83. loc := LOC_REFERENCE;
  84. paraloc^.reference.index := NR_STACK_POINTER_REG;
  85. if (target_info.abi <> abi_powerpc_aix) then
  86. reference.offset := sizeof(aint) * (nr - 8)
  87. else
  88. reference.offset := sizeof(aint) * (nr);
  89. end;
  90. end;
  91. end;
  92. function getparaloc(p: tdef): tcgloc;
  93. begin
  94. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  95. if push_addr_param for the def is true
  96. }
  97. case p.deftype of
  98. orddef:
  99. result := LOC_REGISTER;
  100. floatdef:
  101. result := LOC_FPUREGISTER;
  102. enumdef:
  103. result := LOC_REGISTER;
  104. pointerdef:
  105. result := LOC_REGISTER;
  106. formaldef:
  107. result := LOC_REGISTER;
  108. classrefdef:
  109. result := LOC_REGISTER;
  110. recorddef:
  111. result := LOC_REGISTER;
  112. objectdef:
  113. if is_object(p) then
  114. result := LOC_REFERENCE
  115. else
  116. result := LOC_REGISTER;
  117. stringdef:
  118. if is_shortstring(p) or is_longstring(p) then
  119. result := LOC_REFERENCE
  120. else
  121. result := LOC_REGISTER;
  122. procvardef:
  123. if (po_methodpointer in tprocvardef(p).procoptions) then
  124. result := LOC_REFERENCE
  125. else
  126. result := LOC_REGISTER;
  127. filedef:
  128. result := LOC_REGISTER;
  129. arraydef:
  130. result := LOC_REFERENCE;
  131. setdef:
  132. if is_smallset(p) then
  133. result := LOC_REGISTER
  134. else
  135. result := LOC_REFERENCE;
  136. variantdef:
  137. result := LOC_REFERENCE;
  138. { avoid problems with errornous definitions }
  139. errordef:
  140. result := LOC_REGISTER;
  141. else
  142. internalerror(2002071001);
  143. end;
  144. end;
  145. function tppcparamanager.push_addr_param(varspez: tvarspez; def: tdef;
  146. calloption: tproccalloption): boolean;
  147. begin
  148. result := false;
  149. { var,out always require address }
  150. if varspez in [vs_var, vs_out] then
  151. begin
  152. result := true;
  153. exit;
  154. end;
  155. case def.deftype of
  156. variantdef,
  157. formaldef:
  158. result := true;
  159. recorddef:
  160. result :=
  161. ((varspez = vs_const) and
  162. ((calloption = pocall_mwpascal) or
  163. (not (calloption in [pocall_cdecl, pocall_cppdecl]) and
  164. (def.size > 8)
  165. )
  166. )
  167. );
  168. arraydef:
  169. result := (tarraydef(def).highrange >= tarraydef(def).lowrange) or
  170. is_open_array(def) or
  171. is_array_of_const(def) or
  172. is_array_constructor(def);
  173. objectdef:
  174. result := is_object(def);
  175. setdef:
  176. result := (tsetdef(def).settype <> smallset);
  177. stringdef:
  178. result := tstringdef(def).string_typ in [st_shortstring, st_longstring];
  179. procvardef:
  180. result := po_methodpointer in tprocvardef(def).procoptions;
  181. end;
  182. end;
  183. procedure tppcparamanager.init_values(var curintreg, curfloatreg, curmmreg:
  184. tsuperregister; var cur_stack_offset: aword);
  185. begin
  186. { register parameter save area begins at 48(r2) }
  187. cur_stack_offset := 48;
  188. curintreg := RS_R3;
  189. curfloatreg := RS_F1;
  190. curmmreg := RS_M2;
  191. end;
  192. procedure tppcparamanager.create_funcretloc_info(p: tabstractprocdef; side:
  193. tcallercallee);
  194. var
  195. retcgsize: tcgsize;
  196. begin
  197. { Constructors return self instead of a boolean }
  198. if (p.proctypeoption = potype_constructor) then
  199. retcgsize := OS_ADDR
  200. else
  201. retcgsize := def_cgsize(p.rettype.def);
  202. location_reset(p.funcretloc[side], LOC_INVALID, OS_NO);
  203. p.funcretloc[side].size := retcgsize;
  204. { void has no location }
  205. if is_void(p.rettype.def) then begin
  206. p.funcretloc[side].loc := LOC_VOID;
  207. exit;
  208. end;
  209. { Return in FPU register? }
  210. if p.rettype.def.deftype = floatdef then begin
  211. p.funcretloc[side].loc := LOC_FPUREGISTER;
  212. p.funcretloc[side].register := NR_FPU_RESULT_REG;
  213. p.funcretloc[side].size := retcgsize;
  214. end else
  215. { Return in register? }
  216. if not ret_in_param(p.rettype.def, p.proccalloption) then begin
  217. p.funcretloc[side].loc := LOC_REGISTER;
  218. p.funcretloc[side].size := retcgsize;
  219. if side = callerside then
  220. p.funcretloc[side].register := newreg(R_INTREGISTER,
  221. RS_FUNCTION_RESULT_REG, cgsize2subreg(retcgsize))
  222. else
  223. p.funcretloc[side].register := newreg(R_INTREGISTER,
  224. RS_FUNCTION_RETURN_REG, cgsize2subreg(retcgsize));
  225. end else begin
  226. p.funcretloc[side].loc := LOC_REFERENCE;
  227. p.funcretloc[side].size := retcgsize;
  228. end;
  229. end;
  230. function tppcparamanager.create_paraloc_info(p: tabstractprocdef; side:
  231. tcallercallee): longint;
  232. var
  233. cur_stack_offset: aword;
  234. curintreg, curfloatreg, curmmreg: tsuperregister;
  235. begin
  236. init_values(curintreg, curfloatreg, curmmreg, cur_stack_offset);
  237. result := create_paraloc_info_intern(p, side, p.paras, curintreg, curfloatreg,
  238. curmmreg, cur_stack_offset);
  239. create_funcretloc_info(p, side);
  240. end;
  241. function tppcparamanager.create_paraloc_info_intern(p: tabstractprocdef; side:
  242. tcallercallee; paras: tparalist;
  243. var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset:
  244. aword): longint;
  245. var
  246. stack_offset: longint;
  247. paralen: aint;
  248. nextintreg, nextfloatreg, nextmmreg, maxfpureg: tsuperregister;
  249. paradef: tdef;
  250. paraloc: pcgparalocation;
  251. i: integer;
  252. hp: tparavarsym;
  253. loc: tcgloc;
  254. paracgsize: tcgsize;
  255. begin
  256. {$IFDEF extdebug}
  257. if po_explicitparaloc in p.procoptions then
  258. internalerror(200411141);
  259. {$ENDIF extdebug}
  260. result := 0;
  261. nextintreg := curintreg;
  262. nextfloatreg := curfloatreg;
  263. nextmmreg := curmmreg;
  264. stack_offset := cur_stack_offset;
  265. maxfpureg := RS_F13;
  266. for i := 0 to paras.count - 1 do begin
  267. hp := tparavarsym(paras[i]);
  268. paradef := hp.vartype.def;
  269. { Syscall for Morphos can have already a paraloc set }
  270. if (vo_has_explicit_paraloc in hp.varoptions) then begin
  271. if not (vo_is_syscall_lib in hp.varoptions) then
  272. internalerror(200412153);
  273. continue;
  274. end;
  275. hp.paraloc[side].reset;
  276. { currently only support C-style array of const }
  277. if (p.proccalloption in [pocall_cdecl, pocall_cppdecl]) and
  278. is_array_of_const(paradef) then begin
  279. paraloc := hp.paraloc[side].add_location;
  280. { hack: the paraloc must be valid, but is not actually used }
  281. paraloc^.loc := LOC_REGISTER;
  282. paraloc^.register := NR_R0;
  283. paraloc^.size := OS_ADDR;
  284. break;
  285. end;
  286. if (hp.varspez in [vs_var, vs_out]) or
  287. push_addr_param(hp.varspez, paradef, p.proccalloption) or
  288. is_open_array(paradef) or
  289. is_array_of_const(paradef) then begin
  290. paradef := voidpointertype.def;
  291. loc := LOC_REGISTER;
  292. paracgsize := OS_ADDR;
  293. paralen := tcgsize2size[OS_ADDR];
  294. end else begin
  295. if not is_special_array(paradef) then
  296. paralen := paradef.size
  297. else
  298. paralen := tcgsize2size[def_cgsize(paradef)];
  299. if (paradef.deftype = recorddef) and
  300. (hp.varspez in [vs_value, vs_const]) then begin
  301. { if a record has only one field and that field is }
  302. { non-composite (not array or record), it must be }
  303. { passed according to the rules of that type. }
  304. if (trecorddef(hp.vartype.def).symtable.symindex.count = 1) and
  305. (not trecorddef(hp.vartype.def).isunion) and
  306. (tabstractvarsym(trecorddef(hp.vartype.def).symtable.symindex.search(1)).vartype.def.deftype = floatdef) then begin
  307. paradef :=
  308. tabstractvarsym(trecorddef(hp.vartype.def).symtable.symindex.search(1)).vartype.def;
  309. loc := getparaloc(paradef);
  310. paracgsize := def_cgsize(paradef);
  311. end else begin
  312. loc := LOC_REGISTER;
  313. paracgsize := int_cgsize(paralen);
  314. end;
  315. end else begin
  316. loc := getparaloc(paradef);
  317. paracgsize := def_cgsize(paradef);
  318. { for things like formaldef }
  319. if (paracgsize = OS_NO) then begin
  320. paracgsize := OS_ADDR;
  321. paralen := tcgsize2size[OS_ADDR];
  322. end;
  323. end
  324. end;
  325. hp.paraloc[side].alignment := std_param_align;
  326. hp.paraloc[side].size := paracgsize;
  327. hp.paraloc[side].intsize := paralen;
  328. if (paralen = 0) then
  329. if (paradef.deftype = recorddef) then begin
  330. paraloc := hp.paraloc[side].add_location;
  331. paraloc^.loc := LOC_VOID;
  332. end else
  333. internalerror(2005011310);
  334. { can become < 0 for e.g. 3-byte records }
  335. while (paralen > 0) do begin
  336. paraloc := hp.paraloc[side].add_location;
  337. if (loc = LOC_REGISTER) and
  338. (nextintreg <= RS_R10) then begin
  339. paraloc^.loc := loc;
  340. { make sure we don't lose whether or not the type is signed }
  341. if (paradef.deftype <> orddef) then
  342. paracgsize := int_cgsize(paralen);
  343. if (paracgsize in [OS_NO]) then
  344. paraloc^.size := OS_INT
  345. else
  346. paraloc^.size := paracgsize;
  347. paraloc^.register := newreg(R_INTREGISTER, nextintreg, R_SUBNONE);
  348. inc(nextintreg);
  349. dec(paralen, tcgsize2size[paraloc^.size]);
  350. inc(stack_offset, tcgsize2size[paraloc^.size]);
  351. end else if (loc = LOC_FPUREGISTER) and
  352. (nextfloatreg <= maxfpureg) then begin
  353. paraloc^.loc := loc;
  354. paraloc^.size := paracgsize;
  355. paraloc^.register := newreg(R_FPUREGISTER, nextfloatreg, R_SUBWHOLE);
  356. { the PPC64 ABI says that the GPR index is increased for every parameter, no matter
  357. which type it is stored in }
  358. inc(nextintreg);
  359. inc(nextfloatreg);
  360. dec(paralen, tcgsize2size[paraloc^.size]);
  361. inc(stack_offset, tcgsize2size[paraloc^.size]);
  362. end else if (loc = LOC_MMREGISTER) then begin
  363. { Altivec not supported }
  364. internalerror(200510192);
  365. end else begin
  366. { either LOC_REFERENCE, or one of the above which must be passed on the
  367. stack because of insufficient registers }
  368. paraloc^.loc := LOC_REFERENCE;
  369. paraloc^.size := int_cgsize(paralen);
  370. if (side = callerside) then
  371. paraloc^.reference.index := NR_STACK_POINTER_REG
  372. else
  373. { during procedure entry, R12 contains the old stack pointer }
  374. paraloc^.reference.index := NR_R12;
  375. paraloc^.reference.offset := stack_offset;
  376. { TODO: change this to the next power of two (natural alignment) }
  377. inc(stack_offset, align(paralen, 8));
  378. paralen := 0;
  379. end;
  380. end;
  381. end;
  382. curintreg := nextintreg;
  383. curfloatreg := nextfloatreg;
  384. curmmreg := nextmmreg;
  385. cur_stack_offset := stack_offset;
  386. result := stack_offset;
  387. end;
  388. function tppcparamanager.create_varargs_paraloc_info(p: tabstractprocdef;
  389. varargspara: tvarargsparalist): longint;
  390. var
  391. cur_stack_offset: aword;
  392. parasize, l: longint;
  393. curintreg, firstfloatreg, curfloatreg, curmmreg: tsuperregister;
  394. i: integer;
  395. hp: tparavarsym;
  396. paraloc: pcgparalocation;
  397. begin
  398. init_values(curintreg, curfloatreg, curmmreg, cur_stack_offset);
  399. firstfloatreg := curfloatreg;
  400. result := create_paraloc_info_intern(p, callerside, p.paras, curintreg,
  401. curfloatreg, curmmreg, cur_stack_offset);
  402. if (p.proccalloption in [pocall_cdecl, pocall_cppdecl]) then
  403. { just continue loading the parameters in the registers }
  404. begin
  405. result := create_paraloc_info_intern(p, callerside, varargspara, curintreg,
  406. curfloatreg, curmmreg, cur_stack_offset);
  407. { varargs routines have to reserve at least 64 bytes for the AIX abi }
  408. if (result < 64) then
  409. result := 64;
  410. end
  411. else
  412. begin
  413. parasize := cur_stack_offset;
  414. for i := 0 to varargspara.count - 1 do
  415. begin
  416. hp := tparavarsym(varargspara[i]);
  417. hp.paraloc[callerside].alignment := 8;
  418. paraloc := hp.paraloc[callerside].add_location;
  419. paraloc^.loc := LOC_REFERENCE;
  420. paraloc^.size := def_cgsize(hp.vartype.def);
  421. paraloc^.reference.index := NR_STACK_POINTER_REG;
  422. l := push_size(hp.varspez, hp.vartype.def, p.proccalloption);
  423. paraloc^.reference.offset := parasize;
  424. parasize := parasize + l;
  425. end;
  426. result := parasize;
  427. end;
  428. if curfloatreg <> firstfloatreg then
  429. include(varargspara.varargsinfo, va_uses_float_reg);
  430. end;
  431. function tppcparamanager.parseparaloc(p: tparavarsym; const s: string): boolean;
  432. begin
  433. { not supported/required for PowerPC64-linux target }
  434. internalerror(200404182);
  435. result := true;
  436. end;
  437. begin
  438. paramanager := tppcparamanager.create;
  439. end.