File: reg_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 (174 lines) | stat: -rw-r--r-- 3,530 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
162
163
164
165
166
167
168
169
170
171
172
173
174
package reg

import (
	"testing"
	"testing/quick"
)

func TestIDFields(t *testing.T) {
	f := func(v uint8, kind Kind, idx Index) bool {
		id := newid(v, kind, idx)
		return id.Kind() == kind && id.Index() == idx
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestIDIsVirtual(t *testing.T) {
	cases := []Virtual{
		GeneralPurpose.Virtual(42, S64),
		Vector.Virtual(42, S128),
	}
	for _, r := range cases {
		if !r.ID().IsVirtual() {
			t.FailNow()
		}
	}
}

func TestIDIsPhysical(t *testing.T) {
	cases := []Physical{AL, AH, AX, EAX, RAX, X1, Y2, Z31}
	for _, r := range cases {
		if !r.ID().IsPhysical() {
			t.FailNow()
		}
	}
}

func TestSpecSize(t *testing.T) {
	cases := []struct {
		Spec Spec
		Size uint
	}{
		{S0, 0},
		{S8L, 1},
		{S8H, 1},
		{S16, 2},
		{S32, 4},
		{S64, 8},
		{S128, 16},
		{S256, 32},
		{S512, 64},
	}
	for _, c := range cases {
		if c.Spec.Size() != c.Size {
			t.Errorf("%v.Size() = %d; expect = %d", c.Spec, c.Spec.Size(), c.Size)
		}
	}
}

func TestToVirtual(t *testing.T) {
	v := GeneralPurpose.Virtual(42, S32)
	if ToVirtual(v) != v {
		t.Errorf("ToVirtual(v) != v for virtual register")
	}
	if ToVirtual(ECX) != nil {
		t.Errorf("ToVirtual should be nil for physical registers")
	}
}

func TestToPhysical(t *testing.T) {
	v := GeneralPurpose.Virtual(42, S32)
	if ToPhysical(v) != nil {
		t.Errorf("ToPhysical should be nil for virtual registers")
	}
	if ToPhysical(ECX) != ECX {
		t.Errorf("ToPhysical(p) != p for physical register")
	}
}

func TestFamilyLookup(t *testing.T) {
	cases := []struct {
		Family *Family
		ID     Index
		Spec   Spec
		Expect Physical
	}{
		{GeneralPurpose, 0, S8, AL},
		{GeneralPurpose, 1, S8L, CL},
		{GeneralPurpose, 2, S8H, DH},
		{GeneralPurpose, 3, S16, BX},
		{GeneralPurpose, 9, S32, R9L},
		{GeneralPurpose, 13, S64, R13},
		{GeneralPurpose, 13, S512, nil},
		{GeneralPurpose, 133, S64, nil},
		{Vector, 1, S128, X1},
		{Vector, 13, S256, Y13},
		{Vector, 27, S512, Z27},
		{Vector, 1, S16, nil},
		{Vector, 299, S256, nil},
	}
	for _, c := range cases {
		got := c.Family.Lookup(c.ID, c.Spec)
		if got != c.Expect {
			t.Errorf("idx=%v spec=%v: lookup got %v expect %v", c.ID, c.Spec, got, c.Expect)
		}
	}
}

func TestPhysicalAs(t *testing.T) {
	cases := []struct {
		Register Physical
		Spec     Spec
		Expect   Physical
	}{
		{DX, S8L, DL},
		{DX, S8H, DH},
		{DX, S8, DL},
		{DX, S16, DX},
		{DX, S32, EDX},
		{DX, S64, RDX},
		{DX, S256, nil},
	}
	for _, c := range cases {
		got := c.Register.as(c.Spec)
		if got != c.Expect {
			t.Errorf("%s.as(%v) = %v; expect %v", c.Register.Asm(), c.Spec, got, c.Expect)
		}
	}
}

func TestVirtualAs(t *testing.T) {
	v := GeneralPurpose.Virtual(0, S64)
	specs := []Spec{S8, S8L, S8H, S16, S32, S64}
	for _, s := range specs {
		if v.as(s).Mask() != s.Mask() {
			t.FailNow()
		}
	}
}

func TestLookupPhysical(t *testing.T) {
	cases := []struct {
		Kind   Kind
		Index  Index
		Spec   Spec
		Expect Physical
	}{
		{KindGP, 0, S8L, AL},
		{KindGP, 1, S8H, CH},
		{KindGP, 7, S8, DIB},
		{KindGP, 8, S16, R8W},
		{KindGP, 9, S32, R9L},
		{KindGP, 10, S64, R10},

		{KindVector, 7, S128, X7},
		{KindVector, 17, S256, Y17},
		{KindVector, 27, S512, Z27},
	}
	for _, c := range cases {
		if got := LookupPhysical(c.Kind, c.Index, c.Spec); !Equal(got, c.Expect) {
			t.FailNow()
		}
	}
}

func TestLookupIDSelf(t *testing.T) {
	cases := []Physical{AL, AH, AX, EAX, RAX, X1, Y2, Z31}
	for _, r := range cases {
		if got := LookupID(r.ID(), r.spec()); !Equal(got, r) {
			t.FailNow()
		}
	}
}