cpupara.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. function get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;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,sysutils,
  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 cdecl_pocalls) 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 cdecl_pocalls) 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. function tcpuparamanager.get_saved_registers_int(calloption : tproccalloption):tcpuregisterarray;
  260. const
  261. saveregs : array[0..3] of tsuperregister = (RS_EBX,RS_ESI,RS_EDI,RS_EBP);
  262. begin
  263. result:=saveregs;
  264. end;
  265. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): TCGPara;
  266. var
  267. retcgsize : tcgsize;
  268. paraloc : pcgparalocation;
  269. fdef,
  270. usedef: tdef;
  271. handled: boolean;
  272. begin
  273. if not assigned(forcetempdef) then
  274. usedef:=p.returndef
  275. else
  276. usedef:=forcetempdef;
  277. { on darwin/i386, if a record has only one field and that field is a
  278. single or double, it has to be returned like a single/double }
  279. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,
  280. system_i386_freebsd,system_i386_openbsd,
  281. system_i386_os2,system_i386_emx]) and
  282. ((usedef.typ=recorddef) or
  283. is_object(usedef)) and
  284. tabstractrecordsymtable(tabstractrecorddef(usedef).symtable).has_single_field(fdef) and
  285. (fdef.typ=floatdef) and
  286. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  287. usedef:=fdef;
  288. handled:=set_common_funcretloc_info(p,usedef,retcgsize,result);
  289. { normally forcetempdef is passed straight through to
  290. set_common_funcretloc_info and that one will correctly determine whether
  291. the location is a temporary one, but that doesn't work here because we
  292. sometimes have to change the type }
  293. result.temporary:=assigned(forcetempdef);
  294. if handled then
  295. exit;
  296. { darwin/x86 requires that results < sizeof(aint) are sign/zero
  297. extended to sizeof(aint) }
  298. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  299. (side=calleeside) and
  300. (result.intsize>0) and
  301. (result.intsize<sizeof(aint)) then
  302. begin
  303. result.def:=sinttype;
  304. result.intsize:=sizeof(aint);
  305. retcgsize:=OS_SINT;
  306. result.size:=retcgsize;
  307. end;
  308. { Return in FPU register? }
  309. if result.def.typ=floatdef then
  310. begin
  311. paraloc:=result.add_location;
  312. paraloc^.loc:=LOC_FPUREGISTER;
  313. paraloc^.register:=NR_FPU_RESULT_REG;
  314. paraloc^.size:=retcgsize;
  315. paraloc^.def:=result.def;
  316. end
  317. else
  318. { Return in register }
  319. begin
  320. paraloc:=result.add_location;
  321. paraloc^.loc:=LOC_REGISTER;
  322. if retcgsize in [OS_64,OS_S64] then
  323. begin
  324. { low 32bits }
  325. if side=callerside then
  326. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  327. else
  328. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  329. paraloc^.size:=OS_32;
  330. paraloc^.def:=u32inttype;
  331. { high 32bits }
  332. paraloc:=result.add_location;
  333. paraloc^.loc:=LOC_REGISTER;
  334. if side=callerside then
  335. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  336. else
  337. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  338. paraloc^.size:=OS_32;
  339. paraloc^.def:=u32inttype;
  340. end
  341. else
  342. begin
  343. paraloc^.size:=retcgsize;
  344. paraloc^.def:=result.def;
  345. if side=callerside then
  346. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  347. else
  348. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  349. end;
  350. end;
  351. end;
  352. procedure tcpuparamanager.create_stdcall_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;var parasize:longint);
  353. var
  354. i : integer;
  355. hp : tparavarsym;
  356. paradef : tdef;
  357. paraloc : pcgparalocation;
  358. l,
  359. paralen,
  360. varalign : longint;
  361. paraalign : shortint;
  362. paracgsize : tcgsize;
  363. firstparaloc,
  364. pushaddr : boolean;
  365. begin
  366. paraalign:=get_para_align(p.proccalloption);
  367. { we push Flags and CS as long
  368. to cope with the IRETD
  369. and we save 6 register + 4 selectors }
  370. if po_interrupt in p.procoptions then
  371. inc(parasize,8+6*4+4*2);
  372. { Offset is calculated like:
  373. sub esp,12
  374. mov [esp+8],para3
  375. mov [esp+4],para2
  376. mov [esp],para1
  377. call function
  378. That means for pushes the para with the
  379. highest offset (see para3) needs to be pushed first
  380. }
  381. if p.proccalloption in pushleftright_pocalls then
  382. i:=paras.count-1
  383. else
  384. i:=0;
  385. while ((p.proccalloption in pushleftright_pocalls) and (i>=0)) or
  386. (not(p.proccalloption in pushleftright_pocalls) and (i<=paras.count-1)) do
  387. begin
  388. hp:=tparavarsym(paras[i]);
  389. paradef:=hp.vardef;
  390. { syscall for AROS can have already a paraloc set }
  391. if (vo_has_explicit_paraloc in hp.varoptions) then
  392. begin
  393. if not(vo_is_syscall_lib in hp.varoptions) then
  394. internalerror(2016090105);
  395. if p.proccalloption in pushleftright_pocalls then
  396. dec(i)
  397. else
  398. inc(i);
  399. continue;
  400. end;
  401. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  402. if pushaddr then
  403. begin
  404. paralen:=sizeof(aint);
  405. paracgsize:=OS_ADDR;
  406. paradef:=cpointerdef.getreusable_no_free(paradef);
  407. end
  408. else
  409. begin
  410. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  411. { darwin/x86 requires that parameters < sizeof(aint) are sign/ }
  412. { zero extended to sizeof(aint) }
  413. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  414. (side = callerside) and
  415. (paralen > 0) and
  416. (paralen < sizeof(aint)) then
  417. begin
  418. paralen:=sizeof(aint);
  419. paracgsize:=OS_SINT;
  420. paradef:=sinttype;
  421. end
  422. else
  423. paracgsize:=def_cgsize(paradef);
  424. end;
  425. hp.paraloc[side].reset;
  426. hp.paraloc[side].size:=paracgsize;
  427. hp.paraloc[side].intsize:=paralen;
  428. hp.paraloc[side].def:=paradef;
  429. hp.paraloc[side].Alignment:=paraalign;
  430. { Copy to stack? }
  431. if (paracgsize=OS_NO) or
  432. (use_fixed_stack) then
  433. begin
  434. paraloc:=hp.paraloc[side].add_location;
  435. paraloc^.loc:=LOC_REFERENCE;
  436. paraloc^.size:=paracgsize;
  437. paraloc^.def:=paradef;
  438. if side=callerside then
  439. paraloc^.reference.index:=NR_STACK_POINTER_REG
  440. else
  441. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  442. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  443. { don't let push_size return 16, because then we can }
  444. { read past the end of the heap since the value is only }
  445. { 10 bytes long (JM) }
  446. if (paracgsize = OS_F80) and
  447. (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  448. paralen:=16;
  449. paraloc^.reference.offset:=parasize;
  450. if side=calleeside then
  451. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  452. parasize:=align(parasize+paralen,varalign);
  453. end
  454. else
  455. begin
  456. if paralen=0 then
  457. internalerror(200501163);
  458. firstparaloc:=true;
  459. while (paralen>0) do
  460. begin
  461. paraloc:=hp.paraloc[side].add_location;
  462. paraloc^.loc:=LOC_REFERENCE;
  463. { single and double need a single location }
  464. if (paracgsize in [OS_F64,OS_F32]) then
  465. begin
  466. paraloc^.size:=paracgsize;
  467. paraloc^.def:=paradef;
  468. l:=paralen;
  469. end
  470. else
  471. begin
  472. { We can allocate at maximum 32 bits per location }
  473. if paralen>sizeof(aint) then
  474. begin
  475. l:=sizeof(aint);
  476. paraloc^.def:=uinttype;
  477. end
  478. else
  479. begin
  480. l:=paralen;
  481. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  482. end;
  483. paraloc^.size:=int_cgsize(l);
  484. end;
  485. if (side=callerside) or
  486. (po_nostackframe in p.procoptions) then
  487. paraloc^.reference.index:=NR_STACK_POINTER_REG
  488. else
  489. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  490. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  491. paraloc^.reference.offset:=parasize;
  492. if side=calleeside then
  493. if not(po_nostackframe in p.procoptions) then
  494. inc(paraloc^.reference.offset,target_info.first_parm_offset)
  495. else
  496. { return addres }
  497. inc(paraloc^.reference.offset,4);
  498. parasize:=align(parasize+l,varalign);
  499. dec(paralen,l);
  500. firstparaloc:=false;
  501. end;
  502. end;
  503. if p.proccalloption in pushleftright_pocalls then
  504. dec(i)
  505. else
  506. inc(i);
  507. end;
  508. end;
  509. procedure tcpuparamanager.create_register_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  510. var parareg,parasize:longint);
  511. var
  512. hp : tparavarsym;
  513. paradef : tdef;
  514. paraloc : pcgparalocation;
  515. paracgsize : tcgsize;
  516. i : integer;
  517. l,
  518. paralen,
  519. varalign : longint;
  520. paraalign : shortint;
  521. pass : byte;
  522. firstparaloc,
  523. pushaddr : boolean;
  524. begin
  525. if paras.count=0 then
  526. exit;
  527. paraalign:=get_para_align(p.proccalloption);
  528. { clean up here so we can later detect properly if a parameter has been
  529. assigned or not
  530. }
  531. for i:=0 to paras.count-1 do
  532. tparavarsym(paras[i]).paraloc[side].reset;
  533. { Register parameters are assigned from left to right,
  534. stack parameters from right to left so assign first the
  535. register parameters in a first pass, in the second
  536. pass all unhandled parameters are done }
  537. for pass:=1 to 2 do
  538. begin
  539. if pass=1 then
  540. i:=0
  541. else
  542. i:=paras.count-1;
  543. while true do
  544. begin
  545. hp:=tparavarsym(paras[i]);
  546. paradef:=hp.vardef;
  547. if not(assigned(hp.paraloc[side].location)) then
  548. begin
  549. pushaddr:=push_addr_param(hp.varspez,hp.vardef,p.proccalloption);
  550. if pushaddr then
  551. begin
  552. paralen:=sizeof(aint);
  553. paracgsize:=OS_ADDR;
  554. paradef:=cpointerdef.getreusable_no_free(paradef);
  555. end
  556. else
  557. begin
  558. paralen:=push_size(hp.varspez,hp.vardef,p.proccalloption);
  559. paracgsize:=def_cgsize(hp.vardef);
  560. end;
  561. hp.paraloc[side].size:=paracgsize;
  562. hp.paraloc[side].intsize:=paralen;
  563. hp.paraloc[side].Alignment:=paraalign;
  564. hp.paraloc[side].def:=paradef;
  565. {
  566. EAX
  567. EDX
  568. ECX
  569. Stack
  570. Stack
  571. 64bit values,floats,arrays and records are always
  572. on the stack.
  573. In case of po_delphi_nested_cc, the parent frame pointer
  574. is also always passed on the stack.
  575. }
  576. if (parareg<=high(parasupregs)) and
  577. (paralen<=sizeof(aint)) and
  578. (not(hp.vardef.typ in [floatdef,recorddef,arraydef]) or
  579. pushaddr or
  580. is_dynamic_array(hp.vardef)) and
  581. (not(vo_is_parentfp in hp.varoptions) or
  582. not(po_delphi_nested_cc in p.procoptions)) then
  583. begin
  584. if pass=1 then
  585. begin
  586. paraloc:=hp.paraloc[side].add_location;
  587. paraloc^.size:=paracgsize;
  588. paraloc^.def:=paradef;
  589. paraloc^.loc:=LOC_REGISTER;
  590. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[parareg],cgsize2subreg(R_INTREGISTER,paracgsize));
  591. inc(parareg);
  592. end;
  593. end
  594. else
  595. if pass=2 then
  596. begin
  597. { Copy to stack? }
  598. if (use_fixed_stack) or
  599. (paracgsize=OS_NO) then
  600. begin
  601. paraloc:=hp.paraloc[side].add_location;
  602. paraloc^.loc:=LOC_REFERENCE;
  603. paraloc^.size:=paracgsize;
  604. paraloc^.def:=paradef;
  605. if side=callerside then
  606. paraloc^.reference.index:=NR_STACK_POINTER_REG
  607. else
  608. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  609. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  610. paraloc^.reference.offset:=parasize;
  611. if side=calleeside then
  612. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  613. parasize:=align(parasize+paralen,varalign);
  614. end
  615. else
  616. begin
  617. if paralen=0 then
  618. internalerror(200501163);
  619. firstparaloc:=true;
  620. while (paralen>0) do
  621. begin
  622. paraloc:=hp.paraloc[side].add_location;
  623. paraloc^.loc:=LOC_REFERENCE;
  624. { Extended and double need a single location }
  625. if (paracgsize in [OS_F64,OS_F32]) then
  626. begin
  627. paraloc^.size:=paracgsize;
  628. paraloc^.def:=paradef;
  629. l:=paralen;
  630. end
  631. else
  632. begin
  633. { We can allocate at maximum 32 bits per location }
  634. if paralen>sizeof(aint) then
  635. begin
  636. l:=sizeof(aint);
  637. paraloc^.def:=uinttype;
  638. end
  639. else
  640. begin
  641. l:=paralen;
  642. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  643. end;
  644. paraloc^.size:=int_cgsize(l);
  645. end;
  646. if side=callerside then
  647. paraloc^.reference.index:=NR_STACK_POINTER_REG
  648. else
  649. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  650. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  651. paraloc^.reference.offset:=parasize;
  652. if side=calleeside then
  653. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  654. parasize:=align(parasize+l,varalign);
  655. dec(paralen,l);
  656. firstparaloc:=false;
  657. end;
  658. end;
  659. end;
  660. end;
  661. case pass of
  662. 1:
  663. begin
  664. if i=paras.count-1 then
  665. break;
  666. inc(i);
  667. end;
  668. 2:
  669. begin
  670. if i=0 then
  671. break;
  672. dec(i);
  673. end;
  674. end;
  675. end;
  676. end;
  677. end;
  678. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  679. var
  680. parasize,
  681. parareg : longint;
  682. begin
  683. parasize:=0;
  684. parareg:=0;
  685. case p.proccalloption of
  686. pocall_register :
  687. create_register_paraloc_info(p,side,p.paras,parareg,parasize);
  688. pocall_internproc :
  689. begin
  690. { Use default calling }
  691. {$warnings off}
  692. if (pocall_default=pocall_register) then
  693. create_register_paraloc_info(p,side,p.paras,parareg,parasize)
  694. else
  695. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  696. {$warnings on}
  697. end;
  698. else
  699. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  700. end;
  701. create_funcretloc_info(p,side);
  702. result:=parasize;
  703. end;
  704. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  705. var
  706. parasize : longint;
  707. begin
  708. parasize:=0;
  709. { calculate the registers for the normal parameters }
  710. create_stdcall_paraloc_info(p,callerside,p.paras,parasize);
  711. { append the varargs }
  712. create_stdcall_paraloc_info(p,callerside,varargspara,parasize);
  713. result:=parasize;
  714. end;
  715. procedure tcpuparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  716. begin
  717. { Never a need for temps when value is pushed (calls inside parameters
  718. will simply allocate even more stack space for their parameters) }
  719. if not(use_fixed_stack) then
  720. can_use_final_stack_loc:=true;
  721. inherited createtempparaloc(list,calloption,parasym,can_use_final_stack_loc,cgpara);
  722. end;
  723. begin
  724. paramanager:=tcpuparamanager.create;
  725. end.