aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/gc/asm_test.go
blob: db800aba20c6a1832a30cb6900522236071e82b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gc

import (
	"bytes"
	"fmt"
	"internal/testenv"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"strings"
	"testing"
)

// TestAssembly checks to make sure the assembly generated for
// functions contains certain expected instructions.
func TestAssembly(t *testing.T) {
	if testing.Short() {
		t.Skip("slow test; skipping")
	}
	testenv.MustHaveGoBuild(t)
	if runtime.GOOS == "windows" {
		// TODO: remove if we can get "go tool compile -S" to work on windows.
		t.Skipf("skipping test: recursive windows compile not working")
	}
	dir, err := ioutil.TempDir("", "TestAssembly")
	if err != nil {
		t.Fatalf("could not create directory: %v", err)
	}
	defer os.RemoveAll(dir)

	for _, test := range asmTests {
		asm := compileToAsm(t, dir, test.arch, test.os, fmt.Sprintf(template, test.function))
		// Get rid of code for "".init. Also gets rid of type algorithms & other junk.
		if i := strings.Index(asm, "\n\"\".init "); i >= 0 {
			asm = asm[:i+1]
		}
		for _, r := range test.regexps {
			if b, err := regexp.MatchString(r, asm); !b || err != nil {
				t.Errorf("expected:%s\ngo:%s\nasm:%s\n", r, test.function, asm)
			}
		}
	}
}

// compile compiles the package pkg for architecture arch and
// returns the generated assembly.  dir is a scratch directory.
func compileToAsm(t *testing.T, dir, goarch, goos, pkg string) string {
	// Create source.
	src := filepath.Join(dir, "test.go")
	f, err := os.Create(src)
	if err != nil {
		panic(err)
	}
	f.Write([]byte(pkg))
	f.Close()

	// First, install any dependencies we need.  This builds the required export data
	// for any packages that are imported.
	// TODO: extract dependencies automatically?
	var stdout, stderr bytes.Buffer
	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", filepath.Join(dir, "encoding/binary.a"), "encoding/binary")
	cmd.Env = mergeEnvLists([]string{"GOARCH=" + goarch, "GOOS=" + goos}, os.Environ())
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	if err := cmd.Run(); err != nil {
		panic(err)
	}
	if s := stdout.String(); s != "" {
		panic(fmt.Errorf("Stdout = %s\nWant empty", s))
	}
	if s := stderr.String(); s != "" {
		panic(fmt.Errorf("Stderr = %s\nWant empty", s))
	}

	// Now, compile the individual file for which we want to see the generated assembly.
	cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", dir, "-S", "-o", filepath.Join(dir, "out.o"), src)
	cmd.Env = mergeEnvLists([]string{"GOARCH=" + goarch, "GOOS=" + goos}, os.Environ())
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	if err := cmd.Run(); err != nil {
		panic(err)
	}
	if s := stderr.String(); s != "" {
		panic(fmt.Errorf("Stderr = %s\nWant empty", s))
	}
	return stdout.String()
}

// template to convert a function to a full file
const template = `
package main
%s
`

type asmTest struct {
	// architecture to compile to
	arch string
	// os to compile to
	os string
	// function to compile
	function string
	// regexps that must match the generated assembly
	regexps []string
}

var asmTests = [...]asmTest{
	{"amd64", "linux", `
func f(x int) int {
	return x * 64
}
`,
		[]string{"\tSHLQ\t\\$6,"},
	},
	{"amd64", "linux", `
func f(x int) int {
	return x * 96
}`,
		[]string{"\tSHLQ\t\\$5,", "\tLEAQ\t\\(.*\\)\\(.*\\*2\\),"},
	},
	// Load-combining tests.
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte) uint64 {
	return binary.LittleEndian.Uint64(b)
}
`,
		[]string{"\tMOVQ\t\\(.*\\),"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, i int) uint64 {
	return binary.LittleEndian.Uint64(b[i:])
}
`,
		[]string{"\tMOVQ\t\\(.*\\)\\(.*\\*1\\),"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte) uint32 {
	return binary.LittleEndian.Uint32(b)
}
`,
		[]string{"\tMOVL\t\\(.*\\),"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, i int) uint32 {
	return binary.LittleEndian.Uint32(b[i:])
}
`,
		[]string{"\tMOVL\t\\(.*\\)\\(.*\\*1\\),"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte) uint64 {
	return binary.BigEndian.Uint64(b)
}
`,
		[]string{"\tBSWAPQ\t"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, i int) uint64 {
	return binary.BigEndian.Uint64(b[i:])
}
`,
		[]string{"\tBSWAPQ\t"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, v uint64) {
	binary.BigEndian.PutUint64(b, v)
}
`,
		[]string{"\tBSWAPQ\t"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte) uint32 {
	return binary.BigEndian.Uint32(b)
}
`,
		[]string{"\tBSWAPL\t"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, i int) uint32 {
	return binary.BigEndian.Uint32(b[i:])
}
`,
		[]string{"\tBSWAPL\t"},
	},
	{"amd64", "linux", `
import "encoding/binary"
func f(b []byte, v uint32) {
	binary.BigEndian.PutUint32(b, v)
}
`,
		[]string{"\tBSWAPL\t"},
	},
	{"386", "linux", `
import "encoding/binary"
func f(b []byte) uint32 {
	return binary.LittleEndian.Uint32(b)
}
`,
		[]string{"\tMOVL\t\\(.*\\),"},
	},
	{"386", "linux", `
import "encoding/binary"
func f(b []byte, i int) uint32 {
	return binary.LittleEndian.Uint32(b[i:])
}
`,
		[]string{"\tMOVL\t\\(.*\\)\\(.*\\*1\\),"},
	},

	// Structure zeroing.  See issue #18370.
	{"amd64", "linux", `
type T struct {
	a, b, c int
}
func f(t *T) {
	*t = T{}
}
`,
		[]string{"\tMOVQ\t\\$0, \\(.*\\)", "\tMOVQ\t\\$0, 8\\(.*\\)", "\tMOVQ\t\\$0, 16\\(.*\\)"},
	},
	// TODO: add a test for *t = T{3,4,5} when we fix that.
}

// mergeEnvLists merges the two environment lists such that
// variables with the same name in "in" replace those in "out".
// This always returns a newly allocated slice.
func mergeEnvLists(in, out []string) []string {
	out = append([]string(nil), out...)
NextVar:
	for _, inkv := range in {
		k := strings.SplitAfterN(inkv, "=", 2)[0]
		for i, outkv := range out {
			if strings.HasPrefix(outkv, k) {
				out[i] = inkv
				continue NextVar
			}
		}
		out = append(out, inkv)
	}
	return out
}

// TestLineNumber checks to make sure the generated assembly has line numbers
// see issue #16214
func TestLineNumber(t *testing.T) {
	testenv.MustHaveGoBuild(t)
	dir, err := ioutil.TempDir("", "TestLineNumber")
	if err != nil {
		t.Fatalf("could not create directory: %v", err)
	}
	defer os.RemoveAll(dir)

	src := filepath.Join(dir, "x.go")
	err = ioutil.WriteFile(src, []byte(issue16214src), 0644)
	if err != nil {
		t.Fatalf("could not write file: %v", err)
	}

	cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("fail to run go tool compile: %v", err)
	}

	if strings.Contains(string(out), "unknown line number") {
		t.Errorf("line number missing in assembly:\n%s", out)
	}
}

var issue16214src = `
package main

func Mod32(x uint32) uint32 {
	return x % 3 // frontend rewrites it as HMUL with 2863311531, the LITERAL node has Lineno 0
}
`