cpupara.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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,symsym,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:tvarargsparalist):longint;override;
  38. private
  39. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  40. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  41. var intparareg,parasize:longint);
  42. end;
  43. implementation
  44. uses
  45. cutils,verbose,systems,
  46. defutil,
  47. cgutils,cgobj;
  48. type
  49. tparasupregs = array[0..5] of tsuperregister;
  50. pparasupregs = ^tparasupregs;
  51. const
  52. paraoutsupregs : tparasupregs = (RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5);
  53. parainsupregs : tparasupregs = (RS_I0,RS_I1,RS_I2,RS_I3,RS_I4,RS_I5);
  54. function TSparcParaManager.get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;
  55. begin
  56. result:=[RS_G1,RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5,RS_O6,RS_O7];
  57. end;
  58. function tsparcparamanager.get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;
  59. begin
  60. result:=[RS_F0..RS_F31];
  61. end;
  62. procedure TSparcParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint;var cgpara : tcgpara);
  63. var
  64. paraloc : pcgparalocation;
  65. begin
  66. if nr<1 then
  67. InternalError(2002100806);
  68. cgpara.reset;
  69. cgpara.size:=OS_INT;
  70. cgpara.alignment:=std_param_align;
  71. paraloc:=cgpara.add_location;
  72. with paraloc^ do
  73. begin
  74. { The six first parameters are passed into registers }
  75. dec(nr);
  76. if nr<6 then
  77. begin
  78. loc:=LOC_REGISTER;
  79. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  80. end
  81. else
  82. begin
  83. { The other parameters are passed on the stack }
  84. loc:=LOC_REFERENCE;
  85. reference.index:=NR_STACK_POINTER_REG;
  86. reference.offset:=92+(nr-6)*4;
  87. end;
  88. size:=OS_INT;
  89. end;
  90. end;
  91. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  92. begin
  93. result:=false;
  94. end;
  95. { true if a parameter is too large to copy and only the address is pushed }
  96. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  97. begin
  98. result:=false;
  99. { var,out always require address }
  100. if varspez in [vs_var,vs_out] then
  101. begin
  102. result:=true;
  103. exit;
  104. end;
  105. case def.deftype of
  106. recorddef,
  107. arraydef,
  108. variantdef,
  109. formaldef :
  110. push_addr_param:=true;
  111. objectdef :
  112. result:=is_object(def);
  113. stringdef :
  114. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  115. procvardef :
  116. result:=(po_methodpointer in tprocvardef(def).procoptions);
  117. setdef :
  118. result:=(tsetdef(def).settype<>smallset);
  119. end;
  120. end;
  121. procedure tsparcparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  122. var
  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. location_reset(p.funcretloc[side],LOC_INVALID,OS_NO);
  131. p.funcretloc[side].size:=retcgsize;
  132. { void has no location }
  133. if is_void(p.rettype.def) then
  134. begin
  135. p.funcretloc[side].loc:=LOC_VOID;
  136. exit;
  137. end;
  138. { Return in FPU register? }
  139. if p.rettype.def.deftype=floatdef then
  140. begin
  141. p.funcretloc[side].loc:=LOC_FPUREGISTER;
  142. p.funcretloc[side].register:=NR_FPU_RESULT_REG;
  143. if retcgsize=OS_F64 then
  144. setsubreg(p.funcretloc[side].register,R_SUBFD);
  145. p.funcretloc[side].size:=retcgsize;
  146. end
  147. else
  148. { Return in register? }
  149. if not ret_in_param(p.rettype.def,p.proccalloption) then
  150. begin
  151. {$ifndef cpu64bit}
  152. if retcgsize in [OS_64,OS_S64] then
  153. begin
  154. p.funcretloc[side].loc:=LOC_REGISTER;
  155. { high }
  156. if (side=callerside) or (p.proccalloption=pocall_inline)then
  157. p.funcretloc[side].register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  158. else
  159. p.funcretloc[side].register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  160. { low }
  161. if (side=callerside) or (p.proccalloption=pocall_inline) then
  162. p.funcretloc[side].register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  163. else
  164. p.funcretloc[side].register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  165. end
  166. else
  167. {$endif cpu64bit}
  168. begin
  169. p.funcretloc[side].loc:=LOC_REGISTER;
  170. p.funcretloc[side].size:=retcgsize;
  171. if (side=callerside) or (p.proccalloption=pocall_inline)then
  172. p.funcretloc[side].register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(retcgsize))
  173. else
  174. p.funcretloc[side].register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(retcgsize));
  175. end;
  176. end
  177. else
  178. begin
  179. p.funcretloc[side].loc:=LOC_REFERENCE;
  180. p.funcretloc[side].size:=retcgsize;
  181. end;
  182. end;
  183. procedure tsparcparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  184. var intparareg,parasize:longint);
  185. var
  186. paraloc : pcgparalocation;
  187. i : integer;
  188. hp : tparavarsym;
  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. for i:=0 to paras.count-1 do
  198. begin
  199. hp:=tparavarsym(paras[i]);
  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.vartype.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.varspez,hp.vartype.def,p.proccalloption) then
  213. paracgsize:=OS_ADDR
  214. else
  215. begin
  216. paracgsize:=def_cgSize(hp.vartype.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. end;
  253. end;
  254. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  255. var
  256. intparareg,
  257. parasize : longint;
  258. begin
  259. intparareg:=0;
  260. parasize:=0;
  261. { calculate the registers for the normal parameters }
  262. create_paraloc_info_intern(p,callerside,p.paras,intparareg,parasize);
  263. { append the varargs }
  264. create_paraloc_info_intern(p,callerside,varargspara,intparareg,parasize);
  265. result:=parasize;
  266. end;
  267. function tsparcparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  268. var
  269. intparareg,
  270. parasize : longint;
  271. begin
  272. intparareg:=0;
  273. parasize:=0;
  274. create_paraloc_info_intern(p,side,p.paras,intparareg,parasize);
  275. { Create Function result paraloc }
  276. create_funcretloc_info(p,side);
  277. { We need to return the size allocated on the stack }
  278. result:=parasize;
  279. end;
  280. begin
  281. ParaManager:=TSparcParaManager.create;
  282. end.
  283. {
  284. $Log$
  285. Revision 1.51 2004-11-22 22:01:19 peter
  286. * fixed varargs
  287. * replaced dynarray with tlist
  288. Revision 1.50 2004/11/21 18:13:31 peter
  289. * fixed funcretloc for sparc
  290. Revision 1.49 2004/11/21 17:54:59 peter
  291. * ttempcreatenode.create_reg merged into .create with parameter
  292. whether a register is allowed
  293. * funcret_paraloc renamed to funcretloc
  294. Revision 1.48 2004/11/21 17:17:04 florian
  295. * changed funcret location back to tlocation
  296. Revision 1.47 2004/11/15 23:35:31 peter
  297. * tparaitem removed, use tparavarsym instead
  298. * parameter order is now calculated from paranr value in tparavarsym
  299. Revision 1.46 2004/11/07 00:33:45 florian
  300. * marked o* registers as volatile
  301. Revision 1.45 2004/10/24 17:32:53 florian
  302. * fixed several arm compiler bugs
  303. Revision 1.44 2004/10/05 20:41:02 peter
  304. * more spilling rewrites
  305. Revision 1.43 2004/09/27 21:24:17 peter
  306. * fixed passing of flaot parameters. The general size is still float,
  307. only the size of the locations is now OS_32
  308. Revision 1.42 2004/09/25 20:28:39 florian
  309. * handling of C styled varargs fixed
  310. Revision 1.41 2004/09/21 17:25:13 peter
  311. * paraloc branch merged
  312. Revision 1.40.4.4 2004/09/19 18:08:15 peter
  313. * fixed order of 64 bit funcret registers
  314. Revision 1.40.4.3 2004/09/17 17:19:26 peter
  315. * fixed 64 bit unaryminus for sparc
  316. * fixed 64 bit inlining
  317. * signness of not operation
  318. Revision 1.40.4.2 2004/09/12 13:36:40 peter
  319. * fixed alignment issues
  320. Revision 1.40.4.1 2004/08/31 20:43:06 peter
  321. * paraloc patch
  322. Revision 1.40 2004/06/20 08:55:32 florian
  323. * logs truncated
  324. Revision 1.39 2004/06/16 20:07:10 florian
  325. * dwarf branch merged
  326. Revision 1.38.2.2 2004/05/31 22:08:21 peter
  327. * fix passing of >6 arguments
  328. Revision 1.38.2.1 2004/05/25 21:38:35 peter
  329. * constructors return self
  330. Revision 1.38 2004/03/15 14:39:56 mazen
  331. * make sparc para manager quite similar to ppc one to help
  332. copying evolutions.
  333. + Add support to var args in registers. need to be verfied as it
  334. was just copying ppc's one
  335. Revision 1.37 2004/03/09 13:05:49 mazen
  336. + give location for 64bit to fix IE 200402061
  337. }