golang.org/x/arch@v0.17.0/s390x/s390xasm/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 s390xasm 6 7 import ( 8 "encoding/hex" 9 "io/ioutil" 10 "path" 11 "strings" 12 "testing" 13 ) 14 15 func TestDecode(t *testing.T) { 16 files, err := ioutil.ReadDir("testdata") 17 if err != nil { 18 t.Fatal(err) 19 } 20 for _, f := range files { 21 if !strings.HasPrefix(f.Name(), "decode") { 22 continue 23 } 24 filename := path.Join("testdata", f.Name()) 25 data, err := ioutil.ReadFile(filename) 26 if err != nil { 27 t.Fatal(err) 28 } 29 decode(data, t, filename) 30 } 31 } 32 33 // Provide a fake symbol to verify PCrel argument decoding. 34 func symlookup(pc uint64) (string, uint64) { 35 foopc := uint64(0x100000) 36 if pc >= foopc && pc < foopc+0x10 { 37 return "foo", foopc 38 } 39 return "", 0 40 } 41 42 func decode(data []byte, t *testing.T, filename string) { 43 all := string(data) 44 // Simulate PC based on number of instructions found in the test file. 45 pc := uint64(0) 46 for strings.Contains(all, "\t\t") { 47 all = strings.Replace(all, "\t\t", "\t", -1) 48 } 49 for _, line := range strings.Split(all, "\n") { 50 line = strings.TrimSpace(line) 51 if line == "" || strings.HasPrefix(line, "#") { 52 continue 53 } 54 f := strings.SplitN(line, "\t", 3) 55 i := strings.Index(f[0], "|") 56 if i < 0 { 57 t.Errorf("%s: parsing %q: missing | separator", filename, f[0]) 58 continue 59 } 60 if i%2 != 0 { 61 t.Errorf("%s: parsing %q: misaligned | separator", filename, f[0]) 62 } 63 size := i / 2 64 code, err := hex.DecodeString(f[0][:i] + f[0][i+1:]) 65 if err != nil { 66 t.Errorf("%s: parsing %q: %v", filename, f[0], err) 67 continue 68 } 69 syntax, asm := f[1], f[2] 70 inst, err := Decode(code) 71 var out string 72 if err != nil { 73 out = "error: " + err.Error() 74 } else { 75 switch syntax { 76 case "gnu": 77 out = GNUSyntax(inst, pc) 78 case "plan9": 79 out = GoSyntax(inst, pc, nil) 80 default: 81 t.Errorf("unknown syntax %q", syntax) 82 continue 83 } 84 } 85 pc += uint64(size) 86 if out != asm || inst.Len != size { 87 t.Errorf("%s: Decode(%s) [%s] = %s want %s", filename, f[0], syntax, out, asm) 88 } 89 } 90 }