Files
avo/operand/checks.go

255 lines
6.4 KiB
Go
Raw Normal View History

2018-11-26 10:13:04 -08:00
package operand
import (
"math"
"github.com/mmcloughlin/avo/reg"
2018-11-26 10:13:04 -08:00
)
2018-12-02 22:29:30 -08:00
// Pure type assertion checks:
// IsRegister returns whether op has type reg.Register.
func IsRegister(op Op) bool { _, ok := op.(reg.Register); return ok }
// IsMem returns whether op has type Mem.
func IsMem(op Op) bool { _, ok := op.(Mem); return ok }
// IsImm returns whether op has type Imm.
func IsImm(op Op) bool { _, ok := op.(Imm); return ok }
// IsRel returns whether op has type Rel.
func IsRel(op Op) bool { _, ok := op.(Rel); return ok }
// Checks corresponding to specific operand types in the Intel Manual:
// Is1 returns true if op is the immediate constant 1.
2018-12-02 12:28:33 -08:00
func Is1(op Op) bool {
i, ok := op.(Imm)
return ok && i == 1
2018-11-26 10:13:04 -08:00
}
// Is3 returns true if op is the immediate constant 3.
2018-12-02 12:28:33 -08:00
func Is3(op Op) bool {
i, ok := op.(Imm)
return ok && i == 3
2018-11-26 10:13:04 -08:00
}
// IsImm2u returns true if op is a 2-bit unsigned immediate (less than 4).
2018-12-02 12:28:33 -08:00
func IsImm2u(op Op) bool {
i, ok := op.(Imm)
return ok && i < 4
2018-11-26 10:13:04 -08:00
}
// IsImm8 returns true is op is an 8-bit immediate.
2018-12-02 12:28:33 -08:00
func IsImm8(op Op) bool {
i, ok := op.(Imm)
return ok && i <= math.MaxUint8
2018-11-26 10:13:04 -08:00
}
// IsImm16 returns true is op is a 16-bit immediate.
2018-12-02 12:28:33 -08:00
func IsImm16(op Op) bool {
i, ok := op.(Imm)
return ok && i <= math.MaxUint16
2018-11-26 10:13:04 -08:00
}
// IsImm32 returns true is op is a 32-bit immediate.
2018-12-02 12:28:33 -08:00
func IsImm32(op Op) bool {
i, ok := op.(Imm)
return ok && i <= math.MaxUint32
2018-11-26 10:13:04 -08:00
}
// IsImm64 returns true is op is a 64-bit immediate.
2018-12-02 12:28:33 -08:00
func IsImm64(op Op) bool {
_, ok := op.(Imm)
return ok
2018-11-26 10:13:04 -08:00
}
// IsAl returns true if op is the AL register.
2018-12-02 12:28:33 -08:00
func IsAl(op Op) bool {
return op == reg.AL
2018-11-26 10:13:04 -08:00
}
// IsCl returns true if op is the CL register.
2018-12-02 12:28:33 -08:00
func IsCl(op Op) bool {
return op == reg.CL
2018-11-26 10:13:04 -08:00
}
// IsAx returns true if op is the 16-bit AX register.
2018-12-02 12:28:33 -08:00
func IsAx(op Op) bool {
return op == reg.AX
2018-11-26 10:13:04 -08:00
}
// IsEax returns true if op is the 32-bit EAX register.
2018-12-02 12:28:33 -08:00
func IsEax(op Op) bool {
return op == reg.EAX
2018-11-26 10:13:04 -08:00
}
// IsRax returns true if op is the 64-bit RAX register.
2018-12-02 12:28:33 -08:00
func IsRax(op Op) bool {
return op == reg.RAX
2018-11-26 10:13:04 -08:00
}
// IsR8 returns true if op is an 8-bit general-purpose register.
2018-12-02 12:28:33 -08:00
func IsR8(op Op) bool {
return IsGP(op, 1)
2018-11-26 10:13:04 -08:00
}
// IsR16 returns true if op is a 16-bit general-purpose register.
2018-12-02 12:28:33 -08:00
func IsR16(op Op) bool {
return IsGP(op, 2)
2018-11-26 10:13:04 -08:00
}
// IsR32 returns true if op is a 32-bit general-purpose register.
2018-12-02 12:28:33 -08:00
func IsR32(op Op) bool {
return IsGP(op, 4)
2018-11-26 10:13:04 -08:00
}
// IsR64 returns true if op is a 64-bit general-purpose register.
2018-12-02 12:28:33 -08:00
func IsR64(op Op) bool {
return IsGP(op, 8)
}
// IsPseudo returns true if op is a pseudo register.
func IsPseudo(op Op) bool {
2018-12-13 00:18:44 -08:00
return IsRegisterKind(op, reg.Internal)
}
// IsGP returns true if op is a general-purpose register of size n bytes.
2018-12-02 12:28:33 -08:00
func IsGP(op Op, n uint) bool {
2018-11-26 23:35:26 -08:00
return IsRegisterKindSize(op, reg.GP, n)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsXmm0 returns true if op is the X0 register.
2018-12-02 12:28:33 -08:00
func IsXmm0(op Op) bool {
2018-11-26 23:35:26 -08:00
return op == reg.X0
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsXmm returns true if op is a 128-bit XMM register.
2018-12-02 12:28:33 -08:00
func IsXmm(op Op) bool {
2018-11-26 23:35:26 -08:00
return IsRegisterKindSize(op, reg.SSEAVX, 16)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsYmm returns true if op is a 256-bit YMM register.
2018-12-02 12:28:33 -08:00
func IsYmm(op Op) bool {
2018-11-26 23:35:26 -08:00
return IsRegisterKindSize(op, reg.SSEAVX, 32)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsRegisterKindSize returns true if op is a register of the given kind and size in bytes.
2018-12-02 12:28:33 -08:00
func IsRegisterKindSize(op Op, k reg.Kind, n uint) bool {
2018-12-02 21:35:33 -08:00
r, ok := op.(reg.Register)
2018-11-26 23:35:26 -08:00
return ok && r.Kind() == k && r.Bytes() == n
}
2018-12-13 00:18:44 -08:00
// IsRegisterKind returns true if op is a register of the given kind.
func IsRegisterKind(op Op, k reg.Kind) bool {
r, ok := op.(reg.Register)
return ok && r.Kind() == k
}
2018-11-26 23:35:26 -08:00
// IsM returns true if op is a 16-, 32- or 64-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM(op Op) bool {
2018-11-26 23:35:26 -08:00
// TODO(mbm): confirm "m" check is defined correctly
// Intel manual: "A 16-, 32- or 64-bit operand in memory."
return IsM16(op) || IsM32(op) || IsM64(op)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsM8 returns true if op is an 8-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM8(op Op) bool {
2018-11-26 23:35:26 -08:00
// TODO(mbm): confirm "m8" check is defined correctly
// Intel manual: "A byte operand in memory, usually expressed as a variable or
// array name, but pointed to by the DS:(E)SI or ES:(E)DI registers. In 64-bit
// mode, it is pointed to by the RSI or RDI registers."
return IsMSize(op, 1)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsM16 returns true if op is a 16-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM16(op Op) bool {
2018-11-26 23:35:26 -08:00
return IsMSize(op, 2)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsM32 returns true if op is a 16-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM32(op Op) bool {
2018-11-26 23:35:26 -08:00
return IsMSize(op, 4)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsM64 returns true if op is a 64-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM64(op Op) bool {
2018-11-26 23:35:26 -08:00
return IsMSize(op, 8)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsMSize returns true if op is a memory operand using general-purpose address
// registers of the given size in bytes.
2018-12-02 12:28:33 -08:00
func IsMSize(op Op, n uint) bool {
2018-11-26 23:35:26 -08:00
// TODO(mbm): should memory operands have a size attribute as well?
2018-12-14 22:12:28 -08:00
// TODO(mbm): m8,m16,m32,m64 checks do not actually check size
2018-11-26 23:35:26 -08:00
m, ok := op.(Mem)
2018-12-13 00:18:44 -08:00
return ok && IsMReg(m.Base) && (m.Index == nil || IsMReg(m.Index))
}
2018-12-13 00:18:44 -08:00
// IsMReg returns true if op is a register that can be used in a memory operand.
func IsMReg(op Op) bool {
return IsPseudo(op) || IsRegisterKind(op, reg.GP)
2018-11-26 23:35:26 -08:00
}
// IsM128 returns true if op is a 128-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM128(op Op) bool {
2018-11-26 23:35:26 -08:00
// TODO(mbm): should "m128" be the same as "m64"?
return IsM64(op)
2018-11-26 10:13:04 -08:00
}
2018-11-26 23:35:26 -08:00
// IsM256 returns true if op is a 256-bit memory operand.
2018-12-02 12:28:33 -08:00
func IsM256(op Op) bool {
2018-11-26 23:35:26 -08:00
// TODO(mbm): should "m256" be the same as "m64"?
return IsM64(op)
2018-11-26 10:13:04 -08:00
}
// IsVm32x returns true if op is a vector memory operand with 32-bit XMM index.
2018-12-02 12:28:33 -08:00
func IsVm32x(op Op) bool {
return IsVmx(op)
2018-11-26 10:13:04 -08:00
}
// IsVm64x returns true if op is a vector memory operand with 64-bit XMM index.
2018-12-02 12:28:33 -08:00
func IsVm64x(op Op) bool {
return IsVmx(op)
}
// IsVmx returns true if op is a vector memory operand with XMM index.
2018-12-02 12:28:33 -08:00
func IsVmx(op Op) bool {
return isvm(op, IsXmm)
2018-11-26 10:13:04 -08:00
}
// IsVm32y returns true if op is a vector memory operand with 32-bit YMM index.
2018-12-02 12:28:33 -08:00
func IsVm32y(op Op) bool {
return IsVmy(op)
2018-11-26 10:13:04 -08:00
}
// IsVm64y returns true if op is a vector memory operand with 64-bit YMM index.
2018-12-02 12:28:33 -08:00
func IsVm64y(op Op) bool {
return IsVmy(op)
}
// IsVmy returns true if op is a vector memory operand with YMM index.
2018-12-02 12:28:33 -08:00
func IsVmy(op Op) bool {
return isvm(op, IsYmm)
}
2018-12-02 12:28:33 -08:00
func isvm(op Op, idx func(Op) bool) bool {
m, ok := op.(Mem)
return ok && IsR64(m.Base) && idx(m.Index)
2018-11-26 10:13:04 -08:00
}
2018-11-27 22:08:11 -08:00
// IsRel8 returns true if op is an 8-bit offset relative to instruction pointer.
2018-12-02 12:28:33 -08:00
func IsRel8(op Op) bool {
2018-11-27 22:08:11 -08:00
r, ok := op.(Rel)
return ok && r == Rel(int8(r))
2018-11-26 10:13:04 -08:00
}
2018-11-27 22:08:11 -08:00
// IsRel32 returns true if op is an offset relative to instruction pointer, or a
// label reference.
2018-12-02 12:28:33 -08:00
func IsRel32(op Op) bool {
2018-11-27 22:08:11 -08:00
// TODO(mbm): should labels be considered separately?
_, rel := op.(Rel)
_, label := op.(LabelRef)
return rel || label
2018-11-26 10:13:04 -08:00
}