ncgcal.pas 41 KB

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