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