ncgcal.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. { Check for passing a constant to var,out parameter }
  413. if (parasym.varspez in [vs_var,vs_out]) and
  414. (left.location.loc<>LOC_REFERENCE) then
  415. begin
  416. { passing self to a var parameter is allowed in
  417. TP and delphi }
  418. if not((left.location.loc=LOC_CREFERENCE) and
  419. is_self_node(left)) then
  420. internalerror(200106041);
  421. end;
  422. { Force to be in memory }
  423. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  424. location_force_mem(current_asmdata.CurrAsmList,left.location);
  425. push_addr_para;
  426. end;
  427. end
  428. else
  429. push_value_para;
  430. end;
  431. current_procinfo.CurrTrueLabel:=otlabel;
  432. current_procinfo.CurrFalseLabel:=oflabel;
  433. { update return location in callnode when this is the function
  434. result }
  435. if assigned(parasym) and
  436. (vo_is_funcret in parasym.varoptions) then
  437. location_copy(aktcallnode.location,left.location);
  438. end;
  439. { next parameter }
  440. if assigned(right) then
  441. tcallparanode(right).secondcallparan;
  442. end;
  443. {*****************************************************************************
  444. TCGCALLNODE
  445. *****************************************************************************}
  446. procedure tcgcallnode.extra_interrupt_code;
  447. begin
  448. end;
  449. procedure tcgcallnode.extra_call_code;
  450. begin
  451. end;
  452. procedure tcgcallnode.extra_post_call_code;
  453. begin
  454. end;
  455. procedure tcgcallnode.pop_parasize(pop_size:longint);
  456. begin
  457. end;
  458. procedure tcgcallnode.handle_return_value;
  459. var
  460. cgsize : tcgsize;
  461. retloc : tlocation;
  462. hregister : tregister;
  463. tempnode : tnode;
  464. begin
  465. cgsize:=procdefinition.funcretloc[callerside].size;
  466. { structured results are easy to handle....
  467. needed also when result_no_used !! }
  468. if (procdefinition.proctypeoption<>potype_constructor) and
  469. paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  470. begin
  471. { Location should be setup by the funcret para }
  472. if location.loc<>LOC_REFERENCE then
  473. internalerror(200304241);
  474. end
  475. else
  476. { ansi/widestrings must be registered, so we can dispose them }
  477. if resulttype.def.needs_inittable then
  478. begin
  479. if procdefinition.funcretloc[callerside].loc<>LOC_REGISTER then
  480. internalerror(200409261);
  481. { the FUNCTION_RESULT_REG is already allocated }
  482. if getsupreg(procdefinition.funcretloc[callerside].register)<first_int_imreg then
  483. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  484. if not assigned(funcretnode) then
  485. begin
  486. { reg_ref could generate two instrcutions and allocate a register so we've to
  487. save the result first before releasing it }
  488. hregister:=cg.getaddressregister(current_asmdata.CurrAsmList);
  489. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,procdefinition.funcretloc[callerside].register,hregister);
  490. location_reset(location,LOC_REFERENCE,OS_ADDR);
  491. location.reference:=refcountedtemp;
  492. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  493. end
  494. else
  495. begin
  496. hregister := cg.getaddressregister(current_asmdata.CurrAsmList);
  497. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,procdefinition.funcretloc[callerside].register,hregister);
  498. { in case of a regular funcretnode with ret_in_param, the }
  499. { original funcretnode isn't touched -> make sure it's }
  500. { the same here (not sure if it's necessary) }
  501. tempnode := funcretnode.getcopy;
  502. tempnode.pass_2;
  503. location := tempnode.location;
  504. tempnode.free;
  505. cg.g_decrrefcount(current_asmdata.CurrAsmList,resulttype.def,location.reference);
  506. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,hregister,location.reference);
  507. end;
  508. end
  509. else
  510. { normal (ordinal,float,pointer) result value }
  511. begin
  512. { we have only to handle the result if it is used }
  513. if (cnf_return_value_used in callnodeflags) then
  514. begin
  515. location.loc:=procdefinition.funcretloc[callerside].loc;
  516. case procdefinition.funcretloc[callerside].loc of
  517. LOC_FPUREGISTER:
  518. begin
  519. location_reset(location,LOC_FPUREGISTER,cgsize);
  520. location.register:=procdefinition.funcretloc[callerside].register;
  521. {$ifdef x86}
  522. tcgx86(cg).inc_fpu_stack;
  523. {$else x86}
  524. if getsupreg(procdefinition.funcretloc[callerside].register)<first_fpu_imreg then
  525. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  526. hregister:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  527. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,location.size,location.register,hregister);
  528. location.register:=hregister;
  529. {$endif x86}
  530. end;
  531. LOC_REGISTER:
  532. begin
  533. if cgsize<>OS_NO then
  534. begin
  535. location_reset(location,LOC_REGISTER,cgsize);
  536. {$ifndef cpu64bit}
  537. if cgsize in [OS_64,OS_S64] then
  538. begin
  539. retloc:=procdefinition.funcretloc[callerside];
  540. if retloc.loc<>LOC_REGISTER then
  541. internalerror(200409141);
  542. { the function result registers are already allocated }
  543. if getsupreg(retloc.register64.reglo)<first_int_imreg then
  544. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reglo);
  545. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  546. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reglo,location.register64.reglo);
  547. if getsupreg(retloc.register64.reghi)<first_int_imreg then
  548. cg.ungetcpuregister(current_asmdata.CurrAsmList,retloc.register64.reghi);
  549. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  550. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,retloc.register64.reghi,location.register64.reghi);
  551. end
  552. else
  553. {$endif cpu64bit}
  554. begin
  555. { change register size after the unget because the
  556. getregister was done for the full register
  557. def_cgsize(resulttype.def) is used here because
  558. it could be a constructor call }
  559. if getsupreg(procdefinition.funcretloc[callerside].register)<first_int_imreg then
  560. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  561. location.register:=cg.getintregister(current_asmdata.CurrAsmList,def_cgsize(resulttype.def));
  562. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,def_cgsize(resulttype.def),procdefinition.funcretloc[callerside].register,location.register);
  563. end;
  564. end
  565. else
  566. begin
  567. if resulttype.def.size>0 then
  568. internalerror(200305131);
  569. end;
  570. end;
  571. LOC_MMREGISTER:
  572. begin
  573. location_reset(location,LOC_MMREGISTER,cgsize);
  574. if getsupreg(procdefinition.funcretloc[callerside].register)<first_mm_imreg then
  575. cg.ungetcpuregister(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside].register);
  576. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,cgsize);
  577. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,procdefinition.funcretloc[callerside].register,location.register,mms_movescalar);
  578. end;
  579. else
  580. internalerror(200405023);
  581. end;
  582. end
  583. else
  584. begin
  585. {$ifdef x86}
  586. { release FPU stack }
  587. if procdefinition.funcretloc[callerside].loc=LOC_FPUREGISTER then
  588. emit_reg(A_FSTP,S_NO,NR_FPU_RESULT_REG);
  589. {$endif x86}
  590. if cgsize<>OS_NO then
  591. location_free(current_asmdata.CurrAsmList,procdefinition.funcretloc[callerside]);
  592. location_reset(location,LOC_VOID,OS_NO);
  593. end;
  594. end;
  595. { When the result is not used we need to finalize the result and
  596. can release the temp }
  597. if not(cnf_return_value_used in callnodeflags) then
  598. begin
  599. if location.loc=LOC_REFERENCE then
  600. begin
  601. if resulttype.def.needs_inittable then
  602. cg.g_finalize(current_asmdata.CurrAsmList,resulttype.def,location.reference);
  603. tg.ungetiftemp(current_asmdata.CurrAsmList,location.reference)
  604. end;
  605. end;
  606. end;
  607. procedure tcgcallnode.release_para_temps;
  608. var
  609. hp : tnode;
  610. ppn : tcallparanode;
  611. begin
  612. { Release temps from parameters }
  613. ppn:=tcallparanode(left);
  614. while assigned(ppn) do
  615. begin
  616. if assigned(ppn.left) then
  617. begin
  618. { don't release the funcret temp }
  619. if not(assigned(ppn.parasym)) or
  620. not(vo_is_funcret in ppn.parasym.varoptions) then
  621. location_freetemp(current_asmdata.CurrAsmList,ppn.left.location);
  622. { process also all nodes of an array of const }
  623. hp:=ppn.left;
  624. while (hp.nodetype=typeconvn) do
  625. hp:=ttypeconvnode(hp).left;
  626. if (hp.nodetype=arrayconstructorn) and
  627. assigned(tarrayconstructornode(hp).left) then
  628. begin
  629. while assigned(hp) do
  630. begin
  631. location_freetemp(current_asmdata.CurrAsmList,tarrayconstructornode(hp).left.location);
  632. hp:=tarrayconstructornode(hp).right;
  633. end;
  634. end;
  635. end;
  636. ppn:=tcallparanode(ppn.right);
  637. end;
  638. end;
  639. procedure tcgcallnode.pushparas;
  640. var
  641. ppn : tcgcallparanode;
  642. callerparaloc,
  643. tmpparaloc : pcgparalocation;
  644. sizeleft: aint;
  645. htempref,
  646. href : treference;
  647. begin
  648. { copy all resources to the allocated registers }
  649. ppn:=tcgcallparanode(left);
  650. while assigned(ppn) do
  651. begin
  652. if (ppn.left.nodetype<>nothingn) then
  653. begin
  654. { better check for the real location of the parameter here, when stack passed parameters
  655. are saved temporary in registers, checking for the tmpparaloc.loc is wrong
  656. }
  657. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.tempcgpara);
  658. tmpparaloc:=ppn.tempcgpara.location;
  659. sizeleft:=ppn.tempcgpara.intsize;
  660. callerparaloc:=ppn.parasym.paraloc[callerside].location;
  661. while assigned(callerparaloc) do
  662. begin
  663. { Every paraloc must have a matching tmpparaloc }
  664. if not assigned(tmpparaloc) then
  665. internalerror(200408224);
  666. if callerparaloc^.size<>tmpparaloc^.size then
  667. internalerror(200408225);
  668. case callerparaloc^.loc of
  669. LOC_REGISTER:
  670. begin
  671. if tmpparaloc^.loc<>LOC_REGISTER then
  672. internalerror(200408221);
  673. if getsupreg(callerparaloc^.register)<first_int_imreg then
  674. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  675. cg.a_load_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  676. tmpparaloc^.register,callerparaloc^.register);
  677. end;
  678. LOC_FPUREGISTER:
  679. begin
  680. if tmpparaloc^.loc<>LOC_FPUREGISTER then
  681. internalerror(200408222);
  682. if getsupreg(callerparaloc^.register)<first_fpu_imreg then
  683. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  684. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,ppn.tempcgpara.size,tmpparaloc^.register,callerparaloc^.register);
  685. end;
  686. LOC_MMREGISTER:
  687. begin
  688. if tmpparaloc^.loc<>LOC_MMREGISTER then
  689. internalerror(200408223);
  690. if getsupreg(callerparaloc^.register)<first_mm_imreg then
  691. cg.getcpuregister(current_asmdata.CurrAsmList,callerparaloc^.register);
  692. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,
  693. tmpparaloc^.register,callerparaloc^.register,mms_movescalar);
  694. end;
  695. LOC_REFERENCE:
  696. begin
  697. if use_fixed_stack then
  698. begin
  699. { Can't have a data copied to the stack, every location
  700. must contain a valid size field }
  701. if (ppn.tempcgpara.size=OS_NO) and
  702. ((tmpparaloc^.loc<>LOC_REFERENCE) or
  703. assigned(tmpparaloc^.next)) then
  704. internalerror(200501281);
  705. reference_reset_base(href,callerparaloc^.reference.index,callerparaloc^.reference.offset);
  706. { copy parameters in case they were moved to a temp. location because we've a fixed stack }
  707. case tmpparaloc^.loc of
  708. LOC_REFERENCE:
  709. begin
  710. reference_reset_base(htempref,tmpparaloc^.reference.index,tmpparaloc^.reference.offset);
  711. { use concatcopy, because it can also be a float which fails when
  712. load_ref_ref is used }
  713. if (ppn.tempcgpara.size <> OS_NO) then
  714. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,tcgsize2size[tmpparaloc^.size])
  715. else
  716. cg.g_concatcopy(current_asmdata.CurrAsmList,htempref,href,sizeleft)
  717. end;
  718. LOC_REGISTER:
  719. cg.a_load_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href);
  720. LOC_FPUREGISTER:
  721. cg.a_loadfpu_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.register,href);
  722. LOC_MMREGISTER:
  723. cg.a_loadmm_reg_ref(current_asmdata.CurrAsmList,tmpparaloc^.size,tmpparaloc^.size,tmpparaloc^.register,href,mms_movescalar);
  724. else
  725. internalerror(200402081);
  726. end;
  727. end;
  728. end;
  729. end;
  730. dec(sizeleft,tcgsize2size[tmpparaloc^.size]);
  731. callerparaloc:=callerparaloc^.next;
  732. tmpparaloc:=tmpparaloc^.next;
  733. end;
  734. end;
  735. ppn:=tcgcallparanode(ppn.right);
  736. end;
  737. end;
  738. procedure tcgcallnode.freeparas;
  739. var
  740. ppn : tcgcallparanode;
  741. begin
  742. { free the resources allocated for the parameters }
  743. ppn:=tcgcallparanode(left);
  744. while assigned(ppn) do
  745. begin
  746. if (ppn.left.nodetype<>nothingn) then
  747. begin
  748. if (ppn.parasym.paraloc[callerside].location^.loc <> LOC_REFERENCE) then
  749. paramanager.freeparaloc(current_asmdata.CurrAsmList,ppn.parasym.paraloc[callerside]);
  750. end;
  751. ppn:=tcgcallparanode(ppn.right);
  752. end;
  753. end;
  754. procedure tcgcallnode.pass_2;
  755. var
  756. regs_to_save_int,
  757. regs_to_save_fpu,
  758. regs_to_save_mm : Tcpuregisterset;
  759. href : treference;
  760. pop_size : longint;
  761. vmtoffset : aint;
  762. pvreg,
  763. vmtreg : tregister;
  764. oldaktcallnode : tcallnode;
  765. sym : tasmsymbol;
  766. begin
  767. if not assigned(procdefinition) or
  768. not procdefinition.has_paraloc_info then
  769. internalerror(200305264);
  770. if assigned(methodpointerinit) then
  771. secondpass(methodpointerinit);
  772. if resulttype.def.needs_inittable and
  773. not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) and
  774. not assigned(funcretnode) then
  775. begin
  776. tg.gettemptyped(current_asmdata.CurrAsmList,resulttype.def,tt_normal,refcountedtemp);
  777. cg.g_decrrefcount(current_asmdata.CurrAsmList,resulttype.def,refcountedtemp);
  778. end;
  779. regs_to_save_int:=paramanager.get_volatile_registers_int(procdefinition.proccalloption);
  780. regs_to_save_fpu:=paramanager.get_volatile_registers_fpu(procdefinition.proccalloption);
  781. regs_to_save_mm:=paramanager.get_volatile_registers_mm(procdefinition.proccalloption);
  782. { Include Function result registers }
  783. if (not is_void(resulttype.def)) then
  784. begin
  785. case procdefinition.funcretloc[callerside].loc of
  786. LOC_REGISTER,
  787. LOC_CREGISTER:
  788. include(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  789. LOC_FPUREGISTER,
  790. LOC_CFPUREGISTER:
  791. include(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  792. LOC_MMREGISTER,
  793. LOC_CMMREGISTER:
  794. include(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  795. LOC_REFERENCE,
  796. LOC_VOID:
  797. ;
  798. else
  799. internalerror(2004110213);
  800. end;
  801. end;
  802. { Process parameters, register parameters will be loaded
  803. in imaginary registers. The actual load to the correct
  804. register is done just before the call }
  805. oldaktcallnode:=aktcallnode;
  806. aktcallnode:=self;
  807. if assigned(left) then
  808. tcallparanode(left).secondcallparan;
  809. aktcallnode:=oldaktcallnode;
  810. { procedure variable or normal function call ? }
  811. if (right=nil) then
  812. begin
  813. { When methodpointer is typen we don't need (and can't) load
  814. a pointer. We can directly call the correct procdef (PFV) }
  815. if (po_virtualmethod in procdefinition.procoptions) and
  816. assigned(methodpointer) and
  817. (methodpointer.nodetype<>typen) then
  818. begin
  819. { virtual methods require an index }
  820. if tprocdef(procdefinition).extnumber=$ffff then
  821. internalerror(200304021);
  822. secondpass(methodpointer);
  823. { Load VMT from self }
  824. if methodpointer.resulttype.def.deftype=objectdef then
  825. gen_load_vmt_register(current_asmdata.CurrAsmList,tobjectdef(methodpointer.resulttype.def),methodpointer.location,vmtreg)
  826. else
  827. begin
  828. { Load VMT value in register }
  829. location_force_reg(current_asmdata.CurrAsmList,methodpointer.location,OS_ADDR,false);
  830. vmtreg:=methodpointer.location.register;
  831. end;
  832. { test validity of VMT }
  833. if not(is_interface(tprocdef(procdefinition)._class)) and
  834. not(is_cppclass(tprocdef(procdefinition)._class)) then
  835. cg.g_maybe_testvmt(current_asmdata.CurrAsmList,vmtreg,tprocdef(procdefinition)._class);
  836. { Call through VMT, generate a VTREF symbol to notify the linker }
  837. vmtoffset:=tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber);
  838. if not is_interface(tprocdef(procdefinition)._class) then
  839. begin
  840. inc(current_asmdata.NextVTEntryNr);
  841. 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));
  842. end;
  843. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  844. reference_reset_base(href,vmtreg,vmtoffset);
  845. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,href,pvreg);
  846. { Load parameters that are in temporary registers in the
  847. correct parameter register }
  848. if assigned(left) then
  849. begin
  850. pushparas;
  851. { free the resources allocated for the parameters }
  852. freeparas;
  853. end;
  854. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  855. if cg.uses_registers(R_FPUREGISTER) then
  856. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  857. if cg.uses_registers(R_MMREGISTER) then
  858. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  859. { call method }
  860. extra_call_code;
  861. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  862. extra_post_call_code;
  863. end
  864. else
  865. begin
  866. { Load parameters that are in temporary registers in the
  867. correct parameter register }
  868. if assigned(left) then
  869. begin
  870. pushparas;
  871. { free the resources allocated for the parameters }
  872. freeparas;
  873. end;
  874. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  875. if cg.uses_registers(R_FPUREGISTER) then
  876. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  877. if cg.uses_registers(R_MMREGISTER) then
  878. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  879. if procdefinition.proccalloption=pocall_syscall then
  880. do_syscall
  881. else
  882. begin
  883. { Calling interrupt from the same code requires some
  884. extra code }
  885. if (po_interrupt in procdefinition.procoptions) then
  886. extra_interrupt_code;
  887. extra_call_code;
  888. cg.a_call_name(current_asmdata.CurrAsmList,tprocdef(procdefinition).mangledname);
  889. extra_post_call_code;
  890. end;
  891. end;
  892. end
  893. else
  894. { now procedure variable case }
  895. begin
  896. secondpass(right);
  897. pvreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  898. { Only load OS_ADDR from the reference }
  899. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  900. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,right.location.reference,pvreg)
  901. else
  902. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_ADDR,right.location,pvreg);
  903. location_freetemp(current_asmdata.CurrAsmList,right.location);
  904. { Load parameters that are in temporary registers in the
  905. correct parameter register }
  906. if assigned(left) then
  907. begin
  908. pushparas;
  909. { free the resources allocated for the parameters }
  910. freeparas;
  911. end;
  912. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  913. if cg.uses_registers(R_FPUREGISTER) then
  914. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  915. if cg.uses_registers(R_MMREGISTER) then
  916. cg.alloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  917. { Calling interrupt from the same code requires some
  918. extra code }
  919. if (po_interrupt in procdefinition.procoptions) then
  920. extra_interrupt_code;
  921. extra_call_code;
  922. cg.a_call_reg(current_asmdata.CurrAsmList,pvreg);
  923. extra_post_call_code;
  924. end;
  925. { Need to remove the parameters from the stack? }
  926. if (procdefinition.proccalloption in clearstack_pocalls) then
  927. begin
  928. pop_size:=pushedparasize;
  929. { for Cdecl functions we don't need to pop the funcret when it
  930. was pushed by para }
  931. if paramanager.ret_in_param(procdefinition.rettype.def,procdefinition.proccalloption) then
  932. dec(pop_size,sizeof(aint));
  933. { Remove parameters/alignment from the stack }
  934. pop_parasize(pop_size);
  935. end;
  936. { Release registers, but not the registers that contain the
  937. function result }
  938. if (not is_void(resulttype.def)) then
  939. begin
  940. case procdefinition.funcretloc[callerside].loc of
  941. LOC_REGISTER,
  942. LOC_CREGISTER:
  943. begin
  944. {$ifndef cpu64bit}
  945. if procdefinition.funcretloc[callerside].size in [OS_64,OS_S64] then
  946. begin
  947. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reghi));
  948. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register64.reglo));
  949. end
  950. else
  951. {$endif cpu64bit}
  952. exclude(regs_to_save_int,getsupreg(procdefinition.funcretloc[callerside].register));
  953. end;
  954. LOC_FPUREGISTER,
  955. LOC_CFPUREGISTER:
  956. exclude(regs_to_save_fpu,getsupreg(procdefinition.funcretloc[callerside].register));
  957. LOC_MMREGISTER,
  958. LOC_CMMREGISTER:
  959. exclude(regs_to_save_mm,getsupreg(procdefinition.funcretloc[callerside].register));
  960. LOC_REFERENCE,
  961. LOC_VOID:
  962. ;
  963. else
  964. internalerror(2004110214);
  965. end;
  966. end;
  967. if cg.uses_registers(R_MMREGISTER) then
  968. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_MMREGISTER,regs_to_save_mm);
  969. if cg.uses_registers(R_FPUREGISTER) then
  970. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_FPUREGISTER,regs_to_save_fpu);
  971. cg.dealloccpuregisters(current_asmdata.CurrAsmList,R_INTREGISTER,regs_to_save_int);
  972. { handle function results }
  973. if (not is_void(resulttype.def)) then
  974. handle_return_value
  975. else
  976. location_reset(location,LOC_VOID,OS_NO);
  977. { perhaps i/o check ? }
  978. if (cs_check_io in aktlocalswitches) and
  979. (po_iocheck in procdefinition.procoptions) and
  980. not(po_iocheck in current_procinfo.procdef.procoptions) and
  981. { no IO check for methods and procedure variables }
  982. (right=nil) and
  983. not(po_virtualmethod in procdefinition.procoptions) then
  984. begin
  985. cg.allocallcpuregisters(current_asmdata.CurrAsmList);
  986. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_IOCHECK');
  987. cg.deallocallcpuregisters(current_asmdata.CurrAsmList);
  988. end;
  989. { release temps of paras }
  990. release_para_temps;
  991. if assigned(methodpointerdone) then
  992. secondpass(methodpointerdone);
  993. end;
  994. begin
  995. ccallparanode:=tcgcallparanode;
  996. ccallnode:=tcgcallnode;
  997. end.