Improved x64 encoder

This commit is contained in:
2024-07-20 12:49:26 +02:00
parent 6b5dd4c687
commit 7c2e373562
9 changed files with 214 additions and 49 deletions

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