cpupara.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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. result:=init_aggregate_classification(def,varspez,byte_offset,words,classes);
  462. if (words=0) then
  463. exit;
  464. isbitpacked:=is_packed_array(def);
  465. if not isbitpacked then
  466. begin
  467. elesize:=def.elesize;
  468. size:=elesize;
  469. end
  470. else
  471. begin
  472. elesize:=def.elepackedbitsize;
  473. bitoffset:=0;
  474. end;
  475. (* Merge the elements of the array. *)
  476. i:=0;
  477. elecount:=def.elecount;
  478. repeat
  479. if not isbitpacked then
  480. begin
  481. { size does not change }
  482. new_byte_offset:=byte_offset+i*elesize;
  483. { If [..] an object [..] contains unaligned fields, it has class
  484. MEMORY }
  485. if align(new_byte_offset,def.alignment)<>new_byte_offset then
  486. begin
  487. result:=0;
  488. exit;
  489. end;
  490. end
  491. else
  492. begin
  493. { calculate the number of bytes spanned by this bitpacked
  494. element }
  495. size:=((bitoffset+elesize+7) div 8)-(bitoffset div 8);
  496. new_byte_offset:=byte_offset+(elesize*i) div 8;
  497. { bit offset of next element }
  498. inc(bitoffset,elesize);
  499. end;
  500. num:=classify_aggregate_element(def.elementdef,varspez,size,classes,new_byte_offset);
  501. if (num=0) then
  502. exit(0);
  503. inc(i);
  504. until (i=elecount);
  505. result:=finalize_aggregate_classification(def,words,classes);
  506. end;
  507. function classify_argument(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint;
  508. begin
  509. case def.typ of
  510. orddef,
  511. enumdef,
  512. pointerdef,
  513. classrefdef:
  514. result:=classify_as_integer_argument(def,real_size,classes,byte_offset);
  515. formaldef:
  516. result:=classify_as_integer_argument(voidpointertype,voidpointertype.size,classes,byte_offset);
  517. floatdef:
  518. begin
  519. classes[0].def:=def;
  520. case tfloatdef(def).floattype of
  521. s32real:
  522. begin
  523. if byte_offset=0 then
  524. classes[0].typ:=X86_64_SSESF_CLASS
  525. else
  526. begin
  527. { if we have e.g. a record with two successive "single"
  528. fields, we need a 64 bit rather than a 32 bit load }
  529. classes[0].typ:=X86_64_SSE_CLASS;
  530. classes[0].def:=getarraydef(s32floattype,2);
  531. end;
  532. result:=1;
  533. end;
  534. s64real:
  535. begin
  536. classes[0].typ:=X86_64_SSEDF_CLASS;
  537. result:=1;
  538. end;
  539. s80real,
  540. sc80real:
  541. begin
  542. classes[0].typ:=X86_64_X87_CLASS;
  543. classes[1].typ:=X86_64_X87UP_CLASS;
  544. classes[1].def:=def;
  545. result:=2;
  546. end;
  547. s64comp,
  548. s64currency:
  549. begin
  550. classes[0].typ:=X86_64_INTEGER_CLASS;
  551. result:=1;
  552. end;
  553. s128real:
  554. begin
  555. classes[0].typ:=X86_64_SSE_CLASS;
  556. classes[0].def:=getarraydef(s32floattype,2);
  557. classes[1].typ:=X86_64_SSEUP_CLASS;
  558. classes[1].def:=getarraydef(s32floattype,2);
  559. result:=2;
  560. end;
  561. else
  562. internalerror(2010060301);
  563. end;
  564. end;
  565. recorddef:
  566. result:=classify_record(def,varspez,classes,byte_offset);
  567. objectdef:
  568. begin
  569. if is_object(def) then
  570. { pass by reference, like ppc and i386 }
  571. result:=0
  572. else
  573. { all kinds of pointer types: class, objcclass, interface, ... }
  574. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset);
  575. end;
  576. setdef:
  577. begin
  578. if is_smallset(def) then
  579. result:=classify_as_integer_argument(def,def.size,classes,byte_offset)
  580. else
  581. result:=0;
  582. end;
  583. stringdef:
  584. begin
  585. if (tstringdef(def).stringtype in [st_shortstring,st_longstring]) then
  586. result:=0
  587. else
  588. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  589. end;
  590. arraydef:
  591. begin
  592. { a dynamic array is treated like a pointer }
  593. if is_dynamic_array(def) then
  594. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset)
  595. { other special arrays are passed on the stack }
  596. else if is_open_array(def) or
  597. is_array_of_const(def) then
  598. result:=0
  599. else
  600. { normal array }
  601. result:=classify_normal_array(tarraydef(def),varspez,classes,byte_offset);
  602. end;
  603. { the file record is definitely too big }
  604. filedef:
  605. result:=0;
  606. procvardef:
  607. begin
  608. if (po_methodpointer in tprocvardef(def).procoptions) then
  609. begin
  610. { treat as TMethod record }
  611. def:=search_system_type('TMETHOD').typedef;
  612. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  613. end
  614. else
  615. { pointer }
  616. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  617. end;
  618. variantdef:
  619. begin
  620. { same as tvardata record }
  621. def:=search_system_type('TVARDATA').typedef;
  622. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  623. end;
  624. else
  625. internalerror(2010021405);
  626. end;
  627. end;
  628. procedure getvalueparaloc(varspez:tvarspez;def:tdef;var loc1,loc2:tx64paraclass);
  629. var
  630. size: aint;
  631. i: longint;
  632. classes: tx64paraclasses;
  633. numclasses: longint;
  634. begin
  635. { init the classes array, because even if classify_argument inits only
  636. one element we copy both to loc1/loc2 in case "1" is returned }
  637. for i:=low(classes) to high(classes) do
  638. begin
  639. classes[i].typ:=X86_64_NO_CLASS;
  640. classes[i].def:=nil;
  641. end;
  642. { def.size internalerrors for open arrays and dynamic arrays, since
  643. their size cannot be determined at compile-time.
  644. classify_argument does not look at the realsize argument for arrays
  645. cases, but we obviously do have to pass something... }
  646. if is_special_array(def) then
  647. size:=-1
  648. else
  649. size:=def.size;
  650. numclasses:=classify_argument(def,varspez,size,classes,0);
  651. case numclasses of
  652. 0:
  653. begin
  654. loc1.typ:=X86_64_MEMORY_CLASS;
  655. loc1.def:=def;
  656. loc2.typ:=X86_64_NO_CLASS;
  657. end;
  658. 1,2:
  659. begin
  660. { If the class is X87, X87UP or COMPLEX_X87, it is passed in memory }
  661. if classes[0].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  662. classes[0].typ:=X86_64_MEMORY_CLASS;
  663. if classes[1].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  664. classes[1].typ:=X86_64_MEMORY_CLASS;
  665. loc1:=classes[0];
  666. loc2:=classes[1];
  667. end
  668. else
  669. { 4 can only happen for _m256 vectors, not yet supported }
  670. internalerror(2010021501);
  671. end;
  672. end;
  673. function tx86_64paramanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  674. var
  675. classes: tx64paraclasses;
  676. numclasses: longint;
  677. begin
  678. if handle_common_ret_in_param(def,pd,result) then
  679. exit;
  680. fillchar(classes,sizeof(classes),0);
  681. case def.typ of
  682. { for records it depends on their contents and size }
  683. recorddef,
  684. { make sure we handle 'procedure of object' correctly }
  685. procvardef:
  686. begin
  687. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  688. result:=(numclasses=0);
  689. end;
  690. else
  691. result:=inherited ret_in_param(def,pd);
  692. end;
  693. end;
  694. function tx86_64paramanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  695. var
  696. paraloc : pcgparalocation;
  697. begin
  698. if not assigned(cgpara.location) then
  699. internalerror(200410102);
  700. result:=true;
  701. { All locations are LOC_REFERENCE }
  702. paraloc:=cgpara.location;
  703. while assigned(paraloc) do
  704. begin
  705. if (paraloc^.loc<>LOC_REFERENCE) then
  706. begin
  707. result:=false;
  708. exit;
  709. end;
  710. paraloc:=paraloc^.next;
  711. end;
  712. end;
  713. { true if a parameter is too large to copy and only the address is pushed }
  714. function tx86_64paramanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  715. var
  716. classes: tx64paraclasses;
  717. numclasses: longint;
  718. begin
  719. fillchar(classes,sizeof(classes),0);
  720. result:=false;
  721. { var,out,constref always require address }
  722. if varspez in [vs_var,vs_out,vs_constref] then
  723. begin
  724. result:=true;
  725. exit;
  726. end;
  727. { Only vs_const, vs_value here }
  728. case def.typ of
  729. formaldef :
  730. result:=true;
  731. recorddef :
  732. begin
  733. { MetroWerks Pascal: const records always passed by reference
  734. (for Mac OS X interfaces) }
  735. if (calloption=pocall_mwpascal) and
  736. (varspez=vs_const) then
  737. result:=true
  738. { Win ABI depends on size to pass it in a register or not }
  739. else if (target_info.system=system_x86_64_win64) then
  740. result:=not aggregate_in_registers_win64(varspez,def.size)
  741. { pass constant parameters that would be passed via memory by
  742. reference for non-cdecl/cppdecl, and make sure that the tmethod
  743. record (size=16) is passed the same way as a complex procvar }
  744. else if ((varspez=vs_const) and
  745. not(calloption in cdecl_pocalls)) or
  746. (def.size=16) then
  747. begin
  748. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  749. result:=numclasses=0;
  750. end
  751. else
  752. { SysV ABI always passes it as value parameter }
  753. result:=false;
  754. end;
  755. arraydef :
  756. begin
  757. { cdecl array of const need to be ignored and therefor be puhsed
  758. as value parameter with length 0 }
  759. if ((calloption in cdecl_pocalls) and
  760. is_array_of_const(def)) or
  761. is_dynamic_array(def) then
  762. result:=false
  763. else
  764. { pass all arrays by reference to be compatible with C (passing
  765. an array by value (= copying it on the stack) does not exist,
  766. because an array is the same as a pointer there }
  767. result:=true
  768. end;
  769. objectdef :
  770. begin
  771. { don't treat objects like records, because we only know wheter
  772. or not they'll have a VMT after the entire object is parsed
  773. -> if they are used as function result from one of their own
  774. methods, their size can still change after we've determined
  775. whether this function result should be returned by reference or
  776. by value }
  777. if is_object(def) then
  778. result:=true;
  779. end;
  780. variantdef,
  781. stringdef,
  782. procvardef,
  783. setdef :
  784. begin
  785. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  786. result:=numclasses=0;
  787. end;
  788. end;
  789. end;
  790. function tx86_64paramanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  791. begin
  792. if target_info.system=system_x86_64_win64 then
  793. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  794. else
  795. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  796. end;
  797. function tx86_64paramanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  798. begin
  799. if target_info.system=system_x86_64_win64 then
  800. result:=[RS_XMM0..RS_XMM5]
  801. else
  802. result:=[RS_XMM0..RS_XMM15];
  803. end;
  804. function tx86_64paramanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  805. begin
  806. result:=[RS_ST0..RS_ST7];
  807. end;
  808. procedure tx86_64paramanager.getintparaloc(pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);
  809. var
  810. paraloc : pcgparalocation;
  811. psym : tparavarsym;
  812. pdef : tdef;
  813. begin
  814. psym:=tparavarsym(pd.paras[nr-1]);
  815. pdef:=psym.vardef;
  816. if push_addr_param(psym.varspez,pdef,pd.proccalloption) then
  817. pdef:=getpointerdef(pdef);
  818. cgpara.reset;
  819. cgpara.size:=def_cgsize(pdef);
  820. cgpara.intsize:=tcgsize2size[cgpara.size];
  821. cgpara.alignment:=get_para_align(pd.proccalloption);
  822. cgpara.def:=pdef;
  823. paraloc:=cgpara.add_location;
  824. with paraloc^ do
  825. begin
  826. size:=def_cgsize(pdef);
  827. paraloc^.def:=pdef;
  828. if target_info.system=system_x86_64_win64 then
  829. begin
  830. if nr<1 then
  831. internalerror(200304303)
  832. else if nr<=high(paraintsupregs_winx64)+1 then
  833. begin
  834. loc:=LOC_REGISTER;
  835. register:=newreg(R_INTREGISTER,paraintsupregs_winx64[nr-1],cgsize2subreg(R_INTREGISTER,size));
  836. end
  837. else
  838. begin
  839. loc:=LOC_REFERENCE;
  840. reference.index:=NR_STACK_POINTER_REG;
  841. reference.offset:=(nr-6)*sizeof(aint);
  842. end;
  843. end
  844. else
  845. begin
  846. if nr<1 then
  847. internalerror(200304303)
  848. else if nr<=high(paraintsupregs)+1 then
  849. begin
  850. loc:=LOC_REGISTER;
  851. register:=newreg(R_INTREGISTER,paraintsupregs[nr-1],cgsize2subreg(R_INTREGISTER,size));
  852. end
  853. else
  854. begin
  855. loc:=LOC_REFERENCE;
  856. reference.index:=NR_STACK_POINTER_REG;
  857. reference.offset:=(nr-6)*sizeof(aint);
  858. end;
  859. end;
  860. end;
  861. end;
  862. function tx86_64paramanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  863. const
  864. intretregs: array[0..1] of tregister = (NR_FUNCTION_RETURN_REG,NR_FUNCTION_RETURN_REG_HIGH);
  865. mmretregs: array[0..1] of tregister = (NR_MM_RESULT_REG,NR_MM_RESULT_REG_HIGH);
  866. var
  867. classes: tx64paraclasses;
  868. i,
  869. numclasses: longint;
  870. intretregidx,
  871. mmretregidx: longint;
  872. retcgsize : tcgsize;
  873. paraloc : pcgparalocation;
  874. begin
  875. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  876. exit;
  877. { integer sizes < 32 bit have to be sign/zero extended to 32 bit on
  878. the callee side (caller can expect those bits are valid) }
  879. if (side=calleeside) and
  880. (retcgsize in [OS_8,OS_S8,OS_16,OS_S16]) then
  881. begin
  882. retcgsize:=OS_S32;
  883. result.def:=s32inttype;
  884. result.intsize:=4;
  885. result.size:=retcgsize;
  886. end;
  887. { Return in FPU register? -> don't use classify_argument(), because
  888. currency and comp need special treatment here (they are integer class
  889. when passing as parameter, but LOC_FPUREGISTER as function result) }
  890. if result.def.typ=floatdef then
  891. begin
  892. paraloc:=result.add_location;
  893. paraloc^.def:=result.def;
  894. case tfloatdef(result.def).floattype of
  895. s32real:
  896. begin
  897. paraloc^.loc:=LOC_MMREGISTER;
  898. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMS);
  899. paraloc^.size:=OS_F32;
  900. end;
  901. s64real:
  902. begin
  903. paraloc^.loc:=LOC_MMREGISTER;
  904. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMD);
  905. paraloc^.size:=OS_F64;
  906. end;
  907. { the first two only exist on targets with an x87, on others
  908. they are replace by int64 }
  909. s64currency,
  910. s64comp,
  911. s80real,
  912. sc80real:
  913. begin
  914. paraloc^.loc:=LOC_FPUREGISTER;
  915. paraloc^.register:=NR_FPU_RESULT_REG;
  916. paraloc^.size:=retcgsize;
  917. end;
  918. else
  919. internalerror(200405034);
  920. end;
  921. end
  922. else
  923. { Return in register }
  924. begin
  925. fillchar(classes,sizeof(classes),0);
  926. numclasses:=classify_argument(result.def,vs_value,result.def.size,classes,0);
  927. { this would mean a memory return }
  928. if (numclasses=0) then
  929. internalerror(2010021502);
  930. { this would mean an _m256 vector (valid, but not yet supported) }
  931. if (numclasses>2) then
  932. internalerror(2010021503);
  933. intretregidx:=0;
  934. mmretregidx:=0;
  935. for i:=0 to numclasses-1 do
  936. begin
  937. paraloc:=result.add_location;
  938. paraloc^.def:=classes[i].def;
  939. case classes[i].typ of
  940. X86_64_INTEGERSI_CLASS,
  941. X86_64_INTEGER_CLASS:
  942. begin
  943. paraloc^.loc:=LOC_REGISTER;
  944. paraloc^.register:=intretregs[intretregidx];
  945. if classes[i].typ=X86_64_INTEGER_CLASS then
  946. begin
  947. paraloc^.size:=OS_64;
  948. if paraloc^.def.size<>8 then
  949. paraloc^.def:=u64inttype;
  950. end
  951. else if result.intsize in [1,2,4] then
  952. begin
  953. paraloc^.size:=retcgsize;
  954. paraloc^.def:=result.def;
  955. end
  956. else
  957. begin
  958. paraloc^.size:=OS_32;
  959. if paraloc^.def.size<>4 then
  960. paraloc^.def:=u32inttype;
  961. end;
  962. setsubreg(paraloc^.register,cgsize2subreg(R_INTREGISTER,paraloc^.size));
  963. inc(intretregidx);
  964. end;
  965. X86_64_SSE_CLASS,
  966. X86_64_SSEUP_CLASS,
  967. X86_64_SSESF_CLASS,
  968. X86_64_SSEDF_CLASS:
  969. begin
  970. paraloc^.loc:=LOC_MMREGISTER;
  971. paraloc^.register:=mmretregs[mmretregidx];
  972. case classes[i].typ of
  973. X86_64_SSESF_CLASS:
  974. begin
  975. setsubreg(paraloc^.register,R_SUBMMS);
  976. paraloc^.size:=OS_F32;
  977. end;
  978. X86_64_SSEDF_CLASS:
  979. begin
  980. setsubreg(paraloc^.register,R_SUBMMD);
  981. paraloc^.size:=OS_F64;
  982. end;
  983. else
  984. begin
  985. setsubreg(paraloc^.register,R_SUBQ);
  986. paraloc^.size:=OS_M64;
  987. end;
  988. end;
  989. inc(mmretregidx);
  990. end;
  991. X86_64_NO_CLASS:
  992. begin
  993. { empty record/array }
  994. if (i<>0) or
  995. (numclasses<>1) then
  996. internalerror(2010060302);
  997. paraloc^.loc:=LOC_VOID;
  998. paraloc^.def:=voidtype;
  999. end;
  1000. else
  1001. internalerror(2010021504);
  1002. end;
  1003. end;
  1004. end;
  1005. end;
  1006. procedure tx86_64paramanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  1007. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  1008. var
  1009. hp : tparavarsym;
  1010. paradef : tdef;
  1011. paraloc : pcgparalocation;
  1012. subreg : tsubregister;
  1013. pushaddr : boolean;
  1014. paracgsize : tcgsize;
  1015. loc : array[1..2] of tx64paraclass;
  1016. needintloc,
  1017. needmmloc,
  1018. paralen,
  1019. locidx,
  1020. i,
  1021. varalign,
  1022. paraalign : longint;
  1023. begin
  1024. paraalign:=get_para_align(p.proccalloption);
  1025. { Register parameters are assigned from left to right }
  1026. for i:=0 to paras.count-1 do
  1027. begin
  1028. hp:=tparavarsym(paras[i]);
  1029. paradef:=hp.vardef;
  1030. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  1031. if pushaddr then
  1032. begin
  1033. loc[1].typ:=X86_64_INTEGER_CLASS;
  1034. loc[2].typ:=X86_64_NO_CLASS;
  1035. paracgsize:=OS_ADDR;
  1036. paralen:=sizeof(pint);
  1037. paradef:=getpointerdef(paradef);
  1038. loc[1].def:=paradef;
  1039. end
  1040. else
  1041. begin
  1042. getvalueparaloc(hp.varspez,paradef,loc[1],loc[2]);
  1043. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  1044. paracgsize:=def_cgsize(paradef);
  1045. { integer sizes < 32 bit have to be sign/zero extended to 32 bit
  1046. on the caller side }
  1047. if (side=callerside) and
  1048. (paracgsize in [OS_8,OS_S8,OS_16,OS_S16]) then
  1049. begin
  1050. paracgsize:=OS_S32;
  1051. paralen:=4;
  1052. paradef:=s32inttype;
  1053. loc[1].def:=paradef;
  1054. end;
  1055. end;
  1056. { cheat for now, we should copy the value to an mm reg as well (FK) }
  1057. if varargsparas and
  1058. (target_info.system = system_x86_64_win64) and
  1059. (paradef.typ = floatdef) then
  1060. begin
  1061. loc[2].typ:=X86_64_NO_CLASS;
  1062. if paracgsize=OS_F64 then
  1063. begin
  1064. loc[1].typ:=X86_64_INTEGER_CLASS;
  1065. paracgsize:=OS_64;
  1066. paradef:=u64inttype;
  1067. end
  1068. else
  1069. begin
  1070. loc[1].typ:=X86_64_INTEGERSI_CLASS;
  1071. paracgsize:=OS_32;
  1072. paradef:=u32inttype;
  1073. end;
  1074. loc[1].def:=paradef;
  1075. end;
  1076. hp.paraloc[side].reset;
  1077. hp.paraloc[side].size:=paracgsize;
  1078. hp.paraloc[side].intsize:=paralen;
  1079. hp.paraloc[side].Alignment:=paraalign;
  1080. hp.paraloc[side].def:=paradef;
  1081. if paralen>0 then
  1082. begin
  1083. { Enough registers free? }
  1084. needintloc:=0;
  1085. needmmloc:=0;
  1086. for locidx:=low(loc) to high(loc) do
  1087. case loc[locidx].typ of
  1088. X86_64_INTEGER_CLASS,
  1089. X86_64_INTEGERSI_CLASS:
  1090. inc(needintloc);
  1091. X86_64_SSE_CLASS,
  1092. X86_64_SSESF_CLASS,
  1093. X86_64_SSEDF_CLASS,
  1094. X86_64_SSEUP_CLASS:
  1095. inc(needmmloc);
  1096. end;
  1097. { the "-1" is because we can also use the current register }
  1098. if ((target_info.system=system_x86_64_win64) and
  1099. ((intparareg+needintloc-1 > high(paraintsupregs_winx64)) or
  1100. (mmparareg+needmmloc-1 > high(parammsupregs_winx64)))) or
  1101. ((target_info.system<>system_x86_64_win64) and
  1102. ((intparareg+needintloc-1 > high(paraintsupregs)) or
  1103. (mmparareg+needmmloc-1 > high(parammsupregs)))) then
  1104. begin
  1105. { If there are no registers available for any
  1106. eightbyte of an argument, the whole argument is
  1107. passed on the stack. }
  1108. loc[low(loc)].typ:=X86_64_MEMORY_CLASS;
  1109. loc[low(loc)].def:=paradef;
  1110. for locidx:=succ(low(loc)) to high(loc) do
  1111. loc[locidx].typ:=X86_64_NO_CLASS;
  1112. end;
  1113. locidx:=1;
  1114. while (paralen>0) do
  1115. begin
  1116. if locidx>2 then
  1117. internalerror(200501283);
  1118. { Allocate }
  1119. case loc[locidx].typ of
  1120. X86_64_INTEGER_CLASS,
  1121. X86_64_INTEGERSI_CLASS:
  1122. begin
  1123. paraloc:=hp.paraloc[side].add_location;
  1124. paraloc^.loc:=LOC_REGISTER;
  1125. paraloc^.def:=loc[locidx].def;
  1126. if (paracgsize=OS_NO) or (loc[2].typ<>X86_64_NO_CLASS) then
  1127. begin
  1128. if loc[locidx].typ=X86_64_INTEGER_CLASS then
  1129. begin
  1130. paraloc^.size:=OS_INT;
  1131. paraloc^.def:=u64inttype;
  1132. subreg:=R_SUBWHOLE;
  1133. end
  1134. else
  1135. begin
  1136. paraloc^.size:=OS_32;
  1137. paraloc^.def:=u32inttype;
  1138. subreg:=R_SUBD;
  1139. end;
  1140. end
  1141. else
  1142. begin
  1143. paraloc^.size:=paracgsize;
  1144. paraloc^.def:=paradef;
  1145. { s64comp is pushed in an int register }
  1146. if paraloc^.size=OS_C64 then
  1147. begin
  1148. paraloc^.size:=OS_64;
  1149. paraloc^.def:=u64inttype;
  1150. end;
  1151. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  1152. end;
  1153. { winx64 uses different registers }
  1154. if target_info.system=system_x86_64_win64 then
  1155. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  1156. else
  1157. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  1158. { matching mm register must be skipped }
  1159. if target_info.system=system_x86_64_win64 then
  1160. inc(mmparareg);
  1161. inc(intparareg);
  1162. dec(paralen,tcgsize2size[paraloc^.size]);
  1163. end;
  1164. X86_64_SSE_CLASS,
  1165. X86_64_SSESF_CLASS,
  1166. X86_64_SSEDF_CLASS,
  1167. X86_64_SSEUP_CLASS:
  1168. begin
  1169. paraloc:=hp.paraloc[side].add_location;
  1170. paraloc^.loc:=LOC_MMREGISTER;
  1171. paraloc^.def:=loc[locidx].def;
  1172. case loc[locidx].typ of
  1173. X86_64_SSESF_CLASS:
  1174. begin
  1175. subreg:=R_SUBMMS;
  1176. paraloc^.size:=OS_F32;
  1177. end;
  1178. X86_64_SSEDF_CLASS:
  1179. begin
  1180. subreg:=R_SUBMMD;
  1181. paraloc^.size:=OS_F64;
  1182. end;
  1183. else
  1184. begin
  1185. subreg:=R_SUBQ;
  1186. paraloc^.size:=OS_M64;
  1187. end;
  1188. end;
  1189. { winx64 uses different registers }
  1190. if target_info.system=system_x86_64_win64 then
  1191. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg)
  1192. else
  1193. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  1194. { matching int register must be skipped }
  1195. if target_info.system=system_x86_64_win64 then
  1196. inc(intparareg);
  1197. inc(mmparareg);
  1198. dec(paralen,tcgsize2size[paraloc^.size]);
  1199. end;
  1200. X86_64_MEMORY_CLASS :
  1201. begin
  1202. paraloc:=hp.paraloc[side].add_location;
  1203. paraloc^.loc:=LOC_REFERENCE;
  1204. paraloc^.def:=loc[locidx].def;
  1205. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  1206. however, since int_cgsize is called in many places in the
  1207. compiler where only a few can already handle OS_128, fixing it
  1208. properly is out of the question to release 2.2.0 in time. (DM)}
  1209. if paracgsize=OS_128 then
  1210. if paralen=8 then
  1211. paraloc^.size:=OS_64
  1212. else if paralen=16 then
  1213. paraloc^.size:=OS_128
  1214. else
  1215. internalerror(200707143)
  1216. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  1217. paraloc^.size:=int_float_cgsize(paralen)
  1218. else
  1219. paraloc^.size:=int_cgsize(paralen);
  1220. if side=callerside then
  1221. paraloc^.reference.index:=NR_STACK_POINTER_REG
  1222. else
  1223. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  1224. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  1225. paraloc^.reference.offset:=parasize;
  1226. parasize:=align(parasize+paralen,varalign);
  1227. paralen:=0;
  1228. end;
  1229. else
  1230. internalerror(2010053113);
  1231. end;
  1232. if (locidx<2) and
  1233. (loc[locidx+1].typ<>X86_64_NO_CLASS) then
  1234. inc(locidx);
  1235. end;
  1236. end
  1237. else
  1238. begin
  1239. paraloc:=hp.paraloc[side].add_location;
  1240. paraloc^.loc:=LOC_VOID;
  1241. paraloc^.def:=paradef;
  1242. end;
  1243. end;
  1244. { Register parameters are assigned from left-to-right, but the
  1245. offsets on the stack are right-to-left. There is no need
  1246. to reverse the offset, only adapt the calleeside with the
  1247. start offset of the first param on the stack }
  1248. if side=calleeside then
  1249. begin
  1250. for i:=0 to paras.count-1 do
  1251. begin
  1252. hp:=tparavarsym(paras[i]);
  1253. paraloc:=hp.paraloc[side].location;
  1254. while paraloc<>nil do
  1255. begin
  1256. with paraloc^ do
  1257. if (loc=LOC_REFERENCE) then
  1258. inc(reference.offset,target_info.first_parm_offset);
  1259. paraloc:=paraloc^.next;
  1260. end;
  1261. end;
  1262. end;
  1263. end;
  1264. function tx86_64paramanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  1265. var
  1266. intparareg,mmparareg,
  1267. parasize : longint;
  1268. begin
  1269. intparareg:=0;
  1270. mmparareg:=0;
  1271. if target_info.system=system_x86_64_win64 then
  1272. parasize:=4*8
  1273. else
  1274. parasize:=0;
  1275. { calculate the registers for the normal parameters }
  1276. create_paraloc_info_intern(p,callerside,p.paras,intparareg,mmparareg,parasize,false);
  1277. { append the varargs }
  1278. create_paraloc_info_intern(p,callerside,varargspara,intparareg,mmparareg,parasize,true);
  1279. { store used no. of SSE registers, that needs to be passed in %AL }
  1280. varargspara.mmregsused:=mmparareg;
  1281. result:=parasize;
  1282. end;
  1283. function tx86_64paramanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  1284. var
  1285. intparareg,mmparareg,
  1286. parasize : longint;
  1287. begin
  1288. intparareg:=0;
  1289. mmparareg:=0;
  1290. if target_info.system=system_x86_64_win64 then
  1291. parasize:=4*8
  1292. else
  1293. parasize:=0;
  1294. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  1295. { Create Function result paraloc }
  1296. create_funcretloc_info(p,side);
  1297. { We need to return the size allocated on the stack }
  1298. result:=parasize;
  1299. end;
  1300. begin
  1301. paramanager:=tx86_64paramanager.create;
  1302. end.