ncgcal.pas 42 KB

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