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