cpupara.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. {
  2. $Id$
  3. Copyright (c) 2002 by Florian Klaempfl
  4. Generates the argument location information for x86-64 target
  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 bymethodpointer
  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. }
  18. { Generates the argument location information for x86-64 target.
  19. }
  20. unit cpupara;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. cpubase,cgbase,
  26. symconst,symbase,symtype,symdef,
  27. aasmtai,
  28. paramgr;
  29. type
  30. tx86_64paramanager = class(tparamanager)
  31. private
  32. procedure create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  33. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;firstpara:tparaitem;
  34. var intparareg,mmparareg,parasize:longint);
  35. public
  36. function getintparaloc(calloption : tproccalloption; nr : longint) : tparalocation;override;
  37. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;override;
  38. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;override;
  39. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;override;
  40. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  41. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargspara):longint;override;
  42. end;
  43. implementation
  44. uses
  45. cutils,verbose,
  46. cpuinfo,systems,
  47. defutil,
  48. tgobj;
  49. const
  50. paraintsupregs : array[0..5] of tsuperregister = (RS_RDI,RS_RSI,RS_RDX,RS_RCX,RS_R8,RS_R9);
  51. parammsupregs : array[0..7] of tsuperregister = (RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7);
  52. procedure getvalueparaloc(p : tdef;var paraloc:tparalocation);
  53. begin
  54. paraloc.size:=def_cgsize(p);
  55. paraloc.loc:=LOC_INVALID;
  56. paraloc.lochigh:=LOC_INVALID;
  57. case p.deftype of
  58. orddef:
  59. begin
  60. paraloc.loc:=LOC_REGISTER;
  61. {$warning TODO 128bit also needs lochigh}
  62. end;
  63. floatdef:
  64. begin
  65. case tfloatdef(p).typ of
  66. s80real:
  67. paraloc.loc:=LOC_REFERENCE;
  68. s32real,
  69. s64real,
  70. s64currency :
  71. paraloc.loc:=LOC_MMREGISTER;
  72. s64comp :
  73. begin
  74. paraloc.loc:=LOC_REGISTER;
  75. { Force Integer size }
  76. paraloc.size:=OS_64;
  77. end;
  78. s128real:
  79. begin
  80. paraloc.loc:=LOC_MMREGISTER;
  81. paraloc.lochigh:=LOC_MMREGISTER;
  82. {$warning TODO float 128bit needs SSEUP lochigh}
  83. end;
  84. end;
  85. end;
  86. recorddef:
  87. begin
  88. if p.size<=16 then
  89. begin
  90. {$warning TODO location depends on the fields}
  91. paraloc.loc:=LOC_REFERENCE;
  92. end
  93. else
  94. paraloc.loc:=LOC_REFERENCE;
  95. end;
  96. objectdef:
  97. begin
  98. if is_object(p) then
  99. paraloc.loc:=LOC_REFERENCE
  100. else
  101. paraloc.loc:=LOC_REGISTER;
  102. end;
  103. arraydef:
  104. paraloc.loc:=LOC_REFERENCE;
  105. variantdef:
  106. paraloc.loc:=LOC_REFERENCE;
  107. stringdef:
  108. if is_shortstring(p) or is_longstring(p) then
  109. paraloc.loc:=LOC_REFERENCE
  110. else
  111. paraloc.loc:=LOC_REGISTER;
  112. setdef:
  113. if is_smallset(p) then
  114. paraloc.loc:=LOC_REGISTER
  115. else
  116. paraloc.loc:=LOC_REFERENCE;
  117. procvardef:
  118. begin
  119. { This is a record < 16 bytes }
  120. if (po_methodpointer in tprocvardef(p).procoptions) then
  121. begin
  122. paraloc.loc:=LOC_REGISTER;
  123. paraloc.lochigh:=LOC_REGISTER;
  124. end
  125. else
  126. paraloc.loc:=LOC_REGISTER;
  127. end;
  128. else
  129. begin
  130. { default for pointers,enums,etc }
  131. paraloc.loc:=LOC_REGISTER;
  132. end;
  133. end;
  134. end;
  135. function tx86_64paramanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  136. begin
  137. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  138. end;
  139. function tx86_64paramanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  140. begin
  141. result:=[RS_XMM0..RS_XMM15];
  142. end;
  143. function tx86_64paramanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  144. begin
  145. result:=[RS_ST0..RS_ST7];
  146. end;
  147. function tx86_64paramanager.getintparaloc(calloption : tproccalloption; nr : longint): tparalocation;
  148. begin
  149. fillchar(result,sizeof(tparalocation),0);
  150. result.size:=OS_INT;
  151. if nr<1 then
  152. internalerror(200304303)
  153. else if nr<=high(paraintsupregs)+1 then
  154. begin
  155. result.loc:=LOC_REGISTER;
  156. result.register:=newreg(R_INTREGISTER,paraintsupregs[nr-1],R_SUBWHOLE);
  157. end
  158. else
  159. begin
  160. result.loc:=LOC_REFERENCE;
  161. result.reference.index:=NR_STACK_POINTER_REG;
  162. result.reference.offset:=(nr-6)*8;
  163. end;
  164. end;
  165. procedure tx86_64paramanager.create_funcret_paraloc_info(p : tabstractprocdef; side: tcallercallee);
  166. var
  167. paraloc : tparalocation;
  168. begin
  169. { Function return }
  170. fillchar(paraloc,sizeof(tparalocation),0);
  171. if (p.proctypeoption=potype_constructor) then
  172. paraloc.size:=OS_ADDR
  173. else
  174. paraloc.size:=def_cgsize(p.rettype.def);
  175. if paraloc.size<>OS_NO then
  176. begin
  177. { Return in FPU register? }
  178. if p.rettype.def.deftype=floatdef then
  179. begin
  180. case tfloatdef(p.rettype.def).typ of
  181. s32real,s64real:
  182. begin
  183. paraloc.loc:=LOC_MMREGISTER;
  184. paraloc.register:=NR_MM_RESULT_REG;
  185. end;
  186. s64currency,
  187. s64comp,
  188. s80real:
  189. begin
  190. paraloc.loc:=LOC_FPUREGISTER;
  191. paraloc.register:=NR_FPU_RESULT_REG;
  192. end;
  193. else
  194. internalerror(200405034);
  195. end;
  196. end
  197. else
  198. { Return in register? }
  199. if not ret_in_param(p.rettype.def,p.proccalloption) then
  200. begin
  201. paraloc.loc:=LOC_REGISTER;
  202. paraloc.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(paraloc.size));
  203. end
  204. else
  205. begin
  206. paraloc.loc:=LOC_REFERENCE;
  207. end;
  208. end
  209. else
  210. paraloc.loc:=LOC_INVALID;
  211. p.funcret_paraloc[side]:=paraloc;
  212. end;
  213. procedure tx86_64paramanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;firstpara:tparaitem;
  214. var intparareg,mmparareg,parasize:longint);
  215. var
  216. hp : tparaitem;
  217. paraloc : tparalocation;
  218. subreg : tsubregister;
  219. pushaddr : boolean;
  220. l,
  221. varalign,
  222. paraalign : longint;
  223. begin
  224. paraalign:=get_para_align(p.proccalloption);
  225. { Register parameters are assigned from left to right }
  226. hp:=firstpara;
  227. while assigned(hp) do
  228. begin
  229. pushaddr:=push_addr_param(hp.paratyp,hp.paratype.def,p.proccalloption);
  230. if pushaddr then
  231. begin
  232. paraloc.size:=OS_ADDR;
  233. paraloc.loc:=LOC_REGISTER;
  234. paraloc.lochigh:=LOC_INVALID;
  235. end
  236. else
  237. getvalueparaloc(hp.paratype.def,paraloc);
  238. paraloc.alignment:=paraalign;
  239. { Location low }
  240. if (paraloc.loc=LOC_REGISTER) and
  241. (intparareg<=high(paraintsupregs)) then
  242. begin
  243. if (paraloc.size=OS_NO) or (paraloc.lochigh<>LOC_INVALID) then
  244. subreg:=R_SUBWHOLE
  245. else
  246. subreg:=cgsize2subreg(paraloc.size);
  247. paraloc.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  248. inc(intparareg);
  249. end
  250. else if (paraloc.loc=LOC_MMREGISTER) and
  251. (mmparareg<=high(parammsupregs)) then
  252. begin
  253. paraloc.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],R_SUBNONE);
  254. inc(mmparareg);
  255. end
  256. else
  257. begin
  258. paraloc.loc:=LOC_REFERENCE;
  259. paraloc.lochigh:=LOC_INVALID;
  260. if side=callerside then
  261. paraloc.reference.index:=NR_STACK_POINTER_REG
  262. else
  263. paraloc.reference.index:=NR_FRAME_POINTER_REG;
  264. l:=push_size(hp.paratyp,hp.paratype.def,p.proccalloption);
  265. varalign:=size_2_align(l);
  266. paraloc.reference.offset:=parasize;
  267. varalign:=used_align(varalign,paraalign,paraalign);
  268. parasize:=align(parasize+l,varalign);
  269. end;
  270. { Location High if required }
  271. if (paraloc.lochigh<>LOC_INVALID) then
  272. begin
  273. if (paraloc.lochigh=LOC_REGISTER) and
  274. (intparareg<=high(paraintsupregs)) then
  275. begin
  276. paraloc.registerhigh:=newreg(R_INTREGISTER,paraintsupregs[intparareg],R_SUBWHOLE);
  277. inc(intparareg);
  278. end
  279. else
  280. if (paraloc.lochigh=LOC_MMREGISTER) and
  281. (mmparareg<=high(parammsupregs)) then
  282. begin
  283. paraloc.registerhigh:=newreg(R_MMREGISTER,parammsupregs[mmparareg],R_SUBNONE);
  284. inc(mmparareg);
  285. end
  286. else
  287. begin
  288. { Release when location low has already registers
  289. assigned }
  290. if paraloc.loc=LOC_REGISTER then
  291. dec(intparareg);
  292. if paraloc.loc=LOC_MMREGISTER then
  293. dec(mmparareg);
  294. { Overwrite with LOC_REFERENCE }
  295. paraloc.loc:=LOC_REFERENCE;
  296. paraloc.lochigh:=LOC_INVALID;
  297. fillchar(paraloc.reference,sizeof(paraloc.reference),0);
  298. if side=callerside then
  299. paraloc.reference.index:=NR_STACK_POINTER_REG
  300. else
  301. paraloc.reference.index:=NR_FRAME_POINTER_REG;
  302. l:=push_size(hp.paratyp,hp.paratype.def,p.proccalloption);
  303. varalign:=size_2_align(l);
  304. paraloc.reference.offset:=parasize;
  305. varalign:=used_align(varalign,paraalign,paraalign);
  306. parasize:=align(parasize+l,varalign);
  307. end;
  308. end;
  309. hp.paraloc[side]:=paraloc;
  310. hp:=tparaitem(hp.next);
  311. end;
  312. { Register parameters are assigned from left-to-right, but the
  313. offsets on the stack are right-to-left. There is no need
  314. to reverse the offset, only adapt the calleeside with the
  315. start offset of the first param on the stack }
  316. if side=calleeside then
  317. begin
  318. hp:=tparaitem(p.para.first);
  319. while assigned(hp) do
  320. begin
  321. if (hp.paraloc[side].loc=LOC_REFERENCE) then
  322. inc(hp.paraloc[side].reference.offset,target_info.first_parm_offset);
  323. hp:=tparaitem(hp.next);
  324. end;
  325. end;
  326. end;
  327. function tx86_64paramanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargspara):longint;
  328. var
  329. intparareg,mmparareg,
  330. parasize : longint;
  331. begin
  332. intparareg:=0;
  333. mmparareg:=0;
  334. parasize:=0;
  335. { calculate the registers for the normal parameters }
  336. create_paraloc_info_intern(p,callerside,tparaitem(p.para.first),intparareg,mmparareg,parasize);
  337. { append the varargs }
  338. create_paraloc_info_intern(p,callerside,tparaitem(varargspara.first),intparareg,mmparareg,parasize);
  339. { store used no. of SSE registers, that needs to be passed in %AL }
  340. varargspara.mmregsused:=mmparareg;
  341. result:=parasize;
  342. end;
  343. function tx86_64paramanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  344. var
  345. intparareg,mmparareg,
  346. parasize : longint;
  347. begin
  348. intparareg:=0;
  349. mmparareg:=0;
  350. parasize:=0;
  351. create_paraloc_info_intern(p,side,tparaitem(p.para.first),intparareg,mmparareg,parasize);
  352. { Create Function result paraloc }
  353. create_funcret_paraloc_info(p,side);
  354. { We need to return the size allocated on the stack }
  355. result:=parasize;
  356. end;
  357. begin
  358. paramanager:=tx86_64paramanager.create;
  359. end.
  360. {
  361. $Log$
  362. Revision 1.9 2004-06-20 08:55:32 florian
  363. * logs truncated
  364. Revision 1.8 2004/06/16 20:07:11 florian
  365. * dwarf branch merged
  366. Revision 1.7.2.7 2004/05/03 20:18:52 peter
  367. * fixes for tprintf
  368. Revision 1.7.2.6 2004/05/02 21:37:35 florian
  369. * setting of func. ret. for i386 fixed
  370. Revision 1.7.2.5 2004/05/02 20:56:55 florian
  371. * more fixes to handle_return_value update
  372. Revision 1.7.2.4 2004/05/02 19:08:01 florian
  373. * rewrote tcgcallnode.handle_return_value
  374. }