cpupara.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. {
  2. Copyright (c) 2002 by Florian Klaempfl
  3. Generates the argument location information for i386
  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. aasmtai,aasmdata,cpubase,cgbase,cgutils,
  23. symconst,symtype,symsym,symdef,
  24. parabase,paramgr;
  25. type
  26. tcpuparamanager = class(tparamanager)
  27. function param_use_paraloc(const cgpara:tcgpara):boolean;override;
  28. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  29. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  30. function get_para_align(calloption : tproccalloption):byte;override;
  31. function get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;override;
  32. function get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;override;
  33. function get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;override;
  34. procedure get_para_regoff(proccalloption: tproccalloption; paraloc: pcgparalocation; out reg: Byte; out off: LongInt);override;
  35. function create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;override;
  36. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  37. procedure createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);override;
  38. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): TCGPara;override;
  39. private
  40. procedure create_stdcall_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;var parasize:longint);
  41. procedure create_register_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;var parareg,parasize:longint);
  42. end;
  43. implementation
  44. uses
  45. cutils,
  46. systems,verbose,
  47. symtable,
  48. defutil;
  49. const
  50. parasupregs : array[0..2] of tsuperregister = (RS_EAX,RS_EDX,RS_ECX);
  51. {****************************************************************************
  52. tcpuparamanager
  53. ****************************************************************************}
  54. function tcpuparamanager.param_use_paraloc(const cgpara:tcgpara):boolean;
  55. var
  56. paraloc : pcgparalocation;
  57. begin
  58. if not assigned(cgpara.location) then
  59. internalerror(200410102);
  60. result:=true;
  61. { All locations are LOC_REFERENCE }
  62. paraloc:=cgpara.location;
  63. while assigned(paraloc) do
  64. begin
  65. if (paraloc^.loc<>LOC_REFERENCE) then
  66. begin
  67. result:=false;
  68. exit;
  69. end;
  70. paraloc:=paraloc^.next;
  71. end;
  72. end;
  73. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  74. var
  75. size: longint;
  76. begin
  77. if handle_common_ret_in_param(def,pd,result) then
  78. exit;
  79. case target_info.system of
  80. system_i386_win32 :
  81. begin
  82. case def.typ of
  83. recorddef :
  84. begin
  85. { Win32 GCC returns small records in the FUNCTION_RETURN_REG up to 8 bytes in registers.
  86. For stdcall and register we follow delphi instead of GCC which returns
  87. only records of a size of 1,2 or 4 bytes in FUNCTION_RETURN_REG }
  88. if ((pd.proccalloption in [pocall_stdcall,pocall_register]) and
  89. (def.size in [1,2,4])) or
  90. ((pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  91. (def.size>0) and
  92. (def.size<=8)) then
  93. begin
  94. result:=false;
  95. exit;
  96. end;
  97. end;
  98. end;
  99. end;
  100. system_i386_os2,
  101. system_i386_emx:
  102. begin
  103. case def.typ of
  104. recorddef :
  105. begin
  106. { EMX port of GCC returns small records in the FUNCTION_RETURN_REG up to 4 bytes in registers. }
  107. if ((pd.proccalloption in [pocall_cdecl,pocall_cppdecl]) and
  108. (def.size>0) and
  109. (def.size<=4)) then
  110. begin
  111. result:=false;
  112. exit;
  113. end;
  114. end;
  115. end;
  116. end;
  117. system_i386_freebsd,
  118. system_i386_openbsd,
  119. system_i386_darwin,
  120. system_i386_iphonesim :
  121. begin
  122. if pd.proccalloption in cdecl_pocalls then
  123. begin
  124. case def.typ of
  125. recorddef :
  126. begin
  127. size:=def.size;
  128. if (size>0) and
  129. (size<=8) and
  130. { only if size is a power of 2 }
  131. ((size and (size-1)) = 0) then
  132. begin
  133. result:=false;
  134. exit;
  135. end;
  136. end;
  137. procvardef:
  138. begin
  139. result:=false;
  140. exit;
  141. end;
  142. end;
  143. end;
  144. end;
  145. end;
  146. result:=inherited ret_in_param(def,pd);
  147. end;
  148. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  149. begin
  150. result:=false;
  151. { var,out,constref always require address }
  152. if varspez in [vs_var,vs_out,vs_constref] then
  153. begin
  154. result:=true;
  155. exit;
  156. end;
  157. { Only vs_const, vs_value here }
  158. case def.typ of
  159. variantdef :
  160. begin
  161. { variants are small enough to be passed by value except if
  162. required by the windows api
  163. variants are somethings very delphi/windows specific so do it like
  164. windows/delphi (FK)
  165. }
  166. if ((target_info.system=system_i386_win32) and
  167. (calloption in [pocall_stdcall,pocall_safecall]) and
  168. (varspez=vs_const)) or
  169. (calloption=pocall_register) then
  170. result:=true
  171. else
  172. result:=false;
  173. end;
  174. formaldef :
  175. result:=true;
  176. recorddef :
  177. begin
  178. { Delphi stdcall passes records on the stack for call by value }
  179. if (target_info.system=system_i386_win32) and
  180. (calloption=pocall_stdcall) and
  181. (varspez=vs_value) then
  182. result:=false
  183. else
  184. result:=
  185. (not(calloption in (cdecl_pocalls)) and
  186. (def.size>sizeof(aint))) or
  187. (((calloption = pocall_mwpascal) or (target_info.system=system_i386_wince)) and
  188. (varspez=vs_const));
  189. end;
  190. arraydef :
  191. begin
  192. { array of const values are pushed on the stack as
  193. well as dyn. arrays }
  194. if (calloption in cdecl_pocalls) then
  195. result:=not(is_array_of_const(def) or
  196. is_dynamic_array(def))
  197. else
  198. begin
  199. result:=(
  200. (tarraydef(def).highrange>=tarraydef(def).lowrange) and
  201. (def.size>sizeof(aint))
  202. ) or
  203. is_open_array(def) or
  204. is_array_of_const(def) or
  205. is_array_constructor(def);
  206. end;
  207. end;
  208. objectdef :
  209. result:=is_object(def);
  210. stringdef :
  211. result:= (tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  212. procvardef :
  213. result:=not(calloption in cdecl_pocalls) and not tprocvardef(def).is_addressonly;
  214. setdef :
  215. result:=not(calloption in cdecl_pocalls) and (not is_smallset(def));
  216. end;
  217. end;
  218. function tcpuparamanager.get_para_align(calloption : tproccalloption):byte;
  219. begin
  220. if calloption=pocall_oldfpccall then
  221. begin
  222. if target_info.system in [system_i386_go32v2,system_i386_watcom] then
  223. result:=2
  224. else
  225. result:=4;
  226. end
  227. else
  228. result:=std_param_align;
  229. end;
  230. function tcpuparamanager.get_volatile_registers_int(calloption : tproccalloption):tcpuregisterset;
  231. begin
  232. case calloption of
  233. pocall_internproc :
  234. result:=[];
  235. pocall_register,
  236. pocall_safecall,
  237. pocall_stdcall,
  238. pocall_cdecl,
  239. pocall_syscall,
  240. pocall_cppdecl,
  241. pocall_mwpascal :
  242. result:=[RS_EAX,RS_EDX,RS_ECX];
  243. pocall_far16,
  244. pocall_pascal,
  245. pocall_oldfpccall :
  246. result:=[RS_EAX,RS_EDX,RS_ECX,RS_ESI,RS_EDI,RS_EBX];
  247. else
  248. internalerror(200309071);
  249. end;
  250. end;
  251. function tcpuparamanager.get_volatile_registers_fpu(calloption : tproccalloption):tcpuregisterset;
  252. begin
  253. result:=[0..first_fpu_imreg-1];
  254. end;
  255. function tcpuparamanager.get_volatile_registers_mm(calloption : tproccalloption):tcpuregisterset;
  256. begin
  257. result:=[0..first_mm_imreg-1];
  258. end;
  259. procedure tcpuparamanager.get_para_regoff(proccalloption: tproccalloption; paraloc: pcgparalocation; out reg: Byte; out off: LongInt);
  260. var
  261. I : SizeInt;
  262. begin
  263. with paraloc^ do
  264. case loc of
  265. LOC_REGISTER:
  266. begin
  267. for I := 0 to high(parasupregs) do
  268. if getsupreg(register)=parasupregs[I] then
  269. begin
  270. reg:=I;
  271. break;
  272. end;
  273. off:=0;
  274. end;
  275. LOC_REFERENCE:
  276. begin
  277. reg:=255;
  278. off:=reference.offset;
  279. end;
  280. end;
  281. end;
  282. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): TCGPara;
  283. var
  284. retcgsize : tcgsize;
  285. paraloc : pcgparalocation;
  286. fdef,
  287. usedef: tdef;
  288. handled: boolean;
  289. begin
  290. if not assigned(forcetempdef) then
  291. usedef:=p.returndef
  292. else
  293. usedef:=forcetempdef;
  294. { on darwin/i386, if a record has only one field and that field is a
  295. single or double, it has to be returned like a single/double }
  296. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,
  297. system_i386_freebsd,system_i386_openbsd,
  298. system_i386_os2,system_i386_emx]) and
  299. ((usedef.typ=recorddef) or
  300. is_object(usedef)) and
  301. tabstractrecordsymtable(tabstractrecorddef(usedef).symtable).has_single_field(fdef) and
  302. (fdef.typ=floatdef) and
  303. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  304. usedef:=fdef;
  305. handled:=set_common_funcretloc_info(p,usedef,retcgsize,result);
  306. { normally forcetempdef is passed straight through to
  307. set_common_funcretloc_info and that one will correctly determine whether
  308. the location is a temporary one, but that doesn't work here because we
  309. sometimes have to change the type }
  310. result.temporary:=assigned(forcetempdef);
  311. if handled then
  312. exit;
  313. { darwin/x86 requires that results < sizeof(aint) are sign/zero
  314. extended to sizeof(aint) }
  315. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  316. (side=calleeside) and
  317. (result.intsize>0) and
  318. (result.intsize<sizeof(aint)) then
  319. begin
  320. result.def:=sinttype;
  321. result.intsize:=sizeof(aint);
  322. retcgsize:=OS_SINT;
  323. result.size:=retcgsize;
  324. end;
  325. { Return in FPU register? }
  326. if result.def.typ=floatdef then
  327. begin
  328. paraloc:=result.add_location;
  329. paraloc^.loc:=LOC_FPUREGISTER;
  330. paraloc^.register:=NR_FPU_RESULT_REG;
  331. paraloc^.size:=retcgsize;
  332. paraloc^.def:=result.def;
  333. end
  334. else
  335. { Return in register }
  336. begin
  337. paraloc:=result.add_location;
  338. paraloc^.loc:=LOC_REGISTER;
  339. if retcgsize in [OS_64,OS_S64] then
  340. begin
  341. { low 32bits }
  342. if side=callerside then
  343. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  344. else
  345. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  346. paraloc^.size:=OS_32;
  347. paraloc^.def:=u32inttype;
  348. { high 32bits }
  349. paraloc:=result.add_location;
  350. paraloc^.loc:=LOC_REGISTER;
  351. if side=callerside then
  352. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  353. else
  354. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  355. paraloc^.size:=OS_32;
  356. paraloc^.def:=u32inttype;
  357. end
  358. else
  359. begin
  360. paraloc^.size:=retcgsize;
  361. paraloc^.def:=result.def;
  362. if side=callerside then
  363. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  364. else
  365. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  366. end;
  367. end;
  368. end;
  369. procedure tcpuparamanager.create_stdcall_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;var parasize:longint);
  370. var
  371. i : integer;
  372. hp : tparavarsym;
  373. paradef : tdef;
  374. paraloc : pcgparalocation;
  375. l,
  376. paralen,
  377. varalign : longint;
  378. paraalign : shortint;
  379. paracgsize : tcgsize;
  380. firstparaloc,
  381. pushaddr : boolean;
  382. begin
  383. paraalign:=get_para_align(p.proccalloption);
  384. { we push Flags and CS as long
  385. to cope with the IRETD
  386. and we save 6 register + 4 selectors }
  387. if po_interrupt in p.procoptions then
  388. inc(parasize,8+6*4+4*2);
  389. { Offset is calculated like:
  390. sub esp,12
  391. mov [esp+8],para3
  392. mov [esp+4],para2
  393. mov [esp],para1
  394. call function
  395. That means for pushes the para with the
  396. highest offset (see para3) needs to be pushed first
  397. }
  398. if p.proccalloption in pushleftright_pocalls then
  399. i:=paras.count-1
  400. else
  401. i:=0;
  402. while ((p.proccalloption in pushleftright_pocalls) and (i>=0)) or
  403. (not(p.proccalloption in pushleftright_pocalls) and (i<=paras.count-1)) do
  404. begin
  405. hp:=tparavarsym(paras[i]);
  406. paradef:=hp.vardef;
  407. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  408. if pushaddr then
  409. begin
  410. paralen:=sizeof(aint);
  411. paracgsize:=OS_ADDR;
  412. paradef:=cpointerdef.getreusable_no_free(paradef);
  413. end
  414. else
  415. begin
  416. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  417. { darwin/x86 requires that parameters < sizeof(aint) are sign/ }
  418. { zero extended to sizeof(aint) }
  419. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  420. (side = callerside) and
  421. (paralen > 0) and
  422. (paralen < sizeof(aint)) then
  423. begin
  424. paralen:=sizeof(aint);
  425. paracgsize:=OS_SINT;
  426. paradef:=sinttype;
  427. end
  428. else
  429. paracgsize:=def_cgsize(paradef);
  430. end;
  431. hp.paraloc[side].reset;
  432. hp.paraloc[side].size:=paracgsize;
  433. hp.paraloc[side].intsize:=paralen;
  434. hp.paraloc[side].def:=paradef;
  435. hp.paraloc[side].Alignment:=paraalign;
  436. { Copy to stack? }
  437. if (paracgsize=OS_NO) or
  438. (use_fixed_stack) then
  439. begin
  440. paraloc:=hp.paraloc[side].add_location;
  441. paraloc^.loc:=LOC_REFERENCE;
  442. paraloc^.size:=paracgsize;
  443. paraloc^.def:=paradef;
  444. if side=callerside then
  445. paraloc^.reference.index:=NR_STACK_POINTER_REG
  446. else
  447. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  448. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  449. { don't let push_size return 16, because then we can }
  450. { read past the end of the heap since the value is only }
  451. { 10 bytes long (JM) }
  452. if (paracgsize = OS_F80) and
  453. (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  454. paralen:=16;
  455. paraloc^.reference.offset:=parasize;
  456. if side=calleeside then
  457. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  458. parasize:=align(parasize+paralen,varalign);
  459. end
  460. else
  461. begin
  462. if paralen=0 then
  463. internalerror(200501163);
  464. firstparaloc:=true;
  465. while (paralen>0) do
  466. begin
  467. paraloc:=hp.paraloc[side].add_location;
  468. paraloc^.loc:=LOC_REFERENCE;
  469. { single and double need a single location }
  470. if (paracgsize in [OS_F64,OS_F32]) then
  471. begin
  472. paraloc^.size:=paracgsize;
  473. paraloc^.def:=paradef;
  474. l:=paralen;
  475. end
  476. else
  477. begin
  478. { We can allocate at maximum 32 bits per location }
  479. if paralen>sizeof(aint) then
  480. begin
  481. l:=sizeof(aint);
  482. paraloc^.def:=uinttype;
  483. end
  484. else
  485. begin
  486. l:=paralen;
  487. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  488. end;
  489. paraloc^.size:=int_cgsize(l);
  490. end;
  491. if (side=callerside) or
  492. (po_nostackframe in p.procoptions) then
  493. paraloc^.reference.index:=NR_STACK_POINTER_REG
  494. else
  495. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  496. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  497. paraloc^.reference.offset:=parasize;
  498. if side=calleeside then
  499. if not(po_nostackframe in p.procoptions) then
  500. inc(paraloc^.reference.offset,target_info.first_parm_offset)
  501. else
  502. { return addres }
  503. inc(paraloc^.reference.offset,4);
  504. parasize:=align(parasize+l,varalign);
  505. dec(paralen,l);
  506. firstparaloc:=false;
  507. end;
  508. end;
  509. if p.proccalloption in pushleftright_pocalls then
  510. dec(i)
  511. else
  512. inc(i);
  513. end;
  514. end;
  515. procedure tcpuparamanager.create_register_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  516. var parareg,parasize:longint);
  517. var
  518. hp : tparavarsym;
  519. paradef : tdef;
  520. paraloc : pcgparalocation;
  521. paracgsize : tcgsize;
  522. i : integer;
  523. l,
  524. paralen,
  525. varalign : longint;
  526. paraalign : shortint;
  527. pass : byte;
  528. firstparaloc,
  529. pushaddr : boolean;
  530. begin
  531. if paras.count=0 then
  532. exit;
  533. paraalign:=get_para_align(p.proccalloption);
  534. { clean up here so we can later detect properly if a parameter has been
  535. assigned or not
  536. }
  537. for i:=0 to paras.count-1 do
  538. tparavarsym(paras[i]).paraloc[side].reset;
  539. { Register parameters are assigned from left to right,
  540. stack parameters from right to left so assign first the
  541. register parameters in a first pass, in the second
  542. pass all unhandled parameters are done }
  543. for pass:=1 to 2 do
  544. begin
  545. if pass=1 then
  546. i:=0
  547. else
  548. i:=paras.count-1;
  549. while true do
  550. begin
  551. hp:=tparavarsym(paras[i]);
  552. paradef:=hp.vardef;
  553. if not(assigned(hp.paraloc[side].location)) then
  554. begin
  555. pushaddr:=push_addr_param(hp.varspez,hp.vardef,p.proccalloption);
  556. if pushaddr then
  557. begin
  558. paralen:=sizeof(aint);
  559. paracgsize:=OS_ADDR;
  560. paradef:=cpointerdef.getreusable_no_free(paradef);
  561. end
  562. else
  563. begin
  564. paralen:=push_size(hp.varspez,hp.vardef,p.proccalloption);
  565. paracgsize:=def_cgsize(hp.vardef);
  566. end;
  567. hp.paraloc[side].size:=paracgsize;
  568. hp.paraloc[side].intsize:=paralen;
  569. hp.paraloc[side].Alignment:=paraalign;
  570. hp.paraloc[side].def:=paradef;
  571. {
  572. EAX
  573. EDX
  574. ECX
  575. Stack
  576. Stack
  577. 64bit values,floats,arrays and records are always
  578. on the stack.
  579. In case of po_delphi_nested_cc, the parent frame pointer
  580. is also always passed on the stack.
  581. }
  582. if (parareg<=high(parasupregs)) and
  583. (paralen<=sizeof(aint)) and
  584. (not(hp.vardef.typ in [floatdef,recorddef,arraydef]) or
  585. pushaddr or
  586. is_dynamic_array(hp.vardef)) and
  587. (not(vo_is_parentfp in hp.varoptions) or
  588. not(po_delphi_nested_cc in p.procoptions)) then
  589. begin
  590. if pass=1 then
  591. begin
  592. paraloc:=hp.paraloc[side].add_location;
  593. paraloc^.size:=paracgsize;
  594. paraloc^.def:=paradef;
  595. paraloc^.loc:=LOC_REGISTER;
  596. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[parareg],cgsize2subreg(R_INTREGISTER,paracgsize));
  597. inc(parareg);
  598. end;
  599. end
  600. else
  601. if pass=2 then
  602. begin
  603. { Copy to stack? }
  604. if (use_fixed_stack) or
  605. (paracgsize=OS_NO) then
  606. begin
  607. paraloc:=hp.paraloc[side].add_location;
  608. paraloc^.loc:=LOC_REFERENCE;
  609. paraloc^.size:=paracgsize;
  610. paraloc^.def:=paradef;
  611. if side=callerside then
  612. paraloc^.reference.index:=NR_STACK_POINTER_REG
  613. else
  614. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  615. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  616. paraloc^.reference.offset:=parasize;
  617. if side=calleeside then
  618. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  619. parasize:=align(parasize+paralen,varalign);
  620. end
  621. else
  622. begin
  623. if paralen=0 then
  624. internalerror(200501163);
  625. firstparaloc:=true;
  626. while (paralen>0) do
  627. begin
  628. paraloc:=hp.paraloc[side].add_location;
  629. paraloc^.loc:=LOC_REFERENCE;
  630. { Extended and double need a single location }
  631. if (paracgsize in [OS_F64,OS_F32]) then
  632. begin
  633. paraloc^.size:=paracgsize;
  634. paraloc^.def:=paradef;
  635. l:=paralen;
  636. end
  637. else
  638. begin
  639. { We can allocate at maximum 32 bits per location }
  640. if paralen>sizeof(aint) then
  641. begin
  642. l:=sizeof(aint);
  643. paraloc^.def:=uinttype;
  644. end
  645. else
  646. begin
  647. l:=paralen;
  648. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  649. end;
  650. paraloc^.size:=int_cgsize(l);
  651. end;
  652. if side=callerside then
  653. paraloc^.reference.index:=NR_STACK_POINTER_REG
  654. else
  655. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  656. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  657. paraloc^.reference.offset:=parasize;
  658. if side=calleeside then
  659. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  660. parasize:=align(parasize+l,varalign);
  661. dec(paralen,l);
  662. firstparaloc:=false;
  663. end;
  664. end;
  665. end;
  666. end;
  667. case pass of
  668. 1:
  669. begin
  670. if i=paras.count-1 then
  671. break;
  672. inc(i);
  673. end;
  674. 2:
  675. begin
  676. if i=0 then
  677. break;
  678. dec(i);
  679. end;
  680. end;
  681. end;
  682. end;
  683. end;
  684. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  685. var
  686. parasize,
  687. parareg : longint;
  688. begin
  689. parasize:=0;
  690. parareg:=0;
  691. case p.proccalloption of
  692. pocall_register :
  693. create_register_paraloc_info(p,side,p.paras,parareg,parasize);
  694. pocall_internproc :
  695. begin
  696. { Use default calling }
  697. {$warnings off}
  698. if (pocall_default=pocall_register) then
  699. create_register_paraloc_info(p,side,p.paras,parareg,parasize)
  700. else
  701. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  702. {$warnings on}
  703. end;
  704. else
  705. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  706. end;
  707. create_funcretloc_info(p,side);
  708. result:=parasize;
  709. end;
  710. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  711. var
  712. parasize : longint;
  713. begin
  714. parasize:=0;
  715. { calculate the registers for the normal parameters }
  716. create_stdcall_paraloc_info(p,callerside,p.paras,parasize);
  717. { append the varargs }
  718. create_stdcall_paraloc_info(p,callerside,varargspara,parasize);
  719. result:=parasize;
  720. end;
  721. procedure tcpuparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  722. begin
  723. { Never a need for temps when value is pushed (calls inside parameters
  724. will simply allocate even more stack space for their parameters) }
  725. if not(use_fixed_stack) then
  726. can_use_final_stack_loc:=true;
  727. inherited createtempparaloc(list,calloption,parasym,can_use_final_stack_loc,cgpara);
  728. end;
  729. begin
  730. paramanager:=tcpuparamanager.create;
  731. end.