]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/db_disasm.c
Merge bmake-20240309
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / 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 /*
29  * Instruction disassembler.
30  */
31 #include <sys/param.h>
32 #include <sys/kdb.h>
33
34 #include <ddb/ddb.h>
35 #include <ddb/db_access.h>
36 #include <ddb/db_sym.h>
37
38 /*
39  * Size attributes
40  */
41 #define BYTE    0
42 #define WORD    1
43 #define LONG    2
44 #define QUAD    3
45 #define SNGL    4
46 #define DBLR    5
47 #define EXTR    6
48 #define SDEP    7
49 #define NONE    8
50
51 /*
52  * Addressing modes
53  */
54 #define E       1                       /* general effective address */
55 #define Eind    2                       /* indirect address (jump, call) */
56 #define Ew      3                       /* address, word size */
57 #define Eb      4                       /* address, byte size */
58 #define R       5                       /* register, in 'reg' field */
59 #define Rw      6                       /* word register, in 'reg' field */
60 #define Ri      7                       /* register in instruction */
61 #define S       8                       /* segment reg, in 'reg' field */
62 #define Si      9                       /* segment reg, in instruction */
63 #define A       10                      /* accumulator */
64 #define BX      11                      /* (bx) */
65 #define CL      12                      /* cl, for shifts */
66 #define DX      13                      /* dx, for IO */
67 #define SI      14                      /* si */
68 #define DI      15                      /* di */
69 #define CR      16                      /* control register */
70 #define DR      17                      /* debug register */
71 #define TR      18                      /* test register */
72 #define I       19                      /* immediate, unsigned */
73 #define Is      20                      /* immediate, signed */
74 #define Ib      21                      /* byte immediate, unsigned */
75 #define Ibs     22                      /* byte immediate, signed */
76 #define Iw      23                      /* word immediate, unsigned */
77 #define O       25                      /* direct address */
78 #define Db      26                      /* byte displacement from EIP */
79 #define Dl      27                      /* long displacement from EIP */
80 #define o1      28                      /* constant 1 */
81 #define o3      29                      /* constant 3 */
82 #define OS      30                      /* immediate offset/segment */
83 #define ST      31                      /* FP stack top */
84 #define STI     32                      /* FP stack */
85 #define X       33                      /* extended FP op */
86 #define XA      34                      /* for 'fstcw %ax' */
87 #define El      35                      /* address, long size */
88 #define Ril     36                      /* long register in instruction */
89 #define Iba     37                      /* byte immediate, don't print if 0xa */
90
91 struct inst {
92         const char *    i_name;         /* name */
93         bool    i_has_modrm;            /* has regmodrm byte */
94         short   i_size;                 /* operand size */
95         int     i_mode;                 /* addressing modes */
96         const void *    i_extra;        /* pointer to extra opcode table */
97 };
98
99 #define op1(x)          (x)
100 #define op2(x,y)        ((x)|((y)<<8))
101 #define op3(x,y,z)      ((x)|((y)<<8)|((z)<<16))
102
103 struct finst {
104         const char *    f_name;         /* name for memory instruction */
105         int     f_size;                 /* size for memory instruction */
106         int     f_rrmode;               /* mode for rr instruction */
107         const void *    f_rrname;       /* name for rr instruction
108                                            (or pointer to table) */
109 };
110
111 static const char * const db_Grp6[] = {
112         "sldt",
113         "str",
114         "lldt",
115         "ltr",
116         "verr",
117         "verw",
118         "",
119         ""
120 };
121
122 static const char * const db_Grp7[] = {
123         "sgdt",
124         "sidt",
125         "lgdt",
126         "lidt",
127         "smsw",
128         "",
129         "lmsw",
130         "invlpg"
131 };
132
133 static const char * const db_Grp8[] = {
134         "",
135         "",
136         "",
137         "",
138         "bt",
139         "bts",
140         "btr",
141         "btc"
142 };
143
144 static const char * const db_Grp9[] = {
145         "",
146         "cmpxchg8b",
147         "",
148         "",
149         "",
150         "",
151         "",
152         ""
153 };
154
155 static const char * const db_Grp15[] = {
156         "fxsave",
157         "fxrstor",
158         "ldmxcsr",
159         "stmxcsr",
160         "",
161         "",
162         "",
163         "clflush"
164 };
165
166 static const char * const db_Grp15b[] = {
167         "",
168         "",
169         "",
170         "",
171         "",
172         "lfence",
173         "mfence",
174         "sfence"
175 };
176
177 static const struct inst db_inst_0f0x[] = {
178 /*00*/  { "",      true,  NONE,  op1(Ew),     db_Grp6 },
179 /*01*/  { "",      true,  NONE,  op1(Ew),     db_Grp7 },
180 /*02*/  { "lar",   true,  LONG,  op2(E,R),    0 },
181 /*03*/  { "lsl",   true,  LONG,  op2(E,R),    0 },
182 /*04*/  { "",      false, NONE,  0,           0 },
183 /*05*/  { "syscall",false,NONE,  0,           0 },
184 /*06*/  { "clts",  false, NONE,  0,           0 },
185 /*07*/  { "sysret",false, NONE,  0,           0 },
186
187 /*08*/  { "invd",  false, NONE,  0,           0 },
188 /*09*/  { "wbinvd",false, NONE,  0,           0 },
189 /*0a*/  { "",      false, NONE,  0,           0 },
190 /*0b*/  { "",      false, NONE,  0,           0 },
191 /*0c*/  { "",      false, NONE,  0,           0 },
192 /*0d*/  { "",      false, NONE,  0,           0 },
193 /*0e*/  { "",      false, NONE,  0,           0 },
194 /*0f*/  { "",      false, NONE,  0,           0 },
195 };
196
197 static const struct inst db_inst_0f1x[] = {
198 /*10*/  { "",      false, NONE,  0,           0 },
199 /*11*/  { "",      false, NONE,  0,           0 },
200 /*12*/  { "",      false, NONE,  0,           0 },
201 /*13*/  { "",      false, NONE,  0,           0 },
202 /*14*/  { "",      false, NONE,  0,           0 },
203 /*15*/  { "",      false, NONE,  0,           0 },
204 /*16*/  { "",      false, NONE,  0,           0 },
205 /*17*/  { "",      false, NONE,  0,           0 },
206
207 /*18*/  { "",      false, NONE,  0,           0 },
208 /*19*/  { "",      false, NONE,  0,           0 },
209 /*1a*/  { "",      false, NONE,  0,           0 },
210 /*1b*/  { "",      false, NONE,  0,           0 },
211 /*1c*/  { "",      false, NONE,  0,           0 },
212 /*1d*/  { "",      false, NONE,  0,           0 },
213 /*1e*/  { "",      false, NONE,  0,           0 },
214 /*1f*/  { "nopl",  true,  SDEP,  0,           "nopw" },
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*/  { "sysenter",false,NONE,  0,          0 },
243 /*35*/  { "sysexit",false,NONE,  0,           0 },
244 /*36*/  { "",      false, NONE,  0,           0 },
245 /*37*/  { "getsec",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 /*ae*/  { "",      true,  LONG,  op1(E),      db_Grp15 },
334 /*af*/  { "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         db_inst_0f1x,
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*/  { "inc",   false, LONG,  op1(Ri),    0 },
650 /*41*/  { "inc",   false, LONG,  op1(Ri),    0 },
651 /*42*/  { "inc",   false, LONG,  op1(Ri),    0 },
652 /*43*/  { "inc",   false, LONG,  op1(Ri),    0 },
653 /*44*/  { "inc",   false, LONG,  op1(Ri),    0 },
654 /*45*/  { "inc",   false, LONG,  op1(Ri),    0 },
655 /*46*/  { "inc",   false, LONG,  op1(Ri),    0 },
656 /*47*/  { "inc",   false, LONG,  op1(Ri),    0 },
657
658 /*48*/  { "dec",   false, LONG,  op1(Ri),    0 },
659 /*49*/  { "dec",   false, LONG,  op1(Ri),    0 },
660 /*4a*/  { "dec",   false, LONG,  op1(Ri),    0 },
661 /*4b*/  { "dec",   false, LONG,  op1(Ri),    0 },
662 /*4c*/  { "dec",   false, LONG,  op1(Ri),    0 },
663 /*4d*/  { "dec",   false, LONG,  op1(Ri),    0 },
664 /*4e*/  { "dec",   false, LONG,  op1(Ri),    0 },
665 /*4f*/  { "dec",   false, LONG,  op1(Ri),    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*/  { "arpl",  true,  NONE,  op2(Rw,Ew), 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(I, Ri),  0 },
786 /*b9*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
787 /*ba*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
788 /*bb*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
789 /*bc*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
790 /*bd*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
791 /*be*/  { "mov",   false, LONG,  op2(I, Ri),  0 },
792 /*bf*/  { "mov",   false, LONG,  op2(I, 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(byte)     ((byte)>>6)
872 #define f_reg(byte)     (((byte)>>3)&0x7)
873 #define f_rm(byte)      ((byte)&0x7)
874
875 #define sib_ss(byte)    ((byte)>>6)
876 #define sib_index(byte) (((byte)>>3)&0x7)
877 #define sib_base(byte)  ((byte)&0x7)
878
879 struct i_addr {
880         bool            is_reg; /* if reg, reg number is in 'disp' */
881         int             disp;
882         const char *    base;
883         const char *    index;
884         int             ss;
885         bool            defss;  /* set if %ss is the default segment */
886 };
887
888 static const char * const db_index_reg_16[8] = {
889         "%bx,%si",
890         "%bx,%di",
891         "%bp,%si",
892         "%bp,%di",
893         "%si",
894         "%di",
895         "%bp",
896         "%bx"
897 };
898
899 static const char * const db_reg[3][8] = {
900         { "%al",  "%cl",  "%dl",  "%bl",  "%ah",  "%ch",  "%dh",  "%bh" },
901         { "%ax",  "%cx",  "%dx",  "%bx",  "%sp",  "%bp",  "%si",  "%di" },
902         { "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi" }
903 };
904
905 static const char * const db_seg_reg[8] = {
906         "%es", "%cs", "%ss", "%ds", "%fs", "%gs", "", ""
907 };
908
909 /*
910  * lengths for size attributes
911  */
912 static const int db_lengths[] = {
913         1,      /* BYTE */
914         2,      /* WORD */
915         4,      /* LONG */
916         8,      /* QUAD */
917         4,      /* SNGL */
918         8,      /* DBLR */
919         10,     /* EXTR */
920 };
921
922 #define get_value_inc(result, loc, size, is_signed) \
923         result = db_get_value((loc), (size), (is_signed)); \
924         (loc) += (size);
925
926 static db_addr_t
927                 db_disasm_esc(db_addr_t loc, int inst, bool short_addr,
928                     int size, const char *seg);
929 static void     db_print_address(const char *seg, int size,
930                     struct i_addr *addrp);
931 static db_addr_t
932                 db_read_address(db_addr_t loc, bool short_addr, int regmodrm,
933                     struct i_addr *addrp);
934
935 /*
936  * Read address at location and return updated location.
937  */
938 static db_addr_t
939 db_read_address(db_addr_t loc, bool short_addr, int regmodrm,
940     struct i_addr *addrp)
941 {
942         int             mod, rm, sib, index, disp;
943
944         mod = f_mod(regmodrm);
945         rm  = f_rm(regmodrm);
946
947         if (mod == 3) {
948             addrp->is_reg = true;
949             addrp->disp = rm;
950             return (loc);
951         }
952         addrp->is_reg = false;
953         addrp->index = NULL;
954         addrp->ss = 0;
955         addrp->defss = false;
956
957         if (short_addr) {
958             if (rm == 2 || rm == 3 || (rm == 6 && mod != 0))
959                 addrp->defss = true;
960             switch (mod) {
961                 case 0:
962                     if (rm == 6) {
963                         get_value_inc(disp, loc, 2, false);
964                         addrp->disp = disp;
965                         addrp->base = NULL;
966                     }
967                     else {
968                         addrp->disp = 0;
969                         addrp->base = db_index_reg_16[rm];
970                     }
971                     break;
972                 case 1:
973                     get_value_inc(disp, loc, 1, true);
974                     disp &= 0xFFFF;
975                     addrp->disp = disp;
976                     addrp->base = db_index_reg_16[rm];
977                     break;
978                 case 2:
979                     get_value_inc(disp, loc, 2, false);
980                     addrp->disp = disp;
981                     addrp->base = db_index_reg_16[rm];
982                     break;
983             }
984         }
985         else {
986             if (rm == 4) {
987                 get_value_inc(sib, loc, 1, false);
988                 rm = sib_base(sib);
989                 index = sib_index(sib);
990                 if (index != 4)
991                     addrp->index = db_reg[LONG][index];
992                 addrp->ss = sib_ss(sib);
993             }
994
995             switch (mod) {
996                 case 0:
997                     if (rm == 5) {
998                         get_value_inc(addrp->disp, loc, 4, false);
999                         addrp->base = NULL;
1000                     }
1001                     else {
1002                         addrp->disp = 0;
1003                         addrp->base = db_reg[LONG][rm];
1004                     }
1005                     break;
1006
1007                 case 1:
1008                     get_value_inc(disp, loc, 1, true);
1009                     addrp->disp = disp;
1010                     addrp->base = db_reg[LONG][rm];
1011                     break;
1012
1013                 case 2:
1014                     get_value_inc(disp, loc, 4, false);
1015                     addrp->disp = disp;
1016                     addrp->base = db_reg[LONG][rm];
1017                     break;
1018             }
1019         }
1020         return (loc);
1021 }
1022
1023 static void
1024 db_print_address(const char *seg, int size, struct i_addr *addrp)
1025 {
1026         if (addrp->is_reg) {
1027             db_printf("%s", db_reg[size][addrp->disp]);
1028             return;
1029         }
1030
1031         if (seg) {
1032             db_printf("%s:", seg);
1033         }
1034         else if (addrp->defss) {
1035             db_printf("%%ss:");
1036         }
1037
1038         db_printsym((db_addr_t)addrp->disp, DB_STGY_ANY);
1039         if (addrp->base != NULL || addrp->index != NULL) {
1040             db_printf("(");
1041             if (addrp->base)
1042                 db_printf("%s", addrp->base);
1043             if (addrp->index)
1044                 db_printf(",%s,%d", addrp->index, 1<<addrp->ss);
1045             db_printf(")");
1046         }
1047 }
1048
1049 /*
1050  * Disassemble floating-point ("escape") instruction
1051  * and return updated location.
1052  */
1053 static db_addr_t
1054 db_disasm_esc(db_addr_t loc, int inst, bool short_addr, int size,
1055     const char *seg)
1056 {
1057         int             regmodrm;
1058         const struct finst *    fp;
1059         int             mod;
1060         struct i_addr   address;
1061         const char *    name;
1062
1063         get_value_inc(regmodrm, loc, 1, false);
1064         fp = &db_Esc_inst[inst - 0xd8][f_reg(regmodrm)];
1065         mod = f_mod(regmodrm);
1066         if (mod != 3) {
1067             if (*fp->f_name == '\0') {
1068                 db_printf("<bad instruction>");
1069                 return (loc);
1070             }
1071             /*
1072              * Normal address modes.
1073              */
1074             loc = db_read_address(loc, short_addr, regmodrm, &address);
1075             db_printf("%s", fp->f_name);
1076             switch(fp->f_size) {
1077                 case SNGL:
1078                     db_printf("s");
1079                     break;
1080                 case DBLR:
1081                     db_printf("l");
1082                     break;
1083                 case EXTR:
1084                     db_printf("t");
1085                     break;
1086                 case WORD:
1087                     db_printf("s");
1088                     break;
1089                 case LONG:
1090                     db_printf("l");
1091                     break;
1092                 case QUAD:
1093                     db_printf("q");
1094                     break;
1095                 default:
1096                     break;
1097             }
1098             db_printf("\t");
1099             db_print_address(seg, BYTE, &address);
1100         }
1101         else {
1102             /*
1103              * 'reg-reg' - special formats
1104              */
1105             switch (fp->f_rrmode) {
1106                 case op2(ST,STI):
1107                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1108                     db_printf("%s\t%%st,%%st(%d)",name,f_rm(regmodrm));
1109                     break;
1110                 case op2(STI,ST):
1111                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1112                     db_printf("%s\t%%st(%d),%%st",name, f_rm(regmodrm));
1113                     break;
1114                 case op1(STI):
1115                     name = (fp->f_rrname) ? fp->f_rrname : fp->f_name;
1116                     db_printf("%s\t%%st(%d)",name, f_rm(regmodrm));
1117                     break;
1118                 case op1(X):
1119                     name = ((const char * const *)fp->f_rrname)[f_rm(regmodrm)];
1120                     if (*name == '\0')
1121                         goto bad;
1122                     db_printf("%s", name);
1123                     break;
1124                 case op1(XA):
1125                     name = ((const char * const *)fp->f_rrname)[f_rm(regmodrm)];
1126                     if (*name == '\0')
1127                         goto bad;
1128                     db_printf("%s\t%%ax", name);
1129                     break;
1130                 default:
1131                 bad:
1132                     db_printf("<bad instruction>");
1133                     break;
1134             }
1135         }
1136
1137         return (loc);
1138 }
1139
1140 /*
1141  * Disassemble instruction at 'loc'.  'altfmt' specifies an
1142  * (optional) alternate format.  Return address of start of
1143  * next instruction.
1144  */
1145 db_addr_t
1146 db_disasm(db_addr_t loc, bool altfmt)
1147 {
1148         int     inst;
1149         int     size;
1150         bool    short_addr;
1151         const char *    seg;
1152         const struct inst *     ip;
1153         const char *    i_name;
1154         int     i_size;
1155         int     i_mode;
1156         int     regmodrm = 0;
1157         bool    first;
1158         int     displ;
1159         bool    prefix;
1160         bool    rep;
1161         int     imm;
1162         int     imm2;
1163         int     len;
1164         struct i_addr   address;
1165
1166         if (db_segsize(kdb_frame) == 16)
1167            altfmt = !altfmt;
1168         get_value_inc(inst, loc, 1, false);
1169         if (altfmt) {
1170             short_addr = true;
1171             size = WORD;
1172         }
1173         else {
1174             short_addr = false;
1175             size = LONG;
1176         }
1177         seg = NULL;
1178
1179         /*
1180          * Get prefixes
1181          */
1182         rep = false;
1183         prefix = true;
1184         do {
1185             switch (inst) {
1186                 case 0x66:
1187                     size = (altfmt ? LONG : WORD);
1188                     break;
1189                 case 0x67:
1190                     short_addr = !altfmt;
1191                     break;
1192                 case 0x26:
1193                     seg = "%es";
1194                     break;
1195                 case 0x36:
1196                     seg = "%ss";
1197                     break;
1198                 case 0x2e:
1199                     seg = "%cs";
1200                     break;
1201                 case 0x3e:
1202                     seg = "%ds";
1203                     break;
1204                 case 0x64:
1205                     seg = "%fs";
1206                     break;
1207                 case 0x65:
1208                     seg = "%gs";
1209                     break;
1210                 case 0xf0:
1211                     db_printf("lock ");
1212                     break;
1213                 case 0xf2:
1214                     db_printf("repne ");
1215                     break;
1216                 case 0xf3:
1217                     rep = true;
1218                     break;
1219                 default:
1220                     prefix = false;
1221                     break;
1222             }
1223             if (prefix) {
1224                 get_value_inc(inst, loc, 1, false);
1225             }
1226             if (rep) {
1227                 if (inst == 0x90) {
1228                     db_printf("pause\n");
1229                     return (loc);
1230                 }
1231                 db_printf("repe ");     /* XXX repe VS rep */
1232                 rep = false;
1233             }
1234         } while (prefix);
1235
1236         if (inst >= 0xd8 && inst <= 0xdf) {
1237             loc = db_disasm_esc(loc, inst, short_addr, size, seg);
1238             db_printf("\n");
1239             return (loc);
1240         }
1241
1242         if (inst == 0x0f) {
1243             get_value_inc(inst, loc, 1, false);
1244             ip = db_inst_0f[inst>>4];
1245             if (ip == NULL) {
1246                 ip = &db_bad_inst;
1247             }
1248             else {
1249                 ip = &ip[inst&0xf];
1250             }
1251         }
1252         else
1253             ip = &db_inst_table[inst];
1254
1255         if (ip->i_has_modrm) {
1256             get_value_inc(regmodrm, loc, 1, false);
1257             loc = db_read_address(loc, short_addr, regmodrm, &address);
1258         }
1259
1260         i_name = ip->i_name;
1261         i_size = ip->i_size;
1262         i_mode = ip->i_mode;
1263
1264         if (ip->i_extra == db_Grp1 || ip->i_extra == db_Grp2 ||
1265             ip->i_extra == db_Grp6 || ip->i_extra == db_Grp7 ||
1266             ip->i_extra == db_Grp8 || ip->i_extra == db_Grp9 ||
1267             ip->i_extra == db_Grp15) {
1268             i_name = ((const char * const *)ip->i_extra)[f_reg(regmodrm)];
1269         }
1270         else if (ip->i_extra == db_Grp3) {
1271             ip = ip->i_extra;
1272             ip = &ip[f_reg(regmodrm)];
1273             i_name = ip->i_name;
1274             i_mode = ip->i_mode;
1275         }
1276         else if (ip->i_extra == db_Grp4 || ip->i_extra == db_Grp5) {
1277             ip = ip->i_extra;
1278             ip = &ip[f_reg(regmodrm)];
1279             i_name = ip->i_name;
1280             i_mode = ip->i_mode;
1281             i_size = ip->i_size;
1282         }
1283
1284         /* Special cases that don't fit well in the tables. */
1285         if (ip->i_extra == db_Grp7 && f_mod(regmodrm) == 3) {
1286                 switch (regmodrm) {
1287                 case 0xc8:
1288                         i_name = "monitor";
1289                         i_size = NONE;
1290                         i_mode = 0;
1291                         break;
1292                 case 0xc9:
1293                         i_name = "mwait";
1294                         i_size = NONE;
1295                         i_mode = 0;
1296                         break;
1297                 }
1298         }
1299         if (ip->i_extra == db_Grp15 && f_mod(regmodrm) == 3) {
1300                 i_name = db_Grp15b[f_reg(regmodrm)];
1301                 i_size = NONE;
1302                 i_mode = 0;
1303         }
1304
1305         if (i_size == SDEP) {
1306             if (size == WORD)
1307                 db_printf("%s", i_name);
1308             else
1309                 db_printf("%s", (const char *)ip->i_extra);
1310         }
1311         else {
1312             db_printf("%s", i_name);
1313             if (i_size != NONE) {
1314                 if (i_size == BYTE) {
1315                     db_printf("b");
1316                     size = BYTE;
1317                 }
1318                 else if (i_size == WORD) {
1319                     db_printf("w");
1320                     size = WORD;
1321                 }
1322                 else if (size == WORD)
1323                     db_printf("w");
1324                 else
1325                     db_printf("l");
1326             }
1327         }
1328         db_printf("\t");
1329         for (first = true;
1330              i_mode != 0;
1331              i_mode >>= 8, first = false)
1332         {
1333             if (!first)
1334                 db_printf(",");
1335
1336             switch (i_mode & 0xFF) {
1337                 case E:
1338                     db_print_address(seg, size, &address);
1339                     break;
1340
1341                 case Eind:
1342                     db_printf("*");
1343                     db_print_address(seg, size, &address);
1344                     break;
1345
1346                 case El:
1347                     db_print_address(seg, LONG, &address);
1348                     break;
1349
1350                 case Ew:
1351                     db_print_address(seg, WORD, &address);
1352                     break;
1353
1354                 case Eb:
1355                     db_print_address(seg, BYTE, &address);
1356                     break;
1357
1358                 case R:
1359                     db_printf("%s", db_reg[size][f_reg(regmodrm)]);
1360                     break;
1361
1362                 case Rw:
1363                     db_printf("%s", db_reg[WORD][f_reg(regmodrm)]);
1364                     break;
1365
1366                 case Ri:
1367                     db_printf("%s", db_reg[size][f_rm(inst)]);
1368                     break;
1369
1370                 case Ril:
1371                     db_printf("%s", db_reg[LONG][f_rm(inst)]);
1372                     break;
1373
1374                 case S:
1375                     db_printf("%s", db_seg_reg[f_reg(regmodrm)]);
1376                     break;
1377
1378                 case Si:
1379                     db_printf("%s", db_seg_reg[f_reg(inst)]);
1380                     break;
1381
1382                 case A:
1383                     db_printf("%s", db_reg[size][0]);   /* acc */
1384                     break;
1385
1386                 case BX:
1387                     if (seg)
1388                         db_printf("%s:", seg);
1389                     db_printf("(%s)", short_addr ? "%bx" : "%ebx");
1390                     break;
1391
1392                 case CL:
1393                     db_printf("%%cl");
1394                     break;
1395
1396                 case DX:
1397                     db_printf("%%dx");
1398                     break;
1399
1400                 case SI:
1401                     if (seg)
1402                         db_printf("%s:", seg);
1403                     db_printf("(%s)", short_addr ? "%si" : "%esi");
1404                     break;
1405
1406                 case DI:
1407                     db_printf("%%es:(%s)", short_addr ? "%di" : "%edi");
1408                     break;
1409
1410                 case CR:
1411                     db_printf("%%cr%d", f_reg(regmodrm));
1412                     break;
1413
1414                 case DR:
1415                     db_printf("%%dr%d", f_reg(regmodrm));
1416                     break;
1417
1418                 case TR:
1419                     db_printf("%%tr%d", f_reg(regmodrm));
1420                     break;
1421
1422                 case I:
1423                     len = db_lengths[size];
1424                     get_value_inc(imm, loc, len, false);
1425                     db_printf("$%#r", imm);
1426                     break;
1427
1428                 case Is:
1429                     len = db_lengths[size];
1430                     get_value_inc(imm, loc, len, false);
1431                     db_printf("$%+#r", imm);
1432                     break;
1433
1434                 case Ib:
1435                     get_value_inc(imm, loc, 1, false);
1436                     db_printf("$%#r", imm);
1437                     break;
1438
1439                 case Iba:
1440                     get_value_inc(imm, loc, 1, false);
1441                     if (imm != 0x0a)
1442                         db_printf("$%#r", imm);
1443                     break;
1444
1445                 case Ibs:
1446                     get_value_inc(imm, loc, 1, true);
1447                     if (size == WORD)
1448                         imm &= 0xFFFF;
1449                     db_printf("$%+#r", imm);
1450                     break;
1451
1452                 case Iw:
1453                     get_value_inc(imm, loc, 2, false);
1454                     db_printf("$%#r", imm);
1455                     break;
1456
1457                 case O:
1458                     len = (short_addr ? 2 : 4);
1459                     get_value_inc(displ, loc, len, false);
1460                     if (seg)
1461                         db_printf("%s:%+#r",seg, displ);
1462                     else
1463                         db_printsym((db_addr_t)displ, DB_STGY_ANY);
1464                     break;
1465
1466                 case Db:
1467                     get_value_inc(displ, loc, 1, true);
1468                     displ += loc;
1469                     if (size == WORD)
1470                         displ &= 0xFFFF;
1471                     db_printsym((db_addr_t)displ, DB_STGY_XTRN);
1472                     break;
1473
1474                 case Dl:
1475                     len = db_lengths[size];
1476                     get_value_inc(displ, loc, len, false);
1477                     displ += loc;
1478                     if (size == WORD)
1479                         displ &= 0xFFFF;
1480                     db_printsym((db_addr_t)displ, DB_STGY_XTRN);
1481                     break;
1482
1483                 case o1:
1484                     db_printf("$1");
1485                     break;
1486
1487                 case o3:
1488                     db_printf("$3");
1489                     break;
1490
1491                 case OS:
1492                     len = db_lengths[size];
1493                     get_value_inc(imm, loc, len, false);        /* offset */
1494                     get_value_inc(imm2, loc, 2, false); /* segment */
1495                     db_printf("$%#r,%#r", imm2, imm);
1496                     break;
1497             }
1498         }
1499         db_printf("\n");
1500         return (loc);
1501 }