parser.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit does the parsing process
  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 parser;
  19. {$i defines.inc}
  20. interface
  21. {$ifdef PREPROCWRITE}
  22. procedure preprocess(const filename:string);
  23. {$endif PREPROCWRITE}
  24. procedure compile(const filename:string);
  25. procedure initparser;
  26. procedure doneparser;
  27. implementation
  28. uses
  29. cutils,cclasses,
  30. globtype,version,tokens,systems,globals,verbose,
  31. symbase,symtable,symsym,fmodule,fppu,aasm,
  32. hcodegen,
  33. script,gendef,
  34. {$ifdef BrowserLog}
  35. browlog,
  36. {$endif BrowserLog}
  37. {$ifdef UseExcept}
  38. tpexcept,
  39. {$endif UseExcept}
  40. {$ifdef GDB}
  41. gdb,
  42. {$endif GDB}
  43. comphook,
  44. scanner,scandir,
  45. pbase,ptype,pmodules,cresstr;
  46. procedure initparser;
  47. begin
  48. { ^M means a string or a char, because we don't parse a }
  49. { type declaration }
  50. ignore_equal:=false;
  51. { we didn't parse a object or class declaration }
  52. { and no function header }
  53. testcurobject:=0;
  54. { a long time, this was forgotten }
  55. aktprocsym:=nil;
  56. current_module:=nil;
  57. compiled_module:=nil;
  58. procinfo:=nil;
  59. loaded_units:=TLinkedList.Create;
  60. usedunits:=TLinkedList.Create;
  61. { global switches }
  62. aktglobalswitches:=initglobalswitches;
  63. { initialize scanner }
  64. InitScanner;
  65. InitScannerDirectives;
  66. { scanner }
  67. c:=#0;
  68. pattern:='';
  69. orgpattern:='';
  70. current_scanner:=nil;
  71. { memory sizes }
  72. if heapsize=0 then
  73. heapsize:=target_info.heapsize;
  74. if maxheapsize=0 then
  75. maxheapsize:=target_info.maxheapsize;
  76. if stacksize=0 then
  77. stacksize:=target_info.stacksize;
  78. { open assembler response }
  79. AsmRes:=TAsmScript.Create(outputexedir+'ppas');
  80. { open deffile }
  81. DefFile:=TDefFile.Create(outputexedir+inputfile+target_info.defext);
  82. { list of generated .o files, so the linker can remove them }
  83. SmartLinkOFiles:=TStringList.Create;
  84. end;
  85. procedure doneparser;
  86. begin
  87. { unload units }
  88. loaded_units.free;
  89. usedunits.free;
  90. { if there was an error in the scanner, the scanner is
  91. still assinged }
  92. if assigned(current_scanner) then
  93. begin
  94. current_scanner.free;
  95. current_scanner:=nil;
  96. end;
  97. { close scanner }
  98. DoneScanner;
  99. { close ppas,deffile }
  100. asmres.free;
  101. deffile.free;
  102. { free list of .o files }
  103. SmartLinkOFiles.Free;
  104. end;
  105. procedure default_macros;
  106. var
  107. hp : tstringlistitem;
  108. begin
  109. { commandline }
  110. hp:=tstringlistitem(initdefines.first);
  111. while assigned(hp) do
  112. begin
  113. current_scanner.def_macro(hp.str);
  114. hp:=tstringlistitem(hp.next);
  115. end;
  116. { set macros for version checking }
  117. current_scanner.set_macro('FPC_VERSION',version_nr);
  118. current_scanner.set_macro('FPC_RELEASE',release_nr);
  119. current_scanner.set_macro('FPC_PATCH',patch_nr);
  120. end;
  121. {$ifdef PREPROCWRITE}
  122. procedure preprocess(const filename:string);
  123. var
  124. i : longint;
  125. begin
  126. new(preprocfile,init('pre'));
  127. { default macros }
  128. current_scanner^.macros:=new(pdictionary,init);
  129. default_macros;
  130. { initialize a module }
  131. current_module:=new(pmodule,init(filename,false));
  132. main_module:=current_module;
  133. { startup scanner, and save in current_module }
  134. current_scanner:=new(pscannerfile,Init(filename));
  135. current_module.scanner:=current_scanner;
  136. { loop until EOF is found }
  137. repeat
  138. current_scanner^.readtoken;
  139. preprocfile^.AddSpace;
  140. case token of
  141. _ID :
  142. begin
  143. preprocfile^.Add(orgpattern);
  144. end;
  145. _REALNUMBER,
  146. _INTCONST :
  147. preprocfile^.Add(pattern);
  148. _CSTRING :
  149. begin
  150. i:=0;
  151. while (i<length(pattern)) do
  152. begin
  153. inc(i);
  154. if pattern[i]='''' then
  155. begin
  156. insert('''',pattern,i);
  157. inc(i);
  158. end;
  159. end;
  160. preprocfile^.Add(''''+pattern+'''');
  161. end;
  162. _CCHAR :
  163. begin
  164. case pattern[1] of
  165. #39 :
  166. pattern:='''''''';
  167. #0..#31,
  168. #128..#255 :
  169. begin
  170. str(ord(pattern[1]),pattern);
  171. pattern:='#'+pattern;
  172. end;
  173. else
  174. pattern:=''''+pattern[1]+'''';
  175. end;
  176. preprocfile^.Add(pattern);
  177. end;
  178. _EOF :
  179. break;
  180. else
  181. preprocfile^.Add(tokeninfo^[token].str)
  182. end;
  183. until false;
  184. { free scanner }
  185. dispose(current_scanner,done);
  186. current_scanner:=nil;
  187. { close }
  188. dispose(preprocfile,done);
  189. end;
  190. {$endif PREPROCWRITE}
  191. procedure compile(const filename:string);
  192. var
  193. { scanner }
  194. oldidtoken,
  195. oldtoken : ttoken;
  196. oldtokenpos : tfileposinfo;
  197. oldc : char;
  198. oldpattern,
  199. oldorgpattern : string;
  200. old_block_type : tblock_type;
  201. oldcurrent_scanner,prev_scanner,
  202. scanner : tscannerfile;
  203. { symtable }
  204. oldrefsymtable,
  205. olddefaultsymtablestack,
  206. oldsymtablestack : tsymtable;
  207. oldprocprefix : string;
  208. oldaktprocsym : tprocsym;
  209. oldoverloaded_operators : toverloaded_operators;
  210. { cg }
  211. oldnextlabelnr : longint;
  212. oldparse_only : boolean;
  213. { asmlists }
  214. oldimports,
  215. oldexports,
  216. oldresource,
  217. oldrttilist,
  218. oldresourcestringlist,
  219. oldbsssegment,
  220. olddatasegment,
  221. oldcodesegment,
  222. oldexprasmlist,
  223. olddebuglist,
  224. oldwithdebuglist,
  225. oldconsts : taasmoutput;
  226. oldasmsymbollist : tdictionary;
  227. { resourcestrings }
  228. OldResourceStrings : tResourceStrings;
  229. { akt.. things }
  230. oldaktlocalswitches : tlocalswitches;
  231. oldaktmoduleswitches : tmoduleswitches;
  232. oldaktfilepos : tfileposinfo;
  233. oldaktpackenum,oldaktmaxfpuregisters : longint;
  234. oldaktpackrecords : tpackrecords;
  235. oldaktoutputformat : tasm;
  236. oldaktspecificoptprocessor,
  237. oldaktoptprocessor : tprocessors;
  238. oldaktasmmode : tasmmode;
  239. oldaktinterfacetype: tinterfacetypes;
  240. oldaktmodeswitches : tmodeswitches;
  241. old_compiled_module : tmodule;
  242. oldaktexceptblock : integer;
  243. oldstatement_level : integer;
  244. prev_name : pstring;
  245. {$ifdef USEEXCEPT}
  246. {$ifndef Delphi}
  247. recoverpos : jmp_buf;
  248. oldrecoverpos : pjmp_buf;
  249. {$endif Delphi}
  250. {$endif useexcept}
  251. {$ifdef newcg}
  252. oldcg : pcg;
  253. {$endif newcg}
  254. {$ifdef GDB}
  255. store_dbx : plongint;
  256. {$endif GDB}
  257. begin
  258. inc(compile_level);
  259. prev_name:=stringdup(parser_current_file);
  260. parser_current_file:=filename;
  261. old_compiled_module:=compiled_module;
  262. { save symtable state }
  263. oldsymtablestack:=symtablestack;
  264. olddefaultsymtablestack:=defaultsymtablestack;
  265. oldrefsymtable:=refsymtable;
  266. oldprocprefix:=procprefix;
  267. oldaktprocsym:=aktprocsym;
  268. move(overloaded_operators,oldoverloaded_operators,sizeof(toverloaded_operators));
  269. { save scanner state }
  270. oldc:=c;
  271. oldpattern:=pattern;
  272. oldorgpattern:=orgpattern;
  273. oldtoken:=token;
  274. oldidtoken:=idtoken;
  275. old_block_type:=block_type;
  276. oldtokenpos:=akttokenpos;
  277. oldcurrent_scanner:=current_scanner;
  278. { save cg }
  279. oldnextlabelnr:=nextlabelnr;
  280. oldparse_only:=parse_only;
  281. { save assembler lists }
  282. olddatasegment:=datasegment;
  283. oldbsssegment:=bsssegment;
  284. oldcodesegment:=codesegment;
  285. olddebuglist:=debuglist;
  286. oldwithdebuglist:=withdebuglist;
  287. oldconsts:=consts;
  288. oldrttilist:=rttilist;
  289. oldexprasmlist:=exprasmlist;
  290. oldimports:=importssection;
  291. oldexports:=exportssection;
  292. oldresource:=resourcesection;
  293. oldresourcestringlist:=resourcestringlist;
  294. oldasmsymbollist:=asmsymbollist;
  295. OldResourceStrings:=ResourceStrings;
  296. { save akt... state }
  297. { handle the postponed case first }
  298. if localswitcheschanged then
  299. begin
  300. aktlocalswitches:=nextaktlocalswitches;
  301. localswitcheschanged:=false;
  302. end;
  303. oldaktlocalswitches:=aktlocalswitches;
  304. oldaktmoduleswitches:=aktmoduleswitches;
  305. oldaktpackrecords:=aktpackrecords;
  306. oldaktpackenum:=aktpackenum;
  307. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  308. oldaktoutputformat:=aktoutputformat;
  309. oldaktoptprocessor:=aktoptprocessor;
  310. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  311. oldaktasmmode:=aktasmmode;
  312. oldaktinterfacetype:=aktinterfacetype;
  313. oldaktfilepos:=aktfilepos;
  314. oldaktmodeswitches:=aktmodeswitches;
  315. oldstatement_level:=statement_level;
  316. oldaktexceptblock:=aktexceptblock;
  317. {$ifdef newcg}
  318. oldcg:=cg;
  319. {$endif newcg}
  320. {$ifdef GDB}
  321. store_dbx:=dbx_counter;
  322. dbx_counter:=nil;
  323. {$endif GDB}
  324. { show info }
  325. Message1(parser_i_compiling,filename);
  326. { reset symtable }
  327. symtablestack:=nil;
  328. defaultsymtablestack:=nil;
  329. systemunit:=nil;
  330. refsymtable:=nil;
  331. aktprocsym:=nil;
  332. procprefix:='';
  333. registerdef:=true;
  334. statement_level:=0;
  335. aktexceptblock:=0;
  336. aktmaxfpuregisters:=-1;
  337. fillchar(overloaded_operators,sizeof(toverloaded_operators),0);
  338. { reset the unit or create a new program }
  339. if assigned(current_module) then
  340. begin
  341. {current_module.reset this is wrong !! }
  342. scanner:=tscannerfile(current_module.scanner);
  343. current_module.reset;
  344. tscannerfile(current_module.scanner):=scanner;
  345. end
  346. else
  347. begin
  348. current_module:=tppumodule.create(filename,false);
  349. main_module:=current_module;
  350. end;
  351. { a unit compiled at command line must be inside the loaded_unit list }
  352. if (compile_level=1) then
  353. loaded_units.insert(current_module);
  354. { Set the module to use for verbose }
  355. SetCompileModule(current_module);
  356. compiled_module:=current_module;
  357. current_module.in_compile:=true;
  358. { Load current state from the init values }
  359. aktlocalswitches:=initlocalswitches;
  360. aktmoduleswitches:=initmoduleswitches;
  361. aktmodeswitches:=initmodeswitches;
  362. {$IFDEF Testvarsets}
  363. aktsetalloc:=initsetalloc;
  364. {$ENDIF}
  365. aktpackrecords:=initpackrecords;
  366. aktpackenum:=initpackenum;
  367. aktoutputformat:=initoutputformat;
  368. aktoptprocessor:=initoptprocessor;
  369. aktspecificoptprocessor:=initspecificoptprocessor;
  370. aktasmmode:=initasmmode;
  371. aktinterfacetype:=initinterfacetype;
  372. { startup scanner, and save in current_module }
  373. current_scanner:=tscannerfile.Create(filename);
  374. { macros }
  375. default_macros;
  376. { read the first token }
  377. current_scanner.readtoken;
  378. prev_scanner:=tscannerfile(current_module.scanner);
  379. current_module.scanner:=current_scanner;
  380. { init code generator for a new module }
  381. codegen_newmodule;
  382. {$ifdef newcg}
  383. {$ifdef i386}
  384. cg:=new(pcg386,init);
  385. {$endif i386}
  386. {$ifdef alpha}
  387. cg:=new(pcgalpha,init);
  388. {$endif alpha}
  389. {$ifdef powerpc}
  390. cg:=new(pcgppc,init);
  391. {$endif powerpc}
  392. {$endif newcg}
  393. { If the compile level > 1 we get a nice "unit expected" error
  394. message if we are trying to use a program as unit.}
  395. {$ifdef USEEXCEPT}
  396. if setjmp(recoverpos)=0 then
  397. begin
  398. oldrecoverpos:=recoverpospointer;
  399. recoverpospointer:=@recoverpos;
  400. {$endif USEEXCEPT}
  401. if (token=_UNIT) or (compile_level>1) then
  402. begin
  403. current_module.is_unit:=true;
  404. proc_unit;
  405. end
  406. else
  407. proc_program(token=_LIBRARY);
  408. {$ifdef USEEXCEPT}
  409. recoverpospointer:=oldrecoverpos;
  410. end
  411. else
  412. begin
  413. recoverpospointer:=oldrecoverpos;
  414. longjump_used:=true;
  415. end;
  416. {$endif USEEXCEPT}
  417. { clear memory }
  418. {$ifdef Splitheap}
  419. if testsplit then
  420. begin
  421. { temp heap should be empty after that !!!}
  422. codegen_donemodule;
  423. Releasetempheap;
  424. end;
  425. {$endif Splitheap}
  426. { restore old state, close trees, > 0.99.5 has heapblocks, so
  427. it's the default to release the trees }
  428. codegen_donemodule;
  429. {$ifdef newcg}
  430. dispose(cg,done);
  431. {$endif newcg}
  432. { free ppu }
  433. if assigned(tppumodule(current_module).ppufile) then
  434. begin
  435. tppumodule(current_module).ppufile.free;
  436. tppumodule(current_module).ppufile:=nil;
  437. end;
  438. { free scanner }
  439. current_scanner.free;
  440. current_scanner:=nil;
  441. { restore previous scanner !! }
  442. current_module.scanner:=prev_scanner;
  443. if assigned(prev_scanner) then
  444. prev_scanner.invalid:=true;
  445. if (compile_level>1) then
  446. begin
  447. {$ifdef newcg}
  448. cg:=oldcg;
  449. {$endif newcg}
  450. {$ifdef GDB}
  451. dbx_counter:=store_dbx;
  452. {$endif GDB}
  453. { restore scanner }
  454. c:=oldc;
  455. pattern:=oldpattern;
  456. orgpattern:=oldorgpattern;
  457. token:=oldtoken;
  458. idtoken:=oldidtoken;
  459. akttokenpos:=oldtokenpos;
  460. block_type:=old_block_type;
  461. current_scanner:=oldcurrent_scanner;
  462. { restore cg }
  463. nextlabelnr:=oldnextlabelnr;
  464. parse_only:=oldparse_only;
  465. { restore asmlists }
  466. exprasmlist:=oldexprasmlist;
  467. datasegment:=olddatasegment;
  468. bsssegment:=oldbsssegment;
  469. codesegment:=oldcodesegment;
  470. consts:=oldconsts;
  471. debuglist:=olddebuglist;
  472. withdebuglist:=oldwithdebuglist;
  473. importssection:=oldimports;
  474. exportssection:=oldexports;
  475. resourcesection:=oldresource;
  476. rttilist:=oldrttilist;
  477. resourcestringlist:=oldresourcestringlist;
  478. asmsymbollist:=oldasmsymbollist;
  479. ResourceStrings:=OldResourceStrings;
  480. { restore symtable state }
  481. refsymtable:=oldrefsymtable;
  482. symtablestack:=oldsymtablestack;
  483. defaultsymtablestack:=olddefaultsymtablestack;
  484. aktprocsym:=oldaktprocsym;
  485. procprefix:=oldprocprefix;
  486. move(oldoverloaded_operators,overloaded_operators,sizeof(toverloaded_operators));
  487. aktlocalswitches:=oldaktlocalswitches;
  488. aktmoduleswitches:=oldaktmoduleswitches;
  489. aktpackrecords:=oldaktpackrecords;
  490. aktpackenum:=oldaktpackenum;
  491. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  492. aktoutputformat:=oldaktoutputformat;
  493. aktoptprocessor:=oldaktoptprocessor;
  494. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  495. aktasmmode:=oldaktasmmode;
  496. aktinterfacetype:=oldaktinterfacetype;
  497. aktfilepos:=oldaktfilepos;
  498. aktmodeswitches:=oldaktmodeswitches;
  499. statement_level:=oldstatement_level;
  500. aktexceptblock:=oldaktexceptblock;
  501. end;
  502. { Shut down things when the last file is compiled }
  503. if (compile_level=1) then
  504. begin
  505. { Close script }
  506. if (not AsmRes.Empty) then
  507. begin
  508. Message1(exec_i_closing_script,AsmRes.Fn);
  509. AsmRes.WriteToDisk;
  510. end;
  511. {$ifdef USEEXCEPT}
  512. if not longjump_used then
  513. {$endif USEEXCEPT}
  514. { do not create browsers on errors !! }
  515. if status.errorcount=0 then
  516. begin
  517. {$ifdef BrowserLog}
  518. { Write Browser Log }
  519. if (cs_browser_log in aktglobalswitches) and
  520. (cs_browser in aktmoduleswitches) then
  521. begin
  522. if browserlog.elements_to_list.empty then
  523. begin
  524. Message1(parser_i_writing_browser_log,browserlog.Fname);
  525. WriteBrowserLog;
  526. end
  527. else
  528. browserlog.list_elements;
  529. end;
  530. {$endif BrowserLog}
  531. { Write Browser Collections }
  532. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  533. end;
  534. if current_module.in_second_compile then
  535. begin
  536. current_module.in_second_compile:=false;
  537. current_module.in_compile:=true;
  538. end
  539. else
  540. current_module.in_compile:=false;
  541. (* Obsolete code aktprocsym
  542. is disposed by the localsymtable disposal (PM)
  543. { Free last aktprocsym }
  544. if assigned(aktprocsym) and (aktprocsym.owner=nil) then
  545. begin
  546. { init parts are not needed in units !! }
  547. if current_module.is_unit then
  548. aktprocsym.definition.forwarddef:=false;
  549. dispose(aktprocsym,done);
  550. end; *)
  551. end;
  552. dec(compile_level);
  553. parser_current_file:=prev_name^;
  554. stringdispose(prev_name);
  555. compiled_module:=old_compiled_module;
  556. {$ifdef USEEXCEPT}
  557. if longjump_used then
  558. longjmp(recoverpospointer^,1);
  559. {$endif USEEXCEPT}
  560. end;
  561. end.
  562. {
  563. $Log$
  564. Revision 1.18 2001-05-06 14:49:17 peter
  565. * ppu object to class rewrite
  566. * move ppu read and write stuff to fppu
  567. Revision 1.17 2001/04/18 22:01:54 peter
  568. * registration of targets and assemblers
  569. Revision 1.16 2001/04/15 09:48:30 peter
  570. * fixed crash in labelnode
  571. * easier detection of goto and label in try blocks
  572. Revision 1.15 2001/04/13 18:08:37 peter
  573. * scanner object to class
  574. Revision 1.14 2001/04/13 01:22:10 peter
  575. * symtable change to classes
  576. * range check generation and errors fixed, make cycle DEBUG=1 works
  577. * memory leaks fixed
  578. Revision 1.13 2000/12/25 00:07:27 peter
  579. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  580. tlinkedlist objects)
  581. Revision 1.12 2000/12/24 12:24:38 peter
  582. * moved preprocessfile into a conditional
  583. Revision 1.11 2000/11/29 00:30:34 florian
  584. * unused units removed from uses clause
  585. * some changes for widestrings
  586. Revision 1.10 2000/11/12 22:17:46 peter
  587. * some realname updates for messages
  588. Revision 1.9 2000/11/04 14:25:20 florian
  589. + merged Attila's changes for interfaces, not tested yet
  590. Revision 1.8 2000/10/31 22:02:49 peter
  591. * symtable splitted, no real code changes
  592. Revision 1.7 2000/10/14 10:14:51 peter
  593. * moehrendorf oct 2000 rewrite
  594. Revision 1.6 2000/10/01 19:48:25 peter
  595. * lot of compile updates for cg11
  596. Revision 1.5 2000/09/24 15:06:20 peter
  597. * use defines.inc
  598. Revision 1.4 2000/08/27 16:11:51 peter
  599. * moved some util functions from globals,cobjects to cutils
  600. * splitted files into finput,fmodule
  601. Revision 1.3 2000/08/12 15:34:22 peter
  602. + usedasmsymbollist to check and reset only the used symbols (merged)
  603. Revision 1.2 2000/07/13 11:32:44 michael
  604. + removed logs
  605. }