ncgcal.pas 41 KB

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