parser.pas 20 KB

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