parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. oldaktoptimizecputype,
  283. oldaktcputype : tcputype;
  284. oldaktfputype : tfputype;
  285. oldaktasmmode : tasmmode;
  286. oldaktinterfacetype: tinterfacetypes;
  287. oldaktmodeswitches : tmodeswitches;
  288. oldaktoptimizerswitches : toptimizerswitches;
  289. old_compiled_module : tmodule;
  290. oldcurrent_procinfo : tprocinfo;
  291. oldaktdefproccall : tproccalloption;
  292. oldsourcecodepage : tcodepagestring;
  293. end;
  294. var
  295. olddata : polddata;
  296. begin
  297. inc(compile_level);
  298. parser_current_file:=filename;
  299. { Uses heap memory instead of placing everything on the
  300. stack. This is needed because compile() can be called
  301. recursively }
  302. new(olddata);
  303. with olddata^ do
  304. begin
  305. old_compiled_module:=compiled_module;
  306. { save symtable state }
  307. oldsymtablestack:=symtablestack;
  308. oldmacrosymtablestack:=macrosymtablestack;
  309. oldcurrent_procinfo:=current_procinfo;
  310. oldaktdefproccall:=aktdefproccall;
  311. { save scanner state }
  312. oldc:=c;
  313. oldpattern:=pattern;
  314. oldorgpattern:=orgpattern;
  315. oldtoken:=token;
  316. oldidtoken:=idtoken;
  317. old_block_type:=block_type;
  318. oldtokenpos:=akttokenpos;
  319. oldsourcecodepage:=aktsourcecodepage;
  320. { save cg }
  321. oldparse_only:=parse_only;
  322. { save assembler lists }
  323. oldasmlist:=asmlist;
  324. oldexprasmlist:=exprasmlist;
  325. oldobjectlibrary:=objectlibrary;
  326. Oldresourcestrings:=resourcestrings;
  327. { save akt... state }
  328. { handle the postponed case first }
  329. if localswitcheschanged then
  330. begin
  331. aktlocalswitches:=nextaktlocalswitches;
  332. localswitcheschanged:=false;
  333. end;
  334. oldaktlocalswitches:=aktlocalswitches;
  335. oldaktmoduleswitches:=aktmoduleswitches;
  336. oldaktalignment:=aktalignment;
  337. oldaktpackenum:=aktpackenum;
  338. oldaktpackrecords:=aktpackrecords;
  339. oldaktfputype:=aktfputype;
  340. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  341. oldaktcputype:=aktcputype;
  342. oldaktoptimizecputype:=aktoptimizecputype;
  343. oldaktasmmode:=aktasmmode;
  344. oldaktinterfacetype:=aktinterfacetype;
  345. oldaktfilepos:=aktfilepos;
  346. oldaktmodeswitches:=aktmodeswitches;
  347. oldaktoptimizerswitches:=aktoptimizerswitches;
  348. end;
  349. { reset parser, a previous fatal error could have left these variables in an unreliable state, this is
  350. important for the IDE }
  351. afterassignment:=false;
  352. in_args:=false;
  353. got_addrn:=false;
  354. getprocvardef:=nil;
  355. { show info }
  356. Message1(parser_i_compiling,filename);
  357. { reset symtable }
  358. symtablestack:=tsymtablestack.create;
  359. macrosymtablestack:=tsymtablestack.create;
  360. systemunit:=nil;
  361. aktdefproccall:=initdefproccall;
  362. aktexceptblock:=0;
  363. exceptblockcounter:=0;
  364. aktmaxfpuregisters:=-1;
  365. { reset the unit or create a new program }
  366. { a unit compiled at command line must be inside the loaded_unit list }
  367. if (compile_level=1) then
  368. begin
  369. if assigned(current_module) then
  370. internalerror(200501158);
  371. current_module:=tppumodule.create(nil,filename,'',false);
  372. addloadedunit(current_module);
  373. main_module:=current_module;
  374. current_module.state:=ms_compile;
  375. end;
  376. if not(assigned(current_module) and
  377. (current_module.state in [ms_compile,ms_second_compile])) then
  378. internalerror(200212281);
  379. { Set the module to use for verbose }
  380. compiled_module:=current_module;
  381. SetCompileModule(current_module);
  382. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  383. { Load current state from the init values }
  384. aktlocalswitches:=initlocalswitches;
  385. aktmoduleswitches:=initmoduleswitches;
  386. aktmodeswitches:=initmodeswitches;
  387. aktoptimizerswitches:=initoptimizerswitches;
  388. {$IFDEF Testvarsets}
  389. aktsetalloc:=initsetalloc;
  390. {$ENDIF}
  391. aktalignment:=initalignment;
  392. aktfputype:=initfputype;
  393. aktpackenum:=initpackenum;
  394. aktpackrecords:=0;
  395. aktcputype:=initcputype;
  396. aktoptimizecputype:=initoptimizecputype;
  397. aktasmmode:=initasmmode;
  398. aktinterfacetype:=initinterfacetype;
  399. { startup scanner and load the first file }
  400. current_scanner:=tscannerfile.Create(filename);
  401. current_scanner.firstfile;
  402. current_module.scanner:=current_scanner;
  403. { init macros before anything in the file is parsed.}
  404. current_module.localmacrosymtable:= tmacrosymtable.create(false);
  405. macrosymtablestack.push(initialmacrosymtable);
  406. macrosymtablestack.push(current_module.localmacrosymtable);
  407. { read the first token }
  408. current_scanner.readtoken(false);
  409. { init code generator for a new module }
  410. init_module;
  411. { If the compile level > 1 we get a nice "unit expected" error
  412. message if we are trying to use a program as unit.}
  413. try
  414. try
  415. if (token=_UNIT) or (compile_level>1) then
  416. begin
  417. current_module.is_unit:=true;
  418. proc_unit;
  419. end
  420. else
  421. proc_program(token=_LIBRARY);
  422. except
  423. on ECompilerAbort do
  424. raise;
  425. on Exception do
  426. begin
  427. { Increase errorcounter to prevent some
  428. checks during cleanup }
  429. inc(status.errorcount);
  430. raise;
  431. end;
  432. end;
  433. finally
  434. { restore old state }
  435. done_module;
  436. if assigned(current_module) then
  437. begin
  438. { module is now compiled }
  439. tppumodule(current_module).state:=ms_compiled;
  440. { free ppu }
  441. if assigned(tppumodule(current_module).ppufile) then
  442. begin
  443. tppumodule(current_module).ppufile.free;
  444. tppumodule(current_module).ppufile:=nil;
  445. end;
  446. { free scanner }
  447. if assigned(current_module.scanner) then
  448. begin
  449. if current_scanner=tscannerfile(current_module.scanner) then
  450. current_scanner:=nil;
  451. tscannerfile(current_module.scanner).free;
  452. current_module.scanner:=nil;
  453. end;
  454. { free symtable stack }
  455. if assigned(symtablestack) then
  456. begin
  457. symtablestack.free;
  458. symtablestack:=nil;
  459. end;
  460. if assigned(macrosymtablestack) then
  461. begin
  462. macrosymtablestack.free;
  463. macrosymtablestack:=nil;
  464. end;
  465. end;
  466. with olddata^ do
  467. begin
  468. { restore scanner }
  469. c:=oldc;
  470. pattern:=oldpattern;
  471. orgpattern:=oldorgpattern;
  472. token:=oldtoken;
  473. idtoken:=oldidtoken;
  474. akttokenpos:=oldtokenpos;
  475. block_type:=old_block_type;
  476. { restore cg }
  477. parse_only:=oldparse_only;
  478. { restore asmlists }
  479. exprasmlist:=oldexprasmlist;
  480. asmlist:=oldasmlist;
  481. { object data }
  482. resourcestrings:=oldresourcestrings;
  483. objectlibrary:=oldobjectlibrary;
  484. { restore previous scanner }
  485. if assigned(old_compiled_module) then
  486. current_scanner:=tscannerfile(old_compiled_module.scanner)
  487. else
  488. current_scanner:=nil;
  489. if assigned(current_scanner) then
  490. parser_current_file:=current_scanner.inputfile.name^;
  491. { restore symtable state }
  492. symtablestack:=oldsymtablestack;
  493. macrosymtablestack:=oldmacrosymtablestack;
  494. aktdefproccall:=oldaktdefproccall;
  495. current_procinfo:=oldcurrent_procinfo;
  496. aktsourcecodepage:=oldsourcecodepage;
  497. aktlocalswitches:=oldaktlocalswitches;
  498. aktmoduleswitches:=oldaktmoduleswitches;
  499. aktalignment:=oldaktalignment;
  500. aktpackenum:=oldaktpackenum;
  501. aktpackrecords:=oldaktpackrecords;
  502. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  503. aktcputype:=oldaktcputype;
  504. aktoptimizecputype:=oldaktoptimizecputype;
  505. aktfputype:=oldaktfputype;
  506. aktasmmode:=oldaktasmmode;
  507. aktinterfacetype:=oldaktinterfacetype;
  508. aktfilepos:=oldaktfilepos;
  509. aktmodeswitches:=oldaktmodeswitches;
  510. aktoptimizerswitches:=oldaktoptimizerswitches;
  511. aktexceptblock:=0;
  512. exceptblockcounter:=0;
  513. end;
  514. { Shut down things when the last file is compiled succesfull }
  515. if (compile_level=1) and
  516. (status.errorcount=0) then
  517. begin
  518. parser_current_file:='';
  519. { Close script }
  520. if (not AsmRes.Empty) then
  521. begin
  522. Message1(exec_i_closing_script,AsmRes.Fn);
  523. AsmRes.WriteToDisk;
  524. end;
  525. { do not create browsers on errors !! }
  526. if status.errorcount=0 then
  527. begin
  528. {$ifdef BrowserLog}
  529. { Write Browser Log }
  530. if (cs_browser_log in aktglobalswitches) and
  531. (cs_browser in aktmoduleswitches) then
  532. begin
  533. if browserlog.elements_to_list.empty then
  534. begin
  535. Message1(parser_i_writing_browser_log,browserlog.Fname);
  536. WriteBrowserLog;
  537. end
  538. else
  539. browserlog.list_elements;
  540. end;
  541. {$endif BrowserLog}
  542. { Write Browser Collections, also used by the TextMode IDE to
  543. retrieve a list of sourcefiles }
  544. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  545. end;
  546. end;
  547. dec(compile_level);
  548. compiled_module:=olddata^.old_compiled_module;
  549. SetCompileModule(compiled_module);
  550. dispose(olddata);
  551. end;
  552. end;
  553. end.