ncgcal.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. else if hp2.nodetype=addrn then
  464. hp2:=tunarynode(hp2).left;
  465. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  466. hp:=tarrayconstructornode(hp).right;
  467. end;
  468. end;
  469. end;
  470. ppn:=tcallparanode(ppn.right);
  471. end;
  472. end;
  473. procedure tcgcallnode.pushparas;
  474. var
  475. ppn : tcgcallparanode;
  476. callerparaloc,
  477. tmpparaloc : pcgparalocation;
  478. sizeleft: aint;
  479. htempref,
  480. href : treference;
  481. calleralignment,
  482. tmpalignment: longint;
  483. skipiffinalloc: boolean;
  484. begin
  485. { copy all resources to the allocated registers }
  486. ppn:=tcgcallparanode(left);
  487. while assigned(ppn) do
  488. begin
  489. if (ppn.left.nodetype<>nothingn) then
  490. begin
  491. { better check for the real location of the parameter here, when stack passed parameters
  492. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  493. }
  494. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  495. tmpparaloc:=ppn.tempcgpara.location;
  496. sizeleft:=ppn.tempcgpara.intsize;
  497. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  498. tmpalignment:=ppn.tempcgpara.alignment;
  499. if (tmpalignment=0) or
  500. (calleralignment=0) then
  501. internalerror(2009020701);
  502. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  503. skipiffinalloc:=
  504. not paramanager.use_fixed_stack or
  505. not(ppn.followed_by_stack_tainting_call_cached);
  506. while assigned(callerparaloc) do
  507. begin
  508. { Every paraloc must have a matching tmpparaloc }
  509. if not assigned(tmpparaloc) then
  510. internalerror(200408224);
  511. if callerparaloc^.size<>tmpparaloc^.size then
  512. internalerror(200408225);
  513. case callerparaloc^.loc of
  514. LOC_REGISTER:
  515. begin
  516. if tmpparaloc^.loc<>LOC_REGISTER then
  517. internalerror(200408221);
  518. if getsupreg(callerparaloc^.register)<first_int_imreg then
  519. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  520. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  521. tmpparaloc^.register,callerparaloc^.register);
  522. end;
  523. LOC_FPUREGISTER:
  524. begin
  525. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  526. internalerror(200408222);
  527. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  528. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  529. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  530. end;
  531. LOC_MMREGISTER:
  532. begin
  533. if tmpparaloc^.loc<>LOC_MMREGISTER then
  534. internalerror(200408223);
  535. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  536. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  537. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  538. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  539. end;
  540. LOC_REFERENCE:
  541. begin
  542. if not(skipiffinalloc and
  543. paramanager.is_stack_paraloc(callerparaloc)) then
  544. begin
  545. { Can't have a data copied to the stack, every location
  546. must contain a valid size field }
  547. if (tmpparaloc^.size=OS_NO) and
  548. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  549. assigned(tmpparaloc^.next)) then
  550. internalerror(200501281);
  551. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  552. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  553. case tmpparaloc^.loc of
  554. LOC_REFERENCE:
  555. begin
  556. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  557. { use concatcopy, because it can also be a float which fails when
  558. load_ref_ref is used }
  559. if (ppn.tempcgpara.size <> OS_NO) then
  560. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  561. else
  562. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  563. end;
  564. LOC_REGISTER:
  565. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  566. LOC_FPUREGISTER:
  567. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  568. LOC_MMREGISTER:
  569. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  570. else
  571. internalerror(200402081);
  572. end;
  573. end;
  574. end;
  575. end;
  576. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  577. callerparaloc:=callerparaloc^.next;
  578. tmpparaloc:=tmpparaloc^.next;
  579. end;
  580. end;
  581. ppn:=tcgcallparanode(ppn.right);
  582. end;
  583. end;
  584. procedure tcgcallnode.freeparas;
  585. var
  586. ppn : tcgcallparanode;
  587. begin
  588. { free the resources allocated for the parameters }
  589. ppn:=tcgcallparanode(left);
  590. while assigned(ppn) do
  591. begin
  592. if (ppn.left.nodetype<>nothingn) then
  593. begin
  594. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  595. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  596. end;
  597. ppn:=tcgcallparanode(ppn.right);
  598. end;
  599. end;
  600. procedure tcgcallnode.pass_generate_code;
  601. var
  602. name_to_call: shortstring;
  603. regs_to_save_int,
  604. regs_to_save_fpu,
  605. regs_to_save_mm : Tcpuregisterset;
  606. href : treference;
  607. pop_size : longint;
  608. vmtoffset : aint;
  609. pvreg,
  610. vmtreg : tregister;
  611. oldaktcallnode : tcallnode;
  612. retlocitem: pcgparalocation;
  613. {$ifdef vtentry}
  614. sym : tasmsymbol;
  615. {$endif vtentry}
  616. {$if defined(x86) or defined(arm) or defined(sparc)}
  617. cgpara : tcgpara;
  618. {$endif}
  619. begin
  620. if not assigned(procdefinition) or
  621. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  622. internalerror(200305264);
  623. extra_pre_call_code;
  624. if assigned(callinitblock) then
  625. secondpass(tnode(callinitblock));
  626. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  627. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  628. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  629. { Include Function result registers }
  630. if (not is_void(resultdef)) then
  631. begin
  632. { The forced returntype may have a different size than the one
  633. declared for the procdef }
  634. if not assigned(typedef) then
  635. retloc:=procdefinition.funcretloc[callerside]
  636. else
  637. retloc:=paramanager.get_funcretloc(procdefinition,callerside,typedef);
  638. retlocitem:=retloc.location;
  639. while assigned(retlocitem) do
  640. begin
  641. case retlocitem^.loc of
  642. LOC_REGISTER:
  643. include(regs_to_save_int,getsupreg(retlocitem^.register));
  644. LOC_FPUREGISTER:
  645. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  646. LOC_MMREGISTER:
  647. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  648. LOC_REFERENCE,
  649. LOC_VOID:
  650. ;
  651. else
  652. internalerror(2004110213);
  653. end;
  654. retlocitem:=retlocitem^.next;
  655. end;
  656. end;
  657. { Process parameters, register parameters will be loaded
  658. in imaginary registers. The actual load to the correct
  659. register is done just before the call }
  660. oldaktcallnode:=aktcallnode;
  661. aktcallnode:=self;
  662. if assigned(left) then
  663. tcallparanode(left).secondcallparan;
  664. aktcallnode:=oldaktcallnode;
  665. { procedure variable or normal function call ? }
  666. if (right=nil) then
  667. begin
  668. { register call for WPO (must be done before wpo test below,
  669. otherwise optimised called methods are no longer registered)
  670. }
  671. if (po_virtualmethod in procdefinition.procoptions) and
  672. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  673. assigned(methodpointer) and
  674. (methodpointer.nodetype<>typen) and
  675. (not assigned(current_procinfo) or
  676. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  677. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  678. {$ifdef vtentry}
  679. if not is_interface(tprocdef(procdefinition)._class) then
  680. begin
  681. inc(current_asmdata.NextVTEntryNr);
  682. 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));
  683. end;
  684. {$endif vtentry}
  685. name_to_call:='';
  686. if assigned(fobjcforcedprocname) then
  687. name_to_call:=fobjcforcedprocname^;
  688. { in the JVM, virtual method calls are also name-based }
  689. {$ifndef jvm}
  690. { When methodpointer is typen we don't need (and can't) load
  691. a pointer. We can directly call the correct procdef (PFV) }
  692. if (name_to_call='') and
  693. (po_virtualmethod in procdefinition.procoptions) and
  694. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  695. assigned(methodpointer) and
  696. (methodpointer.nodetype<>typen) and
  697. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  698. begin
  699. { virtual methods require an index }
  700. if tprocdef(procdefinition).extnumber=$ffff then
  701. internalerror(200304021);
  702. secondpass(methodpointer);
  703. { Load VMT from self }
  704. if methodpointer.resultdef.typ=objectdef then
  705. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resultdef),methodpointer.location,vmtreg)
  706. else
  707. begin
  708. { Load VMT value in register }
  709. { todo: fix vmt type for high level cg }
  710. hlcg.location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,voidpointertype,voidpointertype,false);
  711. vmtreg:=methodpointer.location.register;
  712. end;
  713. { test validity of VMT }
  714. if not(is_interface(tprocdef(procdefinition).struct)) and
  715. not(is_cppclass(tprocdef(procdefinition).struct)) then
  716. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tobjectdef(tprocdef(procdefinition).struct));
  717. { Call through VMT, generate a VTREF symbol to notify the linker }
  718. vmtoffset:=tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber);
  719. { register call for WPO }
  720. if (not assigned(current_procinfo) or
  721. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  722. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  723. {$ifndef x86}
  724. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  725. {$endif not x86}
  726. reference_reset_base(href,vmtreg,vmtoffset,sizeof(pint));
  727. {$ifndef x86}
  728. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  729. {$endif not x86}
  730. { Load parameters that are in temporary registers in the
  731. correct parameter register }
  732. if assigned(left) then
  733. begin
  734. pushparas;
  735. { free the resources allocated for the parameters }
  736. freeparas;
  737. end;
  738. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  739. if cg.uses_registers(R_FPUREGISTER) then
  740. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  741. if cg.uses_registers(R_MMREGISTER) then
  742. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  743. { call method }
  744. extra_call_code;
  745. {$ifdef x86}
  746. hlcg.a_call_ref(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),href);
  747. {$else x86}
  748. hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg);
  749. {$endif x86}
  750. extra_post_call_code;
  751. end
  752. else
  753. {$endif jvm}
  754. begin
  755. { Load parameters that are in temporary registers in the
  756. correct parameter register }
  757. if assigned(left) then
  758. begin
  759. pushparas;
  760. { free the resources allocated for the parameters }
  761. freeparas;
  762. end;
  763. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  764. if cg.uses_registers(R_FPUREGISTER) then
  765. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  766. if cg.uses_registers(R_MMREGISTER) then
  767. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  768. if procdefinition.proccalloption=pocall_syscall then
  769. do_syscall
  770. else
  771. begin
  772. { Calling interrupt from the same code requires some
  773. extra code }
  774. if (po_interrupt in procdefinition.procoptions) then
  775. extra_interrupt_code;
  776. extra_call_code;
  777. if (name_to_call='') then
  778. if cnf_inherited in callnodeflags then
  779. hlcg.a_call_name_inherited(current_asmdata.CurrAsmList,tprocdef(procdefinition),tprocdef(procdefinition).mangledname)
  780. else
  781. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),tprocdef(procdefinition).mangledname,typedef,po_weakexternal in procdefinition.procoptions).resetiftemp
  782. else
  783. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,typedef,po_weakexternal in procdefinition.procoptions).resetiftemp;
  784. extra_post_call_code;
  785. end;
  786. end;
  787. end
  788. else
  789. { now procedure variable case }
  790. begin
  791. secondpass(right);
  792. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  793. { Only load OS_ADDR from the reference (when converting to hlcg:
  794. watch out with procedure of object) }
  795. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  796. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  797. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  798. begin
  799. { in case left is a method pointer and we are on a big endian target, then
  800. the method address is stored in registerhi }
  801. if (target_info.endian=endian_big) and (right.location.size in [OS_PAIR,OS_SPAIR]) then
  802. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location.registerhi,pvreg)
  803. else
  804. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location.register,pvreg);
  805. end
  806. else
  807. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location,pvreg);
  808. location_freetemp(current_asmdata.CurrAsmList,right.location);
  809. { Load parameters that are in temporary registers in the
  810. correct parameter register }
  811. if assigned(left) then
  812. begin
  813. pushparas;
  814. { free the resources allocated for the parameters }
  815. freeparas;
  816. end;
  817. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  818. if cg.uses_registers(R_FPUREGISTER) then
  819. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  820. if cg.uses_registers(R_MMREGISTER) then
  821. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  822. { Calling interrupt from the same code requires some
  823. extra code }
  824. if (po_interrupt in procdefinition.procoptions) then
  825. extra_interrupt_code;
  826. extra_call_code;
  827. hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg);
  828. extra_post_call_code;
  829. end;
  830. { Need to remove the parameters from the stack? }
  831. if (procdefinition.proccalloption in clearstack_pocalls) then
  832. begin
  833. pop_size:=pushedparasize;
  834. { for Cdecl functions we don't need to pop the funcret when it
  835. was pushed by para. Except for safecall functions with
  836. safecall-exceptions enabled. In that case the funcret is always
  837. returned as a para which is considered a normal para on the
  838. c-side, so the funcret has to be pop'ed normally. }
  839. if not ((procdefinition.proccalloption=pocall_safecall) and
  840. (tf_safecall_exceptions in target_info.flags)) and
  841. paramanager.ret_in_param(procdefinition.returndef,procdefinition.proccalloption) then
  842. dec(pop_size,sizeof(pint));
  843. { Remove parameters/alignment from the stack }
  844. pop_parasize(pop_size);
  845. end
  846. { frame pointer parameter is popped by the caller when it's passed the
  847. Delphi way }
  848. else if (po_delphi_nested_cc in procdefinition.procoptions) and
  849. not paramanager.use_fixed_stack then
  850. pop_parasize(sizeof(pint));
  851. { Release registers, but not the registers that contain the
  852. function result }
  853. if (not is_void(resultdef)) then
  854. begin
  855. retlocitem:=retloc.location;
  856. while assigned(retlocitem) do
  857. begin
  858. case retlocitem^.loc of
  859. LOC_REGISTER:
  860. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  861. LOC_FPUREGISTER:
  862. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  863. LOC_MMREGISTER:
  864. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  865. LOC_REFERENCE,
  866. LOC_VOID:
  867. ;
  868. else
  869. internalerror(2004110214);
  870. end;
  871. retlocitem:=retlocitem^.next;
  872. end;
  873. end;
  874. if cg.uses_registers(R_MMREGISTER) then
  875. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  876. if cg.uses_registers(R_FPUREGISTER) then
  877. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  878. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  879. {$ifdef SUPPORT_SAFECALL}
  880. if (procdefinition.proccalloption=pocall_safecall) and
  881. (tf_safecall_exceptions in target_info.flags) then
  882. begin
  883. cgpara.init;
  884. paramanager.getintparaloc(pocall_default,1,s32inttype,cgpara);
  885. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_INT,NR_FUNCTION_RESULT_REG,cgpara);
  886. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  887. cgpara.done;
  888. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK');
  889. end;
  890. {$endif}
  891. { handle function results }
  892. if (not is_void(resultdef)) then
  893. handle_return_value
  894. else
  895. location_reset(location,LOC_VOID,OS_NO);
  896. { convert persistent temps for parameters and function result to normal temps }
  897. if assigned(callcleanupblock) then
  898. secondpass(tnode(callcleanupblock));
  899. { copy back copy-out parameters if any }
  900. copy_back_paras;
  901. { release temps and finalize unused return values, must be
  902. after the callcleanupblock because that converts temps
  903. from persistent to normal }
  904. release_unused_return_value;
  905. { release temps of paras }
  906. release_para_temps;
  907. { perhaps i/o check ? }
  908. if (cs_check_io in current_settings.localswitches) and
  909. (po_iocheck in procdefinition.procoptions) and
  910. not(po_iocheck in current_procinfo.procdef.procoptions) and
  911. { no IO check for methods and procedure variables }
  912. (right=nil) and
  913. not(po_virtualmethod in procdefinition.procoptions) then
  914. begin
  915. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  916. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK',false);
  917. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  918. end;
  919. end;
  920. destructor tcgcallnode.destroy;
  921. begin
  922. retloc.resetiftemp;
  923. inherited destroy;
  924. end;
  925. begin
  926. ccallparanode:=tcgcallparanode;
  927. ccallnode:=tcgcallnode;
  928. end.