cpupara.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. function parseparaloc(p : tparavarsym;const s : string) : boolean;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 [pocall_cdecl,pocall_cppdecl]) 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 [pocall_cdecl,pocall_cppdecl]) 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. result:=[RS_EAX,RS_EDX,RS_ECX];
  243. pocall_far16,
  244. pocall_pascal,
  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.parseparaloc(p : tparavarsym;const s : string) : boolean;
  260. var
  261. paraloc : pcgparalocation;
  262. begin
  263. result:=false;
  264. case target_info.system of
  265. system_i386_aros:
  266. begin
  267. p.paraloc[callerside].alignment:=4;
  268. paraloc:=p.paraloc[callerside].add_location;
  269. paraloc^.loc:=LOC_REGISTER;
  270. paraloc^.size:=def_cgsize(p.vardef);
  271. paraloc^.def:=p.vardef;
  272. paraloc^.register:=std_regnum_search(lowercase(s));
  273. if paraloc^.register = NR_NO then
  274. exit;
  275. { copy to callee side }
  276. p.paraloc[calleeside].add_location^:=paraloc^;
  277. end;
  278. else
  279. internalerror(2016090103);
  280. end;
  281. result:=true;
  282. end;
  283. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): TCGPara;
  284. var
  285. retcgsize : tcgsize;
  286. paraloc : pcgparalocation;
  287. fdef,
  288. usedef: tdef;
  289. handled: boolean;
  290. begin
  291. if not assigned(forcetempdef) then
  292. usedef:=p.returndef
  293. else
  294. usedef:=forcetempdef;
  295. { on darwin/i386, if a record has only one field and that field is a
  296. single or double, it has to be returned like a single/double }
  297. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,
  298. system_i386_freebsd,system_i386_openbsd,
  299. system_i386_os2,system_i386_emx]) and
  300. ((usedef.typ=recorddef) or
  301. is_object(usedef)) and
  302. tabstractrecordsymtable(tabstractrecorddef(usedef).symtable).has_single_field(fdef) and
  303. (fdef.typ=floatdef) and
  304. (tfloatdef(fdef).floattype in [s32real,s64real]) then
  305. usedef:=fdef;
  306. handled:=set_common_funcretloc_info(p,usedef,retcgsize,result);
  307. { normally forcetempdef is passed straight through to
  308. set_common_funcretloc_info and that one will correctly determine whether
  309. the location is a temporary one, but that doesn't work here because we
  310. sometimes have to change the type }
  311. result.temporary:=assigned(forcetempdef);
  312. if handled then
  313. exit;
  314. { darwin/x86 requires that results < sizeof(aint) are sign/zero
  315. extended to sizeof(aint) }
  316. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  317. (side=calleeside) and
  318. (result.intsize>0) and
  319. (result.intsize<sizeof(aint)) then
  320. begin
  321. result.def:=sinttype;
  322. result.intsize:=sizeof(aint);
  323. retcgsize:=OS_SINT;
  324. result.size:=retcgsize;
  325. end;
  326. { Return in FPU register? }
  327. if result.def.typ=floatdef then
  328. begin
  329. paraloc:=result.add_location;
  330. paraloc^.loc:=LOC_FPUREGISTER;
  331. paraloc^.register:=NR_FPU_RESULT_REG;
  332. paraloc^.size:=retcgsize;
  333. paraloc^.def:=result.def;
  334. end
  335. else
  336. { Return in register }
  337. begin
  338. paraloc:=result.add_location;
  339. paraloc^.loc:=LOC_REGISTER;
  340. if retcgsize in [OS_64,OS_S64] then
  341. begin
  342. { low 32bits }
  343. if side=callerside then
  344. paraloc^.register:=NR_FUNCTION_RESULT64_LOW_REG
  345. else
  346. paraloc^.register:=NR_FUNCTION_RETURN64_LOW_REG;
  347. paraloc^.size:=OS_32;
  348. paraloc^.def:=u32inttype;
  349. { high 32bits }
  350. paraloc:=result.add_location;
  351. paraloc^.loc:=LOC_REGISTER;
  352. if side=callerside then
  353. paraloc^.register:=NR_FUNCTION_RESULT64_HIGH_REG
  354. else
  355. paraloc^.register:=NR_FUNCTION_RETURN64_HIGH_REG;
  356. paraloc^.size:=OS_32;
  357. paraloc^.def:=u32inttype;
  358. end
  359. else
  360. begin
  361. paraloc^.size:=retcgsize;
  362. paraloc^.def:=result.def;
  363. if side=callerside then
  364. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RESULT_REG,cgsize2subreg(R_INTREGISTER,retcgsize))
  365. else
  366. paraloc^.register:=newreg(R_INTREGISTER,RS_FUNCTION_RETURN_REG,cgsize2subreg(R_INTREGISTER,retcgsize));
  367. end;
  368. end;
  369. end;
  370. procedure tcpuparamanager.create_stdcall_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;var parasize:longint);
  371. var
  372. i : integer;
  373. hp : tparavarsym;
  374. paradef : tdef;
  375. paraloc : pcgparalocation;
  376. l,
  377. paralen,
  378. varalign : longint;
  379. paraalign : shortint;
  380. paracgsize : tcgsize;
  381. firstparaloc,
  382. pushaddr : boolean;
  383. begin
  384. paraalign:=get_para_align(p.proccalloption);
  385. { we push Flags and CS as long
  386. to cope with the IRETD
  387. and we save 6 register + 4 selectors }
  388. if po_interrupt in p.procoptions then
  389. inc(parasize,8+6*4+4*2);
  390. { Offset is calculated like:
  391. sub esp,12
  392. mov [esp+8],para3
  393. mov [esp+4],para2
  394. mov [esp],para1
  395. call function
  396. That means for pushes the para with the
  397. highest offset (see para3) needs to be pushed first
  398. }
  399. if p.proccalloption in pushleftright_pocalls then
  400. i:=paras.count-1
  401. else
  402. i:=0;
  403. while ((p.proccalloption in pushleftright_pocalls) and (i>=0)) or
  404. (not(p.proccalloption in pushleftright_pocalls) and (i<=paras.count-1)) do
  405. begin
  406. hp:=tparavarsym(paras[i]);
  407. paradef:=hp.vardef;
  408. { syscall for AROS can have already a paraloc set }
  409. if (vo_has_explicit_paraloc in hp.varoptions) then
  410. begin
  411. if not(vo_is_syscall_lib in hp.varoptions) then
  412. internalerror(2016090105);
  413. if p.proccalloption in pushleftright_pocalls then
  414. dec(i)
  415. else
  416. inc(i);
  417. continue;
  418. end;
  419. pushaddr:=push_addr_param(hp.varspez,paradef,p.proccalloption);
  420. if pushaddr then
  421. begin
  422. paralen:=sizeof(aint);
  423. paracgsize:=OS_ADDR;
  424. paradef:=cpointerdef.getreusable_no_free(paradef);
  425. end
  426. else
  427. begin
  428. paralen:=push_size(hp.varspez,paradef,p.proccalloption);
  429. { darwin/x86 requires that parameters < sizeof(aint) are sign/ }
  430. { zero extended to sizeof(aint) }
  431. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  432. (side = callerside) and
  433. (paralen > 0) and
  434. (paralen < sizeof(aint)) then
  435. begin
  436. paralen:=sizeof(aint);
  437. paracgsize:=OS_SINT;
  438. paradef:=sinttype;
  439. end
  440. else
  441. paracgsize:=def_cgsize(paradef);
  442. end;
  443. hp.paraloc[side].reset;
  444. hp.paraloc[side].size:=paracgsize;
  445. hp.paraloc[side].intsize:=paralen;
  446. hp.paraloc[side].def:=paradef;
  447. hp.paraloc[side].Alignment:=paraalign;
  448. { Copy to stack? }
  449. if (paracgsize=OS_NO) or
  450. (use_fixed_stack) then
  451. begin
  452. paraloc:=hp.paraloc[side].add_location;
  453. paraloc^.loc:=LOC_REFERENCE;
  454. paraloc^.size:=paracgsize;
  455. paraloc^.def:=paradef;
  456. if side=callerside then
  457. paraloc^.reference.index:=NR_STACK_POINTER_REG
  458. else
  459. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  460. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  461. { don't let push_size return 16, because then we can }
  462. { read past the end of the heap since the value is only }
  463. { 10 bytes long (JM) }
  464. if (paracgsize = OS_F80) and
  465. (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  466. paralen:=16;
  467. paraloc^.reference.offset:=parasize;
  468. if side=calleeside then
  469. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  470. parasize:=align(parasize+paralen,varalign);
  471. end
  472. else
  473. begin
  474. if paralen=0 then
  475. internalerror(200501163);
  476. firstparaloc:=true;
  477. while (paralen>0) do
  478. begin
  479. paraloc:=hp.paraloc[side].add_location;
  480. paraloc^.loc:=LOC_REFERENCE;
  481. { single and double need a single location }
  482. if (paracgsize in [OS_F64,OS_F32]) then
  483. begin
  484. paraloc^.size:=paracgsize;
  485. paraloc^.def:=paradef;
  486. l:=paralen;
  487. end
  488. else
  489. begin
  490. { We can allocate at maximum 32 bits per location }
  491. if paralen>sizeof(aint) then
  492. begin
  493. l:=sizeof(aint);
  494. paraloc^.def:=uinttype;
  495. end
  496. else
  497. begin
  498. l:=paralen;
  499. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  500. end;
  501. paraloc^.size:=int_cgsize(l);
  502. end;
  503. if (side=callerside) or
  504. (po_nostackframe in p.procoptions) then
  505. paraloc^.reference.index:=NR_STACK_POINTER_REG
  506. else
  507. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  508. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  509. paraloc^.reference.offset:=parasize;
  510. if side=calleeside then
  511. if not(po_nostackframe in p.procoptions) then
  512. inc(paraloc^.reference.offset,target_info.first_parm_offset)
  513. else
  514. { return addres }
  515. inc(paraloc^.reference.offset,4);
  516. parasize:=align(parasize+l,varalign);
  517. dec(paralen,l);
  518. firstparaloc:=false;
  519. end;
  520. end;
  521. if p.proccalloption in pushleftright_pocalls then
  522. dec(i)
  523. else
  524. inc(i);
  525. end;
  526. end;
  527. procedure tcpuparamanager.create_register_paraloc_info(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  528. var parareg,parasize:longint);
  529. var
  530. hp : tparavarsym;
  531. paradef : tdef;
  532. paraloc : pcgparalocation;
  533. paracgsize : tcgsize;
  534. i : integer;
  535. l,
  536. paralen,
  537. varalign : longint;
  538. paraalign : shortint;
  539. pass : byte;
  540. firstparaloc,
  541. pushaddr : boolean;
  542. begin
  543. if paras.count=0 then
  544. exit;
  545. paraalign:=get_para_align(p.proccalloption);
  546. { clean up here so we can later detect properly if a parameter has been
  547. assigned or not
  548. }
  549. for i:=0 to paras.count-1 do
  550. tparavarsym(paras[i]).paraloc[side].reset;
  551. { Register parameters are assigned from left to right,
  552. stack parameters from right to left so assign first the
  553. register parameters in a first pass, in the second
  554. pass all unhandled parameters are done }
  555. for pass:=1 to 2 do
  556. begin
  557. if pass=1 then
  558. i:=0
  559. else
  560. i:=paras.count-1;
  561. while true do
  562. begin
  563. hp:=tparavarsym(paras[i]);
  564. paradef:=hp.vardef;
  565. if not(assigned(hp.paraloc[side].location)) then
  566. begin
  567. pushaddr:=push_addr_param(hp.varspez,hp.vardef,p.proccalloption);
  568. if pushaddr then
  569. begin
  570. paralen:=sizeof(aint);
  571. paracgsize:=OS_ADDR;
  572. paradef:=cpointerdef.getreusable_no_free(paradef);
  573. end
  574. else
  575. begin
  576. paralen:=push_size(hp.varspez,hp.vardef,p.proccalloption);
  577. paracgsize:=def_cgsize(hp.vardef);
  578. end;
  579. hp.paraloc[side].size:=paracgsize;
  580. hp.paraloc[side].intsize:=paralen;
  581. hp.paraloc[side].Alignment:=paraalign;
  582. hp.paraloc[side].def:=paradef;
  583. {
  584. EAX
  585. EDX
  586. ECX
  587. Stack
  588. Stack
  589. 64bit values,floats,arrays and records are always
  590. on the stack.
  591. In case of po_delphi_nested_cc, the parent frame pointer
  592. is also always passed on the stack.
  593. }
  594. if (parareg<=high(parasupregs)) and
  595. (paralen<=sizeof(aint)) and
  596. (not(hp.vardef.typ in [floatdef,recorddef,arraydef]) or
  597. pushaddr or
  598. is_dynamic_array(hp.vardef)) and
  599. (not(vo_is_parentfp in hp.varoptions) or
  600. not(po_delphi_nested_cc in p.procoptions)) then
  601. begin
  602. if pass=1 then
  603. begin
  604. paraloc:=hp.paraloc[side].add_location;
  605. paraloc^.size:=paracgsize;
  606. paraloc^.def:=paradef;
  607. paraloc^.loc:=LOC_REGISTER;
  608. paraloc^.register:=newreg(R_INTREGISTER,parasupregs[parareg],cgsize2subreg(R_INTREGISTER,paracgsize));
  609. inc(parareg);
  610. end;
  611. end
  612. else
  613. if pass=2 then
  614. begin
  615. { Copy to stack? }
  616. if (use_fixed_stack) or
  617. (paracgsize=OS_NO) then
  618. begin
  619. paraloc:=hp.paraloc[side].add_location;
  620. paraloc^.loc:=LOC_REFERENCE;
  621. paraloc^.size:=paracgsize;
  622. paraloc^.def:=paradef;
  623. if side=callerside then
  624. paraloc^.reference.index:=NR_STACK_POINTER_REG
  625. else
  626. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  627. varalign:=used_align(size_2_align(paralen),paraalign,paraalign);
  628. paraloc^.reference.offset:=parasize;
  629. if side=calleeside then
  630. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  631. parasize:=align(parasize+paralen,varalign);
  632. end
  633. else
  634. begin
  635. if paralen=0 then
  636. internalerror(200501163);
  637. firstparaloc:=true;
  638. while (paralen>0) do
  639. begin
  640. paraloc:=hp.paraloc[side].add_location;
  641. paraloc^.loc:=LOC_REFERENCE;
  642. { Extended and double need a single location }
  643. if (paracgsize in [OS_F64,OS_F32]) then
  644. begin
  645. paraloc^.size:=paracgsize;
  646. paraloc^.def:=paradef;
  647. l:=paralen;
  648. end
  649. else
  650. begin
  651. { We can allocate at maximum 32 bits per location }
  652. if paralen>sizeof(aint) then
  653. begin
  654. l:=sizeof(aint);
  655. paraloc^.def:=uinttype;
  656. end
  657. else
  658. begin
  659. l:=paralen;
  660. paraloc^.def:=get_paraloc_def(paradef,l,firstparaloc);
  661. end;
  662. paraloc^.size:=int_cgsize(l);
  663. end;
  664. if side=callerside then
  665. paraloc^.reference.index:=NR_STACK_POINTER_REG
  666. else
  667. paraloc^.reference.index:=NR_FRAME_POINTER_REG;
  668. varalign:=used_align(size_2_align(l),paraalign,paraalign);
  669. paraloc^.reference.offset:=parasize;
  670. if side=calleeside then
  671. inc(paraloc^.reference.offset,target_info.first_parm_offset);
  672. parasize:=align(parasize+l,varalign);
  673. dec(paralen,l);
  674. firstparaloc:=false;
  675. end;
  676. end;
  677. end;
  678. end;
  679. case pass of
  680. 1:
  681. begin
  682. if i=paras.count-1 then
  683. break;
  684. inc(i);
  685. end;
  686. 2:
  687. begin
  688. if i=0 then
  689. break;
  690. dec(i);
  691. end;
  692. end;
  693. end;
  694. end;
  695. end;
  696. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  697. var
  698. parasize,
  699. parareg : longint;
  700. begin
  701. parasize:=0;
  702. parareg:=0;
  703. case p.proccalloption of
  704. pocall_register :
  705. create_register_paraloc_info(p,side,p.paras,parareg,parasize);
  706. pocall_internproc :
  707. begin
  708. { Use default calling }
  709. {$warnings off}
  710. if (pocall_default=pocall_register) then
  711. create_register_paraloc_info(p,side,p.paras,parareg,parasize)
  712. else
  713. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  714. {$warnings on}
  715. end;
  716. else
  717. create_stdcall_paraloc_info(p,side,p.paras,parasize);
  718. end;
  719. create_funcretloc_info(p,side);
  720. result:=parasize;
  721. end;
  722. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  723. var
  724. parasize : longint;
  725. begin
  726. parasize:=0;
  727. { calculate the registers for the normal parameters }
  728. create_stdcall_paraloc_info(p,callerside,p.paras,parasize);
  729. { append the varargs }
  730. create_stdcall_paraloc_info(p,callerside,varargspara,parasize);
  731. result:=parasize;
  732. end;
  733. procedure tcpuparamanager.createtempparaloc(list: TAsmList;calloption : tproccalloption;parasym : tparavarsym;can_use_final_stack_loc : boolean;var cgpara:TCGPara);
  734. begin
  735. { Never a need for temps when value is pushed (calls inside parameters
  736. will simply allocate even more stack space for their parameters) }
  737. if not(use_fixed_stack) then
  738. can_use_final_stack_loc:=true;
  739. inherited createtempparaloc(list,calloption,parasym,can_use_final_stack_loc,cgpara);
  740. end;
  741. begin
  742. paramanager:=tcpuparamanager.create;
  743. end.