reg: rename Bytes() to Size() (#74)
It was pointed out #73 that Bytes() is a poor name for the size of the register in bytes. In idiomatic Go you would probably expect a Bytes() method to return []byte. This diff changes the Bytes() to Size(). As a result the Size type also needed to be renamed, and Width seemed a reasonable choice. Fixes #73
This commit is contained in:
committed by
GitHub
parent
7a0eb66183
commit
138eaf8dc3
@@ -39,7 +39,7 @@ func (c *Context) Load(src gotypes.Component, dst reg.Register) reg.Register {
|
|||||||
c.adderror(err)
|
c.adderror(err)
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Bytes()), b.Type)
|
c.mov(b.Addr, dst, int(gotypes.Sizes.Sizeof(b.Type)), int(dst.Size()), b.Type)
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ func (c *Context) Store(src reg.Register, dst gotypes.Component) {
|
|||||||
c.adderror(err)
|
c.adderror(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.mov(src, b.Addr, int(src.Bytes()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type)
|
c.mov(src, b.Addr, int(src.Size()), int(gotypes.Sizes.Sizeof(b.Type)), b.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dereference loads a pointer and returns its element type.
|
// Dereference loads a pointer and returns its element type.
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ func IsYMM(op Op) bool {
|
|||||||
// IsRegisterKindSize returns true if op is a register of the given kind and size in bytes.
|
// IsRegisterKindSize returns true if op is a register of the given kind and size in bytes.
|
||||||
func IsRegisterKindSize(op Op, k reg.Kind, n uint) bool {
|
func IsRegisterKindSize(op Op, k reg.Kind, n uint) bool {
|
||||||
r, ok := op.(reg.Register)
|
r, ok := op.(reg.Register)
|
||||||
return ok && r.Kind() == k && r.Bytes() == n
|
return ok && r.Kind() == k && r.Size() == n
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRegisterKind returns true if op is a register of the given kind.
|
// IsRegisterKind returns true if op is a register of the given kind.
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ func NewCollection() *Collection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VirtualRegister allocates and returns a new virtual register of the given kind and size.
|
// VirtualRegister allocates and returns a new virtual register of the given kind and width.
|
||||||
func (c *Collection) VirtualRegister(k Kind, s Size) Virtual {
|
func (c *Collection) VirtualRegister(k Kind, w Width) Virtual {
|
||||||
vid := c.vid[k]
|
vid := c.vid[k]
|
||||||
c.vid[k]++
|
c.vid[k]++
|
||||||
return NewVirtual(vid, k, s)
|
return NewVirtual(vid, k, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GP8 allocates and returns a general-purpose 8-bit register.
|
// GP8 allocates and returns a general-purpose 8-bit register.
|
||||||
@@ -32,8 +32,8 @@ func (c *Collection) GP32() GPVirtual { return c.GP(B32) }
|
|||||||
// GP64 allocates and returns a general-purpose 64-bit register.
|
// GP64 allocates and returns a general-purpose 64-bit register.
|
||||||
func (c *Collection) GP64() GPVirtual { return c.GP(B64) }
|
func (c *Collection) GP64() GPVirtual { return c.GP(B64) }
|
||||||
|
|
||||||
// GP allocates and returns a general-purpose register of the given size.
|
// GP allocates and returns a general-purpose register of the given width.
|
||||||
func (c *Collection) GP(s Size) GPVirtual { return newgpv(c.VirtualRegister(KindGP, s)) }
|
func (c *Collection) GP(w Width) GPVirtual { return newgpv(c.VirtualRegister(KindGP, w)) }
|
||||||
|
|
||||||
// XMM allocates and returns a 128-bit vector register.
|
// XMM allocates and returns a 128-bit vector register.
|
||||||
func (c *Collection) XMM() VecVirtual { return c.Vec(B128) }
|
func (c *Collection) XMM() VecVirtual { return c.Vec(B128) }
|
||||||
@@ -44,5 +44,5 @@ func (c *Collection) YMM() VecVirtual { return c.Vec(B256) }
|
|||||||
// ZMM allocates and returns a 512-bit vector register.
|
// ZMM allocates and returns a 512-bit vector register.
|
||||||
func (c *Collection) ZMM() VecVirtual { return c.Vec(B512) }
|
func (c *Collection) ZMM() VecVirtual { return c.Vec(B512) }
|
||||||
|
|
||||||
// Vec allocates and returns a vector register of the given size.
|
// Vec allocates and returns a vector register of the given width.
|
||||||
func (c *Collection) Vec(s Size) VecVirtual { return newvecv(c.VirtualRegister(KindVector, s)) }
|
func (c *Collection) Vec(w Width) VecVirtual { return newvecv(c.VirtualRegister(KindVector, w)) }
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package reg
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestSpecBytes(t *testing.T) {
|
func TestSpecSize(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Spec Spec
|
Spec Spec
|
||||||
Bytes uint
|
Size uint
|
||||||
}{
|
}{
|
||||||
{S0, 0},
|
{S0, 0},
|
||||||
{S8L, 1},
|
{S8L, 1},
|
||||||
@@ -18,8 +18,8 @@ func TestSpecBytes(t *testing.T) {
|
|||||||
{S512, 64},
|
{S512, 64},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
if c.Spec.Bytes() != c.Bytes {
|
if c.Spec.Size() != c.Size {
|
||||||
t.Errorf("%v.Bytes() = %d; expect = %d", c.Spec, c.Spec.Bytes(), c.Bytes)
|
t.Errorf("%v.Size() = %d; expect = %d", c.Spec, c.Spec.Size(), c.Size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
reg/types.go
44
reg/types.go
@@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Size is a register size.
|
// Width is a register width.
|
||||||
type Size uint
|
type Width uint
|
||||||
|
|
||||||
// Typical register sizes.
|
// Typical register width values.
|
||||||
const (
|
const (
|
||||||
B8 Size = 1 << iota
|
B8 Width = 1 << iota
|
||||||
B16
|
B16
|
||||||
B32
|
B32
|
||||||
B64
|
B64
|
||||||
@@ -19,8 +19,8 @@ const (
|
|||||||
B512
|
B512
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bytes returns the register size in bytes.
|
// Size returns the register width in bytes.
|
||||||
func (s Size) Bytes() uint { return uint(s) }
|
func (w Width) Size() uint { return uint(w) }
|
||||||
|
|
||||||
// Kind is a class of registers.
|
// Kind is a class of registers.
|
||||||
type Kind uint8
|
type Kind uint8
|
||||||
@@ -47,8 +47,8 @@ func (f *Family) add(r Physical) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Virtual returns a virtual register from this family's kind.
|
// Virtual returns a virtual register from this family's kind.
|
||||||
func (f *Family) Virtual(id VID, s Size) Virtual {
|
func (f *Family) Virtual(id VID, w Width) Virtual {
|
||||||
return NewVirtual(id, f.Kind, s)
|
return NewVirtual(id, f.Kind, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registers returns the registers in this family.
|
// Registers returns the registers in this family.
|
||||||
@@ -78,7 +78,7 @@ func (f *Family) Lookup(id PID, s Spec) Physical {
|
|||||||
// Register represents a virtual or physical register.
|
// Register represents a virtual or physical register.
|
||||||
type Register interface {
|
type Register interface {
|
||||||
Kind() Kind
|
Kind() Kind
|
||||||
Bytes() uint
|
Size() uint
|
||||||
Asm() string
|
Asm() string
|
||||||
as(Spec) Register
|
as(Spec) Register
|
||||||
register()
|
register()
|
||||||
@@ -105,16 +105,16 @@ func ToVirtual(r Register) Virtual {
|
|||||||
type virtual struct {
|
type virtual struct {
|
||||||
id VID
|
id VID
|
||||||
kind Kind
|
kind Kind
|
||||||
Size
|
Width
|
||||||
mask uint16
|
mask uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVirtual builds a Virtual register.
|
// NewVirtual builds a Virtual register.
|
||||||
func NewVirtual(id VID, k Kind, s Size) Virtual {
|
func NewVirtual(id VID, k Kind, w Width) Virtual {
|
||||||
return virtual{
|
return virtual{
|
||||||
id: id,
|
id: id,
|
||||||
kind: k,
|
kind: k,
|
||||||
Size: s,
|
Width: w,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,19 +123,19 @@ func (v virtual) Kind() Kind { return v.kind }
|
|||||||
|
|
||||||
func (v virtual) Asm() string {
|
func (v virtual) Asm() string {
|
||||||
// TODO(mbm): decide on virtual register syntax
|
// TODO(mbm): decide on virtual register syntax
|
||||||
return fmt.Sprintf("<virtual:%v:%v:%v>", v.id, v.Kind(), v.Bytes())
|
return fmt.Sprintf("<virtual:%v:%v:%v>", v.id, v.Kind(), v.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v virtual) SatisfiedBy(p Physical) bool {
|
func (v virtual) SatisfiedBy(p Physical) bool {
|
||||||
return v.Kind() == p.Kind() && v.Bytes() == p.Bytes() && (v.mask == 0 || v.mask == p.Mask())
|
return v.Kind() == p.Kind() && v.Size() == p.Size() && (v.mask == 0 || v.mask == p.Mask())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v virtual) as(s Spec) Register {
|
func (v virtual) as(s Spec) Register {
|
||||||
return virtual{
|
return virtual{
|
||||||
id: v.id,
|
id: v.id,
|
||||||
kind: v.kind,
|
kind: v.kind,
|
||||||
Size: Size(s.Bytes()),
|
Width: Width(s.Size()),
|
||||||
mask: s.Mask(),
|
mask: s.Mask(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +229,8 @@ func (s Spec) Mask() uint16 {
|
|||||||
return uint16(s)
|
return uint16(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes returns the register size in bytes.
|
// Size returns the register width in bytes.
|
||||||
func (s Spec) Bytes() uint {
|
func (s Spec) Size() uint {
|
||||||
x := uint(s)
|
x := uint(s)
|
||||||
return (x >> 1) + (x & 1)
|
return (x >> 1) + (x & 1)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user