parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. oldaktspecificoptprocessor,
  282. oldaktoptprocessor : tprocessors;
  283. oldaktfputype : tfputype;
  284. oldaktasmmode : tasmmode;
  285. oldaktinterfacetype: tinterfacetypes;
  286. oldaktmodeswitches : tmodeswitches;
  287. old_compiled_module : tmodule;
  288. oldcurrent_procinfo : tprocinfo;
  289. oldaktdefproccall : tproccalloption;
  290. oldsourcecodepage : tcodepagestring;
  291. end;
  292. var
  293. olddata : polddata;
  294. begin
  295. inc(compile_level);
  296. parser_current_file:=filename;
  297. { Uses heap memory instead of placing everything on the
  298. stack. This is needed because compile() can be called
  299. recursively }
  300. new(olddata);
  301. with olddata^ do
  302. begin
  303. old_compiled_module:=compiled_module;
  304. { save symtable state }
  305. oldsymtablestack:=symtablestack;
  306. oldmacrosymtablestack:=macrosymtablestack;
  307. olddefaultsymtablestack:=defaultsymtablestack;
  308. olddefaultmacrosymtablestack:=defaultmacrosymtablestack;
  309. oldrefsymtable:=refsymtable;
  310. oldcurrent_procinfo:=current_procinfo;
  311. oldaktdefproccall:=aktdefproccall;
  312. { save scanner state }
  313. oldc:=c;
  314. oldpattern:=pattern;
  315. oldorgpattern:=orgpattern;
  316. oldtoken:=token;
  317. oldidtoken:=idtoken;
  318. old_block_type:=block_type;
  319. oldtokenpos:=akttokenpos;
  320. oldsourcecodepage:=aktsourcecodepage;
  321. { save cg }
  322. oldparse_only:=parse_only;
  323. { save assembler lists }
  324. oldasmlist:=asmlist;
  325. oldexprasmlist:=exprasmlist;
  326. oldobjectlibrary:=objectlibrary;
  327. Oldresourcestrings:=resourcestrings;
  328. { save akt... state }
  329. { handle the postponed case first }
  330. if localswitcheschanged then
  331. begin
  332. aktlocalswitches:=nextaktlocalswitches;
  333. localswitcheschanged:=false;
  334. end;
  335. oldaktlocalswitches:=aktlocalswitches;
  336. oldaktmoduleswitches:=aktmoduleswitches;
  337. oldaktalignment:=aktalignment;
  338. oldaktpackenum:=aktpackenum;
  339. oldaktpackrecords:=aktpackrecords;
  340. oldaktfputype:=aktfputype;
  341. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  342. oldaktoptprocessor:=aktoptprocessor;
  343. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  344. oldaktasmmode:=aktasmmode;
  345. oldaktinterfacetype:=aktinterfacetype;
  346. oldaktfilepos:=aktfilepos;
  347. oldaktmodeswitches:=aktmodeswitches;
  348. end;
  349. { show info }
  350. Message1(parser_i_compiling,filename);
  351. { reset symtable }
  352. symtablestack:=nil;
  353. macrosymtablestack:=nil;
  354. defaultsymtablestack:=nil;
  355. defaultmacrosymtablestack:=nil;
  356. systemunit:=nil;
  357. refsymtable:=nil;
  358. aktdefproccall:=initdefproccall;
  359. registerdef:=true;
  360. aktexceptblock:=0;
  361. exceptblockcounter:=0;
  362. aktmaxfpuregisters:=-1;
  363. { reset the unit or create a new program }
  364. { a unit compiled at command line must be inside the loaded_unit list }
  365. if (compile_level=1) then
  366. begin
  367. if assigned(current_module) then
  368. internalerror(200501158);
  369. current_module:=tppumodule.create(nil,filename,'',false);
  370. addloadedunit(current_module);
  371. main_module:=current_module;
  372. current_module.state:=ms_compile;
  373. end;
  374. if not(assigned(current_module) and
  375. (current_module.state in [ms_compile,ms_second_compile])) then
  376. internalerror(200212281);
  377. { Set the module to use for verbose }
  378. compiled_module:=current_module;
  379. SetCompileModule(current_module);
  380. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  381. { Load current state from the init values }
  382. aktlocalswitches:=initlocalswitches;
  383. aktmoduleswitches:=initmoduleswitches;
  384. aktmodeswitches:=initmodeswitches;
  385. {$IFDEF Testvarsets}
  386. aktsetalloc:=initsetalloc;
  387. {$ENDIF}
  388. aktalignment:=initalignment;
  389. aktfputype:=initfputype;
  390. aktpackenum:=initpackenum;
  391. aktpackrecords:=0;
  392. aktoptprocessor:=initoptprocessor;
  393. aktspecificoptprocessor:=initspecificoptprocessor;
  394. aktasmmode:=initasmmode;
  395. aktinterfacetype:=initinterfacetype;
  396. { startup scanner and load the first file }
  397. current_scanner:=tscannerfile.Create(filename);
  398. current_scanner.firstfile;
  399. current_module.scanner:=current_scanner;
  400. { init macros before anything in the file is parsed.}
  401. macrosymtablestack:= initialmacrosymtable;
  402. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  403. current_module.localmacrosymtable.next:= initialmacrosymtable;
  404. macrosymtablestack:= current_module.localmacrosymtable;
  405. { read the first token }
  406. current_scanner.readtoken;
  407. { init code generator for a new module }
  408. init_module;
  409. { If the compile level > 1 we get a nice "unit expected" error
  410. message if we are trying to use a program as unit.}
  411. try
  412. try
  413. if (token=_UNIT) or (compile_level>1) then
  414. begin
  415. current_module.is_unit:=true;
  416. proc_unit;
  417. end
  418. else
  419. proc_program(token=_LIBRARY);
  420. except
  421. on ECompilerAbort do
  422. raise;
  423. on Exception do
  424. begin
  425. { Increase errorcounter to prevent some
  426. checks during cleanup }
  427. inc(status.errorcount);
  428. raise;
  429. end;
  430. end;
  431. finally
  432. { restore old state }
  433. done_module;
  434. if assigned(current_module) then
  435. begin
  436. { module is now compiled }
  437. tppumodule(current_module).state:=ms_compiled;
  438. { free ppu }
  439. if assigned(tppumodule(current_module).ppufile) then
  440. begin
  441. tppumodule(current_module).ppufile.free;
  442. tppumodule(current_module).ppufile:=nil;
  443. end;
  444. { free scanner }
  445. if assigned(current_module.scanner) then
  446. begin
  447. if current_scanner=tscannerfile(current_module.scanner) then
  448. current_scanner:=nil;
  449. tscannerfile(current_module.scanner).free;
  450. current_module.scanner:=nil;
  451. end;
  452. end;
  453. if (compile_level>1) then
  454. begin
  455. with olddata^ do
  456. begin
  457. { restore scanner }
  458. c:=oldc;
  459. pattern:=oldpattern;
  460. orgpattern:=oldorgpattern;
  461. token:=oldtoken;
  462. idtoken:=oldidtoken;
  463. akttokenpos:=oldtokenpos;
  464. block_type:=old_block_type;
  465. { restore cg }
  466. parse_only:=oldparse_only;
  467. { restore asmlists }
  468. exprasmlist:=oldexprasmlist;
  469. asmlist:=oldasmlist;
  470. { object data }
  471. resourcestrings:=oldresourcestrings;
  472. objectlibrary:=oldobjectlibrary;
  473. { restore previous scanner }
  474. if assigned(old_compiled_module) then
  475. current_scanner:=tscannerfile(old_compiled_module.scanner)
  476. else
  477. current_scanner:=nil;
  478. if assigned(current_scanner) then
  479. parser_current_file:=current_scanner.inputfile.name^;
  480. { restore symtable state }
  481. refsymtable:=oldrefsymtable;
  482. symtablestack:=oldsymtablestack;
  483. macrosymtablestack:=oldmacrosymtablestack;
  484. defaultsymtablestack:=olddefaultsymtablestack;
  485. defaultmacrosymtablestack:=olddefaultmacrosymtablestack;
  486. aktdefproccall:=oldaktdefproccall;
  487. current_procinfo:=oldcurrent_procinfo;
  488. aktsourcecodepage:=oldsourcecodepage;
  489. aktlocalswitches:=oldaktlocalswitches;
  490. aktmoduleswitches:=oldaktmoduleswitches;
  491. aktalignment:=oldaktalignment;
  492. aktpackenum:=oldaktpackenum;
  493. aktpackrecords:=oldaktpackrecords;
  494. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  495. aktoptprocessor:=oldaktoptprocessor;
  496. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  497. aktfputype:=oldaktfputype;
  498. aktasmmode:=oldaktasmmode;
  499. aktinterfacetype:=oldaktinterfacetype;
  500. aktfilepos:=oldaktfilepos;
  501. aktmodeswitches:=oldaktmodeswitches;
  502. aktexceptblock:=0;
  503. exceptblockcounter:=0;
  504. end;
  505. end
  506. else
  507. begin
  508. { Shut down things when the last file is compiled succesfull }
  509. if (compile_level=1) and
  510. (status.errorcount=0) then
  511. begin
  512. parser_current_file:='';
  513. { Close script }
  514. if (not AsmRes.Empty) then
  515. begin
  516. Message1(exec_i_closing_script,AsmRes.Fn);
  517. AsmRes.WriteToDisk;
  518. end;
  519. { do not create browsers on errors !! }
  520. if status.errorcount=0 then
  521. begin
  522. {$ifdef BrowserLog}
  523. { Write Browser Log }
  524. if (cs_browser_log in aktglobalswitches) and
  525. (cs_browser in aktmoduleswitches) then
  526. begin
  527. if browserlog.elements_to_list.empty then
  528. begin
  529. Message1(parser_i_writing_browser_log,browserlog.Fname);
  530. WriteBrowserLog;
  531. end
  532. else
  533. browserlog.list_elements;
  534. end;
  535. {$endif BrowserLog}
  536. { Write Browser Collections, also used by the TextMode IDE to
  537. retrieve a list of sourcefiles }
  538. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  539. end;
  540. end;
  541. end;
  542. dec(compile_level);
  543. compiled_module:=olddata^.old_compiled_module;
  544. SetCompileModule(compiled_module);
  545. dispose(olddata);
  546. end;
  547. end;
  548. end.