/* Chrysalide - Outil d'analyse de fichiers binaires
 * registers.c - aides auxiliaires relatives aux registres ARM v4/5/6
 *
 * Copyright (C) 2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "register.h"


#include <stdio.h>


#include "../../register-int.h"



/* Représentation d'un registre ARM v4/5/6 (instance) */
struct _GArmV456Register
{
    GArchRegister parent;                   /* Instance parente            */

    uint8_t index;                          /* Indice du registre          */

};


/* Représentation d'un registre ARM v4/5/6 (classe) */
struct _GArmV456RegisterClass
{
    GArchRegisterClass parent;              /* Classe parente              */

};


#define MAX_REGNAME_LEN 8


/* Initialise la classe des registres ARM v4/5/6. */
static void g_armv456_register_class_init(GArmV456RegisterClass *);

/* Initialise une instance de registre ARM v4/5/6. */
static void g_armv456_register_init(GArmV456Register *);

/* Produit une empreinte à partir d'un registre. */
static guint g_armv456_register_hash(const GArmV456Register *);



/* Indique le type défini pour une représentation d'un registre ARM v4/5/6. */
G_DEFINE_TYPE(GArmV456Register, g_armv456_register, G_TYPE_ARCH_REGISTER);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des registres ARM v4/5/6.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_armv456_register_class_init(GArmV456RegisterClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reg = instance à initialiser.                                *
*                                                                             *
*  Description : Initialise une instance de registre ARM v4/5/6.              *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_armv456_register_init(GArmV456Register *reg)
{
    GArchRegister *base;                    /* Version basique             */

    base = G_ARCH_REGISTER(reg);

    base->hash = (reg_hash_fc)g_armv456_register_hash;
    base->compare = (reg_compare_fc)g_armv456_register_compare;
    base->print = (reg_print_fc)g_armv456_register_print;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : index = indice du registre correspondant.                    *
*                                                                             *
*  Description : Crée une réprésentation de registre ARM v4/5/6.              *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GArmV456Register *g_armv456_register_new(uint8_t index)
{
    GArmV456Register *result;                /* Structure à retourner       */

    result = g_object_new(G_TYPE_ARMV456_REGISTER, NULL);

    result->index = index;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reg = registre à consulter.                                  *
*                                                                             *
*  Description : Fournit l'indice d'un registre ARM v4/5/6.                   *
*                                                                             *
*  Retour      : Inditifiant représentant le registre.                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

uint16_t g_armv456_register_get_index(const GArmV456Register *reg)
{
    return reg->index;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reg = opérande à consulter pour le calcul.                   *
*                                                                             *
*  Description : Produit une empreinte à partir d'un registre.                *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static guint g_armv456_register_hash(const GArmV456Register *reg)
{
    return reg->index;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : a = premier opérande à consulter.                            *
*                b = second opérande à consulter.                             *
*                                                                             *
*  Description : Compare un registre avec un autre.                           *
*                                                                             *
*  Retour      : Bilan de la comparaison.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

int g_armv456_register_compare(const GArmV456Register *a, const GArmV456Register *b)
{
    int result;                             /* Bilan à retourner           */

    if (a->index < b->index)
        result = -1;
    else if (a->index > b->index)
        result = 1;
    else
        result = 0;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : reg    = registre à transcrire.                              *
*                line   = ligne tampon où imprimer l'opérande donné.          *
*                syntax = type de représentation demandée.                    *
*                                                                             *
*  Description : Traduit un registre en version humainement lisible.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_armv456_register_print(const GArmV456Register *reg, GBufferLine *line, AsmSyntax syntax)
{
    char key[MAX_REGNAME_LEN];              /* Mot clef principal          */
    size_t klen;                            /* Taille de ce mot clef       */

    switch (reg->index)
    {
        case 0 ... 12:
            klen = snprintf(key, MAX_REGNAME_LEN, "r%hhu", reg->index);
            break;
        case 13:
            klen = snprintf(key, MAX_REGNAME_LEN, "sp");
            break;
        case 14:
            klen = snprintf(key, MAX_REGNAME_LEN, "lr");
            break;
        case 15:
            klen = snprintf(key, MAX_REGNAME_LEN, "pc");
            break;
        case 16:
            klen = snprintf(key, MAX_REGNAME_LEN, "cpsr");
            break;
        case 17:
            klen = snprintf(key, MAX_REGNAME_LEN, "spsr");
            break;
        default:
            klen = snprintf(key, MAX_REGNAME_LEN, "r??");
            break;
    }

    g_buffer_line_insert_text(line, BLC_ASSEMBLY, key, klen, RTT_REGISTER);

}