parser.pas 23 KB

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