71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package x64_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.akyoto.dev/cli/q/src/arch/x64"
|
|
"git.akyoto.dev/cli/q/src/cpu"
|
|
"git.akyoto.dev/go/assert"
|
|
)
|
|
|
|
func TestShiftLeftNumber(t *testing.T) {
|
|
usagePatterns := []struct {
|
|
Register cpu.Register
|
|
Number int
|
|
Code []byte
|
|
}{
|
|
{x64.RAX, 1, []byte{0x48, 0xC1, 0xE0, 0x01}},
|
|
{x64.RCX, 1, []byte{0x48, 0xC1, 0xE1, 0x01}},
|
|
{x64.RDX, 1, []byte{0x48, 0xC1, 0xE2, 0x01}},
|
|
{x64.RBX, 1, []byte{0x48, 0xC1, 0xE3, 0x01}},
|
|
{x64.RSP, 1, []byte{0x48, 0xC1, 0xE4, 0x01}},
|
|
{x64.RBP, 1, []byte{0x48, 0xC1, 0xE5, 0x01}},
|
|
{x64.RSI, 1, []byte{0x48, 0xC1, 0xE6, 0x01}},
|
|
{x64.RDI, 1, []byte{0x48, 0xC1, 0xE7, 0x01}},
|
|
{x64.R8, 1, []byte{0x49, 0xC1, 0xE0, 0x01}},
|
|
{x64.R9, 1, []byte{0x49, 0xC1, 0xE1, 0x01}},
|
|
{x64.R10, 1, []byte{0x49, 0xC1, 0xE2, 0x01}},
|
|
{x64.R11, 1, []byte{0x49, 0xC1, 0xE3, 0x01}},
|
|
{x64.R12, 1, []byte{0x49, 0xC1, 0xE4, 0x01}},
|
|
{x64.R13, 1, []byte{0x49, 0xC1, 0xE5, 0x01}},
|
|
{x64.R14, 1, []byte{0x49, 0xC1, 0xE6, 0x01}},
|
|
{x64.R15, 1, []byte{0x49, 0xC1, 0xE7, 0x01}},
|
|
}
|
|
|
|
for _, pattern := range usagePatterns {
|
|
t.Logf("shl %s, %x", pattern.Register, pattern.Number)
|
|
code := x64.ShiftLeftNumber(nil, pattern.Register, byte(pattern.Number))
|
|
assert.DeepEqual(t, code, pattern.Code)
|
|
}
|
|
}
|
|
|
|
func TestShiftRightSignedNumber(t *testing.T) {
|
|
usagePatterns := []struct {
|
|
Register cpu.Register
|
|
Number int
|
|
Code []byte
|
|
}{
|
|
{x64.RAX, 1, []byte{0x48, 0xC1, 0xF8, 0x01}},
|
|
{x64.RCX, 1, []byte{0x48, 0xC1, 0xF9, 0x01}},
|
|
{x64.RDX, 1, []byte{0x48, 0xC1, 0xFA, 0x01}},
|
|
{x64.RBX, 1, []byte{0x48, 0xC1, 0xFB, 0x01}},
|
|
{x64.RSP, 1, []byte{0x48, 0xC1, 0xFC, 0x01}},
|
|
{x64.RBP, 1, []byte{0x48, 0xC1, 0xFD, 0x01}},
|
|
{x64.RSI, 1, []byte{0x48, 0xC1, 0xFE, 0x01}},
|
|
{x64.RDI, 1, []byte{0x48, 0xC1, 0xFF, 0x01}},
|
|
{x64.R8, 1, []byte{0x49, 0xC1, 0xF8, 0x01}},
|
|
{x64.R9, 1, []byte{0x49, 0xC1, 0xF9, 0x01}},
|
|
{x64.R10, 1, []byte{0x49, 0xC1, 0xFA, 0x01}},
|
|
{x64.R11, 1, []byte{0x49, 0xC1, 0xFB, 0x01}},
|
|
{x64.R12, 1, []byte{0x49, 0xC1, 0xFC, 0x01}},
|
|
{x64.R13, 1, []byte{0x49, 0xC1, 0xFD, 0x01}},
|
|
{x64.R14, 1, []byte{0x49, 0xC1, 0xFE, 0x01}},
|
|
{x64.R15, 1, []byte{0x49, 0xC1, 0xFF, 0x01}},
|
|
}
|
|
|
|
for _, pattern := range usagePatterns {
|
|
t.Logf("sar %s, %x", pattern.Register, pattern.Number)
|
|
code := x64.ShiftRightSignedNumber(nil, pattern.Register, byte(pattern.Number))
|
|
assert.DeepEqual(t, code, pattern.Code)
|
|
}
|
|
}
|