reg

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: BSD-3-Clause Imports: 2 Imported by: 49

Documentation

Overview

Package reg provides types for physical and virtual registers, and definitions of x86-64 register families.

Index

Constants

This section is empty.

Variables

View Source
var (
	Pseudo         = &Family{Kind: KindPseudo}
	GeneralPurpose = &Family{Kind: KindGP}
	Vector         = &Family{Kind: KindVector}
	Opmask         = &Family{Kind: KindOpmask}

	Families = []*Family{
		Pseudo,
		GeneralPurpose,
		Vector,
		Opmask,
	}
)

Declare register families.

View Source
var (
	FramePointer   = Pseudo.define(S0, 0, "FP")
	ProgramCounter = Pseudo.define(S0, 0, "PC")
	StaticBase     = Pseudo.define(S0, 0, "SB")
	StackPointer   = Pseudo.define(S0, 0, "SP")
)

Pseudo registers.

View Source
var (
	// Low byte.
	AL = gp(S8L, 0, "AL")
	CL = gp(S8L, 1, "CL")
	DL = gp(S8L, 2, "DL")
	BL = gp(S8L, 3, "BL")

	// High byte.
	AH = gp(S8H, 0, "AH")
	CH = gp(S8H, 1, "CH")
	DH = gp(S8H, 2, "DH")
	BH = gp(S8H, 3, "BH")

	// 8-bit.
	SPB  = gp(S8, 4, "SP", Restricted)
	BPB  = gp(S8, 5, "BP", BasePointer)
	SIB  = gp(S8, 6, "SI")
	DIB  = gp(S8, 7, "DI")
	R8B  = gp(S8, 8, "R8")
	R9B  = gp(S8, 9, "R9")
	R10B = gp(S8, 10, "R10")
	R11B = gp(S8, 11, "R11")
	R12B = gp(S8, 12, "R12")
	R13B = gp(S8, 13, "R13")
	R14B = gp(S8, 14, "R14")
	R15B = gp(S8, 15, "R15")

	// 16-bit.
	AX   = gp(S16, 0, "AX")
	CX   = gp(S16, 1, "CX")
	DX   = gp(S16, 2, "DX")
	BX   = gp(S16, 3, "BX")
	SP   = gp(S16, 4, "SP", Restricted)
	BP   = gp(S16, 5, "BP", BasePointer)
	SI   = gp(S16, 6, "SI")
	DI   = gp(S16, 7, "DI")
	R8W  = gp(S16, 8, "R8")
	R9W  = gp(S16, 9, "R9")
	R10W = gp(S16, 10, "R10")
	R11W = gp(S16, 11, "R11")
	R12W = gp(S16, 12, "R12")
	R13W = gp(S16, 13, "R13")
	R14W = gp(S16, 14, "R14")
	R15W = gp(S16, 15, "R15")

	// 32-bit.
	EAX  = gp(S32, 0, "AX")
	ECX  = gp(S32, 1, "CX")
	EDX  = gp(S32, 2, "DX")
	EBX  = gp(S32, 3, "BX")
	ESP  = gp(S32, 4, "SP", Restricted)
	EBP  = gp(S32, 5, "BP", BasePointer)
	ESI  = gp(S32, 6, "SI")
	EDI  = gp(S32, 7, "DI")
	R8L  = gp(S32, 8, "R8")
	R9L  = gp(S32, 9, "R9")
	R10L = gp(S32, 10, "R10")
	R11L = gp(S32, 11, "R11")
	R12L = gp(S32, 12, "R12")
	R13L = gp(S32, 13, "R13")
	R14L = gp(S32, 14, "R14")
	R15L = gp(S32, 15, "R15")

	// 64-bit.
	RAX = gp(S64, 0, "AX")
	RCX = gp(S64, 1, "CX")
	RDX = gp(S64, 2, "DX")
	RBX = gp(S64, 3, "BX")
	RSP = gp(S64, 4, "SP", Restricted)
	RBP = gp(S64, 5, "BP", BasePointer)
	RSI = gp(S64, 6, "SI")
	RDI = gp(S64, 7, "DI")
	R8  = gp(S64, 8, "R8")
	R9  = gp(S64, 9, "R9")
	R10 = gp(S64, 10, "R10")
	R11 = gp(S64, 11, "R11")
	R12 = gp(S64, 12, "R12")
	R13 = gp(S64, 13, "R13")
	R14 = gp(S64, 14, "R14")
	R15 = gp(S64, 15, "R15")
)

General purpose registers.

View Source
var (
	// 128-bit.
	X0  = vec(S128, 0, "X0")
	X1  = vec(S128, 1, "X1")
	X2  = vec(S128, 2, "X2")
	X3  = vec(S128, 3, "X3")
	X4  = vec(S128, 4, "X4")
	X5  = vec(S128, 5, "X5")
	X6  = vec(S128, 6, "X6")
	X7  = vec(S128, 7, "X7")
	X8  = vec(S128, 8, "X8")
	X9  = vec(S128, 9, "X9")
	X10 = vec(S128, 10, "X10")
	X11 = vec(S128, 11, "X11")
	X12 = vec(S128, 12, "X12")
	X13 = vec(S128, 13, "X13")
	X14 = vec(S128, 14, "X14")
	X15 = vec(S128, 15, "X15")
	X16 = vec(S128, 16, "X16")
	X17 = vec(S128, 17, "X17")
	X18 = vec(S128, 18, "X18")
	X19 = vec(S128, 19, "X19")
	X20 = vec(S128, 20, "X20")
	X21 = vec(S128, 21, "X21")
	X22 = vec(S128, 22, "X22")
	X23 = vec(S128, 23, "X23")
	X24 = vec(S128, 24, "X24")
	X25 = vec(S128, 25, "X25")
	X26 = vec(S128, 26, "X26")
	X27 = vec(S128, 27, "X27")
	X28 = vec(S128, 28, "X28")
	X29 = vec(S128, 29, "X29")
	X30 = vec(S128, 30, "X30")
	X31 = vec(S128, 31, "X31")

	// 256-bit.
	Y0  = vec(S256, 0, "Y0")
	Y1  = vec(S256, 1, "Y1")
	Y2  = vec(S256, 2, "Y2")
	Y3  = vec(S256, 3, "Y3")
	Y4  = vec(S256, 4, "Y4")
	Y5  = vec(S256, 5, "Y5")
	Y6  = vec(S256, 6, "Y6")
	Y7  = vec(S256, 7, "Y7")
	Y8  = vec(S256, 8, "Y8")
	Y9  = vec(S256, 9, "Y9")
	Y10 = vec(S256, 10, "Y10")
	Y11 = vec(S256, 11, "Y11")
	Y12 = vec(S256, 12, "Y12")
	Y13 = vec(S256, 13, "Y13")
	Y14 = vec(S256, 14, "Y14")
	Y15 = vec(S256, 15, "Y15")
	Y16 = vec(S256, 16, "Y16")
	Y17 = vec(S256, 17, "Y17")
	Y18 = vec(S256, 18, "Y18")
	Y19 = vec(S256, 19, "Y19")
	Y20 = vec(S256, 20, "Y20")
	Y21 = vec(S256, 21, "Y21")
	Y22 = vec(S256, 22, "Y22")
	Y23 = vec(S256, 23, "Y23")
	Y24 = vec(S256, 24, "Y24")
	Y25 = vec(S256, 25, "Y25")
	Y26 = vec(S256, 26, "Y26")
	Y27 = vec(S256, 27, "Y27")
	Y28 = vec(S256, 28, "Y28")
	Y29 = vec(S256, 29, "Y29")
	Y30 = vec(S256, 30, "Y30")
	Y31 = vec(S256, 31, "Y31")

	// 512-bit.
	Z0  = vec(S512, 0, "Z0")
	Z1  = vec(S512, 1, "Z1")
	Z2  = vec(S512, 2, "Z2")
	Z3  = vec(S512, 3, "Z3")
	Z4  = vec(S512, 4, "Z4")
	Z5  = vec(S512, 5, "Z5")
	Z6  = vec(S512, 6, "Z6")
	Z7  = vec(S512, 7, "Z7")
	Z8  = vec(S512, 8, "Z8")
	Z9  = vec(S512, 9, "Z9")
	Z10 = vec(S512, 10, "Z10")
	Z11 = vec(S512, 11, "Z11")
	Z12 = vec(S512, 12, "Z12")
	Z13 = vec(S512, 13, "Z13")
	Z14 = vec(S512, 14, "Z14")
	Z15 = vec(S512, 15, "Z15")
	Z16 = vec(S512, 16, "Z16")
	Z17 = vec(S512, 17, "Z17")
	Z18 = vec(S512, 18, "Z18")
	Z19 = vec(S512, 19, "Z19")
	Z20 = vec(S512, 20, "Z20")
	Z21 = vec(S512, 21, "Z21")
	Z22 = vec(S512, 22, "Z22")
	Z23 = vec(S512, 23, "Z23")
	Z24 = vec(S512, 24, "Z24")
	Z25 = vec(S512, 25, "Z25")
	Z26 = vec(S512, 26, "Z26")
	Z27 = vec(S512, 27, "Z27")
	Z28 = vec(S512, 28, "Z28")
	Z29 = vec(S512, 29, "Z29")
	Z30 = vec(S512, 30, "Z30")
	Z31 = vec(S512, 31, "Z31")
)

Vector registers.

View Source
var (
	K0 = opmask(S64, 0, "K0", Restricted)
	K1 = opmask(S64, 1, "K1")
	K2 = opmask(S64, 2, "K2")
	K3 = opmask(S64, 3, "K3")
	K4 = opmask(S64, 4, "K4")
	K5 = opmask(S64, 5, "K5")
	K6 = opmask(S64, 6, "K6")
	K7 = opmask(S64, 7, "K7")
)

Opmask registers.

Note that while K0 is a physical opmask register (it is a valid opmask source and destination operand), it cannot be used as an opmask predicate value because in that context K0 means "all true" or "no mask" regardless of the actual contents of the physical register. For that reason, K0 should never be assigned as a "general purpose" opmask register. However, it can be explicitly operated upon by name as non-predicate operand, for example to hold a constant or temporary value during calculations on other opmask registers.

Functions

func Equal

func Equal(a, b Register) bool

Equal reports whether a and b are equal registers.

Types

type Allocation

type Allocation map[ID]ID

Allocation records a register allocation.

func NewEmptyAllocation

func NewEmptyAllocation() Allocation

NewEmptyAllocation builds an empty register allocation.

func (Allocation) LookupDefault

func (a Allocation) LookupDefault(id ID) ID

LookupDefault returns the register ID assigned by this allocation, returning id if none is found.

func (Allocation) LookupRegister

func (a Allocation) LookupRegister(r Register) Physical

LookupRegister the allocation for register r, or return nil if there is none.

func (Allocation) LookupRegisterDefault

func (a Allocation) LookupRegisterDefault(r Register) Register

LookupRegisterDefault returns the register assigned to r, or r itself if there is none.

func (Allocation) Merge

func (a Allocation) Merge(b Allocation) error

Merge allocations from b into a. Errors if there is disagreement on a common register.

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection represents a collection of virtual registers. This is primarily useful for allocating virtual registers with distinct IDs.

func NewCollection

func NewCollection() *Collection

NewCollection builds an empty register collection.

func (*Collection) GP

func (c *Collection) GP(s Spec) GPVirtual

GP allocates and returns a general-purpose register of the given width.

func (*Collection) GP16

func (c *Collection) GP16() GPVirtual

GP16 allocates and returns a general-purpose 16-bit register.

func (*Collection) GP32

func (c *Collection) GP32() GPVirtual

GP32 allocates and returns a general-purpose 32-bit register.

func (*Collection) GP64

func (c *Collection) GP64() GPVirtual

GP64 allocates and returns a general-purpose 64-bit register.

func (*Collection) GP8

func (c *Collection) GP8() GPVirtual

GP8 allocates and returns a general-purpose 8-bit register (low byte).

func (*Collection) GP8H

func (c *Collection) GP8H() GPVirtual

GP8H allocates and returns a general-purpose 8-bit register (high byte).

func (*Collection) GP8L

func (c *Collection) GP8L() GPVirtual

GP8L allocates and returns a general-purpose 8-bit register (low byte).

func (*Collection) K added in v0.4.0

func (c *Collection) K() OpmaskVirtual

K allocates and returns an opmask register.

func (*Collection) Vec

func (c *Collection) Vec(s Spec) VecVirtual

Vec allocates and returns a vector register of the given width.

func (*Collection) VirtualRegister

func (c *Collection) VirtualRegister(k Kind, s Spec) Virtual

VirtualRegister allocates and returns a new virtual register of the given kind and width.

func (*Collection) XMM

func (c *Collection) XMM() VecVirtual

XMM allocates and returns a 128-bit vector register.

func (*Collection) YMM

func (c *Collection) YMM() VecVirtual

YMM allocates and returns a 256-bit vector register.

func (*Collection) ZMM

func (c *Collection) ZMM() VecVirtual

ZMM allocates and returns a 512-bit vector register.

type Family

type Family struct {
	Kind Kind
	// contains filtered or unexported fields
}

Family is a collection of Physical registers of a common kind.

func FamilyOfKind

func FamilyOfKind(k Kind) *Family

FamilyOfKind returns the Family of registers of the given kind, or nil if not found.

func (*Family) Lookup

func (f *Family) Lookup(idx Index, s Spec) Physical

Lookup returns the register with given physical index and spec. Returns nil if no such register exists.

func (*Family) Registers

func (f *Family) Registers() []Physical

Registers returns the registers in this family.

func (*Family) Virtual

func (f *Family) Virtual(idx Index, s Spec) Virtual

Virtual returns a virtual register from this family's kind.

type GP

type GP interface {
	As8() Register
	As8L() Register
	As8H() Register
	As16() Register
	As32() Register
	As64() Register
}

GP provides additional methods for general purpose registers.

type GPPhysical

type GPPhysical interface {
	Physical
	GP
}

GPPhysical is a general-purpose physical register.

type GPVirtual

type GPVirtual interface {
	Virtual
	GP
}

GPVirtual is a general-purpose virtual register.

type ID

type ID uint32

ID is a register identifier.

func (ID) Index

func (id ID) Index() Index

Index extracts the index from the register ID.

func (ID) IsPhysical

func (id ID) IsPhysical() bool

IsPhysical reports whether this is an ID for a physical register.

func (ID) IsVirtual

func (id ID) IsVirtual() bool

IsVirtual reports whether this is an ID for a virtual register.

func (ID) Kind

func (id ID) Kind() Kind

Kind extracts the kind from the register ID.

type Index

type Index uint16

Index of a register within a kind.

type Info

type Info uint8

Info is a bitmask of register properties.

const (
	None       Info = 0
	Restricted Info = 1 << iota
	BasePointer
)

Defined register Info flags.

type Kind

type Kind uint8

Kind is a class of registers.

const (
	KindPseudo Kind = iota
	KindGP
	KindVector
	KindOpmask
)

Register kinds.

type MaskSet

type MaskSet map[ID]uint16

MaskSet maps register IDs to masks.

func NewEmptyMaskSet

func NewEmptyMaskSet() MaskSet

NewEmptyMaskSet builds an empty register mask set.

func NewMaskSetFromRegisters

func NewMaskSetFromRegisters(rs []Register) MaskSet

NewMaskSetFromRegisters forms a mask set from the given register list.

func (MaskSet) Add

func (s MaskSet) Add(id ID, mask uint16) bool

Add mask to the given register ID. Reports whether this made any change to the set.

func (MaskSet) AddRegister

func (s MaskSet) AddRegister(r Register) bool

AddRegister is a convenience for adding the register's (ID, mask) to the set. Reports whether this made any change to the set.

func (MaskSet) Clone

func (s MaskSet) Clone() MaskSet

Clone returns a copy of s.

func (MaskSet) Difference

func (s MaskSet) Difference(t MaskSet) MaskSet

Difference returns the set of registers in s but not t.

func (MaskSet) DifferenceUpdate

func (s MaskSet) DifferenceUpdate(t MaskSet) bool

DifferenceUpdate removes every element of t from s.

func (MaskSet) Discard

func (s MaskSet) Discard(id ID, mask uint16) bool

Discard clears masked bits from register ID. Reports whether this made any change to the set.

func (MaskSet) DiscardRegister

func (s MaskSet) DiscardRegister(r Register) bool

DiscardRegister is a convenience for discarding the register's (ID, mask) from the set. Reports whether this made any change to the set.

func (MaskSet) Equals

func (s MaskSet) Equals(t MaskSet) bool

Equals returns true if s and t contain the same masks.

func (MaskSet) OfKind

func (s MaskSet) OfKind(k Kind) MaskSet

OfKind returns the set of elements of s with kind k.

func (MaskSet) Update

func (s MaskSet) Update(t MaskSet) bool

Update adds masks in t to s. Reports whether this made any change to the set.

type OpmaskPhysical added in v0.4.0

type OpmaskPhysical interface {
	Physical
}

OpmaskPhysical is a opmask physical register.

type OpmaskVirtual added in v0.4.0

type OpmaskVirtual interface {
	Virtual
}

OpmaskVirtual is a virtual opmask register.

type Physical

type Physical interface {
	PhysicalIndex() Index
	Info() Info
	Register
}

Physical is a concrete register.

func LookupID

func LookupID(id ID, s Spec) Physical

LookupID returns the physical register with the given id and spec, or nil if not found.

func LookupPhysical

func LookupPhysical(k Kind, idx Index, s Spec) Physical

LookupPhysical returns the physical register with the given parameters, or nil if not found.

func ToPhysical

func ToPhysical(r Register) Physical

ToPhysical converts r to Physical if possible, otherwise returns nil.

type Register

type Register interface {
	ID() ID
	Kind() Kind
	Size() uint
	Mask() uint16
	Asm() string
	// contains filtered or unexported methods
}

Register represents a virtual or physical register.

type Spec

type Spec uint16

Spec defines the size of a register as well as the bit ranges it occupies in an underlying physical register.

const (
	S0   Spec = 0x0 // zero value reserved for pseudo registers
	S8L  Spec = 0x1
	S8H  Spec = 0x2
	S8        = S8L
	S16  Spec = 0x3
	S32  Spec = 0x7
	S64  Spec = 0xf
	S128 Spec = 0x1f
	S256 Spec = 0x3f
	S512 Spec = 0x7f
)

Spec values required for x86-64.

func (Spec) Mask

func (s Spec) Mask() uint16

Mask returns a mask representing which bytes of an underlying register are used by this register. This is almost always the low bytes, except for the case of the high-byte registers. If bit n of the mask is set, this means bytes 2^(n-1) to 2^n-1 are used.

func (Spec) Size

func (s Spec) Size() uint

Size returns the register width in bytes.

type Vec

type Vec interface {
	AsX() Register
	AsY() Register
	AsZ() Register
}

Vec provides methods for vector registers.

type VecPhysical

type VecPhysical interface {
	Physical
	Vec
}

VecPhysical is a physical vector register.

type VecVirtual

type VecVirtual interface {
	Virtual
	Vec
}

VecVirtual is a virtual vector register.

type Virtual

type Virtual interface {
	VirtualIndex() Index
	Register
}

Virtual is a register of a given type and size, not yet allocated to a physical register.

func NewVirtual

func NewVirtual(idx Index, k Kind, s Spec) Virtual

NewVirtual builds a Virtual register.

func ToVirtual

func ToVirtual(r Register) Virtual

ToVirtual converts r to Virtual if possible, otherwise returns nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL