github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/engine/wazevo/backend/regalloc/regset.go (about) 1 package regalloc 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type regSet uint64 9 10 func (rs regSet) format(info *RegisterInfo) string { //nolint:unused 11 var ret []string 12 for i := 0; i < 64; i++ { 13 if rs&(1<<uint(i)) != 0 { 14 ret = append(ret, info.RealRegName(RealReg(i))) 15 } 16 } 17 return strings.Join(ret, ", ") 18 } 19 20 func (rs regSet) has(r RealReg) bool { 21 return rs&(1<<uint(r)) != 0 22 } 23 24 func (rs regSet) add(r RealReg) regSet { 25 if r >= 64 { 26 return rs 27 } 28 return rs | 1<<uint(r) 29 } 30 31 func (rs regSet) range_(f func(allocatedRealReg RealReg)) { 32 for i := 0; i < 64; i++ { 33 if rs&(1<<uint(i)) != 0 { 34 f(RealReg(i)) 35 } 36 } 37 } 38 39 type regInUseSet struct { 40 set regSet 41 vrs [64]VReg 42 } 43 44 func (rs *regInUseSet) reset() { 45 rs.set = 0 46 for i := range rs.vrs { 47 rs.vrs[i] = VRegInvalid 48 } 49 } 50 51 func (rs *regInUseSet) format(info *RegisterInfo) string { //nolint:unused 52 var ret []string 53 for i := 0; i < 64; i++ { 54 if rs.set&(1<<uint(i)) != 0 { 55 vr := rs.vrs[i] 56 ret = append(ret, fmt.Sprintf("(%s->v%d)", info.RealRegName(RealReg(i)), vr.ID())) 57 } 58 } 59 return strings.Join(ret, ", ") 60 } 61 62 func (rs *regInUseSet) has(r RealReg) bool { 63 if r >= 64 { 64 return false 65 } 66 return rs.set&(1<<uint(r)) != 0 67 } 68 69 func (rs *regInUseSet) get(r RealReg) VReg { 70 if r >= 64 { 71 return VRegInvalid 72 } 73 return rs.vrs[r] 74 } 75 76 func (rs *regInUseSet) remove(r RealReg) { 77 if r >= 64 { 78 return 79 } 80 rs.set &= ^(1 << uint(r)) 81 rs.vrs[r] = VRegInvalid 82 } 83 84 func (rs *regInUseSet) add(r RealReg, vr VReg) { 85 if r >= 64 { 86 return 87 } 88 rs.set |= 1 << uint(r) 89 rs.vrs[r] = vr 90 } 91 92 func (rs *regInUseSet) range_(f func(allocatedRealReg RealReg, vr VReg)) { 93 for i := 0; i < 64; i++ { 94 if rs.set&(1<<uint(i)) != 0 { 95 f(RealReg(i), rs.vrs[i]) 96 } 97 } 98 }