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