ncgcal.pas 52 KB

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