parser.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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. interface
  23. procedure compile(const filename:string;compile_system:boolean);
  24. procedure initparser;
  25. implementation
  26. uses
  27. cobjects,verbose,comphook,systems,globals,
  28. symtable,files,aasm,hcodegen,
  29. assemble,link,script,gendef,
  30. {$ifdef UseBrowser}
  31. browser,
  32. {$endif UseBrowser}
  33. scanner,pbase,pdecl,psystem,pmodules;
  34. procedure initparser;
  35. begin
  36. forwardsallowed:=false;
  37. { ^M means a string or a char, because we don't parse a }
  38. { type declaration }
  39. ignore_equal:=false;
  40. { we didn't parse a object or class declaration }
  41. { and no function header }
  42. testcurobject:=0;
  43. { a long time, this was forgotten }
  44. aktprocsym:=nil;
  45. current_module:=nil;
  46. loaded_units.init;
  47. usedunits.init;
  48. { global switches }
  49. aktglobalswitches:=initglobalswitches;
  50. { memory sizes }
  51. if heapsize=0 then
  52. heapsize:=target_info.heapsize;
  53. if stacksize=0 then
  54. stacksize:=target_info.stacksize;
  55. end;
  56. procedure default_macros;
  57. var
  58. hp : pstring_item;
  59. begin
  60. { commandline }
  61. hp:=pstring_item(initdefines.first);
  62. while assigned(hp) do
  63. begin
  64. def_macro(hp^.str^);
  65. hp:=pstring_item(hp^.next);
  66. end;
  67. { set macros for version checking }
  68. set_macro('FPC_VERSION',version_nr);
  69. set_macro('FPC_RELEASE',release_nr);
  70. set_macro('FPC_PATCH',patch_nr);
  71. end;
  72. procedure compile(const filename:string;compile_system:boolean);
  73. var
  74. { scanner }
  75. oldtoken : ttoken;
  76. oldtokenpos : tfileposinfo;
  77. oldc : char;
  78. oldpattern,
  79. oldorgpattern : string;
  80. old_block_type : tblock_type;
  81. oldcurrent_scanner : pscannerfile;
  82. { symtable }
  83. oldmacros,
  84. oldrefsymtable,
  85. oldsymtablestack : psymtable;
  86. oldprocprefix : string;
  87. oldaktprocsym : pprocsym;
  88. { cg }
  89. oldnextlabelnr : longint;
  90. oldparse_only : boolean;
  91. { asmlists }
  92. oldimports,
  93. oldexports,
  94. oldresource,
  95. oldrttilist,
  96. oldbsssegment,
  97. olddatasegment,
  98. oldcodesegment,
  99. oldexprasmlist,
  100. olddebuglist,
  101. oldinternals,
  102. oldexternals,
  103. oldconsts : paasmoutput;
  104. { akt.. things }
  105. oldaktlocalswitches : tlocalswitches;
  106. oldaktmoduleswitches : tmoduleswitches;
  107. oldaktfilepos : tfileposinfo;
  108. oldaktpackrecords : word;
  109. oldaktoutputformat : tasm;
  110. oldaktoptprocessor : tprocessors;
  111. oldaktasmmode : tasmmode;
  112. label
  113. done;
  114. begin
  115. inc(compile_level);
  116. { save symtable state }
  117. oldsymtablestack:=symtablestack;
  118. oldrefsymtable:=refsymtable;
  119. oldmacros:=macros;
  120. oldprocprefix:=procprefix;
  121. oldaktprocsym:=aktprocsym;
  122. { save scanner state }
  123. oldc:=c;
  124. oldpattern:=pattern;
  125. oldorgpattern:=orgpattern;
  126. oldtoken:=token;
  127. old_block_type:=block_type;
  128. oldtokenpos:=tokenpos;
  129. oldcurrent_scanner:=current_scanner;
  130. { save cg }
  131. oldnextlabelnr:=nextlabelnr;
  132. oldparse_only:=parse_only;
  133. { save assembler lists }
  134. olddatasegment:=datasegment;
  135. oldbsssegment:=bsssegment;
  136. oldcodesegment:=codesegment;
  137. olddebuglist:=debuglist;
  138. oldexternals:=externals;
  139. oldinternals:=internals;
  140. oldconsts:=consts;
  141. oldrttilist:=rttilist;
  142. oldexprasmlist:=exprasmlist;
  143. oldimports:=importssection;
  144. oldexports:=exportssection;
  145. oldresource:=resourcesection;
  146. { save akt... state }
  147. oldaktlocalswitches:=aktlocalswitches;
  148. oldaktmoduleswitches:=aktmoduleswitches;
  149. oldaktpackrecords:=aktpackrecords;
  150. oldaktoutputformat:=aktoutputformat;
  151. oldaktoptprocessor:=aktoptprocessor;
  152. oldaktasmmode:=aktasmmode;
  153. oldaktfilepos:=aktfilepos;
  154. { show info }
  155. Message1(parser_i_compiling,filename);
  156. { reset symtable }
  157. symtablestack:=nil;
  158. refsymtable:=nil;
  159. procprefix:='';
  160. aktprocsym:=nil;
  161. { macros }
  162. macros:=new(psymtable,init(macrosymtable));
  163. macros^.name:=stringdup('Conditionals for '+filename);
  164. default_macros;
  165. { reset the unit or create a new program }
  166. if assigned(current_module) then
  167. begin
  168. current_module^.sourcefiles.done;
  169. current_module^.sourcefiles.init;
  170. current_module^.used_units.done;
  171. current_module^.used_units.init;
  172. current_module^.linkofiles.done;
  173. current_module^.linkofiles.init;
  174. current_module^.linkstaticlibs.done;
  175. current_module^.linkstaticlibs.init;
  176. current_module^.linksharedlibs.done;
  177. current_module^.linksharedlibs.init;
  178. end
  179. else
  180. begin
  181. current_module:=new(pmodule,init(filename,false));
  182. main_module:=current_module;
  183. end;
  184. { Load current state from the init values }
  185. aktlocalswitches:=initlocalswitches;
  186. aktmoduleswitches:=initmoduleswitches;
  187. aktpackrecords:=initpackrecords;
  188. aktoutputformat:=initoutputformat;
  189. aktoptprocessor:=initoptprocessor;
  190. aktasmmode:=initasmmode;
  191. { we need this to make the system unit }
  192. if compile_system then
  193. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  194. { startup scanner }
  195. current_scanner:=new(pscannerfile,Init(filename));
  196. token:=current_scanner^.yylex;
  197. { global switches are read, so further changes aren't allowed }
  198. current_module^.in_main:=true;
  199. { init code generator for a new module }
  200. codegen_newmodule;
  201. {$ifdef GDB}
  202. reset_gdb_info;
  203. {$endif GDB}
  204. { Handle things which need to be once }
  205. if (compile_level=1) then
  206. begin
  207. { open assembler response }
  208. AsmRes.Init('ppas');
  209. end;
  210. { load system unit always }
  211. loadsystemunit;
  212. registerdef:=true;
  213. make_ref:=true;
  214. { current return type is void }
  215. procinfo.retdef:=voiddef;
  216. { reset lexical level }
  217. lexlevel:=0;
  218. { parse source }
  219. if (token=_UNIT) or (compile_level>1) then
  220. begin
  221. current_module^.is_unit:=true;
  222. { If the compile level > 1 we get a nice "unit expected" error
  223. message if we are trying to use a program as unit.}
  224. proc_unit;
  225. if current_module^.compiled then
  226. goto done;
  227. end
  228. else
  229. begin
  230. proc_program(token=_LIBRARY);
  231. end;
  232. if status.errorcount=0 then
  233. begin
  234. GenerateAsm(filename);
  235. if (cs_smartlink in aktmoduleswitches) then
  236. Linker.MakeStaticLibrary(SmartLinkPath(FileName),SmartLinkFilesCnt);
  237. { add the files for the linker from current_module, this must be
  238. after the makestaticlibrary, because it will add the library
  239. name (PFV) }
  240. Linker.AddModuleFiles(current_module);
  241. { Check linking => we are at first level in compile }
  242. if (compile_level=1) then
  243. begin
  244. if (cs_link_deffile in aktglobalswitches) then
  245. deffile.writefile;
  246. if (not current_module^.is_unit) then
  247. Linker.MakeExecutable;
  248. end;
  249. end
  250. else
  251. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  252. done:
  253. { clear memory }
  254. {$ifdef Splitheap}
  255. if testsplit then
  256. begin
  257. { temp heap should be empty after that !!!}
  258. codegen_donemodule;
  259. Releasetempheap;
  260. end;
  261. {$endif Splitheap}
  262. { restore old state, close trees }
  263. {$ifdef VER0_99_5}
  264. if dispose_asm_lists then
  265. {$else}
  266. {$ifndef go32v2}
  267. {$ifndef linux}
  268. if dispose_asm_lists then
  269. {$endif}
  270. {$endif}
  271. {$endif}
  272. codegen_donemodule;
  273. {$ifdef GDB}
  274. reset_gdb_info;
  275. {$endif GDB}
  276. { free ppu }
  277. if assigned(current_module^.ppufile) then
  278. begin
  279. dispose(current_module^.ppufile,done);
  280. current_module^.ppufile:=nil;
  281. end;
  282. { free scanner }
  283. dispose(current_scanner,done);
  284. { free macros }
  285. {!!! No check for unused macros yet !!! }
  286. dispose(macros,done);
  287. { restore scanner }
  288. c:=oldc;
  289. pattern:=oldpattern;
  290. orgpattern:=oldorgpattern;
  291. token:=oldtoken;
  292. tokenpos:=oldtokenpos;
  293. block_type:=old_block_type;
  294. current_scanner:=oldcurrent_scanner;
  295. { restore cg }
  296. nextlabelnr:=oldnextlabelnr;
  297. parse_only:=oldparse_only;
  298. { restore asmlists }
  299. exprasmlist:=oldexprasmlist;
  300. datasegment:=olddatasegment;
  301. bsssegment:=oldbsssegment;
  302. codesegment:=oldcodesegment;
  303. consts:=oldconsts;
  304. debuglist:=olddebuglist;
  305. externals:=oldexternals;
  306. internals:=oldinternals;
  307. importssection:=oldimports;
  308. exportssection:=oldexports;
  309. resourcesection:=oldresource;
  310. rttilist:=oldrttilist;
  311. { restore symtable state }
  312. if (compile_level>1) then
  313. begin
  314. refsymtable:=oldrefsymtable;
  315. symtablestack:=oldsymtablestack;
  316. end;
  317. macros:=oldmacros;
  318. aktprocsym:=oldaktprocsym;
  319. procprefix:=oldprocprefix;
  320. { restore current state }
  321. aktlocalswitches:=oldaktlocalswitches;
  322. aktmoduleswitches:=oldaktmoduleswitches;
  323. aktpackrecords:=oldaktpackrecords;
  324. aktoutputformat:=oldaktoutputformat;
  325. aktoptprocessor:=oldaktoptprocessor;
  326. aktasmmode:=oldaktasmmode;
  327. aktfilepos:=oldaktfilepos;
  328. { Shut down things when the last file is compiled }
  329. if (compile_level=1) then
  330. begin
  331. { Close script }
  332. if (not AsmRes.Empty) then
  333. begin
  334. Message1(exec_i_closing_script,AsmRes.Fn);
  335. AsmRes.WriteToDisk;
  336. end;
  337. {$ifdef UseBrowser}
  338. { Write Browser }
  339. if cs_browser in initswitches then
  340. begin
  341. Message1(parser_i_writing_browser_log,Browse.Fname);
  342. Browse.CreateLog;
  343. write_browser_log;
  344. Browse.CloseLog;
  345. end;
  346. {$endif UseBrowser}
  347. end;
  348. dec(compile_level);
  349. end;
  350. end.
  351. {
  352. $Log$
  353. Revision 1.36 1998-08-14 21:56:36 peter
  354. * setting the outputfile using -o works now to create static libs
  355. Revision 1.35 1998/08/12 19:22:09 peter
  356. * reset also the link* lists when recompiling an existing unit
  357. Revision 1.34 1998/08/10 23:58:56 peter
  358. * fixed asmlist dispose for 0.99.5
  359. Revision 1.33 1998/08/10 14:50:07 peter
  360. + localswitches, moduleswitches, globalswitches splitting
  361. Revision 1.32 1998/08/10 10:18:28 peter
  362. + Compiler,Comphook unit which are the new interface units to the
  363. compiler
  364. Revision 1.31 1998/07/14 21:46:46 peter
  365. * updated messages file
  366. Revision 1.30 1998/07/14 14:46:49 peter
  367. * released NEWINPUT
  368. Revision 1.29 1998/07/07 11:19:59 peter
  369. + NEWINPUT for a better inputfile and scanner object
  370. Revision 1.28 1998/06/25 11:15:33 pierre
  371. * ppu files where not closed in newppu !!
  372. second compilation was impossible due to too many opened files
  373. (not visible in 'make cycle' as we remove all the ppu files)
  374. Revision 1.27 1998/06/17 14:10:15 peter
  375. * small os2 fixes
  376. * fixed interdependent units with newppu (remake3 under linux works now)
  377. Revision 1.26 1998/06/16 08:56:23 peter
  378. + targetcpu
  379. * cleaner pmodules for newppu
  380. Revision 1.25 1998/06/15 15:38:07 pierre
  381. * small bug in systems.pas corrected
  382. + operators in different units better hanlded
  383. Revision 1.24 1998/06/13 00:10:08 peter
  384. * working browser and newppu
  385. * some small fixes against crashes which occured in bp7 (but not in
  386. fpc?!)
  387. Revision 1.23 1998/06/08 22:59:48 peter
  388. * smartlinking works for win32
  389. * some defines to exclude some compiler parts
  390. Revision 1.22 1998/06/05 17:47:28 peter
  391. * some better uses clauses
  392. Revision 1.21 1998/06/04 23:51:49 peter
  393. * m68k compiles
  394. + .def file creation moved to gendef.pas so it could also be used
  395. for win32
  396. Revision 1.20 1998/06/03 22:48:55 peter
  397. + wordbool,longbool
  398. * rename bis,von -> high,low
  399. * moved some systemunit loading/creating to psystem.pas
  400. Revision 1.19 1998/05/27 19:45:04 peter
  401. * symtable.pas splitted into includefiles
  402. * symtable adapted for $ifdef NEWPPU
  403. Revision 1.18 1998/05/23 01:21:15 peter
  404. + aktasmmode, aktoptprocessor, aktoutputformat
  405. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  406. + $LIBNAME to set the library name where the unit will be put in
  407. * splitted cgi386 a bit (codeseg to large for bp7)
  408. * nasm, tasm works again. nasm moved to ag386nsm.pas
  409. Revision 1.17 1998/05/20 09:42:34 pierre
  410. + UseTokenInfo now default
  411. * unit in interface uses and implementation uses gives error now
  412. * only one error for unknown symbol (uses lastsymknown boolean)
  413. the problem came from the label code !
  414. + first inlined procedures and function work
  415. (warning there might be allowed cases were the result is still wrong !!)
  416. * UseBrower updated gives a global list of all position of all used symbols
  417. with switch -gb
  418. Revision 1.16 1998/05/12 10:47:00 peter
  419. * moved printstatus to verb_def
  420. + V_Normal which is between V_Error and V_Warning and doesn't have a
  421. prefix like error: warning: and is included in V_Default
  422. * fixed some messages
  423. * first time parameter scan is only for -v and -T
  424. - removed old style messages
  425. Revision 1.15 1998/05/11 13:07:54 peter
  426. + $ifdef NEWPPU for the new ppuformat
  427. + $define GDB not longer required
  428. * removed all warnings and stripped some log comments
  429. * no findfirst/findnext anymore to remove smartlink *.o files
  430. Revision 1.14 1998/05/06 18:36:53 peter
  431. * tai_section extended with code,data,bss sections and enumerated type
  432. * ident 'compiled by FPC' moved to pmodules
  433. * small fix for smartlink
  434. Revision 1.13 1998/05/06 08:38:42 pierre
  435. * better position info with UseTokenInfo
  436. UseTokenInfo greatly simplified
  437. + added check for changed tree after first time firstpass
  438. (if we could remove all the cases were it happen
  439. we could skip all firstpass if firstpasscount > 1)
  440. Only with ExtDebug
  441. Revision 1.12 1998/05/04 17:54:28 peter
  442. + smartlinking works (only case jumptable left todo)
  443. * redesign of systems.pas to support assemblers and linkers
  444. + Unitname is now also in the PPU-file, increased version to 14
  445. Revision 1.11 1998/05/01 16:38:45 florian
  446. * handling of private and protected fixed
  447. + change_keywords_to_tp implemented to remove
  448. keywords which aren't supported by tp
  449. * break and continue are now symbols of the system unit
  450. + widestring, longstring and ansistring type released
  451. Revision 1.10 1998/05/01 07:43:56 florian
  452. + basics for rtti implemented
  453. + switch $m (generate rtti for published sections)
  454. Revision 1.9 1998/04/30 15:59:40 pierre
  455. * GDB works again better :
  456. correct type info in one pass
  457. + UseTokenInfo for better source position
  458. * fixed one remaining bug in scanner for line counts
  459. * several little fixes
  460. Revision 1.8 1998/04/29 10:33:55 pierre
  461. + added some code for ansistring (not complete nor working yet)
  462. * corrected operator overloading
  463. * corrected nasm output
  464. + started inline procedures
  465. + added starstarn : use ** for exponentiation (^ gave problems)
  466. + started UseTokenInfo cond to get accurate positions
  467. Revision 1.7 1998/04/27 23:10:28 peter
  468. + new scanner
  469. * $makelib -> if smartlink
  470. * small filename fixes pmodule.setfilename
  471. * moved import from files.pas -> import.pas
  472. Revision 1.6 1998/04/21 10:16:48 peter
  473. * patches from strasbourg
  474. * objects is not used anymore in the fpc compiled version
  475. Revision 1.5 1998/04/10 14:41:43 peter
  476. * removed some Hints
  477. * small speed optimization for AsmLn
  478. Revision 1.4 1998/04/08 16:58:03 pierre
  479. * several bugfixes
  480. ADD ADC and AND are also sign extended
  481. nasm output OK (program still crashes at end
  482. and creates wrong assembler files !!)
  483. procsym types sym in tdef removed !!
  484. Revision 1.3 1998/04/07 22:45:04 florian
  485. * bug0092, bug0115 and bug0121 fixed
  486. + packed object/class/array
  487. }