golang.org/x/arch@v0.17.0/riscv64/riscv64asm/decode_test.go (about) 1 // Copyright 2024 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 riscv64asm 6 7 import ( 8 "bufio" 9 "encoding/hex" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 ) 15 16 func testDecode(t *testing.T, syntax string) { 17 input := filepath.Join("testdata", syntax+"cases.txt") 18 f, err := os.Open(input) 19 if err != nil { 20 t.Fatal(err) 21 } 22 defer f.Close() 23 scanner := bufio.NewScanner(f) 24 for scanner.Scan() { 25 line := strings.TrimSpace(scanner.Text()) 26 if line == "" || strings.HasPrefix(line, "#") { 27 continue 28 } 29 f := strings.SplitN(line, "\t", 2) 30 i := strings.Index(f[0], "|") 31 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 asm0 := strings.Replace(f[1], " ", " ", -1) 45 asm := strings.TrimSpace(asm0) 46 inst, decodeErr := Decode(code) 47 if decodeErr != nil && decodeErr != errUnknown { 48 if asm == "illegalins" && decodeErr == errShort { 49 continue 50 } 51 // Some rarely used system instructions are not supported 52 // Following logicals will filter such unknown instructions 53 t.Errorf("parsing %x: %s", code, decodeErr) 54 continue 55 } 56 57 var out string 58 switch syntax { 59 case "gnu": 60 out = GNUSyntax(inst) 61 case "plan9": 62 out = GoSyntax(inst, 0, nil, nil) 63 default: 64 t.Errorf("unknown syntax %q", syntax) 65 continue 66 } 67 68 if asm != out { 69 t.Errorf("Decode(%s) [%s] = %s want %s", f[0], syntax, out, asm) 70 } 71 } 72 } 73 74 func TestDecodeGNUSyntax(t *testing.T) { 75 testDecode(t, "gnu") 76 } 77 78 func TestDecodeGoSyntax(t *testing.T) { 79 testDecode(t, "plan9") 80 }