Debian Codegraph @

reg/types.go

raw symbols SCIP
1 package reg
2
3 import (
4 "errors"
5 "fmt"
6 )
7
8 // Kind is a class of registers.
9 type Kind uint8
10
11 // Index of a register within a kind.
12 type Index uint16
13
14 // Family is a collection of Physical registers of a common kind.
15 type Family struct {
16 Kind Kind
17 registers []Physical
18 }
19
20 // define builds a register and adds it to the Family.
21 func (f *Family) define(s Spec, idx Index, name string, flags ...Info) Physical {
22 r := newregister(f, s, idx, name, flags...)
23 f.add(r)
24 return r
25 }
26
27 // add r to the family.
28 func (f *Family) add(r Physical) {
29 if r.Kind() != f.Kind {
30 panic("bad kind")
31 }
32 f.registers = append(f.registers, r)
33 }
34
35 // Virtual returns a virtual register from this family's kind.
36 func (f *Family) Virtual(idx Index, s Spec) Virtual {
37 return NewVirtual(idx, f.Kind, s)
38 }
39
40 // Registers returns the registers in this family.
41 func (f *Family) Registers() []Physical {
42 return append([]Physical(nil), f.registers...)
43 }
44
45 // Lookup returns the register with given physical index and spec. Returns nil if no such register exists.
46 func (f *Family) Lookup(idx Index, s Spec) Physical {
47 for _, r := range f.registers {
48 if r.PhysicalIndex() == idx && r.Mask() == s.Mask() {
49 return r
50 }
51 }
52 return nil
53 }
54
55 // ID is a register identifier.
56 type ID uint32
57
58 // newid builds a new register ID from the virtual flag v, kind and index.
59 func newid(v uint8, kind Kind, idx Index) ID {
60 return ID(v) | (ID(kind) << 8) | (ID(idx) << 16)
61 }
62
63 // IsVirtual reports whether this is an ID for a virtual register.
64 func (id ID) IsVirtual() bool { return (id & 1) == 1 }
65
66 // IsPhysical reports whether this is an ID for a physical register.
67 func (id ID) IsPhysical() bool { return !id.IsVirtual() }
68
69 // Kind extracts the kind from the register ID.
70 func (id ID) Kind() Kind { return Kind(id >> 8) }
71
72 // Index extracts the index from the register ID.
73 func (id ID) Index() Index { return Index(id >> 16) }
74
75 // Register represents a virtual or physical register.
76 type Register interface {
77 ID() ID
78 Kind() Kind
79 Size() uint
80 Mask() uint16
81 Asm() string
82 as(Spec) Register
83 spec() Spec
84 register()
85 }
86
87 // Equal reports whether a and b are equal registers.
88 func Equal(a, b Register) bool {
89 return (a.ID() == b.ID()) && (a.Mask() == b.Mask())
90 }
91
92 // Virtual is a register of a given type and size, not yet allocated to a physical register.
93 type Virtual interface {
94 VirtualIndex() Index
95 Register
96 }
97
98 // ToVirtual converts r to Virtual if possible, otherwise returns nil.
99 func ToVirtual(r Register) Virtual {
100 if v, ok := r.(Virtual); ok {
101 return v
102 }
103 return nil
104 }
105
106 type virtual struct {
107 idx Index
108 kind Kind
109 Spec
110 }
111
112 // NewVirtual builds a Virtual register.
113 func NewVirtual(idx Index, k Kind, s Spec) Virtual {
114 return virtual{
115 idx: idx,
116 kind: k,
117 Spec: s,
118 }
119 }
120
121 func (v virtual) ID() ID { return newid(1, v.kind, v.idx) }
122 func (v virtual) VirtualIndex() Index { return v.idx }
123 func (v virtual) Kind() Kind { return v.kind }
124
125 func (v virtual) Asm() string {
126 // TODO(mbm): decide on virtual register syntax
127 return fmt.Sprintf("<virtual:%v:%v:%v>", v.idx, v.Kind(), v.Size())
128 }
129
130 func (v virtual) as(s Spec) Register {
131 return virtual{
132 idx: v.idx,
133 kind: v.kind,
134 Spec: s,
135 }
136 }
137
138 func (v virtual) spec() Spec { return v.Spec }
139 func (v virtual) register() {}
140
141 // Info is a bitmask of register properties.
142 type Info uint8
143
144 // Defined register Info flags.
145 const (
146 None Info = 0
147 Restricted Info = 1 << iota
148 BasePointer
149 )
150
151 // Physical is a concrete register.
152 type Physical interface {
153 PhysicalIndex() Index
154 Info() Info
155 Register
156 }
157
158 // ToPhysical converts r to Physical if possible, otherwise returns nil.
159 func ToPhysical(r Register) Physical {
160 if p, ok := r.(Physical); ok {
161 return p
162 }
163 return nil
164 }
165
166 // register implements Physical.
167 type register struct {
168 family *Family
169 idx Index
170 name string
171 info Info
172 Spec
173 }
174
175 func newregister(f *Family, s Spec, idx Index, name string, flags ...Info) register {
176 r := register{
177 family: f,
178 idx: idx,
179 name: name,
180 info: None,
181 Spec: s,
182 }
183 for _, flag := range flags {
184 r.info |= flag
185 }
186 return r
187 }
188
189 func (r register) ID() ID { return newid(0, r.Kind(), r.idx) }
190 func (r register) PhysicalIndex() Index { return r.idx }
191 func (r register) Kind() Kind { return r.family.Kind }
192 func (r register) Asm() string { return r.name }
193 func (r register) Info() Info { return r.info }
194
195 func (r register) as(s Spec) Register {
196 return r.family.Lookup(r.PhysicalIndex(), s)
197 }
198
199 func (r register) spec() Spec { return r.Spec }
200 func (r register) register() {}
201
202 // Spec defines the size of a register as well as the bit ranges it occupies in
203 // an underlying physical register.
204 type Spec uint16
205
206 // Spec values required for x86-64.
207 const (
208 S0 Spec = 0x0 // zero value reserved for pseudo registers
209 S8L Spec = 0x1
210 S8H Spec = 0x2
211 S8 = S8L
212 S16 Spec = 0x3
213 S32 Spec = 0x7
214 S64 Spec = 0xf
215 S128 Spec = 0x1f
216 S256 Spec = 0x3f
217 S512 Spec = 0x7f
218 )
219
220 // Mask returns a mask representing which bytes of an underlying register are
221 // used by this register. This is almost always the low bytes, except for the
222 // case of the high-byte registers. If bit n of the mask is set, this means
223 // bytes 2^(n-1) to 2^n-1 are used.
224 func (s Spec) Mask() uint16 {
225 return uint16(s)
226 }
227
228 // Size returns the register width in bytes.
229 func (s Spec) Size() uint {
230 x := uint(s)
231 return (x >> 1) + (x & 1)
232 }
233
234 // LookupPhysical returns the physical register with the given parameters, or nil if not found.
235 func LookupPhysical(k Kind, idx Index, s Spec) Physical {
236 f := FamilyOfKind(k)
237 if f == nil {
238 return nil
239 }
240 return f.Lookup(idx, s)
241 }
242
243 // LookupID returns the physical register with the given id and spec, or nil if not found.
244 func LookupID(id ID, s Spec) Physical {
245 if id.IsVirtual() {
246 return nil
247 }
248 return LookupPhysical(id.Kind(), id.Index(), s)
249 }
250
251 // Allocation records a register allocation.
252 type Allocation map[ID]ID
253
254 // NewEmptyAllocation builds an empty register allocation.
255 func NewEmptyAllocation() Allocation {
256 return Allocation{}
257 }
258
259 // Merge allocations from b into a. Errors if there is disagreement on a common
260 // register.
261 func (a Allocation) Merge(b Allocation) error {
262 for id, p := range b {
263 if alt, found := a[id]; found && alt != p {
264 return errors.New("disagreement on overlapping register")
265 }
266 a[id] = p
267 }
268 return nil
269 }
270
271 // LookupDefault returns the register ID assigned by this allocation, returning
272 // id if none is found.
273 func (a Allocation) LookupDefault(id ID) ID {
274 if _, found := a[id]; found {
275 return a[id]
276 }
277 return id
278 }
279
280 // LookupRegister the allocation for register r, or return nil if there is none.
281 func (a Allocation) LookupRegister(r Register) Physical {
282 // Return immediately if it is already a physical register.
283 if p := ToPhysical(r); p != nil {
284 return p
285 }
286
287 // Lookup an allocation for this virtual ID.
288 id, found := a[r.ID()]
289 if !found {
290 return nil
291 }
292
293 return LookupID(id, r.spec())
294 }
295
296 // LookupRegisterDefault returns the register assigned to r, or r itself if there is none.
297 func (a Allocation) LookupRegisterDefault(r Register) Register {
298 if r == nil {
299 return nil
300 }
301 if p := a.LookupRegister(r); p != nil {
302 return p
303 }
304 return r
305 }