cpupara.pas 30 KB

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