parser.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. dos,cobjects,globals,scanner,systems,symtable,tree,aasm,
  28. types,strings,pass_1,hcodegen,files,verbose,script,import
  29. {$ifdef i386}
  30. ,i386
  31. ,cgi386
  32. ,cgai386
  33. ,tgeni386
  34. ,aopt386
  35. {$endif i386}
  36. {$ifdef m68k}
  37. ,m68k
  38. ,cg68k
  39. ,tgen68k
  40. ,cga68k
  41. {$endif m68k}
  42. { parser units }
  43. ,pbase,pmodules,pdecl,
  44. { assembling & linking }
  45. assemble,
  46. link;
  47. { dummy variable for search when calling exec }
  48. var
  49. file_found : boolean;
  50. procedure readconstdefs;
  51. begin
  52. s32bitdef:=porddef(globaldef('longint'));
  53. u32bitdef:=porddef(globaldef('ulong'));
  54. cstringdef:=pstringdef(globaldef('string'));
  55. clongstringdef:=pstringdef(globaldef('longstring'));
  56. cansistringdef:=pstringdef(globaldef('ansistring'));
  57. cwidestringdef:=pstringdef(globaldef('widestring'));
  58. cchardef:=porddef(globaldef('char'));
  59. {$ifdef i386}
  60. c64floatdef:=pfloatdef(globaldef('s64real'));
  61. {$endif}
  62. {$ifdef m68k}
  63. c64floatdef:=pfloatdef(globaldef('s32real'));
  64. {$endif m68k}
  65. s80floatdef:=pfloatdef(globaldef('s80real'));
  66. s32fixeddef:=pfloatdef(globaldef('cs32fixed'));
  67. voiddef:=porddef(globaldef('void'));
  68. u8bitdef:=porddef(globaldef('byte'));
  69. u16bitdef:=porddef(globaldef('word'));
  70. booldef:=porddef(globaldef('boolean'));
  71. voidpointerdef:=ppointerdef(globaldef('void_pointer'));
  72. cfiledef:=pfiledef(globaldef('file'));
  73. end;
  74. procedure initparser;
  75. begin
  76. forwardsallowed:=false;
  77. { ^M means a string or a char, because we don't parse a }
  78. { type declaration }
  79. block_type:=bt_general;
  80. ignore_equal:=false;
  81. { we didn't parse a object or class declaration }
  82. { and no function header }
  83. testcurobject:=0;
  84. { create error defintion }
  85. generrordef:=new(perrordef,init);
  86. symtablestack:=nil;
  87. { a long time, this was forgotten }
  88. aktprocsym:=nil;
  89. current_module:=nil;
  90. loaded_units.init;
  91. usedunits.init;
  92. end;
  93. { moved out to save stack }
  94. var
  95. addparam : string;
  96. procedure compile(const filename:string;compile_system:boolean);
  97. var
  98. hp : pmodule;
  99. { some variables to save the compiler state }
  100. oldtoken : ttoken;
  101. {$ifdef UseTokenInfo}
  102. oldtokenpos : tfileposinfo;
  103. {$endif UseTokenInfo}
  104. oldpattern : stringid;
  105. oldpreprocstack : ppreprocstack;
  106. oldorgpattern,oldprocprefix : string;
  107. old_block_type : tblock_type;
  108. oldinputbuffer,
  109. oldinputpointer : pchar;
  110. olds_point,oldparse_only : boolean;
  111. oldc : char;
  112. oldcomment_level : word;
  113. oldimports,oldexports,oldresource,oldrttilist,
  114. oldbsssegment,olddatasegment,oldcodesegment,
  115. oldexprasmlist,olddebuglist,
  116. oldinternals,oldexternals,oldconsts : paasmoutput;
  117. oldnextlabelnr : longint;
  118. oldswitches : Tcswitches;
  119. oldmacros,oldrefsymtable,oldsymtablestack : psymtable;
  120. procedure def_macro(const s : string);
  121. var
  122. mac : pmacrosym;
  123. begin
  124. mac:=pmacrosym(macros^.search(s));
  125. if mac=nil then
  126. begin
  127. mac:=new(pmacrosym,init(s));
  128. Message1(parser_m_macro_defined,mac^.name);
  129. macros^.insert(mac);
  130. end;
  131. mac^.defined:=true;
  132. end;
  133. procedure set_macro(const s : string;value : string);
  134. var
  135. mac : pmacrosym;
  136. begin
  137. mac:=pmacrosym(macros^.search(s));
  138. if mac=nil then
  139. begin
  140. mac:=new(pmacrosym,init(s));
  141. macros^.insert(mac);
  142. end
  143. else
  144. begin
  145. if assigned(mac^.buftext) then
  146. freemem(mac^.buftext,mac^.buflen);
  147. end;
  148. Message2(parser_m_macro_set_to,mac^.name,value);
  149. mac^.buflen:=length(value);
  150. getmem(mac^.buftext,mac^.buflen);
  151. move(value[1],mac^.buftext^,mac^.buflen);
  152. mac^.defined:=true;
  153. end;
  154. procedure define_macros;
  155. var
  156. hp : pstring_item;
  157. begin
  158. hp:=pstring_item(commandlinedefines.first);
  159. while assigned(hp) do
  160. begin
  161. def_macro(hp^.str^);
  162. hp:=pstring_item(hp^.next);
  163. end;
  164. { set macros for version checking }
  165. set_macro('FPC_VERSION',version_nr);
  166. set_macro('FPC_RELEASE',release_nr);
  167. set_macro('FPC_PATCH',patch_nr);
  168. end;
  169. label
  170. done;
  171. begin {compile}
  172. inc(compile_level);
  173. { save old state }
  174. { save symtable state }
  175. oldsymtablestack:=symtablestack;
  176. symtablestack:=nil;
  177. oldrefsymtable:=refsymtable;
  178. refsymtable:=nil;
  179. oldprocprefix:=procprefix;
  180. { a long time, this was only in init_parser
  181. but it should be reset to zero for each module }
  182. aktprocsym:=nil;
  183. { first, we assume a program }
  184. if not(assigned(current_module)) then
  185. begin
  186. current_module:=new(pmodule,init(filename,false));
  187. main_module:=current_module;
  188. end;
  189. { save scanner state }
  190. oldmacros:=macros;
  191. oldpattern:=pattern;
  192. oldtoken:=token;
  193. {$ifdef UseTokenInfo}
  194. oldtokenpos:=tokenpos;
  195. {$endif UseTokenInfo}
  196. oldorgpattern:=orgpattern;
  197. old_block_type:=block_type;
  198. oldpreprocstack:=preprocstack;
  199. oldinputbuffer:=inputbuffer;
  200. oldinputpointer:=inputpointer;
  201. olds_point:=s_point;
  202. oldc:=c;
  203. oldcomment_level:=comment_level;
  204. oldparse_only:=parse_only;
  205. { save assembler lists }
  206. olddatasegment:=datasegment;
  207. oldbsssegment:=bsssegment;
  208. oldcodesegment:=codesegment;
  209. olddebuglist:=debuglist;
  210. oldexternals:=externals;
  211. oldinternals:=internals;
  212. oldconsts:=consts;
  213. oldrttilist:=rttilist;
  214. oldexprasmlist:=exprasmlist;
  215. oldimports:=importssection;
  216. oldexports:=exportssection;
  217. oldresource:=resourcesection;
  218. oldswitches:=aktswitches;
  219. oldnextlabelnr:=nextlabelnr;
  220. Message1(parser_i_compiling,filename);
  221. InitScanner(filename);
  222. aktswitches:=initswitches;
  223. { we need this to make the system unit }
  224. if compile_system then
  225. aktswitches:=aktswitches+[cs_compilesystem];
  226. aktpackrecords:=initpackrecords;
  227. { init code generator for a new module }
  228. codegen_newmodule;
  229. macros:=new(psymtable,init(macrosymtable));
  230. define_macros;
  231. { startup scanner }
  232. token:=yylex;
  233. reset_gdb_info;
  234. { init asm writing }
  235. datasegment:=new(paasmoutput,init);
  236. codesegment:=new(paasmoutput,init);
  237. bsssegment:=new(paasmoutput,init);
  238. debuglist:=new(paasmoutput,init);
  239. externals:=new(paasmoutput,init);
  240. internals:=new(paasmoutput,init);
  241. consts:=new(paasmoutput,init);
  242. rttilist:=new(paasmoutput,init);
  243. importssection:=nil;
  244. exportssection:=nil;
  245. resourcesection:=nil;
  246. { global switches are read, so further changes aren't allowed }
  247. current_module^.in_main:=true;
  248. { open assembler response }
  249. if (compile_level=1) then
  250. AsmRes.Init('ppas');
  251. { if the current file isn't a system unit }
  252. { the the system unit will be loaded }
  253. if not(cs_compilesystem in aktswitches) then
  254. begin
  255. { should be done in unit system (changing the field system_unit)
  256. FK
  257. }
  258. hp:=loadunit(upper(target_info.system_unit),true,true);
  259. systemunit:=hp^.symtable;
  260. readconstdefs;
  261. { we could try to overload caret by default }
  262. symtablestack:=systemunit;
  263. { if POWER is defined in the RTL then use it for starstar overloading }
  264. getsym('POWER',false);
  265. if assigned(srsym) and (srsym^.typ=procsym) and
  266. (overloaded_operators[STARSTAR]=nil) then
  267. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  268. end
  269. else
  270. begin
  271. { create definitions for constants }
  272. registerdef:=false;
  273. s32bitdef:=new(porddef,init(s32bit,$80000000,$7fffffff));
  274. u32bitdef:=new(porddef,init(u32bit,0,$ffffffff));
  275. cstringdef:=new(pstringdef,init(255));
  276. { should we give a length to the default long and ansi string definition ?? }
  277. clongstringdef:=new(pstringdef,longinit(-1));
  278. cansistringdef:=new(pstringdef,ansiinit(-1));
  279. cwidestringdef:=new(pstringdef,wideinit(-1));
  280. cchardef:=new(porddef,init(uchar,0,255));
  281. {$ifdef i386}
  282. c64floatdef:=new(pfloatdef,init(s64real));
  283. s80floatdef:=new(pfloatdef,init(s80real));
  284. {$endif}
  285. {$ifdef m68k}
  286. c64floatdef:=new(pfloatdef,init(s32real));
  287. if (cs_fp_emulation in aktswitches) then
  288. s80floatdef:=new(pfloatdef,init(s32real))
  289. else
  290. s80floatdef:=new(pfloatdef,init(s80real));
  291. {$endif}
  292. s32fixeddef:=new(pfloatdef,init(f32bit));
  293. { some other definitions }
  294. voiddef:=new(porddef,init(uvoid,0,0));
  295. u8bitdef:=new(porddef,init(u8bit,0,255));
  296. u16bitdef:=new(porddef,init(u16bit,0,65535));
  297. booldef:=new(porddef,init(bool8bit,0,1));
  298. voidpointerdef:=new(ppointerdef,init(voiddef));
  299. cfiledef:=new(pfiledef,init(ft_untyped,nil));
  300. systemunit:=nil;
  301. end;
  302. registerdef:=true;
  303. { current return type is void }
  304. procinfo.retdef:=voiddef;
  305. { reset lexical level }
  306. lexlevel:=0;
  307. { parse source }
  308. if (token=_UNIT) or (compile_level>1) then
  309. begin
  310. current_module^.is_unit:=true;
  311. { If the compile level > 1 we get a nice "unit expected" error
  312. message if we are trying to use a program as unit.}
  313. proc_unit;
  314. if current_module^.compiled then
  315. goto done;
  316. end
  317. else
  318. begin
  319. proc_program(token=_LIBRARY);
  320. end;
  321. if errorcount=0 then
  322. begin
  323. if current_module^.uses_imports then
  324. importlib^.generatelib;
  325. GenerateAsm(filename);
  326. if smartlink then
  327. begin
  328. Linker.SetLibName(FileName);
  329. Linker.MakeStaticLibrary(SmartLinkPath(FileName),SmartLinkFilesCnt);
  330. end;
  331. { add the files for the linker from current_module, this must be
  332. after the makestaticlibrary, because it will add the library
  333. name (PFV) }
  334. addlinkerfiles(current_module);
  335. { Check linking => we are at first level in compile }
  336. if (compile_level=1) and (not current_module^.is_unit) then
  337. begin
  338. if (cs_no_linking in initswitches) then
  339. externlink:=true;
  340. if Linker.ExeName='' then
  341. Linker.SetExeName(FileName);
  342. Linker.MakeExecutable;
  343. end;
  344. end
  345. else
  346. Message1(unit_f_errors_in_unit,tostr(errorcount));
  347. done:
  348. { clear memory }
  349. {$ifdef Splitheap}
  350. if testsplit then
  351. begin
  352. { temp heap should be empty after that !!!}
  353. codegen_donemodule;
  354. Releasetempheap;
  355. end;
  356. {$endif Splitheap}
  357. { restore old state, close trees }
  358. if dispose_asm_lists then
  359. codegen_donemodule;
  360. reset_gdb_info;
  361. { restore symtable state }
  362. {$ifdef UseBrowser}
  363. if (compile_level>1) then
  364. { we want to keep the current symtablestack }
  365. {$endif UseBrowser}
  366. begin
  367. refsymtable:=oldrefsymtable;
  368. symtablestack:=oldsymtablestack;
  369. end;
  370. procprefix:=oldprocprefix;
  371. { close the inputfiles }
  372. {$ifndef UseBrowser}
  373. { but not if we want the names for the browser ! }
  374. current_module^.sourcefiles.done;
  375. {$endif not UseBrowser}
  376. { restore scanner state }
  377. pattern:=oldpattern;
  378. token:=oldtoken;
  379. {$ifdef UseTokenInfo}
  380. tokenpos:=oldtokenpos;
  381. {$endif UseTokenInfo}
  382. orgpattern:=oldorgpattern;
  383. block_type:=old_block_type;
  384. { call donescanner before restoring preprocstack, because }
  385. { donescanner tests for a empty preprocstack }
  386. { and can also check for unused macros }
  387. donescanner(current_module^.compiled);
  388. dispose(macros,done);
  389. macros:=oldmacros;
  390. preprocstack:=oldpreprocstack;
  391. aktswitches:=oldswitches;
  392. inputbuffer:=oldinputbuffer;
  393. inputpointer:=oldinputpointer;
  394. s_point:=olds_point;
  395. c:=oldc;
  396. comment_level:=oldcomment_level;
  397. parse_only:=oldparse_only;
  398. { restore asmlists }
  399. datasegment:=olddatasegment;
  400. bsssegment:=oldbsssegment;
  401. codesegment:=oldcodesegment;
  402. consts:=oldconsts;
  403. debuglist:=olddebuglist;
  404. externals:=oldexternals;
  405. internals:=oldinternals;
  406. importssection:=oldimports;
  407. exportssection:=oldexports;
  408. resourcesection:=oldresource;
  409. nextlabelnr:=oldnextlabelnr;
  410. exprasmlist:=oldexprasmlist;
  411. if (compile_level=1) then
  412. begin
  413. if (not AsmRes.Empty) then
  414. begin
  415. Message1(exec_i_closing_script,AsmRes.Fn);
  416. AsmRes.WriteToDisk;
  417. end;
  418. end;
  419. dec(compile_level);
  420. end;
  421. end.
  422. {
  423. $Log$
  424. Revision 1.16 1998-05-12 10:47:00 peter
  425. * moved printstatus to verb_def
  426. + V_Normal which is between V_Error and V_Warning and doesn't have a
  427. prefix like error: warning: and is included in V_Default
  428. * fixed some messages
  429. * first time parameter scan is only for -v and -T
  430. - removed old style messages
  431. Revision 1.15 1998/05/11 13:07:54 peter
  432. + $ifdef NEWPPU for the new ppuformat
  433. + $define GDB not longer required
  434. * removed all warnings and stripped some log comments
  435. * no findfirst/findnext anymore to remove smartlink *.o files
  436. Revision 1.14 1998/05/06 18:36:53 peter
  437. * tai_section extended with code,data,bss sections and enumerated type
  438. * ident 'compiled by FPC' moved to pmodules
  439. * small fix for smartlink
  440. Revision 1.13 1998/05/06 08:38:42 pierre
  441. * better position info with UseTokenInfo
  442. UseTokenInfo greatly simplified
  443. + added check for changed tree after first time firstpass
  444. (if we could remove all the cases were it happen
  445. we could skip all firstpass if firstpasscount > 1)
  446. Only with ExtDebug
  447. Revision 1.12 1998/05/04 17:54:28 peter
  448. + smartlinking works (only case jumptable left todo)
  449. * redesign of systems.pas to support assemblers and linkers
  450. + Unitname is now also in the PPU-file, increased version to 14
  451. Revision 1.11 1998/05/01 16:38:45 florian
  452. * handling of private and protected fixed
  453. + change_keywords_to_tp implemented to remove
  454. keywords which aren't supported by tp
  455. * break and continue are now symbols of the system unit
  456. + widestring, longstring and ansistring type released
  457. Revision 1.10 1998/05/01 07:43:56 florian
  458. + basics for rtti implemented
  459. + switch $m (generate rtti for published sections)
  460. Revision 1.9 1998/04/30 15:59:40 pierre
  461. * GDB works again better :
  462. correct type info in one pass
  463. + UseTokenInfo for better source position
  464. * fixed one remaining bug in scanner for line counts
  465. * several little fixes
  466. Revision 1.8 1998/04/29 10:33:55 pierre
  467. + added some code for ansistring (not complete nor working yet)
  468. * corrected operator overloading
  469. * corrected nasm output
  470. + started inline procedures
  471. + added starstarn : use ** for exponentiation (^ gave problems)
  472. + started UseTokenInfo cond to get accurate positions
  473. Revision 1.7 1998/04/27 23:10:28 peter
  474. + new scanner
  475. * $makelib -> if smartlink
  476. * small filename fixes pmodule.setfilename
  477. * moved import from files.pas -> import.pas
  478. Revision 1.6 1998/04/21 10:16:48 peter
  479. * patches from strasbourg
  480. * objects is not used anymore in the fpc compiled version
  481. Revision 1.5 1998/04/10 14:41:43 peter
  482. * removed some Hints
  483. * small speed optimization for AsmLn
  484. Revision 1.4 1998/04/08 16:58:03 pierre
  485. * several bugfixes
  486. ADD ADC and AND are also sign extended
  487. nasm output OK (program still crashes at end
  488. and creates wrong assembler files !!)
  489. procsym types sym in tdef removed !!
  490. Revision 1.3 1998/04/07 22:45:04 florian
  491. * bug0092, bug0115 and bug0121 fixed
  492. + packed object/class/array
  493. }