parser.pas 17 KB

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