ncgcal.pas 43 KB

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