package x64_test import ( "testing" "git.akyoto.dev/cli/q/src/cpu" "git.akyoto.dev/cli/q/src/x64" "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.SubRegisterNumber(nil, pattern.Register, pattern.Number) assert.DeepEqual(t, code, pattern.Code) } } func TestSubRegisterRegister(t *testing.T) { usagePatterns := []struct { Left cpu.Register Right cpu.Register Code []byte }{ {x64.RAX, x64.R15, []byte{0x4C, 0x29, 0xF8}}, {x64.RCX, x64.R14, []byte{0x4C, 0x29, 0xF1}}, {x64.RDX, x64.R13, []byte{0x4C, 0x29, 0xEA}}, {x64.RBX, x64.R12, []byte{0x4C, 0x29, 0xE3}}, {x64.RSP, x64.R11, []byte{0x4C, 0x29, 0xDC}}, {x64.RBP, x64.R10, []byte{0x4C, 0x29, 0xD5}}, {x64.RSI, x64.R9, []byte{0x4C, 0x29, 0xCE}}, {x64.RDI, x64.R8, []byte{0x4C, 0x29, 0xC7}}, {x64.R8, x64.RDI, []byte{0x49, 0x29, 0xF8}}, {x64.R9, x64.RSI, []byte{0x49, 0x29, 0xF1}}, {x64.R10, x64.RBP, []byte{0x49, 0x29, 0xEA}}, {x64.R11, x64.RSP, []byte{0x49, 0x29, 0xE3}}, {x64.R12, x64.RBX, []byte{0x49, 0x29, 0xDC}}, {x64.R13, x64.RDX, []byte{0x49, 0x29, 0xD5}}, {x64.R14, x64.RCX, []byte{0x49, 0x29, 0xCE}}, {x64.R15, x64.RAX, []byte{0x49, 0x29, 0xC7}}, } for _, pattern := range usagePatterns { t.Logf("sub %s, %s", pattern.Left, pattern.Right) code := x64.SubRegisterRegister(nil, pattern.Left, pattern.Right) assert.DeepEqual(t, code, pattern.Code) } }