Added more tests

This commit is contained in:
Eduard Urbach 2024-06-24 17:51:24 +02:00
parent b018d8de61
commit 862598cfc1
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 142 additions and 25 deletions

10
src/build/arch/x64/Add.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

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