ncgcal.pas 41 KB

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