ncgcal.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. symdef,node,ncal;
  25. type
  26. tcgcallparanode = class(tcallparanode)
  27. private
  28. tempcgpara : tcgpara;
  29. procedure push_addr_para;
  30. procedure push_value_para;
  31. public
  32. constructor create(expr,next : tnode);override;
  33. destructor destroy;override;
  34. procedure secondcallparan;override;
  35. end;
  36. tcgcallnode = class(tcallnode)
  37. private
  38. procedure handle_return_value;
  39. procedure release_unused_return_value;
  40. procedure release_para_temps;
  41. procedure pushparas;
  42. procedure freeparas;
  43. protected
  44. retloc: tcgpara;
  45. framepointer_paraloc : tcgpara;
  46. {# This routine is used to push the current frame pointer
  47. on the stack. This is used in nested routines where the
  48. value of the frame pointer is always pushed as an extra
  49. parameter.
  50. The default handling is the standard handling used on
  51. most stack based machines, where the frame pointer is
  52. the first invisible parameter.
  53. }
  54. procedure pop_parasize(pop_size:longint);virtual;
  55. procedure extra_interrupt_code;virtual;
  56. procedure extra_call_code;virtual;
  57. procedure extra_post_call_code;virtual;
  58. procedure do_syscall;virtual;abstract;
  59. { The function result is returned in a tcgpara. This tcgpara has to
  60. be translated into a tlocation so the rest of the code generator
  61. can work with it. This routine decides what the most appropriate
  62. tlocation is and sets self.location based on that. }
  63. procedure set_result_location(realresdef: tstoreddef);virtual;
  64. public
  65. procedure pass_generate_code;override;
  66. destructor destroy;override;
  67. end;
  68. implementation
  69. uses
  70. systems,
  71. cutils,verbose,globals,
  72. cpuinfo,
  73. symconst,symtable,defutil,paramgr,
  74. cgbase,pass_2,
  75. aasmbase,aasmtai,aasmdata,
  76. nbas,nmem,nld,ncnv,nutils,
  77. {$ifdef x86}
  78. cga,cgx86,aasmcpu,
  79. {$endif x86}
  80. ncgutil,
  81. cgobj,tgobj,
  82. procinfo,
  83. wpobase;
  84. {*****************************************************************************
  85. TCGCALLPARANODE
  86. *****************************************************************************}
  87. constructor tcgcallparanode.create(expr,next : tnode);
  88. begin
  89. inherited create(expr,next);
  90. tempcgpara.init;
  91. end;
  92. destructor tcgcallparanode.destroy;
  93. begin
  94. tempcgpara.done;
  95. inherited destroy;
  96. end;
  97. procedure tcgcallparanode.push_addr_para;
  98. begin
  99. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  100. internalerror(200304235);
  101. cg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  102. end;
  103. procedure tcgcallparanode.push_value_para;
  104. begin
  105. { we've nothing to push when the size of the parameter is 0 }
  106. if left.resultdef.size=0 then
  107. exit;
  108. { Move flags and jump in register to make it less complex }
  109. if left.location.loc in [LOC_FLAGS,LOC_JUMP,LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF] then
  110. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),false);
  111. { load the parameter's tlocation into its cgpara }
  112. gen_load_loc_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location,tempcgpara)
  113. end;
  114. procedure tcgcallparanode.secondcallparan;
  115. var
  116. href : treference;
  117. otlabel,
  118. oflabel : tasmlabel;
  119. begin
  120. if not(assigned(parasym)) then
  121. internalerror(200304242);
  122. { Skip nothingn nodes which are used after disabling
  123. a parameter }
  124. if (left.nodetype<>nothingn) then
  125. begin
  126. otlabel:=current_procinfo.CurrTrueLabel;
  127. oflabel:=current_procinfo.CurrFalseLabel;
  128. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  129. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  130. secondpass(left);
  131. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  132. { release memory for refcnt out parameters }
  133. if (parasym.varspez=vs_out) and
  134. is_managed_type(left.resultdef) then
  135. begin
  136. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false,sizeof(pint));
  137. if is_open_array(resultdef) then
  138. begin
  139. { if elementdef is not managed, omit fpc_decref_array
  140. because it won't do anything anyway }
  141. if is_managed_type(tarraydef(resultdef).elementdef) then
  142. begin
  143. if third=nil then
  144. InternalError(201103063);
  145. secondpass(third);
  146. cg.g_array_rtti_helper(current_asmdata.CurrAsmList,tarraydef(resultdef).elementdef,
  147. href,third.location,'FPC_DECREF_ARRAY');
  148. end;
  149. end
  150. else
  151. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  152. end;
  153. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,not followed_by_stack_tainting_call_cached,tempcgpara);
  154. { handle varargs first, because parasym is not valid }
  155. if (cpf_varargs_para in callparaflags) then
  156. begin
  157. if paramanager.push_addr_param(vs_value,left.resultdef,
  158. aktcallnode.procdefinition.proccalloption) then
  159. push_addr_para
  160. else
  161. push_value_para;
  162. end
  163. { hidden parameters }
  164. else if (vo_is_hidden_para in parasym.varoptions) then
  165. begin
  166. { don't push a node that already generated a pointer type
  167. by address for implicit hidden parameters }
  168. if (vo_is_funcret in parasym.varoptions) or
  169. { pass "this" in C++ classes explicitly as pointer
  170. because push_addr_param might not be true for them }
  171. (is_cppclass(parasym.vardef) and (vo_is_self in parasym.varoptions)) or
  172. (not(left.resultdef.typ in [pointerdef,classrefdef]) and
  173. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  174. aktcallnode.procdefinition.proccalloption)) then
  175. push_addr_para
  176. else
  177. push_value_para;
  178. end
  179. { formal def }
  180. else if (parasym.vardef.typ=formaldef) then
  181. begin
  182. { allow passing of a constant to a const formaldef }
  183. if (parasym.varspez=vs_const) and
  184. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  185. location_force_mem(current_asmdata.CurrAsmList,left.location);
  186. push_addr_para;
  187. end
  188. { Normal parameter }
  189. else
  190. begin
  191. { don't push a node that already generated a pointer type
  192. by address for implicit hidden parameters }
  193. if (not(
  194. (vo_is_hidden_para in parasym.varoptions) and
  195. (left.resultdef.typ in [pointerdef,classrefdef])
  196. ) and
  197. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  198. aktcallnode.procdefinition.proccalloption)) and
  199. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  200. not(
  201. is_array_of_const(parasym.vardef) and
  202. is_dynamic_array(left.resultdef)
  203. ) then
  204. begin
  205. { Passing a var parameter to a var parameter, we can
  206. just push the address transparently }
  207. if (left.nodetype=loadn) and
  208. (tloadnode(left).is_addr_param_load) then
  209. begin
  210. if (left.location.reference.index<>NR_NO) or
  211. (left.location.reference.offset<>0) then
  212. internalerror(200410107);
  213. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_ADDR,left.location.reference.base,tempcgpara)
  214. end
  215. else
  216. begin
  217. { Force to be in memory }
  218. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  219. location_force_mem(current_asmdata.CurrAsmList,left.location);
  220. push_addr_para;
  221. end;
  222. end
  223. else
  224. push_value_para;
  225. end;
  226. current_procinfo.CurrTrueLabel:=otlabel;
  227. current_procinfo.CurrFalseLabel:=oflabel;
  228. { update return location in callnode when this is the function
  229. result }
  230. if assigned(parasym) and
  231. (vo_is_funcret in parasym.varoptions) then
  232. location_copy(aktcallnode.location,left.location);
  233. end;
  234. { next parameter }
  235. if assigned(right) then
  236. tcallparanode(right).secondcallparan;
  237. end;
  238. {*****************************************************************************
  239. TCGCALLNODE
  240. *****************************************************************************}
  241. procedure tcgcallnode.extra_interrupt_code;
  242. begin
  243. end;
  244. procedure tcgcallnode.extra_call_code;
  245. begin
  246. end;
  247. procedure tcgcallnode.extra_post_call_code;
  248. begin
  249. end;
  250. procedure tcgcallnode.set_result_location(realresdef: tstoreddef);
  251. begin
  252. if realresdef.is_intregable or
  253. realresdef.is_fpuregable or
  254. { avoid temporarily storing pointer-sized entities that can't be
  255. regvars, such as reference-counted pointers, to memory --
  256. no exception can occur right now (except in case of existing
  257. memory corruption), and we'd store them to a regular temp
  258. anyway and that is not safer than keeping them in a register }
  259. ((realresdef.size=sizeof(aint)) and
  260. (retloc.location^.loc=LOC_REGISTER) and
  261. not assigned(retloc.location^.next)) then
  262. location_allocate_register(current_asmdata.CurrAsmList,location,realresdef,false)
  263. else
  264. begin
  265. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),0);
  266. tg.GetTemp(current_asmdata.CurrAsmList,retloc.intsize,retloc.Alignment,tt_normal,location.reference);
  267. end;
  268. end;
  269. procedure tcgcallnode.pop_parasize(pop_size:longint);
  270. begin
  271. end;
  272. procedure tcgcallnode.handle_return_value;
  273. var
  274. realresdef: tstoreddef;
  275. begin
  276. { Check that the return location is set when the result is passed in
  277. a parameter }
  278. if (procdefinition.proctypeoption<>potype_constructor) and
  279. paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  280. begin
  281. { self.location is set near the end of secondcallparan so it
  282. refers to the implicit result parameter }
  283. if location.loc<>LOC_REFERENCE then
  284. internalerror(200304241);
  285. exit;
  286. end;
  287. if not assigned(typedef) then
  288. realresdef:=tstoreddef(resultdef)
  289. else
  290. realresdef:=tstoreddef(typedef);
  291. {$ifdef x86}
  292. if (retloc.location^.loc=LOC_FPUREGISTER) then
  293. begin
  294. tcgx86(cg).inc_fpu_stack;
  295. location_reset(location,LOC_FPUREGISTER,retloc.location^.size);
  296. location.register:=retloc.location^.register;
  297. end
  298. else
  299. {$endif x86}
  300. begin
  301. { get a tlocation that can hold the return value that's currently in
  302. the the return value's tcgpara }
  303. set_result_location(realresdef);
  304. { Do not move the physical register to a virtual one in case
  305. the return value is not used, because if the virtual one is
  306. then mapped to the same register as the physical one, we will
  307. end up with two deallocs of this register (one inserted here,
  308. one inserted by the register allocator), which unbalances the
  309. register allocation information. The return register(s) will
  310. be freed by location_free() in release_unused_return_value
  311. (mantis #13536). }
  312. if (cnf_return_value_used in callnodeflags) or
  313. assigned(funcretnode) then
  314. begin
  315. gen_load_cgpara_loc(current_asmdata.CurrAsmList,realresdef,retloc,location,false);
  316. {$ifdef arm}
  317. if (resultdef.typ=floatdef) and
  318. (location.loc=LOC_REGISTER) and
  319. (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) then
  320. begin
  321. location_force_mem(current_asmdata.CurrAsmList,location);
  322. end;
  323. {$endif arm}
  324. end;
  325. end;
  326. { copy value to the final location if this was already provided to the
  327. callnode. This must be done after the call node, because the location can
  328. also be used as parameter and may not be finalized yet }
  329. if assigned(funcretnode) then
  330. begin
  331. funcretnode.pass_generate_code;
  332. { Decrease refcount for refcounted types, this can be skipped when
  333. we have used a temp, because then it is already done from tempcreatenode.
  334. Also no finalize is needed, because there is no risk of exceptions from the
  335. function since this is code is only executed after the function call has returned }
  336. if is_managed_type(funcretnode.resultdef) and
  337. (funcretnode.nodetype<>temprefn) then
  338. cg.g_decrrefcount(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  339. case location.loc of
  340. LOC_REGISTER :
  341. begin
  342. {$ifndef cpu64bitalu}
  343. if location.size in [OS_64,OS_S64] then
  344. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  345. else
  346. {$endif}
  347. cg.a_load_reg_loc(current_asmdata.CurrAsmList,location.size,location.register,funcretnode.location);
  348. location_free(current_asmdata.CurrAsmList,location);
  349. end;
  350. LOC_REFERENCE:
  351. begin
  352. case funcretnode.location.loc of
  353. LOC_REGISTER:
  354. cg.a_load_ref_reg(current_asmdata.CurrAsmList,location.size,location.size,location.reference,funcretnode.location.register);
  355. LOC_REFERENCE:
  356. cg.g_concatcopy(current_asmdata.CurrAsmList,location.reference,funcretnode.location.reference,resultdef.size);
  357. else
  358. internalerror(200802121);
  359. end;
  360. location_freetemp(current_asmdata.CurrAsmList,location);
  361. end;
  362. else
  363. internalerror(200709085);
  364. end;
  365. location := funcretnode.location;
  366. end;
  367. end;
  368. procedure tcgcallnode.release_unused_return_value;
  369. begin
  370. { When the result is not used we need to finalize the result and
  371. can release the temp. This need to be after the callcleanupblock
  372. tree is generated, because that converts the temp from persistent to normal }
  373. if not(cnf_return_value_used in callnodeflags) then
  374. begin
  375. case location.loc of
  376. LOC_REFERENCE :
  377. begin
  378. if is_managed_type(resultdef) then
  379. cg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  380. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  381. end;
  382. {$ifdef x86}
  383. LOC_FPUREGISTER :
  384. begin
  385. { release FPU stack }
  386. emit_reg(A_FSTP,S_NO,NR_FPU_RESULT_REG);
  387. tcgx86(cg).dec_fpu_stack;
  388. end;
  389. {$endif x86}
  390. end;
  391. if (retloc.intsize<>0) then
  392. paramanager.freecgpara(current_asmdata.CurrAsmList,retloc);
  393. location_reset(location,LOC_VOID,OS_NO);
  394. end;
  395. end;
  396. procedure tcgcallnode.release_para_temps;
  397. var
  398. hp,
  399. hp2 : tnode;
  400. ppn : tcallparanode;
  401. begin
  402. { Release temps from parameters }
  403. ppn:=tcallparanode(left);
  404. while assigned(ppn) do
  405. begin
  406. if assigned(ppn.left) then
  407. begin
  408. { don't release the funcret temp }
  409. if not(assigned(ppn.parasym)) or
  410. not(vo_is_funcret in ppn.parasym.varoptions) then
  411. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  412. { process also all nodes of an array of const }
  413. hp:=ppn.left;
  414. while (hp.nodetype=typeconvn) do
  415. hp:=ttypeconvnode(hp).left;
  416. if (hp.nodetype=arrayconstructorn) and
  417. assigned(tarrayconstructornode(hp).left) then
  418. begin
  419. while assigned(hp) do
  420. begin
  421. hp2:=tarrayconstructornode(hp).left;
  422. { ignore typeconvs and addrn inserted by arrayconstructn for
  423. passing a shortstring }
  424. if (hp2.nodetype=typeconvn) and
  425. (tunarynode(hp2).left.nodetype=addrn) then
  426. hp2:=tunarynode(tunarynode(hp2).left).left;
  427. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  428. hp:=tarrayconstructornode(hp).right;
  429. end;
  430. end;
  431. end;
  432. ppn:=tcallparanode(ppn.right);
  433. end;
  434. end;
  435. procedure tcgcallnode.pushparas;
  436. var
  437. ppn : tcgcallparanode;
  438. callerparaloc,
  439. tmpparaloc : pcgparalocation;
  440. sizeleft: aint;
  441. htempref,
  442. href : treference;
  443. calleralignment,
  444. tmpalignment: longint;
  445. skipiffinalloc: boolean;
  446. begin
  447. { copy all resources to the allocated registers }
  448. ppn:=tcgcallparanode(left);
  449. while assigned(ppn) do
  450. begin
  451. if (ppn.left.nodetype<>nothingn) then
  452. begin
  453. { better check for the real location of the parameter here, when stack passed parameters
  454. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  455. }
  456. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  457. tmpparaloc:=ppn.tempcgpara.location;
  458. sizeleft:=ppn.tempcgpara.intsize;
  459. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  460. tmpalignment:=ppn.tempcgpara.alignment;
  461. if (tmpalignment=0) or
  462. (calleralignment=0) then
  463. internalerror(2009020701);
  464. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  465. skipiffinalloc:=
  466. not paramanager.use_fixed_stack or
  467. not(ppn.followed_by_stack_tainting_call_cached);
  468. while assigned(callerparaloc) do
  469. begin
  470. { Every paraloc must have a matching tmpparaloc }
  471. if not assigned(tmpparaloc) then
  472. internalerror(200408224);
  473. if callerparaloc^.size<>tmpparaloc^.size then
  474. internalerror(200408225);
  475. case callerparaloc^.loc of
  476. LOC_REGISTER:
  477. begin
  478. if tmpparaloc^.loc<>LOC_REGISTER then
  479. internalerror(200408221);
  480. if getsupreg(callerparaloc^.register)<first_int_imreg then
  481. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  482. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  483. tmpparaloc^.register,callerparaloc^.register);
  484. end;
  485. LOC_FPUREGISTER:
  486. begin
  487. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  488. internalerror(200408222);
  489. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  490. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  491. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  492. end;
  493. LOC_MMREGISTER:
  494. begin
  495. if tmpparaloc^.loc<>LOC_MMREGISTER then
  496. internalerror(200408223);
  497. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  498. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  499. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  500. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  501. end;
  502. LOC_REFERENCE:
  503. begin
  504. if not(skipiffinalloc and
  505. paramanager.is_stack_paraloc(callerparaloc)) then
  506. begin
  507. { Can't have a data copied to the stack, every location
  508. must contain a valid size field }
  509. if (tmpparaloc^.size=OS_NO) and
  510. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  511. assigned(tmpparaloc^.next)) then
  512. internalerror(200501281);
  513. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  514. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  515. case tmpparaloc^.loc of
  516. LOC_REFERENCE:
  517. begin
  518. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  519. { use concatcopy, because it can also be a float which fails when
  520. load_ref_ref is used }
  521. if (ppn.tempcgpara.size <> OS_NO) then
  522. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  523. else
  524. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  525. end;
  526. LOC_REGISTER:
  527. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  528. LOC_FPUREGISTER:
  529. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  530. LOC_MMREGISTER:
  531. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  532. else
  533. internalerror(200402081);
  534. end;
  535. end;
  536. end;
  537. end;
  538. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  539. callerparaloc:=callerparaloc^.next;
  540. tmpparaloc:=tmpparaloc^.next;
  541. end;
  542. end;
  543. ppn:=tcgcallparanode(ppn.right);
  544. end;
  545. end;
  546. procedure tcgcallnode.freeparas;
  547. var
  548. ppn : tcgcallparanode;
  549. begin
  550. { free the resources allocated for the parameters }
  551. ppn:=tcgcallparanode(left);
  552. while assigned(ppn) do
  553. begin
  554. if (ppn.left.nodetype<>nothingn) then
  555. begin
  556. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  557. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  558. end;
  559. ppn:=tcgcallparanode(ppn.right);
  560. end;
  561. end;
  562. procedure tcgcallnode.pass_generate_code;
  563. var
  564. name_to_call: shortstring;
  565. regs_to_save_int,
  566. regs_to_save_fpu,
  567. regs_to_save_mm : Tcpuregisterset;
  568. href : treference;
  569. pop_size : longint;
  570. vmtoffset : aint;
  571. pvreg,
  572. vmtreg : tregister;
  573. oldaktcallnode : tcallnode;
  574. retlocitem: pcgparalocation;
  575. {$ifdef vtentry}
  576. sym : tasmsymbol;
  577. {$endif vtentry}
  578. {$ifdef x86_64}
  579. cgpara : tcgpara;
  580. {$endif x86_64}
  581. begin
  582. if not assigned(procdefinition) or
  583. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  584. internalerror(200305264);
  585. if assigned(callinitblock) then
  586. secondpass(tnode(callinitblock));
  587. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  588. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  589. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  590. { Include Function result registers }
  591. if (not is_void(resultdef)) then
  592. begin
  593. { The forced returntype may have a different size than the one
  594. declared for the procdef }
  595. if not assigned(typedef) then
  596. retloc:=procdefinition.funcretloc[callerside]
  597. else
  598. retloc:=paramanager.get_funcretloc(procdefinition,callerside,typedef);
  599. retlocitem:=retloc.location;
  600. while assigned(retlocitem) do
  601. begin
  602. case retlocitem^.loc of
  603. LOC_REGISTER:
  604. include(regs_to_save_int,getsupreg(retlocitem^.register));
  605. LOC_FPUREGISTER:
  606. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  607. LOC_MMREGISTER:
  608. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  609. LOC_REFERENCE,
  610. LOC_VOID:
  611. ;
  612. else
  613. internalerror(2004110213);
  614. end;
  615. retlocitem:=retlocitem^.next;
  616. end;
  617. end;
  618. { Process parameters, register parameters will be loaded
  619. in imaginary registers. The actual load to the correct
  620. register is done just before the call }
  621. oldaktcallnode:=aktcallnode;
  622. aktcallnode:=self;
  623. if assigned(left) then
  624. tcallparanode(left).secondcallparan;
  625. aktcallnode:=oldaktcallnode;
  626. { procedure variable or normal function call ? }
  627. if (right=nil) then
  628. begin
  629. { register call for WPO (must be done before wpo test below,
  630. otherwise optimised called methods are no longer registered)
  631. }
  632. if (po_virtualmethod in procdefinition.procoptions) and
  633. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  634. assigned(methodpointer) and
  635. (methodpointer.nodetype<>typen) and
  636. (not assigned(current_procinfo) or
  637. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  638. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  639. {$ifdef vtentry}
  640. if not is_interface(tprocdef(procdefinition)._class) then
  641. begin
  642. inc(current_asmdata.NextVTEntryNr);
  643. 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));
  644. end;
  645. {$endif vtentry}
  646. name_to_call:='';
  647. if assigned(fobjcforcedprocname) then
  648. name_to_call:=fobjcforcedprocname^;
  649. { When methodpointer is typen we don't need (and can't) load
  650. a pointer. We can directly call the correct procdef (PFV) }
  651. if (name_to_call='') and
  652. (po_virtualmethod in procdefinition.procoptions) and
  653. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  654. assigned(methodpointer) and
  655. (methodpointer.nodetype<>typen) and
  656. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  657. begin
  658. { virtual methods require an index }
  659. if tprocdef(procdefinition).extnumber=$ffff then
  660. internalerror(200304021);
  661. secondpass(methodpointer);
  662. { Load VMT from self }
  663. if methodpointer.resultdef.typ=objectdef then
  664. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resultdef),methodpointer.location,vmtreg)
  665. else
  666. begin
  667. { Load VMT value in register }
  668. location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,OS_ADDR,false);
  669. vmtreg:=methodpointer.location.register;
  670. end;
  671. { test validity of VMT }
  672. if not(is_interface(tprocdef(procdefinition).struct)) and
  673. not(is_cppclass(tprocdef(procdefinition).struct)) then
  674. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tobjectdef(tprocdef(procdefinition).struct));
  675. { Call through VMT, generate a VTREF symbol to notify the linker }
  676. vmtoffset:=tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber);
  677. { register call for WPO }
  678. if (not assigned(current_procinfo) or
  679. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  680. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  681. {$ifndef x86}
  682. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  683. {$endif not x86}
  684. reference_reset_base(href,vmtreg,vmtoffset,sizeof(pint));
  685. {$ifndef x86}
  686. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  687. {$endif not x86}
  688. { Load parameters that are in temporary registers in the
  689. correct parameter register }
  690. if assigned(left) then
  691. begin
  692. pushparas;
  693. { free the resources allocated for the parameters }
  694. freeparas;
  695. end;
  696. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  697. if cg.uses_registers(R_FPUREGISTER) then
  698. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  699. if cg.uses_registers(R_MMREGISTER) then
  700. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  701. { call method }
  702. extra_call_code;
  703. {$ifdef x86}
  704. cg.a_call_ref(current_asmdata.CurrAsmList,href);
  705. {$else x86}
  706. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  707. {$endif x86}
  708. extra_post_call_code;
  709. end
  710. else
  711. begin
  712. { Load parameters that are in temporary registers in the
  713. correct parameter register }
  714. if assigned(left) then
  715. begin
  716. pushparas;
  717. { free the resources allocated for the parameters }
  718. freeparas;
  719. end;
  720. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  721. if cg.uses_registers(R_FPUREGISTER) then
  722. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  723. if cg.uses_registers(R_MMREGISTER) then
  724. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  725. if procdefinition.proccalloption=pocall_syscall then
  726. do_syscall
  727. else
  728. begin
  729. { Calling interrupt from the same code requires some
  730. extra code }
  731. if (po_interrupt in procdefinition.procoptions) then
  732. extra_interrupt_code;
  733. extra_call_code;
  734. if (name_to_call='') then
  735. cg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition).mangledname,po_weakexternal in procdefinition.procoptions)
  736. else
  737. cg.a_call_name(current_asmdata.CurrAsmList,name_to_call,po_weakexternal in procdefinition.procoptions);
  738. extra_post_call_code;
  739. end;
  740. end;
  741. end
  742. else
  743. { now procedure variable case }
  744. begin
  745. secondpass(right);
  746. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  747. { Only load OS_ADDR from the reference }
  748. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  749. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  750. else
  751. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,right.location,pvreg);
  752. location_freetemp(current_asmdata.CurrAsmList,right.location);
  753. { Load parameters that are in temporary registers in the
  754. correct parameter register }
  755. if assigned(left) then
  756. begin
  757. pushparas;
  758. { free the resources allocated for the parameters }
  759. freeparas;
  760. end;
  761. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  762. if cg.uses_registers(R_FPUREGISTER) then
  763. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  764. if cg.uses_registers(R_MMREGISTER) then
  765. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  766. { Calling interrupt from the same code requires some
  767. extra code }
  768. if (po_interrupt in procdefinition.procoptions) then
  769. extra_interrupt_code;
  770. extra_call_code;
  771. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  772. extra_post_call_code;
  773. end;
  774. { Need to remove the parameters from the stack? }
  775. if (procdefinition.proccalloption in clearstack_pocalls) then
  776. begin
  777. pop_size:=pushedparasize;
  778. { for Cdecl functions we don't need to pop the funcret when it
  779. was pushed by para. Except for safecall functions with
  780. safecall-exceptions enabled. In that case the funcret is always
  781. returned as a para which is considered a normal para on the
  782. c-side, so the funcret has to be pop'ed normally. }
  783. if not ((procdefinition.proccalloption=pocall_safecall) and
  784. (tf_safecall_exceptions in target_info.flags)) and
  785. paramanager.ret_in_param(procdefinition.returndef,procdefinition.proccalloption) then
  786. dec(pop_size,sizeof(pint));
  787. { Remove parameters/alignment from the stack }
  788. pop_parasize(pop_size);
  789. end
  790. { frame pointer parameter is popped by the caller when it's passed the
  791. Delphi way }
  792. else if (po_delphi_nested_cc in procdefinition.procoptions) and
  793. not paramanager.use_fixed_stack then
  794. pop_parasize(sizeof(pint));
  795. { Release registers, but not the registers that contain the
  796. function result }
  797. if (not is_void(resultdef)) then
  798. begin
  799. retlocitem:=retloc.location;
  800. while assigned(retlocitem) do
  801. begin
  802. case retlocitem^.loc of
  803. LOC_REGISTER:
  804. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  805. LOC_FPUREGISTER:
  806. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  807. LOC_MMREGISTER:
  808. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  809. LOC_REFERENCE,
  810. LOC_VOID:
  811. ;
  812. else
  813. internalerror(2004110214);
  814. end;
  815. retlocitem:=retlocitem^.next;
  816. end;
  817. end;
  818. {$if defined(x86) or defined(arm)}
  819. if (procdefinition.proccalloption=pocall_safecall) and
  820. (tf_safecall_exceptions in target_info.flags) then
  821. begin
  822. {$ifdef x86_64}
  823. cgpara.init;
  824. paramanager.getintparaloc(pocall_default,1,cgpara);
  825. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_ADDR,NR_RAX,cgpara);
  826. cgpara.done;
  827. {$endif x86_64}
  828. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  829. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK',false);
  830. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  831. end;
  832. {$endif}
  833. if cg.uses_registers(R_MMREGISTER) then
  834. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  835. if cg.uses_registers(R_FPUREGISTER) then
  836. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  837. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  838. { handle function results }
  839. if (not is_void(resultdef)) then
  840. handle_return_value
  841. else
  842. location_reset(location,LOC_VOID,OS_NO);
  843. { convert persistent temps for parameters and function result to normal temps }
  844. if assigned(callcleanupblock) then
  845. secondpass(tnode(callcleanupblock));
  846. { release temps and finalize unused return values, must be
  847. after the callcleanupblock because that converts temps
  848. from persistent to normal }
  849. release_unused_return_value;
  850. { release temps of paras }
  851. release_para_temps;
  852. { perhaps i/o check ? }
  853. if (cs_check_io in current_settings.localswitches) and
  854. (po_iocheck in procdefinition.procoptions) and
  855. not(po_iocheck in current_procinfo.procdef.procoptions) and
  856. { no IO check for methods and procedure variables }
  857. (right=nil) and
  858. not(po_virtualmethod in procdefinition.procoptions) then
  859. begin
  860. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  861. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK',false);
  862. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  863. end;
  864. end;
  865. destructor tcgcallnode.destroy;
  866. begin
  867. if assigned(typedef) then
  868. retloc.done;
  869. inherited destroy;
  870. end;
  871. begin
  872. ccallparanode:=tcgcallparanode;
  873. ccallnode:=tcgcallnode;
  874. end.