parser.pas 20 KB

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