psub.pas 63 KB

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