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/* itab prefix bits */
35#define P_none          ( 0 )
36#define P_cast          ( 1 << 0 )
37#define P_CAST(n)       ( ( n >> 0 ) & 1 )
38#define P_rexb          ( 1 << 1 )
39#define P_REXB(n)       ( ( n >> 1 ) & 1 )
40#define P_inv64         ( 1 << 4 )
41#define P_INV64(n)      ( ( n >> 4 ) & 1 )
42#define P_rexw          ( 1 << 5 )
43#define P_REXW(n)       ( ( n >> 5 ) & 1 )
44#define P_def64         ( 1 << 7 )
45#define P_DEF64(n)      ( ( n >> 7 ) & 1 )
46#define P_rexr          ( 1 << 8 )
47#define P_REXR(n)       ( ( n >> 8 ) & 1 )
48#define P_oso           ( 1 << 9 )
49#define P_OSO(n)        ( ( n >> 9 ) & 1 )
50#define P_aso           ( 1 << 10 )
51#define P_ASO(n)        ( ( n >> 10 ) & 1 )
52#define P_rexx          ( 1 << 11 )
53#define P_REXX(n)       ( ( n >> 11 ) & 1 )
54#define P_ImpAddr       ( 1 << 12 )
55#define P_IMPADDR(n)    ( ( n >> 12 ) & 1 )
56#define P_seg           ( 1 << 13 )
57#define P_SEG(n)        ( ( n >> 13 ) & 1 )
58#define P_str           ( 1 << 14 )
59#define P_STR(n)        ( ( n >> 14 ) & 1 )
60#define P_strz          ( 1 << 15 )
61#define P_STR_ZF(n)     ( ( n >> 15 ) & 1 )
62
63/* operand type constants -- order is important! */
64
65enum ud_operand_code {
66    OP_NONE,
67
68    OP_A,      OP_E,      OP_M,       OP_G,
69    OP_I,      OP_F,
70
71    OP_R0,     OP_R1,     OP_R2,      OP_R3,
72    OP_R4,     OP_R5,     OP_R6,      OP_R7,
73
74    OP_AL,     OP_CL,     OP_DL,
75    OP_AX,     OP_CX,     OP_DX,
76    OP_eAX,    OP_eCX,    OP_eDX,
77    OP_rAX,    OP_rCX,    OP_rDX,
78
79    OP_ES,     OP_CS,     OP_SS,      OP_DS,
80    OP_FS,     OP_GS,
81
82    OP_ST0,    OP_ST1,    OP_ST2,     OP_ST3,
83    OP_ST4,    OP_ST5,    OP_ST6,     OP_ST7,
84
85    OP_J,      OP_S,      OP_O,
86    OP_I1,     OP_I3,     OP_sI,
87
88    OP_V,      OP_W,      OP_Q,       OP_P,
89    OP_U,      OP_N,      OP_MU,
90
91    OP_R,      OP_C,      OP_D,
92
93    OP_MR
94} UD_ATTR_PACKED;
95
96
97/* operand size constants */
98
99enum ud_operand_size {
100    SZ_NA  = 0,
101    SZ_Z   = 1,
102    SZ_V   = 2,
103    SZ_RDQ = 7,
104
105    /* the following values are used as is,
106     * and thus hard-coded. changing them
107     * will break internals
108     */
109    SZ_B   = 8,
110    SZ_W   = 16,
111    SZ_D   = 32,
112    SZ_Q   = 64,
113    SZ_T   = 80,
114    SZ_O   = 128,
115
116    SZ_Y   = 17,
117
118    /*
119     * complex size types, that encode sizes for operands
120     * of type MR (memory or register), for internal use
121     * only. Id space 256 and above.
122     */
123    SZ_BD  = (SZ_B << 8) | SZ_D,
124    SZ_BV  = (SZ_B << 8) | SZ_V,
125    SZ_WD  = (SZ_W << 8) | SZ_D,
126    SZ_WV  = (SZ_W << 8) | SZ_V,
127    SZ_WY  = (SZ_W << 8) | SZ_Y,
128    SZ_DY  = (SZ_D << 8) | SZ_Y,
129    SZ_WO  = (SZ_W << 8) | SZ_O,
130    SZ_DO  = (SZ_D << 8) | SZ_O,
131    SZ_QO  = (SZ_Q << 8) | SZ_O,
132
133} UD_ATTR_PACKED;
134
135
136/* resolve complex size type.
137 */
138static inline enum ud_operand_size
139Mx_mem_size(enum ud_operand_size size)
140{
141    return (size >> 8) & 0xff;
142}
143
144static inline enum ud_operand_size
145Mx_reg_size(enum ud_operand_size size)
146{
147    return size & 0xff;
148}
149
150/* A single operand of an entry in the instruction table.
151 * (internal use only)
152 */
153struct ud_itab_entry_operand
154{
155  enum ud_operand_code type;
156  enum ud_operand_size size;
157};
158
159
160/* A single entry in an instruction table.
161 *(internal use only)
162 */
163struct ud_itab_entry
164{
165  enum ud_mnemonic_code         mnemonic;
166  struct ud_itab_entry_operand  operand1;
167  struct ud_itab_entry_operand  operand2;
168  struct ud_itab_entry_operand  operand3;
169  uint32_t                      prefix;
170};
171
172struct ud_lookup_table_list_entry {
173    const uint16_t *table;
174    enum ud_table_type type;
175    const char *meta;
176};
177
178
179
180static inline int
181ud_opcode_field_sext(uint8_t primary_opcode)
182{
183  return (primary_opcode & 0x02) != 0;
184}
185
186extern struct ud_itab_entry ud_itab[];
187extern struct ud_lookup_table_list_entry ud_lookup_table_list[];
188
189#endif /* UD_DECODE_H */
190
191/* vim:cindent
192 * vim:expandtab
193 * vim:ts=4
194 * vim:sw=4
195 */
196