treelang.texi
来自「理解和实践操作系统的一本好书」· TEXI 代码 · 共 1,311 行 · 第 1/3 页
TEXI
1,311 行
indicate variable is permanent, or function has file scope only@item automaticindicate that variable is allocated for the life of the current scope@item external_referenceindicate that variable or function is defined in another file@item external_definitionindicate that variable or function is to be accessible from other files@item intvariable is an integer (same as C int) @item charvariable is a character (same as C char)@item unsignedvariable is unsigned. If this is not present, the variable is signed@item returnstart function return statement@item voidused as function type to indicate function returns nothing@end itemize@itemNames consist of any letter or "_" followed by any number of letters,numbers, or "_". "$" is not allowed in a name. All names must be globallyunique, i.e. may not be used twice in any context, and mustnot be a keyword. Names and keywords are case sensitive. For example:@smallexamplea A _a a_ IF_X@end smallexampleare all different names.@end itemize@node Parsing Syntax, Compiler Overview, Lexical Syntax, Top@chapter Parsing Syntax@cindex Parsing SyntaxDeclarations are built up from the lexical elements described above. Afile may contain one of more declarations.@itemize @bullet@itemdeclaration: variable declaration OR function prototype OR function declaration@itemFunction Prototype: storage type NAME ( optional_parameter_list )@smallexamplestatic int add (int a, int b)@end smallexample@itemvariable_declaration: storage type NAME initial;Example:@smallexampleint temp1 = 1;@end smallexampleA variable declaration can be outside a function, or at the start of afunction.@itemstorage: automatic OR static OR external_reference OR external_definitionThis defines the scope, duration and visibility of a function or variable@enumerate 1@item automatic: This means a variable is allocated at start of the current scope andreleased when the current scope is exited. This can only be used for variableswithin functions. It cannot be used for functions.@item static: This means a variable is allocated at start of program andremains allocated until the program as a whole ends. For a function, itmeans that the function is only visible within the current file.@itemexternal_definition: For a variable, which must be defined outside afunction, it means that the variable is visible from other files. For afunction, it means that the function is visible from another file.@itemexternal_reference: For a variable, which must be defined outside afunction, it means that the variable is defined in another file. For afunction, it means that the function is defined in another file.@end enumerate@itemtype: int OR unsigned int OR char OR unsigned char OR voidThis defines the data type of a variable or the return type of a function.@enumerate a@item int: The variable is a signed integer. The function returns a signed integer.@item unsigned int: The variable is an unsigned integer. The function returns an unsigned integer.@item char: The variable is a signed character. The function returns a signed character.@item unsigned char: The variable is an unsigned character. The function returns an unsigned character.@end enumerate@itemparameter_list OR parameter [, parameter]...@itemparameter: variable_declaration ,The variable declarations must not have initializations.@item initial: = value@itemvalue: integer_constantValues without a unary plus or minus are considered to be unsigned.@smallexamplee.g.@: 1 +2 -3@end smallexample@itemfunction_declaration: name @{ variable_declarations statements @}A function consists of the function name then the declarations (if any)and statements (if any) within one pair of braces.The details of the function arguments come from the functionprototype. The function prototype must precede the function declarationin the file.@itemstatement: if_statement OR expression_statement OR return_statement@itemif_statement: if ( expression ) @{ variable_declarations statements @}else @{ variable_declarations statements @}The first lot of statements is executed if the expression isnonzero. Otherwise the second lot of statements is executed. Eitherlist of statements may be empty, but both sets of braces and the else must be present. @smallexampleif (a==b) @{// @r{nothing}@}else@{a=b;@}@end smallexample@itemexpression_statement: expression;The expression is executed, including any side effects.@itemreturn_statement: return expression_opt;Returns from the function. If the function is void, the expression mustbe absent, and if the function is not void the expression must bepresent.@itemexpression: variable OR integer_constant OR expression + expressionOR expression - expression OR expression == expression OR ( expression )OR variable = expression OR function_callAn expression can be a constant or a variable reference or afunction_call. Expressions can be combined as a sum of two expressionsor the difference of two expressions, or an equality test of twoexpressions. An assignment is also an expression. Expressions and operatorprecedence work as in C.@itemfunction_call: function_name ( optional_comma_separated_expressions )This invokes the function, passing to it the values of the expressionsas actual parameters.@end itemize@cindex compilers@node Compiler Overview, TREELANG and GCC, Parsing Syntax, Top@chapter Compiler Overviewtreelang is run as part of the GCC compiler. @itemize @bullet@cindex source code@cindex file, source@cindex code, source@cindex source file@itemIt reads a user's program, stored in a file and containing instructionswritten in the appropriate language (Treelang, C, and so on). This filecontains @dfn{source code}.@cindex translation of user programs@cindex machine code@cindex code, machine@cindex mistakes@itemIt translates the user's program into instructions a computer can carryout more quickly than it takes to translate the instructions in thefirst place. These instructions are called @dfn{machine code}---codedesigned to be efficiently translated and processed by a machine such asa computer. Humans usually aren't as good writing machine code as theyare at writing Treelang or C, because it is easy to make tiny mistakeswriting machine code. When writing Treelang or C, it is easy to makebig mistakes. But you can only make one mistake, because the compilerstops after it finds any problem.@cindex debugger@cindex bugs, finding@cindex @code{gdb}, command@cindex commands, @code{gdb}@itemIt provides information in the generated machine codethat can make it easier to find bugs in the program(using a debugging tool, called a @dfn{debugger},such as @code{gdb}).@cindex libraries@cindex linking@cindex @code{ld} command@cindex commands, @code{ld}@itemIt locates and gathers machine code already generated to perform actionsrequested by statements in the user's program. This machine code isorganized into @dfn{libraries} and is located and gathered during the@dfn{link} phase of the compilation process. (Linking often is thoughtof as a separate step, because it can be directly invoked via the@code{ld} command. However, the @code{gcc} command, as with mostcompiler commands, automatically performs the linking step by calling on@code{ld} directly, unless asked to not do so by the user.)@cindex language, incorrect use of@cindex incorrect use of language@itemIt attempts to diagnose cases where the user's program containsincorrect usages of the language. The @dfn{diagnostics} produced by thecompiler indicate the problem and the location in the user's source filewhere the problem was first noticed. The user can use this informationto locate and fix the problem.The compiler stops after the first error. There are no plans to fixthis, ever, as it would vastly complicate the implementation of treelangto little or no benefit.@cindex diagnostics, incorrect@cindex incorrect diagnostics@cindex error messages, incorrect@cindex incorrect error messages(Sometimes an incorrect usage of the language leads to a situation wherethe compiler can not make any sense of what it reads---while a humanmight be able to---and thus ends up complaining about an incorrect``problem'' it encounters that, in fact, reflects a misunderstanding ofthe programmer's intention.)@cindex warnings@cindex questionable instructions@itemThere are a few warnings in treelang. For example an unused static functiongenerate a warnings when -Wunused-function is specified, similarly an unusedstatic variable generates a warning when -Wunused-variable are specified.The only treelang specific warning is a warning when an expression is in areturn statement for functions that return void.@end itemize@cindex components of treelang@cindex @code{treelang}, components of@code{treelang} consists of several components:@cindex @code{gcc}, command@cindex commands, @code{gcc}@itemize @bullet@itemA modified version of the @code{gcc} command, which also might beinstalled as the system's @code{cc} command.(In many cases, @code{cc} refers to thesystem's ``native'' C compiler, whichmight be a non-GNU compiler, or an older versionof @code{GCC} considered more stable or that isused to build the operating system kernel.)@cindex @code{treelang}, command@cindex commands, @code{treelang}@itemThe @code{treelang} command itself.@itemThe @code{libc} run-time library. This library contains the machinecode needed to support capabilities of the Treelang language that arenot directly provided by the machine code generated by the@code{treelang} compilation phase. This is the same library that themain C compiler uses (libc).@cindex @code{tree1}, program@cindex programs, @code{tree1}@cindex assembler@cindex @code{as} command@cindex commands, @code{as}@cindex assembly code@cindex code, assembly@itemThe compiler itself, is internally named @code{tree1}.Note that @code{tree1} does not generate machine code directly---itgenerates @dfn{assembly code} that is a more readable formof machine code, leaving the conversion to actual machine codeto an @dfn{assembler}, usually named @code{as}.@end itemize@code{GCC} is often thought of as ``the C compiler'' only,but it does more than that.Based on command-line options and the names given for fileson the command line, @code{gcc} determines which actions to perform, includingpreprocessing, compiling (in a variety of possible languages), assembling,and linking.@cindex driver, gcc command as@cindex @code{gcc}, command as driver@cindex executable file@cindex files, executable@cindex cc1 program@cindex programs, cc1@cindex preprocessor@cindex cpp program@cindex programs, cppFor example, the command @samp{gcc foo.c} @dfn{drives} the file@file{foo.c} through the preprocessor @code{cpp}, thenthe C compiler (internally named@code{cc1}), then the assembler (usually @code{as}), then the linker(@code{ld}), producing an executable program named @file{a.out} (onUNIX systems).@cindex treelang program@cindex programs, treelangAs another example, the command @samp{gcc foo.tree} would do much thesame as @samp{gcc foo.c}, but instead of using the C compiler named@code{cc1}, @code{gcc} would use the treelang compiler (named@code{tree1}). However there is no preprocessor for treelang.@cindex @code{tree1}, program@cindex programs, @code{tree1}In a GNU Treelang installation, @code{gcc} recognizes Treelang sourcefiles by name just like it does C and C++ source files. It knows to usethe Treelang compiler named @code{tree1}, instead of @code{cc1} or@code{cc1plus}, to compile Treelang files. If a file's name ends in@code{.tree} then GCC knows that the program is written in treelang. Youcan also manually override the language.@cindex @code{gcc}, not recognizing Treelang source@cindex unrecognized file format@cindex file format not recognizedNon-Treelang-related operation of @code{gcc} is generallyunaffected by installing the GNU Treelang version of @code{gcc}.However, without the installed version of @code{gcc} being theGNU Treelang version, @code{gcc} will not be able to compileand link Treelang programs.@cindex printing version information@cindex version information, printingThe command @samp{gcc -v x.tree} where @samp{x.tree} is a file whichmust exist but whose contents are ignored, is a quick way to displayversion information for the various programs used to compile a typicalTreelang source file. The @code{tree1} program represents most of what is unique to GNUTreelang; @code{tree1} is a combination of two rather large chunks ofcode.@cindex GCC Back End (GBE)@cindex GBE@cindex @code{GCC}, back end@cindex back end, GCC@cindex code generatorOne chunk is the so-called @dfn{GNU Back End}, or GBE,which knows how to generate fast code for a wide variety of processors.The same GBE is used by the C, C++, and Treelang compiler programs @code{cc1},@code{cc1plus}, and @code{tree1}, plus others.Often the GBE is referred to as the ``GCC back end'' oreven just ``GCC''---in this manual, the term GBE is usedwhenever the distinction is important.@cindex GNU Treelang Front End (TFE)@cindex tree1@cindex @code{treelang}, front end@cindex front end, @code{treelang}The other chunk of @code{tree1} is the majority of what is unique aboutGNU Treelang---the code that knows how to interpret Treelang programs todetermine what they are intending to do, and then communicate thatknowledge to the GBE for actual compilation of those programs. Thischunk is called the @dfn{Treelang Front End} (TFE). The @code{cc1} and@code{cc1plus} programs have their own front ends, for the C and C++languages, respectively. These fronts ends are responsible fordiagnosing incorrect usage of their respective languages by the programsthe process, and are responsible for most of the warnings aboutquestionable constructs as well. (The GBE in principle handlesproducing some warnings, like those concerning possible references toundefined variables, but these warnings should not occur in treelangprograms as the front end is meant to pick them up first).Because so much is shared among the compilers for various languages,much of the behavior and many of the user-selectable options for thesecompilers are similar.For example, diagnostics (error messages andwarnings) are similar in appearance; command-lineoptions like @samp{-Wall} have generally similar effects; and the qualityof generated code (in terms of speed and size) is roughly similar(since that work is done by the shared GBE).@node TREELANG and GCC, Compiler, Compiler Overview, Top@chapter Compile Treelang, C, or Other Programs@cindex compiling programs@cindex programs, compiling@cindex @code{gcc}, command@cindex commands, @code{gcc}A GNU Treelang installation includes a modified version of the @code{gcc}command.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?