cpupara.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generates the argument location information for x86-64 target
  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 bymethodpointer
  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. }
  17. unit cpupara;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cpubase,cgbase,cgutils,
  23. symconst,symtype,symsym,symdef,
  24. aasmtai,aasmdata,
  25. parabase,paramgr;
  26. type
  27. tx86_64paramanager = class(tparamanager)
  28. private
  29. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  30. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  31. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  32. public
  33. function param_use_paraloc(const cgpara:tcgpara):boolean;override;
  34. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  35. function ret_in_param(def : tdef;calloption : tproccalloption) : boolean;override;
  36. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);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:tvarargsparalist):longint;override;
  42. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tlocation;override;
  43. end;
  44. implementation
  45. uses
  46. cutils,verbose,
  47. systems,
  48. defutil;
  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. paraintsupregs_winx64 : array[0..3] of tsuperregister = (RS_RCX,RS_RDX,RS_R8,RS_R9);
  53. parammsupregs_winx64 : array[0..3] of tsuperregister = (RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3);
  54. function structure_in_registers(varspez:tvarspez;size:longint):boolean;
  55. begin
  56. if (target_info.system=system_x86_64_win64) then
  57. { TODO: Temporary hack: vs_const parameters are always passed by reference for win64}
  58. result:=(varspez=vs_value) and (size in [1,2,4,8])
  59. else
  60. result:=(size<=16);
  61. end;
  62. procedure getvalueparaloc(varspez:tvarspez;p : tdef;var loc1,loc2:tcgloc);
  63. begin
  64. loc1:=LOC_INVALID;
  65. loc2:=LOC_INVALID;
  66. case p.typ of
  67. orddef:
  68. begin
  69. loc1:=LOC_REGISTER;
  70. { TODO: 128bit also needs lochigh}
  71. end;
  72. floatdef:
  73. begin
  74. case tfloatdef(p).floattype of
  75. s80real:
  76. loc1:=LOC_REFERENCE;
  77. s32real,
  78. s64real :
  79. loc1:=LOC_MMREGISTER;
  80. s64currency,
  81. s64comp :
  82. loc1:=LOC_REGISTER;
  83. s128real:
  84. begin
  85. loc1:=LOC_MMREGISTER;
  86. loc2:=LOC_MMREGISTER;
  87. { TODO: float 128bit needs SSEUP lochigh}
  88. end;
  89. end;
  90. end;
  91. recorddef:
  92. begin
  93. if structure_in_registers(varspez,p.size) then
  94. begin
  95. loc1:=LOC_REGISTER;
  96. if p.size>8 then
  97. loc2:=LOC_REGISTER;
  98. end
  99. else
  100. loc1:=LOC_REFERENCE;
  101. end;
  102. objectdef:
  103. begin
  104. if is_object(p) then
  105. begin
  106. if structure_in_registers(varspez,p.size) then
  107. loc1:=LOC_REGISTER
  108. else
  109. loc1:=LOC_REFERENCE;
  110. end
  111. else
  112. loc1:=LOC_REGISTER;
  113. end;
  114. arraydef:
  115. begin
  116. if not(is_special_array(p)) and
  117. (target_info.system=system_x86_64_win64) and
  118. structure_in_registers(varspez,p.size) then
  119. begin
  120. loc1:=LOC_REGISTER;
  121. if p.size>8 then
  122. loc2:=LOC_REGISTER;
  123. end
  124. else
  125. loc1:=LOC_REFERENCE;
  126. end;
  127. variantdef:
  128. { linux abi }
  129. if target_info.system<>system_x86_64_win64 then
  130. loc1:=LOC_REGISTER
  131. else
  132. loc1:=LOC_REFERENCE;
  133. stringdef:
  134. if is_shortstring(p) or is_longstring(p) then
  135. begin
  136. { handle long and shortstrings like arrays }
  137. if structure_in_registers(varspez,p.size) then
  138. begin
  139. loc1:=LOC_REGISTER;
  140. if p.size>8 then
  141. loc2:=LOC_REGISTER;
  142. end
  143. else
  144. loc1:=LOC_REFERENCE;
  145. end
  146. else
  147. loc1:=LOC_REGISTER;
  148. setdef:
  149. if is_smallset(p) then
  150. loc1:=LOC_REGISTER
  151. else
  152. loc1:=LOC_REFERENCE;
  153. procvardef:
  154. begin
  155. if (po_methodpointer in tprocvardef(p).procoptions) then
  156. begin
  157. { This is a record of 16 bytes }
  158. if structure_in_registers(varspez,p.size) then
  159. begin
  160. loc1:=LOC_REGISTER;
  161. loc2:=LOC_REGISTER;
  162. end
  163. else
  164. loc1:=LOC_REFERENCE;
  165. end
  166. else
  167. loc1:=LOC_REGISTER;
  168. end;
  169. else
  170. begin
  171. { default for pointers,enums,etc }
  172. loc1:=LOC_REGISTER;
  173. end;
  174. end;
  175. end;
  176. function tx86_64paramanager.ret_in_param(def : tdef;calloption : tproccalloption) : boolean;
  177. var
  178. l,loc1,loc2 : tcgloc;
  179. i : longint;
  180. begin
  181. case target_info.system of
  182. system_x86_64_win64:
  183. result:=(calloption=pocall_safecall) or
  184. (def.size>8) or not(def.size in [1,2,4,8])
  185. else
  186. { return method pointers in LOC_REGISTER like records of the same size;
  187. this is SysV only }
  188. if (def.typ=procvardef) and
  189. (po_methodpointer in tprocvardef(def).procoptions) then
  190. result:=false
  191. { handle objectdefs by the default code because they have no equivalence in C }
  192. else if (def.typ in [recorddef {,arraydef }]) and (def.size<=16) then
  193. begin
  194. case def.typ of
  195. recorddef:
  196. begin
  197. l:=LOC_MMREGISTER;
  198. for i:=0 to tabstractrecorddef(def).symtable.SymList.count-1 do
  199. begin
  200. getvalueparaloc(vs_value,tfieldvarsym(tabstractrecorddef(def).symtable.SymList[i]).vardef,loc1,loc2);
  201. case loc1 of
  202. LOC_REGISTER:
  203. if l<>LOC_REFERENCE then
  204. l:=LOC_REGISTER;
  205. LOC_MMREGISTER:
  206. ;
  207. else
  208. l:=LOC_REFERENCE;
  209. end;
  210. end;
  211. end;
  212. arraydef:
  213. begin
  214. getvalueparaloc(vs_value,tarraydef(def).elementdef,l,loc2);
  215. if not(l in [LOC_MMREGISTER,LOC_REGISTER]) then
  216. l:=LOC_REFERENCE;
  217. end;
  218. end;
  219. result:=l=LOC_REFERENCE;
  220. end
  221. else
  222. result:=inherited ret_in_param(def,calloption);
  223. end;
  224. end;
  225. function tx86_64paramanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  226. var
  227. paraloc : pcgparalocation;
  228. begin
  229. if not assigned(cgpara.location) then
  230. internalerror(200410102);
  231. result:=true;
  232. { All locations are LOC_REFERENCE }
  233. paraloc:=cgpara.location;
  234. while assigned(paraloc) do
  235. begin
  236. if (paraloc^.loc<>LOC_REFERENCE) then
  237. begin
  238. result:=false;
  239. exit;
  240. end;
  241. paraloc:=paraloc^.next;
  242. end;
  243. end;
  244. { true if a parameter is too large to copy and only the address is pushed }
  245. function tx86_64paramanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  246. begin
  247. result:=false;
  248. { var,out always require address }
  249. if varspez in [vs_var,vs_out] then
  250. begin
  251. result:=true;
  252. exit;
  253. end;
  254. { Only vs_const, vs_value here }
  255. case def.typ of
  256. variantdef,
  257. formaldef :
  258. result:=true;
  259. recorddef :
  260. begin
  261. { Win ABI depends on size to pass it in a register or not }
  262. if (target_info.system=system_x86_64_win64) then
  263. result:=not structure_in_registers(varspez,def.size)
  264. else
  265. { linux ABI always passes it as value parameter }
  266. result:=false;
  267. end;
  268. arraydef :
  269. begin
  270. { cdecl array of const need to be ignored and therefor be puhsed
  271. as value parameter with length 0 }
  272. if (calloption in [pocall_cdecl,pocall_cppdecl]) and
  273. (is_array_of_const(def) or
  274. is_dynamic_array(def)) then
  275. result:=false
  276. else
  277. result:=true;
  278. end;
  279. objectdef :
  280. begin
  281. if is_object(def) then
  282. result:=not structure_in_registers(varspez,def.size);
  283. end;
  284. stringdef :
  285. begin
  286. if (tstringdef(def).stringtype in [st_shortstring,st_longstring]) then
  287. result:=not structure_in_registers(varspez,def.size);
  288. end;
  289. procvardef :
  290. begin
  291. if (po_methodpointer in tprocvardef(def).procoptions) then
  292. result:=not structure_in_registers(varspez,def.size);
  293. end;
  294. setdef :
  295. result:=not is_smallset(def);
  296. end;
  297. end;
  298. function tx86_64paramanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  299. begin
  300. if target_info.system=system_x86_64_win64 then
  301. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  302. else
  303. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  304. end;
  305. function tx86_64paramanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  306. begin
  307. if target_info.system=system_x86_64_win64 then
  308. result:=[RS_XMM0..RS_XMM5]
  309. else
  310. result:=[RS_XMM0..RS_XMM15];
  311. end;
  312. function tx86_64paramanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  313. begin
  314. result:=[RS_ST0..RS_ST7];
  315. end;
  316. procedure tx86_64paramanager.getintparaloc(calloption : tproccalloption; nr : longint;var cgpara:TCGPara);
  317. var
  318. paraloc : pcgparalocation;
  319. begin
  320. cgpara.reset;
  321. cgpara.size:=OS_ADDR;
  322. cgpara.intsize:=sizeof(pint);
  323. cgpara.alignment:=get_para_align(calloption);
  324. paraloc:=cgpara.add_location;
  325. with paraloc^ do
  326. begin
  327. size:=OS_INT;
  328. if target_info.system=system_x86_64_win64 then
  329. begin
  330. if nr<1 then
  331. internalerror(200304303)
  332. else if nr<=high(paraintsupregs_winx64)+1 then
  333. begin
  334. loc:=LOC_REGISTER;
  335. register:=newreg(R_INTREGISTER,paraintsupregs_winx64[nr-1],R_SUBWHOLE);
  336. end
  337. else
  338. begin
  339. loc:=LOC_REFERENCE;
  340. reference.index:=NR_STACK_POINTER_REG;
  341. reference.offset:=(nr-6)*sizeof(aint);
  342. end;
  343. end
  344. else
  345. begin
  346. if nr<1 then
  347. internalerror(200304303)
  348. else if nr<=high(paraintsupregs)+1 then
  349. begin
  350. loc:=LOC_REGISTER;
  351. register:=newreg(R_INTREGISTER,paraintsupregs[nr-1],R_SUBWHOLE);
  352. end
  353. else
  354. begin
  355. loc:=LOC_REFERENCE;
  356. reference.index:=NR_STACK_POINTER_REG;
  357. reference.offset:=(nr-6)*sizeof(aint);
  358. end;
  359. end;
  360. end;
  361. end;
  362. procedure tx86_64paramanager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  363. begin
  364. p.funcretloc[side]:=get_funcretloc(p,side,p.returndef);
  365. end;
  366. function tx86_64paramanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tlocation;
  367. var
  368. retcgsize : tcgsize;
  369. begin
  370. { Constructors return self instead of a boolean }
  371. if (p.proctypeoption=potype_constructor) then
  372. retcgsize:=OS_ADDR
  373. else
  374. retcgsize:=def_cgsize(def);
  375. location_reset(result,LOC_INVALID,OS_NO);
  376. { void has no location }
  377. if is_void(def) then
  378. begin
  379. location_reset(result,LOC_VOID,OS_NO);
  380. exit;
  381. end;
  382. { Return is passed as var parameter }
  383. if ret_in_param(def,p.proccalloption) then
  384. begin
  385. result.loc:=LOC_REFERENCE;
  386. result.size:=retcgsize;
  387. exit;
  388. end;
  389. { Return in FPU register? }
  390. if def.typ=floatdef then
  391. begin
  392. case tfloatdef(def).floattype of
  393. s32real,s64real:
  394. begin
  395. result.loc:=LOC_MMREGISTER;
  396. result.register:=NR_MM_RESULT_REG;
  397. result.size:=retcgsize;
  398. end;
  399. s64currency,
  400. s64comp,
  401. s80real:
  402. begin
  403. result.loc:=LOC_FPUREGISTER;
  404. result.register:=NR_FPU_RESULT_REG;
  405. result.size:=retcgsize;
  406. end;
  407. else
  408. internalerror(200405034);
  409. end;
  410. end
  411. else
  412. { Return in register }
  413. begin
  414. result.loc:=LOC_REGISTER;
  415. if retcgsize=OS_NO then
  416. begin
  417. case def.size of
  418. 0..4:
  419. begin
  420. result.size:=OS_32;
  421. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,R_SUBD);
  422. end;
  423. 5..8:
  424. begin
  425. result.size:=OS_64;
  426. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,R_SUBQ);
  427. end;
  428. 9..16:
  429. begin
  430. result.size:=OS_128;
  431. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,R_SUBWHOLE);
  432. result.registerhi:=newreg(R_INTREGISTER,RS_RDX,R_SUBWHOLE);
  433. end;
  434. end;
  435. end
  436. else if retcgsize in [OS_128,OS_S128] then
  437. begin
  438. result.size:=retcgsize;
  439. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,R_SUBWHOLE);
  440. result.registerhi:=newreg(R_INTREGISTER,RS_RDX,R_SUBWHOLE);
  441. end
  442. else
  443. begin
  444. result.size:=retcgsize;
  445. if side=callerside then
  446. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  447. else
  448. result.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  449. end;
  450. end;
  451. end;
  452. procedure tx86_64paramanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  453. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  454. var
  455. hp : tparavarsym;
  456. paraloc : pcgparalocation;
  457. subreg : tsubregister;
  458. pushaddr : boolean;
  459. paracgsize : tcgsize;
  460. loc : array[1..2] of tcgloc;
  461. paralen,
  462. locidx,
  463. i,
  464. varalign,
  465. paraalign : longint;
  466. begin
  467. paraalign:=get_para_align(p.proccalloption);
  468. { Register parameters are assigned from left to right }
  469. for i:=0 to paras.count-1 do
  470. begin
  471. hp:=tparavarsym(paras[i]);
  472. pushaddr:=push_addr_param(hp.varspez,hp.vardef,p.proccalloption);
  473. if pushaddr then
  474. begin
  475. loc[1]:=LOC_REGISTER;
  476. loc[2]:=LOC_INVALID;
  477. paracgsize:=OS_ADDR;
  478. paralen:=sizeof(aint);
  479. end
  480. else
  481. begin
  482. getvalueparaloc(hp.varspez,hp.vardef,loc[1],loc[2]);
  483. paralen:=push_size(hp.varspez,hp.vardef,p.proccalloption);
  484. paracgsize:=def_cgsize(hp.vardef);
  485. end;
  486. { cheat for now, we should copy the value to an mm reg as well (FK) }
  487. if varargsparas and
  488. (target_info.system = system_x86_64_win64) and
  489. (hp.vardef.typ = floatdef) then
  490. begin
  491. loc[1] := LOC_REGISTER;
  492. loc[2] := LOC_INVALID;
  493. if paracgsize = OS_F64 then
  494. paracgsize := OS_64
  495. else
  496. paracgsize := OS_32;
  497. end;
  498. hp.paraloc[side].reset;
  499. hp.paraloc[side].size:=paracgsize;
  500. hp.paraloc[side].intsize:=paralen;
  501. hp.paraloc[side].Alignment:=paraalign;
  502. if paralen>0 then
  503. begin
  504. locidx:=1;
  505. while (paralen>0) do
  506. begin
  507. if locidx>2 then
  508. internalerror(200501283);
  509. { Enough registers free? }
  510. case loc[locidx] of
  511. LOC_REGISTER :
  512. begin
  513. { winx64 uses different registers }
  514. if ((target_info.system=system_x86_64_win64) and
  515. (intparareg>high(paraintsupregs_winx64))) or
  516. ((target_info.system<>system_x86_64_win64) and
  517. (intparareg>high(paraintsupregs))) then
  518. loc[locidx]:=LOC_REFERENCE;
  519. end;
  520. LOC_MMREGISTER :
  521. begin
  522. { winx64 uses different registers }
  523. if ((target_info.system=system_x86_64_win64) and
  524. (mmparareg>high(parammsupregs_winx64))) or
  525. ((target_info.system<>system_x86_64_win64) and
  526. (mmparareg>high(parammsupregs))) then
  527. loc[locidx]:=LOC_REFERENCE;
  528. end;
  529. end;
  530. { Allocate }
  531. case loc[locidx] of
  532. LOC_REGISTER :
  533. begin
  534. paraloc:=hp.paraloc[side].add_location;
  535. paraloc^.loc:=LOC_REGISTER;
  536. if (paracgsize=OS_NO) or (loc[2]<>LOC_INVALID) then
  537. begin
  538. paraloc^.size:=OS_INT;
  539. subreg:=R_SUBWHOLE;
  540. end
  541. else
  542. begin
  543. paraloc^.size:=paracgsize;
  544. { s64comp is pushed in an int register }
  545. if paraloc^.size=OS_C64 then
  546. paraloc^.size:=OS_64;
  547. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  548. end;
  549. { winx64 uses different registers }
  550. if target_info.system=system_x86_64_win64 then
  551. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  552. else
  553. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  554. { matching mm register must be skipped }
  555. if target_info.system=system_x86_64_win64 then
  556. inc(mmparareg);
  557. inc(intparareg);
  558. dec(paralen,tcgsize2size[paraloc^.size]);
  559. end;
  560. LOC_MMREGISTER :
  561. begin
  562. paraloc:=hp.paraloc[side].add_location;
  563. paraloc^.loc:=LOC_MMREGISTER;
  564. case paracgsize of
  565. OS_F32:
  566. subreg:=R_SUBMMS;
  567. OS_F64:
  568. subreg:=R_SUBMMD;
  569. else
  570. subreg:=R_SUBMMWHOLE;
  571. end;
  572. { winx64 uses different registers }
  573. if target_info.system=system_x86_64_win64 then
  574. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg)
  575. else
  576. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  577. if paracgsize=OS_F128 then
  578. paraloc^.size:=OS_F64
  579. else
  580. paraloc^.size:=paracgsize;
  581. { matching int register must be skipped }
  582. if target_info.system=system_x86_64_win64 then
  583. inc(intparareg);
  584. inc(mmparareg);
  585. dec(paralen,tcgsize2size[paraloc^.size]);
  586. end;
  587. LOC_REFERENCE :
  588. begin
  589. paraloc:=hp.paraloc[side].add_location;
  590. paraloc^.loc:=LOC_REFERENCE;
  591. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  592. however, since int_cgsize is called in many places in the
  593. compiler where only a few can already handle OS_128, fixing it
  594. properly is out of the question to release 2.2.0 in time. (DM)}
  595. if paracgsize=OS_128 then
  596. if paralen=8 then
  597. paraloc^.size:=OS_64
  598. else if paralen=16 then
  599. paraloc^.size:=OS_128
  600. else
  601. internalerror(200707143)
  602. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  603. paraloc^.size:=int_float_cgsize(paralen)
  604. else
  605. paraloc^.size:=int_cgsize(paralen);
  606. if side=callerside then
  607. paraloc^.reference.index:=NR_STACK_POINTER_REG
  608. else
  609. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  610. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  611. paraloc^.reference.offset:=parasize;
  612. parasize:=align(parasize+paralen,varalign);
  613. paralen:=0;
  614. end;
  615. end;
  616. if (locidx<2) and
  617. (loc[locidx+1]<>LOC_INVALID) then
  618. inc(locidx);
  619. end;
  620. end
  621. else
  622. begin
  623. paraloc:=hp.paraloc[side].add_location;
  624. paraloc^.loc:=LOC_VOID;
  625. end;
  626. end;
  627. { Register parameters are assigned from left-to-right, but the
  628. offsets on the stack are right-to-left. There is no need
  629. to reverse the offset, only adapt the calleeside with the
  630. start offset of the first param on the stack }
  631. if side=calleeside then
  632. begin
  633. for i:=0 to paras.count-1 do
  634. begin
  635. hp:=tparavarsym(paras[i]);
  636. paraloc:=hp.paraloc[side].location;
  637. while paraloc<>nil do
  638. begin
  639. with paraloc^ do
  640. if (loc=LOC_REFERENCE) then
  641. inc(reference.offset,target_info.first_parm_offset);
  642. paraloc:=paraloc^.next;
  643. end;
  644. end;
  645. end;
  646. end;
  647. function tx86_64paramanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  648. var
  649. intparareg,mmparareg,
  650. parasize : longint;
  651. begin
  652. intparareg:=0;
  653. mmparareg:=0;
  654. if target_info.system=system_x86_64_win64 then
  655. parasize:=4*8
  656. else
  657. parasize:=0;
  658. { calculate the registers for the normal parameters }
  659. create_paraloc_info_intern(p,callerside,p.paras,intparareg,mmparareg,parasize,false);
  660. { append the varargs }
  661. create_paraloc_info_intern(p,callerside,varargspara,intparareg,mmparareg,parasize,true);
  662. { store used no. of SSE registers, that needs to be passed in %AL }
  663. varargspara.mmregsused:=mmparareg;
  664. result:=parasize;
  665. end;
  666. function tx86_64paramanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  667. var
  668. intparareg,mmparareg,
  669. parasize : longint;
  670. begin
  671. intparareg:=0;
  672. mmparareg:=0;
  673. if target_info.system=system_x86_64_win64 then
  674. parasize:=4*8
  675. else
  676. parasize:=0;
  677. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  678. { Create Function result paraloc }
  679. create_funcretloc_info(p,side);
  680. { We need to return the size allocated on the stack }
  681. result:=parasize;
  682. end;
  683. begin
  684. paramanager:=tx86_64paramanager.create;
  685. end.