ncgcal.pas 62 KB

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