cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Calling conventions for the SPARC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *****************************************************************************}
  16. unit cpupara;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. globtype,
  21. cclasses,
  22. aasmtai,aasmdata,
  23. cpubase,cpuinfo,
  24. symconst,symbase,symsym,symtype,symdef,paramgr,parabase,cgbase,cgutils;
  25. type
  26. TSparcParaManager=class(TParaManager)
  27. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  28. function get_volatile_registers_int(calloption : tproccalloption):TCpuRegisterSet;override;
  29. function get_volatile_registers_fpu(calloption : tproccalloption):TCpuRegisterSet;override;
  30. {Returns a structure giving the information on the storage of the parameter
  31. (which must be an integer parameter)
  32. @param(nr Parameter number of routine, starting from 1)}
  33. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara : TCGPara);override;
  34. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  35. function create_varargs_paraloc_info(p : TAbstractProcDef; varargspara:tvarargsparalist):longint;override;
  36. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tlocation;override;
  37. private
  38. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  39. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  40. var intparareg,parasize:longint);
  41. end;
  42. implementation
  43. uses
  44. cutils,verbose,systems,
  45. defutil,
  46. 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,RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5,RS_O6,RS_O7];
  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_ADDR;
  69. cgpara.intsize:=sizeof(pint);
  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. { true if a parameter is too large to copy and only the address is pushed }
  92. function tsparcparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  93. begin
  94. result:=false;
  95. { var,out always require address }
  96. if varspez in [vs_var,vs_out] then
  97. begin
  98. result:=true;
  99. exit;
  100. end;
  101. case def.typ of
  102. arraydef:
  103. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  104. is_open_array(def) or
  105. is_array_of_const(def) or
  106. is_array_constructor(def);
  107. recorddef,
  108. variantdef,
  109. formaldef :
  110. result:=true;
  111. objectdef :
  112. result:=is_object(def);
  113. stringdef :
  114. result:=(tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  115. procvardef :
  116. result:=(po_methodpointer in tprocvardef(def).procoptions);
  117. setdef :
  118. result:=not is_smallset(def);
  119. end;
  120. end;
  121. procedure tsparcparamanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  122. begin
  123. p.funcretloc[side]:=get_funcretloc(p,side,p.returndef);
  124. end;
  125. function tsparcparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tlocation;
  126. var
  127. retcgsize : tcgsize;
  128. begin
  129. { Constructors return self instead of a boolean }
  130. if (p.proctypeoption=potype_constructor) then
  131. retcgsize:=OS_ADDR
  132. else
  133. retcgsize:=def_cgsize(def);
  134. location_reset(result,LOC_INVALID,OS_NO);
  135. result.size:=retcgsize;
  136. { void has no location }
  137. if is_void(def) then
  138. begin
  139. result.loc:=LOC_VOID;
  140. exit;
  141. end;
  142. { Return is passed as var parameter }
  143. if ret_in_param(def,p.proccalloption) then
  144. begin
  145. result.loc:=LOC_REFERENCE;
  146. result.size:=retcgsize;
  147. exit;
  148. end;
  149. { Return in FPU register? }
  150. if def.typ=floatdef then
  151. begin
  152. result.loc:=LOC_FPUREGISTER;
  153. result.register:=NR_FPU_RESULT_REG;
  154. if retcgsize=OS_F64 then
  155. setsubreg(result.register,R_SUBFD);
  156. result.size:=retcgsize;
  157. end
  158. else
  159. { Return in register }
  160. begin
  161. {$ifndef cpu64bitaddr}
  162. if retcgsize in [OS_64,OS_S64] then
  163. begin
  164. result.loc:=LOC_REGISTER;
  165. { high }
  166. if (side=callerside) or (po_inline in p.procoptions) then
  167. result.register64.reghi:=NR_FUNCTION_RESULT64_HIGH_REG
  168. else
  169. result.register64.reghi:=NR_FUNCTION_RETURN64_HIGH_REG;
  170. { low }
  171. if (side=callerside) or (po_inline in p.procoptions) then
  172. result.register64.reglo:=NR_FUNCTION_RESULT64_LOW_REG
  173. else
  174. result.register64.reglo:=NR_FUNCTION_RETURN64_LOW_REG;
  175. end
  176. else
  177. {$endif not cpu64bitaddr}
  178. begin
  179. result.loc:=LOC_REGISTER;
  180. result.size:=retcgsize;
  181. if (side=callerside) then
  182. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  183. else
  184. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  185. end;
  186. end;
  187. end;
  188. procedure tsparcparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  189. var intparareg,parasize:longint);
  190. var
  191. paraloc : pcgparalocation;
  192. i : integer;
  193. hp : tparavarsym;
  194. paracgsize : tcgsize;
  195. hparasupregs : pparasupregs;
  196. paralen : longint;
  197. begin
  198. if side=callerside then
  199. hparasupregs:=@paraoutsupregs
  200. else
  201. hparasupregs:=@parainsupregs;
  202. for i:=0 to paras.count-1 do
  203. begin
  204. hp:=tparavarsym(paras[i]);
  205. { currently only support C-style array of const,
  206. there should be no location assigned to the vararg array itself }
  207. if (p.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  208. is_array_of_const(hp.vardef) then
  209. begin
  210. paraloc:=hp.paraloc[side].add_location;
  211. { hack: the paraloc must be valid, but is not actually used }
  212. paraloc^.loc:=LOC_REGISTER;
  213. paraloc^.register:=NR_G0;
  214. paraloc^.size:=OS_ADDR;
  215. break;
  216. end;
  217. if push_addr_param(hp.varspez,hp.vardef,p.proccalloption) then
  218. paracgsize:=OS_ADDR
  219. else
  220. begin
  221. paracgsize:=def_cgSize(hp.vardef);
  222. if paracgsize=OS_NO then
  223. paracgsize:=OS_ADDR;
  224. end;
  225. hp.paraloc[side].reset;
  226. hp.paraloc[side].size:=paracgsize;
  227. if (side = callerside) then
  228. hp.paraloc[side].Alignment:=std_param_align
  229. else
  230. hp.paraloc[side].Alignment:=hp.vardef.alignment;
  231. paralen:=tcgsize2size[paracgsize];
  232. hp.paraloc[side].intsize:=paralen;
  233. while paralen>0 do
  234. begin
  235. paraloc:=hp.paraloc[side].add_location;
  236. { Floats are passed in int registers,
  237. We can allocate at maximum 32 bits per register }
  238. if paracgsize in [OS_64,OS_S64,OS_F32,OS_F64] then
  239. paraloc^.size:=OS_32
  240. else
  241. paraloc^.size:=paracgsize;
  242. { ret in param? }
  243. if vo_is_funcret in hp.varoptions then
  244. begin
  245. paraloc^.loc:=LOC_REFERENCE;
  246. if side=callerside then
  247. paraloc^.reference.index:=NR_STACK_POINTER_REG
  248. else
  249. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  250. paraloc^.reference.offset:=64;
  251. end
  252. else if (intparareg<=high(tparasupregs)) then
  253. begin
  254. paraloc^.loc:=LOC_REGISTER;
  255. paraloc^.register:=newreg(R_INTREGISTER,hparasupregs^[intparareg],R_SUBWHOLE);
  256. inc(intparareg);
  257. end
  258. else
  259. begin
  260. paraloc^.loc:=LOC_REFERENCE;
  261. if side=callerside then
  262. paraloc^.reference.index:=NR_STACK_POINTER_REG
  263. else
  264. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  265. paraloc^.reference.offset:=target_info.first_parm_offset+parasize;
  266. { Parameters are aligned at 4 bytes }
  267. inc(parasize,align(tcgsize2size[paraloc^.size],sizeof(pint)));
  268. end;
  269. dec(paralen,tcgsize2size[paraloc^.size]);
  270. end;
  271. end;
  272. end;
  273. function TSparcParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  274. var
  275. intparareg,
  276. parasize : longint;
  277. begin
  278. intparareg:=0;
  279. parasize:=0;
  280. { calculate the registers for the normal parameters }
  281. create_paraloc_info_intern(p,callerside,p.paras,intparareg,parasize);
  282. { append the varargs }
  283. create_paraloc_info_intern(p,callerside,varargspara,intparareg,parasize);
  284. result:=parasize;
  285. end;
  286. function tsparcparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  287. var
  288. intparareg,
  289. parasize : longint;
  290. begin
  291. intparareg:=0;
  292. parasize:=0;
  293. create_paraloc_info_intern(p,side,p.paras,intparareg,parasize);
  294. { Create Function result paraloc }
  295. create_funcretloc_info(p,side);
  296. { We need to return the size allocated on the stack }
  297. result:=parasize;
  298. end;
  299. begin
  300. ParaManager:=TSparcParaManager.create;
  301. end.