Implemented x64 memory loads

This commit is contained in:
2024-07-20 15:48:42 +02:00
parent 7c2e373562
commit d35c07ed1c
5 changed files with 255 additions and 87 deletions

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)
}
}