From 862598cfc18d91743e8e5b833493906abfd6ce68 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 24 Jun 2024 17:51:24 +0200 Subject: [PATCH] Added more tests --- src/build/arch/x64/Add.go | 10 +++++ src/build/arch/x64/Add_test.go | 56 ++++++++++++++++++++++++++ src/build/arch/x64/Registers.go | 40 +++++++++--------- src/build/arch/x64/{Math.go => Sub.go} | 5 --- src/build/arch/x64/Sub_test.go | 56 ++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 25 deletions(-) create mode 100644 src/build/arch/x64/Add.go create mode 100644 src/build/arch/x64/Add_test.go rename src/build/arch/x64/{Math.go => Sub.go} (57%) create mode 100644 src/build/arch/x64/Sub_test.go diff --git a/src/build/arch/x64/Add.go b/src/build/arch/x64/Add.go new file mode 100644 index 0000000..d394968 --- /dev/null +++ b/src/build/arch/x64/Add.go @@ -0,0 +1,10 @@ +package x64 + +import ( + "git.akyoto.dev/cli/q/src/build/cpu" +) + +// AddRegNum adds a number to the given register. +func AddRegNum(code []byte, destination cpu.Register, number int) []byte { + return numberToRegister(0x83, 0x81, 0b000, code, destination, number) +} diff --git a/src/build/arch/x64/Add_test.go b/src/build/arch/x64/Add_test.go new file mode 100644 index 0000000..221dbbe --- /dev/null +++ b/src/build/arch/x64/Add_test.go @@ -0,0 +1,56 @@ +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 TestAddRegisterNumber(t *testing.T) { + usagePatterns := []struct { + Register cpu.Register + Number int + Code []byte + }{ + {x64.RAX, 1, []byte{0x48, 0x83, 0xc0, 0x01}}, + {x64.RCX, 1, []byte{0x48, 0x83, 0xc1, 0x01}}, + {x64.RDX, 1, []byte{0x48, 0x83, 0xc2, 0x01}}, + {x64.RBX, 1, []byte{0x48, 0x83, 0xc3, 0x01}}, + {x64.RSP, 1, []byte{0x48, 0x83, 0xc4, 0x01}}, + {x64.RBP, 1, []byte{0x48, 0x83, 0xc5, 0x01}}, + {x64.RSI, 1, []byte{0x48, 0x83, 0xc6, 0x01}}, + {x64.RDI, 1, []byte{0x48, 0x83, 0xc7, 0x01}}, + {x64.R8, 1, []byte{0x49, 0x83, 0xc0, 0x01}}, + {x64.R9, 1, []byte{0x49, 0x83, 0xc1, 0x01}}, + {x64.R10, 1, []byte{0x49, 0x83, 0xc2, 0x01}}, + {x64.R11, 1, []byte{0x49, 0x83, 0xc3, 0x01}}, + {x64.R12, 1, []byte{0x49, 0x83, 0xc4, 0x01}}, + {x64.R13, 1, []byte{0x49, 0x83, 0xc5, 0x01}}, + {x64.R14, 1, []byte{0x49, 0x83, 0xc6, 0x01}}, + {x64.R15, 1, []byte{0x49, 0x83, 0xc7, 0x01}}, + {x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC1, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC2, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RBX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC3, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RSP, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC4, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RBP, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC5, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RSI, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC6, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RDI, 0x7FFFFFFF, []byte{0x48, 0x81, 0xC7, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R8, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC0, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R9, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC1, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R10, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC2, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R11, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC3, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R12, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC4, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R13, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC5, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R14, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC6, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R15, 0x7FFFFFFF, []byte{0x49, 0x81, 0xC7, 0xFF, 0xFF, 0xFF, 0x7F}}, + } + + for _, pattern := range usagePatterns { + t.Logf("add %s, %x", pattern.Register, pattern.Number) + code := x64.AddRegNum(nil, pattern.Register, pattern.Number) + assert.DeepEqual(t, code, pattern.Code) + } +} diff --git a/src/build/arch/x64/Registers.go b/src/build/arch/x64/Registers.go index 7aaa3c8..ac47f67 100644 --- a/src/build/arch/x64/Registers.go +++ b/src/build/arch/x64/Registers.go @@ -3,26 +3,26 @@ package x64 import "git.akyoto.dev/cli/q/src/build/cpu" const ( - rax = iota - rcx - rdx - rbx - rsp - rbp - rsi - rdi - r8 - r9 - r10 - r11 - r12 - r13 - r14 - r15 + RAX = iota + RCX + RDX + RBX + RSP + RBP + RSI + RDI + R8 + R9 + R10 + R11 + R12 + R13 + R14 + R15 ) -const SyscallReturn = rax +const SyscallReturn = RAX -var GeneralRegisters = []cpu.Register{rbx, rbp, r12, r13, r14, r15} -var SyscallRegisters = []cpu.Register{rax, rdi, rsi, rdx, r10, r8, r9} -var ReturnValueRegisters = []cpu.Register{rax, rcx, r11} +var GeneralRegisters = []cpu.Register{RBX, RBP, R12, R13, R14, R15} +var SyscallRegisters = []cpu.Register{RAX, RDI, RSI, RDX, R10, R8, R9} +var ReturnValueRegisters = []cpu.Register{RAX, RCX, R11} diff --git a/src/build/arch/x64/Math.go b/src/build/arch/x64/Sub.go similarity index 57% rename from src/build/arch/x64/Math.go rename to src/build/arch/x64/Sub.go index 92a7137..af9511c 100644 --- a/src/build/arch/x64/Math.go +++ b/src/build/arch/x64/Sub.go @@ -4,11 +4,6 @@ import ( "git.akyoto.dev/cli/q/src/build/cpu" ) -// AddRegNum adds a number to the given register. -func AddRegNum(code []byte, destination cpu.Register, number int) []byte { - return numberToRegister(0x83, 0x81, 0b000, code, destination, number) -} - // SubRegNum subtracts a number from the given register. func SubRegNum(code []byte, destination cpu.Register, number int) []byte { return numberToRegister(0x83, 0x81, 0b101, code, destination, number) diff --git a/src/build/arch/x64/Sub_test.go b/src/build/arch/x64/Sub_test.go new file mode 100644 index 0000000..a0b0b10 --- /dev/null +++ b/src/build/arch/x64/Sub_test.go @@ -0,0 +1,56 @@ +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 TestSubRegisterNumber(t *testing.T) { + usagePatterns := []struct { + Register cpu.Register + Number int + Code []byte + }{ + {x64.RAX, 1, []byte{0x48, 0x83, 0xE8, 0x01}}, + {x64.RCX, 1, []byte{0x48, 0x83, 0xE9, 0x01}}, + {x64.RDX, 1, []byte{0x48, 0x83, 0xEA, 0x01}}, + {x64.RBX, 1, []byte{0x48, 0x83, 0xEB, 0x01}}, + {x64.RSP, 1, []byte{0x48, 0x83, 0xEC, 0x01}}, + {x64.RBP, 1, []byte{0x48, 0x83, 0xED, 0x01}}, + {x64.RSI, 1, []byte{0x48, 0x83, 0xEE, 0x01}}, + {x64.RDI, 1, []byte{0x48, 0x83, 0xEF, 0x01}}, + {x64.R8, 1, []byte{0x49, 0x83, 0xE8, 0x01}}, + {x64.R9, 1, []byte{0x49, 0x83, 0xE9, 0x01}}, + {x64.R10, 1, []byte{0x49, 0x83, 0xEA, 0x01}}, + {x64.R11, 1, []byte{0x49, 0x83, 0xEB, 0x01}}, + {x64.R12, 1, []byte{0x49, 0x83, 0xEC, 0x01}}, + {x64.R13, 1, []byte{0x49, 0x83, 0xED, 0x01}}, + {x64.R14, 1, []byte{0x49, 0x83, 0xEE, 0x01}}, + {x64.R15, 1, []byte{0x49, 0x83, 0xEF, 0x01}}, + {x64.RAX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE8, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RCX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xE9, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RDX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEA, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RBX, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEB, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RSP, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEC, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RBP, 0x7FFFFFFF, []byte{0x48, 0x81, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RSI, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEE, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.RDI, 0x7FFFFFFF, []byte{0x48, 0x81, 0xEF, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R8, 0x7FFFFFFF, []byte{0x49, 0x81, 0xE8, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R9, 0x7FFFFFFF, []byte{0x49, 0x81, 0xE9, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R10, 0x7FFFFFFF, []byte{0x49, 0x81, 0xEA, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R11, 0x7FFFFFFF, []byte{0x49, 0x81, 0xEB, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R12, 0x7FFFFFFF, []byte{0x49, 0x81, 0xEC, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R13, 0x7FFFFFFF, []byte{0x49, 0x81, 0xED, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R14, 0x7FFFFFFF, []byte{0x49, 0x81, 0xEE, 0xFF, 0xFF, 0xFF, 0x7F}}, + {x64.R15, 0x7FFFFFFF, []byte{0x49, 0x81, 0xEF, 0xFF, 0xFF, 0xFF, 0x7F}}, + } + + for _, pattern := range usagePatterns { + t.Logf("sub %s, %x", pattern.Register, pattern.Number) + code := x64.SubRegNum(nil, pattern.Register, pattern.Number) + assert.DeepEqual(t, code, pattern.Code) + } +}