Sync with trunk (48237)
[reactos.git] / ntoskrnl / kdbg / amd64 / i386-dis.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/kdbg/i386/i386-dis.c
5 * PURPOSE: No purpose listed.
6 *
7 * PROGRAMMERS: No programmer listed.
8 */
9
10 #include <ntoskrnl.h>
11 #define NDEBUG
12 #include <debug.h>
13
14 /* ReactOS compatibility stuff. */
15 #define PARAMS(X) X
16 #define PTR void*
17 typedef enum bfd_flavour
18 {
19 bfd_target_unknown_flavour,
20 } bfd_flavour;
21 typedef enum bfd_architecture
22 {
23 bfd_arch_i386,
24 } bfd_arch;
25 typedef ULONG_PTR bfd_vma;
26 typedef unsigned char bfd_byte;
27 enum bfd_endian { BFD_ENDIAN_BIG, BIG_ENDIAN_LITTLE, BFD_ENDIAN_UNKNOWN };
28 typedef void* bfd;
29 typedef LONG_PTR bfd_signed_vma;
30 #define bfd_mach_x86_64_intel_syntax 0
31 #define bfd_mach_x86_64 1
32 #define bfd_mach_i386_i386_intel_syntax 2
33 #define bfd_mach_i386_i386 3
34 #define bfd_mach_i386_i8086 4
35 #define abort() DbgBreakPoint();
36 #define _(X) X
37 #define ATTRIBUTE_UNUSED
38 extern int sprintf(char *str, const char *format, ...);
39 #define sprintf_vma(BUF, VMA) sprintf(BUF, "%p", VMA)
40 #include <setjmp.h>
41 #define _INC_SETJMPEX
42 struct disassemble_info;
43
44 int
45 print_insn_i386 (bfd_vma pc, struct disassemble_info *info);
46
47 int
48 KdbpPrintDisasm(void* Ignored, const char* fmt, ...)
49 {
50 va_list ap;
51 static char buffer[256];
52 int ret;
53
54 va_start(ap, fmt);
55 ret = vsprintf(buffer, fmt, ap);
56 DbgPrint("%s", buffer);
57 va_end(ap);
58 return(ret);
59 }
60
61 int
62 KdbpNopPrintDisasm(void* Ignored, const char* fmt, ...)
63 {
64 return(0);
65 }
66
67 static int
68 KdbpReadMemory(ULONG_PTR Addr, unsigned char* Data, unsigned int Length,
69 struct disassemble_info * Ignored)
70 {
71 return KdbpSafeReadMemory(Data, (void *)Addr, Length); /* 0 means no error */
72 }
73
74 static void
75 KdbpMemoryError(int Status, ULONG_PTR Addr,
76 struct disassemble_info * Ignored)
77 {
78 }
79
80 static void
81 KdbpPrintAddressInCode(ULONG_PTR Addr, struct disassemble_info * Ignored)
82 {
83 if (!KdbSymPrintAddress((void*)Addr))
84 {
85 DbgPrint("<%p>", Addr);
86 }
87 }
88
89 static void
90 KdbpNopPrintAddress(ULONG_PTR Addr, struct disassemble_info * Ignored)
91 {
92 }
93
94 #include "dis-asm.h"
95
96 LONG
97 KdbpGetInstLength(IN ULONG_PTR Address)
98 {
99 disassemble_info info;
100
101 info.fprintf_func = KdbpNopPrintDisasm;
102 info.stream = NULL;
103 info.application_data = NULL;
104 info.flavour = bfd_target_unknown_flavour;
105 info.arch = bfd_arch_i386;
106 info.mach = bfd_mach_x86_64;
107 info.insn_sets = 0;
108 info.flags = 0;
109 info.read_memory_func = KdbpReadMemory;
110 info.memory_error_func = KdbpMemoryError;
111 info.print_address_func = KdbpNopPrintAddress;
112 info.symbol_at_address_func = NULL;
113 info.buffer = NULL;
114 info.buffer_vma = info.buffer_length = 0;
115 info.bytes_per_chunk = 0;
116 info.display_endian = BIG_ENDIAN_LITTLE;
117 info.disassembler_options = NULL;
118
119 return(print_insn_i386(Address, &info));
120 }
121
122 LONG
123 KdbpDisassemble(IN ULONG_PTR Address, IN ULONG IntelSyntax)
124 {
125 disassemble_info info;
126
127 info.fprintf_func = KdbpPrintDisasm;
128 info.stream = NULL;
129 info.application_data = NULL;
130 info.flavour = bfd_target_unknown_flavour;
131 info.arch = bfd_arch_i386;
132 info.mach = IntelSyntax ? bfd_mach_i386_i386_intel_syntax : bfd_mach_i386_i386;
133 info.insn_sets = 0;
134 info.flags = 0;
135 info.read_memory_func = KdbpReadMemory;
136 info.memory_error_func = KdbpMemoryError;
137 info.print_address_func = KdbpPrintAddressInCode;
138 info.symbol_at_address_func = NULL;
139 info.buffer = NULL;
140 info.buffer_vma = info.buffer_length = 0;
141 info.bytes_per_chunk = 0;
142 info.display_endian = BIG_ENDIAN_LITTLE;
143 info.disassembler_options = NULL;
144
145 return(print_insn_i386(Address, &info));
146 }
147
148 /* Print i386 instructions for GDB, the GNU debugger.
149 Copyright 1988, 1989, 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
150 2001
151 Free Software Foundation, Inc.
152
153 This file is part of GDB.
154
155 This program is free software; you can redistribute it and/or modify
156 it under the terms of the GNU General Public License as published by
157 the Free Software Foundation; either version 2 of the License, or
158 (at your option) any later version.
159
160 This program is distributed in the hope that it will be useful,
161 but WITHOUT ANY WARRANTY; without even the implied warranty of
162 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
163 GNU General Public License for more details.
164
165 You should have received a copy of the GNU General Public License along
166 with this program; if not, write to the Free Software Foundation, Inc.,
167 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
168 */
169
170 /*
171 * 80386 instruction printer by Pace Willisson (pace@prep.ai.mit.edu)
172 * July 1988
173 * modified by John Hassey (hassey@dg-rtp.dg.com)
174 * x86-64 support added by Jan Hubicka (jh@suse.cz)
175 */
176
177 /*
178 * The main tables describing the instructions is essentially a copy
179 * of the "Opcode Map" chapter (Appendix A) of the Intel 80386
180 * Programmers Manual. Usually, there is a capital letter, followed
181 * by a small letter. The capital letter tell the addressing mode,
182 * and the small letter tells about the operand size. Refer to
183 * the Intel manual for details.
184 */
185
186 #include "dis-asm.h"
187 #if 0
188 #include "sysdep.h"
189 #include "opintl.h"
190 #endif
191
192 #define MAXLEN 20
193
194 #include <setjmp.h>
195
196 #ifndef UNIXWARE_COMPAT
197 /* Set non-zero for broken, compatible instructions. Set to zero for
198 non-broken opcodes. */
199 #define UNIXWARE_COMPAT 1
200 #endif
201
202 static int fetch_data PARAMS ((struct disassemble_info *, bfd_byte *));
203 static void ckprefix PARAMS ((void));
204 static const char *prefix_name PARAMS ((int, int));
205 static int print_insn PARAMS ((bfd_vma, disassemble_info *));
206 static void dofloat PARAMS ((int));
207 static void OP_ST PARAMS ((int, int));
208 static void OP_STi PARAMS ((int, int));
209 static int putop PARAMS ((const char *, int));
210 static void oappend PARAMS ((const char *));
211 static void append_seg PARAMS ((void));
212 static void OP_indirE PARAMS ((int, int));
213 static void print_operand_value PARAMS ((char *, int, bfd_vma));
214 static void OP_E PARAMS ((int, int));
215 static void OP_G PARAMS ((int, int));
216 static bfd_vma get64 PARAMS ((void));
217 static bfd_signed_vma get32 PARAMS ((void));
218 static bfd_signed_vma get32s PARAMS ((void));
219 static int get16 PARAMS ((void));
220 static void set_op PARAMS ((bfd_vma, int));
221 static void OP_REG PARAMS ((int, int));
222 static void OP_IMREG PARAMS ((int, int));
223 static void OP_I PARAMS ((int, int));
224 static void OP_I64 PARAMS ((int, int));
225 static void OP_sI PARAMS ((int, int));
226 static void OP_J PARAMS ((int, int));
227 static void OP_SEG PARAMS ((int, int));
228 static void OP_DIR PARAMS ((int, int));
229 static void OP_OFF PARAMS ((int, int));
230 static void OP_OFF64 PARAMS ((int, int));
231 static void ptr_reg PARAMS ((int, int));
232 static void OP_ESreg PARAMS ((int, int));
233 static void OP_DSreg PARAMS ((int, int));
234 static void OP_C PARAMS ((int, int));
235 static void OP_D PARAMS ((int, int));
236 static void OP_T PARAMS ((int, int));
237 static void OP_Rd PARAMS ((int, int));
238 static void OP_MMX PARAMS ((int, int));
239 static void OP_XMM PARAMS ((int, int));
240 static void OP_EM PARAMS ((int, int));
241 static void OP_EX PARAMS ((int, int));
242 static void OP_MS PARAMS ((int, int));
243 static void OP_XS PARAMS ((int, int));
244 static void OP_3DNowSuffix PARAMS ((int, int));
245 static void OP_SIMD_Suffix PARAMS ((int, int));
246 static void SIMD_Fixup PARAMS ((int, int));
247 static void BadOp PARAMS ((void));
248
249 struct dis_private {
250 /* Points to first byte not fetched. */
251 bfd_byte *max_fetched;
252 bfd_byte the_buffer[MAXLEN];
253 bfd_vma insn_start;
254 int orig_sizeflag;
255 jmp_buf bailout;
256 };
257
258 /* The opcode for the fwait instruction, which we treat as a prefix
259 when we can. */
260 #define FWAIT_OPCODE (0x9b)
261
262 /* Set to 1 for 64bit mode disassembly. */
263 static int mode_64bit = 1;
264
265 /* Flags for the prefixes for the current instruction. See below. */
266 static int prefixes;
267
268 /* REX prefix the current instruction. See below. */
269 static int rex;
270 /* Bits of REX we've already used. */
271 static int rex_used;
272 #define REX_MODE64 8
273 #define REX_EXTX 4
274 #define REX_EXTY 2
275 #define REX_EXTZ 1
276 /* Mark parts used in the REX prefix. When we are testing for
277 empty prefix (for 8bit register REX extension), just mask it
278 out. Otherwise test for REX bit is excuse for existence of REX
279 only in case value is nonzero. */
280 #define USED_REX(value) \
281 { \
282 if (value) \
283 rex_used |= (rex & value) ? (value) | 0x40 : 0; \
284 else \
285 rex_used |= 0x40; \
286 }
287
288 /* Flags for prefixes which we somehow handled when printing the
289 current instruction. */
290 static int used_prefixes;
291
292 /* Flags stored in PREFIXES. */
293 #define PREFIX_REPZ 1
294 #define PREFIX_REPNZ 2
295 #define PREFIX_LOCK 4
296 #define PREFIX_CS 8
297 #define PREFIX_SS 0x10
298 #define PREFIX_DS 0x20
299 #define PREFIX_ES 0x40
300 #define PREFIX_FS 0x80
301 #define PREFIX_GS 0x100
302 #define PREFIX_DATA 0x200
303 #define PREFIX_ADDR 0x400
304 #define PREFIX_FWAIT 0x800
305
306 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
307 to ADDR (exclusive) are valid. Returns 1 for success, longjmps
308 on error. */
309 #define FETCH_DATA(info, addr) \
310 ((addr) <= ((struct dis_private *) (info->private_data))->max_fetched \
311 ? 1 : fetch_data ((info), (addr)))
312
313 static int
314 fetch_data (info, addr)
315 struct disassemble_info *info;
316 bfd_byte *addr;
317 {
318 int status;
319 struct dis_private *priv = (struct dis_private *) info->private_data;
320 bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
321
322 status = (*info->read_memory_func) (start,
323 priv->max_fetched,
324 addr - priv->max_fetched,
325 info);
326 if (status != 0)
327 {
328 /* If we did manage to read at least one byte, then
329 print_insn_i386 will do something sensible. Otherwise, print
330 an error. We do that here because this is where we know
331 STATUS. */
332 if (priv->max_fetched == priv->the_buffer)
333 (*info->memory_error_func) (status, start, info);
334 longjmp (priv->bailout, 1);
335 }
336 else
337 priv->max_fetched = addr;
338 return 1;
339 }
340
341 #define XX NULL, 0
342
343 #define Eb OP_E, b_mode
344 #define Ev OP_E, v_mode
345 #define Ed OP_E, d_mode
346 #define indirEb OP_indirE, b_mode
347 #define indirEv OP_indirE, v_mode
348 #define Ew OP_E, w_mode
349 #define Ma OP_E, v_mode
350 #define M OP_E, 0 /* lea, lgdt, etc. */
351 #define Mp OP_E, 0 /* 32 or 48 bit memory operand for LDS, LES etc */
352 #define Gb OP_G, b_mode
353 #define Gv OP_G, v_mode
354 #define Gd OP_G, d_mode
355 #define Gw OP_G, w_mode
356 #define Rd OP_Rd, d_mode
357 #define Rm OP_Rd, m_mode
358 #define Ib OP_I, b_mode
359 #define sIb OP_sI, b_mode /* sign extened byte */
360 #define Iv OP_I, v_mode
361 #define Iq OP_I, q_mode
362 #define Iv64 OP_I64, v_mode
363 #define Iw OP_I, w_mode
364 #define Jb OP_J, b_mode
365 #define Jv OP_J, v_mode
366 #define Cm OP_C, m_mode
367 #define Dm OP_D, m_mode
368 #define Td OP_T, d_mode
369
370 #define RMeAX OP_REG, eAX_reg
371 #define RMeBX OP_REG, eBX_reg
372 #define RMeCX OP_REG, eCX_reg
373 #define RMeDX OP_REG, eDX_reg
374 #define RMeSP OP_REG, eSP_reg
375 #define RMeBP OP_REG, eBP_reg
376 #define RMeSI OP_REG, eSI_reg
377 #define RMeDI OP_REG, eDI_reg
378 #define RMrAX OP_REG, rAX_reg
379 #define RMrBX OP_REG, rBX_reg
380 #define RMrCX OP_REG, rCX_reg
381 #define RMrDX OP_REG, rDX_reg
382 #define RMrSP OP_REG, rSP_reg
383 #define RMrBP OP_REG, rBP_reg
384 #define RMrSI OP_REG, rSI_reg
385 #define RMrDI OP_REG, rDI_reg
386 #define RMAL OP_REG, al_reg
387 #define RMAL OP_REG, al_reg
388 #define RMCL OP_REG, cl_reg
389 #define RMDL OP_REG, dl_reg
390 #define RMBL OP_REG, bl_reg
391 #define RMAH OP_REG, ah_reg
392 #define RMCH OP_REG, ch_reg
393 #define RMDH OP_REG, dh_reg
394 #define RMBH OP_REG, bh_reg
395 #define RMAX OP_REG, ax_reg
396 #define RMDX OP_REG, dx_reg
397
398 #define eAX OP_IMREG, eAX_reg
399 #define eBX OP_IMREG, eBX_reg
400 #define eCX OP_IMREG, eCX_reg
401 #define eDX OP_IMREG, eDX_reg
402 #define eSP OP_IMREG, eSP_reg
403 #define eBP OP_IMREG, eBP_reg
404 #define eSI OP_IMREG, eSI_reg
405 #define eDI OP_IMREG, eDI_reg
406 #define AL OP_IMREG, al_reg
407 #define AL OP_IMREG, al_reg
408 #define CL OP_IMREG, cl_reg
409 #define DL OP_IMREG, dl_reg
410 #define BL OP_IMREG, bl_reg
411 #define AH OP_IMREG, ah_reg
412 #define CH OP_IMREG, ch_reg
413 #define DH OP_IMREG, dh_reg
414 #define BH OP_IMREG, bh_reg
415 #define AX OP_IMREG, ax_reg
416 #define DX OP_IMREG, dx_reg
417 #define indirDX OP_IMREG, indir_dx_reg
418
419 #define Sw OP_SEG, w_mode
420 #define Ap OP_DIR, 0
421 #define Ob OP_OFF, b_mode
422 #define Ob64 OP_OFF64, b_mode
423 #define Ov OP_OFF, v_mode
424 #define Ov64 OP_OFF64, v_mode
425 #define Xb OP_DSreg, eSI_reg
426 #define Xv OP_DSreg, eSI_reg
427 #define Yb OP_ESreg, eDI_reg
428 #define Yv OP_ESreg, eDI_reg
429 #define DSBX OP_DSreg, eBX_reg
430
431 #define es OP_REG, es_reg
432 #define ss OP_REG, ss_reg
433 #define cs OP_REG, cs_reg
434 #define ds OP_REG, ds_reg
435 #define fs OP_REG, fs_reg
436 #define gs OP_REG, gs_reg
437
438 #define MX OP_MMX, 0
439 #define XM OP_XMM, 0
440 #define EM OP_EM, v_mode
441 #define EX OP_EX, v_mode
442 #define MS OP_MS, v_mode
443 #define XS OP_XS, v_mode
444 #define None OP_E, 0
445 #define OPSUF OP_3DNowSuffix, 0
446 #define OPSIMD OP_SIMD_Suffix, 0
447
448 #define cond_jump_flag NULL, cond_jump_mode
449 #define loop_jcxz_flag NULL, loop_jcxz_mode
450
451 /* bits in sizeflag */
452 #define SUFFIX_ALWAYS 4
453 #define AFLAG 2
454 #define DFLAG 1
455
456 #define b_mode 1 /* byte operand */
457 #define v_mode 2 /* operand size depends on prefixes */
458 #define w_mode 3 /* word operand */
459 #define d_mode 4 /* double word operand */
460 #define q_mode 5 /* quad word operand */
461 #define x_mode 6
462 #define m_mode 7 /* d_mode in 32bit, q_mode in 64bit mode. */
463 #define cond_jump_mode 8
464 #define loop_jcxz_mode 9
465
466 #define es_reg 100
467 #define cs_reg 101
468 #define ss_reg 102
469 #define ds_reg 103
470 #define fs_reg 104
471 #define gs_reg 105
472
473 #define eAX_reg 108
474 #define eCX_reg 109
475 #define eDX_reg 110
476 #define eBX_reg 111
477 #define eSP_reg 112
478 #define eBP_reg 113
479 #define eSI_reg 114
480 #define eDI_reg 115
481
482 #define al_reg 116
483 #define cl_reg 117
484 #define dl_reg 118
485 #define bl_reg 119
486 #define ah_reg 120
487 #define ch_reg 121
488 #define dh_reg 122
489 #define bh_reg 123
490
491 #define ax_reg 124
492 #define cx_reg 125
493 #define dx_reg 126
494 #define bx_reg 127
495 #define sp_reg 128
496 #define bp_reg 129
497 #define si_reg 130
498 #define di_reg 131
499
500 #define rAX_reg 132
501 #define rCX_reg 133
502 #define rDX_reg 134
503 #define rBX_reg 135
504 #define rSP_reg 136
505 #define rBP_reg 137
506 #define rSI_reg 138
507 #define rDI_reg 139
508
509 #define indir_dx_reg 150
510
511 #define FLOATCODE 1
512 #define USE_GROUPS 2
513 #define USE_PREFIX_USER_TABLE 3
514 #define X86_64_SPECIAL 4
515
516 #define FLOAT NULL, NULL, FLOATCODE, NULL, 0, NULL, 0
517
518 #define GRP1b NULL, NULL, USE_GROUPS, NULL, 0, NULL, 0
519 #define GRP1S NULL, NULL, USE_GROUPS, NULL, 1, NULL, 0
520 #define GRP1Ss NULL, NULL, USE_GROUPS, NULL, 2, NULL, 0
521 #define GRP2b NULL, NULL, USE_GROUPS, NULL, 3, NULL, 0
522 #define GRP2S NULL, NULL, USE_GROUPS, NULL, 4, NULL, 0
523 #define GRP2b_one NULL, NULL, USE_GROUPS, NULL, 5, NULL, 0
524 #define GRP2S_one NULL, NULL, USE_GROUPS, NULL, 6, NULL, 0
525 #define GRP2b_cl NULL, NULL, USE_GROUPS, NULL, 7, NULL, 0
526 #define GRP2S_cl NULL, NULL, USE_GROUPS, NULL, 8, NULL, 0
527 #define GRP3b NULL, NULL, USE_GROUPS, NULL, 9, NULL, 0
528 #define GRP3S NULL, NULL, USE_GROUPS, NULL, 10, NULL, 0
529 #define GRP4 NULL, NULL, USE_GROUPS, NULL, 11, NULL, 0
530 #define GRP5 NULL, NULL, USE_GROUPS, NULL, 12, NULL, 0
531 #define GRP6 NULL, NULL, USE_GROUPS, NULL, 13, NULL, 0
532 #define GRP7 NULL, NULL, USE_GROUPS, NULL, 14, NULL, 0
533 #define GRP8 NULL, NULL, USE_GROUPS, NULL, 15, NULL, 0
534 #define GRP9 NULL, NULL, USE_GROUPS, NULL, 16, NULL, 0
535 #define GRP10 NULL, NULL, USE_GROUPS, NULL, 17, NULL, 0
536 #define GRP11 NULL, NULL, USE_GROUPS, NULL, 18, NULL, 0
537 #define GRP12 NULL, NULL, USE_GROUPS, NULL, 19, NULL, 0
538 #define GRP13 NULL, NULL, USE_GROUPS, NULL, 20, NULL, 0
539 #define GRP14 NULL, NULL, USE_GROUPS, NULL, 21, NULL, 0
540 #define GRPAMD NULL, NULL, USE_GROUPS, NULL, 22, NULL, 0
541
542 #define PREGRP0 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 0, NULL, 0
543 #define PREGRP1 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 1, NULL, 0
544 #define PREGRP2 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 2, NULL, 0
545 #define PREGRP3 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 3, NULL, 0
546 #define PREGRP4 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 4, NULL, 0
547 #define PREGRP5 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 5, NULL, 0
548 #define PREGRP6 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 6, NULL, 0
549 #define PREGRP7 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 7, NULL, 0
550 #define PREGRP8 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 8, NULL, 0
551 #define PREGRP9 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 9, NULL, 0
552 #define PREGRP10 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 10, NULL, 0
553 #define PREGRP11 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 11, NULL, 0
554 #define PREGRP12 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 12, NULL, 0
555 #define PREGRP13 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 13, NULL, 0
556 #define PREGRP14 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 14, NULL, 0
557 #define PREGRP15 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 15, NULL, 0
558 #define PREGRP16 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 16, NULL, 0
559 #define PREGRP17 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 17, NULL, 0
560 #define PREGRP18 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 18, NULL, 0
561 #define PREGRP19 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 19, NULL, 0
562 #define PREGRP20 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 20, NULL, 0
563 #define PREGRP21 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 21, NULL, 0
564 #define PREGRP22 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 22, NULL, 0
565 #define PREGRP23 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 23, NULL, 0
566 #define PREGRP24 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 24, NULL, 0
567 #define PREGRP25 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 25, NULL, 0
568 #define PREGRP26 NULL, NULL, USE_PREFIX_USER_TABLE, NULL, 26, NULL, 0
569
570 #define X86_64_0 NULL, NULL, X86_64_SPECIAL, NULL, 0, NULL, 0
571
572 typedef void (*op_rtn) PARAMS ((int bytemode, int sizeflag));
573
574 struct dis386 {
575 const char *name;
576 op_rtn op1;
577 int bytemode1;
578 op_rtn op2;
579 int bytemode2;
580 op_rtn op3;
581 int bytemode3;
582 };
583
584 /* Upper case letters in the instruction names here are macros.
585 'A' => print 'b' if no register operands or suffix_always is true
586 'B' => print 'b' if suffix_always is true
587 'E' => print 'e' if 32-bit form of jcxz
588 'F' => print 'w' or 'l' depending on address size prefix (loop insns)
589 'H' => print ",pt" or ",pn" branch hint
590 'L' => print 'l' if suffix_always is true
591 'N' => print 'n' if instruction has no wait "prefix"
592 'O' => print 'd', or 'o'
593 'P' => print 'w', 'l' or 'q' if instruction has an operand size prefix,
594 . or suffix_always is true. print 'q' if rex prefix is present.
595 'Q' => print 'w', 'l' or 'q' if no register operands or suffix_always
596 . is true
597 'R' => print 'w', 'l' or 'q' ("wd" or "dq" in intel mode)
598 'S' => print 'w', 'l' or 'q' if suffix_always is true
599 'T' => print 'q' in 64bit mode and behave as 'P' otherwise
600 'U' => print 'q' in 64bit mode and behave as 'Q' otherwise
601 'X' => print 's', 'd' depending on data16 prefix (for XMM)
602 'W' => print 'b' or 'w' ("w" or "de" in intel mode)
603 'Y' => 'q' if instruction has an REX 64bit overwrite prefix
604
605 Many of the above letters print nothing in Intel mode. See "putop"
606 for the details.
607
608 Braces '{' and '}', and vertical bars '|', indicate alternative
609 mnemonic strings for AT&T, Intel, X86_64 AT&T, and X86_64 Intel
610 modes. In cases where there are only two alternatives, the X86_64
611 instruction is reserved, and "(bad)" is printed.
612 */
613
614 static const struct dis386 dis386[] = {
615 /* 00 */
616 { "addB", Eb, Gb, XX },
617 { "addS", Ev, Gv, XX },
618 { "addB", Gb, Eb, XX },
619 { "addS", Gv, Ev, XX },
620 { "addB", AL, Ib, XX },
621 { "addS", eAX, Iv, XX },
622 { "push{T|}", es, XX, XX },
623 { "pop{T|}", es, XX, XX },
624 /* 08 */
625 { "orB", Eb, Gb, XX },
626 { "orS", Ev, Gv, XX },
627 { "orB", Gb, Eb, XX },
628 { "orS", Gv, Ev, XX },
629 { "orB", AL, Ib, XX },
630 { "orS", eAX, Iv, XX },
631 { "push{T|}", cs, XX, XX },
632 { "(bad)", XX, XX, XX }, /* 0x0f extended opcode escape */
633 /* 10 */
634 { "adcB", Eb, Gb, XX },
635 { "adcS", Ev, Gv, XX },
636 { "adcB", Gb, Eb, XX },
637 { "adcS", Gv, Ev, XX },
638 { "adcB", AL, Ib, XX },
639 { "adcS", eAX, Iv, XX },
640 { "push{T|}", ss, XX, XX },
641 { "popT|}", ss, XX, XX },
642 /* 18 */
643 { "sbbB", Eb, Gb, XX },
644 { "sbbS", Ev, Gv, XX },
645 { "sbbB", Gb, Eb, XX },
646 { "sbbS", Gv, Ev, XX },
647 { "sbbB", AL, Ib, XX },
648 { "sbbS", eAX, Iv, XX },
649 { "push{T|}", ds, XX, XX },
650 { "pop{T|}", ds, XX, XX },
651 /* 20 */
652 { "andB", Eb, Gb, XX },
653 { "andS", Ev, Gv, XX },
654 { "andB", Gb, Eb, XX },
655 { "andS", Gv, Ev, XX },
656 { "andB", AL, Ib, XX },
657 { "andS", eAX, Iv, XX },
658 { "(bad)", XX, XX, XX }, /* SEG ES prefix */
659 { "daa{|}", XX, XX, XX },
660 /* 28 */
661 { "subB", Eb, Gb, XX },
662 { "subS", Ev, Gv, XX },
663 { "subB", Gb, Eb, XX },
664 { "subS", Gv, Ev, XX },
665 { "subB", AL, Ib, XX },
666 { "subS", eAX, Iv, XX },
667 { "(bad)", XX, XX, XX }, /* SEG CS prefix */
668 { "das{|}", XX, XX, XX },
669 /* 30 */
670 { "xorB", Eb, Gb, XX },
671 { "xorS", Ev, Gv, XX },
672 { "xorB", Gb, Eb, XX },
673 { "xorS", Gv, Ev, XX },
674 { "xorB", AL, Ib, XX },
675 { "xorS", eAX, Iv, XX },
676 { "(bad)", XX, XX, XX }, /* SEG SS prefix */
677 { "aaa{|}", XX, XX, XX },
678 /* 38 */
679 { "cmpB", Eb, Gb, XX },
680 { "cmpS", Ev, Gv, XX },
681 { "cmpB", Gb, Eb, XX },
682 { "cmpS", Gv, Ev, XX },
683 { "cmpB", AL, Ib, XX },
684 { "cmpS", eAX, Iv, XX },
685 { "(bad)", XX, XX, XX }, /* SEG DS prefix */
686 { "aas{|}", XX, XX, XX },
687 /* 40 */
688 { "inc{S|}", RMeAX, XX, XX },
689 { "inc{S|}", RMeCX, XX, XX },
690 { "inc{S|}", RMeDX, XX, XX },
691 { "inc{S|}", RMeBX, XX, XX },
692 { "inc{S|}", RMeSP, XX, XX },
693 { "inc{S|}", RMeBP, XX, XX },
694 { "inc{S|}", RMeSI, XX, XX },
695 { "inc{S|}", RMeDI, XX, XX },
696 /* 48 */
697 { "dec{S|}", RMeAX, XX, XX },
698 { "dec{S|}", RMeCX, XX, XX },
699 { "dec{S|}", RMeDX, XX, XX },
700 { "dec{S|}", RMeBX, XX, XX },
701 { "dec{S|}", RMeSP, XX, XX },
702 { "dec{S|}", RMeBP, XX, XX },
703 { "dec{S|}", RMeSI, XX, XX },
704 { "dec{S|}", RMeDI, XX, XX },
705 /* 50 */
706 { "pushS", RMrAX, XX, XX },
707 { "pushS", RMrCX, XX, XX },
708 { "pushS", RMrDX, XX, XX },
709 { "pushS", RMrBX, XX, XX },
710 { "pushS", RMrSP, XX, XX },
711 { "pushS", RMrBP, XX, XX },
712 { "pushS", RMrSI, XX, XX },
713 { "pushS", RMrDI, XX, XX },
714 /* 58 */
715 { "popS", RMrAX, XX, XX },
716 { "popS", RMrCX, XX, XX },
717 { "popS", RMrDX, XX, XX },
718 { "popS", RMrBX, XX, XX },
719 { "popS", RMrSP, XX, XX },
720 { "popS", RMrBP, XX, XX },
721 { "popS", RMrSI, XX, XX },
722 { "popS", RMrDI, XX, XX },
723 /* 60 */
724 { "pusha{P|}", XX, XX, XX },
725 { "popa{P|}", XX, XX, XX },
726 { "bound{S|}", Gv, Ma, XX },
727 { X86_64_0 },
728 { "(bad)", XX, XX, XX }, /* seg fs */
729 { "(bad)", XX, XX, XX }, /* seg gs */
730 { "(bad)", XX, XX, XX }, /* op size prefix */
731 { "(bad)", XX, XX, XX }, /* adr size prefix */
732 /* 68 */
733 { "pushT", Iq, XX, XX },
734 { "imulS", Gv, Ev, Iv },
735 { "pushT", sIb, XX, XX },
736 { "imulS", Gv, Ev, sIb },
737 { "ins{b||b|}", Yb, indirDX, XX },
738 { "ins{R||R|}", Yv, indirDX, XX },
739 { "outs{b||b|}", indirDX, Xb, XX },
740 { "outs{R||R|}", indirDX, Xv, XX },
741 /* 70 */
742 { "joH", Jb, XX, cond_jump_flag },
743 { "jnoH", Jb, XX, cond_jump_flag },
744 { "jbH", Jb, XX, cond_jump_flag },
745 { "jaeH", Jb, XX, cond_jump_flag },
746 { "jeH", Jb, XX, cond_jump_flag },
747 { "jneH", Jb, XX, cond_jump_flag },
748 { "jbeH", Jb, XX, cond_jump_flag },
749 { "jaH", Jb, XX, cond_jump_flag },
750 /* 78 */
751 { "jsH", Jb, XX, cond_jump_flag },
752 { "jnsH", Jb, XX, cond_jump_flag },
753 { "jpH", Jb, XX, cond_jump_flag },
754 { "jnpH", Jb, XX, cond_jump_flag },
755 { "jlH", Jb, XX, cond_jump_flag },
756 { "jgeH", Jb, XX, cond_jump_flag },
757 { "jleH", Jb, XX, cond_jump_flag },
758 { "jgH", Jb, XX, cond_jump_flag },
759 /* 80 */
760 { GRP1b },
761 { GRP1S },
762 { "(bad)", XX, XX, XX },
763 { GRP1Ss },
764 { "testB", Eb, Gb, XX },
765 { "testS", Ev, Gv, XX },
766 { "xchgB", Eb, Gb, XX },
767 { "xchgS", Ev, Gv, XX },
768 /* 88 */
769 { "movB", Eb, Gb, XX },
770 { "movS", Ev, Gv, XX },
771 { "movB", Gb, Eb, XX },
772 { "movS", Gv, Ev, XX },
773 { "movQ", Ev, Sw, XX },
774 { "leaS", Gv, M, XX },
775 { "movQ", Sw, Ev, XX },
776 { "popU", Ev, XX, XX },
777 /* 90 */
778 { "nop", XX, XX, XX },
779 /* FIXME: NOP with REPz prefix is called PAUSE. */
780 { "xchgS", RMeCX, eAX, XX },
781 { "xchgS", RMeDX, eAX, XX },
782 { "xchgS", RMeBX, eAX, XX },
783 { "xchgS", RMeSP, eAX, XX },
784 { "xchgS", RMeBP, eAX, XX },
785 { "xchgS", RMeSI, eAX, XX },
786 { "xchgS", RMeDI, eAX, XX },
787 /* 98 */
788 { "cW{tR||tR|}", XX, XX, XX },
789 { "cR{tO||tO|}", XX, XX, XX },
790 { "lcall{T|}", Ap, XX, XX },
791 { "(bad)", XX, XX, XX }, /* fwait */
792 { "pushfT", XX, XX, XX },
793 { "popfT", XX, XX, XX },
794 { "sahf{|}", XX, XX, XX },
795 { "lahf{|}", XX, XX, XX },
796 /* a0 */
797 { "movB", AL, Ob64, XX },
798 { "movS", eAX, Ov64, XX },
799 { "movB", Ob64, AL, XX },
800 { "movS", Ov64, eAX, XX },
801 { "movs{b||b|}", Yb, Xb, XX },
802 { "movs{R||R|}", Yv, Xv, XX },
803 { "cmps{b||b|}", Xb, Yb, XX },
804 { "cmps{R||R|}", Xv, Yv, XX },
805 /* a8 */
806 { "testB", AL, Ib, XX },
807 { "testS", eAX, Iv, XX },
808 { "stosB", Yb, AL, XX },
809 { "stosS", Yv, eAX, XX },
810 { "lodsB", AL, Xb, XX },
811 { "lodsS", eAX, Xv, XX },
812 { "scasB", AL, Yb, XX },
813 { "scasS", eAX, Yv, XX },
814 /* b0 */
815 { "movB", RMAL, Ib, XX },
816 { "movB", RMCL, Ib, XX },
817 { "movB", RMDL, Ib, XX },
818 { "movB", RMBL, Ib, XX },
819 { "movB", RMAH, Ib, XX },
820 { "movB", RMCH, Ib, XX },
821 { "movB", RMDH, Ib, XX },
822 { "movB", RMBH, Ib, XX },
823 /* b8 */
824 { "movS", RMeAX, Iv64, XX },
825 { "movS", RMeCX, Iv64, XX },
826 { "movS", RMeDX, Iv64, XX },
827 { "movS", RMeBX, Iv64, XX },
828 { "movS", RMeSP, Iv64, XX },
829 { "movS", RMeBP, Iv64, XX },
830 { "movS", RMeSI, Iv64, XX },
831 { "movS", RMeDI, Iv64, XX },
832 /* c0 */
833 { GRP2b },
834 { GRP2S },
835 { "retT", Iw, XX, XX },
836 { "retT", XX, XX, XX },
837 { "les{S|}", Gv, Mp, XX },
838 { "ldsS", Gv, Mp, XX },
839 { "movA", Eb, Ib, XX },
840 { "movQ", Ev, Iv, XX },
841 /* c8 */
842 { "enterT", Iw, Ib, XX },
843 { "leaveT", XX, XX, XX },
844 { "lretP", Iw, XX, XX },
845 { "lretP", XX, XX, XX },
846 { "int3", XX, XX, XX },
847 { "int", Ib, XX, XX },
848 { "into{|}", XX, XX, XX },
849 { "iretP", XX, XX, XX },
850 /* d0 */
851 { GRP2b_one },
852 { GRP2S_one },
853 { GRP2b_cl },
854 { GRP2S_cl },
855 { "aam{|}", sIb, XX, XX },
856 { "aad{|}", sIb, XX, XX },
857 { "(bad)", XX, XX, XX },
858 { "xlat", DSBX, XX, XX },
859 /* d8 */
860 { FLOAT },
861 { FLOAT },
862 { FLOAT },
863 { FLOAT },
864 { FLOAT },
865 { FLOAT },
866 { FLOAT },
867 { FLOAT },
868 /* e0 */
869 { "loopneFH", Jb, XX, loop_jcxz_flag },
870 { "loopeFH", Jb, XX, loop_jcxz_flag },
871 { "loopFH", Jb, XX, loop_jcxz_flag },
872 { "jEcxzH", Jb, XX, loop_jcxz_flag },
873 { "inB", AL, Ib, XX },
874 { "inS", eAX, Ib, XX },
875 { "outB", Ib, AL, XX },
876 { "outS", Ib, eAX, XX },
877 /* e8 */
878 { "callT", Jv, XX, XX },
879 { "jmpT", Jv, XX, XX },
880 { "ljmp{T|}", Ap, XX, XX },
881 { "jmp", Jb, XX, XX },
882 { "inB", AL, indirDX, XX },
883 { "inS", eAX, indirDX, XX },
884 { "outB", indirDX, AL, XX },
885 { "outS", indirDX, eAX, XX },
886 /* f0 */
887 { "(bad)", XX, XX, XX }, /* lock prefix */
888 { "(bad)", XX, XX, XX },
889 { "(bad)", XX, XX, XX }, /* repne */
890 { "(bad)", XX, XX, XX }, /* repz */
891 { "hlt", XX, XX, XX },
892 { "cmc", XX, XX, XX },
893 { GRP3b },
894 { GRP3S },
895 /* f8 */
896 { "clc", XX, XX, XX },
897 { "stc", XX, XX, XX },
898 { "cli", XX, XX, XX },
899 { "sti", XX, XX, XX },
900 { "cld", XX, XX, XX },
901 { "std", XX, XX, XX },
902 { GRP4 },
903 { GRP5 },
904 };
905
906 static const struct dis386 dis386_twobyte[] = {
907 /* 00 */
908 { GRP6 },
909 { GRP7 },
910 { "larS", Gv, Ew, XX },
911 { "lslS", Gv, Ew, XX },
912 { "(bad)", XX, XX, XX },
913 { "syscall", XX, XX, XX },
914 { "clts", XX, XX, XX },
915 { "sysretP", XX, XX, XX },
916 /* 08 */
917 { "invd", XX, XX, XX },
918 { "wbinvd", XX, XX, XX },
919 { "(bad)", XX, XX, XX },
920 { "ud2a", XX, XX, XX },
921 { "(bad)", XX, XX, XX },
922 { GRPAMD },
923 { "femms", XX, XX, XX },
924 { "", MX, EM, OPSUF }, /* See OP_3DNowSuffix. */
925 /* 10 */
926 { PREGRP8 },
927 { PREGRP9 },
928 { "movlpX", XM, EX, SIMD_Fixup, 'h' }, /* really only 2 operands */
929 { "movlpX", EX, XM, SIMD_Fixup, 'h' },
930 { "unpcklpX", XM, EX, XX },
931 { "unpckhpX", XM, EX, XX },
932 { "movhpX", XM, EX, SIMD_Fixup, 'l' },
933 { "movhpX", EX, XM, SIMD_Fixup, 'l' },
934 /* 18 */
935 { GRP14 },
936 { "(bad)", XX, XX, XX },
937 { "(bad)", XX, XX, XX },
938 { "(bad)", XX, XX, XX },
939 { "(bad)", XX, XX, XX },
940 { "(bad)", XX, XX, XX },
941 { "(bad)", XX, XX, XX },
942 { "(bad)", XX, XX, XX },
943 /* 20 */
944 { "movL", Rm, Cm, XX },
945 { "movL", Rm, Dm, XX },
946 { "movL", Cm, Rm, XX },
947 { "movL", Dm, Rm, XX },
948 { "movL", Rd, Td, XX },
949 { "(bad)", XX, XX, XX },
950 { "movL", Td, Rd, XX },
951 { "(bad)", XX, XX, XX },
952 /* 28 */
953 { "movapX", XM, EX, XX },
954 { "movapX", EX, XM, XX },
955 { PREGRP2 },
956 { "movntpX", Ev, XM, XX },
957 { PREGRP4 },
958 { PREGRP3 },
959 { "ucomisX", XM,EX, XX },
960 { "comisX", XM,EX, XX },
961 /* 30 */
962 { "wrmsr", XX, XX, XX },
963 { "rdtsc", XX, XX, XX },
964 { "rdmsr", XX, XX, XX },
965 { "rdpmc", XX, XX, XX },
966 { "sysenter", XX, XX, XX },
967 { "sysexit", XX, XX, XX },
968 { "(bad)", XX, XX, XX },
969 { "(bad)", XX, XX, XX },
970 /* 38 */
971 { "(bad)", XX, XX, XX },
972 { "(bad)", XX, XX, XX },
973 { "(bad)", XX, XX, XX },
974 { "(bad)", XX, XX, XX },
975 { "(bad)", XX, XX, XX },
976 { "(bad)", XX, XX, XX },
977 { "(bad)", XX, XX, XX },
978 { "(bad)", XX, XX, XX },
979 /* 40 */
980 { "cmovo", Gv, Ev, XX },
981 { "cmovno", Gv, Ev, XX },
982 { "cmovb", Gv, Ev, XX },
983 { "cmovae", Gv, Ev, XX },
984 { "cmove", Gv, Ev, XX },
985 { "cmovne", Gv, Ev, XX },
986 { "cmovbe", Gv, Ev, XX },
987 { "cmova", Gv, Ev, XX },
988 /* 48 */
989 { "cmovs", Gv, Ev, XX },
990 { "cmovns", Gv, Ev, XX },
991 { "cmovp", Gv, Ev, XX },
992 { "cmovnp", Gv, Ev, XX },
993 { "cmovl", Gv, Ev, XX },
994 { "cmovge", Gv, Ev, XX },
995 { "cmovle", Gv, Ev, XX },
996 { "cmovg", Gv, Ev, XX },
997 /* 50 */
998 { "movmskpX", Gd, XS, XX },
999 { PREGRP13 },
1000 { PREGRP12 },
1001 { PREGRP11 },
1002 { "andpX", XM, EX, XX },
1003 { "andnpX", XM, EX, XX },
1004 { "orpX", XM, EX, XX },
1005 { "xorpX", XM, EX, XX },
1006 /* 58 */
1007 { PREGRP0 },
1008 { PREGRP10 },
1009 { PREGRP17 },
1010 { PREGRP16 },
1011 { PREGRP14 },
1012 { PREGRP7 },
1013 { PREGRP5 },
1014 { PREGRP6 },
1015 /* 60 */
1016 { "punpcklbw", MX, EM, XX },
1017 { "punpcklwd", MX, EM, XX },
1018 { "punpckldq", MX, EM, XX },
1019 { "packsswb", MX, EM, XX },
1020 { "pcmpgtb", MX, EM, XX },
1021 { "pcmpgtw", MX, EM, XX },
1022 { "pcmpgtd", MX, EM, XX },
1023 { "packuswb", MX, EM, XX },
1024 /* 68 */
1025 { "punpckhbw", MX, EM, XX },
1026 { "punpckhwd", MX, EM, XX },
1027 { "punpckhdq", MX, EM, XX },
1028 { "packssdw", MX, EM, XX },
1029 { PREGRP26 },
1030 { PREGRP24 },
1031 { "movd", MX, Ed, XX },
1032 { PREGRP19 },
1033 /* 70 */
1034 { PREGRP22 },
1035 { GRP10 },
1036 { GRP11 },
1037 { GRP12 },
1038 { "pcmpeqb", MX, EM, XX },
1039 { "pcmpeqw", MX, EM, XX },
1040 { "pcmpeqd", MX, EM, XX },
1041 { "emms", XX, XX, XX },
1042 /* 78 */
1043 { "(bad)", XX, XX, XX },
1044 { "(bad)", XX, XX, XX },
1045 { "(bad)", XX, XX, XX },
1046 { "(bad)", XX, XX, XX },
1047 { "(bad)", XX, XX, XX },
1048 { "(bad)", XX, XX, XX },
1049 { PREGRP23 },
1050 { PREGRP20 },
1051 /* 80 */
1052 { "joH", Jv, XX, cond_jump_flag },
1053 { "jnoH", Jv, XX, cond_jump_flag },
1054 { "jbH", Jv, XX, cond_jump_flag },
1055 { "jaeH", Jv, XX, cond_jump_flag },
1056 { "jeH", Jv, XX, cond_jump_flag },
1057 { "jneH", Jv, XX, cond_jump_flag },
1058 { "jbeH", Jv, XX, cond_jump_flag },
1059 { "jaH", Jv, XX, cond_jump_flag },
1060 /* 88 */
1061 { "jsH", Jv, XX, cond_jump_flag },
1062 { "jnsH", Jv, XX, cond_jump_flag },
1063 { "jpH", Jv, XX, cond_jump_flag },
1064 { "jnpH", Jv, XX, cond_jump_flag },
1065 { "jlH", Jv, XX, cond_jump_flag },
1066 { "jgeH", Jv, XX, cond_jump_flag },
1067 { "jleH", Jv, XX, cond_jump_flag },
1068 { "jgH", Jv, XX, cond_jump_flag },
1069 /* 90 */
1070 { "seto", Eb, XX, XX },
1071 { "setno", Eb, XX, XX },
1072 { "setb", Eb, XX, XX },
1073 { "setae", Eb, XX, XX },
1074 { "sete", Eb, XX, XX },
1075 { "setne", Eb, XX, XX },
1076 { "setbe", Eb, XX, XX },
1077 { "seta", Eb, XX, XX },
1078 /* 98 */
1079 { "sets", Eb, XX, XX },
1080 { "setns", Eb, XX, XX },
1081 { "setp", Eb, XX, XX },
1082 { "setnp", Eb, XX, XX },
1083 { "setl", Eb, XX, XX },
1084 { "setge", Eb, XX, XX },
1085 { "setle", Eb, XX, XX },
1086 { "setg", Eb, XX, XX },
1087 /* a0 */
1088 { "pushT", fs, XX, XX },
1089 { "popT", fs, XX, XX },
1090 { "cpuid", XX, XX, XX },
1091 { "btS", Ev, Gv, XX },
1092 { "shldS", Ev, Gv, Ib },
1093 { "shldS", Ev, Gv, CL },
1094 { "(bad)", XX, XX, XX },
1095 { "(bad)", XX, XX, XX },
1096 /* a8 */
1097 { "pushT", gs, XX, XX },
1098 { "popT", gs, XX, XX },
1099 { "rsm", XX, XX, XX },
1100 { "btsS", Ev, Gv, XX },
1101 { "shrdS", Ev, Gv, Ib },
1102 { "shrdS", Ev, Gv, CL },
1103 { GRP13 },
1104 { "imulS", Gv, Ev, XX },
1105 /* b0 */
1106 { "cmpxchgB", Eb, Gb, XX },
1107 { "cmpxchgS", Ev, Gv, XX },
1108 { "lssS", Gv, Mp, XX },
1109 { "btrS", Ev, Gv, XX },
1110 { "lfsS", Gv, Mp, XX },
1111 { "lgsS", Gv, Mp, XX },
1112 { "movz{bR|x|bR|x}", Gv, Eb, XX },
1113 { "movz{wR|x|wR|x}", Gv, Ew, XX }, /* yes, there really is movzww ! */
1114 /* b8 */
1115 { "(bad)", XX, XX, XX },
1116 { "ud2b", XX, XX, XX },
1117 { GRP8 },
1118 { "btcS", Ev, Gv, XX },
1119 { "bsfS", Gv, Ev, XX },
1120 { "bsrS", Gv, Ev, XX },
1121 { "movs{bR|x|bR|x}", Gv, Eb, XX },
1122 { "movs{wR|x|wR|x}", Gv, Ew, XX }, /* yes, there really is movsww ! */
1123 /* c0 */
1124 { "xaddB", Eb, Gb, XX },
1125 { "xaddS", Ev, Gv, XX },
1126 { PREGRP1 },
1127 { "movntiS", Ev, Gv, XX },
1128 { "pinsrw", MX, Ed, Ib },
1129 { "pextrw", Gd, MS, Ib },
1130 { "shufpX", XM, EX, Ib },
1131 { GRP9 },
1132 /* c8 */
1133 { "bswap", RMeAX, XX, XX },
1134 { "bswap", RMeCX, XX, XX },
1135 { "bswap", RMeDX, XX, XX },
1136 { "bswap", RMeBX, XX, XX },
1137 { "bswap", RMeSP, XX, XX },
1138 { "bswap", RMeBP, XX, XX },
1139 { "bswap", RMeSI, XX, XX },
1140 { "bswap", RMeDI, XX, XX },
1141 /* d0 */
1142 { "(bad)", XX, XX, XX },
1143 { "psrlw", MX, EM, XX },
1144 { "psrld", MX, EM, XX },
1145 { "psrlq", MX, EM, XX },
1146 { "paddq", MX, EM, XX },
1147 { "pmullw", MX, EM, XX },
1148 { PREGRP21 },
1149 { "pmovmskb", Gd, MS, XX },
1150 /* d8 */
1151 { "psubusb", MX, EM, XX },
1152 { "psubusw", MX, EM, XX },
1153 { "pminub", MX, EM, XX },
1154 { "pand", MX, EM, XX },
1155 { "paddusb", MX, EM, XX },
1156 { "paddusw", MX, EM, XX },
1157 { "pmaxub", MX, EM, XX },
1158 { "pandn", MX, EM, XX },
1159 /* e0 */
1160 { "pavgb", MX, EM, XX },
1161 { "psraw", MX, EM, XX },
1162 { "psrad", MX, EM, XX },
1163 { "pavgw", MX, EM, XX },
1164 { "pmulhuw", MX, EM, XX },
1165 { "pmulhw", MX, EM, XX },
1166 { PREGRP15 },
1167 { PREGRP25 },
1168 /* e8 */
1169 { "psubsb", MX, EM, XX },
1170 { "psubsw", MX, EM, XX },
1171 { "pminsw", MX, EM, XX },
1172 { "por", MX, EM, XX },
1173 { "paddsb", MX, EM, XX },
1174 { "paddsw", MX, EM, XX },
1175 { "pmaxsw", MX, EM, XX },
1176 { "pxor", MX, EM, XX },
1177 /* f0 */
1178 { "(bad)", XX, XX, XX },
1179 { "psllw", MX, EM, XX },
1180 { "pslld", MX, EM, XX },
1181 { "psllq", MX, EM, XX },
1182 { "pmuludq", MX, EM, XX },
1183 { "pmaddwd", MX, EM, XX },
1184 { "psadbw", MX, EM, XX },
1185 { PREGRP18 },
1186 /* f8 */
1187 { "psubb", MX, EM, XX },
1188 { "psubw", MX, EM, XX },
1189 { "psubd", MX, EM, XX },
1190 { "psubq", MX, EM, XX },
1191 { "paddb", MX, EM, XX },
1192 { "paddw", MX, EM, XX },
1193 { "paddd", MX, EM, XX },
1194 { "(bad)", XX, XX, XX }
1195 };
1196
1197 static const unsigned char onebyte_has_modrm[256] = {
1198 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1199 /* ------------------------------- */
1200 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
1201 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
1202 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
1203 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
1204 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
1205 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
1206 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
1207 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
1208 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
1209 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
1210 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
1211 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
1212 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
1213 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
1214 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
1215 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
1216 /* ------------------------------- */
1217 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1218 };
1219
1220 static const unsigned char twobyte_has_modrm[256] = {
1221 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1222 /* ------------------------------- */
1223 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1224 /* 10 */ 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0, /* 1f */
1225 /* 20 */ 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1, /* 2f */
1226 /* 30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 3f */
1227 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1228 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1229 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1230 /* 70 */ 1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1, /* 7f */
1231 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1232 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1233 /* a0 */ 0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1, /* af */
1234 /* b0 */ 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1, /* bf */
1235 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1236 /* d0 */ 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1237 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1238 /* f0 */ 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1239 /* ------------------------------- */
1240 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1241 };
1242
1243 static const unsigned char twobyte_uses_SSE_prefix[256] = {
1244 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1245 /* ------------------------------- */
1246 /* 00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0f */
1247 /* 10 */ 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1f */
1248 /* 20 */ 0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0, /* 2f */
1249 /* 30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 3f */
1250 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 4f */
1251 /* 50 */ 0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* 5f */
1252 /* 60 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1, /* 6f */
1253 /* 70 */ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, /* 7f */
1254 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1255 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 9f */
1256 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* af */
1257 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* bf */
1258 /* c0 */ 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, /* cf */
1259 /* d0 */ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0, /* df */
1260 /* e0 */ 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0, /* ef */
1261 /* f0 */ 0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0 /* ff */
1262 /* ------------------------------- */
1263 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1264 };
1265
1266 static char obuf[100];
1267 static char *obufp;
1268 static char scratchbuf[100];
1269 static unsigned char *start_codep;
1270 static unsigned char *insn_codep;
1271 static unsigned char *codep;
1272 static disassemble_info *the_info;
1273 static int mod;
1274 static int rm;
1275 static int reg;
1276 static unsigned char need_modrm;
1277
1278 /* If we are accessing mod/rm/reg without need_modrm set, then the
1279 values are stale. Hitting this abort likely indicates that you
1280 need to update onebyte_has_modrm or twobyte_has_modrm. */
1281 #define MODRM_CHECK if (!need_modrm) abort ()
1282
1283 static const char **names64;
1284 static const char **names32;
1285 static const char **names16;
1286 static const char **names8;
1287 static const char **names8rex;
1288 static const char **names_seg;
1289 static const char **index16;
1290
1291 static const char *intel_names64[] = {
1292 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1293 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1294 };
1295 static const char *intel_names32[] = {
1296 "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
1297 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
1298 };
1299 static const char *intel_names16[] = {
1300 "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
1301 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
1302 };
1303 static const char *intel_names8[] = {
1304 "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
1305 };
1306 static const char *intel_names8rex[] = {
1307 "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
1308 "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b"
1309 };
1310 static const char *intel_names_seg[] = {
1311 "es", "cs", "ss", "ds", "fs", "gs", "?", "?",
1312 };
1313 static const char *intel_index16[] = {
1314 "bx+si", "bx+di", "bp+si", "bp+di", "si", "di", "bp", "bx"
1315 };
1316
1317 static const char *att_names64[] = {
1318 "%rax", "%rcx", "%rdx", "%rbx", "%rsp", "%rbp", "%rsi", "%rdi",
1319 "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"
1320 };
1321 static const char *att_names32[] = {
1322 "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi",
1323 "%r8d", "%r9d", "%r10d", "%r11d", "%r12d", "%r13d", "%r14d", "%r15d"
1324 };
1325 static const char *att_names16[] = {
1326 "%ax", "%cx", "%dx", "%bx", "%sp", "%bp", "%si", "%di",
1327 "%r8w", "%r9w", "%r10w", "%r11w", "%r12w", "%r13w", "%r14w", "%r15w"
1328 };
1329 static const char *att_names8[] = {
1330 "%al", "%cl", "%dl", "%bl", "%ah", "%ch", "%dh", "%bh",
1331 };
1332 static const char *att_names8rex[] = {
1333 "%al", "%cl", "%dl", "%bl", "%spl", "%bpl", "%sil", "%dil",
1334 "%r8b", "%r9b", "%r10b", "%r11b", "%r12b", "%r13b", "%r14b", "%r15b"
1335 };
1336 static const char *att_names_seg[] = {
1337 "%es", "%cs", "%ss", "%ds", "%fs", "%gs", "%?", "%?",
1338 };
1339 static const char *att_index16[] = {
1340 "%bx,%si", "%bx,%di", "%bp,%si", "%bp,%di", "%si", "%di", "%bp", "%bx"
1341 };
1342
1343 static const struct dis386 grps[][8] = {
1344 /* GRP1b */
1345 {
1346 { "addA", Eb, Ib, XX },
1347 { "orA", Eb, Ib, XX },
1348 { "adcA", Eb, Ib, XX },
1349 { "sbbA", Eb, Ib, XX },
1350 { "andA", Eb, Ib, XX },
1351 { "subA", Eb, Ib, XX },
1352 { "xorA", Eb, Ib, XX },
1353 { "cmpA", Eb, Ib, XX }
1354 },
1355 /* GRP1S */
1356 {
1357 { "addQ", Ev, Iv, XX },
1358 { "orQ", Ev, Iv, XX },
1359 { "adcQ", Ev, Iv, XX },
1360 { "sbbQ", Ev, Iv, XX },
1361 { "andQ", Ev, Iv, XX },
1362 { "subQ", Ev, Iv, XX },
1363 { "xorQ", Ev, Iv, XX },
1364 { "cmpQ", Ev, Iv, XX }
1365 },
1366 /* GRP1Ss */
1367 {
1368 { "addQ", Ev, sIb, XX },
1369 { "orQ", Ev, sIb, XX },
1370 { "adcQ", Ev, sIb, XX },
1371 { "sbbQ", Ev, sIb, XX },
1372 { "andQ", Ev, sIb, XX },
1373 { "subQ", Ev, sIb, XX },
1374 { "xorQ", Ev, sIb, XX },
1375 { "cmpQ", Ev, sIb, XX }
1376 },
1377 /* GRP2b */
1378 {
1379 { "rolA", Eb, Ib, XX },
1380 { "rorA", Eb, Ib, XX },
1381 { "rclA", Eb, Ib, XX },
1382 { "rcrA", Eb, Ib, XX },
1383 { "shlA", Eb, Ib, XX },
1384 { "shrA", Eb, Ib, XX },
1385 { "(bad)", XX, XX, XX },
1386 { "sarA", Eb, Ib, XX },
1387 },
1388 /* GRP2S */
1389 {
1390 { "rolQ", Ev, Ib, XX },
1391 { "rorQ", Ev, Ib, XX },
1392 { "rclQ", Ev, Ib, XX },
1393 { "rcrQ", Ev, Ib, XX },
1394 { "shlQ", Ev, Ib, XX },
1395 { "shrQ", Ev, Ib, XX },
1396 { "(bad)", XX, XX, XX },
1397 { "sarQ", Ev, Ib, XX },
1398 },
1399 /* GRP2b_one */
1400 {
1401 { "rolA", Eb, XX, XX },
1402 { "rorA", Eb, XX, XX },
1403 { "rclA", Eb, XX, XX },
1404 { "rcrA", Eb, XX, XX },
1405 { "shlA", Eb, XX, XX },
1406 { "shrA", Eb, XX, XX },
1407 { "(bad)", XX, XX, XX },
1408 { "sarA", Eb, XX, XX },
1409 },
1410 /* GRP2S_one */
1411 {
1412 { "rolQ", Ev, XX, XX },
1413 { "rorQ", Ev, XX, XX },
1414 { "rclQ", Ev, XX, XX },
1415 { "rcrQ", Ev, XX, XX },
1416 { "shlQ", Ev, XX, XX },
1417 { "shrQ", Ev, XX, XX },
1418 { "(bad)", XX, XX, XX},
1419 { "sarQ", Ev, XX, XX },
1420 },
1421 /* GRP2b_cl */
1422 {
1423 { "rolA", Eb, CL, XX },
1424 { "rorA", Eb, CL, XX },
1425 { "rclA", Eb, CL, XX },
1426 { "rcrA", Eb, CL, XX },
1427 { "shlA", Eb, CL, XX },
1428 { "shrA", Eb, CL, XX },
1429 { "(bad)", XX, XX, XX },
1430 { "sarA", Eb, CL, XX },
1431 },
1432 /* GRP2S_cl */
1433 {
1434 { "rolQ", Ev, CL, XX },
1435 { "rorQ", Ev, CL, XX },
1436 { "rclQ", Ev, CL, XX },
1437 { "rcrQ", Ev, CL, XX },
1438 { "shlQ", Ev, CL, XX },
1439 { "shrQ", Ev, CL, XX },
1440 { "(bad)", XX, XX, XX },
1441 { "sarQ", Ev, CL, XX }
1442 },
1443 /* GRP3b */
1444 {
1445 { "testA", Eb, Ib, XX },
1446 { "(bad)", Eb, XX, XX },
1447 { "notA", Eb, XX, XX },
1448 { "negA", Eb, XX, XX },
1449 { "mulA", Eb, XX, XX }, /* Don't print the implicit %al register, */
1450 { "imulA", Eb, XX, XX }, /* to distinguish these opcodes from other */
1451 { "divA", Eb, XX, XX }, /* mul/imul opcodes. Do the same for div */
1452 { "idivA", Eb, XX, XX } /* and idiv for consistency. */
1453 },
1454 /* GRP3S */
1455 {
1456 { "testQ", Ev, Iv, XX },
1457 { "(bad)", XX, XX, XX },
1458 { "notQ", Ev, XX, XX },
1459 { "negQ", Ev, XX, XX },
1460 { "mulQ", Ev, XX, XX }, /* Don't print the implicit register. */
1461 { "imulQ", Ev, XX, XX },
1462 { "divQ", Ev, XX, XX },
1463 { "idivQ", Ev, XX, XX },
1464 },
1465 /* GRP4 */
1466 {
1467 { "incA", Eb, XX, XX },
1468 { "decA", Eb, XX, XX },
1469 { "(bad)", XX, XX, XX },
1470 { "(bad)", XX, XX, XX },
1471 { "(bad)", XX, XX, XX },
1472 { "(bad)", XX, XX, XX },
1473 { "(bad)", XX, XX, XX },
1474 { "(bad)", XX, XX, XX },
1475 },
1476 /* GRP5 */
1477 {
1478 { "incQ", Ev, XX, XX },
1479 { "decQ", Ev, XX, XX },
1480 { "callT", indirEv, XX, XX },
1481 { "lcallT", indirEv, XX, XX },
1482 { "jmpT", indirEv, XX, XX },
1483 { "ljmpT", indirEv, XX, XX },
1484 { "pushU", Ev, XX, XX },
1485 { "(bad)", XX, XX, XX },
1486 },
1487 /* GRP6 */
1488 {
1489 { "sldtQ", Ev, XX, XX },
1490 { "strQ", Ev, XX, XX },
1491 { "lldt", Ew, XX, XX },
1492 { "ltr", Ew, XX, XX },
1493 { "verr", Ew, XX, XX },
1494 { "verw", Ew, XX, XX },
1495 { "(bad)", XX, XX, XX },
1496 { "(bad)", XX, XX, XX }
1497 },
1498 /* GRP7 */
1499 {
1500 { "sgdtQ", M, XX, XX },
1501 { "sidtQ", M, XX, XX },
1502 { "lgdtQ", M, XX, XX },
1503 { "lidtQ", M, XX, XX },
1504 { "smswQ", Ev, XX, XX },
1505 { "(bad)", XX, XX, XX },
1506 { "lmsw", Ew, XX, XX },
1507 { "invlpg", Ew, XX, XX },
1508 },
1509 /* GRP8 */
1510 {
1511 { "(bad)", XX, XX, XX },
1512 { "(bad)", XX, XX, XX },
1513 { "(bad)", XX, XX, XX },
1514 { "(bad)", XX, XX, XX },
1515 { "btQ", Ev, Ib, XX },
1516 { "btsQ", Ev, Ib, XX },
1517 { "btrQ", Ev, Ib, XX },
1518 { "btcQ", Ev, Ib, XX },
1519 },
1520 /* GRP9 */
1521 {
1522 { "(bad)", XX, XX, XX },
1523 { "cmpxchg8b", Ev, XX, XX },
1524 { "(bad)", XX, XX, XX },
1525 { "(bad)", XX, XX, XX },
1526 { "(bad)", XX, XX, XX },
1527 { "(bad)", XX, XX, XX },
1528 { "(bad)", XX, XX, XX },
1529 { "(bad)", XX, XX, XX },
1530 },
1531 /* GRP10 */
1532 {
1533 { "(bad)", XX, XX, XX },
1534 { "(bad)", XX, XX, XX },
1535 { "psrlw", MS, Ib, XX },
1536 { "(bad)", XX, XX, XX },
1537 { "psraw", MS, Ib, XX },
1538 { "(bad)", XX, XX, XX },
1539 { "psllw", MS, Ib, XX },
1540 { "(bad)", XX, XX, XX },
1541 },
1542 /* GRP11 */
1543 {
1544 { "(bad)", XX, XX, XX },
1545 { "(bad)", XX, XX, XX },
1546 { "psrld", MS, Ib, XX },
1547 { "(bad)", XX, XX, XX },
1548 { "psrad", MS, Ib, XX },
1549 { "(bad)", XX, XX, XX },
1550 { "pslld", MS, Ib, XX },
1551 { "(bad)", XX, XX, XX },
1552 },
1553 /* GRP12 */
1554 {
1555 { "(bad)", XX, XX, XX },
1556 { "(bad)", XX, XX, XX },
1557 { "psrlq", MS, Ib, XX },
1558 { "psrldq", MS, Ib, XX },
1559 { "(bad)", XX, XX, XX },
1560 { "(bad)", XX, XX, XX },
1561 { "psllq", MS, Ib, XX },
1562 { "pslldq", MS, Ib, XX },
1563 },
1564 /* GRP13 */
1565 {
1566 { "fxsave", Ev, XX, XX },
1567 { "fxrstor", Ev, XX, XX },
1568 { "ldmxcsr", Ev, XX, XX },
1569 { "stmxcsr", Ev, XX, XX },
1570 { "(bad)", XX, XX, XX },
1571 { "lfence", None, XX, XX },
1572 { "mfence", None, XX, XX },
1573 { "sfence", None, XX, XX },
1574 /* FIXME: the sfence with memory operand is clflush! */
1575 },
1576 /* GRP14 */
1577 {
1578 { "prefetchnta", Ev, XX, XX },
1579 { "prefetcht0", Ev, XX, XX },
1580 { "prefetcht1", Ev, XX, XX },
1581 { "prefetcht2", Ev, XX, XX },
1582 { "(bad)", XX, XX, XX },
1583 { "(bad)", XX, XX, XX },
1584 { "(bad)", XX, XX, XX },
1585 { "(bad)", XX, XX, XX },
1586 },
1587 /* GRPAMD */
1588 {
1589 { "prefetch", Eb, XX, XX },
1590 { "prefetchw", Eb, XX, XX },
1591 { "(bad)", XX, XX, XX },
1592 { "(bad)", XX, XX, XX },
1593 { "(bad)", XX, XX, XX },
1594 { "(bad)", XX, XX, XX },
1595 { "(bad)", XX, XX, XX },
1596 { "(bad)", XX, XX, XX },
1597 }
1598 };
1599
1600 static const struct dis386 prefix_user_table[][4] = {
1601 /* PREGRP0 */
1602 {
1603 { "addps", XM, EX, XX },
1604 { "addss", XM, EX, XX },
1605 { "addpd", XM, EX, XX },
1606 { "addsd", XM, EX, XX },
1607 },
1608 /* PREGRP1 */
1609 {
1610 { "", XM, EX, OPSIMD }, /* See OP_SIMD_SUFFIX. */
1611 { "", XM, EX, OPSIMD },
1612 { "", XM, EX, OPSIMD },
1613 { "", XM, EX, OPSIMD },
1614 },
1615 /* PREGRP2 */
1616 {
1617 { "cvtpi2ps", XM, EM, XX },
1618 { "cvtsi2ssY", XM, Ev, XX },
1619 { "cvtpi2pd", XM, EM, XX },
1620 { "cvtsi2sdY", XM, Ev, XX },
1621 },
1622 /* PREGRP3 */
1623 {
1624 { "cvtps2pi", MX, EX, XX },
1625 { "cvtss2siY", Gv, EX, XX },
1626 { "cvtpd2pi", MX, EX, XX },
1627 { "cvtsd2siY", Gv, EX, XX },
1628 },
1629 /* PREGRP4 */
1630 {
1631 { "cvttps2pi", MX, EX, XX },
1632 { "cvttss2siY", Gv, EX, XX },
1633 { "cvttpd2pi", MX, EX, XX },
1634 { "cvttsd2siY", Gv, EX, XX },
1635 },
1636 /* PREGRP5 */
1637 {
1638 { "divps", XM, EX, XX },
1639 { "divss", XM, EX, XX },
1640 { "divpd", XM, EX, XX },
1641 { "divsd", XM, EX, XX },
1642 },
1643 /* PREGRP6 */
1644 {
1645 { "maxps", XM, EX, XX },
1646 { "maxss", XM, EX, XX },
1647 { "maxpd", XM, EX, XX },
1648 { "maxsd", XM, EX, XX },
1649 },
1650 /* PREGRP7 */
1651 {
1652 { "minps", XM, EX, XX },
1653 { "minss", XM, EX, XX },
1654 { "minpd", XM, EX, XX },
1655 { "minsd", XM, EX, XX },
1656 },
1657 /* PREGRP8 */
1658 {
1659 { "movups", XM, EX, XX },
1660 { "movss", XM, EX, XX },
1661 { "movupd", XM, EX, XX },
1662 { "movsd", XM, EX, XX },
1663 },
1664 /* PREGRP9 */
1665 {
1666 { "movups", EX, XM, XX },
1667 { "movss", EX, XM, XX },
1668 { "movupd", EX, XM, XX },
1669 { "movsd", EX, XM, XX },
1670 },
1671 /* PREGRP10 */
1672 {
1673 { "mulps", XM, EX, XX },
1674 { "mulss", XM, EX, XX },
1675 { "mulpd", XM, EX, XX },
1676 { "mulsd", XM, EX, XX },
1677 },
1678 /* PREGRP11 */
1679 {
1680 { "rcpps", XM, EX, XX },
1681 { "rcpss", XM, EX, XX },
1682 { "(bad)", XM, EX, XX },
1683 { "(bad)", XM, EX, XX },
1684 },
1685 /* PREGRP12 */
1686 {
1687 { "rsqrtps", XM, EX, XX },
1688 { "rsqrtss", XM, EX, XX },
1689 { "(bad)", XM, EX, XX },
1690 { "(bad)", XM, EX, XX },
1691 },
1692 /* PREGRP13 */
1693 {
1694 { "sqrtps", XM, EX, XX },
1695 { "sqrtss", XM, EX, XX },
1696 { "sqrtpd", XM, EX, XX },
1697 { "sqrtsd", XM, EX, XX },
1698 },
1699 /* PREGRP14 */
1700 {
1701 { "subps", XM, EX, XX },
1702 { "subss", XM, EX, XX },
1703 { "subpd", XM, EX, XX },
1704 { "subsd", XM, EX, XX },
1705 },
1706 /* PREGRP15 */
1707 {
1708 { "(bad)", XM, EX, XX },
1709 { "cvtdq2pd", XM, EX, XX },
1710 { "cvttpd2dq", XM, EX, XX },
1711 { "cvtpd2dq", XM, EX, XX },
1712 },
1713 /* PREGRP16 */
1714 {
1715 { "cvtdq2ps", XM, EX, XX },
1716 { "cvttps2dq",XM, EX, XX },
1717 { "cvtps2dq",XM, EX, XX },
1718 { "(bad)", XM, EX, XX },
1719 },
1720 /* PREGRP17 */
1721 {
1722 { "cvtps2pd", XM, EX, XX },
1723 { "cvtss2sd", XM, EX, XX },
1724 { "cvtpd2ps", XM, EX, XX },
1725 { "cvtsd2ss", XM, EX, XX },
1726 },
1727 /* PREGRP18 */
1728 {
1729 { "maskmovq", MX, MS, XX },
1730 { "(bad)", XM, EX, XX },
1731 { "maskmovdqu", XM, EX, XX },
1732 { "(bad)", XM, EX, XX },
1733 },
1734 /* PREGRP19 */
1735 {
1736 { "movq", MX, EM, XX },
1737 { "movdqu", XM, EX, XX },
1738 { "movdqa", XM, EX, XX },
1739 { "(bad)", XM, EX, XX },
1740 },
1741 /* PREGRP20 */
1742 {
1743 { "movq", EM, MX, XX },
1744 { "movdqu", EX, XM, XX },
1745 { "movdqa", EX, XM, XX },
1746 { "(bad)", EX, XM, XX },
1747 },
1748 /* PREGRP21 */
1749 {
1750 { "(bad)", EX, XM, XX },
1751 { "movq2dq", XM, MS, XX },
1752 { "movq", EX, XM, XX },
1753 { "movdq2q", MX, XS, XX },
1754 },
1755 /* PREGRP22 */
1756 {
1757 { "pshufw", MX, EM, Ib },
1758 { "pshufhw", XM, EX, Ib },
1759 { "pshufd", XM, EX, Ib },
1760 { "pshuflw", XM, EX, Ib },
1761 },
1762 /* PREGRP23 */
1763 {
1764 { "movd", Ed, MX, XX },
1765 { "movq", XM, EX, XX },
1766 { "movd", Ed, XM, XX },
1767 { "(bad)", Ed, XM, XX },
1768 },
1769 /* PREGRP24 */
1770 {
1771 { "(bad)", MX, EX, XX },
1772 { "(bad)", XM, EX, XX },
1773 { "punpckhqdq", XM, EX, XX },
1774 { "(bad)", XM, EX, XX },
1775 },
1776 /* PREGRP25 */
1777 {
1778 { "movntq", Ev, MX, XX },
1779 { "(bad)", Ev, XM, XX },
1780 { "movntdq", Ev, XM, XX },
1781 { "(bad)", Ev, XM, XX },
1782 },
1783 /* PREGRP26 */
1784 {
1785 { "(bad)", MX, EX, XX },
1786 { "(bad)", XM, EX, XX },
1787 { "punpcklqdq", XM, EX, XX },
1788 { "(bad)", XM, EX, XX },
1789 },
1790 };
1791
1792 static const struct dis386 x86_64_table[][2] = {
1793 {
1794 { "arpl", Ew, Gw, XX },
1795 { "movs{||lq|xd}", Gv, Ed, XX },
1796 },
1797 };
1798
1799 #define INTERNAL_DISASSEMBLER_ERROR _("<internal disassembler error>")
1800
1801 static void
1802 ckprefix ()
1803 {
1804 int newrex;
1805 rex = 0;
1806 prefixes = 0;
1807 used_prefixes = 0;
1808 rex_used = 0;
1809 while (1)
1810 {
1811 FETCH_DATA (the_info, codep + 1);
1812 newrex = 0;
1813 switch (*codep)
1814 {
1815 /* REX prefixes family. */
1816 case 0x40:
1817 case 0x41:
1818 case 0x42:
1819 case 0x43:
1820 case 0x44:
1821 case 0x45:
1822 case 0x46:
1823 case 0x47:
1824 case 0x48:
1825 case 0x49:
1826 case 0x4a:
1827 case 0x4b:
1828 case 0x4c:
1829 case 0x4d:
1830 case 0x4e:
1831 case 0x4f:
1832 if (mode_64bit)
1833 newrex = *codep;
1834 else
1835 return;
1836 break;
1837 case 0xf3:
1838 prefixes |= PREFIX_REPZ;
1839 break;
1840 case 0xf2:
1841 prefixes |= PREFIX_REPNZ;
1842 break;
1843 case 0xf0:
1844 prefixes |= PREFIX_LOCK;
1845 break;
1846 case 0x2e:
1847 prefixes |= PREFIX_CS;
1848 break;
1849 case 0x36:
1850 prefixes |= PREFIX_SS;
1851 break;
1852 case 0x3e:
1853 prefixes |= PREFIX_DS;
1854 break;
1855 case 0x26:
1856 prefixes |= PREFIX_ES;
1857 break;
1858 case 0x64:
1859 prefixes |= PREFIX_FS;
1860 break;
1861 case 0x65:
1862 prefixes |= PREFIX_GS;
1863 break;
1864 case 0x66:
1865 prefixes |= PREFIX_DATA;
1866 break;
1867 case 0x67:
1868 prefixes |= PREFIX_ADDR;
1869 break;
1870 case FWAIT_OPCODE:
1871 /* fwait is really an instruction. If there are prefixes
1872 before the fwait, they belong to the fwait, *not* to the
1873 following instruction. */
1874 if (prefixes)
1875 {
1876 prefixes |= PREFIX_FWAIT;
1877 codep++;
1878 return;
1879 }
1880 prefixes = PREFIX_FWAIT;
1881 break;
1882 default:
1883 return;
1884 }
1885 /* Rex is ignored when followed by another prefix. */
1886 if (rex)
1887 {
1888 oappend (prefix_name (rex, 0));
1889 oappend (" ");
1890 }
1891 rex = newrex;
1892 codep++;
1893 }
1894 }
1895
1896 /* Return the name of the prefix byte PREF, or NULL if PREF is not a
1897 prefix byte. */
1898
1899 static const char *
1900 prefix_name (pref, sizeflag)
1901 int pref;
1902 int sizeflag;
1903 {
1904 switch (pref)
1905 {
1906 /* REX prefixes family. */
1907 case 0x40:
1908 return "rex";
1909 case 0x41:
1910 return "rexZ";
1911 case 0x42:
1912 return "rexY";
1913 case 0x43:
1914 return "rexYZ";
1915 case 0x44:
1916 return "rexX";
1917 case 0x45:
1918 return "rexXZ";
1919 case 0x46:
1920 return "rexXY";
1921 case 0x47:
1922 return "rexXYZ";
1923 case 0x48:
1924 return "rex64";
1925 case 0x49:
1926 return "rex64Z";
1927 case 0x4a:
1928 return "rex64Y";
1929 case 0x4b:
1930 return "rex64YZ";
1931 case 0x4c:
1932 return "rex64X";
1933 case 0x4d:
1934 return "rex64XZ";
1935 case 0x4e:
1936 return "rex64XY";
1937 case 0x4f:
1938 return "rex64XYZ";
1939 case 0xf3:
1940 return "repz";
1941 case 0xf2:
1942 return "repnz";
1943 case 0xf0:
1944 return "lock";
1945 case 0x2e:
1946 return "cs";
1947 case 0x36:
1948 return "ss";
1949 case 0x3e:
1950 return "ds";
1951 case 0x26:
1952 return "es";
1953 case 0x64:
1954 return "fs";
1955 case 0x65:
1956 return "gs";
1957 case 0x66:
1958 return (sizeflag & DFLAG) ? "data16" : "data32";
1959 case 0x67:
1960 if (mode_64bit)
1961 return (sizeflag & AFLAG) ? "addr32" : "addr64";
1962 else
1963 return ((sizeflag & AFLAG) && !mode_64bit) ? "addr16" : "addr32";
1964 case FWAIT_OPCODE:
1965 return "fwait";
1966 default:
1967 return NULL;
1968 }
1969 }
1970
1971 static char op1out[100], op2out[100], op3out[100];
1972 static int op_ad, op_index[3];
1973 static bfd_vma op_address[3];
1974 static bfd_vma op_riprel[3];
1975 static bfd_vma start_pc;
1976 \f
1977 /*
1978 * On the 386's of 1988, the maximum length of an instruction is 15 bytes.
1979 * (see topic "Redundant prefixes" in the "Differences from 8086"
1980 * section of the "Virtual 8086 Mode" chapter.)
1981 * 'pc' should be the address of this instruction, it will
1982 * be used to print the target address if this is a relative jump or call
1983 * The function returns the length of this instruction in bytes.
1984 */
1985
1986 static char intel_syntax;
1987 static char open_char;
1988 static char close_char;
1989 static char separator_char;
1990 static char scale_char;
1991
1992 /* Here for backwards compatibility. When gdb stops using
1993 print_insn_i386_att and print_insn_i386_intel these functions can
1994 disappear, and print_insn_i386 be merged into print_insn. */
1995 int
1996 print_insn_i386_att (pc, info)
1997 bfd_vma pc;
1998 disassemble_info *info;
1999 {
2000 intel_syntax = 0;
2001
2002 return print_insn (pc, info);
2003 }
2004
2005 int
2006 print_insn_i386_intel (pc, info)
2007 bfd_vma pc;
2008 disassemble_info *info;
2009 {
2010 intel_syntax = 1;
2011
2012 return print_insn (pc, info);
2013 }
2014
2015 int
2016 print_insn_i386 (pc, info)
2017 bfd_vma pc;
2018 disassemble_info *info;
2019 {
2020 intel_syntax = -1;
2021
2022 return print_insn (pc, info);
2023 }
2024
2025 static int
2026 print_insn (pc, info)
2027 bfd_vma pc;
2028 disassemble_info *info;
2029 {
2030 const struct dis386 *dp;
2031 int i;
2032 int two_source_ops;
2033 char *first, *second, *third;
2034 int needcomma;
2035 unsigned char uses_SSE_prefix;
2036 int sizeflag;
2037 /*const char *p;*/
2038 struct dis_private priv;
2039
2040 mode_64bit = (info->mach == bfd_mach_x86_64_intel_syntax
2041 || info->mach == bfd_mach_x86_64);
2042
2043 if (intel_syntax == -1)
2044 intel_syntax = (info->mach == bfd_mach_i386_i386_intel_syntax
2045 || info->mach == bfd_mach_x86_64_intel_syntax);
2046
2047 if (info->mach == bfd_mach_i386_i386
2048 || info->mach == bfd_mach_x86_64
2049 || info->mach == bfd_mach_i386_i386_intel_syntax
2050 || info->mach == bfd_mach_x86_64_intel_syntax)
2051 priv.orig_sizeflag = AFLAG | DFLAG;
2052 else if (info->mach == bfd_mach_i386_i8086)
2053 priv.orig_sizeflag = 0;
2054 else
2055 abort ();
2056
2057 #if 0
2058 for (p = info->disassembler_options; p != NULL; )
2059 {
2060 if (strncmp (p, "x86-64", 6) == 0)
2061 {
2062 mode_64bit = 1;
2063 priv.orig_sizeflag = AFLAG | DFLAG;
2064 }
2065 else if (strncmp (p, "i386", 4) == 0)
2066 {
2067 mode_64bit = 0;
2068 priv.orig_sizeflag = AFLAG | DFLAG;
2069 }
2070 else if (strncmp (p, "i8086", 5) == 0)
2071 {
2072 mode_64bit = 0;
2073 priv.orig_sizeflag = 0;
2074 }
2075 else if (strncmp (p, "intel", 5) == 0)
2076 {
2077 intel_syntax = 1;
2078 }
2079 else if (strncmp (p, "att", 3) == 0)
2080 {
2081 intel_syntax = 0;
2082 }
2083 else if (strncmp (p, "addr", 4) == 0)
2084 {
2085 if (p[4] == '1' && p[5] == '6')
2086 priv.orig_sizeflag &= ~AFLAG;
2087 else if (p[4] == '3' && p[5] == '2')
2088 priv.orig_sizeflag |= AFLAG;
2089 }
2090 else if (strncmp (p, "data", 4) == 0)
2091 {
2092 if (p[4] == '1' && p[5] == '6')
2093 priv.orig_sizeflag &= ~DFLAG;
2094 else if (p[4] == '3' && p[5] == '2')
2095 priv.orig_sizeflag |= DFLAG;
2096 }
2097 else if (strncmp (p, "suffix", 6) == 0)
2098 priv.orig_sizeflag |= SUFFIX_ALWAYS;
2099
2100 p = strchr (p, ',');
2101 if (p != NULL)
2102 p++;
2103 }
2104 #else
2105 mode_64bit = 1;
2106 priv.orig_sizeflag = AFLAG | DFLAG;
2107 intel_syntax = 1;
2108 #endif
2109
2110 if (intel_syntax)
2111 {
2112 names64 = intel_names64;
2113 names32 = intel_names32;
2114 names16 = intel_names16;
2115 names8 = intel_names8;
2116 names8rex = intel_names8rex;
2117 names_seg = intel_names_seg;
2118 index16 = intel_index16;
2119 open_char = '[';
2120 close_char = ']';
2121 separator_char = '+';
2122 scale_char = '*';
2123 }
2124 else
2125 {
2126 names64 = att_names64;
2127 names32 = att_names32;
2128 names16 = att_names16;
2129 names8 = att_names8;
2130 names8rex = att_names8rex;
2131 names_seg = att_names_seg;
2132 index16 = att_index16;
2133 open_char = '(';
2134 close_char = ')';
2135 separator_char = ',';
2136 scale_char = ',';
2137 }
2138
2139 /* The output looks better if we put 7 bytes on a line, since that
2140 puts most long word instructions on a single line. */
2141 info->bytes_per_line = 7;
2142
2143 info->private_data = (PTR) &priv;
2144 priv.max_fetched = priv.the_buffer;
2145 priv.insn_start = pc;
2146
2147 obuf[0] = 0;
2148 op1out[0] = 0;
2149 op2out[0] = 0;
2150 op3out[0] = 0;
2151
2152 op_index[0] = op_index[1] = op_index[2] = -1;
2153
2154 the_info = info;
2155 start_pc = pc;
2156 start_codep = priv.the_buffer;
2157 codep = priv.the_buffer;
2158
2159 if (_setjmp (priv.bailout) != 0)
2160 {
2161 const char *name;
2162
2163 /* Getting here means we tried for data but didn't get it. That
2164 means we have an incomplete instruction of some sort. Just
2165 print the first byte as a prefix or a .byte pseudo-op. */
2166 if (codep > priv.the_buffer)
2167 {
2168 name = prefix_name (priv.the_buffer[0], priv.orig_sizeflag);
2169 if (name != NULL)
2170 (*info->fprintf_func) (info->stream, "%s", name);
2171 else
2172 {
2173 /* Just print the first byte as a .byte instruction. */
2174 (*info->fprintf_func) (info->stream, ".byte 0x%x",
2175 (unsigned int) priv.the_buffer[0]);
2176 }
2177
2178 return 1;
2179 }
2180
2181 return -1;
2182 }
2183
2184 obufp = obuf;
2185 ckprefix ();
2186
2187 insn_codep = codep;
2188 sizeflag = priv.orig_sizeflag;
2189
2190 FETCH_DATA (info, codep + 1);
2191 two_source_ops = (*codep == 0x62) || (*codep == 0xc8);
2192
2193 if ((prefixes & PREFIX_FWAIT)
2194 && ((*codep < 0xd8) || (*codep > 0xdf)))
2195 {
2196 const char *name;
2197
2198 /* fwait not followed by floating point instruction. Print the
2199 first prefix, which is probably fwait itself. */
2200 name = prefix_name (priv.the_buffer[0], priv.orig_sizeflag);
2201 if (name == NULL)
2202 name = INTERNAL_DISASSEMBLER_ERROR;
2203 (*info->fprintf_func) (info->stream, "%s", name);
2204 return 1;
2205 }
2206
2207 if (*codep == 0x0f)
2208 {
2209 FETCH_DATA (info, codep + 2);
2210 dp = &dis386_twobyte[*++codep];
2211 need_modrm = twobyte_has_modrm[*codep];
2212 uses_SSE_prefix = twobyte_uses_SSE_prefix[*codep];
2213 }
2214 else
2215 {
2216 dp = &dis386[*codep];
2217 need_modrm = onebyte_has_modrm[*codep];
2218 uses_SSE_prefix = 0;
2219 }
2220 codep++;
2221
2222 if (!uses_SSE_prefix && (prefixes & PREFIX_REPZ))
2223 {
2224 oappend ("repz ");
2225 used_prefixes |= PREFIX_REPZ;
2226 }
2227 if (!uses_SSE_prefix && (prefixes & PREFIX_REPNZ))
2228 {
2229 oappend ("repnz ");
2230 used_prefixes |= PREFIX_REPNZ;
2231 }
2232 if (prefixes & PREFIX_LOCK)
2233 {
2234 oappend ("lock ");
2235 used_prefixes |= PREFIX_LOCK;
2236 }
2237
2238 if (prefixes & PREFIX_ADDR)
2239 {
2240 sizeflag ^= AFLAG;
2241 if (dp->bytemode3 != loop_jcxz_mode || intel_syntax)
2242 {
2243 if ((sizeflag & AFLAG) || mode_64bit)
2244 oappend ("addr32 ");
2245 else
2246 oappend ("addr16 ");
2247 used_prefixes |= PREFIX_ADDR;
2248 }
2249 }
2250
2251 if (!uses_SSE_prefix && (prefixes & PREFIX_DATA))
2252 {
2253 sizeflag ^= DFLAG;
2254 if (dp->bytemode3 == cond_jump_mode
2255 && dp->bytemode1 == v_mode
2256 && !intel_syntax)
2257 {
2258 if (sizeflag & DFLAG)
2259 oappend ("data32 ");
2260 else
2261 oappend ("data16 ");
2262 used_prefixes |= PREFIX_DATA;
2263 }
2264 }
2265
2266 if (need_modrm)
2267 {
2268 FETCH_DATA (info, codep + 1);
2269 mod = (*codep >> 6) & 3;
2270 reg = (*codep >> 3) & 7;
2271 rm = *codep & 7;
2272 }
2273
2274 if (dp->name == NULL && dp->bytemode1 == FLOATCODE)
2275 {
2276 dofloat (sizeflag);
2277 }
2278 else
2279 {
2280 int index;
2281 if (dp->name == NULL)
2282 {
2283 switch (dp->bytemode1)
2284 {
2285 case USE_GROUPS:
2286 dp = &grps[dp->bytemode2][reg];
2287 break;
2288
2289 case USE_PREFIX_USER_TABLE:
2290 index = 0;
2291 used_prefixes |= (prefixes & PREFIX_REPZ);
2292 if (prefixes & PREFIX_REPZ)
2293 index = 1;
2294 else
2295 {
2296 used_prefixes |= (prefixes & PREFIX_DATA);
2297 if (prefixes & PREFIX_DATA)
2298 index = 2;
2299 else
2300 {
2301 used_prefixes |= (prefixes & PREFIX_REPNZ);
2302 if (prefixes & PREFIX_REPNZ)
2303 index = 3;
2304 }
2305 }
2306 dp = &prefix_user_table[dp->bytemode2][index];
2307 break;
2308
2309 case X86_64_SPECIAL:
2310 dp = &x86_64_table[dp->bytemode2][mode_64bit];
2311 break;
2312
2313 default:
2314 oappend (INTERNAL_DISASSEMBLER_ERROR);
2315 break;
2316 }
2317 }
2318
2319 if (putop (dp->name, sizeflag) == 0)
2320 {
2321 obufp = op1out;
2322 op_ad = 2;
2323 if (dp->op1)
2324 (*dp->op1) (dp->bytemode1, sizeflag);
2325
2326 obufp = op2out;
2327 op_ad = 1;
2328 if (dp->op2)
2329 (*dp->op2) (dp->bytemode2, sizeflag);
2330
2331 obufp = op3out;
2332 op_ad = 0;
2333 if (dp->op3)
2334 (*dp->op3) (dp->bytemode3, sizeflag);
2335 }
2336 }
2337
2338 /* See if any prefixes were not used. If so, print the first one
2339 separately. If we don't do this, we'll wind up printing an
2340 instruction stream which does not precisely correspond to the
2341 bytes we are disassembling. */
2342 if ((prefixes & ~used_prefixes) != 0)
2343 {
2344 const char *name;
2345
2346 name = prefix_name (priv.the_buffer[0], priv.orig_sizeflag);
2347 if (name == NULL)
2348 name = INTERNAL_DISASSEMBLER_ERROR;
2349 (*info->fprintf_func) (info->stream, "%s", name);
2350 return 1;
2351 }
2352 if (rex & ~rex_used)
2353 {
2354 const char *name;
2355 name = prefix_name (rex | 0x40, priv.orig_sizeflag);
2356 if (name == NULL)
2357 name = INTERNAL_DISASSEMBLER_ERROR;
2358 (*info->fprintf_func) (info->stream, "%s ", name);
2359 }
2360
2361 obufp = obuf + strlen (obuf);
2362 for (i = strlen (obuf); i < 6; i++)
2363 oappend (" ");
2364 oappend (" ");
2365 (*info->fprintf_func) (info->stream, "%s", obuf);
2366
2367 /* The enter and bound instructions are printed with operands in the same
2368 order as the intel book; everything else is printed in reverse order. */
2369 if (intel_syntax || two_source_ops)
2370 {
2371 first = op1out;
2372 second = op2out;
2373 third = op3out;
2374 op_ad = op_index[0];
2375 op_index[0] = op_index[2];
2376 op_index[2] = op_ad;
2377 }
2378 else
2379 {
2380 first = op3out;
2381 second = op2out;
2382 third = op1out;
2383 }
2384 needcomma = 0;
2385 if (*first)
2386 {
2387 if (op_index[0] != -1 && !op_riprel[0])
2388 (*info->print_address_func) ((bfd_vma) op_address[op_index[0]], info);
2389 else
2390 (*info->fprintf_func) (info->stream, "%s", first);
2391 needcomma = 1;
2392 }
2393 if (*second)
2394 {
2395 if (needcomma)
2396 (*info->fprintf_func) (info->stream, ",");
2397 if (op_index[1] != -1 && !op_riprel[1])
2398 (*info->print_address_func) ((bfd_vma) op_address[op_index[1]], info);
2399 else
2400 (*info->fprintf_func) (info->stream, "%s", second);
2401 needcomma = 1;
2402 }
2403 if (*third)
2404 {
2405 if (needcomma)
2406 (*info->fprintf_func) (info->stream, ",");
2407 if (op_index[2] != -1 && !op_riprel[2])
2408 (*info->print_address_func) ((bfd_vma) op_address[op_index[2]], info);
2409 else
2410 (*info->fprintf_func) (info->stream, "%s", third);
2411 }
2412 for (i = 0; i < 3; i++)
2413 if (op_index[i] != -1 && op_riprel[i])
2414 {
2415 (*info->fprintf_func) (info->stream, " # ");
2416 (*info->print_address_func) ((bfd_vma) (start_pc + codep - start_codep
2417 + op_address[op_index[i]]), info);
2418 }
2419 return codep - priv.the_buffer;
2420 }
2421
2422 static const char *float_mem[] = {
2423 /* d8 */
2424 "fadd{s||s|}",
2425 "fmul{s||s|}",
2426 "fcom{s||s|}",
2427 "fcomp{s||s|}",
2428 "fsub{s||s|}",
2429 "fsubr{s||s|}",
2430 "fdiv{s||s|}",
2431 "fdivr{s||s|}",
2432 /* d9 */
2433 "fld{s||s|}",
2434 "(bad)",
2435 "fst{s||s|}",
2436 "fstp{s||s|}",
2437 "fldenv",
2438 "fldcw",
2439 "fNstenv",
2440 "fNstcw",
2441 /* da */
2442 "fiadd{l||l|}",
2443 "fimul{l||l|}",
2444 "ficom{l||l|}",
2445 "ficomp{l||l|}",
2446 "fisub{l||l|}",
2447 "fisubr{l||l|}",
2448 "fidiv{l||l|}",
2449 "fidivr{l||l|}",
2450 /* db */
2451 "fild{l||l|}",
2452 "(bad)",
2453 "fist{l||l|}",
2454 "fistp{l||l|}",
2455 "(bad)",
2456 "fld{t||t|}",
2457 "(bad)",
2458 "fstp{t||t|}",
2459 /* dc */
2460 "fadd{l||l|}",
2461 "fmul{l||l|}",
2462 "fcom{l||l|}",
2463 "fcomp{l||l|}",
2464 "fsub{l||l|}",
2465 "fsubr{l||l|}",
2466 "fdiv{l||l|}",
2467 "fdivr{l||l|}",
2468 /* dd */
2469 "fld{l||l|}",
2470 "(bad)",
2471 "fst{l||l|}",
2472 "fstp{l||l|}",
2473 "frstor",
2474 "(bad)",
2475 "fNsave",
2476 "fNstsw",
2477 /* de */
2478 "fiadd",
2479 "fimul",
2480 "ficom",
2481 "ficomp",
2482 "fisub",
2483 "fisubr",
2484 "fidiv",
2485 "fidivr",
2486 /* df */
2487 "fild",
2488 "(bad)",
2489 "fist",
2490 "fistp",
2491 "fbld",
2492 "fild{ll||ll|}",
2493 "fbstp",
2494 "fistpll",
2495 };
2496
2497 #define ST OP_ST, 0
2498 #define STi OP_STi, 0
2499
2500 #define FGRPd9_2 NULL, NULL, 0, NULL, 0, NULL, 0
2501 #define FGRPd9_4 NULL, NULL, 1, NULL, 0, NULL, 0
2502 #define FGRPd9_5 NULL, NULL, 2, NULL, 0, NULL, 0
2503 #define FGRPd9_6 NULL, NULL, 3, NULL, 0, NULL, 0
2504 #define FGRPd9_7 NULL, NULL, 4, NULL, 0, NULL, 0
2505 #define FGRPda_5 NULL, NULL, 5, NULL, 0, NULL, 0
2506 #define FGRPdb_4 NULL, NULL, 6, NULL, 0, NULL, 0
2507 #define FGRPde_3 NULL, NULL, 7, NULL, 0, NULL, 0
2508 #define FGRPdf_4 NULL, NULL, 8, NULL, 0, NULL, 0
2509
2510 static const struct dis386 float_reg[][8] = {
2511 /* d8 */
2512 {
2513 { "fadd", ST, STi, XX },
2514 { "fmul", ST, STi, XX },
2515 { "fcom", STi, XX, XX },
2516 { "fcomp", STi, XX, XX },
2517 { "fsub", ST, STi, XX },
2518 { "fsubr", ST, STi, XX },
2519 { "fdiv", ST, STi, XX },
2520 { "fdivr", ST, STi, XX },
2521 },
2522 /* d9 */
2523 {
2524 { "fld", STi, XX, XX },
2525 { "fxch", STi, XX, XX },
2526 { FGRPd9_2 },
2527 { "(bad)", XX, XX, XX },
2528 { FGRPd9_4 },
2529 { FGRPd9_5 },
2530 { FGRPd9_6 },
2531 { FGRPd9_7 },
2532 },
2533 /* da */
2534 {
2535 { "fcmovb", ST, STi, XX },
2536 { "fcmove", ST, STi, XX },
2537 { "fcmovbe",ST, STi, XX },
2538 { "fcmovu", ST, STi, XX },
2539 { "(bad)", XX, XX, XX },
2540 { FGRPda_5 },
2541 { "(bad)", XX, XX, XX },
2542 { "(bad)", XX, XX, XX },
2543 },
2544 /* db */
2545 {
2546 { "fcmovnb",ST, STi, XX },
2547 { "fcmovne",ST, STi, XX },
2548 { "fcmovnbe",ST, STi, XX },
2549 { "fcmovnu",ST, STi, XX },
2550 { FGRPdb_4 },
2551 { "fucomi", ST, STi, XX },
2552 { "fcomi", ST, STi, XX },
2553 { "(bad)", XX, XX, XX },
2554 },
2555 /* dc */
2556 {
2557 { "fadd", STi, ST, XX },
2558 { "fmul", STi, ST, XX },
2559 { "(bad)", XX, XX, XX },
2560 { "(bad)", XX, XX, XX },
2561 #if UNIXWARE_COMPAT
2562 { "fsub", STi, ST, XX },
2563 { "fsubr", STi, ST, XX },
2564 { "fdiv", STi, ST, XX },
2565 { "fdivr", STi, ST, XX },
2566 #else
2567 { "fsubr", STi, ST, XX },
2568 { "fsub", STi, ST, XX },
2569 { "fdivr", STi, ST, XX },
2570 { "fdiv", STi, ST, XX },
2571 #endif
2572 },
2573 /* dd */
2574 {
2575 { "ffree", STi, XX, XX },
2576 { "(bad)", XX, XX, XX },
2577 { "fst", STi, XX, XX },
2578 { "fstp", STi, XX, XX },
2579 { "fucom", STi, XX, XX },
2580 { "fucomp", STi, XX, XX },
2581 { "(bad)", XX, XX, XX },
2582 { "(bad)", XX, XX, XX },
2583 },
2584 /* de */
2585 {
2586 { "faddp", STi, ST, XX },
2587 { "fmulp", STi, ST, XX },
2588 { "(bad)", XX, XX, XX },
2589 { FGRPde_3 },
2590 #if UNIXWARE_COMPAT
2591 { "fsubp", STi, ST, XX },
2592 { "fsubrp", STi, ST, XX },
2593 { "fdivp", STi, ST, XX },
2594 { "fdivrp", STi, ST, XX },
2595 #else
2596 { "fsubrp", STi, ST, XX },
2597 { "fsubp", STi, ST, XX },
2598 { "fdivrp", STi, ST, XX },
2599 { "fdivp", STi, ST, XX },
2600 #endif
2601 },
2602 /* df */
2603 {
2604 { "ffreep", STi, XX, XX },
2605 { "(bad)", XX, XX, XX },
2606 { "(bad)", XX, XX, XX },
2607 { "(bad)", XX, XX, XX },
2608 { FGRPdf_4 },
2609 { "fucomip",ST, STi, XX },
2610 { "fcomip", ST, STi, XX },
2611 { "(bad)", XX, XX, XX },
2612 },
2613 };
2614
2615 static char *fgrps[][8] = {
2616 /* d9_2 0 */
2617 {
2618 "fnop","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)",
2619 },
2620
2621 /* d9_4 1 */
2622 {
2623 "fchs","fabs","(bad)","(bad)","ftst","fxam","(bad)","(bad)",
2624 },
2625
2626 /* d9_5 2 */
2627 {
2628 "fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz","(bad)",
2629 },
2630
2631 /* d9_6 3 */
2632 {
2633 "f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp",
2634 },
2635
2636 /* d9_7 4 */
2637 {
2638 "fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos",
2639 },
2640
2641 /* da_5 5 */
2642 {
2643 "(bad)","fucompp","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)",
2644 },
2645
2646 /* db_4 6 */
2647 {
2648 "feni(287 only)","fdisi(287 only)","fNclex","fNinit",
2649 "fNsetpm(287 only)","(bad)","(bad)","(bad)",
2650 },
2651
2652 /* de_3 7 */
2653 {
2654 "(bad)","fcompp","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)",
2655 },
2656
2657 /* df_4 8 */
2658 {
2659 "fNstsw","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)","(bad)",
2660 },
2661 };
2662
2663 static void
2664 dofloat (sizeflag)
2665 int sizeflag;
2666 {
2667 const struct dis386 *dp;
2668 unsigned char floatop;
2669
2670 floatop = codep[-1];
2671
2672 if (mod != 3)
2673 {
2674 putop (float_mem[(floatop - 0xd8) * 8 + reg], sizeflag);
2675 obufp = op1out;
2676 if (floatop == 0xdb)
2677 OP_E (x_mode, sizeflag);
2678 else if (floatop == 0xdd)
2679 OP_E (d_mode, sizeflag);
2680 else
2681 OP_E (v_mode, sizeflag);
2682 return;
2683 }
2684 /* Skip mod/rm byte. */
2685 MODRM_CHECK;
2686 codep++;
2687
2688 dp = &float_reg[floatop - 0xd8][reg];
2689 if (dp->name == NULL)
2690 {
2691 putop (fgrps[dp->bytemode1][rm], sizeflag);
2692
2693 /* Instruction fnstsw is only one with strange arg. */
2694 if (floatop == 0xdf && codep[-1] == 0xe0)
2695 strcpy (op1out, names16[0]);
2696 }
2697 else
2698 {
2699 putop (dp->name, sizeflag);
2700
2701 obufp = op1out;
2702 if (dp->op1)
2703 (*dp->op1) (dp->bytemode1, sizeflag);
2704 obufp = op2out;
2705 if (dp->op2)
2706 (*dp->op2) (dp->bytemode2, sizeflag);
2707 }
2708 }
2709
2710 static void
2711 OP_ST (bytemode, sizeflag)
2712 int bytemode ATTRIBUTE_UNUSED;
2713 int sizeflag ATTRIBUTE_UNUSED;
2714 {
2715 oappend ("%st");
2716 }
2717
2718 static void
2719 OP_STi (bytemode, sizeflag)
2720 int bytemode ATTRIBUTE_UNUSED;
2721 int sizeflag ATTRIBUTE_UNUSED;
2722 {
2723 sprintf (scratchbuf, "%%st(%d)", rm);
2724 oappend (scratchbuf + intel_syntax);
2725 }
2726
2727 /* Capital letters in template are macros. */
2728 static int
2729 putop (template, sizeflag)
2730 const char *template;
2731 int sizeflag;
2732 {
2733 const char *p;
2734 int alt;
2735
2736 for (p = template; *p; p++)
2737 {
2738 switch (*p)
2739 {
2740 default:
2741 *obufp++ = *p;
2742 break;
2743 case '{':
2744 alt = 0;
2745 if (intel_syntax)
2746 alt += 1;
2747 if (mode_64bit)
2748 alt += 2;
2749 while (alt != 0)
2750 {
2751 while (*++p != '|')
2752 {
2753 if (*p == '}')
2754 {
2755 /* Alternative not valid. */
2756 strcpy (obuf, "(bad)");
2757 obufp = obuf + 5;
2758 return 1;
2759 }
2760 else if (*p == '\0')
2761 abort ();
2762 }
2763 alt--;
2764 }
2765 break;
2766 case '|':
2767 while (*++p != '}')
2768 {
2769 if (*p == '\0')
2770 abort ();
2771 }
2772 break;
2773 case '}':
2774 break;
2775 case 'A':
2776 if (intel_syntax)
2777 break;
2778 if (mod != 3 || (sizeflag & SUFFIX_ALWAYS))
2779 *obufp++ = 'b';
2780 break;
2781 case 'B':
2782 if (intel_syntax)
2783 break;
2784 if (sizeflag & SUFFIX_ALWAYS)
2785 *obufp++ = 'b';
2786 break;
2787 case 'E': /* For jcxz/jecxz */
2788 if (mode_64bit)
2789 {
2790 if (sizeflag & AFLAG)
2791 *obufp++ = 'r';
2792 else
2793 *obufp++ = 'e';
2794 }
2795 else
2796 if (sizeflag & AFLAG)
2797 *obufp++ = 'e';
2798 used_prefixes |= (prefixes & PREFIX_ADDR);
2799 break;
2800 case 'F':
2801 if (intel_syntax)
2802 break;
2803 if ((prefixes & PREFIX_ADDR) || (sizeflag & SUFFIX_ALWAYS))
2804 {
2805 if (sizeflag & AFLAG)
2806 *obufp++ = mode_64bit ? 'q' : 'l';
2807 else
2808 *obufp++ = mode_64bit ? 'l' : 'w';
2809 used_prefixes |= (prefixes & PREFIX_ADDR);
2810 }
2811 break;
2812 case 'H':
2813 if (intel_syntax)
2814 break;
2815 if ((prefixes & (PREFIX_CS | PREFIX_DS)) == PREFIX_CS
2816 || (prefixes & (PREFIX_CS | PREFIX_DS)) == PREFIX_DS)
2817 {
2818 used_prefixes |= prefixes & (PREFIX_CS | PREFIX_DS);
2819 *obufp++ = ',';
2820 *obufp++ = 'p';
2821 if (prefixes & PREFIX_DS)
2822 *obufp++ = 't';
2823 else
2824 *obufp++ = 'n';
2825 }
2826 break;
2827 case 'L':
2828 if (intel_syntax)
2829 break;
2830 if (sizeflag & SUFFIX_ALWAYS)
2831 *obufp++ = 'l';
2832 break;
2833 case 'N':
2834 if ((prefixes & PREFIX_FWAIT) == 0)
2835 *obufp++ = 'n';
2836 else
2837 used_prefixes |= PREFIX_FWAIT;
2838 break;
2839 case 'O':
2840 USED_REX (REX_MODE64);
2841 if (rex & REX_MODE64)
2842 *obufp++ = 'o';
2843 else
2844 *obufp++ = 'd';
2845 break;
2846 case 'T':
2847 if (intel_syntax)
2848 break;
2849 if (mode_64bit)
2850 {
2851 *obufp++ = 'q';
2852 break;
2853 }
2854 /* Fall through. */
2855 case 'P':
2856 if (intel_syntax)
2857 break;
2858 if ((prefixes & PREFIX_DATA)
2859 || (rex & REX_MODE64)
2860 || (sizeflag & SUFFIX_ALWAYS))
2861 {
2862 USED_REX (REX_MODE64);
2863 if (rex & REX_MODE64)
2864 *obufp++ = 'q';
2865 else
2866 {
2867 if (sizeflag & DFLAG)
2868 *obufp++ = 'l';
2869 else
2870 *obufp++ = 'w';
2871 used_prefixes |= (prefixes & PREFIX_DATA);
2872 }
2873 }
2874 break;
2875 case 'U':
2876 if (intel_syntax)
2877 break;
2878 if (mode_64bit)
2879 {
2880 *obufp++ = 'q';
2881 break;
2882 }
2883 /* Fall through. */
2884 case 'Q':
2885 if (intel_syntax)
2886 break;
2887 USED_REX (REX_MODE64);
2888 if (mod != 3 || (sizeflag & SUFFIX_ALWAYS))
2889 {
2890 if (rex & REX_MODE64)
2891 *obufp++ = 'q';
2892 else
2893 {
2894 if (sizeflag & DFLAG)
2895 *obufp++ = 'l';
2896 else
2897 *obufp++ = 'w';
2898 used_prefixes |= (prefixes & PREFIX_DATA);
2899 }
2900 }
2901 break;
2902 case 'R':
2903 USED_REX (REX_MODE64);
2904 if (intel_syntax)
2905 {
2906 if (rex & REX_MODE64)
2907 {
2908 *obufp++ = 'q';
2909 *obufp++ = 't';
2910 }
2911 else if (sizeflag & DFLAG)
2912 {
2913 *obufp++ = 'd';
2914 *obufp++ = 'q';
2915 }
2916 else
2917 {
2918 *obufp++ = 'w';