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" import "git.akyoto.dev/cli/q/src/build/cpu"
const ( const (
rax = iota RAX = iota
rcx RCX
rdx RDX
rbx RBX
rsp RSP
rbp RBP
rsi RSI
rdi RDI
r8 R8
r9 R9
r10 R10
r11 R11
r12 R12
r13 R13
r14 R14
r15 R15
) )
const SyscallReturn = rax const SyscallReturn = RAX
var GeneralRegisters = []cpu.Register{rbx, rbp, r12, r13, r14, r15} var GeneralRegisters = []cpu.Register{RBX, RBP, R12, R13, R14, R15}
var SyscallRegisters = []cpu.Register{rax, rdi, rsi, rdx, r10, r8, r9} var SyscallRegisters = []cpu.Register{RAX, RDI, RSI, RDX, R10, R8, R9}
var ReturnValueRegisters = []cpu.Register{rax, rcx, r11} var ReturnValueRegisters = []cpu.Register{RAX, RCX, R11}

View File

@ -4,11 +4,6 @@ import (
"git.akyoto.dev/cli/q/src/build/cpu" "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. // SubRegNum subtracts a number from the given register.
func SubRegNum(code []byte, destination cpu.Register, number int) []byte { func SubRegNum(code []byte, destination cpu.Register, number int) []byte {
return numberToRegister(0x83, 0x81, 0b101, code, destination, number) 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)
}
}