| 1 | package reg |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "testing/quick" |
| 6 | ) |
| 7 | |
| 8 | func TestIDFields(t *testing.T) { |
| 9 | f := func(v uint8, kind Kind, idx Index) bool { |
| 10 | id := newid(v, kind, idx) |
| 11 | return id.Kind() == kind && id.Index() == idx |
| 12 | } |
| 13 | if err := quick.Check(f, nil); err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | func TestIDIsVirtual(t *testing.T) { |
| 19 | cases := []Virtual{ |
| 20 | GeneralPurpose.Virtual(42, S64), |
| 21 | Vector.Virtual(42, S128), |
| 22 | Opmask.Virtual(42, S64), |
| 23 | } |
| 24 | for _, r := range cases { |
| 25 | if !r.ID().IsVirtual() { |
| 26 | t.FailNow() |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestIDIsPhysical(t *testing.T) { |
| 32 | cases := []Physical{AL, AH, AX, EAX, RAX, X1, Y2, Z31, K1} |
| 33 | for _, r := range cases { |
| 34 | if !r.ID().IsPhysical() { |
| 35 | t.FailNow() |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestSpecSize(t *testing.T) { |
| 41 | cases := []struct { |
| 42 | Spec Spec |
| 43 | Size uint |
| 44 | }{ |
| 45 | {S0, 0}, |
| 46 | {S8L, 1}, |
| 47 | {S8H, 1}, |
| 48 | {S16, 2}, |
| 49 | {S32, 4}, |
| 50 | {S64, 8}, |
| 51 | {S128, 16}, |
| 52 | {S256, 32}, |
| 53 | {S512, 64}, |
| 54 | } |
| 55 | for _, c := range cases { |
| 56 | if c.Spec.Size() != c.Size { |
| 57 | t.Errorf("%v.Size() = %d; expect = %d", c.Spec, c.Spec.Size(), c.Size) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestToVirtual(t *testing.T) { |
| 63 | v := GeneralPurpose.Virtual(42, S32) |
| 64 | if ToVirtual(v) != v { |
| 65 | t.Errorf("ToVirtual(v) != v for virtual register") |
| 66 | } |
| 67 | if ToVirtual(ECX) != nil { |
| 68 | t.Errorf("ToVirtual should be nil for physical registers") |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestToPhysical(t *testing.T) { |
| 73 | v := GeneralPurpose.Virtual(42, S32) |
| 74 | if ToPhysical(v) != nil { |
| 75 | t.Errorf("ToPhysical should be nil for virtual registers") |
| 76 | } |
| 77 | if ToPhysical(ECX) != ECX { |
| 78 | t.Errorf("ToPhysical(p) != p for physical register") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestFamilyLookup(t *testing.T) { |
| 83 | cases := []struct { |
| 84 | Family *Family |
| 85 | ID Index |
| 86 | Spec Spec |
| 87 | Expect Physical |
| 88 | }{ |
| 89 | {GeneralPurpose, 0, S8, AL}, |
| 90 | {GeneralPurpose, 1, S8L, CL}, |
| 91 | {GeneralPurpose, 2, S8H, DH}, |
| 92 | {GeneralPurpose, 3, S16, BX}, |
| 93 | {GeneralPurpose, 9, S32, R9L}, |
| 94 | {GeneralPurpose, 13, S64, R13}, |
| 95 | {GeneralPurpose, 13, S512, nil}, |
| 96 | {GeneralPurpose, 133, S64, nil}, |
| 97 | {Vector, 1, S128, X1}, |
| 98 | {Vector, 13, S256, Y13}, |
| 99 | {Vector, 27, S512, Z27}, |
| 100 | {Vector, 1, S16, nil}, |
| 101 | {Vector, 299, S256, nil}, |
| 102 | {Opmask, 1, S64, K1}, |
| 103 | {Opmask, 8, S64, nil}, |
| 104 | {Opmask, 0, S64, K0}, |
| 105 | } |
| 106 | for _, c := range cases { |
| 107 | got := c.Family.Lookup(c.ID, c.Spec) |
| 108 | if got != c.Expect { |
| 109 | t.Errorf("idx=%v spec=%v: lookup got %v expect %v", c.ID, c.Spec, got, c.Expect) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestPhysicalAs(t *testing.T) { |
| 115 | cases := []struct { |
| 116 | Register Physical |
| 117 | Spec Spec |
| 118 | Expect Physical |
| 119 | }{ |
| 120 | {DX, S8L, DL}, |
| 121 | {DX, S8H, DH}, |
| 122 | {DX, S8, DL}, |
| 123 | {DX, S16, DX}, |
| 124 | {DX, S32, EDX}, |
| 125 | {DX, S64, RDX}, |
| 126 | {DX, S256, nil}, |
| 127 | } |
| 128 | for _, c := range cases { |
| 129 | got := c.Register.as(c.Spec) |
| 130 | if got != c.Expect { |
| 131 | t.Errorf("%s.as(%v) = %v; expect %v", c.Register.Asm(), c.Spec, got, c.Expect) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestVirtualAs(t *testing.T) { |
| 137 | v := GeneralPurpose.Virtual(0, S64) |
| 138 | specs := []Spec{S8, S8L, S8H, S16, S32, S64} |
| 139 | for _, s := range specs { |
| 140 | if v.as(s).Mask() != s.Mask() { |
| 141 | t.FailNow() |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestLookupPhysical(t *testing.T) { |
| 147 | cases := []struct { |
| 148 | Kind Kind |
| 149 | Index Index |
| 150 | Spec Spec |
| 151 | Expect Physical |
| 152 | }{ |
| 153 | {KindGP, 0, S8L, AL}, |
| 154 | {KindGP, 1, S8H, CH}, |
| 155 | {KindGP, 7, S8, DIB}, |
| 156 | {KindGP, 8, S16, R8W}, |
| 157 | {KindGP, 9, S32, R9L}, |
| 158 | {KindGP, 10, S64, R10}, |
| 159 | |
| 160 | {KindVector, 7, S128, X7}, |
| 161 | {KindVector, 17, S256, Y17}, |
| 162 | {KindVector, 27, S512, Z27}, |
| 163 | |
| 164 | {KindOpmask, 1, S64, K1}, |
| 165 | } |
| 166 | for _, c := range cases { |
| 167 | if got := LookupPhysical(c.Kind, c.Index, c.Spec); !Equal(got, c.Expect) { |
| 168 | t.FailNow() |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestLookupIDSelf(t *testing.T) { |
| 174 | cases := []Physical{AL, AH, AX, EAX, RAX, X1, Y2, Z31, K1} |
| 175 | for _, r := range cases { |
| 176 | if got := LookupID(r.ID(), r.spec()); !Equal(got, r) { |
| 177 | t.FailNow() |
| 178 | } |
| 179 | } |
| 180 | } |