cpupara.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and David Zhang
  3. Calling conventions for the MIPSEL
  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. unit cpupara;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. globtype,globals,
  21. cclasses,
  22. aasmtai,
  23. cpubase,cpuinfo,
  24. symconst,symbase,symsym,symtype,symdef,paramgr,parabase,cgbase,cgutils;
  25. const
  26. { The value below is OK for O32 and N32 calling conventions }
  27. MIPS_MAX_REGISTERS_USED_IN_CALL = 6;
  28. { All ABI seem to start with $4 i.e. $a0 }
  29. MIPS_FIRST_REGISTER_USED_IN_CALL = RS_R4;
  30. { O32 ABI uses $a0 to $a3, i.e R4 to R7 }
  31. MIPS_LAST_REGISTER_USED_IN_CALL_ABI_O32 = RS_R7;
  32. { N32 ABI uses also R8 and R9 }
  33. MIPS_LAST_REGISTER_USED_IN_CALL_ABI_N32 = RS_R9;
  34. { The calculation below is based on the assumption
  35. that all registers used for ABI calls are
  36. ordered and follow each other }
  37. MIPS_NB_REGISTERS_USED_IN_CALL_O32 =
  38. MIPS_LAST_REGISTER_USED_IN_CALL_ABI_O32
  39. - MIPS_FIRST_REGISTER_USED_IN_CALL + 1;
  40. MIPS_NB_REGISTERS_USED_IN_CALL_N32 =
  41. MIPS_LAST_REGISTER_USED_IN_CALL_ABI_N32
  42. - MIPS_FIRST_REGISTER_USED_IN_CALL + 1;
  43. { Set O32 ABI as default }
  44. const
  45. mips_nb_used_registers : longint = MIPS_NB_REGISTERS_USED_IN_CALL_O32;
  46. { Might need to be changed if we support N64 ABI later }
  47. {$ifdef MIPS64}
  48. mips_sizeof_register_param : longint = 8;
  49. {$else MIPS64}
  50. mips_sizeof_register_param : longint = 4;
  51. {$endif MIPS64}
  52. type
  53. tparasupregs = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of tsuperregister;
  54. tparasupregsused = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of boolean;
  55. const
  56. parasupregs : tparasupregs = (RS_R4, RS_R5, RS_R6, RS_R7, RS_R8, RS_R9);
  57. type
  58. tcpuparamanager=class(TParaManager)
  59. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  60. function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override;
  61. function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override;
  62. function get_saved_registers_int(calloption : tproccalloption):TCpuRegisterArray;override;
  63. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  64. function create_varargs_paraloc_info(p : TAbstractProcDef; side: tcallercallee; varargspara:tvarargsparalist):longint;override;
  65. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  66. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  67. private
  68. intparareg,
  69. intparasize : longint;
  70. can_use_float : boolean;
  71. function is_abi_record(def: tdef): boolean;
  72. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist);
  73. end;
  74. implementation
  75. uses
  76. cutils,verbose,systems,
  77. defutil, cpupi, procinfo,
  78. cgobj;
  79. function tcpuparamanager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  80. begin
  81. { O32 ABI values }
  82. result:=[RS_R1..RS_R15,RS_R24..RS_R25,RS_R31];
  83. end;
  84. function tcpuparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  85. begin
  86. { O32 ABI values }
  87. result:=[RS_F0..RS_F19];
  88. end;
  89. function tcpuparamanager.get_saved_registers_int(calloption : tproccalloption):TCpuRegisterArray;
  90. const
  91. saved_regs : tcpuregisterarray =
  92. (RS_NO);
  93. begin
  94. result:=saved_regs;
  95. end;
  96. { whether "def" must be treated as record when used as function result,
  97. i.e. its address passed in a0 }
  98. function tcpuparamanager.is_abi_record(def: tdef): boolean;
  99. begin
  100. result:=(def.typ=recorddef) or
  101. ((def.typ=procvardef) and not tprocvardef(def).is_addressonly);
  102. end;
  103. function tcpuparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  104. var
  105. paraloc: pcgparalocation;
  106. begin
  107. paraloc:=cgpara.location;
  108. if not assigned(paraloc) then
  109. internalerror(200410102);
  110. result:=(paraloc^.loc=LOC_REFERENCE) and (paraloc^.next=nil);
  111. end;
  112. { true if a parameter is too large to copy and only the address is pushed }
  113. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  114. begin
  115. result:=false;
  116. { var,out,constref always require address }
  117. if varspez in [vs_var,vs_out,vs_constref] then
  118. begin
  119. result:=true;
  120. exit;
  121. end;
  122. case def.typ of
  123. recorddef:
  124. { According to 032 ABI we should have }
  125. result:=false;
  126. arraydef:
  127. result:=true; {(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  128. is_open_array(def) or
  129. is_array_of_const(def) or
  130. is_array_constructor(def);}
  131. variantdef,
  132. formaldef :
  133. result:=true;
  134. objectdef :
  135. result:=is_object(def);
  136. stringdef :
  137. result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  138. procvardef :
  139. { If we always push records by value, we have to handle methodpointers that way too. }
  140. result:=false; {not tprocvardef(def).is_addressonly;}
  141. setdef :
  142. result:=not(is_smallset(def));
  143. else
  144. ;
  145. end;
  146. end;
  147. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  148. var
  149. paraloc : pcgparalocation;
  150. retcgsize : tcgsize;
  151. retdef : tdef;
  152. begin
  153. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  154. begin
  155. { Return is passed as var parameter,
  156. in this case we use the first register R4 for it }
  157. if assigned(forcetempdef) then
  158. retdef:=forcetempdef
  159. else
  160. retdef:=p.returndef;
  161. if ret_in_param(retdef,p) and
  162. is_abi_record(retdef) then
  163. begin
  164. if intparareg=0 then
  165. inc(intparareg);
  166. end;
  167. exit;
  168. end;
  169. paraloc:=result.add_location;
  170. { Return in FPU register? }
  171. if result.def.typ=floatdef then
  172. begin
  173. if (p.proccalloption in [pocall_softfloat]) or (cs_fp_emulation in current_settings.moduleswitches) then
  174. begin
  175. case retcgsize of
  176. OS_64,
  177. OS_F64:
  178. begin
  179. { low }
  180. paraloc^.loc:=LOC_REGISTER;
  181. if side=callerside then
  182. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  183. else
  184. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  185. paraloc^.size:=OS_32;
  186. paraloc^.def:=u32inttype;
  187. { high }
  188. paraloc:=result.add_location;
  189. paraloc^.loc:=LOC_REGISTER;
  190. if side=callerside then
  191. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  192. else
  193. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  194. paraloc^.size:=OS_32;
  195. paraloc^.def:=u32inttype;
  196. end;
  197. OS_32,
  198. OS_F32:
  199. begin
  200. paraloc^.loc:=LOC_REGISTER;
  201. if side=callerside then
  202. paraloc^.register:=NR_FUNCTION_RESULT_REG
  203. else
  204. paraloc^.register:=NR_FUNCTION_RETURN_REG;
  205. paraloc^.size:=OS_32;
  206. paraloc^.def:=u32inttype;
  207. end;
  208. else
  209. internalerror(2024092901);
  210. end;
  211. end
  212. else
  213. begin
  214. paraloc^.loc:=LOC_FPUREGISTER;
  215. paraloc^.register:=NR_FPU_RESULT_REG;
  216. if retcgsize=OS_F64 then
  217. setsubreg(paraloc^.register,R_SUBFD);
  218. paraloc^.size:=retcgsize;
  219. paraloc^.def:=result.def;
  220. end;
  221. end
  222. else
  223. { Return in register }
  224. begin
  225. {$ifndef cpu64bitalu}
  226. if retcgsize in [OS_64,OS_S64] then
  227. begin
  228. { low }
  229. paraloc^.loc:=LOC_REGISTER;
  230. if side=callerside then
  231. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  232. else
  233. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  234. paraloc^.size:=OS_32;
  235. paraloc^.def:=u32inttype;
  236. { high }
  237. paraloc:=result.add_location;
  238. paraloc^.loc:=LOC_REGISTER;
  239. if side=callerside then
  240. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  241. else
  242. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  243. paraloc^.size:=OS_32;
  244. paraloc^.def:=u32inttype;
  245. end
  246. else
  247. {$endif cpu64bitalu}
  248. begin
  249. paraloc^.loc:=LOC_REGISTER;
  250. paraloc^.size:=retcgsize;
  251. paraloc^.def:=result.def;
  252. if side=callerside then
  253. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  254. else
  255. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  256. end;
  257. end
  258. end;
  259. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist);
  260. var
  261. paraloc : pcgparalocation;
  262. i,j : integer;
  263. hp : tparavarsym;
  264. paracgsize : tcgsize;
  265. paralen : longint;
  266. locdef : tdef;
  267. paradef : tdef;
  268. fpparareg : integer;
  269. reg : tsuperregister;
  270. alignment : longint;
  271. tmp : longint;
  272. firstparaloc : boolean;
  273. reg_and_stack: boolean;
  274. begin
  275. fpparareg := 0;
  276. for i:=0 to paras.count-1 do
  277. begin
  278. hp:=tparavarsym(paras[i]);
  279. paradef := hp.vardef;
  280. { currently only support C-style array of const }
  281. if (p.proccalloption in cstylearrayofconst) and
  282. is_array_of_const(paradef) then
  283. begin
  284. paraloc:=hp.paraloc[side].add_location;
  285. { hack: the paraloc must be valid, but is not actually used }
  286. paraloc^.loc:=LOC_REGISTER;
  287. paraloc^.register:=NR_R0;
  288. paraloc^.size:=OS_ADDR;
  289. paraloc^.def:=voidpointertype;
  290. break;
  291. end;
  292. if push_addr_param(hp.varspez,paradef,p.proccalloption) then
  293. begin
  294. paracgsize := OS_ADDR;
  295. paralen := tcgsize2size[paracgsize];
  296. paradef := cpointerdef.getreusable_no_free(paradef);
  297. end
  298. else
  299. begin
  300. paracgsize := def_cgsize(paradef);
  301. { for things like formaldef }
  302. if (paracgsize=OS_NO) and (paradef.typ <> recorddef) then
  303. begin
  304. paracgsize:=OS_ADDR;
  305. paradef:=voidpointertype;
  306. end;
  307. if not is_special_array(paradef) then
  308. paralen := paradef.size
  309. else
  310. paralen := tcgsize2size[paracgsize];
  311. end;
  312. if (paracgsize in [OS_64, OS_S64, OS_F64]) or (paradef.alignment = 8) then
  313. alignment := 8
  314. else
  315. alignment := 4;
  316. //writeln('para: ',hp.Name,' typ=',hp.vardef.typ,' paracgsize=',paracgsize,' align=',hp.vardef.alignment);
  317. hp.paraloc[side].reset;
  318. hp.paraloc[side].Alignment:=alignment;
  319. locdef:=paradef;
  320. if (paracgsize=OS_NO) or
  321. { Ordinals on caller side must be promoted to machine word }
  322. ((target_info.endian=endian_big) and // applies to little-endian too?
  323. (paradef.typ<>recorddef) and
  324. (side=callerside) and
  325. (paralen<tcgsize2size[OS_INT]))then
  326. begin
  327. if is_signed(paradef) then
  328. begin
  329. paracgsize:=OS_S32;
  330. locdef:=s32inttype;
  331. end
  332. else
  333. begin
  334. paracgsize:=OS_32;
  335. locdef:=u32inttype;
  336. end;
  337. paralen:=align(paralen,4);
  338. end
  339. else
  340. paralen:=tcgsize2size[paracgsize];
  341. hp.paraloc[side].intsize:=paralen;
  342. hp.paraloc[side].size:=paracgsize;
  343. hp.paraloc[side].def:=paradef;
  344. if (paralen=0) then
  345. if (paradef.typ=recorddef) then
  346. begin
  347. paraloc:=hp.paraloc[side].add_location;
  348. paraloc^.loc:=LOC_VOID;
  349. end
  350. else
  351. internalerror(2013020601);
  352. { check the alignment, mips O32ABI require a nature alignment }
  353. tmp := align(intparasize, alignment) - intparasize;
  354. while tmp > 0 do
  355. begin
  356. inc(intparareg);
  357. inc(intparasize,4);
  358. dec(tmp,4);
  359. end;
  360. { any non-float args will disable the use the floating regs }
  361. { up to two fp args }
  362. if (not(paracgsize in [OS_F32, OS_F64])) or (fpparareg = 2) then
  363. can_use_float := false;
  364. firstparaloc:=true;
  365. { Is parameter split between stack and registers? }
  366. reg_and_stack:=(side=calleeside) and
  367. (paralen+intparasize>16) and (intparasize<16);
  368. while paralen>0 do
  369. begin
  370. paraloc:=hp.paraloc[side].add_location;
  371. {$ifdef cpu64bitalu}
  372. if paracgsize in [OS_128,OS_S128] then
  373. begin
  374. if paralen>4 then
  375. begin
  376. paraloc^.size:=OS_64;
  377. paraloc^.def:=u64inttype;
  378. end
  379. end
  380. else
  381. {$else cpu64bitalu}
  382. { We can allocate at maximum 32 bits per register on mips32 }
  383. if (paracgsize in [OS_64,OS_S64]) or
  384. ((paracgsize in [OS_F32,OS_F64]) and
  385. not(can_use_float)) then
  386. begin
  387. paraloc^.size:=OS_32;
  388. paraloc^.def:=u32inttype;
  389. end
  390. else
  391. {$endif cpu64bitalu}
  392. begin
  393. paraloc^.size:=paracgsize;
  394. paraloc^.def:=locdef;
  395. end;
  396. { ret in param? }
  397. if (vo_is_funcret in hp.varoptions) and
  398. is_abi_record(hp.vardef) then
  399. begin
  400. { This should be the first parameter }
  401. //if (intparareg<>1) then
  402. // Comment(V_Warning,'intparareg should be one for funcret in tcpuparamanager.create_paraloc_info_intern');
  403. paraloc^.loc:=LOC_REGISTER;
  404. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[0],R_SUBWHOLE);
  405. inc(intparasize,align(tcgsize2size[paraloc^.size],sizeof(aint)));
  406. end
  407. { "In case of po_delphi_nested_cc, the parent frame pointer
  408. is always passed on the stack". On other targets it is
  409. used to provide caller-side stack cleanup and prevent stackframe
  410. optimization. For MIPS this does not matter. }
  411. else if (intparareg<mips_nb_used_registers) and
  412. (not reg_and_stack) {and
  413. (not(vo_is_parentfp in hp.varoptions) or
  414. not(po_delphi_nested_cc in p.procoptions))} then
  415. begin
  416. if (can_use_float) then
  417. begin
  418. paraloc^.loc:=LOC_FPUREGISTER;
  419. if (fpparareg = 0) then
  420. reg := RS_F12
  421. else
  422. reg := RS_F14;
  423. if (paraloc^.size = OS_F64) then
  424. begin
  425. paraloc^.register:=newreg(R_FPUREGISTER, reg, R_SUBFD);
  426. inc(fpparareg);
  427. inc(intparareg);
  428. inc(intparareg);
  429. inc(intparasize,8);
  430. end
  431. else
  432. begin
  433. paraloc^.register:=newreg(R_FPUREGISTER, reg, R_SUBFS);
  434. inc(fpparareg);
  435. inc(intparareg);
  436. inc(intparasize,sizeof(aint));
  437. end;
  438. end
  439. else { not can use float }
  440. begin
  441. paraloc^.loc:=LOC_REGISTER;
  442. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[intparareg],R_SUBWHOLE);
  443. { big-endian targets require that record data stored in parameter
  444. registers is left-aligned }
  445. if (target_info.endian=endian_big) and
  446. (paradef.typ=recorddef) and
  447. (paralen<tcgsize2size[OS_INT]) then
  448. begin
  449. paraloc^.shiftval := (sizeof(aint)-tcgsize2size[paraloc^.size])*(-8);
  450. paraloc^.size := OS_INT;
  451. paraloc^.def := osuinttype;
  452. end;
  453. inc(intparareg);
  454. inc(intparasize,align(tcgsize2size[paraloc^.size],mips_sizeof_register_param));
  455. end;
  456. end
  457. else
  458. begin
  459. if reg_and_stack then
  460. begin
  461. for j:=intparareg to mips_nb_used_registers-1 do
  462. tcpuprocinfo(current_procinfo).register_used[j]:=true;
  463. { all registers used now }
  464. intparareg:=mips_nb_used_registers;
  465. end;
  466. paraloc^.loc:=LOC_REFERENCE;
  467. if (paradef.typ=floatdef) then
  468. paraloc^.size:=int_float_cgsize(paralen)
  469. else
  470. paraloc^.size:=int_cgsize(paralen);
  471. paraloc^.def:=get_paraloc_def(locdef,paralen,firstparaloc);
  472. if side=callerside then
  473. begin
  474. paraloc^.reference.index := NR_STACK_POINTER_REG;
  475. paraloc^.reference.offset:=intparasize;
  476. end
  477. else
  478. begin
  479. if (po_nostackframe in p.procoptions) then
  480. paraloc^.reference.index := NR_STACK_POINTER_REG
  481. else
  482. begin
  483. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  484. if assigned(current_procinfo) then
  485. include(current_procinfo.flags,pi_needs_stackframe);
  486. end;
  487. paraloc^.reference.offset:=intparasize;
  488. if (target_info.endian=endian_big) and
  489. (paralen<tcgsize2size[OS_INT]) and
  490. (paradef.typ<>recorddef) then
  491. inc(paraloc^.reference.offset,4-paralen);
  492. end;
  493. inc(intparasize,align(paralen,mips_sizeof_register_param));
  494. paralen:=0;
  495. end;
  496. dec(paralen,tcgsize2size[paraloc^.size]);
  497. firstparaloc:=false;
  498. end;
  499. end;
  500. { O32 ABI reqires at least 16 bytes }
  501. if (intparasize < 16) then
  502. intparasize := 16;
  503. end;
  504. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  505. begin
  506. intparareg:=0;
  507. intparasize:=0;
  508. can_use_float := not ((p.proccalloption in [pocall_softfloat]) or (cs_fp_emulation in current_settings.moduleswitches));
  509. { Create Function result paraloc }
  510. create_funcretloc_info(p,callerside);
  511. { calculate the registers for the normal parameters }
  512. create_paraloc_info_intern(p,side,p.paras);
  513. { append the varargs }
  514. can_use_float := false;
  515. { restore correct intparasize value }
  516. if intparareg < 4 then
  517. intparasize:=intparareg * 4;
  518. if assigned(varargspara) then
  519. begin
  520. if side=callerside then
  521. create_paraloc_info_intern(p,side,varargspara)
  522. else
  523. internalerror(2019021922);
  524. end;
  525. create_funcretloc_info(p,side);
  526. { We need to return the size allocated on the stack }
  527. result:=intparasize;
  528. end;
  529. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  530. begin
  531. intparareg:=0;
  532. intparasize:=0;
  533. can_use_float := not ((p.proccalloption in [pocall_softfloat]) or (cs_fp_emulation in current_settings.moduleswitches));
  534. { Create Function result paraloc }
  535. create_funcretloc_info(p,side);
  536. create_paraloc_info_intern(p,side,p.paras);
  537. { We need to return the size allocated on the stack }
  538. result:=intparasize;
  539. end;
  540. begin
  541. ParaManager:=tcpuparamanager.create;
  542. end.