parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. oldimports,oldexports,oldresource,oldrttilist,
  112. oldbsssegment,olddatasegment,oldcodesegment,
  113. oldexprasmlist,olddebuglist,
  114. oldinternals,oldexternals,oldconsts : paasmoutput;
  115. oldnextlabelnr : longint;
  116. oldswitches : Tcswitches;
  117. oldmacros,oldrefsymtable,oldsymtablestack : psymtable;
  118. procedure def_macro(const s : string);
  119. var
  120. mac : pmacrosym;
  121. begin
  122. mac:=pmacrosym(macros^.search(s));
  123. if mac=nil then
  124. begin
  125. mac:=new(pmacrosym,init(s));
  126. Message1(parser_m_macro_defined,mac^.name);
  127. macros^.insert(mac);
  128. end;
  129. mac^.defined:=true;
  130. end;
  131. procedure set_macro(const s : string;value : string);
  132. var
  133. mac : pmacrosym;
  134. begin
  135. mac:=pmacrosym(macros^.search(s));
  136. if mac=nil then
  137. begin
  138. mac:=new(pmacrosym,init(s));
  139. macros^.insert(mac);
  140. end
  141. else
  142. begin
  143. if assigned(mac^.buftext) then
  144. freemem(mac^.buftext,mac^.buflen);
  145. end;
  146. Message2(parser_m_macro_set_to,mac^.name,value);
  147. mac^.buflen:=length(value);
  148. getmem(mac^.buftext,mac^.buflen);
  149. move(value[1],mac^.buftext^,mac^.buflen);
  150. mac^.defined:=true;
  151. end;
  152. procedure define_macros;
  153. var
  154. hp : pstring_item;
  155. begin
  156. hp:=pstring_item(commandlinedefines.first);
  157. while assigned(hp) do
  158. begin
  159. def_macro(hp^.str^);
  160. hp:=pstring_item(hp^.next);
  161. end;
  162. { set macros for version checking }
  163. set_macro('FPC_VERSION',version_nr);
  164. set_macro('FPC_RELEASE',release_nr);
  165. set_macro('FPC_PATCH',patch_nr);
  166. end;
  167. label
  168. done;
  169. begin {compile}
  170. inc(compile_level);
  171. { save old state }
  172. { save symtable state }
  173. oldsymtablestack:=symtablestack;
  174. symtablestack:=nil;
  175. oldrefsymtable:=refsymtable;
  176. refsymtable:=nil;
  177. oldprocprefix:=procprefix;
  178. { a long time, this was only in init_parser
  179. but it should be reset to zero for each module }
  180. aktprocsym:=nil;
  181. { first, we assume a program }
  182. if not(assigned(current_module)) then
  183. begin
  184. current_module:=new(pmodule,init(filename,false));
  185. main_module:=current_module;
  186. end;
  187. { save scanner state }
  188. oldmacros:=macros;
  189. oldpattern:=pattern;
  190. oldtoken:=token;
  191. oldtokenpos:=tokenpos;
  192. oldorgpattern:=orgpattern;
  193. old_block_type:=block_type;
  194. oldpreprocstack:=preprocstack;
  195. oldinputbuffer:=inputbuffer;
  196. oldinputpointer:=inputpointer;
  197. olds_point:=s_point;
  198. oldc:=c;
  199. oldcomment_level:=comment_level;
  200. oldparse_only:=parse_only;
  201. { save assembler lists }
  202. olddatasegment:=datasegment;
  203. oldbsssegment:=bsssegment;
  204. oldcodesegment:=codesegment;
  205. olddebuglist:=debuglist;
  206. oldexternals:=externals;
  207. oldinternals:=internals;
  208. oldconsts:=consts;
  209. oldrttilist:=rttilist;
  210. oldexprasmlist:=exprasmlist;
  211. oldimports:=importssection;
  212. oldexports:=exportssection;
  213. oldresource:=resourcesection;
  214. oldswitches:=aktswitches;
  215. oldnextlabelnr:=nextlabelnr;
  216. Message1(parser_i_compiling,filename);
  217. InitScanner(filename);
  218. aktswitches:=initswitches;
  219. { we need this to make the system unit }
  220. if compile_system then
  221. aktswitches:=aktswitches+[cs_compilesystem];
  222. aktpackrecords:=initpackrecords;
  223. { init code generator for a new module }
  224. codegen_newmodule;
  225. macros:=new(psymtable,init(macrosymtable));
  226. macros^.name:=stringdup('Conditionals for '+filename);
  227. define_macros;
  228. { startup scanner }
  229. token:=yylex;
  230. reset_gdb_info;
  231. { init asm writing }
  232. datasegment:=new(paasmoutput,init);
  233. codesegment:=new(paasmoutput,init);
  234. bsssegment:=new(paasmoutput,init);
  235. debuglist:=new(paasmoutput,init);
  236. externals:=new(paasmoutput,init);
  237. internals:=new(paasmoutput,init);
  238. consts:=new(paasmoutput,init);
  239. rttilist:=new(paasmoutput,init);
  240. importssection:=nil;
  241. exportssection:=nil;
  242. resourcesection:=nil;
  243. { global switches are read, so further changes aren't allowed }
  244. current_module^.in_main:=true;
  245. { open assembler response }
  246. if (compile_level=1) then
  247. AsmRes.Init('ppas');
  248. { if the current file isn't a system unit }
  249. { the the system unit will be loaded }
  250. if not(cs_compilesystem in aktswitches) then
  251. begin
  252. { should be done in unit system (changing the field system_unit)
  253. FK
  254. }
  255. hp:=loadunit(upper(target_info.system_unit),true,true);
  256. systemunit:=hp^.symtable;
  257. make_ref:=false;
  258. readconstdefs;
  259. { we could try to overload caret by default }
  260. symtablestack:=systemunit;
  261. { if POWER is defined in the RTL then use it for starstar overloading }
  262. getsym('POWER',false);
  263. if assigned(srsym) and (srsym^.typ=procsym) and
  264. (overloaded_operators[STARSTAR]=nil) then
  265. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  266. make_ref:=true;
  267. end
  268. else
  269. begin
  270. { create definitions for constants }
  271. registerdef:=false;
  272. s32bitdef:=new(porddef,init(s32bit,$80000000,$7fffffff));
  273. u32bitdef:=new(porddef,init(u32bit,0,$ffffffff));
  274. cstringdef:=new(pstringdef,init(255));
  275. { should we give a length to the default long and ansi string definition ?? }
  276. clongstringdef:=new(pstringdef,longinit(-1));
  277. cansistringdef:=new(pstringdef,ansiinit(-1));
  278. cwidestringdef:=new(pstringdef,wideinit(-1));
  279. cchardef:=new(porddef,init(uchar,0,255));
  280. {$ifdef i386}
  281. c64floatdef:=new(pfloatdef,init(s64real));
  282. s80floatdef:=new(pfloatdef,init(s80real));
  283. {$endif}
  284. {$ifdef m68k}
  285. c64floatdef:=new(pfloatdef,init(s32real));
  286. if (cs_fp_emulation in aktswitches) then
  287. s80floatdef:=new(pfloatdef,init(s32real))
  288. else
  289. s80floatdef:=new(pfloatdef,init(s80real));
  290. {$endif}
  291. s32fixeddef:=new(pfloatdef,init(f32bit));
  292. { some other definitions }
  293. voiddef:=new(porddef,init(uvoid,0,0));
  294. u8bitdef:=new(porddef,init(u8bit,0,255));
  295. u16bitdef:=new(porddef,init(u16bit,0,65535));
  296. booldef:=new(porddef,init(bool8bit,0,1));
  297. voidpointerdef:=new(ppointerdef,init(voiddef));
  298. cfiledef:=new(pfiledef,init(ft_untyped,nil));
  299. systemunit:=nil;
  300. end;
  301. registerdef:=true;
  302. make_ref:=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. {$ifdef UseBrowser}
  373. { we need the names for the browser ! }
  374. current_module^.sourcefiles.close_all;
  375. {$else UseBrowser}
  376. current_module^.sourcefiles.done;
  377. {$endif not UseBrowser}
  378. { restore scanner state }
  379. pattern:=oldpattern;
  380. token:=oldtoken;
  381. tokenpos:=oldtokenpos;
  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.17 1998-05-20 09:42:34 pierre
  425. + UseTokenInfo now default
  426. * unit in interface uses and implementation uses gives error now
  427. * only one error for unknown symbol (uses lastsymknown boolean)
  428. the problem came from the label code !
  429. + first inlined procedures and function work
  430. (warning there might be allowed cases were the result is still wrong !!)
  431. * UseBrower updated gives a global list of all position of all used symbols
  432. with switch -gb
  433. Revision 1.16 1998/05/12 10:47:00 peter
  434. * moved printstatus to verb_def
  435. + V_Normal which is between V_Error and V_Warning and doesn't have a
  436. prefix like error: warning: and is included in V_Default
  437. * fixed some messages
  438. * first time parameter scan is only for -v and -T
  439. - removed old style messages
  440. Revision 1.15 1998/05/11 13:07:54 peter
  441. + $ifdef NEWPPU for the new ppuformat
  442. + $define GDB not longer required
  443. * removed all warnings and stripped some log comments
  444. * no findfirst/findnext anymore to remove smartlink *.o files
  445. Revision 1.14 1998/05/06 18:36:53 peter
  446. * tai_section extended with code,data,bss sections and enumerated type
  447. * ident 'compiled by FPC' moved to pmodules
  448. * small fix for smartlink
  449. Revision 1.13 1998/05/06 08:38:42 pierre
  450. * better position info with UseTokenInfo
  451. UseTokenInfo greatly simplified
  452. + added check for changed tree after first time firstpass
  453. (if we could remove all the cases were it happen
  454. we could skip all firstpass if firstpasscount > 1)
  455. Only with ExtDebug
  456. Revision 1.12 1998/05/04 17:54:28 peter
  457. + smartlinking works (only case jumptable left todo)
  458. * redesign of systems.pas to support assemblers and linkers
  459. + Unitname is now also in the PPU-file, increased version to 14
  460. Revision 1.11 1998/05/01 16:38:45 florian
  461. * handling of private and protected fixed
  462. + change_keywords_to_tp implemented to remove
  463. keywords which aren't supported by tp
  464. * break and continue are now symbols of the system unit
  465. + widestring, longstring and ansistring type released
  466. Revision 1.10 1998/05/01 07:43:56 florian
  467. + basics for rtti implemented
  468. + switch $m (generate rtti for published sections)
  469. Revision 1.9 1998/04/30 15:59:40 pierre
  470. * GDB works again better :
  471. correct type info in one pass
  472. + UseTokenInfo for better source position
  473. * fixed one remaining bug in scanner for line counts
  474. * several little fixes
  475. Revision 1.8 1998/04/29 10:33:55 pierre
  476. + added some code for ansistring (not complete nor working yet)
  477. * corrected operator overloading
  478. * corrected nasm output
  479. + started inline procedures
  480. + added starstarn : use ** for exponentiation (^ gave problems)
  481. + started UseTokenInfo cond to get accurate positions
  482. Revision 1.7 1998/04/27 23:10:28 peter
  483. + new scanner
  484. * $makelib -> if smartlink
  485. * small filename fixes pmodule.setfilename
  486. * moved import from files.pas -> import.pas
  487. Revision 1.6 1998/04/21 10:16:48 peter
  488. * patches from strasbourg
  489. * objects is not used anymore in the fpc compiled version
  490. Revision 1.5 1998/04/10 14:41:43 peter
  491. * removed some Hints
  492. * small speed optimization for AsmLn
  493. Revision 1.4 1998/04/08 16:58:03 pierre
  494. * several bugfixes
  495. ADD ADC and AND are also sign extended
  496. nasm output OK (program still crashes at end
  497. and creates wrong assembler files !!)
  498. procsym types sym in tdef removed !!
  499. Revision 1.3 1998/04/07 22:45:04 florian
  500. * bug0092, bug0115 and bug0121 fixed
  501. + packed object/class/array
  502. }