Improved x64 encoder

This commit is contained in:
Eduard Urbach 2024-07-20 12:49:26 +02:00
parent 6b5dd4c687
commit 7c2e373562
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 214 additions and 49 deletions

View File

@ -6,10 +6,10 @@ import (
// AddRegisterNumber adds a number to the given register.
func AddRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, 1, AddressDirect, 0, byte(destination), number, 0x83, 0x81)
return encodeNum(code, AddressDirect, 0, destination, number, 0x83, 0x81)
}
// AddRegisterRegister adds a register value into another register.
func AddRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, 1, AddressDirect, byte(operand), byte(destination), 0x01)
return encode(code, AddressDirect, operand, destination, 8, 0x01)
}

View File

@ -4,10 +4,10 @@ import "git.akyoto.dev/cli/q/src/build/cpu"
// Compares the register with the number and sets the status flags in the EFLAGS register.
func CompareRegisterNumber(code []byte, register cpu.Register, number int) []byte {
return encodeNum(code, 1, AddressDirect, 0b111, byte(register), number, 0x83, 0x81)
return encodeNum(code, AddressDirect, 0b111, register, number, 0x83, 0x81)
}
// CompareRegisterRegister compares a register with a register and sets the status flags in the EFLAGS register.
func CompareRegisterRegister(code []byte, registerA cpu.Register, registerB cpu.Register) []byte {
return encode(code, 1, AddressDirect, byte(registerB), byte(registerA), 0x39)
return encode(code, AddressDirect, registerB, registerA, 8, 0x39)
}

View File

@ -19,5 +19,5 @@ func MoveRegisterNumber32(code []byte, destination cpu.Register, number uint32)
// MoveRegisterRegister64 moves a register value into another register.
func MoveRegisterRegister64(code []byte, destination cpu.Register, source cpu.Register) []byte {
return encode(code, 1, AddressDirect, byte(source), byte(destination), 0x89)
return encode(code, AddressDirect, source, destination, 8, 0x89)
}

View File

@ -4,10 +4,10 @@ import "git.akyoto.dev/cli/q/src/build/cpu"
// MulRegisterNumber multiplies a register with a number.
func MulRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, 1, AddressDirect, byte(destination), byte(destination), number, 0x6B, 0x69)
return encodeNum(code, AddressDirect, destination, destination, number, 0x6B, 0x69)
}
// MulRegisterRegister multiplies a register with another register.
func MulRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, 1, AddressDirect, byte(destination), byte(operand), 0x0F, 0xAF)
return encode(code, AddressDirect, destination, operand, 8, 0x0F, 0xAF)
}

View File

@ -7,40 +7,10 @@ import (
)
// StoreNumber stores a number into the memory address included in the given register.
func StoreNumber(code []byte, register cpu.Register, offset byte, byteCount byte, number int) []byte {
if byteCount == 2 {
code = append(code, 0x66)
}
func StoreNumber(code []byte, register cpu.Register, offset byte, numBytes byte, number int) []byte {
code = store(code, 0xC6, 0xC7, register, offset, numBytes, 0b000)
opCode := byte(0xC7)
if byteCount == 1 {
opCode = 0xC6
}
mod := AddressMemory
if offset != 0 || register == RBP || register == R13 {
mod = AddressMemoryOffset8
}
is64Bit := byte(0)
if byteCount == 8 {
is64Bit = 1
}
code = encode(code, is64Bit, mod, 0b000, byte(register), opCode)
if register == RSP || register == R12 {
code = append(code, SIB(0b00, 0b100, 0b100))
}
if mod == AddressMemoryOffset8 {
code = append(code, offset)
}
switch byteCount {
switch numBytes {
case 8, 4:
return binary.LittleEndian.AppendUint32(code, uint32(number))
@ -50,3 +20,39 @@ func StoreNumber(code []byte, register cpu.Register, offset byte, byteCount byte
return append(code, byte(number))
}
// StoreRegister stores the contents of the `source` register into the memory address included in the given register.
func StoreRegister(code []byte, register cpu.Register, offset byte, numBytes byte, source cpu.Register) []byte {
return store(code, 0x88, 0x89, register, offset, numBytes, source)
}
// store encodes a write to memory.
func store(code []byte, opCode8 byte, opCode32 byte, register cpu.Register, offset byte, numBytes byte, source cpu.Register) []byte {
if numBytes == 2 {
code = append(code, 0x66)
}
opCode := opCode32
if numBytes == 1 {
opCode = opCode8
}
mod := AddressMemory
if offset != 0 || register == RBP || register == R13 {
mod = AddressMemoryOffset8
}
code = encode(code, mod, source, register, numBytes, opCode)
if register == RSP || register == R12 {
code = append(code, SIB(0b00, 0b100, 0b100))
}
if mod == AddressMemoryOffset8 {
code = append(code, offset)
}
return code
}

View File

@ -155,3 +155,151 @@ func TestStoreNumber(t *testing.T) {
assert.DeepEqual(t, code, pattern.Code)
}
}
func TestStoreRegister(t *testing.T) {
usagePatterns := []struct {
RegisterTo cpu.Register
Offset byte
ByteCount byte
RegisterFrom cpu.Register
Code []byte
}{
// No offset
{x64.RAX, 0, 8, x64.R15, []byte{0x4C, 0x89, 0x38}},
{x64.RAX, 0, 4, x64.R15, []byte{0x44, 0x89, 0x38}},
{x64.RAX, 0, 2, x64.R15, []byte{0x66, 0x44, 0x89, 0x38}},
{x64.RAX, 0, 1, x64.R15, []byte{0x44, 0x88, 0x38}},
{x64.RCX, 0, 8, x64.R14, []byte{0x4C, 0x89, 0x31}},
{x64.RCX, 0, 4, x64.R14, []byte{0x44, 0x89, 0x31}},
{x64.RCX, 0, 2, x64.R14, []byte{0x66, 0x44, 0x89, 0x31}},
{x64.RCX, 0, 1, x64.R14, []byte{0x44, 0x88, 0x31}},
{x64.RDX, 0, 8, x64.R13, []byte{0x4C, 0x89, 0x2A}},
{x64.RDX, 0, 4, x64.R13, []byte{0x44, 0x89, 0x2A}},
{x64.RDX, 0, 2, x64.R13, []byte{0x66, 0x44, 0x89, 0x2A}},
{x64.RDX, 0, 1, x64.R13, []byte{0x44, 0x88, 0x2A}},
{x64.RBX, 0, 8, x64.R12, []byte{0x4C, 0x89, 0x23}},
{x64.RBX, 0, 4, x64.R12, []byte{0x44, 0x89, 0x23}},
{x64.RBX, 0, 2, x64.R12, []byte{0x66, 0x44, 0x89, 0x23}},
{x64.RBX, 0, 1, x64.R12, []byte{0x44, 0x88, 0x23}},
{x64.RDI, 0, 8, x64.R11, []byte{0x4C, 0x89, 0x1F}},
{x64.RDI, 0, 4, x64.R11, []byte{0x44, 0x89, 0x1F}},
{x64.RDI, 0, 2, x64.R11, []byte{0x66, 0x44, 0x89, 0x1F}},
{x64.RDI, 0, 1, x64.R11, []byte{0x44, 0x88, 0x1F}},
{x64.RSI, 0, 8, x64.R10, []byte{0x4C, 0x89, 0x16}},
{x64.RSI, 0, 4, x64.R10, []byte{0x44, 0x89, 0x16}},
{x64.RSI, 0, 2, x64.R10, []byte{0x66, 0x44, 0x89, 0x16}},
{x64.RSI, 0, 1, x64.R10, []byte{0x44, 0x88, 0x16}},
{x64.RBP, 0, 8, x64.R9, []byte{0x4C, 0x89, 0x4D, 0x00}},
{x64.RBP, 0, 4, x64.R9, []byte{0x44, 0x89, 0x4D, 0x00}},
{x64.RBP, 0, 2, x64.R9, []byte{0x66, 0x44, 0x89, 0x4D, 0x00}},
{x64.RBP, 0, 1, x64.R9, []byte{0x44, 0x88, 0x4D, 0x00}},
{x64.RSP, 0, 8, x64.R8, []byte{0x4C, 0x89, 0x04, 0x24}},
{x64.RSP, 0, 4, x64.R8, []byte{0x44, 0x89, 0x04, 0x24}},
{x64.RSP, 0, 2, x64.R8, []byte{0x66, 0x44, 0x89, 0x04, 0x24}},
{x64.RSP, 0, 1, x64.R8, []byte{0x44, 0x88, 0x04, 0x24}},
{x64.R8, 0, 8, x64.RDI, []byte{0x49, 0x89, 0x38}},
{x64.R8, 0, 4, x64.RDI, []byte{0x41, 0x89, 0x38}},
{x64.R8, 0, 2, x64.RDI, []byte{0x66, 0x41, 0x89, 0x38}},
{x64.R8, 0, 1, x64.RDI, []byte{0x41, 0x88, 0x38}},
{x64.R9, 0, 8, x64.RSI, []byte{0x49, 0x89, 0x31}},
{x64.R9, 0, 4, x64.RSI, []byte{0x41, 0x89, 0x31}},
{x64.R9, 0, 2, x64.RSI, []byte{0x66, 0x41, 0x89, 0x31}},
{x64.R9, 0, 1, x64.RSI, []byte{0x41, 0x88, 0x31}},
{x64.R10, 0, 8, x64.RBP, []byte{0x49, 0x89, 0x2A}},
{x64.R10, 0, 4, x64.RBP, []byte{0x41, 0x89, 0x2A}},
{x64.R10, 0, 2, x64.RBP, []byte{0x66, 0x41, 0x89, 0x2A}},
{x64.R10, 0, 1, x64.RBP, []byte{0x41, 0x88, 0x2A}},
{x64.R11, 0, 8, x64.RSP, []byte{0x49, 0x89, 0x23}},
{x64.R11, 0, 4, x64.RSP, []byte{0x41, 0x89, 0x23}},
{x64.R11, 0, 2, x64.RSP, []byte{0x66, 0x41, 0x89, 0x23}},
{x64.R11, 0, 1, x64.RSP, []byte{0x41, 0x88, 0x23}},
{x64.R12, 0, 8, x64.RBX, []byte{0x49, 0x89, 0x1C, 0x24}},
{x64.R12, 0, 4, x64.RBX, []byte{0x41, 0x89, 0x1C, 0x24}},
{x64.R12, 0, 2, x64.RBX, []byte{0x66, 0x41, 0x89, 0x1C, 0x24}},
{x64.R12, 0, 1, x64.RBX, []byte{0x41, 0x88, 0x1C, 0x24}},
{x64.R13, 0, 8, x64.RDX, []byte{0x49, 0x89, 0x55, 0x00}},
{x64.R13, 0, 4, x64.RDX, []byte{0x41, 0x89, 0x55, 0x00}},
{x64.R13, 0, 2, x64.RDX, []byte{0x66, 0x41, 0x89, 0x55, 0x00}},
{x64.R13, 0, 1, x64.RDX, []byte{0x41, 0x88, 0x55, 0x00}},
{x64.R14, 0, 8, x64.RCX, []byte{0x49, 0x89, 0x0E}},
{x64.R14, 0, 4, x64.RCX, []byte{0x41, 0x89, 0x0E}},
{x64.R14, 0, 2, x64.RCX, []byte{0x66, 0x41, 0x89, 0x0E}},
{x64.R14, 0, 1, x64.RCX, []byte{0x41, 0x88, 0x0E}},
{x64.R15, 0, 8, x64.RAX, []byte{0x49, 0x89, 0x07}},
{x64.R15, 0, 4, x64.RAX, []byte{0x41, 0x89, 0x07}},
{x64.R15, 0, 2, x64.RAX, []byte{0x66, 0x41, 0x89, 0x07}},
{x64.R15, 0, 1, x64.RAX, []byte{0x41, 0x88, 0x07}},
// Offset of 1
{x64.RAX, 1, 8, x64.R15, []byte{0x4C, 0x89, 0x78, 0x01}},
{x64.RAX, 1, 4, x64.R15, []byte{0x44, 0x89, 0x78, 0x01}},
{x64.RAX, 1, 2, x64.R15, []byte{0x66, 0x44, 0x89, 0x78, 0x01}},
{x64.RAX, 1, 1, x64.R15, []byte{0x44, 0x88, 0x78, 0x01}},
{x64.RCX, 1, 8, x64.R14, []byte{0x4C, 0x89, 0x71, 0x01}},
{x64.RCX, 1, 4, x64.R14, []byte{0x44, 0x89, 0x71, 0x01}},
{x64.RCX, 1, 2, x64.R14, []byte{0x66, 0x44, 0x89, 0x71, 0x01}},
{x64.RCX, 1, 1, x64.R14, []byte{0x44, 0x88, 0x71, 0x01}},
{x64.RDX, 1, 8, x64.R13, []byte{0x4C, 0x89, 0x6A, 0x01}},
{x64.RDX, 1, 4, x64.R13, []byte{0x44, 0x89, 0x6A, 0x01}},
{x64.RDX, 1, 2, x64.R13, []byte{0x66, 0x44, 0x89, 0x6A, 0x01}},
{x64.RDX, 1, 1, x64.R13, []byte{0x44, 0x88, 0x6A, 0x01}},
{x64.RBX, 1, 8, x64.R12, []byte{0x4C, 0x89, 0x63, 0x01}},
{x64.RBX, 1, 4, x64.R12, []byte{0x44, 0x89, 0x63, 0x01}},
{x64.RBX, 1, 2, x64.R12, []byte{0x66, 0x44, 0x89, 0x63, 0x01}},
{x64.RBX, 1, 1, x64.R12, []byte{0x44, 0x88, 0x63, 0x01}},
{x64.RDI, 1, 8, x64.R11, []byte{0x4C, 0x89, 0x5F, 0x01}},
{x64.RDI, 1, 4, x64.R11, []byte{0x44, 0x89, 0x5F, 0x01}},
{x64.RDI, 1, 2, x64.R11, []byte{0x66, 0x44, 0x89, 0x5F, 0x01}},
{x64.RDI, 1, 1, x64.R11, []byte{0x44, 0x88, 0x5F, 0x01}},
{x64.RSI, 1, 8, x64.R10, []byte{0x4C, 0x89, 0x56, 0x01}},
{x64.RSI, 1, 4, x64.R10, []byte{0x44, 0x89, 0x56, 0x01}},
{x64.RSI, 1, 2, x64.R10, []byte{0x66, 0x44, 0x89, 0x56, 0x01}},
{x64.RSI, 1, 1, x64.R10, []byte{0x44, 0x88, 0x56, 0x01}},
{x64.RBP, 1, 8, x64.R9, []byte{0x4C, 0x89, 0x4D, 0x01}},
{x64.RBP, 1, 4, x64.R9, []byte{0x44, 0x89, 0x4D, 0x01}},
{x64.RBP, 1, 2, x64.R9, []byte{0x66, 0x44, 0x89, 0x4D, 0x01}},
{x64.RBP, 1, 1, x64.R9, []byte{0x44, 0x88, 0x4D, 0x01}},
{x64.RSP, 1, 8, x64.R8, []byte{0x4C, 0x89, 0x44, 0x24, 0x01}},
{x64.RSP, 1, 4, x64.R8, []byte{0x44, 0x89, 0x44, 0x24, 0x01}},
{x64.RSP, 1, 2, x64.R8, []byte{0x66, 0x44, 0x89, 0x44, 0x24, 0x01}},
{x64.RSP, 1, 1, x64.R8, []byte{0x44, 0x88, 0x44, 0x24, 01}},
{x64.R8, 1, 8, x64.RDI, []byte{0x49, 0x89, 0x78, 0x01}},
{x64.R8, 1, 4, x64.RDI, []byte{0x41, 0x89, 0x78, 0x01}},
{x64.R8, 1, 2, x64.RDI, []byte{0x66, 0x41, 0x89, 0x78, 0x01}},
{x64.R8, 1, 1, x64.RDI, []byte{0x41, 0x88, 0x78, 0x01}},
{x64.R9, 1, 8, x64.RSI, []byte{0x49, 0x89, 0x71, 0x01}},
{x64.R9, 1, 4, x64.RSI, []byte{0x41, 0x89, 0x71, 0x01}},
{x64.R9, 1, 2, x64.RSI, []byte{0x66, 0x41, 0x89, 0x71, 0x01}},
{x64.R9, 1, 1, x64.RSI, []byte{0x41, 0x88, 0x71, 0x01}},
{x64.R10, 1, 8, x64.RBP, []byte{0x49, 0x89, 0x6A, 0x01}},
{x64.R10, 1, 4, x64.RBP, []byte{0x41, 0x89, 0x6A, 0x01}},
{x64.R10, 1, 2, x64.RBP, []byte{0x66, 0x41, 0x89, 0x6A, 0x01}},
{x64.R10, 1, 1, x64.RBP, []byte{0x41, 0x88, 0x6A, 0x01}},
{x64.R11, 1, 8, x64.RSP, []byte{0x49, 0x89, 0x63, 0x01}},
{x64.R11, 1, 4, x64.RSP, []byte{0x41, 0x89, 0x63, 0x01}},
{x64.R11, 1, 2, x64.RSP, []byte{0x66, 0x41, 0x89, 0x63, 0x01}},
{x64.R11, 1, 1, x64.RSP, []byte{0x41, 0x88, 0x63, 0x01}},
{x64.R12, 1, 8, x64.RBX, []byte{0x49, 0x89, 0x5C, 0x24, 0x01}},
{x64.R12, 1, 4, x64.RBX, []byte{0x41, 0x89, 0x5C, 0x24, 0x01}},
{x64.R12, 1, 2, x64.RBX, []byte{0x66, 0x41, 0x89, 0x5C, 0x24, 0x01}},
{x64.R12, 1, 1, x64.RBX, []byte{0x41, 0x88, 0x5C, 0x24, 01}},
{x64.R13, 1, 8, x64.RDX, []byte{0x49, 0x89, 0x55, 0x01}},
{x64.R13, 1, 4, x64.RDX, []byte{0x41, 0x89, 0x55, 0x01}},
{x64.R13, 1, 2, x64.RDX, []byte{0x66, 0x41, 0x89, 0x55, 0x01}},
{x64.R13, 1, 1, x64.RDX, []byte{0x41, 0x88, 0x55, 0x01}},
{x64.R14, 1, 8, x64.RCX, []byte{0x49, 0x89, 0x4E, 0x01}},
{x64.R14, 1, 4, x64.RCX, []byte{0x41, 0x89, 0x4E, 0x01}},
{x64.R14, 1, 2, x64.RCX, []byte{0x66, 0x41, 0x89, 0x4E, 0x01}},
{x64.R14, 1, 1, x64.RCX, []byte{0x41, 0x88, 0x4E, 0x01}},
{x64.R15, 1, 8, x64.RAX, []byte{0x49, 0x89, 0x47, 0x01}},
{x64.R15, 1, 4, x64.RAX, []byte{0x41, 0x89, 0x47, 0x01}},
{x64.R15, 1, 2, x64.RAX, []byte{0x66, 0x41, 0x89, 0x47, 0x01}},
{x64.R15, 1, 1, x64.RAX, []byte{0x41, 0x88, 0x47, 0x01}},
}
for _, pattern := range usagePatterns {
t.Logf("store %dB [%s+%d], %s", pattern.ByteCount, pattern.RegisterTo, pattern.Offset, pattern.RegisterFrom)
code := x64.StoreRegister(nil, pattern.RegisterTo, pattern.Offset, pattern.ByteCount, pattern.RegisterFrom)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -6,10 +6,10 @@ import (
// SubRegisterNumber subtracts a number from the given register.
func SubRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
return encodeNum(code, 1, AddressDirect, 0b101, byte(destination), number, 0x83, 0x81)
return encodeNum(code, AddressDirect, 0b101, destination, number, 0x83, 0x81)
}
// SubRegisterRegister subtracts a register value from another register.
func SubRegisterRegister(code []byte, destination cpu.Register, operand cpu.Register) []byte {
return encode(code, 1, AddressDirect, byte(operand), byte(destination), 0x29)
return encode(code, AddressDirect, operand, destination, 8, 0x29)
}

View File

@ -1,11 +1,18 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// encode is the core function that encodes an instruction.
func encode(code []byte, w byte, mod byte, reg byte, rm byte, opCodes ...byte) []byte {
func encode(code []byte, mod byte, reg cpu.Register, rm cpu.Register, numBytes byte, opCodes ...byte) []byte {
w := byte(0) // Indicates a 64-bit register.
r := byte(0) // Extension to the "reg" field in ModRM.
x := byte(0) // Extension to the SIB index field.
b := byte(0) // Extension to the "rm" field in ModRM or the SIB base (r8 up to r15 use this).
if numBytes == 8 {
w = 1
}
if reg > 0b111 {
r = 1
reg &= 0b111
@ -16,12 +23,12 @@ func encode(code []byte, w byte, mod byte, reg byte, rm byte, opCodes ...byte) [
rm &= 0b111
}
if w != 0 || r != 0 || x != 0 || b != 0 {
if w != 0 || r != 0 || x != 0 || b != 0 || (numBytes == 1 && (reg == RSP || reg == RBP || reg == RSI || reg == RDI)) {
code = append(code, REX(w, r, x, b))
}
code = append(code, opCodes...)
code = append(code, ModRM(mod, reg, rm))
code = append(code, ModRM(mod, byte(reg), byte(rm)))
return code
}

View File

@ -1,14 +1,18 @@
package x64
import "encoding/binary"
import (
"encoding/binary"
"git.akyoto.dev/cli/q/src/build/cpu"
)
// encodeNum encodes an instruction with up to two registers and a number parameter.
func encodeNum(code []byte, w byte, mod byte, reg byte, rm byte, number int, opCode8 byte, opCode32 byte) []byte {
func encodeNum(code []byte, mod byte, reg cpu.Register, rm cpu.Register, number int, opCode8 byte, opCode32 byte) []byte {
if SizeOf(int64(number)) == 1 {
code = encode(code, w, mod, reg, rm, opCode8)
code = encode(code, mod, reg, rm, 8, opCode8)
return append(code, byte(number))
}
code = encode(code, w, mod, reg, rm, opCode32)
code = encode(code, mod, reg, rm, 8, opCode32)
return binary.LittleEndian.AppendUint32(code, uint32(number))
}