parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. { list of generated .o files, so the linker can remove them }
  97. SmartLinkOFiles.init;
  98. end;
  99. procedure doneparser;
  100. begin
  101. { unload units }
  102. loaded_units.done;
  103. usedunits.done;
  104. { close ppas,deffile }
  105. asmres.done;
  106. deffile.done;
  107. { free list of .o files }
  108. SmartLinkOFiles.done;
  109. end;
  110. procedure default_macros;
  111. var
  112. hp : pstring_item;
  113. begin
  114. { commandline }
  115. hp:=pstring_item(initdefines.first);
  116. while assigned(hp) do
  117. begin
  118. def_macro(hp^.str^);
  119. hp:=pstring_item(hp^.next);
  120. end;
  121. { set macros for version checking }
  122. set_macro('FPC_VERSION',version_nr);
  123. set_macro('FPC_RELEASE',release_nr);
  124. set_macro('FPC_PATCH',patch_nr);
  125. end;
  126. procedure preprocess(const filename:string);
  127. var
  128. i : longint;
  129. begin
  130. new(preprocfile,init('pre'));
  131. { default macros }
  132. macros:=new(psymtable,init(macrosymtable));
  133. macros^.name:=stringdup('Conditionals for '+filename);
  134. default_macros;
  135. { initialize a module }
  136. current_module:=new(pmodule,init(filename,false));
  137. main_module:=current_module;
  138. { startup scanner, and save in current_module }
  139. current_scanner:=new(pscannerfile,Init(filename));
  140. current_module^.scanner:=current_scanner;
  141. { loop until EOF is found }
  142. repeat
  143. current_scanner^.readtoken;
  144. preprocfile^.AddSpace;
  145. case token of
  146. _ID :
  147. begin
  148. preprocfile^.Add(orgpattern);
  149. end;
  150. _REALNUMBER,
  151. _INTCONST :
  152. preprocfile^.Add(pattern);
  153. _CSTRING :
  154. begin
  155. i:=0;
  156. while (i<length(pattern)) do
  157. begin
  158. inc(i);
  159. if pattern[i]='''' then
  160. begin
  161. insert('''',pattern,i);
  162. inc(i);
  163. end;
  164. end;
  165. preprocfile^.Add(''''+pattern+'''');
  166. end;
  167. _CCHAR :
  168. begin
  169. case pattern[1] of
  170. #39 :
  171. pattern:='''''''';
  172. #0..#31,
  173. #128..#255 :
  174. begin
  175. str(ord(pattern[1]),pattern);
  176. pattern:='#'+pattern;
  177. end;
  178. else
  179. pattern:=''''+pattern[1]+'''';
  180. end;
  181. preprocfile^.Add(pattern);
  182. end;
  183. _EOF :
  184. break;
  185. else
  186. preprocfile^.Add(tokeninfo^[token].str)
  187. end;
  188. until false;
  189. { free scanner }
  190. dispose(current_scanner,done);
  191. { close }
  192. dispose(preprocfile,done);
  193. end;
  194. procedure compile(const filename:string;compile_system:boolean);
  195. var
  196. { scanner }
  197. oldidtoken,
  198. oldtoken : ttoken;
  199. oldtokenpos : tfileposinfo;
  200. oldc : char;
  201. oldpattern,
  202. oldorgpattern : string;
  203. old_block_type : tblock_type;
  204. oldcurrent_scanner,prev_scanner,
  205. scanner : pscannerfile;
  206. { symtable }
  207. oldmacros,
  208. oldrefsymtable,
  209. olddefaultsymtablestack,
  210. oldsymtablestack : psymtable;
  211. oldprocprefix : string;
  212. oldaktprocsym : pprocsym;
  213. { cg }
  214. oldnextlabelnr : longint;
  215. oldparse_only : boolean;
  216. { asmlists }
  217. oldimports,
  218. oldexports,
  219. oldresource,
  220. oldrttilist,
  221. oldresourcestringlist,
  222. oldbsssegment,
  223. olddatasegment,
  224. oldcodesegment,
  225. oldexprasmlist,
  226. olddebuglist,
  227. oldconsts : paasmoutput;
  228. oldasmsymbollist : pasmsymbollist;
  229. { akt.. things }
  230. oldaktlocalswitches : tlocalswitches;
  231. oldaktmoduleswitches : tmoduleswitches;
  232. oldaktfilepos : tfileposinfo;
  233. oldaktpackenum,oldaktmaxfpuregisters : longint;
  234. oldaktpackrecords : tpackrecords;
  235. oldaktoutputformat : tasm;
  236. oldaktspecificoptprocessor,
  237. oldaktoptprocessor : tprocessors;
  238. oldaktasmmode : tasmmode;
  239. oldaktmodeswitches : tmodeswitches;
  240. old_compiled_module : pmodule;
  241. prev_name : pstring;
  242. {$ifdef USEEXCEPT}
  243. recoverpos : jmp_buf;
  244. oldrecoverpos : pjmp_buf;
  245. {$endif useexcept}
  246. {$ifdef newcg}
  247. oldcg : pcg;
  248. {$endif newcg}
  249. {$ifdef GDB}
  250. store_dbx : plongint;
  251. {$endif GDB}
  252. begin
  253. inc(compile_level);
  254. prev_name:=stringdup(parser_current_file);
  255. parser_current_file:=filename;
  256. old_compiled_module:=compiled_module;
  257. { save symtable state }
  258. oldsymtablestack:=symtablestack;
  259. olddefaultsymtablestack:=defaultsymtablestack;
  260. oldrefsymtable:=refsymtable;
  261. oldmacros:=macros;
  262. oldprocprefix:=procprefix;
  263. oldaktprocsym:=aktprocsym;
  264. { save scanner state }
  265. oldc:=c;
  266. oldpattern:=pattern;
  267. oldorgpattern:=orgpattern;
  268. oldtoken:=token;
  269. oldidtoken:=idtoken;
  270. old_block_type:=block_type;
  271. oldtokenpos:=tokenpos;
  272. oldcurrent_scanner:=current_scanner;
  273. { save cg }
  274. oldnextlabelnr:=nextlabelnr;
  275. oldparse_only:=parse_only;
  276. { save assembler lists }
  277. olddatasegment:=datasegment;
  278. oldbsssegment:=bsssegment;
  279. oldcodesegment:=codesegment;
  280. olddebuglist:=debuglist;
  281. oldconsts:=consts;
  282. oldrttilist:=rttilist;
  283. oldexprasmlist:=exprasmlist;
  284. oldimports:=importssection;
  285. oldexports:=exportssection;
  286. oldresource:=resourcesection;
  287. oldresourcestringlist:=resourcestringlist;
  288. oldasmsymbollist:=asmsymbollist;
  289. { save akt... state }
  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. oldaktfilepos:=aktfilepos;
  300. oldaktmodeswitches:=aktmodeswitches;
  301. {$ifdef newcg}
  302. oldcg:=cg;
  303. {$endif newcg}
  304. {$ifdef GDB}
  305. store_dbx:=dbx_counter;
  306. dbx_counter:=nil;
  307. {$endif GDB}
  308. { show info }
  309. Message1(parser_i_compiling,filename);
  310. { reset symtable }
  311. symtablestack:=nil;
  312. defaultsymtablestack:=nil;
  313. systemunit:=nil;
  314. refsymtable:=nil;
  315. aktprocsym:=nil;
  316. procprefix:='';
  317. registerdef:=true;
  318. aktmaxfpuregisters:=-1;
  319. { macros }
  320. macros:=new(psymtable,init(macrosymtable));
  321. macros^.name:=stringdup('Conditionals for '+filename);
  322. default_macros;
  323. { reset the unit or create a new program }
  324. if assigned(current_module) then
  325. begin
  326. {current_module^.reset this is wrong !! }
  327. scanner:=current_module^.scanner;
  328. current_module^.reset;
  329. current_module^.scanner:=scanner;
  330. end
  331. else
  332. begin
  333. current_module:=new(pmodule,init(filename,false));
  334. main_module:=current_module;
  335. end;
  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. { we need this to make the system unit }
  352. if compile_system then
  353. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  354. { startup scanner, and save in current_module }
  355. current_scanner:=new(pscannerfile,Init(filename));
  356. current_scanner^.readtoken;
  357. prev_scanner:=current_module^.scanner;
  358. current_module^.scanner:=current_scanner;
  359. { init code generator for a new module }
  360. codegen_newmodule;
  361. {$ifdef newcg}
  362. {$ifdef i386}
  363. cg:=new(pcg386,init);
  364. {$endif i386}
  365. {$ifdef alpha}
  366. cg:=new(pcgalpha,init);
  367. {$endif alpha}
  368. {$ifdef powerpc}
  369. cg:=new(pcgppc,init);
  370. {$endif powerpc}
  371. {$endif newcg}
  372. { If the compile level > 1 we get a nice "unit expected" error
  373. message if we are trying to use a program as unit.}
  374. {$ifdef USEEXCEPT}
  375. if setjmp(recoverpos)=0 then
  376. begin
  377. oldrecoverpos:=recoverpospointer;
  378. recoverpospointer:=@recoverpos;
  379. {$endif USEEXCEPT}
  380. if (token=_UNIT) or (compile_level>1) then
  381. begin
  382. current_module^.is_unit:=true;
  383. proc_unit;
  384. end
  385. else
  386. proc_program(token=_LIBRARY);
  387. {$ifdef USEEXCEPT}
  388. recoverpospointer:=oldrecoverpos;
  389. end
  390. else
  391. begin
  392. recoverpospointer:=oldrecoverpos;
  393. longjump_used:=true;
  394. end;
  395. {$endif USEEXCEPT}
  396. { clear memory }
  397. {$ifdef Splitheap}
  398. if testsplit then
  399. begin
  400. { temp heap should be empty after that !!!}
  401. codegen_donemodule;
  402. Releasetempheap;
  403. end;
  404. {$endif Splitheap}
  405. { restore old state, close trees, > 0.99.5 has heapblocks, so
  406. it's the default to release the trees }
  407. codegen_donemodule;
  408. {$ifdef newcg}
  409. dispose(cg,done);
  410. {$endif newcg}
  411. { free ppu }
  412. if assigned(current_module^.ppufile) then
  413. begin
  414. dispose(current_module^.ppufile,done);
  415. current_module^.ppufile:=nil;
  416. end;
  417. { free scanner }
  418. dispose(current_scanner,done);
  419. { restore previous scanner !! }
  420. current_module^.scanner:=prev_scanner;
  421. if assigned(prev_scanner) then
  422. prev_scanner^.invalid:=true;
  423. { free macros }
  424. {!!! No check for unused macros yet !!! }
  425. dispose(macros,done);
  426. if (compile_level>1) then
  427. begin
  428. {$ifdef newcg}
  429. cg:=oldcg;
  430. {$endif newcg}
  431. {$ifdef GDB}
  432. dbx_counter:=store_dbx;
  433. {$endif GDB}
  434. { restore scanner }
  435. c:=oldc;
  436. pattern:=oldpattern;
  437. orgpattern:=oldorgpattern;
  438. token:=oldtoken;
  439. idtoken:=oldidtoken;
  440. tokenpos:=oldtokenpos;
  441. block_type:=old_block_type;
  442. current_scanner:=oldcurrent_scanner;
  443. { restore cg }
  444. nextlabelnr:=oldnextlabelnr;
  445. parse_only:=oldparse_only;
  446. { restore asmlists }
  447. exprasmlist:=oldexprasmlist;
  448. datasegment:=olddatasegment;
  449. bsssegment:=oldbsssegment;
  450. codesegment:=oldcodesegment;
  451. consts:=oldconsts;
  452. debuglist:=olddebuglist;
  453. importssection:=oldimports;
  454. exportssection:=oldexports;
  455. resourcesection:=oldresource;
  456. rttilist:=oldrttilist;
  457. resourcestringlist:=oldresourcestringlist;
  458. asmsymbollist:=oldasmsymbollist;
  459. { restore symtable state }
  460. refsymtable:=oldrefsymtable;
  461. symtablestack:=oldsymtablestack;
  462. defaultsymtablestack:=olddefaultsymtablestack;
  463. macros:=oldmacros;
  464. aktprocsym:=oldaktprocsym;
  465. procprefix:=oldprocprefix;
  466. aktlocalswitches:=oldaktlocalswitches;
  467. aktmoduleswitches:=oldaktmoduleswitches;
  468. aktpackrecords:=oldaktpackrecords;
  469. aktpackenum:=oldaktpackenum;
  470. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  471. aktoutputformat:=oldaktoutputformat;
  472. aktoptprocessor:=oldaktoptprocessor;
  473. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  474. aktasmmode:=oldaktasmmode;
  475. aktfilepos:=oldaktfilepos;
  476. aktmodeswitches:=oldaktmodeswitches;
  477. end;
  478. { Shut down things when the last file is compiled }
  479. if (compile_level=1) then
  480. begin
  481. { Close script }
  482. if (not AsmRes.Empty) then
  483. begin
  484. Message1(exec_i_closing_script,AsmRes.Fn);
  485. AsmRes.WriteToDisk;
  486. end;
  487. {$ifdef USEEXCEPT}
  488. if not longjump_used then
  489. {$endif USEEXCEPT}
  490. { do not create browsers on errors !! }
  491. if status.errorcount=0 then
  492. begin
  493. {$ifdef BrowserLog}
  494. { Write Browser Log }
  495. if (cs_browser_log in aktglobalswitches) and
  496. (cs_browser in aktmoduleswitches) then
  497. begin
  498. if browserlog.elements_to_list^.empty then
  499. begin
  500. Message1(parser_i_writing_browser_log,browserlog.Fname);
  501. WriteBrowserLog;
  502. end
  503. else
  504. browserlog.list_elements;
  505. end;
  506. {$endif BrowserLog}
  507. {$ifdef BrowserCol}
  508. { Write Browser Collections }
  509. CreateBrowserCol;
  510. {$endif}
  511. end;
  512. if current_module^.in_second_compile then
  513. begin
  514. current_module^.in_second_compile:=false;
  515. current_module^.in_compile:=true;
  516. end
  517. else
  518. current_module^.in_compile:=false;
  519. (* Obsolete code aktprocsym
  520. is disposed by the localsymtable disposal (PM)
  521. { Free last aktprocsym }
  522. if assigned(aktprocsym) and (aktprocsym^.owner=nil) then
  523. begin
  524. { init parts are not needed in units !! }
  525. if current_module^.is_unit then
  526. aktprocsym^.definition^.forwarddef:=false;
  527. dispose(aktprocsym,done);
  528. end; *)
  529. end;
  530. dec(compile_level);
  531. parser_current_file:=prev_name^;
  532. stringdispose(prev_name);
  533. compiled_module:=old_compiled_module;
  534. {$ifdef USEEXCEPT}
  535. if longjump_used then
  536. longjmp(recoverpospointer^,1);
  537. {$endif USEEXCEPT}
  538. end;
  539. end.
  540. {
  541. $Log$
  542. Revision 1.100 2000-02-14 20:58:44 marco
  543. * Basic structures for new sethandling implemented.
  544. Revision 1.99 2000/02/09 13:22:55 peter
  545. * log truncated
  546. Revision 1.98 2000/01/23 21:29:17 florian
  547. * CMOV support in optimizer (in define USECMOV)
  548. + start of support of exceptions in constructors
  549. Revision 1.97 2000/01/11 09:52:06 peter
  550. * fixed placing of .sl directories
  551. * use -b again for base-file selection
  552. * fixed group writing for linux with smartlinking
  553. Revision 1.96 2000/01/07 01:14:28 peter
  554. * updated copyright to 2000
  555. Revision 1.95 2000/01/04 15:15:52 florian
  556. + added compiler switch $maxfpuregisters
  557. + fixed a small problem in secondvecn
  558. Revision 1.94 1999/12/02 17:34:34 peter
  559. * preprocessor support. But it fails on the caret in type blocks
  560. Revision 1.93 1999/11/24 11:41:03 pierre
  561. * defaultsymtablestack is now restored after parser.compile
  562. Revision 1.92 1999/11/18 15:34:46 pierre
  563. * Notes/Hints for local syms changed to
  564. Set_varstate function
  565. Revision 1.91 1999/11/09 23:48:47 pierre
  566. * some DBX work, still does not work
  567. Revision 1.90 1999/11/06 14:34:21 peter
  568. * truncated log to 20 revs
  569. Revision 1.89 1999/10/22 10:39:34 peter
  570. * split type reading from pdecl to ptype unit
  571. * parameter_dec routine is now used for procedure and procvars
  572. Revision 1.88 1999/10/12 21:20:45 florian
  573. * new codegenerator compiles again
  574. Revision 1.87 1999/10/03 19:44:41 peter
  575. * removed objpasunit reference, tvarrec is now searched in systemunit
  576. where it already was located
  577. Revision 1.86 1999/10/01 08:02:45 peter
  578. * forward type declaration rewritten
  579. Revision 1.85 1999/09/16 08:02:39 pierre
  580. + old_compiled_module to avoid wrong file info when load PPU files
  581. Revision 1.84 1999/09/15 22:09:23 florian
  582. + rtti is now automatically generated for published classes, i.e.
  583. they are handled like an implicit property
  584. Revision 1.83 1999/08/31 15:51:11 pierre
  585. * in_second_compile cleaned up, in_compile and in_second_load added
  586. Revision 1.82 1999/08/26 20:24:41 michael
  587. + Hopefuly last fixes for resourcestrings
  588. Revision 1.81 1999/08/04 13:02:48 jonas
  589. * all tokens now start with an underscore
  590. * PowerPC compiles!!
  591. Revision 1.80 1999/08/03 17:09:37 florian
  592. * the alpha compiler can be compiled now
  593. Revision 1.79 1999/08/01 23:36:40 florian
  594. * some changes to compile the new code generator
  595. }