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. 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. 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);
  370. var
  371. href: treference;
  372. srsym: tsym;
  373. srsymtable: tsymtable;
  374. literaldef: trecorddef;
  375. begin
  376. literaldef:=get_block_literal_type_for_proc(tabstractprocdef(right.resultdef));
  377. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cpointerdef.getreusable(literaldef),true);
  378. { load the invoke pointer }
  379. hlcg.reference_reset_base(href,right.resultdef,right.location.register,0,right.resultdef.alignment);
  380. if not searchsym_in_record(literaldef,'INVOKE',srsym,srsymtable) or
  381. (srsym.typ<>fieldvarsym) or
  382. (tfieldvarsym(srsym).vardef<>voidpointertype) then
  383. internalerror(2014071506);
  384. href.offset:=tfieldvarsym(srsym).fieldoffset;
  385. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,tfieldvarsym(srsym).vardef,procdefinition,href,toreg);
  386. end;
  387. function tcgcallnode.get_call_reg(list: TAsmList): tregister;
  388. begin
  389. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition.address_type);
  390. end;
  391. procedure tcgcallnode.unget_call_reg(list: TAsmList; reg: tregister);
  392. begin
  393. { nothing to do by default }
  394. end;
  395. procedure tcgcallnode.set_result_location(realresdef: tstoreddef);
  396. begin
  397. if realresdef.is_intregable or
  398. realresdef.is_fpuregable or
  399. { avoid temporarily storing pointer-sized entities that can't be
  400. regvars, such as reference-counted pointers, to memory --
  401. no exception can occur right now (except in case of existing
  402. memory corruption), and we'd store them to a regular temp
  403. anyway and that is not safer than keeping them in a register }
  404. ((realresdef.size=sizeof(aint)) and
  405. (retloc.location^.loc=LOC_REGISTER) and
  406. not assigned(retloc.location^.next)) then
  407. location_allocate_register(current_asmdata.CurrAsmList,location,realresdef,false)
  408. else
  409. begin
  410. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),0);
  411. tg.gethltemp(current_asmdata.CurrAsmList,realresdef,retloc.intsize,tt_normal,location.reference);
  412. end;
  413. end;
  414. procedure tcgcallnode.do_release_unused_return_value;
  415. begin
  416. case location.loc of
  417. LOC_REFERENCE :
  418. begin
  419. if is_managed_type(resultdef) then
  420. hlcg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  421. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  422. end;
  423. end;
  424. end;
  425. procedure tcgcallnode.pop_parasize(pop_size:longint);
  426. begin
  427. end;
  428. procedure tcgcallnode.handle_return_value;
  429. var
  430. realresdef: tstoreddef;
  431. begin
  432. { Check that the return location is set when the result is passed in
  433. a parameter }
  434. if paramanager.ret_in_param(resultdef,procdefinition) then
  435. begin
  436. { self.location is set near the end of secondcallparan so it
  437. refers to the implicit result parameter }
  438. if location.loc<>LOC_REFERENCE then
  439. internalerror(200304241);
  440. exit;
  441. end;
  442. if not assigned(typedef) then
  443. realresdef:=tstoreddef(resultdef)
  444. else
  445. realresdef:=tstoreddef(typedef);
  446. { get a tlocation that can hold the return value that's currently in
  447. the return value's tcgpara }
  448. set_result_location(realresdef);
  449. { Do not move the physical register to a virtual one in case
  450. the return value is not used, because if the virtual one is
  451. then mapped to the same register as the physical one, we will
  452. end up with two deallocs of this register (one inserted here,
  453. one inserted by the register allocator), which unbalances the
  454. register allocation information. The return register(s) will
  455. be freed by location_free() in release_unused_return_value
  456. (mantis #13536). }
  457. if (cnf_return_value_used in callnodeflags) or
  458. assigned(funcretnode) then
  459. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,realresdef,retloc,location,false);
  460. { copy value to the final location if this was already provided to the
  461. callnode. This must be done after the call node, because the location can
  462. also be used as parameter and may not be finalized yet }
  463. if assigned(funcretnode) then
  464. begin
  465. funcretnode.pass_generate_code;
  466. { Decrease refcount for refcounted types, this can be skipped when
  467. we have used a temp, because then it is already done from tempcreatenode.
  468. Also no finalize is needed, because there is no risk of exceptions from the
  469. function since this is code is only executed after the function call has returned }
  470. if is_managed_type(funcretnode.resultdef) and
  471. (funcretnode.nodetype<>temprefn) then
  472. hlcg.g_finalize(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  473. case location.loc of
  474. LOC_REGISTER :
  475. begin
  476. {$ifndef cpu64bitalu}
  477. if location.size in [OS_64,OS_S64] then
  478. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  479. else
  480. {$endif}
  481. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,resultdef,resultdef,location.register,funcretnode.location);
  482. location_free(current_asmdata.CurrAsmList,location);
  483. end;
  484. LOC_REFERENCE:
  485. begin
  486. case funcretnode.location.loc of
  487. LOC_REGISTER:
  488. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,resultdef,resultdef,location.reference,funcretnode.location.register);
  489. LOC_REFERENCE:
  490. hlcg.g_concatcopy(current_asmdata.CurrAsmList,resultdef,location.reference,funcretnode.location.reference);
  491. else
  492. internalerror(200802121);
  493. end;
  494. location_freetemp(current_asmdata.CurrAsmList,location);
  495. end;
  496. else
  497. internalerror(200709085);
  498. end;
  499. location := funcretnode.location;
  500. end;
  501. end;
  502. procedure tcgcallnode.release_unused_return_value;
  503. begin
  504. { When the result is not used we need to finalize the result and
  505. can release the temp. This need to be after the callcleanupblock
  506. tree is generated, because that converts the temp from persistent to normal }
  507. if not(cnf_return_value_used in callnodeflags) then
  508. begin
  509. do_release_unused_return_value;
  510. if (retloc.intsize<>0) then
  511. paramanager.freecgpara(current_asmdata.CurrAsmList,retloc);
  512. location_reset(location,LOC_VOID,OS_NO);
  513. end;
  514. end;
  515. procedure tcgcallnode.copy_back_paras;
  516. var
  517. ppn : tcallparanode;
  518. begin
  519. ppn:=tcallparanode(left);
  520. while assigned(ppn) do
  521. begin
  522. if assigned(ppn.paracopyback) then
  523. secondpass(ppn.paracopyback);
  524. ppn:=tcallparanode(ppn.right);
  525. end;
  526. end;
  527. procedure tcgcallnode.release_para_temps;
  528. var
  529. hp,
  530. hp2 : tnode;
  531. ppn : tcallparanode;
  532. begin
  533. { Release temps from parameters }
  534. ppn:=tcallparanode(left);
  535. while assigned(ppn) do
  536. begin
  537. if assigned(ppn.left) then
  538. begin
  539. { don't release the funcret temp }
  540. if not(assigned(ppn.parasym)) or
  541. not(vo_is_funcret in ppn.parasym.varoptions) then
  542. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  543. { process also all nodes of an array of const }
  544. hp:=ppn.left;
  545. while (hp.nodetype=typeconvn) do
  546. hp:=ttypeconvnode(hp).left;
  547. if (hp.nodetype=arrayconstructorn) and
  548. assigned(tarrayconstructornode(hp).left) then
  549. begin
  550. while assigned(hp) do
  551. begin
  552. hp2:=tarrayconstructornode(hp).left;
  553. { ignore typeconvs and addrn inserted by arrayconstructn for
  554. passing a shortstring }
  555. if (hp2.nodetype=typeconvn) and
  556. (tunarynode(hp2).left.nodetype=addrn) then
  557. hp2:=tunarynode(tunarynode(hp2).left).left
  558. else if hp2.nodetype=addrn then
  559. hp2:=tunarynode(hp2).left;
  560. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  561. hp:=tarrayconstructornode(hp).right;
  562. end;
  563. end;
  564. end;
  565. ppn:=tcallparanode(ppn.right);
  566. end;
  567. setlength(paralocs,0);
  568. end;
  569. procedure tcgcallnode.pushparas;
  570. var
  571. ppn : tcgcallparanode;
  572. callerparaloc,
  573. tmpparaloc : pcgparalocation;
  574. sizeleft: aint;
  575. htempref,
  576. href : treference;
  577. calleralignment,
  578. tmpalignment, i: longint;
  579. skipiffinalloc: boolean;
  580. begin
  581. { copy all resources to the allocated registers }
  582. ppn:=tcgcallparanode(left);
  583. while assigned(ppn) do
  584. begin
  585. if (ppn.left.nodetype<>nothingn) then
  586. begin
  587. { better check for the real location of the parameter here, when stack passed parameters
  588. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  589. }
  590. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  591. tmpparaloc:=ppn.tempcgpara.location;
  592. sizeleft:=ppn.tempcgpara.intsize;
  593. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  594. tmpalignment:=ppn.tempcgpara.alignment;
  595. if (tmpalignment=0) or
  596. (calleralignment=0) then
  597. internalerror(2009020701);
  598. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  599. skipiffinalloc:=
  600. not paramanager.use_fixed_stack or
  601. not(ppn.followed_by_stack_tainting_call_cached);
  602. while assigned(callerparaloc) do
  603. begin
  604. { Every paraloc must have a matching tmpparaloc }
  605. if not assigned(tmpparaloc) then
  606. internalerror(200408224);
  607. if callerparaloc^.size<>tmpparaloc^.size then
  608. internalerror(200408225);
  609. case callerparaloc^.loc of
  610. LOC_REGISTER:
  611. begin
  612. if tmpparaloc^.loc<>LOC_REGISTER then
  613. internalerror(200408221);
  614. if getsupreg(callerparaloc^.register)<first_int_imreg then
  615. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  616. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  617. tmpparaloc^.register,callerparaloc^.register);
  618. end;
  619. LOC_FPUREGISTER:
  620. begin
  621. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  622. internalerror(200408222);
  623. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  624. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  625. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,callerparaloc^.register);
  626. end;
  627. LOC_MMREGISTER:
  628. begin
  629. if tmpparaloc^.loc<>LOC_MMREGISTER then
  630. internalerror(200408223);
  631. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  632. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  633. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  634. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  635. end;
  636. LOC_REFERENCE:
  637. begin
  638. if not(skipiffinalloc and
  639. paramanager.is_stack_paraloc(callerparaloc)) then
  640. begin
  641. { Can't have a data copied to the stack, every location
  642. must contain a valid size field }
  643. if (tmpparaloc^.size=OS_NO) and
  644. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  645. assigned(tmpparaloc^.next)) then
  646. internalerror(200501281);
  647. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  648. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  649. case tmpparaloc^.loc of
  650. LOC_REFERENCE:
  651. begin
  652. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  653. { use concatcopy, because it can also be a float which fails when
  654. load_ref_ref is used }
  655. if (ppn.tempcgpara.size <> OS_NO) then
  656. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  657. else
  658. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  659. end;
  660. LOC_REGISTER:
  661. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  662. LOC_FPUREGISTER:
  663. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  664. LOC_MMREGISTER:
  665. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  666. else
  667. internalerror(200402081);
  668. end;
  669. end;
  670. end;
  671. end;
  672. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  673. callerparaloc:=callerparaloc^.next;
  674. tmpparaloc:=tmpparaloc^.next;
  675. end;
  676. end;
  677. ppn:=tcgcallparanode(ppn.right);
  678. end;
  679. setlength(paralocs,procdefinition.paras.count);
  680. for i:=0 to procdefinition.paras.count-1 do
  681. paralocs[i]:=@tparavarsym(procdefinition.paras[i]).paraloc[callerside];
  682. end;
  683. function tcgcallnode.load_complex_procvar_codeptr: tregister;
  684. var
  685. srcreg: tregister;
  686. codeprocdef: tabstractprocdef;
  687. begin
  688. { this is safe even on i8086, because procvardef code pointers are
  689. always far there (so the current state of far calls vs the state
  690. of far calls where the procvardef was defined does not matter,
  691. even though the procvardef constructor called by getcopyas looks at
  692. it) }
  693. codeprocdef:=cprocvardef.getreusableprocaddr(procdefinition);
  694. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,codeprocdef);
  695. { in case we have a method pointer on a big endian target in registers,
  696. the method address is stored in registerhi (it's the first field
  697. in the tmethod record) }
  698. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  699. begin
  700. if not(right.location.size in [OS_PAIR,OS_SPAIR]) then
  701. internalerror(2014081401);
  702. if (target_info.endian=endian_big) then
  703. srcreg:=right.location.registerhi
  704. else
  705. srcreg:=right.location.register;
  706. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,codeprocdef,codeprocdef,srcreg,result)
  707. end
  708. else
  709. begin
  710. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,procdefinition);
  711. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(procdefinition),cpointerdef.getreusable(codeprocdef),right.location.reference);
  712. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,codeprocdef,codeprocdef,right.location.reference,result);
  713. end;
  714. end;
  715. function tcgcallnode.load_procvar_codeptr: tregister;
  716. begin
  717. if po_is_block in procdefinition.procoptions then
  718. begin
  719. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition);
  720. load_block_invoke(result);
  721. end
  722. else if not(procdefinition.is_addressonly) then
  723. result:=load_complex_procvar_codeptr
  724. else
  725. begin
  726. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition);
  727. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,procdefinition,procdefinition,right.location,result);
  728. end;
  729. end;
  730. procedure tcgcallnode.freeparas;
  731. var
  732. ppn : tcgcallparanode;
  733. begin
  734. { free the resources allocated for the parameters }
  735. ppn:=tcgcallparanode(left);
  736. while assigned(ppn) do
  737. begin
  738. if (ppn.left.nodetype<>nothingn) then
  739. begin
  740. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  741. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  742. end;
  743. ppn:=tcgcallparanode(ppn.right);
  744. end;
  745. end;
  746. procedure tcgcallnode.pass_generate_code;
  747. var
  748. name_to_call: TSymStr;
  749. regs_to_save_int,
  750. regs_to_save_address,
  751. regs_to_save_fpu,
  752. regs_to_save_mm : Tcpuregisterset;
  753. href : treference;
  754. pop_size : longint;
  755. pvreg : tregister;
  756. oldaktcallnode : tcallnode;
  757. retlocitem: pcgparalocation;
  758. pd : tprocdef;
  759. callref: boolean;
  760. {$ifdef vtentry}
  761. sym : tasmsymbol;
  762. vmtoffset : aint;
  763. {$endif vtentry}
  764. {$ifdef SUPPORT_SAFECALL}
  765. cgpara : tcgpara;
  766. {$endif}
  767. begin
  768. if not assigned(procdefinition) or
  769. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  770. internalerror(200305264);
  771. extra_pre_call_code;
  772. if assigned(callinitblock) then
  773. secondpass(tnode(callinitblock));
  774. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  775. regs_to_save_address:=paramanager.get_volatile_registers_address(procdefinition.proccalloption);
  776. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  777. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  778. { Include Function result registers }
  779. if (not is_void(resultdef)) then
  780. begin
  781. { The forced returntype may have a different size than the one
  782. declared for the procdef }
  783. retloc:=hlcg.get_call_result_cgpara(procdefinition,typedef);
  784. retlocitem:=retloc.location;
  785. while assigned(retlocitem) do
  786. begin
  787. case retlocitem^.loc of
  788. LOC_REGISTER:
  789. case getregtype(retlocitem^.register) of
  790. R_INTREGISTER:
  791. include(regs_to_save_int,getsupreg(retlocitem^.register));
  792. R_ADDRESSREGISTER:
  793. include(regs_to_save_address,getsupreg(retlocitem^.register));
  794. R_TEMPREGISTER:
  795. ;
  796. else
  797. internalerror(2014020102);
  798. end;
  799. LOC_FPUREGISTER:
  800. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  801. LOC_MMREGISTER:
  802. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  803. LOC_REFERENCE,
  804. LOC_VOID:
  805. ;
  806. else
  807. internalerror(2004110213);
  808. end;
  809. retlocitem:=retlocitem^.next;
  810. end;
  811. end;
  812. { Process parameters, register parameters will be loaded
  813. in imaginary registers. The actual load to the correct
  814. register is done just before the call }
  815. oldaktcallnode:=aktcallnode;
  816. aktcallnode:=self;
  817. if assigned(left) then
  818. tcallparanode(left).secondcallparan;
  819. aktcallnode:=oldaktcallnode;
  820. { procedure variable or normal function call ? }
  821. if (right=nil) then
  822. begin
  823. { register call for WPO (must be done before wpo test below,
  824. otherwise optimised called methods are no longer registered)
  825. }
  826. if (po_virtualmethod in procdefinition.procoptions) and
  827. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  828. assigned(methodpointer) and
  829. (methodpointer.nodetype<>typen) and
  830. (not assigned(current_procinfo) or
  831. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  832. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  833. {$ifdef vtentry}
  834. if not is_interface(tprocdef(procdefinition)._class) then
  835. begin
  836. inc(current_asmdata.NextVTEntryNr);
  837. 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));
  838. end;
  839. {$endif vtentry}
  840. name_to_call:=forcedprocname;
  841. { When methodpointer is typen we don't need (and can't) load
  842. a pointer. We can directly call the correct procdef (PFV) }
  843. if (name_to_call='') and
  844. (po_virtualmethod in procdefinition.procoptions) and
  845. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  846. assigned(methodpointer) and
  847. (methodpointer.nodetype<>typen) and
  848. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  849. begin
  850. { virtual methods require an index }
  851. if tprocdef(procdefinition).extnumber=$ffff then
  852. internalerror(200304021);
  853. { load the VMT entry (address of the virtual method) }
  854. secondpass(vmt_entry);
  855. { register call for WPO }
  856. if (not assigned(current_procinfo) or
  857. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  858. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  859. if not(vmt_entry.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  860. internalerror(2015052502);
  861. href:=vmt_entry.location.reference;
  862. pvreg:=NR_NO;
  863. callref:=can_call_ref(href);
  864. if not callref then
  865. begin
  866. pvreg:=get_call_reg(current_asmdata.CurrAsmList);
  867. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,
  868. vmt_entry.resultdef,vmt_entry.resultdef,
  869. href,pvreg);
  870. end;
  871. { Load parameters that are in temporary registers in the
  872. correct parameter register }
  873. if assigned(left) then
  874. begin
  875. reorder_parameters;
  876. pushparas;
  877. { free the resources allocated for the parameters }
  878. freeparas;
  879. end;
  880. if callref then
  881. extra_call_ref_code(href);
  882. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  883. if cg.uses_registers(R_ADDRESSREGISTER) then
  884. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  885. if cg.uses_registers(R_FPUREGISTER) then
  886. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  887. if cg.uses_registers(R_MMREGISTER) then
  888. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  889. { call method }
  890. extra_call_code;
  891. retloc.resetiftemp;
  892. if callref then
  893. retloc:=do_call_ref(href)
  894. else
  895. begin
  896. retloc:=hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg,paralocs);
  897. unget_call_reg(current_asmdata.CurrAsmList,pvreg);
  898. end;
  899. extra_post_call_code;
  900. end
  901. else
  902. begin
  903. { Load parameters that are in temporary registers in the
  904. correct parameter register }
  905. if assigned(left) then
  906. begin
  907. reorder_parameters;
  908. pushparas;
  909. { free the resources allocated for the parameters }
  910. freeparas;
  911. end;
  912. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  913. if cg.uses_registers(R_ADDRESSREGISTER) then
  914. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  915. if cg.uses_registers(R_FPUREGISTER) then
  916. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  917. if cg.uses_registers(R_MMREGISTER) then
  918. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  919. if procdefinition.proccalloption=pocall_syscall then
  920. do_syscall
  921. else
  922. begin
  923. { Calling interrupt from the same code requires some
  924. extra code }
  925. if (po_interrupt in procdefinition.procoptions) then
  926. extra_interrupt_code;
  927. extra_call_code;
  928. retloc.resetiftemp;
  929. if (name_to_call='') then
  930. name_to_call:=tprocdef(procdefinition).mangledname;
  931. if cnf_inherited in callnodeflags then
  932. retloc:=hlcg.a_call_name_inherited(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,paralocs)
  933. else
  934. retloc:=hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,paralocs,typedef,po_weakexternal in procdefinition.procoptions);
  935. extra_post_call_code;
  936. end;
  937. end;
  938. end
  939. else
  940. { now procedure variable case }
  941. begin
  942. secondpass(right);
  943. { can we directly call the procvar in a memory location? }
  944. callref:=false;
  945. if not(po_is_block in procdefinition.procoptions) and
  946. (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  947. begin
  948. href:=right.location.reference;
  949. callref:=can_call_ref(href);
  950. end;
  951. if not callref then
  952. pvreg:=load_procvar_codeptr
  953. else
  954. pvreg:=NR_INVALID;
  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,procdefinition,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.