cpupara.pas 53 KB

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