ncgcal.pas 51 KB

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