parser.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. var
  208. i : Tasmlist;
  209. begin
  210. exprasmlist:=taasmoutput.create;
  211. for i:=low(Tasmlist) to high(Tasmlist) do
  212. asmlist[i]:=Taasmoutput.create;
  213. { PIC data }
  214. {$ifdef powerpc}
  215. if target_info.system=system_powerpc_darwin then
  216. asmlist[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  217. {$endif powerpc}
  218. { Resource strings }
  219. cresstr.resourcestrings:=Tresourcestrings.Create;
  220. { use the librarydata from current_module }
  221. objectlibrary:=current_module.librarydata;
  222. end;
  223. procedure done_module;
  224. var
  225. {$ifdef MEMDEBUG}
  226. d : tmemdebug;
  227. {$endif}
  228. i:Tasmlist;
  229. begin
  230. {$ifdef MEMDEBUG}
  231. d:=tmemdebug.create(current_module.modulename^+' - asmlists');
  232. {$endif}
  233. for i:=low(Tasmlist) to high(Tasmlist) do
  234. if asmlist[i]<>nil then
  235. asmlist[i].free;
  236. {$ifdef MEMDEBUG}
  237. d.free;
  238. {$endif}
  239. { resource strings }
  240. cresstr.resourcestrings.free;
  241. objectlibrary:=nil;
  242. end;
  243. {*****************************************************************************
  244. Compile a source file
  245. *****************************************************************************}
  246. procedure compile(const filename:string);
  247. type
  248. polddata=^tolddata;
  249. tolddata=record
  250. { scanner }
  251. oldidtoken,
  252. oldtoken : ttoken;
  253. oldtokenpos : tfileposinfo;
  254. oldc : char;
  255. oldpattern,
  256. oldorgpattern : string;
  257. old_block_type : tblock_type;
  258. { symtable }
  259. oldrefsymtable,
  260. olddefaultsymtablestack,
  261. oldsymtablestack : tsymtable;
  262. olddefaultmacrosymtablestack,
  263. oldmacrosymtablestack : tsymtable;
  264. oldaktprocsym : tprocsym;
  265. { cg }
  266. oldparse_only : boolean;
  267. { asmlists }
  268. oldexprasmlist:Taasmoutput;
  269. oldasmlist:array[Tasmlist] of Taasmoutput;
  270. oldobjectlibrary : tasmlibrarydata;
  271. { al_resourcestrings }
  272. Oldresourcestrings : tresourcestrings;
  273. { akt.. things }
  274. oldaktlocalswitches : tlocalswitches;
  275. oldaktmoduleswitches : tmoduleswitches;
  276. oldaktfilepos : tfileposinfo;
  277. oldaktpackrecords,
  278. oldaktpackenum : shortint;
  279. oldaktmaxfpuregisters : longint;
  280. oldaktalignment : talignmentinfo;
  281. oldaktoutputformat : tasm;
  282. oldaktspecificoptprocessor,
  283. oldaktoptprocessor : tprocessors;
  284. oldaktfputype : tfputype;
  285. oldaktasmmode : tasmmode;
  286. oldaktinterfacetype: tinterfacetypes;
  287. oldaktmodeswitches : tmodeswitches;
  288. old_compiled_module : tmodule;
  289. oldcurrent_procinfo : tprocinfo;
  290. oldaktdefproccall : tproccalloption;
  291. oldsourcecodepage : tcodepagestring;
  292. {$ifdef GDB}
  293. store_dbx : plongint;
  294. {$endif GDB}
  295. end;
  296. var
  297. olddata : polddata;
  298. begin
  299. inc(compile_level);
  300. parser_current_file:=filename;
  301. { Uses heap memory instead of placing everything on the
  302. stack. This is needed because compile() can be called
  303. recursively }
  304. new(olddata);
  305. with olddata^ do
  306. begin
  307. old_compiled_module:=compiled_module;
  308. { save symtable state }
  309. oldsymtablestack:=symtablestack;
  310. oldmacrosymtablestack:=macrosymtablestack;
  311. olddefaultsymtablestack:=defaultsymtablestack;
  312. olddefaultmacrosymtablestack:=defaultmacrosymtablestack;
  313. oldrefsymtable:=refsymtable;
  314. oldcurrent_procinfo:=current_procinfo;
  315. oldaktdefproccall:=aktdefproccall;
  316. { save scanner state }
  317. oldc:=c;
  318. oldpattern:=pattern;
  319. oldorgpattern:=orgpattern;
  320. oldtoken:=token;
  321. oldidtoken:=idtoken;
  322. old_block_type:=block_type;
  323. oldtokenpos:=akttokenpos;
  324. oldsourcecodepage:=aktsourcecodepage;
  325. { save cg }
  326. oldparse_only:=parse_only;
  327. { save assembler lists }
  328. oldasmlist:=asmlist;
  329. oldexprasmlist:=exprasmlist;
  330. oldobjectlibrary:=objectlibrary;
  331. Oldresourcestrings:=resourcestrings;
  332. { save akt... state }
  333. { handle the postponed case first }
  334. if localswitcheschanged then
  335. begin
  336. aktlocalswitches:=nextaktlocalswitches;
  337. localswitcheschanged:=false;
  338. end;
  339. oldaktlocalswitches:=aktlocalswitches;
  340. oldaktmoduleswitches:=aktmoduleswitches;
  341. oldaktalignment:=aktalignment;
  342. oldaktpackenum:=aktpackenum;
  343. oldaktpackrecords:=aktpackrecords;
  344. oldaktfputype:=aktfputype;
  345. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  346. oldaktoutputformat:=aktoutputformat;
  347. oldaktoptprocessor:=aktoptprocessor;
  348. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  349. oldaktasmmode:=aktasmmode;
  350. oldaktinterfacetype:=aktinterfacetype;
  351. oldaktfilepos:=aktfilepos;
  352. oldaktmodeswitches:=aktmodeswitches;
  353. {$ifdef GDB}
  354. store_dbx:=dbx_counter;
  355. dbx_counter:=nil;
  356. {$endif GDB}
  357. end;
  358. { show info }
  359. Message1(parser_i_compiling,filename);
  360. { reset symtable }
  361. symtablestack:=nil;
  362. macrosymtablestack:=nil;
  363. defaultsymtablestack:=nil;
  364. defaultmacrosymtablestack:=nil;
  365. systemunit:=nil;
  366. refsymtable:=nil;
  367. aktdefproccall:=initdefproccall;
  368. registerdef:=true;
  369. aktexceptblock:=0;
  370. exceptblockcounter:=0;
  371. aktmaxfpuregisters:=-1;
  372. { reset the unit or create a new program }
  373. { a unit compiled at command line must be inside the loaded_unit list }
  374. if (compile_level=1) then
  375. begin
  376. if assigned(current_module) then
  377. internalerror(200501158);
  378. current_module:=tppumodule.create(nil,filename,'',false);
  379. addloadedunit(current_module);
  380. main_module:=current_module;
  381. current_module.state:=ms_compile;
  382. end;
  383. if not(assigned(current_module) and
  384. (current_module.state in [ms_compile,ms_second_compile])) then
  385. internalerror(200212281);
  386. { Set the module to use for verbose }
  387. compiled_module:=current_module;
  388. SetCompileModule(current_module);
  389. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  390. { Load current state from the init values }
  391. aktlocalswitches:=initlocalswitches;
  392. aktmoduleswitches:=initmoduleswitches;
  393. aktmodeswitches:=initmodeswitches;
  394. {$IFDEF Testvarsets}
  395. aktsetalloc:=initsetalloc;
  396. {$ENDIF}
  397. aktalignment:=initalignment;
  398. aktfputype:=initfputype;
  399. aktpackenum:=initpackenum;
  400. aktpackrecords:=0;
  401. aktoutputformat:=initoutputformat;
  402. set_target_asm(aktoutputformat);
  403. aktoptprocessor:=initoptprocessor;
  404. aktspecificoptprocessor:=initspecificoptprocessor;
  405. aktasmmode:=initasmmode;
  406. aktinterfacetype:=initinterfacetype;
  407. { startup scanner and load the first file }
  408. current_scanner:=tscannerfile.Create(filename);
  409. current_scanner.firstfile;
  410. current_module.scanner:=current_scanner;
  411. { init macros before anything in the file is parsed.}
  412. macrosymtablestack:= initialmacrosymtable;
  413. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  414. current_module.localmacrosymtable.next:= initialmacrosymtable;
  415. macrosymtablestack:= current_module.localmacrosymtable;
  416. { read the first token }
  417. current_scanner.readtoken;
  418. { init code generator for a new module }
  419. init_module;
  420. { If the compile level > 1 we get a nice "unit expected" error
  421. message if we are trying to use a program as unit.}
  422. try
  423. try
  424. if (token=_UNIT) or (compile_level>1) then
  425. begin
  426. current_module.is_unit:=true;
  427. proc_unit;
  428. end
  429. else
  430. proc_program(token=_LIBRARY);
  431. except
  432. on ECompilerAbort do
  433. raise;
  434. on Exception do
  435. begin
  436. { Increase errorcounter to prevent some
  437. checks during cleanup }
  438. inc(status.errorcount);
  439. raise;
  440. end;
  441. end;
  442. finally
  443. { restore old state }
  444. done_module;
  445. if assigned(current_module) then
  446. begin
  447. { module is now compiled }
  448. tppumodule(current_module).state:=ms_compiled;
  449. { free ppu }
  450. if assigned(tppumodule(current_module).ppufile) then
  451. begin
  452. tppumodule(current_module).ppufile.free;
  453. tppumodule(current_module).ppufile:=nil;
  454. end;
  455. { free scanner }
  456. if assigned(current_module.scanner) then
  457. begin
  458. if current_scanner=tscannerfile(current_module.scanner) then
  459. current_scanner:=nil;
  460. tscannerfile(current_module.scanner).free;
  461. current_module.scanner:=nil;
  462. end;
  463. end;
  464. if (compile_level>1) then
  465. begin
  466. with olddata^ do
  467. begin
  468. { restore scanner }
  469. c:=oldc;
  470. pattern:=oldpattern;
  471. orgpattern:=oldorgpattern;
  472. token:=oldtoken;
  473. idtoken:=oldidtoken;
  474. akttokenpos:=oldtokenpos;
  475. block_type:=old_block_type;
  476. { restore cg }
  477. parse_only:=oldparse_only;
  478. { restore asmlists }
  479. exprasmlist:=oldexprasmlist;
  480. asmlist:=oldasmlist;
  481. { object data }
  482. resourcestrings:=oldresourcestrings;
  483. objectlibrary:=oldobjectlibrary;
  484. { restore previous scanner }
  485. if assigned(old_compiled_module) then
  486. current_scanner:=tscannerfile(old_compiled_module.scanner)
  487. else
  488. current_scanner:=nil;
  489. if assigned(current_scanner) then
  490. parser_current_file:=current_scanner.inputfile.name^;
  491. { restore symtable state }
  492. refsymtable:=oldrefsymtable;
  493. symtablestack:=oldsymtablestack;
  494. macrosymtablestack:=oldmacrosymtablestack;
  495. defaultsymtablestack:=olddefaultsymtablestack;
  496. defaultmacrosymtablestack:=olddefaultmacrosymtablestack;
  497. aktdefproccall:=oldaktdefproccall;
  498. current_procinfo:=oldcurrent_procinfo;
  499. aktsourcecodepage:=oldsourcecodepage;
  500. aktlocalswitches:=oldaktlocalswitches;
  501. aktmoduleswitches:=oldaktmoduleswitches;
  502. aktalignment:=oldaktalignment;
  503. aktpackenum:=oldaktpackenum;
  504. aktpackrecords:=oldaktpackrecords;
  505. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  506. aktoutputformat:=oldaktoutputformat;
  507. set_target_asm(aktoutputformat);
  508. aktoptprocessor:=oldaktoptprocessor;
  509. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  510. aktfputype:=oldaktfputype;
  511. aktasmmode:=oldaktasmmode;
  512. aktinterfacetype:=oldaktinterfacetype;
  513. aktfilepos:=oldaktfilepos;
  514. aktmodeswitches:=oldaktmodeswitches;
  515. aktexceptblock:=0;
  516. exceptblockcounter:=0;
  517. {$ifdef GDB}
  518. dbx_counter:=store_dbx;
  519. {$endif GDB}
  520. end;
  521. end
  522. else
  523. begin
  524. { Shut down things when the last file is compiled succesfull }
  525. if (compile_level=1) and
  526. (status.errorcount=0) then
  527. begin
  528. parser_current_file:='';
  529. { Close script }
  530. if (not AsmRes.Empty) then
  531. begin
  532. Message1(exec_i_closing_script,AsmRes.Fn);
  533. AsmRes.WriteToDisk;
  534. end;
  535. { do not create browsers on errors !! }
  536. if status.errorcount=0 then
  537. begin
  538. {$ifdef BrowserLog}
  539. { Write Browser Log }
  540. if (cs_browser_log in aktglobalswitches) and
  541. (cs_browser in aktmoduleswitches) then
  542. begin
  543. if browserlog.elements_to_list.empty then
  544. begin
  545. Message1(parser_i_writing_browser_log,browserlog.Fname);
  546. WriteBrowserLog;
  547. end
  548. else
  549. browserlog.list_elements;
  550. end;
  551. {$endif BrowserLog}
  552. { Write Browser Collections, also used by the TextMode IDE to
  553. retrieve a list of sourcefiles }
  554. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  555. end;
  556. end;
  557. end;
  558. dec(compile_level);
  559. compiled_module:=olddata^.old_compiled_module;
  560. SetCompileModule(compiled_module);
  561. dispose(olddata);
  562. end;
  563. end;
  564. end.