1 /* udis86 - libudis86/decode.h
3 * Copyright (c) 2002-2009 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
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.
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.
32 #define MAX_INSN_LENGTH 15
34 /* register classes */
43 /* itab prefix bits */
45 #define P_cast ( 1 << 0 )
46 #define P_CAST(n) ( ( n >> 0 ) & 1 )
47 #define P_c1 ( 1 << 0 )
48 #define P_C1(n) ( ( n >> 0 ) & 1 )
49 #define P_rexb ( 1 << 1 )
50 #define P_REXB(n) ( ( n >> 1 ) & 1 )
51 #define P_depM ( 1 << 2 )
52 #define P_DEPM(n) ( ( n >> 2 ) & 1 )
53 #define P_c3 ( 1 << 3 )
54 #define P_C3(n) ( ( n >> 3 ) & 1 )
55 #define P_inv64 ( 1 << 4 )
56 #define P_INV64(n) ( ( n >> 4 ) & 1 )
57 #define P_rexw ( 1 << 5 )
58 #define P_REXW(n) ( ( n >> 5 ) & 1 )
59 #define P_c2 ( 1 << 6 )
60 #define P_C2(n) ( ( n >> 6 ) & 1 )
61 #define P_def64 ( 1 << 7 )
62 #define P_DEF64(n) ( ( n >> 7 ) & 1 )
63 #define P_rexr ( 1 << 8 )
64 #define P_REXR(n) ( ( n >> 8 ) & 1 )
65 #define P_oso ( 1 << 9 )
66 #define P_OSO(n) ( ( n >> 9 ) & 1 )
67 #define P_aso ( 1 << 10 )
68 #define P_ASO(n) ( ( n >> 10 ) & 1 )
69 #define P_rexx ( 1 << 11 )
70 #define P_REXX(n) ( ( n >> 11 ) & 1 )
71 #define P_ImpAddr ( 1 << 12 )
72 #define P_IMPADDR(n) ( ( n >> 12 ) & 1 )
73 #define P_seg ( 1 << 13 )
74 #define P_SEG(n) ( ( n >> 13 ) & 1 )
75 #define P_sext ( 1 << 14 )
76 #define P_SEXT(n) ( ( n >> 14 ) & 1 )
79 #define REX_W(r) ( ( 0xF & ( r ) ) >> 3 )
80 #define REX_R(r) ( ( 0x7 & ( r ) ) >> 2 )
81 #define REX_X(r) ( ( 0x3 & ( r ) ) >> 1 )
82 #define REX_B(r) ( ( 0x1 & ( r ) ) >> 0 )
83 #define REX_PFX_MASK(n) ( ( P_REXW(n) << 3 ) | \
84 ( P_REXR(n) << 2 ) | \
85 ( P_REXX(n) << 1 ) | \
88 /* scable-index-base bits */
89 #define SIB_S(b) ( ( b ) >> 6 )
90 #define SIB_I(b) ( ( ( b ) >> 3 ) & 7 )
91 #define SIB_B(b) ( ( b ) & 7 )
94 #define MODRM_REG(b) ( ( ( b ) >> 3 ) & 7 )
95 #define MODRM_NNN(b) ( ( ( b ) >> 3 ) & 7 )
96 #define MODRM_MOD(b) ( ( ( b ) >> 6 ) & 3 )
97 #define MODRM_RM(b) ( ( b ) & 7 )
99 /* operand type constants -- order is important! */
101 enum ud_operand_code {
104 OP_A, OP_E, OP_M, OP_G,
107 OP_AL, OP_CL, OP_DL, OP_BL,
108 OP_AH, OP_CH, OP_DH, OP_BH,
110 OP_ALr8b, OP_CLr9b, OP_DLr10b, OP_BLr11b,
111 OP_AHr12b, OP_CHr13b, OP_DHr14b, OP_BHr15b,
113 OP_AX, OP_CX, OP_DX, OP_BX,
114 OP_SI, OP_DI, OP_SP, OP_BP,
116 OP_rAX, OP_rCX, OP_rDX, OP_rBX,
117 OP_rSP, OP_rBP, OP_rSI, OP_rDI,
119 OP_rAXr8, OP_rCXr9, OP_rDXr10, OP_rBXr11,
120 OP_rSPr12, OP_rBPr13, OP_rSIr14, OP_rDIr15,
122 OP_eAX, OP_eCX, OP_eDX, OP_eBX,
123 OP_eSP, OP_eBP, OP_eSI, OP_eDI,
125 OP_ES, OP_CS, OP_SS, OP_DS,
128 OP_ST0, OP_ST1, OP_ST2, OP_ST3,
129 OP_ST4, OP_ST5, OP_ST6, OP_ST7,
134 OP_V, OP_W, OP_Q, OP_P,
136 OP_R, OP_C, OP_D, OP_VR, OP_PR,
142 /* operand size constants */
144 enum ud_operand_size {
154 /* the following values are used as is,
155 * and thus hard-coded. changing them
156 * will break internals
171 /* A single operand of an entry in the instruction table.
172 * (internal use only)
174 struct ud_itab_entry_operand
176 enum ud_operand_code type;
177 enum ud_operand_size size;
181 /* A single entry in an instruction table.
186 enum ud_mnemonic_code mnemonic;
187 struct ud_itab_entry_operand operand1;
188 struct ud_itab_entry_operand operand2;
189 struct ud_itab_entry_operand operand3;
193 struct ud_lookup_table_list_entry {
194 const uint16_t *table;
195 enum ud_table_type type;
200 extern const char * ud_lookup_mnemonic( enum ud_mnemonic_code c );
202 static inline unsigned int sse_pfx_idx( const unsigned int pfx )
209 return ( ( pfx & 0xf ) + 1 ) / 2;
212 static inline unsigned int mode_idx( const unsigned int mode )
218 return ( mode / 32 );
221 static inline unsigned int modrm_mod_idx( const unsigned int mod )
226 return ( mod + 1 ) / 4;
229 static inline unsigned int vendor_idx( const unsigned int vendor )
232 case UD_VENDOR_AMD: return 0;
233 case UD_VENDOR_INTEL: return 1;
234 case UD_VENDOR_ANY: return 2;
239 static inline unsigned int is_group_ptr( uint16_t ptr )
241 return ( 0x8000 & ptr );
244 static inline unsigned int group_idx( uint16_t ptr )
246 return ( ~0x8000 & ptr );
250 extern struct ud_itab_entry ud_itab[];
251 extern struct ud_lookup_table_list_entry ud_lookup_table_list[];
253 #endif /* UD_DECODE_H */