2
0

ncgcal.pas 45 KB

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