ncgcal.pas 43 KB

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