Skip to content

Commit 33e8ba1

Browse files
authored
Merge pull request #12 from cpunion/fix/arm64-sysreg-mrs-msr
test: cover arm64 stdlib sysreg cross-target paths
2 parents 7f506d4 + a7e4d58 commit 33e8ba1

3 files changed

Lines changed: 95 additions & 33 deletions

File tree

stdlib_arm64_test_helpers_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build !llgo
2+
// +build !llgo
3+
4+
package plan9asm
5+
6+
import (
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
)
13+
14+
const arm64LinuxGNUTriple = "aarch64-unknown-linux-gnu"
15+
16+
func testGOROOT(t *testing.T) string {
17+
t.Helper()
18+
if goroot := os.Getenv("GOROOT"); goroot != "" {
19+
return goroot
20+
}
21+
goroot, err := testGoEnv("GOROOT")
22+
if err != nil || goroot == "" {
23+
t.Skip("GOROOT not available")
24+
}
25+
return goroot
26+
}
27+
28+
func testGoEnv(key string) (string, error) {
29+
out, err := exec.Command("go", "env", key).CombinedOutput()
30+
if err != nil {
31+
return "", err
32+
}
33+
return strings.TrimSpace(string(out)), nil
34+
}
35+
36+
func compileLLVMToObject(t *testing.T, llc, triple, llName, objName, ll string) {
37+
t.Helper()
38+
tmp := t.TempDir()
39+
llPath := filepath.Join(tmp, llName)
40+
objPath := filepath.Join(tmp, objName)
41+
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
42+
t.Fatal(err)
43+
}
44+
cmd := exec.Command(llc, "-mtriple="+triple, "-filetype=obj", llPath, "-o", objPath)
45+
out, err := cmd.CombinedOutput()
46+
if err != nil {
47+
s := string(out)
48+
if llcUnsupportedTarget(s) {
49+
t.Skipf("llc does not support triple %q: %s", triple, strings.TrimSpace(s))
50+
}
51+
t.Fatalf("llc failed: %v\n%s", err, s)
52+
}
53+
}

stdlib_cpu_arm64_test.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ package plan9asm
55

66
import (
77
"os"
8-
"os/exec"
98
"path/filepath"
10-
"runtime"
119
"strings"
1210
"testing"
1311
)
1412

1513
func TestStdlibInternalCPU_ARM64_Compile(t *testing.T) {
16-
if runtime.GOARCH != "arm64" {
17-
t.Skip("host is not arm64")
18-
}
1914
llc, _, ok := findLlcAndClang(t)
2015
if !ok {
2116
t.Skip("llc not found")
2217
}
2318

24-
goroot := runtime.GOROOT()
19+
goroot := testGOROOT(t)
2520
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "cpu", "cpu_arm64.s"))
2621
if err != nil {
22+
if os.IsNotExist(err) {
23+
t.Skip("internal/cpu/cpu_arm64.s not present in this GOROOT")
24+
}
2725
t.Fatal(err)
2826
}
2927

@@ -55,23 +53,14 @@ func TestStdlibInternalCPU_ARM64_Compile(t *testing.T) {
5553
},
5654
}
5755
ll, err := Translate(file, Options{
58-
TargetTriple: testTargetTriple(runtime.GOOS, runtime.GOARCH),
56+
TargetTriple: arm64LinuxGNUTriple,
5957
ResolveSym: resolve,
6058
Sigs: sigs,
59+
Goarch: "arm64",
6160
})
6261
if err != nil {
6362
t.Fatal(err)
6463
}
6564

66-
tmp := t.TempDir()
67-
llPath := filepath.Join(tmp, "cpu.ll")
68-
objPath := filepath.Join(tmp, "cpu.o")
69-
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
70-
t.Fatal(err)
71-
}
72-
cmd := exec.Command(llc, "-filetype=obj", llPath, "-o", objPath)
73-
out, err := cmd.CombinedOutput()
74-
if err != nil {
75-
t.Fatalf("llc failed: %v\n%s", err, string(out))
76-
}
65+
compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "cpu.ll", "cpu.o", ll)
7766
}

stdlib_runtime_sys_arm64_test.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@ package plan9asm
55

66
import (
77
"os"
8-
"os/exec"
98
"path/filepath"
10-
"runtime"
119
"strings"
1210
"testing"
1311
)
1412

1513
func TestStdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
16-
if runtime.GOARCH != "arm64" {
17-
t.Skip("host is not arm64")
18-
}
1914
llc, _, ok := findLlcAndClang(t)
2015
if !ok {
2116
t.Skip("llc not found")
2217
}
2318

24-
goroot := runtime.GOROOT()
19+
goroot := testGOROOT(t)
2520
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "runtime", "sys", "dit_arm64.s"))
2621
if err != nil {
2722
if os.IsNotExist(err) {
@@ -61,24 +56,49 @@ func TestStdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
6156
},
6257
}
6358
ll, err := Translate(file, Options{
64-
TargetTriple: testTargetTriple(runtime.GOOS, runtime.GOARCH),
59+
TargetTriple: arm64LinuxGNUTriple,
6560
ResolveSym: resolve,
6661
Sigs: sigs,
67-
Goarch: runtime.GOARCH,
62+
Goarch: "arm64",
6863
})
6964
if err != nil {
7065
t.Fatal(err)
7166
}
7267

73-
tmp := t.TempDir()
74-
llPath := filepath.Join(tmp, "dit.ll")
75-
objPath := filepath.Join(tmp, "dit.o")
76-
if err := os.WriteFile(llPath, []byte(ll), 0644); err != nil {
68+
compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "dit.ll", "dit.o", ll)
69+
}
70+
71+
func TestTranslateGoModule_StdlibInternalRuntimeSys_ARM64_Compile(t *testing.T) {
72+
llc, _, ok := findLlcAndClang(t)
73+
if !ok {
74+
t.Skip("llc not found")
75+
}
76+
77+
goroot := testGOROOT(t)
78+
src, err := os.ReadFile(filepath.Join(goroot, "src", "internal", "runtime", "sys", "dit_arm64.s"))
79+
if err != nil {
80+
if os.IsNotExist(err) {
81+
t.Skip("internal/runtime/sys/dit_arm64.s not present in this GOROOT")
82+
}
7783
t.Fatal(err)
7884
}
79-
cmd := exec.Command(llc, "-filetype=obj", llPath, "-o", objPath)
80-
out, err := cmd.CombinedOutput()
85+
pkg := mustGoPackage(t, "internal/runtime/sys", `package sys
86+
func EnableDIT() bool
87+
func DITEnabled() bool
88+
func DisableDIT()
89+
`)
90+
91+
tr, err := TranslateGoModule(pkg, src, GoModuleOptions{
92+
FileName: "dit_arm64.s",
93+
GOOS: "linux",
94+
GOARCH: "arm64",
95+
TargetTriple: arm64LinuxGNUTriple,
96+
ResolveSym: testResolveSym("internal/runtime/sys"),
97+
})
8198
if err != nil {
82-
t.Fatalf("llc failed: %v\n%s", err, string(out))
99+
t.Fatal(err)
83100
}
101+
defer tr.Module.Dispose()
102+
103+
compileLLVMToObject(t, llc, arm64LinuxGNUTriple, "dit-gomod.ll", "dit-gomod.o", tr.Module.String())
84104
}

0 commit comments

Comments
 (0)