parser.pas 23 KB

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