cpupara.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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; side: tcallercallee; 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. pocall_pascal:
  243. result:=[RS_EAX,RS_EDX,RS_ECX];
  244. pocall_far16,
  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. saveregs_oldfpccall : array[0..0] of tsuperregister = (RS_EBP);
  263. begin
  264. case calloption of
  265. pocall_internproc,
  266. pocall_register,
  267. pocall_safecall,
  268. pocall_stdcall,
  269. pocall_cdecl,
  270. pocall_syscall,
  271. pocall_cppdecl,
  272. pocall_mwpascal,
  273. pocall_pascal:
  274. result:=saveregs;
  275. pocall_far16,
  276. pocall_oldfpccall :
  277. result:=saveregs_oldfpccall;
  278. else
  279. internalerror(2018050401);
  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. { syscall for AROS can have already a paraloc set }
  408. if (vo_has_explicit_paraloc in hp.varoptions) then
  409. begin
  410. if not(vo_is_syscall_lib in hp.varoptions) then
  411. internalerror(2016090105);
  412. if p.proccalloption in pushleftright_pocalls then
  413. dec(i)
  414. else
  415. inc(i);
  416. continue;
  417. end;
  418. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  419. if pushaddr then
  420. begin
  421. paralen:=sizeof(aint);
  422. paracgsize:=OS_ADDR;
  423. paradef:=cpointerdef.getreusable_no_free(paradef);
  424. end
  425. else
  426. begin
  427. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  428. paracgsize:=def_cgsize(paradef);
  429. end;
  430. hp.paraloc[side].reset;
  431. hp.paraloc[side].size:=paracgsize;
  432. hp.paraloc[side].intsize:=paralen;
  433. hp.paraloc[side].def:=paradef;
  434. hp.paraloc[side].Alignment:=paraalign;
  435. { darwin/x86 requires that parameters < sizeof(aint) are sign/ }
  436. { zero extended to sizeof(aint) }
  437. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  438. (side = callerside) and
  439. (paralen > 0) and
  440. (paralen < sizeof(aint)) then
  441. begin
  442. paracgsize:=OS_SINT;
  443. paradef:=sinttype;
  444. end;
  445. { Copy to stack? }
  446. if (paracgsize=OS_NO) or
  447. (use_fixed_stack) then
  448. begin
  449. paraloc:=hp.paraloc[side].add_location;
  450. paraloc^.loc:=LOC_REFERENCE;
  451. paraloc^.size:=paracgsize;
  452. paraloc^.def:=paradef;
  453. if side=callerside then
  454. paraloc^.reference.index:=NR_STACK_POINTER_REG
  455. else
  456. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  457. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  458. { don't let push_size return 16, because then we can }
  459. { read past the end of the heap since the value is only }
  460. { 10 bytes long (JM) }
  461. if (paracgsize = OS_F80) and
  462. (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  463. paralen:=16;
  464. paraloc^.reference.offset:=parasize;
  465. if side=calleeside then
  466. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  467. parasize:=align(parasize+paralen,varalign);
  468. end
  469. else
  470. begin
  471. if paralen=0 then
  472. internalerror(200501163);
  473. firstparaloc:=true;
  474. while (paralen>0) do
  475. begin
  476. paraloc:=hp.paraloc[side].add_location;
  477. paraloc^.loc:=LOC_REFERENCE;
  478. { single and double need a single location }
  479. if (paracgsize in [OS_F64,OS_F32]) then
  480. begin
  481. paraloc^.size:=paracgsize;
  482. paraloc^.def:=paradef;
  483. l:=paralen;
  484. end
  485. else
  486. begin
  487. { We can allocate at maximum 32 bits per location }
  488. if paralen>sizeof(aint) then
  489. begin
  490. l:=sizeof(aint);
  491. paraloc^.def:=uinttype;
  492. end
  493. else
  494. begin
  495. l:=paralen;
  496. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  497. end;
  498. paraloc^.size:=int_cgsize(l);
  499. end;
  500. if (side=callerside) or
  501. (po_nostackframe in p.procoptions) then
  502. paraloc^.reference.index:=NR_STACK_POINTER_REG
  503. else
  504. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  505. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  506. paraloc^.reference.offset:=parasize;
  507. if side=calleeside then
  508. if not(po_nostackframe in p.procoptions) then
  509. inc(paraloc^.reference.offset,target_info.first_parm_offset)
  510. else
  511. { return addres }
  512. inc(paraloc^.reference.offset,4);
  513. parasize:=align(parasize+l,varalign);
  514. dec(paralen,l);
  515. firstparaloc:=false;
  516. end;
  517. end;
  518. if p.proccalloption in pushleftright_pocalls then
  519. dec(i)
  520. else
  521. inc(i);
  522. end;
  523. end;
  524. procedure tcpuparamanager.create_register_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  525. var parareg,parasize:longint);
  526. var
  527. hp : tparavarsym;
  528. paradef : tdef;
  529. paraloc : pcgparalocation;
  530. paracgsize : tcgsize;
  531. i : integer;
  532. l,
  533. paralen,
  534. varalign : longint;
  535. paraalign : shortint;
  536. pass : byte;
  537. firstparaloc,
  538. pushaddr : boolean;
  539. begin
  540. if paras.count=0 then
  541. exit;
  542. paraalign:=get_para_align(p.proccalloption);
  543. { clean up here so we can later detect properly if a parameter has been
  544. assigned or not
  545. }
  546. for i:=0 to paras.count-1 do
  547. tparavarsym(paras[i]).paraloc[side].reset;
  548. { Register parameters are assigned from left to right,
  549. stack parameters from right to left so assign first the
  550. register parameters in a first pass, in the second
  551. pass all unhandled parameters are done }
  552. for pass:=1 to 2 do
  553. begin
  554. if pass=1 then
  555. i:=0
  556. else
  557. i:=paras.count-1;
  558. while true do
  559. begin
  560. hp:=tparavarsym(paras[i]);
  561. paradef:=hp.vardef;
  562. if not(assigned(hp.paraloc[side].location)) then
  563. begin
  564. pushaddr:=push_addr_param(hp.varspez,hp.vardef,p.proccalloption);
  565. if pushaddr then
  566. begin
  567. paralen:=sizeof(aint);
  568. paracgsize:=OS_ADDR;
  569. paradef:=cpointerdef.getreusable_no_free(paradef);
  570. end
  571. else
  572. begin
  573. paralen:=push_size(hp.varspez,hp.vardef,p.proccalloption);
  574. paracgsize:=def_cgsize(hp.vardef);
  575. end;
  576. hp.paraloc[side].size:=paracgsize;
  577. hp.paraloc[side].intsize:=paralen;
  578. hp.paraloc[side].Alignment:=paraalign;
  579. hp.paraloc[side].def:=paradef;
  580. {
  581. EAX
  582. EDX
  583. ECX
  584. Stack
  585. Stack
  586. 64bit values,floats,arrays and records are always
  587. on the stack.
  588. In case of po_delphi_nested_cc, the parent frame pointer
  589. is also always passed on the stack.
  590. }
  591. if (parareg<=high(parasupregs)) and
  592. (paralen<=sizeof(aint)) and
  593. (not(hp.vardef.typ in [floatdef,recorddef,arraydef]) or
  594. pushaddr or
  595. is_dynamic_array(hp.vardef)) and
  596. (not(vo_is_parentfp in hp.varoptions) or
  597. not(po_delphi_nested_cc in p.procoptions)) then
  598. begin
  599. if pass=1 then
  600. begin
  601. paraloc:=hp.paraloc[side].add_location;
  602. paraloc^.size:=paracgsize;
  603. paraloc^.def:=paradef;
  604. paraloc^.loc:=LOC_REGISTER;
  605. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[parareg],cgsize2subreg(R_INTREGISTER,paracgsize));
  606. inc(parareg);
  607. end;
  608. end
  609. else
  610. if pass=2 then
  611. begin
  612. { Copy to stack? }
  613. if (use_fixed_stack) or
  614. (paracgsize=OS_NO) then
  615. begin
  616. paraloc:=hp.paraloc[side].add_location;
  617. paraloc^.loc:=LOC_REFERENCE;
  618. paraloc^.size:=paracgsize;
  619. paraloc^.def:=paradef;
  620. if side=callerside then
  621. paraloc^.reference.index:=NR_STACK_POINTER_REG
  622. else
  623. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  624. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  625. paraloc^.reference.offset:=parasize;
  626. if side=calleeside then
  627. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  628. parasize:=align(parasize+paralen,varalign);
  629. end
  630. else
  631. begin
  632. if paralen=0 then
  633. internalerror(200501163);
  634. firstparaloc:=true;
  635. while (paralen>0) do
  636. begin
  637. paraloc:=hp.paraloc[side].add_location;
  638. paraloc^.loc:=LOC_REFERENCE;
  639. { Extended and double need a single location }
  640. if (paracgsize in [OS_F64,OS_F32]) then
  641. begin
  642. paraloc^.size:=paracgsize;
  643. paraloc^.def:=paradef;
  644. l:=paralen;
  645. end
  646. else
  647. begin
  648. { We can allocate at maximum 32 bits per location }
  649. if paralen>sizeof(aint) then
  650. begin
  651. l:=sizeof(aint);
  652. paraloc^.def:=uinttype;
  653. end
  654. else
  655. begin
  656. l:=paralen;
  657. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  658. end;
  659. paraloc^.size:=int_cgsize(l);
  660. end;
  661. if side=callerside then
  662. paraloc^.reference.index:=NR_STACK_POINTER_REG
  663. else
  664. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  665. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  666. paraloc^.reference.offset:=parasize;
  667. if side=calleeside then
  668. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  669. parasize:=align(parasize+l,varalign);
  670. dec(paralen,l);
  671. firstparaloc:=false;
  672. end;
  673. end;
  674. end;
  675. end;
  676. case pass of
  677. 1:
  678. begin
  679. if i=paras.count-1 then
  680. break;
  681. inc(i);
  682. end;
  683. 2:
  684. begin
  685. if i=0 then
  686. break;
  687. dec(i);
  688. end;
  689. end;
  690. end;
  691. end;
  692. end;
  693. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  694. var
  695. parasize,
  696. parareg : longint;
  697. begin
  698. parasize:=0;
  699. parareg:=0;
  700. case p.proccalloption of
  701. pocall_register :
  702. create_register_paraloc_info(p,side,p.paras,parareg,parasize);
  703. pocall_internproc :
  704. begin
  705. { Use default calling }
  706. {$warnings off}
  707. if (pocall_default=pocall_register) then
  708. create_register_paraloc_info(p,side,p.paras,parareg,parasize)
  709. else
  710. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  711. {$warnings on}
  712. end;
  713. else
  714. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  715. end;
  716. create_funcretloc_info(p,side);
  717. result:=parasize;
  718. end;
  719. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  720. var
  721. parasize : longint;
  722. begin
  723. parasize:=0;
  724. { calculate the registers for the normal parameters }
  725. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  726. { append the varargs }
  727. if assigned(varargspara) then
  728. begin
  729. if side=callerside then
  730. create_stdcall_paraloc_info(p,side,varargspara,parasize)
  731. else
  732. internalerror(2019021926);
  733. end;
  734. create_funcretloc_info(p,side);
  735. result:=parasize;
  736. end;
  737. procedure tcpuparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  738. begin
  739. { Never a need for temps when value is pushed (calls inside parameters
  740. will simply allocate even more stack space for their parameters) }
  741. if not(use_fixed_stack) then
  742. can_use_final_stack_loc:=true;
  743. inherited createtempparaloc(list,calloption,parasym,can_use_final_stack_loc,cgpara);
  744. end;
  745. begin
  746. paramanager:=tcpuparamanager.create;
  747. end.