cpupara.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. def : tdef;
  102. begin
  103. if nr<1 then
  104. InternalError(2002100806);
  105. def:=tparavarsym(pd.paras[nr-1]).vardef;
  106. cgpara.reset;
  107. cgpara.size:=def_cgsize(def);
  108. cgpara.intsize:=tcgsize2size[cgpara.size];
  109. cgpara.alignment:=std_param_align;
  110. cgpara.def:=def;
  111. paraloc:=cgpara.add_location;
  112. with paraloc^ do
  113. begin
  114. { MIPS: ABI dependent number of first parameters
  115. are passed into registers }
  116. dec(nr);
  117. if nr<mips_nb_used_registers then
  118. begin
  119. loc:=LOC_REGISTER;
  120. register:=newreg(R_INTREGISTER,parasupregs[nr],R_SUBWHOLE);
  121. end
  122. else
  123. begin
  124. { The other parameters are passed on the stack }
  125. loc:=LOC_REFERENCE;
  126. reference.index:=NR_STACK_POINTER_REG;
  127. reference.offset:=nr*mips_sizeof_register_param;
  128. end;
  129. size:=OS_INT;
  130. { Be sure to reserve enough stack space tp cope with
  131. that parameter }
  132. if assigned(current_procinfo) then
  133. TMIPSProcinfo(current_procinfo).allocate_push_parasize((nr+1)*mips_sizeof_register_param);
  134. end;
  135. end;
  136. { true if a parameter is too large to copy and only the address is pushed }
  137. function TMIPSParaManager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  138. begin
  139. result:=false;
  140. { var,out,constref always require address }
  141. if varspez in [vs_var,vs_out,vs_constref] then
  142. begin
  143. result:=true;
  144. exit;
  145. end;
  146. case def.typ of
  147. recorddef:
  148. { According to 032 ABI we should have }
  149. result:=false;
  150. arraydef:
  151. result:=true; {(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  152. is_open_array(def) or
  153. is_array_of_const(def) or
  154. is_array_constructor(def);}
  155. variantdef,
  156. formaldef :
  157. result:=true;
  158. objectdef :
  159. result:=is_object(def);
  160. stringdef :
  161. result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  162. procvardef :
  163. { If we always push records by value, we have to handle methodpointers that way too. }
  164. result:=false; {not tprocvardef(def).is_addressonly;}
  165. setdef :
  166. result:=not(is_smallset(def));
  167. end;
  168. end;
  169. function TMIPSParaManager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  170. var
  171. paraloc : pcgparalocation;
  172. retcgsize : tcgsize;
  173. retdef : tdef;
  174. begin
  175. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  176. begin
  177. { Return is passed as var parameter,
  178. in this case we use the first register R4 for it }
  179. if assigned(forcetempdef) then
  180. retdef:=forcetempdef
  181. else
  182. retdef:=p.returndef;
  183. if ret_in_param(retdef,p) and
  184. is_abi_record(retdef) then
  185. begin
  186. if intparareg=0 then
  187. inc(intparareg);
  188. end;
  189. exit;
  190. end;
  191. paraloc:=result.add_location;
  192. { Return in FPU register? }
  193. if result.def.typ=floatdef then
  194. begin
  195. paraloc^.loc:=LOC_FPUREGISTER;
  196. paraloc^.register:=NR_FPU_RESULT_REG;
  197. if retcgsize=OS_F64 then
  198. setsubreg(paraloc^.register,R_SUBFD);
  199. paraloc^.size:=retcgsize;
  200. end
  201. else
  202. { Return in register }
  203. begin
  204. {$ifndef cpu64bitalu}
  205. if retcgsize in [OS_64,OS_S64] then
  206. begin
  207. { low }
  208. paraloc^.loc:=LOC_REGISTER;
  209. if side=callerside then
  210. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  211. else
  212. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  213. paraloc^.size:=OS_32;
  214. { high }
  215. paraloc:=result.add_location;
  216. paraloc^.loc:=LOC_REGISTER;
  217. if side=callerside then
  218. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  219. else
  220. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  221. paraloc^.size:=OS_32;
  222. end
  223. else
  224. {$endif cpu64bitalu}
  225. begin
  226. paraloc^.loc:=LOC_REGISTER;
  227. paraloc^.size:=retcgsize;
  228. if side=callerside then
  229. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  230. else
  231. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  232. end;
  233. end
  234. end;
  235. procedure TMIPSParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist);
  236. var
  237. paraloc : pcgparalocation;
  238. i : integer;
  239. hp : tparavarsym;
  240. paracgsize : tcgsize;
  241. paralen : longint;
  242. paradef : tdef;
  243. fpparareg : integer;
  244. reg : tsuperregister;
  245. alignment : longint;
  246. tmp : longint;
  247. begin
  248. fpparareg := 0;
  249. for i:=0 to paras.count-1 do
  250. begin
  251. hp:=tparavarsym(paras[i]);
  252. paradef := hp.vardef;
  253. { currently only support C-style array of const }
  254. if (p.proccalloption in cstylearrayofconst) and
  255. is_array_of_const(paradef) then
  256. begin
  257. paraloc:=hp.paraloc[side].add_location;
  258. { hack: the paraloc must be valid, but is not actually used }
  259. paraloc^.loc:=LOC_REGISTER;
  260. paraloc^.register:=NR_R0;
  261. paraloc^.size:=OS_ADDR;
  262. break;
  263. end;
  264. if push_addr_param(hp.varspez,paradef,p.proccalloption) then
  265. begin
  266. paracgsize := OS_ADDR;
  267. paralen := tcgsize2size[paracgsize];
  268. paradef := getpointerdef(paradef);
  269. end
  270. else
  271. begin
  272. paracgsize := def_cgsize(paradef);
  273. { for things like formaldef }
  274. if (paracgsize=OS_NO) and (paradef.typ <> recorddef) then
  275. begin
  276. paracgsize:=OS_ADDR;
  277. paradef:=voidpointertype;
  278. end;
  279. if not is_special_array(paradef) then
  280. paralen := paradef.size
  281. else
  282. paralen := tcgsize2size[paracgsize];
  283. end;
  284. if (paracgsize in [OS_64, OS_S64, OS_F64]) or (paradef.alignment = 8) then
  285. alignment := 8
  286. else
  287. alignment := 4;
  288. //writeln('para: ',hp.Name,' typ=',hp.vardef.typ,' paracgsize=',paracgsize,' align=',hp.vardef.alignment);
  289. hp.paraloc[side].reset;
  290. hp.paraloc[side].Alignment:=alignment;
  291. if (paracgsize=OS_NO) or
  292. { Ordinals on caller side must be promoted to machine word }
  293. ((target_info.endian=endian_big) and // applies to little-endian too?
  294. (paradef.typ<>recorddef) and
  295. (side=callerside) and
  296. (paralen<tcgsize2size[OS_INT]))then
  297. begin
  298. if is_signed(paradef) then
  299. paracgsize:=OS_S32
  300. else
  301. paracgsize:=OS_32;
  302. paralen:=align(paralen,4);
  303. end
  304. else
  305. paralen:=tcgsize2size[paracgsize];
  306. hp.paraloc[side].intsize:=paralen;
  307. hp.paraloc[side].size:=paracgsize;
  308. hp.paraloc[side].def:=paradef;
  309. if (paralen=0) then
  310. if (paradef.typ=recorddef) then
  311. begin
  312. paraloc:=hp.paraloc[side].add_location;
  313. paraloc^.loc:=LOC_VOID;
  314. end
  315. else
  316. internalerror(2013020601);
  317. { check the alignment, mips O32ABI require a nature alignment }
  318. tmp := align(intparasize, alignment) - intparasize;
  319. while tmp > 0 do
  320. begin
  321. inc(intparareg);
  322. inc(intparasize,4);
  323. dec(tmp,4);
  324. end;
  325. { any non-float args will disable the use the floating regs }
  326. { up to two fp args }
  327. if (not(paracgsize in [OS_F32, OS_F64])) or (fpparareg = 2) then
  328. can_use_float := false;
  329. while paralen>0 do
  330. begin
  331. paraloc:=hp.paraloc[side].add_location;
  332. { We can allocate at maximum 32 bits per register }
  333. if (paracgsize in [OS_64,OS_S64]) or
  334. ((paracgsize in [OS_F32,OS_F64]) and
  335. not(can_use_float)) then
  336. paraloc^.size:=OS_32
  337. else
  338. paraloc^.size:=paracgsize;
  339. { ret in param? }
  340. if (vo_is_funcret in hp.varoptions) and
  341. is_abi_record(hp.vardef) then
  342. begin
  343. { This should be the first parameter }
  344. //if (intparareg<>1) then
  345. // Comment(V_Warning,'intparareg should be one for funcret in TMipsParaManager.create_paraloc_info_intern');
  346. paraloc^.loc:=LOC_REGISTER;
  347. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[0],R_SUBWHOLE);
  348. inc(intparasize,align(tcgsize2size[paraloc^.size],sizeof(aint)));
  349. end
  350. { In case of po_delphi_nested_cc, the parent frame pointer
  351. is always passed on the stack. }
  352. else if (intparareg<mips_nb_used_registers) and
  353. (not(vo_is_parentfp in hp.varoptions) or
  354. not(po_delphi_nested_cc in p.procoptions)) then
  355. begin
  356. if (can_use_float) then
  357. begin
  358. paraloc^.loc:=LOC_FPUREGISTER;
  359. if (fpparareg = 0) then
  360. reg := RS_F12
  361. else
  362. reg := RS_F14;
  363. if (paraloc^.size = OS_F64) then
  364. begin
  365. paraloc^.register:=newreg(R_FPUREGISTER, reg, R_SUBFD);
  366. inc(fpparareg);
  367. inc(intparareg);
  368. inc(intparareg);
  369. inc(intparasize,8);
  370. end
  371. else
  372. begin
  373. paraloc^.register:=newreg(R_FPUREGISTER, reg, R_SUBFS);
  374. inc(fpparareg);
  375. inc(intparareg);
  376. inc(intparasize,sizeof(aint));
  377. end;
  378. end
  379. else { not can use float }
  380. begin
  381. paraloc^.loc:=LOC_REGISTER;
  382. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[intparareg],R_SUBWHOLE);
  383. { big-endian targets require that record data stored in parameter
  384. registers is left-aligned }
  385. if (target_info.endian=endian_big) and
  386. (paradef.typ=recorddef) and
  387. (paralen<tcgsize2size[OS_INT]) then
  388. begin
  389. paraloc^.shiftval := (sizeof(aint)-tcgsize2size[paraloc^.size])*(-8);
  390. paraloc^.size := OS_INT;
  391. end;
  392. inc(intparareg);
  393. inc(intparasize,align(tcgsize2size[paraloc^.size],mips_sizeof_register_param));
  394. end;
  395. end
  396. else
  397. begin
  398. paraloc^.loc:=LOC_REFERENCE;
  399. paraloc^.size:=int_cgsize(paralen);
  400. if side=callerside then
  401. begin
  402. paraloc^.reference.index := NR_STACK_POINTER_REG;
  403. paraloc^.reference.offset:=intparasize;
  404. end
  405. else
  406. begin
  407. if (po_nostackframe in p.procoptions) then
  408. paraloc^.reference.index := NR_STACK_POINTER_REG
  409. else
  410. begin
  411. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  412. if assigned(current_procinfo) then
  413. TMIPSProcinfo(current_procinfo).needs_frame_pointer := true;
  414. end;
  415. paraloc^.reference.offset:=intparasize;
  416. if (target_info.endian=endian_big) and
  417. (paralen<tcgsize2size[OS_INT]) and
  418. (paradef.typ<>recorddef) then
  419. inc(paraloc^.reference.offset,4-paralen);
  420. end;
  421. inc(intparasize,align(paralen,mips_sizeof_register_param));
  422. paralen:=0;
  423. end;
  424. dec(paralen,tcgsize2size[paraloc^.size]);
  425. end;
  426. end;
  427. { O32 ABI reqires at least 16 bytes }
  428. if (intparasize < 16) then
  429. intparasize := 16;
  430. end;
  431. function TMIPSParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  432. begin
  433. intparareg:=0;
  434. intparasize:=0;
  435. can_use_float := true;
  436. { Create Function result paraloc }
  437. create_funcretloc_info(p,callerside);
  438. { calculate the registers for the normal parameters }
  439. create_paraloc_info_intern(p,callerside,p.paras);
  440. { append the varargs }
  441. can_use_float := false;
  442. { restore correct intparasize value }
  443. if intparareg < 4 then
  444. intparasize:=intparareg * 4;
  445. create_paraloc_info_intern(p,callerside,varargspara);
  446. { We need to return the size allocated on the stack }
  447. result:=intparasize;
  448. end;
  449. function TMIPSParaManager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  450. begin
  451. intparareg:=0;
  452. intparasize:=0;
  453. can_use_float := true;
  454. { Create Function result paraloc }
  455. create_funcretloc_info(p,side);
  456. create_paraloc_info_intern(p,side,p.paras);
  457. { We need to return the size allocated on the stack }
  458. result:=intparasize;
  459. end;
  460. begin
  461. ParaManager:=TMIPSParaManager.create;
  462. end.