parser.pas 21 KB

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