parser.pas 18 KB

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