ncgcal.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for call nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgcal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. globtype,
  23. parabase,cgutils,
  24. aasmdata,cgbase,
  25. symdef,node,ncal;
  26. type
  27. tcgcallparanode = class(tcallparanode)
  28. protected
  29. procedure push_addr_para;
  30. procedure push_value_para;virtual;
  31. procedure push_formal_para;virtual;
  32. procedure push_copyout_para;virtual;abstract;
  33. public
  34. tempcgpara : tcgpara;
  35. constructor create(expr,next : tnode);override;
  36. destructor destroy;override;
  37. procedure secondcallparan;override;
  38. end;
  39. { tcgcallnode }
  40. tcgcallnode = class(tcallnode)
  41. private
  42. procedure handle_return_value;
  43. procedure release_unused_return_value;
  44. procedure copy_back_paras;
  45. procedure release_para_temps;
  46. procedure reorder_parameters;
  47. procedure freeparas;
  48. protected
  49. retloc: tcgpara;
  50. paralocs: array of pcgpara;
  51. framepointer_paraloc : tcgpara;
  52. {# This routine is used to push the current frame pointer
  53. on the stack. This is used in nested routines where the
  54. value of the frame pointer is always pushed as an extra
  55. parameter.
  56. The default handling is the standard handling used on
  57. most stack based machines, where the frame pointer is
  58. the first invisible parameter.
  59. }
  60. procedure pop_parasize(pop_size:longint);virtual;
  61. procedure extra_interrupt_code;virtual;
  62. procedure extra_pre_call_code;virtual;
  63. procedure extra_call_code;virtual;
  64. procedure extra_post_call_code;virtual;
  65. procedure do_syscall;virtual;abstract;
  66. { The function result is returned in a tcgpara. This tcgpara has to
  67. be translated into a tlocation so the rest of the code generator
  68. can work with it. This routine decides what the most appropriate
  69. tlocation is and sets self.location based on that. }
  70. procedure set_result_location(realresdef: tstoreddef);virtual;
  71. { if an unused return value is in another location than a
  72. LOC_REFERENCE, this method will be called to perform the necessary
  73. cleanups. By default it does not do anything }
  74. procedure do_release_unused_return_value;virtual;
  75. { Override the following three methods to support calls to address in
  76. 'ref' without loading it into register (only x86 targets probably).
  77. If can_call_ref returns true, it should do required simplification
  78. on ref. }
  79. function can_call_ref(var ref: treference):boolean;virtual;
  80. procedure extra_call_ref_code(var ref: treference);virtual;
  81. function do_call_ref(ref: treference): tcgpara;virtual;
  82. { store all the parameters in the temporary paralocs in their final
  83. location, and create the paralocs array that will be passed to
  84. hlcg.a_call_* }
  85. procedure pushparas;virtual;
  86. { loads the code pointer of a complex procvar (one with a self/
  87. parentfp/... and a procedure address) into a register and returns it }
  88. procedure load_complex_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef); virtual;
  89. { loads the procvar code pointer into a register with type def }
  90. procedure load_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef);
  91. procedure load_block_invoke(toreg: tregister; out callprocdef: tabstractprocdef);
  92. function get_call_reg(list: TAsmList): tregister; virtual;
  93. procedure unget_call_reg(list: TAsmList; reg: tregister); virtual;
  94. public
  95. procedure pass_generate_code;override;
  96. destructor destroy;override;
  97. end;
  98. implementation
  99. uses
  100. systems,
  101. cutils,verbose,globals,
  102. cpuinfo,
  103. symconst,symbase,symtable,symtype,symsym,defutil,paramgr,
  104. pass_2,
  105. aasmbase,aasmtai,
  106. nbas,nmem,nld,ncnv,nutils,
  107. ncgutil,blockutl,
  108. cgobj,tgobj,hlcgobj,
  109. procinfo,
  110. wpobase;
  111. {*****************************************************************************
  112. TCGCALLPARANODE
  113. *****************************************************************************}
  114. constructor tcgcallparanode.create(expr,next : tnode);
  115. begin
  116. inherited create(expr,next);
  117. tempcgpara.init;
  118. end;
  119. destructor tcgcallparanode.destroy;
  120. begin
  121. tempcgpara.done;
  122. inherited destroy;
  123. end;
  124. procedure tcgcallparanode.push_addr_para;
  125. begin
  126. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  127. internalerror(200304235);
  128. hlcg.a_loadaddr_ref_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location.reference,tempcgpara);
  129. end;
  130. procedure tcgcallnode.reorder_parameters;
  131. var
  132. hpcurr,hpprev,hpnext,hpreversestart : tcgcallparanode;
  133. begin
  134. { All parameters are now in temporary locations. If we move them to
  135. their regular locations in the same order, then we get the
  136. following pattern for register parameters:
  137. mov para1, tempreg1
  138. mov para2, tempreg2
  139. mov para3, tempreg3
  140. mov tempreg1, parareg1
  141. mov tempreg2, parareg2
  142. mov tempreg3, parareg3
  143. The result is that all tempregs conflict with all pararegs.
  144. A better solution is to use:
  145. mov para1, tempreg1
  146. mov para2, tempreg2
  147. mov para3, tempreg3
  148. mov tempreg3, parareg3
  149. mov tempreg2, parareg2
  150. mov tempreg1, parareg1
  151. This way, tempreg2 can be the same as parareg1 etc.
  152. To achieve this, we invert the order of all LOC_XREGISTER
  153. paras (JM).
  154. }
  155. hpcurr:=tcgcallparanode(left);
  156. { assume all LOC_REFERENCE parameters come first
  157. (see tcallnode.order_parameters)
  158. }
  159. hpreversestart:=nil;
  160. while assigned(hpcurr) do
  161. begin
  162. if not(hpcurr.parasym.paraloc[callerside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  163. hpreversestart:=hpcurr;
  164. hpcurr:=tcgcallparanode(hpcurr.right);
  165. end;
  166. { since all register tempparalocs have basically a complexity of 1,
  167. (unless there are large stack offsets that require a temp register on
  168. some architectures, but that's minor), we don't have to care about
  169. the internal relative order of different register type parameters
  170. }
  171. hpprev:=nil;
  172. hpcurr:=tcgcallparanode(left);
  173. while (hpcurr<>hpreversestart) do
  174. begin
  175. hpnext:=tcgcallparanode(hpcurr.right);
  176. if not(hpcurr.parasym.paraloc[callerside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  177. begin
  178. { remove hpcurr from chain }
  179. if assigned(hpprev) then
  180. hpprev.right:=hpnext
  181. else
  182. left:=hpnext;
  183. { insert right after hpreversestart, so every element will
  184. be inserted right before the previously moved one ->
  185. reverse order; hpreversestart itself is the last register
  186. parameter }
  187. hpcurr.right:=hpreversestart.right;
  188. hpreversestart.right:=hpcurr;
  189. end
  190. else
  191. hpprev:=hpcurr;
  192. hpcurr:=hpnext;
  193. end;
  194. end;
  195. procedure tcgcallparanode.push_value_para;
  196. begin
  197. { we've nothing to push when the size of the parameter is 0
  198. -- except in case of the self parameter of an emptry record on e.g.
  199. the JVM target }
  200. if (left.resultdef.size=0) and
  201. not(vo_is_self in parasym.varoptions) then
  202. exit;
  203. { Move flags and jump in register to make it less complex }
  204. if left.location.loc in [LOC_FLAGS,LOC_JUMP,LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF] then
  205. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  206. { load the parameter's tlocation into its cgpara }
  207. hlcg.gen_load_loc_cgpara(current_asmdata.CurrAsmList,left.resultdef,left.location,tempcgpara)
  208. end;
  209. procedure tcgcallparanode.push_formal_para;
  210. begin
  211. { allow passing of a constant to a const formaldef }
  212. if (parasym.varspez=vs_const) and
  213. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  214. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  215. push_addr_para;
  216. end;
  217. procedure tcgcallparanode.secondcallparan;
  218. var
  219. pushaddr: boolean;
  220. begin
  221. if not(assigned(parasym)) then
  222. internalerror(200304242);
  223. { Skip nothingn nodes which are used after disabling
  224. a parameter }
  225. if (left.nodetype<>nothingn) then
  226. begin
  227. if assigned(fparainit) then
  228. secondpass(fparainit);
  229. secondpass(left);
  230. hlcg.maybe_change_load_node_reg(current_asmdata.CurrAsmList,left,true);
  231. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,not followed_by_stack_tainting_call_cached,tempcgpara);
  232. { handle varargs first, because parasym is not valid }
  233. if (cpf_varargs_para in callparaflags) then
  234. begin
  235. if paramanager.push_addr_param(vs_value,left.resultdef,
  236. aktcallnode.procdefinition.proccalloption) then
  237. push_addr_para
  238. else
  239. push_value_para;
  240. end
  241. { hidden parameters }
  242. else if (vo_is_hidden_para in parasym.varoptions) then
  243. begin
  244. { don't push a node that already generated a pointer type
  245. by address for implicit hidden parameters }
  246. pushaddr:=(vo_is_funcret in parasym.varoptions) or
  247. { pass "this" in C++ classes explicitly as pointer
  248. because push_addr_param might not be true for them }
  249. (is_cppclass(parasym.vardef) and (vo_is_self in parasym.varoptions)) or
  250. (
  251. (
  252. not(left.resultdef.typ in [pointerdef,classrefdef]) or
  253. (
  254. { allow pointerdefs (as self) to be passed as addr
  255. param if the method is part of a type helper which
  256. extends a pointer type }
  257. (vo_is_self in parasym.varoptions) and
  258. (aktcallnode.procdefinition.owner.symtabletype=objectsymtable) and
  259. (is_objectpascal_helper(tdef(aktcallnode.procdefinition.owner.defowner))) and
  260. (tobjectdef(aktcallnode.procdefinition.owner.defowner).extendeddef.typ=pointerdef)
  261. )
  262. ) and
  263. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  264. aktcallnode.procdefinition.proccalloption));
  265. if pushaddr then
  266. begin
  267. { objects or advanced records could be located in registers if they are the result of a type case, see e.g. webtbs\tw26075.pp }
  268. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  269. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  270. push_addr_para
  271. end
  272. else
  273. push_value_para;
  274. end
  275. { formal def }
  276. else if (parasym.vardef.typ=formaldef) then
  277. push_formal_para
  278. { Normal parameter }
  279. else if paramanager.push_copyout_param(parasym.varspez,parasym.vardef,
  280. aktcallnode.procdefinition.proccalloption) then
  281. push_copyout_para
  282. else
  283. begin
  284. { don't push a node that already generated a pointer type
  285. by address for implicit hidden parameters }
  286. if (not(
  287. (vo_is_hidden_para in parasym.varoptions) and
  288. (left.resultdef.typ in [pointerdef,classrefdef])
  289. ) and
  290. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  291. aktcallnode.procdefinition.proccalloption)) and
  292. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  293. not(
  294. is_array_of_const(parasym.vardef) and
  295. is_dynamic_array(left.resultdef)
  296. ) then
  297. begin
  298. { Passing a var parameter to a var parameter, we can
  299. just push the address transparently }
  300. if (left.nodetype=loadn) and
  301. (tloadnode(left).is_addr_param_load) then
  302. begin
  303. if (left.location.reference.index<>NR_NO) or
  304. (left.location.reference.offset<>0) then
  305. internalerror(200410107);
  306. hlcg.a_load_reg_cgpara(current_asmdata.CurrAsmList,cpointerdef.getreusable(left.resultdef),left.location.reference.base,tempcgpara)
  307. end
  308. else
  309. begin
  310. { Force to be in memory }
  311. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  312. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  313. push_addr_para;
  314. end;
  315. end
  316. else
  317. push_value_para;
  318. end;
  319. { update return location in callnode when this is the function
  320. result }
  321. if assigned(parasym) and
  322. (
  323. { for type helper/record constructor check that it is self parameter }
  324. (
  325. (vo_is_self in parasym.varoptions) and
  326. (aktcallnode.procdefinition.proctypeoption=potype_constructor) and
  327. (parasym.vardef.typ<>objectdef)
  328. ) or
  329. (vo_is_funcret in parasym.varoptions)
  330. ) then
  331. location_copy(aktcallnode.location,left.location);
  332. end;
  333. { next parameter }
  334. if assigned(right) then
  335. tcallparanode(right).secondcallparan;
  336. end;
  337. {*****************************************************************************
  338. TCGCALLNODE
  339. *****************************************************************************}
  340. {$if first_mm_imreg = 0}
  341. {$WARN 4044 OFF} { Comparison might be always false ... }
  342. {$endif}
  343. procedure tcgcallnode.extra_interrupt_code;
  344. begin
  345. end;
  346. procedure tcgcallnode.extra_pre_call_code;
  347. begin
  348. end;
  349. procedure tcgcallnode.extra_call_code;
  350. begin
  351. end;
  352. procedure tcgcallnode.extra_post_call_code;
  353. begin
  354. end;
  355. function tcgcallnode.can_call_ref(var ref: treference): boolean;
  356. begin
  357. result:=false;
  358. end;
  359. procedure tcgcallnode.extra_call_ref_code(var ref: treference);
  360. begin
  361. { no action by default }
  362. end;
  363. function tcgcallnode.do_call_ref(ref: treference): tcgpara;
  364. begin
  365. InternalError(2014012901);
  366. { silence warning }
  367. result.init;
  368. end;
  369. procedure tcgcallnode.load_block_invoke(toreg: tregister; out callprocdef: tabstractprocdef);
  370. var
  371. href: treference;
  372. literaldef: trecorddef;
  373. begin
  374. literaldef:=get_block_literal_type_for_proc(tabstractprocdef(right.resultdef));
  375. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cpointerdef.getreusable(literaldef),true);
  376. { load the invoke pointer }
  377. hlcg.reference_reset_base(href,right.resultdef,right.location.register,0,right.resultdef.alignment);
  378. hlcg.g_load_field_reg_by_name(current_asmdata.CurrAsmList,literaldef,procdefinition,'INVOKE',href,toreg);
  379. callprocdef:=procdefinition;
  380. end;
  381. function tcgcallnode.get_call_reg(list: TAsmList): tregister;
  382. begin
  383. result:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition.address_type);
  384. end;
  385. procedure tcgcallnode.unget_call_reg(list: TAsmList; reg: tregister);
  386. begin
  387. { nothing to do by default }
  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(
  536. (vo_is_funcret in ppn.parasym.varoptions) or
  537. (
  538. (vo_is_self in ppn.parasym.varoptions) and
  539. (procdefinition.proctypeoption=potype_constructor) and
  540. (ppn.parasym.vardef.typ<>objectdef)
  541. )
  542. )then
  543. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  544. { process also all nodes of an array of const }
  545. hp:=ppn.left;
  546. while (hp.nodetype=typeconvn) do
  547. hp:=ttypeconvnode(hp).left;
  548. if (hp.nodetype=arrayconstructorn) and
  549. assigned(tarrayconstructornode(hp).left) then
  550. begin
  551. while assigned(hp) do
  552. begin
  553. hp2:=tarrayconstructornode(hp).left;
  554. { ignore typeconvs and addrn inserted by arrayconstructn for
  555. passing a shortstring }
  556. if (hp2.nodetype=typeconvn) and
  557. (tunarynode(hp2).left.nodetype=addrn) then
  558. hp2:=tunarynode(tunarynode(hp2).left).left
  559. else if hp2.nodetype=addrn then
  560. hp2:=tunarynode(hp2).left;
  561. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  562. hp:=tarrayconstructornode(hp).right;
  563. end;
  564. end;
  565. end;
  566. ppn:=tcallparanode(ppn.right);
  567. end;
  568. setlength(paralocs,0);
  569. end;
  570. procedure tcgcallnode.pushparas;
  571. var
  572. ppn : tcgcallparanode;
  573. callerparaloc,
  574. tmpparaloc : pcgparalocation;
  575. sizeleft: aint;
  576. htempref,
  577. href : treference;
  578. calleralignment,
  579. tmpalignment, i: longint;
  580. skipiffinalloc: boolean;
  581. begin
  582. { copy all resources to the allocated registers }
  583. ppn:=tcgcallparanode(left);
  584. while assigned(ppn) do
  585. begin
  586. if (ppn.left.nodetype<>nothingn) then
  587. begin
  588. { better check for the real location of the parameter here, when stack passed parameters
  589. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  590. }
  591. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.tempcgpara);
  592. tmpparaloc:=ppn.tempcgpara.location;
  593. sizeleft:=ppn.tempcgpara.intsize;
  594. calleralignment:=ppn.parasym.paraloc[callerside].alignment;
  595. tmpalignment:=ppn.tempcgpara.alignment;
  596. if (tmpalignment=0) or
  597. (calleralignment=0) then
  598. internalerror(2009020701);
  599. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  600. skipiffinalloc:=
  601. not paramanager.use_fixed_stack or
  602. not(ppn.followed_by_stack_tainting_call_cached);
  603. while assigned(callerparaloc) do
  604. begin
  605. { Every paraloc must have a matching tmpparaloc }
  606. if not assigned(tmpparaloc) then
  607. internalerror(200408224);
  608. if callerparaloc^.size<>tmpparaloc^.size then
  609. internalerror(200408225);
  610. case callerparaloc^.loc of
  611. LOC_REGISTER:
  612. begin
  613. if tmpparaloc^.loc<>LOC_REGISTER then
  614. internalerror(200408221);
  615. if getsupreg(callerparaloc^.register)<first_int_imreg then
  616. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  617. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  618. tmpparaloc^.register,callerparaloc^.register);
  619. end;
  620. LOC_FPUREGISTER:
  621. begin
  622. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  623. internalerror(200408222);
  624. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  625. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  626. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,callerparaloc^.register);
  627. end;
  628. LOC_MMREGISTER:
  629. begin
  630. if tmpparaloc^.loc<>LOC_MMREGISTER then
  631. internalerror(200408223);
  632. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  633. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  634. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  635. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  636. end;
  637. LOC_REFERENCE:
  638. begin
  639. if not(skipiffinalloc and
  640. paramanager.is_stack_paraloc(callerparaloc)) then
  641. begin
  642. { Can't have a data copied to the stack, every location
  643. must contain a valid size field }
  644. if (tmpparaloc^.size=OS_NO) and
  645. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  646. assigned(tmpparaloc^.next)) then
  647. internalerror(200501281);
  648. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset,calleralignment);
  649. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  650. case tmpparaloc^.loc of
  651. LOC_REFERENCE:
  652. begin
  653. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset,tmpalignment);
  654. { use concatcopy, because it can also be a float which fails when
  655. load_ref_ref is used }
  656. if (ppn.tempcgpara.size <> OS_NO) then
  657. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  658. else
  659. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  660. end;
  661. LOC_REGISTER:
  662. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  663. LOC_FPUREGISTER:
  664. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  665. LOC_MMREGISTER:
  666. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  667. else
  668. internalerror(200402081);
  669. end;
  670. end;
  671. end;
  672. end;
  673. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  674. callerparaloc:=callerparaloc^.next;
  675. tmpparaloc:=tmpparaloc^.next;
  676. end;
  677. end;
  678. ppn:=tcgcallparanode(ppn.right);
  679. end;
  680. setlength(paralocs,procdefinition.paras.count);
  681. for i:=0 to procdefinition.paras.count-1 do
  682. paralocs[i]:=@tparavarsym(procdefinition.paras[i]).paraloc[callerside];
  683. end;
  684. procedure tcgcallnode.load_complex_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef);
  685. var
  686. srcreg: tregister;
  687. begin
  688. { this is safe even on i8086, because procvardef code pointers are
  689. always far there (so the current state of far calls vs the state
  690. of far calls where the procvardef was defined does not matter,
  691. even though the procvardef constructor called by getcopyas looks at
  692. it) }
  693. callprocdef:=cprocvardef.getreusableprocaddr(procdefinition);
  694. reg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,callprocdef);
  695. { in case we have a method pointer on a big endian target in registers,
  696. the method address is stored in registerhi (it's the first field
  697. in the tmethod record) }
  698. if (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  699. begin
  700. if not(right.location.size in [OS_PAIR,OS_SPAIR]) then
  701. internalerror(2014081401);
  702. if (target_info.endian=endian_big) then
  703. srcreg:=right.location.registerhi
  704. else
  705. srcreg:=right.location.register;
  706. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,callprocdef,callprocdef,srcreg,reg)
  707. end
  708. else
  709. begin
  710. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,procdefinition);
  711. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,cpointerdef.getreusable(procdefinition),cpointerdef.getreusable(callprocdef),right.location.reference);
  712. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,callprocdef,callprocdef,right.location.reference,reg);
  713. end;
  714. end;
  715. procedure tcgcallnode.load_procvar_codeptr(out reg: tregister; out callprocdef: tabstractprocdef);
  716. begin
  717. if po_is_block in procdefinition.procoptions then
  718. begin
  719. reg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition);
  720. load_block_invoke(reg,callprocdef);
  721. end
  722. else if not(procdefinition.is_addressonly) then
  723. load_complex_procvar_codeptr(reg,callprocdef)
  724. else
  725. begin
  726. reg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,procdefinition);
  727. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,procdefinition,procdefinition,right.location,reg);
  728. callprocdef:=procdefinition;
  729. end;
  730. callprocdef.init_paraloc_info(callerside);
  731. end;
  732. procedure tcgcallnode.freeparas;
  733. var
  734. ppn : tcgcallparanode;
  735. begin
  736. { free the resources allocated for the parameters }
  737. ppn:=tcgcallparanode(left);
  738. while assigned(ppn) do
  739. begin
  740. if (ppn.left.nodetype<>nothingn) then
  741. begin
  742. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  743. paramanager.freecgpara(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  744. end;
  745. ppn:=tcgcallparanode(ppn.right);
  746. end;
  747. end;
  748. procedure tcgcallnode.pass_generate_code;
  749. var
  750. name_to_call: TSymStr;
  751. regs_to_save_int,
  752. regs_to_save_address,
  753. regs_to_save_fpu,
  754. regs_to_save_mm : Tcpuregisterset;
  755. href : treference;
  756. pop_size : longint;
  757. pvreg : tregister;
  758. oldaktcallnode : tcallnode;
  759. retlocitem: pcgparalocation;
  760. callpvdef: tabstractprocdef;
  761. pd : tprocdef;
  762. callref: boolean;
  763. {$ifdef vtentry}
  764. sym : tasmsymbol;
  765. vmtoffset : aint;
  766. {$endif vtentry}
  767. {$ifdef SUPPORT_SAFECALL}
  768. cgpara : tcgpara;
  769. {$endif}
  770. begin
  771. if not assigned(procdefinition) or
  772. not(procdefinition.has_paraloc_info in [callerside,callbothsides]) then
  773. internalerror(200305264);
  774. extra_pre_call_code;
  775. if assigned(callinitblock) then
  776. secondpass(tnode(callinitblock));
  777. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  778. regs_to_save_address:=paramanager.get_volatile_registers_address(procdefinition.proccalloption);
  779. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  780. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  781. { Include Function result registers }
  782. if (not is_void(resultdef)) then
  783. begin
  784. { The forced returntype may have a different size than the one
  785. declared for the procdef }
  786. retloc:=hlcg.get_call_result_cgpara(procdefinition,typedef);
  787. retlocitem:=retloc.location;
  788. while assigned(retlocitem) do
  789. begin
  790. case retlocitem^.loc of
  791. LOC_REGISTER:
  792. case getregtype(retlocitem^.register) of
  793. R_INTREGISTER:
  794. include(regs_to_save_int,getsupreg(retlocitem^.register));
  795. R_ADDRESSREGISTER:
  796. include(regs_to_save_address,getsupreg(retlocitem^.register));
  797. R_TEMPREGISTER:
  798. ;
  799. else
  800. internalerror(2014020102);
  801. end;
  802. LOC_FPUREGISTER:
  803. include(regs_to_save_fpu,getsupreg(retlocitem^.register));
  804. LOC_MMREGISTER:
  805. include(regs_to_save_mm,getsupreg(retlocitem^.register));
  806. LOC_REFERENCE,
  807. LOC_VOID:
  808. ;
  809. else
  810. internalerror(2004110213);
  811. end;
  812. retlocitem:=retlocitem^.next;
  813. end;
  814. end;
  815. { Process parameters, register parameters will be loaded
  816. in imaginary registers. The actual load to the correct
  817. register is done just before the call }
  818. oldaktcallnode:=aktcallnode;
  819. aktcallnode:=self;
  820. if assigned(left) then
  821. tcallparanode(left).secondcallparan;
  822. aktcallnode:=oldaktcallnode;
  823. { procedure variable or normal function call ? }
  824. if (right=nil) then
  825. begin
  826. { register call for WPO (must be done before wpo test below,
  827. otherwise optimised called methods are no longer registered)
  828. }
  829. if (po_virtualmethod in procdefinition.procoptions) and
  830. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  831. assigned(methodpointer) and
  832. (methodpointer.nodetype<>typen) and
  833. (not assigned(current_procinfo) or
  834. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  835. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  836. {$ifdef vtentry}
  837. if not is_interface(tprocdef(procdefinition)._class) then
  838. begin
  839. inc(current_asmdata.NextVTEntryNr);
  840. 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));
  841. end;
  842. {$endif vtentry}
  843. name_to_call:=forcedprocname;
  844. { When methodpointer is typen we don't need (and can't) load
  845. a pointer. We can directly call the correct procdef (PFV) }
  846. if (name_to_call='') and
  847. (po_virtualmethod in procdefinition.procoptions) and
  848. not is_objectpascal_helper(tprocdef(procdefinition).struct) and
  849. assigned(methodpointer) and
  850. (methodpointer.nodetype<>typen) and
  851. not wpoinfomanager.can_be_devirtualized(methodpointer.resultdef,procdefinition,name_to_call) then
  852. begin
  853. { virtual methods require an index }
  854. if tprocdef(procdefinition).extnumber=$ffff then
  855. internalerror(200304021);
  856. { load the VMT entry (address of the virtual method) }
  857. secondpass(vmt_entry);
  858. { register call for WPO }
  859. if (not assigned(current_procinfo) or
  860. wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname)) then
  861. tobjectdef(tprocdef(procdefinition).struct).register_vmt_call(tprocdef(procdefinition).extnumber);
  862. if not(vmt_entry.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  863. internalerror(2015052502);
  864. href:=vmt_entry.location.reference;
  865. pvreg:=NR_NO;
  866. callref:=can_call_ref(href);
  867. if not callref then
  868. begin
  869. pvreg:=get_call_reg(current_asmdata.CurrAsmList);
  870. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,
  871. vmt_entry.resultdef,vmt_entry.resultdef,
  872. href,pvreg);
  873. end;
  874. { Load parameters that are in temporary registers in the
  875. correct parameter register }
  876. if assigned(left) then
  877. begin
  878. reorder_parameters;
  879. pushparas;
  880. { free the resources allocated for the parameters }
  881. freeparas;
  882. end;
  883. if callref then
  884. extra_call_ref_code(href);
  885. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  886. if cg.uses_registers(R_ADDRESSREGISTER) then
  887. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  888. if cg.uses_registers(R_FPUREGISTER) then
  889. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  890. if cg.uses_registers(R_MMREGISTER) then
  891. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  892. { call method }
  893. extra_call_code;
  894. retloc.resetiftemp;
  895. if callref then
  896. retloc:=do_call_ref(href)
  897. else
  898. begin
  899. hlcg.g_ptrtypecast_reg(current_asmdata.CurrAsmList,vmt_entry.resultdef,cpointerdef.getreusable(procdefinition),pvreg);
  900. retloc:=hlcg.a_call_reg(current_asmdata.CurrAsmList,tabstractprocdef(procdefinition),pvreg,paralocs);
  901. unget_call_reg(current_asmdata.CurrAsmList,pvreg);
  902. end;
  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 not(po_is_block in procdefinition.procoptions) and
  950. (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  951. begin
  952. href:=right.location.reference;
  953. callref:=can_call_ref(href);
  954. end;
  955. if not callref then
  956. load_procvar_codeptr(pvreg,callpvdef)
  957. else
  958. begin
  959. pvreg:=NR_INVALID;
  960. callpvdef:=nil;
  961. end;
  962. location_freetemp(current_asmdata.CurrAsmList,right.location);
  963. { Load parameters that are in temporary registers in the
  964. correct parameter register }
  965. if assigned(left) then
  966. begin
  967. reorder_parameters;
  968. pushparas;
  969. { free the resources allocated for the parameters }
  970. freeparas;
  971. end;
  972. if callref then
  973. extra_call_ref_code(href);
  974. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  975. if cg.uses_registers(R_ADDRESSREGISTER) then
  976. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  977. if cg.uses_registers(R_FPUREGISTER) then
  978. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  979. if cg.uses_registers(R_MMREGISTER) then
  980. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  981. { Calling interrupt from the same code requires some
  982. extra code }
  983. if (po_interrupt in procdefinition.procoptions) then
  984. extra_interrupt_code;
  985. extra_call_code;
  986. retloc.resetiftemp;
  987. if callref then
  988. retloc:=do_call_ref(href)
  989. else
  990. retloc:=hlcg.a_call_reg(current_asmdata.CurrAsmList,callpvdef,pvreg,paralocs);
  991. extra_post_call_code;
  992. end;
  993. { Need to remove the parameters from the stack? }
  994. if (procdefinition.proccalloption in clearstack_pocalls) then
  995. begin
  996. pop_size:=pushedparasize;
  997. { for Cdecl functions we don't need to pop the funcret when it
  998. was pushed by para. Except for safecall functions with
  999. safecall-exceptions enabled. In that case the funcret is always
  1000. returned as a para which is considered a normal para on the
  1001. c-side, so the funcret has to be pop'ed normally. }
  1002. if not ((procdefinition.proccalloption=pocall_safecall) and
  1003. (tf_safecall_exceptions in target_info.flags)) and
  1004. paramanager.ret_in_param(procdefinition.returndef,procdefinition) then
  1005. dec(pop_size,sizeof(pint));
  1006. { Remove parameters/alignment from the stack }
  1007. pop_parasize(pop_size);
  1008. end
  1009. { frame pointer parameter is popped by the caller when it's passed the
  1010. Delphi way }
  1011. else if (po_delphi_nested_cc in procdefinition.procoptions) and
  1012. not paramanager.use_fixed_stack then
  1013. pop_parasize(sizeof(pint));
  1014. { Release registers, but not the registers that contain the
  1015. function result }
  1016. if (not is_void(resultdef)) then
  1017. begin
  1018. retlocitem:=retloc.location;
  1019. while assigned(retlocitem) do
  1020. begin
  1021. case retlocitem^.loc of
  1022. LOC_REGISTER:
  1023. case getregtype(retlocitem^.register) of
  1024. R_INTREGISTER:
  1025. exclude(regs_to_save_int,getsupreg(retlocitem^.register));
  1026. R_ADDRESSREGISTER:
  1027. exclude(regs_to_save_address,getsupreg(retlocitem^.register));
  1028. R_TEMPREGISTER:
  1029. ;
  1030. else
  1031. internalerror(2014020103);
  1032. end;
  1033. LOC_FPUREGISTER:
  1034. exclude(regs_to_save_fpu,getsupreg(retlocitem^.register));
  1035. LOC_MMREGISTER:
  1036. exclude(regs_to_save_mm,getsupreg(retlocitem^.register));
  1037. LOC_REFERENCE,
  1038. LOC_VOID:
  1039. ;
  1040. else
  1041. internalerror(2004110214);
  1042. end;
  1043. retlocitem:=retlocitem^.next;
  1044. end;
  1045. end;
  1046. if cg.uses_registers(R_MMREGISTER) then
  1047. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  1048. if cg.uses_registers(R_FPUREGISTER) then
  1049. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  1050. if cg.uses_registers(R_ADDRESSREGISTER) then
  1051. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_ADDRESSREGISTER,regs_to_save_address);
  1052. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  1053. {$ifdef SUPPORT_SAFECALL}
  1054. if (procdefinition.proccalloption=pocall_safecall) and
  1055. (tf_safecall_exceptions in target_info.flags) then
  1056. begin
  1057. pd:=search_system_proc('fpc_safecallcheck');
  1058. cgpara.init;
  1059. paramanager.getintparaloc(current_asmdata.CurrAsmList,pd,1,cgpara);
  1060. cg.a_load_reg_cgpara(current_asmdata.CurrAsmList,OS_INT,NR_FUNCTION_RESULT_REG,cgpara);
  1061. paramanager.freecgpara(current_asmdata.CurrAsmList,cgpara);
  1062. cg.g_call(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK');
  1063. cgpara.done;
  1064. end;
  1065. {$endif}
  1066. { handle function results }
  1067. if (not is_void(resultdef)) then
  1068. handle_return_value
  1069. else
  1070. location_reset(location,LOC_VOID,OS_NO);
  1071. { convert persistent temps for parameters and function result to normal temps }
  1072. if assigned(callcleanupblock) then
  1073. secondpass(tnode(callcleanupblock));
  1074. { copy back copy-out parameters if any }
  1075. copy_back_paras;
  1076. { release temps and finalize unused return values, must be
  1077. after the callcleanupblock because that converts temps
  1078. from persistent to normal }
  1079. release_unused_return_value;
  1080. { release temps of paras }
  1081. release_para_temps;
  1082. { perhaps i/o check ? }
  1083. if (cs_check_io in current_settings.localswitches) and
  1084. (po_iocheck in procdefinition.procoptions) and
  1085. not(po_iocheck in current_procinfo.procdef.procoptions) and
  1086. { no IO check for methods and procedure variables }
  1087. (right=nil) and
  1088. not(po_virtualmethod in procdefinition.procoptions) then
  1089. begin
  1090. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_iocheck',[],nil).resetiftemp;
  1091. end;
  1092. end;
  1093. destructor tcgcallnode.destroy;
  1094. begin
  1095. retloc.resetiftemp;
  1096. inherited destroy;
  1097. end;
  1098. begin
  1099. ccallparanode:=tcgcallparanode;
  1100. ccallnode:=tcgcallnode;
  1101. end.