Text file test/codegen/README

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  The codegen directory contains code generation tests for the gc
     6  compiler.
     7  
     8  
     9  - Introduction
    10  
    11  The test harness compiles Go code inside files in this directory and
    12  matches the generated assembly (the output of `go tool compile -S`)
    13  against a set of regexps to be specified in comments that follow a
    14  special syntax (described below). The test driver is implemented as a
    15  step of the top-level test/run.go suite, called "asmcheck".
    16  
    17  The codegen harness is part of the all.bash test suite, but for
    18  performance reasons only the codegen tests for the host machine's
    19  GOARCH are enabled by default, and only on GOOS=linux.
    20  
    21  To perform comprehensive tests for all the supported architectures
    22  (even on a non-Linux system), one can run the following command
    23  
    24    $ ../bin/go run run.go -all_codegen -v codegen
    25  
    26  in the top-level test directory. This is recommended after any change
    27  that affect the compiler's code.
    28  
    29  The test harness compiles the tests with the same go toolchain that is
    30  used to run run.go. After writing tests for a newly added codegen
    31  transformation, it can be useful to first run the test harness with a
    32  toolchain from a released Go version (and verify that the new tests
    33  fail), and then re-runnig the tests using the devel toolchain.
    34  
    35  
    36  - Regexps comments syntax
    37  
    38  Instructions to match are specified inside plain comments that start
    39  with an architecture tag, followed by a colon and a quoted Go-style
    40  regexp to be matched. For example, the following test:
    41  
    42    func Sqrt(x float64) float64 {
    43    	   // amd64:"SQRTSD"
    44    	   // arm64:"FSQRTD"
    45    	   return math.Sqrt(x)
    46    }
    47  
    48  verifies that math.Sqrt calls are intrinsified to a SQRTSD instruction
    49  on amd64, and to a FSQRTD instruction on arm64.
    50  
    51  It is possible to put multiple architectures checks into the same
    52  line, as:
    53  
    54    // amd64:"SQRTSD" arm64:"FSQRTD"
    55  
    56  although this form should be avoided when doing so would make the
    57  regexps line excessively long and difficult to read.
    58  
    59  Comments that are on their own line will be matched against the first
    60  subsequent non-comment line. Inline comments are also supported; the
    61  regexp will be matched against the code found on the same line:
    62  
    63    func Sqrt(x float64) float64 {
    64    	   return math.Sqrt(x) // arm:"SQRTD"
    65    }
    66  
    67  It's possible to specify a comma-separated list of regexps to be
    68  matched. For example, the following test:
    69  
    70    func TZ8(n uint8) int {
    71    	   // amd64:"BSFQ","ORQ\t\\$256"
    72    	   return bits.TrailingZeros8(n)
    73    }
    74  
    75  verifies that the code generated for a bits.TrailingZeros8 call on
    76  amd64 contains both a "BSFQ" instruction and an "ORQ $256".
    77  
    78  Note how the ORQ regex includes a tab char (\t). In the Go assembly
    79  syntax, operands are separated from opcodes by a tabulation.
    80  
    81  Regexps can be quoted using either " or `. Special characters must be
    82  escaped accordingly. Both of these are accepted, and equivalent:
    83  
    84    // amd64:"ADDQ\t\\$3"
    85    // amd64:`ADDQ\t\$3`
    86  
    87  and they'll match this assembly line:
    88  
    89    ADDQ	$3
    90  
    91  Negative matches can be specified using a - before the quoted regexp.
    92  For example:
    93  
    94    func MoveSmall() {
    95    	   x := [...]byte{1, 2, 3, 4, 5, 6, 7}
    96    	   copy(x[1:], x[:]) // arm64:-".*memmove"
    97    }
    98  
    99  verifies that NO memmove call is present in the assembly generated for
   100  the copy() line.
   101  
   102  
   103  - Architecture specifiers
   104  
   105  There are three different ways to specify on which architecture a test
   106  should be run:
   107  
   108  * Specify only the architecture (eg: "amd64"). This indicates that the
   109    check should be run on all the supported architecture variants. For
   110    instance, arm checks will be run against all supported GOARM
   111    variations (5,6,7).
   112  * Specify both the architecture and a variant, separated by a slash
   113    (eg: "arm/7"). This means that the check will be run only on that
   114    specific variant.
   115  * Specify the operating system, the architecture and the variant,
   116    separated by slashes (eg: "plan9/386/sse2", "plan9/amd64/"). This is
   117    needed in the rare case that you need to do a codegen test affected
   118    by a specific operating system; by default, tests are compiled only
   119    targeting linux.
   120  
   121  
   122  - Remarks, and Caveats
   123  
   124  -- Write small test functions
   125  
   126  As a general guideline, test functions should be small, to avoid
   127  possible interactions between unrelated lines of code that may be
   128  introduced, for example, by the compiler's optimization passes.
   129  
   130  Any given line of Go code could get assigned more instructions than it
   131  may appear from reading the source. In particular, matching all MOV
   132  instructions should be avoided; the compiler may add them for
   133  unrelated reasons and this may render the test ineffective.
   134  
   135  -- Line matching logic
   136  
   137  Regexps are always matched from the start of the instructions line.
   138  This means, for example, that the "MULQ" regexp is equivalent to
   139  "^MULQ" (^ representing the start of the line), and it will NOT match
   140  the following assembly line:
   141  
   142    IMULQ	$99, AX
   143  
   144  To force a match at any point of the line, ".*MULQ" should be used.
   145  
   146  For the same reason, a negative regexp like -"memmove" is not enough
   147  to make sure that no memmove call is included in the assembly. A
   148  memmove call looks like this:
   149  
   150    CALL	runtime.memmove(SB)
   151  
   152  To make sure that the "memmove" symbol does not appear anywhere in the
   153  assembly, the negative regexp to be used is -".*memmove".
   154  

View as plain text