ncgcal.pas 58 KB

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