Skip to content

Commit 8bca7ef

Browse files
committed
cmd/compile: support placeholder name '$' in code generation tests
This change adds to the code-generation harness in asm_test.go support for the use of a '$' placeholder name for test functions. A few of uninformative function names are also changed to use the placeholder, to confirm that the change works as expected. Fixes #21500 Change-Id: Iba168bd85efc9822253305d003b06682cf8a6c5c Reviewed-on: https://go-review.googlesource.com/57292 Run-TryBot: Alberto Donizetti <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 5e5a1ed commit 8bca7ef

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

src/cmd/compile/internal/gc/asm_test.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,49 @@ import (
1818
"testing"
1919
)
2020

21+
// This file contains code generation tests.
22+
//
23+
// Each test is defined in a variable of type asmTest. Tests are
24+
// architecture-specific, and they are grouped in arrays of tests, one
25+
// for each architecture.
26+
//
27+
// Each asmTest consists in a function to be compiled and an array of
28+
// regexps that will be matched to the generated assembly. For
29+
// example, the following amd64 test
30+
//
31+
// {
32+
// `
33+
// func f0(x int) int {
34+
// return x * 64
35+
// }
36+
// `,
37+
// []string{"\tSHLQ\t[$]6,"},
38+
// }
39+
//
40+
// verifies that the code the compiler generates for a multiplication
41+
// by 64 contains a 'SHLQ' instruction.
42+
//
43+
// Since all the tests for a given architecture are dumped in the same
44+
// file, the function names must be unique. As a workaround for this
45+
// restriction, the test harness supports the use of a '$' placeholder
46+
// for function names. The func f0 above can be also written as
47+
//
48+
// {
49+
// `
50+
// func $(x int) int {
51+
// return x * 64
52+
// }
53+
// `,
54+
// []string{"\tSHLQ\t[$]6,"},
55+
// }
56+
//
57+
// Each '$'-function will be given a unique name of form f<N>_<arch>,
58+
// where <N> is the test index in the test array, and <arch> is the
59+
// test's architecture.
60+
//
61+
// It is allowed to mix named and unnamed functions in the same test
62+
// array; the named function will retain their original names.
63+
2164
// TestAssembly checks to make sure the assembly generated for
2265
// functions contains certain expected instructions.
2366
func TestAssembly(t *testing.T) {
@@ -41,8 +84,13 @@ func TestAssembly(t *testing.T) {
4184

4285
asm := ats.compileToAsm(tt, dir)
4386

44-
for _, at := range ats.tests {
45-
funcName := nameRegexp.FindString(at.function)[len("func "):]
87+
for i, at := range ats.tests {
88+
var funcName string
89+
if strings.Contains(at.function, "func $") {
90+
funcName = fmt.Sprintf("f%d_%s", i, ats.arch)
91+
} else {
92+
funcName = nameRegexp.FindString(at.function)[len("func "):]
93+
}
4694
fa := funcAsm(tt, asm, funcName)
4795
if fa != "" {
4896
at.verifyAsm(tt, fa)
@@ -74,8 +122,7 @@ func funcAsm(t *testing.T, asm string, funcName string) string {
74122
}
75123

76124
type asmTest struct {
77-
// function to compile, must be named fX,
78-
// where X is this test's index in asmTests.tests.
125+
// function to compile
79126
function string
80127
// regexps that must match the generated assembly
81128
regexps []string
@@ -103,8 +150,9 @@ func (ats *asmTests) generateCode() []byte {
103150
fmt.Fprintf(&buf, "import %q\n", s)
104151
}
105152

106-
for _, t := range ats.tests {
107-
fmt.Fprintln(&buf, t.function)
153+
for i, t := range ats.tests {
154+
function := strings.Replace(t.function, "func $", fmt.Sprintf("func f%d_%s", i, ats.arch), 1)
155+
fmt.Fprintln(&buf, function)
108156
}
109157

110158
return buf.Bytes()
@@ -358,7 +406,7 @@ var linuxAMD64Tests = []*asmTest{
358406
type T1 struct {
359407
a, b, c int
360408
}
361-
func f18(t *T1) {
409+
func $(t *T1) {
362410
*t = T1{}
363411
}
364412
`,
@@ -951,17 +999,18 @@ var linux386Tests = []*asmTest{
951999
`,
9521000
[]string{"\tMOVL\t\\(.*\\)\\(.*\\*1\\),"},
9531001
},
1002+
9541003
// multiplication merging tests
9551004
{
9561005
`
957-
func mul1(n int) int {
1006+
func $(n int) int {
9581007
return 9*n + 14*n
9591008
}`,
9601009
[]string{"\tIMULL\t[$]23"}, // 23*n
9611010
},
9621011
{
9631012
`
964-
func mul2(a, n int) int {
1013+
func $(a, n int) int {
9651014
return 19*a + a*n
9661015
}`,
9671016
[]string{"\tADDL\t[$]19", "\tIMULL"}, // (n+19)*a

0 commit comments

Comments
 (0)