package asm_test

import (
	"testing"

	"github.com/akyoto/asm"
	"github.com/akyoto/assert"
)

func TestMulRegisterNumber(t *testing.T) {
	usagePatterns := []struct {
		Register string
		Number   int64
		Code     []byte
	}{
		{"rax", 2, []byte{0x48, 0x6b, 0xc0, 0x02}},
		{"eax", 2, []byte{0x6b, 0xc0, 0x02}},
		{"ax", 2, []byte{0x66, 0x6b, 0xc0, 0x02}},
		{"rcx", 2, []byte{0x48, 0x6b, 0xc9, 0x02}},
		{"ecx", 2, []byte{0x6b, 0xc9, 0x02}},
		{"cx", 2, []byte{0x66, 0x6b, 0xc9, 0x02}},
		{"rdx", 2, []byte{0x48, 0x6b, 0xd2, 0x02}},
		{"edx", 2, []byte{0x6b, 0xd2, 0x02}},
		{"dx", 2, []byte{0x66, 0x6b, 0xd2, 0x02}},
		{"rbx", 2, []byte{0x48, 0x6b, 0xdb, 0x02}},
		{"ebx", 2, []byte{0x6b, 0xdb, 0x02}},
		{"bx", 2, []byte{0x66, 0x6b, 0xdb, 0x02}},
		{"rsi", 2, []byte{0x48, 0x6b, 0xf6, 0x02}},
		{"esi", 2, []byte{0x6b, 0xf6, 0x02}},
		{"si", 2, []byte{0x66, 0x6b, 0xf6, 0x02}},
		{"rdi", 2, []byte{0x48, 0x6b, 0xff, 0x02}},
		{"edi", 2, []byte{0x6b, 0xff, 0x02}},
		{"di", 2, []byte{0x66, 0x6b, 0xff, 0x02}},
		{"rsp", 2, []byte{0x48, 0x6b, 0xe4, 0x02}},
		{"esp", 2, []byte{0x6b, 0xe4, 0x02}},
		{"sp", 2, []byte{0x66, 0x6b, 0xe4, 0x02}},
		{"rbp", 2, []byte{0x48, 0x6b, 0xed, 0x02}},
		{"ebp", 2, []byte{0x6b, 0xed, 0x02}},
		{"bp", 2, []byte{0x66, 0x6b, 0xed, 0x02}},
		{"r8", 2, []byte{0x4d, 0x6b, 0xc0, 0x02}},
		{"r8d", 2, []byte{0x45, 0x6b, 0xc0, 0x02}},
		{"r8w", 2, []byte{0x66, 0x45, 0x6b, 0xc0, 0x02}},
		{"r9", 2, []byte{0x4d, 0x6b, 0xc9, 0x02}},
		{"r9d", 2, []byte{0x45, 0x6b, 0xc9, 0x02}},
		{"r9w", 2, []byte{0x66, 0x45, 0x6b, 0xc9, 0x02}},
		{"r10", 2, []byte{0x4d, 0x6b, 0xd2, 0x02}},
		{"r10d", 2, []byte{0x45, 0x6b, 0xd2, 0x02}},
		{"r10w", 2, []byte{0x66, 0x45, 0x6b, 0xd2, 0x02}},
		{"r11", 2, []byte{0x4d, 0x6b, 0xdb, 0x02}},
		{"r11d", 2, []byte{0x45, 0x6b, 0xdb, 0x02}},
		{"r11w", 2, []byte{0x66, 0x45, 0x6b, 0xdb, 0x02}},
		{"r12", 2, []byte{0x4d, 0x6b, 0xe4, 0x02}},
		{"r12d", 2, []byte{0x45, 0x6b, 0xe4, 0x02}},
		{"r12w", 2, []byte{0x66, 0x45, 0x6b, 0xe4, 0x02}},
		{"r13", 2, []byte{0x4d, 0x6b, 0xed, 0x02}},
		{"r13d", 2, []byte{0x45, 0x6b, 0xed, 0x02}},
		{"r13w", 2, []byte{0x66, 0x45, 0x6b, 0xed, 0x02}},
		{"r14", 2, []byte{0x4d, 0x6b, 0xf6, 0x02}},
		{"r14d", 2, []byte{0x45, 0x6b, 0xf6, 0x02}},
		{"r14w", 2, []byte{0x66, 0x45, 0x6b, 0xf6, 0x02}},
		{"r15", 2, []byte{0x4d, 0x6b, 0xff, 0x02}},
		{"r15d", 2, []byte{0x45, 0x6b, 0xff, 0x02}},
		{"r15w", 2, []byte{0x66, 0x45, 0x6b, 0xff, 0x02}},
	}

	for _, pattern := range usagePatterns {
		a := asm.New()
		t.Logf("imul %s, %d", pattern.Register, pattern.Number)
		a.MulRegisterNumber(pattern.Register, uint64(pattern.Number))
		assert.DeepEqual(t, a.Code(), pattern.Code)
	}
}

func TestMulRegisterRegister(t *testing.T) {
	usagePatterns := []struct {
		Destination string
		Source      string
		Code        []byte
	}{
		{"rax", "rax", []byte{0x48, 0x0f, 0xaf, 0xc0}},
		{"eax", "eax", []byte{0x0f, 0xaf, 0xc0}},
		{"ax", "ax", []byte{0x66, 0x0f, 0xaf, 0xc0}},
		{"rcx", "rcx", []byte{0x48, 0x0f, 0xaf, 0xc9}},
		{"ecx", "ecx", []byte{0x0f, 0xaf, 0xc9}},
		{"cx", "cx", []byte{0x66, 0x0f, 0xaf, 0xc9}},
		{"rdx", "rdx", []byte{0x48, 0x0f, 0xaf, 0xd2}},
		{"edx", "edx", []byte{0x0f, 0xaf, 0xd2}},
		{"dx", "dx", []byte{0x66, 0x0f, 0xaf, 0xd2}},
		{"rbx", "rbx", []byte{0x48, 0x0f, 0xaf, 0xdb}},
		{"ebx", "ebx", []byte{0x0f, 0xaf, 0xdb}},
		{"bx", "bx", []byte{0x66, 0x0f, 0xaf, 0xdb}},
		{"rsi", "rsi", []byte{0x48, 0x0f, 0xaf, 0xf6}},
		{"esi", "esi", []byte{0x0f, 0xaf, 0xf6}},
		{"si", "si", []byte{0x66, 0x0f, 0xaf, 0xf6}},
		{"rdi", "rdi", []byte{0x48, 0x0f, 0xaf, 0xff}},
		{"edi", "edi", []byte{0x0f, 0xaf, 0xff}},
		{"di", "di", []byte{0x66, 0x0f, 0xaf, 0xff}},
		{"rsp", "rsp", []byte{0x48, 0x0f, 0xaf, 0xe4}},
		{"esp", "esp", []byte{0x0f, 0xaf, 0xe4}},
		{"sp", "sp", []byte{0x66, 0x0f, 0xaf, 0xe4}},
		{"rbp", "rbp", []byte{0x48, 0x0f, 0xaf, 0xed}},
		{"ebp", "ebp", []byte{0x0f, 0xaf, 0xed}},
		{"bp", "bp", []byte{0x66, 0x0f, 0xaf, 0xed}},
		{"r8", "r8", []byte{0x4d, 0x0f, 0xaf, 0xc0}},
		{"r8d", "r8d", []byte{0x45, 0x0f, 0xaf, 0xc0}},
		{"r8w", "r8w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xc0}},
		{"r9", "r9", []byte{0x4d, 0x0f, 0xaf, 0xc9}},
		{"r9d", "r9d", []byte{0x45, 0x0f, 0xaf, 0xc9}},
		{"r9w", "r9w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xc9}},
		{"r10", "r10", []byte{0x4d, 0x0f, 0xaf, 0xd2}},
		{"r10d", "r10d", []byte{0x45, 0x0f, 0xaf, 0xd2}},
		{"r10w", "r10w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xd2}},
		{"r11", "r11", []byte{0x4d, 0x0f, 0xaf, 0xdb}},
		{"r11d", "r11d", []byte{0x45, 0x0f, 0xaf, 0xdb}},
		{"r11w", "r11w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xdb}},
		{"r12", "r12", []byte{0x4d, 0x0f, 0xaf, 0xe4}},
		{"r12d", "r12d", []byte{0x45, 0x0f, 0xaf, 0xe4}},
		{"r12w", "r12w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xe4}},
		{"r13", "r13", []byte{0x4d, 0x0f, 0xaf, 0xed}},
		{"r13d", "r13d", []byte{0x45, 0x0f, 0xaf, 0xed}},
		{"r13w", "r13w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xed}},
		{"r14", "r14", []byte{0x4d, 0x0f, 0xaf, 0xf6}},
		{"r14d", "r14d", []byte{0x45, 0x0f, 0xaf, 0xf6}},
		{"r14w", "r14w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xf6}},
		{"r15", "r15", []byte{0x4d, 0x0f, 0xaf, 0xff}},
		{"r15d", "r15d", []byte{0x45, 0x0f, 0xaf, 0xff}},
		{"r15w", "r15w", []byte{0x66, 0x45, 0x0f, 0xaf, 0xff}},

		// Test the order
		{"rax", "rcx", []byte{0x48, 0x0f, 0xaf, 0xc1}},
		{"rcx", "rax", []byte{0x48, 0x0f, 0xaf, 0xc8}},
	}

	for _, pattern := range usagePatterns {
		a := asm.New()
		t.Logf("imul %s, %s", pattern.Destination, pattern.Source)
		a.MulRegisterRegister(pattern.Destination, pattern.Source)
		assert.DeepEqual(t, a.Code(), pattern.Code)
	}
}
