ncgcal.pas 55 KB

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