refactor to use reg.Set

This commit is contained in:
Michael McLoughlin
2018-12-03 22:39:43 -08:00
parent faafa00e40
commit 9376a230cf
6 changed files with 118 additions and 30 deletions

71
reg/set.go Normal file
View File

@@ -0,0 +1,71 @@
package reg
// Set is a set of registers.
type Set map[ID]Register
// NewEmptySet builds an empty register set.
func NewEmptySet() Set {
return Set{}
}
// NewSetFromSlice forms a set from the given register list.
func NewSetFromSlice(rs []Register) Set {
s := NewEmptySet()
for _, r := range rs {
s.Add(r)
}
return s
}
// Clone returns a copy of s.
func (s Set) Clone() Set {
c := NewEmptySet()
for _, r := range s {
c.Add(r)
}
return c
}
// Add r to s.
func (s Set) Add(r Register) {
s[r.ID()] = r
}
// Discard removes r from s, if present.
func (s Set) Discard(r Register) {
delete(s, r.ID())
}
// Update adds every register in t to s.
func (s Set) Update(t Set) {
for _, r := range t {
s.Add(r)
}
}
// Difference returns the set of registers in s but not t.
func (s Set) Difference(t Set) Set {
d := s.Clone()
d.DifferenceUpdate(t)
return d
}
// DifferenceUpdate removes every element of t from s.
func (s Set) DifferenceUpdate(t Set) {
for _, r := range t {
s.Discard(r)
}
}
// Equals returns true if s and t contain the same registers.
func (s Set) Equals(t Set) bool {
if len(s) != len(t) {
return false
}
for _, r := range s {
if _, found := t[r.ID()]; !found {
return false
}
}
return true
}

12
reg/set_test.go Normal file
View File

@@ -0,0 +1,12 @@
package reg
import "testing"
func TestFamilyRegisterSets(t *testing.T) {
fs := []*Family{GeneralPurpose, SIMD}
for _, f := range fs {
if len(f.Set()) != len(f.Registers()) {
t.Fatal("family set and list should have same size")
}
}
}

View File

@@ -2,7 +2,6 @@ package reg
import (
"fmt"
"math"
)
type Size uint
@@ -41,6 +40,24 @@ func (f *Family) Virtual(id VID, s Size) Virtual {
return NewVirtual(id, f.Kind, s)
}
// Registers returns the registers in this family.
func (f *Family) Registers() []Physical {
rs := make([]Physical, 0, len(f.registers))
for _, r := range f.registers {
rs = append(rs, r)
}
return rs
}
// Set returns the set of registers in the family.
func (f *Family) Set() Set {
s := NewEmptySet()
for _, r := range f.registers {
s.Add(r)
}
return s
}
type private interface {
private()
}
@@ -82,7 +99,7 @@ func (v virtual) VirtualID() VID { return v.id }
func (v virtual) Kind() Kind { return v.kind }
func (v virtual) ID() ID {
return (ID(math.MaxUint16) << 16) | ID(v.VirtualID())
return (ID(1) << 31) | ID(v.VirtualID())
}
func (v virtual) Asm() string {
@@ -106,7 +123,7 @@ type register struct {
}
func (r register) PhysicalID() PID { return r.id }
func (r register) ID() ID { return ID(r.id) }
func (r register) ID() ID { return (ID(r.Mask()) << 16) | ID(r.id) }
func (r register) Kind() Kind { return r.kind }
func (r register) Asm() string { return r.name }
func (r register) private() {}