ncgcal.pas 41 KB

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