cpupara.pas 29 KB

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