parser.pas 20 KB

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