ncgcal.pas 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  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. begin
  506. {$ifdef callparatemp}
  507. { free call-by-reference temps }
  508. if (ppn.left.nodetype = typeconvn) and
  509. (ttypeconvnode(ppn.left).left.nodetype = derefn) and
  510. (tderefnode(ttypeconvnode(ppn.left).left).left.nodetype = temprefn) then
  511. location_freetemp(exprasmlist,tderefnode(ttypeconvnode(ppn.left).left).left.location)
  512. else
  513. {$endif callparatemp}
  514. location_freetemp(exprasmlist,ppn.left.location);
  515. end;
  516. { process also all nodes of an array of const }
  517. if ppn.left.nodetype=arrayconstructorn then
  518. begin
  519. if assigned(tarrayconstructornode(ppn.left).left) then
  520. begin
  521. hp:=ppn.left;
  522. while assigned(hp) do
  523. begin
  524. location_freetemp(exprasmlist,tarrayconstructornode(hp).left.location);
  525. hp:=tarrayconstructornode(hp).right;
  526. end;
  527. end;
  528. end;
  529. end;
  530. ppn:=tcallparanode(ppn.right);
  531. end;
  532. end;
  533. procedure tcgcallnode.normal_pass_2;
  534. var
  535. regs_to_push_int : Tsupregset;
  536. regs_to_push_other : tregisterset;
  537. unusedstate: pointer;
  538. pushedother : tpushedsavedother;
  539. pushedint : tpushedsavedint;
  540. oldpushedparasize : longint;
  541. { adress returned from an I/O-error }
  542. iolabel : tasmlabel;
  543. { help reference pointer }
  544. href : treference;
  545. pushedregs : tmaybesave;
  546. para_alignment,
  547. pop_size : longint;
  548. accreg : tregister;
  549. oldaktcallnode : tcallnode;
  550. begin
  551. if not assigned(procdefinition) then
  552. internalerror(200305264);
  553. iolabel:=nil;
  554. rg.saveunusedstate(unusedstate);
  555. { if we allocate the temp. location for ansi- or widestrings }
  556. { already here, we avoid later a push/pop }
  557. if is_widestring(resulttype.def) then
  558. begin
  559. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  560. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  561. end
  562. else if is_ansistring(resulttype.def) then
  563. begin
  564. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  565. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  566. end;
  567. if (procdefinition.proccalloption in [pocall_cdecl,pocall_cppdecl,pocall_stdcall]) then
  568. para_alignment:=4
  569. else
  570. para_alignment:=aktalignment.paraalign;
  571. { proc variables destroy all registers }
  572. if (right=nil) and
  573. { virtual methods too }
  574. not(po_virtualmethod in procdefinition.procoptions) then
  575. begin
  576. if (cs_check_io in aktlocalswitches) and
  577. (po_iocheck in procdefinition.procoptions) and
  578. not(po_iocheck in current_procdef.procoptions) then
  579. begin
  580. objectlibrary.getaddrlabel(iolabel);
  581. cg.a_label(exprasmlist,iolabel);
  582. end
  583. else
  584. iolabel:=nil;
  585. { save all used registers and possible registers
  586. used for the return value }
  587. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  588. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  589. if (not is_void(resulttype.def)) and
  590. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  591. begin
  592. include(regs_to_push_int,RS_ACCUMULATOR);
  593. {$ifndef cpu64bit}
  594. if resulttype.def.size>sizeof(aword) then
  595. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  596. {$endif cpu64bit}
  597. end;
  598. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  599. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  600. { on the ppc, ever procedure saves the non-volatile registers it uses itself }
  601. { and must make sure it saves its volatile registers before doing a call }
  602. {$ifdef i386}
  603. { give used registers through }
  604. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  605. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  606. {$endif i386}
  607. end
  608. else
  609. begin
  610. regs_to_push_int := all_intregisters;
  611. regs_to_push_other := all_registers;
  612. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  613. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  614. {$ifdef i386}
  615. rg.usedinproc:=all_registers;
  616. {$endif i386}
  617. { no IO check for methods and procedure variables }
  618. iolabel:=nil;
  619. end;
  620. { Initialize for pushing the parameters }
  621. oldpushedparasize:=pushedparasize;
  622. pushedparasize:=0;
  623. { Align stack if required }
  624. pop_size:=align_parasize;
  625. { Push parameters }
  626. oldaktcallnode:=aktcallnode;
  627. aktcallnode:=self;
  628. { process procvar. Done here already, because otherwise it may }
  629. { destroy registers containing a parameter for the actual }
  630. { function call (e.g. if it's a function, its result will }
  631. { overwrite r3, which contains the first parameter) (JM) }
  632. if assigned(right) then
  633. secondpass(right);
  634. if (po_virtualmethod in procdefinition.procoptions) and
  635. assigned(methodpointer) then
  636. begin
  637. secondpass(methodpointer);
  638. location_force_reg(exprasmlist,methodpointer.location,OS_ADDR,false);
  639. { virtual methods require an index }
  640. if tprocdef(procdefinition).extnumber=-1 then
  641. internalerror(200304021);
  642. { VMT should already be loaded in a register }
  643. if methodpointer.location.register.number=NR_NO then
  644. internalerror(200304022);
  645. { test validity of VMT }
  646. if not(is_interface(tprocdef(procdefinition)._class)) and
  647. not(is_cppclass(tprocdef(procdefinition)._class)) then
  648. cg.g_maybe_testvmt(exprasmlist,methodpointer.location.register,tprocdef(procdefinition)._class);
  649. end;
  650. if assigned(left) then
  651. begin
  652. {$ifndef newra}
  653. if assigned(right) then
  654. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  655. else
  656. if assigned(methodpointer) then
  657. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  658. {$endif}
  659. tcallparanode(left).secondcallparan(
  660. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  661. para_alignment,0);
  662. {$ifndef newra}
  663. if assigned(right) then
  664. maybe_restore(exprasmlist,right.location,pushedregs)
  665. else
  666. if assigned(methodpointer) then
  667. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  668. {$endif newra}
  669. end;
  670. aktcallnode:=oldaktcallnode;
  671. { procedure variable or normal function call ? }
  672. if (right=nil) then
  673. begin
  674. { push base pointer ?}
  675. if (current_procdef.parast.symtablelevel>=normal_function_level) and
  676. assigned(tprocdef(procdefinition).parast) and
  677. ((tprocdef(procdefinition).parast.symtablelevel)>normal_function_level) then
  678. push_framepointer;
  679. rg.saveintregvars(exprasmlist,regs_to_push_int);
  680. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  681. if (po_virtualmethod in procdefinition.procoptions) and
  682. assigned(methodpointer) then
  683. begin
  684. { call method }
  685. reference_reset_base(href,methodpointer.location.register,
  686. tprocdef(procdefinition)._class.vmtmethodoffset(tprocdef(procdefinition).extnumber));
  687. cg.a_call_ref(exprasmlist,href);
  688. { release vmt register }
  689. rg.ungetaddressregister(exprasmlist,methodpointer.location.register);
  690. end
  691. else
  692. begin
  693. { Calling interrupt from the same code requires some
  694. extra code }
  695. if (po_interrupt in procdefinition.procoptions) then
  696. extra_interrupt_code;
  697. cg.a_call_name(exprasmlist,tprocdef(procdefinition).mangledname);
  698. end;
  699. end
  700. else
  701. { now procedure variable case }
  702. begin
  703. { Calling interrupt from the same code requires some
  704. extra code }
  705. if (po_interrupt in procdefinition.procoptions) then
  706. extra_interrupt_code;
  707. rg.saveintregvars(exprasmlist,ALL_INTREGISTERS);
  708. rg.saveotherregvars(exprasmlist,ALL_REGISTERS);
  709. cg.a_call_loc(exprasmlist,right.location);
  710. location_release(exprasmlist,right.location);
  711. location_freetemp(exprasmlist,right.location);
  712. end;
  713. { Need to remove the parameters from the stack? }
  714. if (po_clearstack in procdefinition.procoptions) then
  715. begin
  716. { the old pop_size was already included in pushedparasize }
  717. pop_size:=pushedparasize;
  718. { for Cdecl functions we don't need to pop the funcret when it
  719. was pushed by para }
  720. if paramanager.ret_in_param(procdefinition.rettype.def,procdefinition.proccalloption) then
  721. dec(pop_size,POINTER_SIZE);
  722. end;
  723. { Remove parameters/alignment from the stack }
  724. if pop_size>0 then
  725. pop_parasize(pop_size);
  726. {$ifdef powerpc}
  727. { this calculation must be done in pass_1 anyway, so don't worry }
  728. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  729. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  730. {$endif powerpc}
  731. { Restore }
  732. pushedparasize:=oldpushedparasize;
  733. rg.restoreunusedstate(unusedstate);
  734. {$ifdef TEMPREGDEBUG}
  735. testregisters32;
  736. {$endif TEMPREGDEBUG}
  737. { handle function results }
  738. if (not is_void(resulttype.def)) then
  739. handle_return_value
  740. else
  741. location_reset(location,LOC_VOID,OS_NO);
  742. { perhaps i/o check ? }
  743. if iolabel<>nil then
  744. begin
  745. reference_reset_symbol(href,iolabel,0);
  746. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  747. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  748. end;
  749. { restore registers }
  750. rg.restoreusedotherregisters(exprasmlist,pushedother);
  751. rg.restoreusedintregisters(exprasmlist,pushedint);
  752. { release temps of paras }
  753. release_para_temps;
  754. { if return value is not used }
  755. if (not(nf_return_value_used in flags)) and (not is_void(resulttype.def)) then
  756. begin
  757. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  758. begin
  759. { data which must be finalized ? }
  760. if (resulttype.def.needs_inittable) then
  761. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  762. { release unused temp }
  763. tg.ungetiftemp(exprasmlist,location.reference)
  764. end
  765. else if location.loc=LOC_FPUREGISTER then
  766. begin
  767. {$ifdef x86}
  768. { release FPU stack }
  769. accreg.enum:=FPU_RESULT_REG;
  770. emit_reg(A_FSTP,S_NO,accreg);
  771. {
  772. dec(trgcpu(rg).fpuvaroffset);
  773. do NOT decrement as the increment before
  774. is not called for unused results PM }
  775. {$endif x86}
  776. end;
  777. end;
  778. end;
  779. procedure tcgcallnode.inlined_pass_2;
  780. var
  781. regs_to_push_int : Tsupregset;
  782. regs_to_push_other : tregisterset;
  783. unusedstate: pointer;
  784. pushedother : tpushedsavedother;
  785. pushedint : tpushedsavedint;
  786. oldpushedparasize : longint;
  787. { adress returned from an I/O-error }
  788. iolabel : tasmlabel;
  789. { help reference pointer }
  790. href : treference;
  791. pushedregs : tmaybesave;
  792. accreg : tregister;
  793. oldaktcallnode : tcallnode;
  794. oldprocdef : tprocdef;
  795. i : longint;
  796. oldprocinfo : tprocinfo;
  797. oldinlining_procedure : boolean;
  798. inlineentrycode,inlineexitcode : TAAsmoutput;
  799. oldexitlabel:tasmlabel;
  800. oldregstate: pointer;
  801. old_local_fixup,
  802. old_para_fixup : longint;
  803. pararef,
  804. localsref : treference;
  805. {$ifdef GDB}
  806. startlabel,endlabel : tasmlabel;
  807. pp : pchar;
  808. mangled_length : longint;
  809. {$endif GDB}
  810. begin
  811. if not(assigned(procdefinition) and (procdefinition.deftype=procdef)) then
  812. internalerror(200305262);
  813. oldinlining_procedure:=inlining_procedure;
  814. oldexitlabel:=aktexitlabel;
  815. oldprocdef:=current_procdef;
  816. oldprocinfo:=current_procinfo;
  817. objectlibrary.getlabel(aktexitlabel);
  818. { we're inlining a procedure }
  819. inlining_procedure:=true;
  820. { deallocate the registers used for the current procedure's regvars }
  821. if assigned(current_procdef.regvarinfo) then
  822. begin
  823. with pregvarinfo(current_procdef.regvarinfo)^ do
  824. for i := 1 to maxvarregs do
  825. if assigned(regvars[i]) then
  826. store_regvar(exprasmlist,regvars[i].reg);
  827. rg.saveStateForInline(oldregstate);
  828. { make sure the register allocator knows what the regvars in the }
  829. { inlined code block are (JM) }
  830. rg.resetusableregisters;
  831. rg.clearregistercount;
  832. {$ifndef newra}
  833. rg.cleartempgen;
  834. {$endif}
  835. if assigned(tprocdef(procdefinition).regvarinfo) then
  836. with pregvarinfo(tprocdef(procdefinition).regvarinfo)^ do
  837. for i := 1 to maxvarregs do
  838. if assigned(regvars[i]) then
  839. begin
  840. {Fix me!!}
  841. {tmpreg:=rg.makeregsize(regvars[i].reg,OS_INT);
  842. rg.makeregvar(tmpreg);}
  843. internalerror(200301232);
  844. end;
  845. end;
  846. { create temp procinfo }
  847. current_procinfo:=cprocinfo.create(nil);
  848. current_procinfo.procdef:=tprocdef(procdefinition);
  849. current_procdef:=current_procinfo.procdef;
  850. { Localsymtable }
  851. current_procdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  852. if current_procdef.localst.datasize>0 then
  853. begin
  854. old_local_fixup:=current_procdef.localst.address_fixup;
  855. tg.GetTemp(exprasmlist,current_procdef.localst.datasize,tt_persistent,localsref);
  856. if tg.direction>0 then
  857. current_procdef.localst.address_fixup:=localsref.offset
  858. else
  859. current_procdef.localst.address_fixup:=localsref.offset+current_procdef.localst.datasize;
  860. {$ifdef extdebug}
  861. Comment(V_debug,'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup));
  862. exprasmList.concat(tai_comment.Create(strpnew(
  863. 'inlined local symtable ('+tostr(current_procdef.localst.datasize)+' bytes) is at offset '+tostr(current_procdef.localst.address_fixup))));
  864. {$endif extdebug}
  865. end;
  866. { Parasymtable }
  867. current_procdef.parast.symtablelevel:=oldprocdef.localst.symtablelevel;
  868. if current_procdef.parast.datasize>0 then
  869. begin
  870. old_para_fixup:=current_procdef.parast.address_fixup;
  871. tg.GetTemp(exprasmlist,current_procdef.parast.datasize,tt_persistent,pararef);
  872. current_procdef.parast.address_fixup:=pararef.offset;
  873. {$ifdef extdebug}
  874. Comment(V_debug,'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup));
  875. exprasmList.concat(tai_comment.Create(strpnew(
  876. 'inlined para symtable ('+tostr(current_procdef.parast.datasize)+' bytes) is at offset '+tostr(current_procdef.parast.address_fixup))));
  877. {$endif extdebug}
  878. end;
  879. { Calculate offsets }
  880. current_procinfo.after_header;
  881. exprasmList.concat(Tai_Marker.Create(InlineStart));
  882. {$ifdef extdebug}
  883. exprasmList.concat(tai_comment.Create(strpnew('Start of inlined proc')));
  884. {$endif extdebug}
  885. {$ifdef GDB}
  886. if (cs_debuginfo in aktmoduleswitches) then
  887. begin
  888. objectlibrary.getaddrlabel(startlabel);
  889. objectlibrary.getaddrlabel(endlabel);
  890. cg.a_label(exprasmlist,startlabel);
  891. tprocdef(procdefinition).localst.symtabletype:=inlinelocalsymtable;
  892. procdefinition.parast.symtabletype:=inlineparasymtable;
  893. { Here we must include the para and local symtable info }
  894. procdefinition.concatstabto(withdebuglist);
  895. { set it back for safety }
  896. tprocdef(procdefinition).localst.symtabletype:=localsymtable;
  897. procdefinition.parast.symtabletype:=parasymtable;
  898. mangled_length:=length(oldprocdef.mangledname);
  899. getmem(pp,mangled_length+50);
  900. strpcopy(pp,'192,0,0,'+startlabel.name);
  901. if (target_info.use_function_relative_addresses) then
  902. begin
  903. strpcopy(strend(pp),'-');
  904. strpcopy(strend(pp),oldprocdef.mangledname);
  905. end;
  906. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  907. end;
  908. {$endif GDB}
  909. iolabel:=nil;
  910. rg.saveunusedstate(unusedstate);
  911. { if we allocate the temp. location for ansi- or widestrings }
  912. { already here, we avoid later a push/pop }
  913. if is_widestring(resulttype.def) then
  914. begin
  915. tg.GetTemp(exprasmlist,pointer_size,tt_widestring,refcountedtemp);
  916. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  917. end
  918. else if is_ansistring(resulttype.def) then
  919. begin
  920. tg.GetTemp(exprasmlist,pointer_size,tt_ansistring,refcountedtemp);
  921. cg.g_decrrefcount(exprasmlist,resulttype.def,refcountedtemp,false);
  922. end;
  923. if (cs_check_io in aktlocalswitches) and
  924. (po_iocheck in procdefinition.procoptions) and
  925. not(po_iocheck in current_procdef.procoptions) then
  926. begin
  927. objectlibrary.getaddrlabel(iolabel);
  928. cg.a_label(exprasmlist,iolabel);
  929. end
  930. else
  931. iolabel:=nil;
  932. { save all used registers and possible registers
  933. used for the return value }
  934. regs_to_push_int := tprocdef(procdefinition).usedintregisters;
  935. regs_to_push_other := tprocdef(procdefinition).usedotherregisters;
  936. if (not is_void(resulttype.def)) and
  937. (not paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption)) then
  938. begin
  939. include(regs_to_push_int,RS_ACCUMULATOR);
  940. {$ifndef cpu64bit}
  941. if resulttype.def.size>sizeof(aword) then
  942. include(regs_to_push_int,RS_ACCUMULATORHIGH);
  943. {$endif cpu64bit}
  944. end;
  945. rg.saveusedintregisters(exprasmlist,pushedint,regs_to_push_int);
  946. rg.saveusedotherregisters(exprasmlist,pushedother,regs_to_push_other);
  947. {$ifdef i386}
  948. { give used registers through }
  949. rg.usedintinproc:=rg.usedintinproc + tprocdef(procdefinition).usedintregisters;
  950. rg.usedinproc:=rg.usedinproc + tprocdef(procdefinition).usedotherregisters;
  951. {$endif i386}
  952. { Initialize for pushing the parameters }
  953. oldpushedparasize:=pushedparasize;
  954. pushedparasize:=0;
  955. { Push parameters }
  956. oldaktcallnode:=aktcallnode;
  957. aktcallnode:=self;
  958. if assigned(left) then
  959. begin
  960. {$ifndef newra}
  961. if assigned(right) then
  962. maybe_save(exprasmlist,left.registers32,right.location,pushedregs)
  963. else
  964. if assigned(methodpointer) then
  965. maybe_save(exprasmlist,left.registers32,methodpointer.location,pushedregs);
  966. {$endif}
  967. { we push from right to left, so start with parameters at the end of
  968. the parameter block }
  969. tcallparanode(left).secondcallparan(
  970. (po_leftright in procdefinition.procoptions),procdefinition.proccalloption,
  971. 0,procdefinition.parast.address_fixup+procdefinition.parast.datasize);
  972. {$ifndef newra}
  973. if assigned(right) then
  974. maybe_restore(exprasmlist,right.location,pushedregs)
  975. else
  976. if assigned(methodpointer) then
  977. maybe_restore(exprasmlist,methodpointer.location,pushedregs);
  978. {$endif newra}
  979. end;
  980. aktcallnode:=oldaktcallnode;
  981. rg.saveintregvars(exprasmlist,regs_to_push_int);
  982. rg.saveotherregvars(exprasmlist,regs_to_push_other);
  983. { takes care of local data initialization }
  984. inlineentrycode:=TAAsmoutput.Create;
  985. inlineexitcode:=TAAsmoutput.Create;
  986. geninlineentrycode(inlineentrycode,0);
  987. if po_assembler in current_procdef.procoptions then
  988. inlineentrycode.insert(Tai_marker.Create(asmblockstart));
  989. exprasmList.concatlist(inlineentrycode);
  990. { process the inline code }
  991. secondpass(inlinecode);
  992. {$ifdef powerpc}
  993. { this calculation must be done in pass_1 anyway, so don't worry }
  994. if tppcprocinfo(current_procinfo).maxpushedparasize<pushedparasize then
  995. tppcprocinfo(current_procinfo).maxpushedparasize:=pushedparasize;
  996. {$endif powerpc}
  997. { Restore }
  998. pushedparasize:=oldpushedparasize;
  999. rg.restoreunusedstate(unusedstate);
  1000. {$ifdef TEMPREGDEBUG}
  1001. testregisters32;
  1002. {$endif TEMPREGDEBUG}
  1003. geninlineexitcode(inlineexitcode,true);
  1004. if po_assembler in current_procdef.procoptions then
  1005. inlineexitcode.concat(Tai_marker.Create(asmblockend));
  1006. exprasmList.concatlist(inlineexitcode);
  1007. inlineentrycode.free;
  1008. inlineexitcode.free;
  1009. {$ifdef extdebug}
  1010. exprasmList.concat(tai_comment.Create(strpnew('End of inlined proc')));
  1011. {$endif extdebug}
  1012. exprasmList.concat(Tai_Marker.Create(InlineEnd));
  1013. {we can free the local data now, reset also the fixup address }
  1014. if current_procdef.localst.datasize>0 then
  1015. begin
  1016. tg.UnGetTemp(exprasmlist,localsref);
  1017. current_procdef.localst.address_fixup:=old_local_fixup;
  1018. end;
  1019. {we can free the para data now, reset also the fixup address }
  1020. if current_procdef.parast.datasize>0 then
  1021. begin
  1022. tg.UnGetTemp(exprasmlist,pararef);
  1023. current_procdef.parast.address_fixup:=old_para_fixup;
  1024. end;
  1025. { free return reference }
  1026. if (resulttype.def.size>0) then
  1027. begin
  1028. { from now on the result can be freed normally }
  1029. // if assigned(funcretnode) and
  1030. // paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  1031. // tg.ChangeTempType(exprasmlist,funcretnode.location.reference,tt_normal);
  1032. end;
  1033. { handle function results }
  1034. if (not is_void(resulttype.def)) then
  1035. handle_return_value
  1036. else
  1037. location_reset(location,LOC_VOID,OS_NO);
  1038. { perhaps i/o check ? }
  1039. if iolabel<>nil then
  1040. begin
  1041. reference_reset_symbol(href,iolabel,0);
  1042. cg.a_paramaddr_ref(exprasmlist,href,paramanager.getintparaloc(1));
  1043. cg.a_call_name(exprasmlist,'FPC_IOCHECK');
  1044. end;
  1045. { restore registers }
  1046. rg.restoreusedotherregisters(exprasmlist,pushedother);
  1047. rg.restoreusedintregisters(exprasmlist,pushedint);
  1048. { release temps of paras }
  1049. release_para_temps;
  1050. { if return value is not used }
  1051. if (not is_void(resulttype.def)) and
  1052. (not(nf_return_value_used in flags)) then
  1053. begin
  1054. if location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  1055. begin
  1056. { data which must be finalized ? }
  1057. if (resulttype.def.needs_inittable) then
  1058. cg.g_finalize(exprasmlist,resulttype.def,location.reference,false);
  1059. { release unused temp }
  1060. tg.ungetiftemp(exprasmlist,location.reference)
  1061. end
  1062. else if location.loc=LOC_FPUREGISTER then
  1063. begin
  1064. {$ifdef x86}
  1065. { release FPU stack }
  1066. accreg.enum:=FPU_RESULT_REG;
  1067. emit_reg(A_FSTP,S_NO,accreg);
  1068. {
  1069. dec(trgcpu(rg).fpuvaroffset);
  1070. do NOT decrement as the increment before
  1071. is not called for unused results PM }
  1072. {$endif x86}
  1073. end;
  1074. end;
  1075. { release procinfo }
  1076. current_procinfo.free;
  1077. current_procinfo:=oldprocinfo;
  1078. {$ifdef GDB}
  1079. if (cs_debuginfo in aktmoduleswitches) then
  1080. begin
  1081. cg.a_label(exprasmlist,endlabel);
  1082. strpcopy(pp,'224,0,0,'+endlabel.name);
  1083. if (target_info.use_function_relative_addresses) then
  1084. begin
  1085. strpcopy(strend(pp),'-');
  1086. strpcopy(strend(pp),oldprocdef.mangledname);
  1087. end;
  1088. withdebugList.concat(Tai_stabn.Create(strnew(pp)));
  1089. freemem(pp,mangled_length+50);
  1090. end;
  1091. {$endif GDB}
  1092. { restore }
  1093. current_procdef:=oldprocdef;
  1094. aktexitlabel:=oldexitlabel;
  1095. inlining_procedure:=oldinlining_procedure;
  1096. { reallocate the registers used for the current procedure's regvars, }
  1097. { since they may have been used and then deallocated in the inlined }
  1098. { procedure (JM) }
  1099. if assigned(current_procdef.regvarinfo) then
  1100. rg.restoreStateAfterInline(oldregstate);
  1101. end;
  1102. procedure tcgcallnode.pass_2;
  1103. begin
  1104. if assigned(inlinecode) then
  1105. inlined_pass_2
  1106. else
  1107. normal_pass_2;
  1108. end;
  1109. begin
  1110. ccallparanode:=tcgcallparanode;
  1111. ccallnode:=tcgcallnode;
  1112. end.
  1113. {
  1114. $Log$
  1115. Revision 1.77 2003-05-29 10:05:40 jonas
  1116. * free callparatemps created for call-by-reference parameters
  1117. Revision 1.76 2003/05/28 23:58:18 jonas
  1118. * added missing initialization of rg.usedint{in,by}proc
  1119. * ppc now also saves/restores used fpu registers
  1120. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  1121. i386
  1122. Revision 1.75 2003/05/26 21:17:17 peter
  1123. * procinlinenode removed
  1124. * aktexit2label removed, fast exit removed
  1125. + tcallnode.inlined_pass_2 added
  1126. Revision 1.74 2003/05/25 11:34:17 peter
  1127. * methodpointer self pushing fixed
  1128. Revision 1.73 2003/05/25 08:59:16 peter
  1129. * inline fixes
  1130. Revision 1.72 2003/05/24 13:36:54 jonas
  1131. * save fpu results in a normal fpu register on non-x86 processors
  1132. Revision 1.71 2003/05/23 19:35:50 jonas
  1133. - undid previous commit, it was wrong
  1134. Revision 1.70 2003/05/23 19:11:58 jonas
  1135. * fixed tests for whether a certain int register is unused
  1136. Revision 1.69 2003/05/23 18:01:56 jonas
  1137. * fixed ppc compiler
  1138. Revision 1.68 2003/05/23 14:27:35 peter
  1139. * remove some unit dependencies
  1140. * current_procinfo changes to store more info
  1141. Revision 1.67 2003/05/17 13:30:08 jonas
  1142. * changed tt_persistant to tt_persistent :)
  1143. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1144. temps, but a ttemptype, so you can also create ansistring temps etc
  1145. Revision 1.66 2003/05/16 14:33:31 peter
  1146. * regvar fixes
  1147. Revision 1.65 2003/05/15 18:58:53 peter
  1148. * removed selfpointer_offset, vmtpointer_offset
  1149. * tvarsym.adjusted_address
  1150. * address in localsymtable is now in the real direction
  1151. * removed some obsolete globals
  1152. Revision 1.64 2003/05/14 19:36:54 jonas
  1153. * patch from Peter for int64 function results
  1154. Revision 1.63 2003/05/13 19:14:41 peter
  1155. * failn removed
  1156. * inherited result code check moven to pexpr
  1157. Revision 1.62 2003/05/13 15:18:18 peter
  1158. * generate code for procvar first before pushing parameters. Made
  1159. the already existing code for powerpc available for all platforms
  1160. Revision 1.61 2003/05/12 18:17:55 jonas
  1161. * moved fpc_check_object call earlier for the ppc, so it can't destroy
  1162. already-loaded parameter registers
  1163. Revision 1.60 2003/05/11 21:48:38 jonas
  1164. * fixed procvar bug on the ppc (load procvar before loading para's,
  1165. because the procvar may otherwise destroy the already loaded paras)
  1166. Revision 1.59 2003/05/09 17:47:02 peter
  1167. * self moved to hidden parameter
  1168. * removed hdisposen,hnewn,selfn
  1169. Revision 1.58 2003/05/05 14:53:16 peter
  1170. * vs_hidden replaced by is_hidden boolean
  1171. Revision 1.57 2003/04/30 20:53:32 florian
  1172. * error when address of an abstract method is taken
  1173. * fixed some x86-64 problems
  1174. * merged some more x86-64 and i386 code
  1175. Revision 1.56 2003/04/29 07:28:52 michael
  1176. + Patch from peter to fix wrong pushing of ansistring function results in open array
  1177. Revision 1.55 2003/04/27 11:21:33 peter
  1178. * aktprocdef renamed to current_procdef
  1179. * procinfo renamed to current_procinfo
  1180. * procinfo will now be stored in current_module so it can be
  1181. cleaned up properly
  1182. * gen_main_procsym changed to create_main_proc and release_main_proc
  1183. to also generate a tprocinfo structure
  1184. * fixed unit implicit initfinal
  1185. Revision 1.54 2003/04/27 07:29:50 peter
  1186. * current_procdef cleanup, current_procdef is now always nil when parsing
  1187. a new procdef declaration
  1188. * aktprocsym removed
  1189. * lexlevel removed, use symtable.symtablelevel instead
  1190. * implicit init/final code uses the normal genentry/genexit
  1191. * funcret state checking updated for new funcret handling
  1192. Revision 1.53 2003/04/25 20:59:33 peter
  1193. * removed funcretn,funcretsym, function result is now in varsym
  1194. and aliases for result and function name are added using absolutesym
  1195. * vs_hidden parameter for funcret passed in parameter
  1196. * vs_hidden fixes
  1197. * writenode changed to printnode and released from extdebug
  1198. * -vp option added to generate a tree.log with the nodetree
  1199. * nicer printnode for statements, callnode
  1200. Revision 1.52 2003/04/25 08:25:26 daniel
  1201. * Ifdefs around a lot of calls to cleartempgen
  1202. * Fixed registers that are allocated but not freed in several nodes
  1203. * Tweak to register allocator to cause less spills
  1204. * 8-bit registers now interfere with esi,edi and ebp
  1205. Compiler can now compile rtl successfully when using new register
  1206. allocator
  1207. Revision 1.51 2003/04/22 23:50:22 peter
  1208. * firstpass uses expectloc
  1209. * checks if there are differences between the expectloc and
  1210. location.loc from secondpass in EXTDEBUG
  1211. Revision 1.50 2003/04/22 14:33:38 peter
  1212. * removed some notes/hints
  1213. Revision 1.49 2003/04/22 13:47:08 peter
  1214. * fixed C style array of const
  1215. * fixed C array passing
  1216. * fixed left to right with high parameters
  1217. Revision 1.48 2003/04/22 10:09:34 daniel
  1218. + Implemented the actual register allocator
  1219. + Scratch registers unavailable when new register allocator used
  1220. + maybe_save/maybe_restore unavailable when new register allocator used
  1221. Revision 1.47 2003/04/22 09:49:44 peter
  1222. * do not load self when calling a non-inherited class constructor
  1223. Revision 1.46 2003/04/21 20:03:32 peter
  1224. * forgot to copy vmtrefaddr to selfrefaddr when self=vmt
  1225. Revision 1.45 2003/04/21 13:53:16 jonas
  1226. - removed copying of all paras when secondpassing a callnode (this used
  1227. to be necessary for inlinign support, but currently the whole inlined
  1228. procedure is already copied in advance). Note that the compiler crashes
  1229. when compiling ucomplex with -dTEST_INLINE (also after fixing the
  1230. syntax errors), but that was also the case before this change.
  1231. Revision 1.44 2003/04/10 17:57:52 peter
  1232. * vs_hidden released
  1233. Revision 1.43 2003/04/06 21:11:23 olle
  1234. * changed newasmsymbol to newasmsymboldata for data symbols
  1235. Revision 1.42 2003/04/04 15:38:56 peter
  1236. * moved generic code from n386cal to ncgcal, i386 now also
  1237. uses the generic ncgcal
  1238. Revision 1.41 2003/03/28 19:16:56 peter
  1239. * generic constructor working for i386
  1240. * remove fixed self register
  1241. * esi added as address register for i386
  1242. Revision 1.40 2003/03/06 11:35:50 daniel
  1243. * Fixed internalerror 7843 issue
  1244. Revision 1.39 2003/02/19 22:00:14 daniel
  1245. * Code generator converted to new register notation
  1246. - Horribily outdated todo.txt removed
  1247. Revision 1.38 2003/02/15 22:17:38 carl
  1248. * bugfix of FPU emulation code
  1249. Revision 1.37 2003/02/12 22:10:07 carl
  1250. * load_frame_pointer is now generic
  1251. * change fpu emulation routine names
  1252. Revision 1.36 2003/01/30 21:46:57 peter
  1253. * self fixes for static methods (merged)
  1254. Revision 1.35 2003/01/22 20:45:15 mazen
  1255. * making math code in RTL compiling.
  1256. *NB : This does NOT mean necessary that it will generate correct code!
  1257. Revision 1.34 2003/01/17 12:03:45 daniel
  1258. * Optalign conditional code adapted to record Tregister
  1259. Revision 1.33 2003/01/08 18:43:56 daniel
  1260. * Tregister changed into a record
  1261. Revision 1.32 2002/12/15 22:50:00 florian
  1262. + some stuff for the new hidden parameter handling added
  1263. Revision 1.31 2002/12/15 21:30:12 florian
  1264. * tcallnode.paraitem introduced, all references to defcoll removed
  1265. Revision 1.30 2002/11/27 20:04:39 peter
  1266. * cdecl array of const fixes
  1267. Revision 1.29 2002/11/25 17:43:17 peter
  1268. * splitted defbase in defutil,symutil,defcmp
  1269. * merged isconvertable and is_equal into compare_defs(_ext)
  1270. * made operator search faster by walking the list only once
  1271. Revision 1.28 2002/11/18 17:31:54 peter
  1272. * pass proccalloption to ret_in_xxx and push_xxx functions
  1273. Revision 1.27 2002/11/16 15:34:30 florian
  1274. * generic location for float results
  1275. Revision 1.26 2002/11/15 01:58:51 peter
  1276. * merged changes from 1.0.7 up to 04-11
  1277. - -V option for generating bug report tracing
  1278. - more tracing for option parsing
  1279. - errors for cdecl and high()
  1280. - win32 import stabs
  1281. - win32 records<=8 are returned in eax:edx (turned off by default)
  1282. - heaptrc update
  1283. - more info for temp management in .s file with EXTDEBUG
  1284. Revision 1.25 2002/10/05 12:43:25 carl
  1285. * fixes for Delphi 6 compilation
  1286. (warning : Some features do not work under Delphi)
  1287. Revision 1.24 2002/09/30 07:00:45 florian
  1288. * fixes to common code to get the alpha compiler compiled applied
  1289. Revision 1.23 2002/09/17 18:54:02 jonas
  1290. * a_load_reg_reg() now has two size parameters: source and dest. This
  1291. allows some optimizations on architectures that don't encode the
  1292. register size in the register name.
  1293. Revision 1.22 2002/09/07 15:25:02 peter
  1294. * old logs removed and tabs fixed
  1295. Revision 1.21 2002/09/07 11:50:02 jonas
  1296. * fixed small regalloction info bug
  1297. Revision 1.20 2002/09/02 11:25:20 florian
  1298. * fixed generic procedure variable calling
  1299. Revision 1.19 2002/09/01 21:04:48 florian
  1300. * several powerpc related stuff fixed
  1301. Revision 1.18 2002/09/01 18:43:27 peter
  1302. * include accumulator in regs_to_push list
  1303. Revision 1.17 2002/09/01 12:13:00 peter
  1304. * use a_call_reg
  1305. * ungetiftemp for procvar of object temp
  1306. Revision 1.16 2002/08/25 19:25:18 peter
  1307. * sym.insert_in_data removed
  1308. * symtable.insertvardata/insertconstdata added
  1309. * removed insert_in_data call from symtable.insert, it needs to be
  1310. called separatly. This allows to deref the address calculation
  1311. * procedures now calculate the parast addresses after the procedure
  1312. directives are parsed. This fixes the cdecl parast problem
  1313. * push_addr_param has an extra argument that specifies if cdecl is used
  1314. or not
  1315. Revision 1.15 2002/08/23 16:14:48 peter
  1316. * tempgen cleanup
  1317. * tt_noreuse temp type added that will be used in genentrycode
  1318. Revision 1.14 2002/08/20 16:55:38 peter
  1319. * don't write (stabs)line info when inlining a procedure
  1320. Revision 1.13 2002/08/19 19:36:42 peter
  1321. * More fixes for cross unit inlining, all tnodes are now implemented
  1322. * Moved pocall_internconst to po_internconst because it is not a
  1323. calling type at all and it conflicted when inlining of these small
  1324. functions was requested
  1325. Revision 1.12 2002/08/18 20:06:23 peter
  1326. * inlining is now also allowed in interface
  1327. * renamed write/load to ppuwrite/ppuload
  1328. * tnode storing in ppu
  1329. * nld,ncon,nbas are already updated for storing in ppu
  1330. Revision 1.11 2002/08/17 22:09:44 florian
  1331. * result type handling in tcgcal.pass_2 overhauled
  1332. * better tnode.dowrite
  1333. * some ppc stuff fixed
  1334. Revision 1.10 2002/08/17 09:23:35 florian
  1335. * first part of procinfo rewrite
  1336. Revision 1.9 2002/08/13 21:40:55 florian
  1337. * more fixes for ppc calling conventions
  1338. Revision 1.8 2002/08/13 18:01:51 carl
  1339. * rename swatoperands to swapoperands
  1340. + m68k first compilable version (still needs a lot of testing):
  1341. assembler generator, system information , inline
  1342. assembler reader.
  1343. Revision 1.7 2002/08/12 15:08:39 carl
  1344. + stab register indexes for powerpc (moved from gdb to cpubase)
  1345. + tprocessor enumeration moved to cpuinfo
  1346. + linker in target_info is now a class
  1347. * many many updates for m68k (will soon start to compile)
  1348. - removed some ifdef or correct them for correct cpu
  1349. Revision 1.6 2002/08/11 14:32:26 peter
  1350. * renamed current_library to objectlibrary
  1351. Revision 1.5 2002/08/11 13:24:11 peter
  1352. * saving of asmsymbols in ppu supported
  1353. * asmsymbollist global is removed and moved into a new class
  1354. tasmlibrarydata that will hold the info of a .a file which
  1355. corresponds with a single module. Added librarydata to tmodule
  1356. to keep the library info stored for the module. In the future the
  1357. objectfiles will also be stored to the tasmlibrarydata class
  1358. * all getlabel/newasmsymbol and friends are moved to the new class
  1359. Revision 1.4 2002/08/06 20:55:20 florian
  1360. * first part of ppc calling conventions fix
  1361. Revision 1.3 2002/07/20 11:57:53 florian
  1362. * types.pas renamed to defbase.pas because D6 contains a types
  1363. unit so this would conflicts if D6 programms are compiled
  1364. + Willamette/SSE2 instructions to assembler added
  1365. Revision 1.2 2002/07/13 19:38:43 florian
  1366. * some more generic calling stuff fixed
  1367. }