ncgcal.pas 55 KB

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