ncgcal.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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. verbose,globals,
  105. symconst,symtable,symtype,symsym,defutil,paramgr,
  106. pass_2,
  107. nld,ncnv,
  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.get_syscall_libbase_paraloc: pcgparalocation;
  374. var
  375. hsym: tsym;
  376. begin
  377. hsym:=tsym(procdefinition.parast.Find('syscalllib'));
  378. if not assigned(hsym) then
  379. internalerror(2016110605);
  380. result:=tparavarsym(hsym).paraloc[callerside].location;
  381. if not assigned(result) then
  382. internalerror(2016110604);
  383. end;
  384. procedure tcgcallnode.get_syscall_call_ref(out tmpref: treference; reg: tregister);
  385. var
  386. libparaloc: pcgparalocation;
  387. begin
  388. libparaloc:=get_syscall_libbase_paraloc;
  389. case libparaloc^.loc of
  390. LOC_REGISTER:
  391. reference_reset_base(tmpref,libparaloc^.register,-tprocdef(procdefinition).extnumber,ctempposinvalid,sizeof(pint),[]);
  392. LOC_REFERENCE:
  393. begin
  394. reference_reset_base(tmpref,libparaloc^.reference.index,libparaloc^.reference.offset,ctempposinvalid,sizeof(pint),[]);
  395. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,tmpref,reg);
  396. reference_reset_base(tmpref,reg,-tprocdef(procdefinition).extnumber,ctempposinvalid,sizeof(pint),[]);
  397. end;
  398. else
  399. begin
  400. reference_reset(tmpref,0,[]);
  401. internalerror(2016090202);
  402. end;
  403. end;
  404. end;
  405. function tcgcallnode.can_call_ref(var ref: treference): boolean;
  406. begin
  407. result:=false;
  408. end;
  409. procedure tcgcallnode.extra_call_ref_code(var ref: treference);
  410. begin
  411. { no action by default }
  412. end;
  413. function tcgcallnode.do_call_ref(ref: treference): tcgpara;
  414. begin
  415. InternalError(2014012901);
  416. { silence warning }
  417. result.init;
  418. end;
  419. procedure tcgcallnode.load_block_invoke(out toreg: tregister; out callprocdef: tabstractprocdef);
  420. var
  421. href: treference;
  422. literaldef: trecorddef;
  423. begin
  424. literaldef:=get_block_literal_type_for_proc(tabstractprocdef(right.resultdef));
  425. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cpointerdef.getreusable(literaldef),true);
  426. { load the invoke pointer }
  427. hlcg.reference_reset_base(href,right.resultdef,right.location.register,0,ctempposinvalid,right.resultdef.alignment,[]);
  428. callprocdef:=cprocvardef.getreusableprocaddr(procdefinition);
  429. toreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,callprocdef);
  430. hlcg.g_load_field_reg_by_name(current_asmdata.CurrAsmList,literaldef,callprocdef,'INVOKE',href,toreg);
  431. end;
  432. function tcgcallnode.get_call_reg(list: TAsmList): tregister;
  433. begin
  434. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition.address_type);
  435. end;
  436. procedure tcgcallnode.unget_call_reg(list: TAsmList; reg: tregister);
  437. begin
  438. { nothing to do by default }
  439. end;
  440. procedure tcgcallnode.set_result_location(realresdef: tstoreddef);
  441. begin
  442. if realresdef.is_intregable or
  443. realresdef.is_fpuregable or
  444. { avoid temporarily storing pointer-sized entities that can't be
  445. regvars, such as reference-counted pointers, to memory --
  446. no exception can occur right now (except in case of existing
  447. memory corruption), and we'd store them to a regular temp
  448. anyway and that is not safer than keeping them in a register }
  449. ((realresdef.size=sizeof(aint)) and
  450. (retloc.location^.loc=LOC_REGISTER) and
  451. not assigned(retloc.location^.next)) then
  452. location_allocate_register(current_asmdata.CurrAsmList,location,realresdef,false)
  453. else
  454. begin
  455. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),0,[]);
  456. tg.gethltemp(current_asmdata.CurrAsmList,realresdef,retloc.intsize,tt_normal,location.reference);
  457. end;
  458. end;
  459. procedure tcgcallnode.do_release_unused_return_value;
  460. begin
  461. case location.loc of
  462. LOC_REFERENCE :
  463. begin
  464. if is_managed_type(resultdef) then
  465. hlcg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  466. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  467. end;
  468. end;
  469. end;
  470. procedure tcgcallnode.pop_parasize(pop_size:longint);
  471. begin
  472. end;
  473. procedure tcgcallnode.handle_return_value;
  474. var
  475. realresdef: tstoreddef;
  476. begin
  477. { Check that the return location is set when the result is passed in
  478. a parameter }
  479. if paramanager.ret_in_param(resultdef,procdefinition) then
  480. begin
  481. { self.location is set near the end of secondcallparan so it
  482. refers to the implicit result parameter }
  483. if location.loc<>LOC_REFERENCE then
  484. internalerror(200304241);
  485. exit;
  486. end;
  487. if not assigned(typedef) then
  488. realresdef:=tstoreddef(resultdef)
  489. else
  490. realresdef:=tstoreddef(typedef);
  491. { get a tlocation that can hold the return value that's currently in
  492. the return value's tcgpara }
  493. set_result_location(realresdef);
  494. { Do not move the physical register to a virtual one in case
  495. the return value is not used, because if the virtual one is
  496. then mapped to the same register as the physical one, we will
  497. end up with two deallocs of this register (one inserted here,
  498. one inserted by the register allocator), which unbalances the
  499. register allocation information. The return register(s) will
  500. be freed by location_free() in release_unused_return_value
  501. (mantis #13536). }
  502. if (cnf_return_value_used in callnodeflags) or
  503. assigned(funcretnode) then
  504. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,realresdef,retloc,location,false);
  505. { copy value to the final location if this was already provided to the
  506. callnode. This must be done after the call node, because the location can
  507. also be used as parameter and may not be finalized yet }
  508. if assigned(funcretnode) then
  509. begin
  510. funcretnode.pass_generate_code;
  511. { Decrease refcount for refcounted types, this can be skipped when
  512. we have used a temp, because then it is already done from tempcreatenode.
  513. Also no finalize is needed, because there is no risk of exceptions from the
  514. function since this is code is only executed after the function call has returned }
  515. if is_managed_type(funcretnode.resultdef) and
  516. (funcretnode.nodetype<>temprefn) then
  517. hlcg.g_finalize(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  518. case location.loc of
  519. LOC_REGISTER :
  520. begin
  521. {$ifndef cpu64bitalu}
  522. if location.size in [OS_64,OS_S64] then
  523. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  524. else
  525. {$endif}
  526. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,resultdef,resultdef,location.register,funcretnode.location);
  527. location_free(current_asmdata.CurrAsmList,location);
  528. end;
  529. LOC_REFERENCE:
  530. begin
  531. case funcretnode.location.loc of
  532. LOC_REGISTER:
  533. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,resultdef,resultdef,location.reference,funcretnode.location.register);
  534. LOC_REFERENCE:
  535. hlcg.g_concatcopy(current_asmdata.CurrAsmList,resultdef,location.reference,funcretnode.location.reference);
  536. else
  537. internalerror(200802121);
  538. end;
  539. location_freetemp(current_asmdata.CurrAsmList,location);
  540. end;
  541. else
  542. internalerror(200709085);
  543. end;
  544. location := funcretnode.location;
  545. end;
  546. end;
  547. procedure tcgcallnode.release_unused_return_value;
  548. begin
  549. { When the result is not used we need to finalize the result and
  550. can release the temp. This need to be after the callcleanupblock
  551. tree is generated, because that converts the temp from persistent to normal }
  552. if not(cnf_return_value_used in callnodeflags) then
  553. begin
  554. do_release_unused_return_value;
  555. if (retloc.intsize<>0) then
  556. paramanager.freecgpara(current_asmdata.CurrAsmList,retloc);
  557. location_reset(location,LOC_VOID,OS_NO);
  558. end;
  559. end;
  560. procedure tcgcallnode.copy_back_paras;
  561. var
  562. ppn : tcallparanode;
  563. begin
  564. ppn:=tcallparanode(left);
  565. while assigned(ppn) do
  566. begin
  567. if assigned(ppn.paracopyback) then
  568. secondpass(ppn.paracopyback);
  569. ppn:=tcallparanode(ppn.right);
  570. end;
  571. end;
  572. procedure tcgcallnode.release_para_temps;
  573. var
  574. hp,
  575. hp2 : tnode;
  576. ppn : tcallparanode;
  577. begin
  578. { Release temps from parameters }
  579. ppn:=tcallparanode(left);
  580. while assigned(ppn) do
  581. begin
  582. if assigned(ppn.left) then
  583. begin
  584. { don't release the funcret temp }
  585. if not(assigned(ppn.parasym)) or
  586. not(
  587. (vo_is_funcret in ppn.parasym.varoptions) or
  588. (
  589. (vo_is_self in ppn.parasym.varoptions) and
  590. (procdefinition.proctypeoption=potype_constructor) and
  591. (ppn.parasym.vardef.typ<>objectdef)
  592. )
  593. )then
  594. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  595. { process also all nodes of an array of const }
  596. hp:=ppn.left;
  597. while (hp.nodetype=typeconvn) do
  598. hp:=ttypeconvnode(hp).left;
  599. if (hp.nodetype=arrayconstructorn) and
  600. assigned(tarrayconstructornode(hp).left) then
  601. begin
  602. while assigned(hp) do
  603. begin
  604. hp2:=tarrayconstructornode(hp).left;
  605. { ignore typeconvs and addrn inserted by arrayconstructn for
  606. passing a shortstring }
  607. if (hp2.nodetype=typeconvn) and
  608. (tunarynode(hp2).left.nodetype=addrn) then
  609. hp2:=tunarynode(tunarynode(hp2).left).left
  610. else if hp2.nodetype=addrn then
  611. hp2:=tunarynode(hp2).left;
  612. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  613. hp:=tarrayconstructornode(hp).right;
  614. end;
  615. end;
  616. end;
  617. ppn:=tcallparanode(ppn.right);
  618. end;
  619. setlength(paralocs,0);
  620. end;
  621. procedure tcgcallnode.pushparas;
  622. var
  623. ppn : tcgcallparanode;
  624. callerparaloc,
  625. tmpparaloc : pcgparalocation;
  626. sizeleft: aint;
  627. htempref,
  628. href : treference;
  629. calleralignment,
  630. tmpalignment, i: longint;
  631. skipiffinalloc: boolean;
  632. begin
  633. { copy all resources to the allocated registers }
  634. ppn:=tcgcallparanode(left);
  635. while assigned(ppn) do
  636. begin
  637. if (ppn.left.nodetype<>nothingn) then
  638. begin
  639. { better check for the real location of the parameter here, when stack passed parameters
  640. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  641. }
  642. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  643. tmpparaloc:=ppn.tempcgpara.location;
  644. sizeleft:=ppn.parasym.paraloc[callerside].intsize;
  645. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  646. tmpalignment:=ppn.tempcgpara.alignment;
  647. if (tmpalignment=0) or
  648. (calleralignment=0) then
  649. internalerror(2009020701);
  650. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  651. skipiffinalloc:=
  652. not paramanager.use_fixed_stack or
  653. not(ppn.followed_by_stack_tainting_call_cached);
  654. while assigned(callerparaloc) do
  655. begin
  656. { Every paraloc must have a matching tmpparaloc }
  657. if not assigned(tmpparaloc) then
  658. internalerror(200408224);
  659. if callerparaloc^.size<>tmpparaloc^.size then
  660. internalerror(200408225);
  661. case callerparaloc^.loc of
  662. LOC_REGISTER:
  663. begin
  664. if tmpparaloc^.loc<>LOC_REGISTER then
  665. internalerror(200408221);
  666. if getsupreg(callerparaloc^.register)<first_int_imreg then
  667. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  668. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  669. tmpparaloc^.register,callerparaloc^.register);
  670. end;
  671. LOC_FPUREGISTER:
  672. begin
  673. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  674. internalerror(200408222);
  675. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  676. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  677. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,callerparaloc^.register);
  678. end;
  679. LOC_MMREGISTER:
  680. begin
  681. if tmpparaloc^.loc<>LOC_MMREGISTER then
  682. internalerror(200408223);
  683. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  684. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  685. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  686. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  687. end;
  688. LOC_REFERENCE:
  689. begin
  690. if not(skipiffinalloc and
  691. paramanager.is_stack_paraloc(callerparaloc)) then
  692. begin
  693. { Can't have a data copied to the stack, every location
  694. must contain a valid size field }
  695. if (tmpparaloc^.size=OS_NO) and
  696. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  697. assigned(tmpparaloc^.next)) then
  698. internalerror(200501281);
  699. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,ctempposinvalid,calleralignment,[]);
  700. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  701. case tmpparaloc^.loc of
  702. LOC_REFERENCE:
  703. begin
  704. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,ctempposinvalid,tmpalignment,[]);
  705. { use concatcopy, because it can also be a float which fails when
  706. load_ref_ref is used }
  707. if (tmpparaloc^.size <> OS_NO) then
  708. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  709. else
  710. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  711. end;
  712. LOC_REGISTER:
  713. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  714. LOC_FPUREGISTER:
  715. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  716. LOC_MMREGISTER:
  717. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  718. else
  719. internalerror(200402081);
  720. end;
  721. end;
  722. end;
  723. end;
  724. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  725. callerparaloc:=callerparaloc^.next;
  726. tmpparaloc:=tmpparaloc^.next;
  727. end;
  728. end;
  729. ppn:=tcgcallparanode(ppn.right);
  730. end;
  731. setlength(paralocs,procdefinition.paras.count);
  732. for i:=0 to procdefinition.paras.count-1 do
  733. paralocs[i]:=@tparavarsym(procdefinition.paras[i]).paraloc[callerside];
  734. end;
  735. procedure tcgcallnode.load_complex_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef);
  736. var
  737. srcreg: tregister;
  738. begin
  739. { this is safe even on i8086, because procvardef code pointers are
  740. always far there (so the current state of far calls vs the state
  741. of far calls where the procvardef was defined does not matter,
  742. even though the procvardef constructor called by getcopyas looks at
  743. it) }
  744. callprocdef:=cprocvardef.getreusableprocaddr(procdefinition);
  745. reg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,callprocdef);
  746. { in case we have a method pointer on a big endian target in registers,
  747. the method address is stored in registerhi (it's the first field
  748. in the tmethod record) }
  749. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  750. begin
  751. if not(right.location.size in [OS_PAIR,OS_SPAIR]) then
  752. internalerror(2014081401);
  753. if (target_info.endian=endian_big) then
  754. srcreg:=right.location.registerhi
  755. else
  756. srcreg:=right.location.register;
  757. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,callprocdef,callprocdef,srcreg,reg)
  758. end
  759. else
  760. begin
  761. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,procdefinition);
  762. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(procdefinition),cpointerdef.getreusable(callprocdef),right.location.reference);
  763. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,callprocdef,callprocdef,right.location.reference,reg);
  764. end;
  765. end;
  766. procedure tcgcallnode.load_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef);
  767. begin
  768. if po_is_block in procdefinition.procoptions then
  769. load_block_invoke(reg,callprocdef)
  770. else if not(procdefinition.is_addressonly) then
  771. load_complex_procvar_codeptr(reg,callprocdef)
  772. else
  773. begin
  774. reg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition);
  775. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,procdefinition,procdefinition,right.location,reg);
  776. callprocdef:=procdefinition;
  777. end;
  778. callprocdef.init_paraloc_info(callerside);
  779. end;
  780. procedure tcgcallnode.freeparas;
  781. var
  782. ppn : tcgcallparanode;
  783. begin
  784. { free the resources allocated for the parameters }
  785. ppn:=tcgcallparanode(left);
  786. while assigned(ppn) do
  787. begin
  788. if (ppn.left.nodetype<>nothingn) then
  789. begin
  790. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  791. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  792. end;
  793. ppn:=tcgcallparanode(ppn.right);
  794. end;
  795. end;
  796. procedure tcgcallnode.pass_generate_code;
  797. var
  798. name_to_call: TSymStr;
  799. regs_to_save_int,
  800. regs_to_save_address,
  801. regs_to_save_fpu,
  802. regs_to_save_mm : Tcpuregisterset;
  803. href : treference;
  804. pop_size : longint;
  805. pvreg : tregister;
  806. oldaktcallnode : tcallnode;
  807. retlocitem: pcgparalocation;
  808. callpvdef: tabstractprocdef;
  809. pd : tprocdef;
  810. callref: boolean;
  811. {$ifdef vtentry}
  812. sym : tasmsymbol;
  813. vmtoffset : aint;
  814. {$endif vtentry}
  815. {$ifdef SUPPORT_SAFECALL}
  816. cgpara : tcgpara;
  817. {$endif}
  818. begin
  819. if not assigned(procdefinition) or
  820. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  821. internalerror(200305264);
  822. extra_pre_call_code;
  823. if assigned(callinitblock) then
  824. secondpass(tnode(callinitblock));
  825. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  826. regs_to_save_address:=paramanager.get_volatile_registers_address(procdefinition.proccalloption);
  827. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  828. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  829. { Include Function result registers }
  830. if (not is_void(resultdef)) then
  831. begin
  832. { The forced returntype may have a different size than the one
  833. declared for the procdef }
  834. retloc:=hlcg.get_call_result_cgpara(procdefinition,typedef);
  835. retlocitem:=retloc.location;
  836. while assigned(retlocitem) do
  837. begin
  838. case retlocitem^.loc of
  839. LOC_REGISTER:
  840. case getregtype(retlocitem^.register) of
  841. R_INTREGISTER:
  842. include(regs_to_save_int,getsupreg(retlocitem^.register));
  843. R_ADDRESSREGISTER:
  844. include(regs_to_save_address,getsupreg(retlocitem^.register));
  845. R_TEMPREGISTER:
  846. ;
  847. else
  848. internalerror(2014020102);
  849. end;
  850. LOC_FPUREGISTER:
  851. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  852. LOC_MMREGISTER:
  853. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  854. LOC_REFERENCE,
  855. LOC_VOID:
  856. ;
  857. else
  858. internalerror(2004110213);
  859. end;
  860. retlocitem:=retlocitem^.next;
  861. end;
  862. end;
  863. { Process parameters, register parameters will be loaded
  864. in imaginary registers. The actual load to the correct
  865. register is done just before the call }
  866. oldaktcallnode:=aktcallnode;
  867. aktcallnode:=self;
  868. if assigned(left) then
  869. tcallparanode(left).secondcallparan;
  870. aktcallnode:=oldaktcallnode;
  871. { procedure variable or normal function call ? }
  872. if (right=nil) then
  873. begin
  874. { register call for WPO (must be done before wpo test below,
  875. otherwise optimised called methods are no longer registered)
  876. }
  877. if (po_virtualmethod in procdefinition.procoptions) and
  878. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  879. assigned(methodpointer) and
  880. (methodpointer.nodetype<>typen) and
  881. (not assigned(current_procinfo) or
  882. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  883. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  884. {$ifdef vtentry}
  885. if not is_interface(tprocdef(procdefinition)._class) then
  886. begin
  887. inc(current_asmdata.NextVTEntryNr);
  888. 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));
  889. end;
  890. {$endif vtentry}
  891. name_to_call:=forcedprocname;
  892. { When methodpointer is typen we don't need (and can't) load
  893. a pointer. We can directly call the correct procdef (PFV) }
  894. if (name_to_call='') and
  895. (po_virtualmethod in procdefinition.procoptions) and
  896. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  897. assigned(methodpointer) and
  898. (methodpointer.nodetype<>typen) and
  899. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  900. begin
  901. { virtual methods require an index }
  902. if tprocdef(procdefinition).extnumber=$ffff then
  903. internalerror(200304021);
  904. { load the VMT entry (address of the virtual method) }
  905. secondpass(vmt_entry);
  906. { register call for WPO }
  907. if (not assigned(current_procinfo) or
  908. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  909. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  910. if not(vmt_entry.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  911. internalerror(2015052502);
  912. href:=vmt_entry.location.reference;
  913. pvreg:=NR_NO;
  914. callref:=can_call_ref(href);
  915. if not callref then
  916. begin
  917. pvreg:=get_call_reg(current_asmdata.CurrAsmList);
  918. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,
  919. vmt_entry.resultdef,vmt_entry.resultdef,
  920. href,pvreg);
  921. end;
  922. { Load parameters that are in temporary registers in the
  923. correct parameter register }
  924. if assigned(left) then
  925. begin
  926. reorder_parameters;
  927. pushparas;
  928. end;
  929. if callref then
  930. extra_call_ref_code(href);
  931. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  932. if cg.uses_registers(R_ADDRESSREGISTER) then
  933. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  934. if cg.uses_registers(R_FPUREGISTER) then
  935. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  936. if cg.uses_registers(R_MMREGISTER) then
  937. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  938. { call method }
  939. extra_call_code;
  940. retloc.resetiftemp;
  941. if callref then
  942. retloc:=do_call_ref(href)
  943. else
  944. begin
  945. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,vmt_entry.resultdef,cpointerdef.getreusable(procdefinition),pvreg);
  946. retloc:=hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg,paralocs);
  947. unget_call_reg(current_asmdata.CurrAsmList,pvreg);
  948. end;
  949. extra_post_call_code;
  950. end
  951. else
  952. begin
  953. { Load parameters that are in temporary registers in the
  954. correct parameter register }
  955. if assigned(left) then
  956. begin
  957. reorder_parameters;
  958. pushparas;
  959. end;
  960. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  961. if cg.uses_registers(R_ADDRESSREGISTER) then
  962. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  963. if cg.uses_registers(R_FPUREGISTER) then
  964. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  965. if cg.uses_registers(R_MMREGISTER) then
  966. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  967. if procdefinition.proccalloption=pocall_syscall then
  968. do_syscall
  969. else
  970. begin
  971. { Calling interrupt from the same code requires some
  972. extra code }
  973. if (po_interrupt in procdefinition.procoptions) then
  974. extra_interrupt_code;
  975. extra_call_code;
  976. retloc.resetiftemp;
  977. if (name_to_call='') then
  978. name_to_call:=tprocdef(procdefinition).mangledname;
  979. if cnf_inherited in callnodeflags then
  980. retloc:=hlcg.a_call_name_inherited(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,paralocs)
  981. else
  982. retloc:=hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,paralocs,typedef,po_weakexternal in procdefinition.procoptions);
  983. extra_post_call_code;
  984. end;
  985. end;
  986. end
  987. else
  988. { now procedure variable case }
  989. begin
  990. secondpass(right);
  991. { can we directly call the procvar in a memory location? }
  992. callref:=false;
  993. if not(po_is_block in procdefinition.procoptions) and
  994. (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  995. begin
  996. href:=right.location.reference;
  997. callref:=can_call_ref(href);
  998. end;
  999. if not callref then
  1000. load_procvar_codeptr(pvreg,callpvdef)
  1001. else
  1002. begin
  1003. pvreg:=NR_INVALID;
  1004. callpvdef:=nil;
  1005. end;
  1006. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1007. { Load parameters that are in temporary registers in the
  1008. correct parameter register }
  1009. if assigned(left) then
  1010. begin
  1011. reorder_parameters;
  1012. pushparas;
  1013. end;
  1014. if callref then
  1015. extra_call_ref_code(href);
  1016. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  1017. if cg.uses_registers(R_ADDRESSREGISTER) then
  1018. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  1019. if cg.uses_registers(R_FPUREGISTER) then
  1020. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  1021. if cg.uses_registers(R_MMREGISTER) then
  1022. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  1023. { Calling interrupt from the same code requires some
  1024. extra code }
  1025. if (po_interrupt in procdefinition.procoptions) then
  1026. extra_interrupt_code;
  1027. extra_call_code;
  1028. retloc.resetiftemp;
  1029. if callref then
  1030. retloc:=do_call_ref(href)
  1031. else
  1032. retloc:=hlcg.a_call_reg(current_asmdata.CurrAsmList,callpvdef,pvreg,paralocs);
  1033. extra_post_call_code;
  1034. end;
  1035. { free the resources allocated for the parameters }
  1036. if assigned(left) then
  1037. freeparas;
  1038. { Need to remove the parameters from the stack? }
  1039. if procdefinition.proccalloption in clearstack_pocalls then
  1040. begin
  1041. pop_size:=pushedparasize;
  1042. { for Cdecl functions we don't need to pop the funcret when it
  1043. was pushed by para. Except for safecall functions with
  1044. safecall-exceptions enabled. In that case the funcret is always
  1045. returned as a para which is considered a normal para on the
  1046. c-side, so the funcret has to be pop'ed normally. }
  1047. if not ((procdefinition.proccalloption=pocall_safecall) and
  1048. (tf_safecall_exceptions in target_info.flags)) and
  1049. paramanager.ret_in_param(procdefinition.returndef,procdefinition) then
  1050. dec(pop_size,sizeof(pint));
  1051. { Remove parameters/alignment from the stack }
  1052. pop_parasize(pop_size);
  1053. end
  1054. { in case we use a fixed stack, we did not push anything, if the stack is
  1055. really adjusted because a ret xxx was done, depends on
  1056. pop_parasize which uses pushedparasize to determine this
  1057. This does not apply to interrupt procedures, their ret statment never clears any stack parameters }
  1058. else if paramanager.use_fixed_stack and
  1059. not(po_interrupt in procdefinition.procoptions) and
  1060. (target_info.abi=abi_i386_dynalignedstack) then
  1061. begin
  1062. { however, a delphi style frame pointer for a nested subroutine
  1063. is not cleared by the callee, so we have to compensate for this
  1064. by passing 4 as pushedparasize does include it }
  1065. if po_delphi_nested_cc in procdefinition.procoptions then
  1066. pop_parasize(sizeof(pint))
  1067. else
  1068. pop_parasize(0);
  1069. end
  1070. { frame pointer parameter is popped by the caller when it's passed the
  1071. Delphi way }
  1072. else if (po_delphi_nested_cc in procdefinition.procoptions) and
  1073. not paramanager.use_fixed_stack then
  1074. pop_parasize(sizeof(pint));
  1075. { Release registers, but not the registers that contain the
  1076. function result }
  1077. if (not is_void(resultdef)) then
  1078. begin
  1079. retlocitem:=retloc.location;
  1080. while assigned(retlocitem) do
  1081. begin
  1082. case retlocitem^.loc of
  1083. LOC_REGISTER:
  1084. case getregtype(retlocitem^.register) of
  1085. R_INTREGISTER:
  1086. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  1087. R_ADDRESSREGISTER:
  1088. exclude(regs_to_save_address,getsupreg(retlocitem^.register));
  1089. R_TEMPREGISTER:
  1090. ;
  1091. else
  1092. internalerror(2014020103);
  1093. end;
  1094. LOC_FPUREGISTER:
  1095. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  1096. LOC_MMREGISTER:
  1097. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  1098. LOC_REFERENCE,
  1099. LOC_VOID:
  1100. ;
  1101. else
  1102. internalerror(2004110214);
  1103. end;
  1104. retlocitem:=retlocitem^.next;
  1105. end;
  1106. end;
  1107. if cg.uses_registers(R_MMREGISTER) then
  1108. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  1109. if cg.uses_registers(R_FPUREGISTER) then
  1110. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  1111. if cg.uses_registers(R_ADDRESSREGISTER) then
  1112. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  1113. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  1114. {$ifdef SUPPORT_SAFECALL}
  1115. if (procdefinition.proccalloption=pocall_safecall) and
  1116. (tf_safecall_exceptions in target_info.flags) then
  1117. begin
  1118. pd:=search_system_proc('fpc_safecallcheck');
  1119. cgpara.init;
  1120. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  1121. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_INT,NR_FUNCTION_RESULT_REG,cgpara);
  1122. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  1123. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK');
  1124. cgpara.done;
  1125. end;
  1126. {$endif}
  1127. { handle function results }
  1128. if (not is_void(resultdef)) then
  1129. handle_return_value
  1130. else
  1131. location_reset(location,LOC_VOID,OS_NO);
  1132. { convert persistent temps for parameters and function result to normal temps }
  1133. if assigned(callcleanupblock) then
  1134. secondpass(tnode(callcleanupblock));
  1135. { copy back copy-out parameters if any }
  1136. copy_back_paras;
  1137. { release temps and finalize unused return values, must be
  1138. after the callcleanupblock because that converts temps
  1139. from persistent to normal }
  1140. release_unused_return_value;
  1141. { release temps of paras }
  1142. release_para_temps;
  1143. { check for fpu exceptions }
  1144. if cnf_check_fpu_exceptions in callnodeflags then
  1145. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  1146. { perhaps i/o check ? }
  1147. if (cs_check_io in current_settings.localswitches) and
  1148. (po_iocheck in procdefinition.procoptions) and
  1149. not(po_iocheck in current_procinfo.procdef.procoptions) and
  1150. { no IO check for methods and procedure variables }
  1151. (right=nil) and
  1152. not(po_virtualmethod in procdefinition.procoptions) then
  1153. begin
  1154. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_iocheck',[],nil).resetiftemp;
  1155. end;
  1156. end;
  1157. destructor tcgcallnode.destroy;
  1158. begin
  1159. retloc.resetiftemp;
  1160. inherited destroy;
  1161. end;
  1162. begin
  1163. ccallparanode:=tcgcallparanode;
  1164. ccallnode:=tcgcallnode;
  1165. end.