parser.pas 21 KB

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