ncgcal.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 assembler for in call nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published bymethodpointer
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgcal;
  19. {$i fpcdefs.inc}
  20. interface
  21. { $define AnsiStrRef}
  22. uses
  23. cpubase,
  24. globtype,
  25. symdef,node,ncal;
  26. type
  27. tcgcallparanode = class(tcallparanode)
  28. procedure secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;
  29. para_alignment,para_offset : longint);override;
  30. end;
  31. tcgcallnode = class(tcallnode)
  32. private
  33. procedure release_para_temps;
  34. procedure normal_pass_2;
  35. procedure inlined_pass_2;
  36. protected
  37. refcountedtemp : treference;
  38. procedure handle_return_value;
  39. {# This routine is used to push the current frame pointer
  40. on the stack. This is used in nested routines where the
  41. value of the frame pointer is always pushed as an extra
  42. parameter.
  43. The default handling is the standard handling used on
  44. most stack based machines, where the frame pointer is
  45. the first invisible parameter.
  46. }
  47. function align_parasize:longint;virtual;
  48. procedure pop_parasize(pop_size:longint);virtual;
  49. procedure push_framepointer;virtual;
  50. procedure extra_interrupt_code;virtual;
  51. public
  52. procedure pass_2;override;
  53. end;
  54. implementation
  55. uses
  56. systems,
  57. cutils,verbose,globals,
  58. symconst,symbase,symsym,symtable,defutil,paramgr,
  59. {$ifdef GDB}
  60. {$ifdef delphi}
  61. sysutils,
  62. {$else}
  63. strings,
  64. {$endif}
  65. gdb,
  66. {$endif GDB}
  67. cginfo,cgbase,pass_2,
  68. cpuinfo,aasmbase,aasmtai,aasmcpu,
  69. nbas,nmem,nld,ncnv,
  70. {$ifdef x86}
  71. cga,
  72. {$endif x86}
  73. {$ifdef cpu64bit}
  74. cg64f64,
  75. {$else cpu64bit}
  76. cg64f32,
  77. {$endif cpu64bit}
  78. {$ifdef powerpc}
  79. cpupi,
  80. {$endif powerpc}
  81. ncgutil,cgobj,tgobj,regvars,rgobj,rgcpu;
  82. var
  83. { Current callnode, this is needed for having a link
  84. between the callparanodes and the callnode they belong to }
  85. aktcallnode : tcallnode;
  86. {*****************************************************************************
  87. TCGCALLPARANODE
  88. *****************************************************************************}
  89. procedure tcgcallparanode.secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;para_alignment,para_offset : longint);
  90. var
  91. otlabel,
  92. oflabel : tasmlabel;
  93. tmpreg : tregister;
  94. href : treference;
  95. begin
  96. if not(assigned(paraitem.paratype.def) or
  97. assigned(paraitem.parasym)) then
  98. internalerror(200304242);
  99. { set default para_alignment to target_info.stackalignment }
  100. if para_alignment=0 then
  101. para_alignment:=aktalignment.paraalign;
  102. { push from left to right if specified }
  103. if push_from_left_to_right and assigned(right) then
  104. begin
  105. tcallparanode(right).secondcallparan(push_from_left_to_right,
  106. calloption,para_alignment,para_offset);
  107. end;
  108. otlabel:=truelabel;
  109. oflabel:=falselabel;
  110. objectlibrary.getlabel(truelabel);
  111. objectlibrary.getlabel(falselabel);
  112. secondpass(left);
  113. { handle varargs first, because defcoll is not valid }
  114. if (nf_varargs_para in flags) then
  115. begin
  116. if paramanager.push_addr_param(left.resulttype.def,calloption) then
  117. begin
  118. inc(pushedparasize,POINTER_SIZE);
  119. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  120. location_release(exprasmlist,left.location);
  121. end
  122. else
  123. push_value_para(exprasmlist,left,calloption,para_offset,para_alignment,paraitem.paraloc);
  124. end
  125. { hidden parameters }
  126. else if paraitem.is_hidden then
  127. begin
  128. { don't push a node that already generated a pointer type
  129. by address for implicit hidden parameters }
  130. if (vo_is_funcret in tvarsym(paraitem.parasym).varoptions) or
  131. (not(left.resulttype.def.deftype in [pointerdef,classrefdef]) and
  132. paramanager.push_addr_param(paraitem.paratype.def,calloption)) then
  133. begin
  134. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  135. internalerror(200305071);
  136. inc(pushedparasize,POINTER_SIZE);
  137. if calloption=pocall_inline then
  138. begin
  139. {$ifdef newra}
  140. tmpreg:=rg.getaddressregister(exprasmlist);
  141. {$else}
  142. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  143. {$endif}
  144. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  145. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  146. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  147. {$ifdef newra}
  148. rg.ungetregisterint(exprasmlist,tmpreg);
  149. {$else}
  150. cg.free_scratch_reg(exprasmlist,tmpreg);
  151. {$endif}
  152. end
  153. else
  154. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  155. location_release(exprasmlist,left.location);
  156. end
  157. else
  158. begin
  159. push_value_para(exprasmlist,left,calloption,
  160. para_offset,para_alignment,paraitem.paraloc);
  161. end;
  162. end
  163. { filter array of const c styled args }
  164. else if is_array_of_const(left.resulttype.def) and (nf_cargs in left.flags) then
  165. begin
  166. { nothing, everything is already pushed }
  167. end
  168. { in codegen.handleread.. paraitem.data is set to nil }
  169. else if assigned(paraitem.paratype.def) and
  170. (paraitem.paratype.def.deftype=formaldef) then
  171. begin
  172. { allow passing of a constant to a const formaldef }
  173. if (tvarsym(paraitem.parasym).varspez=vs_const) and
  174. (left.location.loc=LOC_CONSTANT) then
  175. location_force_mem(exprasmlist,left.location);
  176. { allow @var }
  177. inc(pushedparasize,POINTER_SIZE);
  178. if (left.nodetype=addrn) and
  179. (not(nf_procvarload in left.flags)) then
  180. begin
  181. if calloption=pocall_inline then
  182. begin
  183. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  184. cg.a_load_loc_ref(exprasmlist,left.location,href);
  185. end
  186. else
  187. cg.a_param_loc(exprasmlist,left.location,paraitem.paraloc);
  188. location_release(exprasmlist,left.location);
  189. end
  190. else
  191. begin
  192. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  193. internalerror(200304235);
  194. if calloption=pocall_inline then
  195. begin
  196. {$ifdef newra}
  197. tmpreg:=rg.getaddressregister(exprasmlist);
  198. {$else}
  199. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  200. {$endif newra}
  201. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  202. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  203. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  204. {$ifdef newra}
  205. rg.ungetregisterint(exprasmlist,tmpreg);
  206. {$else}
  207. cg.free_scratch_reg(exprasmlist,tmpreg);
  208. {$endif}
  209. end
  210. else
  211. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  212. location_release(exprasmlist,left.location);
  213. end;
  214. end
  215. { handle call by reference parameter }
  216. else if (paraitem.paratyp in [vs_var,vs_out]) then
  217. begin
  218. if (left.location.loc<>LOC_REFERENCE) then
  219. begin
  220. { passing self to a var parameter is allowed in
  221. TP and delphi }
  222. if not((left.location.loc=LOC_CREFERENCE) and
  223. is_self_node(left)) then
  224. internalerror(200106041);
  225. end;
  226. if (paraitem.paratyp=vs_out) and
  227. assigned(paraitem.paratype.def) and
  228. not is_class(paraitem.paratype.def) and
  229. paraitem.paratype.def.needs_inittable then
  230. cg.g_finalize(exprasmlist,paraitem.paratype.def,left.location.reference,false);
  231. inc(pushedparasize,POINTER_SIZE);
  232. if calloption=pocall_inline then
  233. begin
  234. {$ifdef newra}
  235. tmpreg:=rg.getaddressregister(exprasmlist);
  236. {$else}
  237. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  238. {$endif}
  239. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  240. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  241. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  242. {$ifdef newra}
  243. rg.ungetregisterint(exprasmlist,tmpreg);
  244. {$else}
  245. cg.free_scratch_reg(exprasmlist,tmpreg);
  246. {$endif}
  247. end
  248. else
  249. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  250. location_release(exprasmlist,left.location);
  251. end
  252. else
  253. begin
  254. { don't push a node that already generated a pointer type
  255. by address for implicit hidden parameters }
  256. if (not(
  257. paraitem.is_hidden and
  258. (left.resulttype.def.deftype in [pointerdef,classrefdef])
  259. ) and
  260. paramanager.push_addr_param(paraitem.paratype.def,calloption)) then
  261. begin
  262. if not(left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  263. begin
  264. { allow passing nil to a procvardef (methodpointer) }
  265. if (left.nodetype=typeconvn) and
  266. (left.resulttype.def.deftype=procvardef) and
  267. (ttypeconvnode(left).left.nodetype=niln) then
  268. begin
  269. tg.GetTemp(exprasmlist,tcgsize2size[left.location.size],tt_normal,href);
  270. if not (left.location.size in [OS_64,OS_S64]) then
  271. cg.a_load_loc_ref(exprasmlist,left.location,href)
  272. else
  273. cg64.a_load64_loc_ref(exprasmlist,left.location,href);
  274. location_reset(left.location,LOC_REFERENCE,left.location.size);
  275. left.location.reference:=href;
  276. end
  277. else
  278. internalerror(200204011);
  279. end;
  280. inc(pushedparasize,POINTER_SIZE);
  281. if calloption=pocall_inline then
  282. begin
  283. {$ifdef newra}
  284. tmpreg:=rg.getaddressregister(exprasmlist);
  285. {$else}
  286. tmpreg:=cg.get_scratch_reg_address(exprasmlist);
  287. {$endif}
  288. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,tmpreg);
  289. reference_reset_base(href,current_procinfo.framepointer,para_offset-pushedparasize);
  290. cg.a_load_reg_ref(exprasmlist,OS_ADDR,tmpreg,href);
  291. {$ifdef newra}
  292. rg.ungetregisterint(exprasmlist,tmpreg);
  293. {$else}
  294. cg.free_scratch_reg(exprasmlist,tmpreg);
  295. {$endif}
  296. end
  297. else
  298. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paraitem.paraloc);
  299. location_release(exprasmlist,left.location);
  300. end
  301. else
  302. begin
  303. push_value_para(exprasmlist,left,calloption,
  304. para_offset,para_alignment,paraitem.paraloc);
  305. end;
  306. end;
  307. truelabel:=otlabel;
  308. falselabel:=oflabel;
  309. { update return location in callnode when this is the function
  310. result }
  311. if (vo_is_funcret in tvarsym(paraitem.parasym).varoptions) then
  312. begin
  313. location_copy(aktcallnode.location,left.location);
  314. end;
  315. { push from right to left }
  316. if not push_from_left_to_right and assigned(right) then
  317. begin
  318. tcallparanode(right).secondcallparan(push_from_left_to_right,
  319. calloption,para_alignment,para_offset);
  320. end;
  321. end;
  322. {*****************************************************************************
  323. TCGCALLNODE
  324. *****************************************************************************}
  325. procedure tcgcallnode.extra_interrupt_code;
  326. begin
  327. end;
  328. function tcgcallnode.align_parasize:longint;
  329. begin
  330. result:=0;
  331. end;
  332. procedure tcgcallnode.pop_parasize(pop_size:longint);
  333. begin
  334. end;
  335. procedure tcgcallnode.push_framepointer;
  336. var
  337. href : treference;
  338. hregister : tregister;
  339. i : integer;
  340. begin
  341. { this routine is itself not nested }
  342. if current_procdef.parast.symtablelevel=(tprocdef(procdefinition).parast.symtablelevel) then
  343. begin
  344. reference_reset_base(href,current_procinfo.framepointer,current_procinfo.framepointer_offset);
  345. cg.a_param_ref(exprasmlist,OS_ADDR,href,paramanager.getintparaloc(1));
  346. end
  347. { one nesting level }
  348. else if (current_procdef.parast.symtablelevel=(tprocdef(procdefinition).parast.symtablelevel)-1) then
  349. begin
  350. cg.a_param_reg(exprasmlist,OS_ADDR,current_procinfo.framepointer,paramanager.getintparaloc(1));
  351. end
  352. { very complex nesting level ... }
  353. else if (current_procdef.parast.symtablelevel>(tprocdef(procdefinition).parast.symtablelevel)) then
  354. begin
  355. hregister:=rg.getaddressregister(exprasmlist);
  356. reference_reset_base(href,current_procinfo.framepointer,current_procinfo.framepointer_offset);
  357. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  358. i:=current_procdef.parast.symtablelevel;
  359. while (i>tprocdef(procdefinition).parast.symtablelevel) do
  360. begin
  361. reference_reset_base(href,hregister,current_procinfo.framepointer_offset);
  362. cg.a_load_ref_reg(exprasmlist,OS_ADDR,href,hregister);
  363. dec(i);
  364. end;
  365. cg.a_param_reg(exprasmlist,OS_ADDR,hregister,paramanager.getintparaloc(1));
  366. rg.ungetaddressregister(exprasmlist,hregister);
  367. end;
  368. end;
  369. procedure tcgcallnode.handle_return_value;
  370. var
  371. cgsize : tcgsize;
  372. r,hregister : tregister;
  373. nr:Tnewregister;
  374. begin
  375. { structured results are easy to handle.... }
  376. { needed also when result_no_used !! }
  377. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  378. begin
  379. { Location should be setup by the funcret para }
  380. if location.loc<>LOC_REFERENCE then
  381. internalerror(200304241);
  382. end
  383. else
  384. { ansi/widestrings must be registered, so we can dispose them }
  385. if is_ansistring(resulttype.def) or
  386. is_widestring(resulttype.def) then
  387. begin
  388. location_reset(location,LOC_CREFERENCE,OS_ADDR);
  389. location.reference:=refcountedtemp;
  390. r.enum:=accumulator;
  391. cg.a_reg_alloc(exprasmlist,r);
  392. cg.a_load_reg_ref(exprasmlist,OS_ADDR,r,location.reference);
  393. cg.a_reg_dealloc(exprasmlist,r);
  394. end
  395. else
  396. { we have only to handle the result if it is used }
  397. if (nf_return_value_used in flags) then
  398. begin
  399. if (resulttype.def.deftype=floatdef) then
  400. begin
  401. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  402. {$ifdef cpufpemu}
  403. if cs_fp_emulation in aktmoduleswitches then
  404. location.register.enum := accumulator
  405. else
  406. {$endif cpufpemu}
  407. location.register.enum:=FPU_RESULT_REG;
  408. {$ifdef x86}
  409. inc(trgcpu(rg).fpuvaroffset);
  410. {$else x86}
  411. hregister := rg.getregisterfpu(exprasmlist);
  412. cg.a_loadfpu_reg_reg(exprasmlist,location.register,hregister);
  413. location.register := hregister;
  414. {$endif x86}
  415. end
  416. else
  417. begin
  418. cgsize:=def_cgsize(resulttype.def);
  419. { an object constructor is a function with pointer result }
  420. if (procdefinition.proctypeoption=potype_constructor) then
  421. cgsize:=OS_ADDR;
  422. if cgsize<>OS_NO then
  423. begin
  424. location_reset(location,LOC_REGISTER,cgsize);
  425. {$ifndef cpu64bit}
  426. if cgsize in [OS_64,OS_S64] then
  427. begin
  428. {Move the function result to free registers, preferably the
  429. accumulator/accumulatorhigh, so no move is necessary.}
  430. r.enum:=R_INTREGISTER;
  431. r.number:=NR_ACCUMULATOR;
  432. hregister.enum:=R_INTREGISTER;
  433. hregister.number:=NR_ACCUMULATORHIGH;
  434. {$ifdef newra}
  435. rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATOR);
  436. rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATORHIGH);
  437. rg.ungetregisterint(exprasmlist,r);
  438. rg.ungetregisterint(exprasmlist,hregister);
  439. location.registerlow:=rg.getregisterint(exprasmlist,OS_INT);
  440. location.registerhigh:=rg.getregisterint(exprasmlist,OS_INT);
  441. {$else newra}
  442. if RS_ACCUMULATOR in rg.unusedregsint then
  443. location.registerlow:=rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATOR)
  444. else
  445. cg.a_reg_alloc(exprasmlist,r);
  446. if RS_ACCUMULATORHIGH in rg.unusedregsint then
  447. location.registerhigh:=rg.getexplicitregisterint(exprasmlist,NR_ACCUMULATORHIGH)
  448. else
  449. cg.a_reg_alloc(exprasmlist,hregister);
  450. { do this after both low,high are allocated, else it is possible that
  451. low will be loaded in the register that still contains high }
  452. if location.registerlow.number=NR_NO then
  453. location.registerlow:=rg.getregisterint(exprasmlist,OS_INT);
  454. if location.registerhigh.number=NR_NO then
  455. location.registerhigh:=rg.getregisterint(exprasmlist,OS_INT);
  456. {$endif newra}
  457. cg64.a_load64_reg_reg(exprasmlist,joinreg64(r,hregister),
  458. location.register64);
  459. end
  460. else
  461. {$endif cpu64bit}
  462. begin
  463. {Move the function result to a free register, preferably the
  464. accumulator, so no move is necessary.}
  465. nr:=RS_ACCUMULATOR shl 8 or cgsize2subreg(cgsize);
  466. r.enum:=R_INTREGISTER;
  467. r.number:=nr;
  468. {$ifdef newra}
  469. rg.getexplicitregisterint(exprasmlist,nr);
  470. rg.ungetregisterint(exprasmlist,r);
  471. location.register:=rg.getregisterint(exprasmlist,cgsize);
  472. {$else newra}
  473. cg.a_reg_alloc(exprasmlist,r);
  474. if RS_ACCUMULATOR in rg.unusedregsint then
  475. location.register:=rg.getexplicitregisterint(exprasmlist,nr)
  476. else
  477. location.register:=rg.getregisterint(exprasmlist,cgsize);
  478. {$endif newra}
  479. cg.a_load_reg_reg(exprasmlist,cgsize,cgsize,r,location.register);
  480. end;
  481. end
  482. else
  483. begin
  484. if resulttype.def.size>0 then
  485. internalerror(200305131);
  486. end;
  487. end;
  488. end
  489. else
  490. location_reset(location,LOC_VOID,OS_NO);
  491. end;
  492. procedure tcgcallnode.release_para_temps;
  493. var
  494. hp : tnode;
  495. ppn : tcallparanode;
  496. begin
  497. { Release temps from parameters }
  498. ppn:=tcallparanode(left);
  499. while assigned(ppn) do
  500. begin
  501. if assigned(ppn.left) then
  502. begin
  503. { don't release the funcret temp }
  504. if not(vo_is_funcret in tvarsym(ppn.paraitem.parasym).varoptions) then
  505. location_freetemp(exprasmlist,ppn.left.location);
  506. { process also all nodes of an array of const }
  507. if ppn.left.nodetype=arrayconstructorn then
  508. begin
  509. if assigned(tarrayconstructornode(ppn.left).left) then
  510. begin
  511. hp:=ppn.left;
  512. while assigned(hp) do
  513. begin
  514. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  515. hp:=tarrayconstructornode(hp).right;
  516. end;
  517. end;
  518. end;
  519. end;
  520. ppn:=tcallparanode(ppn.right);
  521. end;
  522. end;
  523. procedure tcgcallnode.normal_pass_2;
  524. var
  525. regs_to_push_int : Tsupregset;
  526. regs_to_push_other : tregisterset;
  527. unusedstate: pointer;
  528. pushedother : tpushedsavedother;
  529. pushedint : tpushedsavedint;
  530. oldpushedparasize : longint;
  531. { adress returned from an I/O-error }
  532. iolabel : tasmlabel;
  533. { help reference pointer }
  534. href : treference;
  535. pushedregs : tmaybesave;
  536. para_alignment,
  537. pop_size : longint;
  538. accreg : tregister;
  539. oldaktcallnode : tcallnode;
  540. begin
  541. if not assigned(procdefinition) then
  542. internalerror(200305264);
  543. iolabel:=nil;
  544. rg.saveunusedstate(unusedstate);
  545. { if we allocate the temp. location for ansi- or widestrings }
  546. { already here, we avoid later a push/pop }
  547. if is_widestring(resulttype.def) then
  548. begin
  549. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  550. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  551. end
  552. else if is_ansistring(resulttype.def) then
  553. begin
  554. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  555. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  556. end;
  557. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  558. para_alignment:=4
  559. else
  560. para_alignment:=aktalignment.paraalign;
  561. { proc variables destroy all registers }
  562. if (right=nil) and
  563. { virtual methods too }
  564. not(po_virtualmethod in procdefinition.procoptions) then
  565. begin
  566. if (cs_check_io in aktlocalswitches) and
  567. (po_iocheck in procdefinition.procoptions) and
  568. not(po_iocheck in current_procdef.procoptions) then
  569. begin
  570. objectlibrary.getaddrlabel(iolabel);
  571. cg.a_label(exprasmlist,iolabel);
  572. end
  573. else
  574. iolabel:=nil;
  575. { save all used registers and possible registers
  576. used for the return value }
  577. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  578. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  579. if (not is_void(resulttype.def)) and
  580. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  581. begin
  582. include(regs_to_push_int,RS_ACCUMULATOR);
  583. {$ifndef cpu64bit}
  584. if resulttype.def.size>sizeof(aword) then
  585. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  586. {$endif cpu64bit}
  587. end;
  588. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  589. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  590. { give used registers through }
  591. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  592. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  593. end
  594. else
  595. begin
  596. regs_to_push_int := all_intregisters;
  597. regs_to_push_other := all_registers;
  598. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  599. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  600. rg.usedinproc:=all_registers;
  601. { no IO check for methods and procedure variables }
  602. iolabel:=nil;
  603. end;
  604. { Initialize for pushing the parameters }
  605. oldpushedparasize:=pushedparasize;
  606. pushedparasize:=0;
  607. { Align stack if required }
  608. pop_size:=align_parasize;
  609. { Push parameters }
  610. oldaktcallnode:=aktcallnode;
  611. aktcallnode:=self;
  612. { process procvar. Done here already, because otherwise it may }
  613. { destroy registers containing a parameter for the actual }
  614. { function call (e.g. if it's a function, its result will }
  615. { overwrite r3, which contains the first parameter) (JM) }
  616. if assigned(right) then
  617. secondpass(right);
  618. if (po_virtualmethod in procdefinition.procoptions) and
  619. assigned(methodpointer) then
  620. begin
  621. secondpass(methodpointer);
  622. location_force_reg(exprasmlist,methodpointer.location,OS_ADDR,false);
  623. { virtual methods require an index }
  624. if tprocdef(procdefinition).extnumber=-1 then
  625. internalerror(200304021);
  626. { VMT should already be loaded in a register }
  627. if methodpointer.location.register.number=NR_NO then
  628. internalerror(200304022);
  629. { test validity of VMT }
  630. if not(is_interface(tprocdef(procdefinition)._class)) and
  631. not(is_cppclass(tprocdef(procdefinition)._class)) then
  632. cg.g_maybe_testvmt(exprasmlist,methodpointer.location.register,tprocdef(procdefinition)._class);
  633. end;
  634. if assigned(left) then
  635. begin
  636. {$ifndef newra}
  637. if assigned(right) then
  638. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  639. else
  640. if assigned(methodpointer) then
  641. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  642. {$endif}
  643. tcallparanode(left).secondcallparan(
  644. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  645. para_alignment,0);
  646. {$ifndef newra}
  647. if assigned(right) then
  648. maybe_restore(exprasmlist,right.location,pushedregs)
  649. else
  650. if assigned(methodpointer) then
  651. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  652. {$endif newra}
  653. end;
  654. aktcallnode:=oldaktcallnode;
  655. { procedure variable or normal function call ? }
  656. if (right=nil) then
  657. begin
  658. { push base pointer ?}
  659. if (current_procdef.parast.symtablelevel>=normal_function_level) and
  660. assigned(tprocdef(procdefinition).parast) and
  661. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  662. push_framepointer;
  663. rg.saveintregvars(exprasmlist,regs_to_push_int);
  664. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  665. if (po_virtualmethod in procdefinition.procoptions) and
  666. assigned(methodpointer) then
  667. begin
  668. { call method }
  669. reference_reset_base(href,methodpointer.location.register,
  670. tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber));
  671. cg.a_call_ref(exprasmlist,href);
  672. { release vmt register }
  673. rg.ungetaddressregister(exprasmlist,methodpointer.location.register);
  674. end
  675. else
  676. begin
  677. { Calling interrupt from the same code requires some
  678. extra code }
  679. if (po_interrupt in procdefinition.procoptions) then
  680. extra_interrupt_code;
  681. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  682. end;
  683. end
  684. else
  685. { now procedure variable case }
  686. begin
  687. { Calling interrupt from the same code requires some
  688. extra code }
  689. if (po_interrupt in procdefinition.procoptions) then
  690. extra_interrupt_code;
  691. rg.saveintregvars(exprasmlist,ALL_INTREGISTERS);
  692. rg.saveotherregvars(exprasmlist,ALL_REGISTERS);
  693. cg.a_call_loc(exprasmlist,right.location);
  694. location_release(exprasmlist,right.location);
  695. location_freetemp(exprasmlist,right.location);
  696. end;
  697. { Need to remove the parameters from the stack? }
  698. if (po_clearstack in procdefinition.procoptions) then
  699. begin
  700. { the old pop_size was already included in pushedparasize }
  701. pop_size:=pushedparasize;
  702. { for Cdecl functions we don't need to pop the funcret when it
  703. was pushed by para }
  704. if paramanager.ret_in_param(procdefinition.rettype.def,procdefinition.proccalloption) then
  705. dec(pop_size,POINTER_SIZE);
  706. end;
  707. { Remove parameters/alignment from the stack }
  708. if pop_size>0 then
  709. pop_parasize(pop_size);
  710. {$ifdef powerpc}
  711. { this calculation must be done in pass_1 anyway, so don't worry }
  712. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  713. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  714. {$endif powerpc}
  715. { Restore }
  716. pushedparasize:=oldpushedparasize;
  717. rg.restoreunusedstate(unusedstate);
  718. {$ifdef TEMPREGDEBUG}
  719. testregisters32;
  720. {$endif TEMPREGDEBUG}
  721. { handle function results }
  722. if (not is_void(resulttype.def)) then
  723. handle_return_value
  724. else
  725. location_reset(location,LOC_VOID,OS_NO);
  726. { perhaps i/o check ? }
  727. if iolabel<>nil then
  728. begin
  729. reference_reset_symbol(href,iolabel,0);
  730. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  731. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  732. end;
  733. { restore registers }
  734. rg.restoreusedotherregisters(exprasmlist,pushedother);
  735. rg.restoreusedintregisters(exprasmlist,pushedint);
  736. { release temps of paras }
  737. release_para_temps;
  738. { if return value is not used }
  739. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  740. begin
  741. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  742. begin
  743. { data which must be finalized ? }
  744. if (resulttype.def.needs_inittable) then
  745. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  746. { release unused temp }
  747. tg.ungetiftemp(exprasmlist,location.reference)
  748. end
  749. else if location.loc=LOC_FPUREGISTER then
  750. begin
  751. {$ifdef x86}
  752. { release FPU stack }
  753. accreg.enum:=FPU_RESULT_REG;
  754. emit_reg(A_FSTP,S_NO,accreg);
  755. {
  756. dec(trgcpu(rg).fpuvaroffset);
  757. do NOT decrement as the increment before
  758. is not called for unused results PM }
  759. {$endif x86}
  760. end;
  761. end;
  762. end;
  763. procedure tcgcallnode.inlined_pass_2;
  764. var
  765. regs_to_push_int : Tsupregset;
  766. regs_to_push_other : tregisterset;
  767. unusedstate: pointer;
  768. pushedother : tpushedsavedother;
  769. pushedint : tpushedsavedint;
  770. oldpushedparasize : longint;
  771. { adress returned from an I/O-error }
  772. iolabel : tasmlabel;
  773. { help reference pointer }
  774. href : treference;
  775. pushedregs : tmaybesave;
  776. accreg : tregister;
  777. oldaktcallnode : tcallnode;
  778. oldprocdef : tprocdef;
  779. i : longint;
  780. oldprocinfo : tprocinfo;
  781. oldinlining_procedure : boolean;
  782. inlineentrycode,inlineexitcode : TAAsmoutput;
  783. oldexitlabel:tasmlabel;
  784. oldregstate: pointer;
  785. old_local_fixup,
  786. old_para_fixup : longint;
  787. pararef,
  788. localsref : treference;
  789. {$ifdef GDB}
  790. startlabel,endlabel : tasmlabel;
  791. pp : pchar;
  792. mangled_length : longint;
  793. {$endif GDB}
  794. begin
  795. if not(assigned(procdefinition) and (procdefinition.deftype=procdef)) then
  796. internalerror(200305262);
  797. oldinlining_procedure:=inlining_procedure;
  798. oldexitlabel:=aktexitlabel;
  799. oldprocdef:=current_procdef;
  800. oldprocinfo:=current_procinfo;
  801. objectlibrary.getlabel(aktexitlabel);
  802. { we're inlining a procedure }
  803. inlining_procedure:=true;
  804. { deallocate the registers used for the current procedure's regvars }
  805. if assigned(current_procdef.regvarinfo) then
  806. begin
  807. with pregvarinfo(current_procdef.regvarinfo)^ do
  808. for i := 1 to maxvarregs do
  809. if assigned(regvars[i]) then
  810. store_regvar(exprasmlist,regvars[i].reg);
  811. rg.saveStateForInline(oldregstate);
  812. { make sure the register allocator knows what the regvars in the }
  813. { inlined code block are (JM) }
  814. rg.resetusableregisters;
  815. rg.clearregistercount;
  816. {$ifndef newra}
  817. rg.cleartempgen;
  818. {$endif}
  819. if assigned(tprocdef(procdefinition).regvarinfo) then
  820. with pregvarinfo(tprocdef(procdefinition).regvarinfo)^ do
  821. for i := 1 to maxvarregs do
  822. if assigned(regvars[i]) then
  823. begin
  824. {Fix me!!}
  825. {tmpreg:=rg.makeregsize(regvars[i].reg,OS_INT);
  826. rg.makeregvar(tmpreg);}
  827. internalerror(200301232);
  828. end;
  829. end;
  830. { create temp procinfo }
  831. current_procinfo:=cprocinfo.create(nil);
  832. current_procinfo.procdef:=tprocdef(procdefinition);
  833. current_procdef:=current_procinfo.procdef;
  834. { Localsymtable }
  835. current_procdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  836. if current_procdef.localst.datasize>0 then
  837. begin
  838. old_local_fixup:=current_procdef.localst.address_fixup;
  839. tg.GetTemp(exprasmlist,current_procdef.localst.datasize,tt_persistent,localsref);
  840. if tg.direction>0 then
  841. current_procdef.localst.address_fixup:=localsref.offset
  842. else
  843. current_procdef.localst.address_fixup:=localsref.offset+current_procdef.localst.datasize;
  844. {$ifdef extdebug}
  845. Comment(V_debug,'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup));
  846. exprasmList.concat(tai_comment.Create(strpnew(
  847. 'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup))));
  848. {$endif extdebug}
  849. end;
  850. { Parasymtable }
  851. current_procdef.parast.symtablelevel:=oldprocdef.localst.symtablelevel;
  852. if current_procdef.parast.datasize>0 then
  853. begin
  854. old_para_fixup:=current_procdef.parast.address_fixup;
  855. tg.GetTemp(exprasmlist,current_procdef.parast.datasize,tt_persistent,pararef);
  856. current_procdef.parast.address_fixup:=pararef.offset;
  857. {$ifdef extdebug}
  858. Comment(V_debug,'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup));
  859. exprasmList.concat(tai_comment.Create(strpnew(
  860. 'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup))));
  861. {$endif extdebug}
  862. end;
  863. { Calculate offsets }
  864. current_procinfo.after_header;
  865. exprasmList.concat(Tai_Marker.Create(InlineStart));
  866. {$ifdef extdebug}
  867. exprasmList.concat(tai_comment.Create(strpnew('Start of inlined proc')));
  868. {$endif extdebug}
  869. {$ifdef GDB}
  870. if (cs_debuginfo in aktmoduleswitches) then
  871. begin
  872. objectlibrary.getaddrlabel(startlabel);
  873. objectlibrary.getaddrlabel(endlabel);
  874. cg.a_label(exprasmlist,startlabel);
  875. tprocdef(procdefinition).localst.symtabletype:=inlinelocalsymtable;
  876. procdefinition.parast.symtabletype:=inlineparasymtable;
  877. { Here we must include the para and local symtable info }
  878. procdefinition.concatstabto(withdebuglist);
  879. { set it back for safety }
  880. tprocdef(procdefinition).localst.symtabletype:=localsymtable;
  881. procdefinition.parast.symtabletype:=parasymtable;
  882. mangled_length:=length(oldprocdef.mangledname);
  883. getmem(pp,mangled_length+50);
  884. strpcopy(pp,'192,0,0,'+startlabel.name);
  885. if (target_info.use_function_relative_addresses) then
  886. begin
  887. strpcopy(strend(pp),'-');
  888. strpcopy(strend(pp),oldprocdef.mangledname);
  889. end;
  890. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  891. end;
  892. {$endif GDB}
  893. iolabel:=nil;
  894. rg.saveunusedstate(unusedstate);
  895. { if we allocate the temp. location for ansi- or widestrings }
  896. { already here, we avoid later a push/pop }
  897. if is_widestring(resulttype.def) then
  898. begin
  899. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  900. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  901. end
  902. else if is_ansistring(resulttype.def) then
  903. begin
  904. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  905. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  906. end;
  907. if (cs_check_io in aktlocalswitches) and
  908. (po_iocheck in procdefinition.procoptions) and
  909. not(po_iocheck in current_procdef.procoptions) then
  910. begin
  911. objectlibrary.getaddrlabel(iolabel);
  912. cg.a_label(exprasmlist,iolabel);
  913. end
  914. else
  915. iolabel:=nil;
  916. { save all used registers and possible registers
  917. used for the return value }
  918. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  919. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  920. if (not is_void(resulttype.def)) and
  921. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  922. begin
  923. include(regs_to_push_int,RS_ACCUMULATOR);
  924. {$ifndef cpu64bit}
  925. if resulttype.def.size>sizeof(aword) then
  926. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  927. {$endif cpu64bit}
  928. end;
  929. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  930. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  931. { give used registers through }
  932. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  933. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  934. { Initialize for pushing the parameters }
  935. oldpushedparasize:=pushedparasize;
  936. pushedparasize:=0;
  937. { Push parameters }
  938. oldaktcallnode:=aktcallnode;
  939. aktcallnode:=self;
  940. if assigned(left) then
  941. begin
  942. {$ifndef newra}
  943. if assigned(right) then
  944. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  945. else
  946. if assigned(methodpointer) then
  947. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  948. {$endif}
  949. { we push from right to left, so start with parameters at the end of
  950. the parameter block }
  951. tcallparanode(left).secondcallparan(
  952. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  953. 0,procdefinition.parast.address_fixup+procdefinition.parast.datasize);
  954. {$ifndef newra}
  955. if assigned(right) then
  956. maybe_restore(exprasmlist,right.location,pushedregs)
  957. else
  958. if assigned(methodpointer) then
  959. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  960. {$endif newra}
  961. end;
  962. aktcallnode:=oldaktcallnode;
  963. rg.saveintregvars(exprasmlist,regs_to_push_int);
  964. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  965. { takes care of local data initialization }
  966. inlineentrycode:=TAAsmoutput.Create;
  967. inlineexitcode:=TAAsmoutput.Create;
  968. geninlineentrycode(inlineentrycode,0);
  969. if po_assembler in current_procdef.procoptions then
  970. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  971. exprasmList.concatlist(inlineentrycode);
  972. { process the inline code }
  973. secondpass(inlinecode);
  974. {$ifdef powerpc}
  975. { this calculation must be done in pass_1 anyway, so don't worry }
  976. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  977. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  978. {$endif powerpc}
  979. { Restore }
  980. pushedparasize:=oldpushedparasize;
  981. rg.restoreunusedstate(unusedstate);
  982. {$ifdef TEMPREGDEBUG}
  983. testregisters32;
  984. {$endif TEMPREGDEBUG}
  985. geninlineexitcode(inlineexitcode,true);
  986. if po_assembler in current_procdef.procoptions then
  987. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  988. exprasmList.concatlist(inlineexitcode);
  989. inlineentrycode.free;
  990. inlineexitcode.free;
  991. {$ifdef extdebug}
  992. exprasmList.concat(tai_comment.Create(strpnew('End of inlined proc')));
  993. {$endif extdebug}
  994. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  995. {we can free the local data now, reset also the fixup address }
  996. if current_procdef.localst.datasize>0 then
  997. begin
  998. tg.UnGetTemp(exprasmlist,localsref);
  999. current_procdef.localst.address_fixup:=old_local_fixup;
  1000. end;
  1001. {we can free the para data now, reset also the fixup address }
  1002. if current_procdef.parast.datasize>0 then
  1003. begin
  1004. tg.UnGetTemp(exprasmlist,pararef);
  1005. current_procdef.parast.address_fixup:=old_para_fixup;
  1006. end;
  1007. { free return reference }
  1008. if (resulttype.def.size>0) then
  1009. begin
  1010. { from now on the result can be freed normally }
  1011. // if assigned(funcretnode) and
  1012. // paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1013. // tg.ChangeTempType(exprasmlist,funcretnode.location.reference,tt_normal);
  1014. end;
  1015. { handle function results }
  1016. if (not is_void(resulttype.def)) then
  1017. handle_return_value
  1018. else
  1019. location_reset(location,LOC_VOID,OS_NO);
  1020. { perhaps i/o check ? }
  1021. if iolabel<>nil then
  1022. begin
  1023. reference_reset_symbol(href,iolabel,0);
  1024. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  1025. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1026. end;
  1027. { restore registers }
  1028. rg.restoreusedotherregisters(exprasmlist,pushedother);
  1029. rg.restoreusedintregisters(exprasmlist,pushedint);
  1030. { release temps of paras }
  1031. release_para_temps;
  1032. { if return value is not used }
  1033. if (not is_void(resulttype.def)) and
  1034. (not(nf_return_value_used in flags)) then
  1035. begin
  1036. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1037. begin
  1038. { data which must be finalized ? }
  1039. if (resulttype.def.needs_inittable) then
  1040. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1041. { release unused temp }
  1042. tg.ungetiftemp(exprasmlist,location.reference)
  1043. end
  1044. else if location.loc=LOC_FPUREGISTER then
  1045. begin
  1046. {$ifdef x86}
  1047. { release FPU stack }
  1048. accreg.enum:=FPU_RESULT_REG;
  1049. emit_reg(A_FSTP,S_NO,accreg);
  1050. {
  1051. dec(trgcpu(rg).fpuvaroffset);
  1052. do NOT decrement as the increment before
  1053. is not called for unused results PM }
  1054. {$endif x86}
  1055. end;
  1056. end;
  1057. { release procinfo }
  1058. current_procinfo.free;
  1059. current_procinfo:=oldprocinfo;
  1060. {$ifdef GDB}
  1061. if (cs_debuginfo in aktmoduleswitches) then
  1062. begin
  1063. cg.a_label(exprasmlist,endlabel);
  1064. strpcopy(pp,'224,0,0,'+endlabel.name);
  1065. if (target_info.use_function_relative_addresses) then
  1066. begin
  1067. strpcopy(strend(pp),'-');
  1068. strpcopy(strend(pp),oldprocdef.mangledname);
  1069. end;
  1070. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1071. freemem(pp,mangled_length+50);
  1072. end;
  1073. {$endif GDB}
  1074. { restore }
  1075. current_procdef:=oldprocdef;
  1076. aktexitlabel:=oldexitlabel;
  1077. inlining_procedure:=oldinlining_procedure;
  1078. { reallocate the registers used for the current procedure's regvars, }
  1079. { since they may have been used and then deallocated in the inlined }
  1080. { procedure (JM) }
  1081. if assigned(current_procdef.regvarinfo) then
  1082. rg.restoreStateAfterInline(oldregstate);
  1083. end;
  1084. procedure tcgcallnode.pass_2;
  1085. begin
  1086. if assigned(inlinecode) then
  1087. inlined_pass_2
  1088. else
  1089. normal_pass_2;
  1090. end;
  1091. begin
  1092. ccallparanode:=tcgcallparanode;
  1093. ccallnode:=tcgcallnode;
  1094. end.
  1095. {
  1096. $Log$
  1097. Revision 1.75 2003-05-26 21:17:17 peter
  1098. * procinlinenode removed
  1099. * aktexit2label removed, fast exit removed
  1100. + tcallnode.inlined_pass_2 added
  1101. Revision 1.74 2003/05/25 11:34:17 peter
  1102. * methodpointer self pushing fixed
  1103. Revision 1.73 2003/05/25 08:59:16 peter
  1104. * inline fixes
  1105. Revision 1.72 2003/05/24 13:36:54 jonas
  1106. * save fpu results in a normal fpu register on non-x86 processors
  1107. Revision 1.71 2003/05/23 19:35:50 jonas
  1108. - undid previous commit, it was wrong
  1109. Revision 1.70 2003/05/23 19:11:58 jonas
  1110. * fixed tests for whether a certain int register is unused
  1111. Revision 1.69 2003/05/23 18:01:56 jonas
  1112. * fixed ppc compiler
  1113. Revision 1.68 2003/05/23 14:27:35 peter
  1114. * remove some unit dependencies
  1115. * current_procinfo changes to store more info
  1116. Revision 1.67 2003/05/17 13:30:08 jonas
  1117. * changed tt_persistant to tt_persistent :)
  1118. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1119. temps, but a ttemptype, so you can also create ansistring temps etc
  1120. Revision 1.66 2003/05/16 14:33:31 peter
  1121. * regvar fixes
  1122. Revision 1.65 2003/05/15 18:58:53 peter
  1123. * removed selfpointer_offset, vmtpointer_offset
  1124. * tvarsym.adjusted_address
  1125. * address in localsymtable is now in the real direction
  1126. * removed some obsolete globals
  1127. Revision 1.64 2003/05/14 19:36:54 jonas
  1128. * patch from Peter for int64 function results
  1129. Revision 1.63 2003/05/13 19:14:41 peter
  1130. * failn removed
  1131. * inherited result code check moven to pexpr
  1132. Revision 1.62 2003/05/13 15:18:18 peter
  1133. * generate code for procvar first before pushing parameters. Made
  1134. the already existing code for powerpc available for all platforms
  1135. Revision 1.61 2003/05/12 18:17:55 jonas
  1136. * moved fpc_check_object call earlier for the ppc, so it can't destroy
  1137. already-loaded parameter registers
  1138. Revision 1.60 2003/05/11 21:48:38 jonas
  1139. * fixed procvar bug on the ppc (load procvar before loading para's,
  1140. because the procvar may otherwise destroy the already loaded paras)
  1141. Revision 1.59 2003/05/09 17:47:02 peter
  1142. * self moved to hidden parameter
  1143. * removed hdisposen,hnewn,selfn
  1144. Revision 1.58 2003/05/05 14:53:16 peter
  1145. * vs_hidden replaced by is_hidden boolean
  1146. Revision 1.57 2003/04/30 20:53:32 florian
  1147. * error when address of an abstract method is taken
  1148. * fixed some x86-64 problems
  1149. * merged some more x86-64 and i386 code
  1150. Revision 1.56 2003/04/29 07:28:52 michael
  1151. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1152. Revision 1.55 2003/04/27 11:21:33 peter
  1153. * aktprocdef renamed to current_procdef
  1154. * procinfo renamed to current_procinfo
  1155. * procinfo will now be stored in current_module so it can be
  1156. cleaned up properly
  1157. * gen_main_procsym changed to create_main_proc and release_main_proc
  1158. to also generate a tprocinfo structure
  1159. * fixed unit implicit initfinal
  1160. Revision 1.54 2003/04/27 07:29:50 peter
  1161. * current_procdef cleanup, current_procdef is now always nil when parsing
  1162. a new procdef declaration
  1163. * aktprocsym removed
  1164. * lexlevel removed, use symtable.symtablelevel instead
  1165. * implicit init/final code uses the normal genentry/genexit
  1166. * funcret state checking updated for new funcret handling
  1167. Revision 1.53 2003/04/25 20:59:33 peter
  1168. * removed funcretn,funcretsym, function result is now in varsym
  1169. and aliases for result and function name are added using absolutesym
  1170. * vs_hidden parameter for funcret passed in parameter
  1171. * vs_hidden fixes
  1172. * writenode changed to printnode and released from extdebug
  1173. * -vp option added to generate a tree.log with the nodetree
  1174. * nicer printnode for statements, callnode
  1175. Revision 1.52 2003/04/25 08:25:26 daniel
  1176. * Ifdefs around a lot of calls to cleartempgen
  1177. * Fixed registers that are allocated but not freed in several nodes
  1178. * Tweak to register allocator to cause less spills
  1179. * 8-bit registers now interfere with esi,edi and ebp
  1180. Compiler can now compile rtl successfully when using new register
  1181. allocator
  1182. Revision 1.51 2003/04/22 23:50:22 peter
  1183. * firstpass uses expectloc
  1184. * checks if there are differences between the expectloc and
  1185. location.loc from secondpass in EXTDEBUG
  1186. Revision 1.50 2003/04/22 14:33:38 peter
  1187. * removed some notes/hints
  1188. Revision 1.49 2003/04/22 13:47:08 peter
  1189. * fixed C style array of const
  1190. * fixed C array passing
  1191. * fixed left to right with high parameters
  1192. Revision 1.48 2003/04/22 10:09:34 daniel
  1193. + Implemented the actual register allocator
  1194. + Scratch registers unavailable when new register allocator used
  1195. + maybe_save/maybe_restore unavailable when new register allocator used
  1196. Revision 1.47 2003/04/22 09:49:44 peter
  1197. * do not load self when calling a non-inherited class constructor
  1198. Revision 1.46 2003/04/21 20:03:32 peter
  1199. * forgot to copy vmtrefaddr to selfrefaddr when self=vmt
  1200. Revision 1.45 2003/04/21 13:53:16 jonas
  1201. - removed copying of all paras when secondpassing a callnode (this used
  1202. to be necessary for inlinign support, but currently the whole inlined
  1203. procedure is already copied in advance). Note that the compiler crashes
  1204. when compiling ucomplex with -dTEST_INLINE (also after fixing the
  1205. syntax errors), but that was also the case before this change.
  1206. Revision 1.44 2003/04/10 17:57:52 peter
  1207. * vs_hidden released
  1208. Revision 1.43 2003/04/06 21:11:23 olle
  1209. * changed newasmsymbol to newasmsymboldata for data symbols
  1210. Revision 1.42 2003/04/04 15:38:56 peter
  1211. * moved generic code from n386cal to ncgcal, i386 now also
  1212. uses the generic ncgcal
  1213. Revision 1.41 2003/03/28 19:16:56 peter
  1214. * generic constructor working for i386
  1215. * remove fixed self register
  1216. * esi added as address register for i386
  1217. Revision 1.40 2003/03/06 11:35:50 daniel
  1218. * Fixed internalerror 7843 issue
  1219. Revision 1.39 2003/02/19 22:00:14 daniel
  1220. * Code generator converted to new register notation
  1221. - Horribily outdated todo.txt removed
  1222. Revision 1.38 2003/02/15 22:17:38 carl
  1223. * bugfix of FPU emulation code
  1224. Revision 1.37 2003/02/12 22:10:07 carl
  1225. * load_frame_pointer is now generic
  1226. * change fpu emulation routine names
  1227. Revision 1.36 2003/01/30 21:46:57 peter
  1228. * self fixes for static methods (merged)
  1229. Revision 1.35 2003/01/22 20:45:15 mazen
  1230. * making math code in RTL compiling.
  1231. *NB : This does NOT mean necessary that it will generate correct code!
  1232. Revision 1.34 2003/01/17 12:03:45 daniel
  1233. * Optalign conditional code adapted to record Tregister
  1234. Revision 1.33 2003/01/08 18:43:56 daniel
  1235. * Tregister changed into a record
  1236. Revision 1.32 2002/12/15 22:50:00 florian
  1237. + some stuff for the new hidden parameter handling added
  1238. Revision 1.31 2002/12/15 21:30:12 florian
  1239. * tcallnode.paraitem introduced, all references to defcoll removed
  1240. Revision 1.30 2002/11/27 20:04:39 peter
  1241. * cdecl array of const fixes
  1242. Revision 1.29 2002/11/25 17:43:17 peter
  1243. * splitted defbase in defutil,symutil,defcmp
  1244. * merged isconvertable and is_equal into compare_defs(_ext)
  1245. * made operator search faster by walking the list only once
  1246. Revision 1.28 2002/11/18 17:31:54 peter
  1247. * pass proccalloption to ret_in_xxx and push_xxx functions
  1248. Revision 1.27 2002/11/16 15:34:30 florian
  1249. * generic location for float results
  1250. Revision 1.26 2002/11/15 01:58:51 peter
  1251. * merged changes from 1.0.7 up to 04-11
  1252. - -V option for generating bug report tracing
  1253. - more tracing for option parsing
  1254. - errors for cdecl and high()
  1255. - win32 import stabs
  1256. - win32 records<=8 are returned in eax:edx (turned off by default)
  1257. - heaptrc update
  1258. - more info for temp management in .s file with EXTDEBUG
  1259. Revision 1.25 2002/10/05 12:43:25 carl
  1260. * fixes for Delphi 6 compilation
  1261. (warning : Some features do not work under Delphi)
  1262. Revision 1.24 2002/09/30 07:00:45 florian
  1263. * fixes to common code to get the alpha compiler compiled applied
  1264. Revision 1.23 2002/09/17 18:54:02 jonas
  1265. * a_load_reg_reg() now has two size parameters: source and dest. This
  1266. allows some optimizations on architectures that don't encode the
  1267. register size in the register name.
  1268. Revision 1.22 2002/09/07 15:25:02 peter
  1269. * old logs removed and tabs fixed
  1270. Revision 1.21 2002/09/07 11:50:02 jonas
  1271. * fixed small regalloction info bug
  1272. Revision 1.20 2002/09/02 11:25:20 florian
  1273. * fixed generic procedure variable calling
  1274. Revision 1.19 2002/09/01 21:04:48 florian
  1275. * several powerpc related stuff fixed
  1276. Revision 1.18 2002/09/01 18:43:27 peter
  1277. * include accumulator in regs_to_push list
  1278. Revision 1.17 2002/09/01 12:13:00 peter
  1279. * use a_call_reg
  1280. * ungetiftemp for procvar of object temp
  1281. Revision 1.16 2002/08/25 19:25:18 peter
  1282. * sym.insert_in_data removed
  1283. * symtable.insertvardata/insertconstdata added
  1284. * removed insert_in_data call from symtable.insert, it needs to be
  1285. called separatly. This allows to deref the address calculation
  1286. * procedures now calculate the parast addresses after the procedure
  1287. directives are parsed. This fixes the cdecl parast problem
  1288. * push_addr_param has an extra argument that specifies if cdecl is used
  1289. or not
  1290. Revision 1.15 2002/08/23 16:14:48 peter
  1291. * tempgen cleanup
  1292. * tt_noreuse temp type added that will be used in genentrycode
  1293. Revision 1.14 2002/08/20 16:55:38 peter
  1294. * don't write (stabs)line info when inlining a procedure
  1295. Revision 1.13 2002/08/19 19:36:42 peter
  1296. * More fixes for cross unit inlining, all tnodes are now implemented
  1297. * Moved pocall_internconst to po_internconst because it is not a
  1298. calling type at all and it conflicted when inlining of these small
  1299. functions was requested
  1300. Revision 1.12 2002/08/18 20:06:23 peter
  1301. * inlining is now also allowed in interface
  1302. * renamed write/load to ppuwrite/ppuload
  1303. * tnode storing in ppu
  1304. * nld,ncon,nbas are already updated for storing in ppu
  1305. Revision 1.11 2002/08/17 22:09:44 florian
  1306. * result type handling in tcgcal.pass_2 overhauled
  1307. * better tnode.dowrite
  1308. * some ppc stuff fixed
  1309. Revision 1.10 2002/08/17 09:23:35 florian
  1310. * first part of procinfo rewrite
  1311. Revision 1.9 2002/08/13 21:40:55 florian
  1312. * more fixes for ppc calling conventions
  1313. Revision 1.8 2002/08/13 18:01:51 carl
  1314. * rename swatoperands to swapoperands
  1315. + m68k first compilable version (still needs a lot of testing):
  1316. assembler generator, system information , inline
  1317. assembler reader.
  1318. Revision 1.7 2002/08/12 15:08:39 carl
  1319. + stab register indexes for powerpc (moved from gdb to cpubase)
  1320. + tprocessor enumeration moved to cpuinfo
  1321. + linker in target_info is now a class
  1322. * many many updates for m68k (will soon start to compile)
  1323. - removed some ifdef or correct them for correct cpu
  1324. Revision 1.6 2002/08/11 14:32:26 peter
  1325. * renamed current_library to objectlibrary
  1326. Revision 1.5 2002/08/11 13:24:11 peter
  1327. * saving of asmsymbols in ppu supported
  1328. * asmsymbollist global is removed and moved into a new class
  1329. tasmlibrarydata that will hold the info of a .a file which
  1330. corresponds with a single module. Added librarydata to tmodule
  1331. to keep the library info stored for the module. In the future the
  1332. objectfiles will also be stored to the tasmlibrarydata class
  1333. * all getlabel/newasmsymbol and friends are moved to the new class
  1334. Revision 1.4 2002/08/06 20:55:20 florian
  1335. * first part of ppc calling conventions fix
  1336. Revision 1.3 2002/07/20 11:57:53 florian
  1337. * types.pas renamed to defbase.pas because D6 contains a types
  1338. unit so this would conflicts if D6 programms are compiled
  1339. + Willamette/SSE2 instructions to assembler added
  1340. Revision 1.2 2002/07/13 19:38:43 florian
  1341. * some more generic calling stuff fixed
  1342. }