parser.pas 19 KB

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