]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/db_disasm.c
This commit was generated by cvs2svn to compensate for changes in r165009,
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / db_disasm.c
1 /*-
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * Instruction disassembler.
32  */
33 #include <sys/param.h>
34
35 #include <ddb/ddb.h>
36 #include <ddb/db_access.h>
37 #include <ddb/db_sym.h>
38
39 /*
40  * Size attributes
41  */
42 #define BYTE    0
43 #define WORD    1
44 #define LONG    2
45 #define QUAD    3
46 #define SNGL    4
47 #define DBLR    5
48 #define EXTR    6
49 #define SDEP    7
50 #define NONE    8
51
52 /*
53  * REX prefix and bits
54  */
55 #define REX_B   1
56 #define REX_X   2
57 #define REX_R   4
58 #define REX_W   8
59 #define REX     0x40
60
61 /*
62  * Addressing modes
63  */
64 #define E       1                       /* general effective address */
65 #define Eind    2                       /* indirect address (jump, call) */
66 #define Ew      3                       /* address, word size */
67 #define Eb      4                       /* address, byte size */
68 #define R       5                       /* register, in 'reg' field */
69 #define Rw      6                       /* word register, in 'reg' field */
70 #define Ri      7                       /* register in instruction */
71 #define S       8                       /* segment reg, in 'reg' field */
72 #define Si      9                       /* segment reg, in instruction */
73 #define A       10                      /* accumulator */
74 #define BX      11                      /* (bx) */
75 #define CL      12                      /* cl, for shifts */
76 #define DX      13                      /* dx, for IO */
77 #define SI      14                      /* si */
78 #define DI      15                      /* di */
79 #define CR      16                      /* control register */
80 #define DR      17                      /* debug register */
81 #define TR      18                      /* test register */
82 #define I       19                      /* immediate, unsigned */
83 #define Is      20                      /* immediate, signed */
84 #define Ib      21                      /* byte immediate, unsigned */
85 #define Ibs     22                      /* byte immediate, signed */
86 #define Iw      23                      /* word immediate, unsigned */
87 #define Ilq     24                      /* long/quad immediate, unsigned */
88 #define O       25                      /* direct address */
89 #define Db      26                      /* byte displacement from EIP */
90 #define Dl      27                      /* long displacement from EIP */
91 #define o1      28                      /* constant 1 */
92 #define o3      29                      /* constant 3 */
93 #define OS      30                      /* immediate offset/segment */
94 #define ST      31                      /* FP stack top */
95 #define STI     32                      /* FP stack */
96 #define X       33                      /* extended FP op */
97 #define XA      34                      /* for 'fstcw %ax' */
98 #define El      35                      /* address, long/quad size */
99 #define Ril     36                      /* long register in instruction */
100 #define Iba     37                      /* byte immediate, don't print if 0xa */
101 #define EL      38                      /* address, explicitly long size */
102
103 struct inst {
104         const char *    i_name;         /* name */
105         short   i_has_modrm;            /* has regmodrm byte */
106         short   i_size;                 /* operand size */
107         int     i_mode;                 /* addressing modes */
108         const void *    i_extra;        /* pointer to extra opcode table */
109 };
110
111 #define op1(x)          (x)
112 #define op2(x,y)        ((x)|((y)<<8))
113 #define op3(x,y,z)      ((x)|((y)<<8)|((z)<<16))
114
115 struct finst {
116         const char *    f_name;         /* name for memory instruction */
117         int     f_size;                 /* size for memory instruction */
118         int     f_rrmode;               /* mode for rr instruction */
119         const void *    f_rrname;       /* name for rr instruction
120                                            (or pointer to table) */
121 };
122
123 static const char * const db_Grp6[] = {
124         "sldt",
125         "str",
126         "lldt",
127         "ltr",
128         "verr",
129         "verw",
130         "",
131         ""
132 };
133
134 static const char * const db_Grp7[] = {
135         "sgdt",
136         "sidt",
137         "lgdt",
138         "lidt",
139         "smsw",
140         "",
141         "lmsw",
142         "invlpg"
143 };
144
145 static const char * const db_Grp8[] = {
146         "",
147         "",
148         "",
149         "",
150         "bt",
151         "bts",
152         "btr",
153         "btc"
154 };
155
156 static const char * const db_Grp9[] = {
157         "",
158         "cmpxchg8b",
159         "",
160         "",
161         "",
162         "",
163         "",
164         ""
165 };
166
167 static const struct inst db_inst_0f0x[] = {
168 /*00*/  { "",      TRUE,  NONE,  op1(Ew),     db_Grp6 },
169 /*01*/  { "",      TRUE,  NONE,  op1(Ew),     db_Grp7 },
170 /*02*/  { "lar",   TRUE,  LONG,  op2(E,R),    0 },
171 /*03*/  { "lsl",   TRUE,  LONG,  op2(E,R),    0 },
172 /*04*/  { "",      FALSE, NONE,  0,           0 },
173 /*05*/  { "",      FALSE, NONE,  0,           0 },
174 /*06*/  { "clts",  FALSE, NONE,  0,           0 },
175 /*07*/  { "",      FALSE, NONE,  0,           0 },
176
177 /*08*/  { "invd",  FALSE, NONE,  0,           0 },
178 /*09*/  { "wbinvd",FALSE, NONE,  0,           0 },
179 /*0a*/  { "",      FALSE, NONE,  0,           0 },
180 /*0b*/  { "",      FALSE, NONE,  0,           0 },
181 /*0c*/  { "",      FALSE, NONE,  0,           0 },
182 /*0d*/  { "",      FALSE, NONE,  0,           0 },
183 /*0e*/  { "",      FALSE, NONE,  0,           0 },
184 /*0f*/  { "",      FALSE, NONE,  0,           0 },
185 };
186
187 static const struct inst db_inst_0f2x[] = {
188 /*20*/  { "mov",   TRUE,  LONG,  op2(CR,El),  0 },
189 /*21*/  { "mov",   TRUE,  LONG,  op2(DR,El),  0 },
190 /*22*/  { "mov",   TRUE,  LONG,  op2(El,CR),  0 },
191 /*23*/  { "mov",   TRUE,  LONG,  op2(El,DR),  0 },
192 /*24*/  { "mov",   TRUE,  LONG,  op2(TR,El),  0 },
193 /*25*/  { "",      FALSE, NONE,  0,           0 },
194 /*26*/  { "mov",   TRUE,  LONG,  op2(El,TR),  0 },
195 /*27*/  { "",      FALSE, NONE,  0,           0 },
196
197 /*28*/  { "",      FALSE, NONE,  0,           0 },
198 /*29*/  { "",      FALSE, NONE,  0,           0 },
199 /*2a*/  { "",      FALSE, NONE,  0,           0 },
200 /*2b*/  { "",      FALSE, NONE,  0,           0 },
201 /*2c*/  { "",      FALSE, NONE,  0,           0 },
202 /*2d*/  { "",      FALSE, NONE,  0,           0 },
203 /*2e*/  { "",      FALSE, NONE,  0,           0 },
204 /*2f*/  { "",      FALSE, NONE,  0,           0 },
205 };
206
207 static const struct inst db_inst_0f3x[] = {
208 /*30*/  { "wrmsr", FALSE, NONE,  0,           0 },
209 /*31*/  { "rdtsc", FALSE, NONE,  0,           0 },
210 /*32*/  { "rdmsr", FALSE, NONE,  0,           0 },
211 /*33*/  { "rdpmc", FALSE, NONE,  0,           0 },
212 /*34*/  { "",      FALSE, NONE,  0,           0 },
213 /*35*/  { "",      FALSE, NONE,  0,           0 },
214 /*36*/  { "",      FALSE, NONE,  0,           0 },
215 /*37*/  { "",      FALSE, NONE,  0,           0 },
216
217 /*38*/  { "",      FALSE, NONE,  0,           0 },
218 /*39*/  { "",      FALSE, NONE,  0,           0 },
219 /*3a*/  { "",      FALSE, NONE,  0,           0 },
220 /*3b*/  { "",      FALSE, NONE,  0,           0 },
221 /*3c*/  { "",      FALSE, NONE,  0,           0 },
222 /*3d*/  { "",      FALSE, NONE,  0,           0 },
223 /*3e*/  { "",      FALSE, NONE,  0,           0 },
224 /*3f*/  { "",      FALSE, NONE,  0,           0 },
225 };
226
227 static const struct inst db_inst_0f4x[] = {
228 /*40*/  { "cmovo",  TRUE, NONE,  op2(E, R),   0 },
229 /*41*/  { "cmovno", TRUE, NONE,  op2(E, R),   0 },
230 /*42*/  { "cmovb",  TRUE, NONE,  op2(E, R),   0 },
231 /*43*/  { "cmovnb", TRUE, NONE,  op2(E, R),   0 },
232 /*44*/  { "cmovz",  TRUE, NONE,  op2(E, R),   0 },
233 /*45*/  { "cmovnz", TRUE, NONE,  op2(E, R),   0 },
234 /*46*/  { "cmovbe", TRUE, NONE,  op2(E, R),   0 },
235 /*47*/  { "cmovnbe",TRUE, NONE,  op2(E, R),   0 },
236
237 /*48*/  { "cmovs",  TRUE, NONE,  op2(E, R),   0 },
238 /*49*/  { "cmovns", TRUE, NONE,  op2(E, R),   0 },
239 /*4a*/  { "cmovp",  TRUE, NONE,  op2(E, R),   0 },
240 /*4b*/  { "cmovnp", TRUE, NONE,  op2(E, R),   0 },
241 /*4c*/  { "cmovl",  TRUE, NONE,  op2(E, R),   0 },
242 /*4d*/  { "cmovnl", TRUE, NONE,  op2(E, R),   0 },
243 /*4e*/  { "cmovle", TRUE, NONE,  op2(E, R),   0 },
244 /*4f*/  { "cmovnle",TRUE, NONE,  op2(E, R),   0 },
245 };
246
247 static const struct inst db_inst_0f8x[] = {
248 /*80*/  { "jo",    FALSE, NONE,  op1(Dl),     0 },
249 /*81*/  { "jno",   FALSE, NONE,  op1(Dl),     0 },
250 /*82*/  { "jb",    FALSE, NONE,  op1(Dl),     0 },
251 /*83*/  { "jnb",   FALSE, NONE,  op1(Dl),     0 },
252 /*84*/  { "jz",    FALSE, NONE,  op1(Dl),     0 },
253 /*85*/  { "jnz",   FALSE, NONE,  op1(Dl),     0 },
254 /*86*/  { "jbe",   FALSE, NONE,  op1(Dl),     0 },
255 /*87*/  { "jnbe",  FALSE, NONE,  op1(Dl),     0 },
256
257 /*88*/  { "js",    FALSE, NONE,  op1(Dl),     0 },
258 /*89*/  { "jns",   FALSE, NONE,  op1(Dl),     0 },
259 /*8a*/  { "jp",    FALSE, NONE,  op1(Dl),     0 },
260 /*8b*/  { "jnp",   FALSE, NONE,  op1(Dl),     0 },
261 /*8c*/  { "jl",    FALSE, NONE,  op1(Dl),     0 },
262 /*8d*/  { "jnl",   FALSE, NONE,  op1(Dl),     0 },
263 /*8e*/  { "jle",   FALSE, NONE,  op1(Dl),     0 },
264 /*8f*/  { "jnle",  FALSE, NONE,  op1(Dl),     0 },
265 };
266
267 static const struct inst db_inst_0f9x[] = {
268 /*90*/  { "seto",  TRUE,  NONE,  op1(Eb),     0 },
269 /*91*/  { "setno", TRUE,  NONE,  op1(Eb),     0 },
270 /*92*/  { "setb",  TRUE,  NONE,  op1(Eb),     0 },
271 /*93*/  { "setnb", TRUE,  NONE,  op1(Eb),     0 },
272 /*94*/  { "setz",  TRUE,  NONE,  op1(Eb),     0 },
273 /*95*/  { "setnz", TRUE,  NONE,  op1(Eb),     0 },
274 /*96*/  { "setbe", TRUE,  NONE,  op1(Eb),     0 },
275 /*97*/  { "setnbe",TRUE,  NONE,  op1(Eb),     0 },
276
277 /*98*/  { "sets",  TRUE,  NONE,  op1(Eb),     0 },
278 /*99*/  { "setns", TRUE,  NONE,  op1(Eb),     0 },
279 /*9a*/  { "setp",  TRUE,  NONE,  op1(Eb),     0 },
280 /*9b*/  { "setnp", TRUE,  NONE,  op1(Eb),     0 },
281 /*9c*/  { "setl",  TRUE,  NONE,  op1(Eb),     0 },
282 /*9d*/  { "setnl", TRUE,  NONE,  op1(Eb),     0 },
283 /*9e*/  { "setle", TRUE,  NONE,  op1(Eb),     0 },
284 /*9f*/  { "setnle",TRUE,  NONE,  op1(Eb),     0 },
285 };
286
287 static const struct inst db_inst_0fax[] = {
288 /*a0*/  { "push",  FALSE, NONE,  op1(Si),     0 },
289 /*a1*/  { "pop",   FALSE, NONE,  op1(Si),     0 },
290 /*a2*/  { "cpuid", FALSE, NONE,  0,           0 },
291 /*a3*/  { "bt",    TRUE,  LONG,  op2(R,E),    0 },
292 /*a4*/  { "shld",  TRUE,  LONG,  op3(Ib,R,E), 0 },
293 /*a5*/  { "shld",  TRUE,  LONG,  op3(CL,R,E), 0 },
294 /*a6*/  { "",      FALSE, NONE,  0,           0 },
295 /*a7*/  { "",      FALSE, NONE,  0,           0 },
296
297 /*a8*/  { "push",  FALSE, NONE,  op1(Si),     0 },
298 /*a9*/  { "pop",   FALSE, NONE,  op1(Si),     0 },
299 /*aa*/  { "rsm",   FALSE, NONE,  0,           0 },
300 /*ab*/  { "bts",   TRUE,  LONG,  op2(R,E),    0 },
301 /*ac*/  { "shrd",  TRUE,  LONG,  op3(Ib,R,E), 0 },
302 /*ad*/  { "shrd",  TRUE,  LONG,  op3(CL,R,E), 0 },
303 /*a6*/  { "",      FALSE, NONE,  0,           0 },
304 /*a7*/  { "imul",  TRUE,  LONG,  op2(E,R),    0 },
305 };
306
307 static const struct inst db_inst_0fbx[] = {
308 /*b0*/  { "cmpxchg",TRUE, BYTE,  op2(R, E),   0 },
309 /*b0*/  { "cmpxchg",TRUE, LONG,  op2(R, E),   0 },
310 /*b2*/  { "lss",   TRUE,  LONG,  op2(E, R),   0 },
311 /*b3*/  { "btr",   TRUE,  LONG,  op2(R, E),   0 },
312 /*b4*/  { "lfs",   TRUE,  LONG,  op2(E, R),   0 },
313 /*b5*/  { "lgs",   TRUE,  LONG,  op2(E, R),   0 },
314 /*b6*/  { "movzb", TRUE,  LONG,  op2(Eb, R),  0 },
315 /*b7*/  { "movzw", TRUE,  LONG,  op2(Ew, R),  0 },
316
317 /*b8*/  { "",      FALSE, NONE,  0,           0 },
318 /*b9*/  { "",      FALSE, NONE,  0,           0 },
319 /*ba*/  { "",      TRUE,  LONG,  op2(Ib, E),  db_Grp8 },
320 /*bb*/  { "btc",   TRUE,  LONG,  op2(R, E),   0 },
321 /*bc*/  { "bsf",   TRUE,  LONG,  op2(E, R),   0 },
322 /*bd*/  { "bsr",   TRUE,  LONG,  op2(E, R),   0 },
323 /*be*/  { "movsb", TRUE,  LONG,  op2(Eb, R),  0 },
324 /*bf*/  { "movsw", TRUE,  LONG,  op2(Ew, R),  0 },
325 };
326
327 static const struct inst db_inst_0fcx[] = {
328 /*c0*/  { "xadd",  TRUE,  BYTE,  op2(R, E),   0 },
329 /*c1*/  { "xadd",  TRUE,  LONG,  op2(R, E),   0 },
330 /*c2*/  { "",      FALSE, NONE,  0,           0 },
331 /*c3*/  { "",      FALSE, NONE,  0,           0 },
332 /*c4*/  { "",      FALSE, NONE,  0,           0 },
333 /*c5*/  { "",      FALSE, NONE,  0,           0 },
334 /*c6*/  { "",      FALSE, NONE,  0,           0 },
335 /*c7*/  { "",      TRUE,  NONE,  op1(E),      db_Grp9 },
336 /*c8*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
337 /*c9*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
338 /*ca*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
339 /*cb*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
340 /*cc*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
341 /*cd*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
342 /*ce*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
343 /*cf*/  { "bswap", FALSE, LONG,  op1(Ril),    0 },
344 };
345
346 static const struct inst * const db_inst_0f[] = {
347         db_inst_0f0x,
348         0,
349         db_inst_0f2x,
350         db_inst_0f3x,
351         db_inst_0f4x,
352         0,
353         0,
354         0,
355         db_inst_0f8x,
356         db_inst_0f9x,
357         db_inst_0fax,
358         db_inst_0fbx,
359         db_inst_0fcx,
360         0,
361         0,
362         0
363 };
364
365 static const char * const db_Esc92[] = {
366         "fnop", "",     "",     "",     "",     "",     "",     ""
367 };
368 static const char * const db_Esc94[] = {
369         "fchs", "fabs", "",     "",     "ftst", "fxam", "",     ""
370 };
371 static const char * const db_Esc95[] = {
372         "fld1", "fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz",""
373 };
374 static const char * const db_Esc96[] = {
375         "f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp",
376         "fincstp"
377 };
378 static const char * const db_Esc97[] = {
379         "fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"
380 };
381
382 static const char * const db_Esca5[] = {
383         "",     "fucompp","",   "",     "",     "",     "",     ""
384 };
385
386 static const char * const db_Escb4[] = {
387         "fneni","fndisi",       "fnclex","fninit","fsetpm",     "",     "",     ""
388 };
389
390 static const char * const db_Esce3[] = {
391         "",     "fcompp","",    "",     "",     "",     "",     ""
392 };
393
394 static const char * const db_Escf4[] = {
395         "fnstsw","",    "",     "",     "",     "",     "",     ""
396 };
397
398 static const struct finst db_Esc8[] = {
399 /*0*/   { "fadd",   SNGL,  op2(STI,ST), 0 },
400 /*1*/   { "fmul",   SNGL,  op2(STI,ST), 0 },
401 /*2*/   { "fcom",   SNGL,  op2(STI,ST), 0 },
402 /*3*/   { "fcomp",  SNGL,  op2(STI,ST), 0 },
403 /*4*/   { "fsub",   SNGL,  op2(STI,ST), 0 },
404 /*5*/   { "fsubr",  SNGL,  op2(STI,ST), 0 },
405 /*6*/   { "fdiv",   SNGL,  op2(STI,ST), 0 },
406 /*7*/   { "fdivr",  SNGL,  op2(STI,ST), 0 },
407 };
408
409 static const struct finst db_Esc9[] = {
410 /*0*/   { "fld",    SNGL,  op1(STI),    0 },
411 /*1*/   { "",       NONE,  op1(STI),    "fxch" },
412 /*2*/   { "fst",    SNGL,  op1(X),      db_Esc92 },
413 /*3*/   { "fstp",   SNGL,  0,           0 },
414 /*4*/   { "fldenv", NONE,  op1(X),      db_Esc94 },
415 /*5*/   { "fldcw",  NONE,  op1(X),      db_Esc95 },
416 /*6*/   { "fnstenv",NONE,  op1(X),      db_Esc96 },
417 /*7*/   { "fnstcw", NONE,  op1(X),      db_Esc97 },
418 };
419
420 static const struct finst db_Esca[] = {
421 /*0*/   { "fiadd",  LONG,  0,           0 },
422 /*1*/   { "fimul",  LONG,  0,           0 },
423 /*2*/   { "ficom",  LONG,  0,           0 },
424 /*3*/   { "ficomp", LONG,  0,           0 },
425 /*4*/   { "fisub",  LONG,  0,           0 },
426 /*5*/   { "fisubr", LONG,  op1(X),      db_Esca5 },
427 /*6*/   { "fidiv",  LONG,  0,           0 },
428 /*7*/   { "fidivr", LONG,  0,           0 }
429 };
430
431 static const struct finst db_Escb[] = {
432 /*0*/   { "fild",   LONG,  0,           0 },
433 /*1*/   { "",       NONE,  0,           0 },
434 /*2*/   { "fist",   LONG,  0,           0 },
435 /*3*/   { "fistp",  LONG,  0,           0 },
436 /*4*/   { "",       WORD,  op1(X),      db_Escb4 },
437 /*5*/   { "fld",    EXTR,  0,           0 },
438 /*6*/   { "",       WORD,  0,           0 },
439 /*7*/   { "fstp",   EXTR,  0,           0 },
440 };
441
442 static const struct finst db_Escc[] = {
443 /*0*/   { "fadd",   DBLR,  op2(ST,STI), 0 },
444 /*1*/   { "fmul",   DBLR,  op2(ST,STI), 0 },
445 /*2*/   { "fcom",   DBLR,  0,           0 },
446 /*3*/   { "fcomp",  DBLR,  0,           0 },
447 /*4*/   { "fsub",   DBLR,  op2(ST,STI), "fsubr" },
448 /*5*/   { "fsubr",  DBLR,  op2(ST,STI), "fsub" },
449 /*6*/   { "fdiv",   DBLR,  op2(ST,STI), "fdivr" },
450 /*7*/   { "fdivr",  DBLR,  op2(ST,STI), "fdiv" },
451 };
452
453 static const struct finst db_Escd[] = {
454 /*0*/   { "fld",    DBLR,  op1(STI),    "ffree" },
455 /*1*/   { "",       NONE,  0,           0 },
456 /*2*/   { "fst",    DBLR,  op1(STI),    0 },
457 /*3*/   { "fstp",   DBLR,  op1(STI),    0 },
458 /*4*/   { "frstor", NONE,  op1(STI),    "fucom" },
459 /*5*/   { "",       NONE,  op1(STI),    "fucomp" },
460 /*6*/   { "fnsave", NONE,  0,           0 },
461 /*7*/   { "fnstsw", NONE,  0,           0 },
462 };
463
464 static const struct finst db_Esce[] = {
465 /*0*/   { "fiadd",  WORD,  op2(ST,STI), "faddp" },
466 /*1*/   { "fimul",  WORD,  op2(ST,STI), "fmulp" },
467 /*2*/   { "ficom",  WORD,  0,           0 },
468 /*3*/   { "ficomp", WORD,  op1(X),      db_Esce3 },
469 /*4*/   { "fisub",  WORD,  op2(ST,STI), "fsubrp" },
470 /*5*/   { "fisubr", WORD,  op2(ST,STI), "fsubp" },
471 /*6*/   { "fidiv",  WORD,  op2(ST,STI), "fdivrp" },
472 /*7*/   { "fidivr", WORD,  op2(ST,STI), "fdivp" },
473 };
474
475 static const struct finst db_Escf[] = {
476 /*0*/   { "fild",   WORD,  0,           0 },
477 /*1*/   { "",       NONE,  0,           0 },
478 /*2*/   { "fist",   WORD,  0,           0 },
479 /*3*/   { "fistp",  WORD,  0,           0 },
480 /*4*/   { "fbld",   NONE,  op1(XA),     db_Escf4 },
481 /*5*/   { "fild",   QUAD,  0,           0 },
482 /*6*/   { "fbstp",  NONE,  0,           0 },
483 /*7*/   { "fistp",  QUAD,  0,           0 },
484 };
485
486 static const struct finst * const db_Esc_inst[] = {
487         db_Esc8, db_Esc9, db_Esca, db_Escb,
488         db_Escc, db_Escd, db_Esce, db_Escf
489 };
490
491 static const char * const db_Grp1[] = {
492         "add",
493         "or",
494         "adc",
495         "sbb",
496         "and",
497         "sub",
498         "xor",
499         "cmp"
500 };
501
502 static const char * const db_Grp2[] = {
503         "rol",
504         "ror",
505         "rcl",
506         "rcr",
507         "shl",
508         "shr",
509         "shl",
510         "sar"
511 };
512
513 static const struct inst db_Grp3[] = {
514         { "test",  TRUE, NONE, op2(I,E), 0 },
515         { "test",  TRUE, NONE, op2(I,E), 0 },
516         { "not",   TRUE, NONE, op1(E),   0 },
517         { "neg",   TRUE, NONE, op1(E),   0 },
518         { "mul",   TRUE, NONE, op2(E,A), 0 },
519         { "imul",  TRUE, NONE, op2(E,A), 0 },
520         { "div",   TRUE, NONE, op2(E,A), 0 },
521         { "idiv",  TRUE, NONE, op2(E,A), 0 },
522 };
523
524 static const struct inst db_Grp4[] = {
525         { "inc",   TRUE, BYTE, op1(E),   0 },
526         { "dec",   TRUE, BYTE, op1(E),   0 },
527         { "",      TRUE, NONE, 0,        0 },
528         { "",      TRUE, NONE, 0,        0 },
529         { "",      TRUE, NONE, 0,        0 },
530         { "",      TRUE, NONE, 0,        0 },
531         { "",      TRUE, NONE, 0,        0 },
532         { "",      TRUE, NONE, 0,        0 }
533 };
534
535 static const struct inst db_Grp5[] = {
536         { "inc",   TRUE, LONG, op1(E),   0 },
537         { "dec",   TRUE, LONG, op1(E),   0 },
538         { "call",  TRUE, LONG, op1(Eind),0 },
539         { "lcall", TRUE, LONG, op1(Eind),0 },
540         { "jmp",   TRUE, LONG, op1(Eind),0 },
541         { "ljmp",  TRUE, LONG, op1(Eind),0 },
542         { "push",  TRUE, LONG, op1(E),   0 },
543         { "",      TRUE, NONE, 0,        0 }
544 };
545
546 static const struct inst db_inst_table[256] = {
547 /*00*/  { "add",   TRUE,  BYTE,  op2(R, E),  0 },
548 /*01*/  { "add",   TRUE,  LONG,  op2(R, E),  0 },
549 /*02*/  { "add",   TRUE,  BYTE,  op2(E, R),  0 },
550 /*03*/  { "add",   TRUE,  LONG,  op2(E, R),  0 },
551 /*04*/  { "add",   FALSE, BYTE,  op2(I, A),  0 },
552 /*05*/  { "add",   FALSE, LONG,  op2(Is, A), 0 },
553 /*06*/  { "push",  FALSE, NONE,  op1(Si),    0 },
554 /*07*/  { "pop",   FALSE, NONE,  op1(Si),    0 },
555
556 /*08*/  { "or",    TRUE,  BYTE,  op2(R, E),  0 },
557 /*09*/  { "or",    TRUE,  LONG,  op2(R, E),  0 },
558 /*0a*/  { "or",    TRUE,  BYTE,  op2(E, R),  0 },
559 /*0b*/  { "or",    TRUE,  LONG,  op2(E, R),  0 },
560 /*0c*/  { "or",    FALSE, BYTE,  op2(I, A),  0 },
561 /*0d*/  { "or",    FALSE, LONG,  op2(I, A),  0 },
562 /*0e*/  { "push",  FALSE, NONE,  op1(Si),    0 },
563 /*0f*/  { "",      FALSE, NONE,  0,          0 },
564
565 /*10*/  { "adc",   TRUE,  BYTE,  op2(R, E),  0 },
566 /*11*/  { "adc",   TRUE,  LONG,  op2(R, E),  0 },
567 /*12*/  { "adc",   TRUE,  BYTE,  op2(E, R),  0 },
568 /*13*/  { "adc",   TRUE,  LONG,  op2(E, R),  0 },
569 /*14*/  { "adc",   FALSE, BYTE,  op2(I, A),  0 },
570 /*15*/  { "adc",   FALSE, LONG,  op2(Is, A), 0 },
571 /*16*/  { "push",  FALSE, NONE,  op1(Si),    0 },
572 /*17*/  { "pop",   FALSE, NONE,  op1(Si),    0 },
573
574 /*18*/  { "sbb",   TRUE,  BYTE,  op2(R, E),  0 },
575 /*19*/  { "sbb",   TRUE,  LONG,  op2(R, E),  0 },
576 /*1a*/  { "sbb",   TRUE,  BYTE,  op2(E, R),  0 },
577 /*1b*/  { "sbb",   TRUE,  LONG,  op2(E, R),  0 },
578 /*1c*/  { "sbb",   FALSE, BYTE,  op2(I, A),  0 },
579 /*1d*/  { "sbb",   FALSE, LONG,  op2(Is, A), 0 },
580 /*1e*/  { "push",  FALSE, NONE,  op1(Si),    0 },
581 /*1f*/  { "pop",   FALSE, NONE,  op1(Si),    0 },
582
583 /*20*/  { "and",   TRUE,  BYTE,  op2(R, E),  0 },
584 /*21*/  { "and",   TRUE,  LONG,  op2(R, E),  0 },
585 /*22*/  { "and",   TRUE,  BYTE,  op2(E, R),  0 },
586 /*23*/  { "and",   TRUE,  LONG,  op2(E, R),  0 },
587 /*24*/  { "and",   FALSE, BYTE,  op2(I, A),  0 },
588 /*25*/  { "and",   FALSE, LONG,  op2(I, A),  0 },
589 /*26*/  { "",      FALSE, NONE,  0,          0 },
590 /*27*/  { "daa",   FALSE, NONE,  0,          0 },
591
592 /*28*/  { "sub",   TRUE,  BYTE,  op2(R, E),  0 },
593 /*29*/  { "sub",   TRUE,  LONG,  op2(R, E),  0 },
594 /*2a*/  { "sub",   TRUE,  BYTE,  op2(E, R),  0 },
595 /*2b*/  { "sub",   TRUE,  LONG,  op2(E, R),  0 },
596 /*2c*/  { "sub",   FALSE, BYTE,  op2(I, A),  0 },
597 /*2d*/  { "sub",   FALSE, LONG,  op2(Is, A), 0 },
598 /*2e*/  { "",      FALSE, NONE,  0,          0 },
599 /*2f*/  { "das",   FALSE, NONE,  0,          0 },
600
601 /*30*/  { "xor",   TRUE,  BYTE,  op2(R, E),  0 },
602 /*31*/  { "xor",   TRUE,  LONG,  op2(R, E),  0 },
603 /*32*/  { "xor",   TRUE,  BYTE,  op2(E, R),  0 },
604 /*33*/  { "xor",   TRUE,  LONG,  op2(E, R),  0 },
605 /*34*/  { "xor",   FALSE, BYTE,  op2(I, A),  0 },
606 /*35*/  { "xor",   FALSE, LONG,  op2(I, A),  0 },
607 /*36*/  { "",      FALSE, NONE,  0,          0 },
608 /*37*/  { "aaa",   FALSE, NONE,  0,          0 },
609
610 /*38*/  { "cmp",   TRUE,  BYTE,  op2(R, E),  0 },
611 /*39*/  { "cmp",   TRUE,  LONG,  op2(R, E),  0 },
612 /*3a*/  { "cmp",   TRUE,  BYTE,  op2(E, R),  0 },
613 /*3b*/  { "cmp",   TRUE,  LONG,  op2(E, R),  0 },
614 /*3c*/  { "cmp",   FALSE, BYTE,  op2(I, A),  0 },
615 /*3d*/  { "cmp",   FALSE, LONG,  op2(Is, A), 0 },
616 /*3e*/  { "",      FALSE, NONE,  0,          0 },
617 /*3f*/  { "aas",   FALSE, NONE,  0,          0 },
618
619 /*40*/  { "rex",   FALSE, NONE,  0,          0 },
620 /*41*/  { "rex.b", FALSE, NONE,  0,          0 },
621 /*42*/  { "rex.x", FALSE, NONE,  0,          0 },
622 /*43*/  { "rex.xb", FALSE, NONE, 0,          0 },
623 /*44*/  { "rex.r", FALSE, NONE,  0,          0 },
624 /*45*/  { "rex.rb", FALSE, NONE, 0,          0 },
625 /*46*/  { "rex.rx", FALSE, NONE, 0,          0 },
626 /*47*/  { "rex.rxb", FALSE, NONE, 0,         0 },
627
628 /*48*/  { "rex.w", FALSE, NONE,  0,          0 },
629 /*49*/  { "rex.wb", FALSE, NONE, 0,          0 },
630 /*4a*/  { "rex.wx", FALSE, NONE, 0,          0 },
631 /*4b*/  { "rex.wxb", FALSE, NONE, 0,         0 },
632 /*4c*/  { "rex.wr", FALSE, NONE, 0,          0 },
633 /*4d*/  { "rex.wrb", FALSE, NONE, 0,         0 },
634 /*4e*/  { "rex.wrx", FALSE, NONE, 0,         0 },
635 /*4f*/  { "rex.wrxb", FALSE, NONE, 0,        0 },
636
637 /*50*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
638 /*51*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
639 /*52*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
640 /*53*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
641 /*54*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
642 /*55*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
643 /*56*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
644 /*57*/  { "push",  FALSE, LONG,  op1(Ri),    0 },
645
646 /*58*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
647 /*59*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
648 /*5a*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
649 /*5b*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
650 /*5c*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
651 /*5d*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
652 /*5e*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
653 /*5f*/  { "pop",   FALSE, LONG,  op1(Ri),    0 },
654
655 /*60*/  { "pusha", FALSE, LONG,  0,          0 },
656 /*61*/  { "popa",  FALSE, LONG,  0,          0 },
657 /*62*/  { "bound", TRUE,  LONG,  op2(E, R),  0 },
658 /*63*/  { "movslq",  TRUE,  NONE,  op2(EL,R), 0 },
659
660 /*64*/  { "",      FALSE, NONE,  0,          0 },
661 /*65*/  { "",      FALSE, NONE,  0,          0 },
662 /*66*/  { "",      FALSE, NONE,  0,          0 },
663 /*67*/  { "",      FALSE, NONE,  0,          0 },
664
665 /*68*/  { "push",  FALSE, LONG,  op1(I),     0 },
666 /*69*/  { "imul",  TRUE,  LONG,  op3(I,E,R), 0 },
667 /*6a*/  { "push",  FALSE, LONG,  op1(Ibs),   0 },
668 /*6b*/  { "imul",  TRUE,  LONG,  op3(Ibs,E,R),0 },
669 /*6c*/  { "ins",   FALSE, BYTE,  op2(DX, DI), 0 },
670 /*6d*/  { "ins",   FALSE, LONG,  op2(DX, DI), 0 },
671 /*6e*/  { "outs",  FALSE, BYTE,  op2(SI, DX), 0 },
672 /*6f*/  { "outs",  FALSE, LONG,  op2(SI, DX), 0 },
673
674 /*70*/  { "jo",    FALSE, NONE,  op1(Db),     0 },
675 /*71*/  { "jno",   FALSE, NONE,  op1(Db),     0 },
676 /*72*/  { "jb",    FALSE, NONE,  op1(Db),     0 },
677 /*73*/  { "jnb",   FALSE, NONE,  op1(Db),     0 },
678 /*74*/  { "jz",    FALSE, NONE,  op1(Db),     0 },
679 /*75*/  { "jnz",   FALSE, NONE,  op1(Db),     0 },
680 /*76*/  { "jbe",   FALSE, NONE,  op1(Db),     0 },
681 /*77*/  { "jnbe",  FALSE, NONE,  op1(Db),     0 },
682
683 /*78*/  { "js",    FALSE, NONE,  op1(Db),     0 },
684 /*79*/  { "jns",   FALSE, NONE,  op1(Db),     0 },
685 /*7a*/  { "jp",    FALSE, NONE,  op1(Db),     0 },
686 /*7b*/  { "jnp",   FALSE, NONE,  op1(Db),     0 },
687 /*7c*/  { "jl",    FALSE, NONE,  op1(Db),     0 },
688 /*7d*/  { "jnl",   FALSE, NONE,  op1(Db),     0 },
689 /*7e*/  { "jle",   FALSE, NONE,  op1(Db),     0 },
690 /*7f*/  { "jnle",  FALSE, NONE,  op1(Db),     0 },
691
692 /*80*/  { "",      TRUE,  BYTE,  op2(I, E),   db_Grp1 },
693 /*81*/  { "",      TRUE,  LONG,  op2(I, E),   db_Grp1 },
694 /*82*/  { "",      TRUE,  BYTE,  op2(I, E),   db_Grp1 },
695 /*83*/  { "",      TRUE,  LONG,  op2(Ibs,E),  db_Grp1 },
696 /*84*/  { "test",  TRUE,  BYTE,  op2(R, E),   0 },
697 /*85*/  { "test",  TRUE,  LONG,  op2(R, E),   0 },
698 /*86*/  { "xchg",  TRUE,  BYTE,  op2(R, E),   0 },
699 /*87*/  { "xchg",  TRUE,  LONG,  op2(R, E),   0 },
700
701 /*88*/  { "mov",   TRUE,  BYTE,  op2(R, E),   0 },
702 /*89*/  { "mov",   TRUE,  LONG,  op2(R, E),   0 },
703 /*8a*/  { "mov",   TRUE,  BYTE,  op2(E, R),   0 },
704 /*8b*/  { "mov",   TRUE,  LONG,  op2(E, R),   0 },
705 /*8c*/  { "mov",   TRUE,  NONE,  op2(S, Ew),  0 },
706 /*8d*/  { "lea",   TRUE,  LONG,  op2(E, R),   0 },
707 /*8e*/  { "mov",   TRUE,  NONE,  op2(Ew, S),  0 },
708 /*8f*/  { "pop",   TRUE,  LONG,  op1(E),      0 },
709
710 /*90*/  { "nop",   FALSE, NONE,  0,           0 },
711 /*91*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
712 /*92*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
713 /*93*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
714 /*94*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
715 /*95*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
716 /*96*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
717 /*97*/  { "xchg",  FALSE, LONG,  op2(A, Ri),  0 },
718
719 /*98*/  { "cbw",   FALSE, SDEP,  0,           "cwde" }, /* cbw/cwde */
720 /*99*/  { "cwd",   FALSE, SDEP,  0,           "cdq"  }, /* cwd/cdq */
721 /*9a*/  { "lcall", FALSE, NONE,  op1(OS),     0 },
722 /*9b*/  { "wait",  FALSE, NONE,  0,           0 },
723 /*9c*/  { "pushf", FALSE, LONG,  0,           0 },
724 /*9d*/  { "popf",  FALSE, LONG,  0,           0 },
725 /*9e*/  { "sahf",  FALSE, NONE,  0,           0 },
726 /*9f*/  { "lahf",  FALSE, NONE,  0,           0 },
727
728 /*a0*/  { "mov",   FALSE, BYTE,  op2(O, A),   0 },
729 /*a1*/  { "mov",   FALSE, LONG,  op2(O, A),   0 },
730 /*a2*/  { "mov",   FALSE, BYTE,  op2(A, O),   0 },
731 /*a3*/  { "mov",   FALSE, LONG,  op2(A, O),   0 },
732 /*a4*/  { "movs",  FALSE, BYTE,  op2(SI,DI),  0 },
733 /*a5*/  { "movs",  FALSE, LONG,  op2(SI,DI),  0 },
734 /*a6*/  { "cmps",  FALSE, BYTE,  op2(SI,DI),  0 },
735 /*a7*/  { "cmps",  FALSE, LONG,  op2(SI,DI),  0 },
736
737 /*a8*/  { "test",  FALSE, BYTE,  op2(I, A),   0 },
738 /*a9*/  { "test",  FALSE, LONG,  op2(I, A),   0 },
739 /*aa*/  { "stos",  FALSE, BYTE,  op1(DI),     0 },
740 /*ab*/  { "stos",  FALSE, LONG,  op1(DI),     0 },
741 /*ac*/  { "lods",  FALSE, BYTE,  op1(SI),     0 },
742 /*ad*/  { "lods",  FALSE, LONG,  op1(SI),     0 },
743 /*ae*/  { "scas",  FALSE, BYTE,  op1(SI),     0 },
744 /*af*/  { "scas",  FALSE, LONG,  op1(SI),     0 },
745
746 /*b0*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
747 /*b1*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
748 /*b2*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
749 /*b3*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
750 /*b4*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
751 /*b5*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
752 /*b6*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
753 /*b7*/  { "mov",   FALSE, BYTE,  op2(I, Ri),  0 },
754
755 /*b8*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
756 /*b9*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
757 /*ba*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
758 /*bb*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
759 /*bc*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
760 /*bd*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
761 /*be*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
762 /*bf*/  { "mov",   FALSE, LONG,  op2(Ilq, Ri),  0 },
763
764 /*c0*/  { "",      TRUE,  BYTE,  op2(Ib, E),  db_Grp2 },
765 /*c1*/  { "",      TRUE,  LONG,  op2(Ib, E),  db_Grp2 },
766 /*c2*/  { "ret",   FALSE, NONE,  op1(Iw),     0 },
767 /*c3*/  { "ret",   FALSE, NONE,  0,           0 },
768 /*c4*/  { "les",   TRUE,  LONG,  op2(E, R),   0 },
769 /*c5*/  { "lds",   TRUE,  LONG,  op2(E, R),   0 },
770 /*c6*/  { "mov",   TRUE,  BYTE,  op2(I, E),   0 },
771 /*c7*/  { "mov",   TRUE,  LONG,  op2(I, E),   0 },
772
773 /*c8*/  { "enter", FALSE, NONE,  op2(Iw, Ib), 0 },
774 /*c9*/  { "leave", FALSE, NONE,  0,           0 },
775 /*ca*/  { "lret",  FALSE, NONE,  op1(Iw),     0 },
776 /*cb*/  { "lret",  FALSE, NONE,  0,           0 },
777 /*cc*/  { "int",   FALSE, NONE,  op1(o3),     0 },
778 /*cd*/  { "int",   FALSE, NONE,  op1(Ib),     0 },
779 /*ce*/  { "into",  FALSE, NONE,  0,           0 },
780 /*cf*/  { "iret",  FALSE, NONE,  0,           0 },
781
782 /*d0*/  { "",      TRUE,  BYTE,  op2(o1, E),  db_Grp2 },
783 /*d1*/  { "",      TRUE,  LONG,  op2(o1, E),  db_Grp2 },
784 /*d2*/  { "",      TRUE,  BYTE,  op2(CL, E),  db_Grp2 },
785 /*d3*/  { "",      TRUE,  LONG,  op2(CL, E),  db_Grp2 },
786 /*d4*/  { "aam",   FALSE, NONE,  op1(Iba),    0 },
787 /*d5*/  { "aad",   FALSE, NONE,  op1(Iba),    0 },
788 /*d6*/  { ".byte\t0xd6", FALSE, NONE, 0,      0 },
789 /*d7*/  { "xlat",  FALSE, BYTE,  op1(BX),     0 },
790
791 /*d8*/  { "",      TRUE,  NONE,  0,           db_Esc8 },
792 /*d9*/  { "",      TRUE,  NONE,  0,           db_Esc9 },
793 /*da*/  { "",      TRUE,  NONE,  0,           db_Esca },
794 /*db*/  { "",      TRUE,  NONE,  0,           db_Escb },
795 /*dc*/  { "",      TRUE,  NONE,  0,           db_Escc },
796 /*dd*/  { "",      TRUE,  NONE,  0,           db_Escd },
797 /*de*/  { "",      TRUE,  NONE,  0,           db_Esce },
798 /*df*/  { "",      TRUE,  NONE,  0,           db_Escf },
799
800 /*e0*/  { "loopne",FALSE, NONE,  op1(Db),     0 },
801 /*e1*/  { "loope", FALSE, NONE,  op1(Db),     0 },
802 /*e2*/  { "loop",  FALSE, NONE,  op1(Db),     0 },
803 /*e3*/  { "jcxz",  FALSE, SDEP,  op1(Db),     "jecxz" },
804 /*e4*/  { "in",    FALSE, BYTE,  op2(Ib, A),  0 },
805 /*e5*/  { "in",    FALSE, LONG,  op2(Ib, A) , 0 },
806 /*e6*/  { "out",   FALSE, BYTE,  op2(A, Ib),  0 },
807 /*e7*/  { "out",   FALSE, LONG,  op2(A, Ib) , 0 },
808
809 /*e8*/  { "call",  FALSE, NONE,  op1(Dl),     0 },
810 /*e9*/  { "jmp",   FALSE, NONE,  op1(Dl),     0 },
811 /*ea*/  { "ljmp",  FALSE, NONE,  op1(OS),     0 },
812 /*eb*/  { "jmp",   FALSE, NONE,  op1(Db),     0 },
813 /*ec*/  { "in",    FALSE, BYTE,  op2(DX, A),  0 },
814 /*ed*/  { "in",    FALSE, LONG,  op2(DX, A) , 0 },
815 /*ee*/  { "out",   FALSE, BYTE,  op2(A, DX),  0 },
816 /*ef*/  { "out",   FALSE, LONG,  op2(A, DX) , 0 },
817
818 /*f0*/  { "",      FALSE, NONE,  0,          0 },
819 /*f1*/  { ".byte\t0xf1", FALSE, NONE, 0,     0 },
820 /*f2*/  { "",      FALSE, NONE,  0,          0 },
821 /*f3*/  { "",      FALSE, NONE,  0,          0 },
822 /*f4*/  { "hlt",   FALSE, NONE,  0,          0 },
823 /*f5*/  { "cmc",   FALSE, NONE,  0,          0 },
824 /*f6*/  { "",      TRUE,  BYTE,  0,          db_Grp3 },
825 /*f7*/  { "",      TRUE,  LONG,  0,          db_Grp3 },
826
827 /*f8*/  { "clc",   FALSE, NONE,  0,          0 },
828 /*f9*/  { "stc",   FALSE, NONE,  0,          0 },
829 /*fa*/  { "cli",   FALSE, NONE,  0,          0 },
830 /*fb*/  { "sti",   FALSE, NONE,  0,          0 },
831 /*fc*/  { "cld",   FALSE, NONE,  0,          0 },
832 /*fd*/  { "std",   FALSE, NONE,  0,          0 },
833 /*fe*/  { "",      TRUE,  NONE,  0,          db_Grp4 },
834 /*ff*/  { "",      TRUE,  NONE,  0,          db_Grp5 },
835 };
836
837 static const struct inst db_bad_inst =
838         { "???",   FALSE, NONE,  0,           0 }
839 ;
840
841 #define f_mod(rex, byte)        ((byte)>>6)
842 #define f_reg(rex, byte)        ((((byte)>>3)&0x7) | (rex & REX_R ? 0x8 : 0x0))
843 #define f_rm(rex, byte)         (((byte)&0x7) | (rex & REX_B ? 0x8 : 0x0))
844
845 #define sib_ss(rex, byte)       ((byte)>>6)
846 #define sib_index(rex, byte)    ((((byte)>>3)&0x7) | (rex & REX_X ? 0x8 : 0x0))
847 #define sib_base(rex, byte)     (((byte)&0x7) | (rex & REX_B ? 0x8 : 0x0))
848
849 struct i_addr {
850         int             is_reg; /* if reg, reg number is in 'disp' */
851         int             disp;
852         const char *    base;
853         const char *    index;
854         int             ss;
855 };
856
857 static const char * const db_reg[2][4][16] = {
858
859         {{"%al",  "%cl",  "%dl",  "%bl",  "%ah",  "%ch",  "%dh",  "%bh",
860           "%r8b", "%r9b", "%r10b", "%r11b", "%r12b", "%r13b", "%r14b", "%r15b" },
861         { "%ax",  "%cx",  "%dx",  "%bx",  "%sp",  "%bp",  "%si",  "%di",
862           "%r8w", "%r9w", "%r10w", "%r11w", "%r12w", "%r13w", "%r14w", "%r15w" },
863         { "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi",
864           "%r8d", "%r9d", "%r10d", "%r11d", "%r12d", "%r13d", "%r14d", "%r15d" },
865         { "%rax", "%rcx", "%rdx", "%rbx", "%rsp", "%rbp", "%rsi", "%rdi",
866           "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15" }},
867
868         {{"%al",  "%cl",  "%dl",  "%bl",  "%spl",  "%bpl",  "%sil",  "%dil",
869           "%r8b", "%r9b", "%r10b", "%r11b", "%r12b", "%r13b", "%r14b", "%r15b" },
870         { "%ax",  "%cx",  "%dx",  "%bx",  "%sp",  "%bp",  "%si",  "%di",
871           "%r8w", "%r9w", "%r10w", "%r11w", "%r12w", "%r13w", "%r14w", "%r15w" },
872         { "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi",
873           "%r8d", "%r9d", "%r10d", "%r11d", "%r12d", "%r13d", "%r14d", "%r15d" },
874         { "%rax", "%rcx", "%rdx", "%rbx", "%rsp", "%rbp", "%rsi", "%rdi",
875           "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15" }}
876 };
877
878 static const char * const db_seg_reg[8] = {
879         "%es", "%cs", "%ss", "%ds", "%fs", "%gs", "", ""
880 };
881
882 /*
883  * lengths for size attributes
884  */
885 static const int db_lengths[] = {
886         1,      /* BYTE */
887         2,      /* WORD */
888         4,      /* LONG */
889         8,      /* QUAD */
890         4,      /* SNGL */
891         8,      /* DBLR */
892         10,     /* EXTR */
893 };
894
895 #define get_value_inc(result, loc, size, is_signed) \
896         result = db_get_value((loc), (size), (is_signed)); \
897         (loc) += (size);
898
899 static db_addr_t
900                 db_disasm_esc(db_addr_t loc, int inst, int rex, int short_addr,
901                     int size, const char *seg);
902 static void     db_print_address(const char *seg, int size, int rex,
903                     struct i_addr *addrp);
904 static db_addr_t
905                 db_read_address(db_addr_t loc, int short_addr, int rex, int regmodrm,
906                     struct i_addr *addrp);
907
908 /*
909  * Read address at location and return updated location.
910  */
911 static db_addr_t
912 db_read_address(loc, short_addr, rex, regmodrm, addrp)
913         db_addr_t       loc;
914         int             short_addr;
915         int             rex;
916         int             regmodrm;
917         struct i_addr * addrp;          /* out */
918 {
919         int             mod, rm, sib, index, disp, size, have_sib;
920
921         mod = f_mod(rex, regmodrm);
922         rm  = f_rm(rex, regmodrm);
923
924         if (mod == 3) {
925             addrp->is_reg = TRUE;
926             addrp->disp = rm;
927             return (loc);
928         }
929         addrp->is_reg = FALSE;
930         addrp->index = 0;
931
932         if (short_addr)
933             size = LONG;
934         else
935             size = QUAD;
936
937         if ((rm & 0x7) == 4) {
938             get_value_inc(sib, loc, 1, FALSE);
939             rm = sib_base(rex, sib);
940             index = sib_index(rex, sib);
941             if (index != 4)
942                 addrp->index = db_reg[1][size][index];
943             addrp->ss = sib_ss(rex, sib);
944             have_sib = 1;
945         } else
946             have_sib = 0;
947
948         switch (mod) {
949             case 0:
950                 if (rm == 5) {
951                     get_value_inc(addrp->disp, loc, 4, FALSE);
952                     if (have_sib)
953                         addrp->base = 0;
954                     else if (short_addr)
955                         addrp->base = "%eip";
956                     else
957                         addrp->base = "%rip";
958                 } else {
959                     addrp->disp = 0;
960                     addrp->base = db_reg[1][size][rm];
961                 }
962                 break;
963
964             case 1:
965                 get_value_inc(disp, loc, 1, TRUE);
966                 addrp->disp = disp;
967                 addrp->base = db_reg[1][size][rm];
968                 break;
969
970             case 2:
971                 get_value_inc(disp, loc, 4, FALSE);
972                 addrp->disp = disp;
973                 addrp->base = db_reg[1][size][rm];
974                 break;
975         }
976         return (loc);
977 }
978
979 static void
980 db_print_address(seg, size, rex, addrp)
981         const char *    seg;
982         int             size;
983         int             rex;
984         struct i_addr * addrp;
985 {
986         if (addrp->is_reg) {
987             db_printf("%s", db_reg[rex != 0 ? 1 : 0][(size == LONG && (rex & REX_W)) ? QUAD : size][addrp->disp]);
988             return;
989         }
990
991         if (seg) {
992             db_printf("%s:", seg);
993         }
994
995         if (addrp->disp != 0 || (addrp->base == 0 && addrp->index == 0))
996                 db_printsym((db_addr_t)addrp->disp, DB_STGY_ANY);
997         if (addrp->base != 0 || addrp->index != 0) {
998             db_printf("(");
999             if (addrp->base)
1000                 db_printf("%s", addrp->base);
1001             if (addrp->index)
1002                 db_printf(",%s,%d", addrp->index, 1<<addrp->ss);
1003             db_printf(")");
1004         }
1005 }
1006
1007 /*
1008  * Disassemble floating-point ("escape") instruction
1009  * and return updated location.
1010  */
1011 static db_addr_t
1012 db_disasm_esc(loc, inst, rex, short_addr, size, seg)
1013         db_addr_t       loc;
1014         int             inst;
1015         int             rex;
1016         int             short_addr;
1017         int             size;
1018         const char *    seg;
1019 {
1020         int             regmodrm;
1021         const struct finst *    fp;
1022         int             mod;
1023         struct i_addr   address;
1024         const char *    name;
1025
1026         get_value_inc(regmodrm, loc, 1, FALSE);
1027         fp = &db_Esc_inst[inst - 0xd8][f_reg(rex, regmodrm)];
1028         mod = f_mod(rex, regmodrm);
1029         if (mod != 3) {
1030             if (*fp->f_name == '\0') {
1031                 db_printf("<bad instruction>");
1032                 return (loc);
1033             }
1034             /*
1035              * Normal address modes.
1036              */
1037             loc = db_read_address(loc, short_addr, rex, regmodrm, &address);
1038             db_printf("%s", fp->f_name);
1039             switch(fp->f_size) {
1040                 case SNGL:
1041                     db_printf("s");
1042                     break;
1043                 case DBLR:
1044                     db_printf("l");
1045                     break;
1046                 case EXTR:
1047                     db_printf("t");
1048                     break;
1049                 case WORD:
1050                     db_printf("s");
1051                     break;
1052                 case LONG:
1053                     db_printf("l");
1054                     break;
1055                 case QUAD:
1056                     db_printf("q");
1057                     break;
1058                 default:
1059                     break;
1060             }
1061             db_printf("\t");
1062             db_print_address(seg, BYTE, rex, &address);
1063         }
1064         else {
1065             /*
1066              * 'reg-reg' - special formats
1067              */
1068             switch (fp->f_rrmode) {
1069                 case op2(ST,STI):
1070                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1071                     db_printf("%s\t%%st,%%st(%d)",name,f_rm(rex, regmodrm));
1072                     break;
1073                 case op2(STI,ST):
1074                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1075                     db_printf("%s\t%%st(%d),%%st",name, f_rm(rex, regmodrm));
1076                     break;
1077                 case op1(STI):
1078                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1079                     db_printf("%s\t%%st(%d)",name, f_rm(rex, regmodrm));
1080                     break;
1081                 case op1(X):
1082                     name = ((const char * const *)fp->f_rrname)[f_rm(rex, regmodrm)];
1083                     if (*name == '\0')
1084                         goto bad;
1085                     db_printf("%s", name);
1086                     break;
1087                 case op1(XA):
1088                     name = ((const char * const *)fp->f_rrname)[f_rm(rex, regmodrm)];
1089                     if (*name == '\0')
1090                         goto bad;
1091                     db_printf("%s\t%%ax", name);
1092                     break;
1093                 default:
1094                 bad:
1095                     db_printf("<bad instruction>");
1096                     break;
1097             }
1098         }
1099
1100         return (loc);
1101 }
1102
1103 /*
1104  * Disassemble instruction at 'loc'.  'altfmt' specifies an
1105  * (optional) alternate format.  Return address of start of
1106  * next instruction.
1107  */
1108 db_addr_t
1109 db_disasm(loc, altfmt)
1110         db_addr_t       loc;
1111         boolean_t       altfmt;
1112 {
1113         int     inst;
1114         int     size;
1115         int     short_addr;
1116         const char *    seg;
1117         const struct inst *     ip;
1118         const char *    i_name;
1119         int     i_size;
1120         int     i_mode;
1121         int     rex = 0;
1122         int     regmodrm = 0;
1123         boolean_t       first;
1124         int     displ;
1125         int     prefix;
1126         int     imm;
1127         int     imm2;
1128         long    imm64;
1129         int     len;
1130         struct i_addr   address;
1131
1132         get_value_inc(inst, loc, 1, FALSE);
1133         short_addr = FALSE;
1134         size = LONG;
1135         seg = 0;
1136
1137         /*
1138          * Get prefixes
1139          */
1140         prefix = TRUE;
1141         do {
1142             switch (inst) {
1143                 case 0x66:              /* data16 */
1144                     size = WORD;
1145                     break;
1146                 case 0x67:
1147                     short_addr = TRUE;
1148                     break;
1149                 case 0x26:
1150                     seg = "%es";
1151                     break;
1152                 case 0x36:
1153                     seg = "%ss";
1154                     break;
1155                 case 0x2e:
1156                     seg = "%cs";
1157                     break;
1158                 case 0x3e:
1159                     seg = "%ds";
1160                     break;
1161                 case 0x64:
1162                     seg = "%fs";
1163                     break;
1164                 case 0x65:
1165                     seg = "%gs";
1166                     break;
1167                 case 0xf0:
1168                     db_printf("lock ");
1169                     break;
1170                 case 0xf2:
1171                     db_printf("repne ");
1172                     break;
1173                 case 0xf3:
1174                     db_printf("repe "); /* XXX repe VS rep */
1175                     break;
1176                 default:
1177                     prefix = FALSE;
1178                     break;
1179             }
1180             if (inst >= 0x40 && inst < 0x50) {
1181                 rex = inst;
1182                 prefix = TRUE;
1183             }
1184             if (prefix) {
1185                 get_value_inc(inst, loc, 1, FALSE);
1186             }
1187         } while (prefix);
1188
1189         if (inst >= 0xd8 && inst <= 0xdf) {
1190             loc = db_disasm_esc(loc, inst, rex, short_addr, size, seg);
1191             db_printf("\n");
1192             return (loc);
1193         }
1194
1195         if (inst == 0x0f) {
1196             get_value_inc(inst, loc, 1, FALSE);
1197             ip = db_inst_0f[inst>>4];
1198             if (ip == 0) {
1199                 ip = &db_bad_inst;
1200             }
1201             else {
1202                 ip = &ip[inst&0xf];
1203             }
1204         }
1205         else
1206             ip = &db_inst_table[inst];
1207
1208         if (ip->i_has_modrm) {
1209             get_value_inc(regmodrm, loc, 1, FALSE);
1210             loc = db_read_address(loc, short_addr, rex, regmodrm, &address);
1211         }
1212
1213         i_name = ip->i_name;
1214         i_size = ip->i_size;
1215         i_mode = ip->i_mode;
1216
1217         if (ip->i_extra == db_Grp1 || ip->i_extra == db_Grp2 ||
1218             ip->i_extra == db_Grp6 || ip->i_extra == db_Grp7 ||
1219             ip->i_extra == db_Grp8 || ip->i_extra == db_Grp9) {
1220             i_name = ((const char * const *)ip->i_extra)[f_reg(rex, regmodrm)];
1221         }
1222         else if (ip->i_extra == db_Grp3) {
1223             ip = ip->i_extra;
1224             ip = &ip[f_reg(rex, regmodrm)];
1225             i_name = ip->i_name;
1226             i_mode = ip->i_mode;
1227         }
1228         else if (ip->i_extra == db_Grp4 || ip->i_extra == db_Grp5) {
1229             ip = ip->i_extra;
1230             ip = &ip[f_reg(rex, regmodrm)];
1231             i_name = ip->i_name;
1232             i_mode = ip->i_mode;
1233             i_size = ip->i_size;
1234         }
1235
1236         if (i_size == SDEP) {
1237             if (size == WORD)
1238                 db_printf("%s", i_name);
1239             else
1240                 db_printf("%s", (const char *)ip->i_extra);
1241         }
1242         else {
1243             db_printf("%s", i_name);
1244             if ((inst >= 0x50 && inst <= 0x5f) || inst == 0x68 || inst == 0x6a) {
1245                 i_size = NONE;
1246                 db_printf("q");
1247             }
1248             if (i_size != NONE) {
1249                 if (i_size == BYTE) {
1250                     db_printf("b");
1251                     size = BYTE;
1252                 }
1253                 else if (i_size == WORD) {
1254                     db_printf("w");
1255                     size = WORD;
1256                 }
1257                 else if (size == WORD)
1258                     db_printf("w");
1259                 else {
1260                     if (rex & REX_W)
1261                         db_printf("q");
1262                     else
1263                         db_printf("l");
1264                 }
1265             }
1266         }
1267         db_printf("\t");
1268         for (first = TRUE;
1269              i_mode != 0;
1270              i_mode >>= 8, first = FALSE)
1271         {
1272             if (!first)
1273                 db_printf(",");
1274
1275             switch (i_mode & 0xFF) {
1276
1277                 case E:
1278                     db_print_address(seg, size, rex, &address);
1279                     break;
1280
1281                 case Eind:
1282                     db_printf("*");
1283                     db_print_address(seg, size, rex, &address);
1284                     break;
1285
1286                 case El:
1287                     db_print_address(seg, (rex & REX_W) ? QUAD : LONG, rex, &address);
1288                     break;
1289
1290                 case EL:
1291                     db_print_address(seg, LONG, 0, &address);
1292                     break;
1293
1294                 case Ew:
1295                     db_print_address(seg, WORD, rex, &address);
1296                     break;
1297
1298                 case Eb:
1299                     db_print_address(seg, BYTE, rex, &address);
1300                     break;
1301
1302                 case R:
1303                     db_printf("%s", db_reg[rex != 0 ? 1 : 0][(size == LONG && (rex & REX_W)) ? QUAD : size][f_reg(rex, regmodrm)]);
1304                     break;
1305
1306                 case Rw:
1307                     db_printf("%s", db_reg[rex != 0 ? 1 : 0][WORD][f_reg(rex, regmodrm)]);
1308                     break;
1309
1310                 case Ri:
1311                     db_printf("%s", db_reg[0][QUAD][f_rm(rex, inst)]);
1312                     break;
1313
1314                 case Ril:
1315                     db_printf("%s", db_reg[rex != 0 ? 1 : 0][(rex & REX_R) ? QUAD : LONG][f_rm(rex, inst)]);
1316                     break;
1317
1318                 case S:
1319                     db_printf("%s", db_seg_reg[f_reg(rex, regmodrm)]);
1320                     break;
1321
1322                 case Si:
1323                     db_printf("%s", db_seg_reg[f_reg(rex, inst)]);
1324                     break;
1325
1326                 case A:
1327                     db_printf("%s", db_reg[rex != 0 ? 1 : 0][size][0]); /* acc */
1328                     break;
1329
1330                 case BX:
1331                     if (seg)
1332                         db_printf("%s:", seg);
1333                     db_printf("(%s)", short_addr ? "%bx" : "%ebx");
1334                     break;
1335
1336                 case CL:
1337                     db_printf("%%cl");
1338                     break;
1339
1340                 case DX:
1341                     db_printf("%%dx");
1342                     break;
1343
1344                 case SI:
1345                     if (seg)
1346                         db_printf("%s:", seg);
1347                     db_printf("(%s)", short_addr ? "%si" : "%rsi");
1348                     break;
1349
1350                 case DI:
1351                     db_printf("%%es:(%s)", short_addr ? "%di" : "%rdi");
1352                     break;
1353
1354                 case CR:
1355                     db_printf("%%cr%d", f_reg(rex, regmodrm));
1356                     break;
1357
1358                 case DR:
1359                     db_printf("%%dr%d", f_reg(rex, regmodrm));
1360                     break;
1361
1362                 case TR:
1363                     db_printf("%%tr%d", f_reg(rex, regmodrm));
1364                     break;
1365
1366                 case I:
1367                     len = db_lengths[size];
1368                     get_value_inc(imm, loc, len, FALSE);
1369                     db_printf("$%#r", imm);
1370                     break;
1371
1372                 case Is:
1373                     len = db_lengths[(size == LONG && (rex & REX_W)) ? QUAD : size];
1374                     get_value_inc(imm, loc, len, FALSE);
1375                     db_printf("$%+#r", imm);
1376                     break;
1377
1378                 case Ib:
1379                     get_value_inc(imm, loc, 1, FALSE);
1380                     db_printf("$%#r", imm);
1381                     break;
1382
1383                 case Iba:
1384                     get_value_inc(imm, loc, 1, FALSE);
1385                     if (imm != 0x0a)
1386                         db_printf("$%#r", imm);
1387                     break;
1388
1389                 case Ibs:
1390                     get_value_inc(imm, loc, 1, TRUE);
1391                     if (size == WORD)
1392                         imm &= 0xFFFF;
1393                     db_printf("$%+#r", imm);
1394                     break;
1395
1396                 case Iw:
1397                     get_value_inc(imm, loc, 2, FALSE);
1398                     db_printf("$%#r", imm);
1399                     break;
1400
1401                 case Ilq:
1402                     len = db_lengths[rex & REX_W ? QUAD : LONG];
1403                     get_value_inc(imm64, loc, len, FALSE);
1404                     db_printf("$%#lr", imm64);
1405                     break;
1406
1407                 case O:
1408                     len = (short_addr ? 2 : 4);
1409                     get_value_inc(displ, loc, len, FALSE);
1410                     if (seg)
1411                         db_printf("%s:%+#r",seg, displ);
1412                     else
1413                         db_printsym((db_addr_t)displ, DB_STGY_ANY);
1414                     break;
1415
1416                 case Db:
1417                     get_value_inc(displ, loc, 1, TRUE);
1418                     displ += loc;
1419                     if (size == WORD)
1420                         displ &= 0xFFFF;
1421                     db_printsym((db_addr_t)displ, DB_STGY_XTRN);
1422                     break;
1423
1424                 case Dl:
1425                     len = db_lengths[(size == LONG && (rex & REX_W)) ? QUAD : size];
1426                     get_value_inc(displ, loc, len, FALSE);
1427                     displ += loc;
1428                     if (size == WORD)
1429                         displ &= 0xFFFF;
1430                     db_printsym((db_addr_t)displ, DB_STGY_XTRN);
1431                     break;
1432
1433                 case o1:
1434                     db_printf("$1");
1435                     break;
1436
1437                 case o3:
1438                     db_printf("$3");
1439                     break;
1440
1441                 case OS:
1442                     len = db_lengths[size];
1443                     get_value_inc(imm, loc, len, FALSE);        /* offset */
1444                     get_value_inc(imm2, loc, 2, FALSE); /* segment */
1445                     db_printf("$%#r,%#r", imm2, imm);
1446                     break;
1447             }
1448         }
1449         db_printf("\n");
1450         return (loc);
1451 }