parser.pas 21 KB

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