cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  43. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; firstpara: tparaitem;
  44. var intparareg,parasize:longint);
  45. end;
  46. implementation
  47. uses
  48. verbose,systems,
  49. defutil,cgobj;
  50. type
  51. tparasupregs = array[0..5] of tsuperregister;
  52. pparasupregs = ^tparasupregs;
  53. const
  54. paraoutsupregs : tparasupregs = (RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5);
  55. parainsupregs : tparasupregs = (RS_I0,RS_I1,RS_I2,RS_I3,RS_I4,RS_I5);
  56. function TSparcParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  57. begin
  58. result:=[RS_G1];
  59. end;
  60. function tsparcparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  61. begin
  62. result:=[RS_F0..RS_F31];
  63. end;
  64. function TSparcParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint) : tparalocation;
  65. begin
  66. if nr<1 then
  67. InternalError(2002100806);
  68. FillChar(GetIntParaLoc,SizeOf(TParaLocation),0);
  69. result.lochigh:=LOC_INVALID;
  70. Dec(nr);
  71. with GetIntParaLoc do
  72. begin
  73. { The six first parameters are passed into registers }
  74. if nr<6 then
  75. begin
  76. loc:=LOC_REGISTER;
  77. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  78. end
  79. else
  80. { The other parameters are passed on the stack }
  81. begin
  82. loc:=LOC_REFERENCE;
  83. reference.index:=NR_STACK_POINTER_REG;
  84. reference.offset:=92+(nr-6)*4;
  85. end;
  86. size:=OS_INT;
  87. end;
  88. end;
  89. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  90. begin
  91. result:=false;
  92. end;
  93. { true if a parameter is too large to copy and only the address is pushed }
  94. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  95. begin
  96. result:=false;
  97. { var,out always require address }
  98. if varspez in [vs_var,vs_out] then
  99. begin
  100. result:=true;
  101. exit;
  102. end;
  103. case def.deftype of
  104. recorddef,
  105. arraydef,
  106. variantdef,
  107. formaldef :
  108. push_addr_param:=true;
  109. objectdef :
  110. result:=is_object(def);
  111. stringdef :
  112. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  113. procvardef :
  114. result:=(po_methodpointer in tprocvardef(def).procoptions);
  115. setdef :
  116. result:=(tsetdef(def).settype<>smallset);
  117. end;
  118. end;
  119. procedure tsparcparamanager.create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  120. var
  121. paraloc : tparalocation;
  122. begin
  123. fillchar(paraloc,sizeof(tparalocation),0);
  124. paraloc.size:=def_cgsize(p.rettype.def);
  125. paraloc.Alignment:= std_param_align;
  126. { Constructors return self }
  127. if (p.proctypeoption=potype_constructor) then
  128. begin
  129. paraloc.size:=OS_ADDR;
  130. paraloc.loc:=LOC_REGISTER;
  131. if side=callerside then
  132. paraloc.register:=NR_FUNCTION_RESULT_REG
  133. else
  134. paraloc.register:=NR_FUNCTION_RETURN_REG;
  135. end
  136. else
  137. { Return in FPU register? }
  138. if p.rettype.def.deftype=floatdef then
  139. begin
  140. paraloc.loc:=LOC_FPUREGISTER;
  141. paraloc.register:=NR_FPU_RESULT_REG;
  142. end
  143. else
  144. { Return in register? }
  145. if not ret_in_param(p.rettype.def,p.proccalloption) then
  146. begin
  147. paraloc.loc:=LOC_REGISTER;
  148. {$ifndef cpu64bit}
  149. if paraloc.size in [OS_64,OS_S64] then
  150. begin
  151. paraloc.lochigh:=LOC_REGISTER;
  152. if side=callerside then
  153. paraloc.register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  154. else
  155. paraloc.register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  156. if side=callerside then
  157. paraloc.register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  158. else
  159. paraloc.register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  160. end
  161. else
  162. {$endif cpu64bit}
  163. begin
  164. if side=callerside then
  165. paraloc.register:=NR_FUNCTION_RESULT_REG
  166. else
  167. paraloc.register:=NR_FUNCTION_RETURN_REG;
  168. end;
  169. end
  170. else
  171. begin
  172. paraloc.loc:=LOC_REFERENCE;
  173. end;
  174. p.funcret_paraloc[side]:=paraloc;
  175. end;
  176. procedure tsparcparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;firstpara:tparaitem;
  177. var intparareg,parasize:longint);
  178. var
  179. paraloc : tparalocation;
  180. hp : tparaitem;
  181. is_64bit: boolean;
  182. hparasupregs : pparasupregs;
  183. begin
  184. if side=callerside then
  185. hparasupregs:=@paraoutsupregs
  186. else
  187. hparasupregs:=@parainsupregs;
  188. hp:=firstpara;
  189. while assigned(hp) do
  190. begin
  191. fillchar(paraloc,sizeof(paraloc),0);
  192. paraloc.Alignment:= std_param_align;
  193. if push_addr_param(hp.paratyp,hp.paratype.def,p.proccalloption) or (hp.paratyp in [vs_var,vs_out]) then
  194. paraloc.size:=OS_ADDR
  195. else
  196. begin
  197. paraloc.size:=def_cgSize(hp.paratype.def);
  198. if paraloc.size=OS_NO then
  199. paraloc.size:=OS_ADDR;
  200. end;
  201. is_64bit:=(paraloc.size in [OS_64,OS_S64,OS_F64]);
  202. if (intparareg<=high(tparasupregs)-ord(is_64bit)) then
  203. begin
  204. paraloc.loc:=LOC_REGISTER;
  205. { big endian }
  206. if is_64bit then
  207. begin
  208. paraloc.registerhigh:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  209. paraloc.lochigh:=LOC_REGISTER;
  210. inc(intparareg);
  211. end;
  212. paraloc.registerlow:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  213. inc(intparareg);
  214. end
  215. else
  216. begin
  217. paraloc.loc:=LOC_REFERENCE;
  218. { Low part need to be in O5 if still available }
  219. if (intparareg<=high(tparasupregs)) then
  220. begin
  221. paraloc.low_in_reg:=true;
  222. paraloc.lowreg:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  223. inc(intparareg);
  224. end;
  225. if side=callerside then
  226. paraloc.reference.index:=NR_STACK_POINTER_REG
  227. else
  228. paraloc.reference.index:=NR_FRAME_POINTER_REG;
  229. paraloc.reference.offset:=target_info.first_parm_offset+parasize;
  230. if is_64bit and
  231. (not paraloc.low_in_reg) then
  232. inc(parasize,8)
  233. else
  234. inc(parasize,4);
  235. end;
  236. hp.paraloc[side]:=paraloc;
  237. hp:=TParaItem(hp.Next);
  238. end;
  239. end;
  240. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargspara):longint;
  241. var
  242. intparareg,
  243. parasize : longint;
  244. begin
  245. intparareg:=0;
  246. parasize:=0;
  247. { calculate the registers for the normal parameters }
  248. create_paraloc_info_intern(p,callerside,tparaitem(p.para.first),intparareg,parasize);
  249. { append the varargs }
  250. create_paraloc_info_intern(p,callerside,tparaitem(varargspara.first),intparareg,parasize);
  251. result:=parasize;
  252. end;
  253. function tsparcparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  254. var
  255. intparareg,
  256. parasize : longint;
  257. begin
  258. intparareg:=0;
  259. parasize:=0;
  260. create_paraloc_info_intern(p,side,tparaitem(p.para.first),intparareg,parasize);
  261. { Create Function result paraloc }
  262. create_funcret_paraloc_info(p,side);
  263. { We need to return the size allocated on the stack }
  264. result:=parasize;
  265. end;
  266. procedure tsparcparamanager.allocparaloc(list: taasmoutput; const loc: tparalocation);
  267. begin
  268. if (loc.loc=LOC_REFERENCE) and
  269. (loc.low_in_reg) then
  270. cg.GetExplicitRegister(list,loc.lowreg);
  271. inherited allocparaloc(list,loc);
  272. end;
  273. procedure tsparcparamanager.freeparaloc(list: taasmoutput; const loc: tparalocation);
  274. begin
  275. if (loc.loc=LOC_REFERENCE) and
  276. (loc.low_in_reg) then
  277. cg.UnGetRegister(list,loc.lowreg);
  278. inherited freeparaloc(list,loc);
  279. end;
  280. procedure tsparcparamanager.splitparaloc64(const locpara:tparalocation;var loclopara,lochipara:tparalocation);
  281. begin
  282. { Word 0 is in register, word 1 is in reference }
  283. if (locpara.loc=LOC_REFERENCE) and locpara.low_in_reg then
  284. begin
  285. { high }
  286. lochipara:=locpara;
  287. if locpara.size=OS_S64 then
  288. lochipara.size:=OS_S32
  289. else
  290. lochipara.size:=OS_32;
  291. lochipara.low_in_reg:=false;
  292. { low }
  293. loclopara:=locpara;
  294. loclopara.size:=OS_32;
  295. loclopara.loc:=LOC_REGISTER;
  296. loclopara.register:=locpara.lowreg;
  297. end
  298. else
  299. inherited splitparaloc64(locpara,loclopara,lochipara);
  300. end;
  301. begin
  302. ParaManager:=TSparcParaManager.create;
  303. end.
  304. {
  305. $Log$
  306. Revision 1.40 2004-06-20 08:55:32 florian
  307. * logs truncated
  308. Revision 1.39 2004/06/16 20:07:10 florian
  309. * dwarf branch merged
  310. Revision 1.38.2.2 2004/05/31 22:08:21 peter
  311. * fix passing of >6 arguments
  312. Revision 1.38.2.1 2004/05/25 21:38:35 peter
  313. * constructors return self
  314. Revision 1.38 2004/03/15 14:39:56 mazen
  315. * make sparc para manager quite similar to ppc one to help
  316. copying evolutions.
  317. + Add support to var args in registers. need to be verfied as it
  318. was just copying ppc's one
  319. Revision 1.37 2004/03/09 13:05:49 mazen
  320. + give location for 64bit to fix IE 200402061
  321. }