ncgcal.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. protected
  28. tempcgpara : tcgpara;
  29. procedure push_addr_para;
  30. procedure push_value_para;virtual;
  31. procedure push_formal_para;virtual;
  32. procedure push_copyout_para;virtual;abstract;
  33. public
  34. constructor create(expr,next : tnode);override;
  35. destructor destroy;override;
  36. procedure secondcallparan;override;
  37. end;
  38. { tcgcallnode }
  39. tcgcallnode = class(tcallnode)
  40. private
  41. procedure handle_return_value;
  42. procedure release_unused_return_value;
  43. procedure copy_back_paras;
  44. procedure release_para_temps;
  45. procedure pushparas;
  46. procedure freeparas;
  47. protected
  48. retloc: tcgpara;
  49. framepointer_paraloc : tcgpara;
  50. {# This routine is used to push the current frame pointer
  51. on the stack. This is used in nested routines where the
  52. value of the frame pointer is always pushed as an extra
  53. parameter.
  54. The default handling is the standard handling used on
  55. most stack based machines, where the frame pointer is
  56. the first invisible parameter.
  57. }
  58. procedure pop_parasize(pop_size:longint);virtual;
  59. procedure extra_interrupt_code;virtual;
  60. procedure extra_pre_call_code;virtual;
  61. procedure extra_call_code;virtual;
  62. procedure extra_post_call_code;virtual;
  63. procedure do_syscall;virtual;abstract;
  64. { The function result is returned in a tcgpara. This tcgpara has to
  65. be translated into a tlocation so the rest of the code generator
  66. can work with it. This routine decides what the most appropriate
  67. tlocation is and sets self.location based on that. }
  68. procedure set_result_location(realresdef: tstoreddef);virtual;
  69. { if an unused return value is in another location than a
  70. LOC_REFERENCE, this method will be called to perform the necessary
  71. cleanups. By default it does not do anything }
  72. procedure do_release_unused_return_value;virtual;
  73. public
  74. procedure pass_generate_code;override;
  75. destructor destroy;override;
  76. end;
  77. implementation
  78. uses
  79. systems,
  80. cutils,verbose,globals,
  81. cpuinfo,
  82. symconst,symtable,symtype,defutil,paramgr,
  83. cgbase,pass_2,
  84. aasmbase,aasmtai,aasmdata,
  85. nbas,nmem,nld,ncnv,nutils,
  86. {$ifdef x86}
  87. cga,cgx86,aasmcpu,
  88. {$endif x86}
  89. ncgutil,
  90. cgobj,tgobj,hlcgobj,
  91. procinfo,
  92. wpobase;
  93. {*****************************************************************************
  94. TCGCALLPARANODE
  95. *****************************************************************************}
  96. constructor tcgcallparanode.create(expr,next : tnode);
  97. begin
  98. inherited create(expr,next);
  99. tempcgpara.init;
  100. end;
  101. destructor tcgcallparanode.destroy;
  102. begin
  103. tempcgpara.done;
  104. inherited destroy;
  105. end;
  106. procedure tcgcallparanode.push_addr_para;
  107. begin
  108. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  109. internalerror(200304235);
  110. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location.reference,tempcgpara);
  111. end;
  112. procedure tcgcallparanode.push_value_para;
  113. begin
  114. { we've nothing to push when the size of the parameter is 0
  115. -- except in case of the self parameter of an emptry record on e.g.
  116. the JVM target }
  117. if (left.resultdef.size=0) and
  118. not(vo_is_self in parasym.varoptions) then
  119. exit;
  120. { Move flags and jump in register to make it less complex }
  121. if left.location.loc in [LOC_FLAGS,LOC_JUMP,LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF] then
  122. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  123. { load the parameter's tlocation into its cgpara }
  124. hlcg.gen_load_loc_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location,tempcgpara)
  125. end;
  126. procedure tcgcallparanode.push_formal_para;
  127. begin
  128. { allow passing of a constant to a const formaldef }
  129. if (parasym.varspez=vs_const) and
  130. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  131. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  132. push_addr_para;
  133. end;
  134. procedure tcgcallparanode.secondcallparan;
  135. var
  136. href : treference;
  137. otlabel,
  138. oflabel : tasmlabel;
  139. begin
  140. if not(assigned(parasym)) then
  141. internalerror(200304242);
  142. { Skip nothingn nodes which are used after disabling
  143. a parameter }
  144. if (left.nodetype<>nothingn) then
  145. begin
  146. otlabel:=current_procinfo.CurrTrueLabel;
  147. oflabel:=current_procinfo.CurrFalseLabel;
  148. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  149. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  150. if assigned(fparainit) then
  151. secondpass(fparainit);
  152. secondpass(left);
  153. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  154. { release memory for refcnt out parameters }
  155. if (parasym.varspez=vs_out) and
  156. is_managed_type(left.resultdef) and
  157. not(target_info.system in systems_garbage_collected_managed_types) then
  158. begin
  159. hlcg.location_get_data_ref(current_asmdata.CurrAsmList,left.resultdef,left.location,href,false,sizeof(pint));
  160. if is_open_array(resultdef) then
  161. begin
  162. { if elementdef is not managed, omit fpc_decref_array
  163. because it won't do anything anyway }
  164. if is_managed_type(tarraydef(resultdef).elementdef) then
  165. begin
  166. if third=nil then
  167. InternalError(201103063);
  168. secondpass(third);
  169. hlcg.g_array_rtti_helper(current_asmdata.CurrAsmList,tarraydef(resultdef).elementdef,
  170. href,third.location,'fpc_finalize_array');
  171. end;
  172. end
  173. else
  174. hlcg.g_finalize(current_asmdata.CurrAsmList,left.resultdef,href)
  175. end;
  176. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,not followed_by_stack_tainting_call_cached,tempcgpara);
  177. { handle varargs first, because parasym is not valid }
  178. if (cpf_varargs_para in callparaflags) then
  179. begin
  180. if paramanager.push_addr_param(vs_value,left.resultdef,
  181. aktcallnode.procdefinition.proccalloption) then
  182. push_addr_para
  183. else
  184. push_value_para;
  185. end
  186. { hidden parameters }
  187. else if (vo_is_hidden_para in parasym.varoptions) then
  188. begin
  189. { don't push a node that already generated a pointer type
  190. by address for implicit hidden parameters }
  191. if (vo_is_funcret in parasym.varoptions) or
  192. { pass "this" in C++ classes explicitly as pointer
  193. because push_addr_param might not be true for them }
  194. (is_cppclass(parasym.vardef) and (vo_is_self in parasym.varoptions)) or
  195. (
  196. (
  197. not(left.resultdef.typ in [pointerdef,classrefdef]) or
  198. (
  199. { allow pointerdefs (as self) to be passed as addr
  200. param if the method is part of a type helper which
  201. extends a pointer type }
  202. (vo_is_self in parasym.varoptions) and
  203. (aktcallnode.procdefinition.owner.symtabletype=objectsymtable) and
  204. (is_objectpascal_helper(tdef(aktcallnode.procdefinition.owner.defowner))) and
  205. (tobjectdef(aktcallnode.procdefinition.owner.defowner).extendeddef.typ=pointerdef)
  206. )
  207. ) and
  208. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  209. aktcallnode.procdefinition.proccalloption)) then
  210. push_addr_para
  211. else
  212. push_value_para;
  213. end
  214. { formal def }
  215. else if (parasym.vardef.typ=formaldef) then
  216. push_formal_para
  217. { Normal parameter }
  218. else if paramanager.push_copyout_param(parasym.varspez,parasym.vardef,
  219. aktcallnode.procdefinition.proccalloption) then
  220. push_copyout_para
  221. else
  222. begin
  223. { don't push a node that already generated a pointer type
  224. by address for implicit hidden parameters }
  225. if (not(
  226. (vo_is_hidden_para in parasym.varoptions) and
  227. (left.resultdef.typ in [pointerdef,classrefdef])
  228. ) and
  229. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  230. aktcallnode.procdefinition.proccalloption)) and
  231. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  232. not(
  233. is_array_of_const(parasym.vardef) and
  234. is_dynamic_array(left.resultdef)
  235. ) then
  236. begin
  237. { Passing a var parameter to a var parameter, we can
  238. just push the address transparently }
  239. if (left.nodetype=loadn) and
  240. (tloadnode(left).is_addr_param_load) then
  241. begin
  242. if (left.location.reference.index<>NR_NO) or
  243. (left.location.reference.offset<>0) then
  244. internalerror(200410107);
  245. hlcg.a_load_reg_cgpara(current_asmdata.CurrAsmList,voidpointertype,left.location.reference.base,tempcgpara)
  246. end
  247. else
  248. begin
  249. { Force to be in memory }
  250. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  251. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  252. push_addr_para;
  253. end;
  254. end
  255. else
  256. push_value_para;
  257. end;
  258. current_procinfo.CurrTrueLabel:=otlabel;
  259. current_procinfo.CurrFalseLabel:=oflabel;
  260. { update return location in callnode when this is the function
  261. result }
  262. if assigned(parasym) and
  263. (
  264. { for type helper/record constructor check that it is self parameter }
  265. (
  266. (vo_is_self in parasym.varoptions) and
  267. (aktcallnode.procdefinition.proctypeoption=potype_constructor) and
  268. (parasym.vardef.typ<>objectdef)
  269. ) or
  270. (vo_is_funcret in parasym.varoptions)
  271. ) then
  272. location_copy(aktcallnode.location,left.location);
  273. end;
  274. { next parameter }
  275. if assigned(right) then
  276. tcallparanode(right).secondcallparan;
  277. end;
  278. {*****************************************************************************
  279. TCGCALLNODE
  280. *****************************************************************************}
  281. {$if first_mm_imreg = 0}
  282. {$WARN 4044 OFF} { Comparison might be always false ... }
  283. {$endif}
  284. procedure tcgcallnode.extra_interrupt_code;
  285. begin
  286. end;
  287. procedure tcgcallnode.extra_pre_call_code;
  288. begin
  289. end;
  290. procedure tcgcallnode.extra_call_code;
  291. begin
  292. end;
  293. procedure tcgcallnode.extra_post_call_code;
  294. begin
  295. end;
  296. procedure tcgcallnode.set_result_location(realresdef: tstoreddef);
  297. begin
  298. if realresdef.is_intregable or
  299. realresdef.is_fpuregable or
  300. { avoid temporarily storing pointer-sized entities that can't be
  301. regvars, such as reference-counted pointers, to memory --
  302. no exception can occur right now (except in case of existing
  303. memory corruption), and we'd store them to a regular temp
  304. anyway and that is not safer than keeping them in a register }
  305. ((realresdef.size=sizeof(aint)) and
  306. (retloc.location^.loc=LOC_REGISTER) and
  307. not assigned(retloc.location^.next)) then
  308. location_allocate_register(current_asmdata.CurrAsmList,location,realresdef,false)
  309. else
  310. begin
  311. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),0);
  312. tg.gethltemp(current_asmdata.CurrAsmList,realresdef,retloc.intsize,tt_normal,location.reference);
  313. end;
  314. end;
  315. procedure tcgcallnode.do_release_unused_return_value;
  316. begin
  317. case location.loc of
  318. LOC_REFERENCE :
  319. begin
  320. if is_managed_type(resultdef) then
  321. hlcg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  322. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  323. end;
  324. end;
  325. end;
  326. procedure tcgcallnode.pop_parasize(pop_size:longint);
  327. begin
  328. end;
  329. procedure tcgcallnode.handle_return_value;
  330. var
  331. realresdef: tstoreddef;
  332. begin
  333. { Check that the return location is set when the result is passed in
  334. a parameter }
  335. if paramanager.ret_in_param(resultdef,procdefinition) then
  336. begin
  337. { self.location is set near the end of secondcallparan so it
  338. refers to the implicit result parameter }
  339. if location.loc<>LOC_REFERENCE then
  340. internalerror(200304241);
  341. exit;
  342. end;
  343. if not assigned(typedef) then
  344. realresdef:=tstoreddef(resultdef)
  345. else
  346. realresdef:=tstoreddef(typedef);
  347. {$ifdef x86}
  348. if (retloc.location^.loc=LOC_FPUREGISTER) then
  349. begin
  350. tcgx86(cg).inc_fpu_stack;
  351. location_reset(location,LOC_FPUREGISTER,retloc.location^.size);
  352. location.register:=retloc.location^.register;
  353. end
  354. else
  355. {$endif x86}
  356. begin
  357. { get a tlocation that can hold the return value that's currently in
  358. the the return value's tcgpara }
  359. set_result_location(realresdef);
  360. { Do not move the physical register to a virtual one in case
  361. the return value is not used, because if the virtual one is
  362. then mapped to the same register as the physical one, we will
  363. end up with two deallocs of this register (one inserted here,
  364. one inserted by the register allocator), which unbalances the
  365. register allocation information. The return register(s) will
  366. be freed by location_free() in release_unused_return_value
  367. (mantis #13536). }
  368. if (cnf_return_value_used in callnodeflags) or
  369. assigned(funcretnode) then
  370. begin
  371. hlcg.gen_load_cgpara_loc(current_asmdata.CurrAsmList,realresdef,retloc,location,false);
  372. {$ifdef arm}
  373. if (resultdef.typ=floatdef) and
  374. (location.loc=LOC_REGISTER) and
  375. (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) then
  376. begin
  377. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,resultdef);
  378. end;
  379. {$endif arm}
  380. end;
  381. end;
  382. { copy value to the final location if this was already provided to the
  383. callnode. This must be done after the call node, because the location can
  384. also be used as parameter and may not be finalized yet }
  385. if assigned(funcretnode) then
  386. begin
  387. funcretnode.pass_generate_code;
  388. { Decrease refcount for refcounted types, this can be skipped when
  389. we have used a temp, because then it is already done from tempcreatenode.
  390. Also no finalize is needed, because there is no risk of exceptions from the
  391. function since this is code is only executed after the function call has returned }
  392. if is_managed_type(funcretnode.resultdef) and
  393. (funcretnode.nodetype<>temprefn) then
  394. hlcg.g_finalize(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  395. case location.loc of
  396. LOC_REGISTER :
  397. begin
  398. {$ifndef cpu64bitalu}
  399. if location.size in [OS_64,OS_S64] then
  400. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  401. else
  402. {$endif}
  403. hlcg.a_load_reg_loc(current_asmdata.CurrAsmList,resultdef,resultdef,location.register,funcretnode.location);
  404. location_free(current_asmdata.CurrAsmList,location);
  405. end;
  406. LOC_REFERENCE:
  407. begin
  408. case funcretnode.location.loc of
  409. LOC_REGISTER:
  410. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,resultdef,resultdef,location.reference,funcretnode.location.register);
  411. LOC_REFERENCE:
  412. hlcg.g_concatcopy(current_asmdata.CurrAsmList,resultdef,location.reference,funcretnode.location.reference);
  413. else
  414. internalerror(200802121);
  415. end;
  416. location_freetemp(current_asmdata.CurrAsmList,location);
  417. end;
  418. else
  419. internalerror(200709085);
  420. end;
  421. location := funcretnode.location;
  422. end;
  423. end;
  424. procedure tcgcallnode.release_unused_return_value;
  425. begin
  426. { When the result is not used we need to finalize the result and
  427. can release the temp. This need to be after the callcleanupblock
  428. tree is generated, because that converts the temp from persistent to normal }
  429. if not(cnf_return_value_used in callnodeflags) then
  430. begin
  431. do_release_unused_return_value;
  432. if (retloc.intsize<>0) then
  433. paramanager.freecgpara(current_asmdata.CurrAsmList,retloc);
  434. location_reset(location,LOC_VOID,OS_NO);
  435. end;
  436. end;
  437. procedure tcgcallnode.copy_back_paras;
  438. var
  439. ppn : tcallparanode;
  440. begin
  441. ppn:=tcallparanode(left);
  442. while assigned(ppn) do
  443. begin
  444. if assigned(ppn.paracopyback) then
  445. secondpass(ppn.paracopyback);
  446. ppn:=tcallparanode(ppn.right);
  447. end;
  448. end;
  449. procedure tcgcallnode.release_para_temps;
  450. var
  451. hp,
  452. hp2 : tnode;
  453. ppn : tcallparanode;
  454. begin
  455. { Release temps from parameters }
  456. ppn:=tcallparanode(left);
  457. while assigned(ppn) do
  458. begin
  459. if assigned(ppn.left) then
  460. begin
  461. { don't release the funcret temp }
  462. if not(assigned(ppn.parasym)) or
  463. not(vo_is_funcret in ppn.parasym.varoptions) then
  464. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  465. { process also all nodes of an array of const }
  466. hp:=ppn.left;
  467. while (hp.nodetype=typeconvn) do
  468. hp:=ttypeconvnode(hp).left;
  469. if (hp.nodetype=arrayconstructorn) and
  470. assigned(tarrayconstructornode(hp).left) then
  471. begin
  472. while assigned(hp) do
  473. begin
  474. hp2:=tarrayconstructornode(hp).left;
  475. { ignore typeconvs and addrn inserted by arrayconstructn for
  476. passing a shortstring }
  477. if (hp2.nodetype=typeconvn) and
  478. (tunarynode(hp2).left.nodetype=addrn) then
  479. hp2:=tunarynode(tunarynode(hp2).left).left
  480. else if hp2.nodetype=addrn then
  481. hp2:=tunarynode(hp2).left;
  482. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  483. hp:=tarrayconstructornode(hp).right;
  484. end;
  485. end;
  486. end;
  487. ppn:=tcallparanode(ppn.right);
  488. end;
  489. end;
  490. procedure tcgcallnode.pushparas;
  491. var
  492. ppn : tcgcallparanode;
  493. callerparaloc,
  494. tmpparaloc : pcgparalocation;
  495. sizeleft: aint;
  496. htempref,
  497. href : treference;
  498. calleralignment,
  499. tmpalignment: longint;
  500. skipiffinalloc: boolean;
  501. begin
  502. { copy all resources to the allocated registers }
  503. ppn:=tcgcallparanode(left);
  504. while assigned(ppn) do
  505. begin
  506. if (ppn.left.nodetype<>nothingn) then
  507. begin
  508. { better check for the real location of the parameter here, when stack passed parameters
  509. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  510. }
  511. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  512. tmpparaloc:=ppn.tempcgpara.location;
  513. sizeleft:=ppn.tempcgpara.intsize;
  514. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  515. tmpalignment:=ppn.tempcgpara.alignment;
  516. if (tmpalignment=0) or
  517. (calleralignment=0) then
  518. internalerror(2009020701);
  519. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  520. skipiffinalloc:=
  521. not paramanager.use_fixed_stack or
  522. not(ppn.followed_by_stack_tainting_call_cached);
  523. while assigned(callerparaloc) do
  524. begin
  525. { Every paraloc must have a matching tmpparaloc }
  526. if not assigned(tmpparaloc) then
  527. internalerror(200408224);
  528. if callerparaloc^.size<>tmpparaloc^.size then
  529. internalerror(200408225);
  530. case callerparaloc^.loc of
  531. LOC_REGISTER:
  532. begin
  533. if tmpparaloc^.loc<>LOC_REGISTER then
  534. internalerror(200408221);
  535. if getsupreg(callerparaloc^.register)<first_int_imreg then
  536. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  537. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  538. tmpparaloc^.register,callerparaloc^.register);
  539. end;
  540. LOC_FPUREGISTER:
  541. begin
  542. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  543. internalerror(200408222);
  544. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  545. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  546. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  547. end;
  548. LOC_MMREGISTER:
  549. begin
  550. if tmpparaloc^.loc<>LOC_MMREGISTER then
  551. internalerror(200408223);
  552. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  553. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  554. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  555. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  556. end;
  557. LOC_REFERENCE:
  558. begin
  559. if not(skipiffinalloc and
  560. paramanager.is_stack_paraloc(callerparaloc)) then
  561. begin
  562. { Can't have a data copied to the stack, every location
  563. must contain a valid size field }
  564. if (tmpparaloc^.size=OS_NO) and
  565. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  566. assigned(tmpparaloc^.next)) then
  567. internalerror(200501281);
  568. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  569. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  570. case tmpparaloc^.loc of
  571. LOC_REFERENCE:
  572. begin
  573. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  574. { use concatcopy, because it can also be a float which fails when
  575. load_ref_ref is used }
  576. if (ppn.tempcgpara.size <> OS_NO) then
  577. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  578. else
  579. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  580. end;
  581. LOC_REGISTER:
  582. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  583. LOC_FPUREGISTER:
  584. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  585. LOC_MMREGISTER:
  586. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  587. else
  588. internalerror(200402081);
  589. end;
  590. end;
  591. end;
  592. end;
  593. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  594. callerparaloc:=callerparaloc^.next;
  595. tmpparaloc:=tmpparaloc^.next;
  596. end;
  597. end;
  598. ppn:=tcgcallparanode(ppn.right);
  599. end;
  600. end;
  601. procedure tcgcallnode.freeparas;
  602. var
  603. ppn : tcgcallparanode;
  604. begin
  605. { free the resources allocated for the parameters }
  606. ppn:=tcgcallparanode(left);
  607. while assigned(ppn) do
  608. begin
  609. if (ppn.left.nodetype<>nothingn) then
  610. begin
  611. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  612. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  613. end;
  614. ppn:=tcgcallparanode(ppn.right);
  615. end;
  616. end;
  617. procedure tcgcallnode.pass_generate_code;
  618. var
  619. name_to_call: shortstring;
  620. regs_to_save_int,
  621. regs_to_save_fpu,
  622. regs_to_save_mm : Tcpuregisterset;
  623. href : treference;
  624. pop_size : longint;
  625. vmtoffset : aint;
  626. pvreg,
  627. vmtreg : tregister;
  628. oldaktcallnode : tcallnode;
  629. retlocitem: pcgparalocation;
  630. pd : tprocdef;
  631. {$ifdef vtentry}
  632. sym : tasmsymbol;
  633. {$endif vtentry}
  634. {$if defined(x86) or defined(arm) or defined(sparc)}
  635. cgpara : tcgpara;
  636. {$endif}
  637. begin
  638. if not assigned(procdefinition) or
  639. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  640. internalerror(200305264);
  641. extra_pre_call_code;
  642. if assigned(callinitblock) then
  643. secondpass(tnode(callinitblock));
  644. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  645. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  646. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  647. { Include Function result registers }
  648. if (not is_void(resultdef)) then
  649. begin
  650. { The forced returntype may have a different size than the one
  651. declared for the procdef }
  652. if not assigned(typedef) then
  653. retloc:=procdefinition.funcretloc[callerside]
  654. else
  655. retloc:=paramanager.get_funcretloc(procdefinition,callerside,typedef);
  656. retlocitem:=retloc.location;
  657. while assigned(retlocitem) do
  658. begin
  659. case retlocitem^.loc of
  660. LOC_REGISTER:
  661. include(regs_to_save_int,getsupreg(retlocitem^.register));
  662. LOC_FPUREGISTER:
  663. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  664. LOC_MMREGISTER:
  665. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  666. LOC_REFERENCE,
  667. LOC_VOID:
  668. ;
  669. else
  670. internalerror(2004110213);
  671. end;
  672. retlocitem:=retlocitem^.next;
  673. end;
  674. end;
  675. { Process parameters, register parameters will be loaded
  676. in imaginary registers. The actual load to the correct
  677. register is done just before the call }
  678. oldaktcallnode:=aktcallnode;
  679. aktcallnode:=self;
  680. if assigned(left) then
  681. tcallparanode(left).secondcallparan;
  682. aktcallnode:=oldaktcallnode;
  683. { procedure variable or normal function call ? }
  684. if (right=nil) then
  685. begin
  686. { register call for WPO (must be done before wpo test below,
  687. otherwise optimised called methods are no longer registered)
  688. }
  689. if (po_virtualmethod in procdefinition.procoptions) and
  690. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  691. assigned(methodpointer) and
  692. (methodpointer.nodetype<>typen) and
  693. (not assigned(current_procinfo) or
  694. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  695. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  696. {$ifdef vtentry}
  697. if not is_interface(tprocdef(procdefinition)._class) then
  698. begin
  699. inc(current_asmdata.NextVTEntryNr);
  700. 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));
  701. end;
  702. {$endif vtentry}
  703. name_to_call:='';
  704. if assigned(fobjcforcedprocname) then
  705. name_to_call:=fobjcforcedprocname^;
  706. { in the JVM, virtual method calls are also name-based }
  707. {$ifndef jvm}
  708. { When methodpointer is typen we don't need (and can't) load
  709. a pointer. We can directly call the correct procdef (PFV) }
  710. if (name_to_call='') and
  711. (po_virtualmethod in procdefinition.procoptions) and
  712. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  713. assigned(methodpointer) and
  714. (methodpointer.nodetype<>typen) and
  715. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  716. begin
  717. { virtual methods require an index }
  718. if tprocdef(procdefinition).extnumber=$ffff then
  719. internalerror(200304021);
  720. secondpass(methodpointer);
  721. { Load VMT from self }
  722. if methodpointer.resultdef.typ=objectdef then
  723. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resultdef),methodpointer.location,vmtreg)
  724. else
  725. begin
  726. { Load VMT value in register }
  727. { todo: fix vmt type for high level cg }
  728. hlcg.location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,voidpointertype,voidpointertype,false);
  729. vmtreg:=methodpointer.location.register;
  730. end;
  731. { test validity of VMT }
  732. if not(is_interface(tprocdef(procdefinition).struct)) and
  733. not(is_cppclass(tprocdef(procdefinition).struct)) then
  734. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tobjectdef(tprocdef(procdefinition).struct));
  735. { Call through VMT, generate a VTREF symbol to notify the linker }
  736. vmtoffset:=tobjectdef(tprocdef(procdefinition).struct).vmtmethodoffset(tprocdef(procdefinition).extnumber);
  737. { register call for WPO }
  738. if (not assigned(current_procinfo) or
  739. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  740. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  741. {$ifndef x86}
  742. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  743. {$endif not x86}
  744. reference_reset_base(href,vmtreg,vmtoffset,sizeof(pint));
  745. {$ifndef x86}
  746. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  747. {$endif not x86}
  748. { Load parameters that are in temporary registers in the
  749. correct parameter register }
  750. if assigned(left) then
  751. begin
  752. pushparas;
  753. { free the resources allocated for the parameters }
  754. freeparas;
  755. end;
  756. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  757. if cg.uses_registers(R_FPUREGISTER) then
  758. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  759. if cg.uses_registers(R_MMREGISTER) then
  760. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  761. { call method }
  762. extra_call_code;
  763. {$ifdef x86}
  764. hlcg.a_call_ref(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),href);
  765. {$else x86}
  766. hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg);
  767. {$endif x86}
  768. extra_post_call_code;
  769. end
  770. else
  771. {$endif jvm}
  772. begin
  773. { Load parameters that are in temporary registers in the
  774. correct parameter register }
  775. if assigned(left) then
  776. begin
  777. pushparas;
  778. { free the resources allocated for the parameters }
  779. freeparas;
  780. end;
  781. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  782. if cg.uses_registers(R_FPUREGISTER) then
  783. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  784. if cg.uses_registers(R_MMREGISTER) then
  785. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  786. if procdefinition.proccalloption=pocall_syscall then
  787. do_syscall
  788. else
  789. begin
  790. { Calling interrupt from the same code requires some
  791. extra code }
  792. if (po_interrupt in procdefinition.procoptions) then
  793. extra_interrupt_code;
  794. extra_call_code;
  795. if (name_to_call='') then
  796. if cnf_inherited in callnodeflags then
  797. hlcg.a_call_name_inherited(current_asmdata.CurrAsmList,tprocdef(procdefinition),tprocdef(procdefinition).mangledname)
  798. else
  799. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),tprocdef(procdefinition).mangledname,typedef,po_weakexternal in procdefinition.procoptions).resetiftemp
  800. else
  801. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition),name_to_call,typedef,po_weakexternal in procdefinition.procoptions).resetiftemp;
  802. extra_post_call_code;
  803. end;
  804. end;
  805. end
  806. else
  807. { now procedure variable case }
  808. begin
  809. secondpass(right);
  810. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  811. { Only load OS_ADDR from the reference (when converting to hlcg:
  812. watch out with procedure of object) }
  813. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  814. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  815. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  816. begin
  817. { in case left is a method pointer and we are on a big endian target, then
  818. the method address is stored in registerhi }
  819. if (target_info.endian=endian_big) and (right.location.size in [OS_PAIR,OS_SPAIR]) then
  820. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location.registerhi,pvreg)
  821. else
  822. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location.register,pvreg);
  823. end
  824. else
  825. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,voidpointertype,voidpointertype,right.location,pvreg);
  826. location_freetemp(current_asmdata.CurrAsmList,right.location);
  827. { Load parameters that are in temporary registers in the
  828. correct parameter register }
  829. if assigned(left) then
  830. begin
  831. pushparas;
  832. { free the resources allocated for the parameters }
  833. freeparas;
  834. end;
  835. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  836. if cg.uses_registers(R_FPUREGISTER) then
  837. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  838. if cg.uses_registers(R_MMREGISTER) then
  839. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  840. { Calling interrupt from the same code requires some
  841. extra code }
  842. if (po_interrupt in procdefinition.procoptions) then
  843. extra_interrupt_code;
  844. extra_call_code;
  845. hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg);
  846. extra_post_call_code;
  847. end;
  848. { Need to remove the parameters from the stack? }
  849. if (procdefinition.proccalloption in clearstack_pocalls) then
  850. begin
  851. pop_size:=pushedparasize;
  852. { for Cdecl functions we don't need to pop the funcret when it
  853. was pushed by para. Except for safecall functions with
  854. safecall-exceptions enabled. In that case the funcret is always
  855. returned as a para which is considered a normal para on the
  856. c-side, so the funcret has to be pop'ed normally. }
  857. if not ((procdefinition.proccalloption=pocall_safecall) and
  858. (tf_safecall_exceptions in target_info.flags)) and
  859. paramanager.ret_in_param(procdefinition.returndef,procdefinition) then
  860. dec(pop_size,sizeof(pint));
  861. { Remove parameters/alignment from the stack }
  862. pop_parasize(pop_size);
  863. end
  864. { frame pointer parameter is popped by the caller when it's passed the
  865. Delphi way }
  866. else if (po_delphi_nested_cc in procdefinition.procoptions) and
  867. not paramanager.use_fixed_stack then
  868. pop_parasize(sizeof(pint));
  869. { Release registers, but not the registers that contain the
  870. function result }
  871. if (not is_void(resultdef)) then
  872. begin
  873. retlocitem:=retloc.location;
  874. while assigned(retlocitem) do
  875. begin
  876. case retlocitem^.loc of
  877. LOC_REGISTER:
  878. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  879. LOC_FPUREGISTER:
  880. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  881. LOC_MMREGISTER:
  882. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  883. LOC_REFERENCE,
  884. LOC_VOID:
  885. ;
  886. else
  887. internalerror(2004110214);
  888. end;
  889. retlocitem:=retlocitem^.next;
  890. end;
  891. end;
  892. if cg.uses_registers(R_MMREGISTER) then
  893. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  894. if cg.uses_registers(R_FPUREGISTER) then
  895. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  896. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  897. {$ifdef SUPPORT_SAFECALL}
  898. if (procdefinition.proccalloption=pocall_safecall) and
  899. (tf_safecall_exceptions in target_info.flags) then
  900. begin
  901. pd:=search_system_proc('fpc_safecallcheck');
  902. cgpara.init;
  903. paramanager.getintparaloc(pd,1,cgpara);
  904. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_INT,NR_FUNCTION_RESULT_REG,cgpara);
  905. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  906. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK');
  907. cgpara.done;
  908. end;
  909. {$endif}
  910. { handle function results }
  911. if (not is_void(resultdef)) then
  912. handle_return_value
  913. else
  914. location_reset(location,LOC_VOID,OS_NO);
  915. { convert persistent temps for parameters and function result to normal temps }
  916. if assigned(callcleanupblock) then
  917. secondpass(tnode(callcleanupblock));
  918. { copy back copy-out parameters if any }
  919. copy_back_paras;
  920. { release temps and finalize unused return values, must be
  921. after the callcleanupblock because that converts temps
  922. from persistent to normal }
  923. release_unused_return_value;
  924. { release temps of paras }
  925. release_para_temps;
  926. { perhaps i/o check ? }
  927. if (cs_check_io in current_settings.localswitches) and
  928. (po_iocheck in procdefinition.procoptions) and
  929. not(po_iocheck in current_procinfo.procdef.procoptions) and
  930. { no IO check for methods and procedure variables }
  931. (right=nil) and
  932. not(po_virtualmethod in procdefinition.procoptions) then
  933. begin
  934. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  935. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK',false);
  936. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  937. end;
  938. end;
  939. destructor tcgcallnode.destroy;
  940. begin
  941. retloc.resetiftemp;
  942. inherited destroy;
  943. end;
  944. begin
  945. ccallparanode:=tcgcallparanode;
  946. ccallnode:=tcgcallnode;
  947. end.