📄 md.texi
字号:
@c Copyright (C) 1988, 1989, 1992 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* 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.* Constraints:: When not all operands are general operands.* 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.* Insn Canonicalizations::Canonicalization of Instructions* Peephole Definitions::Defining machine-specific peephole optimizations.* Expander Definitions::Generating a sequence of several RTL insns for a standard operation.* Insn Splitting:: Splitting Instructions into Multiple Instructions* Insn Attributes:: Specifying the value of attributes for generated insns.@end menu@node Patterns, Example, Machine Desc, Machine Desc@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.@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}.@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, RTL Template, Patterns, Machine Desc@section Example of @code{define_insn}@cindex @code{define_insn} exampleHere is an actual example of an instruction pattern, for the 68000/68020.@example(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 exampleThis 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, Output Template, Example, Machine Desc@section RTL Template for Generating and Recognizing Insns@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.@var{predicate} is a string that is the name of a C function that accepts twoarguments, an expression and a machine mode. During matching, thefunction will be called with the putative operand as the expression and@var{m} as the mode argument (if @var{m} is not specified,@code{VOIDmode} will be used, which normally causes @var{predicate} to acceptany mode). If it returns zero, this instruction pattern fails to match.@var{predicate} may be an empty string; then it means no test is to be doneon the operand, so anything which occurs in this position is valid.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}).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 general_operandOn CISC machines, @var{predicate} is most often @code{"general_operand"}.This function checks that the putative operand is either a constant, aregister or a memory reference, and that it is valid for mode @var{m}.@findex register_operandFor an operand that must be a register, @var{predicate} should be@code{"register_operand"}. It would be valid to use@code{"general_operand"}, since the reload pass would copy anynon-register operands through registers, but this would make GNU CC doextra work, it would prevent invariant operands (such as constant) frombeing removed from loops, and it would prevent the register allocatorfrom doing the best possible job. On RISC machines, it is usually mostefficient to allow @var{predicate} to accept only objects that theconstraints allow.@findex immediate_operandFor an operand that must be a constant, either use@code{"immediate_operand"} for @var{predicate}, or make the instructionpattern's extra condition require a constant, or both. You cannotexpect the constraints to do this work! If the constraints allow onlyconstants, but the predicate allows something else, the compiler willcrash when that case arises.@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 completely equivalent to@example(match_operand:@var{m} @var{n} "scratch_operand" @var{pred})@end examplebut, 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 them when necessary.@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} behaves exactly like@code{match_operand}: the operand is substituted into the insn beingconstructed. But in matching, @code{match_dup} behaves differently.It assumes that operand number @var{n} has already been determined bya @code{match_operand} appearing earlier in the recognition template,and it matches only an identical-looking expression.@findex match_operator@item (match_operator:@var{m} @var{n} @var{predicate} [@var{operands}@dots{}])This pattern is a kind of placeholder for a variable RTL expressioncode.When constructing an insn, it stands for an RTL expression whoseexpression code is taken from that of operand @var{n}, and whoseoperands are constructed from the patterns @var{operands}.When matching an expression, it matches an expression if the function@var{predicate} returns nonzero on that expression @emph{and} thepatterns @var{operands} match the operands of the expression.Suppose that the function @code{commutative_operator} is defined asfollows, to match any expression whose operator is one of thecommutative arithmetic operators of RTL and whose mode is @var{mode}:@exampleintcommutative_operator (x, mode) rtx x; enum machine_mode mode;@{ enum rtx_code code = GET_CODE (x); if (GET_MODE (x) != mode) return 0; return GET_RTX_CLASS (code) == 'c' || code == EQ || code == NE;@}@end exampleThen the following pattern will match any RTL expression consistingof a commutative operator applied to two general operands:@example(match_operator:SI 3 "commutative_operator" [(match_operand:SI 1 "general_operand" "g") (match_operand:SI 2 "general_operand" "g")])@end exampleHere the vector @code{[@var{operands}@dots{}]} contains two patternsbecause the expressions to be matched all contain two operands.When this pattern does match, the two operands of the commutativeoperator are recorded as operands 1 and 2 of the insn. (This is doneby the two instances of @code{match_operand}.) Operand 3 of the insn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -