Debian Codegraph @

x86/inst_test.go

raw symbols SCIP
1 package x86
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/mmcloughlin/avo/ir"
8 "github.com/mmcloughlin/avo/operand"
9 "github.com/mmcloughlin/avo/reg"
10 )
11
12 func TestCases(t *testing.T) {
13 must := MustInstruction(t)
14
15 m128 := operand.Mem{Base: reg.RAX}
16
17 cases := []struct {
18 Name string
19 Instruction *ir.Instruction
20 Expect *ir.Instruction
21 }{
22 // In the merge-masking case, the output register should also be an
23 // input. This test confirms that Z3 appears in the input operands list.
24 {
25 Name: "avx512_masking_merging_input_registers",
26 Instruction: must(VPADDD(reg.Z1, reg.Z2, reg.K1, reg.Z3)),
27 Expect: &ir.Instruction{
28 Opcode: "VPADDD",
29 Operands: []operand.Op{reg.Z1, reg.Z2, reg.K1, reg.Z3},
30 Inputs: []operand.Op{reg.Z1, reg.Z2, reg.K1, reg.Z3},
31 Outputs: []operand.Op{reg.Z3},
32 ISA: []string{"AVX512F"},
33 },
34 },
35 // In the zeroing-masking case, the output register is not an input.
36 // This test case is the same as above, but with the zeroing suffix. In
37 // this case Z3 should not be an input.
38 {
39 Name: "avx512_masking_zeroing_input_registers",
40 Instruction: must(VPADDD_Z(reg.Z1, reg.Z2, reg.K1, reg.Z3)),
41 Expect: &ir.Instruction{
42 Opcode: "VPADDD",
43 Suffixes: []string{"Z"},
44 Operands: []operand.Op{reg.Z1, reg.Z2, reg.K1, reg.Z3},
45 Inputs: []operand.Op{reg.Z1, reg.Z2, reg.K1}, // not Z3
46 Outputs: []operand.Op{reg.Z3},
47 ISA: []string{"AVX512F"},
48 },
49 },
50 // Many existing AVX instructions gained EVEX-encoded forms when AVX-512
51 // was added. In a previous broken implementation, this led to multiple
52 // forms of the same instruction in the database, both the VEX and EVEX
53 // encoded versions. This causes the computed ISA list to be wrong,
54 // since it can think AVX-512 is required when in fact the instruction
55 // existed before. These test cases confirm the correct ISA is selected.
56 {
57 Name: "vex_evex_xmm_xmm_xmm",
58 Instruction: must(VFMADD132PS(reg.X1, reg.X2, reg.X3)),
59 Expect: &ir.Instruction{
60 Opcode: "VFMADD132PS",
61 Operands: []operand.Op{reg.X1, reg.X2, reg.X3},
62 Inputs: []operand.Op{reg.X1, reg.X2, reg.X3},
63 Outputs: []operand.Op{reg.X3},
64 ISA: []string{"FMA3"}, // not AVX512F
65 },
66 },
67 {
68 Name: "vex_evex_m128_xmm_xmm",
69 Instruction: must(VFMADD132PS(m128, reg.X2, reg.X3)),
70 Expect: &ir.Instruction{
71 Opcode: "VFMADD132PS",
72 Operands: []operand.Op{m128, reg.X2, reg.X3},
73 Inputs: []operand.Op{m128, reg.X2, reg.X3},
74 Outputs: []operand.Op{reg.X3},
75 ISA: []string{"FMA3"}, // not AVX512F
76 },
77 },
78 }
79
80 for _, c := range cases {
81 c := c // scopelint
82 t.Run(c.Name, func(t *testing.T) {
83 if !reflect.DeepEqual(c.Instruction, c.Expect) {
84 t.Logf(" got = %#v", c.Instruction)
85 t.Logf("expect = %#v", c.Expect)
86 t.FailNow()
87 }
88 })
89 }
90 }
91
92 func MustInstruction(t *testing.T) func(*ir.Instruction, error) *ir.Instruction {
93 t.Helper()
94 return func(i *ir.Instruction, err error) *ir.Instruction {
95 t.Helper()
96 if err != nil {
97 t.Fatal(err)
98 }
99 return i
100 }
101 }