ncgcal.pas 52 KB

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