ncgcal.pas 53 KB

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