parser.pas 19 KB

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