ncgcal.pas 52 KB

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