cpupara.pas 15 KB

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