ncgcal.pas 53 KB

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