ncgcal.pas 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate assembler for call nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgcal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. globtype,
  23. parabase,cgutils,
  24. symdef,node,ncal;
  25. type
  26. tcgcallparanode = class(tcallparanode)
  27. private
  28. tempcgpara : tcgpara;
  29. procedure push_addr_para;
  30. procedure push_value_para;
  31. public
  32. constructor create(expr,next : tnode);override;
  33. destructor destroy;override;
  34. procedure secondcallparan;override;
  35. end;
  36. tcgcallnode = class(tcallnode)
  37. private
  38. procedure handle_return_value;
  39. procedure release_unused_return_value;
  40. procedure release_para_temps;
  41. procedure pushparas;
  42. procedure freeparas;
  43. protected
  44. framepointer_paraloc : tcgpara;
  45. {# This routine is used to push the current frame pointer
  46. on the stack. This is used in nested routines where the
  47. value of the frame pointer is always pushed as an extra
  48. parameter.
  49. The default handling is the standard handling used on
  50. most stack based machines, where the frame pointer is
  51. the first invisible parameter.
  52. }
  53. procedure pop_parasize(pop_size:longint);virtual;
  54. procedure extra_interrupt_code;virtual;
  55. procedure extra_call_code;virtual;
  56. procedure extra_post_call_code;virtual;
  57. procedure do_syscall;virtual;abstract;
  58. public
  59. procedure pass_generate_code;override;
  60. end;
  61. implementation
  62. uses
  63. systems,
  64. cutils,verbose,globals,
  65. cpuinfo,
  66. symconst,symtable,defutil,paramgr,
  67. cgbase,pass_2,
  68. aasmbase,aasmtai,aasmdata,
  69. nbas,nmem,nld,ncnv,nutils,
  70. {$ifdef x86}
  71. cga,cgx86,aasmcpu,
  72. {$endif x86}
  73. ncgutil,
  74. cgobj,tgobj,
  75. procinfo;
  76. {*****************************************************************************
  77. TCGCALLPARANODE
  78. *****************************************************************************}
  79. constructor tcgcallparanode.create(expr,next : tnode);
  80. begin
  81. inherited create(expr,next);
  82. tempcgpara.init;
  83. end;
  84. destructor tcgcallparanode.destroy;
  85. begin
  86. tempcgpara.done;
  87. inherited destroy;
  88. end;
  89. procedure tcgcallparanode.push_addr_para;
  90. begin
  91. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  92. internalerror(200304235);
  93. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  94. end;
  95. procedure tcgcallparanode.push_value_para;
  96. {$ifdef i386}
  97. var
  98. href : treference;
  99. size : longint;
  100. {$endif i386}
  101. begin
  102. { we've nothing to push when the size of the parameter is 0 }
  103. if left.resultdef.size=0 then
  104. exit;
  105. { Move flags and jump in register to make it less complex }
  106. if left.location.loc in [LOC_FLAGS,LOC_JUMP,LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF] then
  107. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resultdef),false);
  108. { Handle Floating point types differently
  109. This doesn't depend on emulator settings, emulator settings should
  110. be handled by cpupara }
  111. if left.resultdef.typ=floatdef then
  112. begin
  113. {$ifdef i386}
  114. if tempcgpara.location^.loc<>LOC_REFERENCE then
  115. internalerror(200309291);
  116. case left.location.loc of
  117. LOC_FPUREGISTER,
  118. LOC_CFPUREGISTER:
  119. begin
  120. size:=align(TCGSize2Size[left.location.size],tempcgpara.alignment);
  121. if tempcgpara.location^.reference.index=NR_STACK_POINTER_REG then
  122. begin
  123. cg.g_stackpointer_alloc(current_asmdata.CurrAsmList,size);
  124. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  125. end
  126. else
  127. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  128. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,left.location.size,left.location.size,left.location.register,href);
  129. end;
  130. LOC_MMREGISTER,
  131. LOC_CMMREGISTER:
  132. begin
  133. size:=align(tfloatdef(left.resultdef).size,tempcgpara.alignment);
  134. if tempcgpara.location^.reference.index=NR_STACK_POINTER_REG then
  135. begin
  136. cg.g_stackpointer_alloc(current_asmdata.CurrAsmList,size);
  137. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  138. end
  139. else
  140. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  141. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,left.location.size,left.location.size,left.location.register,href,mms_movescalar);
  142. end;
  143. LOC_REFERENCE,
  144. LOC_CREFERENCE :
  145. begin
  146. size:=align(left.resultdef.size,tempcgpara.alignment);
  147. if (not use_fixed_stack) and
  148. (tempcgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  149. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  150. else
  151. begin
  152. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  153. cg.g_concatcopy(current_asmdata.CurrAsmList,left.location.reference,href,size);
  154. end;
  155. end;
  156. else
  157. internalerror(2002042430);
  158. end;
  159. {$else i386}
  160. case left.location.loc of
  161. LOC_MMREGISTER,
  162. LOC_CMMREGISTER:
  163. case tempcgpara.location^.loc of
  164. LOC_REFERENCE,
  165. LOC_CREFERENCE,
  166. LOC_MMREGISTER,
  167. LOC_CMMREGISTER:
  168. cg.a_parammm_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara,mms_movescalar);
  169. {$ifdef x86_64}
  170. LOC_REGISTER,
  171. LOC_CREGISTER :
  172. begin
  173. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVD,S_NO,left.location.register,tempcgpara.location^.register));
  174. end;
  175. {$endif x86_64}
  176. LOC_FPUREGISTER,
  177. LOC_CFPUREGISTER:
  178. begin
  179. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  180. cg.a_paramfpu_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara);
  181. end;
  182. else
  183. internalerror(200204249);
  184. end;
  185. LOC_FPUREGISTER,
  186. LOC_CFPUREGISTER:
  187. case tempcgpara.location^.loc of
  188. LOC_MMREGISTER,
  189. LOC_CMMREGISTER:
  190. begin
  191. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  192. cg.a_parammm_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara,mms_movescalar);
  193. end;
  194. {$ifdef cpu64bitalu}
  195. LOC_REGISTER,
  196. LOC_CREGISTER :
  197. begin
  198. location_force_mem(current_asmdata.CurrAsmList,left.location);
  199. { force integer size }
  200. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  201. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  202. end;
  203. {$endif cpu64bitalu}
  204. {$ifdef powerpc}
  205. LOC_REGISTER,
  206. LOC_CREGISTER :
  207. begin
  208. { aix abi passes floats of varargs in both fpu and }
  209. { integer registers }
  210. location_force_mem(current_asmdata.CurrAsmList,left.location);
  211. { force integer size }
  212. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  213. if (left.location.size in [OS_32,OS_S32]) then
  214. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  215. else
  216. cg64.a_param64_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  217. end;
  218. {$endif powerpc}
  219. {$if defined(sparc) or defined(arm) or defined(m68k)}
  220. { sparc and arm pass floats in normal registers }
  221. LOC_REGISTER,
  222. LOC_CREGISTER,
  223. {$endif sparc}
  224. LOC_REFERENCE,
  225. LOC_CREFERENCE,
  226. LOC_FPUREGISTER,
  227. LOC_CFPUREGISTER:
  228. cg.a_paramfpu_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara);
  229. else
  230. internalerror(2002042433);
  231. end;
  232. LOC_REFERENCE,
  233. LOC_CREFERENCE:
  234. case tempcgpara.location^.loc of
  235. LOC_MMREGISTER,
  236. LOC_CMMREGISTER:
  237. cg.a_parammm_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara,mms_movescalar);
  238. {$ifdef cpu64bitalu}
  239. LOC_REGISTER,
  240. LOC_CREGISTER :
  241. begin
  242. { force integer size }
  243. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  244. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  245. end;
  246. {$endif cpu64bitalu}
  247. {$ifdef powerpc}
  248. { x86_64 pushes s64comp in normal register }
  249. LOC_REGISTER,
  250. LOC_CREGISTER :
  251. begin
  252. { force integer size }
  253. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  254. if (left.location.size in [OS_32,OS_S32]) then
  255. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  256. else
  257. cg64.a_param64_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  258. end;
  259. {$endif powerpc}
  260. {$if defined(sparc) or defined(arm) or defined(m68k)}
  261. { sparc and arm pass floats in normal registers }
  262. LOC_REGISTER,
  263. LOC_CREGISTER,
  264. {$endif}
  265. LOC_REFERENCE,
  266. LOC_CREFERENCE,
  267. LOC_FPUREGISTER,
  268. LOC_CFPUREGISTER:
  269. cg.a_paramfpu_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  270. else
  271. internalerror(2002042431);
  272. end;
  273. LOC_REGISTER,
  274. LOC_CREGISTER :
  275. begin
  276. {$ifndef cpu64bitalu}
  277. { use cg64 only for int64, not for 8 byte records }
  278. if is_64bit(left.resultdef) then
  279. cg64.a_param64_loc(current_asmdata.CurrAsmList,left.location,tempcgpara)
  280. else
  281. {$endif not cpu64bitalu}
  282. begin
  283. {$ifndef cpu64bitalu}
  284. { Only a_param_ref supports multiple locations, when the
  285. value is still a const or in a register then write it
  286. to a reference first. This situation can be triggered
  287. by typecasting an int64 constant to a record of 8 bytes }
  288. if left.location.size in [OS_64,OS_S64] then
  289. location_force_mem(current_asmdata.CurrAsmList,left.location);
  290. {$endif not cpu64bitalu}
  291. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,tempcgpara);
  292. end;
  293. end;
  294. else
  295. internalerror(2002042432);
  296. end;
  297. {$endif i386}
  298. end
  299. else
  300. begin
  301. case left.location.loc of
  302. LOC_CONSTANT,
  303. LOC_REGISTER,
  304. LOC_CREGISTER,
  305. LOC_REFERENCE,
  306. LOC_CREFERENCE :
  307. begin
  308. {$ifndef cpu64bitalu}
  309. { use cg64 only for int64, not for 8 byte records }
  310. if is_64bit(left.resultdef) then
  311. cg64.a_param64_loc(current_asmdata.CurrAsmList,left.location,tempcgpara)
  312. else
  313. {$endif not cpu64bitalu}
  314. begin
  315. {$ifndef cpu64bitalu}
  316. { Only a_param_ref supports multiple locations, when the
  317. value is still a const or in a register then write it
  318. to a reference first. This situation can be triggered
  319. by typecasting an int64 constant to a record of 8 bytes }
  320. if left.location.size in [OS_64,OS_S64] then
  321. location_force_mem(current_asmdata.CurrAsmList,left.location);
  322. {$endif not cpu64bitalu}
  323. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,tempcgpara);
  324. end;
  325. end;
  326. {$ifdef SUPPORT_MMX}
  327. LOC_MMXREGISTER,
  328. LOC_CMMXREGISTER:
  329. cg.a_parammm_reg(current_asmdata.CurrAsmList,OS_M64,left.location.register,tempcgpara,nil);
  330. {$endif SUPPORT_MMX}
  331. else
  332. internalerror(200204241);
  333. end;
  334. end;
  335. end;
  336. procedure tcgcallparanode.secondcallparan;
  337. var
  338. href : treference;
  339. otlabel,
  340. oflabel : tasmlabel;
  341. begin
  342. if not(assigned(parasym)) then
  343. internalerror(200304242);
  344. { Skip nothingn nodes which are used after disabling
  345. a parameter }
  346. if (left.nodetype<>nothingn) then
  347. begin
  348. otlabel:=current_procinfo.CurrTrueLabel;
  349. oflabel:=current_procinfo.CurrFalseLabel;
  350. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  351. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  352. secondpass(left);
  353. maybechangeloadnodereg(current_asmdata.CurrAsmList,left,true);
  354. { release memory for refcnt out parameters }
  355. if (parasym.varspez=vs_out) and
  356. (left.resultdef.needs_inittable) then
  357. begin
  358. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  359. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resultdef,href);
  360. end;
  361. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,tempcgpara);
  362. { handle varargs first, because parasym is not valid }
  363. if (cpf_varargs_para in callparaflags) then
  364. begin
  365. if paramanager.push_addr_param(vs_value,left.resultdef,
  366. aktcallnode.procdefinition.proccalloption) then
  367. push_addr_para
  368. else
  369. push_value_para;
  370. end
  371. { hidden parameters }
  372. else if (vo_is_hidden_para in parasym.varoptions) then
  373. begin
  374. { don't push a node that already generated a pointer type
  375. by address for implicit hidden parameters }
  376. if (vo_is_funcret in parasym.varoptions) or
  377. { pass "this" in C++ classes explicitly as pointer
  378. because push_addr_param might not be true for them }
  379. (is_cppclass(parasym.vardef) and (vo_is_self in parasym.varoptions)) or
  380. (not(left.resultdef.typ in [pointerdef,classrefdef]) and
  381. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  382. aktcallnode.procdefinition.proccalloption)) then
  383. push_addr_para
  384. else
  385. push_value_para;
  386. end
  387. { formal def }
  388. else if (parasym.vardef.typ=formaldef) then
  389. begin
  390. { allow passing of a constant to a const formaldef }
  391. if (parasym.varspez=vs_const) and
  392. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  393. location_force_mem(current_asmdata.CurrAsmList,left.location);
  394. push_addr_para;
  395. end
  396. { Normal parameter }
  397. else
  398. begin
  399. { don't push a node that already generated a pointer type
  400. by address for implicit hidden parameters }
  401. if (not(
  402. (vo_is_hidden_para in parasym.varoptions) and
  403. (left.resultdef.typ in [pointerdef,classrefdef])
  404. ) and
  405. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  406. aktcallnode.procdefinition.proccalloption)) and
  407. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  408. not(
  409. is_array_of_const(parasym.vardef) and
  410. is_dynamic_array(left.resultdef)
  411. ) then
  412. begin
  413. { Passing a var parameter to a var parameter, we can
  414. just push the address transparently }
  415. if (left.nodetype=loadn) and
  416. (tloadnode(left).is_addr_param_load) then
  417. begin
  418. if (left.location.reference.index<>NR_NO) or
  419. (left.location.reference.offset<>0) then
  420. internalerror(200410107);
  421. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location.reference.base,tempcgpara)
  422. end
  423. else
  424. begin
  425. { Force to be in memory }
  426. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  427. location_force_mem(current_asmdata.CurrAsmList,left.location);
  428. push_addr_para;
  429. end;
  430. end
  431. else
  432. push_value_para;
  433. end;
  434. current_procinfo.CurrTrueLabel:=otlabel;
  435. current_procinfo.CurrFalseLabel:=oflabel;
  436. { update return location in callnode when this is the function
  437. result }
  438. if assigned(parasym) and
  439. (vo_is_funcret in parasym.varoptions) then
  440. location_copy(aktcallnode.location,left.location);
  441. end;
  442. { next parameter }
  443. if assigned(right) then
  444. tcallparanode(right).secondcallparan;
  445. end;
  446. {*****************************************************************************
  447. TCGCALLNODE
  448. *****************************************************************************}
  449. procedure tcgcallnode.extra_interrupt_code;
  450. begin
  451. end;
  452. procedure tcgcallnode.extra_call_code;
  453. begin
  454. end;
  455. procedure tcgcallnode.extra_post_call_code;
  456. begin
  457. end;
  458. procedure tcgcallnode.pop_parasize(pop_size:longint);
  459. begin
  460. end;
  461. procedure tcgcallnode.handle_return_value;
  462. var
  463. tmpcgsize,
  464. cgsize : tcgsize;
  465. retloc : tlocation;
  466. {$ifdef cpu64bitaddr}
  467. ref : treference;
  468. {$endif cpu64bitaddr}
  469. {$ifndef x86}
  470. hregister : tregister;
  471. {$endif not x86}
  472. begin
  473. { Check that the return location is set when the result is passed in
  474. a parameter }
  475. if (procdefinition.proctypeoption<>potype_constructor) and
  476. paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  477. begin
  478. if location.loc<>LOC_REFERENCE then
  479. internalerror(200304241);
  480. exit;
  481. end;
  482. { Load normal (ordinal,float,pointer) result value from accumulator }
  483. cgsize:=procdefinition.funcretloc[callerside].size;
  484. case procdefinition.funcretloc[callerside].loc of
  485. LOC_FPUREGISTER:
  486. begin
  487. location_reset(location,LOC_FPUREGISTER,cgsize);
  488. location.register:=procdefinition.funcretloc[callerside].register;
  489. {$ifdef x86}
  490. tcgx86(cg).inc_fpu_stack;
  491. {$else x86}
  492. if getsupreg(procdefinition.funcretloc[callerside].register)<first_fpu_imreg then
  493. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  494. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  495. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,location.size,location.size,location.register,hregister);
  496. location.register:=hregister;
  497. {$endif x86}
  498. end;
  499. LOC_REGISTER:
  500. begin
  501. if cgsize<>OS_NO then
  502. begin
  503. location_reset(location,LOC_REGISTER,cgsize);
  504. {$ifdef cpu64bitaddr}
  505. { x86-64 system v abi:
  506. structs with up to 16 bytes are returned in registers }
  507. if cgsize in [OS_128,OS_S128] then
  508. begin
  509. tg.GetTemp(current_asmdata.CurrAsmList,16,tt_normal,ref);
  510. location_reset(location,LOC_REFERENCE,OS_NO);
  511. location.reference:=ref;
  512. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_64,OS_64,procdefinition.funcretloc[callerside].register,ref);
  513. inc(ref.offset,8);
  514. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_64,OS_64,procdefinition.funcretloc[callerside].registerhi,ref);
  515. end
  516. else
  517. {$else cpu64bitaddr}
  518. if cgsize in [OS_64,OS_S64] then
  519. begin
  520. retloc:=procdefinition.funcretloc[callerside];
  521. if retloc.loc<>LOC_REGISTER then
  522. internalerror(200409141);
  523. { the function result registers are already allocated }
  524. if getsupreg(retloc.register64.reglo)<first_int_imreg then
  525. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reglo);
  526. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  527. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reglo,location.register64.reglo);
  528. if getsupreg(retloc.register64.reghi)<first_int_imreg then
  529. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reghi);
  530. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  531. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reghi,location.register64.reghi);
  532. end
  533. else
  534. {$endif not cpu64bitaddr}
  535. begin
  536. { change register size after the unget because the
  537. getregister was done for the full register
  538. def_cgsize(resultdef) is used here because
  539. it could be a constructor call }
  540. if getsupreg(procdefinition.funcretloc[callerside].register)<first_int_imreg then
  541. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  542. { but use def_size only if it returns something valid because in
  543. case of odd sized structured results in registers def_cgsize(resultdef)
  544. could return OS_NO }
  545. if def_cgsize(resultdef)<>OS_NO then
  546. tmpcgsize:=def_cgsize(resultdef)
  547. else
  548. tmpcgsize:=cgsize;
  549. location.register:=cg.getintregister(current_asmdata.CurrAsmList,tmpcgsize);
  550. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,tmpcgsize,procdefinition.funcretloc[callerside].register,location.register);
  551. end;
  552. {$ifdef arm}
  553. if (resultdef.typ=floatdef) and (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) then
  554. begin
  555. location_force_mem(current_asmdata.CurrAsmList,location);
  556. end;
  557. {$endif arm}
  558. end
  559. else
  560. begin
  561. if resultdef.size>0 then
  562. internalerror(200305131);
  563. end;
  564. end;
  565. LOC_MMREGISTER:
  566. begin
  567. location_reset(location,LOC_MMREGISTER,cgsize);
  568. if getsupreg(procdefinition.funcretloc[callerside].register)<first_mm_imreg then
  569. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  570. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,cgsize);
  571. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,procdefinition.funcretloc[callerside].register,location.register,mms_movescalar);
  572. end;
  573. else
  574. internalerror(200405023);
  575. end;
  576. { copy value to the final location if this was already provided to the
  577. callnode. This must be done after the call node, because the location can
  578. also be used as parameter and may not be finalized yet }
  579. if assigned(funcretnode) then
  580. begin
  581. funcretnode.pass_generate_code;
  582. { Decrease refcount for refcounted types, this can be skipped when
  583. we have used a temp, because then it is already done from tempcreatenode.
  584. Also no finalize is needed, because there is no risk of exceptions from the
  585. function since this is code is only executed after the function call has returned }
  586. if funcretnode.resultdef.needs_inittable and
  587. (funcretnode.nodetype<>temprefn) then
  588. cg.g_decrrefcount(current_asmdata.CurrAsmList,funcretnode.resultdef,funcretnode.location.reference);
  589. case location.loc of
  590. LOC_REGISTER :
  591. {$ifndef cpu64bitalu}
  592. if cgsize in [OS_64,OS_S64] then
  593. cg64.a_load64_reg_loc(current_asmdata.CurrAsmList,location.register64,funcretnode.location)
  594. else
  595. {$endif}
  596. cg.a_load_reg_loc(current_asmdata.CurrAsmList,cgsize,location.register,funcretnode.location);
  597. LOC_REFERENCE:
  598. begin
  599. case funcretnode.location.loc of
  600. LOC_REGISTER:
  601. cg.a_load_ref_reg(current_asmdata.CurrAsmList,cgsize,cgsize,location.reference,funcretnode.location.register);
  602. LOC_REFERENCE:
  603. cg.g_concatcopy(current_asmdata.CurrAsmList,location.reference,funcretnode.location.reference,resultdef.size);
  604. else
  605. internalerror(200802121);
  606. end;
  607. end;
  608. else
  609. internalerror(200709085);
  610. end;
  611. location := funcretnode.location;
  612. end;
  613. end;
  614. procedure tcgcallnode.release_unused_return_value;
  615. begin
  616. { When the result is not used we need to finalize the result and
  617. can release the temp. This need to be after the callcleanupblock
  618. tree is generated, because that converts the temp from persistent to normal }
  619. if not(cnf_return_value_used in callnodeflags) then
  620. begin
  621. case location.loc of
  622. LOC_REFERENCE :
  623. begin
  624. if resultdef.needs_inittable then
  625. cg.g_finalize(current_asmdata.CurrAsmList,resultdef,location.reference);
  626. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference);
  627. end;
  628. {$ifdef x86}
  629. LOC_FPUREGISTER :
  630. begin
  631. { release FPU stack }
  632. emit_reg(A_FSTP,S_NO,NR_FPU_RESULT_REG);
  633. tcgx86(cg).dec_fpu_stack;
  634. end;
  635. {$endif x86}
  636. end;
  637. if procdefinition.funcretloc[callerside].size<>OS_NO then
  638. location_free(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside]);
  639. location_reset(location,LOC_VOID,OS_NO);
  640. end;
  641. end;
  642. procedure tcgcallnode.release_para_temps;
  643. var
  644. hp,
  645. hp2 : tnode;
  646. ppn : tcallparanode;
  647. begin
  648. { Release temps from parameters }
  649. ppn:=tcallparanode(left);
  650. while assigned(ppn) do
  651. begin
  652. if assigned(ppn.left) then
  653. begin
  654. { don't release the funcret temp }
  655. if not(assigned(ppn.parasym)) or
  656. not(vo_is_funcret in ppn.parasym.varoptions) then
  657. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  658. { process also all nodes of an array of const }
  659. hp:=ppn.left;
  660. while (hp.nodetype=typeconvn) do
  661. hp:=ttypeconvnode(hp).left;
  662. if (hp.nodetype=arrayconstructorn) and
  663. assigned(tarrayconstructornode(hp).left) then
  664. begin
  665. while assigned(hp) do
  666. begin
  667. hp2:=tarrayconstructornode(hp).left;
  668. { ignore typeconvs and addrn inserted by arrayconstructn for
  669. passing a shortstring }
  670. if (hp2.nodetype=typeconvn) and
  671. (tunarynode(hp2).left.nodetype=addrn) then
  672. hp2:=tunarynode(tunarynode(hp2).left).left;
  673. location_freetemp(current_asmdata.CurrAsmList,hp2.location);
  674. hp:=tarrayconstructornode(hp).right;
  675. end;
  676. end;
  677. end;
  678. ppn:=tcallparanode(ppn.right);
  679. end;
  680. end;
  681. procedure tcgcallnode.pushparas;
  682. var
  683. ppn : tcgcallparanode;
  684. callerparaloc,
  685. tmpparaloc : pcgparalocation;
  686. sizeleft: aint;
  687. htempref,
  688. href : treference;
  689. begin
  690. { copy all resources to the allocated registers }
  691. ppn:=tcgcallparanode(left);
  692. while assigned(ppn) do
  693. begin
  694. if (ppn.left.nodetype<>nothingn) then
  695. begin
  696. { better check for the real location of the parameter here, when stack passed parameters
  697. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  698. }
  699. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.tempcgpara);
  700. tmpparaloc:=ppn.tempcgpara.location;
  701. sizeleft:=ppn.tempcgpara.intsize;
  702. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  703. while assigned(callerparaloc) do
  704. begin
  705. { Every paraloc must have a matching tmpparaloc }
  706. if not assigned(tmpparaloc) then
  707. internalerror(200408224);
  708. if callerparaloc^.size<>tmpparaloc^.size then
  709. internalerror(200408225);
  710. case callerparaloc^.loc of
  711. LOC_REGISTER:
  712. begin
  713. if tmpparaloc^.loc<>LOC_REGISTER then
  714. internalerror(200408221);
  715. if getsupreg(callerparaloc^.register)<first_int_imreg then
  716. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  717. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  718. tmpparaloc^.register,callerparaloc^.register);
  719. end;
  720. LOC_FPUREGISTER:
  721. begin
  722. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  723. internalerror(200408222);
  724. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  725. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  726. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  727. end;
  728. LOC_MMREGISTER:
  729. begin
  730. if tmpparaloc^.loc<>LOC_MMREGISTER then
  731. internalerror(200408223);
  732. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  733. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  734. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  735. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  736. end;
  737. LOC_REFERENCE:
  738. begin
  739. if use_fixed_stack then
  740. begin
  741. { Can't have a data copied to the stack, every location
  742. must contain a valid size field }
  743. if (ppn.tempcgpara.size=OS_NO) and
  744. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  745. assigned(tmpparaloc^.next)) then
  746. internalerror(200501281);
  747. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset);
  748. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  749. case tmpparaloc^.loc of
  750. LOC_REFERENCE:
  751. begin
  752. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset);
  753. { use concatcopy, because it can also be a float which fails when
  754. load_ref_ref is used }
  755. if (ppn.tempcgpara.size <> OS_NO) then
  756. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  757. else
  758. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  759. end;
  760. LOC_REGISTER:
  761. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  762. LOC_FPUREGISTER:
  763. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  764. LOC_MMREGISTER:
  765. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  766. else
  767. internalerror(200402081);
  768. end;
  769. end;
  770. end;
  771. end;
  772. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  773. callerparaloc:=callerparaloc^.next;
  774. tmpparaloc:=tmpparaloc^.next;
  775. end;
  776. end;
  777. ppn:=tcgcallparanode(ppn.right);
  778. end;
  779. end;
  780. procedure tcgcallnode.freeparas;
  781. var
  782. ppn : tcgcallparanode;
  783. begin
  784. { free the resources allocated for the parameters }
  785. ppn:=tcgcallparanode(left);
  786. while assigned(ppn) do
  787. begin
  788. if (ppn.left.nodetype<>nothingn) then
  789. begin
  790. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  791. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  792. end;
  793. ppn:=tcgcallparanode(ppn.right);
  794. end;
  795. end;
  796. procedure tcgcallnode.pass_generate_code;
  797. var
  798. regs_to_save_int,
  799. regs_to_save_fpu,
  800. regs_to_save_mm : Tcpuregisterset;
  801. href : treference;
  802. pop_size : longint;
  803. vmtoffset : aint;
  804. pvreg,
  805. vmtreg : tregister;
  806. oldaktcallnode : tcallnode;
  807. {$ifdef vtentry}
  808. sym : tasmsymbol;
  809. {$endif vtentry}
  810. {$ifdef x86_64}
  811. cgpara : tcgpara;
  812. {$endif x86_64}
  813. begin
  814. if not assigned(procdefinition) or
  815. not procdefinition.has_paraloc_info then
  816. internalerror(200305264);
  817. if assigned(callinitblock) then
  818. secondpass(callinitblock);
  819. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  820. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  821. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  822. { Include Function result registers }
  823. if (not is_void(resultdef)) then
  824. begin
  825. case procdefinition.funcretloc[callerside].loc of
  826. LOC_REGISTER,
  827. LOC_CREGISTER:
  828. include(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  829. LOC_FPUREGISTER,
  830. LOC_CFPUREGISTER:
  831. include(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  832. LOC_MMREGISTER,
  833. LOC_CMMREGISTER:
  834. include(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  835. LOC_REFERENCE,
  836. LOC_VOID:
  837. ;
  838. else
  839. internalerror(2004110213);
  840. end;
  841. end;
  842. { Process parameters, register parameters will be loaded
  843. in imaginary registers. The actual load to the correct
  844. register is done just before the call }
  845. oldaktcallnode:=aktcallnode;
  846. aktcallnode:=self;
  847. if assigned(left) then
  848. tcallparanode(left).secondcallparan;
  849. aktcallnode:=oldaktcallnode;
  850. { procedure variable or normal function call ? }
  851. if (right=nil) then
  852. begin
  853. { When methodpointer is typen we don't need (and can't) load
  854. a pointer. We can directly call the correct procdef (PFV) }
  855. if (po_virtualmethod in procdefinition.procoptions) and
  856. assigned(methodpointer) and
  857. (methodpointer.nodetype<>typen) then
  858. begin
  859. { virtual methods require an index }
  860. if tprocdef(procdefinition).extnumber=$ffff then
  861. internalerror(200304021);
  862. secondpass(methodpointer);
  863. { Load VMT from self }
  864. if methodpointer.resultdef.typ=objectdef then
  865. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resultdef),methodpointer.location,vmtreg)
  866. else
  867. begin
  868. { Load VMT value in register }
  869. location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,OS_ADDR,false);
  870. vmtreg:=methodpointer.location.register;
  871. end;
  872. { test validity of VMT }
  873. if not(is_interface(tprocdef(procdefinition)._class)) and
  874. not(is_cppclass(tprocdef(procdefinition)._class)) then
  875. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tprocdef(procdefinition)._class);
  876. { Call through VMT, generate a VTREF symbol to notify the linker }
  877. vmtoffset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  878. {$ifdef vtentry}
  879. if not is_interface(tprocdef(procdefinition)._class) then
  880. begin
  881. inc(current_asmdata.NextVTEntryNr);
  882. current_asmdata.CurrAsmList.Concat(tai_symbol.CreateName('VTREF'+tostr(current_asmdata.NextVTEntryNr)+'_'+tprocdef(procdefinition)._class.vmt_mangledname+'$$'+tostr(vmtoffset div sizeof(pint)),AT_FUNCTION,0));
  883. end;
  884. {$endif vtentry}
  885. {$ifndef x86}
  886. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  887. {$endif not x86}
  888. reference_reset_base(href,vmtreg,vmtoffset);
  889. {$ifndef x86}
  890. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  891. {$endif not x86}
  892. { Load parameters that are in temporary registers in the
  893. correct parameter register }
  894. if assigned(left) then
  895. begin
  896. pushparas;
  897. { free the resources allocated for the parameters }
  898. freeparas;
  899. end;
  900. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  901. if cg.uses_registers(R_FPUREGISTER) then
  902. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  903. if cg.uses_registers(R_MMREGISTER) then
  904. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  905. { call method }
  906. extra_call_code;
  907. {$ifdef x86}
  908. cg.a_call_ref(current_asmdata.CurrAsmList,href);
  909. {$else x86}
  910. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  911. {$endif x86}
  912. extra_post_call_code;
  913. end
  914. else
  915. begin
  916. { Load parameters that are in temporary registers in the
  917. correct parameter register }
  918. if assigned(left) then
  919. begin
  920. pushparas;
  921. { free the resources allocated for the parameters }
  922. freeparas;
  923. end;
  924. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  925. if cg.uses_registers(R_FPUREGISTER) then
  926. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  927. if cg.uses_registers(R_MMREGISTER) then
  928. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  929. if procdefinition.proccalloption=pocall_syscall then
  930. do_syscall
  931. else
  932. begin
  933. { Calling interrupt from the same code requires some
  934. extra code }
  935. if (po_interrupt in procdefinition.procoptions) then
  936. extra_interrupt_code;
  937. extra_call_code;
  938. cg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition).mangledname,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. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  948. { Only load OS_ADDR from the reference }
  949. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  950. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  951. else
  952. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,right.location,pvreg);
  953. location_freetemp(current_asmdata.CurrAsmList,right.location);
  954. { Load parameters that are in temporary registers in the
  955. correct parameter register }
  956. if assigned(left) then
  957. begin
  958. pushparas;
  959. { free the resources allocated for the parameters }
  960. freeparas;
  961. end;
  962. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  963. if cg.uses_registers(R_FPUREGISTER) then
  964. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  965. if cg.uses_registers(R_MMREGISTER) then
  966. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  967. { Calling interrupt from the same code requires some
  968. extra code }
  969. if (po_interrupt in procdefinition.procoptions) then
  970. extra_interrupt_code;
  971. extra_call_code;
  972. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  973. extra_post_call_code;
  974. end;
  975. { Need to remove the parameters from the stack? }
  976. if (procdefinition.proccalloption in clearstack_pocalls) then
  977. begin
  978. pop_size:=pushedparasize;
  979. { for Cdecl functions we don't need to pop the funcret when it
  980. was pushed by para }
  981. if paramanager.ret_in_param(procdefinition.returndef,procdefinition.proccalloption) then
  982. dec(pop_size,sizeof(pint));
  983. { Remove parameters/alignment from the stack }
  984. pop_parasize(pop_size);
  985. end;
  986. { Release registers, but not the registers that contain the
  987. function result }
  988. if (not is_void(resultdef)) then
  989. begin
  990. case procdefinition.funcretloc[callerside].loc of
  991. LOC_REGISTER,
  992. LOC_CREGISTER:
  993. begin
  994. {$ifndef cpu64bitalu}
  995. if procdefinition.funcretloc[callerside].size in [OS_64,OS_S64] then
  996. begin
  997. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reghi));
  998. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reglo));
  999. end
  1000. else
  1001. {$endif not cpu64bitalu}
  1002. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  1003. end;
  1004. LOC_FPUREGISTER,
  1005. LOC_CFPUREGISTER:
  1006. exclude(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  1007. LOC_MMREGISTER,
  1008. LOC_CMMREGISTER:
  1009. exclude(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  1010. LOC_REFERENCE,
  1011. LOC_VOID:
  1012. ;
  1013. else
  1014. internalerror(2004110214);
  1015. end;
  1016. end;
  1017. {$if defined(x86) or defined(arm)}
  1018. if procdefinition.proccalloption=pocall_safecall then
  1019. begin
  1020. {$ifdef x86_64}
  1021. cgpara.init;
  1022. paramanager.getintparaloc(pocall_default,1,cgpara);
  1023. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,NR_RAX,cgpara);
  1024. cgpara.done;
  1025. {$endif x86_64}
  1026. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1027. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK',false);
  1028. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1029. end;
  1030. {$endif}
  1031. if cg.uses_registers(R_MMREGISTER) then
  1032. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  1033. if cg.uses_registers(R_FPUREGISTER) then
  1034. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  1035. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  1036. { handle function results }
  1037. if (not is_void(resultdef)) then
  1038. handle_return_value
  1039. else
  1040. location_reset(location,LOC_VOID,OS_NO);
  1041. { convert persistent temps for parameters and function result to normal temps }
  1042. if assigned(callcleanupblock) then
  1043. secondpass(callcleanupblock);
  1044. { release temps and finalize unused return values, must be
  1045. after the callcleanupblock because that converts temps
  1046. from persistent to normal }
  1047. release_unused_return_value;
  1048. { release temps of paras }
  1049. release_para_temps;
  1050. { perhaps i/o check ? }
  1051. if (cs_check_io in current_settings.localswitches) and
  1052. (po_iocheck in procdefinition.procoptions) and
  1053. not(po_iocheck in current_procinfo.procdef.procoptions) and
  1054. { no IO check for methods and procedure variables }
  1055. (right=nil) and
  1056. not(po_virtualmethod in procdefinition.procoptions) then
  1057. begin
  1058. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  1059. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK',false);
  1060. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  1061. end;
  1062. end;
  1063. begin
  1064. ccallparanode:=tcgcallparanode;
  1065. ccallnode:=tcgcallnode;
  1066. end.