ncgcal.pas 42 KB

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