ncgcal.pas 52 KB

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