ncgcal.pas 43 KB

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