parser.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit does the parsing process
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit parser;
  18. {$i fpcdefs.inc}
  19. interface
  20. {$ifdef PREPROCWRITE}
  21. procedure preprocess(const filename:string);
  22. {$endif PREPROCWRITE}
  23. procedure compile(const filename:string);
  24. procedure initparser;
  25. procedure doneparser;
  26. implementation
  27. uses
  28. sysutils,
  29. cutils,cclasses,
  30. globtype,version,tokens,systems,globals,verbose,
  31. symbase,symtable,symsym,
  32. finput,fmodule,fppu,
  33. aasmbase,aasmtai,
  34. cgbase,
  35. script,gendef,
  36. {$ifdef BrowserCol}
  37. browcol,
  38. {$endif BrowserCol}
  39. {$ifdef BrowserLog}
  40. browlog,
  41. {$endif BrowserLog}
  42. {$ifdef GDB}
  43. gdb,
  44. {$endif GDB}
  45. comphook,
  46. scanner,scandir,
  47. pbase,ptype,psystem,pmodules,psub,
  48. cresstr,cpuinfo,procinfo;
  49. procedure initparser;
  50. begin
  51. { ^M means a string or a char, because we don't parse a }
  52. { type declaration }
  53. ignore_equal:=false;
  54. { we didn't parse a object or class declaration }
  55. { and no function header }
  56. testcurobject:=0;
  57. { Current compiled module/proc }
  58. objectlibrary:=nil;
  59. current_module:=nil;
  60. compiled_module:=nil;
  61. current_procinfo:=nil;
  62. SetCompileModule(nil);
  63. loaded_units:=TLinkedList.Create;
  64. usedunits:=TLinkedList.Create;
  65. { global switches }
  66. aktglobalswitches:=initglobalswitches;
  67. aktsourcecodepage:=initsourcecodepage;
  68. { initialize scanner }
  69. InitScanner;
  70. InitScannerDirectives;
  71. { scanner }
  72. c:=#0;
  73. pattern:='';
  74. orgpattern:='';
  75. current_scanner:=nil;
  76. { register all nodes and tais }
  77. registernodes;
  78. registertais;
  79. { memory sizes }
  80. if stacksize=0 then
  81. stacksize:=target_info.stacksize;
  82. { open assembler response }
  83. if cs_link_on_target in aktglobalswitches then
  84. GenerateAsmRes(outputexedir+inputfile+'_ppas')
  85. else
  86. GenerateAsmRes(outputexedir+'ppas');
  87. { open deffile }
  88. DefFile:=TDefFile.Create(outputexedir+inputfile+target_info.defext);
  89. { list of generated .o files, so the linker can remove them }
  90. SmartLinkOFiles:=TStringList.Create;
  91. { codegen }
  92. if paraprintnodetree<>0 then
  93. printnode_reset;
  94. { target specific stuff }
  95. case target_info.system of
  96. system_powerpc_morphos:
  97. include(supported_calling_conventions,pocall_syscall);
  98. system_m68k_amiga:
  99. include(supported_calling_conventions,pocall_syscall);
  100. end;
  101. end;
  102. procedure doneparser;
  103. begin
  104. { Reset current compiling info, so destroy routines can't
  105. reference the data that might already be destroyed }
  106. objectlibrary:=nil;
  107. current_module:=nil;
  108. compiled_module:=nil;
  109. current_procinfo:=nil;
  110. SetCompileModule(nil);
  111. { unload units }
  112. loaded_units.free;
  113. usedunits.free;
  114. { if there was an error in the scanner, the scanner is
  115. still assinged }
  116. if assigned(current_scanner) then
  117. begin
  118. current_scanner.free;
  119. current_scanner:=nil;
  120. end;
  121. { close scanner }
  122. DoneScanner;
  123. { close ppas,deffile }
  124. asmres.free;
  125. deffile.free;
  126. { free list of .o files }
  127. SmartLinkOFiles.Free;
  128. end;
  129. {$ifdef PREPROCWRITE}
  130. procedure preprocess(const filename:string);
  131. var
  132. i : longint;
  133. begin
  134. new(preprocfile,init('pre'));
  135. { initialize a module }
  136. current_module:=new(pmodule,init(filename,false));
  137. macrosymtablestack:= initialmacrosymtable;
  138. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  139. current_module.localmacrosymtable.next:= initialmacrosymtable;
  140. macrosymtablestack:= current_module.localmacrosymtable;
  141. ConsolidateMode;
  142. main_module:=current_module;
  143. { startup scanner, and save in current_module }
  144. current_scanner:=new(pscannerfile,Init(filename));
  145. current_module.scanner:=current_scanner;
  146. { loop until EOF is found }
  147. repeat
  148. current_scanner^.readtoken;
  149. preprocfile^.AddSpace;
  150. case token of
  151. _ID :
  152. begin
  153. preprocfile^.Add(orgpattern);
  154. end;
  155. _REALNUMBER,
  156. _INTCONST :
  157. preprocfile^.Add(pattern);
  158. _CSTRING :
  159. begin
  160. i:=0;
  161. while (i<length(pattern)) do
  162. begin
  163. inc(i);
  164. if pattern[i]='''' then
  165. begin
  166. insert('''',pattern,i);
  167. inc(i);
  168. end;
  169. end;
  170. preprocfile^.Add(''''+pattern+'''');
  171. end;
  172. _CCHAR :
  173. begin
  174. case pattern[1] of
  175. #39 :
  176. pattern:='''''''';
  177. #0..#31,
  178. #128..#255 :
  179. begin
  180. str(ord(pattern[1]),pattern);
  181. pattern:='#'+pattern;
  182. end;
  183. else
  184. pattern:=''''+pattern[1]+'''';
  185. end;
  186. preprocfile^.Add(pattern);
  187. end;
  188. _EOF :
  189. break;
  190. else
  191. preprocfile^.Add(tokeninfo^[token].str)
  192. end;
  193. until false;
  194. { free scanner }
  195. dispose(current_scanner,done);
  196. current_scanner:=nil;
  197. { close }
  198. dispose(preprocfile,done);
  199. end;
  200. {$endif PREPROCWRITE}
  201. {*****************************************************************************
  202. Create information for a new module
  203. *****************************************************************************}
  204. procedure init_module;
  205. begin
  206. { Create assembler output lists for CG }
  207. exprasmlist:=taasmoutput.create;
  208. datasegment:=taasmoutput.create;
  209. codesegment:=taasmoutput.create;
  210. bsssegment:=taasmoutput.create;
  211. debuglist:=taasmoutput.create;
  212. withdebuglist:=taasmoutput.create;
  213. consts:=taasmoutput.create;
  214. rttilist:=taasmoutput.create;
  215. picdata:=taasmoutput.create;
  216. if target_info.system=system_powerpc_darwin then
  217. picdata.concat(tai_simple.create(ait_non_lazy_symbol_pointer));
  218. ResourceStringList:=Nil;
  219. importssection:=nil;
  220. exportssection:=nil;
  221. resourcesection:=nil;
  222. { Resource strings }
  223. ResourceStrings:=TResourceStrings.Create;
  224. { use the librarydata from current_module }
  225. objectlibrary:=current_module.librarydata;
  226. end;
  227. procedure done_module;
  228. {$ifdef MEMDEBUG}
  229. var
  230. d : tmemdebug;
  231. {$endif}
  232. begin
  233. {$ifdef MEMDEBUG}
  234. d:=tmemdebug.create(current_module.modulename^+' - asmlists');
  235. {$endif}
  236. exprasmlist.free;
  237. codesegment.free;
  238. bsssegment.free;
  239. datasegment.free;
  240. debuglist.free;
  241. withdebuglist.free;
  242. consts.free;
  243. rttilist.free;
  244. picdata.free;
  245. if assigned(ResourceStringList) then
  246. ResourceStringList.free;
  247. if assigned(importssection) then
  248. importssection.free;
  249. if assigned(exportssection) then
  250. exportssection.free;
  251. if assigned(resourcesection) then
  252. resourcesection.free;
  253. {$ifdef MEMDEBUG}
  254. d.free;
  255. {$endif}
  256. { resource strings }
  257. ResourceStrings.free;
  258. objectlibrary:=nil;
  259. end;
  260. {*****************************************************************************
  261. Compile a source file
  262. *****************************************************************************}
  263. procedure compile(const filename:string);
  264. type
  265. polddata=^tolddata;
  266. tolddata=record
  267. { scanner }
  268. oldidtoken,
  269. oldtoken : ttoken;
  270. oldtokenpos : tfileposinfo;
  271. oldc : char;
  272. oldpattern,
  273. oldorgpattern : string;
  274. old_block_type : tblock_type;
  275. { symtable }
  276. oldrefsymtable,
  277. olddefaultsymtablestack,
  278. oldsymtablestack : tsymtable;
  279. olddefaultmacrosymtablestack,
  280. oldmacrosymtablestack : tsymtable;
  281. oldaktprocsym : tprocsym;
  282. { cg }
  283. oldparse_only : boolean;
  284. { asmlists }
  285. oldimports,
  286. oldexports,
  287. oldresource,
  288. oldrttilist,
  289. oldpicdata,
  290. oldresourcestringlist,
  291. oldbsssegment,
  292. olddatasegment,
  293. oldcodesegment,
  294. oldexprasmlist,
  295. olddebuglist,
  296. oldwithdebuglist,
  297. oldconsts : taasmoutput;
  298. oldobjectlibrary : tasmlibrarydata;
  299. { resourcestrings }
  300. OldResourceStrings : tResourceStrings;
  301. { akt.. things }
  302. oldaktlocalswitches : tlocalswitches;
  303. oldaktmoduleswitches : tmoduleswitches;
  304. oldaktfilepos : tfileposinfo;
  305. oldaktpackrecords,
  306. oldaktpackenum : shortint;
  307. oldaktmaxfpuregisters : longint;
  308. oldaktalignment : talignmentinfo;
  309. oldaktoutputformat : tasm;
  310. oldaktspecificoptprocessor,
  311. oldaktoptprocessor : tprocessors;
  312. oldaktfputype : tfputype;
  313. oldaktasmmode : tasmmode;
  314. oldaktinterfacetype: tinterfacetypes;
  315. oldaktmodeswitches : tmodeswitches;
  316. old_compiled_module : tmodule;
  317. oldcurrent_procinfo : tprocinfo;
  318. oldaktdefproccall : tproccalloption;
  319. oldsourcecodepage : tcodepagestring;
  320. {$ifdef GDB}
  321. store_dbx : plongint;
  322. {$endif GDB}
  323. end;
  324. var
  325. olddata : polddata;
  326. begin
  327. inc(compile_level);
  328. parser_current_file:=filename;
  329. { Uses heap memory instead of placing everything on the
  330. stack. This is needed because compile() can be called
  331. recursively }
  332. new(olddata);
  333. with olddata^ do
  334. begin
  335. old_compiled_module:=compiled_module;
  336. { save symtable state }
  337. oldsymtablestack:=symtablestack;
  338. oldmacrosymtablestack:=macrosymtablestack;
  339. olddefaultsymtablestack:=defaultsymtablestack;
  340. olddefaultmacrosymtablestack:=defaultmacrosymtablestack;
  341. oldrefsymtable:=refsymtable;
  342. oldcurrent_procinfo:=current_procinfo;
  343. oldaktdefproccall:=aktdefproccall;
  344. { save scanner state }
  345. oldc:=c;
  346. oldpattern:=pattern;
  347. oldorgpattern:=orgpattern;
  348. oldtoken:=token;
  349. oldidtoken:=idtoken;
  350. old_block_type:=block_type;
  351. oldtokenpos:=akttokenpos;
  352. oldsourcecodepage:=aktsourcecodepage;
  353. { save cg }
  354. oldparse_only:=parse_only;
  355. { save assembler lists }
  356. olddatasegment:=datasegment;
  357. oldbsssegment:=bsssegment;
  358. oldcodesegment:=codesegment;
  359. olddebuglist:=debuglist;
  360. oldwithdebuglist:=withdebuglist;
  361. oldconsts:=consts;
  362. oldrttilist:=rttilist;
  363. oldpicdata:=picdata;
  364. oldexprasmlist:=exprasmlist;
  365. oldimports:=importssection;
  366. oldexports:=exportssection;
  367. oldresource:=resourcesection;
  368. oldresourcestringlist:=resourcestringlist;
  369. oldobjectlibrary:=objectlibrary;
  370. OldResourceStrings:=ResourceStrings;
  371. { save akt... state }
  372. { handle the postponed case first }
  373. if localswitcheschanged then
  374. begin
  375. aktlocalswitches:=nextaktlocalswitches;
  376. localswitcheschanged:=false;
  377. end;
  378. oldaktlocalswitches:=aktlocalswitches;
  379. oldaktmoduleswitches:=aktmoduleswitches;
  380. oldaktalignment:=aktalignment;
  381. oldaktpackenum:=aktpackenum;
  382. oldaktpackrecords:=aktpackrecords;
  383. oldaktfputype:=aktfputype;
  384. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  385. oldaktoutputformat:=aktoutputformat;
  386. oldaktoptprocessor:=aktoptprocessor;
  387. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  388. oldaktasmmode:=aktasmmode;
  389. oldaktinterfacetype:=aktinterfacetype;
  390. oldaktfilepos:=aktfilepos;
  391. oldaktmodeswitches:=aktmodeswitches;
  392. {$ifdef GDB}
  393. store_dbx:=dbx_counter;
  394. dbx_counter:=nil;
  395. {$endif GDB}
  396. end;
  397. { show info }
  398. Message1(parser_i_compiling,filename);
  399. { reset symtable }
  400. symtablestack:=nil;
  401. macrosymtablestack:=nil;
  402. defaultsymtablestack:=nil;
  403. defaultmacrosymtablestack:=nil;
  404. systemunit:=nil;
  405. refsymtable:=nil;
  406. aktdefproccall:=initdefproccall;
  407. registerdef:=true;
  408. aktexceptblock:=0;
  409. exceptblockcounter:=0;
  410. aktmaxfpuregisters:=-1;
  411. { reset the unit or create a new program }
  412. { a unit compiled at command line must be inside the loaded_unit list }
  413. if (compile_level=1) then
  414. begin
  415. if assigned(current_module) then
  416. internalerror(200501158);
  417. current_module:=tppumodule.create(nil,filename,'',false);
  418. addloadedunit(current_module);
  419. main_module:=current_module;
  420. current_module.state:=ms_compile;
  421. end;
  422. if not(assigned(current_module) and
  423. (current_module.state in [ms_compile,ms_second_compile])) then
  424. internalerror(200212281);
  425. { Set the module to use for verbose }
  426. compiled_module:=current_module;
  427. SetCompileModule(current_module);
  428. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  429. { Load current state from the init values }
  430. aktlocalswitches:=initlocalswitches;
  431. aktmoduleswitches:=initmoduleswitches;
  432. aktmodeswitches:=initmodeswitches;
  433. {$IFDEF Testvarsets}
  434. aktsetalloc:=initsetalloc;
  435. {$ENDIF}
  436. aktalignment:=initalignment;
  437. aktfputype:=initfputype;
  438. aktpackenum:=initpackenum;
  439. aktpackrecords:=0;
  440. aktoutputformat:=initoutputformat;
  441. set_target_asm(aktoutputformat);
  442. aktoptprocessor:=initoptprocessor;
  443. aktspecificoptprocessor:=initspecificoptprocessor;
  444. aktasmmode:=initasmmode;
  445. aktinterfacetype:=initinterfacetype;
  446. { startup scanner and load the first file }
  447. current_scanner:=tscannerfile.Create(filename);
  448. current_scanner.firstfile;
  449. current_module.scanner:=current_scanner;
  450. { init macros before anything in the file is parsed.}
  451. macrosymtablestack:= initialmacrosymtable;
  452. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  453. current_module.localmacrosymtable.next:= initialmacrosymtable;
  454. macrosymtablestack:= current_module.localmacrosymtable;
  455. { read the first token }
  456. current_scanner.readtoken;
  457. { init code generator for a new module }
  458. init_module;
  459. { If the compile level > 1 we get a nice "unit expected" error
  460. message if we are trying to use a program as unit.}
  461. try
  462. try
  463. if (token=_UNIT) or (compile_level>1) then
  464. begin
  465. current_module.is_unit:=true;
  466. proc_unit;
  467. end
  468. else
  469. proc_program(token=_LIBRARY);
  470. except
  471. on ECompilerAbort do
  472. raise;
  473. on Exception do
  474. begin
  475. { Increase errorcounter to prevent some
  476. checks during cleanup }
  477. inc(status.errorcount);
  478. raise;
  479. end;
  480. end;
  481. finally
  482. { restore old state }
  483. done_module;
  484. if assigned(current_module) then
  485. begin
  486. { module is now compiled }
  487. tppumodule(current_module).state:=ms_compiled;
  488. { free ppu }
  489. if assigned(tppumodule(current_module).ppufile) then
  490. begin
  491. tppumodule(current_module).ppufile.free;
  492. tppumodule(current_module).ppufile:=nil;
  493. end;
  494. { free scanner }
  495. if assigned(current_module.scanner) then
  496. begin
  497. if current_scanner=tscannerfile(current_module.scanner) then
  498. current_scanner:=nil;
  499. tscannerfile(current_module.scanner).free;
  500. current_module.scanner:=nil;
  501. end;
  502. end;
  503. if (compile_level>1) then
  504. begin
  505. with olddata^ do
  506. begin
  507. { restore scanner }
  508. c:=oldc;
  509. pattern:=oldpattern;
  510. orgpattern:=oldorgpattern;
  511. token:=oldtoken;
  512. idtoken:=oldidtoken;
  513. akttokenpos:=oldtokenpos;
  514. block_type:=old_block_type;
  515. { restore cg }
  516. parse_only:=oldparse_only;
  517. { restore asmlists }
  518. exprasmlist:=oldexprasmlist;
  519. datasegment:=olddatasegment;
  520. bsssegment:=oldbsssegment;
  521. codesegment:=oldcodesegment;
  522. consts:=oldconsts;
  523. debuglist:=olddebuglist;
  524. withdebuglist:=oldwithdebuglist;
  525. importssection:=oldimports;
  526. exportssection:=oldexports;
  527. resourcesection:=oldresource;
  528. rttilist:=oldrttilist;
  529. picdata:=oldpicdata;
  530. resourcestringlist:=oldresourcestringlist;
  531. { object data }
  532. ResourceStrings:=OldResourceStrings;
  533. objectlibrary:=oldobjectlibrary;
  534. { restore previous scanner }
  535. if assigned(old_compiled_module) then
  536. current_scanner:=tscannerfile(old_compiled_module.scanner)
  537. else
  538. current_scanner:=nil;
  539. if assigned(current_scanner) then
  540. parser_current_file:=current_scanner.inputfile.name^;
  541. { restore symtable state }
  542. refsymtable:=oldrefsymtable;
  543. symtablestack:=oldsymtablestack;
  544. macrosymtablestack:=oldmacrosymtablestack;
  545. defaultsymtablestack:=olddefaultsymtablestack;
  546. defaultmacrosymtablestack:=olddefaultmacrosymtablestack;
  547. aktdefproccall:=oldaktdefproccall;
  548. current_procinfo:=oldcurrent_procinfo;
  549. aktsourcecodepage:=oldsourcecodepage;
  550. aktlocalswitches:=oldaktlocalswitches;
  551. aktmoduleswitches:=oldaktmoduleswitches;
  552. aktalignment:=oldaktalignment;
  553. aktpackenum:=oldaktpackenum;
  554. aktpackrecords:=oldaktpackrecords;
  555. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  556. aktoutputformat:=oldaktoutputformat;
  557. set_target_asm(aktoutputformat);
  558. aktoptprocessor:=oldaktoptprocessor;
  559. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  560. aktfputype:=oldaktfputype;
  561. aktasmmode:=oldaktasmmode;
  562. aktinterfacetype:=oldaktinterfacetype;
  563. aktfilepos:=oldaktfilepos;
  564. aktmodeswitches:=oldaktmodeswitches;
  565. aktexceptblock:=0;
  566. exceptblockcounter:=0;
  567. {$ifdef GDB}
  568. dbx_counter:=store_dbx;
  569. {$endif GDB}
  570. end;
  571. end
  572. else
  573. begin
  574. { Shut down things when the last file is compiled succesfull }
  575. if (compile_level=1) and
  576. (status.errorcount=0) then
  577. begin
  578. parser_current_file:='';
  579. { Close script }
  580. if (not AsmRes.Empty) then
  581. begin
  582. Message1(exec_i_closing_script,AsmRes.Fn);
  583. AsmRes.WriteToDisk;
  584. end;
  585. { do not create browsers on errors !! }
  586. if status.errorcount=0 then
  587. begin
  588. {$ifdef BrowserLog}
  589. { Write Browser Log }
  590. if (cs_browser_log in aktglobalswitches) and
  591. (cs_browser in aktmoduleswitches) then
  592. begin
  593. if browserlog.elements_to_list.empty then
  594. begin
  595. Message1(parser_i_writing_browser_log,browserlog.Fname);
  596. WriteBrowserLog;
  597. end
  598. else
  599. browserlog.list_elements;
  600. end;
  601. {$endif BrowserLog}
  602. { Write Browser Collections, also used by the TextMode IDE to
  603. retrieve a list of sourcefiles }
  604. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  605. end;
  606. end;
  607. end;
  608. dec(compile_level);
  609. compiled_module:=olddata^.old_compiled_module;
  610. SetCompileModule(compiled_module);
  611. dispose(olddata);
  612. end;
  613. end;
  614. end.