ncgcal.pas 52 KB

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