]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/udis86/lib/contrib/libudis86/decode.h
update
[l4.git] / l4 / pkg / udis86 / lib / contrib / libudis86 / decode.h
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 "itab.h"
31
32 #define MAX_INSN_LENGTH 15
33
34 /* register classes */
35 #define T_NONE  0
36 #define T_GPR   1
37 #define T_MMX   2
38 #define T_CRG   3
39 #define T_DBG   4
40 #define T_SEG   5
41 #define T_XMM   6
42
43 /* itab prefix bits */
44 #define P_none          ( 0 )
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 )
77
78 /* rex prefix bits */
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 ) | \
86                           ( P_REXB(n) << 0 ) )
87
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 )
92
93 /* modrm bits */
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 )
98
99 /* operand type constants -- order is important! */
100
101 enum ud_operand_code {
102     OP_NONE,
103
104     OP_A,      OP_E,      OP_M,       OP_G,       
105     OP_I,
106
107     OP_AL,     OP_CL,     OP_DL,      OP_BL,
108     OP_AH,     OP_CH,     OP_DH,      OP_BH,
109
110     OP_ALr8b,  OP_CLr9b,  OP_DLr10b,  OP_BLr11b,
111     OP_AHr12b, OP_CHr13b, OP_DHr14b,  OP_BHr15b,
112
113     OP_AX,     OP_CX,     OP_DX,      OP_BX,
114     OP_SI,     OP_DI,     OP_SP,      OP_BP,
115
116     OP_rAX,    OP_rCX,    OP_rDX,     OP_rBX,  
117     OP_rSP,    OP_rBP,    OP_rSI,     OP_rDI,
118
119     OP_rAXr8,  OP_rCXr9,  OP_rDXr10,  OP_rBXr11,  
120     OP_rSPr12, OP_rBPr13, OP_rSIr14,  OP_rDIr15,
121
122     OP_eAX,    OP_eCX,    OP_eDX,     OP_eBX,
123     OP_eSP,    OP_eBP,    OP_eSI,     OP_eDI,
124
125     OP_ES,     OP_CS,     OP_SS,      OP_DS,  
126     OP_FS,     OP_GS,
127
128     OP_ST0,    OP_ST1,    OP_ST2,     OP_ST3,
129     OP_ST4,    OP_ST5,    OP_ST6,     OP_ST7,
130
131     OP_J,      OP_S,      OP_O,          
132     OP_I1,     OP_I3, 
133
134     OP_V,      OP_W,      OP_Q,       OP_P, 
135
136     OP_R,      OP_C,  OP_D,       OP_VR,  OP_PR,
137
138     OP_MR
139 } UD_ATTR_PACKED;
140
141
142 /* operand size constants */
143
144 enum ud_operand_size {
145     SZ_NA  = 0,
146     SZ_Z   = 1,
147     SZ_V   = 2,
148     SZ_P   = 3,
149     SZ_WP  = 4,
150     SZ_DP  = 5,
151     SZ_MDQ = 6,
152     SZ_RDQ = 7,
153
154     /* the following values are used as is,
155      * and thus hard-coded. changing them 
156      * will break internals 
157      */
158     SZ_B   = 8,
159     SZ_W   = 16,
160     SZ_D   = 32,
161     SZ_Q   = 64,
162     SZ_T   = 80,
163     SZ_O   = 128,
164
165     SZ_WV  = 17,
166     SZ_BV  = 18,
167
168 } UD_ATTR_PACKED;
169
170
171 /* A single operand of an entry in the instruction table. 
172  * (internal use only)
173  */
174 struct ud_itab_entry_operand 
175 {
176   enum ud_operand_code type;
177   enum ud_operand_size size;
178 };
179
180
181 /* A single entry in an instruction table. 
182  *(internal use only)
183  */
184 struct ud_itab_entry 
185 {
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;
190   uint32_t                      prefix;
191 };
192
193 struct ud_lookup_table_list_entry {
194     const uint16_t *table;
195     enum ud_table_type type;
196     const char *meta;
197 };
198      
199
200 extern const char * ud_lookup_mnemonic( enum ud_mnemonic_code c );
201
202 static inline unsigned int sse_pfx_idx( const unsigned int pfx ) 
203 {
204     /* 00 = 0
205      * f2 = 1
206      * f3 = 2
207      * 66 = 3
208      */
209     return ( ( pfx & 0xf ) + 1 ) / 2;
210 }
211
212 static inline unsigned int mode_idx( const unsigned int mode ) 
213 {
214     /* 16 = 0
215      * 32 = 1
216      * 64 = 2
217      */
218     return ( mode / 32 );
219 }
220
221 static inline unsigned int modrm_mod_idx( const unsigned int mod )
222 {
223     /* !11 = 0
224      *  11 = 1
225      */
226     return ( mod + 1 ) / 4;
227 }
228
229 static inline unsigned int vendor_idx( const unsigned int vendor )
230 {
231     switch ( vendor ) {
232         case UD_VENDOR_AMD: return 0;
233         case UD_VENDOR_INTEL: return 1;
234         case UD_VENDOR_ANY: return 2; 
235         default: return 2;
236     }
237 }
238
239 static inline unsigned int is_group_ptr( uint16_t ptr )
240 {
241     return ( 0x8000 & ptr );
242 }
243
244 static inline unsigned int group_idx( uint16_t ptr )
245 {
246     return ( ~0x8000 & ptr );
247 }
248
249
250 extern struct ud_itab_entry ud_itab[];
251 extern struct ud_lookup_table_list_entry ud_lookup_table_list[];
252
253 #endif /* UD_DECODE_H */
254
255 /* vim:cindent
256  * vim:expandtab
257  * vim:ts=4
258  * vim:sw=4
259  */