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,parabase,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. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara : TCGPara);override;
  36. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  37. function create_varargs_paraloc_info(p : TAbstractProcDef; varargspara:tvarargspara):longint;override;
  38. private
  39. procedure create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  40. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; firstpara: tparaitem;
  41. var intparareg,parasize:longint);
  42. end;
  43. implementation
  44. uses
  45. cutils,verbose,systems,
  46. defutil,cgobj;
  47. type
  48. tparasupregs = array[0..5] of tsuperregister;
  49. pparasupregs = ^tparasupregs;
  50. const
  51. paraoutsupregs : tparasupregs = (RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5);
  52. parainsupregs : tparasupregs = (RS_I0,RS_I1,RS_I2,RS_I3,RS_I4,RS_I5);
  53. function TSparcParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  54. begin
  55. result:=[RS_G1];
  56. end;
  57. function tsparcparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  58. begin
  59. result:=[RS_F0..RS_F31];
  60. end;
  61. procedure TSparcParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint;var cgpara : tcgpara);
  62. var
  63. paraloc : pcgparalocation;
  64. begin
  65. if nr<1 then
  66. InternalError(2002100806);
  67. cgpara.reset;
  68. cgpara.size:=OS_INT;
  69. cgpara.alignment:=std_param_align;
  70. paraloc:=cgpara.add_location;
  71. with paraloc^ do
  72. begin
  73. { The six first parameters are passed into registers }
  74. dec(nr);
  75. if nr<6 then
  76. begin
  77. loc:=LOC_REGISTER;
  78. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  79. end
  80. else
  81. begin
  82. { The other parameters are passed on the stack }
  83. loc:=LOC_REFERENCE;
  84. reference.index:=NR_STACK_POINTER_REG;
  85. reference.offset:=92+(nr-6)*4;
  86. end;
  87. size:=OS_INT;
  88. end;
  89. end;
  90. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  91. begin
  92. result:=false;
  93. end;
  94. { true if a parameter is too large to copy and only the address is pushed }
  95. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  96. begin
  97. result:=false;
  98. { var,out always require address }
  99. if varspez in [vs_var,vs_out] then
  100. begin
  101. result:=true;
  102. exit;
  103. end;
  104. case def.deftype of
  105. recorddef,
  106. arraydef,
  107. variantdef,
  108. formaldef :
  109. push_addr_param:=true;
  110. objectdef :
  111. result:=is_object(def);
  112. stringdef :
  113. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  114. procvardef :
  115. result:=(po_methodpointer in tprocvardef(def).procoptions);
  116. setdef :
  117. result:=(tsetdef(def).settype<>smallset);
  118. end;
  119. end;
  120. procedure tsparcparamanager.create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  121. var
  122. paraloc : pcgparalocation;
  123. retcgsize : tcgsize;
  124. begin
  125. { Constructors return self instead of a boolean }
  126. if (p.proctypeoption=potype_constructor) then
  127. retcgsize:=OS_ADDR
  128. else
  129. retcgsize:=def_cgsize(p.rettype.def);
  130. p.funcret_paraloc[side].reset;
  131. p.funcret_paraloc[side].Alignment:=std_param_align;
  132. p.funcret_paraloc[side].size:=retcgsize;
  133. { void has no location }
  134. if is_void(p.rettype.def) then
  135. exit;
  136. paraloc:=p.funcret_paraloc[side].add_location;
  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. paraloc^.size:=retcgsize;
  143. end
  144. else
  145. { Return in register? }
  146. if not ret_in_param(p.rettype.def,p.proccalloption) then
  147. begin
  148. {$ifndef cpu64bit}
  149. if retcgsize in [OS_64,OS_S64] then
  150. begin
  151. { high }
  152. paraloc^.loc:=LOC_REGISTER;
  153. paraloc^.size:=OS_32;
  154. if (side=callerside) or (p.proccalloption=pocall_inline)then
  155. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  156. else
  157. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  158. { low }
  159. paraloc:=p.funcret_paraloc[side].add_location;
  160. paraloc^.loc:=LOC_REGISTER;
  161. paraloc^.size:=OS_32;
  162. if (side=callerside) or (p.proccalloption=pocall_inline) then
  163. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  164. else
  165. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  166. end
  167. else
  168. {$endif cpu64bit}
  169. begin
  170. paraloc^.loc:=LOC_REGISTER;
  171. paraloc^.size:=retcgsize;
  172. if (side=callerside) or (p.proccalloption=pocall_inline)then
  173. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(retcgsize))
  174. else
  175. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(retcgsize));
  176. end;
  177. end
  178. else
  179. begin
  180. paraloc^.loc:=LOC_REFERENCE;
  181. paraloc^.size:=retcgsize;
  182. end;
  183. end;
  184. procedure tsparcparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;firstpara:tparaitem;
  185. var intparareg,parasize:longint);
  186. var
  187. paraloc : pcgparalocation;
  188. hp : tparaitem;
  189. paracgsize : tcgsize;
  190. hparasupregs : pparasupregs;
  191. paralen : longint;
  192. begin
  193. if side=callerside then
  194. hparasupregs:=@paraoutsupregs
  195. else
  196. hparasupregs:=@parainsupregs;
  197. hp:=firstpara;
  198. while assigned(hp) do
  199. begin
  200. { currently only support C-style array of const,
  201. there should be no location assigned to the vararg array itself }
  202. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  203. is_array_of_const(hp.paratype.def) then
  204. begin
  205. paraloc:=hp.paraloc[side].add_location;
  206. { hack: the paraloc must be valid, but is not actually used }
  207. paraloc^.loc:=LOC_REGISTER;
  208. paraloc^.register:=NR_G0;
  209. paraloc^.size:=OS_ADDR;
  210. break;
  211. end;
  212. if push_addr_param(hp.paratyp,hp.paratype.def,p.proccalloption) then
  213. paracgsize:=OS_ADDR
  214. else
  215. begin
  216. paracgsize:=def_cgSize(hp.paratype.def);
  217. if paracgsize=OS_NO then
  218. paracgsize:=OS_ADDR;
  219. end;
  220. hp.paraloc[side].reset;
  221. hp.paraloc[side].size:=paracgsize;
  222. hp.paraloc[side].Alignment:=std_param_align;
  223. paralen:=tcgsize2size[paracgsize];
  224. while (paralen>0) do
  225. begin
  226. paraloc:=hp.paraloc[side].add_location;
  227. { Floats are passed in int registers,
  228. We can allocate at maximum 32 bits per register }
  229. if paracgsize in [OS_64,OS_S64,OS_F32,OS_F64] then
  230. paraloc^.size:=OS_32
  231. else
  232. paraloc^.size:=paracgsize;
  233. if (intparareg<=high(tparasupregs)) then
  234. begin
  235. paraloc^.loc:=LOC_REGISTER;
  236. paraloc^.register:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  237. inc(intparareg);
  238. end
  239. else
  240. begin
  241. paraloc^.loc:=LOC_REFERENCE;
  242. if side=callerside then
  243. paraloc^.reference.index:=NR_STACK_POINTER_REG
  244. else
  245. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  246. paraloc^.reference.offset:=target_info.first_parm_offset+parasize;
  247. { Parameters are aligned at 4 bytes }
  248. inc(parasize,align(tcgsize2size[paraloc^.size],sizeof(aint)));
  249. end;
  250. dec(paralen,tcgsize2size[paraloc^.size]);
  251. end;
  252. hp:=TParaItem(hp.Next);
  253. end;
  254. end;
  255. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargspara):longint;
  256. var
  257. intparareg,
  258. parasize : longint;
  259. begin
  260. intparareg:=0;
  261. parasize:=0;
  262. { calculate the registers for the normal parameters }
  263. create_paraloc_info_intern(p,callerside,tparaitem(p.para.first),intparareg,parasize);
  264. { append the varargs }
  265. create_paraloc_info_intern(p,callerside,tparaitem(varargspara.first),intparareg,parasize);
  266. result:=parasize;
  267. end;
  268. function tsparcparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  269. var
  270. intparareg,
  271. parasize : longint;
  272. begin
  273. intparareg:=0;
  274. parasize:=0;
  275. create_paraloc_info_intern(p,side,tparaitem(p.para.first),intparareg,parasize);
  276. { Create Function result paraloc }
  277. create_funcret_paraloc_info(p,side);
  278. { We need to return the size allocated on the stack }
  279. result:=parasize;
  280. end;
  281. begin
  282. ParaManager:=TSparcParaManager.create;
  283. end.
  284. {
  285. $Log$
  286. Revision 1.43 2004-09-27 21:24:17 peter
  287. * fixed passing of flaot parameters. The general size is still float,
  288. only the size of the locations is now OS_32
  289. Revision 1.42 2004/09/25 20:28:39 florian
  290. * handling of C styled varargs fixed
  291. Revision 1.41 2004/09/21 17:25:13 peter
  292. * paraloc branch merged
  293. Revision 1.40.4.4 2004/09/19 18:08:15 peter
  294. * fixed order of 64 bit funcret registers
  295. Revision 1.40.4.3 2004/09/17 17:19:26 peter
  296. * fixed 64 bit unaryminus for sparc
  297. * fixed 64 bit inlining
  298. * signness of not operation
  299. Revision 1.40.4.2 2004/09/12 13:36:40 peter
  300. * fixed alignment issues
  301. Revision 1.40.4.1 2004/08/31 20:43:06 peter
  302. * paraloc patch
  303. Revision 1.40 2004/06/20 08:55:32 florian
  304. * logs truncated
  305. Revision 1.39 2004/06/16 20:07:10 florian
  306. * dwarf branch merged
  307. Revision 1.38.2.2 2004/05/31 22:08:21 peter
  308. * fix passing of >6 arguments
  309. Revision 1.38.2.1 2004/05/25 21:38:35 peter
  310. * constructors return self
  311. Revision 1.38 2004/03/15 14:39:56 mazen
  312. * make sparc para manager quite similar to ppc one to help
  313. copying evolutions.
  314. + Add support to var args in registers. need to be verfied as it
  315. was just copying ppc's one
  316. Revision 1.37 2004/03/09 13:05:49 mazen
  317. + give location for 64bit to fix IE 200402061
  318. }