Debian Codegraph @

internal/inst/table_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package inst_test
2
3 import (
4 "os"
5 "reflect"
6 "sort"
7 "strings"
8 "testing"
9
10 "github.com/mmcloughlin/avo/internal/gen"
11 "github.com/mmcloughlin/avo/internal/inst"
12 "github.com/mmcloughlin/avo/internal/test"
13 "github.com/mmcloughlin/avo/printer"
14 )
15
16 func TestHaveInstructions(t *testing.T) {
17 n := len(inst.Instructions)
18 t.Logf("number of instructions = %d", n)
19 if n == 0 {
20 t.Fatalf("no instructions")
21 }
22 }
23
24 func TestOpcodeDupes(t *testing.T) {
25 count := map[string]int{}
26 for _, i := range inst.Instructions {
27 count[i.Opcode]++
28 }
29
30 for opcode, n := range count {
31 if n > 1 {
32 t.Errorf("opcode %s appears %d times", opcode, n)
33 }
34 }
35 }
36
37 func TestFormDupes(t *testing.T) {
38 for _, i := range inst.Instructions {
39 if HasFormDupe(i) {
40 t.Errorf("%s has duplicate forms", i.Opcode)
41 }
42 }
43 }
44
45 func HasFormDupe(i inst.Instruction) bool {
46 n := len(i.Forms)
47 for a := 0; a < n; a++ {
48 for b := a + 1; b < n; b++ {
49 if reflect.DeepEqual(i.Forms[a], i.Forms[b]) {
50 return true
51 }
52 }
53 }
54 return false
55 }
56
57 func TestInstructionProperties(t *testing.T) {
58 for _, i := range inst.Instructions {
59 if len(i.Opcode) == 0 {
60 t.Errorf("empty opcode")
61 }
62 if len(i.Forms) == 0 {
63 t.Errorf("instruction %s has no forms", i.Opcode)
64 }
65 if len(i.Arities()) == 0 {
66 t.Errorf("instruction %s has empty arities list", i.Opcode)
67 }
68 if i.IsNiladic() && len(i.Forms) != 1 {
69 t.Errorf("%s breaks our expectation that niladic functions have one form", i.Opcode)
70 }
71 }
72 }
73
74 func TestHaveSuffixes(t *testing.T) {
75 for _, i := range inst.Instructions {
76 for _, f := range i.Forms {
77 if len(f.SupportedSuffixes()) == 0 {
78 t.Errorf("%s: no supported suffixes", i.Opcode)
79 }
80 }
81 }
82 }
83
84 func TestAcceptsSuffixes(t *testing.T) {
85 // Verify consistency between the AcceptsSuffixes and SupportedSuffixes methods.
86 for _, i := range inst.Instructions {
87 for _, f := range i.Forms {
88 expect := false
89 for _, suffixes := range f.SupportedSuffixes() {
90 if len(suffixes) > 0 {
91 expect = true
92 }
93 }
94
95 if got := f.AcceptsSuffixes(); got != expect {
96 t.Errorf("%s: AcceptsSuffixes() = %v; expect %v", i.Opcode, got, expect)
97 }
98 }
99 }
100 }
101
102 func TestSuffixesClasses(t *testing.T) {
103 // Verify that all instructions in a suffix class support the same suffixes.
104 reps := map[string][]inst.Suffixes{}
105 for _, i := range inst.Instructions {
106 for _, f := range i.Forms {
107 class := f.SuffixesClass()
108 expect, ok := reps[class]
109 if !ok {
110 t.Logf("new class %q: representative from instruction %s", class, i.Opcode)
111 reps[class] = f.SupportedSuffixes()
112 continue
113 }
114
115 got := f.SupportedSuffixes()
116 if !reflect.DeepEqual(expect, got) {
117 t.Fatalf("suffixes mismatch for class %q", class)
118 }
119 }
120 }
121 }
122
123 func TestSuffixesHaveSummaries(t *testing.T) {
124 set := map[inst.Suffix]bool{}
125 for _, i := range inst.Instructions {
126 for _, f := range i.Forms {
127 for _, suffixes := range f.SupportedSuffixes() {
128 for _, suffix := range suffixes {
129 set[suffix] = true
130 }
131 }
132 }
133 }
134
135 for suffix := range set {
136 if suffix.Summary() == "" {
137 t.Errorf("suffix %q missing summary", suffix)
138 }
139 }
140 }
141
142 func TestISASorted(t *testing.T) {
143 for _, i := range inst.Instructions {
144 for _, f := range i.Forms {
145 if !sort.StringsAreSorted(f.ISA) {
146 t.Fatalf("%s: isa not sorted", i.Opcode)
147 }
148 }
149 }
150 }
151
152 func TestAssembles(t *testing.T) {
153 g := gen.NewAsmTest(printer.NewArgvConfig())
154 b, err := g.Generate(inst.Instructions)
155 if err != nil {
156 t.Fatal(err)
157 }
158 test.Assembles(t, b)
159 }
160
161 func TestLookup(t *testing.T) {
162 if _, found := inst.Lookup("CPUID"); !found {
163 t.Fatalf("missing CPUID")
164 }
165 if _, found := inst.Lookup(strings.Repeat("XXX", 13)); found {
166 t.Fatalf("lookup returns true on an absurd opcode")
167 }
168 }
169
170 func TestInstructionArities(t *testing.T) {
171 cases := map[string][]int{
172 "AESDEC": {2},
173 "EXTRACTPS": {3},
174 "SHRQ": {2, 3},
175 "VMOVHPD": {2, 3},
176 }
177 for opcode, expect := range cases {
178 i, ok := inst.Lookup(opcode)
179 if !ok {
180 t.Fatalf("could not find %s", opcode)
181 }
182 got := i.Arities()
183 if !reflect.DeepEqual(got, expect) {
184 t.Errorf("arity of %s is %v expected %v", opcode, got, expect)
185 }
186 }
187 }
188
189 func TestStdLibOpcodes(t *testing.T) {
190 b, err := os.ReadFile("testdata/stdlibopcodes.txt")
191 if err != nil {
192 t.Fatal(err)
193 }
194 opcodes := strings.Fields(string(b))
195 for _, opcode := range opcodes {
196 if _, found := inst.Lookup(opcode); !found {
197 t.Errorf("missing instruction %s (used in stdlib asm)", opcode)
198 }
199 }
200 }
201
202 func TestCancellingInputs(t *testing.T) {
203 // Expect all instruction forms with cancelling inputs to have two input operands of register type.
204 //
205 // Reference: https://github.com/Maratyszcza/PeachPy/blob/01d15157a973a4ae16b8046313ddab371ea582db/peachpy/x86_64/instructions.py#L136-L138
206 //
207 // assert len(input_operands) == 2, "Instruction forms with cancelling inputs must have two inputs"
208 // assert all(map(lambda op: isinstance(op, Register), input_operands)), \
209 // "Both inputs of instruction form with cancelling inputs must be registers"
210 //
211 for _, i := range inst.Instructions {
212 for _, f := range i.Forms {
213 if !f.CancellingInputs {
214 continue
215 }
216
217 if len(f.ImplicitOperands) > 0 {
218 t.Errorf("%s: expected no implicit operands", i.Opcode)
219 }
220
221 // Expect two register inputs.
222 n := 0
223 for _, op := range f.Operands {
224 if op.Action.Read() {
225 switch op.Type {
226 case "r8", "r16", "r32", "r64", "xmm", "ymm", "zmm":
227 n++
228 case "k":
229 // skip mask registers
230 default:
231 t.Errorf("%s: unexpected operand type %q for self-cancelling input", i.Opcode, op.Type)
232 }
233 }
234 }
235 if n < 2 {
236 t.Log(f)
237 t.Errorf("%s: expected at least two inputs for self-cancelling form", i.Opcode)
238 }
239 }
240 }
241 }
242
243 func TestFlagOperandTypes(t *testing.T) {
244 for _, i := range inst.Instructions {
245 for _, f := range i.Forms {
246 // Check for memory operands.
247 mem := false
248 for _, op := range f.Operands {
249 if strings.HasPrefix(op.Type, "m") {
250 mem = true
251 }
252 }
253
254 // Broadcast applies to memory instructions only.
255 if f.Broadcast && !mem {
256 t.Errorf("%s: expect broadcast form to have memory operand", i.Opcode)
257 }
258
259 // Embedded rounding must be register-only.
260 if f.EmbeddedRounding && mem {
261 t.Errorf("%s: embedded-rounding only supported for register-only forms", i.Opcode)
262 }
263
264 // Suppress all exceptions is register only.
265 if f.SuppressAllExceptions && mem {
266 t.Errorf("%s: embedded-rounding only supported for register-only forms", i.Opcode)
267 }
268 }
269 }
270 }
271
272 func TestFlagCombinations(t *testing.T) {
273 for _, i := range inst.Instructions {
274 for _, f := range i.Forms {
275 if f.EmbeddedRounding && f.SuppressAllExceptions {
276 t.Errorf("%s: embedded-rounding cannot be combined with suppress-all-exceptions", i.Opcode)
277 }
278 if f.Broadcast && f.EmbeddedRounding {
279 t.Errorf("%s: broadcast cannot be combined with embedded-rounding", i.Opcode)
280 }
281 if f.Broadcast && f.SuppressAllExceptions {
282 t.Errorf("%s: broadcast cannot be combined with suppress-all-exceptions", i.Opcode)
283 }
284 }
285 }
286 }
287
288 func TestZeroingHasMask(t *testing.T) {
289 for _, i := range inst.Instructions {
290 for _, f := range i.Forms {
291 if !f.Zeroing {
292 continue
293 }
294
295 // Expect mask operand.
296 mask := false
297 for _, op := range f.Operands {
298 if op.Type == "k" {
299 mask = true
300 }
301 }
302
303 if !mask {
304 t.Errorf("%s: expect mask operand if zeroing is enabled", i.Opcode)
305 }
306 }
307 }
308 }