📄 md.texi
字号:
@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1996, 1998, 1999, 2000, 2001,@c 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.@c This is part of the GCC manual.@c For copying conditions, see the file gcc.texi.@ifset INTERNALS@node Machine Desc@chapter Machine Descriptions@cindex machine descriptionsA machine description has two parts: a file of instruction patterns(@file{.md} file) and a C header file of macro definitions.The @file{.md} file for a target machine contains a pattern for eachinstruction that the target machine supports (or at least each instructionthat is worth telling the compiler about). It may also contain comments.A semicolon causes the rest of the line to be a comment, unless the semicolonis inside a quoted string.See the next chapter for information on the C header file.@menu* Overview:: How the machine description is used.* Patterns:: How to write instruction patterns.* Example:: An explained example of a @code{define_insn} pattern.* RTL Template:: The RTL template defines what insns match a pattern.* Output Template:: The output template says how to make assembler code from such an insn.* Output Statement:: For more generality, write C code to output the assembler code.* Predicates:: Controlling what kinds of operands can be used for an insn.* Constraints:: Fine-tuning operand selection.* Standard Names:: Names mark patterns to use for code generation.* Pattern Ordering:: When the order of patterns makes a difference.* Dependent Patterns:: Having one pattern may make you need another.* Jump Patterns:: Special considerations for patterns for jump insns.* Looping Patterns:: How to define patterns for special looping insns.* Insn Canonicalizations::Canonicalization of Instructions* Expander Definitions::Generating a sequence of several RTL insns for a standard operation.* Insn Splitting:: Splitting Instructions into Multiple Instructions.* Including Patterns:: Including Patterns in Machine Descriptions.* Peephole Definitions::Defining machine-specific peephole optimizations.* Insn Attributes:: Specifying the value of attributes for generated insns.* Conditional Execution::Generating @code{define_insn} patterns for predication.* Constant Definitions::Defining symbolic constants that can be used in the md file.* Iterators:: Using iterators to generate patterns from a template.@end menu@node Overview@section Overview of How the Machine Description is UsedThere are three main conversions that happen in the compiler:@enumerate@itemThe front end reads the source code and builds a parse tree.@itemThe parse tree is used to generate an RTL insn list based on namedinstruction patterns.@itemThe insn list is matched against the RTL templates to produce assemblercode.@end enumerateFor the generate pass, only the names of the insns matter, from either anamed @code{define_insn} or a @code{define_expand}. The compiler willchoose the pattern with the right name and apply the operands accordingto the documentation later in this chapter, without regard for the RTLtemplate or operand constraints. Note that the names the compiler looksfor are hard-coded in the compiler---it will ignore unnamed patterns andpatterns with names it doesn't know about, but if you don't provide anamed pattern it needs, it will abort.If a @code{define_insn} is used, the template given is inserted into theinsn list. If a @code{define_expand} is used, one of three thingshappens, based on the condition logic. The condition logic may manuallycreate new insns for the insn list, say via @code{emit_insn()}, andinvoke @code{DONE}. For certain named patterns, it may invoke @code{FAIL} to tell thecompiler to use an alternate way of performing that task. If it invokesneither @code{DONE} nor @code{FAIL}, the template given in the patternis inserted, as if the @code{define_expand} were a @code{define_insn}.Once the insn list is generated, various optimization passes convert,replace, and rearrange the insns in the insn list. This is where the@code{define_split} and @code{define_peephole} patterns get used, forexample.Finally, the insn list's RTL is matched up with the RTL templates in the@code{define_insn} patterns, and those patterns are used to emit thefinal assembly code. For this purpose, each named @code{define_insn}acts like it's unnamed, since the names are ignored.@node Patterns@section Everything about Instruction Patterns@cindex patterns@cindex instruction patterns@findex define_insnEach instruction pattern contains an incomplete RTL expression, with piecesto be filled in later, operand constraints that restrict how the pieces canbe filled in, and an output pattern or C code to generate the assembleroutput, all wrapped up in a @code{define_insn} expression.A @code{define_insn} is an RTL expression containing four or five operands:@enumerate@itemAn optional name. The presence of a name indicate that this instructionpattern can perform a certain standard job for the RTL-generationpass of the compiler. This pass knows certain names and will usethe instruction patterns with those names, if the names are definedin the machine description.The absence of a name is indicated by writing an empty stringwhere the name should go. Nameless instruction patterns are neverused for generating RTL code, but they may permit several simpler insnsto be combined later on.Names that are not thus known and used in RTL-generation have noeffect; they are equivalent to no name at all.For the purpose of debugging the compiler, you may also specify aname beginning with the @samp{*} character. Such a name is used onlyfor identifying the instruction in RTL dumps; it is entirely equivalentto having a nameless pattern for all other purposes.@itemThe @dfn{RTL template} (@pxref{RTL Template}) is a vector of incompleteRTL expressions which show what the instruction should look like. It isincomplete because it may contain @code{match_operand},@code{match_operator}, and @code{match_dup} expressions that stand foroperands of the instruction.If the vector has only one element, that element is the template for theinstruction pattern. If the vector has multiple elements, then theinstruction pattern is a @code{parallel} expression containing theelements described.@item@cindex pattern conditions@cindex conditions, in patternsA condition. This is a string which contains a C expression that isthe final test to decide whether an insn body matches this pattern.@cindex named patterns and conditionsFor a named pattern, the condition (if present) may not depend onthe data in the insn being matched, but only the target-machine-typeflags. The compiler needs to test these conditions duringinitialization in order to learn exactly which named instructions areavailable in a particular run.@findex operandsFor nameless patterns, the condition is applied only when matching anindividual insn, and only after the insn has matched the pattern'srecognition template. The insn's operands may be found in the vector@code{operands}. For an insn where the condition has once matched, itcan't be used to control register allocation, for example by excludingcertain hard registers or hard register combinations.@itemThe @dfn{output template}: a string that says how to output matchinginsns as assembler code. @samp{%} in this string specifies whereto substitute the value of an operand. @xref{Output Template}.When simple substitution isn't general enough, you can specify a pieceof C code to compute the output. @xref{Output Statement}.@itemOptionally, a vector containing the values of attributes for insns matchingthis pattern. @xref{Insn Attributes}.@end enumerate@node Example@section Example of @code{define_insn}@cindex @code{define_insn} exampleHere is an actual example of an instruction pattern, for the 68000/68020.@smallexample(define_insn "tstsi" [(set (cc0) (match_operand:SI 0 "general_operand" "rm"))] "" "*@{ if (TARGET_68020 || ! ADDRESS_REG_P (operands[0])) return \"tstl %0\"; return \"cmpl #0,%0\";@}")@end smallexample@noindentThis can also be written using braced strings:@smallexample(define_insn "tstsi" [(set (cc0) (match_operand:SI 0 "general_operand" "rm"))] ""@{ if (TARGET_68020 || ! ADDRESS_REG_P (operands[0])) return "tstl %0"; return "cmpl #0,%0";@})@end smallexampleThis is an instruction that sets the condition codes based on the value ofa general operand. It has no condition, so any insn whose RTL descriptionhas the form shown may be handled according to this pattern. The name@samp{tstsi} means ``test a @code{SImode} value'' and tells the RTL generationpass that, when it is necessary to test such a value, an insn to do socan be constructed using this pattern.The output control string is a piece of C code which chooses whichoutput template to return based on the kind of operand and the specifictype of CPU for which code is being generated.@samp{"rm"} is an operand constraint. Its meaning is explained below.@node RTL Template@section RTL Template@cindex RTL insn template@cindex generating insns@cindex insns, generating@cindex recognizing insns@cindex insns, recognizingThe RTL template is used to define which insns match the particular patternand how to find their operands. For named patterns, the RTL template alsosays how to construct an insn from specified operands.Construction involves substituting specified operands into a copy of thetemplate. Matching involves determining the values that serve as theoperands in the insn being matched. Both of these activities arecontrolled by special expression types that direct matching andsubstitution of the operands.@table @code@findex match_operand@item (match_operand:@var{m} @var{n} @var{predicate} @var{constraint})This expression is a placeholder for operand number @var{n} ofthe insn. When constructing an insn, operand number @var{n}will be substituted at this point. When matching an insn, whateverappears at this position in the insn will be taken as operandnumber @var{n}; but it must satisfy @var{predicate} or this instructionpattern will not match at all.Operand numbers must be chosen consecutively counting from zero ineach instruction pattern. There may be only one @code{match_operand}expression in the pattern for each operand number. Usually operandsare numbered in the order of appearance in @code{match_operand}expressions. In the case of a @code{define_expand}, any operand numbersused only in @code{match_dup} expressions have higher values than allother operand numbers.@var{predicate} is a string that is the name of a function thataccepts two arguments, an expression and a machine mode.@xref{Predicates}. During matching, the function will be called withthe putative operand as the expression and @var{m} as the modeargument (if @var{m} is not specified, @code{VOIDmode} will be used,which normally causes @var{predicate} to accept any mode). If itreturns zero, this instruction pattern fails to match.@var{predicate} may be an empty string; then it means no test is to bedone on the operand, so anything which occurs in this position isvalid.Most of the time, @var{predicate} will reject modes other than @var{m}---butnot always. For example, the predicate @code{address_operand} uses@var{m} as the mode of memory ref that the address should be valid for.Many predicates accept @code{const_int} nodes even though their mode is@code{VOIDmode}.@var{constraint} controls reloading and the choice of the best registerclass to use for a value, as explained later (@pxref{Constraints}).If the constraint would be an empty string, it can be omitted.People are often unclear on the difference between the constraint and thepredicate. The predicate helps decide whether a given insn matches thepattern. The constraint plays no role in this decision; instead, itcontrols various decisions in the case of an insn which does match.@findex match_scratch@item (match_scratch:@var{m} @var{n} @var{constraint})This expression is also a placeholder for operand number @var{n}and indicates that operand must be a @code{scratch} or @code{reg}expression.When matching patterns, this is equivalent to@smallexample(match_operand:@var{m} @var{n} "scratch_operand" @var{pred})@end smallexamplebut, when generating RTL, it produces a (@code{scratch}:@var{m})expression.If the last few expressions in a @code{parallel} are @code{clobber}expressions whose operands are either a hard register or@code{match_scratch}, the combiner can add or delete them whennecessary. @xref{Side Effects}.@findex match_dup@item (match_dup @var{n})This expression is also a placeholder for operand number @var{n}.It is used when the operand needs to appear more than once in theinsn.In construction, @code{match_dup} acts just like @code{match_operand}:the operand is substituted into the insn being constructed. But inmatching, @code{match_dup} behaves differently. It assumes that operand
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -