cpupara.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. RiscV64 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. tcpuparamanager = class(tparamanager)
  28. function get_volatile_registers_int(calloption: tproccalloption): tcpuregisterset; override;
  29. function get_volatile_registers_fpu(calloption: tproccalloption): tcpuregisterset; override;
  30. function push_addr_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean; override;
  31. function ret_in_param(def: tdef; pd: tabstractprocdef): boolean; override;
  32. procedure getcgtempparaloc(list: TAsmList; pd : tabstractprocdef; nr: longint; var cgpara: tcgpara); override;
  33. function create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint; override;
  34. function create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee; varargspara: tvarargsparalist): longint; override;
  35. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  36. function get_saved_registers_fpu(calloption: tproccalloption): tcpuregisterarray;override;
  37. function get_saved_registers_int(calloption: tproccalloption): tcpuregisterarray;override;
  38. private
  39. procedure init_values(var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword);
  40. function create_paraloc_info_intern(p: tabstractprocdef; side: tcallercallee; paras: tparalist; var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword; isVararg : boolean): longint;
  41. function parseparaloc(p: tparavarsym; const s: string): boolean; override;
  42. procedure create_paraloc_for_def(var para: TCGPara; varspez: tvarspez; paradef: tdef; var nextfloatreg, nextintreg: tsuperregister; var stack_offset: aword; const isVararg, forceintmem: boolean; const side: tcallercallee; const p: tabstractprocdef);
  43. end;
  44. implementation
  45. uses
  46. verbose, systems,
  47. globals, cpuinfo,
  48. defutil,symtable,symcpu,
  49. procinfo, cpupi;
  50. function tcpuparamanager.get_volatile_registers_int(calloption: tproccalloption): tcpuregisterset;
  51. begin
  52. result:=[RS_X0..RS_X31]-[RS_X2,RS_X8..RS_X9,RS_X18..RS_X27];
  53. end;
  54. function tcpuparamanager.get_volatile_registers_fpu(calloption: tproccalloption): tcpuregisterset;
  55. begin
  56. result:=[RS_F0..RS_F31]-[RS_F8..RS_F9,RS_F18..RS_F27];
  57. end;
  58. function tcpuparamanager.get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;
  59. const
  60. saved_regs: tcpuregisterarray = (RS_X2,RS_X8,RS_X9,RS_X18,RS_X19,RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X26,RS_X26,RS_X27);
  61. begin
  62. result:=saved_regs;
  63. end;
  64. function tcpuparamanager.get_saved_registers_fpu(calloption : tproccalloption):tcpuregisterarray;
  65. const
  66. saved_regs: tcpuregisterarray = (RS_F8,RS_F9,RS_F18,RS_F19,RS_F20,RS_F21,RS_F22,RS_F23,RS_F24,RS_F25,RS_F26,RS_F27);
  67. begin
  68. result:=saved_regs;
  69. end;
  70. procedure tcpuparamanager.getcgtempparaloc(list: TAsmList; pd : tabstractprocdef; nr: longint; var cgpara: tcgpara);
  71. var
  72. paraloc: pcgparalocation;
  73. psym: tparavarsym;
  74. pdef: tdef;
  75. begin
  76. psym:=tparavarsym(pd.paras[nr-1]);
  77. pdef:=psym.vardef;
  78. if push_addr_param(psym.varspez,pdef,pd.proccalloption) then
  79. pdef:=cpointerdef.getreusable_no_free(pdef);
  80. cgpara.reset;
  81. cgpara.size := def_cgsize(pdef);
  82. cgpara.intsize := tcgsize2size[cgpara.size];
  83. cgpara.alignment := get_para_align(pd.proccalloption);
  84. cgpara.def:=pdef;
  85. paraloc := cgpara.add_location;
  86. with paraloc^ do begin
  87. size := def_cgsize(pdef);
  88. def := pdef;
  89. if (nr <= 8) then begin
  90. if (nr = 0) then
  91. internalerror(200309271);
  92. loc := LOC_REGISTER;
  93. register := newreg(R_INTREGISTER, RS_X10 + nr-1, R_SUBWHOLE);
  94. end else begin
  95. loc := LOC_REFERENCE;
  96. paraloc^.reference.index := NR_STACK_POINTER_REG;
  97. reference.offset := sizeof(aint) * (nr - 9);
  98. end;
  99. end;
  100. end;
  101. function getparaloc(p: tdef): tcgloc;
  102. begin
  103. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  104. if push_addr_param for the def is true
  105. }
  106. case p.typ of
  107. orddef:
  108. result := LOC_REGISTER;
  109. floatdef:
  110. if (cs_fp_emulation in current_settings.moduleswitches) or
  111. (current_settings.fputype in [fpu_soft]) then
  112. result := LOC_REGISTER
  113. else
  114. result := LOC_FPUREGISTER;
  115. enumdef:
  116. result := LOC_REGISTER;
  117. pointerdef:
  118. result := LOC_REGISTER;
  119. formaldef:
  120. result := LOC_REGISTER;
  121. classrefdef:
  122. result := LOC_REGISTER;
  123. procvardef,
  124. recorddef:
  125. result := LOC_REGISTER;
  126. objectdef:
  127. if is_object(p) then
  128. result := LOC_REFERENCE
  129. else
  130. result := LOC_REGISTER;
  131. stringdef:
  132. if is_shortstring(p) or is_longstring(p) then
  133. result := LOC_REFERENCE
  134. else
  135. result := LOC_REGISTER;
  136. filedef:
  137. result := LOC_REGISTER;
  138. arraydef:
  139. if is_dynamic_array(p) then
  140. getparaloc:=LOC_REGISTER
  141. else
  142. result := LOC_REFERENCE;
  143. setdef:
  144. if is_smallset(p) then
  145. result := LOC_REGISTER
  146. else
  147. result := LOC_REFERENCE;
  148. variantdef:
  149. result := LOC_REFERENCE;
  150. { avoid problems with errornous definitions }
  151. errordef:
  152. result := LOC_REGISTER;
  153. else
  154. internalerror(2002071001);
  155. end;
  156. end;
  157. function tcpuparamanager.push_addr_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  158. begin
  159. result := false;
  160. { var,out,constref always require address }
  161. if varspez in [vs_var, vs_out, vs_constref] then
  162. begin
  163. result := true;
  164. exit;
  165. end;
  166. case def.typ of
  167. variantdef,
  168. formaldef:
  169. result := true;
  170. procvardef,
  171. recorddef:
  172. result := (def.size > 16);
  173. arraydef:
  174. result := (tarraydef(def).highrange >= tarraydef(def).lowrange) or
  175. is_open_array(def) or
  176. is_array_of_const(def) or
  177. is_array_constructor(def);
  178. objectdef:
  179. result := is_object(def);
  180. setdef:
  181. result := not is_smallset(def);
  182. stringdef:
  183. result := tstringdef(def).stringtype in [st_shortstring, st_longstring];
  184. else
  185. ;
  186. end;
  187. end;
  188. function tcpuparamanager.ret_in_param(def: tdef; pd: tabstractprocdef): boolean;
  189. var
  190. tmpdef: tdef;
  191. begin
  192. if handle_common_ret_in_param(def,pd,result) then
  193. exit;
  194. { general rule: passed in registers -> returned in registers }
  195. result:=push_addr_param(vs_value,def,pd.proccalloption);
  196. end;
  197. procedure tcpuparamanager.init_values(var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword);
  198. begin
  199. { register parameter save area begins at 48(r2) }
  200. cur_stack_offset := 0;
  201. curintreg := RS_X10;
  202. curfloatreg := RS_F10;
  203. curmmreg := RS_NO;
  204. end;
  205. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  206. var
  207. paraloc: pcgparalocation;
  208. retcgsize: tcgsize;
  209. nextfloatreg, nextintreg, nextmmreg: tsuperregister;
  210. stack_offset: aword;
  211. begin
  212. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  213. exit;
  214. { in this case, it must be returned in registers as if it were passed
  215. as the first parameter }
  216. init_values(nextintreg,nextfloatreg,nextmmreg,stack_offset);
  217. create_paraloc_for_def(result,vs_value,result.def,nextfloatreg,nextintreg,stack_offset,false,false,side,p);
  218. { sanity check (LOC_VOID for empty records) }
  219. if not assigned(result.location) or
  220. not(result.location^.loc in [LOC_REGISTER,LOC_FPUREGISTER,LOC_VOID]) then
  221. internalerror(2014113001);
  222. end;
  223. function tcpuparamanager.create_paraloc_info(p: tabstractprocdef; side: tcallercallee): longint;
  224. var
  225. cur_stack_offset: aword;
  226. curintreg, curfloatreg, curmmreg : tsuperregister;
  227. begin
  228. init_values(curintreg, curfloatreg, curmmreg, cur_stack_offset);
  229. result := create_paraloc_info_intern(p, side, p.paras, curintreg, curfloatreg, curmmreg, cur_stack_offset, false);
  230. create_funcretloc_info(p, side);
  231. end;
  232. function tcpuparamanager.create_paraloc_info_intern(p: tabstractprocdef; side: tcallercallee; paras: tparalist; var curintreg, curfloatreg, curmmreg: tsuperregister; var cur_stack_offset: aword; isVararg : boolean): longint;
  233. var
  234. nextintreg, nextfloatreg, nextmmreg : tsuperregister;
  235. i: integer;
  236. hp: tparavarsym;
  237. paraloc: pcgparalocation;
  238. delphi_nestedfp: boolean;
  239. begin
  240. {$IFDEF extdebug}
  241. if po_explicitparaloc in p.procoptions then
  242. internalerror(200411141);
  243. {$ENDIF extdebug}
  244. result := 0;
  245. nextintreg := curintreg;
  246. nextfloatreg := curfloatreg;
  247. nextmmreg := curmmreg;
  248. for i := 0 to paras.count - 1 do begin
  249. hp := tparavarsym(paras[i]);
  250. if (vo_has_explicit_paraloc in hp.varoptions) then begin
  251. internalerror(200412153);
  252. end;
  253. { currently only support C-style array of const }
  254. if (p.proccalloption in [pocall_cdecl, pocall_cppdecl]) and
  255. is_array_of_const(hp.vardef) then begin
  256. paraloc := hp.paraloc[side].add_location;
  257. { hack: the paraloc must be valid, but is not actually used }
  258. paraloc^.loc := LOC_REGISTER;
  259. paraloc^.register := NR_X0;
  260. paraloc^.size := OS_ADDR;
  261. paraloc^.def := voidpointertype;
  262. break;
  263. end;
  264. delphi_nestedfp:=(vo_is_parentfp in hp.varoptions) and (po_delphi_nested_cc in p.procoptions);
  265. create_paraloc_for_def(hp.paraloc[side], hp.varspez, hp.vardef,
  266. nextfloatreg, nextintreg, cur_stack_offset, isVararg, delphi_nestedfp, side, p);
  267. end;
  268. curintreg := nextintreg;
  269. curfloatreg := nextfloatreg;
  270. curmmreg := nextmmreg;
  271. result := cur_stack_offset;
  272. end;
  273. procedure tcpuparamanager.create_paraloc_for_def(var para: TCGPara; varspez: tvarspez; paradef: tdef; var nextfloatreg, nextintreg: tsuperregister; var stack_offset: aword; const isVararg, forceintmem: boolean; const side: tcallercallee; const p: tabstractprocdef);
  274. var
  275. paracgsize: tcgsize;
  276. loc: tcgloc;
  277. paraloc: pcgparalocation;
  278. { def to use for all paralocs if <> nil }
  279. alllocdef,
  280. { def to use for the current paraloc }
  281. locdef,
  282. tmpdef: tdef;
  283. paralen: aint;
  284. firstparaloc,
  285. paraaligned: boolean;
  286. begin
  287. alllocdef:=nil;
  288. locdef:=nil;
  289. para.reset;
  290. { have we ensured that the next parameter location will be aligned to the
  291. next 8 byte boundary? }
  292. paraaligned:=false;
  293. if push_addr_param(varspez, paradef, p.proccalloption) then begin
  294. paradef := cpointerdef.getreusable_no_free(paradef);
  295. loc := LOC_REGISTER;
  296. paracgsize := OS_ADDR;
  297. paralen := tcgsize2size[OS_ADDR];
  298. end else begin
  299. if not is_special_array(paradef) then
  300. paralen := paradef.size
  301. else
  302. paralen := tcgsize2size[def_cgsize(paradef)];
  303. if (paradef.typ=recorddef) and
  304. tabstractrecordsymtable(tabstractrecorddef(paradef).symtable).has_single_field(tmpdef) and
  305. (tmpdef.typ=floatdef) then
  306. begin
  307. paradef:=tmpdef;
  308. loc:=getparaloc(paradef);
  309. paracgsize:=def_cgsize(paradef)
  310. end
  311. else if (((paradef.typ=arraydef) and not
  312. is_special_array(paradef)) or
  313. (paradef.typ=recorddef)) then
  314. begin
  315. { general fallback rule: pass aggregate types in integer registers
  316. without special adjustments (incl. Darwin h) }
  317. loc:=LOC_REGISTER;
  318. paracgsize:=int_cgsize(paralen);
  319. end
  320. else
  321. begin
  322. loc:=getparaloc(paradef);
  323. paracgsize:=def_cgsize(paradef);
  324. { for things like formaldef }
  325. if (paracgsize=OS_NO) then
  326. begin
  327. paracgsize:=OS_ADDR;
  328. paralen:=tcgsize2size[OS_ADDR];
  329. end;
  330. end
  331. end;
  332. { patch FPU values into integer registers if we are processing varargs }
  333. if (isVararg) and (paradef.typ = floatdef) then begin
  334. loc := LOC_REGISTER;
  335. if paracgsize = OS_F64 then
  336. paracgsize := OS_64
  337. else
  338. paracgsize := OS_32;
  339. end;
  340. para.alignment := std_param_align;
  341. para.size := paracgsize;
  342. para.intsize := paralen;
  343. para.def := paradef;
  344. if (paralen = 0) then
  345. if (paradef.typ = recorddef) then begin
  346. paraloc := para.add_location;
  347. paraloc^.loc := LOC_VOID;
  348. end else
  349. internalerror(2005011310);
  350. if not assigned(alllocdef) then
  351. locdef:=paradef
  352. else
  353. begin
  354. locdef:=alllocdef;
  355. paracgsize:=def_cgsize(locdef);
  356. end;
  357. firstparaloc:=true;
  358. // Parameters passed in 2 registers are passed in a register starting with an even number.
  359. if isVararg and
  360. (paralen > 8) and
  361. (loc = LOC_REGISTER) and
  362. (nextintreg <= RS_X17) and
  363. odd(nextintreg) then
  364. inc(nextintreg);
  365. { can become < 0 for e.g. 3-byte records }
  366. while (paralen > 0) do begin
  367. paraloc := para.add_location;
  368. { In case of po_delphi_nested_cc, the parent frame pointer
  369. is always passed on the stack. }
  370. if (loc = LOC_REGISTER) and
  371. (nextintreg <= RS_X17) and
  372. not forceintmem then begin
  373. paraloc^.loc := loc;
  374. { make sure we don't lose whether or not the type is signed }
  375. if (paracgsize <> OS_NO) and
  376. (paradef.typ <> orddef) and
  377. not assigned(alllocdef) then
  378. begin
  379. paracgsize := int_cgsize(paralen);
  380. locdef:=get_paraloc_def(paradef, paralen, firstparaloc);
  381. end;
  382. if (paracgsize in [OS_NO, OS_128, OS_S128]) then
  383. begin
  384. if (paralen>4) then
  385. begin
  386. paraloc^.size := OS_INT;
  387. paraloc^.def := osuinttype;
  388. end
  389. else
  390. begin
  391. { for 3-byte records aligned in the lower bits of register }
  392. paraloc^.size := OS_32;
  393. paraloc^.def := u32inttype;
  394. end;
  395. end
  396. else
  397. begin
  398. paraloc^.size := paracgsize;
  399. paraloc^.def := locdef;
  400. end;
  401. paraloc^.register := newreg(R_INTREGISTER, nextintreg, R_SUBNONE);
  402. inc(nextintreg);
  403. dec(paralen, tcgsize2size[paraloc^.size]);
  404. end else if (loc = LOC_FPUREGISTER) and
  405. (nextfloatreg <= RS_F17) then begin
  406. paraloc^.loc := loc;
  407. paraloc^.size := paracgsize;
  408. paraloc^.def := locdef;
  409. paraloc^.register := newreg(R_FPUREGISTER, nextfloatreg, R_SUBWHOLE);
  410. { the RiscV ABI says that the GPR index is increased for every parameter, no matter
  411. which type it is stored in
  412. not really, https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#hardware-floating-point-calling-convention says
  413. otherwise, gcc doesn't do it either }
  414. inc(nextfloatreg);
  415. dec(paralen, tcgsize2size[paraloc^.size]);
  416. end else if (loc = LOC_MMREGISTER) then begin
  417. { no mm registers }
  418. internalerror(2018072601);
  419. end else begin
  420. { either LOC_REFERENCE, or one of the above which must be passed on the
  421. stack because of insufficient registers }
  422. paraloc^.loc := LOC_REFERENCE;
  423. case loc of
  424. LOC_FPUREGISTER:
  425. begin
  426. paraloc^.size:=int_float_cgsize(paralen);
  427. case paraloc^.size of
  428. OS_F32: paraloc^.def:=s32floattype;
  429. OS_F64: paraloc^.def:=s64floattype;
  430. else
  431. internalerror(2013060122);
  432. end;
  433. end;
  434. LOC_REGISTER,
  435. LOC_REFERENCE:
  436. begin
  437. paraloc^.size:=int_cgsize(paralen);
  438. paraloc^.def:=get_paraloc_def(paradef, paralen, firstparaloc);
  439. end;
  440. else
  441. internalerror(2006011101);
  442. end;
  443. if (side = callerside) then
  444. paraloc^.reference.index := NR_STACK_POINTER_REG
  445. else begin
  446. { during procedure entry, NR_OLD_STACK_POINTER_REG contains the old stack pointer }
  447. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  448. { create_paraloc_info_intern might be also called when being outside of
  449. code generation so current_procinfo might be not set }
  450. if assigned(current_procinfo) then
  451. trv64procinfo(current_procinfo).needs_frame_pointer := true;
  452. end;
  453. paraloc^.reference.offset := stack_offset;
  454. { align temp contents to next register size }
  455. if not paraaligned then
  456. inc(stack_offset, align(paralen, 8))
  457. else
  458. inc(stack_offset, paralen);
  459. paralen := 0;
  460. end;
  461. firstparaloc:=false;
  462. end;
  463. end;
  464. function tcpuparamanager.create_varargs_paraloc_info(p: tabstractprocdef; side: tcallercallee;
  465. varargspara: tvarargsparalist): longint;
  466. var
  467. cur_stack_offset: aword;
  468. parasize, l: longint;
  469. curintreg, firstfloatreg, curfloatreg, curmmreg: tsuperregister;
  470. i: integer;
  471. hp: tparavarsym;
  472. paraloc: pcgparalocation;
  473. begin
  474. init_values(curintreg, curfloatreg, curmmreg, cur_stack_offset);
  475. firstfloatreg := curfloatreg;
  476. result := create_paraloc_info_intern(p, side, p.paras, curintreg,
  477. curfloatreg, curmmreg, cur_stack_offset, false);
  478. if (p.proccalloption in [pocall_cdecl, pocall_cppdecl, pocall_mwpascal]) then
  479. begin
  480. { just continue loading the parameters in the registers }
  481. if assigned(varargspara) then
  482. begin
  483. if side=callerside then
  484. result := create_paraloc_info_intern(p, side, varargspara, curintreg,
  485. curfloatreg, curmmreg, cur_stack_offset, true)
  486. else
  487. internalerror(2019021918);
  488. if curfloatreg <> firstfloatreg then
  489. include(varargspara.varargsinfo, va_uses_float_reg);
  490. end;
  491. { varargs routines have to reserve at least 64 bytes for the RiscV ABI }
  492. if (result < 64) then
  493. result := 64;
  494. end
  495. else
  496. internalerror(2019021913);
  497. create_funcretloc_info(p, side);
  498. end;
  499. function tcpuparamanager.parseparaloc(p: tparavarsym; const s: string): boolean;
  500. begin
  501. internalerror(200404182);
  502. result := true;
  503. end;
  504. begin
  505. paramanager := tcpuparamanager.create;
  506. end.