| 1 | package reg |
| 2 | |
| 3 | // Collection represents a collection of virtual registers. This is primarily |
| 4 | // useful for allocating virtual registers with distinct IDs. |
| 5 | type Collection struct { |
| 6 | idx map[Kind]Index |
| 7 | } |
| 8 | |
| 9 | // NewCollection builds an empty register collection. |
| 10 | func NewCollection() *Collection { |
| 11 | return &Collection{ |
| 12 | idx: map[Kind]Index{}, |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | // VirtualRegister allocates and returns a new virtual register of the given kind and width. |
| 17 | func (c *Collection) VirtualRegister(k Kind, s Spec) Virtual { |
| 18 | idx := c.idx[k] |
| 19 | c.idx[k]++ |
| 20 | return NewVirtual(idx, k, s) |
| 21 | } |
| 22 | |
| 23 | // GP8L allocates and returns a general-purpose 8-bit register (low byte). |
| 24 | func (c *Collection) GP8L() GPVirtual { return c.GP(S8L) } |
| 25 | |
| 26 | // GP8H allocates and returns a general-purpose 8-bit register (high byte). |
| 27 | func (c *Collection) GP8H() GPVirtual { return c.GP(S8H) } |
| 28 | |
| 29 | // GP8 allocates and returns a general-purpose 8-bit register (low byte). |
| 30 | func (c *Collection) GP8() GPVirtual { return c.GP8L() } |
| 31 | |
| 32 | // GP16 allocates and returns a general-purpose 16-bit register. |
| 33 | func (c *Collection) GP16() GPVirtual { return c.GP(S16) } |
| 34 | |
| 35 | // GP32 allocates and returns a general-purpose 32-bit register. |
| 36 | func (c *Collection) GP32() GPVirtual { return c.GP(S32) } |
| 37 | |
| 38 | // GP64 allocates and returns a general-purpose 64-bit register. |
| 39 | func (c *Collection) GP64() GPVirtual { return c.GP(S64) } |
| 40 | |
| 41 | // GP allocates and returns a general-purpose register of the given width. |
| 42 | func (c *Collection) GP(s Spec) GPVirtual { return newgpv(c.VirtualRegister(KindGP, s)) } |
| 43 | |
| 44 | // XMM allocates and returns a 128-bit vector register. |
| 45 | func (c *Collection) XMM() VecVirtual { return c.Vec(S128) } |
| 46 | |
| 47 | // YMM allocates and returns a 256-bit vector register. |
| 48 | func (c *Collection) YMM() VecVirtual { return c.Vec(S256) } |
| 49 | |
| 50 | // ZMM allocates and returns a 512-bit vector register. |
| 51 | func (c *Collection) ZMM() VecVirtual { return c.Vec(S512) } |
| 52 | |
| 53 | // Vec allocates and returns a vector register of the given width. |
| 54 | func (c *Collection) Vec(s Spec) VecVirtual { return newvecv(c.VirtualRegister(KindVector, s)) } |
| 55 | |
| 56 | // K allocates and returns an opmask register. |
| 57 | func (c *Collection) K() OpmaskVirtual { return newopmaskv(c.VirtualRegister(KindOpmask, S64)) } |