File: table_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.0~git20200523.4439b6b-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,304 kB
  • sloc: xml: 71,029; asm: 13,138; sh: 179; makefile: 35
file content (161 lines) | stat: -rw-r--r-- 3,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package inst_test

import (
	"io/ioutil"
	"reflect"
	"strings"
	"testing"

	"github.com/mmcloughlin/avo/internal/gen"
	"github.com/mmcloughlin/avo/internal/inst"
	"github.com/mmcloughlin/avo/internal/test"
	"github.com/mmcloughlin/avo/printer"
)

func TestHaveInstructions(t *testing.T) {
	n := len(inst.Instructions)
	t.Logf("number of instructions = %d", n)
	if n == 0 {
		t.Fatalf("no instructions")
	}
}

func TestOpcodeDupes(t *testing.T) {
	count := map[string]int{}
	for _, i := range inst.Instructions {
		count[i.Opcode]++
	}

	for opcode, n := range count {
		if n > 1 {
			t.Errorf("opcode %s appears %d times", opcode, n)
		}
	}
}

func TestFormDupes(t *testing.T) {
	for _, i := range inst.Instructions {
		if HasFormDupe(i) {
			t.Errorf("%s has duplicate forms", i.Opcode)
		}
	}
}

func HasFormDupe(i inst.Instruction) bool {
	n := len(i.Forms)
	for a := 0; a < n; a++ {
		for b := a + 1; b < n; b++ {
			if reflect.DeepEqual(i.Forms[a], i.Forms[b]) {
				return true
			}
		}
	}
	return false
}

func TestInstructionProperties(t *testing.T) {
	for _, i := range inst.Instructions {
		if len(i.Opcode) == 0 {
			t.Errorf("empty opcode")
		}
		if len(i.Forms) == 0 {
			t.Errorf("instruction %s has no forms", i.Opcode)
		}
		if len(i.Arities()) == 0 {
			t.Errorf("instruction %s has empty arities list", i.Opcode)
		}
		if i.IsNiladic() && len(i.Forms) != 1 {
			t.Errorf("%s breaks our expectation that niladic functions have one form", i.Opcode)
		}
	}
}

func TestAssembles(t *testing.T) {
	t.Skip() // https://github.com/mmcloughlin/avo/issues/152
	g := gen.NewAsmTest(printer.NewArgvConfig())
	b, err := g.Generate(inst.Instructions)
	if err != nil {
		t.Fatal(err)
	}
	test.Assembles(t, b)
}

func TestLookup(t *testing.T) {
	if _, found := inst.Lookup("CPUID"); !found {
		t.Fatalf("missing CPUID")
	}
	if _, found := inst.Lookup(strings.Repeat("XXX", 13)); found {
		t.Fatalf("lookup returns true on an absurd opcode")
	}
}

func TestInstructionArities(t *testing.T) {
	cases := map[string][]int{
		"AESDEC":    {2},
		"EXTRACTPS": {3},
		"SHRQ":      {2, 3},
		"VMOVHPD":   {2, 3},
	}
	for opcode, expect := range cases {
		i, ok := inst.Lookup(opcode)
		if !ok {
			t.Fatalf("could not find %s", opcode)
		}
		got := i.Arities()
		if !reflect.DeepEqual(got, expect) {
			t.Errorf("arity of %s is %v expected %v", opcode, got, expect)
		}
	}
}

func TestStdLibOpcodes(t *testing.T) {
	b, err := ioutil.ReadFile("testdata/stdlibopcodes.txt")
	if err != nil {
		t.Fatal(err)
	}
	opcodes := strings.Fields(string(b))
	for _, opcode := range opcodes {
		if _, found := inst.Lookup(opcode); !found {
			t.Errorf("missing instruction %s (used in stdlib asm)", opcode)
		}
	}
}

func TestCancellingInputs(t *testing.T) {
	// Expect all instruction forms with cancelling inputs to have two input operands of register type.
	//
	// Reference: https://github.com/Maratyszcza/PeachPy/blob/01d15157a973a4ae16b8046313ddab371ea582db/peachpy/x86_64/instructions.py#L136-L138
	//
	//	            assert len(input_operands) == 2, "Instruction forms with cancelling inputs must have two inputs"
	//	            assert all(map(lambda op: isinstance(op, Register), input_operands)), \
	//	                "Both inputs of instruction form with cancelling inputs must be registers"
	//
	for _, i := range inst.Instructions {
		for _, f := range i.Forms {
			if !f.CancellingInputs {
				continue
			}

			if len(f.ImplicitOperands) > 0 {
				t.Errorf("%s: expected no implicit operands", i.Opcode)
			}

			// Expect two register inputs.
			n := 0
			for _, op := range f.Operands {
				if op.Action.Read() {
					n++
					switch op.Type {
					case "r8", "r16", "r32", "r64", "xmm", "ymm":
						// pass
					default:
						t.Errorf("%s: unexpected operand type %q for self-cancelling input", i.Opcode, op.Type)
					}
				}
			}
			if n != 2 {
				t.Errorf("%s: expected two inputs for self-cancelling form", i.Opcode)
			}
		}
	}
}