cpupara.pas 22 KB

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