ncgcal.pas 42 KB

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