2
0

cpupara.pas 16 KB

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