cpupara.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generates the argument location information for x86-64 target
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cpupara;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cpubase,cgbase,cgutils,
  23. symconst,symtype,symsym,symdef,
  24. aasmtai,aasmdata,
  25. parabase,paramgr;
  26. type
  27. tcpuparamanager = class(tparamanager)
  28. private
  29. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  30. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  31. public
  32. function param_use_paraloc(const cgpara:tcgpara):boolean;override;
  33. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  34. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  35. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;override;
  36. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;override;
  37. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;override;
  38. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  39. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  40. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  41. end;
  42. implementation
  43. uses
  44. cutils,verbose,
  45. systems,
  46. defutil,
  47. symtable;
  48. 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(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint; forward;
  254. function init_aggregate_classification(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 (target_info.system=system_x86_64_win64) 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(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(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(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(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(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(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(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(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(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(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(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(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(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. else
  647. internalerror(2010021405);
  648. end;
  649. end;
  650. procedure getvalueparaloc(varspez:tvarspez;def:tdef;var loc1,loc2:tx64paraclass);
  651. var
  652. size: aint;
  653. i: longint;
  654. classes: tx64paraclasses;
  655. numclasses: longint;
  656. begin
  657. { init the classes array, because even if classify_argument inits only
  658. one element we copy both to loc1/loc2 in case "1" is returned }
  659. for i:=low(classes) to high(classes) do
  660. begin
  661. classes[i].typ:=X86_64_NO_CLASS;
  662. classes[i].def:=nil;
  663. end;
  664. { def.size internalerrors for open arrays and dynamic arrays, since
  665. their size cannot be determined at compile-time.
  666. classify_argument does not look at the realsize argument for arrays
  667. cases, but we obviously do have to pass something... }
  668. if is_special_array(def) then
  669. size:=-1
  670. else
  671. size:=def.size;
  672. numclasses:=classify_argument(def,varspez,size,classes,0);
  673. case numclasses of
  674. 0:
  675. begin
  676. loc1.typ:=X86_64_MEMORY_CLASS;
  677. loc1.def:=def;
  678. loc2.typ:=X86_64_NO_CLASS;
  679. end;
  680. 1,2:
  681. begin
  682. { If the class is X87, X87UP or COMPLEX_X87, it is passed in memory }
  683. if classes[0].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  684. classes[0].typ:=X86_64_MEMORY_CLASS;
  685. if classes[1].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  686. classes[1].typ:=X86_64_MEMORY_CLASS;
  687. loc1:=classes[0];
  688. loc2:=classes[1];
  689. end
  690. else
  691. { 4 can only happen for _m256 vectors, not yet supported }
  692. internalerror(2010021501);
  693. end;
  694. end;
  695. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  696. var
  697. classes: tx64paraclasses;
  698. numclasses: longint;
  699. begin
  700. if handle_common_ret_in_param(def,pd,result) then
  701. exit;
  702. fillchar(classes,sizeof(classes),0);
  703. case def.typ of
  704. { for records it depends on their contents and size }
  705. recorddef,
  706. { make sure we handle 'procedure of object' correctly }
  707. procvardef:
  708. begin
  709. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  710. result:=(numclasses=0);
  711. end;
  712. else
  713. result:=inherited ret_in_param(def,pd);
  714. end;
  715. end;
  716. function tcpuparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  717. var
  718. paraloc : pcgparalocation;
  719. begin
  720. if not assigned(cgpara.location) then
  721. internalerror(200410102);
  722. result:=true;
  723. { All locations are LOC_REFERENCE }
  724. paraloc:=cgpara.location;
  725. while assigned(paraloc) do
  726. begin
  727. if (paraloc^.loc<>LOC_REFERENCE) then
  728. begin
  729. result:=false;
  730. exit;
  731. end;
  732. paraloc:=paraloc^.next;
  733. end;
  734. end;
  735. { true if a parameter is too large to copy and only the address is pushed }
  736. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  737. var
  738. classes: tx64paraclasses;
  739. numclasses: longint;
  740. begin
  741. fillchar(classes,sizeof(classes),0);
  742. result:=false;
  743. { var,out,constref always require address }
  744. if varspez in [vs_var,vs_out,vs_constref] then
  745. begin
  746. result:=true;
  747. exit;
  748. end;
  749. { Only vs_const, vs_value here }
  750. case def.typ of
  751. formaldef :
  752. result:=true;
  753. recorddef :
  754. begin
  755. { MetroWerks Pascal: const records always passed by reference
  756. (for Mac OS X interfaces) }
  757. if (calloption=pocall_mwpascal) and
  758. (varspez=vs_const) then
  759. result:=true
  760. { Win ABI depends on size to pass it in a register or not }
  761. else if (target_info.system=system_x86_64_win64) then
  762. result:=not aggregate_in_registers_win64(varspez,def.size)
  763. { pass constant parameters that would be passed via memory by
  764. reference for non-cdecl/cppdecl, and make sure that the tmethod
  765. record (size=16) is passed the same way as a complex procvar }
  766. else if ((varspez=vs_const) and
  767. not(calloption in cdecl_pocalls)) or
  768. (def.size=16) then
  769. begin
  770. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  771. result:=numclasses=0;
  772. end
  773. else
  774. { SysV ABI always passes it as value parameter }
  775. result:=false;
  776. end;
  777. arraydef :
  778. begin
  779. { cdecl array of const need to be ignored and therefor be puhsed
  780. as value parameter with length 0 }
  781. if ((calloption in cdecl_pocalls) and
  782. is_array_of_const(def)) or
  783. is_dynamic_array(def) then
  784. result:=false
  785. else
  786. { pass all arrays by reference to be compatible with C (passing
  787. an array by value (= copying it on the stack) does not exist,
  788. because an array is the same as a pointer there }
  789. result:=true
  790. end;
  791. objectdef :
  792. begin
  793. { don't treat objects like records, because we only know wheter
  794. or not they'll have a VMT after the entire object is parsed
  795. -> if they are used as function result from one of their own
  796. methods, their size can still change after we've determined
  797. whether this function result should be returned by reference or
  798. by value }
  799. if is_object(def) then
  800. result:=true;
  801. end;
  802. variantdef,
  803. stringdef,
  804. procvardef,
  805. setdef :
  806. begin
  807. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  808. result:=numclasses=0;
  809. end;
  810. end;
  811. end;
  812. function tcpuparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  813. begin
  814. if target_info.system=system_x86_64_win64 then
  815. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  816. else
  817. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  818. end;
  819. function tcpuparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  820. begin
  821. if target_info.system=system_x86_64_win64 then
  822. result:=[RS_XMM0..RS_XMM5]
  823. else
  824. result:=[RS_XMM0..RS_XMM15];
  825. end;
  826. function tcpuparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  827. begin
  828. result:=[RS_ST0..RS_ST7];
  829. end;
  830. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  831. const
  832. intretregs: array[0..1] of tregister = (NR_FUNCTION_RETURN_REG,NR_FUNCTION_RETURN_REG_HIGH);
  833. mmretregs: array[0..1] of tregister = (NR_MM_RESULT_REG,NR_MM_RESULT_REG_HIGH);
  834. var
  835. classes: tx64paraclasses;
  836. i,
  837. numclasses: longint;
  838. intretregidx,
  839. mmretregidx: longint;
  840. retcgsize : tcgsize;
  841. paraloc : pcgparalocation;
  842. begin
  843. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  844. exit;
  845. { Return in FPU register? -> don't use classify_argument(), because
  846. currency and comp need special treatment here (they are integer class
  847. when passing as parameter, but LOC_FPUREGISTER as function result) }
  848. if result.def.typ=floatdef then
  849. begin
  850. paraloc:=result.add_location;
  851. paraloc^.def:=result.def;
  852. case tfloatdef(result.def).floattype of
  853. s32real:
  854. begin
  855. paraloc^.loc:=LOC_MMREGISTER;
  856. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMS);
  857. paraloc^.size:=OS_F32;
  858. end;
  859. s64real:
  860. begin
  861. paraloc^.loc:=LOC_MMREGISTER;
  862. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMD);
  863. paraloc^.size:=OS_F64;
  864. end;
  865. { the first two only exist on targets with an x87, on others
  866. they are replace by int64 }
  867. s64currency,
  868. s64comp,
  869. s80real,
  870. sc80real:
  871. begin
  872. paraloc^.loc:=LOC_FPUREGISTER;
  873. paraloc^.register:=NR_FPU_RESULT_REG;
  874. paraloc^.size:=retcgsize;
  875. end;
  876. else
  877. internalerror(200405034);
  878. end;
  879. end
  880. else
  881. { Return in register }
  882. begin
  883. fillchar(classes,sizeof(classes),0);
  884. numclasses:=classify_argument(result.def,vs_value,result.def.size,classes,0);
  885. { this would mean a memory return }
  886. if (numclasses=0) then
  887. internalerror(2010021502);
  888. { this would mean an _m256 vector (valid, but not yet supported) }
  889. if (numclasses>2) then
  890. internalerror(2010021503);
  891. intretregidx:=0;
  892. mmretregidx:=0;
  893. for i:=0 to numclasses-1 do
  894. begin
  895. paraloc:=result.add_location;
  896. paraloc^.def:=classes[i].def;
  897. case classes[i].typ of
  898. X86_64_INTEGERSI_CLASS,
  899. X86_64_INTEGER_CLASS:
  900. begin
  901. paraloc^.loc:=LOC_REGISTER;
  902. paraloc^.register:=intretregs[intretregidx];
  903. if classes[i].typ=X86_64_INTEGER_CLASS then
  904. begin
  905. paraloc^.size:=OS_64;
  906. if paraloc^.def.size<>8 then
  907. paraloc^.def:=u64inttype;
  908. end
  909. else if result.intsize in [1,2,4] then
  910. begin
  911. paraloc^.size:=def_cgsize(paraloc^.def);
  912. end
  913. else
  914. begin
  915. paraloc^.size:=OS_32;
  916. if paraloc^.def.size<>4 then
  917. paraloc^.def:=u32inttype;
  918. end;
  919. setsubreg(paraloc^.register,cgsize2subreg(R_INTREGISTER,paraloc^.size));
  920. inc(intretregidx);
  921. end;
  922. X86_64_SSE_CLASS,
  923. X86_64_SSEUP_CLASS,
  924. X86_64_SSESF_CLASS,
  925. X86_64_SSEDF_CLASS:
  926. begin
  927. paraloc^.loc:=LOC_MMREGISTER;
  928. paraloc^.register:=mmretregs[mmretregidx];
  929. case classes[i].typ of
  930. X86_64_SSESF_CLASS:
  931. begin
  932. setsubreg(paraloc^.register,R_SUBMMS);
  933. paraloc^.size:=OS_F32;
  934. end;
  935. X86_64_SSEDF_CLASS:
  936. begin
  937. setsubreg(paraloc^.register,R_SUBMMD);
  938. paraloc^.size:=OS_F64;
  939. end;
  940. else
  941. begin
  942. setsubreg(paraloc^.register,R_SUBQ);
  943. paraloc^.size:=OS_M64;
  944. end;
  945. end;
  946. inc(mmretregidx);
  947. end;
  948. X86_64_X87_CLASS:
  949. begin
  950. { must be followed by X86_64_X87UP_CLASS and that must be
  951. the last class }
  952. if (i<>(numclasses-2)) or
  953. (classes[i+1].typ<>X86_64_X87UP_CLASS) then
  954. internalerror(2014110401);
  955. paraloc^.loc:=LOC_FPUREGISTER;
  956. paraloc^.register:=NR_FPU_RESULT_REG;
  957. paraloc^.size:=OS_F80;
  958. break;
  959. end;
  960. X86_64_NO_CLASS:
  961. begin
  962. { empty record/array }
  963. if (i<>0) or
  964. (numclasses<>1) then
  965. internalerror(2010060302);
  966. paraloc^.loc:=LOC_VOID;
  967. paraloc^.def:=voidtype;
  968. end;
  969. else
  970. internalerror(2010021504);
  971. end;
  972. end;
  973. end;
  974. end;
  975. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  976. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  977. var
  978. hp : tparavarsym;
  979. fdef,
  980. paradef : tdef;
  981. paraloc : pcgparalocation;
  982. subreg : tsubregister;
  983. pushaddr : boolean;
  984. paracgsize : tcgsize;
  985. loc : array[1..2] of tx64paraclass;
  986. needintloc,
  987. needmmloc,
  988. paralen,
  989. locidx,
  990. i,
  991. varalign,
  992. paraalign : longint;
  993. begin
  994. paraalign:=get_para_align(p.proccalloption);
  995. { Register parameters are assigned from left to right }
  996. for i:=0 to paras.count-1 do
  997. begin
  998. hp:=tparavarsym(paras[i]);
  999. paradef:=hp.vardef;
  1000. { on win64, if a record has only one field and that field is a
  1001. single or double, it has to be handled like a single/double }
  1002. if (target_info.system=system_x86_64_win64) and
  1003. ((paradef.typ=recorddef) {or
  1004. is_object(paradef)}) and
  1005. tabstractrecordsymtable(tabstractrecorddef(paradef).symtable).has_single_field(fdef) and
  1006. (fdef.typ=floatdef) and
  1007. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  1008. paradef:=fdef;
  1009. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  1010. if pushaddr then
  1011. begin
  1012. loc[1].typ:=X86_64_INTEGER_CLASS;
  1013. loc[2].typ:=X86_64_NO_CLASS;
  1014. paracgsize:=OS_ADDR;
  1015. paralen:=sizeof(pint);
  1016. paradef:=cpointerdef.getreusable_no_free(paradef);
  1017. loc[1].def:=paradef;
  1018. end
  1019. else
  1020. begin
  1021. getvalueparaloc(hp.varspez,paradef,loc[1],loc[2]);
  1022. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  1023. paracgsize:=def_cgsize(paradef);
  1024. end;
  1025. { cheat for now, we should copy the value to an mm reg as well (FK) }
  1026. if varargsparas and
  1027. (target_info.system = system_x86_64_win64) and
  1028. (paradef.typ = floatdef) then
  1029. begin
  1030. loc[2].typ:=X86_64_NO_CLASS;
  1031. if paracgsize=OS_F64 then
  1032. begin
  1033. loc[1].typ:=X86_64_INTEGER_CLASS;
  1034. paracgsize:=OS_64;
  1035. paradef:=u64inttype;
  1036. end
  1037. else
  1038. begin
  1039. loc[1].typ:=X86_64_INTEGERSI_CLASS;
  1040. paracgsize:=OS_32;
  1041. paradef:=u32inttype;
  1042. end;
  1043. loc[1].def:=paradef;
  1044. end;
  1045. hp.paraloc[side].reset;
  1046. hp.paraloc[side].size:=paracgsize;
  1047. hp.paraloc[side].intsize:=paralen;
  1048. hp.paraloc[side].Alignment:=paraalign;
  1049. hp.paraloc[side].def:=paradef;
  1050. if paralen>0 then
  1051. begin
  1052. { Enough registers free? }
  1053. needintloc:=0;
  1054. needmmloc:=0;
  1055. for locidx:=low(loc) to high(loc) do
  1056. case loc[locidx].typ of
  1057. X86_64_INTEGER_CLASS,
  1058. X86_64_INTEGERSI_CLASS:
  1059. inc(needintloc);
  1060. X86_64_SSE_CLASS,
  1061. X86_64_SSESF_CLASS,
  1062. X86_64_SSEDF_CLASS,
  1063. X86_64_SSEUP_CLASS:
  1064. inc(needmmloc);
  1065. end;
  1066. { the "-1" is because we can also use the current register }
  1067. if ((target_info.system=system_x86_64_win64) and
  1068. ((intparareg+needintloc-1 > high(paraintsupregs_winx64)) or
  1069. (mmparareg+needmmloc-1 > high(parammsupregs_winx64)))) or
  1070. ((target_info.system<>system_x86_64_win64) and
  1071. ((intparareg+needintloc-1 > high(paraintsupregs)) or
  1072. (mmparareg+needmmloc-1 > high(parammsupregs)))) then
  1073. begin
  1074. { If there are no registers available for any
  1075. eightbyte of an argument, the whole argument is
  1076. passed on the stack. }
  1077. loc[low(loc)].typ:=X86_64_MEMORY_CLASS;
  1078. loc[low(loc)].def:=paradef;
  1079. for locidx:=succ(low(loc)) to high(loc) do
  1080. loc[locidx].typ:=X86_64_NO_CLASS;
  1081. end;
  1082. locidx:=1;
  1083. while (paralen>0) and
  1084. (locidx<=2) and
  1085. (loc[locidx].typ<>X86_64_NO_CLASS) do
  1086. begin
  1087. { Allocate }
  1088. case loc[locidx].typ of
  1089. X86_64_INTEGER_CLASS,
  1090. X86_64_INTEGERSI_CLASS:
  1091. begin
  1092. paraloc:=hp.paraloc[side].add_location;
  1093. paraloc^.loc:=LOC_REGISTER;
  1094. paraloc^.def:=loc[locidx].def;
  1095. if (paracgsize=OS_NO) or (loc[2].typ<>X86_64_NO_CLASS) then
  1096. begin
  1097. if loc[locidx].typ=X86_64_INTEGER_CLASS then
  1098. begin
  1099. paraloc^.size:=OS_INT;
  1100. paraloc^.def:=u64inttype;
  1101. subreg:=R_SUBWHOLE;
  1102. end
  1103. else
  1104. begin
  1105. paraloc^.size:=OS_32;
  1106. paraloc^.def:=u32inttype;
  1107. subreg:=R_SUBD;
  1108. end;
  1109. end
  1110. else
  1111. begin
  1112. paraloc^.size:=def_cgsize(paraloc^.def);
  1113. { s64comp is pushed in an int register }
  1114. if paraloc^.size=OS_C64 then
  1115. begin
  1116. paraloc^.size:=OS_64;
  1117. paraloc^.def:=u64inttype;
  1118. end;
  1119. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  1120. end;
  1121. { winx64 uses different registers }
  1122. if target_info.system=system_x86_64_win64 then
  1123. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  1124. else
  1125. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  1126. { matching mm register must be skipped }
  1127. if target_info.system=system_x86_64_win64 then
  1128. inc(mmparareg);
  1129. inc(intparareg);
  1130. dec(paralen,tcgsize2size[paraloc^.size]);
  1131. end;
  1132. X86_64_SSE_CLASS,
  1133. X86_64_SSESF_CLASS,
  1134. X86_64_SSEDF_CLASS,
  1135. X86_64_SSEUP_CLASS:
  1136. begin
  1137. paraloc:=hp.paraloc[side].add_location;
  1138. paraloc^.loc:=LOC_MMREGISTER;
  1139. paraloc^.def:=loc[locidx].def;
  1140. case loc[locidx].typ of
  1141. X86_64_SSESF_CLASS:
  1142. begin
  1143. subreg:=R_SUBMMS;
  1144. paraloc^.size:=OS_F32;
  1145. end;
  1146. X86_64_SSEDF_CLASS:
  1147. begin
  1148. subreg:=R_SUBMMD;
  1149. paraloc^.size:=OS_F64;
  1150. end;
  1151. else
  1152. begin
  1153. subreg:=R_SUBQ;
  1154. paraloc^.size:=OS_M64;
  1155. end;
  1156. end;
  1157. { winx64 uses different registers }
  1158. if target_info.system=system_x86_64_win64 then
  1159. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg)
  1160. else
  1161. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  1162. { matching int register must be skipped }
  1163. if target_info.system=system_x86_64_win64 then
  1164. inc(intparareg);
  1165. inc(mmparareg);
  1166. dec(paralen,tcgsize2size[paraloc^.size]);
  1167. end;
  1168. X86_64_MEMORY_CLASS :
  1169. begin
  1170. paraloc:=hp.paraloc[side].add_location;
  1171. paraloc^.loc:=LOC_REFERENCE;
  1172. paraloc^.def:=loc[locidx].def;
  1173. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  1174. however, since int_cgsize is called in many places in the
  1175. compiler where only a few can already handle OS_128, fixing it
  1176. properly is out of the question to release 2.2.0 in time. (DM)}
  1177. if paracgsize=OS_128 then
  1178. if paralen=8 then
  1179. paraloc^.size:=OS_64
  1180. else if paralen=16 then
  1181. paraloc^.size:=OS_128
  1182. else
  1183. internalerror(200707143)
  1184. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  1185. paraloc^.size:=int_float_cgsize(paralen)
  1186. else
  1187. paraloc^.size:=int_cgsize(paralen);
  1188. if side=callerside then
  1189. paraloc^.reference.index:=NR_STACK_POINTER_REG
  1190. else
  1191. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  1192. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  1193. paraloc^.reference.offset:=parasize;
  1194. parasize:=align(parasize+paralen,varalign);
  1195. paralen:=0;
  1196. end;
  1197. else
  1198. internalerror(2010053113);
  1199. end;
  1200. inc(locidx);
  1201. end;
  1202. end
  1203. else
  1204. begin
  1205. paraloc:=hp.paraloc[side].add_location;
  1206. paraloc^.loc:=LOC_VOID;
  1207. paraloc^.def:=paradef;
  1208. end;
  1209. end;
  1210. { Register parameters are assigned from left-to-right, but the
  1211. offsets on the stack are right-to-left. There is no need
  1212. to reverse the offset, only adapt the calleeside with the
  1213. start offset of the first param on the stack }
  1214. if side=calleeside then
  1215. begin
  1216. for i:=0 to paras.count-1 do
  1217. begin
  1218. hp:=tparavarsym(paras[i]);
  1219. paraloc:=hp.paraloc[side].location;
  1220. while paraloc<>nil do
  1221. begin
  1222. with paraloc^ do
  1223. if (loc=LOC_REFERENCE) then
  1224. inc(reference.offset,target_info.first_parm_offset);
  1225. paraloc:=paraloc^.next;
  1226. end;
  1227. end;
  1228. end;
  1229. end;
  1230. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  1231. var
  1232. intparareg,mmparareg,
  1233. parasize : longint;
  1234. begin
  1235. intparareg:=0;
  1236. mmparareg:=0;
  1237. if target_info.system=system_x86_64_win64 then
  1238. parasize:=4*8
  1239. else
  1240. parasize:=0;
  1241. { calculate the registers for the normal parameters }
  1242. create_paraloc_info_intern(p,callerside,p.paras,intparareg,mmparareg,parasize,false);
  1243. { append the varargs }
  1244. create_paraloc_info_intern(p,callerside,varargspara,intparareg,mmparareg,parasize,true);
  1245. { store used no. of SSE registers, that needs to be passed in %AL }
  1246. varargspara.mmregsused:=mmparareg;
  1247. result:=parasize;
  1248. end;
  1249. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  1250. var
  1251. intparareg,mmparareg,
  1252. parasize : longint;
  1253. begin
  1254. intparareg:=0;
  1255. mmparareg:=0;
  1256. if target_info.system=system_x86_64_win64 then
  1257. parasize:=4*8
  1258. else
  1259. parasize:=0;
  1260. create_paraloc_info_intern(p,side,p.paras,intparareg,mmparareg,parasize,false);
  1261. { Create Function result paraloc }
  1262. create_funcretloc_info(p,side);
  1263. { We need to return the size allocated on the stack }
  1264. result:=parasize;
  1265. end;
  1266. begin
  1267. paramanager:=tcpuparamanager.create;
  1268. end.