/*
Copyright (c) 2013, Jurriaan Bremer
All rights reserved.

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

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* 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.
* Neither the name of the darm developer(s) nor the names of its
  contributors may be used to endorse or promote products derived from this
  software without specific prior written permission.

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.
*/
#include <stdio.h>
#include <stdint.h>
#include "thumb-tbl.h"
darm_enctype_t thumb_instr_types[] = {
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//3
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//6
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//9
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//12
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//15
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//18
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM,	//21
    T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_SHIFT_IMM, T_THUMB_3REG,	//25
    T_THUMB_3REG, T_THUMB_3REG, T_THUMB_3REG, T_THUMB_2REG_IMM,							//29
    T_THUMB_2REG_IMM, T_THUMB_2REG_IMM, T_THUMB_2REG_IMM, T_THUMB_HAS_IMM8,	//33
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//37
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//41
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//45
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//49
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//53
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//57
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,	//61
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_GPI,			//65
    T_THUMB_GPI, T_THUMB_GPI, T_THUMB_GPI, T_THUMB_MOD_SP_REG, T_THUMB_CMP,	//70
    T_THUMB_MOV4, T_THUMB_BRANCH_REG, T_THUMB_LDR_PC, T_THUMB_LDR_PC,				//74
    T_THUMB_LDR_PC, T_THUMB_LDR_PC, T_THUMB_LDR_PC, T_THUMB_LDR_PC,					//78
    T_THUMB_LDR_PC, T_THUMB_LDR_PC, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO,				//82
    T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO,			//86
    T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO,			//90
    T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMO,			//94
    T_THUMB_RW_MEMO, T_THUMB_RW_MEMO, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//98
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//102
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//106
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//110
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//114
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//118
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//122
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//126
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//130
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//134
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//138
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_RW_MEMI,			//142
    T_THUMB_RW_MEMI, T_THUMB_RW_MEMI, T_THUMB_STACK, T_THUMB_STACK,					//146
    T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK,	//151
    T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK,	//156
    T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK, T_THUMB_STACK,									//160
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,			//164
    T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8, T_THUMB_HAS_IMM8,			//168
    T_THUMB_ADD_SP_IMM, T_THUMB_ADD_SP_IMM, T_THUMB_ADD_SP_IMM,									//171
    T_THUMB_ADD_SP_IMM, T_THUMB_ADD_SP_IMM, T_THUMB_ADD_SP_IMM,									//174
    T_THUMB_ADD_SP_IMM, T_THUMB_ADD_SP_IMM, T_THUMB_MOD_SP_IMM, T_THUMB_CBZ,		//178
    T_THUMB_EXTEND, T_THUMB_CBZ, T_THUMB_PUSHPOP, T_THUMB_PUSHPOP,							//182
    T_THUMB_SETEND, T_INVLD, T_INVLD, T_THUMB_CBZ, T_THUMB_REV, T_THUMB_CBZ,		//188
    T_THUMB_PUSHPOP, T_THUMB_PUSHPOP, T_THUMB_ONLY_IMM8, T_THUMB_IT_HINTS,			//192
    T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG,							//196
    T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG,							//200
    T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG,							//204
    T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG, T_THUMB_RW_REG,							//208
    T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH,							//211
    T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH,							//214
    T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH,							//217
    T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH,							//220
    T_THUMB_COND_BRANCH, T_THUMB_COND_BRANCH, T_THUMB_ONLY_IMM8,								//223
    T_THUMB_ONLY_IMM8, T_THUMB_UNCOND_BRANCH, T_THUMB_UNCOND_BRANCH,						//226
    T_THUMB_UNCOND_BRANCH, T_THUMB_UNCOND_BRANCH, T_THUMB_UNCOND_BRANCH,				//229
    T_THUMB_UNCOND_BRANCH, T_THUMB_UNCOND_BRANCH, T_THUMB_UNCOND_BRANCH,				//232
    T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD,			//240
    T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD,			//248
    T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD, T_INVLD			//256
};

darm_instr_t thumb_instr_labels[] = {
    I_LSL, I_LSL, I_LSL, I_LSL, I_LSL, I_LSL, I_LSL, I_LSL, I_LSR, I_LSR,				//10
    I_LSR, I_LSR, I_LSR, I_LSR, I_LSR, I_LSR, I_ASR, I_ASR, I_ASR, I_ASR,				//20
    I_ASR, I_ASR, I_ASR, I_ASR, I_ADD, I_ADD, I_SUB, I_SUB, I_ADD, I_ADD,				//30
    I_SUB, I_SUB, I_MOV, I_MOV, I_MOV, I_MOV, I_MOV, I_MOV, I_MOV, I_MOV,				//40
    I_CMP, I_CMP, I_CMP, I_CMP, I_CMP, I_CMP, I_CMP, I_CMP, I_ADD, I_ADD,				//50
    I_ADD, I_ADD, I_ADD, I_ADD, I_ADD, I_ADD, I_SUB, I_SUB, I_SUB, I_SUB,				//60
    I_SUB, I_SUB, I_SUB, I_SUB, I_LSR, I_SBC, I_TST, I_ORR, I_ADD, I_CMP,				//70
    I_MOV, I_BX, I_LDR, I_LDR, I_LDR, I_LDR, I_LDR, I_LDR, I_LDR, I_LDR,				//80
    I_STR, I_STR, I_STRH, I_STRH, I_STRB, I_STRB, I_LDRSB, I_LDRSB, I_LDR,			//89
    I_LDR, I_LDRH, I_LDRH, I_LDRB, I_LDRB, I_LDRSH, I_LDRSH, I_STR, I_STR,			//98
    I_STR, I_STR, I_STR, I_STR, I_STR, I_STR, I_LDR, I_LDR, I_LDR, I_LDR,				//108
    I_LDR, I_LDR, I_LDR, I_LDR, I_STRB, I_STRB, I_STRB, I_STRB, I_STRB,					//117
    I_STRB, I_STRB, I_STRB, I_LDRB, I_LDRB, I_LDRB, I_LDRB, I_LDRB, I_LDRB,			//126
    I_LDRB, I_LDRB, I_STRH, I_STRH, I_STRH, I_STRH, I_STRH, I_STRH, I_STRH,			//135
    I_STRH, I_LDRH, I_LDRH, I_LDRH, I_LDRH, I_LDRH, I_LDRH, I_LDRH, I_LDRH,			//144
    I_STR, I_STR, I_STR, I_STR, I_STR, I_STR, I_STR, I_STR, I_LDR, I_LDR,				//154
    I_LDR, I_LDR, I_LDR, I_LDR, I_LDR, I_LDR, I_ADR, I_ADR, I_ADR, I_ADR,				//164
    I_ADR, I_ADR, I_ADR, I_ADR, I_ADD, I_ADD, I_ADD, I_ADD, I_ADD, I_ADD,				//174
    I_ADD, I_ADD, I_SUB, I_CBZ, I_UXTH, I_CBZ, I_PUSH, I_PUSH, I_SETEND,				//183
    I_INVLD, I_INVLD, I_CBZ, I_REVSH, I_CBZ, I_POP, I_POP, I_BKPT, I_YIELD,			//192
    I_STM, I_STM, I_STM, I_STM, I_STM, I_STM, I_STM, I_STM, I_LDM, I_LDM,				//202
    I_LDM, I_LDM, I_LDM, I_LDM, I_LDM, I_LDM, I_B, I_B, I_B, I_B, I_B, I_B,			//214
    I_B, I_B, I_B, I_B, I_B, I_B, I_B, I_B, I_UDF, I_SVC, I_B, I_B, I_B, I_B,		//228
    I_B, I_B, I_B, I_B, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD,		//238
    I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD,			//246
    I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD, I_INVLD,			//254
    I_INVLD, I_INVLD
};

darm_instr_t type_gpi_instr_lookup[] = {
    I_AND, I_EOR, I_LSL, I_LSR, I_ASR, I_ADC, I_SBC, I_ROR, I_TST, I_RSB,
    I_CMP, I_CMN, I_ORR, I_MUL, I_BIC, I_MVN
};

darm_instr_t type_hints_instr_lookup[] = {
    I_NOP, I_YIELD, I_WFE, I_WFI, I_SEV
};

darm_instr_t type_extend_instr_lookup[] = {
    I_SXTH, I_SXTB, I_UXTH, I_UXTB
};

darm_instr_t type_rev_instr_lookup[] = {
    I_REV, I_REV16, I_INVLD, I_REVSH
};

