The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
ld.h
1 /*
2 x86 Length Disassembler.
3 Copyright (C) 2016 Alessandro Pellegrini
4 Copyright (C) 2013 Byron Platt
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 /* table macros */
23 #define BITMASK32( \
24  b00,b01,b02,b03,b04,b05,b06,b07, \
25  b08,b09,b0a,b0b,b0c,b0d,b0e,b0f, \
26  b10,b11,b12,b13,b14,b15,b16,b17, \
27  b18,b19,b1a,b1b,b1c,b1d,b1e,b1f \
28 ) ( \
29  (b00<<0x00)|(b01<<0x01)|(b02<<0x02)|(b03<<0x03)| \
30  (b04<<0x04)|(b05<<0x05)|(b06<<0x06)|(b07<<0x07)| \
31  (b08<<0x08)|(b09<<0x09)|(b0a<<0x0a)|(b0b<<0x0b)| \
32  (b0c<<0x0c)|(b0d<<0x0d)|(b0e<<0x0e)|(b0f<<0x0f)| \
33  (b10<<0x10)|(b11<<0x11)|(b12<<0x12)|(b13<<0x13)| \
34  (b14<<0x14)|(b15<<0x15)|(b16<<0x16)|(b17<<0x17)| \
35  (b18<<0x18)|(b19<<0x19)|(b1a<<0x1a)|(b1b<<0x1b)| \
36  (b1c<<0x1c)|(b1d<<0x1d)|(b1e<<0x1e)|(b1f<<0x1f) \
37 )
38 #define CHECK_TABLE(t, v) ((t[(v)>>5]>>((v)&0x1f))&1)
39 
40 /* CHECK_PREFIX */
41 const static unsigned int prefix_t[] = {
42  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
43  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 0 */
44  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 1 */
45  BITMASK32(0,0,0,0,0,0,1,0, 0,0,0,0,0,0,1,0, /* 2 */
46  0,0,0,0,0,0,1,0, 0,0,0,0,0,0,1,0), /* 3 */
47  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 4 */
48  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 5 */
49  BITMASK32(0,0,0,0,1,1,1,1, 0,0,0,0,0,0,0,0, /* 6 */
50  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 7 */
51  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
52  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 9 */
53  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* a */
54  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* b */
55  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* c */
56  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* d */
57  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* e */
58  1,0,1,1,0,0,0,0, 0,0,0,0,0,0,0,0) /* f */
59 };
60 #define CHECK_PREFIX(v) CHECK_TABLE(prefix_t, v)
61 
62 /* x64 REX prefix handling */
63 #define CHECK_REX(v) (((v)>>4)==0x4)
64 #define CHECK_REXW(v) ((v)&0x08)
65 
66 /* Check for movs which handle imm64 */
67 #define CHECK_IMM64(v) (((v)&0xf8)==0xb8)
68 
69 /* Check for movs which handles moffs64 */
70 #define CHECK_OFF64(v) ((v)==0xa1)
71 
72 /* CHECK_PREFIX_66 */
73 #define CHECK_PREFIX_66(v) ((v)==0x66)
74 
75 /* CHECK_PREFIX_67 */
76 #define CHECK_PREFIX_67(v) ((v)==0x67)
77 
78 /* CHECK_0F */
79 #define CHECK_0F(v) ((v)==0x0f)
80 
81 /* CHECK_38 */
82 #define CHECK_38(v) ((v)==0x38)
83 
84 /* CHECK_3A */
85 #define CHECK_3A(v) ((v)==0x3a)
86 
87 /* CHECK_MODRM3A */
88 const static unsigned int modrm3a_t[] = {
89  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 0 */
91  0,0,0,0,1,1,1,1, 1,1,1,1,0,1,1,1), /* 1 */
92  BITMASK32(1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0, /* 2 */
93  1,1,1,1,0,0,0,0, 1,1,1,1,0,1,1,1), /* 3 */
94  BITMASK32(1,1,1,1,1,0,1,0, 1,1,1,1,1,0,0,0, /* 4 */
95  1,1,1,0,1,1,1,1, 0,0,0,0,1,1,1,1), /* 5 */
96  BITMASK32(1,1,1,1,0,0,1,1, 1,1,1,1,1,1,1,1, /* 6 */
97  0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1), /* 7 */
98  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
99  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 9 */
100  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* a */
101  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* b */
102  BITMASK32(0,0,0,0,0,0,0,0, 0,0,1,1,1,0,0,0, /* c */
103  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1), /* d */
104  BITMASK32(0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0, /* e */
105  1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0) /* f */
106 };
107 #define CHECK_MODRM3A(v) CHECK_TABLE(modrm3a_t, v)
108 
109 /* CHECK_MODRM38 */
110 const static unsigned int modrm38_t[] = {
111  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
112  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 0 */
113  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 1 */
114  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 2 */
115  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 3 */
116  BITMASK32(1,1,1,1,1,1,1,1, 0,0,0,0,1,1,1,1, /* 4 */
117  1,1,1,1,0,1,0,0, 1,1,1,1,1,1,1,1), /* 5 */
118  BITMASK32(0,0,0,0,1,1,1,0, 0,0,0,0,1,1,1,1, /* 6 */
119  0,0,0,0,1,1,1,1, 1,1,1,1,1,1,1,1), /* 7 */
120  BITMASK32(1,1,1,1,1,0,1,1, 1,1,1,1,1,1,1,1, /* 8 */
121  1,1,1,1,0,0,1,1, 1,1,1,1,1,1,1,1), /* 9 */
122  BITMASK32(1,1,1,1,1,0,1,1, 1,1,1,1,1,1,1,1, /* a */
123  0,0,0,0,1,1,1,1, 1,1,1,1,1,1,1,1), /* b */
124  BITMASK32(0,0,0,0,0,1,1,1, 1,1,1,1,1,1,0,0, /* c */
125  1,1,0,0,1,1,0,0, 0,0,0,1,1,1,1,1), /* d */
126  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* e */
127  1,1,1,1,0,1,1,1, 0,0,0,0,0,0,0,0) /* f */
128 };
129 #define CHECK_MODRM38(v) CHECK_TABLE(modrm38_t, v)
130 
131 /* CHECK_MODRM2 */
132 const static unsigned int modrm2_t[] = {
133  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
134  BITMASK32(1,1,1,1,0,0,0,0, 0,0,0,0,0,0,0,0, /* 0 */
135  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 1 */
136  BITMASK32(1,1,1,1,0,0,0,0, 1,1,1,1,1,1,1,1, /* 2 */
137  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 3 */
138  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 4 */
139  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 5 */
140  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 6 */
141  1,0,0,0,1,1,1,0, 1,1,0,0,1,1,1,1), /* 7 */
142  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
143  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 9 */
144  BITMASK32(0,0,0,1,1,1,0,0, 0,0,0,1,1,1,0,1, /* a */
145  1,1,1,1,1,1,1,1, 1,0,1,1,1,1,1,1), /* b */
146  BITMASK32(1,1,1,1,1,1,1,0, 0,0,0,0,0,0,0,0, /* c */
147  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* d */
148  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* e */
149  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1) /* f */
150 };
151 #define CHECK_MODRM2(v) CHECK_TABLE(modrm2_t, v)
152 
153 /* CHECK_DATA12 */
154 #define CHECK_DATA12(v) ((v)==0xa4||(v)==0xac||(v)==0xba)
155 
156 /* CHECK_DATA662 */
157 #define CHECK_DATA662(v) (((v)&0xf0)==0x80)
158 
159 /* CHECK_MODRM */
160 const static unsigned int modrm_t[] = {
161  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
162  BITMASK32(1,1,1,1,0,0,0,0, 1,1,1,1,0,0,0,0, /* 0 */
163  1,1,1,1,0,0,0,0, 1,1,1,1,0,0,0,0), /* 1 */
164  BITMASK32(1,1,1,1,0,0,0,0, 1,1,1,1,0,0,0,0, /* 2 */
165  1,1,1,1,0,0,0,0, 1,1,1,1,0,0,0,0), /* 3 */
166  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 4 */
167  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 5 */
168  BITMASK32(0,0,1,1,0,0,0,0, 0,1,0,1,0,0,0,0, /* 6 */
169  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 7 */
170  BITMASK32(1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, /* 8 */
171  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 9 */
172  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* a */
173  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* b */
174  BITMASK32(1,1,0,0,1,1,1,1, 0,0,0,0,0,0,0,0, /* c */
175  1,1,1,1,0,0,0,0, 1,1,1,1,1,1,1,1), /* d */
176  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* e */
177  0,0,0,0,0,0,1,1, 0,0,0,0,0,0,1,1) /* f */
178 };
179 #define CHECK_MODRM(v) CHECK_TABLE(modrm_t, v)
180 
181 /* CHECK_TEST */
182 #define CHECK_TEST(v) ((v)==0xf6||(v)==0xf7)
183 
184 /* CHECK_DATA1 */
185 const static unsigned int data1_t[] = {
186  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
187  BITMASK32(0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0, /* 0 */
188  0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0), /* 1 */
189  BITMASK32(0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0, /* 2 */
190  0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0), /* 3 */
191  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 4 */
192  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 5 */
193  BITMASK32(0,0,0,0,0,0,0,0, 0,0,1,1,0,0,0,0, /* 6 */
194  1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1), /* 7 */
195  BITMASK32(1,0,1,1,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
196  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 9 */
197  BITMASK32(0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0, /* a */
198  1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0), /* b */
199  BITMASK32(1,1,0,0,0,0,1,0, 1,0,0,0,0,1,0,0, /* c */
200  0,0,0,0,1,1,0,0, 0,0,0,0,0,0,0,0), /* d */
201  BITMASK32(1,1,1,1,1,1,1,1, 0,0,0,1,0,0,0,0, /* e */
202  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0) /* f */
203 };
204 #define CHECK_DATA1(v) CHECK_TABLE(data1_t, v)
205 
206 /* CHECK_DATA2 */
207 const static unsigned int data2_t[] = {
208  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
209  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 0 */
210  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 1 */
211  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 2 */
212  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 3 */
213  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 4 */
214  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 5 */
215  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 6 */
216  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 7 */
217  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
218  0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0), /* 9 */
219  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* a */
220  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* b */
221  BITMASK32(0,0,1,0,0,0,0,0, 1,0,1,0,0,0,0,0, /* c */
222  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* d */
223  BITMASK32(0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0, /* e */
224  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0) /* f */
225 };
226 #define CHECK_DATA2(v) CHECK_TABLE(data2_t, v)
227 
228 /* CHECK_DATA66 */
229 const static unsigned int data66_t[] = {
230  /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
231  BITMASK32(0,0,0,0,0,1,0,0, 0,0,0,0,0,1,0,0, /* 0 */
232  0,0,0,0,0,1,0,0, 0,0,0,0,0,1,0,0), /* 1 */
233  BITMASK32(0,0,0,0,0,1,0,0, 0,0,0,0,0,1,0,0, /* 2 */
234  0,0,0,0,0,1,0,0, 0,0,0,0,0,1,0,0), /* 3 */
235  BITMASK32(0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 4 */
236  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 5 */
237  BITMASK32(0,0,0,0,0,0,0,0, 1,1,0,0,0,0,0,0, /* 6 */
238  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* 7 */
239  BITMASK32(0,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 8 */
240  0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0), /* 9 */
241  BITMASK32(0,0,0,0,0,0,0,0, 0,1,0,0,0,0,0,0, /* a */
242  0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1), /* b */
243  BITMASK32(0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,0, /* c */
244  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0), /* d */
245  BITMASK32(0,0,0,0,0,0,0,0, 1,1,1,0,0,0,0,0, /* e */
246  0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0) /* f */
247 };
248 #define CHECK_DATA66(v) CHECK_TABLE(data66_t, v)
249 
250 /* CHECK_MEM67 */
251 #define CHECK_MEM67(v) (((v)&0xfc)==0xa0)