cpupara.pas 53 KB

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