cpupara.pas 19 KB

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