cpupara.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.intsize:=tcgsize2size[OS_INT];
  71. cgpara.alignment:=std_param_align;
  72. paraloc:=cgpara.add_location;
  73. with paraloc^ do
  74. begin
  75. { The six first parameters are passed into registers }
  76. dec(nr);
  77. if nr<6 then
  78. begin
  79. loc:=LOC_REGISTER;
  80. register:=newreg(R_INTREGISTER,(RS_O0+nr),R_SUBWHOLE);
  81. end
  82. else
  83. begin
  84. { The other parameters are passed on the stack }
  85. loc:=LOC_REFERENCE;
  86. reference.index:=NR_STACK_POINTER_REG;
  87. reference.offset:=92+(nr-6)*4;
  88. end;
  89. size:=OS_INT;
  90. end;
  91. end;
  92. function tsparcparamanager.copy_value_on_stack(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  93. begin
  94. result:=false;
  95. end;
  96. { true if a parameter is too large to copy and only the address is pushed }
  97. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  98. begin
  99. result:=false;
  100. { var,out always require address }
  101. if varspez in [vs_var,vs_out] then
  102. begin
  103. result:=true;
  104. exit;
  105. end;
  106. case def.deftype of
  107. recorddef,
  108. arraydef,
  109. variantdef,
  110. formaldef :
  111. push_addr_param:=true;
  112. objectdef :
  113. result:=is_object(def);
  114. stringdef :
  115. result:=(tstringdef(def).string_typ in [st_shortstring,st_longstring]);
  116. procvardef :
  117. result:=(po_methodpointer in tprocvardef(def).procoptions);
  118. setdef :
  119. result:=(tsetdef(def).settype<>smallset);
  120. end;
  121. end;
  122. procedure tsparcparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  123. var
  124. retcgsize : tcgsize;
  125. begin
  126. { Constructors return self instead of a boolean }
  127. if (p.proctypeoption=potype_constructor) then
  128. retcgsize:=OS_ADDR
  129. else
  130. retcgsize:=def_cgsize(p.rettype.def);
  131. location_reset(p.funcretloc[side],LOC_INVALID,OS_NO);
  132. p.funcretloc[side].size:=retcgsize;
  133. { void has no location }
  134. if is_void(p.rettype.def) then
  135. begin
  136. p.funcretloc[side].loc:=LOC_VOID;
  137. exit;
  138. end;
  139. { Return in FPU register? }
  140. if p.rettype.def.deftype=floatdef then
  141. begin
  142. p.funcretloc[side].loc:=LOC_FPUREGISTER;
  143. p.funcretloc[side].register:=NR_FPU_RESULT_REG;
  144. if retcgsize=OS_F64 then
  145. setsubreg(p.funcretloc[side].register,R_SUBFD);
  146. p.funcretloc[side].size:=retcgsize;
  147. end
  148. else
  149. { Return in register? }
  150. if not ret_in_param(p.rettype.def,p.proccalloption) then
  151. begin
  152. {$ifndef cpu64bit}
  153. if retcgsize in [OS_64,OS_S64] then
  154. begin
  155. p.funcretloc[side].loc:=LOC_REGISTER;
  156. { high }
  157. if (side=callerside) or (p.proccalloption=pocall_inline)then
  158. p.funcretloc[side].register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  159. else
  160. p.funcretloc[side].register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  161. { low }
  162. if (side=callerside) or (p.proccalloption=pocall_inline) then
  163. p.funcretloc[side].register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  164. else
  165. p.funcretloc[side].register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  166. end
  167. else
  168. {$endif cpu64bit}
  169. begin
  170. p.funcretloc[side].loc:=LOC_REGISTER;
  171. p.funcretloc[side].size:=retcgsize;
  172. if (side=callerside) or (p.proccalloption=pocall_inline)then
  173. p.funcretloc[side].register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(retcgsize))
  174. else
  175. p.funcretloc[side].register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(retcgsize));
  176. end;
  177. end
  178. else
  179. begin
  180. p.funcretloc[side].loc:=LOC_REFERENCE;
  181. p.funcretloc[side].size:=retcgsize;
  182. end;
  183. end;
  184. procedure tsparcparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  185. var intparareg,parasize:longint);
  186. var
  187. paraloc : pcgparalocation;
  188. i : integer;
  189. hp : tparavarsym;
  190. paracgsize : tcgsize;
  191. hparasupregs : pparasupregs;
  192. paralen : longint;
  193. begin
  194. if side=callerside then
  195. hparasupregs:=@paraoutsupregs
  196. else
  197. hparasupregs:=@parainsupregs;
  198. for i:=0 to paras.count-1 do
  199. begin
  200. hp:=tparavarsym(paras[i]);
  201. { currently only support C-style array of const,
  202. there should be no location assigned to the vararg array itself }
  203. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  204. is_array_of_const(hp.vartype.def) then
  205. begin
  206. paraloc:=hp.paraloc[side].add_location;
  207. { hack: the paraloc must be valid, but is not actually used }
  208. paraloc^.loc:=LOC_REGISTER;
  209. paraloc^.register:=NR_G0;
  210. paraloc^.size:=OS_ADDR;
  211. break;
  212. end;
  213. if push_addr_param(hp.varspez,hp.vartype.def,p.proccalloption) then
  214. paracgsize:=OS_ADDR
  215. else
  216. begin
  217. paracgsize:=def_cgSize(hp.vartype.def);
  218. if paracgsize=OS_NO then
  219. paracgsize:=OS_ADDR;
  220. end;
  221. hp.paraloc[side].reset;
  222. hp.paraloc[side].size:=paracgsize;
  223. hp.paraloc[side].Alignment:=std_param_align;
  224. paralen:=tcgsize2size[paracgsize];
  225. hp.paraloc[side].intsize:=paralen;
  226. while paralen>0 do
  227. begin
  228. paraloc:=hp.paraloc[side].add_location;
  229. { Floats are passed in int registers,
  230. We can allocate at maximum 32 bits per register }
  231. if paracgsize in [OS_64,OS_S64,OS_F32,OS_F64] then
  232. paraloc^.size:=OS_32
  233. else
  234. paraloc^.size:=paracgsize;
  235. { ret in param? }
  236. if vo_is_funcret in hp.varoptions then
  237. begin
  238. paraloc^.loc:=LOC_REFERENCE;
  239. if side=callerside then
  240. paraloc^.reference.index:=NR_STACK_POINTER_REG
  241. else
  242. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  243. paraloc^.reference.offset:=64;
  244. end
  245. else if (intparareg<=high(tparasupregs)) then
  246. begin
  247. paraloc^.loc:=LOC_REGISTER;
  248. paraloc^.register:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  249. inc(intparareg);
  250. end
  251. else
  252. begin
  253. paraloc^.loc:=LOC_REFERENCE;
  254. if side=callerside then
  255. paraloc^.reference.index:=NR_STACK_POINTER_REG
  256. else
  257. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  258. paraloc^.reference.offset:=target_info.first_parm_offset+parasize;
  259. { Parameters are aligned at 4 bytes }
  260. inc(parasize,align(tcgsize2size[paraloc^.size],sizeof(aint)));
  261. end;
  262. dec(paralen,tcgsize2size[paraloc^.size]);
  263. end;
  264. end;
  265. end;
  266. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  267. var
  268. intparareg,
  269. parasize : longint;
  270. begin
  271. intparareg:=0;
  272. parasize:=0;
  273. { calculate the registers for the normal parameters }
  274. create_paraloc_info_intern(p,callerside,p.paras,intparareg,parasize);
  275. { append the varargs }
  276. create_paraloc_info_intern(p,callerside,varargspara,intparareg,parasize);
  277. result:=parasize;
  278. end;
  279. function tsparcparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  280. var
  281. intparareg,
  282. parasize : longint;
  283. begin
  284. intparareg:=0;
  285. parasize:=0;
  286. create_paraloc_info_intern(p,side,p.paras,intparareg,parasize);
  287. { Create Function result paraloc }
  288. create_funcretloc_info(p,side);
  289. { We need to return the size allocated on the stack }
  290. result:=parasize;
  291. end;
  292. begin
  293. ParaManager:=TSparcParaManager.create;
  294. end.
  295. {
  296. $Log$
  297. Revision 1.53 2005-01-10 21:50:05 jonas
  298. + support for passing records in registers under darwin
  299. * tcgpara now also has an intsize field, which contains the size in
  300. bytes of the whole parameter
  301. Revision 1.52 2005/01/07 16:22:54 florian
  302. + implemented abi compliant handling of strucutured functions results on sparc platform
  303. Revision 1.51 2004/11/22 22:01:19 peter
  304. * fixed varargs
  305. * replaced dynarray with tlist
  306. Revision 1.50 2004/11/21 18:13:31 peter
  307. * fixed funcretloc for sparc
  308. Revision 1.49 2004/11/21 17:54:59 peter
  309. * ttempcreatenode.create_reg merged into .create with parameter
  310. whether a register is allowed
  311. * funcret_paraloc renamed to funcretloc
  312. Revision 1.48 2004/11/21 17:17:04 florian
  313. * changed funcret location back to tlocation
  314. Revision 1.47 2004/11/15 23:35:31 peter
  315. * tparaitem removed, use tparavarsym instead
  316. * parameter order is now calculated from paranr value in tparavarsym
  317. Revision 1.46 2004/11/07 00:33:45 florian
  318. * marked o* registers as volatile
  319. Revision 1.45 2004/10/24 17:32:53 florian
  320. * fixed several arm compiler bugs
  321. Revision 1.44 2004/10/05 20:41:02 peter
  322. * more spilling rewrites
  323. Revision 1.43 2004/09/27 21:24:17 peter
  324. * fixed passing of flaot parameters. The general size is still float,
  325. only the size of the locations is now OS_32
  326. Revision 1.42 2004/09/25 20:28:39 florian
  327. * handling of C styled varargs fixed
  328. Revision 1.41 2004/09/21 17:25:13 peter
  329. * paraloc branch merged
  330. Revision 1.40.4.4 2004/09/19 18:08:15 peter
  331. * fixed order of 64 bit funcret registers
  332. Revision 1.40.4.3 2004/09/17 17:19:26 peter
  333. * fixed 64 bit unaryminus for sparc
  334. * fixed 64 bit inlining
  335. * signness of not operation
  336. Revision 1.40.4.2 2004/09/12 13:36:40 peter
  337. * fixed alignment issues
  338. Revision 1.40.4.1 2004/08/31 20:43:06 peter
  339. * paraloc patch
  340. Revision 1.40 2004/06/20 08:55:32 florian
  341. * logs truncated
  342. Revision 1.39 2004/06/16 20:07:10 florian
  343. * dwarf branch merged
  344. Revision 1.38.2.2 2004/05/31 22:08:21 peter
  345. * fix passing of >6 arguments
  346. Revision 1.38.2.1 2004/05/25 21:38:35 peter
  347. * constructors return self
  348. Revision 1.38 2004/03/15 14:39:56 mazen
  349. * make sparc para manager quite similar to ppc one to help
  350. copying evolutions.
  351. + Add support to var args in registers. need to be verfied as it
  352. was just copying ppc's one
  353. Revision 1.37 2004/03/09 13:05:49 mazen
  354. + give location for 64bit to fix IE 200402061
  355. }