golang.org/x/arch@v0.17.0/arm64/arm64asm/decode_test.go (about) 1 // Copyright 2017 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 package arm64asm 6 7 import ( 8 "encoding/hex" 9 "io/ioutil" 10 "path/filepath" 11 "strings" 12 "testing" 13 ) 14 15 func testDecode(t *testing.T, syntax string) { 16 input := filepath.Join("testdata", syntax+"cases.txt") 17 data, err := ioutil.ReadFile(input) 18 if err != nil { 19 t.Fatal(err) 20 } 21 all := string(data) 22 for strings.Contains(all, "\t\t") { 23 all = strings.Replace(all, "\t\t", "\t", -1) 24 } 25 for _, line := range strings.Split(all, "\n") { 26 line = strings.TrimSpace(line) 27 if line == "" || strings.HasPrefix(line, "#") { 28 continue 29 } 30 f := strings.SplitN(line, "\t", 2) 31 i := strings.Index(f[0], "|") 32 if i < 0 { 33 t.Errorf("parsing %q: missing | separator", f[0]) 34 continue 35 } 36 if i%2 != 0 { 37 t.Errorf("parsing %q: misaligned | separator", f[0]) 38 } 39 code, err := hex.DecodeString(f[0][:i] + f[0][i+1:]) 40 if err != nil { 41 t.Errorf("parsing %q: %v", f[0], err) 42 continue 43 } 44 asm := f[1] 45 inst, decodeErr := Decode(code) 46 if decodeErr != nil && decodeErr != errUnknown { 47 // Some rarely used system instructions are not supported 48 // Following logicals will filter such unknown instructions 49 50 t.Errorf("parsing %x: %s", code, decodeErr) 51 continue 52 } 53 var out string 54 switch syntax { 55 case "gnu": 56 out = GNUSyntax(inst) 57 case "plan9": 58 out = GoSyntax(inst, 0, nil, nil) 59 default: 60 t.Errorf("unknown syntax %q", syntax) 61 continue 62 } 63 // TODO: system instruction. 64 var Todo = strings.Fields(` 65 sys 66 at 67 ic 68 hvc 69 smc 70 `) 71 if strings.Replace(out, " ", "", -1) != strings.Replace(asm, " ", "", -1) && !hasPrefix(asm, Todo...) { 72 // Exclude MSR since GNU objdump result is incorrect. eg. 0xd504431f msr s0_4_c4_c3_0, xzr 73 if !strings.HasSuffix(asm, " nv") && !strings.HasPrefix(asm, "msr") { 74 t.Errorf("Decode(%s) [%s] = %s, want %s", strings.Trim(f[0], "|"), syntax, out, asm) 75 } 76 } 77 } 78 } 79 80 func TestDecodeGNUSyntax(t *testing.T) { 81 testDecode(t, "gnu") 82 } 83 84 func TestDecodeGoSyntax(t *testing.T) { 85 testDecode(t, "plan9") 86 }