ncgcal.pas 45 KB

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