{ $Id$ Copyright (c) 1998-2002 by Florian Klaempfl This unit does the parsing process This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **************************************************************************** } unit parser; {$i fpcdefs.inc} interface {$ifdef PREPROCWRITE} procedure preprocess(const filename:string); {$endif PREPROCWRITE} procedure compile(const filename:string); procedure initparser; procedure doneparser; implementation uses cutils,cclasses, globtype,version,tokens,systems,globals,verbose, symbase,symtable,symsym, finput,fmodule,fppu, aasmbase,aasmtai, cgbase, script,gendef, {$ifdef BrowserLog} browlog, {$endif BrowserLog} {$ifdef GDB} gdb, {$endif GDB} comphook, scanner,scandir, pbase,ptype,psystem,pmodules,psub, cresstr,cpuinfo,procinfo; procedure initparser; begin { ^M means a string or a char, because we don't parse a } { type declaration } ignore_equal:=false; { we didn't parse a object or class declaration } { and no function header } testcurobject:=0; { Current compiled module/proc } objectlibrary:=nil; current_module:=nil; compiled_module:=nil; current_procinfo:=nil; loaded_units:=TLinkedList.Create; usedunits:=TLinkedList.Create; { global switches } aktglobalswitches:=initglobalswitches; aktsourcecodepage:=initsourcecodepage; { initialize scanner } InitScanner; InitScannerDirectives; { scanner } c:=#0; pattern:=''; orgpattern:=''; current_scanner:=nil; { register all nodes and tais } registernodes; registertais; { memory sizes } if stacksize=0 then stacksize:=target_info.stacksize; { open assembler response } if cs_link_on_target in aktglobalswitches then GenerateAsmRes(outputexedir+inputfile+'_ppas') else GenerateAsmRes(outputexedir+'ppas'); { open deffile } DefFile:=TDefFile.Create(outputexedir+inputfile+target_info.defext); { list of generated .o files, so the linker can remove them } SmartLinkOFiles:=TStringList.Create; { codegen } if paraprintnodetree<>0 then printnode_reset; { target specific stuff } case target_info.system of system_powerpc_morphos: include(supported_calling_conventions,pocall_syscall); system_m68k_amiga: include(supported_calling_conventions,pocall_syscall); end; end; procedure doneparser; begin { Reset current compiling info, so destroy routines can't reference the data that might already be destroyed } objectlibrary:=nil; current_module:=nil; compiled_module:=nil; current_procinfo:=nil; { unload units } loaded_units.free; usedunits.free; { if there was an error in the scanner, the scanner is still assinged } if assigned(current_scanner) then begin current_scanner.free; current_scanner:=nil; end; { close scanner } DoneScanner; { close ppas,deffile } asmres.free; deffile.free; { free list of .o files } SmartLinkOFiles.Free; end; {$ifdef PREPROCWRITE} procedure preprocess(const filename:string); var i : longint; begin new(preprocfile,init('pre')); { initialize a module } current_module:=new(pmodule,init(filename,false)); macrosymtablestack:= initialmacrosymtable; current_module.localmacrosymtable:= tmacrosymtable.create(false); current_module.localmacrosymtable.next:= initialmacrosymtable; macrosymtablestack:= current_module.localmacrosymtable; ConsolidateMode; main_module:=current_module; { startup scanner, and save in current_module } current_scanner:=new(pscannerfile,Init(filename)); current_module.scanner:=current_scanner; { loop until EOF is found } repeat current_scanner^.readtoken; preprocfile^.AddSpace; case token of _ID : begin preprocfile^.Add(orgpattern); end; _REALNUMBER, _INTCONST : preprocfile^.Add(pattern); _CSTRING : begin i:=0; while (i 1 we get a nice "unit expected" error message if we are trying to use a program as unit.} try if (token=_UNIT) or (compile_level>1) then begin current_module.is_unit:=true; proc_unit; end else proc_program(token=_LIBRARY); finally { restore old state } done_module; if assigned(current_module) then begin { module is now compiled } tppumodule(current_module).state:=ms_compiled; { free ppu } if assigned(tppumodule(current_module).ppufile) then begin tppumodule(current_module).ppufile.free; tppumodule(current_module).ppufile:=nil; end; { free scanner } if assigned(current_module.scanner) then begin if current_scanner=tscannerfile(current_module.scanner) then current_scanner:=nil; tscannerfile(current_module.scanner).free; current_module.scanner:=nil; end; end; if (compile_level>1) then begin with olddata^ do begin { restore scanner } c:=oldc; pattern:=oldpattern; orgpattern:=oldorgpattern; token:=oldtoken; idtoken:=oldidtoken; akttokenpos:=oldtokenpos; block_type:=old_block_type; { restore cg } parse_only:=oldparse_only; { restore asmlists } exprasmlist:=oldexprasmlist; datasegment:=olddatasegment; bsssegment:=oldbsssegment; codesegment:=oldcodesegment; consts:=oldconsts; debuglist:=olddebuglist; withdebuglist:=oldwithdebuglist; importssection:=oldimports; exportssection:=oldexports; resourcesection:=oldresource; rttilist:=oldrttilist; picdata:=oldpicdata; resourcestringlist:=oldresourcestringlist; { object data } ResourceStrings:=OldResourceStrings; objectlibrary:=oldobjectlibrary; { restore previous scanner } if assigned(old_compiled_module) then current_scanner:=tscannerfile(old_compiled_module.scanner) else current_scanner:=nil; if assigned(current_scanner) then parser_current_file:=current_scanner.inputfile.name^; { restore symtable state } refsymtable:=oldrefsymtable; symtablestack:=oldsymtablestack; macrosymtablestack:=oldmacrosymtablestack; defaultsymtablestack:=olddefaultsymtablestack; defaultmacrosymtablestack:=olddefaultmacrosymtablestack; aktdefproccall:=oldaktdefproccall; current_procinfo:=oldcurrent_procinfo; aktsourcecodepage:=oldsourcecodepage; aktlocalswitches:=oldaktlocalswitches; aktmoduleswitches:=oldaktmoduleswitches; aktalignment:=oldaktalignment; aktpackenum:=oldaktpackenum; aktpackrecords:=oldaktpackrecords; aktmaxfpuregisters:=oldaktmaxfpuregisters; aktoutputformat:=oldaktoutputformat; set_target_asm(aktoutputformat); aktoptprocessor:=oldaktoptprocessor; aktspecificoptprocessor:=oldaktspecificoptprocessor; aktfputype:=oldaktfputype; aktasmmode:=oldaktasmmode; aktinterfacetype:=oldaktinterfacetype; aktfilepos:=oldaktfilepos; aktmodeswitches:=oldaktmodeswitches; aktexceptblock:=0; exceptblockcounter:=0; {$ifdef GDB} dbx_counter:=store_dbx; {$endif GDB} end; end else begin { Shut down things when the last file is compiled succesfull } if (compile_level=1) and (status.errorcount=0) then begin parser_current_file:=''; { Close script } if (not AsmRes.Empty) then begin Message1(exec_i_closing_script,AsmRes.Fn); AsmRes.WriteToDisk; end; { do not create browsers on errors !! } if status.errorcount=0 then begin {$ifdef BrowserLog} { Write Browser Log } if (cs_browser_log in aktglobalswitches) and (cs_browser in aktmoduleswitches) then begin if browserlog.elements_to_list.empty then begin Message1(parser_i_writing_browser_log,browserlog.Fname); WriteBrowserLog; end else browserlog.list_elements; end; { Write Browser Collections } do_extractsymbolinfo{$ifdef FPC}(){$endif}; {$endif BrowserLog} end; end; end; dec(compile_level); compiled_module:=olddata^.old_compiled_module; SetCompileModule(compiled_module); dispose(olddata); end; end; end. { $Log$ Revision 1.72 2005-01-29 11:36:52 peter * update x86_64 with new cpupara Revision 1.71 2005/01/26 16:23:28 peter * detect arithmetic overflows for constants at compile time * use try..except instead of setjmp Revision 1.70 2005/01/19 22:19:41 peter * unit mapping rewrite * new derefmap added Revision 1.69 2005/01/09 20:24:43 olle * rework of macro subsystem + exportable macros for mode macpas Revision 1.68 2004/10/25 15:38:41 peter * heap and heapsize removed * checkpointer fixes Revision 1.67 2004/10/15 09:14:17 mazen - remove $IFDEF DELPHI and related code - remove $IFDEF FPCPROCVAR and related code Revision 1.66 2004/06/20 08:55:30 florian * logs truncated Revision 1.65 2004/05/12 13:21:09 karoly * few small changes to add syscall support to M68k/Amiga target Revision 1.64 2004/04/28 15:19:03 florian + syscall directive support for MorphOS added Revision 1.63 2004/03/16 16:20:49 peter * reset current_module,current_procinfo so the destroy routines can't access their info anymore, because that can be already destroyed Revision 1.62 2004/03/14 20:08:37 peter * packrecords fixed for settings from $PACKRECORDS * default packrecords now uses value 0 and uses info from aligment structure only, initpackrecords removed Revision 1.61 2004/03/02 17:32:12 florian * make cycle fixed + pic support for darwin + support of importing vars from shared libs on darwin implemented }