cpupara.pas 19 KB

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