parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. 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_amiga:
  96. include(supported_calling_conventions,pocall_syscall);
  97. system_powerpc_morphos:
  98. include(supported_calling_conventions,pocall_syscall);
  99. system_m68k_amiga:
  100. include(supported_calling_conventions,pocall_syscall);
  101. end;
  102. end;
  103. procedure doneparser;
  104. begin
  105. { Reset current compiling info, so destroy routines can't
  106. reference the data that might already be destroyed }
  107. objectlibrary:=nil;
  108. current_module:=nil;
  109. compiled_module:=nil;
  110. current_procinfo:=nil;
  111. SetCompileModule(nil);
  112. { unload units }
  113. if assigned(loaded_units) then
  114. begin
  115. loaded_units.free;
  116. loaded_units:=nil;
  117. end;
  118. if assigned(usedunits) then
  119. begin
  120. usedunits.free;
  121. usedunits:=nil;
  122. end;
  123. { if there was an error in the scanner, the scanner is
  124. still assinged }
  125. if assigned(current_scanner) then
  126. begin
  127. current_scanner.free;
  128. current_scanner:=nil;
  129. end;
  130. { close scanner }
  131. DoneScanner;
  132. { close ppas,deffile }
  133. asmres.free;
  134. deffile.free;
  135. { free list of .o files }
  136. SmartLinkOFiles.Free;
  137. end;
  138. {$ifdef PREPROCWRITE}
  139. procedure preprocess(const filename:string);
  140. var
  141. i : longint;
  142. begin
  143. new(preprocfile,init('pre'));
  144. { initialize a module }
  145. current_module:=new(pmodule,init(filename,false));
  146. macrosymtablestack:= initialmacrosymtable;
  147. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  148. current_module.localmacrosymtable.next:= initialmacrosymtable;
  149. macrosymtablestack:= current_module.localmacrosymtable;
  150. main_module:=current_module;
  151. { startup scanner, and save in current_module }
  152. current_scanner:=new(pscannerfile,Init(filename));
  153. current_module.scanner:=current_scanner;
  154. { loop until EOF is found }
  155. repeat
  156. current_scanner^.readtoken(true);
  157. preprocfile^.AddSpace;
  158. case token of
  159. _ID :
  160. begin
  161. preprocfile^.Add(orgpattern);
  162. end;
  163. _REALNUMBER,
  164. _INTCONST :
  165. preprocfile^.Add(pattern);
  166. _CSTRING :
  167. begin
  168. i:=0;
  169. while (i<length(pattern)) do
  170. begin
  171. inc(i);
  172. if pattern[i]='''' then
  173. begin
  174. insert('''',pattern,i);
  175. inc(i);
  176. end;
  177. end;
  178. preprocfile^.Add(''''+pattern+'''');
  179. end;
  180. _CCHAR :
  181. begin
  182. case pattern[1] of
  183. #39 :
  184. pattern:='''''''';
  185. #0..#31,
  186. #128..#255 :
  187. begin
  188. str(ord(pattern[1]),pattern);
  189. pattern:='#'+pattern;
  190. end;
  191. else
  192. pattern:=''''+pattern[1]+'''';
  193. end;
  194. preprocfile^.Add(pattern);
  195. end;
  196. _EOF :
  197. break;
  198. else
  199. preprocfile^.Add(tokeninfo^[token].str)
  200. end;
  201. until false;
  202. { free scanner }
  203. dispose(current_scanner,done);
  204. current_scanner:=nil;
  205. { close }
  206. dispose(preprocfile,done);
  207. end;
  208. {$endif PREPROCWRITE}
  209. {*****************************************************************************
  210. Create information for a new module
  211. *****************************************************************************}
  212. procedure init_module;
  213. var
  214. i : Tasmlist;
  215. begin
  216. exprasmlist:=taasmoutput.create;
  217. for i:=low(Tasmlist) to high(Tasmlist) do
  218. asmlist[i]:=Taasmoutput.create;
  219. { PIC data }
  220. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  221. asmlist[al_picdata].concat(tai_directive.create(asd_non_lazy_symbol_pointer,''));
  222. { Resource strings }
  223. cresstr.resourcestrings:=Tresourcestrings.Create;
  224. { use the librarydata from current_module }
  225. objectlibrary:=current_module.librarydata;
  226. end;
  227. procedure done_module;
  228. var
  229. {$ifdef MEMDEBUG}
  230. d : tmemdebug;
  231. {$endif}
  232. i:Tasmlist;
  233. begin
  234. {$ifdef MEMDEBUG}
  235. d:=tmemdebug.create(current_module.modulename^+' - asmlists');
  236. {$endif}
  237. for i:=low(Tasmlist) to high(Tasmlist) do
  238. if asmlist[i]<>nil then
  239. asmlist[i].free;
  240. {$ifdef MEMDEBUG}
  241. d.free;
  242. {$endif}
  243. { resource strings }
  244. cresstr.resourcestrings.free;
  245. objectlibrary:=nil;
  246. end;
  247. {*****************************************************************************
  248. Compile a source file
  249. *****************************************************************************}
  250. procedure compile(const filename:string);
  251. type
  252. polddata=^tolddata;
  253. tolddata=record
  254. { scanner }
  255. oldidtoken,
  256. oldtoken : ttoken;
  257. oldtokenpos : tfileposinfo;
  258. oldc : char;
  259. oldpattern,
  260. oldorgpattern : string;
  261. old_block_type : tblock_type;
  262. { symtable }
  263. oldsymtablestack,
  264. oldmacrosymtablestack : tsymtablestack;
  265. oldaktprocsym : tprocsym;
  266. { cg }
  267. oldparse_only : boolean;
  268. { asmlists }
  269. oldexprasmlist:Taasmoutput;
  270. oldasmlist:array[Tasmlist] of Taasmoutput;
  271. oldobjectlibrary : TObjLibraryData;
  272. { al_resourcestrings }
  273. Oldresourcestrings : tresourcestrings;
  274. { akt.. things }
  275. oldaktlocalswitches : tlocalswitches;
  276. oldaktmoduleswitches : tmoduleswitches;
  277. oldaktfilepos : tfileposinfo;
  278. oldaktpackrecords,
  279. oldaktpackenum : shortint;
  280. oldaktmaxfpuregisters : longint;
  281. oldaktalignment : talignmentinfo;
  282. oldaktspecificoptprocessor,
  283. oldaktoptprocessor : tprocessors;
  284. oldaktfputype : tfputype;
  285. oldaktasmmode : tasmmode;
  286. oldaktinterfacetype: tinterfacetypes;
  287. oldaktmodeswitches : tmodeswitches;
  288. old_compiled_module : tmodule;
  289. oldcurrent_procinfo : tprocinfo;
  290. oldaktdefproccall : tproccalloption;
  291. oldsourcecodepage : tcodepagestring;
  292. end;
  293. var
  294. olddata : polddata;
  295. begin
  296. inc(compile_level);
  297. parser_current_file:=filename;
  298. { Uses heap memory instead of placing everything on the
  299. stack. This is needed because compile() can be called
  300. recursively }
  301. new(olddata);
  302. with olddata^ do
  303. begin
  304. old_compiled_module:=compiled_module;
  305. { save symtable state }
  306. oldsymtablestack:=symtablestack;
  307. oldmacrosymtablestack:=macrosymtablestack;
  308. oldcurrent_procinfo:=current_procinfo;
  309. oldaktdefproccall:=aktdefproccall;
  310. { save scanner state }
  311. oldc:=c;
  312. oldpattern:=pattern;
  313. oldorgpattern:=orgpattern;
  314. oldtoken:=token;
  315. oldidtoken:=idtoken;
  316. old_block_type:=block_type;
  317. oldtokenpos:=akttokenpos;
  318. oldsourcecodepage:=aktsourcecodepage;
  319. { save cg }
  320. oldparse_only:=parse_only;
  321. { save assembler lists }
  322. oldasmlist:=asmlist;
  323. oldexprasmlist:=exprasmlist;
  324. oldobjectlibrary:=objectlibrary;
  325. Oldresourcestrings:=resourcestrings;
  326. { save akt... state }
  327. { handle the postponed case first }
  328. if localswitcheschanged then
  329. begin
  330. aktlocalswitches:=nextaktlocalswitches;
  331. localswitcheschanged:=false;
  332. end;
  333. oldaktlocalswitches:=aktlocalswitches;
  334. oldaktmoduleswitches:=aktmoduleswitches;
  335. oldaktalignment:=aktalignment;
  336. oldaktpackenum:=aktpackenum;
  337. oldaktpackrecords:=aktpackrecords;
  338. oldaktfputype:=aktfputype;
  339. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  340. oldaktoptprocessor:=aktoptprocessor;
  341. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  342. oldaktasmmode:=aktasmmode;
  343. oldaktinterfacetype:=aktinterfacetype;
  344. oldaktfilepos:=aktfilepos;
  345. oldaktmodeswitches:=aktmodeswitches;
  346. end;
  347. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  348. important for the IDE }
  349. afterassignment:=false;
  350. in_args:=false;
  351. got_addrn:=false;
  352. getprocvardef:=nil;
  353. { show info }
  354. Message1(parser_i_compiling,filename);
  355. { reset symtable }
  356. symtablestack:=tsymtablestack.create;
  357. macrosymtablestack:=tsymtablestack.create;
  358. systemunit:=nil;
  359. aktdefproccall:=initdefproccall;
  360. aktexceptblock:=0;
  361. exceptblockcounter:=0;
  362. aktmaxfpuregisters:=-1;
  363. { reset the unit or create a new program }
  364. { a unit compiled at command line must be inside the loaded_unit list }
  365. if (compile_level=1) then
  366. begin
  367. if assigned(current_module) then
  368. internalerror(200501158);
  369. current_module:=tppumodule.create(nil,filename,'',false);
  370. addloadedunit(current_module);
  371. main_module:=current_module;
  372. current_module.state:=ms_compile;
  373. end;
  374. if not(assigned(current_module) and
  375. (current_module.state in [ms_compile,ms_second_compile])) then
  376. internalerror(200212281);
  377. { Set the module to use for verbose }
  378. compiled_module:=current_module;
  379. SetCompileModule(current_module);
  380. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  381. { Load current state from the init values }
  382. aktlocalswitches:=initlocalswitches;
  383. aktmoduleswitches:=initmoduleswitches;
  384. aktmodeswitches:=initmodeswitches;
  385. {$IFDEF Testvarsets}
  386. aktsetalloc:=initsetalloc;
  387. {$ENDIF}
  388. aktalignment:=initalignment;
  389. aktfputype:=initfputype;
  390. aktpackenum:=initpackenum;
  391. aktpackrecords:=0;
  392. aktoptprocessor:=initoptprocessor;
  393. aktspecificoptprocessor:=initspecificoptprocessor;
  394. aktasmmode:=initasmmode;
  395. aktinterfacetype:=initinterfacetype;
  396. { startup scanner and load the first file }
  397. current_scanner:=tscannerfile.Create(filename);
  398. current_scanner.firstfile;
  399. current_module.scanner:=current_scanner;
  400. { init macros before anything in the file is parsed.}
  401. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  402. macrosymtablestack.push(initialmacrosymtable);
  403. macrosymtablestack.push(current_module.localmacrosymtable);
  404. { read the first token }
  405. current_scanner.readtoken(false);
  406. { init code generator for a new module }
  407. init_module;
  408. { If the compile level > 1 we get a nice "unit expected" error
  409. message if we are trying to use a program as unit.}
  410. try
  411. try
  412. if (token=_UNIT) or (compile_level>1) then
  413. begin
  414. current_module.is_unit:=true;
  415. proc_unit;
  416. end
  417. else
  418. proc_program(token=_LIBRARY);
  419. except
  420. on ECompilerAbort do
  421. raise;
  422. on Exception do
  423. begin
  424. { Increase errorcounter to prevent some
  425. checks during cleanup }
  426. inc(status.errorcount);
  427. raise;
  428. end;
  429. end;
  430. finally
  431. { restore old state }
  432. done_module;
  433. if assigned(current_module) then
  434. begin
  435. { module is now compiled }
  436. tppumodule(current_module).state:=ms_compiled;
  437. { free ppu }
  438. if assigned(tppumodule(current_module).ppufile) then
  439. begin
  440. tppumodule(current_module).ppufile.free;
  441. tppumodule(current_module).ppufile:=nil;
  442. end;
  443. { free scanner }
  444. if assigned(current_module.scanner) then
  445. begin
  446. if current_scanner=tscannerfile(current_module.scanner) then
  447. current_scanner:=nil;
  448. tscannerfile(current_module.scanner).free;
  449. current_module.scanner:=nil;
  450. end;
  451. { free symtable stack }
  452. if assigned(symtablestack) then
  453. begin
  454. symtablestack.free;
  455. symtablestack:=nil;
  456. end;
  457. if assigned(macrosymtablestack) then
  458. begin
  459. macrosymtablestack.free;
  460. macrosymtablestack:=nil;
  461. end;
  462. end;
  463. with olddata^ do
  464. begin
  465. { restore scanner }
  466. c:=oldc;
  467. pattern:=oldpattern;
  468. orgpattern:=oldorgpattern;
  469. token:=oldtoken;
  470. idtoken:=oldidtoken;
  471. akttokenpos:=oldtokenpos;
  472. block_type:=old_block_type;
  473. { restore cg }
  474. parse_only:=oldparse_only;
  475. { restore asmlists }
  476. exprasmlist:=oldexprasmlist;
  477. asmlist:=oldasmlist;
  478. { object data }
  479. resourcestrings:=oldresourcestrings;
  480. objectlibrary:=oldobjectlibrary;
  481. { restore previous scanner }
  482. if assigned(old_compiled_module) then
  483. current_scanner:=tscannerfile(old_compiled_module.scanner)
  484. else
  485. current_scanner:=nil;
  486. if assigned(current_scanner) then
  487. parser_current_file:=current_scanner.inputfile.name^;
  488. { restore symtable state }
  489. symtablestack:=oldsymtablestack;
  490. macrosymtablestack:=oldmacrosymtablestack;
  491. aktdefproccall:=oldaktdefproccall;
  492. current_procinfo:=oldcurrent_procinfo;
  493. aktsourcecodepage:=oldsourcecodepage;
  494. aktlocalswitches:=oldaktlocalswitches;
  495. aktmoduleswitches:=oldaktmoduleswitches;
  496. aktalignment:=oldaktalignment;
  497. aktpackenum:=oldaktpackenum;
  498. aktpackrecords:=oldaktpackrecords;
  499. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  500. aktoptprocessor:=oldaktoptprocessor;
  501. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  502. aktfputype:=oldaktfputype;
  503. aktasmmode:=oldaktasmmode;
  504. aktinterfacetype:=oldaktinterfacetype;
  505. aktfilepos:=oldaktfilepos;
  506. aktmodeswitches:=oldaktmodeswitches;
  507. aktexceptblock:=0;
  508. exceptblockcounter:=0;
  509. end;
  510. { Shut down things when the last file is compiled succesfull }
  511. if (compile_level=1) and
  512. (status.errorcount=0) then
  513. begin
  514. parser_current_file:='';
  515. { Close script }
  516. if (not AsmRes.Empty) then
  517. begin
  518. Message1(exec_i_closing_script,AsmRes.Fn);
  519. AsmRes.WriteToDisk;
  520. end;
  521. { do not create browsers on errors !! }
  522. if status.errorcount=0 then
  523. begin
  524. {$ifdef BrowserLog}
  525. { Write Browser Log }
  526. if (cs_browser_log in aktglobalswitches) and
  527. (cs_browser in aktmoduleswitches) then
  528. begin
  529. if browserlog.elements_to_list.empty then
  530. begin
  531. Message1(parser_i_writing_browser_log,browserlog.Fname);
  532. WriteBrowserLog;
  533. end
  534. else
  535. browserlog.list_elements;
  536. end;
  537. {$endif BrowserLog}
  538. { Write Browser Collections, also used by the TextMode IDE to
  539. retrieve a list of sourcefiles }
  540. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  541. end;
  542. end;
  543. dec(compile_level);
  544. compiled_module:=olddata^.old_compiled_module;
  545. SetCompileModule(compiled_module);
  546. dispose(olddata);
  547. end;
  548. end;
  549. end.