2018-11-11 22:17:06 -06:00
|
|
|
package reg
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
2019-04-01 20:27:44 -07:00
|
|
|
func TestSpecSize(t *testing.T) {
|
2018-11-11 22:17:06 -06:00
|
|
|
cases := []struct {
|
2019-04-01 20:27:44 -07:00
|
|
|
Spec Spec
|
|
|
|
|
Size uint
|
2018-11-11 22:17:06 -06:00
|
|
|
}{
|
2018-12-06 17:26:33 -08:00
|
|
|
{S0, 0},
|
2018-11-11 22:17:06 -06:00
|
|
|
{S8L, 1},
|
|
|
|
|
{S8H, 1},
|
|
|
|
|
{S16, 2},
|
|
|
|
|
{S32, 4},
|
|
|
|
|
{S64, 8},
|
|
|
|
|
{S128, 16},
|
|
|
|
|
{S256, 32},
|
|
|
|
|
{S512, 64},
|
|
|
|
|
}
|
|
|
|
|
for _, c := range cases {
|
2019-04-01 20:27:44 -07:00
|
|
|
if c.Spec.Size() != c.Size {
|
|
|
|
|
t.Errorf("%v.Size() = %d; expect = %d", c.Spec, c.Spec.Size(), c.Size)
|
2018-11-11 22:17:06 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-02 23:59:29 -08:00
|
|
|
|
2018-12-05 00:05:57 -08:00
|
|
|
func TestToVirtual(t *testing.T) {
|
|
|
|
|
v := GeneralPurpose.Virtual(42, B32)
|
|
|
|
|
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, B32)
|
|
|
|
|
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 TestAreConflicting(t *testing.T) {
|
|
|
|
|
cases := []struct {
|
|
|
|
|
X, Y Physical
|
|
|
|
|
Expect bool
|
|
|
|
|
}{
|
|
|
|
|
{ECX, X3, false},
|
|
|
|
|
{AL, AH, false},
|
|
|
|
|
{AL, AX, true},
|
|
|
|
|
{AL, BX, false},
|
|
|
|
|
{X3, Y4, false},
|
|
|
|
|
{X3, Y3, true},
|
|
|
|
|
{Y3, Z4, false},
|
|
|
|
|
{Y3, Z3, true},
|
|
|
|
|
}
|
|
|
|
|
for _, c := range cases {
|
|
|
|
|
if AreConflicting(c.X, c.Y) != c.Expect {
|
|
|
|
|
t.Errorf("AreConflicting(%s, %s) != %v", c.X, c.Y, c.Expect)
|
|
|
|
|
}
|
2018-12-02 23:59:29 -08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-30 18:40:45 -08:00
|
|
|
|
|
|
|
|
func TestFamilyLookup(t *testing.T) {
|
|
|
|
|
cases := []struct {
|
|
|
|
|
Family *Family
|
|
|
|
|
ID PID
|
|
|
|
|
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("pid=%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) {
|
|
|
|
|
cases := []struct {
|
|
|
|
|
Virtual Register
|
|
|
|
|
Physical Physical
|
|
|
|
|
Match bool
|
|
|
|
|
}{
|
|
|
|
|
{GeneralPurpose.Virtual(0, B8), CL, true},
|
|
|
|
|
{GeneralPurpose.Virtual(0, B8), CH, true},
|
|
|
|
|
{GeneralPurpose.Virtual(0, B32).as(S8L), CL, true},
|
|
|
|
|
{GeneralPurpose.Virtual(0, B32).as(S8L), CH, false},
|
|
|
|
|
{GeneralPurpose.Virtual(0, B16).as(S32), R9L, true},
|
|
|
|
|
{GeneralPurpose.Virtual(0, B16).as(S32), R9, false},
|
|
|
|
|
}
|
|
|
|
|
for _, c := range cases {
|
|
|
|
|
if c.Virtual.(Virtual).SatisfiedBy(c.Physical) != c.Match {
|
|
|
|
|
t.Errorf("%s.SatisfiedBy(%v) != %v", c.Virtual.Asm(), c.Physical, c.Match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|