Debian Codegraph @

riscv64/riscv64asm/objdump_test.go

raw symbols SCIP
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 "strconv"
9 "strings"
10 "testing"
11 )
12
13 func TestObjdumpRISCV64TestDecodeGNUSyntaxdata(t *testing.T) {
14 testObjdumpRISCV64(t, testdataCases(t, "gnu"))
15 }
16 func TestObjdumpRISCV64TestDecodeGoSyntaxdata(t *testing.T) {
17 testObjdumpRISCV64(t, testdataCases(t, "plan9"))
18 }
19
20 func TestObjdumpRISCV64Manual(t *testing.T) {
21 testObjdumpRISCV64(t, hexCases(t, objdumpManualTests))
22 }
23
24 // objdumpManualTests holds test cases that will be run by TestObjdumpRISCV64Manual.
25 // If you are debugging a few cases that turned up in a longer run, it can be useful
26 // to list them here and then use -run=Manual, particularly with tracing enabled.
27 // Note that these are byte sequences, so they must be reversed from the usual
28 // word presentation.
29 var objdumpManualTests = `
30 93020300
31 13000000
32 9b020300
33 afb5b50e
34 73b012c0
35 73f01fc0
36 73a012c0
37 73e01fc0
38 f3223000
39 f3221000
40 f3222000
41 f3123300
42 f3121300
43 f3122300
44 739012c0
45 73d01fc0
46 53a01022
47 53a01020
48 53801022
49 53801020
50 53901022
51 53901020
52 67800000
53 67800200
54 b3026040
55 bb026040
56 9342f3ff
57 f32200c0
58 f32200c8
59 f32220c0
60 f32220c8
61 f32210c0
62 f32210c8
63 `
64
65 // allowedMismatchObjdump reports whether the mismatch between text and dec
66 // should be allowed by the test.
67 func allowedMismatchObjdump(text string, inst *Inst, dec ExtInst, version string) bool {
68 // Allow the mismatch of Branch/Jump instruction's offset.
69 decsp := strings.Split(dec.text, ",")
70
71 switch inst.Op {
72 case BEQ, BGE, BGEU, BLT, BLTU, BNE:
73 if inst.Args[2].(Simm).String() != decsp[len(decsp)-1] {
74 return true
75 }
76 case JAL:
77 if inst.Args[1].(Simm).String() != decsp[len(decsp)-1] {
78 return true
79 }
80 case JALR:
81 if inst.Args[1].(RegOffset).Ofs.String() != decsp[len(decsp)-1] {
82 return true
83 }
84 }
85
86 // Zvk instructions are supported by objdump 2.41 and later.
87 if strings.HasPrefix(dec.text, ".insn") && isZvkOp(inst.Op) && !objdumpVersionAtLeast(version, "2.41") {
88 return true
89 }
90
91 return false
92 }
93
94 func objdumpVersionAtLeast(version, m string) bool {
95 v, ok := versionMajorMinor(version)
96 if !ok {
97 return false
98 }
99 w, ok := versionMajorMinor(m)
100 return ok && v >= w
101 }
102
103 func versionMajorMinor(s string) (int, bool) {
104 maj, minv, ok := strings.Cut(s, ".")
105 if !ok {
106 return 0, false
107 }
108 v, err := strconv.Atoi(maj)
109 if err != nil {
110 return 0, false
111 }
112 w, err := strconv.Atoi(minv)
113 if err != nil {
114 return 0, false
115 }
116 return v*10000 + w, true
117 }
118
119 func isZvkOp(op Op) bool {
120 switch op {
121 case VAESDF_VV, VAESDF_VS, VAESDM_VV, VAESDM_VS,
122 VAESEF_VV, VAESEF_VS, VAESEM_VV, VAESEM_VS,
123 VAESKF1_VI, VAESKF2_VI, VAESZ_VS,
124 VGHSH_VV, VGMUL_VV,
125 VSHA2CH_VV, VSHA2CL_VV, VSHA2MS_VV,
126 VSM3C_VI, VSM3ME_VV, VSM4K_VI, VSM4R_VV, VSM4R_VS:
127 return true
128 }
129 return false
130 }