cpupara.pas 16 KB

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