cpupara.pas 54 KB

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