cpupara.pas 31 KB

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