psub.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing and codegeneration at subroutine level
  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 by
  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 psub;
  19. {$i fpcdefs.inc}
  20. {$define NOOPT}
  21. interface
  22. uses
  23. cclasses,globals,
  24. node,
  25. symdef,cgbase;
  26. type
  27. tcgprocinfo=class(tprocinfo)
  28. { code for the subroutine as tree }
  29. code : tnode;
  30. { positions in the tree for init/final }
  31. initasmnode,
  32. finalasmnode : tnode;
  33. { list to store the procinfo's of the nested procedures }
  34. nestedprocs : tlinkedlist;
  35. constructor create(aparent:tprocinfo);override;
  36. destructor destroy;override;
  37. procedure generate_code;
  38. procedure resetprocdef;
  39. procedure add_to_symtablestack;
  40. procedure remove_from_symtablestack;
  41. procedure parse_body;
  42. end;
  43. procedure printnode_reset;
  44. { reads the declaration blocks }
  45. procedure read_declarations(islibrary : boolean);
  46. { reads declarations in the interface part of a unit }
  47. procedure read_interface_declarations;
  48. procedure add_entry_exit_code(var code:tnode;const entrypos,exitpos:tfileposinfo);
  49. implementation
  50. uses
  51. { common }
  52. cutils,
  53. { global }
  54. globtype,tokens,verbose,comphook,
  55. systems,
  56. { aasm }
  57. cpubase,cpuinfo,aasmbase,aasmtai,
  58. { symtable }
  59. symconst,symbase,symsym,symtype,symtable,defutil,
  60. paramgr,
  61. ppu,fmodule,
  62. { pass 1 }
  63. nutils,nbas,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  64. pass_1,
  65. {$ifdef state_tracking}
  66. nstate,
  67. {$endif state_tracking}
  68. { pass 2 }
  69. {$ifndef NOPASS2}
  70. pass_2,
  71. {$endif}
  72. { parser }
  73. scanner,
  74. pbase,pstatmnt,pdecl,pdecsub,pexports,
  75. { codegen }
  76. tgobj,rgobj,cgobj,
  77. ncgutil,regvars
  78. {$ifndef NOOPT}
  79. {$ifdef i386}
  80. ,aopt386
  81. {$else i386}
  82. ,aoptcpu
  83. {$endif i386}
  84. {$endif}
  85. ;
  86. const
  87. { Maximum number of loops when spilling registers }
  88. maxspillingcounter = 20;
  89. {****************************************************************************
  90. PROCEDURE/FUNCTION BODY PARSING
  91. ****************************************************************************}
  92. procedure initializevars(p:tnamedindexitem;arg:pointer);
  93. var
  94. b : tblocknode;
  95. begin
  96. if tsym(p).typ<>varsym then
  97. exit;
  98. with tvarsym(p) do
  99. begin
  100. if assigned(defaultconstsym) then
  101. begin
  102. b:=tblocknode(arg);
  103. b.left:=cstatementnode.create(
  104. cassignmentnode.create(
  105. cloadnode.create(tsym(p),tsym(p).owner),
  106. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  107. b.left);
  108. end;
  109. end;
  110. end;
  111. function block(islibrary : boolean) : tnode;
  112. begin
  113. { parse const,types and vars }
  114. read_declarations(islibrary);
  115. current_procinfo.handle_body_start;
  116. { do we have an assembler block without the po_assembler?
  117. we should allow this for Delphi compatibility (PFV) }
  118. if (token=_ASM) and (m_delphi in aktmodeswitches) then
  119. include(current_procinfo.procdef.procoptions,po_assembler);
  120. { Handle assembler block different }
  121. if (po_assembler in current_procinfo.procdef.procoptions) then
  122. begin
  123. block:=assembler_block;
  124. exit;
  125. end;
  126. {Unit initialization?.}
  127. if (
  128. assigned(current_procinfo.procdef.localst) and
  129. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  130. (current_module.is_unit)
  131. ) or
  132. islibrary then
  133. begin
  134. if (token=_END) then
  135. begin
  136. consume(_END);
  137. { We need at least a node, else the entry/exit code is not
  138. generated and thus no PASCALMAIN symbol which we need (PFV) }
  139. if islibrary then
  140. block:=cnothingnode.create
  141. else
  142. block:=nil;
  143. end
  144. else
  145. begin
  146. if token=_INITIALIZATION then
  147. begin
  148. { The library init code is already called and does not
  149. need to be in the initfinal table (PFV) }
  150. if not islibrary then
  151. current_module.flags:=current_module.flags or uf_init;
  152. block:=statement_block(_INITIALIZATION);
  153. end
  154. else if (token=_FINALIZATION) then
  155. begin
  156. if (current_module.flags and uf_finalize)<>0 then
  157. block:=statement_block(_FINALIZATION)
  158. else
  159. begin
  160. { can we allow no INITIALIZATION for DLL ??
  161. I think it should work PM }
  162. block:=nil;
  163. exit;
  164. end;
  165. end
  166. else
  167. begin
  168. { The library init code is already called and does not
  169. need to be in the initfinal table (PFV) }
  170. if not islibrary then
  171. current_module.flags:=current_module.flags or uf_init;
  172. block:=statement_block(_BEGIN);
  173. end;
  174. end;
  175. end
  176. else
  177. begin
  178. block:=statement_block(_BEGIN);
  179. if symtablestack.symtabletype=localsymtable then
  180. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}initializevars,block);
  181. end;
  182. if (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  183. (not current_module.is_unit) then
  184. begin
  185. { there's always a call to FPC_DO_EXIT in the main program }
  186. include(current_procinfo.flags,pi_do_call);
  187. end;
  188. if ([cs_check_range,cs_check_overflow] * aktlocalswitches <> []) then
  189. include(current_procinfo.flags,pi_do_call);
  190. end;
  191. {****************************************************************************
  192. PROCEDURE/FUNCTION COMPILING
  193. ****************************************************************************}
  194. procedure printnode_reset;
  195. begin
  196. assign(printnodefile,treelogfilename);
  197. {$I-}
  198. rewrite(printnodefile);
  199. {$I+}
  200. if ioresult<>0 then
  201. begin
  202. Comment(V_Error,'Error creating '+treelogfilename);
  203. exit;
  204. end;
  205. close(printnodefile);
  206. end;
  207. procedure printnode_procdef(pd:tprocdef);
  208. begin
  209. assign(printnodefile,treelogfilename);
  210. {$I-}
  211. append(printnodefile);
  212. if ioresult<>0 then
  213. rewrite(printnodefile);
  214. {$I+}
  215. if ioresult<>0 then
  216. begin
  217. Comment(V_Error,'Error creating '+treelogfilename);
  218. exit;
  219. end;
  220. writeln(printnodefile);
  221. writeln(printnodefile,'*******************************************************************************');
  222. writeln(printnodefile,current_procinfo.procdef.fullprocname(false));
  223. writeln(printnodefile,'*******************************************************************************');
  224. printnode(printnodefile,pd.code);
  225. close(printnodefile);
  226. end;
  227. function generate_initialize_block:tnode;
  228. var
  229. srsym : tsym;
  230. para : tcallparanode;
  231. newstatement : tstatementnode;
  232. htype : ttype;
  233. begin
  234. result:=internalstatements(newstatement,true);
  235. { temp/para/locals initialize code will be inserted here }
  236. tcgprocinfo(current_procinfo).initasmnode:=casmnode.create_get_position;
  237. addstatement(newstatement,tcgprocinfo(current_procinfo).initasmnode);
  238. if assigned(current_procinfo.procdef._class) then
  239. begin
  240. { a constructor needs a help procedure }
  241. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  242. begin
  243. if is_class(current_procinfo.procdef._class) then
  244. begin
  245. if (cs_implicit_exceptions in aktmoduleswitches) then
  246. include(current_procinfo.flags,pi_needs_implicit_finally);
  247. srsym:=search_class_member(current_procinfo.procdef._class,'NEWINSTANCE');
  248. if assigned(srsym) and
  249. (srsym.typ=procsym) then
  250. begin
  251. { if vmt<>0 then newinstance }
  252. addstatement(newstatement,cifnode.create(
  253. caddnode.create(unequaln,
  254. load_vmt_pointer_node,
  255. cnilnode.create),
  256. cassignmentnode.create(
  257. ctypeconvnode.create_explicit(
  258. load_self_pointer_node,
  259. voidpointertype),
  260. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_vmt_pointer_node)),
  261. nil));
  262. end
  263. else
  264. internalerror(200305108);
  265. end
  266. else
  267. if is_object(current_procinfo.procdef._class) then
  268. begin
  269. htype.setdef(current_procinfo.procdef._class);
  270. htype.setdef(tpointerdef.create(htype));
  271. { parameter 3 : vmt_offset }
  272. { parameter 2 : address of pointer to vmt,
  273. this is required to allow setting the vmt to -1 to indicate
  274. that memory was allocated }
  275. { parameter 1 : self pointer }
  276. para:=ccallparanode.create(
  277. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32bittype,false),
  278. ccallparanode.create(
  279. ctypeconvnode.create_explicit(
  280. load_vmt_pointer_node,
  281. voidpointertype),
  282. ccallparanode.create(
  283. ctypeconvnode.create_explicit(
  284. load_self_pointer_node,
  285. voidpointertype),
  286. nil)));
  287. addstatement(newstatement,cassignmentnode.create(
  288. ctypeconvnode.create_explicit(
  289. load_self_pointer_node,
  290. voidpointertype),
  291. ccallnode.createintern('fpc_help_constructor',para)));
  292. end
  293. else
  294. internalerror(200305103);
  295. { if self=nil then exit
  296. calling fail instead of exit is useless because
  297. there is nothing to dispose (PFV) }
  298. addstatement(newstatement,cifnode.create(
  299. caddnode.create(equaln,
  300. load_self_pointer_node,
  301. cnilnode.create),
  302. cexitnode.create(nil),
  303. nil));
  304. end;
  305. { maybe call BeforeDestruction for classes }
  306. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  307. is_class(current_procinfo.procdef._class) then
  308. begin
  309. srsym:=search_class_member(current_procinfo.procdef._class,'BEFOREDESTRUCTION');
  310. if assigned(srsym) and
  311. (srsym.typ=procsym) then
  312. begin
  313. { if vmt<>0 then beforedestruction }
  314. addstatement(newstatement,cifnode.create(
  315. caddnode.create(unequaln,
  316. load_vmt_pointer_node,
  317. cnilnode.create),
  318. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  319. nil));
  320. end
  321. else
  322. internalerror(200305104);
  323. end;
  324. end;
  325. end;
  326. function generate_finalize_block:tnode;
  327. begin
  328. { temp/para/locals finalize code will be inserted here }
  329. tcgprocinfo(current_procinfo).finalasmnode:=casmnode.create_get_position;
  330. result:=tcgprocinfo(current_procinfo).finalasmnode;
  331. end;
  332. function generate_entry_block:tnode;
  333. begin
  334. result:=cnothingnode.create;
  335. end;
  336. function generate_exit_block:tnode;
  337. var
  338. srsym : tsym;
  339. para : tcallparanode;
  340. newstatement : tstatementnode;
  341. begin
  342. generate_exit_block:=internalstatements(newstatement,true);
  343. if assigned(current_procinfo.procdef._class) then
  344. begin
  345. { maybe call AfterConstruction for classes }
  346. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  347. is_class(current_procinfo.procdef._class) then
  348. begin
  349. srsym:=search_class_member(current_procinfo.procdef._class,'AFTERCONSTRUCTION');
  350. if assigned(srsym) and
  351. (srsym.typ=procsym) then
  352. begin
  353. { if vmt<>0 then afterconstruction }
  354. addstatement(newstatement,cifnode.create(
  355. caddnode.create(unequaln,
  356. load_vmt_pointer_node,
  357. cnilnode.create),
  358. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  359. nil));
  360. end
  361. else
  362. internalerror(200305106);
  363. end;
  364. { a destructor needs a help procedure }
  365. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  366. begin
  367. if is_class(current_procinfo.procdef._class) then
  368. begin
  369. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  370. if assigned(srsym) and
  371. (srsym.typ=procsym) then
  372. begin
  373. { if self<>0 and vmt=1 then freeinstance }
  374. addstatement(newstatement,cifnode.create(
  375. caddnode.create(andn,
  376. caddnode.create(unequaln,
  377. load_self_pointer_node,
  378. cnilnode.create),
  379. caddnode.create(equaln,
  380. ctypeconvnode.create(
  381. load_vmt_pointer_node,
  382. voidpointertype),
  383. cpointerconstnode.create(1,voidpointertype))),
  384. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  385. nil));
  386. end
  387. else
  388. internalerror(200305108);
  389. end
  390. else
  391. if is_object(current_procinfo.procdef._class) then
  392. begin
  393. { finalize object data }
  394. if current_procinfo.procdef._class.needs_inittable then
  395. addstatement(newstatement,finalize_data_node(load_self_node));
  396. { parameter 3 : vmt_offset }
  397. { parameter 2 : pointer to vmt }
  398. { parameter 1 : self pointer }
  399. para:=ccallparanode.create(
  400. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32bittype,false),
  401. ccallparanode.create(
  402. ctypeconvnode.create_explicit(
  403. load_vmt_pointer_node,
  404. voidpointertype),
  405. ccallparanode.create(
  406. ctypeconvnode.create_explicit(
  407. load_self_pointer_node,
  408. voidpointertype),
  409. nil)));
  410. addstatement(newstatement,
  411. ccallnode.createintern('fpc_help_destructor',para));
  412. end
  413. else
  414. internalerror(200305105);
  415. end;
  416. end;
  417. end;
  418. function generate_except_block:tnode;
  419. var
  420. pd : tprocdef;
  421. newstatement : tstatementnode;
  422. begin
  423. generate_except_block:=internalstatements(newstatement,true);
  424. { a constructor needs call destructor (if available) when it
  425. is not inherited }
  426. if assigned(current_procinfo.procdef._class) and
  427. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  428. begin
  429. pd:=current_procinfo.procdef._class.searchdestructor;
  430. if assigned(pd) then
  431. begin
  432. { if vmt<>0 then call destructor }
  433. addstatement(newstatement,cifnode.create(
  434. caddnode.create(unequaln,
  435. load_vmt_pointer_node,
  436. cnilnode.create),
  437. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node),
  438. nil));
  439. end;
  440. end
  441. else
  442. begin
  443. { no constructor }
  444. { must be the return value finalized before reraising the exception? }
  445. if (not is_void(current_procinfo.procdef.rettype.def)) and
  446. (current_procinfo.procdef.rettype.def.needs_inittable) and
  447. (not is_class(current_procinfo.procdef.rettype.def)) then
  448. finalize_data_node(load_result_node);
  449. end;
  450. end;
  451. procedure add_entry_exit_code(var code:tnode;const entrypos,exitpos:tfileposinfo);
  452. var
  453. initializecode,
  454. finalizecode,
  455. entrycode,
  456. exitcode,
  457. exceptcode : tnode;
  458. codeblock,
  459. newblock : tblocknode;
  460. codestatement,
  461. newstatement : tstatementnode;
  462. oldfilepos : tfileposinfo;
  463. begin
  464. oldfilepos:=aktfilepos;
  465. { Generate entry,exit and init,final blocks }
  466. aktfilepos:=entrypos;
  467. initializecode:=generate_initialize_block;
  468. entrycode:=generate_entry_block;
  469. aktfilepos:=exitpos;
  470. exitcode:=generate_exit_block;
  471. finalizecode:=generate_finalize_block;
  472. exceptcode:=generate_except_block;
  473. { Generate body of the procedure by combining entry+body+exit }
  474. codeblock:=internalstatements(codestatement,true);
  475. addstatement(codestatement,entrycode);
  476. addstatement(codestatement,code);
  477. addstatement(codestatement,exitcode);
  478. { Generate procedure by combining init+body+final,
  479. depending on the implicit finally we need to add
  480. an try...finally...end wrapper }
  481. newblock:=internalstatements(newstatement,true);
  482. if (pi_needs_implicit_finally in current_procinfo.flags) and
  483. { but it's useless in init/final code of units }
  484. not(current_procinfo.procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) then
  485. begin
  486. addstatement(newstatement,initializecode);
  487. aktfilepos:=entrypos;
  488. addstatement(newstatement,ctryfinallynode.create_implicit(
  489. codeblock,
  490. finalizecode,
  491. exceptcode));
  492. end
  493. else
  494. begin
  495. addstatement(newstatement,initializecode);
  496. addstatement(newstatement,codeblock);
  497. addstatement(newstatement,finalizecode);
  498. end;
  499. resulttypepass(newblock);
  500. code:=newblock;
  501. aktfilepos:=oldfilepos;
  502. end;
  503. {****************************************************************************
  504. TCGProcInfo
  505. ****************************************************************************}
  506. constructor tcgprocinfo.create(aparent:tprocinfo);
  507. begin
  508. inherited Create(aparent);
  509. nestedprocs:=tlinkedlist.create;
  510. end;
  511. destructor tcgprocinfo.destroy;
  512. begin
  513. nestedprocs.free;
  514. inherited destroy;
  515. end;
  516. procedure tcgprocinfo.generate_code;
  517. var
  518. oldprocinfo : tprocinfo;
  519. oldaktmaxfpuregisters : longint;
  520. oldfilepos : tfileposinfo;
  521. templist : Taasmoutput;
  522. headertai : tai;
  523. usesacc,
  524. usesfpu,
  525. usesacchi : boolean;
  526. spillingcounter : integer;
  527. fastspill:boolean;
  528. begin
  529. { the initialization procedure can be empty, then we
  530. don't need to generate anything. When it was an empty
  531. procedure there would be at least a blocknode }
  532. if not assigned(code) then
  533. exit;
  534. { The RA and Tempgen shall not be available yet }
  535. if assigned(rg) or assigned(tg) then
  536. internalerror(200309201);
  537. oldprocinfo:=current_procinfo;
  538. oldfilepos:=aktfilepos;
  539. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  540. current_procinfo:=self;
  541. { get new labels }
  542. aktbreaklabel:=nil;
  543. aktcontinuelabel:=nil;
  544. templist:=Taasmoutput.create;
  545. { add parast/localst to symtablestack }
  546. add_to_symtablestack;
  547. { set the start offset to the start of the temp area in the stack }
  548. tg:=ttgobj.create;
  549. // tg.setfirsttemp(firsttemp_offset);
  550. { Create register allocator }
  551. cg.init_register_allocators;
  552. { generate callee paraloc register info }
  553. paramanager.create_paraloc_info(current_procinfo.procdef,calleeside);
  554. { Allocate space in temp/registers for parast and localst }
  555. gen_alloc_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  556. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  557. gen_alloc_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  558. {$warning FIXME!!}
  559. { FIXME!! If a procedure contains assembler blocks (or is pure assembler), }
  560. { then rg.used_in_proc_int already contains info because of that. However, }
  561. { adding that info happened before initialisation of rg.used_in_proc_int, }
  562. { so this info cannot be valid! Currently only changing this for entire }
  563. { assembler procedures... For non-i386, the changed registers are even }
  564. { always all volatile registers (JM) }
  565. {$ifdef i386}
  566. if (po_assembler in current_procinfo.procdef.procoptions) then
  567. begin
  568. rg.used_in_proc_int:=paramanager.get_volatile_registers_int(pocall_oldfpccall);
  569. rg.used_in_proc_other:=paramanager.get_volatile_registers_int(pocall_oldfpccall);
  570. end;
  571. {$endif i386}
  572. { generate code for the body }
  573. generatecode(code);
  574. { first generate entry and initialize code with the correct
  575. position and switches }
  576. aktfilepos:=entrypos;
  577. aktlocalswitches:=entryswitches;
  578. gen_initialize_code(templist,false);
  579. aktproccode.insertlistafter(tasmnode(initasmnode).currenttai,templist);
  580. { now generate finalize and exit code with the correct position
  581. and switches }
  582. aktfilepos:=exitpos;
  583. aktlocalswitches:=exitswitches;
  584. gen_finalize_code(templist,false);
  585. { the finalcode must be concatted if there was no position available,
  586. using insertlistafter will result in an insert at the start
  587. when currentai=nil }
  588. if assigned(tasmnode(finalasmnode).currenttai) then
  589. aktproccode.insertlistafter(tasmnode(finalasmnode).currenttai,templist)
  590. else
  591. aktproccode.concatlist(templist);
  592. { note: this must be done only after as much code as possible has }
  593. { been generated. The result is that when you ungetregister() a }
  594. { regvar, it will actually free the regvar (and alse free the }
  595. { the regvars at the same time). Doing this too early will }
  596. { confuse the register allocator, as the regvars will still be }
  597. { used. It should be done before loading the result regs (so }
  598. { they don't conflict with the regvars) and before }
  599. { gen_entry_code (that one has to be able to allocate the }
  600. { regvars again) (JM) }
  601. free_regvars(aktproccode);
  602. { add code that will load the return value, this is not done
  603. for assembler routines when they didn't reference the result
  604. variable }
  605. usesacc:=false;
  606. usesfpu:=false;
  607. usesacchi:=false;
  608. gen_load_return_value(templist,usesacc,usesacchi,usesfpu);
  609. aktproccode.concatlist(templist);
  610. { generate symbol and save end of header position }
  611. aktfilepos:=entrypos;
  612. gen_proc_symbol(templist);
  613. headertai:=tai(templist.last);
  614. { add entry code after header }
  615. gen_entry_code(templist,false);
  616. { insert symbol and entry code }
  617. aktproccode.insertlist(templist);
  618. { Free space in temp/registers for parast and localst, must be
  619. done after gen_entry_code }
  620. aktfilepos:=exitpos;
  621. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  622. gen_free_localst(aktproccode,tlocalsymtable(current_procinfo.procdef.localst));
  623. gen_free_parast(aktproccode,tparasymtable(current_procinfo.procdef.parast));
  624. { The procedure body is finished, we can now
  625. allocate the registers }
  626. if not(cs_no_regalloc in aktglobalswitches) then
  627. begin
  628. {Do register allocation.}
  629. spillingcounter:=0;
  630. repeat
  631. {$ifdef ra_debug}
  632. if aktfilepos.line=2502 then
  633. rg.writegraph(spillingcounter);
  634. {$endif ra_debug}
  635. rg.prepare_colouring;
  636. rg.colour_registers;
  637. rg.epilogue_colouring;
  638. fastspill:=true;
  639. if rg.spillednodes<>'' then
  640. begin
  641. inc(spillingcounter);
  642. if spillingcounter>maxspillingcounter then
  643. internalerror(200309041);
  644. fastspill:=rg.spill_registers(aktproccode,headertai,rg.spillednodes);
  645. end;
  646. until (rg.spillednodes='') or not fastspill;
  647. aktproccode.translate_registers(rg.colour);
  648. (*
  649. {$ifndef NoOpt}
  650. if (cs_optimize in aktglobalswitches) and
  651. { do not optimize pure assembler procedures }
  652. not(pi_is_assembler in current_procinfo.flags) then
  653. optimize(aktproccode);
  654. {$endif NoOpt}
  655. *)
  656. end;
  657. translate_regvars(aktproccode,rg.colour);
  658. { Add save and restore of used registers }
  659. aktfilepos:=entrypos;
  660. gen_save_used_regs(templist);
  661. aktproccode.insertlistafter(headertai,templist);
  662. aktfilepos:=exitpos;
  663. gen_restore_used_regs(aktproccode,usesacc,usesacchi,usesfpu);
  664. { Add stack allocation code after header }
  665. aktfilepos:=entrypos;
  666. gen_stackalloc_code(templist);
  667. aktproccode.insertlistafter(headertai,templist);
  668. { Add exit code at the end }
  669. aktfilepos:=exitpos;
  670. gen_exit_code(templist,false,usesacc,usesacchi);
  671. aktproccode.concatlist(templist);
  672. { save local data (casetable) also in the same file }
  673. if assigned(aktlocaldata) and
  674. (not aktlocaldata.empty) then
  675. begin
  676. { because of the limited constant size of the arm, all data access is done pc relative }
  677. if target_info.cpu=cpu_arm then
  678. aktproccode.concatlist(aktlocaldata)
  679. else
  680. begin
  681. aktproccode.concat(Tai_section.Create(sec_data));
  682. aktproccode.concatlist(aktlocaldata);
  683. aktproccode.concat(Tai_section.Create(sec_code));
  684. end;
  685. end;
  686. { add the procedure to the codesegment }
  687. if (cs_create_smart in aktmoduleswitches) then
  688. codesegment.concat(Tai_cut.Create);
  689. codesegment.concatlist(aktproccode);
  690. { only now we can remove the temps }
  691. tg.resettempgen;
  692. { stop tempgen and ra }
  693. tg.free;
  694. cg.done_register_allocators;
  695. tg:=nil;
  696. rg:=nil;
  697. { restore symtablestack }
  698. remove_from_symtablestack;
  699. { restore }
  700. templist.free;
  701. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  702. aktfilepos:=oldfilepos;
  703. current_procinfo:=oldprocinfo;
  704. end;
  705. procedure tcgprocinfo.add_to_symtablestack;
  706. var
  707. _class,hp : tobjectdef;
  708. begin
  709. { insert symtables for the class, but only if it is no nested function }
  710. if assigned(procdef._class) and
  711. not(assigned(parent) and
  712. assigned(parent.procdef) and
  713. assigned(parent.procdef._class)) then
  714. begin
  715. { insert them in the reverse order }
  716. hp:=nil;
  717. repeat
  718. _class:=procdef._class;
  719. while _class.childof<>hp do
  720. _class:=_class.childof;
  721. hp:=_class;
  722. _class.symtable.next:=symtablestack;
  723. symtablestack:=_class.symtable;
  724. until hp=procdef._class;
  725. end;
  726. { insert parasymtable in symtablestack when parsing
  727. a function }
  728. if procdef.parast.symtablelevel>=normal_function_level then
  729. begin
  730. procdef.parast.next:=symtablestack;
  731. symtablestack:=procdef.parast;
  732. end;
  733. procdef.localst.next:=symtablestack;
  734. symtablestack:=procdef.localst;
  735. end;
  736. procedure tcgprocinfo.remove_from_symtablestack;
  737. begin
  738. { remove localst/parast }
  739. if procdef.parast.symtablelevel>=normal_function_level then
  740. symtablestack:=symtablestack.next.next
  741. else
  742. symtablestack:=symtablestack.next;
  743. { remove class member symbol tables }
  744. while symtablestack.symtabletype=objectsymtable do
  745. symtablestack:=symtablestack.next;
  746. end;
  747. procedure tcgprocinfo.resetprocdef;
  748. begin
  749. { the local symtables can be deleted, but the parast }
  750. { doesn't, (checking definitons when calling a }
  751. { function }
  752. { not for a inline procedure !! (PM) }
  753. { at lexlevel = 1 localst is the staticsymtable itself }
  754. { so no dispose here !! }
  755. if assigned(code) and
  756. not(cs_browser in aktmoduleswitches) and
  757. (procdef.proccalloption<>pocall_inline) then
  758. begin
  759. if procdef.parast.symtablelevel>=normal_function_level then
  760. procdef.localst.free;
  761. procdef.localst:=nil;
  762. end;
  763. { remove code tree, if not inline procedure }
  764. if assigned(code) then
  765. begin
  766. { the inline procedure has already got a copy of the tree
  767. stored in current_procinfo.procdef.code }
  768. code.free;
  769. if (procdef.proccalloption<>pocall_inline) then
  770. procdef.code:=nil;
  771. end;
  772. end;
  773. procedure tcgprocinfo.parse_body;
  774. var
  775. oldprocinfo : tprocinfo;
  776. begin
  777. oldprocinfo:=current_procinfo;
  778. current_procinfo:=self;
  779. { calculate the lexical level }
  780. if procdef.parast.symtablelevel>maxnesting then
  781. Message(parser_e_too_much_lexlevel);
  782. { static is also important for local procedures !! }
  783. if (po_staticmethod in procdef.procoptions) then
  784. allow_only_static:=true
  785. else if (procdef.parast.symtablelevel=normal_function_level) then
  786. allow_only_static:=false;
  787. { reset break and continue labels }
  788. block_type:=bt_general;
  789. {$ifdef state_tracking}
  790. { aktstate:=Tstate_storage.create;}
  791. {$endif state_tracking}
  792. { create a local symbol table for this routine }
  793. if not assigned(procdef.localst) then
  794. procdef.insert_localst;
  795. { add parast/localst to symtablestack }
  796. add_to_symtablestack;
  797. { constant symbols are inserted in this symboltable }
  798. constsymtable:=symtablestack;
  799. { save entry info }
  800. entrypos:=aktfilepos;
  801. entryswitches:=aktlocalswitches;
  802. { parse the code ... }
  803. code:=block(current_module.islibrary);
  804. { save exit info }
  805. exitswitches:=aktlocalswitches;
  806. exitpos:=last_endtoken_filepos;
  807. if assigned(code) then
  808. begin
  809. { get a better entry point }
  810. entrypos:=code.fileinfo;
  811. { the procedure is now defined }
  812. procdef.forwarddef:=false;
  813. { add implicit entry and exit code }
  814. add_entry_exit_code(code,entrypos,exitpos);
  815. if (Errorcount=0) then
  816. begin
  817. { check if forwards are resolved }
  818. tstoredsymtable(procdef.localst).check_forwards;
  819. { check if all labels are used }
  820. tstoredsymtable(procdef.localst).checklabels;
  821. { remove cross unit overloads }
  822. tstoredsymtable(procdef.localst).unchain_overloaded;
  823. end;
  824. { check for unused symbols, but only if there is no asm block }
  825. if not(pi_uses_asm in flags) then
  826. begin
  827. { not for unit init, becuase the var can be used in finalize,
  828. it will be done in proc_unit }
  829. if not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize]) then
  830. tstoredsymtable(procdef.localst).allsymbolsused;
  831. tstoredsymtable(procdef.parast).allsymbolsused;
  832. end;
  833. { Finish type checking pass }
  834. do_resulttypepass(code);
  835. { Print the node to tree.log }
  836. if paraprintnodetree=1 then
  837. printnode_procdef(procdef);
  838. end;
  839. { store a copy of the original tree for inline, for
  840. normal procedures only store a reference to the
  841. current tree }
  842. if (procdef.proccalloption=pocall_inline) then
  843. procdef.code:=code.getcopy
  844. else
  845. procdef.code:=code;
  846. { ... remove symbol tables }
  847. remove_from_symtablestack;
  848. {$ifdef state_tracking}
  849. { aktstate.destroy;}
  850. {$endif state_tracking}
  851. { reset to normal non static function }
  852. if (current_procinfo.procdef.parast.symtablelevel=normal_function_level) then
  853. allow_only_static:=false;
  854. current_procinfo:=oldprocinfo;
  855. end;
  856. {****************************************************************************
  857. PROCEDURE/FUNCTION PARSING
  858. ****************************************************************************}
  859. procedure check_init_paras(p:tnamedindexitem;arg:pointer);
  860. begin
  861. if tsym(p).typ<>varsym then
  862. exit;
  863. with tvarsym(p) do
  864. if (not is_class(vartype.def) and
  865. vartype.def.needs_inittable and
  866. (varspez in [vs_value,vs_out])) then
  867. include(current_procinfo.flags,pi_do_call);
  868. end;
  869. procedure read_proc;
  870. {
  871. Parses the procedure directives, then parses the procedure body, then
  872. generates the code for it
  873. }
  874. procedure do_generate_code(pi:tcgprocinfo);
  875. var
  876. hpi : tcgprocinfo;
  877. begin
  878. { generate code for this procedure }
  879. pi.generate_code;
  880. { process nested procs }
  881. hpi:=tcgprocinfo(pi.nestedprocs.first);
  882. while assigned(hpi) do
  883. begin
  884. do_generate_code(hpi);
  885. hpi:=tcgprocinfo(hpi.next);
  886. end;
  887. pi.resetprocdef;
  888. end;
  889. var
  890. old_current_procinfo : tprocinfo;
  891. oldconstsymtable : tsymtable;
  892. oldselftokenmode,
  893. oldfailtokenmode : tmodeswitch;
  894. pdflags : tpdflags;
  895. pd : tprocdef;
  896. isnestedproc : boolean;
  897. begin
  898. { save old state }
  899. oldconstsymtable:=constsymtable;
  900. old_current_procinfo:=current_procinfo;
  901. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  902. to an other procdef }
  903. current_procinfo:=nil;
  904. { parse procedure declaration }
  905. if assigned(old_current_procinfo) and
  906. assigned(old_current_procinfo.procdef) then
  907. pd:=parse_proc_dec(old_current_procinfo.procdef._class)
  908. else
  909. pd:=parse_proc_dec(nil);
  910. { set the default function options }
  911. if parse_only then
  912. begin
  913. pd.forwarddef:=true;
  914. { set also the interface flag, for better error message when the
  915. implementation doesn't much this header }
  916. pd.interfacedef:=true;
  917. include(pd.procoptions,po_public);
  918. pdflags:=[pd_interface];
  919. end
  920. else
  921. begin
  922. pdflags:=[pd_body];
  923. if (not current_module.in_interface) then
  924. include(pdflags,pd_implemen);
  925. if (not current_module.is_unit) or
  926. (cs_create_smart in aktmoduleswitches) then
  927. include(pd.procoptions,po_public);
  928. pd.forwarddef:=false;
  929. end;
  930. { parse the directives that may follow }
  931. parse_proc_directives(pd,pdflags);
  932. { hint directives, these can be separated by semicolons here,
  933. that needs to be handled here with a loop (PFV) }
  934. while try_consume_hintdirective(pd.symoptions) do
  935. Consume(_SEMICOLON);
  936. { everything of the proc definition is known, we can now
  937. calculate the parameters }
  938. calc_parast(pd);
  939. { search for forward declarations }
  940. if not proc_add_definition(pd) then
  941. begin
  942. { A method must be forward defined (in the object declaration) }
  943. if assigned(pd._class) and
  944. (not assigned(old_current_procinfo.procdef._class)) then
  945. begin
  946. Message1(parser_e_header_dont_match_any_member,pd.fullprocname(false));
  947. tprocsym(pd.procsym).write_parameter_lists(pd);
  948. end
  949. else
  950. begin
  951. { Give a better error if there is a forward def in the interface and only
  952. a single implementation }
  953. if (not pd.forwarddef) and
  954. (not pd.interfacedef) and
  955. (tprocsym(pd.procsym).procdef_count>1) and
  956. tprocsym(pd.procsym).first_procdef.forwarddef and
  957. tprocsym(pd.procsym).first_procdef.interfacedef and
  958. not(tprocsym(pd.procsym).procdef_count>2) then
  959. begin
  960. Message1(parser_e_header_dont_match_forward,pd.fullprocname(false));
  961. tprocsym(pd.procsym).write_parameter_lists(pd);
  962. end;
  963. end;
  964. end;
  965. { compile procedure when a body is needed }
  966. if (pd_body in pdflags) then
  967. begin
  968. Message1(parser_d_procedure_start,pd.fullprocname(false));
  969. { create a new procedure }
  970. current_procinfo:=cprocinfo.create(old_current_procinfo);
  971. current_module.procinfo:=current_procinfo;
  972. current_procinfo.procdef:=pd;
  973. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  974. { Insert mangledname }
  975. pd.aliasnames.insert(pd.mangledname);
  976. { Insert result variables in the localst }
  977. insert_funcret_local(pd);
  978. (*
  979. { Insert local copies for value para }
  980. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}insert_local_value_para,nil);
  981. *)
  982. { check if there are para's which require initing -> set }
  983. { pi_do_call (if not yet set) }
  984. if not(pi_do_call in current_procinfo.flags) then
  985. pd.parast.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_init_paras,nil);
  986. { Update parameter information }
  987. if (current_procinfo.procdef.parast.symtablelevel>normal_function_level) then
  988. current_procinfo.allocate_parent_framepointer_parameter;
  989. { add implicit pushes for interrupt routines }
  990. if (po_interrupt in pd.procoptions) then
  991. current_procinfo.allocate_interrupt_parameter;
  992. { set _FAIL as keyword if constructor }
  993. if (pd.proctypeoption=potype_constructor) then
  994. begin
  995. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  996. tokeninfo^[_FAIL].keyword:=m_all;
  997. end;
  998. { set _SELF as keyword if methods }
  999. if assigned(pd._class) then
  1000. begin
  1001. oldselftokenmode:=tokeninfo^[_SELF].keyword;
  1002. tokeninfo^[_SELF].keyword:=m_all;
  1003. end;
  1004. tcgprocinfo(current_procinfo).parse_body;
  1005. { When it's a nested procedure then defer the code generation,
  1006. when back at normal function level then generate the code
  1007. for all defered nested procedures and the current procedure }
  1008. if isnestedproc then
  1009. tcgprocinfo(current_procinfo.parent).nestedprocs.insert(current_procinfo)
  1010. else
  1011. begin
  1012. if status.errorcount=0 then
  1013. do_generate_code(tcgprocinfo(current_procinfo));
  1014. end;
  1015. { reset _FAIL as _SELF normal }
  1016. if (pd.proctypeoption=potype_constructor) then
  1017. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1018. if assigned(pd._class) then
  1019. tokeninfo^[_SELF].keyword:=oldselftokenmode;
  1020. consume(_SEMICOLON);
  1021. { release procinfo }
  1022. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1023. internalerror(200304274);
  1024. current_module.procinfo:=current_procinfo.parent;
  1025. if not isnestedproc then
  1026. current_procinfo.free;
  1027. end;
  1028. { Restore old state }
  1029. constsymtable:=oldconstsymtable;
  1030. current_procinfo:=old_current_procinfo;
  1031. end;
  1032. {****************************************************************************
  1033. DECLARATION PARSING
  1034. ****************************************************************************}
  1035. { search in symtablestack for not complete classes }
  1036. procedure check_forward_class(p : tnamedindexitem;arg:pointer);
  1037. begin
  1038. if (tsym(p).typ=typesym) and
  1039. (ttypesym(p).restype.def.deftype=objectdef) and
  1040. (oo_is_forward in tobjectdef(ttypesym(p).restype.def).objectoptions) then
  1041. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  1042. end;
  1043. procedure read_declarations(islibrary : boolean);
  1044. procedure Not_supported_for_inline(t : ttoken);
  1045. begin
  1046. if (current_procinfo.procdef.proccalloption=pocall_inline) then
  1047. Begin
  1048. Message1(parser_w_not_supported_for_inline,tokenstring(t));
  1049. Message(parser_w_inlining_disabled);
  1050. current_procinfo.procdef.proccalloption:=pocall_default;
  1051. End;
  1052. end;
  1053. begin
  1054. repeat
  1055. if not assigned(current_procinfo) then
  1056. internalerror(200304251);
  1057. case token of
  1058. _LABEL:
  1059. begin
  1060. Not_supported_for_inline(token);
  1061. label_dec;
  1062. end;
  1063. _CONST:
  1064. begin
  1065. Not_supported_for_inline(token);
  1066. const_dec;
  1067. end;
  1068. _TYPE:
  1069. begin
  1070. Not_supported_for_inline(token);
  1071. type_dec;
  1072. end;
  1073. _VAR:
  1074. var_dec;
  1075. _THREADVAR:
  1076. threadvar_dec;
  1077. _CONSTRUCTOR,_DESTRUCTOR,
  1078. _FUNCTION,_PROCEDURE,_OPERATOR,_CLASS:
  1079. begin
  1080. Not_supported_for_inline(token);
  1081. read_proc;
  1082. end;
  1083. _RESOURCESTRING:
  1084. resourcestring_dec;
  1085. _EXPORTS:
  1086. begin
  1087. Not_supported_for_inline(token);
  1088. if not(assigned(current_procinfo.procdef.localst)) or
  1089. (current_procinfo.procdef.localst.symtablelevel>main_program_level) or
  1090. (current_module.is_unit) then
  1091. begin
  1092. Message(parser_e_syntax_error);
  1093. consume_all_until(_SEMICOLON);
  1094. end
  1095. else if islibrary or
  1096. (target_info.system in [system_i386_WIN32,system_i386_wdosx,system_i386_Netware]) then
  1097. read_exports
  1098. else
  1099. begin
  1100. Message(parser_w_unsupported_feature);
  1101. consume(_BEGIN);
  1102. end;
  1103. end
  1104. else break;
  1105. end;
  1106. until false;
  1107. { check for incomplete class definitions, this is only required
  1108. for fpc modes }
  1109. if (m_fpc in aktmodeswitches) then
  1110. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1111. end;
  1112. procedure read_interface_declarations;
  1113. begin
  1114. repeat
  1115. case token of
  1116. _CONST :
  1117. const_dec;
  1118. _TYPE :
  1119. type_dec;
  1120. _VAR :
  1121. var_dec;
  1122. _THREADVAR :
  1123. threadvar_dec;
  1124. _RESOURCESTRING:
  1125. resourcestring_dec;
  1126. _FUNCTION,
  1127. _PROCEDURE,
  1128. _OPERATOR :
  1129. read_proc;
  1130. else
  1131. break;
  1132. end;
  1133. until false;
  1134. { check for incomplete class definitions, this is only required
  1135. for fpc modes }
  1136. if (m_fpc in aktmodeswitches) then
  1137. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}check_forward_class,nil);
  1138. end;
  1139. begin
  1140. cprocinfo:=tcgprocinfo;
  1141. end.
  1142. {
  1143. $Log$
  1144. Revision 1.150 2003-09-25 16:19:32 peter
  1145. * fix filepositions
  1146. * insert spill temp allocations at the start of the proc
  1147. Revision 1.149 2003/09/23 17:56:06 peter
  1148. * locals and paras are allocated in the code generation
  1149. * tvarsym.localloc contains the location of para/local when
  1150. generating code for the current procedure
  1151. Revision 1.148 2003/09/14 19:18:10 peter
  1152. * remove obsolete code already in comments
  1153. Revision 1.147 2003/09/14 12:58:00 peter
  1154. * support mulitple overloads in implementation, this is delphi
  1155. compatible
  1156. * procsym only stores the overloads available in the interface
  1157. Revision 1.146 2003/09/12 19:07:42 daniel
  1158. * Fixed fast spilling functionality by re-adding the code that initializes
  1159. precoloured nodes to degree 255. I would like to play hangman on the one
  1160. who removed that code.
  1161. Revision 1.145 2003/09/10 19:14:31 daniel
  1162. * Failed attempt to restore broken fastspill functionality
  1163. Revision 1.144 2003/09/09 20:59:27 daniel
  1164. * Adding register allocation order
  1165. Revision 1.143 2003/09/09 15:55:44 peter
  1166. * use register with least interferences in spillregister
  1167. Revision 1.142 2003/09/07 22:09:35 peter
  1168. * preparations for different default calling conventions
  1169. * various RA fixes
  1170. Revision 1.141 2003/09/04 14:46:12 peter
  1171. * abort with IE when spilling requires > 20 loops
  1172. Revision 1.140 2003/09/03 15:55:01 peter
  1173. * NEWRA branch merged
  1174. Revision 1.139 2003/09/03 11:18:37 florian
  1175. * fixed arm concatcopy
  1176. + arm support in the common compiler sources added
  1177. * moved some generic cg code around
  1178. + tfputype added
  1179. * ...
  1180. Revision 1.138.2.1 2003/08/31 13:50:16 daniel
  1181. * Remove sorting and use pregenerated indexes
  1182. * Some work on making things compile
  1183. Revision 1.138 2003/08/20 17:48:49 peter
  1184. * fixed stackalloc to not allocate localst.datasize twice
  1185. * order of stackalloc code fixed for implicit init/final
  1186. Revision 1.137 2003/08/20 15:50:35 peter
  1187. * define NOOPT until optimizer is fixed
  1188. Revision 1.136 2003/08/20 09:07:00 daniel
  1189. * New register coding now mandatory, some more convert_registers calls
  1190. removed.
  1191. Revision 1.135 2003/08/20 07:48:03 daniel
  1192. * Made internal assembler use new register coding
  1193. Revision 1.134 2003/08/17 16:59:20 jonas
  1194. * fixed regvars so they work with newra (at least for ppc)
  1195. * fixed some volatile register bugs
  1196. + -dnotranslation option for -dnewra, which causes the registers not to
  1197. be translated from virtual to normal registers. Requires support in
  1198. the assembler writer as well, which is only implemented in aggas/
  1199. agppcgas currently
  1200. Revision 1.133 2003/07/23 11:04:15 jonas
  1201. * split en_exit_code into a part that may allocate a register and a part
  1202. that doesn't, so the former can be done before the register colouring
  1203. has been performed
  1204. Revision 1.132 2003/07/06 17:58:22 peter
  1205. * framepointer fixes for sparc
  1206. * parent framepointer code more generic
  1207. Revision 1.131 2003/07/06 15:31:21 daniel
  1208. * Fixed register allocator. *Lots* of fixes.
  1209. Revision 1.130 2003/07/05 20:15:24 jonas
  1210. * set pi_do_call if range/overflow checking is on
  1211. Revision 1.129 2003/06/17 16:34:44 jonas
  1212. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1213. * renamed all_intregisters to paramanager.get_volatile_registers_int(pocall_default) and made it
  1214. processor dependent
  1215. Revision 1.128 2003/06/14 14:53:50 jonas
  1216. * fixed newra cycle for x86
  1217. * added constants for indicating source and destination operands of the
  1218. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1219. Revision 1.127 2003/06/13 21:19:31 peter
  1220. * current_procdef removed, use current_procinfo.procdef instead
  1221. Revision 1.126 2003/06/12 16:43:07 peter
  1222. * newra compiles for sparc
  1223. Revision 1.125 2003/06/09 12:23:30 peter
  1224. * init/final of procedure data splitted from genentrycode
  1225. * use asmnode getposition to insert final at the correct position
  1226. als for the implicit try...finally
  1227. Revision 1.124 2003/06/07 19:37:43 jonas
  1228. * pi_do_call must always be set for the main program, since it always
  1229. ends with a call to FPC_DO_EXIT
  1230. Revision 1.123 2003/06/07 18:57:04 jonas
  1231. + added freeintparaloc
  1232. * ppc get/freeintparaloc now check whether the parameter regs are
  1233. properly allocated/deallocated (and get an extra list para)
  1234. * ppc a_call_* now internalerrors if pi_do_call is not yet set
  1235. * fixed lot of missing pi_do_call's
  1236. Revision 1.122 2003/06/03 13:01:59 daniel
  1237. * Register allocator finished
  1238. Revision 1.121 2003/05/31 20:23:39 jonas
  1239. * added pi_do_call if a procedure has a value shortstring parameter
  1240. (it's copied to the local stackframe with a helper)
  1241. Revision 1.120 2003/05/30 23:57:08 peter
  1242. * more sparc cleanup
  1243. * accumulator removed, splitted in function_return_reg (called) and
  1244. function_result_reg (caller)
  1245. Revision 1.119 2003/05/28 23:58:18 jonas
  1246. * added missing initialization of rg.usedintin,byproc
  1247. * ppc now also saves/restores used fpu registers
  1248. * ncgcal doesn't add used registers to usedby/inproc anymore, except for
  1249. i386
  1250. Revision 1.118 2003/05/26 21:17:18 peter
  1251. * procinlinenode removed
  1252. * aktexit2label removed, fast exit removed
  1253. + tcallnode.inlined_pass_2 added
  1254. Revision 1.117 2003/05/25 08:59:47 peter
  1255. * do not generate code when there was an error
  1256. Revision 1.116 2003/05/23 18:49:55 jonas
  1257. * generate code for parent procedure before that of nested procedures as
  1258. well (I only need pass_1 to be done for the ppc, but pass_1 and pass_2
  1259. are grouped and it doesn't hurt that pass_2 is done as well)
  1260. Revision 1.115 2003/05/22 21:31:35 peter
  1261. * defer codegeneration for nested procedures
  1262. Revision 1.114 2003/05/16 20:00:39 jonas
  1263. * powerpc nested procedure fixes, should work completely now if all
  1264. local variables of the parent procedure are declared before the
  1265. nested procedures are declared
  1266. Revision 1.113 2003/05/16 14:33:31 peter
  1267. * regvar fixes
  1268. Revision 1.112 2003/05/13 21:26:38 peter
  1269. * only call destructor in except block when there is a destructor
  1270. available
  1271. Revision 1.111 2003/05/13 19:14:41 peter
  1272. * failn removed
  1273. * inherited result code check moven to pexpr
  1274. Revision 1.110 2003/05/13 15:18:49 peter
  1275. * fixed various crashes
  1276. Revision 1.109 2003/05/11 21:37:03 peter
  1277. * moved implicit exception frame from ncgutil to psub
  1278. * constructor/destructor helpers moved from cobj/ncgutil to psub
  1279. Revision 1.108 2003/05/09 17:47:03 peter
  1280. * self moved to hidden parameter
  1281. * removed hdisposen,hnewn,selfn
  1282. Revision 1.107 2003/04/27 11:21:34 peter
  1283. * aktprocdef renamed to current_procinfo.procdef
  1284. * procinfo renamed to current_procinfo
  1285. * procinfo will now be stored in current_module so it can be
  1286. cleaned up properly
  1287. * gen_main_procsym changed to create_main_proc and release_main_proc
  1288. to also generate a tprocinfo structure
  1289. * fixed unit implicit initfinal
  1290. Revision 1.106 2003/04/27 07:29:50 peter
  1291. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1292. a new procdef declaration
  1293. * aktprocsym removed
  1294. * lexlevel removed, use symtable.symtablelevel instead
  1295. * implicit init/final code uses the normal genentry/genexit
  1296. * funcret state checking updated for new funcret handling
  1297. Revision 1.105 2003/04/26 00:31:42 peter
  1298. * set return_offset moved to after_header
  1299. Revision 1.104 2003/04/25 20:59:34 peter
  1300. * removed funcretn,funcretsym, function result is now in varsym
  1301. and aliases for result and function name are added using absolutesym
  1302. * vs_hidden parameter for funcret passed in parameter
  1303. * vs_hidden fixes
  1304. * writenode changed to printnode and released from extdebug
  1305. * -vp option added to generate a tree.log with the nodetree
  1306. * nicer printnode for statements, callnode
  1307. Revision 1.103 2003/04/24 13:03:01 florian
  1308. * comp is now written with its bit pattern to the ppu instead as an extended
  1309. Revision 1.102 2003/04/23 12:35:34 florian
  1310. * fixed several issues with powerpc
  1311. + applied a patch from Jonas for nested function calls (PowerPC only)
  1312. * ...
  1313. Revision 1.101 2003/04/22 14:33:38 peter
  1314. * removed some notes/hints
  1315. Revision 1.100 2003/04/22 13:47:08 peter
  1316. * fixed C style array of const
  1317. * fixed C array passing
  1318. * fixed left to right with high parameters
  1319. Revision 1.99 2003/04/22 10:09:35 daniel
  1320. + Implemented the actual register allocator
  1321. + Scratch registers unavailable when new register allocator used
  1322. + maybe_save/maybe_restore unavailable when new register allocator used
  1323. Revision 1.98 2003/04/17 07:50:24 daniel
  1324. * Some work on interference graph construction
  1325. Revision 1.97 2003/04/16 09:26:55 jonas
  1326. * assembler procedures now again get a stackframe if they have local
  1327. variables. No space is reserved for a function result however.
  1328. Also, the register parameters aren't automatically saved on the stack
  1329. anymore in assembler procedures.
  1330. Revision 1.96 2003/04/05 21:09:31 jonas
  1331. * several ppc/generic result offset related fixes. The "normal" result
  1332. offset seems now to be calculated correctly and a lot of duplicate
  1333. calculations have been removed. Nested functions accessing the parent's
  1334. function result don't work at all though :(
  1335. Revision 1.95 2003/04/02 16:11:34 peter
  1336. * give error when exports is not supported
  1337. Revision 1.94 2003/03/12 22:43:38 jonas
  1338. * more powerpc and generic fixes related to the new register allocator
  1339. Revision 1.93 2003/03/08 08:59:07 daniel
  1340. + $define newra will enable new register allocator
  1341. + getregisterint will return imaginary registers with $newra
  1342. + -sr switch added, will skip register allocation so you can see
  1343. the direct output of the code generator before register allocation
  1344. Revision 1.92 2003/02/19 22:00:14 daniel
  1345. * Code generator converted to new register notation
  1346. - Horribily outdated todo.txt removed
  1347. Revision 1.91 2003/01/09 21:52:37 peter
  1348. * merged some verbosity options.
  1349. * V_LineInfo is a verbosity flag to include line info
  1350. Revision 1.90 2003/01/09 20:40:59 daniel
  1351. * Converted some code in cgx86.pas to new register numbering
  1352. Revision 1.89 2003/01/09 15:49:56 daniel
  1353. * Added register conversion
  1354. Revision 1.88 2003/01/08 18:43:56 daniel
  1355. * Tregister changed into a record
  1356. Revision 1.87 2003/01/03 20:35:08 peter
  1357. * check also interfacedef when checking for matching forwarddef
  1358. Revision 1.86 2003/01/02 11:14:02 michael
  1359. + Patch from peter to support initial values for local variables
  1360. Revision 1.85 2002/12/29 18:59:34 peter
  1361. * fixed parsing of declarations before asm statement
  1362. Revision 1.84 2002/12/29 18:25:18 peter
  1363. * parse declarations before check _ASM token
  1364. Revision 1.83 2002/12/29 14:57:50 peter
  1365. * unit loading changed to first register units and load them
  1366. afterwards. This is needed to support uses xxx in yyy correctly
  1367. * unit dependency check fixed
  1368. Revision 1.82 2002/12/25 01:26:56 peter
  1369. * duplicate procsym-unitsym fix
  1370. Revision 1.81 2002/12/15 13:37:15 peter
  1371. * don't include uf_init for library. The code is already called and
  1372. does not need to be in the initfinal table
  1373. Revision 1.80 2002/12/07 14:27:09 carl
  1374. * 3% memory optimization
  1375. * changed some types
  1376. + added type checking with different size for call node and for
  1377. parameters
  1378. Revision 1.79 2002/11/25 18:43:32 carl
  1379. - removed the invalid if <> checking (Delphi is strange on this)
  1380. + implemented abstract warning on instance creation of class with
  1381. abstract methods.
  1382. * some error message cleanups
  1383. Revision 1.78 2002/11/25 17:43:23 peter
  1384. * splitted defbase in defutil,symutil,defcmp
  1385. * merged isconvertable and is_equal into compare_defs(_ext)
  1386. * made operator search faster by walking the list only once
  1387. Revision 1.77 2002/11/23 22:50:06 carl
  1388. * some small speed optimizations
  1389. + added several new warnings/hints
  1390. Revision 1.76 2002/11/18 17:31:58 peter
  1391. * pass proccalloption to ret_in_xxx and push_xxx functions
  1392. Revision 1.75 2002/11/17 16:31:57 carl
  1393. * memory optimization (3-4%) : cleanup of tai fields,
  1394. cleanup of tdef and tsym fields.
  1395. * make it work for m68k
  1396. Revision 1.74 2002/11/15 01:58:53 peter
  1397. * merged changes from 1.0.7 up to 04-11
  1398. - -V option for generating bug report tracing
  1399. - more tracing for option parsing
  1400. - errors for cdecl and high()
  1401. - win32 import stabs
  1402. - win32 records<=8 are returned in eax:edx (turned off by default)
  1403. - heaptrc update
  1404. - more info for temp management in .s file with EXTDEBUG
  1405. Revision 1.73 2002/11/09 15:32:30 carl
  1406. * noopt for non-i386 targets
  1407. Revision 1.72 2002/09/10 20:31:48 florian
  1408. * call to current_procinfo.after_header added
  1409. Revision 1.71 2002/09/07 15:25:07 peter
  1410. * old logs removed and tabs fixed
  1411. Revision 1.70 2002/09/03 16:26:27 daniel
  1412. * Make Tprocdef.defs protected
  1413. Revision 1.69 2002/08/25 19:25:20 peter
  1414. * sym.insert_in_data removed
  1415. * symtable.insertvardata/insertconstdata added
  1416. * removed insert_in_data call from symtable.insert, it needs to be
  1417. called separatly. This allows to deref the address calculation
  1418. * procedures now calculate the parast addresses after the procedure
  1419. directives are parsed. This fixes the cdecl parast problem
  1420. * push_addr_param has an extra argument that specifies if cdecl is used
  1421. or not
  1422. Revision 1.68 2002/08/17 09:23:41 florian
  1423. * first part of procinfo rewrite
  1424. Revision 1.67 2002/08/16 14:24:59 carl
  1425. * issameref() to test if two references are the same (then emit no opcodes)
  1426. + ret_in_reg to replace ret_in_acc
  1427. (fix some register allocation bugs at the same time)
  1428. + save_std_register now has an extra parameter which is the
  1429. usedinproc registers
  1430. Revision 1.66 2002/08/11 14:32:27 peter
  1431. * renamed current_library to objectlibrary
  1432. Revision 1.65 2002/08/11 13:24:13 peter
  1433. * saving of asmsymbols in ppu supported
  1434. * asmsymbollist global is removed and moved into a new class
  1435. tasmlibrarydata that will hold the info of a .a file which
  1436. corresponds with a single module. Added librarydata to tmodule
  1437. to keep the library info stored for the module. In the future the
  1438. objectfiles will also be stored to the tasmlibrarydata class
  1439. * all getlabel/newasmsymbol and friends are moved to the new class
  1440. Revision 1.64 2002/08/09 19:14:28 carl
  1441. * fixed stackframe parameter (should only contain local size),
  1442. set to zero currently
  1443. Revision 1.63 2002/08/06 20:55:22 florian
  1444. * first part of ppc calling conventions fix
  1445. Revision 1.62 2002/07/26 21:15:41 florian
  1446. * rewrote the system handling
  1447. Revision 1.61 2002/07/20 11:57:56 florian
  1448. * types.pas renamed to defbase.pas because D6 contains a types
  1449. unit so this would conflicts if D6 programms are compiled
  1450. + Willamette/SSE2 instructions to assembler added
  1451. Revision 1.60 2002/07/19 11:41:36 daniel
  1452. * State tracker work
  1453. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1454. allows the state tracker to change while nodes automatically into
  1455. repeat nodes.
  1456. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1457. 'not(a>b)' is optimized into 'a<=b'.
  1458. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1459. by removing the notn and later switchting the true and falselabels. The
  1460. same is done with 'repeat until not a'.
  1461. Revision 1.59 2002/07/15 18:03:15 florian
  1462. * readded removed changes
  1463. Revision 1.57 2002/07/11 14:41:28 florian
  1464. * start of the new generic parameter handling
  1465. Revision 1.58 2002/07/14 18:00:44 daniel
  1466. + Added the beginning of a state tracker. This will track the values of
  1467. variables through procedures and optimize things away.
  1468. Revision 1.56 2002/07/07 09:52:32 florian
  1469. * powerpc target fixed, very simple units can be compiled
  1470. * some basic stuff for better callparanode handling, far from being finished
  1471. Revision 1.55 2002/07/04 20:43:01 florian
  1472. * first x86-64 patches
  1473. Revision 1.54 2002/07/01 18:46:25 peter
  1474. * internal linker
  1475. * reorganized aasm layer
  1476. Revision 1.53 2002/05/18 13:34:14 peter
  1477. * readded missing revisions
  1478. Revision 1.52 2002/05/16 19:46:44 carl
  1479. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1480. + try to fix temp allocation (still in ifdef)
  1481. + generic constructor calls
  1482. + start of tassembler / tmodulebase class cleanup
  1483. Revision 1.51 2002/05/14 19:34:49 peter
  1484. * removed old logs and updated copyright year
  1485. }