cpupara.pas 31 KB

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