cpupara.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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:=getarraydef(s32floattype,2)
  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:=getarraydef(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. { FIXME: in case a record contains empty padding space, e.g. a
  371. "single" field followed by a "double", then we have a problem
  372. because the cgpara helpers cannot figure out that they should
  373. skip 4 bytes after storing the single (LOC_MMREGISTER with size
  374. OS_F32) to memory before storing the double -> for now scale
  375. such locations always up to 64 bits, although this loads/stores
  376. some superfluous data }
  377. { 1) the first part is 32 bit while there is still a second part }
  378. if (classes[1].typ<>X86_64_NO_CLASS) then
  379. case classes[0].typ of
  380. X86_64_INTEGERSI_CLASS:
  381. begin
  382. classes[0].typ:=X86_64_INTEGER_CLASS;
  383. classes[0].def:=s64inttype;
  384. end;
  385. X86_64_SSESF_CLASS:
  386. begin
  387. classes[0].typ:=X86_64_SSE_CLASS;
  388. classes[0].def:=getarraydef(s32floattype,2);
  389. end;
  390. end;
  391. { 2) the second part is 32 bit, but the total size is > 12 bytes }
  392. if (def.size>12) then
  393. case classes[1].typ of
  394. X86_64_INTEGERSI_CLASS:
  395. begin
  396. classes[1].typ:=X86_64_INTEGER_CLASS;
  397. classes[1].def:=s64inttype;
  398. end;
  399. X86_64_SSESF_CLASS:
  400. begin
  401. classes[1].typ:=X86_64_SSE_CLASS;
  402. classes[1].def:=getarraydef(s32floattype,2);
  403. end;
  404. end;
  405. result:=words;
  406. end;
  407. function classify_record(def: tdef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  408. var
  409. vs: tfieldvarsym;
  410. size,
  411. new_byte_offset: aint;
  412. i,
  413. words,
  414. num: longint;
  415. checkalignment: boolean;
  416. begin
  417. result:=init_aggregate_classification(def,varspez,byte_offset,words,classes);
  418. if (words=0) then
  419. exit;
  420. (* Merge the fields of the structure. *)
  421. for i:=0 to tabstractrecorddef(def).symtable.symlist.count-1 do
  422. begin
  423. if tsym(tabstractrecorddef(def).symtable.symlist[i]).typ<>fieldvarsym then
  424. continue;
  425. vs:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[i]);
  426. num:=-1;
  427. checkalignment:=true;
  428. if not tabstractrecordsymtable(tabstractrecorddef(def).symtable).is_packed then
  429. begin
  430. new_byte_offset:=byte_offset+vs.fieldoffset;
  431. size:=vs.vardef.size;
  432. end
  433. else
  434. begin
  435. new_byte_offset:=byte_offset+vs.fieldoffset div 8;
  436. if (vs.vardef.typ in [orddef,enumdef]) then
  437. begin
  438. { calculate the number of bytes spanned by
  439. this bitpacked field }
  440. size:=((vs.fieldoffset+vs.vardef.packedbitsize+7) div 8)-(vs.fieldoffset div 8);
  441. { our bitpacked fields are interpreted as always being
  442. aligned, because unlike in C we don't have char:1, int:1
  443. etc (so everything is basically a char:x) }
  444. checkalignment:=false;
  445. end
  446. else
  447. size:=vs.vardef.size;
  448. end;
  449. { If [..] an object [..] contains unaligned fields, it has class
  450. MEMORY }
  451. if checkalignment and
  452. (align(new_byte_offset,vs.vardef.structalignment)<>new_byte_offset) then
  453. begin
  454. result:=0;
  455. exit;
  456. end;
  457. num:=classify_aggregate_element(vs.vardef,varspez,size,classes,new_byte_offset);
  458. if (num=0) then
  459. exit(0);
  460. end;
  461. result:=finalize_aggregate_classification(def,words,classes);
  462. end;
  463. function classify_normal_array(def: tarraydef; varspez: tvarspez; var classes: tx64paraclasses; byte_offset: aint): longint;
  464. var
  465. i, elecount: aword;
  466. size,
  467. elesize,
  468. new_byte_offset,
  469. bitoffset: aint;
  470. words,
  471. num: longint;
  472. isbitpacked: boolean;
  473. begin
  474. size:=0;
  475. bitoffset:=0;
  476. result:=init_aggregate_classification(def,varspez,byte_offset,words,classes);
  477. if (words=0) then
  478. exit;
  479. isbitpacked:=is_packed_array(def);
  480. if not isbitpacked then
  481. begin
  482. elesize:=def.elesize;
  483. size:=elesize;
  484. end
  485. else
  486. begin
  487. elesize:=def.elepackedbitsize;
  488. bitoffset:=0;
  489. end;
  490. (* Merge the elements of the array. *)
  491. i:=0;
  492. elecount:=def.elecount;
  493. repeat
  494. if not isbitpacked then
  495. begin
  496. { size does not change }
  497. new_byte_offset:=byte_offset+i*elesize;
  498. { If [..] an object [..] contains unaligned fields, it has class
  499. MEMORY }
  500. if align(new_byte_offset,def.alignment)<>new_byte_offset then
  501. begin
  502. result:=0;
  503. exit;
  504. end;
  505. end
  506. else
  507. begin
  508. { calculate the number of bytes spanned by this bitpacked
  509. element }
  510. size:=((bitoffset+elesize+7) div 8)-(bitoffset div 8);
  511. new_byte_offset:=byte_offset+(elesize*i) div 8;
  512. { bit offset of next element }
  513. inc(bitoffset,elesize);
  514. end;
  515. num:=classify_aggregate_element(def.elementdef,varspez,size,classes,new_byte_offset);
  516. if (num=0) then
  517. exit(0);
  518. inc(i);
  519. until (i=elecount);
  520. result:=finalize_aggregate_classification(def,words,classes);
  521. end;
  522. function classify_argument(def: tdef; varspez: tvarspez; real_size: aint; var classes: tx64paraclasses; byte_offset: aint): longint;
  523. begin
  524. case def.typ of
  525. orddef,
  526. enumdef,
  527. pointerdef,
  528. classrefdef:
  529. result:=classify_as_integer_argument(def,real_size,classes,byte_offset);
  530. formaldef:
  531. result:=classify_as_integer_argument(voidpointertype,voidpointertype.size,classes,byte_offset);
  532. floatdef:
  533. begin
  534. classes[0].def:=def;
  535. case tfloatdef(def).floattype of
  536. s32real:
  537. begin
  538. if byte_offset=0 then
  539. classes[0].typ:=X86_64_SSESF_CLASS
  540. else
  541. begin
  542. { if we have e.g. a record with two successive "single"
  543. fields, we need a 64 bit rather than a 32 bit load }
  544. classes[0].typ:=X86_64_SSE_CLASS;
  545. classes[0].def:=getarraydef(s32floattype,2);
  546. end;
  547. result:=1;
  548. end;
  549. s64real:
  550. begin
  551. classes[0].typ:=X86_64_SSEDF_CLASS;
  552. result:=1;
  553. end;
  554. s80real,
  555. sc80real:
  556. begin
  557. classes[0].typ:=X86_64_X87_CLASS;
  558. classes[1].typ:=X86_64_X87UP_CLASS;
  559. classes[1].def:=def;
  560. result:=2;
  561. end;
  562. s64comp,
  563. s64currency:
  564. begin
  565. classes[0].typ:=X86_64_INTEGER_CLASS;
  566. result:=1;
  567. end;
  568. s128real:
  569. begin
  570. classes[0].typ:=X86_64_SSE_CLASS;
  571. classes[0].def:=getarraydef(s32floattype,2);
  572. classes[1].typ:=X86_64_SSEUP_CLASS;
  573. classes[1].def:=getarraydef(s32floattype,2);
  574. result:=2;
  575. end;
  576. else
  577. internalerror(2010060301);
  578. end;
  579. end;
  580. recorddef:
  581. result:=classify_record(def,varspez,classes,byte_offset);
  582. objectdef:
  583. begin
  584. if is_object(def) then
  585. { pass by reference, like ppc and i386 }
  586. result:=0
  587. else
  588. { all kinds of pointer types: class, objcclass, interface, ... }
  589. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset);
  590. end;
  591. setdef:
  592. begin
  593. if is_smallset(def) then
  594. result:=classify_as_integer_argument(def,def.size,classes,byte_offset)
  595. else
  596. result:=0;
  597. end;
  598. stringdef:
  599. begin
  600. if (tstringdef(def).stringtype in [st_shortstring,st_longstring]) then
  601. result:=0
  602. else
  603. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  604. end;
  605. arraydef:
  606. begin
  607. { a dynamic array is treated like a pointer }
  608. if is_dynamic_array(def) then
  609. result:=classify_as_integer_argument(def,voidpointertype.size,classes,byte_offset)
  610. { other special arrays are passed on the stack }
  611. else if is_open_array(def) or
  612. is_array_of_const(def) then
  613. result:=0
  614. else
  615. { normal array }
  616. result:=classify_normal_array(tarraydef(def),varspez,classes,byte_offset);
  617. end;
  618. { the file record is definitely too big }
  619. filedef:
  620. result:=0;
  621. procvardef:
  622. begin
  623. if (po_methodpointer in tprocvardef(def).procoptions) then
  624. begin
  625. { treat as TMethod record }
  626. def:=search_system_type('TMETHOD').typedef;
  627. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  628. end
  629. else
  630. { pointer }
  631. result:=classify_as_integer_argument(def,def.size,classes,byte_offset);
  632. end;
  633. variantdef:
  634. begin
  635. { same as tvardata record }
  636. def:=search_system_type('TVARDATA').typedef;
  637. result:=classify_argument(def,varspez,def.size,classes,byte_offset);
  638. end;
  639. undefineddef:
  640. { show shall we know?
  641. since classify_argument is called during parsing, see tw27685.pp,
  642. we handle undefineddef here }
  643. result:=0;
  644. else
  645. internalerror(2010021405);
  646. end;
  647. end;
  648. procedure getvalueparaloc(varspez:tvarspez;def:tdef;var loc1,loc2:tx64paraclass);
  649. var
  650. size: aint;
  651. i: longint;
  652. classes: tx64paraclasses;
  653. numclasses: longint;
  654. begin
  655. { init the classes array, because even if classify_argument inits only
  656. one element we copy both to loc1/loc2 in case "1" is returned }
  657. for i:=low(classes) to high(classes) do
  658. begin
  659. classes[i].typ:=X86_64_NO_CLASS;
  660. classes[i].def:=nil;
  661. end;
  662. { def.size internalerrors for open arrays and dynamic arrays, since
  663. their size cannot be determined at compile-time.
  664. classify_argument does not look at the realsize argument for arrays
  665. cases, but we obviously do have to pass something... }
  666. if is_special_array(def) then
  667. size:=-1
  668. else
  669. size:=def.size;
  670. numclasses:=classify_argument(def,varspez,size,classes,0);
  671. case numclasses of
  672. 0:
  673. begin
  674. loc1.typ:=X86_64_MEMORY_CLASS;
  675. loc1.def:=def;
  676. loc2.typ:=X86_64_NO_CLASS;
  677. end;
  678. 1,2:
  679. begin
  680. { If the class is X87, X87UP or COMPLEX_X87, it is passed in memory }
  681. if classes[0].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  682. classes[0].typ:=X86_64_MEMORY_CLASS;
  683. if classes[1].typ in [X86_64_X87_CLASS,X86_64_X87UP_CLASS,X86_64_COMPLEX_X87_CLASS] then
  684. classes[1].typ:=X86_64_MEMORY_CLASS;
  685. loc1:=classes[0];
  686. loc2:=classes[1];
  687. end
  688. else
  689. { 4 can only happen for _m256 vectors, not yet supported }
  690. internalerror(2010021501);
  691. end;
  692. end;
  693. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  694. var
  695. classes: tx64paraclasses;
  696. numclasses: longint;
  697. begin
  698. if handle_common_ret_in_param(def,pd,result) then
  699. exit;
  700. fillchar(classes,sizeof(classes),0);
  701. case def.typ of
  702. { for records it depends on their contents and size }
  703. recorddef,
  704. { make sure we handle 'procedure of object' correctly }
  705. procvardef:
  706. begin
  707. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  708. result:=(numclasses=0);
  709. end;
  710. else
  711. result:=inherited ret_in_param(def,pd);
  712. end;
  713. end;
  714. function tcpuparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  715. var
  716. paraloc : pcgparalocation;
  717. begin
  718. if not assigned(cgpara.location) then
  719. internalerror(200410102);
  720. result:=true;
  721. { All locations are LOC_REFERENCE }
  722. paraloc:=cgpara.location;
  723. while assigned(paraloc) do
  724. begin
  725. if (paraloc^.loc<>LOC_REFERENCE) then
  726. begin
  727. result:=false;
  728. exit;
  729. end;
  730. paraloc:=paraloc^.next;
  731. end;
  732. end;
  733. { true if a parameter is too large to copy and only the address is pushed }
  734. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  735. var
  736. classes: tx64paraclasses;
  737. numclasses: longint;
  738. begin
  739. fillchar(classes,sizeof(classes),0);
  740. result:=false;
  741. { var,out,constref always require address }
  742. if varspez in [vs_var,vs_out,vs_constref] then
  743. begin
  744. result:=true;
  745. exit;
  746. end;
  747. { Only vs_const, vs_value here }
  748. case def.typ of
  749. formaldef :
  750. result:=true;
  751. recorddef :
  752. begin
  753. { MetroWerks Pascal: const records always passed by reference
  754. (for Mac OS X interfaces) }
  755. if (calloption=pocall_mwpascal) and
  756. (varspez=vs_const) then
  757. result:=true
  758. { Win ABI depends on size to pass it in a register or not }
  759. else if (target_info.system=system_x86_64_win64) then
  760. result:=not aggregate_in_registers_win64(varspez,def.size)
  761. { pass constant parameters that would be passed via memory by
  762. reference for non-cdecl/cppdecl, and make sure that the tmethod
  763. record (size=16) is passed the same way as a complex procvar }
  764. else if ((varspez=vs_const) and
  765. not(calloption in cdecl_pocalls)) or
  766. (def.size=16) then
  767. begin
  768. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  769. result:=numclasses=0;
  770. end
  771. else
  772. { SysV ABI always passes it as value parameter }
  773. result:=false;
  774. end;
  775. arraydef :
  776. begin
  777. { cdecl array of const need to be ignored and therefor be puhsed
  778. as value parameter with length 0 }
  779. if ((calloption in cdecl_pocalls) and
  780. is_array_of_const(def)) or
  781. is_dynamic_array(def) then
  782. result:=false
  783. else
  784. { pass all arrays by reference to be compatible with C (passing
  785. an array by value (= copying it on the stack) does not exist,
  786. because an array is the same as a pointer there }
  787. result:=true
  788. end;
  789. objectdef :
  790. begin
  791. { don't treat objects like records, because we only know wheter
  792. or not they'll have a VMT after the entire object is parsed
  793. -> if they are used as function result from one of their own
  794. methods, their size can still change after we've determined
  795. whether this function result should be returned by reference or
  796. by value }
  797. if is_object(def) then
  798. result:=true;
  799. end;
  800. variantdef,
  801. stringdef,
  802. procvardef,
  803. setdef :
  804. begin
  805. numclasses:=classify_argument(def,vs_value,def.size,classes,0);
  806. result:=numclasses=0;
  807. end;
  808. end;
  809. end;
  810. function tcpuparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  811. begin
  812. if target_info.system=system_x86_64_win64 then
  813. result:=[RS_RAX,RS_RCX,RS_RDX,RS_R8,RS_R9,RS_R10,RS_R11]
  814. else
  815. result:=[RS_RAX,RS_RCX,RS_RDX,RS_RSI,RS_RDI,RS_R8,RS_R9,RS_R10,RS_R11];
  816. end;
  817. function tcpuparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  818. begin
  819. if target_info.system=system_x86_64_win64 then
  820. result:=[RS_XMM0..RS_XMM5]
  821. else
  822. result:=[RS_XMM0..RS_XMM15];
  823. end;
  824. function tcpuparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  825. begin
  826. result:=[RS_ST0..RS_ST7];
  827. end;
  828. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  829. const
  830. intretregs: array[0..1] of tregister = (NR_FUNCTION_RETURN_REG,NR_FUNCTION_RETURN_REG_HIGH);
  831. mmretregs: array[0..1] of tregister = (NR_MM_RESULT_REG,NR_MM_RESULT_REG_HIGH);
  832. var
  833. classes: tx64paraclasses;
  834. i,
  835. numclasses: longint;
  836. intretregidx,
  837. mmretregidx: longint;
  838. retcgsize : tcgsize;
  839. paraloc : pcgparalocation;
  840. begin
  841. if set_common_funcretloc_info(p,forcetempdef,retcgsize,result) then
  842. exit;
  843. { Return in FPU register? -> don't use classify_argument(), because
  844. currency and comp need special treatment here (they are integer class
  845. when passing as parameter, but LOC_FPUREGISTER as function result) }
  846. if result.def.typ=floatdef then
  847. begin
  848. paraloc:=result.add_location;
  849. paraloc^.def:=result.def;
  850. case tfloatdef(result.def).floattype of
  851. s32real:
  852. begin
  853. paraloc^.loc:=LOC_MMREGISTER;
  854. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMS);
  855. paraloc^.size:=OS_F32;
  856. end;
  857. s64real:
  858. begin
  859. paraloc^.loc:=LOC_MMREGISTER;
  860. paraloc^.register:=newreg(R_MMREGISTER,RS_MM_RESULT_REG,R_SUBMMD);
  861. paraloc^.size:=OS_F64;
  862. end;
  863. { the first two only exist on targets with an x87, on others
  864. they are replace by int64 }
  865. s64currency,
  866. s64comp,
  867. s80real,
  868. sc80real:
  869. begin
  870. paraloc^.loc:=LOC_FPUREGISTER;
  871. paraloc^.register:=NR_FPU_RESULT_REG;
  872. paraloc^.size:=retcgsize;
  873. end;
  874. else
  875. internalerror(200405034);
  876. end;
  877. end
  878. else
  879. { Return in register }
  880. begin
  881. fillchar(classes,sizeof(classes),0);
  882. numclasses:=classify_argument(result.def,vs_value,result.def.size,classes,0);
  883. { this would mean a memory return }
  884. if (numclasses=0) then
  885. internalerror(2010021502);
  886. { this would mean an _m256 vector (valid, but not yet supported) }
  887. if (numclasses>2) then
  888. internalerror(2010021503);
  889. intretregidx:=0;
  890. mmretregidx:=0;
  891. for i:=0 to numclasses-1 do
  892. begin
  893. paraloc:=result.add_location;
  894. paraloc^.def:=classes[i].def;
  895. case classes[i].typ of
  896. X86_64_INTEGERSI_CLASS,
  897. X86_64_INTEGER_CLASS:
  898. begin
  899. paraloc^.loc:=LOC_REGISTER;
  900. paraloc^.register:=intretregs[intretregidx];
  901. if classes[i].typ=X86_64_INTEGER_CLASS then
  902. begin
  903. paraloc^.size:=OS_64;
  904. if paraloc^.def.size<>8 then
  905. paraloc^.def:=u64inttype;
  906. end
  907. else if result.intsize in [1,2,4] then
  908. begin
  909. paraloc^.size:=def_cgsize(paraloc^.def);
  910. end
  911. else
  912. begin
  913. paraloc^.size:=OS_32;
  914. if paraloc^.def.size<>4 then
  915. paraloc^.def:=u32inttype;
  916. end;
  917. setsubreg(paraloc^.register,cgsize2subreg(R_INTREGISTER,paraloc^.size));
  918. inc(intretregidx);
  919. end;
  920. X86_64_SSE_CLASS,
  921. X86_64_SSEUP_CLASS,
  922. X86_64_SSESF_CLASS,
  923. X86_64_SSEDF_CLASS:
  924. begin
  925. paraloc^.loc:=LOC_MMREGISTER;
  926. paraloc^.register:=mmretregs[mmretregidx];
  927. case classes[i].typ of
  928. X86_64_SSESF_CLASS:
  929. begin
  930. setsubreg(paraloc^.register,R_SUBMMS);
  931. paraloc^.size:=OS_F32;
  932. end;
  933. X86_64_SSEDF_CLASS:
  934. begin
  935. setsubreg(paraloc^.register,R_SUBMMD);
  936. paraloc^.size:=OS_F64;
  937. end;
  938. else
  939. begin
  940. setsubreg(paraloc^.register,R_SUBQ);
  941. paraloc^.size:=OS_M64;
  942. end;
  943. end;
  944. inc(mmretregidx);
  945. end;
  946. X86_64_X87_CLASS:
  947. begin
  948. { must be followed by X86_64_X87UP_CLASS and that must be
  949. the last class }
  950. if (i<>(numclasses-2)) or
  951. (classes[i+1].typ<>X86_64_X87UP_CLASS) then
  952. internalerror(2014110401);
  953. paraloc^.loc:=LOC_FPUREGISTER;
  954. paraloc^.register:=NR_FPU_RESULT_REG;
  955. paraloc^.size:=OS_F80;
  956. break;
  957. end;
  958. X86_64_NO_CLASS:
  959. begin
  960. { empty record/array }
  961. if (i<>0) or
  962. (numclasses<>1) then
  963. internalerror(2010060302);
  964. paraloc^.loc:=LOC_VOID;
  965. paraloc^.def:=voidtype;
  966. end;
  967. else
  968. internalerror(2010021504);
  969. end;
  970. end;
  971. end;
  972. end;
  973. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  974. var intparareg,mmparareg,parasize:longint;varargsparas: boolean);
  975. var
  976. hp : tparavarsym;
  977. fdef,
  978. paradef : tdef;
  979. paraloc : pcgparalocation;
  980. subreg : tsubregister;
  981. pushaddr : boolean;
  982. paracgsize : tcgsize;
  983. loc : array[1..2] of tx64paraclass;
  984. needintloc,
  985. needmmloc,
  986. paralen,
  987. locidx,
  988. i,
  989. varalign,
  990. paraalign : longint;
  991. begin
  992. paraalign:=get_para_align(p.proccalloption);
  993. { Register parameters are assigned from left to right }
  994. for i:=0 to paras.count-1 do
  995. begin
  996. hp:=tparavarsym(paras[i]);
  997. paradef:=hp.vardef;
  998. { on win64, if a record has only one field and that field is a
  999. single or double, it has to be handled like a single/double }
  1000. if (target_info.system=system_x86_64_win64) and
  1001. ((paradef.typ=recorddef) {or
  1002. is_object(paradef)}) and
  1003. tabstractrecordsymtable(tabstractrecorddef(paradef).symtable).has_single_field(fdef) and
  1004. (fdef.typ=floatdef) and
  1005. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  1006. paradef:=fdef;
  1007. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  1008. if pushaddr then
  1009. begin
  1010. loc[1].typ:=X86_64_INTEGER_CLASS;
  1011. loc[2].typ:=X86_64_NO_CLASS;
  1012. paracgsize:=OS_ADDR;
  1013. paralen:=sizeof(pint);
  1014. paradef:=cpointerdef.getreusable(paradef);
  1015. loc[1].def:=paradef;
  1016. end
  1017. else
  1018. begin
  1019. getvalueparaloc(hp.varspez,paradef,loc[1],loc[2]);
  1020. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  1021. paracgsize:=def_cgsize(paradef);
  1022. end;
  1023. { cheat for now, we should copy the value to an mm reg as well (FK) }
  1024. if varargsparas and
  1025. (target_info.system = system_x86_64_win64) and
  1026. (paradef.typ = floatdef) then
  1027. begin
  1028. loc[2].typ:=X86_64_NO_CLASS;
  1029. if paracgsize=OS_F64 then
  1030. begin
  1031. loc[1].typ:=X86_64_INTEGER_CLASS;
  1032. paracgsize:=OS_64;
  1033. paradef:=u64inttype;
  1034. end
  1035. else
  1036. begin
  1037. loc[1].typ:=X86_64_INTEGERSI_CLASS;
  1038. paracgsize:=OS_32;
  1039. paradef:=u32inttype;
  1040. end;
  1041. loc[1].def:=paradef;
  1042. end;
  1043. hp.paraloc[side].reset;
  1044. hp.paraloc[side].size:=paracgsize;
  1045. hp.paraloc[side].intsize:=paralen;
  1046. hp.paraloc[side].Alignment:=paraalign;
  1047. hp.paraloc[side].def:=paradef;
  1048. if paralen>0 then
  1049. begin
  1050. { Enough registers free? }
  1051. needintloc:=0;
  1052. needmmloc:=0;
  1053. for locidx:=low(loc) to high(loc) do
  1054. case loc[locidx].typ of
  1055. X86_64_INTEGER_CLASS,
  1056. X86_64_INTEGERSI_CLASS:
  1057. inc(needintloc);
  1058. X86_64_SSE_CLASS,
  1059. X86_64_SSESF_CLASS,
  1060. X86_64_SSEDF_CLASS,
  1061. X86_64_SSEUP_CLASS:
  1062. inc(needmmloc);
  1063. end;
  1064. { the "-1" is because we can also use the current register }
  1065. if ((target_info.system=system_x86_64_win64) and
  1066. ((intparareg+needintloc-1 > high(paraintsupregs_winx64)) or
  1067. (mmparareg+needmmloc-1 > high(parammsupregs_winx64)))) or
  1068. ((target_info.system<>system_x86_64_win64) and
  1069. ((intparareg+needintloc-1 > high(paraintsupregs)) or
  1070. (mmparareg+needmmloc-1 > high(parammsupregs)))) then
  1071. begin
  1072. { If there are no registers available for any
  1073. eightbyte of an argument, the whole argument is
  1074. passed on the stack. }
  1075. loc[low(loc)].typ:=X86_64_MEMORY_CLASS;
  1076. loc[low(loc)].def:=paradef;
  1077. for locidx:=succ(low(loc)) to high(loc) do
  1078. loc[locidx].typ:=X86_64_NO_CLASS;
  1079. end;
  1080. locidx:=1;
  1081. while (paralen>0) do
  1082. begin
  1083. if locidx>2 then
  1084. internalerror(200501283);
  1085. { Allocate }
  1086. case loc[locidx].typ of
  1087. X86_64_INTEGER_CLASS,
  1088. X86_64_INTEGERSI_CLASS:
  1089. begin
  1090. paraloc:=hp.paraloc[side].add_location;
  1091. paraloc^.loc:=LOC_REGISTER;
  1092. paraloc^.def:=loc[locidx].def;
  1093. if (paracgsize=OS_NO) or (loc[2].typ<>X86_64_NO_CLASS) then
  1094. begin
  1095. if loc[locidx].typ=X86_64_INTEGER_CLASS then
  1096. begin
  1097. paraloc^.size:=OS_INT;
  1098. paraloc^.def:=u64inttype;
  1099. subreg:=R_SUBWHOLE;
  1100. end
  1101. else
  1102. begin
  1103. paraloc^.size:=OS_32;
  1104. paraloc^.def:=u32inttype;
  1105. subreg:=R_SUBD;
  1106. end;
  1107. end
  1108. else
  1109. begin
  1110. paraloc^.size:=def_cgsize(paraloc^.def);
  1111. { s64comp is pushed in an int register }
  1112. if paraloc^.size=OS_C64 then
  1113. begin
  1114. paraloc^.size:=OS_64;
  1115. paraloc^.def:=u64inttype;
  1116. end;
  1117. subreg:=cgsize2subreg(R_INTREGISTER,paraloc^.size);
  1118. end;
  1119. { winx64 uses different registers }
  1120. if target_info.system=system_x86_64_win64 then
  1121. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs_winx64[intparareg],subreg)
  1122. else
  1123. paraloc^.register:=newreg(R_INTREGISTER,paraintsupregs[intparareg],subreg);
  1124. { matching mm register must be skipped }
  1125. if target_info.system=system_x86_64_win64 then
  1126. inc(mmparareg);
  1127. inc(intparareg);
  1128. dec(paralen,tcgsize2size[paraloc^.size]);
  1129. end;
  1130. X86_64_SSE_CLASS,
  1131. X86_64_SSESF_CLASS,
  1132. X86_64_SSEDF_CLASS,
  1133. X86_64_SSEUP_CLASS:
  1134. begin
  1135. paraloc:=hp.paraloc[side].add_location;
  1136. paraloc^.loc:=LOC_MMREGISTER;
  1137. paraloc^.def:=loc[locidx].def;
  1138. case loc[locidx].typ of
  1139. X86_64_SSESF_CLASS:
  1140. begin
  1141. subreg:=R_SUBMMS;
  1142. paraloc^.size:=OS_F32;
  1143. end;
  1144. X86_64_SSEDF_CLASS:
  1145. begin
  1146. subreg:=R_SUBMMD;
  1147. paraloc^.size:=OS_F64;
  1148. end;
  1149. else
  1150. begin
  1151. subreg:=R_SUBQ;
  1152. paraloc^.size:=OS_M64;
  1153. end;
  1154. end;
  1155. { winx64 uses different registers }
  1156. if target_info.system=system_x86_64_win64 then
  1157. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs_winx64[mmparareg],subreg)
  1158. else
  1159. paraloc^.register:=newreg(R_MMREGISTER,parammsupregs[mmparareg],subreg);
  1160. { matching int register must be skipped }
  1161. if target_info.system=system_x86_64_win64 then
  1162. inc(intparareg);
  1163. inc(mmparareg);
  1164. dec(paralen,tcgsize2size[paraloc^.size]);
  1165. end;
  1166. X86_64_MEMORY_CLASS :
  1167. begin
  1168. paraloc:=hp.paraloc[side].add_location;
  1169. paraloc^.loc:=LOC_REFERENCE;
  1170. paraloc^.def:=loc[locidx].def;
  1171. {Hack alert!!! We should modify int_cgsize to handle OS_128,
  1172. however, since int_cgsize is called in many places in the
  1173. compiler where only a few can already handle OS_128, fixing it
  1174. properly is out of the question to release 2.2.0 in time. (DM)}
  1175. if paracgsize=OS_128 then
  1176. if paralen=8 then
  1177. paraloc^.size:=OS_64
  1178. else if paralen=16 then
  1179. paraloc^.size:=OS_128
  1180. else
  1181. internalerror(200707143)
  1182. else if paracgsize in [OS_F32,OS_F64,OS_F80,OS_F128] then
  1183. paraloc^.size:=int_float_cgsize(paralen)
  1184. else
  1185. paraloc^.size:=int_cgsize(paralen);
  1186. if side=callerside then
  1187. paraloc^.reference.index:=NR_STACK_POINTER_REG
  1188. else
  1189. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  1190. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  1191. paraloc^.reference.offset:=parasize;
  1192. parasize:=align(parasize+paralen,varalign);
  1193. paralen:=0;
  1194. end;
  1195. else
  1196. internalerror(2010053113);
  1197. end;
  1198. if (locidx<2) and
  1199. (loc[locidx+1].typ<>X86_64_NO_CLASS) then
  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.