cpupara.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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 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. }
  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_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  30. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  31. public
  32. function param_use_paraloc(const cgpara:tcgpara):boolean;override;
  33. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  34. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  35. procedure getintparaloc(pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);override;
  36. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;override;
  37. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;override;
  38. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;override;
  39. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  40. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  41. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  42. end;
  43. implementation
  44. uses
  45. cutils,verbose,
  46. systems,
  47. defutil,
  48. symtable;
  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. {
  55. The argument classification code largely comes from libffi:
  56. ffi64.c - Copyright (c) 2002, 2007 Bo Thorsen <[email protected]>
  57. Copyright (c) 2008 Red Hat, Inc.
  58. x86-64 Foreign Function Interface
  59. Permission is hereby granted, free of charge, to any person obtaining
  60. a copy of this software and associated documentation files (the
  61. ``Software''), to deal in the Software without restriction, including
  62. without limitation the rights to use, copy, modify, merge, publish,
  63. distribute, sublicense, and/or sell copies of the Software, and to
  64. permit persons to whom the Software is furnished to do so, subject to
  65. the following conditions:
  66. The above copyright notice and this permission notice shall be included
  67. in all copies or substantial portions of the Software.
  68. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  69. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  70. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  71. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  72. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  73. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  74. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  75. DEALINGS IN THE SOFTWARE.
  76. ----------------------------------------------------------------------- *)
  77. }
  78. const
  79. MAX_PARA_CLASSES = 4;
  80. type
  81. tx64paraclasstype = (
  82. X86_64_NO_CLASS,
  83. X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS,
  84. X86_64_SSE_CLASS,X86_64_SSESF_CLASS,X86_64_SSEDF_CLASS,X86_64_SSEUP_CLASS,
  85. X86_64_X87_CLASS,X86_64_X87UP_CLASS,
  86. X86_64_COMPLEX_X87_CLASS,
  87. X86_64_MEMORY_CLASS
  88. );
  89. tx64paraclass = record
  90. def: tdef;
  91. typ: tx64paraclasstype;
  92. end;
  93. tx64paraclasses = array[0..MAX_PARA_CLASSES-1] of tx64paraclass;
  94. { Win64-specific helper }
  95. function aggregate_in_registers_win64(varspez:tvarspez;size:longint):boolean;
  96. begin
  97. { TODO: Temporary hack: vs_const parameters are always passed by reference for win64}
  98. result:=(varspez=vs_value) and (size in [1,2,4,8])
  99. end;
  100. (* x86-64 register passing implementation. See x86-64 ABI for details. Goal
  101. of this code is to classify each 8bytes of incoming argument by the register
  102. class and assign registers accordingly. *)
  103. function classify_representative_def(def1, def2: tdef): tdef;
  104. var
  105. def1size, def2size: asizeint;
  106. begin
  107. if not assigned(def1) then
  108. result:=def2
  109. else if not assigned(def2) then
  110. result:=def1
  111. else
  112. begin
  113. def1size:=def1.size;
  114. def2size:=def2.size;
  115. if def1size>def2size then
  116. result:=def1
  117. else if def2size>def1size then
  118. result:=def2
  119. else if def1.alignment>def2.alignment then
  120. result:=def1
  121. else
  122. result:=def2;
  123. end;
  124. end;
  125. (* Classify the argument of type TYPE and mode MODE.
  126. CLASSES will be filled by the register class used to pass each word
  127. of the operand. The number of words is returned. In case the parameter
  128. should be passed in memory, 0 is returned. As a special case for zero
  129. sized containers, classes[0] will be NO_CLASS and 1 is returned.
  130. real_size contains either def.size, or a value derived from
  131. def.bitpackedsize and the field offset denoting the number of bytes
  132. spanned by a bitpacked field
  133. See the x86-64 PS ABI for details.
  134. *)
  135. procedure classify_single_integer_class(def: tdef; size,real_size: aint; var cl: tx64paraclass; byte_offset: aint);
  136. begin
  137. if (byte_offset=0) and
  138. (real_size in [1,2,4,8]) and
  139. (not assigned(cl.def) or
  140. (def.alignment>=cl.def.alignment)) then
  141. cl.def:=def;
  142. if size<=4 then
  143. begin
  144. cl.typ:=X86_64_INTEGERSI_CLASS;
  145. if not assigned(cl.def) or
  146. (cl.def.size<size) then
  147. begin
  148. case size of
  149. 1: cl.def:=u8inttype;
  150. 2: cl.def:=u16inttype;
  151. 3,4: cl.def:=u32inttype;
  152. end;
  153. end;
  154. end
  155. else
  156. begin
  157. cl.typ:=X86_64_INTEGER_CLASS;
  158. if not assigned(cl.def) or
  159. (cl.def.size<size) then
  160. cl.def:=u64inttype;
  161. end;
  162. end;
  163. function classify_as_integer_argument(def: tdef; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint;
  164. var
  165. size: aint;
  166. begin
  167. size:=byte_offset+real_size;
  168. classify_single_integer_class(def,size,real_size,classes[0],byte_offset);
  169. if size<=8 then
  170. result:=1
  171. else
  172. begin
  173. classify_single_integer_class(def,size-8,real_size,classes[1],byte_offset-8);
  174. if size>16 then
  175. internalerror(2010021401);
  176. result:=2;
  177. end
  178. end;
  179. (* Return the union class of CLASS1 and CLASS2.
  180. See the x86-64 PS ABI for details. *)
  181. function merge_classes(class1, class2: tx64paraclass): tx64paraclass;
  182. begin
  183. (* Rule #1: If both classes are equal, this is the resulting class. *)
  184. if (class1.typ=class2.typ) then
  185. begin
  186. result.typ:=class1.typ;
  187. result.def:=classify_representative_def(class1.def,class2.def);
  188. exit;
  189. end;
  190. (* Rule #2: If one of the classes is NO_CLASS, the resulting class is
  191. the other class. *)
  192. if (class1.typ=X86_64_NO_CLASS) then
  193. exit(class2);
  194. if (class2.typ=X86_64_NO_CLASS) then
  195. exit(class1);
  196. (* Rule #3: If one of the classes is MEMORY, the result is MEMORY. *)
  197. if (class1.typ=X86_64_MEMORY_CLASS) then
  198. exit(class1)
  199. else if (class2.typ=X86_64_MEMORY_CLASS) then
  200. exit(class2);
  201. (* Rule #4: If one of the classes is INTEGER, the result is INTEGER. *)
  202. { 32 bit }
  203. if ((class1.typ=X86_64_INTEGERSI_CLASS) and
  204. (class2.typ=X86_64_SSESF_CLASS)) then
  205. exit(class1)
  206. else if ((class2.typ=X86_64_INTEGERSI_CLASS) and
  207. (class1.typ=X86_64_SSESF_CLASS)) then
  208. exit(class2);
  209. { 64 bit }
  210. if (class1.typ in [X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS]) then
  211. begin
  212. result:=class1;
  213. if result.def.size<8 then
  214. begin
  215. result.typ:=X86_64_INTEGER_CLASS;
  216. result.def:=s64inttype;
  217. end;
  218. exit
  219. end
  220. else if (class2.typ in [X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS]) then
  221. begin
  222. result:=class2;
  223. if result.def.size<8 then
  224. begin
  225. result.typ:=X86_64_INTEGER_CLASS;
  226. result.def:=s64inttype;
  227. end;
  228. exit
  229. end;
  230. (* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
  231. MEMORY is used. *)
  232. if (class1.typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS]) then
  233. begin
  234. result:=class1;
  235. result.typ:=X86_64_MEMORY_CLASS;
  236. exit;
  237. end
  238. else if (class2.typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS]) then
  239. begin
  240. result:=class2;
  241. result.typ:=X86_64_MEMORY_CLASS;
  242. exit;
  243. end;
  244. (* Rule #6: Otherwise class SSE is used. *)
  245. if class1.def.size>class2.def.size then
  246. result:=class1
  247. else
  248. result:=class2;
  249. result.typ:=X86_64_SSE_CLASS;
  250. result.def:=getarraydef(s32floattype,2)
  251. end;
  252. function classify_argument(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint; forward;
  253. function init_aggregate_classification(def: tdef; varspez: tvarspez; byte_offset: aint; out words: longint; out classes: tx64paraclasses): longint;
  254. var
  255. i: longint;
  256. begin
  257. words:=0;
  258. { win64 follows a different convention here }
  259. if (target_info.system=system_x86_64_win64) then
  260. begin
  261. if aggregate_in_registers_win64(varspez,def.size) then
  262. begin
  263. classes[0].typ:=X86_64_INTEGER_CLASS;
  264. classes[0].def:=def;
  265. result:=1;
  266. end
  267. else
  268. result:=0;
  269. exit;
  270. end;
  271. (* If the struct is larger than 32 bytes, pass it on the stack. *)
  272. if def.size > 32 then
  273. exit(0);
  274. { if a struct starts an offset not divisible by 8, it can span extra
  275. words }
  276. words:=(def.size+byte_offset mod 8+7) div 8;
  277. (* Zero sized arrays or structures are NO_CLASS. We return 0 to
  278. signal memory class, so handle it as special case. *)
  279. if (words=0) then
  280. begin
  281. classes[0].typ:=X86_64_NO_CLASS;
  282. classes[0].def:=def;
  283. exit(1);
  284. end;
  285. { we'll be merging the classes elements with the subclasses
  286. elements, so initialise them first }
  287. for i:=low(classes) to high(classes) do
  288. begin
  289. classes[i].typ:=X86_64_NO_CLASS;
  290. classes[i].def:=nil;
  291. end;
  292. result:=words;
  293. end;
  294. function classify_aggregate_element(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; new_byte_offset: aint): longint;
  295. var
  296. subclasses: tx64paraclasses;
  297. i,
  298. pos: longint;
  299. begin
  300. fillchar(subclasses,sizeof(subclasses),0);
  301. result:=classify_argument(def,varspez,real_size,subclasses,new_byte_offset mod 8);
  302. if (result=0) then
  303. exit;
  304. pos:=new_byte_offset div 8;
  305. if result-1+pos>high(classes) then
  306. internalerror(2010053108);
  307. for i:=0 to result-1 do
  308. begin
  309. classes[i+pos] :=
  310. merge_classes(subclasses[i],classes[i+pos]);
  311. end;
  312. inc(result,pos);
  313. end;
  314. function finalize_aggregate_classification(def: tdef; words: longint; var classes: tx64paraclasses): longint;
  315. var
  316. i: longint;
  317. begin
  318. if (words>2) then
  319. begin
  320. (* When size > 16 bytes, if the first one isn't
  321. X86_64_SSE_CLASS or any other ones aren't
  322. X86_64_SSEUP_CLASS, everything should be passed in
  323. memory. *)
  324. if (classes[0].typ<>X86_64_SSE_CLASS) then
  325. exit(0);
  326. for i:=1 to words-1 do
  327. if (classes[i].typ<>X86_64_SSEUP_CLASS) then
  328. exit(0);
  329. end;
  330. (* Final merger cleanup. *)
  331. (* The first one must never be X86_64_SSEUP_CLASS or
  332. X86_64_X87UP_CLASS. *)
  333. if (classes[0].typ=X86_64_SSEUP_CLASS) or
  334. (classes[0].typ=X86_64_X87UP_CLASS) then
  335. internalerror(2010021402);
  336. for i:=0 to words-1 do
  337. begin
  338. (* If one class is MEMORY, everything should be passed in
  339. memory. *)
  340. if (classes[i].typ=X86_64_MEMORY_CLASS) then
  341. exit(0);
  342. (* The X86_64_SSEUP_CLASS should be always preceded by
  343. X86_64_SSE_CLASS or X86_64_SSEUP_CLASS. *)
  344. if (classes[i].typ=X86_64_SSEUP_CLASS) and
  345. (classes[i-1].typ<>X86_64_SSE_CLASS) and
  346. (classes[i-1].typ<>X86_64_SSEUP_CLASS) then
  347. begin
  348. classes[i].typ:=X86_64_SSE_CLASS;
  349. classes[i].def:=getarraydef(s32floattype,2);
  350. end;
  351. (* If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
  352. everything should be passed in memory. *)
  353. if (classes[i].typ=X86_64_X87UP_CLASS) and
  354. (classes[i-1].typ<>X86_64_X87_CLASS) then
  355. exit(0);
  356. end;
  357. { FIXME: in case a record contains empty padding space, e.g. a
  358. "single" field followed by a "double", then we have a problem
  359. because the cgpara helpers cannot figure out that they should
  360. skip 4 bytes after storing the single (LOC_MMREGISTER with size
  361. OS_F32) to memory before storing the double -> for now scale
  362. such locations always up to 64 bits, although this loads/stores
  363. some superfluous data }
  364. { 1) the first part is 32 bit while there is still a second part }
  365. if (classes[1].typ<>X86_64_NO_CLASS) then
  366. case classes[0].typ of
  367. X86_64_INTEGERSI_CLASS:
  368. begin
  369. classes[0].typ:=X86_64_INTEGER_CLASS;
  370. classes[0].def:=s64inttype;
  371. end;
  372. X86_64_SSESF_CLASS:
  373. begin
  374. classes[0].typ:=X86_64_SSE_CLASS;
  375. classes[0].def:=getarraydef(s32floattype,2);
  376. end;
  377. end;
  378. { 2) the second part is 32 bit, but the total size is > 12 bytes }
  379. if (def.size>12) then
  380. case classes[1].typ of
  381. X86_64_INTEGERSI_CLASS:
  382. begin
  383. classes[1].typ:=X86_64_INTEGER_CLASS;
  384. classes[1].def:=s64inttype;
  385. end;
  386. X86_64_SSESF_CLASS:
  387. begin
  388. classes[1].typ:=X86_64_SSE_CLASS;
  389. classes[1].def:=getarraydef(s32floattype,2);
  390. end;
  391. end;
  392. result:=words;
  393. end;
  394. function classify_record(def: tdef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  395. var
  396. vs: tfieldvarsym;
  397. size,
  398. new_byte_offset: aint;
  399. i,
  400. words,
  401. num: longint;
  402. checkalignment: boolean;
  403. begin
  404. result:=init_aggregate_classification(def,varspez,byte_offset,words,classes);
  405. if (words=0) then
  406. exit;
  407. (* Merge the fields of the structure. *)
  408. for i:=0 to tabstractrecorddef(def).symtable.symlist.count-1 do
  409. begin
  410. if tsym(tabstractrecorddef(def).symtable.symlist[i]).typ<>fieldvarsym then
  411. continue;
  412. vs:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[i]);
  413. num:=-1;
  414. checkalignment:=true;
  415. if not tabstractrecordsymtable(tabstractrecorddef(def).symtable).is_packed then
  416. begin
  417. new_byte_offset:=byte_offset+vs.fieldoffset;
  418. size:=vs.vardef.size;
  419. end
  420. else
  421. begin
  422. new_byte_offset:=byte_offset+vs.fieldoffset div 8;
  423. if (vs.vardef.typ in [orddef,enumdef]) then
  424. begin
  425. { calculate the number of bytes spanned by
  426. this bitpacked field }
  427. size:=((vs.fieldoffset+vs.vardef.packedbitsize+7) div 8)-(vs.fieldoffset div 8);
  428. { our bitpacked fields are interpreted as always being
  429. aligned, because unlike in C we don't have char:1, int:1
  430. etc (so everything is basically a char:x) }
  431. checkalignment:=false;
  432. end
  433. else
  434. size:=vs.vardef.size;
  435. end;
  436. { If [..] an object [..] contains unaligned fields, it has class
  437. MEMORY }
  438. if checkalignment and
  439. (align(new_byte_offset,vs.vardef.structalignment)<>new_byte_offset) then
  440. begin
  441. result:=0;
  442. exit;
  443. end;
  444. num:=classify_aggregate_element(vs.vardef,varspez,size,classes,new_byte_offset);
  445. if (num=0) then
  446. exit(0);
  447. end;
  448. result:=finalize_aggregate_classification(def,words,classes);
  449. end;
  450. function classify_normal_array(def: tarraydef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  451. var
  452. i, elecount: aword;
  453. size,
  454. elesize,
  455. new_byte_offset,
  456. bitoffset: aint;
  457. words,
  458. num: longint;
  459. isbitpacked: boolean;
  460. begin
  461. size:=0;
  462. bitoffset:=0;
  463. result:=init_aggregate_classification(def,varspez,byte_offset,words,classes);
  464. if (words=0) then
  465. exit;
  466. isbitpacked:=is_packed_array(def);
  467. if not isbitpacked then
  468. begin
  469. elesize:=def.elesize;
  470. size:=elesize;
  471. end
  472. else
  473. begin
  474. elesize:=def.elepackedbitsize;
  475. bitoffset:=0;
  476. end;
  477. (* Merge the elements of the array. *)
  478. i:=0;
  479. elecount:=def.elecount;
  480. repeat
  481. if not isbitpacked then
  482. begin
  483. { size does not change }
  484. new_byte_offset:=byte_offset+i*elesize;
  485. { If [..] an object [..] contains unaligned fields, it has class
  486. MEMORY }
  487. if align(new_byte_offset,def.alignment)<>new_byte_offset then
  488. begin
  489. result:=0;
  490. exit;
  491. end;
  492. end
  493. else
  494. begin
  495. { calculate the number of bytes spanned by this bitpacked
  496. element }
  497. size:=((bitoffset+elesize+7) div 8)-(bitoffset div 8);
  498. new_byte_offset:=byte_offset+(elesize*i) div 8;
  499. { bit offset of next element }
  500. inc(bitoffset,elesize);
  501. end;
  502. num:=classify_aggregate_element(def.elementdef,varspez,size,classes,new_byte_offset);
  503. if (num=0) then
  504. exit(0);
  505. inc(i);
  506. until (i=elecount);
  507. result:=finalize_aggregate_classification(def,words,classes);
  508. end;
  509. function classify_argument(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint;
  510. begin
  511. case def.typ of
  512. orddef,
  513. enumdef,
  514. pointerdef,
  515. classrefdef:
  516. result:=classify_as_integer_argument(def,real_size,classes,byte_offset);
  517. formaldef:
  518. result:=classify_as_integer_argument(voidpointertype,voidpointertype.size,classes,byte_offset);
  519. floatdef:
  520. begin
  521. classes[0].def:=def;
  522. case tfloatdef(def).floattype of
  523. s32real:
  524. begin
  525. if byte_offset=0 then
  526. classes[0].typ:=X86_64_SSESF_CLASS
  527. else
  528. begin
  529. { if we have e.g. a record with two successive "single"
  530. fields, we need a 64 bit rather than a 32 bit load }
  531. classes[0].typ:=X86_64_SSE_CLASS;
  532. classes[0].def:=getarraydef(s32floattype,2);
  533. end;
  534. result:=1;
  535. end;
  536. s64real:
  537. begin
  538. classes[0].typ:=X86_64_SSEDF_CLASS;
  539. result:=1;
  540. end;
  541. s80real,
  542. sc80real:
  543. begin
  544. classes[0].typ:=X86_64_X87_CLASS;
  545. classes[1].typ:=X86_64_X87UP_CLASS;
  546. classes[1].def:=def;
  547. result:=2;
  548. end;
  549. s64comp,
  550. s64currency:
  551. begin
  552. classes[0].typ:=X86_64_INTEGER_CLASS;
  553. result:=1;
  554. end;
  555. s128real:
  556. begin
  557. classes[0].typ:=X86_64_SSE_CLASS;
  558. classes[0].def:=getarraydef(s32floattype,2);
  559. classes[1].typ:=X86_64_SSEUP_CLASS;
  560. classes[1].def:=getarraydef(s32floattype,2);
  561. result:=2;
  562. end;
  563. else
  564. internalerror(2010060301);
  565. end;
  566. end;
  567. recorddef:
  568. result:=classify_record(def,varspez,classes,byte_offset);
  569. objectdef:
  570. begin
  571. if is_object(def) then
  572. { pass by reference, like ppc and i386 }
  573. result:=0
  574. else
  575. { all kinds of pointer types: class, objcclass, interface, ... }
  576. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset);
  577. end;
  578. setdef:
  579. begin
  580. if is_smallset(def) then
  581. result:=classify_as_integer_argument(def,def.size,classes,byte_offset)
  582. else
  583. result:=0;
  584. end;
  585. stringdef:
  586. begin
  587. if (tstringdef(def).stringtype in [st_shortstring,st_longstring]) then
  588. result:=0
  589. else
  590. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  591. end;
  592. arraydef:
  593. begin
  594. { a dynamic array is treated like a pointer }
  595. if is_dynamic_array(def) then
  596. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset)
  597. { other special arrays are passed on the stack }
  598. else if is_open_array(def) or
  599. is_array_of_const(def) then
  600. result:=0
  601. else
  602. { normal array }
  603. result:=classify_normal_array(tarraydef(def),varspez,classes,byte_offset);
  604. end;
  605. { the file record is definitely too big }
  606. filedef:
  607. result:=0;
  608. procvardef:
  609. begin
  610. if (po_methodpointer in tprocvardef(def).procoptions) then
  611. begin
  612. { treat as TMethod record }
  613. def:=search_system_type('TMETHOD').typedef;
  614. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  615. end
  616. else
  617. { pointer }
  618. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  619. end;
  620. variantdef:
  621. begin
  622. { same as tvardata record }
  623. def:=search_system_type('TVARDATA').typedef;
  624. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  625. end;
  626. else
  627. internalerror(2010021405);
  628. end;
  629. end;
  630. procedure getvalueparaloc(varspez:tvarspez;def:tdef;var loc1,loc2:tx64paraclass);
  631. var
  632. size: aint;
  633. i: longint;
  634. classes: tx64paraclasses;
  635. numclasses: longint;
  636. begin
  637. { init the classes array, because even if classify_argument inits only
  638. one element we copy both to loc1/loc2 in case "1" is returned }
  639. for i:=low(classes) to high(classes) do
  640. begin
  641. classes[i].typ:=X86_64_NO_CLASS;
  642. classes[i].def:=nil;
  643. end;
  644. { def.size internalerrors for open arrays and dynamic arrays, since
  645. their size cannot be determined at compile-time.
  646. classify_argument does not look at the realsize argument for arrays
  647. cases, but we obviously do have to pass something... }
  648. if is_special_array(def) then
  649. size:=-1
  650. else
  651. size:=def.size;
  652. numclasses:=classify_argument(def,varspez,size,classes,0);
  653. case numclasses of
  654. 0:
  655. begin
  656. loc1.typ:=X86_64_MEMORY_CLASS;
  657. loc1.def:=def;
  658. loc2.typ:=X86_64_NO_CLASS;
  659. end;
  660. 1,2:
  661. begin
  662. { If the class is X87, X87UP or COMPLEX_X87, it is passed in memory }
  663. if classes[0].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  664. classes[0].typ:=X86_64_MEMORY_CLASS;
  665. if classes[1].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  666. classes[1].typ:=X86_64_MEMORY_CLASS;
  667. loc1:=classes[0];
  668. loc2:=classes[1];
  669. end
  670. else
  671. { 4 can only happen for _m256 vectors, not yet supported }
  672. internalerror(2010021501);
  673. end;
  674. end;
  675. function tx86_64paramanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  676. var
  677. classes: tx64paraclasses;
  678. numclasses: longint;
  679. begin
  680. if handle_common_ret_in_param(def,pd,result) then
  681. exit;
  682. fillchar(classes,sizeof(classes),0);
  683. case def.typ of
  684. { for records it depends on their contents and size }
  685. recorddef,
  686. { make sure we handle 'procedure of object' correctly }
  687. procvardef:
  688. begin
  689. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  690. result:=(numclasses=0);
  691. end;
  692. else
  693. result:=inherited ret_in_param(def,pd);
  694. end;
  695. end;
  696. function tx86_64paramanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  697. var
  698. paraloc : pcgparalocation;
  699. begin
  700. if not assigned(cgpara.location) then
  701. internalerror(200410102);
  702. result:=true;
  703. { All locations are LOC_REFERENCE }
  704. paraloc:=cgpara.location;
  705. while assigned(paraloc) do
  706. begin
  707. if (paraloc^.loc<>LOC_REFERENCE) then
  708. begin
  709. result:=false;
  710. exit;
  711. end;
  712. paraloc:=paraloc^.next;
  713. end;
  714. end;
  715. { true if a parameter is too large to copy and only the address is pushed }
  716. function tx86_64paramanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  717. var
  718. classes: tx64paraclasses;
  719. numclasses: longint;
  720. begin
  721. fillchar(classes,sizeof(classes),0);
  722. result:=false;
  723. { var,out,constref always require address }
  724. if varspez in [vs_var,vs_out,vs_constref] then
  725. begin
  726. result:=true;
  727. exit;
  728. end;
  729. { Only vs_const, vs_value here }
  730. case def.typ of
  731. formaldef :
  732. result:=true;
  733. recorddef :
  734. begin
  735. { MetroWerks Pascal: const records always passed by reference
  736. (for Mac OS X interfaces) }
  737. if (calloption=pocall_mwpascal) and
  738. (varspez=vs_const) then
  739. result:=true
  740. { Win ABI depends on size to pass it in a register or not }
  741. else if (target_info.system=system_x86_64_win64) then
  742. result:=not aggregate_in_registers_win64(varspez,def.size)
  743. { pass constant parameters that would be passed via memory by
  744. reference for non-cdecl/cppdecl, and make sure that the tmethod
  745. record (size=16) is passed the same way as a complex procvar }
  746. else if ((varspez=vs_const) and
  747. not(calloption in cdecl_pocalls)) or
  748. (def.size=16) then
  749. begin
  750. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  751. result:=numclasses=0;
  752. end
  753. else
  754. { SysV ABI always passes it as value parameter }
  755. result:=false;
  756. end;
  757. arraydef :
  758. begin
  759. { cdecl array of const need to be ignored and therefor be puhsed
  760. as value parameter with length 0 }
  761. if ((calloption in cdecl_pocalls) and
  762. is_array_of_const(def)) or
  763. is_dynamic_array(def) then
  764. result:=false
  765. else
  766. { pass all arrays by reference to be compatible with C (passing
  767. an array by value (= copying it on the stack) does not exist,
  768. because an array is the same as a pointer there }
  769. result:=true
  770. end;
  771. objectdef :
  772. begin
  773. { don't treat objects like records, because we only know wheter
  774. or not they'll have a VMT after the entire object is parsed
  775. -> if they are used as function result from one of their own
  776. methods, their size can still change after we've determined
  777. whether this function result should be returned by reference or
  778. by value }
  779. if is_object(def) then
  780. result:=true;
  781. end;
  782. variantdef,
  783. stringdef,
  784. procvardef,
  785. setdef :
  786. begin
  787. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  788. result:=numclasses=0;
  789. end;
  790. end;
  791. end;
  792. function tx86_64paramanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  793. begin
  794. if target_info.system=system_x86_64_win64 then
  795. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  796. else
  797. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  798. end;
  799. function tx86_64paramanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  800. begin
  801. if target_info.system=system_x86_64_win64 then
  802. result:=[RS_XMM0..RS_XMM5]
  803. else
  804. result:=[RS_XMM0..RS_XMM15];
  805. end;
  806. function tx86_64paramanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  807. begin
  808. result:=[RS_ST0..RS_ST7];
  809. end;
  810. procedure tx86_64paramanager.getintparaloc(pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);
  811. var
  812. paraloc : pcgparalocation;
  813. psym : tparavarsym;
  814. pdef : tdef;
  815. begin
  816. psym:=tparavarsym(pd.paras[nr-1]);
  817. pdef:=psym.vardef;
  818. if push_addr_param(psym.varspez,pdef,pd.proccalloption) then
  819. pdef:=getpointerdef(pdef);
  820. cgpara.reset;
  821. cgpara.size:=def_cgsize(pdef);
  822. cgpara.intsize:=tcgsize2size[cgpara.size];
  823. cgpara.alignment:=get_para_align(pd.proccalloption);
  824. cgpara.def:=pdef;
  825. paraloc:=cgpara.add_location;
  826. with paraloc^ do
  827. begin
  828. size:=def_cgsize(pdef);
  829. paraloc^.def:=pdef;
  830. if target_info.system=system_x86_64_win64 then
  831. begin
  832. if nr<1 then
  833. internalerror(200304303)
  834. else if nr<=high(paraintsupregs_winx64)+1 then
  835. begin
  836. loc:=LOC_REGISTER;
  837. register:=newreg(R_INTREGISTER,paraintsupregs_winx64[nr-1],cgsize2subreg(R_INTREGISTER,size));
  838. end
  839. else
  840. begin
  841. loc:=LOC_REFERENCE;
  842. reference.index:=NR_STACK_POINTER_REG;
  843. reference.offset:=(nr-6)*sizeof(aint);
  844. end;
  845. end
  846. else
  847. begin
  848. if nr<1 then
  849. internalerror(200304303)
  850. else if nr<=high(paraintsupregs)+1 then
  851. begin
  852. loc:=LOC_REGISTER;
  853. register:=newreg(R_INTREGISTER,paraintsupregs[nr-1],cgsize2subreg(R_INTREGISTER,size));
  854. end
  855. else
  856. begin
  857. loc:=LOC_REFERENCE;
  858. reference.index:=NR_STACK_POINTER_REG;
  859. reference.offset:=(nr-6)*sizeof(aint);
  860. end;
  861. end;
  862. end;
  863. end;
  864. function tx86_64paramanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  865. const
  866. intretregs: array[0..1] of tregister = (NR_FUNCTION_RETURN_REG,NR_FUNCTION_RETURN_REG_HIGH);
  867. mmretregs: array[0..1] of tregister = (NR_MM_RESULT_REG,NR_MM_RESULT_REG_HIGH);
  868. var
  869. classes: tx64paraclasses;
  870. i,
  871. numclasses: longint;
  872. intretregidx,
  873. mmretregidx: longint;
  874. retcgsize : tcgsize;
  875. paraloc : pcgparalocation;
  876. begin
  877. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  878. exit;
  879. { integer sizes < 32 bit have to be sign/zero extended to 32 bit on
  880. the callee side (caller can expect those bits are valid) }
  881. if (side=calleeside) and
  882. (retcgsize in [OS_8,OS_S8,OS_16,OS_S16]) then
  883. begin
  884. retcgsize:=OS_S32;
  885. result.def:=s32inttype;
  886. result.intsize:=4;
  887. result.size:=retcgsize;
  888. end;
  889. { Return in FPU register? -> don't use classify_argument(), because
  890. currency and comp need special treatment here (they are integer class
  891. when passing as parameter, but LOC_FPUREGISTER as function result) }
  892. if result.def.typ=floatdef then
  893. begin
  894. paraloc:=result.add_location;
  895. paraloc^.def:=result.def;
  896. case tfloatdef(result.def).floattype of
  897. s32real:
  898. begin
  899. paraloc^.loc:=LOC_MMREGISTER;
  900. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMS);
  901. paraloc^.size:=OS_F32;
  902. end;
  903. s64real:
  904. begin
  905. paraloc^.loc:=LOC_MMREGISTER;
  906. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMD);
  907. paraloc^.size:=OS_F64;
  908. end;
  909. { the first two only exist on targets with an x87, on others
  910. they are replace by int64 }
  911. s64currency,
  912. s64comp,
  913. s80real,
  914. sc80real:
  915. begin
  916. paraloc^.loc:=LOC_FPUREGISTER;
  917. paraloc^.register:=NR_FPU_RESULT_REG;
  918. paraloc^.size:=retcgsize;
  919. end;
  920. else
  921. internalerror(200405034);
  922. end;
  923. end
  924. else
  925. { Return in register }
  926. begin
  927. fillchar(classes,sizeof(classes),0);
  928. numclasses:=classify_argument(result.def,vs_value,result.def.size,classes,0);
  929. { this would mean a memory return }
  930. if (numclasses=0) then
  931. internalerror(2010021502);
  932. { this would mean an _m256 vector (valid, but not yet supported) }
  933. if (numclasses>2) then
  934. internalerror(2010021503);
  935. intretregidx:=0;
  936. mmretregidx:=0;
  937. for i:=0 to numclasses-1 do
  938. begin
  939. paraloc:=result.add_location;
  940. paraloc^.def:=classes[i].def;
  941. case classes[i].typ of
  942. X86_64_INTEGERSI_CLASS,
  943. X86_64_INTEGER_CLASS:
  944. begin
  945. paraloc^.loc:=LOC_REGISTER;
  946. paraloc^.register:=intretregs[intretregidx];
  947. if classes[i].typ=X86_64_INTEGER_CLASS then
  948. begin
  949. paraloc^.size:=OS_64;
  950. if paraloc^.def.size<>8 then
  951. paraloc^.def:=u64inttype;
  952. end
  953. else if result.intsize in [1,2,4] then
  954. begin
  955. paraloc^.size:=retcgsize;
  956. paraloc^.def:=result.def;
  957. end
  958. else
  959. begin
  960. paraloc^.size:=OS_32;
  961. if paraloc^.def.size<>4 then
  962. paraloc^.def:=u32inttype;
  963. end;
  964. setsubreg(paraloc^.register,cgsize2subreg(R_INTREGISTER,paraloc^.size));
  965. inc(intretregidx);
  966. end;
  967. X86_64_SSE_CLASS,
  968. X86_64_SSEUP_CLASS,
  969. X86_64_SSESF_CLASS,
  970. X86_64_SSEDF_CLASS:
  971. begin
  972. paraloc^.loc:=LOC_MMREGISTER;
  973. paraloc^.register:=mmretregs[mmretregidx];
  974. case classes[i].typ of
  975. X86_64_SSESF_CLASS:
  976. begin
  977. setsubreg(paraloc^.register,R_SUBMMS);
  978. paraloc^.size:=OS_F32;
  979. end;
  980. X86_64_SSEDF_CLASS:
  981. begin
  982. setsubreg(paraloc^.register,R_SUBMMD);
  983. paraloc^.size:=OS_F64;
  984. end;
  985. else
  986. begin
  987. setsubreg(paraloc^.register,R_SUBQ);
  988. paraloc^.size:=OS_M64;
  989. end;
  990. end;
  991. inc(mmretregidx);
  992. end;
  993. X86_64_NO_CLASS:
  994. begin
  995. { empty record/array }
  996. if (i<>0) or
  997. (numclasses<>1) then
  998. internalerror(2010060302);
  999. paraloc^.loc:=LOC_VOID;
  1000. paraloc^.def:=voidtype;
  1001. end;
  1002. else
  1003. internalerror(2010021504);
  1004. end;
  1005. end;
  1006. end;
  1007. end;
  1008. procedure tx86_64paramanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  1009. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  1010. var
  1011. hp : tparavarsym;
  1012. paradef : tdef;
  1013. paraloc : pcgparalocation;
  1014. subreg : tsubregister;
  1015. pushaddr : boolean;
  1016. paracgsize : tcgsize;
  1017. loc : array[1..2] of tx64paraclass;
  1018. needintloc,
  1019. needmmloc,
  1020. paralen,
  1021. locidx,
  1022. i,
  1023. varalign,
  1024. paraalign : longint;
  1025. begin
  1026. paraalign:=get_para_align(p.proccalloption);
  1027. { Register parameters are assigned from left to right }
  1028. for i:=0 to paras.count-1 do
  1029. begin
  1030. hp:=tparavarsym(paras[i]);
  1031. paradef:=hp.vardef;
  1032. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  1033. if pushaddr then
  1034. begin
  1035. loc[1].typ:=X86_64_INTEGER_CLASS;
  1036. loc[2].typ:=X86_64_NO_CLASS;
  1037. paracgsize:=OS_ADDR;
  1038. paralen:=sizeof(pint);
  1039. paradef:=getpointerdef(paradef);
  1040. loc[1].def:=paradef;
  1041. end
  1042. else
  1043. begin
  1044. getvalueparaloc(hp.varspez,paradef,loc[1],loc[2]);
  1045. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  1046. paracgsize:=def_cgsize(paradef);
  1047. { integer sizes < 32 bit have to be sign/zero extended to 32 bit
  1048. on the caller side }
  1049. if (side=callerside) and
  1050. (paracgsize in [OS_8,OS_S8,OS_16,OS_S16]) then
  1051. begin
  1052. paracgsize:=OS_S32;
  1053. paralen:=4;
  1054. paradef:=s32inttype;
  1055. loc[1].def:=paradef;
  1056. end;
  1057. end;
  1058. { cheat for now, we should copy the value to an mm reg as well (FK) }
  1059. if varargsparas and
  1060. (target_info.system = system_x86_64_win64) and
  1061. (paradef.typ = floatdef) then
  1062. begin
  1063. loc[2].typ:=X86_64_NO_CLASS;
  1064. if paracgsize=OS_F64 then
  1065. begin
  1066. loc[1].typ:=X86_64_INTEGER_CLASS;
  1067. paracgsize:=OS_64;
  1068. paradef:=u64inttype;
  1069. end
  1070. else
  1071. begin
  1072. loc[1].typ:=X86_64_INTEGERSI_CLASS;
  1073. paracgsize:=OS_32;
  1074. paradef:=u32inttype;
  1075. end;
  1076. loc[1].def:=paradef;
  1077. end;
  1078. hp.paraloc[side].reset;
  1079. hp.paraloc[side].size:=paracgsize;
  1080. hp.paraloc[side].intsize:=paralen;
  1081. hp.paraloc[side].Alignment:=paraalign;
  1082. hp.paraloc[side].def:=paradef;
  1083. if paralen>0 then
  1084. begin
  1085. { Enough registers free? }
  1086. needintloc:=0;
  1087. needmmloc:=0;
  1088. for locidx:=low(loc) to high(loc) do
  1089. case loc[locidx].typ of
  1090. X86_64_INTEGER_CLASS,
  1091. X86_64_INTEGERSI_CLASS:
  1092. inc(needintloc);
  1093. X86_64_SSE_CLASS,
  1094. X86_64_SSESF_CLASS,
  1095. X86_64_SSEDF_CLASS,
  1096. X86_64_SSEUP_CLASS:
  1097. inc(needmmloc);
  1098. end;
  1099. { the "-1" is because we can also use the current register }
  1100. if ((target_info.system=system_x86_64_win64) and
  1101. ((intparareg+needintloc-1 > high(paraintsupregs_winx64)) or
  1102. (mmparareg+needmmloc-1 > high(parammsupregs_winx64)))) or
  1103. ((target_info.system<>system_x86_64_win64) and
  1104. ((intparareg+needintloc-1 > high(paraintsupregs)) or
  1105. (mmparareg+needmmloc-1 > high(parammsupregs)))) then
  1106. begin
  1107. { If there are no registers available for any
  1108. eightbyte of an argument, the whole argument is
  1109. passed on the stack. }
  1110. loc[low(loc)].typ:=X86_64_MEMORY_CLASS;
  1111. loc[low(loc)].def:=paradef;
  1112. for locidx:=succ(low(loc)) to high(loc) do
  1113. loc[locidx].typ:=X86_64_NO_CLASS;
  1114. end;
  1115. locidx:=1;
  1116. while (paralen>0) do
  1117. begin
  1118. if locidx>2 then
  1119. internalerror(200501283);
  1120. { Allocate }
  1121. case loc[locidx].typ of
  1122. X86_64_INTEGER_CLASS,
  1123. X86_64_INTEGERSI_CLASS:
  1124. begin
  1125. paraloc:=hp.paraloc[side].add_location;
  1126. paraloc^.loc:=LOC_REGISTER;
  1127. paraloc^.def:=loc[locidx].def;
  1128. if (paracgsize=OS_NO) or (loc[2].typ<>X86_64_NO_CLASS) then
  1129. begin
  1130. if loc[locidx].typ=X86_64_INTEGER_CLASS then
  1131. begin
  1132. paraloc^.size:=OS_INT;
  1133. paraloc^.def:=u64inttype;
  1134. subreg:=R_SUBWHOLE;
  1135. end
  1136. else
  1137. begin
  1138. paraloc^.size:=OS_32;
  1139. paraloc^.def:=u32inttype;
  1140. subreg:=R_SUBD;
  1141. end;
  1142. end
  1143. else
  1144. begin
  1145. paraloc^.size:=paracgsize;
  1146. paraloc^.def:=paradef;
  1147. { s64comp is pushed in an int register }
  1148. if paraloc^.size=OS_C64 then
  1149. begin
  1150. paraloc^.size:=OS_64;
  1151. paraloc^.def:=u64inttype;
  1152. end;
  1153. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  1154. end;
  1155. { winx64 uses different registers }
  1156. if target_info.system=system_x86_64_win64 then
  1157. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  1158. else
  1159. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  1160. { matching mm register must be skipped }
  1161. if target_info.system=system_x86_64_win64 then
  1162. inc(mmparareg);
  1163. inc(intparareg);
  1164. dec(paralen,tcgsize2size[paraloc^.size]);
  1165. end;
  1166. X86_64_SSE_CLASS,
  1167. X86_64_SSESF_CLASS,
  1168. X86_64_SSEDF_CLASS,
  1169. X86_64_SSEUP_CLASS:
  1170. begin
  1171. paraloc:=hp.paraloc[side].add_location;
  1172. paraloc^.loc:=LOC_MMREGISTER;
  1173. paraloc^.def:=loc[locidx].def;
  1174. case loc[locidx].typ of
  1175. X86_64_SSESF_CLASS:
  1176. begin
  1177. subreg:=R_SUBMMS;
  1178. paraloc^.size:=OS_F32;
  1179. end;
  1180. X86_64_SSEDF_CLASS:
  1181. begin
  1182. subreg:=R_SUBMMD;
  1183. paraloc^.size:=OS_F64;
  1184. end;
  1185. else
  1186. begin
  1187. subreg:=R_SUBQ;
  1188. paraloc^.size:=OS_M64;
  1189. end;
  1190. end;
  1191. { winx64 uses different registers }
  1192. if target_info.system=system_x86_64_win64 then
  1193. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg)
  1194. else
  1195. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  1196. { matching int register must be skipped }
  1197. if target_info.system=system_x86_64_win64 then
  1198. inc(intparareg);
  1199. inc(mmparareg);
  1200. dec(paralen,tcgsize2size[paraloc^.size]);
  1201. end;
  1202. X86_64_MEMORY_CLASS :
  1203. begin
  1204. paraloc:=hp.paraloc[side].add_location;
  1205. paraloc^.loc:=LOC_REFERENCE;
  1206. paraloc^.def:=loc[locidx].def;
  1207. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  1208. however, since int_cgsize is called in many places in the
  1209. compiler where only a few can already handle OS_128, fixing it
  1210. properly is out of the question to release 2.2.0 in time. (DM)}
  1211. if paracgsize=OS_128 then
  1212. if paralen=8 then
  1213. paraloc^.size:=OS_64
  1214. else if paralen=16 then
  1215. paraloc^.size:=OS_128
  1216. else
  1217. internalerror(200707143)
  1218. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  1219. paraloc^.size:=int_float_cgsize(paralen)
  1220. else
  1221. paraloc^.size:=int_cgsize(paralen);
  1222. if side=callerside then
  1223. paraloc^.reference.index:=NR_STACK_POINTER_REG
  1224. else
  1225. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  1226. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  1227. paraloc^.reference.offset:=parasize;
  1228. parasize:=align(parasize+paralen,varalign);
  1229. paralen:=0;
  1230. end;
  1231. else
  1232. internalerror(2010053113);
  1233. end;
  1234. if (locidx<2) and
  1235. (loc[locidx+1].typ<>X86_64_NO_CLASS) then
  1236. inc(locidx);
  1237. end;
  1238. end
  1239. else
  1240. begin
  1241. paraloc:=hp.paraloc[side].add_location;
  1242. paraloc^.loc:=LOC_VOID;
  1243. paraloc^.def:=paradef;
  1244. end;
  1245. end;
  1246. { Register parameters are assigned from left-to-right, but the
  1247. offsets on the stack are right-to-left. There is no need
  1248. to reverse the offset, only adapt the calleeside with the
  1249. start offset of the first param on the stack }
  1250. if side=calleeside then
  1251. begin
  1252. for i:=0 to paras.count-1 do
  1253. begin
  1254. hp:=tparavarsym(paras[i]);
  1255. paraloc:=hp.paraloc[side].location;
  1256. while paraloc<>nil do
  1257. begin
  1258. with paraloc^ do
  1259. if (loc=LOC_REFERENCE) then
  1260. inc(reference.offset,target_info.first_parm_offset);
  1261. paraloc:=paraloc^.next;
  1262. end;
  1263. end;
  1264. end;
  1265. end;
  1266. function tx86_64paramanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  1267. var
  1268. intparareg,mmparareg,
  1269. parasize : longint;
  1270. begin
  1271. intparareg:=0;
  1272. mmparareg:=0;
  1273. if target_info.system=system_x86_64_win64 then
  1274. parasize:=4*8
  1275. else
  1276. parasize:=0;
  1277. { calculate the registers for the normal parameters }
  1278. create_paraloc_info_intern(p,callerside,p.paras,intparareg,mmparareg,parasize,false);
  1279. { append the varargs }
  1280. create_paraloc_info_intern(p,callerside,varargspara,intparareg,mmparareg,parasize,true);
  1281. { store used no. of SSE registers, that needs to be passed in %AL }
  1282. varargspara.mmregsused:=mmparareg;
  1283. result:=parasize;
  1284. end;
  1285. function tx86_64paramanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  1286. var
  1287. intparareg,mmparareg,
  1288. parasize : longint;
  1289. begin
  1290. intparareg:=0;
  1291. mmparareg:=0;
  1292. if target_info.system=system_x86_64_win64 then
  1293. parasize:=4*8
  1294. else
  1295. parasize:=0;
  1296. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  1297. { Create Function result paraloc }
  1298. create_funcretloc_info(p,side);
  1299. { We need to return the size allocated on the stack }
  1300. result:=parasize;
  1301. end;
  1302. begin
  1303. paramanager:=tx86_64paramanager.create;
  1304. end.