cpupara.pas 20 KB

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