# Copyright (c) 2014, Fundacion Dr. Manuel Sadosky
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
This module contains all lthe classes that handle the x86 instruction
representation.

"""
from barf.arch import ARCH_X86_MODE_32
from barf.arch import ARCH_X86_MODE_64
from barf.arch import ArchitectureInformation


class X86ArchitectureInformation(ArchitectureInformation):
    """This class describe the Intel x86 architecture."""

    regs_seg = [
        ("cs", 16),
        ("ds", 16),
        ("ss", 16),
        ("es", 16),
        ("fs", 16),
        ("gs", 16),
    ]

    regs_32 = [
        ("eax", 32), ("ax", 16), ("al", 8), ("ah", 8),
        ("ebx", 32), ("bx", 16), ("bl", 8), ("bh", 8),
        ("ecx", 32), ("cx", 16), ("cl", 8), ("ch", 8),
        ("edx", 32), ("dx", 16), ("dl", 8), ("dh", 8),
        ("esi", 32), ("si", 16),
        ("edi", 32), ("di", 16),
        ("esp", 32), ("sp", 16),
        ("ebp", 32), ("bp", 16),
        ("eip", 32),
        ("eflags", 32),
    ]

    regs_32_base = [
        ("eax", 32),
        ("ebx", 32),
        ("ecx", 32),
        ("edx", 32),
        ("esi", 32),
        ("edi", 32),
        ("esp", 32),
        ("ebp", 32),
        ("eip", 32),
        ("eflags", 32),
    ]

    regs_64 = [
        ("rax", 64), ("eax",  32), ("ax",   16), ("al",   8), ("ah", 8),
        ("rbx", 64), ("ebx",  32), ("bx",   16), ("bl",   8), ("bh", 8),
        ("rcx", 64), ("ecx",  32), ("cx",   16), ("cl",   8), ("ch", 8),
        ("rdx", 64), ("edx",  32), ("dx",   16), ("dl",   8), ("dh", 8),
        ("rsi", 64), ("esi",  32), ("si",   16), ("sil",  8),
        ("rdi", 64), ("edi",  32), ("di",   16), ("dil",  8),
        ("rsp", 64), ("esp",  32), ("sp",   16), ("spl",  8),
        ("rbp", 64), ("ebp",  32), ("bp",   16), ("bpl",  8),
        ("r8",  64), ("r8d",  32), ("r8w",  16), ("r8b",  8),
        ("r9",  64), ("r9d",  32), ("r9w",  16), ("r9b",  8),
        ("r10", 64), ("r10d", 32), ("r10w", 16), ("r10b", 8),
        ("r11", 64), ("r11d", 32), ("r11w", 16), ("r11b", 8),
        ("r12", 64), ("r12d", 32), ("r12w", 16), ("r12b", 8),
        ("r13", 64), ("r13d", 32), ("r13w", 16), ("r13b", 8),
        ("r14", 64), ("r14d", 32), ("r14w", 16), ("r14b", 8),
        ("r15", 64), ("r15d", 32), ("r15w", 16), ("r15b", 8),
        ("rip", 64),
        ("rflags", 64)
    ]

    regs_64_base = [
        ("rax", 64),
        ("rbx", 64),
        ("rcx", 64),
        ("rdx", 64),
        ("rsi", 64),
        ("rdi", 64),
        ("rsp", 64),
        ("rbp", 64),
        ("r8",  64),
        ("r9",  64),
        ("r10", 64),
        ("r11", 64),
        ("r12", 64),
        ("r13", 64),
        ("r14", 64),
        ("r15", 64),
        ("rip", 64),
        ("rflags", 64)
    ]

    regs_fpu = [
        ("st0", 80),
        ("st1", 80),
        ("st2", 80),
        ("st3", 80),
        ("st4", 80),
        ("st5", 80),
        ("st6", 80),
        ("st7", 80),
    ]

    regs_mmx = [
        ("mm0", 64),
        ("mm1", 64),
        ("mm2", 64),
        ("mm3", 64),
        ("mm4", 64),
        ("mm5", 64),
        ("mm6", 64),
        ("mm7", 64),
    ]

    regs_xmm_32 = [
        ("xmm0", 128),
        ("xmm1", 128),
        ("xmm2", 128),
        ("xmm3", 128),
        ("xmm4", 128),
        ("xmm5", 128),
        ("xmm6", 128),
        ("xmm7", 128),
    ]

    regs_xmm_64 = [
        ("xmm0",  128),
        ("xmm1",  128),
        ("xmm2",  128),
        ("xmm3",  128),
        ("xmm4",  128),
        ("xmm5",  128),
        ("xmm6",  128),
        ("xmm7",  128),
        ("xmm8",  128),
        ("xmm9",  128),
        ("xmm10", 128),
        ("xmm11", 128),
        ("xmm12", 128),
        ("xmm13", 128),
        ("xmm14", 128),
        ("xmm15", 128),
    ]

    regs_ymm_32 = [
        ("ymm0", 256),
        ("ymm1", 256),
        ("ymm2", 256),
        ("ymm3", 256),
        ("ymm4", 256),
        ("ymm5", 256),
        ("ymm6", 256),
        ("ymm7", 256),
    ]