cpupara.pas 14 KB

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