Files
avo/reg/collection.go

49 lines
1.7 KiB
Go
Raw Normal View History

2018-12-03 20:40:43 -08:00
package reg
2019-01-04 22:06:00 -08:00
// Collection represents a collection of virtual registers. This is primarily
// useful for allocating virtual registers with distinct IDs.
2018-12-03 20:40:43 -08:00
type Collection struct {
vid map[Kind]VID
}
2019-01-04 22:06:00 -08:00
// NewCollection builds an empty register collection.
2018-12-03 20:40:43 -08:00
func NewCollection() *Collection {
return &Collection{
vid: map[Kind]VID{},
}
}
2019-01-04 22:06:00 -08:00
// VirtualRegister allocates and returns a new virtual register of the given kind and size.
2018-12-03 20:40:43 -08:00
func (c *Collection) VirtualRegister(k Kind, s Size) Virtual {
vid := c.vid[k]
c.vid[k]++
return NewVirtual(vid, k, s)
}
2019-01-04 22:06:00 -08:00
// GP8 allocates and returns a general-purpose 8-bit register.
func (c *Collection) GP8() GPVirtual { return c.GP(B8) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// GP16 allocates and returns a general-purpose 16-bit register.
func (c *Collection) GP16() GPVirtual { return c.GP(B16) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// GP32 allocates and returns a general-purpose 32-bit register.
func (c *Collection) GP32() GPVirtual { return c.GP(B32) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// GP64 allocates and returns a general-purpose 64-bit register.
func (c *Collection) GP64() GPVirtual { return c.GP(B64) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// GP allocates and returns a general-purpose register of the given size.
func (c *Collection) GP(s Size) GPVirtual { return newgpv(c.VirtualRegister(KindGP, s)) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// XMM allocates and returns a 128-bit vector register.
func (c *Collection) XMM() VecVirtual { return c.Vec(B128) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// YMM allocates and returns a 256-bit vector register.
func (c *Collection) YMM() VecVirtual { return c.Vec(B256) }
2018-12-03 20:40:43 -08:00
2019-01-04 22:06:00 -08:00
// ZMM allocates and returns a 512-bit vector register.
func (c *Collection) ZMM() VecVirtual { return c.Vec(B512) }
2019-01-04 22:06:00 -08:00
// Vec allocates and returns a vector register of the given size.
func (c *Collection) Vec(s Size) VecVirtual { return newvecv(c.VirtualRegister(KindVector, s)) }