cpupara.pas 53 KB

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