ncgcal.pas 51 KB

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