parser.pas 17 KB

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