1/* udis86 - libudis86/decode.h 2 * 3 * Copyright (c) 2002-2009 Vivek Thampi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26#ifndef UD_DECODE_H 27#define UD_DECODE_H 28 29#include "types.h" 30#include "udint.h" 31#include "itab.h" 32 33#define MAX_INSN_LENGTH 15 34 35/* itab prefix bits */ 36#define P_none ( 0 ) 37 38#define P_inv64 ( 1 << 0 ) 39#define P_INV64(n) ( ( n >> 0 ) & 1 ) 40#define P_def64 ( 1 << 1 ) 41#define P_DEF64(n) ( ( n >> 1 ) & 1 ) 42 43#define P_oso ( 1 << 2 ) 44#define P_OSO(n) ( ( n >> 2 ) & 1 ) 45#define P_aso ( 1 << 3 ) 46#define P_ASO(n) ( ( n >> 3 ) & 1 ) 47 48#define P_rexb ( 1 << 4 ) 49#define P_REXB(n) ( ( n >> 4 ) & 1 ) 50#define P_rexw ( 1 << 5 ) 51#define P_REXW(n) ( ( n >> 5 ) & 1 ) 52#define P_rexr ( 1 << 6 ) 53#define P_REXR(n) ( ( n >> 6 ) & 1 ) 54#define P_rexx ( 1 << 7 ) 55#define P_REXX(n) ( ( n >> 7 ) & 1 ) 56 57#define P_seg ( 1 << 8 ) 58#define P_SEG(n) ( ( n >> 8 ) & 1 ) 59 60#define P_vexl ( 1 << 9 ) 61#define P_VEXL(n) ( ( n >> 9 ) & 1 ) 62#define P_vexw ( 1 << 10 ) 63#define P_VEXW(n) ( ( n >> 10 ) & 1 ) 64 65#define P_str ( 1 << 11 ) 66#define P_STR(n) ( ( n >> 11 ) & 1 ) 67#define P_strz ( 1 << 12 ) 68#define P_STR_ZF(n) ( ( n >> 12 ) & 1 ) 69 70/* operand type constants -- order is important! */ 71 72enum ud_operand_code { 73 OP_NONE, 74 75 OP_A, OP_E, OP_M, OP_G, 76 OP_I, OP_F, 77 78 OP_R0, OP_R1, OP_R2, OP_R3, 79 OP_R4, OP_R5, OP_R6, OP_R7, 80 81 OP_AL, OP_CL, OP_DL, 82 OP_AX, OP_CX, OP_DX, 83 OP_eAX, OP_eCX, OP_eDX, 84 OP_rAX, OP_rCX, OP_rDX, 85 86 OP_ES, OP_CS, OP_SS, OP_DS, 87 OP_FS, OP_GS, 88 89 OP_ST0, OP_ST1, OP_ST2, OP_ST3, 90 OP_ST4, OP_ST5, OP_ST6, OP_ST7, 91 92 OP_J, OP_S, OP_O, 93 OP_I1, OP_I3, OP_sI, 94 95 OP_V, OP_W, OP_Q, OP_P, 96 OP_U, OP_N, OP_MU, OP_H, 97 OP_L, 98 99 OP_R, OP_C, OP_D, 100 101 OP_MR 102} UD_ATTR_PACKED; 103 104 105/* 106 * Operand size constants 107 * 108 * Symbolic constants for various operand sizes. Some of these constants 109 * are given a value equal to the width of the data (SZ_B == 8), such 110 * that they maybe used interchangeably in the internals. Modifying them 111 * will most certainly break things! 112 */ 113typedef uint16_t ud_operand_size_t; 114 115#define SZ_NA 0 116#define SZ_Z 1 117#define SZ_V 2 118#define SZ_Y 3 119#define SZ_X 4 120#define SZ_RDQ 7 121#define SZ_B 8 122#define SZ_W 16 123#define SZ_D 32 124#define SZ_Q 64 125#define SZ_T 80 126#define SZ_O 12 127#define SZ_DQ 128 /* double quad */ 128#define SZ_QQ 256 /* quad quad */ 129 130/* 131 * Complex size types; that encode sizes for operands of type MR (memory or 132 * register); for internal use only. Id space above 256. 133 */ 134#define SZ_BD ((SZ_B << 8) | SZ_D) 135#define SZ_BV ((SZ_B << 8) | SZ_V) 136#define SZ_WD ((SZ_W << 8) | SZ_D) 137#define SZ_WV ((SZ_W << 8) | SZ_V) 138#define SZ_WY ((SZ_W << 8) | SZ_Y) 139#define SZ_DY ((SZ_D << 8) | SZ_Y) 140#define SZ_WO ((SZ_W << 8) | SZ_O) 141#define SZ_DO ((SZ_D << 8) | SZ_O) 142#define SZ_QO ((SZ_Q << 8) | SZ_O) 143 144 145/* resolve complex size type. 146 */ 147static UD_INLINE ud_operand_size_t 148Mx_mem_size(ud_operand_size_t size) 149{ 150 return (size >> 8) & 0xff; 151} 152 153static UD_INLINE ud_operand_size_t 154Mx_reg_size(ud_operand_size_t size) 155{ 156 return size & 0xff; 157} 158 159/* A single operand of an entry in the instruction table. 160 * (internal use only) 161 */ 162struct ud_itab_entry_operand 163{ 164 enum ud_operand_code type; 165 ud_operand_size_t size; 166}; 167 168 169/* A single entry in an instruction table. 170 *(internal use only) 171 */ 172struct ud_itab_entry 173{ 174 enum ud_mnemonic_code mnemonic; 175 struct ud_itab_entry_operand operand1; 176 struct ud_itab_entry_operand operand2; 177 struct ud_itab_entry_operand operand3; 178 struct ud_itab_entry_operand operand4; 179 uint32_t prefix; 180}; 181 182struct ud_lookup_table_list_entry { 183 const uint16_t *table; 184 enum ud_table_type type; 185 const char *meta; 186}; 187 188extern struct ud_itab_entry ud_itab[]; 189extern struct ud_lookup_table_list_entry ud_lookup_table_list[]; 190 191#endif /* UD_DECODE_H */ 192 193/* vim:cindent 194 * vim:expandtab 195 * vim:ts=4 196 * vim:sw=4 197 */ 198