ncgcal.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for call nodes
  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 ncgcal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. globtype,
  23. parabase,cgutils,
  24. symdef,node,ncal;
  25. type
  26. tcgcallparanode = class(tcallparanode)
  27. private
  28. tempcgpara : tcgpara;
  29. procedure push_addr_para;
  30. procedure push_value_para;
  31. public
  32. constructor create(expr,next : tnode);override;
  33. destructor destroy;override;
  34. procedure secondcallparan;override;
  35. end;
  36. tcgcallnode = class(tcallnode)
  37. private
  38. procedure handle_return_value;
  39. procedure release_unused_return_value;
  40. procedure release_para_temps;
  41. procedure pushparas;
  42. procedure freeparas;
  43. protected
  44. retloc: tcgpara;
  45. framepointer_paraloc : tcgpara;
  46. {# This routine is used to push the current frame pointer
  47. on the stack. This is used in nested routines where the
  48. value of the frame pointer is always pushed as an extra
  49. parameter.
  50. The default handling is the standard handling used on
  51. most stack based machines, where the frame pointer is
  52. the first invisible parameter.
  53. }
  54. procedure pop_parasize(pop_size:longint);virtual;
  55. procedure extra_interrupt_code;virtual;
  56. procedure extra_call_code;virtual;
  57. procedure extra_post_call_code;virtual;
  58. procedure do_syscall;virtual;abstract;
  59. { The function result is returned in a tcgpara. This tcgpara has to
  60. be translated into a tlocation so the rest of the code generator
  61. can work with it. This routine decides what the most appropriate
  62. tlocation is and sets self.location based on that. }
  63. procedure set_result_location(realresdef: tstoreddef);virtual;
  64. public
  65. procedure pass_generate_code;override;
  66. destructor destroy;override;
  67. end;
  68. implementation
  69. uses
  70. systems,
  71. cutils,verbose,globals,
  72. cpuinfo,
  73. symconst,symtable,defutil,paramgr,
  74. cgbase,pass_2,
  75. aasmbase,aasmtai,aasmdata,
  76. nbas,nmem,nld,ncnv,nutils,
  77. {$ifdef x86}
  78. cga,cgx86,aasmcpu,
  79. {$endif x86}
  80. ncgutil,
  81. cgobj,tgobj,
  82. procinfo,
  83. wpobase;
  84. {*****************************************************************************
  85. TCGCALLPARANODE
  86. *****************************************************************************}
  87. constructor tcgcallparanode.create(expr,next : tnode);
  88. begin
  89. inherited create(expr,next);
  90. tempcgpara.init;
  91. end;
  92. destructor tcgcallparanode.destroy;
  93. begin
  94. tempcgpara.done;
  95. inherited destroy;
  96. end;
  97. procedure tcgcallparanode.push_addr_para;
  98. begin
  99. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  100. internalerror(200304235);
  101. cg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  102. end;
  103. procedure tcgcallparanode.push_value_para;
  104. begin
  105. { we've nothing to push when the size of the parameter is 0 }
  106. if left.resultdef.size=0 then
  107. exit;
  108. { Move flags and jump in register to make it less complex }
  109. if left.location.loc in [LOC_FLAGS,LOC_JUMP,LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF] then
  110. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),false);
  111. { load the parameter's tlocation into its cgpara }
  112. gen_load_loc_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location,tempcgpara)
  113. end;
  114. procedure tcgcallparanode.secondcallparan;
  115. var
  116. href : treference;
  117. otlabel,
  118. oflabel : tasmlabel;
  119. begin
  120. if not(assigned(parasym)) then
  121. internalerror(200304242);
  122. { Skip nothingn nodes which are used after disabling
  123. a parameter }
  124. if (left.nodetype<>nothingn) then
  125. begin
  126. otlabel:=current_procinfo.CurrTrueLabel;
  127. oflabel:=current_procinfo.CurrFalseLabel;
  128. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  129. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  130. secondpass(left);
  131. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  132. { release memory for refcnt out parameters }
  133. if (parasym.varspez=vs_out) and
  134. is_managed_type(left.resultdef) then
  135. begin
  136. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false,sizeof(pint));
  137. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  138. end;
  139. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,tempcgpara);
  140. { handle varargs first, because parasym is not valid }
  141. if (cpf_varargs_para in callparaflags) then
  142. begin
  143. if paramanager.push_addr_param(vs_value,left.resultdef,
  144. aktcallnode.procdefinition.proccalloption) then
  145. push_addr_para
  146. else
  147. push_value_para;
  148. end
  149. { hidden parameters }
  150. else if (vo_is_hidden_para in parasym.varoptions) then
  151. begin
  152. { don't push a node that already generated a pointer type
  153. by address for implicit hidden parameters }
  154. if (vo_is_funcret in parasym.varoptions) or
  155. { pass "this" in C++ classes explicitly as pointer
  156. because push_addr_param might not be true for them }
  157. (is_cppclass(parasym.vardef) and (vo_is_self in parasym.varoptions)) or
  158. (not(left.resultdef.typ in [pointerdef,classrefdef]) and
  159. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  160. aktcallnode.procdefinition.proccalloption)) then
  161. push_addr_para
  162. else
  163. push_value_para;
  164. end
  165. { formal def }
  166. else if (parasym.vardef.typ=formaldef) then
  167. begin
  168. { allow passing of a constant to a const formaldef }
  169. if (parasym.varspez=vs_const) and
  170. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  171. location_force_mem(current_asmdata.CurrAsmList,left.location);
  172. push_addr_para;
  173. end
  174. { Normal parameter }
  175. else
  176. begin
  177. { don't push a node that already generated a pointer type
  178. by address for implicit hidden parameters }
  179. if (not(
  180. (vo_is_hidden_para in parasym.varoptions) and
  181. (left.resultdef.typ in [pointerdef,classrefdef])
  182. ) and
  183. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  184. aktcallnode.procdefinition.proccalloption)) and
  185. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  186. not(
  187. is_array_of_const(parasym.vardef) and
  188. is_dynamic_array(left.resultdef)
  189. ) then
  190. begin
  191. { Passing a var parameter to a var parameter, we can
  192. just push the address transparently }
  193. if (left.nodetype=loadn) and
  194. (tloadnode(left).is_addr_param_load) then
  195. begin
  196. if (left.location.reference.index<>NR_NO) or
  197. (left.location.reference.offset<>0) then
  198. internalerror(200410107);
  199. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_ADDR,left.location.reference.base,tempcgpara)
  200. end
  201. else
  202. begin
  203. { Force to be in memory }
  204. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  205. location_force_mem(current_asmdata.CurrAsmList,left.location);
  206. push_addr_para;
  207. end;
  208. end
  209. else
  210. push_value_para;
  211. end;
  212. current_procinfo.CurrTrueLabel:=otlabel;
  213. current_procinfo.CurrFalseLabel:=oflabel;
  214. { update return location in callnode when this is the function
  215. result }
  216. if assigned(parasym) and
  217. (vo_is_funcret in parasym.varoptions) then
  218. location_copy(aktcallnode.location,left.location);
  219. end;
  220. { next parameter }
  221. if assigned(right) then
  222. tcallparanode(right).secondcallparan;
  223. end;
  224. {*****************************************************************************
  225. TCGCALLNODE
  226. *****************************************************************************}
  227. procedure tcgcallnode.extra_interrupt_code;
  228. begin
  229. end;
  230. procedure tcgcallnode.extra_call_code;
  231. begin
  232. end;
  233. procedure tcgcallnode.extra_post_call_code;
  234. begin
  235. end;
  236. procedure tcgcallnode.set_result_location(realresdef: tstoreddef);
  237. begin
  238. if realresdef.is_intregable or
  239. realresdef.is_fpuregable or
  240. { avoid temporarily storing pointer-sized entities that can't be
  241. regvars, such as reference-counted pointers, to memory --
  242. no exception can occur right now (except in case of existing
  243. memory corruption), and we'd store them to a regular temp
  244. anyway and that is not safer than keeping them in a register }
  245. ((realresdef.size=sizeof(aint)) and
  246. (retloc.location^.loc=LOC_REGISTER) and
  247. not assigned(retloc.location^.next)) then
  248. location_allocate_register(current_asmdata.CurrAsmList,location,realresdef,false)
  249. else
  250. begin
  251. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),0);
  252. tg.GetTemp(current_asmdata.CurrAsmList,retloc.intsize,retloc.Alignment,tt_normal,location.reference);
  253. end;
  254. end;
  255. procedure tcgcallnode.pop_parasize(pop_size:longint);
  256. begin
  257. end;
  258. procedure tcgcallnode.handle_return_value;
  259. var
  260. realresdef: tstoreddef;
  261. begin
  262. { Check that the return location is set when the result is passed in
  263. a parameter }
  264. if (procdefinition.proctypeoption<>potype_constructor) and
  265. paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  266. begin
  267. { self.location is set near the end of secondcallparan so it
  268. refers to the implicit result parameter }
  269. if location.loc<>LOC_REFERENCE then
  270. internalerror(200304241);
  271. exit;
  272. end;
  273. if not assigned(typedef) then
  274. realresdef:=tstoreddef(resultdef)
  275. else
  276. realresdef:=tstoreddef(typedef);
  277. {$ifdef x86}
  278. if (retloc.location^.loc=LOC_FPUREGISTER) then
  279. begin
  280. tcgx86(cg).inc_fpu_stack;
  281. location_reset(location,LOC_FPUREGISTER,retloc.location^.size);
  282. location.register:=retloc.location^.register;
  283. end
  284. else
  285. {$endif x86}
  286. begin
  287. { get a tlocation that can hold the return value that's currently in
  288. the the return value's tcgpara }
  289. set_result_location(realresdef);
  290. { Do not move the physical register to a virtual one in case
  291. the return value is not used, because if the virtual one is
  292. then mapped to the same register as the physical one, we will
  293. end up with two deallocs of this register (one inserted here,
  294. one inserted by the register allocator), which unbalances the
  295. register allocation information. The return register(s) will
  296. be freed by location_free() in release_unused_return_value
  297. (mantis #13536). }
  298. if (cnf_return_value_used in callnodeflags) or
  299. assigned(funcretnode) then
  300. begin
  301. gen_load_cgpara_loc(current_asmdata.CurrAsmList,realresdef,retloc,location,false);
  302. {$ifdef arm}
  303. if (resultdef.typ=floatdef) and
  304. (location.loc=LOC_REGISTER) and
  305. (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) then
  306. begin
  307. location_force_mem(current_asmdata.CurrAsmList,location);
  308. end;
  309. {$endif arm}
  310. end;
  311. end;
  312. { copy value to the final location if this was already provided to the
  313. callnode. This must be done after the call node, because the location can
  314. also be used as parameter and may not be finalized yet }
  315. if assigned(funcretnode) then
  316. begin
  317. funcretnode.pass_generate_code;
  318. { Decrease refcount for refcounted types, this can be skipped when
  319. we have used a temp, because then it is already done from tempcreatenode.
  320. Also no finalize is needed, because there is no risk of exceptions from the
  321. function since this is code is only executed after the function call has returned }
  322. if is_managed_type(funcretnode.resultdef) and
  323. (funcretnode.nodetype<>temprefn) then
  324. cg.g_decrrefcount(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  325. case location.loc of
  326. LOC_REGISTER :
  327. {$ifndef cpu64bitalu}
  328. if location.size in [OS_64,OS_S64] then
  329. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  330. else
  331. {$endif}
  332. cg.a_load_reg_loc(current_asmdata.CurrAsmList,location.size,location.register,funcretnode.location);
  333. LOC_REFERENCE:
  334. begin
  335. case funcretnode.location.loc of
  336. LOC_REGISTER:
  337. cg.a_load_ref_reg(current_asmdata.CurrAsmList,location.size,location.size,location.reference,funcretnode.location.register);
  338. LOC_REFERENCE:
  339. cg.g_concatcopy(current_asmdata.CurrAsmList,location.reference,funcretnode.location.reference,resultdef.size);
  340. else
  341. internalerror(200802121);
  342. end;
  343. end;
  344. else
  345. internalerror(200709085);
  346. end;
  347. location := funcretnode.location;
  348. end;
  349. end;
  350. procedure tcgcallnode.release_unused_return_value;
  351. begin
  352. { When the result is not used we need to finalize the result and
  353. can release the temp. This need to be after the callcleanupblock
  354. tree is generated, because that converts the temp from persistent to normal }
  355. if not(cnf_return_value_used in callnodeflags) then
  356. begin
  357. case location.loc of
  358. LOC_REFERENCE :
  359. begin
  360. if is_managed_type(resultdef) then
  361. cg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  362. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  363. end;
  364. {$ifdef x86}
  365. LOC_FPUREGISTER :
  366. begin
  367. { release FPU stack }
  368. emit_reg(A_FSTP,S_NO,NR_FPU_RESULT_REG);
  369. tcgx86(cg).dec_fpu_stack;
  370. end;
  371. {$endif x86}
  372. end;
  373. if (retloc.intsize<>0) then
  374. paramanager.freecgpara(current_asmdata.CurrAsmList,retloc);
  375. location_reset(location,LOC_VOID,OS_NO);
  376. end;
  377. end;
  378. procedure tcgcallnode.release_para_temps;
  379. var
  380. hp,
  381. hp2 : tnode;
  382. ppn : tcallparanode;
  383. begin
  384. { Release temps from parameters }
  385. ppn:=tcallparanode(left);
  386. while assigned(ppn) do
  387. begin
  388. if assigned(ppn.left) then
  389. begin
  390. { don't release the funcret temp }
  391. if not(assigned(ppn.parasym)) or
  392. not(vo_is_funcret in ppn.parasym.varoptions) then
  393. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  394. { process also all nodes of an array of const }
  395. hp:=ppn.left;
  396. while (hp.nodetype=typeconvn) do
  397. hp:=ttypeconvnode(hp).left;
  398. if (hp.nodetype=arrayconstructorn) and
  399. assigned(tarrayconstructornode(hp).left) then
  400. begin
  401. while assigned(hp) do
  402. begin
  403. hp2:=tarrayconstructornode(hp).left;
  404. { ignore typeconvs and addrn inserted by arrayconstructn for
  405. passing a shortstring }
  406. if (hp2.nodetype=typeconvn) and
  407. (tunarynode(hp2).left.nodetype=addrn) then
  408. hp2:=tunarynode(tunarynode(hp2).left).left;
  409. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  410. hp:=tarrayconstructornode(hp).right;
  411. end;
  412. end;
  413. end;
  414. ppn:=tcallparanode(ppn.right);
  415. end;
  416. end;
  417. procedure tcgcallnode.pushparas;
  418. var
  419. ppn : tcgcallparanode;
  420. callerparaloc,
  421. tmpparaloc : pcgparalocation;
  422. sizeleft: aint;
  423. htempref,
  424. href : treference;
  425. calleralignment,
  426. tmpalignment: longint;
  427. begin
  428. { copy all resources to the allocated registers }
  429. ppn:=tcgcallparanode(left);
  430. while assigned(ppn) do
  431. begin
  432. if (ppn.left.nodetype<>nothingn) then
  433. begin
  434. { better check for the real location of the parameter here, when stack passed parameters
  435. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  436. }
  437. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  438. tmpparaloc:=ppn.tempcgpara.location;
  439. sizeleft:=ppn.tempcgpara.intsize;
  440. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  441. tmpalignment:=ppn.tempcgpara.alignment;
  442. if (tmpalignment=0) or
  443. (calleralignment=0) then
  444. internalerror(2009020701);
  445. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  446. while assigned(callerparaloc) do
  447. begin
  448. { Every paraloc must have a matching tmpparaloc }
  449. if not assigned(tmpparaloc) then
  450. internalerror(200408224);
  451. if callerparaloc^.size<>tmpparaloc^.size then
  452. internalerror(200408225);
  453. case callerparaloc^.loc of
  454. LOC_REGISTER:
  455. begin
  456. if tmpparaloc^.loc<>LOC_REGISTER then
  457. internalerror(200408221);
  458. if getsupreg(callerparaloc^.register)<first_int_imreg then
  459. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  460. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  461. tmpparaloc^.register,callerparaloc^.register);
  462. end;
  463. LOC_FPUREGISTER:
  464. begin
  465. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  466. internalerror(200408222);
  467. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  468. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  469. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  470. end;
  471. LOC_MMREGISTER:
  472. begin
  473. if tmpparaloc^.loc<>LOC_MMREGISTER then
  474. internalerror(200408223);
  475. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  476. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  477. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  478. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  479. end;
  480. LOC_REFERENCE:
  481. begin
  482. if use_fixed_stack then
  483. begin
  484. { Can't have a data copied to the stack, every location
  485. must contain a valid size field }
  486. if (tmpparaloc^.size=OS_NO) and
  487. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  488. assigned(tmpparaloc^.next)) then
  489. internalerror(200501281);
  490. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  491. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  492. case tmpparaloc^.loc of
  493. LOC_REFERENCE:
  494. begin
  495. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  496. { use concatcopy, because it can also be a float which fails when
  497. load_ref_ref is used }
  498. if (ppn.tempcgpara.size <> OS_NO) then
  499. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  500. else
  501. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  502. end;
  503. LOC_REGISTER:
  504. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  505. LOC_FPUREGISTER:
  506. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  507. LOC_MMREGISTER:
  508. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  509. else
  510. internalerror(200402081);
  511. end;
  512. end;
  513. end;
  514. end;
  515. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  516. callerparaloc:=callerparaloc^.next;
  517. tmpparaloc:=tmpparaloc^.next;
  518. end;
  519. end;
  520. ppn:=tcgcallparanode(ppn.right);
  521. end;
  522. end;
  523. procedure tcgcallnode.freeparas;
  524. var
  525. ppn : tcgcallparanode;
  526. begin
  527. { free the resources allocated for the parameters }
  528. ppn:=tcgcallparanode(left);
  529. while assigned(ppn) do
  530. begin
  531. if (ppn.left.nodetype<>nothingn) then
  532. begin
  533. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  534. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  535. end;
  536. ppn:=tcgcallparanode(ppn.right);
  537. end;
  538. end;
  539. procedure tcgcallnode.pass_generate_code;
  540. var
  541. name_to_call: shortstring;
  542. regs_to_save_int,
  543. regs_to_save_fpu,
  544. regs_to_save_mm : Tcpuregisterset;
  545. href : treference;
  546. pop_size : longint;
  547. vmtoffset : aint;
  548. pvreg,
  549. vmtreg : tregister;
  550. oldaktcallnode : tcallnode;
  551. retlocitem: pcgparalocation;
  552. {$ifdef vtentry}
  553. sym : tasmsymbol;
  554. {$endif vtentry}
  555. {$ifdef x86_64}
  556. cgpara : tcgpara;
  557. {$endif x86_64}
  558. begin
  559. if not assigned(procdefinition) or
  560. not procdefinition.has_paraloc_info then
  561. internalerror(200305264);
  562. if assigned(callinitblock) then
  563. secondpass(tnode(callinitblock));
  564. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  565. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  566. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  567. { Include Function result registers }
  568. if (not is_void(resultdef)) then
  569. begin
  570. { The forced returntype may have a different size than the one
  571. declared for the procdef }
  572. if not assigned(typedef) then
  573. retloc:=procdefinition.funcretloc[callerside]
  574. else
  575. retloc:=paramanager.get_funcretloc(procdefinition,callerside,typedef);
  576. retlocitem:=retloc.location;
  577. while assigned(retlocitem) do
  578. begin
  579. case retlocitem^.loc of
  580. LOC_REGISTER:
  581. include(regs_to_save_int,getsupreg(retlocitem^.register));
  582. LOC_FPUREGISTER:
  583. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  584. LOC_MMREGISTER:
  585. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  586. LOC_REFERENCE,
  587. LOC_VOID:
  588. ;
  589. else
  590. internalerror(2004110213);
  591. end;
  592. retlocitem:=retlocitem^.next;
  593. end;
  594. end;
  595. { Process parameters, register parameters will be loaded
  596. in imaginary registers. The actual load to the correct
  597. register is done just before the call }
  598. oldaktcallnode:=aktcallnode;
  599. aktcallnode:=self;
  600. if assigned(left) then
  601. tcallparanode(left).secondcallparan;
  602. aktcallnode:=oldaktcallnode;
  603. { procedure variable or normal function call ? }
  604. if (right=nil) then
  605. begin
  606. { register call for WPO (must be done before wpo test below,
  607. otherwise optimised called methods are no longer registered)
  608. }
  609. if (po_virtualmethod in procdefinition.procoptions) and
  610. assigned(methodpointer) and
  611. (methodpointer.nodetype<>typen) and
  612. (not assigned(current_procinfo) or
  613. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  614. tprocdef(procdefinition)._class.register_vmt_call(tprocdef(procdefinition).extnumber);
  615. {$ifdef vtentry}
  616. if not is_interface(tprocdef(procdefinition)._class) then
  617. begin
  618. inc(current_asmdata.NextVTEntryNr);
  619. current_asmdata.CurrAsmList.Concat(tai_symbol.CreateName('VTREF'+tostr(current_asmdata.NextVTEntryNr)+'_'+tprocdef(procdefinition)._class.vmt_mangledname+'$$'+tostr(vmtoffset div sizeof(pint)),AT_FUNCTION,0));
  620. end;
  621. {$endif vtentry}
  622. name_to_call:='';
  623. if assigned(fobjcforcedprocname) then
  624. name_to_call:=fobjcforcedprocname^;
  625. { When methodpointer is typen we don't need (and can't) load
  626. a pointer. We can directly call the correct procdef (PFV) }
  627. if (name_to_call='') and
  628. (po_virtualmethod in procdefinition.procoptions) and
  629. assigned(methodpointer) and
  630. (methodpointer.nodetype<>typen) and
  631. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  632. begin
  633. { virtual methods require an index }
  634. if tprocdef(procdefinition).extnumber=$ffff then
  635. internalerror(200304021);
  636. secondpass(methodpointer);
  637. { Load VMT from self }
  638. if methodpointer.resultdef.typ=objectdef then
  639. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resultdef),methodpointer.location,vmtreg)
  640. else
  641. begin
  642. { Load VMT value in register }
  643. location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,OS_ADDR,false);
  644. vmtreg:=methodpointer.location.register;
  645. end;
  646. { test validity of VMT }
  647. if not(is_interface(tprocdef(procdefinition)._class)) and
  648. not(is_cppclass(tprocdef(procdefinition)._class)) then
  649. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tprocdef(procdefinition)._class);
  650. { Call through VMT, generate a VTREF symbol to notify the linker }
  651. vmtoffset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  652. { register call for WPO }
  653. if (not assigned(current_procinfo) or
  654. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  655. tprocdef(procdefinition)._class.register_vmt_call(tprocdef(procdefinition).extnumber);
  656. {$ifndef x86}
  657. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  658. {$endif not x86}
  659. reference_reset_base(href,vmtreg,vmtoffset,sizeof(pint));
  660. {$ifndef x86}
  661. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  662. {$endif not x86}
  663. { Load parameters that are in temporary registers in the
  664. correct parameter register }
  665. if assigned(left) then
  666. begin
  667. pushparas;
  668. { free the resources allocated for the parameters }
  669. freeparas;
  670. end;
  671. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  672. if cg.uses_registers(R_FPUREGISTER) then
  673. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  674. if cg.uses_registers(R_MMREGISTER) then
  675. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  676. { call method }
  677. extra_call_code;
  678. {$ifdef x86}
  679. cg.a_call_ref(current_asmdata.CurrAsmList,href);
  680. {$else x86}
  681. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  682. {$endif x86}
  683. extra_post_call_code;
  684. end
  685. else
  686. begin
  687. { Load parameters that are in temporary registers in the
  688. correct parameter register }
  689. if assigned(left) then
  690. begin
  691. pushparas;
  692. { free the resources allocated for the parameters }
  693. freeparas;
  694. end;
  695. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  696. if cg.uses_registers(R_FPUREGISTER) then
  697. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  698. if cg.uses_registers(R_MMREGISTER) then
  699. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  700. if procdefinition.proccalloption=pocall_syscall then
  701. do_syscall
  702. else
  703. begin
  704. { Calling interrupt from the same code requires some
  705. extra code }
  706. if (po_interrupt in procdefinition.procoptions) then
  707. extra_interrupt_code;
  708. extra_call_code;
  709. if (name_to_call='') then
  710. cg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition).mangledname,po_weakexternal in procdefinition.procoptions)
  711. else
  712. cg.a_call_name(current_asmdata.CurrAsmList,name_to_call,po_weakexternal in procdefinition.procoptions);
  713. extra_post_call_code;
  714. end;
  715. end;
  716. end
  717. else
  718. { now procedure variable case }
  719. begin
  720. secondpass(right);
  721. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  722. { Only load OS_ADDR from the reference }
  723. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  724. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  725. else
  726. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,right.location,pvreg);
  727. location_freetemp(current_asmdata.CurrAsmList,right.location);
  728. { Load parameters that are in temporary registers in the
  729. correct parameter register }
  730. if assigned(left) then
  731. begin
  732. pushparas;
  733. { free the resources allocated for the parameters }
  734. freeparas;
  735. end;
  736. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  737. if cg.uses_registers(R_FPUREGISTER) then
  738. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  739. if cg.uses_registers(R_MMREGISTER) then
  740. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  741. { Calling interrupt from the same code requires some
  742. extra code }
  743. if (po_interrupt in procdefinition.procoptions) then
  744. extra_interrupt_code;
  745. extra_call_code;
  746. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  747. extra_post_call_code;
  748. end;
  749. { Need to remove the parameters from the stack? }
  750. if (procdefinition.proccalloption in clearstack_pocalls) then
  751. begin
  752. pop_size:=pushedparasize;
  753. { for Cdecl functions we don't need to pop the funcret when it
  754. was pushed by para }
  755. if paramanager.ret_in_param(procdefinition.returndef,procdefinition.proccalloption) then
  756. dec(pop_size,sizeof(pint));
  757. { Remove parameters/alignment from the stack }
  758. pop_parasize(pop_size);
  759. end;
  760. { Release registers, but not the registers that contain the
  761. function result }
  762. if (not is_void(resultdef)) then
  763. begin
  764. retlocitem:=retloc.location;
  765. while assigned(retlocitem) do
  766. begin
  767. case retlocitem^.loc of
  768. LOC_REGISTER:
  769. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  770. LOC_FPUREGISTER:
  771. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  772. LOC_MMREGISTER:
  773. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  774. LOC_REFERENCE,
  775. LOC_VOID:
  776. ;
  777. else
  778. internalerror(2004110214);
  779. end;
  780. retlocitem:=retlocitem^.next;
  781. end;
  782. end;
  783. {$if defined(x86) or defined(arm)}
  784. if (procdefinition.proccalloption=pocall_safecall) and
  785. (target_info.system in systems_all_windows) then
  786. begin
  787. {$ifdef x86_64}
  788. cgpara.init;
  789. paramanager.getintparaloc(pocall_default,1,cgpara);
  790. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_ADDR,NR_RAX,cgpara);
  791. cgpara.done;
  792. {$endif x86_64}
  793. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  794. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK',false);
  795. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  796. end;
  797. {$endif}
  798. if cg.uses_registers(R_MMREGISTER) then
  799. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  800. if cg.uses_registers(R_FPUREGISTER) then
  801. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  802. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  803. { handle function results }
  804. if (not is_void(resultdef)) then
  805. handle_return_value
  806. else
  807. location_reset(location,LOC_VOID,OS_NO);
  808. { convert persistent temps for parameters and function result to normal temps }
  809. if assigned(callcleanupblock) then
  810. secondpass(tnode(callcleanupblock));
  811. { release temps and finalize unused return values, must be
  812. after the callcleanupblock because that converts temps
  813. from persistent to normal }
  814. release_unused_return_value;
  815. { release temps of paras }
  816. release_para_temps;
  817. { perhaps i/o check ? }
  818. if (cs_check_io in current_settings.localswitches) and
  819. (po_iocheck in procdefinition.procoptions) and
  820. not(po_iocheck in current_procinfo.procdef.procoptions) and
  821. { no IO check for methods and procedure variables }
  822. (right=nil) and
  823. not(po_virtualmethod in procdefinition.procoptions) then
  824. begin
  825. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  826. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK',false);
  827. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  828. end;
  829. end;
  830. destructor tcgcallnode.destroy;
  831. begin
  832. if assigned(typedef) then
  833. retloc.done;
  834. inherited destroy;
  835. end;
  836. begin
  837. ccallparanode:=tcgcallparanode;
  838. ccallnode:=tcgcallnode;
  839. end.