parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit does the parsing process
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit parser;
  19. {$i defines.inc}
  20. interface
  21. procedure preprocess(const filename:string);
  22. procedure compile(const filename:string;compile_system:boolean);
  23. procedure initparser;
  24. procedure doneparser;
  25. implementation
  26. uses
  27. globtype,version,tokens,systems,
  28. cutils,cobjects,globals,verbose,
  29. symbase,symtable,symsym,fmodule,aasm,
  30. {$ifndef newcg}
  31. hcodegen,
  32. {$endif newcg}
  33. assemble,link,script,gendef,
  34. {$ifdef BrowserLog}
  35. browlog,
  36. {$endif BrowserLog}
  37. {$ifdef UseExcept}
  38. tpexcept,compiler,
  39. {$endif UseExcept}
  40. {$ifdef newcg}
  41. cgobj,
  42. cgcpu,
  43. { cgbase must be after hcodegen to use the correct procinfo !!! }
  44. cgbase,
  45. {$endif newcg}
  46. {$ifdef GDB}
  47. gdb,
  48. {$endif GDB}
  49. comphook,scanner,pbase,ptype,psystem,pmodules,cresstr;
  50. procedure initparser;
  51. begin
  52. { ^M means a string or a char, because we don't parse a }
  53. { type declaration }
  54. ignore_equal:=false;
  55. { we didn't parse a object or class declaration }
  56. { and no function header }
  57. testcurobject:=0;
  58. { a long time, this was forgotten }
  59. aktprocsym:=nil;
  60. current_module:=nil;
  61. compiled_module:=nil;
  62. procinfo:=nil;
  63. loaded_units.init;
  64. usedunits.init;
  65. { global switches }
  66. aktglobalswitches:=initglobalswitches;
  67. { scanner }
  68. c:=#0;
  69. pattern:='';
  70. orgpattern:='';
  71. current_scanner:=nil;
  72. { memory sizes }
  73. if heapsize=0 then
  74. heapsize:=target_info.heapsize;
  75. if maxheapsize=0 then
  76. maxheapsize:=target_info.maxheapsize;
  77. if stacksize=0 then
  78. stacksize:=target_info.stacksize;
  79. { open assembler response }
  80. AsmRes.Init(outputexedir+'ppas');
  81. { open deffile }
  82. DefFile.Init(outputexedir+inputfile+target_os.defext);
  83. { list of generated .o files, so the linker can remove them }
  84. SmartLinkOFiles.init;
  85. end;
  86. procedure doneparser;
  87. begin
  88. { unload units }
  89. loaded_units.done;
  90. usedunits.done;
  91. { close ppas,deffile }
  92. asmres.done;
  93. deffile.done;
  94. { free list of .o files }
  95. SmartLinkOFiles.done;
  96. end;
  97. procedure default_macros;
  98. var
  99. hp : pstring_item;
  100. begin
  101. { commandline }
  102. hp:=pstring_item(initdefines.first);
  103. while assigned(hp) do
  104. begin
  105. current_scanner^.def_macro(hp^.str^);
  106. hp:=pstring_item(hp^.next);
  107. end;
  108. { set macros for version checking }
  109. current_scanner^.set_macro('FPC_VERSION',version_nr);
  110. current_scanner^.set_macro('FPC_RELEASE',release_nr);
  111. current_scanner^.set_macro('FPC_PATCH',patch_nr);
  112. end;
  113. procedure preprocess(const filename:string);
  114. var
  115. i : longint;
  116. begin
  117. new(preprocfile,init('pre'));
  118. { default macros }
  119. current_scanner^.macros:=new(pdictionary,init);
  120. default_macros;
  121. { initialize a module }
  122. current_module:=new(pmodule,init(filename,false));
  123. main_module:=current_module;
  124. { startup scanner, and save in current_module }
  125. current_scanner:=new(pscannerfile,Init(filename));
  126. current_module^.scanner:=current_scanner;
  127. { loop until EOF is found }
  128. repeat
  129. current_scanner^.readtoken;
  130. preprocfile^.AddSpace;
  131. case token of
  132. _ID :
  133. begin
  134. preprocfile^.Add(orgpattern);
  135. end;
  136. _REALNUMBER,
  137. _INTCONST :
  138. preprocfile^.Add(pattern);
  139. _CSTRING :
  140. begin
  141. i:=0;
  142. while (i<length(pattern)) do
  143. begin
  144. inc(i);
  145. if pattern[i]='''' then
  146. begin
  147. insert('''',pattern,i);
  148. inc(i);
  149. end;
  150. end;
  151. preprocfile^.Add(''''+pattern+'''');
  152. end;
  153. _CCHAR :
  154. begin
  155. case pattern[1] of
  156. #39 :
  157. pattern:='''''''';
  158. #0..#31,
  159. #128..#255 :
  160. begin
  161. str(ord(pattern[1]),pattern);
  162. pattern:='#'+pattern;
  163. end;
  164. else
  165. pattern:=''''+pattern[1]+'''';
  166. end;
  167. preprocfile^.Add(pattern);
  168. end;
  169. _EOF :
  170. break;
  171. else
  172. preprocfile^.Add(tokeninfo^[token].str)
  173. end;
  174. until false;
  175. { free scanner }
  176. dispose(current_scanner,done);
  177. { close }
  178. dispose(preprocfile,done);
  179. end;
  180. procedure compile(const filename:string;compile_system:boolean);
  181. var
  182. { scanner }
  183. oldidtoken,
  184. oldtoken : ttoken;
  185. oldtokenpos : tfileposinfo;
  186. oldc : char;
  187. oldpattern,
  188. oldorgpattern : string;
  189. old_block_type : tblock_type;
  190. oldcurrent_scanner,prev_scanner,
  191. scanner : pscannerfile;
  192. { symtable }
  193. oldrefsymtable,
  194. olddefaultsymtablestack,
  195. oldsymtablestack : psymtable;
  196. oldprocprefix : string;
  197. oldaktprocsym : pprocsym;
  198. oldoverloaded_operators : toverloaded_operators;
  199. { cg }
  200. oldnextlabelnr : longint;
  201. oldparse_only : boolean;
  202. { asmlists }
  203. oldimports,
  204. oldexports,
  205. oldresource,
  206. oldrttilist,
  207. oldresourcestringlist,
  208. oldbsssegment,
  209. olddatasegment,
  210. oldcodesegment,
  211. oldexprasmlist,
  212. olddebuglist,
  213. oldwithdebuglist,
  214. oldconsts : paasmoutput;
  215. oldasmsymbollist : pdictionary;
  216. { resourcestrings }
  217. OldResourceStrings : PResourceStrings;
  218. { akt.. things }
  219. oldaktlocalswitches : tlocalswitches;
  220. oldaktmoduleswitches : tmoduleswitches;
  221. oldaktfilepos : tfileposinfo;
  222. oldaktpackenum,oldaktmaxfpuregisters : longint;
  223. oldaktpackrecords : tpackrecords;
  224. oldaktoutputformat : tasm;
  225. oldaktspecificoptprocessor,
  226. oldaktoptprocessor : tprocessors;
  227. oldaktasmmode : tasmmode;
  228. oldaktinterfacetype: tinterfacetypes;
  229. oldaktmodeswitches : tmodeswitches;
  230. old_compiled_module : pmodule;
  231. prev_name : pstring;
  232. {$ifdef USEEXCEPT}
  233. {$ifndef Delphi}
  234. recoverpos : jmp_buf;
  235. oldrecoverpos : pjmp_buf;
  236. {$endif Delphi}
  237. {$endif useexcept}
  238. {$ifdef newcg}
  239. oldcg : pcg;
  240. {$endif newcg}
  241. {$ifdef GDB}
  242. store_dbx : plongint;
  243. {$endif GDB}
  244. begin
  245. inc(compile_level);
  246. prev_name:=stringdup(parser_current_file);
  247. parser_current_file:=filename;
  248. old_compiled_module:=compiled_module;
  249. { save symtable state }
  250. oldsymtablestack:=symtablestack;
  251. olddefaultsymtablestack:=defaultsymtablestack;
  252. oldrefsymtable:=refsymtable;
  253. oldprocprefix:=procprefix;
  254. oldaktprocsym:=aktprocsym;
  255. move(overloaded_operators,oldoverloaded_operators,sizeof(toverloaded_operators));
  256. { save scanner state }
  257. oldc:=c;
  258. oldpattern:=pattern;
  259. oldorgpattern:=orgpattern;
  260. oldtoken:=token;
  261. oldidtoken:=idtoken;
  262. old_block_type:=block_type;
  263. oldtokenpos:=akttokenpos;
  264. oldcurrent_scanner:=current_scanner;
  265. { save cg }
  266. oldnextlabelnr:=nextlabelnr;
  267. oldparse_only:=parse_only;
  268. { save assembler lists }
  269. olddatasegment:=datasegment;
  270. oldbsssegment:=bsssegment;
  271. oldcodesegment:=codesegment;
  272. olddebuglist:=debuglist;
  273. oldwithdebuglist:=withdebuglist;
  274. oldconsts:=consts;
  275. oldrttilist:=rttilist;
  276. oldexprasmlist:=exprasmlist;
  277. oldimports:=importssection;
  278. oldexports:=exportssection;
  279. oldresource:=resourcesection;
  280. oldresourcestringlist:=resourcestringlist;
  281. oldasmsymbollist:=asmsymbollist;
  282. OldResourceStrings:=ResourceStrings;
  283. { save akt... state }
  284. { handle the postponed case first }
  285. if localswitcheschanged then
  286. begin
  287. aktlocalswitches:=nextaktlocalswitches;
  288. localswitcheschanged:=false;
  289. end;
  290. oldaktlocalswitches:=aktlocalswitches;
  291. oldaktmoduleswitches:=aktmoduleswitches;
  292. oldaktpackrecords:=aktpackrecords;
  293. oldaktpackenum:=aktpackenum;
  294. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  295. oldaktoutputformat:=aktoutputformat;
  296. oldaktoptprocessor:=aktoptprocessor;
  297. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  298. oldaktasmmode:=aktasmmode;
  299. oldaktinterfacetype:=aktinterfacetype;
  300. oldaktfilepos:=aktfilepos;
  301. oldaktmodeswitches:=aktmodeswitches;
  302. {$ifdef newcg}
  303. oldcg:=cg;
  304. {$endif newcg}
  305. {$ifdef GDB}
  306. store_dbx:=dbx_counter;
  307. dbx_counter:=nil;
  308. {$endif GDB}
  309. { show info }
  310. Message1(parser_i_compiling,filename);
  311. { reset symtable }
  312. symtablestack:=nil;
  313. defaultsymtablestack:=nil;
  314. systemunit:=nil;
  315. refsymtable:=nil;
  316. aktprocsym:=nil;
  317. procprefix:='';
  318. registerdef:=true;
  319. aktmaxfpuregisters:=-1;
  320. fillchar(overloaded_operators,sizeof(toverloaded_operators),0);
  321. { reset the unit or create a new program }
  322. if assigned(current_module) then
  323. begin
  324. {current_module^.reset this is wrong !! }
  325. scanner:=current_module^.scanner;
  326. current_module^.reset;
  327. current_module^.scanner:=scanner;
  328. end
  329. else
  330. begin
  331. current_module:=new(pmodule,init(filename,false));
  332. main_module:=current_module;
  333. end;
  334. { Set the module to use for verbose }
  335. SetCompileModule(current_module);
  336. compiled_module:=current_module;
  337. current_module^.in_compile:=true;
  338. { Load current state from the init values }
  339. aktlocalswitches:=initlocalswitches;
  340. aktmoduleswitches:=initmoduleswitches;
  341. aktmodeswitches:=initmodeswitches;
  342. {$IFDEF Testvarsets}
  343. aktsetalloc:=initsetalloc;
  344. {$ENDIF}
  345. aktpackrecords:=initpackrecords;
  346. aktpackenum:=initpackenum;
  347. aktoutputformat:=initoutputformat;
  348. aktoptprocessor:=initoptprocessor;
  349. aktspecificoptprocessor:=initspecificoptprocessor;
  350. aktasmmode:=initasmmode;
  351. aktinterfacetype:=initinterfacetype;
  352. { we need this to make the system unit }
  353. if compile_system then
  354. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  355. { startup scanner, and save in current_module }
  356. current_scanner:=new(pscannerfile,Init(filename));
  357. { macros }
  358. default_macros;
  359. { read the first token }
  360. current_scanner^.readtoken;
  361. prev_scanner:=current_module^.scanner;
  362. current_module^.scanner:=current_scanner;
  363. { init code generator for a new module }
  364. codegen_newmodule;
  365. {$ifdef newcg}
  366. {$ifdef i386}
  367. cg:=new(pcg386,init);
  368. {$endif i386}
  369. {$ifdef alpha}
  370. cg:=new(pcgalpha,init);
  371. {$endif alpha}
  372. {$ifdef powerpc}
  373. cg:=new(pcgppc,init);
  374. {$endif powerpc}
  375. {$endif newcg}
  376. { If the compile level > 1 we get a nice "unit expected" error
  377. message if we are trying to use a program as unit.}
  378. {$ifdef USEEXCEPT}
  379. if setjmp(recoverpos)=0 then
  380. begin
  381. oldrecoverpos:=recoverpospointer;
  382. recoverpospointer:=@recoverpos;
  383. {$endif USEEXCEPT}
  384. if (token=_UNIT) or (compile_level>1) then
  385. begin
  386. current_module^.is_unit:=true;
  387. proc_unit;
  388. end
  389. else
  390. proc_program(token=_LIBRARY);
  391. {$ifdef USEEXCEPT}
  392. recoverpospointer:=oldrecoverpos;
  393. end
  394. else
  395. begin
  396. recoverpospointer:=oldrecoverpos;
  397. longjump_used:=true;
  398. end;
  399. {$endif USEEXCEPT}
  400. { clear memory }
  401. {$ifdef Splitheap}
  402. if testsplit then
  403. begin
  404. { temp heap should be empty after that !!!}
  405. codegen_donemodule;
  406. Releasetempheap;
  407. end;
  408. {$endif Splitheap}
  409. { restore old state, close trees, > 0.99.5 has heapblocks, so
  410. it's the default to release the trees }
  411. codegen_donemodule;
  412. {$ifdef newcg}
  413. dispose(cg,done);
  414. {$endif newcg}
  415. { free ppu }
  416. if assigned(current_module^.ppufile) then
  417. begin
  418. dispose(current_module^.ppufile,done);
  419. current_module^.ppufile:=nil;
  420. end;
  421. { free scanner }
  422. dispose(current_scanner,done);
  423. { restore previous scanner !! }
  424. current_module^.scanner:=prev_scanner;
  425. if assigned(prev_scanner) then
  426. prev_scanner^.invalid:=true;
  427. if (compile_level>1) then
  428. begin
  429. {$ifdef newcg}
  430. cg:=oldcg;
  431. {$endif newcg}
  432. {$ifdef GDB}
  433. dbx_counter:=store_dbx;
  434. {$endif GDB}
  435. { restore scanner }
  436. c:=oldc;
  437. pattern:=oldpattern;
  438. orgpattern:=oldorgpattern;
  439. token:=oldtoken;
  440. idtoken:=oldidtoken;
  441. akttokenpos:=oldtokenpos;
  442. block_type:=old_block_type;
  443. current_scanner:=oldcurrent_scanner;
  444. { restore cg }
  445. nextlabelnr:=oldnextlabelnr;
  446. parse_only:=oldparse_only;
  447. { restore asmlists }
  448. exprasmlist:=oldexprasmlist;
  449. datasegment:=olddatasegment;
  450. bsssegment:=oldbsssegment;
  451. codesegment:=oldcodesegment;
  452. consts:=oldconsts;
  453. debuglist:=olddebuglist;
  454. withdebuglist:=oldwithdebuglist;
  455. importssection:=oldimports;
  456. exportssection:=oldexports;
  457. resourcesection:=oldresource;
  458. rttilist:=oldrttilist;
  459. resourcestringlist:=oldresourcestringlist;
  460. asmsymbollist:=oldasmsymbollist;
  461. ResourceStrings:=OldResourceStrings;
  462. { restore symtable state }
  463. refsymtable:=oldrefsymtable;
  464. symtablestack:=oldsymtablestack;
  465. defaultsymtablestack:=olddefaultsymtablestack;
  466. aktprocsym:=oldaktprocsym;
  467. procprefix:=oldprocprefix;
  468. move(oldoverloaded_operators,overloaded_operators,sizeof(toverloaded_operators));
  469. aktlocalswitches:=oldaktlocalswitches;
  470. aktmoduleswitches:=oldaktmoduleswitches;
  471. aktpackrecords:=oldaktpackrecords;
  472. aktpackenum:=oldaktpackenum;
  473. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  474. aktoutputformat:=oldaktoutputformat;
  475. aktoptprocessor:=oldaktoptprocessor;
  476. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  477. aktasmmode:=oldaktasmmode;
  478. aktinterfacetype:=oldaktinterfacetype;
  479. aktfilepos:=oldaktfilepos;
  480. aktmodeswitches:=oldaktmodeswitches;
  481. end;
  482. { Shut down things when the last file is compiled }
  483. if (compile_level=1) then
  484. begin
  485. { Close script }
  486. if (not AsmRes.Empty) then
  487. begin
  488. Message1(exec_i_closing_script,AsmRes.Fn);
  489. AsmRes.WriteToDisk;
  490. end;
  491. {$ifdef USEEXCEPT}
  492. if not longjump_used then
  493. {$endif USEEXCEPT}
  494. { do not create browsers on errors !! }
  495. if status.errorcount=0 then
  496. begin
  497. {$ifdef BrowserLog}
  498. { Write Browser Log }
  499. if (cs_browser_log in aktglobalswitches) and
  500. (cs_browser in aktmoduleswitches) then
  501. begin
  502. if browserlog.elements_to_list^.empty then
  503. begin
  504. Message1(parser_i_writing_browser_log,browserlog.Fname);
  505. WriteBrowserLog;
  506. end
  507. else
  508. browserlog.list_elements;
  509. end;
  510. {$endif BrowserLog}
  511. { Write Browser Collections }
  512. do_extractsymbolinfo;
  513. end;
  514. if current_module^.in_second_compile then
  515. begin
  516. current_module^.in_second_compile:=false;
  517. current_module^.in_compile:=true;
  518. end
  519. else
  520. current_module^.in_compile:=false;
  521. (* Obsolete code aktprocsym
  522. is disposed by the localsymtable disposal (PM)
  523. { Free last aktprocsym }
  524. if assigned(aktprocsym) and (aktprocsym^.owner=nil) then
  525. begin
  526. { init parts are not needed in units !! }
  527. if current_module^.is_unit then
  528. aktprocsym^.definition^.forwarddef:=false;
  529. dispose(aktprocsym,done);
  530. end; *)
  531. end;
  532. dec(compile_level);
  533. parser_current_file:=prev_name^;
  534. stringdispose(prev_name);
  535. compiled_module:=old_compiled_module;
  536. {$ifdef USEEXCEPT}
  537. if longjump_used then
  538. longjmp(recoverpospointer^,1);
  539. {$endif USEEXCEPT}
  540. end;
  541. end.
  542. {
  543. $Log$
  544. Revision 1.9 2000-11-04 14:25:20 florian
  545. + merged Attila's changes for interfaces, not tested yet
  546. Revision 1.8 2000/10/31 22:02:49 peter
  547. * symtable splitted, no real code changes
  548. Revision 1.7 2000/10/14 10:14:51 peter
  549. * moehrendorf oct 2000 rewrite
  550. Revision 1.6 2000/10/01 19:48:25 peter
  551. * lot of compile updates for cg11
  552. Revision 1.5 2000/09/24 15:06:20 peter
  553. * use defines.inc
  554. Revision 1.4 2000/08/27 16:11:51 peter
  555. * moved some util functions from globals,cobjects to cutils
  556. * splitted files into finput,fmodule
  557. Revision 1.3 2000/08/12 15:34:22 peter
  558. + usedasmsymbollist to check and reset only the used symbols (merged)
  559. Revision 1.2 2000/07/13 11:32:44 michael
  560. + removed logs
  561. }