cpupara.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Calling conventions for the SPARC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *****************************************************************************}
  17. unit cpupara;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cclasses,
  23. aasmtai,
  24. cpubase,cpuinfo,
  25. symconst,symbase,symtype,symdef,paramgr,cgbase;
  26. type
  27. TSparcParaManager=class(TParaManager)
  28. function copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  29. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  30. function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override;
  31. function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override;
  32. {Returns a structure giving the information on the storage of the parameter
  33. (which must be an integer parameter)
  34. @param(nr Parameter number of routine, starting from 1)}
  35. function getintparaloc(calloption : tproccalloption; nr : longint) : tparalocation;override;
  36. procedure allocparaloc(list: taasmoutput; const loc: tparalocation);override;
  37. procedure freeparaloc(list: taasmoutput; const loc: tparalocation);override;
  38. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  39. function create_varargs_paraloc_info(p : TAbstractProcDef; varargspara:tvarargspara):longint;override;
  40. procedure splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);override;
  41. private
  42. procedure init_values(var curintreg :tsuperregister; var cur_stack_offset: aWord);
  43. function create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; firstpara: tparaitem;
  44. var curintreg: tsuperregister; var cur_stack_offset: aword):longint;
  45. end;
  46. implementation
  47. uses
  48. verbose,
  49. defutil,cgobj;
  50. function TSparcParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  51. begin
  52. result:=[RS_G1];
  53. end;
  54. function tsparcparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  55. begin
  56. result:=[RS_F0..RS_F31];
  57. end;
  58. function TSparcParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint) : tparalocation;
  59. begin
  60. if nr<1 then
  61. InternalError(2002100806);
  62. FillChar(GetIntParaLoc,SizeOf(TParaLocation),0);
  63. result.lochigh:=LOC_INVALID;
  64. Dec(nr);
  65. with GetIntParaLoc do
  66. begin
  67. { The six first parameters are passed into registers }
  68. if nr<6 then
  69. begin
  70. loc:=LOC_REGISTER;
  71. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  72. end
  73. else
  74. { The other parameters are passed on the stack }
  75. begin
  76. loc:=LOC_REFERENCE;
  77. reference.index:=NR_STACK_POINTER_REG;
  78. reference.offset:=92+(nr-6)*4;
  79. end;
  80. size:=OS_INT;
  81. end;
  82. end;
  83. function getparaloc(p : tdef) : tcgloc;
  84. begin
  85. { Later, the LOC_REFERENCE is in most cases changed into LOC_REGISTER
  86. if push_addr_param for the def is true
  87. }
  88. case p.deftype of
  89. orddef:
  90. getparaloc:=LOC_REGISTER;
  91. floatdef:
  92. getparaloc:=LOC_FPUREGISTER;
  93. enumdef:
  94. getparaloc:=LOC_REGISTER;
  95. pointerdef:
  96. getparaloc:=LOC_REGISTER;
  97. formaldef:
  98. getparaloc:=LOC_REGISTER;
  99. classrefdef:
  100. getparaloc:=LOC_REGISTER;
  101. recorddef:
  102. getparaloc:=LOC_REFERENCE;
  103. objectdef:
  104. if is_object(p) then
  105. getparaloc:=LOC_REFERENCE
  106. else
  107. getparaloc:=LOC_REGISTER;
  108. stringdef:
  109. if is_shortstring(p) or is_longstring(p) then
  110. getparaloc:=LOC_REFERENCE
  111. else
  112. getparaloc:=LOC_REGISTER;
  113. procvardef:
  114. if (po_methodpointer in tprocvardef(p).procoptions) then
  115. getparaloc:=LOC_REFERENCE
  116. else
  117. getparaloc:=LOC_REGISTER;
  118. filedef:
  119. getparaloc:=LOC_REGISTER;
  120. arraydef:
  121. getparaloc:=LOC_REFERENCE;
  122. setdef:
  123. if is_smallset(p) then
  124. getparaloc:=LOC_REGISTER
  125. else
  126. getparaloc:=LOC_REFERENCE;
  127. variantdef:
  128. getparaloc:=LOC_REFERENCE;
  129. { avoid problems with errornous definitions }
  130. errordef:
  131. getparaloc:=LOC_REGISTER;
  132. else
  133. internalerror(2002071001);
  134. end;
  135. end;
  136. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  137. begin
  138. result:=false;
  139. end;
  140. { true if a parameter is too large to copy and only the address is pushed }
  141. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  142. begin
  143. result:=false;
  144. { var,out always require address }
  145. if varspez in [vs_var,vs_out] then
  146. begin
  147. result:=true;
  148. exit;
  149. end;
  150. case def.deftype of
  151. recorddef,
  152. arraydef,
  153. variantdef,
  154. formaldef :
  155. push_addr_param:=true;
  156. objectdef :
  157. result:=is_object(def);
  158. stringdef :
  159. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  160. procvardef :
  161. result:=(po_methodpointer in tprocvardef(def).procoptions);
  162. setdef :
  163. result:=(tsetdef(def).settype<>smallset);
  164. end;
  165. end;
  166. procedure TSparcParaManager.init_values(var curintreg: tsuperregister; var cur_stack_offset: aWord);
  167. begin
  168. CurIntReg:=RS_O0;
  169. cur_stack_offset:=92;
  170. end;
  171. function TSparcParaManager.create_paraloc_info(p:TAbstractProcDef; side: tcallercallee):longint;
  172. var
  173. paraloc : tparalocation;
  174. stack_offset : aWord;
  175. nextintreg : tsuperregister;
  176. hp : tparaitem;
  177. is_64bit : boolean;
  178. begin
  179. init_values(NextIntReg,stack_offset);
  180. { Nested procedures have the parent framepoint in o0 }
  181. if p.parast.symtablelevel>normal_function_level then
  182. inc(NextIntReg);
  183. result := create_paraloc_info_intern(p,side,TParaItem(p.para.First),NextIntReg,stack_offset);
  184. { Function return }
  185. fillchar(paraloc,sizeof(tparalocation),0);
  186. paraloc.size:=def_cgsize(p.rettype.def);
  187. paraloc.Alignment:= std_param_align;
  188. { Return in FPU register? }
  189. if p.rettype.def.deftype=floatdef then
  190. begin
  191. paraloc.loc:=LOC_FPUREGISTER;
  192. paraloc.register:=NR_FPU_RESULT_REG;
  193. end
  194. else
  195. { Return in register? }
  196. if not ret_in_param(p.rettype.def,p.proccalloption) then
  197. begin
  198. paraloc.loc:=LOC_REGISTER;
  199. {$ifndef cpu64bit}
  200. if paraloc.size in [OS_64,OS_S64] then
  201. begin
  202. paraloc.lochigh:=LOC_REGISTER;
  203. if side=callerside then
  204. paraloc.register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  205. else
  206. paraloc.register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  207. if side=callerside then
  208. paraloc.register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  209. else
  210. paraloc.register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  211. end
  212. else
  213. {$endif cpu64bit}
  214. begin
  215. if side=callerside then
  216. paraloc.register:=NR_FUNCTION_RESULT_REG
  217. else
  218. paraloc.register:=NR_FUNCTION_RETURN_REG;
  219. end;
  220. end
  221. else
  222. begin
  223. paraloc.loc:=LOC_REFERENCE;
  224. end;
  225. p.funcret_paraloc[side]:=paraloc;
  226. { Size on stack is not used }
  227. result:=0;
  228. end;
  229. function TSparcParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; firstpara: tparaitem;
  230. var curintreg: tsuperregister; var cur_stack_offset: aword):longint;
  231. var
  232. stack_offset: aword;
  233. nextintreg : tsuperregister;
  234. paradef : tdef;
  235. paraloc : tparalocation;
  236. hp : tparaitem;
  237. loc : tcgloc;
  238. is_64bit: boolean;
  239. begin
  240. result:=0;
  241. nextintreg := curintreg;
  242. stack_offset := cur_stack_offset;
  243. hp:=firstpara;
  244. while assigned(hp) do
  245. begin
  246. fillchar(paraloc,sizeof(paraloc),0);
  247. paraloc.Alignment:= std_param_align;
  248. if push_addr_param(hp.paratyp,hp.paratype.def,p.proccalloption) or (hp.paratyp in [vs_var,vs_out]) then
  249. paraloc.size:=OS_ADDR
  250. else
  251. begin
  252. paraloc.size:=def_cgSize(hp.paratype.def);
  253. if paraloc.size=OS_NO then
  254. paraloc.size:=OS_ADDR;
  255. end;
  256. is_64bit:=(paraloc.size in [OS_64,OS_S64,OS_F64]);
  257. if NextIntReg<=RS_O5-ord(is_64bit) then
  258. begin
  259. paraloc.loc:=LOC_REGISTER;
  260. { big endian }
  261. if is_64bit then
  262. begin
  263. paraloc.registerhigh:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  264. paraloc.lochigh:=LOC_REGISTER;
  265. inc(nextintreg);
  266. end;
  267. paraloc.registerlow:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  268. inc(NextIntReg);
  269. end
  270. else
  271. begin
  272. paraloc.loc:=LOC_REFERENCE;
  273. { Low part need to be in O5 if still available }
  274. if NextIntReg<=RS_O5 then
  275. begin
  276. paraloc.low_in_reg:=true;
  277. paraloc.lowreg:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  278. end;
  279. nextintreg:=RS_O6;
  280. paraloc.reference.index:=NR_STACK_POINTER_REG;
  281. paraloc.reference.offset:=stack_offset;
  282. if is_64bit and
  283. (not paraloc.low_in_reg) then
  284. inc(stack_offset,8)
  285. else
  286. inc(stack_offset,4);
  287. end;
  288. hp.paraloc[side]:=paraloc;
  289. if side = calleeside then
  290. begin
  291. { update callee paraloc and use Ix registers instead
  292. of Ox registers }
  293. if hp.paraloc[calleeside].loc=LOC_REGISTER then
  294. begin
  295. { big endian }
  296. if is_64bit then
  297. setsupreg(hp.paraloc[calleeside].registerhigh,getsupreg(hp.paraloc[calleeside].registerhigh)+(RS_I0-RS_O0));
  298. setsupreg(hp.paraloc[calleeside].registerlow,getsupreg(hp.paraloc[calleeside].registerlow)+(RS_I0-RS_O0));
  299. end
  300. else
  301. begin
  302. if hp.paraloc[calleeside].low_in_reg then
  303. setsupreg(hp.paraloc[calleeside].lowreg,getsupreg(hp.paraloc[calleeside].lowreg)+(RS_I0-RS_O0));
  304. setsupreg(hp.paraloc[calleeside].reference.index,getsupreg(hp.paraloc[calleeside].reference.index)+(RS_I0-RS_O0));
  305. end;
  306. end;
  307. hp:=TParaItem(hp.Next);
  308. end;
  309. end;
  310. procedure tsparcparamanager.allocparaloc(list: taasmoutput; const loc: tparalocation);
  311. begin
  312. if (loc.loc=LOC_REFERENCE) and
  313. (loc.low_in_reg) then
  314. cg.GetExplicitRegister(list,loc.lowreg);
  315. inherited allocparaloc(list,loc);
  316. end;
  317. procedure tsparcparamanager.freeparaloc(list: taasmoutput; const loc: tparalocation);
  318. begin
  319. if (loc.loc=LOC_REFERENCE) and
  320. (loc.low_in_reg) then
  321. cg.UnGetRegister(list,loc.lowreg);
  322. inherited freeparaloc(list,loc);
  323. end;
  324. procedure tsparcparamanager.splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);
  325. begin
  326. { Word 0 is in register, word 1 is in reference }
  327. if (locpara.loc=LOC_REFERENCE) and locpara.low_in_reg then
  328. begin
  329. { high }
  330. lochipara:=locpara;
  331. if locpara.size=OS_S64 then
  332. lochipara.size:=OS_S32
  333. else
  334. lochipara.size:=OS_32;
  335. lochipara.low_in_reg:=false;
  336. { low }
  337. loclopara:=locpara;
  338. loclopara.size:=OS_32;
  339. loclopara.loc:=LOC_REGISTER;
  340. loclopara.register:=locpara.lowreg;
  341. end
  342. else
  343. inherited splitparaloc64(locpara,loclopara,lochipara);
  344. end;
  345. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargspara):longint;
  346. var
  347. cur_stack_offset: aword;
  348. parasize, l: longint;
  349. curintreg, firstfloatreg, curfloatreg, curmmreg: tsuperregister;
  350. hp: tparaitem;
  351. paraloc: tparalocation;
  352. begin
  353. init_values(curintreg,cur_stack_offset);
  354. firstfloatreg:=curfloatreg;
  355. result := create_paraloc_info_intern(p,callerside,tparaitem(p.para.first),curintreg,cur_stack_offset);
  356. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) then
  357. { just continue loading the parameters in the registers }
  358. result := create_paraloc_info_intern(p,callerside,tparaitem(varargspara.first),curintreg,cur_stack_offset)
  359. else
  360. begin
  361. hp := tparaitem(varargspara.first);
  362. parasize := cur_stack_offset;
  363. while assigned(hp) do
  364. begin
  365. paraloc.size:=def_cgsize(hp.paratype.def);
  366. paraloc.lochigh:=LOC_INVALID;
  367. paraloc.loc:=LOC_REFERENCE;
  368. paraloc.alignment:=4;
  369. paraloc.reference.index:=NR_STACK_POINTER_REG;
  370. l:=push_size(hp.paratyp,hp.paratype.def,p.proccalloption);
  371. paraloc.reference.offset:=parasize;
  372. parasize:=parasize+l;
  373. hp.paraloc[callerside]:=paraloc;
  374. hp:=tparaitem(hp.next);
  375. end;
  376. result := parasize;
  377. end;
  378. if curfloatreg<>firstfloatreg then
  379. include(varargspara.varargsinfo,va_uses_float_reg);
  380. end;
  381. begin
  382. ParaManager:=TSparcParaManager.create;
  383. end.
  384. {
  385. $Log$
  386. Revision 1.38 2004-03-15 14:39:56 mazen
  387. * make sparc para manager quite similar to ppc one to help
  388. copying evolutions.
  389. + Add support to var args in registers. need to be verfied as it
  390. was just copying ppc's one
  391. Revision 1.37 2004/03/09 13:05:49 mazen
  392. + give location for 64bit to fix IE 200402061
  393. Revision 1.36 2004/02/25 14:25:47 mazen
  394. * fix compile problem for sparc
  395. Revision 1.35 2003/11/10 19:05:50 peter
  396. * fixed alias/colouring > 255
  397. Revision 1.34 2003/10/24 11:25:32 mazen
  398. -unused units removed from uses clause
  399. *fix related to rg which was removed
  400. Revision 1.33 2003/10/08 21:16:27 olle
  401. * changed to symbolic const for alignment
  402. + alignment set for function result
  403. Revision 1.32 2003/10/08 14:11:36 mazen
  404. + Alignement field added to TParaLocation (=4 as 32 bits archs)
  405. Revision 1.31 2003/10/01 20:34:50 peter
  406. * procinfo unit contains tprocinfo
  407. * cginfo renamed to cgbase
  408. * moved cgmessage to verbose
  409. * fixed ppc and sparc compiles
  410. Revision 1.30 2003/09/14 21:35:15 peter
  411. * new volatile registers proc
  412. Revision 1.29 2003/09/14 19:19:05 peter
  413. * updates for new ra
  414. Revision 1.28 2003/09/03 15:55:01 peter
  415. * NEWRA branch merged
  416. Revision 1.27.2.1 2003/08/31 21:08:16 peter
  417. * first batch of sparc fixes
  418. Revision 1.27 2003/08/11 21:18:20 peter
  419. * start of sparc support for newra
  420. Revision 1.26 2003/07/08 21:25:00 peter
  421. * sparc fixes
  422. Revision 1.25 2003/07/06 22:10:56 peter
  423. * big endian first allocates high
  424. Revision 1.24 2003/07/06 17:58:22 peter
  425. * framepointer fixes for sparc
  426. * parent framepointer code more generic
  427. Revision 1.23 2003/07/05 20:11:41 jonas
  428. * create_paraloc_info() is now called separately for the caller and
  429. callee info
  430. * fixed ppc cycle
  431. Revision 1.22 2003/07/02 22:18:04 peter
  432. * paraloc splitted in callerparaloc,calleeparaloc
  433. * sparc calling convention updates
  434. Revision 1.21 2003/06/17 16:36:59 peter
  435. * freeintparaloc
  436. Revision 1.20 2003/06/09 21:44:14 mazen
  437. * fix compile problem related to modification
  438. of the declareation of GetIntParaLoc in the
  439. ancestor's declaration
  440. Revision 1.19 2003/06/01 21:38:06 peter
  441. * getregisterfpu size parameter added
  442. * op_const_reg size parameter added
  443. * sparc updates
  444. Revision 1.18 2003/05/31 01:00:51 peter
  445. * register fixes
  446. Revision 1.17 2003/05/30 23:57:08 peter
  447. * more sparc cleanup
  448. * accumulator removed, splitted in function_return_reg (called) and
  449. function_result_reg (caller)
  450. Revision 1.16 2003/04/23 13:35:39 peter
  451. * fix sparc compile
  452. Revision 1.15 2003/04/23 12:35:35 florian
  453. * fixed several issues with powerpc
  454. + applied a patch from Jonas for nested function calls (PowerPC only)
  455. * ...
  456. Revision 1.14 2003/01/08 18:43:58 daniel
  457. * Tregister changed into a record
  458. Revision 1.13 2003/01/05 21:32:35 mazen
  459. * fixing several bugs compiling the RTL
  460. Revision 1.12 2002/11/25 19:21:49 mazen
  461. * fixed support of nSparcInline
  462. Revision 1.11 2002/11/25 17:43:28 peter
  463. * splitted defbase in defutil,symutil,defcmp
  464. * merged isconvertable and is_equal into compare_defs(_ext)
  465. * made operator search faster by walking the list only once
  466. Revision 1.10 2002/11/18 17:32:01 peter
  467. * pass proccalloption to ret_in_xxx and push_xxx functions
  468. Revision 1.9 2002/11/03 20:22:40 mazen
  469. * parameter handling updated
  470. Revision 1.8 2002/10/13 21:46:07 mazen
  471. * assembler output format fixed
  472. Revision 1.7 2002/10/10 19:57:51 mazen
  473. * Just to update repsitory
  474. Revision 1.6 2002/10/10 15:10:39 mazen
  475. * Internal error fixed, but usually i386 parameter model used
  476. Revision 1.5 2002/10/09 13:52:19 mazen
  477. just incase some one wolud help me debugging that\!
  478. Revision 1.4 2002/10/08 21:02:22 mazen
  479. * debugging register allocation
  480. Revision 1.3 2002/10/07 20:33:05 mazen
  481. word alignement modified in g_stack_frame
  482. Revision 1.2 2002/10/04 21:57:42 mazen
  483. * register allocation for parameters now done in cpupara, but InternalError(200109223) in cgcpu.pas:1053 is still not fixed du to location_force problem in ncgutils.pas:419
  484. Revision 1.1 2002/08/21 13:30:07 mazen
  485. *** empty log message ***
  486. Revision 1.2 2002/07/11 14:41:34 florian
  487. * start of the new generic parameter handling
  488. Revision 1.1 2002/07/07 09:44:32 florian
  489. * powerpc target fixed, very simple units can be compiled
  490. }