2
0

cpupara.pas 79 KB

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