Implemented x64 memory loads

This commit is contained in:
Eduard Urbach 2024-07-20 15:48:42 +02:00
parent 7c2e373562
commit d35c07ed1c
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 255 additions and 87 deletions

View File

@ -0,0 +1,8 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// LoadRegister loads from memory into a register.
func LoadRegister(code []byte, destination cpu.Register, offset byte, numBytes byte, source cpu.Register) []byte {
return memoryAccess(code, 0x8A, 0x8B, source, offset, numBytes, destination)
}

View File

@ -0,0 +1,157 @@
package x64_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/go/assert"
)
func TestLoadRegister(t *testing.T) {
usagePatterns := []struct {
Destination cpu.Register
Source cpu.Register
Offset byte
NumBytes byte
Code []byte
}{
// No offset
{x64.RAX, x64.R15, 0, 8, []byte{0x49, 0x8B, 0x07}},
{x64.RAX, x64.R15, 0, 4, []byte{0x41, 0x8B, 0x07}},
{x64.RAX, x64.R15, 0, 2, []byte{0x66, 0x41, 0x8B, 0x07}},
{x64.RAX, x64.R15, 0, 1, []byte{0x41, 0x8A, 0x07}},
{x64.RCX, x64.R14, 0, 8, []byte{0x49, 0x8B, 0x0E}},
{x64.RCX, x64.R14, 0, 4, []byte{0x41, 0x8B, 0x0E}},
{x64.RCX, x64.R14, 0, 2, []byte{0x66, 0x41, 0x8B, 0x0E}},
{x64.RCX, x64.R14, 0, 1, []byte{0x41, 0x8A, 0x0E}},
{x64.RDX, x64.R13, 0, 8, []byte{0x49, 0x8B, 0x55, 0x00}},
{x64.RDX, x64.R13, 0, 4, []byte{0x41, 0x8B, 0x55, 0x00}},
{x64.RDX, x64.R13, 0, 2, []byte{0x66, 0x41, 0x8B, 0x55, 0x00}},
{x64.RDX, x64.R13, 0, 1, []byte{0x41, 0x8A, 0x55, 0x00}},
{x64.RBX, x64.R12, 0, 8, []byte{0x49, 0x8B, 0x1C, 0x24}},
{x64.RBX, x64.R12, 0, 4, []byte{0x41, 0x8B, 0x1C, 0x24}},
{x64.RBX, x64.R12, 0, 2, []byte{0x66, 0x41, 0x8B, 0x1C, 0x24}},
{x64.RBX, x64.R12, 0, 1, []byte{0x41, 0x8A, 0x1C, 0x24}},
{x64.RSP, x64.R11, 0, 8, []byte{0x49, 0x8B, 0x23}},
{x64.RSP, x64.R11, 0, 4, []byte{0x41, 0x8B, 0x23}},
{x64.RSP, x64.R11, 0, 2, []byte{0x66, 0x41, 0x8B, 0x23}},
{x64.RSP, x64.R11, 0, 1, []byte{0x41, 0x8A, 0x23}},
{x64.RBP, x64.R10, 0, 8, []byte{0x49, 0x8B, 0x2A}},
{x64.RBP, x64.R10, 0, 4, []byte{0x41, 0x8B, 0x2A}},
{x64.RBP, x64.R10, 0, 2, []byte{0x66, 0x41, 0x8B, 0x2A}},
{x64.RBP, x64.R10, 0, 1, []byte{0x41, 0x8A, 0x2A}},
{x64.RSI, x64.R9, 0, 8, []byte{0x49, 0x8B, 0x31}},
{x64.RSI, x64.R9, 0, 4, []byte{0x41, 0x8B, 0x31}},
{x64.RSI, x64.R9, 0, 2, []byte{0x66, 0x41, 0x8B, 0x31}},
{x64.RSI, x64.R9, 0, 1, []byte{0x41, 0x8A, 0x31}},
{x64.RDI, x64.R8, 0, 8, []byte{0x49, 0x8B, 0x38}},
{x64.RDI, x64.R8, 0, 4, []byte{0x41, 0x8B, 0x38}},
{x64.RDI, x64.R8, 0, 2, []byte{0x66, 0x41, 0x8B, 0x38}},
{x64.RDI, x64.R8, 0, 1, []byte{0x41, 0x8A, 0x38}},
{x64.R8, x64.RDI, 0, 8, []byte{0x4C, 0x8B, 0x07}},
{x64.R8, x64.RDI, 0, 4, []byte{0x44, 0x8B, 0x07}},
{x64.R8, x64.RDI, 0, 2, []byte{0x66, 0x44, 0x8B, 0x07}},
{x64.R8, x64.RDI, 0, 1, []byte{0x44, 0x8A, 0x07}},
{x64.R9, x64.RSI, 0, 8, []byte{0x4C, 0x8B, 0x0E}},
{x64.R9, x64.RSI, 0, 4, []byte{0x44, 0x8B, 0x0E}},
{x64.R9, x64.RSI, 0, 2, []byte{0x66, 0x44, 0x8B, 0x0E}},
{x64.R9, x64.RSI, 0, 1, []byte{0x44, 0x8A, 0x0E}},
{x64.R10, x64.RBP, 0, 8, []byte{0x4C, 0x8B, 0x55, 0x00}},
{x64.R10, x64.RBP, 0, 4, []byte{0x44, 0x8B, 0x55, 0x00}},
{x64.R10, x64.RBP, 0, 2, []byte{0x66, 0x44, 0x8B, 0x55, 0x00}},
{x64.R10, x64.RBP, 0, 1, []byte{0x44, 0x8A, 0x55, 0x00}},
{x64.R11, x64.RSP, 0, 8, []byte{0x4C, 0x8B, 0x1C, 0x24}},
{x64.R11, x64.RSP, 0, 4, []byte{0x44, 0x8B, 0x1C, 0x24}},
{x64.R11, x64.RSP, 0, 2, []byte{0x66, 0x44, 0x8B, 0x1C, 0x24}},
{x64.R11, x64.RSP, 0, 1, []byte{0x44, 0x8A, 0x1C, 0x24}},
{x64.R12, x64.RBX, 0, 8, []byte{0x4C, 0x8B, 0x23}},
{x64.R12, x64.RBX, 0, 4, []byte{0x44, 0x8B, 0x23}},
{x64.R12, x64.RBX, 0, 2, []byte{0x66, 0x44, 0x8B, 0x23}},
{x64.R12, x64.RBX, 0, 1, []byte{0x44, 0x8A, 0x23}},
{x64.R13, x64.RDX, 0, 8, []byte{0x4C, 0x8B, 0x2A}},
{x64.R13, x64.RDX, 0, 4, []byte{0x44, 0x8B, 0x2A}},
{x64.R13, x64.RDX, 0, 2, []byte{0x66, 0x44, 0x8B, 0x2A}},
{x64.R13, x64.RDX, 0, 1, []byte{0x44, 0x8A, 0x2A}},
{x64.R14, x64.RCX, 0, 8, []byte{0x4C, 0x8B, 0x31}},
{x64.R14, x64.RCX, 0, 4, []byte{0x44, 0x8B, 0x31}},
{x64.R14, x64.RCX, 0, 2, []byte{0x66, 0x44, 0x8B, 0x31}},
{x64.R14, x64.RCX, 0, 1, []byte{0x44, 0x8A, 0x31}},
{x64.R15, x64.RAX, 0, 8, []byte{0x4C, 0x8B, 0x38}},
{x64.R15, x64.RAX, 0, 4, []byte{0x44, 0x8B, 0x38}},
{x64.R15, x64.RAX, 0, 2, []byte{0x66, 0x44, 0x8B, 0x38}},
{x64.R15, x64.RAX, 0, 1, []byte{0x44, 0x8A, 0x38}},
// Offset of 1
{x64.RAX, x64.R15, 1, 8, []byte{0x49, 0x8B, 0x47, 0x01}},
{x64.RAX, x64.R15, 1, 4, []byte{0x41, 0x8B, 0x47, 0x01}},
{x64.RAX, x64.R15, 1, 2, []byte{0x66, 0x41, 0x8B, 0x47, 0x01}},
{x64.RAX, x64.R15, 1, 1, []byte{0x41, 0x8A, 0x47, 0x01}},
{x64.RCX, x64.R14, 1, 8, []byte{0x49, 0x8B, 0x4E, 0x01}},
{x64.RCX, x64.R14, 1, 4, []byte{0x41, 0x8B, 0x4E, 0x01}},
{x64.RCX, x64.R14, 1, 2, []byte{0x66, 0x41, 0x8B, 0x4E, 0x01}},
{x64.RCX, x64.R14, 1, 1, []byte{0x41, 0x8A, 0x4E, 0x01}},
{x64.RDX, x64.R13, 1, 8, []byte{0x49, 0x8B, 0x55, 0x01}},
{x64.RDX, x64.R13, 1, 4, []byte{0x41, 0x8B, 0x55, 0x01}},
{x64.RDX, x64.R13, 1, 2, []byte{0x66, 0x41, 0x8B, 0x55, 0x01}},
{x64.RDX, x64.R13, 1, 1, []byte{0x41, 0x8A, 0x55, 0x01}},
{x64.RBX, x64.R12, 1, 8, []byte{0x49, 0x8B, 0x5C, 0x24, 0x01}},
{x64.RBX, x64.R12, 1, 4, []byte{0x41, 0x8B, 0x5C, 0x24, 0x01}},
{x64.RBX, x64.R12, 1, 2, []byte{0x66, 0x41, 0x8B, 0x5C, 0x24, 0x01}},
{x64.RBX, x64.R12, 1, 1, []byte{0x41, 0x8A, 0x5C, 0x24, 0x01}},
{x64.RSP, x64.R11, 1, 8, []byte{0x49, 0x8B, 0x63, 0x01}},
{x64.RSP, x64.R11, 1, 4, []byte{0x41, 0x8B, 0x63, 0x01}},
{x64.RSP, x64.R11, 1, 2, []byte{0x66, 0x41, 0x8B, 0x63, 0x01}},
{x64.RSP, x64.R11, 1, 1, []byte{0x41, 0x8A, 0x63, 0x01}},
{x64.RBP, x64.R10, 1, 8, []byte{0x49, 0x8B, 0x6A, 0x01}},
{x64.RBP, x64.R10, 1, 4, []byte{0x41, 0x8B, 0x6A, 0x01}},
{x64.RBP, x64.R10, 1, 2, []byte{0x66, 0x41, 0x8B, 0x6A, 0x01}},
{x64.RBP, x64.R10, 1, 1, []byte{0x41, 0x8A, 0x6A, 0x01}},
{x64.RSI, x64.R9, 1, 8, []byte{0x49, 0x8B, 0x71, 0x01}},
{x64.RSI, x64.R9, 1, 4, []byte{0x41, 0x8B, 0x71, 0x01}},
{x64.RSI, x64.R9, 1, 2, []byte{0x66, 0x41, 0x8B, 0x71, 0x01}},
{x64.RSI, x64.R9, 1, 1, []byte{0x41, 0x8A, 0x71, 0x01}},
{x64.RDI, x64.R8, 1, 8, []byte{0x49, 0x8B, 0x78, 0x01}},
{x64.RDI, x64.R8, 1, 4, []byte{0x41, 0x8B, 0x78, 0x01}},
{x64.RDI, x64.R8, 1, 2, []byte{0x66, 0x41, 0x8B, 0x78, 0x01}},
{x64.RDI, x64.R8, 1, 1, []byte{0x41, 0x8A, 0x78, 0x01}},
{x64.R8, x64.RDI, 1, 8, []byte{0x4C, 0x8B, 0x47, 0x01}},
{x64.R8, x64.RDI, 1, 4, []byte{0x44, 0x8B, 0x47, 0x01}},
{x64.R8, x64.RDI, 1, 2, []byte{0x66, 0x44, 0x8B, 0x47, 0x01}},
{x64.R8, x64.RDI, 1, 1, []byte{0x44, 0x8A, 0x47, 0x01}},
{x64.R9, x64.RSI, 1, 8, []byte{0x4C, 0x8B, 0x4E, 0x01}},
{x64.R9, x64.RSI, 1, 4, []byte{0x44, 0x8B, 0x4E, 0x01}},
{x64.R9, x64.RSI, 1, 2, []byte{0x66, 0x44, 0x8B, 0x4E, 0x01}},
{x64.R9, x64.RSI, 1, 1, []byte{0x44, 0x8A, 0x4E, 0x01}},
{x64.R10, x64.RBP, 1, 8, []byte{0x4C, 0x8B, 0x55, 0x01}},
{x64.R10, x64.RBP, 1, 4, []byte{0x44, 0x8B, 0x55, 0x01}},
{x64.R10, x64.RBP, 1, 2, []byte{0x66, 0x44, 0x8B, 0x55, 0x01}},
{x64.R10, x64.RBP, 1, 1, []byte{0x44, 0x8A, 0x55, 0x01}},
{x64.R11, x64.RSP, 1, 8, []byte{0x4C, 0x8B, 0x5C, 0x24, 0x01}},
{x64.R11, x64.RSP, 1, 4, []byte{0x44, 0x8B, 0x5C, 0x24, 0x01}},
{x64.R11, x64.RSP, 1, 2, []byte{0x66, 0x44, 0x8B, 0x5C, 0x24, 0x01}},
{x64.R11, x64.RSP, 1, 1, []byte{0x44, 0x8A, 0x5C, 0x24, 0x01}},
{x64.R12, x64.RBX, 1, 8, []byte{0x4C, 0x8B, 0x63, 0x01}},
{x64.R12, x64.RBX, 1, 4, []byte{0x44, 0x8B, 0x63, 0x01}},
{x64.R12, x64.RBX, 1, 2, []byte{0x66, 0x44, 0x8B, 0x63, 0x01}},
{x64.R12, x64.RBX, 1, 1, []byte{0x44, 0x8A, 0x63, 0x01}},
{x64.R13, x64.RDX, 1, 8, []byte{0x4C, 0x8B, 0x6A, 0x01}},
{x64.R13, x64.RDX, 1, 4, []byte{0x44, 0x8B, 0x6A, 0x01}},
{x64.R13, x64.RDX, 1, 2, []byte{0x66, 0x44, 0x8B, 0x6A, 0x01}},
{x64.R13, x64.RDX, 1, 1, []byte{0x44, 0x8A, 0x6A, 0x01}},
{x64.R14, x64.RCX, 1, 8, []byte{0x4C, 0x8B, 0x71, 0x01}},
{x64.R14, x64.RCX, 1, 4, []byte{0x44, 0x8B, 0x71, 0x01}},
{x64.R14, x64.RCX, 1, 2, []byte{0x66, 0x44, 0x8B, 0x71, 0x01}},
{x64.R14, x64.RCX, 1, 1, []byte{0x44, 0x8A, 0x71, 0x01}},
{x64.R15, x64.RAX, 1, 8, []byte{0x4C, 0x8B, 0x78, 0x01}},
{x64.R15, x64.RAX, 1, 4, []byte{0x44, 0x8B, 0x78, 0x01}},
{x64.R15, x64.RAX, 1, 2, []byte{0x66, 0x44, 0x8B, 0x78, 0x01}},
{x64.R15, x64.RAX, 1, 1, []byte{0x44, 0x8A, 0x78, 0x01}},
}
for _, pattern := range usagePatterns {
t.Logf("load %dB %s, [%s+%d]", pattern.NumBytes, pattern.Destination, pattern.Source, pattern.Offset)
code := x64.LoadRegister(nil, pattern.Destination, pattern.Offset, pattern.NumBytes, pattern.Source)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -8,7 +8,7 @@ import (
// StoreNumber stores a number into the memory address included in the given register.
func StoreNumber(code []byte, register cpu.Register, offset byte, numBytes byte, number int) []byte {
code = store(code, 0xC6, 0xC7, register, offset, numBytes, 0b000)
code = memoryAccess(code, 0xC6, 0xC7, register, offset, numBytes, 0b000)
switch numBytes {
case 8, 4:
@ -23,36 +23,5 @@ func StoreNumber(code []byte, register cpu.Register, offset byte, numBytes byte,
// 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
return memoryAccess(code, 0x88, 0x89, register, offset, numBytes, source)
}

View File

@ -10,11 +10,11 @@ import (
func TestStoreNumber(t *testing.T) {
usagePatterns := []struct {
Register cpu.Register
Offset byte
ByteCount byte
Number int
Code []byte
Register cpu.Register
Offset byte
NumBytes byte
Number int
Code []byte
}{
// No offset
{x64.RAX, 0, 8, 0x7F, []byte{0x48, 0xC7, 0x00, 0x7F, 0x00, 0x00, 0x00}},
@ -99,22 +99,22 @@ func TestStoreNumber(t *testing.T) {
{x64.RBX, 1, 4, 0x7F, []byte{0xC7, 0x43, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RBX, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x43, 0x01, 0x7F, 0x00}},
{x64.RBX, 1, 1, 0x7F, []byte{0xC6, 0x43, 0x01, 0x7F}},
{x64.RDI, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x47, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RDI, 1, 4, 0x7F, []byte{0xC7, 0x47, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RDI, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x47, 0x01, 0x7F, 0x00}},
{x64.RDI, 1, 1, 0x7F, []byte{0xC6, 0x47, 0x01, 0x7F}},
{x64.RSI, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x46, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSI, 1, 4, 0x7F, []byte{0xC7, 0x46, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSI, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x46, 0x01, 0x7F, 0x00}},
{x64.RSI, 1, 1, 0x7F, []byte{0xC6, 0x46, 0x01, 0x7F}},
{x64.RBP, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x45, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RBP, 1, 4, 0x7F, []byte{0xC7, 0x45, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RBP, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x45, 0x01, 0x7F, 0x00}},
{x64.RBP, 1, 1, 0x7F, []byte{0xC6, 0x45, 0x01, 0x7F}},
{x64.RSP, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x44, 0x24, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSP, 1, 4, 0x7F, []byte{0xC7, 0x44, 0x24, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSP, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x44, 0x24, 0x01, 0x7F, 0x00}},
{x64.RSP, 1, 1, 0x7F, []byte{0xC6, 0x44, 0x24, 0x01, 0x7F}},
{x64.RBP, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x45, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RBP, 1, 4, 0x7F, []byte{0xC7, 0x45, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RBP, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x45, 0x01, 0x7F, 0x00}},
{x64.RBP, 1, 1, 0x7F, []byte{0xC6, 0x45, 0x01, 0x7F}},
{x64.RSI, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x46, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSI, 1, 4, 0x7F, []byte{0xC7, 0x46, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RSI, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x46, 0x01, 0x7F, 0x00}},
{x64.RSI, 1, 1, 0x7F, []byte{0xC6, 0x46, 0x01, 0x7F}},
{x64.RDI, 1, 8, 0x7F, []byte{0x48, 0xC7, 0x47, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RDI, 1, 4, 0x7F, []byte{0xC7, 0x47, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.RDI, 1, 2, 0x7F, []byte{0x66, 0xC7, 0x47, 0x01, 0x7F, 0x00}},
{x64.RDI, 1, 1, 0x7F, []byte{0xC6, 0x47, 0x01, 0x7F}},
{x64.R8, 1, 8, 0x7F, []byte{0x49, 0xC7, 0x40, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.R8, 1, 4, 0x7F, []byte{0x41, 0xC7, 0x40, 0x01, 0x7F, 0x00, 0x00, 0x00}},
{x64.R8, 1, 2, 0x7F, []byte{0x66, 0x41, 0xC7, 0x40, 0x01, 0x7F, 0x00}},
@ -150,8 +150,8 @@ func TestStoreNumber(t *testing.T) {
}
for _, pattern := range usagePatterns {
t.Logf("store %dB [%s+%d], %d", pattern.ByteCount, pattern.Register, pattern.Offset, pattern.Number)
code := x64.StoreNumber(nil, pattern.Register, pattern.Offset, pattern.ByteCount, pattern.Number)
t.Logf("store %dB [%s+%d], %d", pattern.NumBytes, pattern.Register, pattern.Offset, pattern.Number)
code := x64.StoreNumber(nil, pattern.Register, pattern.Offset, pattern.NumBytes, pattern.Number)
assert.DeepEqual(t, code, pattern.Code)
}
}
@ -160,7 +160,7 @@ func TestStoreRegister(t *testing.T) {
usagePatterns := []struct {
RegisterTo cpu.Register
Offset byte
ByteCount byte
NumBytes byte
RegisterFrom cpu.Register
Code []byte
}{
@ -181,22 +181,22 @@ func TestStoreRegister(t *testing.T) {
{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.RSP, 0, 8, x64.R11, []byte{0x4C, 0x89, 0x1C, 0x24}},
{x64.RSP, 0, 4, x64.R11, []byte{0x44, 0x89, 0x1C, 0x24}},
{x64.RSP, 0, 2, x64.R11, []byte{0x66, 0x44, 0x89, 0x1C, 0x24}},
{x64.RSP, 0, 1, x64.R11, []byte{0x44, 0x88, 0x1C, 0x24}},
{x64.RBP, 0, 8, x64.R10, []byte{0x4C, 0x89, 0x55, 0x00}},
{x64.RBP, 0, 4, x64.R10, []byte{0x44, 0x89, 0x55, 0x00}},
{x64.RBP, 0, 2, x64.R10, []byte{0x66, 0x44, 0x89, 0x55, 0x00}},
{x64.RBP, 0, 1, x64.R10, []byte{0x44, 0x88, 0x55, 0x00}},
{x64.RSI, 0, 8, x64.R9, []byte{0x4C, 0x89, 0x0E}},
{x64.RSI, 0, 4, x64.R9, []byte{0x44, 0x89, 0x0E}},
{x64.RSI, 0, 2, x64.R9, []byte{0x66, 0x44, 0x89, 0x0E}},
{x64.RSI, 0, 1, x64.R9, []byte{0x44, 0x88, 0x0E}},
{x64.RDI, 0, 8, x64.R8, []byte{0x4C, 0x89, 0x07}},
{x64.RDI, 0, 4, x64.R8, []byte{0x44, 0x89, 0x07}},
{x64.RDI, 0, 2, x64.R8, []byte{0x66, 0x44, 0x89, 0x07}},
{x64.RDI, 0, 1, x64.R8, []byte{0x44, 0x88, 0x07}},
{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}},
@ -247,22 +247,22 @@ func TestStoreRegister(t *testing.T) {
{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.RSP, 1, 8, x64.R11, []byte{0x4C, 0x89, 0x5C, 0x24, 0x01}},
{x64.RSP, 1, 4, x64.R11, []byte{0x44, 0x89, 0x5C, 0x24, 0x01}},
{x64.RSP, 1, 2, x64.R11, []byte{0x66, 0x44, 0x89, 0x5C, 0x24, 0x01}},
{x64.RSP, 1, 1, x64.R11, []byte{0x44, 0x88, 0x5C, 0x24, 0x01}},
{x64.RBP, 1, 8, x64.R10, []byte{0x4C, 0x89, 0x55, 0x01}},
{x64.RBP, 1, 4, x64.R10, []byte{0x44, 0x89, 0x55, 0x01}},
{x64.RBP, 1, 2, x64.R10, []byte{0x66, 0x44, 0x89, 0x55, 0x01}},
{x64.RBP, 1, 1, x64.R10, []byte{0x44, 0x88, 0x55, 0x01}},
{x64.RSI, 1, 8, x64.R9, []byte{0x4C, 0x89, 0x4E, 0x01}},
{x64.RSI, 1, 4, x64.R9, []byte{0x44, 0x89, 0x4E, 0x01}},
{x64.RSI, 1, 2, x64.R9, []byte{0x66, 0x44, 0x89, 0x4E, 0x01}},
{x64.RSI, 1, 1, x64.R9, []byte{0x44, 0x88, 0x4E, 0x01}},
{x64.RDI, 1, 8, x64.R8, []byte{0x4C, 0x89, 0x47, 0x01}},
{x64.RDI, 1, 4, x64.R8, []byte{0x44, 0x89, 0x47, 0x01}},
{x64.RDI, 1, 2, x64.R8, []byte{0x66, 0x44, 0x89, 0x47, 0x01}},
{x64.RDI, 1, 1, x64.R8, []byte{0x44, 0x88, 0x47, 0x01}},
{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}},
@ -298,8 +298,8 @@ func TestStoreRegister(t *testing.T) {
}
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)
t.Logf("store %dB [%s+%d], %s", pattern.NumBytes, pattern.RegisterTo, pattern.Offset, pattern.RegisterFrom)
code := x64.StoreRegister(nil, pattern.RegisterTo, pattern.Offset, pattern.NumBytes, pattern.RegisterFrom)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -0,0 +1,34 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// memoryAccess encodes a memory access.
func memoryAccess(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(Scale1, 0b100, 0b100))
}
if mod == AddressMemoryOffset8 {
code = append(code, offset)
}
return code
}