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