parser.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit does the parsing process
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit parser;
  18. {$i fpcdefs.inc}
  19. interface
  20. {$ifdef PREPROCWRITE}
  21. procedure preprocess(const filename:string);
  22. {$endif PREPROCWRITE}
  23. procedure compile(const filename:string);
  24. procedure initparser;
  25. procedure doneparser;
  26. implementation
  27. uses
  28. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  29. sysutils,
  30. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  31. cutils,cclasses,
  32. globtype,version,tokens,systems,globals,verbose,
  33. symbase,symtable,symsym,
  34. finput,fmodule,fppu,
  35. aasmbase,aasmtai,
  36. cgbase,
  37. script,gendef,
  38. {$ifdef BrowserCol}
  39. browcol,
  40. {$endif BrowserCol}
  41. {$ifdef BrowserLog}
  42. browlog,
  43. {$endif BrowserLog}
  44. {$ifdef GDB}
  45. gdb,
  46. {$endif GDB}
  47. comphook,
  48. scanner,scandir,
  49. pbase,ptype,psystem,pmodules,psub,
  50. cresstr,cpuinfo,procinfo;
  51. procedure initparser;
  52. begin
  53. { ^M means a string or a char, because we don't parse a }
  54. { type declaration }
  55. ignore_equal:=false;
  56. { we didn't parse a object or class declaration }
  57. { and no function header }
  58. testcurobject:=0;
  59. { Current compiled module/proc }
  60. objectlibrary:=nil;
  61. current_module:=nil;
  62. compiled_module:=nil;
  63. current_procinfo:=nil;
  64. SetCompileModule(nil);
  65. loaded_units:=TLinkedList.Create;
  66. usedunits:=TLinkedList.Create;
  67. { global switches }
  68. aktglobalswitches:=initglobalswitches;
  69. aktsourcecodepage:=initsourcecodepage;
  70. { initialize scanner }
  71. InitScanner;
  72. InitScannerDirectives;
  73. { scanner }
  74. c:=#0;
  75. pattern:='';
  76. orgpattern:='';
  77. current_scanner:=nil;
  78. { register all nodes and tais }
  79. registernodes;
  80. registertais;
  81. { memory sizes }
  82. if stacksize=0 then
  83. stacksize:=target_info.stacksize;
  84. { open assembler response }
  85. if cs_link_on_target in aktglobalswitches then
  86. GenerateAsmRes(outputexedir+inputfile+'_ppas')
  87. else
  88. GenerateAsmRes(outputexedir+'ppas');
  89. { open deffile }
  90. DefFile:=TDefFile.Create(outputexedir+inputfile+target_info.defext);
  91. { list of generated .o files, so the linker can remove them }
  92. SmartLinkOFiles:=TStringList.Create;
  93. { codegen }
  94. if paraprintnodetree<>0 then
  95. printnode_reset;
  96. { target specific stuff }
  97. case target_info.system of
  98. system_powerpc_morphos:
  99. include(supported_calling_conventions,pocall_syscall);
  100. system_m68k_amiga:
  101. include(supported_calling_conventions,pocall_syscall);
  102. end;
  103. end;
  104. procedure doneparser;
  105. begin
  106. { Reset current compiling info, so destroy routines can't
  107. reference the data that might already be destroyed }
  108. objectlibrary:=nil;
  109. current_module:=nil;
  110. compiled_module:=nil;
  111. current_procinfo:=nil;
  112. SetCompileModule(nil);
  113. { unload units }
  114. loaded_units.free;
  115. usedunits.free;
  116. { if there was an error in the scanner, the scanner is
  117. still assinged }
  118. if assigned(current_scanner) then
  119. begin
  120. current_scanner.free;
  121. current_scanner:=nil;
  122. end;
  123. { close scanner }
  124. DoneScanner;
  125. { close ppas,deffile }
  126. asmres.free;
  127. deffile.free;
  128. { free list of .o files }
  129. SmartLinkOFiles.Free;
  130. end;
  131. {$ifdef PREPROCWRITE}
  132. procedure preprocess(const filename:string);
  133. var
  134. i : longint;
  135. begin
  136. new(preprocfile,init('pre'));
  137. { initialize a module }
  138. current_module:=new(pmodule,init(filename,false));
  139. macrosymtablestack:= initialmacrosymtable;
  140. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  141. current_module.localmacrosymtable.next:= initialmacrosymtable;
  142. macrosymtablestack:= current_module.localmacrosymtable;
  143. ConsolidateMode;
  144. main_module:=current_module;
  145. { startup scanner, and save in current_module }
  146. current_scanner:=new(pscannerfile,Init(filename));
  147. current_module.scanner:=current_scanner;
  148. { loop until EOF is found }
  149. repeat
  150. current_scanner^.readtoken;
  151. preprocfile^.AddSpace;
  152. case token of
  153. _ID :
  154. begin
  155. preprocfile^.Add(orgpattern);
  156. end;
  157. _REALNUMBER,
  158. _INTCONST :
  159. preprocfile^.Add(pattern);
  160. _CSTRING :
  161. begin
  162. i:=0;
  163. while (i<length(pattern)) do
  164. begin
  165. inc(i);
  166. if pattern[i]='''' then
  167. begin
  168. insert('''',pattern,i);
  169. inc(i);
  170. end;
  171. end;
  172. preprocfile^.Add(''''+pattern+'''');
  173. end;
  174. _CCHAR :
  175. begin
  176. case pattern[1] of
  177. #39 :
  178. pattern:='''''''';
  179. #0..#31,
  180. #128..#255 :
  181. begin
  182. str(ord(pattern[1]),pattern);
  183. pattern:='#'+pattern;
  184. end;
  185. else
  186. pattern:=''''+pattern[1]+'''';
  187. end;
  188. preprocfile^.Add(pattern);
  189. end;
  190. _EOF :
  191. break;
  192. else
  193. preprocfile^.Add(tokeninfo^[token].str)
  194. end;
  195. until false;
  196. { free scanner }
  197. dispose(current_scanner,done);
  198. current_scanner:=nil;
  199. { close }
  200. dispose(preprocfile,done);
  201. end;
  202. {$endif PREPROCWRITE}
  203. {*****************************************************************************
  204. Create information for a new module
  205. *****************************************************************************}
  206. procedure init_module;
  207. begin
  208. { Create assembler output lists for CG }
  209. exprasmlist:=taasmoutput.create;
  210. datasegment:=taasmoutput.create;
  211. codesegment:=taasmoutput.create;
  212. bsssegment:=taasmoutput.create;
  213. debuglist:=taasmoutput.create;
  214. withdebuglist:=taasmoutput.create;
  215. consts:=taasmoutput.create;
  216. rttilist:=taasmoutput.create;
  217. picdata:=taasmoutput.create;
  218. if target_info.system=system_powerpc_darwin then
  219. picdata.concat(tai_simple.create(ait_non_lazy_symbol_pointer));
  220. ResourceStringList:=Nil;
  221. importssection:=nil;
  222. exportssection:=nil;
  223. resourcesection:=nil;
  224. { Resource strings }
  225. ResourceStrings:=TResourceStrings.Create;
  226. { use the librarydata from current_module }
  227. objectlibrary:=current_module.librarydata;
  228. end;
  229. procedure done_module;
  230. {$ifdef MEMDEBUG}
  231. var
  232. d : tmemdebug;
  233. {$endif}
  234. begin
  235. {$ifdef MEMDEBUG}
  236. d:=tmemdebug.create(current_module.modulename^+' - asmlists');
  237. {$endif}
  238. exprasmlist.free;
  239. codesegment.free;
  240. bsssegment.free;
  241. datasegment.free;
  242. debuglist.free;
  243. withdebuglist.free;
  244. consts.free;
  245. rttilist.free;
  246. picdata.free;
  247. if assigned(ResourceStringList) then
  248. ResourceStringList.free;
  249. if assigned(importssection) then
  250. importssection.free;
  251. if assigned(exportssection) then
  252. exportssection.free;
  253. if assigned(resourcesection) then
  254. resourcesection.free;
  255. {$ifdef MEMDEBUG}
  256. d.free;
  257. {$endif}
  258. { resource strings }
  259. ResourceStrings.free;
  260. objectlibrary:=nil;
  261. end;
  262. {*****************************************************************************
  263. Compile a source file
  264. *****************************************************************************}
  265. procedure compile(const filename:string);
  266. type
  267. polddata=^tolddata;
  268. tolddata=record
  269. { scanner }
  270. oldidtoken,
  271. oldtoken : ttoken;
  272. oldtokenpos : tfileposinfo;
  273. oldc : char;
  274. oldpattern,
  275. oldorgpattern : string;
  276. old_block_type : tblock_type;
  277. { symtable }
  278. oldrefsymtable,
  279. olddefaultsymtablestack,
  280. oldsymtablestack : tsymtable;
  281. olddefaultmacrosymtablestack,
  282. oldmacrosymtablestack : tsymtable;
  283. oldaktprocsym : tprocsym;
  284. { cg }
  285. oldparse_only : boolean;
  286. { asmlists }
  287. oldimports,
  288. oldexports,
  289. oldresource,
  290. oldrttilist,
  291. oldpicdata,
  292. oldresourcestringlist,
  293. oldbsssegment,
  294. olddatasegment,
  295. oldcodesegment,
  296. oldexprasmlist,
  297. olddebuglist,
  298. oldwithdebuglist,
  299. oldconsts : taasmoutput;
  300. oldobjectlibrary : tasmlibrarydata;
  301. { resourcestrings }
  302. OldResourceStrings : tResourceStrings;
  303. { akt.. things }
  304. oldaktlocalswitches : tlocalswitches;
  305. oldaktmoduleswitches : tmoduleswitches;
  306. oldaktfilepos : tfileposinfo;
  307. oldaktpackrecords,
  308. oldaktpackenum : shortint;
  309. oldaktmaxfpuregisters : longint;
  310. oldaktalignment : talignmentinfo;
  311. oldaktoutputformat : tasm;
  312. oldaktspecificoptprocessor,
  313. oldaktoptprocessor : tprocessors;
  314. oldaktfputype : tfputype;
  315. oldaktasmmode : tasmmode;
  316. oldaktinterfacetype: tinterfacetypes;
  317. oldaktmodeswitches : tmodeswitches;
  318. old_compiled_module : tmodule;
  319. oldcurrent_procinfo : tprocinfo;
  320. oldaktdefproccall : tproccalloption;
  321. oldsourcecodepage : tcodepagestring;
  322. {$ifdef GDB}
  323. store_dbx : plongint;
  324. {$endif GDB}
  325. end;
  326. var
  327. olddata : polddata;
  328. begin
  329. inc(compile_level);
  330. parser_current_file:=filename;
  331. { Uses heap memory instead of placing everything on the
  332. stack. This is needed because compile() can be called
  333. recursively }
  334. new(olddata);
  335. with olddata^ do
  336. begin
  337. old_compiled_module:=compiled_module;
  338. { save symtable state }
  339. oldsymtablestack:=symtablestack;
  340. oldmacrosymtablestack:=macrosymtablestack;
  341. olddefaultsymtablestack:=defaultsymtablestack;
  342. olddefaultmacrosymtablestack:=defaultmacrosymtablestack;
  343. oldrefsymtable:=refsymtable;
  344. oldcurrent_procinfo:=current_procinfo;
  345. oldaktdefproccall:=aktdefproccall;
  346. { save scanner state }
  347. oldc:=c;
  348. oldpattern:=pattern;
  349. oldorgpattern:=orgpattern;
  350. oldtoken:=token;
  351. oldidtoken:=idtoken;
  352. old_block_type:=block_type;
  353. oldtokenpos:=akttokenpos;
  354. oldsourcecodepage:=aktsourcecodepage;
  355. { save cg }
  356. oldparse_only:=parse_only;
  357. { save assembler lists }
  358. olddatasegment:=datasegment;
  359. oldbsssegment:=bsssegment;
  360. oldcodesegment:=codesegment;
  361. olddebuglist:=debuglist;
  362. oldwithdebuglist:=withdebuglist;
  363. oldconsts:=consts;
  364. oldrttilist:=rttilist;
  365. oldpicdata:=picdata;
  366. oldexprasmlist:=exprasmlist;
  367. oldimports:=importssection;
  368. oldexports:=exportssection;
  369. oldresource:=resourcesection;
  370. oldresourcestringlist:=resourcestringlist;
  371. oldobjectlibrary:=objectlibrary;
  372. OldResourceStrings:=ResourceStrings;
  373. { save akt... state }
  374. { handle the postponed case first }
  375. if localswitcheschanged then
  376. begin
  377. aktlocalswitches:=nextaktlocalswitches;
  378. localswitcheschanged:=false;
  379. end;
  380. oldaktlocalswitches:=aktlocalswitches;
  381. oldaktmoduleswitches:=aktmoduleswitches;
  382. oldaktalignment:=aktalignment;
  383. oldaktpackenum:=aktpackenum;
  384. oldaktpackrecords:=aktpackrecords;
  385. oldaktfputype:=aktfputype;
  386. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  387. oldaktoutputformat:=aktoutputformat;
  388. oldaktoptprocessor:=aktoptprocessor;
  389. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  390. oldaktasmmode:=aktasmmode;
  391. oldaktinterfacetype:=aktinterfacetype;
  392. oldaktfilepos:=aktfilepos;
  393. oldaktmodeswitches:=aktmodeswitches;
  394. {$ifdef GDB}
  395. store_dbx:=dbx_counter;
  396. dbx_counter:=nil;
  397. {$endif GDB}
  398. end;
  399. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  400. important for the IDE }
  401. afterassignment:=false;
  402. in_args:=false;
  403. got_addrn:=false;
  404. getprocvardef:=nil;
  405. { show info }
  406. Message1(parser_i_compiling,filename);
  407. { reset symtable }
  408. symtablestack:=nil;
  409. macrosymtablestack:=nil;
  410. defaultsymtablestack:=nil;
  411. defaultmacrosymtablestack:=nil;
  412. systemunit:=nil;
  413. refsymtable:=nil;
  414. aktdefproccall:=initdefproccall;
  415. registerdef:=true;
  416. aktexceptblock:=0;
  417. exceptblockcounter:=0;
  418. aktmaxfpuregisters:=-1;
  419. { reset the unit or create a new program }
  420. { a unit compiled at command line must be inside the loaded_unit list }
  421. if (compile_level=1) then
  422. begin
  423. if assigned(current_module) then
  424. internalerror(200501158);
  425. current_module:=tppumodule.create(nil,filename,'',false);
  426. addloadedunit(current_module);
  427. main_module:=current_module;
  428. current_module.state:=ms_compile;
  429. end;
  430. if not(assigned(current_module) and
  431. (current_module.state in [ms_compile,ms_second_compile])) then
  432. internalerror(200212281);
  433. { Set the module to use for verbose }
  434. compiled_module:=current_module;
  435. SetCompileModule(current_module);
  436. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  437. { Load current state from the init values }
  438. aktlocalswitches:=initlocalswitches;
  439. aktmoduleswitches:=initmoduleswitches;
  440. aktmodeswitches:=initmodeswitches;
  441. {$IFDEF Testvarsets}
  442. aktsetalloc:=initsetalloc;
  443. {$ENDIF}
  444. aktalignment:=initalignment;
  445. aktfputype:=initfputype;
  446. aktpackenum:=initpackenum;
  447. aktpackrecords:=0;
  448. aktoutputformat:=initoutputformat;
  449. set_target_asm(aktoutputformat);
  450. aktoptprocessor:=initoptprocessor;
  451. aktspecificoptprocessor:=initspecificoptprocessor;
  452. aktasmmode:=initasmmode;
  453. aktinterfacetype:=initinterfacetype;
  454. { startup scanner and load the first file }
  455. current_scanner:=tscannerfile.Create(filename);
  456. current_scanner.firstfile;
  457. current_module.scanner:=current_scanner;
  458. { init macros before anything in the file is parsed.}
  459. macrosymtablestack:= initialmacrosymtable;
  460. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  461. current_module.localmacrosymtable.next:= initialmacrosymtable;
  462. macrosymtablestack:= current_module.localmacrosymtable;
  463. { read the first token }
  464. current_scanner.readtoken;
  465. { init code generator for a new module }
  466. init_module;
  467. { If the compile level > 1 we get a nice "unit expected" error
  468. message if we are trying to use a program as unit.}
  469. try
  470. try
  471. if (token=_UNIT) or (compile_level>1) then
  472. begin
  473. current_module.is_unit:=true;
  474. proc_unit;
  475. end
  476. else
  477. proc_program(token=_LIBRARY);
  478. except
  479. on ECompilerAbort do
  480. raise;
  481. on Exception do
  482. begin
  483. { Increase errorcounter to prevent some
  484. checks during cleanup }
  485. inc(status.errorcount);
  486. raise;
  487. end;
  488. end;
  489. finally
  490. { restore old state }
  491. done_module;
  492. if assigned(current_module) then
  493. begin
  494. { module is now compiled }
  495. tppumodule(current_module).state:=ms_compiled;
  496. { free ppu }
  497. if assigned(tppumodule(current_module).ppufile) then
  498. begin
  499. tppumodule(current_module).ppufile.free;
  500. tppumodule(current_module).ppufile:=nil;
  501. end;
  502. { free scanner }
  503. if assigned(current_module.scanner) then
  504. begin
  505. if current_scanner=tscannerfile(current_module.scanner) then
  506. current_scanner:=nil;
  507. tscannerfile(current_module.scanner).free;
  508. current_module.scanner:=nil;
  509. end;
  510. end;
  511. if (compile_level>1) then
  512. begin
  513. with olddata^ do
  514. begin
  515. { restore scanner }
  516. c:=oldc;
  517. pattern:=oldpattern;
  518. orgpattern:=oldorgpattern;
  519. token:=oldtoken;
  520. idtoken:=oldidtoken;
  521. akttokenpos:=oldtokenpos;
  522. block_type:=old_block_type;
  523. { restore cg }
  524. parse_only:=oldparse_only;
  525. { restore asmlists }
  526. exprasmlist:=oldexprasmlist;
  527. datasegment:=olddatasegment;
  528. bsssegment:=oldbsssegment;
  529. codesegment:=oldcodesegment;
  530. consts:=oldconsts;
  531. debuglist:=olddebuglist;
  532. withdebuglist:=oldwithdebuglist;
  533. importssection:=oldimports;
  534. exportssection:=oldexports;
  535. resourcesection:=oldresource;
  536. rttilist:=oldrttilist;
  537. picdata:=oldpicdata;
  538. resourcestringlist:=oldresourcestringlist;
  539. { object data }
  540. ResourceStrings:=OldResourceStrings;
  541. objectlibrary:=oldobjectlibrary;
  542. { restore previous scanner }
  543. if assigned(old_compiled_module) then
  544. current_scanner:=tscannerfile(old_compiled_module.scanner)
  545. else
  546. current_scanner:=nil;
  547. if assigned(current_scanner) then
  548. parser_current_file:=current_scanner.inputfile.name^;
  549. { restore symtable state }
  550. refsymtable:=oldrefsymtable;
  551. symtablestack:=oldsymtablestack;
  552. macrosymtablestack:=oldmacrosymtablestack;
  553. defaultsymtablestack:=olddefaultsymtablestack;
  554. defaultmacrosymtablestack:=olddefaultmacrosymtablestack;
  555. aktdefproccall:=oldaktdefproccall;
  556. current_procinfo:=oldcurrent_procinfo;
  557. aktsourcecodepage:=oldsourcecodepage;
  558. aktlocalswitches:=oldaktlocalswitches;
  559. aktmoduleswitches:=oldaktmoduleswitches;
  560. aktalignment:=oldaktalignment;
  561. aktpackenum:=oldaktpackenum;
  562. aktpackrecords:=oldaktpackrecords;
  563. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  564. aktoutputformat:=oldaktoutputformat;
  565. set_target_asm(aktoutputformat);
  566. aktoptprocessor:=oldaktoptprocessor;
  567. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  568. aktfputype:=oldaktfputype;
  569. aktasmmode:=oldaktasmmode;
  570. aktinterfacetype:=oldaktinterfacetype;
  571. aktfilepos:=oldaktfilepos;
  572. aktmodeswitches:=oldaktmodeswitches;
  573. aktexceptblock:=0;
  574. exceptblockcounter:=0;
  575. {$ifdef GDB}
  576. dbx_counter:=store_dbx;
  577. {$endif GDB}
  578. end;
  579. end
  580. else
  581. begin
  582. { Shut down things when the last file is compiled succesfull }
  583. if (compile_level=1) and
  584. (status.errorcount=0) then
  585. begin
  586. parser_current_file:='';
  587. { Close script }
  588. if (not AsmRes.Empty) then
  589. begin
  590. Message1(exec_i_closing_script,AsmRes.Fn);
  591. AsmRes.WriteToDisk;
  592. end;
  593. { do not create browsers on errors !! }
  594. if status.errorcount=0 then
  595. begin
  596. {$ifdef BrowserLog}
  597. { Write Browser Log }
  598. if (cs_browser_log in aktglobalswitches) and
  599. (cs_browser in aktmoduleswitches) then
  600. begin
  601. if browserlog.elements_to_list.empty then
  602. begin
  603. Message1(parser_i_writing_browser_log,browserlog.Fname);
  604. WriteBrowserLog;
  605. end
  606. else
  607. browserlog.list_elements;
  608. end;
  609. {$endif BrowserLog}
  610. { Write Browser Collections, also used by the TextMode IDE to
  611. retrieve a list of sourcefiles }
  612. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  613. end;
  614. end;
  615. end;
  616. dec(compile_level);
  617. compiled_module:=olddata^.old_compiled_module;
  618. SetCompileModule(compiled_module);
  619. dispose(olddata);
  620. end;
  621. end;
  622. end.