ncgcal.pas 42 KB

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