ncgcal.pas 48 KB

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