cpupara.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. cpubase,cgbase,
  22. aasmtai,globtype,
  23. symconst,symtype,symdef,paramgr;
  24. type
  25. TSparcParaManager=class(TParaManager)
  26. function copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  27. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  28. function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override;
  29. function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override;
  30. {Returns a structure giving the information on the storage of the parameter
  31. (which must be an integer parameter)
  32. @param(nr Parameter number of routine, starting from 1)}
  33. function getintparaloc(calloption : tproccalloption; nr : longint) : tparalocation;override;
  34. procedure allocparaloc(list: taasmoutput; const loc: tparalocation);override;
  35. procedure freeparaloc(list: taasmoutput; const loc: tparalocation);override;
  36. function create_paraloc_info(p:TAbstractProcDef; side: tcallercallee):longint;override;
  37. procedure splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);override;
  38. end;
  39. implementation
  40. uses
  41. verbose,
  42. defutil,cgobj;
  43. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  44. begin
  45. result:=false;
  46. end;
  47. { true if a parameter is too large to copy and only the address is pushed }
  48. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  49. begin
  50. result:=false;
  51. { var,out always require address }
  52. if varspez in [vs_var,vs_out] then
  53. begin
  54. result:=true;
  55. exit;
  56. end;
  57. case def.deftype of
  58. recorddef,
  59. arraydef,
  60. variantdef,
  61. formaldef :
  62. push_addr_param:=true;
  63. objectdef :
  64. result:=is_object(def);
  65. stringdef :
  66. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  67. procvardef :
  68. result:=(po_methodpointer in tprocvardef(def).procoptions);
  69. setdef :
  70. result:=(tsetdef(def).settype<>smallset);
  71. end;
  72. end;
  73. function tsparcparamanager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  74. begin
  75. result:=[RS_G1];
  76. end;
  77. function tsparcparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  78. begin
  79. result:=[first_fpu_supreg..last_fpu_supreg];;
  80. end;
  81. function TSparcParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint) : tparalocation;
  82. begin
  83. if nr<1 then
  84. InternalError(2002100806);
  85. FillChar(GetIntParaLoc,SizeOf(TParaLocation),0);
  86. Dec(nr);
  87. with GetIntParaLoc do
  88. begin
  89. { The six first parameters are passed into registers }
  90. if nr<6 then
  91. begin
  92. loc:=LOC_REGISTER;
  93. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  94. end
  95. else
  96. { The other parameters are passed on the stack }
  97. begin
  98. loc:=LOC_REFERENCE;
  99. reference.index:=NR_STACK_POINTER_REG;
  100. reference.offset:=92+(nr-6)*4;
  101. end;
  102. size:=OS_INT;
  103. end;
  104. end;
  105. procedure tsparcparamanager.allocparaloc(list: taasmoutput; const loc: tparalocation);
  106. begin
  107. if (loc.loc=LOC_REFERENCE) and
  108. (loc.low_in_reg) then
  109. cg.GetExplicitRegister(list,loc.lowreg);
  110. inherited allocparaloc(list,loc);
  111. end;
  112. procedure tsparcparamanager.freeparaloc(list: taasmoutput; const loc: tparalocation);
  113. begin
  114. if (loc.loc=LOC_REFERENCE) and
  115. (loc.low_in_reg) then
  116. cg.UnGetRegister(list,loc.lowreg);
  117. inherited freeparaloc(list,loc);
  118. end;
  119. function TSparcParaManager.create_paraloc_info(p:TAbstractProcDef; side: tcallercallee):longint;
  120. var
  121. nextintreg : tsuperregister;
  122. stack_offset : longint;
  123. hp : tparaitem;
  124. is_64bit : boolean;
  125. paraloc : tparalocation;
  126. begin
  127. nextintreg:=RS_O0;
  128. { Nested procedures have the parent framepoint in o0 }
  129. if p.parast.symtablelevel>normal_function_level then
  130. inc(NextIntReg);
  131. stack_offset:=92;
  132. hp:=TParaItem(p.para.First);
  133. while assigned(hp) do
  134. begin
  135. fillchar(paraloc,sizeof(paraloc),0);
  136. paraloc.Alignment:= std_param_align;
  137. if push_addr_param(hp.paratyp,hp.paratype.def,p.proccalloption) or (hp.paratyp in [vs_var,vs_out]) then
  138. paraloc.size:=OS_ADDR
  139. else
  140. begin
  141. paraloc.size:=def_cgSize(hp.paratype.def);
  142. if paraloc.size=OS_NO then
  143. paraloc.size:=OS_ADDR;
  144. end;
  145. is_64bit:=(paraloc.size in [OS_64,OS_S64,OS_F64]);
  146. if NextIntReg<=RS_O5-ord(is_64bit) then
  147. begin
  148. paraloc.loc:=LOC_REGISTER;
  149. { big endian }
  150. if is_64bit then
  151. begin
  152. paraloc.registerhigh:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  153. inc(nextintreg);
  154. end;
  155. paraloc.registerlow:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  156. inc(NextIntReg);
  157. end
  158. else
  159. begin
  160. paraloc.loc:=LOC_REFERENCE;
  161. { Low part need to be in O5 if still available }
  162. if NextIntReg<=RS_O5 then
  163. begin
  164. paraloc.low_in_reg:=true;
  165. paraloc.lowreg:=newreg(R_INTREGISTER,nextintreg,R_SUBWHOLE);
  166. end;
  167. nextintreg:=RS_O6;
  168. paraloc.reference.index:=NR_STACK_POINTER_REG;
  169. paraloc.reference.offset:=stack_offset;
  170. if is_64bit and
  171. (not paraloc.low_in_reg) then
  172. inc(stack_offset,8)
  173. else
  174. inc(stack_offset,4);
  175. end;
  176. hp.paraloc[side]:=paraloc;
  177. if side = calleeside then
  178. begin
  179. { update callee paraloc and use Ix registers instead
  180. of Ox registers }
  181. if hp.paraloc[calleeside].loc=LOC_REGISTER then
  182. begin
  183. { big endian }
  184. if is_64bit then
  185. setsupreg(hp.paraloc[calleeside].registerhigh,getsupreg(hp.paraloc[calleeside].registerhigh)+(RS_I0-RS_O0));
  186. setsupreg(hp.paraloc[calleeside].registerlow,getsupreg(hp.paraloc[calleeside].registerlow)+(RS_I0-RS_O0));
  187. end
  188. else
  189. begin
  190. if hp.paraloc[calleeside].low_in_reg then
  191. setsupreg(hp.paraloc[calleeside].lowreg,getsupreg(hp.paraloc[calleeside].lowreg)+(RS_I0-RS_O0));
  192. setsupreg(hp.paraloc[calleeside].reference.index,getsupreg(hp.paraloc[calleeside].reference.index)+(RS_I0-RS_O0));
  193. end;
  194. end;
  195. hp:=TParaItem(hp.Next);
  196. end;
  197. { Function return }
  198. fillchar(paraloc,sizeof(tparalocation),0);
  199. paraloc.size:=def_cgsize(p.rettype.def);
  200. paraloc.Alignment:= std_param_align;
  201. { Return in FPU register? }
  202. if p.rettype.def.deftype=floatdef then
  203. begin
  204. paraloc.loc:=LOC_FPUREGISTER;
  205. paraloc.register:=NR_FPU_RESULT_REG;
  206. end
  207. else
  208. { Return in register? }
  209. if not ret_in_param(p.rettype.def,p.proccalloption) then
  210. begin
  211. paraloc.loc:=LOC_REGISTER;
  212. {$ifndef cpu64bit}
  213. if paraloc.size in [OS_64,OS_S64] then
  214. begin
  215. if side=callerside then
  216. paraloc.register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  217. else
  218. paraloc.register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  219. if side=callerside then
  220. paraloc.register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  221. else
  222. paraloc.register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  223. end
  224. else
  225. {$endif cpu64bit}
  226. begin
  227. if side=callerside then
  228. paraloc.register:=NR_FUNCTION_RESULT_REG
  229. else
  230. paraloc.register:=NR_FUNCTION_RETURN_REG;
  231. end;
  232. end
  233. else
  234. begin
  235. paraloc.loc:=LOC_REFERENCE;
  236. end;
  237. p.funcret_paraloc[side]:=paraloc;
  238. { Size on stack is not used }
  239. result:=0;
  240. end;
  241. procedure tsparcparamanager.splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);
  242. begin
  243. { Word 0 is in register, word 1 is in reference }
  244. if (locpara.loc=LOC_REFERENCE) and locpara.low_in_reg then
  245. begin
  246. { high }
  247. lochipara:=locpara;
  248. if locpara.size=OS_S64 then
  249. lochipara.size:=OS_S32
  250. else
  251. lochipara.size:=OS_32;
  252. lochipara.low_in_reg:=false;
  253. { low }
  254. loclopara:=locpara;
  255. loclopara.size:=OS_32;
  256. loclopara.loc:=LOC_REGISTER;
  257. loclopara.register:=locpara.lowreg;
  258. end
  259. else
  260. inherited splitparaloc64(locpara,loclopara,lochipara);
  261. end;
  262. begin
  263. ParaManager:=TSparcParaManager.create;
  264. end.
  265. {
  266. $Log$
  267. Revision 1.34 2003-10-24 11:25:32 mazen
  268. -unused units removed from uses clause
  269. *fix related to rg which was removed
  270. Revision 1.33 2003/10/08 21:16:27 olle
  271. * changed to symbolic const for alignment
  272. + alignment set for function result
  273. Revision 1.32 2003/10/08 14:11:36 mazen
  274. + Alignement field added to TParaLocation (=4 as 32 bits archs)
  275. Revision 1.31 2003/10/01 20:34:50 peter
  276. * procinfo unit contains tprocinfo
  277. * cginfo renamed to cgbase
  278. * moved cgmessage to verbose
  279. * fixed ppc and sparc compiles
  280. Revision 1.30 2003/09/14 21:35:15 peter
  281. * new volatile registers proc
  282. Revision 1.29 2003/09/14 19:19:05 peter
  283. * updates for new ra
  284. Revision 1.28 2003/09/03 15:55:01 peter
  285. * NEWRA branch merged
  286. Revision 1.27.2.1 2003/08/31 21:08:16 peter
  287. * first batch of sparc fixes
  288. Revision 1.27 2003/08/11 21:18:20 peter
  289. * start of sparc support for newra
  290. Revision 1.26 2003/07/08 21:25:00 peter
  291. * sparc fixes
  292. Revision 1.25 2003/07/06 22:10:56 peter
  293. * big endian first allocates high
  294. Revision 1.24 2003/07/06 17:58:22 peter
  295. * framepointer fixes for sparc
  296. * parent framepointer code more generic
  297. Revision 1.23 2003/07/05 20:11:41 jonas
  298. * create_paraloc_info() is now called separately for the caller and
  299. callee info
  300. * fixed ppc cycle
  301. Revision 1.22 2003/07/02 22:18:04 peter
  302. * paraloc splitted in callerparaloc,calleeparaloc
  303. * sparc calling convention updates
  304. Revision 1.21 2003/06/17 16:36:59 peter
  305. * freeintparaloc
  306. Revision 1.20 2003/06/09 21:44:14 mazen
  307. * fix compile problem related to modification
  308. of the declareation of GetIntParaLoc in the
  309. ancestor's declaration
  310. Revision 1.19 2003/06/01 21:38:06 peter
  311. * getregisterfpu size parameter added
  312. * op_const_reg size parameter added
  313. * sparc updates
  314. Revision 1.18 2003/05/31 01:00:51 peter
  315. * register fixes
  316. Revision 1.17 2003/05/30 23:57:08 peter
  317. * more sparc cleanup
  318. * accumulator removed, splitted in function_return_reg (called) and
  319. function_result_reg (caller)
  320. Revision 1.16 2003/04/23 13:35:39 peter
  321. * fix sparc compile
  322. Revision 1.15 2003/04/23 12:35:35 florian
  323. * fixed several issues with powerpc
  324. + applied a patch from Jonas for nested function calls (PowerPC only)
  325. * ...
  326. Revision 1.14 2003/01/08 18:43:58 daniel
  327. * Tregister changed into a record
  328. Revision 1.13 2003/01/05 21:32:35 mazen
  329. * fixing several bugs compiling the RTL
  330. Revision 1.12 2002/11/25 19:21:49 mazen
  331. * fixed support of nSparcInline
  332. Revision 1.11 2002/11/25 17:43:28 peter
  333. * splitted defbase in defutil,symutil,defcmp
  334. * merged isconvertable and is_equal into compare_defs(_ext)
  335. * made operator search faster by walking the list only once
  336. Revision 1.10 2002/11/18 17:32:01 peter
  337. * pass proccalloption to ret_in_xxx and push_xxx functions
  338. Revision 1.9 2002/11/03 20:22:40 mazen
  339. * parameter handling updated
  340. Revision 1.8 2002/10/13 21:46:07 mazen
  341. * assembler output format fixed
  342. Revision 1.7 2002/10/10 19:57:51 mazen
  343. * Just to update repsitory
  344. Revision 1.6 2002/10/10 15:10:39 mazen
  345. * Internal error fixed, but usually i386 parameter model used
  346. Revision 1.5 2002/10/09 13:52:19 mazen
  347. just incase some one wolud help me debugging that\!
  348. Revision 1.4 2002/10/08 21:02:22 mazen
  349. * debugging register allocation
  350. Revision 1.3 2002/10/07 20:33:05 mazen
  351. word alignement modified in g_stack_frame
  352. Revision 1.2 2002/10/04 21:57:42 mazen
  353. * 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
  354. Revision 1.1 2002/08/21 13:30:07 mazen
  355. *** empty log message ***
  356. Revision 1.2 2002/07/11 14:41:34 florian
  357. * start of the new generic parameter handling
  358. Revision 1.1 2002/07/07 09:44:32 florian
  359. * powerpc target fixed, very simple units can be compiled
  360. }