cpupara.pas 16 KB

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