parser.pas 22 KB

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