treelang.texi

来自「理解和实践操作系统的一本好书」· TEXI 代码 · 共 1,311 行 · 第 1/3 页

TEXI
1,311
字号
In a non-Treelang installation, @code{gcc} recognizes C, C++,and Objective-C source files.In a GNU Treelang installation, @code{gcc} also recognizes Treelang sourcefiles and accepts Treelang-specific command-line options, plus somecommand-line options that are designed to cater to Treelang usersbut apply to other languages as well.@xref{G++ and GCC,,Programming Languages Supported by GCC,GCC,Usingthe GNU Compiler Collection (GCC)},for information on the way different languages are handledby the GCC compiler (@code{gcc}).You can use this, combined with the output of the @samp{gcc -v x.tree}command to get the options applicable to treelang.  Treelang programsmust end with the suffix @samp{.tree}.@cindex preprocessorTreelang programs are not by default run through the Cpreprocessor by @code{gcc}. There is no reason why they cannot be run through thepreprocessor manually, but you would need to prevent the preprocessorfrom generating #line directives, using the @samp{-P} option, otherwisetree1 will not accept the input.@node Compiler, Other Languages, TREELANG and GCC, Top@chapter The GNU Treelang CompilerThe GNU Treelang compiler, @code{treelang}, supports programs writtenin the GNU Treelang language.@node Other Languages, treelang internals, Compiler, Top@chapter Other Languages@menu* Interoperating with C and C++::  @end menu@node Interoperating with C and C++,  , Other Languages, Other Languages@section Tools and advice for interoperating with C and C++The output of treelang programs looks like C program code to the linkerand everybody else, so you should be able to freely mix treelang and C(and C++) code, with one proviso.C promotes small integer types to 'int' when used as function parameters and return values in non-prototyped functions.  Since treelang has nonon-prototyped functions, the treelang compiler does not do this.@ifset INTERNALS@node treelang internals, Open Questions, Other Languages, Top@chapter treelang internals@menu* treelang files::              * treelang compiler interfaces::  * Hints and tips::              @end menu@node treelang files, treelang compiler interfaces, treelang internals, treelang internals@section treelang filesTo create a compiler that integrates into GCC, you need create manyfiles.  Some of the files are integrated into the main GCC makefile, tobuild the various parts of the compiler and to run the testsuite.  Others are incorporated into various GCC programs such as@file{gcc.c}.  Finally you must provide the actual programs comprising yourcompiler. @cindex filesThe files are:@enumerate 1@itemCOPYING.  This is the copyright file, assuming you are going to use theGNU General Public License.  You probably need to use the GPL because ifyou use the GCC back end your program and the back end are one program,and the back end is GPLed.This need not be present if the language is incorporated into the mainGCC tree, as the main GCC directory has this file. @itemCOPYING.LIB.  This is the copyright file for those parts of your programthat are not to be covered by the GPL, but are instead to be covered bythe LGPL (Library or Lesser GPL).  This license may be appropriate forthe library routines associated with your compiler. These are theroutines that are linked with the @emph{output} of the compiler.  Usingthe LGPL for these programs allows programs written using your compilerto be closed source. For example LIBC is under the LGPL. This need not be present if the language is incorporated into the mainGCC tree, as the main GCC directory has this file. @itemChangeLog.  Record all the changes to your compiler.  Use the same formatas used in treelang as it is supported by an emacs editing mode and ispart of the FSF coding standard.  Normally each directory has its ownchangelog.  The FSF standard allows but does not require a meaningfulcomment on why the changes were made, above and beyond @emph{why} theywere made.  In the author's opinion it is useful to provide thisinformation.@itemtreelang.texi.  The manual, written in texinfo. Your manual would have adifferent file name.  You need not write it in texinfo if you don't wantdo, but a lot of GNU software does use texinfo. @cindex Make-lang.in@itemMake-lang.in.  This file is part of the make file which in incorporatedwith the GCC make file skeleton (Makefile.in in the GCC directory) tomake Makefile, as part of the configuration process.Makefile in turn is the main instruction to actually buildeverything.  The build instructions are held in the main GCC manual andweb site so they are not repeated here. There are some comments at the top which will help you understand whatyou need to do.There are make commands to build things, remove generated files withvarious degrees of thoroughness, count the lines of code (so you knowhow much progress you are making), build info and html files from thetexinfo source, run the tests etc.@itemREADME.  Just a brief informative text file saying what is in thisdirectory. @cindex config-lang.in@itemconfig-lang.in.  This file is read by the configuration progress and mustbe present. You specify the name of your language, the name(s) of thecompiler(s) including preprocessors you are going to build, whether any,usually generated, files should be excluded from diffs (ie when makingdiff files to send in patches).  Whether the equate 'stagestuff' is usedis unknown (???).@cindex lang.opt@itemlang.opt.  This file is included into @file{gcc.c}, the main GCC driver, andtells it what options your language supports.  This is also used todisplay help.@cindex lang-specs.h@itemlang-specs.h.  This file is also included in @file{gcc.c}. It tells@file{gcc.c} when to call your programs and what options to send them.  Themini-language 'specs' is documented in the source of @file{gcc.c}.  Do notattempt to write a specs file from scratch - use an existing one as the baseand enhance it. @itemYour texi files.  Texinfo can be used to build documentation in HTML,info, dvi and postscript formats. It is a tagged language, is documentedin its own manual, and has its own emacs mode.@itemYour programs.  The relationships between all the programs are explainedin the next section.  You need to write or use the following programs:@itemize @bullet@itemlexer.  This breaks the input into words and passes these to theparser.  This is @file{lex.l} in treelang, which is passed through flex, a lexvariant, to produce C code @file{lex.c}.  Note there is a school of thoughtthat says real men hand code their own lexers.  However, you may prefer towrite far less code and use flex, as was done with treelang.@itemparser.  This breaks the program into recognizable constructs such asexpressions, statements etc.  This is @file{parse.y} in treelang, which ispassed through bison, which is a yacc variant, to produce C code@file{parse.c}.@itemback end interface.  This interfaces to the code generation back end.  Intreelang, this is @file{tree1.c} which mainly interfaces to @file{toplev.c} and@file{treetree.c} which mainly interfaces to everything else. Many languagesmix up the back end interface with the parser, as in the C compiler forexample.  It is a matter of taste which way to do it, but with treelangit is separated out to make the back end interface cleaner and easier tounderstand.@itemheader files.  For function prototypes and common data items.  One pointto note here is that bison can generate a header files with all thenumbers is has assigned to the keywords and symbols, and you can includethe same header in your lexer.  This technique is demonstrated intreelang.@itemcompiler main file.  GCC comes with a file @file{toplev.c} which is aperfectly serviceable main program for your compiler.  GNU Treelang uses@file{toplev.c} but other languages have been known to replace it with theirown main program.  Again this is a matter of taste and how much code youwant to write. @end itemize@end enumerate@node treelang compiler interfaces, Hints and tips, treelang files, treelang internals@section treelang compiler interfaces@cindex driver@cindex toplev.c@menu* treelang driver::             * treelang main compiler::      @end menu@node treelang driver, treelang main compiler, treelang compiler interfaces, treelang compiler interfaces@subsection treelang driverThe GCC compiler consists of a driver, which then executes the variouscompiler phases based on the instructions in the specs files. Typically a program's language will be identified from its suffix(e.g., @file{.tree}) for treelang programs.The driver (@file{gcc.c}) will then drive (exec) in turn a preprocessor,the main compiler, the assembler and the link editor. Options to GCC allow youto override all of this. In the case of treelang programs there is nopreprocessor, and mostly these days the C preprocessor is run within themain C compiler rather than as a separate process, apparently for reasons of speed.You will be using the standard assembler and linkage editor so these areignored from now on. You have to write your own preprocessor if you want one.  This is usuallytotally language specific.  The main point to be aware of is to ensurethat you find some way to pass file name and line number informationthrough to the main compiler so that it can tell the back end thisinformation and so the debugger can find the right source line for eachpiece of code.  That is all there is to say about the preprocessor exceptthat the preprocessor will probably not be the slowest part of thecompiler and will probably not use the most memory so don't waste toomuch time tuning it until you know you need to do so.@node treelang main compiler,  , treelang driver, treelang compiler interfaces@subsection treelang main compilerThe main compiler for treelang consists of @file{toplev.c} from the main GCCcompiler, the parser, lexer and back end interface routines, and theback end routines themselves, of which there are many.@file{toplev.c} does a lot of work for you and you should almost certainlyuse it.Writing this code is the hard part of creating a compiler using GCC.  Theback end interface documentation is incomplete and the interface iscomplex. There are three main aspects to interfacing to the other GCC code. @menu* Interfacing to toplev.c::     * Interfacing to the garbage collection::  * Interfacing to the code generation code. ::  @end menu@node Interfacing to toplev.c, Interfacing to the garbage collection, treelang main compiler, treelang main compiler@subsubsection Interfacing to toplev.cIn treelang this is handled mainly in tree1.cand partly in treetree.c. Peruse toplev.c for details of what you needto do.@node Interfacing to the garbage collection, Interfacing to the code generation code. , Interfacing to toplev.c, treelang main compiler@subsubsection Interfacing to the garbage collectionInterfacing to the garbage collection. In treelang this is mainly intree1.c. Memory allocation in the compiler should be done using the ggc_alloc andkindred routines in ggc*.*. At the end of every 'function' in your language, toplev.c callsthe garbage collection several times. The garbage collection calls markroutines which go through the memory which is still used, telling thegarbage collection not to free it. Then all the memory not used isfreed.What this means is that you need a way to hook into this markingprocess. This is done by calling ggc_add_root. This provides the addressof a callback routine which will be called duing garbage collection andwhich can call ggc_mark to save the storage. If storage is onlyused within the parsing of a function, you do not need to provide a wayto mark it. Note that you can also call ggc_mark_tree to mark any of the back endinternal 'tree' nodes. This routine will follow the branches of thetrees and mark all the subordinate structures. This is useful forexample when you have created a variable declaration that will be usedacross multiple functions, or for a function declaration (from aprototype) that may be used later on. See the next item for more on thetree nodes. @node Interfacing to the code generation code. ,  , Interfacing to the garbage collection, treelang main compiler@subsubsection Interfacing to the code generation code. In treelang this is done in treetree.c. A typedef called 'tree' which isdefined in tree.h and tree.def in the GCC directory and largelyimplemented in tree.c and stmt.c forms the basic interface to thecompiler back end.In general you call various tree routines to generate code, eitherdirectly or through toplev.c. You build up data structures andexpressions in similar ways. You can read some documentation on this which can be found via the GCCmain web page. In particular, the documentation produced by JoachimNadler and translated by Tim Josling can be quite useful. the C compileralso has documentation in the main GCC manual (particularly the currentCVS version) which is useful on a lot of the details.In time it is hoped to enhance this document to provide a morecomprehensive overview of this topic. The main gap is in explaining howit all works together. @node Hints and tips,  , treelang compiler interfaces, treelang internals@section Hints and tips@itemize @bullet@itemTAGS: Use the make ETAGS commands to create TAGS files which can be used inemacs to jump to any symbol quickly. @itemGREP: grep is also a useful way to find all uses of a symbol.@itemTREE: The main routines to look at are tree.h and tree.def. You willprobably want a hardcopy of these. @itemSAMPLE: look at the sample interfacing code in treetree.c. You can usegdb to trace through the code and learn about how it all works. @itemGDB: the GCC back end works well with gdb. It traps abort() and allowsyou to trace back what went wrong. @itemError Checking: The compiler back end does some error and consistencychecking. Often the result of an error is just no code beinggenerated. You will then need to trace through and find out what isgoing wrong. The rtl dump files can help here also.@itemrtl dump files: The main compiler documents these files which are dumpsof the rtl (intermediate code) which is manipulated doing the codegeneration process. This can provide useful clues about what is goingwrong. The rtl 'language' is documented in the main GCC manual.@end itemize@end ifset@node Open Questions, Bugs, treelang internals, Top@chapter Open QuestionsIf you know GCC well, please consider looking at the file treetree.c andresolving any questions marked "???".@node Bugs, Service, Open Questions, Top@chapter Reporting Bugs@cindex bugs@cindex reporting bugsYou can report bugs to @email{@value{email-bugs}}. Please makesure bugs are real before reporting them. Follow the guidelines in themain GCC manual for submitting bug reports.@menu* Sending Patches::             @end menu@node Sending Patches,  , Bugs, Bugs@section Sending Patches for GNU TreelangIf you would like to write bug fixes or improvements for the GNUTreelang compiler, that is very helpful.  Send suggested fixes to@email{@value{email-patches}}.@node Service, Projects, Bugs, Top@chapter How To Get Help with GNU TreelangIf you need help installing, using or changing GNU Treelang, there are twoways to find it:@itemize @bullet@itemLook in the service directory for someone who might help you for a fee.The service directory is found in the file named @file{SERVICE} in theGCC distribution.@itemSend a message to @email{@value{email-general}}.@end itemize@end ifset@ifset INTERNALS@node Projects, Index, Service, Top@chapter Projects@cindex projectsIf you want to contribute to @code{treelang} by doing research,design, specification, documentation, coding, or testing,the following information should give you some ideas.Send a message to @email{@value{email-general}} if you plan to add afeature.The main requirement for treelang is to add features and to adddocumentation. Features are things that the GCC back end can do butwhich are not reflected in treelang. Examples include structures,unions, pointers, arrays.@end ifset@node Index,  , Projects, Top@unnumbered Index@printindex cp@summarycontents@contents@bye

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?