| 1 | package reg |
| 2 | |
| 3 | // MaskSet maps register IDs to masks. |
| 4 | type MaskSet map[ID]uint16 |
| 5 | |
| 6 | // NewEmptyMaskSet builds an empty register mask set. |
| 7 | func NewEmptyMaskSet() MaskSet { |
| 8 | return MaskSet{} |
| 9 | } |
| 10 | |
| 11 | // NewMaskSetFromRegisters forms a mask set from the given register list. |
| 12 | func NewMaskSetFromRegisters(rs []Register) MaskSet { |
| 13 | s := NewEmptyMaskSet() |
| 14 | for _, r := range rs { |
| 15 | s.AddRegister(r) |
| 16 | } |
| 17 | return s |
| 18 | } |
| 19 | |
| 20 | // Clone returns a copy of s. |
| 21 | func (s MaskSet) Clone() MaskSet { |
| 22 | c := NewEmptyMaskSet() |
| 23 | for id, mask := range s { |
| 24 | c.Add(id, mask) |
| 25 | } |
| 26 | return c |
| 27 | } |
| 28 | |
| 29 | // Add mask to the given register ID. |
| 30 | // Reports whether this made any change to the set. |
| 31 | func (s MaskSet) Add(id ID, mask uint16) bool { |
| 32 | if (s[id] & mask) == mask { |
| 33 | return false |
| 34 | } |
| 35 | s[id] |= mask |
| 36 | return true |
| 37 | } |
| 38 | |
| 39 | // AddRegister is a convenience for adding the register's (ID, mask) to the set. |
| 40 | // Reports whether this made any change to the set. |
| 41 | func (s MaskSet) AddRegister(r Register) bool { |
| 42 | return s.Add(r.ID(), r.Mask()) |
| 43 | } |
| 44 | |
| 45 | // Discard clears masked bits from register ID. |
| 46 | // Reports whether this made any change to the set. |
| 47 | func (s MaskSet) Discard(id ID, mask uint16) bool { |
| 48 | if curr, found := s[id]; !found || (curr&mask) == 0 { |
| 49 | return false |
| 50 | } |
| 51 | s[id] &^= mask |
| 52 | if s[id] == 0 { |
| 53 | delete(s, id) |
| 54 | } |
| 55 | return true |
| 56 | } |
| 57 | |
| 58 | // DiscardRegister is a convenience for discarding the register's (ID, mask) from the set. |
| 59 | // Reports whether this made any change to the set. |
| 60 | func (s MaskSet) DiscardRegister(r Register) bool { |
| 61 | return s.Discard(r.ID(), r.Mask()) |
| 62 | } |
| 63 | |
| 64 | // Update adds masks in t to s. |
| 65 | // Reports whether this made any change to the set. |
| 66 | func (s MaskSet) Update(t MaskSet) bool { |
| 67 | change := false |
| 68 | for id, mask := range t { |
| 69 | change = s.Add(id, mask) || change |
| 70 | } |
| 71 | return change |
| 72 | } |
| 73 | |
| 74 | // Difference returns the set of registers in s but not t. |
| 75 | func (s MaskSet) Difference(t MaskSet) MaskSet { |
| 76 | d := s.Clone() |
| 77 | d.DifferenceUpdate(t) |
| 78 | return d |
| 79 | } |
| 80 | |
| 81 | // DifferenceUpdate removes every element of t from s. |
| 82 | func (s MaskSet) DifferenceUpdate(t MaskSet) bool { |
| 83 | change := false |
| 84 | for id, mask := range t { |
| 85 | change = s.Discard(id, mask) || change |
| 86 | } |
| 87 | return change |
| 88 | } |
| 89 | |
| 90 | // Equals returns true if s and t contain the same masks. |
| 91 | func (s MaskSet) Equals(t MaskSet) bool { |
| 92 | if len(s) != len(t) { |
| 93 | return false |
| 94 | } |
| 95 | for id, mask := range s { |
| 96 | if _, found := t[id]; !found || mask != t[id] { |
| 97 | return false |
| 98 | } |
| 99 | } |
| 100 | return true |
| 101 | } |
| 102 | |
| 103 | // OfKind returns the set of elements of s with kind k. |
| 104 | func (s MaskSet) OfKind(k Kind) MaskSet { |
| 105 | t := NewEmptyMaskSet() |
| 106 | for id, mask := range s { |
| 107 | if id.Kind() == k { |
| 108 | t.Add(id, mask) |
| 109 | } |
| 110 | } |
| 111 | return t |
| 112 | } |