parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. {$ifdef tp}
  19. {$E+,N+,D+,F+}
  20. {$endif}
  21. unit parser;
  22. { Use exception catching so the compiler goes futher after a Stop }
  23. {$ifdef i386}
  24. {$define USEEXCEPT}
  25. {$endif}
  26. {$ifdef TP}
  27. {$ifdef DPMI}
  28. {$undef USEEXCEPT}
  29. {$endif}
  30. {$endif}
  31. interface
  32. procedure preprocess(const filename:string);
  33. procedure compile(const filename:string;compile_system:boolean);
  34. procedure initparser;
  35. procedure doneparser;
  36. implementation
  37. uses
  38. globtype,version,tokens,systems,
  39. cobjects,globals,verbose,
  40. symtable,files,aasm,
  41. {$ifndef newcg}
  42. hcodegen,
  43. {$endif newcg}
  44. assemble,link,script,gendef,
  45. {$ifdef BrowserLog}
  46. browlog,
  47. {$endif BrowserLog}
  48. {$ifdef BrowserCol}
  49. browcol,
  50. {$endif BrowserCol}
  51. {$ifdef UseExcept}
  52. tpexcept,compiler,
  53. {$endif UseExcept}
  54. {$ifdef newcg}
  55. cgobj,
  56. cgcpu,
  57. { cgbase must be after hcodegen to use the correct procinfo !!! }
  58. cgbase,
  59. {$endif newcg}
  60. {$ifdef GDB}
  61. gdb,
  62. {$endif GDB}
  63. comphook,tree,scanner,pbase,ptype,psystem,pmodules,cresstr;
  64. procedure initparser;
  65. begin
  66. { ^M means a string or a char, because we don't parse a }
  67. { type declaration }
  68. ignore_equal:=false;
  69. { we didn't parse a object or class declaration }
  70. { and no function header }
  71. testcurobject:=0;
  72. { a long time, this was forgotten }
  73. aktprocsym:=nil;
  74. current_module:=nil;
  75. compiled_module:=nil;
  76. loaded_units.init;
  77. usedunits.init;
  78. { global switches }
  79. aktglobalswitches:=initglobalswitches;
  80. { scanner }
  81. c:=#0;
  82. pattern:='';
  83. orgpattern:='';
  84. current_scanner:=nil;
  85. { memory sizes }
  86. if heapsize=0 then
  87. heapsize:=target_info.heapsize;
  88. if maxheapsize=0 then
  89. maxheapsize:=target_info.maxheapsize;
  90. if stacksize=0 then
  91. stacksize:=target_info.stacksize;
  92. { open assembler response }
  93. AsmRes.Init(outputexedir+'ppas');
  94. { open deffile }
  95. DefFile.Init(outputexedir+inputfile+target_os.defext);
  96. end;
  97. procedure doneparser;
  98. begin
  99. { unload units }
  100. loaded_units.done;
  101. usedunits.done;
  102. { close ppas and deffile }
  103. asmres.done;
  104. deffile.done;
  105. end;
  106. procedure default_macros;
  107. var
  108. hp : pstring_item;
  109. begin
  110. { commandline }
  111. hp:=pstring_item(initdefines.first);
  112. while assigned(hp) do
  113. begin
  114. def_macro(hp^.str^);
  115. hp:=pstring_item(hp^.next);
  116. end;
  117. { set macros for version checking }
  118. set_macro('FPC_VERSION',version_nr);
  119. set_macro('FPC_RELEASE',release_nr);
  120. set_macro('FPC_PATCH',patch_nr);
  121. end;
  122. procedure preprocess(const filename:string);
  123. var
  124. i : longint;
  125. begin
  126. new(preprocfile,init('pre'));
  127. { default macros }
  128. macros:=new(psymtable,init(macrosymtable));
  129. macros^.name:=stringdup('Conditionals for '+filename);
  130. default_macros;
  131. { initialize a module }
  132. current_module:=new(pmodule,init(filename,false));
  133. main_module:=current_module;
  134. { startup scanner, and save in current_module }
  135. current_scanner:=new(pscannerfile,Init(filename));
  136. current_module^.scanner:=current_scanner;
  137. { loop until EOF is found }
  138. repeat
  139. current_scanner^.readtoken;
  140. preprocfile^.AddSpace;
  141. case token of
  142. _ID :
  143. begin
  144. preprocfile^.Add(orgpattern);
  145. end;
  146. _REALNUMBER,
  147. _INTCONST :
  148. preprocfile^.Add(pattern);
  149. _CSTRING :
  150. begin
  151. i:=0;
  152. while (i<length(pattern)) do
  153. begin
  154. inc(i);
  155. if pattern[i]='''' then
  156. begin
  157. insert('''',pattern,i);
  158. inc(i);
  159. end;
  160. end;
  161. preprocfile^.Add(''''+pattern+'''');
  162. end;
  163. _CCHAR :
  164. begin
  165. case pattern[1] of
  166. #39 :
  167. pattern:='''''''';
  168. #0..#31,
  169. #128..#255 :
  170. begin
  171. str(ord(pattern[1]),pattern);
  172. pattern:='#'+pattern;
  173. end;
  174. else
  175. pattern:=''''+pattern[1]+'''';
  176. end;
  177. preprocfile^.Add(pattern);
  178. end;
  179. _EOF :
  180. break;
  181. else
  182. preprocfile^.Add(tokeninfo^[token].str)
  183. end;
  184. until false;
  185. { free scanner }
  186. dispose(current_scanner,done);
  187. { close }
  188. dispose(preprocfile,done);
  189. end;
  190. procedure compile(const filename:string;compile_system:boolean);
  191. var
  192. { scanner }
  193. oldidtoken,
  194. oldtoken : ttoken;
  195. oldtokenpos : tfileposinfo;
  196. oldc : char;
  197. oldpattern,
  198. oldorgpattern : string;
  199. old_block_type : tblock_type;
  200. oldcurrent_scanner,prev_scanner,
  201. scanner : pscannerfile;
  202. { symtable }
  203. oldmacros,
  204. oldrefsymtable,
  205. olddefaultsymtablestack,
  206. oldsymtablestack : psymtable;
  207. oldprocprefix : string;
  208. oldaktprocsym : pprocsym;
  209. { cg }
  210. oldnextlabelnr : longint;
  211. oldparse_only : boolean;
  212. { asmlists }
  213. oldimports,
  214. oldexports,
  215. oldresource,
  216. oldrttilist,
  217. oldresourcestringlist,
  218. oldbsssegment,
  219. olddatasegment,
  220. oldcodesegment,
  221. oldexprasmlist,
  222. olddebuglist,
  223. oldconsts : paasmoutput;
  224. oldasmsymbollist : pasmsymbollist;
  225. { akt.. things }
  226. oldaktlocalswitches : tlocalswitches;
  227. oldaktmoduleswitches : tmoduleswitches;
  228. oldaktfilepos : tfileposinfo;
  229. oldaktpackenum,oldaktmaxfpuregisters : longint;
  230. oldaktpackrecords : tpackrecords;
  231. oldaktoutputformat : tasm;
  232. oldaktoptprocessor : tprocessors;
  233. oldaktasmmode : tasmmode;
  234. oldaktmodeswitches : tmodeswitches;
  235. old_compiled_module : pmodule;
  236. prev_name : pstring;
  237. {$ifdef USEEXCEPT}
  238. recoverpos : jmp_buf;
  239. oldrecoverpos : pjmp_buf;
  240. {$endif useexcept}
  241. {$ifdef newcg}
  242. oldcg : pcg;
  243. {$endif newcg}
  244. {$ifdef GDB}
  245. store_dbx : plongint;
  246. {$endif GDB}
  247. begin
  248. inc(compile_level);
  249. prev_name:=stringdup(parser_current_file);
  250. parser_current_file:=filename;
  251. old_compiled_module:=compiled_module;
  252. { save symtable state }
  253. oldsymtablestack:=symtablestack;
  254. olddefaultsymtablestack:=defaultsymtablestack;
  255. oldrefsymtable:=refsymtable;
  256. oldmacros:=macros;
  257. oldprocprefix:=procprefix;
  258. oldaktprocsym:=aktprocsym;
  259. { save scanner state }
  260. oldc:=c;
  261. oldpattern:=pattern;
  262. oldorgpattern:=orgpattern;
  263. oldtoken:=token;
  264. oldidtoken:=idtoken;
  265. old_block_type:=block_type;
  266. oldtokenpos:=tokenpos;
  267. oldcurrent_scanner:=current_scanner;
  268. { save cg }
  269. oldnextlabelnr:=nextlabelnr;
  270. oldparse_only:=parse_only;
  271. { save assembler lists }
  272. olddatasegment:=datasegment;
  273. oldbsssegment:=bsssegment;
  274. oldcodesegment:=codesegment;
  275. olddebuglist:=debuglist;
  276. oldconsts:=consts;
  277. oldrttilist:=rttilist;
  278. oldexprasmlist:=exprasmlist;
  279. oldimports:=importssection;
  280. oldexports:=exportssection;
  281. oldresource:=resourcesection;
  282. oldresourcestringlist:=resourcestringlist;
  283. oldasmsymbollist:=asmsymbollist;
  284. { save akt... state }
  285. oldaktlocalswitches:=aktlocalswitches;
  286. oldaktmoduleswitches:=aktmoduleswitches;
  287. oldaktpackrecords:=aktpackrecords;
  288. oldaktpackenum:=aktpackenum;
  289. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  290. oldaktoutputformat:=aktoutputformat;
  291. oldaktoptprocessor:=aktoptprocessor;
  292. oldaktasmmode:=aktasmmode;
  293. oldaktfilepos:=aktfilepos;
  294. oldaktmodeswitches:=aktmodeswitches;
  295. {$ifdef newcg}
  296. oldcg:=cg;
  297. {$endif newcg}
  298. {$ifdef GDB}
  299. store_dbx:=dbx_counter;
  300. dbx_counter:=nil;
  301. {$endif GDB}
  302. { show info }
  303. Message1(parser_i_compiling,filename);
  304. { reset symtable }
  305. symtablestack:=nil;
  306. defaultsymtablestack:=nil;
  307. systemunit:=nil;
  308. refsymtable:=nil;
  309. aktprocsym:=nil;
  310. procprefix:='';
  311. registerdef:=true;
  312. aktmaxfpuregisters:=-1;
  313. { macros }
  314. macros:=new(psymtable,init(macrosymtable));
  315. macros^.name:=stringdup('Conditionals for '+filename);
  316. default_macros;
  317. { reset the unit or create a new program }
  318. if assigned(current_module) then
  319. begin
  320. {current_module^.reset this is wrong !! }
  321. scanner:=current_module^.scanner;
  322. current_module^.reset;
  323. current_module^.scanner:=scanner;
  324. end
  325. else
  326. begin
  327. current_module:=new(pmodule,init(filename,false));
  328. main_module:=current_module;
  329. end;
  330. compiled_module:=current_module;
  331. current_module^.in_compile:=true;
  332. { Load current state from the init values }
  333. aktlocalswitches:=initlocalswitches;
  334. aktmoduleswitches:=initmoduleswitches;
  335. aktmodeswitches:=initmodeswitches;
  336. aktpackrecords:=initpackrecords;
  337. aktpackenum:=initpackenum;
  338. aktoutputformat:=initoutputformat;
  339. aktoptprocessor:=initoptprocessor;
  340. aktasmmode:=initasmmode;
  341. { we need this to make the system unit }
  342. if compile_system then
  343. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  344. { startup scanner, and save in current_module }
  345. current_scanner:=new(pscannerfile,Init(filename));
  346. current_scanner^.readtoken;
  347. prev_scanner:=current_module^.scanner;
  348. current_module^.scanner:=current_scanner;
  349. { init code generator for a new module }
  350. codegen_newmodule;
  351. {$ifdef newcg}
  352. {$ifdef i386}
  353. cg:=new(pcg386,init);
  354. {$endif i386}
  355. {$ifdef alpha}
  356. cg:=new(pcgalpha,init);
  357. {$endif alpha}
  358. {$ifdef powerpc}
  359. cg:=new(pcgppc,init);
  360. {$endif powerpc}
  361. {$endif newcg}
  362. { If the compile level > 1 we get a nice "unit expected" error
  363. message if we are trying to use a program as unit.}
  364. {$ifdef USEEXCEPT}
  365. if setjmp(recoverpos)=0 then
  366. begin
  367. oldrecoverpos:=recoverpospointer;
  368. recoverpospointer:=@recoverpos;
  369. {$endif USEEXCEPT}
  370. if (token=_UNIT) or (compile_level>1) then
  371. begin
  372. current_module^.is_unit:=true;
  373. proc_unit;
  374. end
  375. else
  376. proc_program(token=_LIBRARY);
  377. {$ifdef USEEXCEPT}
  378. recoverpospointer:=oldrecoverpos;
  379. end
  380. else
  381. begin
  382. recoverpospointer:=oldrecoverpos;
  383. longjump_used:=true;
  384. end;
  385. {$endif USEEXCEPT}
  386. { clear memory }
  387. {$ifdef Splitheap}
  388. if testsplit then
  389. begin
  390. { temp heap should be empty after that !!!}
  391. codegen_donemodule;
  392. Releasetempheap;
  393. end;
  394. {$endif Splitheap}
  395. { restore old state, close trees, > 0.99.5 has heapblocks, so
  396. it's the default to release the trees }
  397. codegen_donemodule;
  398. {$ifdef newcg}
  399. dispose(cg,done);
  400. {$endif newcg}
  401. { free ppu }
  402. if assigned(current_module^.ppufile) then
  403. begin
  404. dispose(current_module^.ppufile,done);
  405. current_module^.ppufile:=nil;
  406. end;
  407. { free scanner }
  408. dispose(current_scanner,done);
  409. { restore previous scanner !! }
  410. current_module^.scanner:=prev_scanner;
  411. if assigned(prev_scanner) then
  412. prev_scanner^.invalid:=true;
  413. { free macros }
  414. {!!! No check for unused macros yet !!! }
  415. dispose(macros,done);
  416. if (compile_level>1) then
  417. begin
  418. {$ifdef newcg}
  419. cg:=oldcg;
  420. {$endif newcg}
  421. {$ifdef GDB}
  422. dbx_counter:=store_dbx;
  423. {$endif GDB}
  424. { restore scanner }
  425. c:=oldc;
  426. pattern:=oldpattern;
  427. orgpattern:=oldorgpattern;
  428. token:=oldtoken;
  429. idtoken:=oldidtoken;
  430. tokenpos:=oldtokenpos;
  431. block_type:=old_block_type;
  432. current_scanner:=oldcurrent_scanner;
  433. { restore cg }
  434. nextlabelnr:=oldnextlabelnr;
  435. parse_only:=oldparse_only;
  436. { restore asmlists }
  437. exprasmlist:=oldexprasmlist;
  438. datasegment:=olddatasegment;
  439. bsssegment:=oldbsssegment;
  440. codesegment:=oldcodesegment;
  441. consts:=oldconsts;
  442. debuglist:=olddebuglist;
  443. importssection:=oldimports;
  444. exportssection:=oldexports;
  445. resourcesection:=oldresource;
  446. rttilist:=oldrttilist;
  447. resourcestringlist:=oldresourcestringlist;
  448. asmsymbollist:=oldasmsymbollist;
  449. { restore symtable state }
  450. refsymtable:=oldrefsymtable;
  451. symtablestack:=oldsymtablestack;
  452. defaultsymtablestack:=olddefaultsymtablestack;
  453. macros:=oldmacros;
  454. aktprocsym:=oldaktprocsym;
  455. procprefix:=oldprocprefix;
  456. aktlocalswitches:=oldaktlocalswitches;
  457. aktmoduleswitches:=oldaktmoduleswitches;
  458. aktpackrecords:=oldaktpackrecords;
  459. aktpackenum:=oldaktpackenum;
  460. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  461. aktoutputformat:=oldaktoutputformat;
  462. aktoptprocessor:=oldaktoptprocessor;
  463. aktasmmode:=oldaktasmmode;
  464. aktfilepos:=oldaktfilepos;
  465. aktmodeswitches:=oldaktmodeswitches;
  466. end;
  467. { Shut down things when the last file is compiled }
  468. if (compile_level=1) then
  469. begin
  470. { Close script }
  471. if (not AsmRes.Empty) then
  472. begin
  473. Message1(exec_i_closing_script,AsmRes.Fn);
  474. AsmRes.WriteToDisk;
  475. end;
  476. {$ifdef USEEXCEPT}
  477. if not longjump_used then
  478. {$endif USEEXCEPT}
  479. { do not create browsers on errors !! }
  480. if status.errorcount=0 then
  481. begin
  482. {$ifdef BrowserLog}
  483. { Write Browser Log }
  484. if (cs_browser_log in aktglobalswitches) and
  485. (cs_browser in aktmoduleswitches) then
  486. begin
  487. if browserlog.elements_to_list^.empty then
  488. begin
  489. Message1(parser_i_writing_browser_log,browserlog.Fname);
  490. WriteBrowserLog;
  491. end
  492. else
  493. browserlog.list_elements;
  494. end;
  495. {$endif BrowserLog}
  496. {$ifdef BrowserCol}
  497. { Write Browser Collections }
  498. CreateBrowserCol;
  499. {$endif}
  500. end;
  501. if current_module^.in_second_compile then
  502. begin
  503. current_module^.in_second_compile:=false;
  504. current_module^.in_compile:=true;
  505. end
  506. else
  507. current_module^.in_compile:=false;
  508. (* Obsolete code aktprocsym
  509. is disposed by the localsymtable disposal (PM)
  510. { Free last aktprocsym }
  511. if assigned(aktprocsym) and (aktprocsym^.owner=nil) then
  512. begin
  513. { init parts are not needed in units !! }
  514. if current_module^.is_unit then
  515. aktprocsym^.definition^.forwarddef:=false;
  516. dispose(aktprocsym,done);
  517. end; *)
  518. end;
  519. dec(compile_level);
  520. parser_current_file:=prev_name^;
  521. stringdispose(prev_name);
  522. compiled_module:=old_compiled_module;
  523. {$ifdef USEEXCEPT}
  524. if longjump_used then
  525. longjmp(recoverpospointer^,1);
  526. {$endif USEEXCEPT}
  527. end;
  528. end.
  529. {
  530. $Log$
  531. Revision 1.96 2000-01-07 01:14:28 peter
  532. * updated copyright to 2000
  533. Revision 1.95 2000/01/04 15:15:52 florian
  534. + added compiler switch $maxfpuregisters
  535. + fixed a small problem in secondvecn
  536. Revision 1.94 1999/12/02 17:34:34 peter
  537. * preprocessor support. But it fails on the caret in type blocks
  538. Revision 1.93 1999/11/24 11:41:03 pierre
  539. * defaultsymtablestack is now restored after parser.compile
  540. Revision 1.92 1999/11/18 15:34:46 pierre
  541. * Notes/Hints for local syms changed to
  542. Set_varstate function
  543. Revision 1.91 1999/11/09 23:48:47 pierre
  544. * some DBX work, still does not work
  545. Revision 1.90 1999/11/06 14:34:21 peter
  546. * truncated log to 20 revs
  547. Revision 1.89 1999/10/22 10:39:34 peter
  548. * split type reading from pdecl to ptype unit
  549. * parameter_dec routine is now used for procedure and procvars
  550. Revision 1.88 1999/10/12 21:20:45 florian
  551. * new codegenerator compiles again
  552. Revision 1.87 1999/10/03 19:44:41 peter
  553. * removed objpasunit reference, tvarrec is now searched in systemunit
  554. where it already was located
  555. Revision 1.86 1999/10/01 08:02:45 peter
  556. * forward type declaration rewritten
  557. Revision 1.85 1999/09/16 08:02:39 pierre
  558. + old_compiled_module to avoid wrong file info when load PPU files
  559. Revision 1.84 1999/09/15 22:09:23 florian
  560. + rtti is now automatically generated for published classes, i.e.
  561. they are handled like an implicit property
  562. Revision 1.83 1999/08/31 15:51:11 pierre
  563. * in_second_compile cleaned up, in_compile and in_second_load added
  564. Revision 1.82 1999/08/26 20:24:41 michael
  565. + Hopefuly last fixes for resourcestrings
  566. Revision 1.81 1999/08/04 13:02:48 jonas
  567. * all tokens now start with an underscore
  568. * PowerPC compiles!!
  569. Revision 1.80 1999/08/03 17:09:37 florian
  570. * the alpha compiler can be compiled now
  571. Revision 1.79 1999/08/01 23:36:40 florian
  572. * some changes to compile the new code generator
  573. Revision 1.78 1999/07/24 16:22:18 michael
  574. + Improved resourcestring handling
  575. Revision 1.77 1999/07/23 16:05:22 peter
  576. * alignment is now saved in the symtable
  577. * C alignment added for records
  578. * PPU version increased to solve .12 <-> .13 probs
  579. Revision 1.76 1999/07/22 09:37:49 florian
  580. + resourcestring implemented
  581. + start of longstring support
  582. Revision 1.75 1999/06/15 13:23:48 pierre
  583. * don't generate browser if errors during compilation
  584. Revision 1.74 1999/05/27 19:44:41 peter
  585. * removed oldasm
  586. * plabel -> pasmlabel
  587. * -a switches to source writing automaticly
  588. * assembler readers OOPed
  589. * asmsymbol automaticly external
  590. * jumptables and other label fixes for asm readers
  591. Revision 1.73 1999/05/18 22:35:52 pierre
  592. * double dispose of aktprocsym removed
  593. Revision 1.72 1999/04/26 13:31:36 peter
  594. * release storenumber,double_checksum
  595. Revision 1.71 1999/03/26 00:05:33 peter
  596. * released valintern
  597. + deffile is now removed when compiling is finished
  598. * ^( compiles now correct
  599. + static directive
  600. * shrd fixed
  601. Revision 1.70 1999/03/24 23:17:10 peter
  602. * fixed bugs 212,222,225,227,229,231,233
  603. }