cpupara.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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. parabase,paramgr;
  25. type
  26. tcpuparamanager = class(tparamanager)
  27. private
  28. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  29. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  30. public
  31. function param_use_paraloc(const cgpara:tcgpara):boolean;override;
  32. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  33. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  34. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;override;
  35. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;override;
  36. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;override;
  37. function get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;override;
  38. function get_saved_registers_mm(calloption: tproccalloption):tcpuregisterarray;override;
  39. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  40. function create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;override;
  41. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  42. end;
  43. implementation
  44. uses
  45. cutils,verbose,
  46. systems,
  47. globals,defutil,
  48. symtable,symutil,
  49. cpupi,cpuinfo,
  50. cgx86,cgobj,cgcpu;
  51. const
  52. paraintsupregs : array[0..5] of tsuperregister = (RS_RDI,RS_RSI,RS_RDX,RS_RCX,RS_R8,RS_R9);
  53. parammsupregs : array[0..7] of tsuperregister = (RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5,RS_XMM6,RS_XMM7);
  54. paraintsupregs_winx64 : array[0..3] of tsuperregister = (RS_RCX,RS_RDX,RS_R8,RS_R9);
  55. parammsupregs_winx64 : array[0..3] of tsuperregister = (RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3);
  56. parammsupregs_vectorcall : array[0..5] of tsuperregister = (RS_XMM0,RS_XMM1,RS_XMM2,RS_XMM3,RS_XMM4,RS_XMM5);
  57. {
  58. The argument classification code largely comes from libffi:
  59. ffi64.c - Copyright (c) 2002, 2007 Bo Thorsen <[email protected]>
  60. Copyright (c) 2008 Red Hat, Inc.
  61. x86-64 Foreign Function Interface
  62. Permission is hereby granted, free of charge, to any person obtaining
  63. a copy of this software and associated documentation files (the
  64. ``Software''), to deal in the Software without restriction, including
  65. without limitation the rights to use, copy, modify, merge, publish,
  66. distribute, sublicense, and/or sell copies of the Software, and to
  67. permit persons to whom the Software is furnished to do so, subject to
  68. the following conditions:
  69. The above copyright notice and this permission notice shall be included
  70. in all copies or substantial portions of the Software.
  71. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
  72. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  73. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  74. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  75. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  76. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  77. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  78. DEALINGS IN THE SOFTWARE.
  79. ----------------------------------------------------------------------- *)
  80. }
  81. const
  82. { This many classes are required in order to support 4 YMMs (_m256) in a
  83. homogeneous vector aggregate under vectorcall. [Kit] }
  84. MAX_PARA_CLASSES = 16;
  85. type
  86. tx64paraclasstype = (
  87. X86_64_NO_CLASS,
  88. X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS,
  89. X86_64_SSE_CLASS,X86_64_SSESF_CLASS,X86_64_SSEDF_CLASS,X86_64_SSEUP_CLASS,
  90. X86_64_X87_CLASS,X86_64_X87UP_CLASS,
  91. X86_64_COMPLEX_X87_CLASS,
  92. X86_64_MEMORY_CLASS
  93. );
  94. tx64paraclass = record
  95. def: tdef;
  96. typ: tx64paraclasstype;
  97. end;
  98. tx64paraclasses = array[0..MAX_PARA_CLASSES-1] of tx64paraclass;
  99. { Win64-specific helper }
  100. function aggregate_in_registers_win64(varspez:tvarspez;size:longint):boolean;
  101. begin
  102. { TODO: Temporary hack: vs_const parameters are always passed by reference for win64}
  103. result:=(varspez=vs_value) and (size in [1,2,4,8])
  104. end;
  105. (* x86-64 register passing implementation. See x86-64 ABI for details. Goal
  106. of this code is to classify each 8bytes of incoming argument by the register
  107. class and assign registers accordingly. *)
  108. function classify_representative_def(def1, def2: tdef): tdef;
  109. var
  110. def1size, def2size: asizeint;
  111. begin
  112. if not assigned(def1) then
  113. result:=def2
  114. else if not assigned(def2) then
  115. result:=def1
  116. else
  117. begin
  118. def1size:=def1.size;
  119. def2size:=def2.size;
  120. if def1size>def2size then
  121. result:=def1
  122. else if def2size>def1size then
  123. result:=def2
  124. else if def1.alignment>def2.alignment then
  125. result:=def1
  126. else
  127. result:=def2;
  128. end;
  129. end;
  130. (* Classify the argument of type TYPE and mode MODE.
  131. CLASSES will be filled by the register class used to pass each word
  132. of the operand. The number of words is returned. In case the parameter
  133. should be passed in memory, 0 is returned. As a special case for zero
  134. sized containers, classes[0] will be NO_CLASS and 1 is returned.
  135. real_size contains either def.size, or a value derived from
  136. def.bitpackedsize and the field offset denoting the number of bytes
  137. spanned by a bitpacked field
  138. See the x86-64 PS ABI for details.
  139. *)
  140. procedure classify_single_integer_class(def: tdef; size,real_size: aint; var cl: tx64paraclass; byte_offset: aint);
  141. begin
  142. if (byte_offset=0) and
  143. (real_size in [1,2,4,8]) and
  144. (not assigned(cl.def) or
  145. (def.alignment>=cl.def.alignment)) then
  146. cl.def:=def;
  147. if size<=4 then
  148. begin
  149. cl.typ:=X86_64_INTEGERSI_CLASS;
  150. { The ABI does not require any sign/zero extension for parameters,
  151. except for _Bool (= Pascal boolean) to 8 bits. However, some
  152. compilers (clang) extend them to 32 bits anyway and rely on it
  153. -> also do it for compatibility when calling such code }
  154. if not assigned(cl.def) or
  155. (cl.def.typ<>orddef) or
  156. (torddef(cl.def).ordtype<>pasbool1) then
  157. cl.def:=u32inttype;
  158. end
  159. else
  160. begin
  161. cl.typ:=X86_64_INTEGER_CLASS;
  162. if not assigned(cl.def) or
  163. (cl.def.size<size) or
  164. (not(cl.def.typ in [orddef,floatdef,pointerdef,classrefdef]) and
  165. not is_implicit_pointer_object_type(cl.def) and
  166. not is_dynamicstring(cl.def) and
  167. not is_dynamic_array(cl.def)) then
  168. cl.def:=u64inttype;
  169. end;
  170. end;
  171. function classify_as_integer_argument(def: tdef; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint;
  172. var
  173. size: aint;
  174. begin
  175. size:=byte_offset+real_size;
  176. classify_single_integer_class(def,size,real_size,classes[0],byte_offset);
  177. if size<=8 then
  178. result:=1
  179. else
  180. begin
  181. classify_single_integer_class(def,size-8,real_size,classes[1],byte_offset-8);
  182. if size>16 then
  183. internalerror(2010021401);
  184. result:=2;
  185. end
  186. end;
  187. (* Return the union class of CLASS1 and CLASS2.
  188. See the x86-64 PS ABI for details. *)
  189. function merge_classes(class1, class2: tx64paraclass): tx64paraclass;
  190. begin
  191. (* Rule #1: If both classes are equal, this is the resulting class. *)
  192. if (class1.typ=class2.typ) then
  193. begin
  194. result.typ:=class1.typ;
  195. result.def:=classify_representative_def(class1.def,class2.def);
  196. exit;
  197. end;
  198. (* Rule #2: If one of the classes is NO_CLASS, the resulting class is
  199. the other class. *)
  200. if (class1.typ=X86_64_NO_CLASS) then
  201. exit(class2);
  202. if (class2.typ=X86_64_NO_CLASS) then
  203. exit(class1);
  204. (* Rule #3: If one of the classes is MEMORY, the result is MEMORY. *)
  205. if (class1.typ=X86_64_MEMORY_CLASS) then
  206. exit(class1)
  207. else if (class2.typ=X86_64_MEMORY_CLASS) then
  208. exit(class2);
  209. (* Rule #4: If one of the classes is INTEGER, the result is INTEGER. *)
  210. { 32 bit }
  211. if ((class1.typ=X86_64_INTEGERSI_CLASS) and
  212. (class2.typ=X86_64_SSESF_CLASS)) then
  213. exit(class1)
  214. else if ((class2.typ=X86_64_INTEGERSI_CLASS) and
  215. (class1.typ=X86_64_SSESF_CLASS)) then
  216. exit(class2);
  217. { 64 bit }
  218. if (class1.typ in [X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS]) then
  219. begin
  220. result:=class1;
  221. if result.def.size<8 then
  222. begin
  223. result.typ:=X86_64_INTEGER_CLASS;
  224. result.def:=s64inttype;
  225. end;
  226. exit
  227. end
  228. else if (class2.typ in [X86_64_INTEGER_CLASS,X86_64_INTEGERSI_CLASS]) then
  229. begin
  230. result:=class2;
  231. if result.def.size<8 then
  232. begin
  233. result.typ:=X86_64_INTEGER_CLASS;
  234. result.def:=s64inttype;
  235. end;
  236. exit
  237. end;
  238. (* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
  239. MEMORY is used. *)
  240. if (class1.typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS]) then
  241. begin
  242. result:=class1;
  243. result.typ:=X86_64_MEMORY_CLASS;
  244. exit;
  245. end
  246. else if (class2.typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS]) then
  247. begin
  248. result:=class2;
  249. result.typ:=X86_64_MEMORY_CLASS;
  250. exit;
  251. end;
  252. (* Rule #6: Otherwise class SSE is used. *)
  253. if class1.def.size>class2.def.size then
  254. result:=class1
  255. else
  256. result:=class2;
  257. result.typ:=X86_64_SSE_CLASS;
  258. result.def:=s64floattype;
  259. end;
  260. function classify_argument(calloption: tproccalloption; def: tdef; parentdef: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint; round_to_8: Boolean): longint; forward;
  261. function init_aggregate_classification(calloption: tproccalloption; def: tdef; parentdef: tdef; varspez: tvarspez; byte_offset: aint; out words: longint; out classes: tx64paraclasses): longint;
  262. var
  263. i: longint;
  264. begin
  265. words:=0;
  266. { we'll be merging the classes elements with the subclasses
  267. elements, so initialise them first }
  268. for i:=low(classes) to high(classes) do
  269. begin
  270. classes[i].typ:=X86_64_NO_CLASS;
  271. classes[i].def:=nil;
  272. end;
  273. { win64 follows a different convention here }
  274. if x86_64_use_ms_abi(calloption) then
  275. begin
  276. if aggregate_in_registers_win64(varspez,def.size) then
  277. begin
  278. classes[0].typ:=X86_64_INTEGER_CLASS;
  279. classes[0].def:=def;
  280. result:=1;
  281. end
  282. else if (calloption = pocall_vectorcall) then
  283. begin
  284. words := (def.size+byte_offset mod 8+7) div 8;
  285. case words of
  286. 0:
  287. Exit(0);
  288. 1..4:
  289. { Aligned vector or array elements }
  290. Result := words;
  291. else
  292. if ((def.aggregatealignment mod (words shl 3)) = 0) or
  293. Assigned(parentdef) and ((parentdef.aggregatealignment mod 16) = 0)
  294. then
  295. begin
  296. { Field of aligned vector type }
  297. if words = 0 then
  298. begin
  299. classes[0].typ:=X86_64_NO_CLASS;
  300. classes[0].def:=def;
  301. Result := 1;
  302. end
  303. else
  304. Result := words;
  305. end
  306. else
  307. Result := 0;
  308. end;
  309. end
  310. else
  311. Result := 0;
  312. Exit;
  313. end;
  314. (* If the struct is larger than 32 bytes, pass it on the stack. *)
  315. if def.size > 32 then
  316. exit(0);
  317. { if a struct starts an offset not divisible by 8, it can span extra
  318. words }
  319. words:=(def.size+byte_offset mod 8+7) div 8;
  320. (* Zero sized arrays or structures are NO_CLASS. We return 0 to
  321. signal memory class, so handle it as special case. *)
  322. if (words=0) then
  323. begin
  324. classes[0].typ:=X86_64_NO_CLASS;
  325. classes[0].def:=def;
  326. exit(1);
  327. end;
  328. result:=words;
  329. end;
  330. function classify_aggregate_element(calloption: tproccalloption; def: tdef; parentdef: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; new_byte_offset: aint): longint;
  331. var
  332. subclasses: tx64paraclasses;
  333. i,
  334. pos: longint;
  335. begin
  336. fillchar(subclasses,sizeof(subclasses),0);
  337. result:=classify_argument(calloption,def,parentdef,varspez,real_size,subclasses,new_byte_offset, True);
  338. if (result=0) then
  339. exit;
  340. pos:=new_byte_offset div 8;
  341. if result-1+pos>high(classes) then
  342. internalerror(2010053108);
  343. for i:=0 to result-1 do
  344. begin
  345. classes[i+pos] :=
  346. merge_classes(subclasses[i],classes[i+pos]);
  347. end;
  348. inc(result,pos);
  349. end;
  350. function finalize_aggregate_classification(calloption: tproccalloption; def: tdef; words: longint; var classes: tx64paraclasses): longint;
  351. var
  352. i, vecsize, maxvecsize: longint;
  353. begin
  354. { Workaround: It's not immediately possible to determine if a Double is
  355. by itself or is part of an aligned vector. If the latter, correct the
  356. class definitions here. [Kit] }
  357. if (classes[0].typ = X86_64_SSEDF_CLASS) and (classes[1].typ = X86_64_SSEUP_CLASS) then
  358. classes[0].typ := X86_64_SSE_CLASS;
  359. if (words>2) then
  360. begin
  361. { When size > 16 bytes, if the first one isn't
  362. X86_64_SSE_CLASS or any other ones aren't
  363. X86_64_SSEUP_CLASS, everything should be passed in
  364. memory... }
  365. if (classes[0].typ<>X86_64_SSE_CLASS) then
  366. begin
  367. { ... except if the calling convention is 'vectorcall', then
  368. check to see if we don't have an HFA of 3 or 4 Doubles }
  369. if (calloption <> pocall_vectorcall) or (words > 4) then
  370. Exit(0);
  371. for i := 0 to words - 1 do
  372. if classes[i].typ <> X86_64_SSEDF_CLASS then
  373. Exit(0);
  374. Exit(words);
  375. end;
  376. if ((words shl 3) > def.aggregatealignment) then
  377. { The alignment is wrong for this vector size, hence it is unaligned }
  378. Exit(0);
  379. vecsize := 1;
  380. maxvecsize := words;
  381. for i:=1 to words-1 do
  382. if (classes[i].typ=X86_64_SSEUP_CLASS) then
  383. Inc(vecsize)
  384. else
  385. begin
  386. { Exceptional case. Check that we're not dealing an array of
  387. aligned vectors that is itself aligned to a stricter
  388. boundary (e.g. 4 XMM registers that can be merged into a
  389. single ZMM register). }
  390. if
  391. (classes[i].typ <> X86_64_SSE_CLASS) or { Easy case first - is it actually another SSE vector? }
  392. ((vecsize and (vecsize - 1)) <> 0) or { If vecsize is not a power of two, then it is definitely not a valid vector }
  393. (vecsize > maxvecsize) or ((maxvecsize < words) and (vecsize <> maxvecsize)) { Mixture of XMMs and YMMs, for example, is not valid }
  394. then
  395. Exit(0);
  396. classes[i].typ := X86_64_SSEUP_CLASS;
  397. maxvecsize := vecsize;
  398. vecsize := 1;
  399. end;
  400. if vecsize <> maxvecsize then
  401. { Last vector is of a different size }
  402. Exit(0);
  403. if vecsize > 2 then
  404. begin
  405. { Cannot use 256-bit and 512-bit vectors if we're not using AVX }
  406. if not UseAVX then
  407. Exit(0);
  408. { WARNING: There is currently no support for 256-bit and 512-bit
  409. aligned vectors, so if an aggregate contains more than two
  410. eightbyte words, it must be passed in memory. When 256-bit and
  411. 512-bit vectors are fully supported, remove the following
  412. line. [Kit] }
  413. Exit(0);
  414. end;
  415. end;
  416. (* Final merger cleanup. *)
  417. (* The first one must never be X86_64_SSEUP_CLASS or
  418. X86_64_X87UP_CLASS. *)
  419. if (classes[0].typ=X86_64_SSEUP_CLASS) or
  420. (classes[0].typ=X86_64_X87UP_CLASS) then
  421. internalerror(2010021402);
  422. for i:=0 to words-1 do
  423. begin
  424. (* If one class is MEMORY, everything should be passed in
  425. memory. *)
  426. if (classes[i].typ=X86_64_MEMORY_CLASS) then
  427. exit(0);
  428. (* The X86_64_SSEUP_CLASS should be always preceded by
  429. X86_64_SSE_CLASS or X86_64_SSEUP_CLASS. *)
  430. if (classes[i].typ=X86_64_SSEUP_CLASS) and
  431. (classes[i-1].typ<>X86_64_SSE_CLASS) and
  432. (classes[i-1].typ<>X86_64_SSEUP_CLASS) then
  433. begin
  434. classes[i].typ:=X86_64_SSE_CLASS;
  435. classes[i].def:=carraydef.getreusable_no_free(s32floattype,2);
  436. end;
  437. (* If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
  438. everything should be passed in memory. *)
  439. if (classes[i].typ=X86_64_X87UP_CLASS) and
  440. (classes[i-1].typ<>X86_64_X87_CLASS) then
  441. exit(0);
  442. (* FPC addition: because we store an extended in 10 bytes, the
  443. X86_64_X87UP_CLASS can be replaced with e.g. INTEGER if an
  444. extended is followed by e.g. an array [0..5] of byte -> we also
  445. have to check whether each X86_64_X87_CLASS is followed by
  446. X86_64_X87UP_CLASS -- if not, pass in memory
  447. This cannot happen in the original ABI, because there
  448. sizeof(extended) = 16 and hence nothing can be merged with
  449. X86_64_X87UP_CLASS and change it into something else *)
  450. if (classes[i].typ=X86_64_X87_CLASS) and
  451. ((i=(words-1)) or
  452. (classes[i+1].typ<>X86_64_X87UP_CLASS)) then
  453. exit(0);
  454. end;
  455. {$ifndef llvm}
  456. { FIXME: in case a record contains empty padding space, e.g. a
  457. "single" field followed by a "double", then we have a problem
  458. because the cgpara helpers cannot figure out that they should
  459. skip 4 bytes after storing the single (LOC_MMREGISTER with size
  460. OS_F32) to memory before storing the double -> for now scale
  461. such locations always up to 64 bits, although this loads/stores
  462. some superfluous data }
  463. { 1) the first part is 32 bit while there is still a second part }
  464. if (classes[1].typ<>X86_64_NO_CLASS) then
  465. case classes[0].typ of
  466. X86_64_INTEGERSI_CLASS:
  467. begin
  468. classes[0].typ:=X86_64_INTEGER_CLASS;
  469. classes[0].def:=s64inttype;
  470. end;
  471. X86_64_SSESF_CLASS:
  472. begin
  473. classes[0].typ:=X86_64_SSE_CLASS;
  474. classes[0].def:=carraydef.getreusable_no_free(s32floattype,2);
  475. end;
  476. else
  477. ;
  478. end;
  479. { 2) the second part is 32 bit, but the total size is > 12 bytes }
  480. if (def.size>12) then
  481. case classes[1].typ of
  482. X86_64_INTEGERSI_CLASS:
  483. begin
  484. classes[1].typ:=X86_64_INTEGER_CLASS;
  485. classes[1].def:=s64inttype;
  486. end;
  487. X86_64_SSESF_CLASS:
  488. begin
  489. classes[1].typ:=X86_64_SSE_CLASS;
  490. classes[1].def:=carraydef.getreusable_no_free(s32floattype,2);
  491. end;
  492. else
  493. ;
  494. end;
  495. {$endif not llvm}
  496. result:=words;
  497. end;
  498. function try_build_homogeneous_aggregate(def: tdef; words: longint; var classes: tx64paraclasses): longint;
  499. var
  500. i, vecsize, maxvecsize, veccount: longint;
  501. {size, }byte_offset: aint;
  502. vs: TFieldVarSym;
  503. checkalignment: Boolean;
  504. begin
  505. if (words = 0) then
  506. { Should be at least 1 word at this point }
  507. InternalError(2018013100);
  508. case classes[0].typ of
  509. X86_64_SSESF_CLASS:
  510. begin
  511. { Should be an HFA of only a Single }
  512. for i := 1 to High(classes) do
  513. if classes[i].typ <> X86_64_NO_CLASS then
  514. Exit(0);
  515. result := 1;
  516. end;
  517. X86_64_SSEDF_CLASS:
  518. begin
  519. { Possibly an HFA of Doubles }
  520. if TAbstractRecordDef(def).symtable.symlist.count = 0 then
  521. Exit(0);
  522. { Get the information and position on the last entry }
  523. vs:=TFieldVarSym(TAbstractRecordDef(def).symtable.symlist[TAbstractRecordDef(def).symtable.symlist.count - 1]);
  524. //size:=vs.vardef.size;
  525. checkalignment:=true;
  526. if not TAbstractRecordSymtable(TAbstractRecordDef(def).symtable).is_packed then
  527. begin
  528. byte_offset:=vs.fieldoffset;
  529. //size:=vs.vardef.size;
  530. end
  531. else
  532. begin
  533. byte_offset:=vs.fieldoffset div 8;
  534. if (vs.vardef.typ in [orddef,enumdef]) then
  535. begin
  536. { calculate the number of bytes spanned by
  537. this bitpacked field }
  538. //size:=((vs.fieldoffset+vs.vardef.packedbitsize+7) div 8)-(vs.fieldoffset div 8);
  539. { our bitpacked fields are interpreted as always being
  540. aligned, because unlike in C we don't have char:1, int:1
  541. etc (so everything is basically a char:x) }
  542. checkalignment:=false;
  543. end
  544. else
  545. ;//size:=vs.vardef.size;
  546. end;
  547. { If [..] an object [..] contains unaligned fields, it has class
  548. MEMORY }
  549. if checkalignment and
  550. (align(byte_offset,vs.vardef.structalignment)<>byte_offset) then
  551. begin
  552. result:=0;
  553. exit;
  554. end;
  555. if words > 4 then
  556. { HFA too large }
  557. Exit(0);
  558. for i := 1 to words - 1 do
  559. if classes[i].typ <> X86_64_SSEDF_CLASS then
  560. Exit(0);
  561. result := words;
  562. end;
  563. X86_64_SSE_CLASS:
  564. begin
  565. { Determine the nature of the classes.
  566. - If the SSE is by itself, then it is an HFA consisting of 2 Singles.
  567. - If the SSE is followed by an SSESF, then it is an HFA consisting of 3 Singles.
  568. - If the SSE is followed by an SSE and nothing else, then it is an HFA consisting of 4 Singles.
  569. - If the SSE is followed by an SSE, but another class follows, then it is an HFA that is too large.
  570. - If the SSE is followed by an SSEUP, then it is an HVA of some kind.
  571. }
  572. case classes[1].typ of
  573. X86_64_NO_CLASS:
  574. begin
  575. for i := 2 to words - 1 do
  576. if classes[i].typ <> X86_64_NO_CLASS then
  577. { Compound type }
  578. Exit(0);
  579. { Split into 2 Singles again so they correctly fall into separate XMM registers }
  580. classes[0].typ := X86_64_SSESF_CLASS;
  581. if classes[0].def.typ = arraydef then
  582. { Break up the array }
  583. classes[0].def := tdef(tarraydef(classes[0].def).elementdef); { Break up the array }
  584. classes[1].typ := X86_64_SSESF_CLASS;
  585. classes[1].def := classes[0].def;
  586. result := 2;
  587. end;
  588. X86_64_SSESF_CLASS:
  589. begin
  590. for i := 2 to words - 1 do
  591. if classes[i].typ <> X86_64_NO_CLASS then
  592. { Compound type }
  593. Exit(0);
  594. classes[2].typ := X86_64_SSESF_CLASS;
  595. classes[2].def := classes[1].def; { Transfer class 1 to class 2 }
  596. classes[0].typ := X86_64_SSESF_CLASS;
  597. if classes[0].def.typ = arraydef then
  598. { Break up the array }
  599. classes[0].def := tdef(tarraydef(classes[0].def).elementdef);
  600. classes[1].typ := X86_64_SSESF_CLASS;
  601. classes[1].def := classes[0].def;
  602. result := 3;
  603. end;
  604. X86_64_SSE_CLASS:
  605. begin
  606. for i := 2 to words - 1 do
  607. if classes[i].typ <> X86_64_NO_CLASS then
  608. { HFA too large (or not a true HFA) }
  609. Exit(0);
  610. if classes[0].def.typ = arraydef then
  611. { Break up the array }
  612. classes[0].def := tdef(tarraydef(classes[0].def).elementdef);
  613. if classes[1].def.typ = arraydef then
  614. { Break up the array }
  615. classes[2].def := tdef(tarraydef(classes[1].def).elementdef);
  616. classes[1].def := classes[0].def;
  617. classes[3].def := classes[2].def;
  618. classes[0].typ := X86_64_SSESF_CLASS;
  619. classes[1].typ := X86_64_SSESF_CLASS;
  620. classes[2].typ := X86_64_SSESF_CLASS;
  621. classes[3].typ := X86_64_SSESF_CLASS;
  622. result := 4;
  623. end;
  624. X86_64_SSEUP_CLASS:
  625. begin
  626. { Determine vector size }
  627. veccount := 1;
  628. vecsize := 2;
  629. maxvecsize := words;
  630. for i := 2 to words - 1 do
  631. if (classes[i].typ=X86_64_SSEUP_CLASS) then
  632. Inc(vecsize)
  633. else
  634. begin
  635. if
  636. (classes[i].typ <> X86_64_SSE_CLASS) or { Easy case first - is it actually another SSE vector? }
  637. ((vecsize and (vecsize - 1)) <> 0) or { If vecsize is not a power of two, then it is definitely not a valid aggregate }
  638. (vecsize > maxvecsize) or ((maxvecsize < words) and (vecsize <> maxvecsize)) { Mixture of XMMs and YMMs, for example, is not valid }
  639. then
  640. Exit(0);
  641. Inc(veccount);
  642. maxvecsize := vecsize;
  643. vecsize := 1;
  644. end;
  645. if vecsize <> maxvecsize then
  646. { Last vector is of a different size }
  647. Exit(0);
  648. if veccount > 4 then
  649. { HVA too large }
  650. Exit(0);
  651. Result := words;
  652. end;
  653. else
  654. Exit(0);
  655. end;
  656. end;
  657. else
  658. Exit(0);
  659. end;
  660. end;
  661. function classify_record(calloption: tproccalloption; def: tdef; parentdef: tdef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  662. var
  663. vs: tfieldvarsym;
  664. size,
  665. new_byte_offset: aint;
  666. i,
  667. words,
  668. num: longint;
  669. checkalignment: boolean;
  670. begin
  671. result:=init_aggregate_classification(calloption,def,parentdef,varspez,byte_offset,words,classes);
  672. if (words=0) then
  673. exit;
  674. (* Merge the fields of the structure. *)
  675. for i:=0 to tabstractrecorddef(def).symtable.symlist.count-1 do
  676. begin
  677. if not is_normal_fieldvarsym(tsym(tabstractrecorddef(def).symtable.symlist[i])) then
  678. continue;
  679. vs:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[i]);
  680. checkalignment:=true;
  681. if not tabstractrecordsymtable(tabstractrecorddef(def).symtable).is_packed then
  682. begin
  683. new_byte_offset:=byte_offset+vs.fieldoffset;
  684. size:=vs.vardef.size;
  685. end
  686. else
  687. begin
  688. new_byte_offset:=byte_offset+vs.fieldoffset div 8;
  689. if (vs.vardef.typ in [orddef,enumdef]) then
  690. begin
  691. { calculate the number of bytes spanned by
  692. this bitpacked field }
  693. size:=((vs.fieldoffset+vs.vardef.packedbitsize+7) div 8)-(vs.fieldoffset div 8);
  694. { our bitpacked fields are interpreted as always being
  695. aligned, because unlike in C we don't have char:1, int:1
  696. etc (so everything is basically a char:x) }
  697. checkalignment:=false;
  698. end
  699. else
  700. size:=vs.vardef.size;
  701. end;
  702. { If [..] an object [..] contains unaligned fields, it has class
  703. MEMORY }
  704. if checkalignment and
  705. (align(new_byte_offset,vs.vardef.structalignment)<>new_byte_offset) then
  706. begin
  707. result:=0;
  708. exit;
  709. end;
  710. num:=classify_aggregate_element(calloption,vs.vardef,def,varspez,size,classes,new_byte_offset);
  711. if (num=0) then
  712. exit(0);
  713. end;
  714. result:=finalize_aggregate_classification(calloption,def,words,classes);
  715. { There is still one case where it might not have to be passed on the
  716. stack, and that's a homogeneous vector aggregate (HVA) or a
  717. homogeneous float aggregate (HFA) under vectorcall. }
  718. if (calloption = pocall_vectorcall) then
  719. begin
  720. if (result = 0) then
  721. result := try_build_homogeneous_aggregate(def,words,classes)
  722. else
  723. { If we're dealing with an HFA that has 3 or 4 Singles, pairs of
  724. Singles may be merged into a single SSE_CLASS, which must be
  725. split into separate SSESF_CLASS references for vectorcall; this
  726. is only performed in "try_build_homogeneous_aggregate" and not
  727. elsewhere, so accommodate for this exceptional case. [Kit] }
  728. if (result = 2) then
  729. begin
  730. num := try_build_homogeneous_aggregate(def,words,classes);
  731. if num <> 0 then
  732. { If it's equal to zero, just pass 2 and handle the record
  733. type normally }
  734. result := num;
  735. end;
  736. end;
  737. end;
  738. function classify_normal_array(calloption: tproccalloption; def: tarraydef; parentdef: tdef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  739. var
  740. i, elecount: aword;
  741. size,
  742. elesize,
  743. new_byte_offset,
  744. bitoffset: aint;
  745. words,
  746. num: longint;
  747. isbitpacked: boolean;
  748. begin
  749. size:=0;
  750. bitoffset:=0;
  751. result:=init_aggregate_classification(calloption,def,parentdef,varspez,byte_offset,words,classes);
  752. if (words=0) then
  753. exit;
  754. isbitpacked:=is_packed_array(def);
  755. if not isbitpacked then
  756. begin
  757. elesize:=def.elesize;
  758. size:=elesize;
  759. end
  760. else
  761. begin
  762. elesize:=def.elepackedbitsize;
  763. bitoffset:=0;
  764. end;
  765. (* Merge the elements of the array. *)
  766. i:=0;
  767. elecount:=def.elecount;
  768. repeat
  769. if not isbitpacked then
  770. begin
  771. { size does not change }
  772. new_byte_offset:=byte_offset+i*elesize;
  773. { If [..] an object [..] contains unaligned fields, it has class
  774. MEMORY }
  775. if align(new_byte_offset,def.alignment)<>new_byte_offset then
  776. begin
  777. result:=0;
  778. exit;
  779. end;
  780. end
  781. else
  782. begin
  783. { calculate the number of bytes spanned by this bitpacked
  784. element }
  785. size:=((bitoffset+elesize+7) div 8)-(bitoffset div 8);
  786. new_byte_offset:=byte_offset+(elesize*i) div 8;
  787. { bit offset of next element }
  788. inc(bitoffset,elesize);
  789. end;
  790. num:=classify_aggregate_element(calloption,def.elementdef,def,varspez,size,classes,new_byte_offset);
  791. if (num=0) then
  792. exit(0);
  793. inc(i);
  794. until (i=elecount);
  795. result:=finalize_aggregate_classification(calloption,def,words,classes);
  796. end;
  797. function classify_argument(calloption: tproccalloption; def: tdef; parentdef: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint; round_to_8: Boolean): longint;
  798. var
  799. rounded_offset: aint;
  800. begin
  801. if round_to_8 then
  802. rounded_offset := byte_offset mod 8
  803. else
  804. rounded_offset := byte_offset;
  805. case def.typ of
  806. orddef,
  807. enumdef,
  808. pointerdef,
  809. classrefdef:
  810. result:=classify_as_integer_argument(def,real_size,classes,rounded_offset);
  811. formaldef:
  812. result:=classify_as_integer_argument(voidpointertype,voidpointertype.size,classes,rounded_offset);
  813. floatdef:
  814. begin
  815. classes[0].def:=def;
  816. case tfloatdef(def).floattype of
  817. s32real:
  818. begin
  819. if (byte_offset mod 8) = 0 then { Check regardless of the round_to_8 flag }
  820. begin
  821. if Assigned(parentdef) and ((parentdef.aggregatealignment mod 16) = 0) and ((byte_offset mod parentdef.aggregatealignment) <> 0) then
  822. { Third element of an aligned vector }
  823. classes[0].typ:=X86_64_SSEUP_CLASS
  824. else
  825. classes[0].typ:=X86_64_SSESF_CLASS
  826. end
  827. else
  828. begin
  829. if Assigned(parentdef) and ((parentdef.aggregatealignment mod 16) = 0) then
  830. { Fourth element of an aligned vector }
  831. classes[0].typ:=X86_64_SSEUP_CLASS
  832. else
  833. { if we have e.g. a record with two successive "single"
  834. fields, we need a 64 bit rather than a 32 bit load }
  835. classes[0].typ:=X86_64_SSE_CLASS;
  836. classes[0].def:=carraydef.getreusable_no_free(s32floattype,2);
  837. end;
  838. result:=1;
  839. end;
  840. s64real:
  841. begin
  842. if Assigned(parentdef) and ((parentdef.aggregatealignment mod 16) = 0) and ((byte_offset mod parentdef.aggregatealignment) <> 0) then
  843. { Aligned vector of type double }
  844. classes[0].typ:=X86_64_SSEUP_CLASS
  845. else
  846. classes[0].typ:=X86_64_SSEDF_CLASS;
  847. result:=1;
  848. end;
  849. s80real,
  850. sc80real:
  851. begin
  852. classes[0].typ:=X86_64_X87_CLASS;
  853. classes[1].typ:=X86_64_X87UP_CLASS;
  854. classes[1].def:=def;
  855. result:=2;
  856. end;
  857. s64comp,
  858. s64currency:
  859. begin
  860. classes[0].typ:=X86_64_INTEGER_CLASS;
  861. result:=1;
  862. end;
  863. s128real:
  864. begin
  865. classes[0].typ:=X86_64_SSE_CLASS;
  866. classes[0].def:=carraydef.getreusable_no_free(s32floattype,2);
  867. classes[1].typ:=X86_64_SSEUP_CLASS;
  868. classes[1].def:=carraydef.getreusable_no_free(s32floattype,2);
  869. result:=2;
  870. end;
  871. end;
  872. end;
  873. recorddef:
  874. result:=classify_record(calloption,def,parentdef,varspez,classes,rounded_offset);
  875. objectdef:
  876. begin
  877. if is_object(def) then
  878. { pass by reference, like ppc and i386 }
  879. result:=0
  880. else
  881. { all kinds of pointer types: class, objcclass, interface, ... }
  882. result:=classify_as_integer_argument(def,voidpointertype.size,classes,rounded_offset);
  883. end;
  884. setdef:
  885. begin
  886. if is_smallset(def) then
  887. result:=classify_as_integer_argument(def,def.size,classes,rounded_offset)
  888. else
  889. result:=0;
  890. end;
  891. stringdef:
  892. begin
  893. if (tstringdef(def).stringtype in [st_shortstring,st_longstring]) then
  894. result:=0
  895. else
  896. result:=classify_as_integer_argument(def,def.size,classes,rounded_offset);
  897. end;
  898. arraydef:
  899. begin
  900. { a dynamic array is treated like a pointer }
  901. if is_dynamic_array(def) then
  902. result:=classify_as_integer_argument(def,voidpointertype.size,classes,rounded_offset)
  903. { other special arrays are passed on the stack }
  904. else if is_open_array(def) or
  905. is_array_of_const(def) then
  906. result:=0
  907. else
  908. { normal array }
  909. result:=classify_normal_array(calloption,tarraydef(def),parentdef,varspez,classes,rounded_offset);
  910. end;
  911. { the file record is definitely too big }
  912. filedef:
  913. result:=0;
  914. procvardef:
  915. begin
  916. if (po_methodpointer in tprocvardef(def).procoptions) then
  917. begin
  918. { treat as TMethod record }
  919. def:=search_system_type('TMETHOD').typedef;
  920. result:=classify_argument(calloption,def,parentdef,varspez,def.size,classes,rounded_offset, False);
  921. end
  922. else
  923. { pointer }
  924. result:=classify_as_integer_argument(def,def.size,classes,rounded_offset);
  925. end;
  926. variantdef:
  927. begin
  928. { same as tvardata record }
  929. def:=search_system_type('TVARDATA').typedef;
  930. result:=classify_argument(calloption,def,parentdef,varspez,def.size,classes,rounded_offset, False);
  931. end;
  932. undefineddef:
  933. { show shall we know?
  934. since classify_argument is called during parsing, see tw27685.pp,
  935. we handle undefineddef here }
  936. result:=0;
  937. errordef:
  938. { error message should have been thrown already before, so avoid only
  939. an internal error }
  940. result:=0;
  941. else
  942. internalerror(2010021405);
  943. end;
  944. end;
  945. { Returns the size of a single element in the aggregate, or the entire vector, if it is one of these types, 0 otherwise }
  946. function is_simd_vector_type_or_homogeneous_aggregate(calloption: tproccalloption; def: tdef; varspez: tvarspez): aint;
  947. var
  948. numclasses,i,vecsize,veccount,maxvecsize:longint;
  949. classes: tx64paraclasses;
  950. firstclass: tx64paraclasstype;
  951. begin
  952. for i := Low(classes) to High(classes) do
  953. begin
  954. classes[i].typ := X86_64_NO_CLASS;
  955. classes[i].def := nil;
  956. end;
  957. numclasses:=classify_argument(calloption,def,nil,vs_value,def.size,classes,0,False);
  958. if numclasses = 0 then
  959. Exit(0);
  960. firstclass := classes[0].typ;
  961. case firstclass of
  962. X86_64_SSESF_CLASS: { Only valid if the aggregate contains a lone Single }
  963. begin
  964. if (numclasses = 1) and (calloption = pocall_vectorcall) then
  965. Result := 4
  966. else
  967. Result := 0;
  968. Exit;
  969. end;
  970. X86_64_SSEDF_CLASS:
  971. begin
  972. if (numclasses > 1) and (calloption <> pocall_vectorcall) then
  973. Result := 0
  974. else
  975. begin
  976. for i := 1 to numclasses - 1 do
  977. if classes[i].typ <> X86_64_SSEDF_CLASS then
  978. begin
  979. Result := 0;
  980. Exit;
  981. end;
  982. if (def.size div 8) <> numclasses then
  983. { Wrong alignment or compound size }
  984. Result := 0
  985. else
  986. Result := 8;
  987. end;
  988. end;
  989. X86_64_SSE_CLASS:
  990. begin
  991. maxvecsize := numclasses * 2;
  992. if numclasses = 1 then
  993. begin
  994. { 2 Singles }
  995. if calloption = pocall_vectorcall then
  996. Result := 4
  997. else
  998. Result := 0;
  999. Exit;
  1000. end;
  1001. if classes[1].typ = X86_64_SSESF_CLASS then
  1002. begin
  1003. { 3 Singles }
  1004. if numclasses <> 2 then
  1005. Result := 0
  1006. else
  1007. Result := 4;
  1008. Exit;
  1009. end;
  1010. vecsize := 2;
  1011. veccount := 1;
  1012. for i := 1 to numclasses - 1 do
  1013. case classes[i].typ of
  1014. X86_64_SSEUP_CLASS:
  1015. Inc(vecsize, 2);
  1016. X86_64_SSE_CLASS:
  1017. begin
  1018. if (maxvecsize < numclasses * 2) and (vecsize <> maxvecsize) then
  1019. { Different vector sizes }
  1020. Exit(0);
  1021. maxvecsize := vecsize;
  1022. vecsize := 2;
  1023. Inc(veccount);
  1024. end;
  1025. else
  1026. Exit(0);
  1027. end;
  1028. if vecsize <> maxvecsize then
  1029. { Last vector has to be the same size }
  1030. Exit(0);
  1031. { Either an HFA with 4 Singles, or an HVA with up to 4 vectors
  1032. (or a lone SIMD vector if veccount = 1) }
  1033. if (veccount < 4) then
  1034. begin
  1035. if (veccount > 1) and (calloption <> pocall_vectorcall) then
  1036. Result := 0
  1037. else
  1038. if vecsize = 2 then
  1039. { Packed, unaligned array of Singles }
  1040. Result := 4
  1041. else
  1042. Result := vecsize * 8
  1043. end
  1044. else
  1045. Result := 0;
  1046. end;
  1047. else
  1048. Exit(0);
  1049. end;
  1050. end;
  1051. procedure getvalueparaloc(calloption: tproccalloption;varspez:tvarspez;def:tdef;var classes: tx64paraclasses);
  1052. var
  1053. size: aint;
  1054. i: longint;
  1055. numclasses: longint;
  1056. begin
  1057. { init the classes array, because even if classify_argument inits only
  1058. one element we copy both to loc1/loc2 in case "1" is returned }
  1059. for i:=low(classes) to high(classes) do
  1060. begin
  1061. classes[i].typ:=X86_64_NO_CLASS;
  1062. classes[i].def:=nil;
  1063. end;
  1064. { def.size internalerrors for open arrays and dynamic arrays, since
  1065. their size cannot be determined at compile-time.
  1066. classify_argument does not look at the realsize argument for arrays
  1067. cases, but we obviously do have to pass something... }
  1068. if is_special_array(def) then
  1069. size:=-1
  1070. else
  1071. size:=def.size;
  1072. numclasses:=classify_argument(calloption,def,nil,varspez,size,classes,0,False);
  1073. case numclasses of
  1074. 0:
  1075. begin
  1076. classes[0].typ:=X86_64_MEMORY_CLASS;
  1077. classes[0].def:=def;
  1078. end;
  1079. 1..4:
  1080. begin
  1081. { If the class is X87, X87UP or COMPLEX_X87, it is passed in memory }
  1082. for i := 0 to numclasses - 1 do
  1083. begin
  1084. if classes[i].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  1085. classes[i].typ:=X86_64_MEMORY_CLASS;
  1086. end;
  1087. end;
  1088. else
  1089. { 8 can happen for _m512 vectors, but are not yet supported }
  1090. internalerror(2010021501);
  1091. end;
  1092. end;
  1093. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  1094. var
  1095. classes: tx64paraclasses;
  1096. numclasses: longint;
  1097. begin
  1098. if handle_common_ret_in_param(def,pd,result) then
  1099. exit;
  1100. fillchar(classes,sizeof(classes),0);
  1101. case def.typ of
  1102. { for records it depends on their contents and size }
  1103. recorddef,
  1104. { make sure we handle 'procedure of object' correctly }
  1105. procvardef:
  1106. begin
  1107. numclasses:=classify_argument(pd.proccalloption,def,nil,vs_value,def.size,classes,0,False);
  1108. result:=(numclasses=0);
  1109. end;
  1110. else
  1111. result:=inherited ret_in_param(def,pd);
  1112. end;
  1113. end;
  1114. function tcpuparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  1115. var
  1116. paraloc : pcgparalocation;
  1117. begin
  1118. if not assigned(cgpara.location) then
  1119. internalerror(200410102);
  1120. result:=true;
  1121. { All locations are LOC_REFERENCE }
  1122. paraloc:=cgpara.location;
  1123. while assigned(paraloc) do
  1124. begin
  1125. if (paraloc^.loc<>LOC_REFERENCE) then
  1126. begin
  1127. result:=false;
  1128. exit;
  1129. end;
  1130. paraloc:=paraloc^.next;
  1131. end;
  1132. end;
  1133. { true if a parameter is too large to copy and only the address is pushed }
  1134. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  1135. var
  1136. classes: tx64paraclasses;
  1137. numclasses: longint;
  1138. begin
  1139. fillchar(classes,sizeof(classes),0);
  1140. result:=false;
  1141. { var,out,constref always require address }
  1142. if varspez in [vs_var,vs_out,vs_constref] then
  1143. begin
  1144. result:=true;
  1145. exit;
  1146. end;
  1147. { Only vs_const, vs_value here }
  1148. case def.typ of
  1149. formaldef :
  1150. result:=true;
  1151. recorddef :
  1152. begin
  1153. { MetroWerks Pascal: const records always passed by reference
  1154. (for Mac OS X interfaces) }
  1155. if (calloption=pocall_mwpascal) and
  1156. (varspez=vs_const) then
  1157. result:=true
  1158. { Win ABI depends on size to pass it in a register or not }
  1159. else if x86_64_use_ms_abi(calloption) then
  1160. begin
  1161. if calloption = pocall_vectorcall then
  1162. begin
  1163. { "vectorcall" has the addition that it allows for aligned SSE types }
  1164. result :=
  1165. not aggregate_in_registers_win64(varspez,def.size) and
  1166. (is_simd_vector_type_or_homogeneous_aggregate(pocall_vectorcall,def,vs_value) = 0);
  1167. end
  1168. else
  1169. result:=not aggregate_in_registers_win64(varspez,def.size)
  1170. end
  1171. { pass constant parameters that would be passed via memory by
  1172. reference for non-cdecl/cppdecl, and make sure that the tmethod
  1173. record (size=16) is passed the same way as a complex procvar }
  1174. else if ((varspez=vs_const) and
  1175. not(calloption in cdecl_pocalls)) or
  1176. (def.size=16) then
  1177. begin
  1178. numclasses:=classify_argument(calloption,def,nil,vs_value,def.size,classes,0,False);
  1179. result:=numclasses=0;
  1180. end
  1181. else
  1182. { SysV ABI always passes it as value parameter }
  1183. result:=false;
  1184. end;
  1185. arraydef :
  1186. begin
  1187. { cdecl array of const need to be ignored and therefor be puhsed
  1188. as value parameter with length 0 }
  1189. if ((calloption in cdecl_pocalls) and
  1190. is_array_of_const(def)) or
  1191. is_dynamic_array(def) then
  1192. result:=false
  1193. else if (calloption = pocall_vectorcall) then
  1194. begin
  1195. { Pass all arrays by reference unless they are a valid, aligned SIMD type (arrays can't be homogeneous aggregates) }
  1196. result := (is_simd_vector_type_or_homogeneous_aggregate(pocall_vectorcall,def,vs_value) = 0);
  1197. end
  1198. else
  1199. { pass all arrays by reference to be compatible with C (passing
  1200. an array by value (= copying it on the stack) does not exist,
  1201. because an array is the same as a pointer there }
  1202. result:=true
  1203. end;
  1204. objectdef :
  1205. begin
  1206. { don't treat objects like records, because we only know wheter
  1207. or not they'll have a VMT after the entire object is parsed
  1208. -> if they are used as function result from one of their own
  1209. methods, their size can still change after we've determined
  1210. whether this function result should be returned by reference or
  1211. by value }
  1212. if is_object(def) then
  1213. result:=true;
  1214. end;
  1215. variantdef,
  1216. stringdef,
  1217. procvardef,
  1218. setdef :
  1219. begin
  1220. numclasses:=classify_argument(calloption,def,nil,vs_value,def.size,classes,0,False);
  1221. result:=numclasses=0;
  1222. end;
  1223. else
  1224. ;
  1225. end;
  1226. end;
  1227. function tcpuparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  1228. begin
  1229. if x86_64_use_ms_abi(calloption) then
  1230. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  1231. else
  1232. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  1233. end;
  1234. function tcpuparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  1235. begin
  1236. if x86_64_use_ms_abi(calloption) then
  1237. result:=[RS_XMM0..RS_XMM5]
  1238. else
  1239. result:=[RS_XMM0..RS_XMM15];
  1240. { Don't list registers that aren't available }
  1241. if FPUX86_HAS_32MMREGS in fpu_capabilities[current_settings.fputype] then
  1242. result:=result+[RS_XMM16..RS_XMM31];
  1243. end;
  1244. function tcpuparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  1245. begin
  1246. result:=[RS_ST0..RS_ST7];
  1247. end;
  1248. function tcpuparamanager.get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;
  1249. const
  1250. win64_saved_std_regs : tcpuregisterarray = (RS_RBX,RS_RDI,RS_RSI,RS_R12,RS_R13,RS_R14,RS_R15,RS_RBP);
  1251. others_saved_std_regs : tcpuregisterarray = (RS_RBX,RS_R12,RS_R13,RS_R14,RS_R15);
  1252. begin
  1253. if tcgx86_64(cg).use_ms_abi then
  1254. result:=win64_saved_std_regs
  1255. else
  1256. result:=others_saved_std_regs;
  1257. end;
  1258. function tcpuparamanager.get_saved_registers_mm(calloption: tproccalloption):tcpuregisterarray;
  1259. const
  1260. win64_saved_xmm_regs : tcpuregisterarray = (RS_XMM6,RS_XMM7,
  1261. RS_XMM8,RS_XMM9,RS_XMM10,RS_XMM11,RS_XMM12,RS_XMM13,RS_XMM14,RS_XMM15);
  1262. begin
  1263. if tcgx86_64(cg).use_ms_abi then
  1264. result:=win64_saved_xmm_regs
  1265. else
  1266. SetLength(result,0);
  1267. end;
  1268. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  1269. const
  1270. intretregs: array[0..1] of tregister = (NR_FUNCTION_RETURN_REG,NR_FUNCTION_RETURN_REG_HIGH);
  1271. mmretregs: array[0..1] of tregister = (NR_MM_RESULT_REG,NR_MM_RESULT_REG_HIGH);
  1272. mmretregs_vectorcall: array[0..3] of tregister = (NR_XMM0,NR_XMM1,NR_XMM2,NR_XMM3);
  1273. var
  1274. classes: tx64paraclasses;
  1275. i,j,
  1276. numclasses: longint;
  1277. intretregidx,
  1278. mmretregidx: longint;
  1279. retcgsize : tcgsize;
  1280. paraloc : pcgparalocation;
  1281. begin
  1282. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  1283. exit;
  1284. { Return in FPU register? -> don't use classify_argument(), because
  1285. currency and comp need special treatment here (they are integer class
  1286. when passing as parameter, but LOC_FPUREGISTER as function result) }
  1287. if result.def.typ=floatdef then
  1288. begin
  1289. paraloc:=result.add_location;
  1290. paraloc^.def:=result.def;
  1291. case tfloatdef(result.def).floattype of
  1292. s32real:
  1293. begin
  1294. paraloc^.loc:=LOC_MMREGISTER;
  1295. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMS);
  1296. paraloc^.size:=OS_F32;
  1297. end;
  1298. s64real:
  1299. begin
  1300. paraloc^.loc:=LOC_MMREGISTER;
  1301. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMD);
  1302. paraloc^.size:=OS_F64;
  1303. end;
  1304. { the first two only exist on targets with an x87, on others
  1305. they are replace by int64 }
  1306. s64currency,
  1307. s64comp,
  1308. s80real,
  1309. sc80real:
  1310. begin
  1311. paraloc^.loc:=LOC_FPUREGISTER;
  1312. paraloc^.register:=NR_FPU_RESULT_REG;
  1313. paraloc^.size:=retcgsize;
  1314. end;
  1315. else
  1316. internalerror(200405034);
  1317. end;
  1318. end
  1319. else
  1320. { Return in register }
  1321. begin
  1322. fillchar(classes,sizeof(classes),0);
  1323. numclasses:=classify_argument(p.proccalloption,result.def,nil,vs_value,result.def.size,classes,0,False);
  1324. { this would mean a memory return }
  1325. if (numclasses=0) then
  1326. begin
  1327. { we got an error before, so we just skip all the return type generation }
  1328. if result.def.typ=errordef then
  1329. exit;
  1330. internalerror(2010021502);
  1331. end;
  1332. if (numclasses > MAX_PARA_CLASSES) then
  1333. internalerror(2010021503);
  1334. intretregidx:=0;
  1335. mmretregidx:=0;
  1336. i := 0;
  1337. { We can't use a for-loop here because the treatment of the SSEUP class requires skipping over i's }
  1338. while i < numclasses do
  1339. begin
  1340. paraloc:=result.add_location;
  1341. paraloc^.def:=classes[i].def;
  1342. case classes[i].typ of
  1343. X86_64_INTEGERSI_CLASS,
  1344. X86_64_INTEGER_CLASS:
  1345. begin
  1346. paraloc^.loc:=LOC_REGISTER;
  1347. paraloc^.register:=intretregs[intretregidx];
  1348. if classes[i].typ=X86_64_INTEGER_CLASS then
  1349. begin
  1350. paraloc^.size:=OS_64;
  1351. if paraloc^.def.size<>8 then
  1352. paraloc^.def:=u64inttype;
  1353. end
  1354. else if result.intsize in [1,2,4] then
  1355. begin
  1356. { The ABI does not require sign/zero-extended function
  1357. results, but older versions of clang did so and
  1358. on Darwin current versions of clang keep doing so
  1359. for backward compatibility. On other platforms, it
  1360. doesn't and hence we don't either }
  1361. if (i=0) and
  1362. not(target_info.system in systems_darwin) and
  1363. (result.intsize in [1,2]) then
  1364. begin
  1365. paraloc^.size:=int_cgsize(result.intsize);
  1366. paraloc^.def:=cgsize_orddef(paraloc^.size);
  1367. end
  1368. else
  1369. paraloc^.size:=def_cgsize(paraloc^.def);
  1370. end
  1371. else
  1372. begin
  1373. paraloc^.size:=OS_32;
  1374. if paraloc^.def.size<>4 then
  1375. paraloc^.def:=u32inttype;
  1376. end;
  1377. setsubreg(paraloc^.register,cgsize2subreg(R_INTREGISTER,paraloc^.size));
  1378. inc(intretregidx);
  1379. end;
  1380. X86_64_SSE_CLASS,
  1381. X86_64_SSEUP_CLASS,
  1382. X86_64_SSESF_CLASS,
  1383. X86_64_SSEDF_CLASS:
  1384. begin
  1385. paraloc^.loc:=LOC_MMREGISTER;
  1386. if p.proccalloption = pocall_vectorcall then
  1387. paraloc^.register:=mmretregs_vectorcall[mmretregidx]
  1388. else
  1389. paraloc^.register:=mmretregs[mmretregidx];
  1390. case classes[i].typ of
  1391. X86_64_SSESF_CLASS:
  1392. begin
  1393. setsubreg(paraloc^.register,R_SUBMMS);
  1394. paraloc^.size:=OS_F32;
  1395. end;
  1396. X86_64_SSEDF_CLASS:
  1397. begin
  1398. setsubreg(paraloc^.register,R_SUBMMD);
  1399. paraloc^.size:=OS_F64;
  1400. end;
  1401. X86_64_SSE_CLASS:
  1402. begin
  1403. j := 1;
  1404. if not (x86_64_use_ms_abi(p.proccalloption) and (p.proccalloption <> pocall_vectorcall)) then
  1405. while i + j <= numclasses do
  1406. begin
  1407. if classes[i+j].typ <> X86_64_SSEUP_CLASS then
  1408. Break;
  1409. Inc(j);
  1410. end;
  1411. { j = MM word count }
  1412. Inc(i, j - 1);
  1413. case j of
  1414. 1:
  1415. begin
  1416. setsubreg(paraloc^.register,R_SUBQ);
  1417. paraloc^.size:=OS_M64;
  1418. end;
  1419. 2:
  1420. begin
  1421. setsubreg(paraloc^.register,R_SUBMMX);
  1422. paraloc^.size:=OS_M128;
  1423. end;
  1424. 4:
  1425. begin
  1426. setsubreg(paraloc^.register,R_SUBMMY);
  1427. paraloc^.size:=OS_M256; { Currently unsupported }
  1428. end;
  1429. 8:
  1430. begin
  1431. setsubreg(paraloc^.register,R_SUBMMZ);
  1432. paraloc^.size:=OS_M512; { Currently unsupported }
  1433. end;
  1434. else
  1435. InternalError(2018012901);
  1436. end;
  1437. paraloc^.def:=carraydef.getreusable_no_free_vector(paraloc^.def,j);
  1438. end;
  1439. else
  1440. if (x86_64_use_ms_abi(p.proccalloption) and (p.proccalloption <> pocall_vectorcall)) then
  1441. begin
  1442. setsubreg(paraloc^.register,R_SUBQ);
  1443. paraloc^.size:=OS_M64;
  1444. end
  1445. else
  1446. { Should not get here }
  1447. InternalError(2018012900);
  1448. end;
  1449. inc(mmretregidx);
  1450. end;
  1451. X86_64_X87_CLASS:
  1452. begin
  1453. { must be followed by X86_64_X87UP_CLASS and that must be
  1454. the last class }
  1455. if (i<>(numclasses-2)) or
  1456. (classes[i+1].typ<>X86_64_X87UP_CLASS) then
  1457. internalerror(2014110401);
  1458. paraloc^.loc:=LOC_FPUREGISTER;
  1459. paraloc^.register:=NR_FPU_RESULT_REG;
  1460. paraloc^.size:=OS_F80;
  1461. break;
  1462. end;
  1463. X86_64_NO_CLASS:
  1464. begin
  1465. { empty record/array }
  1466. if (i<>0) or
  1467. (numclasses<>1) then
  1468. internalerror(2010060302);
  1469. paraloc^.loc:=LOC_VOID;
  1470. paraloc^.def:=voidtype;
  1471. end;
  1472. else
  1473. internalerror(2010021504);
  1474. end;
  1475. Inc(i);
  1476. end;
  1477. end;
  1478. end;
  1479. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  1480. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  1481. var
  1482. hp : tparavarsym;
  1483. fdef,
  1484. paradef,
  1485. paralocdef : tdef;
  1486. paraloc : pcgparalocation;
  1487. subreg : tsubregister;
  1488. pushaddr : boolean;
  1489. paracgsize : tcgsize;
  1490. { loc[2] onwards are only used for _m256 under vectorcall/SysV, and
  1491. homogeneous vector aggregates and homogeneous float aggreates under
  1492. the vectorcall calling convention. [Kit] }
  1493. loc : tx64paraclasses;
  1494. needintloc,
  1495. needmmloc,
  1496. paralen,
  1497. locidx,
  1498. i,j,
  1499. varalign,
  1500. procparaalign,
  1501. paraalign : longint;
  1502. use_ms_abi : boolean;
  1503. begin
  1504. procparaalign:=get_para_align(p.proccalloption);
  1505. use_ms_abi:=x86_64_use_ms_abi(p.proccalloption);
  1506. { Register parameters are assigned from left to right }
  1507. for i:=0 to paras.count-1 do
  1508. begin
  1509. hp:=tparavarsym(paras[i]);
  1510. paradef:=hp.vardef;
  1511. paralocdef:=hp.vardef;
  1512. { in syscalls the libbase might be set as explicit paraloc }
  1513. if (vo_has_explicit_paraloc in hp.varoptions) then
  1514. if not (vo_is_syscall_lib in hp.varoptions) then
  1515. internalerror(2022010501)
  1516. else
  1517. begin
  1518. paracgsize:=def_cgsize(paradef);
  1519. hp.paraloc[side].def:=paradef;
  1520. hp.paraloc[side].size:=paracgsize;
  1521. hp.paraloc[side].intsize:=tcgsize2size[paracgsize];
  1522. hp.paraloc[side].alignment:=sizeof(pint);
  1523. continue;
  1524. end;
  1525. { on vectorcall, if a record has only one field and that field is a
  1526. single or double, it has to be handled like a single/double }
  1527. if (p.proccalloption=pocall_vectorcall) and
  1528. ((paradef.typ=recorddef) {or
  1529. is_object(paradef)}) and
  1530. tabstractrecordsymtable(tabstractrecorddef(paradef).symtable).has_single_field(fdef) and
  1531. (fdef.typ=floatdef) and
  1532. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  1533. paralocdef:=fdef;
  1534. pushaddr:=push_addr_param(hp.varspez,paralocdef,p.proccalloption);
  1535. if pushaddr then
  1536. begin
  1537. loc[0].typ:=X86_64_INTEGER_CLASS;
  1538. loc[1].typ:=X86_64_NO_CLASS;
  1539. paracgsize:=OS_ADDR;
  1540. paralen:=sizeof(pint);
  1541. paradef:=cpointerdef.getreusable_no_free(paradef);
  1542. paralocdef:=paradef;
  1543. paraalign:=procparaalign;
  1544. loc[0].def:=paralocdef;
  1545. loc[1].def:=nil;
  1546. for j:=2 to high(loc) do
  1547. begin
  1548. loc[j].typ:=X86_64_NO_CLASS;
  1549. loc[j].def:=nil;
  1550. end;
  1551. end
  1552. else
  1553. begin
  1554. getvalueparaloc(p.proccalloption,hp.varspez,paralocdef,loc);
  1555. paralen:=push_size(hp.varspez,paralocdef,p.proccalloption);
  1556. paraalign:=max(procparaalign,paradef.alignment);
  1557. if p.proccalloption = pocall_vectorcall then
  1558. begin
  1559. { TODO: Can this set of instructions be put into 'defutil' without it relying on the argument classification? [Kit] }
  1560. { The SIMD vector types have to be OS_M128 etc., not OS_128 etc.}
  1561. case is_simd_vector_type_or_homogeneous_aggregate(pocall_vectorcall,paralocdef,vs_value) of
  1562. 0:
  1563. { Not a vector or valid aggregate }
  1564. paracgsize:=def_cgsize(paralocdef);
  1565. 4:
  1566. paracgsize:=OS_F32;
  1567. 8:
  1568. paracgsize:=OS_F64;
  1569. 16:
  1570. paracgsize:=OS_M128;
  1571. 32:
  1572. paracgsize:=OS_M256;
  1573. 64:
  1574. paracgsize:=OS_M512;
  1575. else
  1576. InternalError(2018012910);
  1577. end;
  1578. end
  1579. else
  1580. paracgsize:=def_cgsize(paralocdef);
  1581. end;
  1582. { cheat for now, we should copy the value to an mm reg as well (FK) }
  1583. if varargsparas and
  1584. use_ms_abi and
  1585. (paralocdef.typ = floatdef) then
  1586. begin
  1587. loc[1].typ:=X86_64_NO_CLASS;
  1588. if paracgsize=OS_F64 then
  1589. begin
  1590. loc[0].typ:=X86_64_INTEGER_CLASS;
  1591. paracgsize:=OS_64;
  1592. paralocdef:=u64inttype;
  1593. end
  1594. else
  1595. begin
  1596. loc[0].typ:=X86_64_INTEGERSI_CLASS;
  1597. paracgsize:=OS_32;
  1598. paralocdef:=u32inttype;
  1599. end;
  1600. loc[0].def:=paralocdef;
  1601. end;
  1602. hp.paraloc[side].reset;
  1603. hp.paraloc[side].size:=paracgsize;
  1604. hp.paraloc[side].intsize:=paralen;
  1605. hp.paraloc[side].Alignment:=paraalign;
  1606. hp.paraloc[side].def:=paradef;
  1607. if paralen>0 then
  1608. begin
  1609. { Enough registers free? }
  1610. needintloc:=0;
  1611. needmmloc:=0;
  1612. for locidx:=low(loc) to high(loc) do
  1613. case loc[locidx].typ of
  1614. X86_64_INTEGER_CLASS,
  1615. X86_64_INTEGERSI_CLASS:
  1616. inc(needintloc);
  1617. { Note, do NOT include X86_64_SSEUP_CLASS because this links with
  1618. X86_64_SSE_CLASS and we only need one register, not two. [Kit] }
  1619. X86_64_SSE_CLASS,
  1620. X86_64_SSESF_CLASS,
  1621. X86_64_SSEDF_CLASS:
  1622. inc(needmmloc);
  1623. else
  1624. ;
  1625. end;
  1626. { the "-1" is because we can also use the current register }
  1627. if (use_ms_abi and
  1628. ((intparareg+needintloc-1 > high(paraintsupregs_winx64)) or
  1629. ((p.proccalloption = pocall_vectorcall) and (mmparareg+needmmloc-1 > high(parammsupregs_vectorcall))) or
  1630. ((p.proccalloption <> pocall_vectorcall) and (mmparareg+needmmloc-1 > high(parammsupregs_winx64))))) or
  1631. (not use_ms_abi and
  1632. ((intparareg+needintloc-1 > high(paraintsupregs)) or
  1633. (mmparareg+needmmloc-1 > high(parammsupregs)))) then
  1634. begin
  1635. { If there are no registers available for any
  1636. eightbyte of an argument, the whole argument is
  1637. passed on the stack. }
  1638. loc[low(loc)].typ:=X86_64_MEMORY_CLASS;
  1639. loc[low(loc)].def:=paralocdef;
  1640. for locidx:=succ(low(loc)) to high(loc) do
  1641. loc[locidx].typ:=X86_64_NO_CLASS;
  1642. end;
  1643. locidx:=0;
  1644. while (paralen>0) and
  1645. (locidx<=high(loc)) and
  1646. (loc[locidx].typ<>X86_64_NO_CLASS) do
  1647. begin
  1648. { Allocate }
  1649. case loc[locidx].typ of
  1650. X86_64_INTEGER_CLASS,
  1651. X86_64_INTEGERSI_CLASS:
  1652. begin
  1653. paraloc:=hp.paraloc[side].add_location;
  1654. paraloc^.loc:=LOC_REGISTER;
  1655. paraloc^.def:=loc[locidx].def;
  1656. if (paracgsize=OS_NO) or ((locidx<high(loc)) and (loc[locidx+1].typ<>X86_64_NO_CLASS)) then
  1657. begin
  1658. if loc[locidx].typ=X86_64_INTEGER_CLASS then
  1659. begin
  1660. paraloc^.size:=OS_INT;
  1661. paraloc^.def:=u64inttype;
  1662. subreg:=R_SUBWHOLE;
  1663. end
  1664. else
  1665. begin
  1666. paraloc^.size:=OS_32;
  1667. paraloc^.def:=u32inttype;
  1668. subreg:=R_SUBD;
  1669. end;
  1670. end
  1671. else
  1672. begin
  1673. { some compilers sign/zero-extend on the callerside,
  1674. others don't. To be compatible with both, FPC
  1675. extends on the callerside, and assumes no
  1676. extension has been performed on the calleeside.
  1677. This is less efficient, but the alternative is
  1678. occasional crashes when calling code generated
  1679. by certain other compilers, or being called from
  1680. code generated by other compilers.
  1681. Exception: Darwin, since everyone there needs to
  1682. be compatible with the system compiler clang
  1683. (which extends on the caller side).
  1684. Not for LLVM, since there the zero/signext
  1685. attributes by definition only apply to the
  1686. caller side }
  1687. {$ifndef LLVM}
  1688. if not(target_info.system in systems_darwin) and
  1689. (side=calleeside) and
  1690. (hp.paraloc[side].intsize in [1,2]) then
  1691. begin
  1692. paraloc^.def:=hp.paraloc[side].def
  1693. end;
  1694. {$endif not LLVM}
  1695. paraloc^.size:=def_cgsize(paraloc^.def);
  1696. { s64comp/s64currency is pushed in an int register }
  1697. if paraloc^.size=OS_C64 then
  1698. begin
  1699. paraloc^.size:=OS_64;
  1700. paraloc^.def:=u64inttype;
  1701. end;
  1702. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  1703. end;
  1704. { winx64 uses different registers }
  1705. if use_ms_abi then
  1706. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  1707. else
  1708. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  1709. { matching mm register must be skipped }
  1710. if use_ms_abi then
  1711. inc(mmparareg);
  1712. inc(intparareg);
  1713. dec(paralen,tcgsize2size[paraloc^.size]);
  1714. end;
  1715. X86_64_SSE_CLASS,
  1716. X86_64_SSESF_CLASS,
  1717. X86_64_SSEDF_CLASS:
  1718. begin
  1719. paraloc:=hp.paraloc[side].add_location;
  1720. paraloc^.loc:=LOC_MMREGISTER;
  1721. paraloc^.def:=loc[locidx].def;
  1722. case loc[locidx].typ of
  1723. X86_64_SSESF_CLASS:
  1724. begin
  1725. subreg:=R_SUBMMS;
  1726. paraloc^.size:=OS_F32;
  1727. end;
  1728. X86_64_SSEDF_CLASS:
  1729. begin
  1730. subreg:=R_SUBMMD;
  1731. paraloc^.size:=OS_F64;
  1732. end;
  1733. X86_64_SSE_CLASS:
  1734. begin
  1735. subreg:=R_SUBQ;
  1736. paraloc^.size:=OS_M64;
  1737. j := 1;
  1738. if not (use_ms_abi and (p.proccalloption <> pocall_vectorcall)) then
  1739. while locidx + j <= high(loc) do
  1740. begin
  1741. if loc[locidx+j].typ <> X86_64_SSEUP_CLASS then
  1742. Break;
  1743. Inc(j);
  1744. end;
  1745. { j = MM word count }
  1746. Inc(locidx, j - 1);
  1747. case j of
  1748. 1:
  1749. begin
  1750. subreg:=R_SUBQ;
  1751. paraloc^.size:=OS_M64;
  1752. end;
  1753. 2:
  1754. begin
  1755. subreg:=R_SUBMMX;
  1756. paraloc^.size:=OS_M128;
  1757. end;
  1758. 4:
  1759. begin
  1760. subreg:=R_SUBMMY;
  1761. paraloc^.size:=OS_M256; { Currently unsupported }
  1762. end;
  1763. 8:
  1764. begin
  1765. subreg:=R_SUBMMZ;
  1766. paraloc^.size:=OS_M512; { Currently unsupported }
  1767. end;
  1768. else
  1769. InternalError(2018012903);
  1770. end;
  1771. paraloc^.def:=carraydef.getreusable_no_free_vector(paraloc^.def,j);
  1772. end;
  1773. else
  1774. if (use_ms_abi and (p.proccalloption <> pocall_vectorcall)) then
  1775. begin
  1776. subreg:=R_SUBQ;
  1777. paraloc^.size:=OS_M64;
  1778. end
  1779. else
  1780. { Should not get here }
  1781. InternalError(2018012902);
  1782. end;
  1783. { winx64 uses different registers }
  1784. if use_ms_abi then
  1785. begin
  1786. if p.proccalloption = pocall_vectorcall then
  1787. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_vectorcall[mmparareg],subreg)
  1788. else
  1789. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg);
  1790. end
  1791. else
  1792. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  1793. { matching int register must be skipped }
  1794. if use_ms_abi then
  1795. inc(intparareg);
  1796. inc(mmparareg);
  1797. dec(paralen,tcgsize2size[paraloc^.size]);
  1798. end;
  1799. X86_64_MEMORY_CLASS :
  1800. begin
  1801. paraloc:=hp.paraloc[side].add_location;
  1802. paraloc^.loc:=LOC_REFERENCE;
  1803. paraloc^.def:=loc[locidx].def;
  1804. { s64comp/s64currency are passed as integer types
  1805. (important for LLVM here) }
  1806. if paracgsize=OS_C64 then
  1807. begin
  1808. paraloc^.size:=OS_64;
  1809. paraloc^.def:=u64inttype;
  1810. end;
  1811. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  1812. however, since int_cgsize is called in many places in the
  1813. compiler where only a few can already handle OS_128, fixing it
  1814. properly is out of the question to release 2.2.0 in time. (DM)}
  1815. if paracgsize=OS_128 then
  1816. if paralen=8 then
  1817. paraloc^.size:=OS_64
  1818. else if paralen=16 then
  1819. paraloc^.size:=OS_128
  1820. else
  1821. internalerror(200707143)
  1822. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  1823. paraloc^.size:=int_float_cgsize(paralen)
  1824. else
  1825. paraloc^.size:=int_cgsize(paralen);
  1826. if side=callerside then
  1827. paraloc^.reference.index:=NR_STACK_POINTER_REG
  1828. else
  1829. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  1830. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  1831. paraloc^.reference.offset:=align(parasize,varalign);
  1832. parasize:=align(parasize+paralen,varalign);
  1833. paralen:=0;
  1834. end;
  1835. else
  1836. internalerror(2010053113);
  1837. end;
  1838. inc(locidx);
  1839. end;
  1840. end
  1841. else
  1842. begin
  1843. paraloc:=hp.paraloc[side].add_location;
  1844. paraloc^.loc:=LOC_VOID;
  1845. paraloc^.def:=paralocdef;
  1846. end;
  1847. end;
  1848. { Register parameters are assigned from left-to-right, but the
  1849. offsets on the stack are right-to-left. There is no need
  1850. to reverse the offset, only adapt the calleeside with the
  1851. start offset of the first param on the stack }
  1852. if side=calleeside then
  1853. begin
  1854. for i:=0 to paras.count-1 do
  1855. begin
  1856. hp:=tparavarsym(paras[i]);
  1857. paraloc:=hp.paraloc[side].location;
  1858. while paraloc<>nil do
  1859. begin
  1860. with paraloc^ do
  1861. if (loc=LOC_REFERENCE) then
  1862. inc(reference.offset,target_info.first_parm_offset);
  1863. paraloc:=paraloc^.next;
  1864. end;
  1865. end;
  1866. end;
  1867. end;
  1868. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  1869. var
  1870. intparareg,mmparareg,
  1871. parasize : longint;
  1872. begin
  1873. intparareg:=0;
  1874. mmparareg:=0;
  1875. if x86_64_use_ms_abi(p.proccalloption) then
  1876. parasize:=4*8
  1877. else
  1878. parasize:=0;
  1879. { calculate the registers for the normal parameters }
  1880. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  1881. { append the varargs }
  1882. if assigned(varargspara) then
  1883. begin
  1884. if side=callerside then
  1885. create_paraloc_info_intern(p,side,varargspara,intparareg,mmparareg,parasize,true)
  1886. else
  1887. internalerror(2019021917);
  1888. { store used no. of SSE registers, that needs to be passed in %AL }
  1889. varargspara.mmregsused:=mmparareg;
  1890. end;
  1891. create_funcretloc_info(p,side);
  1892. result:=parasize;
  1893. end;
  1894. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  1895. var
  1896. intparareg,mmparareg,
  1897. parasize : longint;
  1898. begin
  1899. intparareg:=0;
  1900. mmparareg:=0;
  1901. if x86_64_use_ms_abi(p.proccalloption) then
  1902. parasize:=4*8
  1903. else
  1904. parasize:=0;
  1905. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  1906. { Create Function result paraloc }
  1907. create_funcretloc_info(p,side);
  1908. { We need to return the size allocated on the stack }
  1909. result:=parasize;
  1910. end;
  1911. begin
  1912. paramanager:=tcpuparamanager.create;
  1913. end.