ncgcal.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 release_para_temps;
  39. procedure pushparas;
  40. procedure freeparas;
  41. protected
  42. framepointer_paraloc : tcgpara;
  43. refcountedtemp : treference;
  44. procedure handle_return_value;
  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_2;override;
  60. end;
  61. implementation
  62. uses
  63. systems,
  64. cutils,verbose,globals,
  65. symconst,symtable,defutil,paramgr,
  66. cgbase,pass_2,
  67. aasmbase,aasmtai,aasmdata,
  68. nbas,nmem,nld,ncnv,nutils,
  69. {$ifdef x86}
  70. cga,cgx86,
  71. {$endif x86}
  72. ncgutil,
  73. cgobj,tgobj,
  74. procinfo;
  75. {*****************************************************************************
  76. TCGCALLPARANODE
  77. *****************************************************************************}
  78. constructor tcgcallparanode.create(expr,next : tnode);
  79. begin
  80. inherited create(expr,next);
  81. tempcgpara.init;
  82. end;
  83. destructor tcgcallparanode.destroy;
  84. begin
  85. tempcgpara.done;
  86. inherited destroy;
  87. end;
  88. procedure tcgcallparanode.push_addr_para;
  89. begin
  90. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  91. internalerror(200304235);
  92. cg.a_paramaddr_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  93. end;
  94. procedure tcgcallparanode.push_value_para;
  95. {$ifdef i386}
  96. var
  97. href : treference;
  98. size : longint;
  99. {$endif i386}
  100. begin
  101. { we've nothing to push when the size of the parameter is 0 }
  102. if left.resulttype.def.size=0 then
  103. exit;
  104. { Move flags and jump in register to make it less complex }
  105. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  106. location_force_reg(current_asmdata.CurrAsmList,left.location,def_cgsize(left.resulttype.def),false);
  107. { Handle Floating point types differently }
  108. if (left.resulttype.def.deftype=floatdef) and not(cs_fp_emulation in aktmoduleswitches) then
  109. begin
  110. {$ifdef i386}
  111. if tempcgpara.location^.loc<>LOC_REFERENCE then
  112. internalerror(200309291);
  113. case left.location.loc of
  114. LOC_FPUREGISTER,
  115. LOC_CFPUREGISTER:
  116. begin
  117. size:=align(TCGSize2Size[left.location.size],tempcgpara.alignment);
  118. if tempcgpara.location^.reference.index=NR_STACK_POINTER_REG then
  119. begin
  120. cg.g_stackpointer_alloc(current_asmdata.CurrAsmList,size);
  121. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  122. end
  123. else
  124. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  125. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,left.location.size,left.location.register,href);
  126. end;
  127. LOC_MMREGISTER,
  128. LOC_CMMREGISTER:
  129. begin
  130. size:=align(tfloatdef(left.resulttype.def).size,tempcgpara.alignment);
  131. if tempcgpara.location^.reference.index=NR_STACK_POINTER_REG then
  132. begin
  133. cg.g_stackpointer_alloc(current_asmdata.CurrAsmList,size);
  134. reference_reset_base(href,NR_STACK_POINTER_REG,0);
  135. end
  136. else
  137. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  138. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,left.location.size,left.location.size,left.location.register,href,mms_movescalar);
  139. end;
  140. LOC_REFERENCE,
  141. LOC_CREFERENCE :
  142. begin
  143. size:=align(left.resulttype.def.size,tempcgpara.alignment);
  144. if (not use_fixed_stack) and
  145. (tempcgpara.location^.reference.index=NR_STACK_POINTER_REG) then
  146. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  147. else
  148. begin
  149. reference_reset_base(href,tempcgpara.location^.reference.index,tempcgpara.location^.reference.offset);
  150. cg.g_concatcopy(current_asmdata.CurrAsmList,left.location.reference,href,size);
  151. end;
  152. end;
  153. else
  154. internalerror(2002042430);
  155. end;
  156. {$else i386}
  157. case left.location.loc of
  158. LOC_MMREGISTER,
  159. LOC_CMMREGISTER:
  160. case tempcgpara.location^.loc of
  161. LOC_REFERENCE,
  162. LOC_CREFERENCE,
  163. LOC_MMREGISTER,
  164. LOC_CMMREGISTER:
  165. cg.a_parammm_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara,mms_movescalar);
  166. LOC_FPUREGISTER,
  167. LOC_CFPUREGISTER:
  168. begin
  169. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  170. cg.a_paramfpu_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara);
  171. end;
  172. else
  173. internalerror(200204249);
  174. end;
  175. LOC_FPUREGISTER,
  176. LOC_CFPUREGISTER:
  177. case tempcgpara.location^.loc of
  178. LOC_MMREGISTER,
  179. LOC_CMMREGISTER:
  180. begin
  181. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  182. cg.a_parammm_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara,mms_movescalar);
  183. end;
  184. {$ifdef x86_64}
  185. { x86_64 pushes s64comp in normal register }
  186. LOC_REGISTER,
  187. LOC_CREGISTER :
  188. begin
  189. location_force_mem(current_asmdata.CurrAsmList,left.location);
  190. { force integer size }
  191. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  192. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  193. end;
  194. {$endif x86_64}
  195. {$ifdef powerpc}
  196. LOC_REGISTER,
  197. LOC_CREGISTER :
  198. begin
  199. { aix abi passes floats of varargs in both fpu and }
  200. { integer registers }
  201. location_force_mem(current_asmdata.CurrAsmList,left.location);
  202. { force integer size }
  203. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  204. if (left.location.size in [OS_32,OS_S32]) then
  205. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  206. else
  207. cg64.a_param64_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  208. end;
  209. {$endif powerpc}
  210. {$ifdef powerpc64}
  211. LOC_REGISTER,
  212. LOC_CREGISTER :
  213. begin
  214. { ppc64 abi passes floats of varargs in integer registers, so force a store }
  215. location_force_mem(current_asmdata.CurrAsmList,left.location);
  216. { force integer size }
  217. left.location.size:=int_cgsize(tcgsize2size[tempcgpara.location^.size]);
  218. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  219. end;
  220. {$endif powerpc64}
  221. {$if defined(sparc) or defined(arm)}
  222. { sparc and arm pass floats in normal registers }
  223. LOC_REGISTER,
  224. LOC_CREGISTER,
  225. {$endif sparc}
  226. LOC_REFERENCE,
  227. LOC_CREFERENCE,
  228. LOC_FPUREGISTER,
  229. LOC_CFPUREGISTER:
  230. cg.a_paramfpu_reg(current_asmdata.CurrAsmList,left.location.size,left.location.register,tempcgpara);
  231. else
  232. internalerror(2002042433);
  233. end;
  234. LOC_REFERENCE,
  235. LOC_CREFERENCE:
  236. case tempcgpara.location^.loc of
  237. LOC_MMREGISTER,
  238. LOC_CMMREGISTER:
  239. cg.a_parammm_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara,mms_movescalar);
  240. {$ifdef x86_64}
  241. { x86_64 pushes s64comp in normal register }
  242. LOC_REGISTER,
  243. LOC_CREGISTER :
  244. begin
  245. { force integer size }
  246. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  247. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  248. end;
  249. {$endif x86_64}
  250. {$ifdef powerpc}
  251. { x86_64 pushes s64comp in normal register }
  252. LOC_REGISTER,
  253. LOC_CREGISTER :
  254. begin
  255. { force integer size }
  256. left.location.size:=int_cgsize(tcgsize2size[left.location.size]);
  257. if (left.location.size in [OS_32,OS_S32]) then
  258. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  259. else
  260. cg64.a_param64_ref(current_asmdata.CurrAsmList,left.location.reference,tempcgpara);
  261. end;
  262. {$endif powerpc}
  263. {$ifdef powerpc64}
  264. LOC_REGISTER,
  265. LOC_CREGISTER :
  266. begin
  267. { force integer size }
  268. left.location.size:=int_cgsize(tcgsize2size[tempcgpara.location^.size]);
  269. cg.a_param_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara)
  270. end;
  271. {$endif powerpc64}
  272. {$if defined(sparc) or defined(arm) }
  273. { sparc and arm pass floats in normal registers }
  274. LOC_REGISTER,
  275. LOC_CREGISTER,
  276. {$endif sparc}
  277. LOC_REFERENCE,
  278. LOC_CREFERENCE,
  279. LOC_FPUREGISTER,
  280. LOC_CFPUREGISTER:
  281. cg.a_paramfpu_ref(current_asmdata.CurrAsmList,left.location.size,left.location.reference,tempcgpara);
  282. else
  283. internalerror(2002042431);
  284. end;
  285. else
  286. internalerror(2002042432);
  287. end;
  288. {$endif i386}
  289. end
  290. else
  291. begin
  292. case left.location.loc of
  293. LOC_CONSTANT,
  294. LOC_REGISTER,
  295. LOC_CREGISTER,
  296. LOC_REFERENCE,
  297. LOC_CREFERENCE :
  298. begin
  299. {$ifndef cpu64bit}
  300. { use cg64 only for int64, not for 8 byte records }
  301. if is_64bit(left.resulttype.def) then
  302. cg64.a_param64_loc(current_asmdata.CurrAsmList,left.location,tempcgpara)
  303. else
  304. {$endif cpu64bit}
  305. begin
  306. {$ifndef cpu64bit}
  307. { Only a_param_ref supports multiple locations, when the
  308. value is still a const or in a register then write it
  309. to a reference first. This situation can be triggered
  310. by typecasting an int64 constant to a record of 8 bytes }
  311. if left.location.size in [OS_64,OS_S64] then
  312. location_force_mem(current_asmdata.CurrAsmList,left.location);
  313. {$endif cpu64bit}
  314. cg.a_param_loc(current_asmdata.CurrAsmList,left.location,tempcgpara);
  315. end;
  316. end;
  317. {$ifdef SUPPORT_MMX}
  318. LOC_MMXREGISTER,
  319. LOC_CMMXREGISTER:
  320. cg.a_parammm_reg(current_asmdata.CurrAsmList,OS_M64,left.location.register,tempcgpara,nil);
  321. {$endif SUPPORT_MMX}
  322. else
  323. internalerror(200204241);
  324. end;
  325. end;
  326. end;
  327. procedure tcgcallparanode.secondcallparan;
  328. var
  329. href : treference;
  330. otlabel,
  331. oflabel : tasmlabel;
  332. begin
  333. if not(assigned(parasym)) then
  334. internalerror(200304242);
  335. { Skip nothingn nodes which are used after disabling
  336. a parameter }
  337. if (left.nodetype<>nothingn) then
  338. begin
  339. otlabel:=current_procinfo.CurrTrueLabel;
  340. oflabel:=current_procinfo.CurrFalseLabel;
  341. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  342. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  343. secondpass(left);
  344. { release memory for refcnt out parameters }
  345. if (parasym.varspez=vs_out) and
  346. (left.resulttype.def.needs_inittable) then
  347. begin
  348. location_get_data_ref(current_asmdata.CurrAsmList,left.location,href,false);
  349. cg.g_decrrefcount(current_asmdata.CurrAsmList,left.resulttype.def,href);
  350. end;
  351. paramanager.createtempparaloc(current_asmdata.CurrAsmList,aktcallnode.procdefinition.proccalloption,parasym,tempcgpara);
  352. { handle varargs first, because parasym is not valid }
  353. if (cpf_varargs_para in callparaflags) then
  354. begin
  355. if paramanager.push_addr_param(vs_value,left.resulttype.def,
  356. aktcallnode.procdefinition.proccalloption) then
  357. push_addr_para
  358. else
  359. push_value_para;
  360. end
  361. { hidden parameters }
  362. else if (vo_is_hidden_para in parasym.varoptions) then
  363. begin
  364. { don't push a node that already generated a pointer type
  365. by address for implicit hidden parameters }
  366. if (vo_is_funcret in parasym.varoptions) or
  367. (not(left.resulttype.def.deftype in [pointerdef,classrefdef]) and
  368. paramanager.push_addr_param(parasym.varspez,parasym.vartype.def,
  369. aktcallnode.procdefinition.proccalloption)) then
  370. push_addr_para
  371. else
  372. push_value_para;
  373. end
  374. { formal def }
  375. else if (parasym.vartype.def.deftype=formaldef) then
  376. begin
  377. { allow passing of a constant to a const formaldef }
  378. if (parasym.varspez=vs_const) and
  379. (left.location.loc in [LOC_CONSTANT,LOC_REGISTER]) then
  380. location_force_mem(current_asmdata.CurrAsmList,left.location);
  381. push_addr_para;
  382. end
  383. { Normal parameter }
  384. else
  385. begin
  386. { don't push a node that already generated a pointer type
  387. by address for implicit hidden parameters }
  388. if (not(
  389. (vo_is_hidden_para in parasym.varoptions) and
  390. (left.resulttype.def.deftype in [pointerdef,classrefdef])
  391. ) and
  392. paramanager.push_addr_param(parasym.varspez,parasym.vartype.def,
  393. aktcallnode.procdefinition.proccalloption)) and
  394. { dyn. arrays passed to an array of const must be passed by value, see tests/webtbs/tw4219.pp }
  395. not(
  396. is_array_of_const(parasym.vartype.def) and
  397. is_dynamic_array(left.resulttype.def)
  398. ) then
  399. begin
  400. { Passing a var parameter to a var parameter, we can
  401. just push the address transparently }
  402. if (left.nodetype=loadn) and
  403. (tloadnode(left).is_addr_param_load) then
  404. begin
  405. if (left.location.reference.index<>NR_NO) or
  406. (left.location.reference.offset<>0) then
  407. internalerror(200410107);
  408. cg.a_param_reg(current_asmdata.CurrAsmList,OS_ADDR,left.location.reference.base,tempcgpara)
  409. end
  410. else
  411. begin
  412. { Force to be in memory }
  413. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  414. location_force_mem(current_asmdata.CurrAsmList,left.location);
  415. push_addr_para;
  416. end;
  417. end
  418. else
  419. push_value_para;
  420. end;
  421. current_procinfo.CurrTrueLabel:=otlabel;
  422. current_procinfo.CurrFalseLabel:=oflabel;
  423. { update return location in callnode when this is the function
  424. result }
  425. if assigned(parasym) and
  426. (vo_is_funcret in parasym.varoptions) then
  427. location_copy(aktcallnode.location,left.location);
  428. end;
  429. { next parameter }
  430. if assigned(right) then
  431. tcallparanode(right).secondcallparan;
  432. end;
  433. {*****************************************************************************
  434. TCGCALLNODE
  435. *****************************************************************************}
  436. procedure tcgcallnode.extra_interrupt_code;
  437. begin
  438. end;
  439. procedure tcgcallnode.extra_call_code;
  440. begin
  441. end;
  442. procedure tcgcallnode.extra_post_call_code;
  443. begin
  444. end;
  445. procedure tcgcallnode.pop_parasize(pop_size:longint);
  446. begin
  447. end;
  448. procedure tcgcallnode.handle_return_value;
  449. var
  450. cgsize : tcgsize;
  451. retloc : tlocation;
  452. hregister : tregister;
  453. tempnode : tnode;
  454. begin
  455. cgsize:=procdefinition.funcretloc[callerside].size;
  456. { structured results are easy to handle....
  457. needed also when result_no_used !! }
  458. if (procdefinition.proctypeoption<>potype_constructor) and
  459. paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  460. begin
  461. { Location should be setup by the funcret para }
  462. if location.loc<>LOC_REFERENCE then
  463. internalerror(200304241);
  464. end
  465. else
  466. { ansi/widestrings must be registered, so we can dispose them }
  467. if resulttype.def.needs_inittable then
  468. begin
  469. if procdefinition.funcretloc[callerside].loc<>LOC_REGISTER then
  470. internalerror(200409261);
  471. { the FUNCTION_RESULT_REG is already allocated }
  472. if getsupreg(procdefinition.funcretloc[callerside].register)<first_int_imreg then
  473. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  474. if not assigned(funcretnode) then
  475. begin
  476. { reg_ref could generate two instrcutions and allocate a register so we've to
  477. save the result first before releasing it }
  478. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  479. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,procdefinition.funcretloc[callerside].register,hregister);
  480. location_reset(location,LOC_REFERENCE,OS_ADDR);
  481. location.reference:=refcountedtemp;
  482. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  483. end
  484. else
  485. begin
  486. hregister := cg.getaddressregister(current_asmdata.CurrAsmList);
  487. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,procdefinition.funcretloc[callerside].register,hregister);
  488. { in case of a regular funcretnode with ret_in_param, the }
  489. { original funcretnode isn't touched -> make sure it's }
  490. { the same here (not sure if it's necessary) }
  491. tempnode := funcretnode.getcopy;
  492. tempnode.pass_2;
  493. location := tempnode.location;
  494. tempnode.free;
  495. cg.g_decrrefcount(current_asmdata.CurrAsmList,resulttype.def,location.reference);
  496. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  497. end;
  498. end
  499. else
  500. { normal (ordinal,float,pointer) result value }
  501. begin
  502. { we have only to handle the result if it is used }
  503. if (cnf_return_value_used in callnodeflags) then
  504. begin
  505. location.loc:=procdefinition.funcretloc[callerside].loc;
  506. case procdefinition.funcretloc[callerside].loc of
  507. LOC_FPUREGISTER:
  508. begin
  509. location_reset(location,LOC_FPUREGISTER,cgsize);
  510. location.register:=procdefinition.funcretloc[callerside].register;
  511. {$ifdef x86}
  512. tcgx86(cg).inc_fpu_stack;
  513. {$else x86}
  514. if getsupreg(procdefinition.funcretloc[callerside].register)<first_fpu_imreg then
  515. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  516. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  517. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,location.size,location.register,hregister);
  518. location.register:=hregister;
  519. {$endif x86}
  520. end;
  521. LOC_REGISTER:
  522. begin
  523. if cgsize<>OS_NO then
  524. begin
  525. location_reset(location,LOC_REGISTER,cgsize);
  526. {$ifndef cpu64bit}
  527. if cgsize in [OS_64,OS_S64] then
  528. begin
  529. retloc:=procdefinition.funcretloc[callerside];
  530. if retloc.loc<>LOC_REGISTER then
  531. internalerror(200409141);
  532. { the function result registers are already allocated }
  533. if getsupreg(retloc.register64.reglo)<first_int_imreg then
  534. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reglo);
  535. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  536. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reglo,location.register64.reglo);
  537. if getsupreg(retloc.register64.reghi)<first_int_imreg then
  538. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reghi);
  539. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  540. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reghi,location.register64.reghi);
  541. end
  542. else
  543. {$endif cpu64bit}
  544. begin
  545. { change register size after the unget because the
  546. getregister was done for the full register
  547. def_cgsize(resulttype.def) is used here because
  548. it could be a constructor call }
  549. if getsupreg(procdefinition.funcretloc[callerside].register)<first_int_imreg then
  550. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  551. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resulttype.def));
  552. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,def_cgsize(resulttype.def),procdefinition.funcretloc[callerside].register,location.register);
  553. end;
  554. end
  555. else
  556. begin
  557. if resulttype.def.size>0 then
  558. internalerror(200305131);
  559. end;
  560. end;
  561. LOC_MMREGISTER:
  562. begin
  563. location_reset(location,LOC_MMREGISTER,cgsize);
  564. if getsupreg(procdefinition.funcretloc[callerside].register)<first_mm_imreg then
  565. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  566. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,cgsize);
  567. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,procdefinition.funcretloc[callerside].register,location.register,mms_movescalar);
  568. end;
  569. else
  570. internalerror(200405023);
  571. end;
  572. end
  573. else
  574. begin
  575. {$ifdef x86}
  576. { release FPU stack }
  577. if procdefinition.funcretloc[callerside].loc=LOC_FPUREGISTER then
  578. emit_reg(A_FSTP,S_NO,NR_FPU_RESULT_REG);
  579. {$endif x86}
  580. if cgsize<>OS_NO then
  581. location_free(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside]);
  582. location_reset(location,LOC_VOID,OS_NO);
  583. end;
  584. end;
  585. { When the result is not used we need to finalize the result and
  586. can release the temp }
  587. if not(cnf_return_value_used in callnodeflags) then
  588. begin
  589. if location.loc=LOC_REFERENCE then
  590. begin
  591. if resulttype.def.needs_inittable then
  592. cg.g_finalize(current_asmdata.CurrAsmList,resulttype.def,location.reference);
  593. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference)
  594. end;
  595. end;
  596. end;
  597. procedure tcgcallnode.release_para_temps;
  598. var
  599. hp : tnode;
  600. ppn : tcallparanode;
  601. begin
  602. { Release temps from parameters }
  603. ppn:=tcallparanode(left);
  604. while assigned(ppn) do
  605. begin
  606. if assigned(ppn.left) then
  607. begin
  608. { don't release the funcret temp }
  609. if not(assigned(ppn.parasym)) or
  610. not(vo_is_funcret in ppn.parasym.varoptions) then
  611. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  612. { process also all nodes of an array of const }
  613. hp:=ppn.left;
  614. while (hp.nodetype=typeconvn) do
  615. hp:=ttypeconvnode(hp).left;
  616. if (hp.nodetype=arrayconstructorn) and
  617. assigned(tarrayconstructornode(hp).left) then
  618. begin
  619. while assigned(hp) do
  620. begin
  621. location_freetemp(current_asmdata.CurrAsmList,tarrayconstructornode(hp).left.location);
  622. hp:=tarrayconstructornode(hp).right;
  623. end;
  624. end;
  625. end;
  626. ppn:=tcallparanode(ppn.right);
  627. end;
  628. end;
  629. procedure tcgcallnode.pushparas;
  630. var
  631. ppn : tcgcallparanode;
  632. callerparaloc,
  633. tmpparaloc : pcgparalocation;
  634. sizeleft: aint;
  635. htempref,
  636. href : treference;
  637. begin
  638. { copy all resources to the allocated registers }
  639. ppn:=tcgcallparanode(left);
  640. while assigned(ppn) do
  641. begin
  642. if (ppn.left.nodetype<>nothingn) then
  643. begin
  644. { better check for the real location of the parameter here, when stack passed parameters
  645. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  646. }
  647. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.tempcgpara);
  648. tmpparaloc:=ppn.tempcgpara.location;
  649. sizeleft:=ppn.tempcgpara.intsize;
  650. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  651. while assigned(callerparaloc) do
  652. begin
  653. { Every paraloc must have a matching tmpparaloc }
  654. if not assigned(tmpparaloc) then
  655. internalerror(200408224);
  656. if callerparaloc^.size<>tmpparaloc^.size then
  657. internalerror(200408225);
  658. case callerparaloc^.loc of
  659. LOC_REGISTER:
  660. begin
  661. if tmpparaloc^.loc<>LOC_REGISTER then
  662. internalerror(200408221);
  663. if getsupreg(callerparaloc^.register)<first_int_imreg then
  664. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  665. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  666. tmpparaloc^.register,callerparaloc^.register);
  667. end;
  668. LOC_FPUREGISTER:
  669. begin
  670. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  671. internalerror(200408222);
  672. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  673. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  674. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  675. end;
  676. LOC_MMREGISTER:
  677. begin
  678. if tmpparaloc^.loc<>LOC_MMREGISTER then
  679. internalerror(200408223);
  680. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  681. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  682. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  683. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  684. end;
  685. LOC_REFERENCE:
  686. begin
  687. if use_fixed_stack then
  688. begin
  689. { Can't have a data copied to the stack, every location
  690. must contain a valid size field }
  691. if (ppn.tempcgpara.size=OS_NO) and
  692. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  693. assigned(tmpparaloc^.next)) then
  694. internalerror(200501281);
  695. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset);
  696. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  697. case tmpparaloc^.loc of
  698. LOC_REFERENCE:
  699. begin
  700. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset);
  701. { use concatcopy, because it can also be a float which fails when
  702. load_ref_ref is used }
  703. if (ppn.tempcgpara.size <> OS_NO) then
  704. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  705. else
  706. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  707. end;
  708. LOC_REGISTER:
  709. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  710. LOC_FPUREGISTER:
  711. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.register,href);
  712. LOC_MMREGISTER:
  713. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  714. else
  715. internalerror(200402081);
  716. end;
  717. end;
  718. end;
  719. end;
  720. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  721. callerparaloc:=callerparaloc^.next;
  722. tmpparaloc:=tmpparaloc^.next;
  723. end;
  724. end;
  725. ppn:=tcgcallparanode(ppn.right);
  726. end;
  727. end;
  728. procedure tcgcallnode.freeparas;
  729. var
  730. ppn : tcgcallparanode;
  731. begin
  732. { free the resources allocated for the parameters }
  733. ppn:=tcgcallparanode(left);
  734. while assigned(ppn) do
  735. begin
  736. if (ppn.left.nodetype<>nothingn) then
  737. begin
  738. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  739. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  740. end;
  741. ppn:=tcgcallparanode(ppn.right);
  742. end;
  743. end;
  744. procedure tcgcallnode.pass_2;
  745. var
  746. regs_to_save_int,
  747. regs_to_save_fpu,
  748. regs_to_save_mm : Tcpuregisterset;
  749. href : treference;
  750. pop_size : longint;
  751. vmtoffset : aint;
  752. pvreg,
  753. vmtreg : tregister;
  754. oldaktcallnode : tcallnode;
  755. sym : tasmsymbol;
  756. begin
  757. if not assigned(procdefinition) or
  758. not procdefinition.has_paraloc_info then
  759. internalerror(200305264);
  760. if assigned(methodpointerinit) then
  761. secondpass(methodpointerinit);
  762. if resulttype.def.needs_inittable and
  763. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  764. not assigned(funcretnode) then
  765. begin
  766. tg.gettemptyped(current_asmdata.CurrAsmList,resulttype.def,tt_normal,refcountedtemp);
  767. cg.g_decrrefcount(current_asmdata.CurrAsmList,resulttype.def,refcountedtemp);
  768. end;
  769. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  770. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  771. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  772. { Include Function result registers }
  773. if (not is_void(resulttype.def)) then
  774. begin
  775. case procdefinition.funcretloc[callerside].loc of
  776. LOC_REGISTER,
  777. LOC_CREGISTER:
  778. include(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  779. LOC_FPUREGISTER,
  780. LOC_CFPUREGISTER:
  781. include(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  782. LOC_MMREGISTER,
  783. LOC_CMMREGISTER:
  784. include(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  785. LOC_REFERENCE,
  786. LOC_VOID:
  787. ;
  788. else
  789. internalerror(2004110213);
  790. end;
  791. end;
  792. { Process parameters, register parameters will be loaded
  793. in imaginary registers. The actual load to the correct
  794. register is done just before the call }
  795. oldaktcallnode:=aktcallnode;
  796. aktcallnode:=self;
  797. if assigned(left) then
  798. tcallparanode(left).secondcallparan;
  799. aktcallnode:=oldaktcallnode;
  800. { procedure variable or normal function call ? }
  801. if (right=nil) then
  802. begin
  803. { When methodpointer is typen we don't need (and can't) load
  804. a pointer. We can directly call the correct procdef (PFV) }
  805. if (po_virtualmethod in procdefinition.procoptions) and
  806. assigned(methodpointer) and
  807. (methodpointer.nodetype<>typen) then
  808. begin
  809. { virtual methods require an index }
  810. if tprocdef(procdefinition).extnumber=$ffff then
  811. internalerror(200304021);
  812. secondpass(methodpointer);
  813. { Load VMT from self }
  814. if methodpointer.resulttype.def.deftype=objectdef then
  815. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resulttype.def),methodpointer.location,vmtreg)
  816. else
  817. begin
  818. { Load VMT value in register }
  819. location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,OS_ADDR,false);
  820. vmtreg:=methodpointer.location.register;
  821. end;
  822. { test validity of VMT }
  823. if not(is_interface(tprocdef(procdefinition)._class)) and
  824. not(is_cppclass(tprocdef(procdefinition)._class)) then
  825. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tprocdef(procdefinition)._class);
  826. { Call through VMT, generate a VTREF symbol to notify the linker }
  827. vmtoffset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  828. if not is_interface(tprocdef(procdefinition)._class) then
  829. begin
  830. inc(current_asmdata.NextVTEntryNr);
  831. current_asmdata.CurrAsmList.Concat(tai_symbol.CreateName('VTREF'+tostr(current_asmdata.NextVTEntryNr)+'_'+tprocdef(procdefinition)._class.vmt_mangledname+'$$'+tostr(vmtoffset div sizeof(aint)),AT_FUNCTION,0));
  832. end;
  833. reference_reset_base(href,vmtreg,vmtoffset);
  834. { Load parameters that are in temporary registers in the
  835. correct parameter register }
  836. if assigned(left) then
  837. begin
  838. pushparas;
  839. { free the resources allocated for the parameters }
  840. freeparas;
  841. end;
  842. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  843. if cg.uses_registers(R_FPUREGISTER) then
  844. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  845. if cg.uses_registers(R_MMREGISTER) then
  846. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  847. { call method }
  848. extra_call_code;
  849. cg.a_call_ref(current_asmdata.CurrAsmList,href);
  850. extra_post_call_code;
  851. end
  852. else
  853. begin
  854. { Load parameters that are in temporary registers in the
  855. correct parameter register }
  856. if assigned(left) then
  857. begin
  858. pushparas;
  859. { free the resources allocated for the parameters }
  860. freeparas;
  861. end;
  862. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  863. if cg.uses_registers(R_FPUREGISTER) then
  864. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  865. if cg.uses_registers(R_MMREGISTER) then
  866. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  867. if procdefinition.proccalloption=pocall_syscall then
  868. do_syscall
  869. else
  870. begin
  871. { Calling interrupt from the same code requires some
  872. extra code }
  873. if (po_interrupt in procdefinition.procoptions) then
  874. extra_interrupt_code;
  875. extra_call_code;
  876. cg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition).mangledname);
  877. extra_post_call_code;
  878. end;
  879. end;
  880. end
  881. else
  882. { now procedure variable case }
  883. begin
  884. secondpass(right);
  885. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  886. { Only load OS_ADDR from the reference }
  887. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  888. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  889. else
  890. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,right.location,pvreg);
  891. location_freetemp(current_asmdata.CurrAsmList,right.location);
  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. { Calling interrupt from the same code requires some
  906. extra code }
  907. if (po_interrupt in procdefinition.procoptions) then
  908. extra_interrupt_code;
  909. extra_call_code;
  910. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  911. extra_post_call_code;
  912. end;
  913. { Need to remove the parameters from the stack? }
  914. if (procdefinition.proccalloption in clearstack_pocalls) then
  915. begin
  916. pop_size:=pushedparasize;
  917. { for Cdecl functions we don't need to pop the funcret when it
  918. was pushed by para }
  919. if paramanager.ret_in_param(procdefinition.rettype.def,procdefinition.proccalloption) then
  920. dec(pop_size,sizeof(aint));
  921. { Remove parameters/alignment from the stack }
  922. pop_parasize(pop_size);
  923. end;
  924. { Release registers, but not the registers that contain the
  925. function result }
  926. if (not is_void(resulttype.def)) then
  927. begin
  928. case procdefinition.funcretloc[callerside].loc of
  929. LOC_REGISTER,
  930. LOC_CREGISTER:
  931. begin
  932. {$ifndef cpu64bit}
  933. if procdefinition.funcretloc[callerside].size in [OS_64,OS_S64] then
  934. begin
  935. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reghi));
  936. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reglo));
  937. end
  938. else
  939. {$endif cpu64bit}
  940. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  941. end;
  942. LOC_FPUREGISTER,
  943. LOC_CFPUREGISTER:
  944. exclude(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  945. LOC_MMREGISTER,
  946. LOC_CMMREGISTER:
  947. exclude(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  948. LOC_REFERENCE,
  949. LOC_VOID:
  950. ;
  951. else
  952. internalerror(2004110214);
  953. end;
  954. end;
  955. {$if defined(x86) or defined(arm)}
  956. if procdefinition.proccalloption=pocall_safecall then
  957. begin
  958. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  959. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_SAFECALLCHECK');
  960. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  961. end;
  962. {$endif}
  963. if cg.uses_registers(R_MMREGISTER) then
  964. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  965. if cg.uses_registers(R_FPUREGISTER) then
  966. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  967. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  968. { handle function results }
  969. if (not is_void(resulttype.def)) then
  970. handle_return_value
  971. else
  972. location_reset(location,LOC_VOID,OS_NO);
  973. { perhaps i/o check ? }
  974. if (cs_check_io in aktlocalswitches) and
  975. (po_iocheck in procdefinition.procoptions) and
  976. not(po_iocheck in current_procinfo.procdef.procoptions) and
  977. { no IO check for methods and procedure variables }
  978. (right=nil) and
  979. not(po_virtualmethod in procdefinition.procoptions) then
  980. begin
  981. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  982. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK');
  983. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  984. end;
  985. { release temps of paras }
  986. release_para_temps;
  987. if assigned(methodpointerdone) then
  988. secondpass(methodpointerdone);
  989. end;
  990. begin
  991. ccallparanode:=tcgcallparanode;
  992. ccallnode:=tcgcallnode;
  993. end.