cpupara.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. 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 = MIPS_NB_REGISTERS_USED_IN_CALL_O32;
  46. { Might need to be changed if we support N64 ABI later }
  47. mips_sizeof_register_param = 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. tparasupregsoffset = array[0..MIPS_MAX_REGISTERS_USED_IN_CALL-1] of longint;
  52. const
  53. parasupregs : tparasupregs = (RS_R4, RS_R5, RS_R6, RS_R7, RS_R8, RS_R9);
  54. type
  55. TMIPSParaManager=class(TParaManager)
  56. var
  57. param_offset:array[0..MIPS_MAX_OFFSET] of ^Aint;
  58. intparareg,
  59. parasize : longint;
  60. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  61. function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override;
  62. function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override;
  63. {Returns a structure giving the information on the storage of the parameter
  64. (which must be an integer parameter)
  65. @param(nr Parameter number of routine, starting from 1)}
  66. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara : TCGPara);override;
  67. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  68. function create_varargs_paraloc_info(p : TAbstractProcDef; varargspara:tvarargsparalist):longint;override;
  69. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tcgpara;override;
  70. private
  71. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  72. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist);
  73. end;
  74. implementation
  75. uses
  76. cutils,verbose,systems,
  77. defutil, cpupi, procinfo,
  78. cgobj;
  79. function TMIPSParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  80. begin
  81. { O32 ABI values }
  82. result:=[RS_R1..RS_R15,RS_R24..RS_R25,RS_R31];
  83. end;
  84. function TMIPSParaManager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  85. begin
  86. { O32 ABI values }
  87. result:=[RS_F0..RS_F19];
  88. end;
  89. procedure TMIPSParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint;var cgpara : tcgpara);
  90. var
  91. paraloc : pcgparalocation;
  92. begin
  93. if nr<1 then
  94. InternalError(2002100806);
  95. cgpara.reset;
  96. cgpara.size:=OS_INT;
  97. cgpara.intsize:=tcgsize2size[OS_INT];
  98. cgpara.alignment:=std_param_align;
  99. paraloc:=cgpara.add_location;
  100. with paraloc^ do
  101. begin
  102. { MIPS: ABI dependent number of first parameters
  103. are passed into registers }
  104. dec(nr);
  105. if nr<mips_nb_used_registers then
  106. begin
  107. loc:=LOC_REGISTER;
  108. register:=newreg(R_INTREGISTER,parasupregs[nr],R_SUBWHOLE);
  109. if assigned(current_procinfo) then
  110. begin
  111. TMIPSProcInfo(current_procinfo).register_used[nr]:=true;
  112. TMIPSProcInfo(current_procinfo).register_offset[nr]:=nr*mips_sizeof_register_param;
  113. end;
  114. end
  115. else
  116. begin
  117. { The other parameters are passed on the stack }
  118. loc:=LOC_REFERENCE;
  119. reference.index:=NR_STACK_POINTER_REG;
  120. reference.offset:=nr*mips_sizeof_register_param;
  121. end;
  122. size:=OS_INT;
  123. end;
  124. end;
  125. { true if a parameter is too large to copy and only the address is pushed }
  126. function TMIPSParaManager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  127. begin
  128. result:=false;
  129. { var,out,constref always require address }
  130. if varspez in [vs_var,vs_out,vs_constref] then
  131. begin
  132. result:=true;
  133. exit;
  134. end;
  135. case def.typ of
  136. recorddef:
  137. result:=true;
  138. { According to 032 ABI we should have
  139. result:=false; buut this cmpletely fails }
  140. arraydef:
  141. result:=true; {(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  142. is_open_array(def) or
  143. is_array_of_const(def) or
  144. is_array_constructor(def);}
  145. variantdef,
  146. formaldef :
  147. result:=true;
  148. objectdef :
  149. result:=is_object(def);
  150. stringdef :
  151. result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  152. procvardef :
  153. result:=not tprocvardef(def).is_addressonly;
  154. setdef :
  155. result:=not(is_smallset(def));
  156. end;
  157. end;
  158. procedure TMIPSParaManager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  159. begin
  160. p.funcretloc[side]:=get_funcretloc(p,side,p.returndef);
  161. end;
  162. function TMIPSParaManager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tcgpara;
  163. var
  164. paraloc : pcgparalocation;
  165. retcgsize : tcgsize;
  166. begin
  167. result.init;
  168. result.alignment:=get_para_align(p.proccalloption);
  169. { void has no location }
  170. if is_void(def) then
  171. begin
  172. paraloc:=result.add_location;
  173. result.size:=OS_NO;
  174. result.intsize:=0;
  175. paraloc^.size:=OS_NO;
  176. paraloc^.loc:=LOC_VOID;
  177. exit;
  178. end;
  179. { Constructors return self instead of a boolean }
  180. if (p.proctypeoption=potype_constructor) then
  181. begin
  182. retcgsize:=OS_ADDR;
  183. result.intsize:=sizeof(pint);
  184. end
  185. else
  186. begin
  187. retcgsize:=def_cgsize(def);
  188. result.intsize:=def.size;
  189. end;
  190. result.size:=retcgsize;
  191. { Return is passed as var parameter,
  192. in this case we use the first register R4 for it }
  193. if ret_in_param(def,p.proccalloption) then
  194. begin
  195. { Reserve first register for ret_in_param }
  196. if intparareg=0 then
  197. inc(intparareg);
  198. if side=calleeside then
  199. begin
  200. paraloc:=result.add_location;
  201. paraloc^.loc:=LOC_REFERENCE;
  202. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  203. { return is at offset zero }
  204. paraloc^.reference.offset:=0;
  205. paraloc^.size:=retcgsize;
  206. end
  207. else
  208. begin
  209. getIntParaLoc(p.proccalloption,1,result);
  210. end;
  211. exit;
  212. end;
  213. paraloc:=result.add_location;
  214. { Return in FPU register? }
  215. if p.returndef.typ=floatdef then
  216. begin
  217. paraloc^.loc:=LOC_FPUREGISTER;
  218. paraloc^.register:=NR_FPU_RESULT_REG;
  219. if retcgsize=OS_F64 then
  220. setsubreg(paraloc^.register,R_SUBFD);
  221. paraloc^.size:=retcgsize;
  222. end
  223. else
  224. { Return in register }
  225. begin
  226. {$ifndef cpu64bitalu}
  227. if retcgsize in [OS_64,OS_S64] then
  228. begin
  229. { low }
  230. paraloc^.loc:=LOC_REGISTER;
  231. if side=callerside then
  232. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  233. else
  234. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  235. paraloc^.size:=OS_32;
  236. { high }
  237. paraloc:=result.add_location;
  238. paraloc^.loc:=LOC_REGISTER;
  239. if side=callerside then
  240. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  241. else
  242. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  243. paraloc^.size:=OS_32;
  244. end
  245. else
  246. {$endif cpu64bitalu}
  247. begin
  248. paraloc^.loc:=LOC_REGISTER;
  249. paraloc^.size:=retcgsize;
  250. if side=callerside then
  251. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  252. else
  253. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  254. end;
  255. end
  256. end;
  257. procedure TMIPSParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist);
  258. var
  259. paraloc : pcgparalocation;
  260. i : integer;
  261. hp : tparavarsym;
  262. paracgsize : tcgsize;
  263. paralen : longint;
  264. paradef : tdef;
  265. fpparareg : integer;
  266. can_use_float : boolean;
  267. reg : tsuperregister;
  268. alignment : longint;
  269. tmp : longint;
  270. begin
  271. fpparareg := 0;
  272. can_use_float := true;
  273. for i:=0 to paras.count-1 do
  274. begin
  275. if i<=MIPS_MAX_OFFSET then
  276. param_offset[i] := Nil;
  277. hp:=tparavarsym(paras[i]);
  278. paradef := hp.vardef;
  279. { currently only support C-style array of const }
  280. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  281. is_array_of_const(paradef) then
  282. begin
  283. paraloc:=hp.paraloc[side].add_location;
  284. { hack: the paraloc must be valid, but is not actually used }
  285. paraloc^.loc:=LOC_REGISTER;
  286. paraloc^.register:=NR_R0;
  287. paraloc^.size:=OS_ADDR;
  288. break;
  289. end;
  290. if (push_addr_param(hp.varspez,paradef,p.proccalloption)) then
  291. begin
  292. paracgsize := OS_ADDR;
  293. paralen := tcgsize2size[paracgsize];
  294. end
  295. else
  296. begin
  297. paracgsize := def_cgsize(paradef);
  298. { for things like formaldef }
  299. if (paracgsize=OS_NO) then
  300. begin
  301. paracgsize:=OS_ADDR;
  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 (hp.vardef.alignment = 8) then
  309. alignment := 8
  310. else
  311. alignment := 4;
  312. hp.paraloc[side].reset;
  313. hp.paraloc[side].Alignment:=alignment;
  314. paralen:=tcgsize2size[paracgsize];
  315. hp.paraloc[side].intsize:=paralen;
  316. hp.paraloc[side].size:=paracgsize;
  317. { check the alignment, mips O32ABI require a nature alignment }
  318. tmp := align(parasize, alignment) - parasize;
  319. while tmp > 0 do
  320. begin
  321. inc(intparareg);
  322. inc(parasize,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 then
  341. begin
  342. { This should be the first parameter }
  343. if assigned(current_procinfo) then
  344. begin
  345. TMIPSProcInfo(current_procinfo).register_used[0]:=true;
  346. TMIPSProcInfo(current_procinfo).register_offset[0]:=0;
  347. end;
  348. //if (intparareg<>1) then
  349. // Comment(V_Warning,'intparareg should be one for funcret in TMipsParaManager.create_paraloc_info_intern');
  350. if side=callerside then
  351. begin
  352. paraloc^.loc:=LOC_REGISTER;
  353. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[0],R_SUBWHOLE);
  354. end
  355. else
  356. begin
  357. paraloc^.loc:=LOC_REFERENCE;
  358. if (po_nostackframe in p.procoptions) then
  359. paraloc^.reference.index := NR_STACK_POINTER_REG
  360. else
  361. begin
  362. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  363. if assigned(current_procinfo) then
  364. TMIPSProcinfo(current_procinfo).needs_frame_pointer := true;
  365. end;
  366. paraloc^.reference.offset:=0;
  367. if i<=MIPS_MAX_OFFSET then
  368. param_offset[i] := @paraloc^.reference.offset;
  369. end;
  370. inc(parasize,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(parasize,8);
  392. end
  393. else
  394. begin
  395. paraloc^.register:=newreg(R_FPUREGISTER, reg, R_SUBFS);
  396. inc(fpparareg);
  397. inc(intparareg);
  398. inc(parasize,sizeof(aint));
  399. end;
  400. end
  401. else { not can use float }
  402. begin
  403. if assigned(current_procinfo) then
  404. begin
  405. TMIPSProcInfo(current_procinfo).register_used[intparareg]:=true;
  406. TMIPSProcInfo(current_procinfo).register_offset[intparareg]:=intparareg*mips_sizeof_register_param;
  407. end;
  408. if side=callerside then
  409. begin
  410. paraloc^.loc:=LOC_REGISTER;
  411. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[intparareg],R_SUBWHOLE);
  412. end
  413. else
  414. begin
  415. paraloc^.loc:=LOC_REFERENCE;
  416. if (po_nostackframe in p.procoptions) then
  417. paraloc^.reference.index := NR_STACK_POINTER_REG
  418. else
  419. begin
  420. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  421. if assigned(current_procinfo) then
  422. TMIPSProcinfo(current_procinfo).needs_frame_pointer := true;
  423. end;
  424. paraloc^.reference.offset:=intparareg*mips_sizeof_register_param;
  425. end;
  426. inc(intparareg);
  427. inc(parasize,align(tcgsize2size[paraloc^.size],mips_sizeof_register_param));
  428. end;
  429. end
  430. else
  431. begin
  432. paraloc^.loc:=LOC_REFERENCE;
  433. if side=callerside then
  434. begin
  435. paraloc^.reference.index := NR_STACK_POINTER_REG;
  436. paraloc^.reference.offset:=parasize;
  437. end
  438. else
  439. begin
  440. if (po_nostackframe in p.procoptions) then
  441. paraloc^.reference.index := NR_STACK_POINTER_REG
  442. else
  443. begin
  444. paraloc^.reference.index := NR_FRAME_POINTER_REG;
  445. if assigned(current_procinfo) then
  446. TMIPSProcinfo(current_procinfo).needs_frame_pointer := true;
  447. end;
  448. paraloc^.reference.offset:=parasize;
  449. if i<=MIPS_MAX_OFFSET then
  450. param_offset[i] := @paraloc^.reference.offset;
  451. end;
  452. inc(parasize,align(tcgsize2size[paraloc^.size],mips_sizeof_register_param));
  453. end;
  454. dec(paralen,tcgsize2size[paraloc^.size]);
  455. end;
  456. end;
  457. end;
  458. function TMIPSParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  459. begin
  460. intparareg:=0;
  461. parasize:=0;
  462. { Create Function result paraloc }
  463. create_funcretloc_info(p,callerside);
  464. { calculate the registers for the normal parameters }
  465. create_paraloc_info_intern(p,callerside,p.paras);
  466. { append the varargs }
  467. create_paraloc_info_intern(p,callerside,varargspara);
  468. { At least 16 bytes must be reserved for parameter
  469. area in O32 ABI, this can be used by called function,
  470. even if it has less parameter }
  471. if (parasize < 16) then
  472. parasize := 16;
  473. result:=parasize;
  474. end;
  475. function TMIPSParaManager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  476. begin
  477. intparareg:=0;
  478. parasize:=0;
  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. { At least 16 bytes must be reserved for parameter
  484. area in O32 ABI, this can be used by called function,
  485. even if it has less parameter }
  486. if (parasize < 16) then
  487. parasize := 16;
  488. result:=parasize;
  489. end;
  490. begin
  491. ParaManager:=TMIPSParaManager.create;
  492. end.