Add target description for avx-avx512.
[external/binutils.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2
3    Copyright (C) 1988-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "opcode/i386.h"
22 #include "arch-utils.h"
23 #include "command.h"
24 #include "dummy-frame.h"
25 #include "dwarf2-frame.h"
26 #include "doublest.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "inferior.h"
31 #include "infrun.h"
32 #include "gdbcmd.h"
33 #include "gdbcore.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "osabi.h"
37 #include "regcache.h"
38 #include "reggroups.h"
39 #include "regset.h"
40 #include "symfile.h"
41 #include "symtab.h"
42 #include "target.h"
43 #include "value.h"
44 #include "dis-asm.h"
45 #include "disasm.h"
46 #include "remote.h"
47 #include "i386-tdep.h"
48 #include "i387-tdep.h"
49 #include "x86-xstate.h"
50
51 #include "record.h"
52 #include "record-full.h"
53 #include "features/i386/i386.c"
54 #include "features/i386/i386-avx.c"
55 #include "features/i386/i386-mpx.c"
56 #include "features/i386/i386-avx-mpx.c"
57 #include "features/i386/i386-avx-avx512.c"
58 #include "features/i386/i386-avx-mpx-avx512.c"
59 #include "features/i386/i386-mmx.c"
60
61 #include "ax.h"
62 #include "ax-gdb.h"
63
64 #include "stap-probe.h"
65 #include "user-regs.h"
66 #include "cli/cli-utils.h"
67 #include "expression.h"
68 #include "parser-defs.h"
69 #include <ctype.h>
70 #include <algorithm>
71
72 /* Register names.  */
73
74 static const char *i386_register_names[] =
75 {
76   "eax",   "ecx",    "edx",   "ebx",
77   "esp",   "ebp",    "esi",   "edi",
78   "eip",   "eflags", "cs",    "ss",
79   "ds",    "es",     "fs",    "gs",
80   "st0",   "st1",    "st2",   "st3",
81   "st4",   "st5",    "st6",   "st7",
82   "fctrl", "fstat",  "ftag",  "fiseg",
83   "fioff", "foseg",  "fooff", "fop",
84   "xmm0",  "xmm1",   "xmm2",  "xmm3",
85   "xmm4",  "xmm5",   "xmm6",  "xmm7",
86   "mxcsr"
87 };
88
89 static const char *i386_zmm_names[] =
90 {
91   "zmm0",  "zmm1",   "zmm2",  "zmm3",
92   "zmm4",  "zmm5",   "zmm6",  "zmm7"
93 };
94
95 static const char *i386_zmmh_names[] =
96 {
97   "zmm0h",  "zmm1h",   "zmm2h",  "zmm3h",
98   "zmm4h",  "zmm5h",   "zmm6h",  "zmm7h"
99 };
100
101 static const char *i386_k_names[] =
102 {
103   "k0",  "k1",   "k2",  "k3",
104   "k4",  "k5",   "k6",  "k7"
105 };
106
107 static const char *i386_ymm_names[] =
108 {
109   "ymm0",  "ymm1",   "ymm2",  "ymm3",
110   "ymm4",  "ymm5",   "ymm6",  "ymm7",
111 };
112
113 static const char *i386_ymmh_names[] =
114 {
115   "ymm0h",  "ymm1h",   "ymm2h",  "ymm3h",
116   "ymm4h",  "ymm5h",   "ymm6h",  "ymm7h",
117 };
118
119 static const char *i386_mpx_names[] =
120 {
121   "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
122 };
123
124 /* Register names for MPX pseudo-registers.  */
125
126 static const char *i386_bnd_names[] =
127 {
128   "bnd0", "bnd1", "bnd2", "bnd3"
129 };
130
131 /* Register names for MMX pseudo-registers.  */
132
133 static const char *i386_mmx_names[] =
134 {
135   "mm0", "mm1", "mm2", "mm3",
136   "mm4", "mm5", "mm6", "mm7"
137 };
138
139 /* Register names for byte pseudo-registers.  */
140
141 static const char *i386_byte_names[] =
142 {
143   "al", "cl", "dl", "bl", 
144   "ah", "ch", "dh", "bh"
145 };
146
147 /* Register names for word pseudo-registers.  */
148
149 static const char *i386_word_names[] =
150 {
151   "ax", "cx", "dx", "bx",
152   "", "bp", "si", "di"
153 };
154
155 /* Constant used for reading/writing pseudo registers.  In 64-bit mode, we have
156    16 lower ZMM regs that extend corresponding xmm/ymm registers.  In addition,
157    we have 16 upper ZMM regs that have to be handled differently.  */
158
159 const int num_lower_zmm_regs = 16;
160
161 /* MMX register?  */
162
163 static int
164 i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
165 {
166   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
167   int mm0_regnum = tdep->mm0_regnum;
168
169   if (mm0_regnum < 0)
170     return 0;
171
172   regnum -= mm0_regnum;
173   return regnum >= 0 && regnum < tdep->num_mmx_regs;
174 }
175
176 /* Byte register?  */
177
178 int
179 i386_byte_regnum_p (struct gdbarch *gdbarch, int regnum)
180 {
181   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
182
183   regnum -= tdep->al_regnum;
184   return regnum >= 0 && regnum < tdep->num_byte_regs;
185 }
186
187 /* Word register?  */
188
189 int
190 i386_word_regnum_p (struct gdbarch *gdbarch, int regnum)
191 {
192   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
193
194   regnum -= tdep->ax_regnum;
195   return regnum >= 0 && regnum < tdep->num_word_regs;
196 }
197
198 /* Dword register?  */
199
200 int
201 i386_dword_regnum_p (struct gdbarch *gdbarch, int regnum)
202 {
203   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
204   int eax_regnum = tdep->eax_regnum;
205
206   if (eax_regnum < 0)
207     return 0;
208
209   regnum -= eax_regnum;
210   return regnum >= 0 && regnum < tdep->num_dword_regs;
211 }
212
213 /* AVX512 register?  */
214
215 int
216 i386_zmmh_regnum_p (struct gdbarch *gdbarch, int regnum)
217 {
218   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
219   int zmm0h_regnum = tdep->zmm0h_regnum;
220
221   if (zmm0h_regnum < 0)
222     return 0;
223
224   regnum -= zmm0h_regnum;
225   return regnum >= 0 && regnum < tdep->num_zmm_regs;
226 }
227
228 int
229 i386_zmm_regnum_p (struct gdbarch *gdbarch, int regnum)
230 {
231   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
232   int zmm0_regnum = tdep->zmm0_regnum;
233
234   if (zmm0_regnum < 0)
235     return 0;
236
237   regnum -= zmm0_regnum;
238   return regnum >= 0 && regnum < tdep->num_zmm_regs;
239 }
240
241 int
242 i386_k_regnum_p (struct gdbarch *gdbarch, int regnum)
243 {
244   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
245   int k0_regnum = tdep->k0_regnum;
246
247   if (k0_regnum < 0)
248     return 0;
249
250   regnum -= k0_regnum;
251   return regnum >= 0 && regnum < I387_NUM_K_REGS;
252 }
253
254 static int
255 i386_ymmh_regnum_p (struct gdbarch *gdbarch, int regnum)
256 {
257   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
258   int ymm0h_regnum = tdep->ymm0h_regnum;
259
260   if (ymm0h_regnum < 0)
261     return 0;
262
263   regnum -= ymm0h_regnum;
264   return regnum >= 0 && regnum < tdep->num_ymm_regs;
265 }
266
267 /* AVX register?  */
268
269 int
270 i386_ymm_regnum_p (struct gdbarch *gdbarch, int regnum)
271 {
272   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
273   int ymm0_regnum = tdep->ymm0_regnum;
274
275   if (ymm0_regnum < 0)
276     return 0;
277
278   regnum -= ymm0_regnum;
279   return regnum >= 0 && regnum < tdep->num_ymm_regs;
280 }
281
282 static int
283 i386_ymmh_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
284 {
285   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
286   int ymm16h_regnum = tdep->ymm16h_regnum;
287
288   if (ymm16h_regnum < 0)
289     return 0;
290
291   regnum -= ymm16h_regnum;
292   return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
293 }
294
295 int
296 i386_ymm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
297 {
298   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
299   int ymm16_regnum = tdep->ymm16_regnum;
300
301   if (ymm16_regnum < 0)
302     return 0;
303
304   regnum -= ymm16_regnum;
305   return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
306 }
307
308 /* BND register?  */
309
310 int
311 i386_bnd_regnum_p (struct gdbarch *gdbarch, int regnum)
312 {
313   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
314   int bnd0_regnum = tdep->bnd0_regnum;
315
316   if (bnd0_regnum < 0)
317     return 0;
318
319   regnum -= bnd0_regnum;
320   return regnum >= 0 && regnum < I387_NUM_BND_REGS;
321 }
322
323 /* SSE register?  */
324
325 int
326 i386_xmm_regnum_p (struct gdbarch *gdbarch, int regnum)
327 {
328   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
329   int num_xmm_regs = I387_NUM_XMM_REGS (tdep);
330
331   if (num_xmm_regs == 0)
332     return 0;
333
334   regnum -= I387_XMM0_REGNUM (tdep);
335   return regnum >= 0 && regnum < num_xmm_regs;
336 }
337
338 /* XMM_512 register?  */
339
340 int
341 i386_xmm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
342 {
343   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
344   int num_xmm_avx512_regs = I387_NUM_XMM_AVX512_REGS (tdep);
345
346   if (num_xmm_avx512_regs == 0)
347     return 0;
348
349   regnum -= I387_XMM16_REGNUM (tdep);
350   return regnum >= 0 && regnum < num_xmm_avx512_regs;
351 }
352
353 static int
354 i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
355 {
356   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
357
358   if (I387_NUM_XMM_REGS (tdep) == 0)
359     return 0;
360
361   return (regnum == I387_MXCSR_REGNUM (tdep));
362 }
363
364 /* FP register?  */
365
366 int
367 i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
368 {
369   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
370
371   if (I387_ST0_REGNUM (tdep) < 0)
372     return 0;
373
374   return (I387_ST0_REGNUM (tdep) <= regnum
375           && regnum < I387_FCTRL_REGNUM (tdep));
376 }
377
378 int
379 i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
380 {
381   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
382
383   if (I387_ST0_REGNUM (tdep) < 0)
384     return 0;
385
386   return (I387_FCTRL_REGNUM (tdep) <= regnum 
387           && regnum < I387_XMM0_REGNUM (tdep));
388 }
389
390 /* BNDr (raw) register?  */
391
392 static int
393 i386_bndr_regnum_p (struct gdbarch *gdbarch, int regnum)
394 {
395   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
396
397    if (I387_BND0R_REGNUM (tdep) < 0)
398      return 0;
399
400   regnum -= tdep->bnd0r_regnum;
401   return regnum >= 0 && regnum < I387_NUM_BND_REGS;
402 }
403
404 /* BND control register?  */
405
406 static int
407 i386_mpx_ctrl_regnum_p (struct gdbarch *gdbarch, int regnum)
408 {
409   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
410
411    if (I387_BNDCFGU_REGNUM (tdep) < 0)
412      return 0;
413
414   regnum -= I387_BNDCFGU_REGNUM (tdep);
415   return regnum >= 0 && regnum < I387_NUM_MPX_CTRL_REGS;
416 }
417
418 /* Return the name of register REGNUM, or the empty string if it is
419    an anonymous register.  */
420
421 static const char *
422 i386_register_name (struct gdbarch *gdbarch, int regnum)
423 {
424   /* Hide the upper YMM registers.  */
425   if (i386_ymmh_regnum_p (gdbarch, regnum))
426     return "";
427
428   /* Hide the upper YMM16-31 registers.  */
429   if (i386_ymmh_avx512_regnum_p (gdbarch, regnum))
430     return "";
431
432   /* Hide the upper ZMM registers.  */
433   if (i386_zmmh_regnum_p (gdbarch, regnum))
434     return "";
435
436   return tdesc_register_name (gdbarch, regnum);
437 }
438
439 /* Return the name of register REGNUM.  */
440
441 const char *
442 i386_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
443 {
444   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
445   if (i386_bnd_regnum_p (gdbarch, regnum))
446     return i386_bnd_names[regnum - tdep->bnd0_regnum];
447   if (i386_mmx_regnum_p (gdbarch, regnum))
448     return i386_mmx_names[regnum - I387_MM0_REGNUM (tdep)];
449   else if (i386_ymm_regnum_p (gdbarch, regnum))
450     return i386_ymm_names[regnum - tdep->ymm0_regnum];
451   else if (i386_zmm_regnum_p (gdbarch, regnum))
452     return i386_zmm_names[regnum - tdep->zmm0_regnum];
453   else if (i386_byte_regnum_p (gdbarch, regnum))
454     return i386_byte_names[regnum - tdep->al_regnum];
455   else if (i386_word_regnum_p (gdbarch, regnum))
456     return i386_word_names[regnum - tdep->ax_regnum];
457
458   internal_error (__FILE__, __LINE__, _("invalid regnum"));
459 }
460
461 /* Convert a dbx register number REG to the appropriate register
462    number used by GDB.  */
463
464 static int
465 i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
466 {
467   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
468
469   /* This implements what GCC calls the "default" register map
470      (dbx_register_map[]).  */
471
472   if (reg >= 0 && reg <= 7)
473     {
474       /* General-purpose registers.  The debug info calls %ebp
475          register 4, and %esp register 5.  */
476       if (reg == 4)
477         return 5;
478       else if (reg == 5)
479         return 4;
480       else return reg;
481     }
482   else if (reg >= 12 && reg <= 19)
483     {
484       /* Floating-point registers.  */
485       return reg - 12 + I387_ST0_REGNUM (tdep);
486     }
487   else if (reg >= 21 && reg <= 28)
488     {
489       /* SSE registers.  */
490       int ymm0_regnum = tdep->ymm0_regnum;
491
492       if (ymm0_regnum >= 0
493           && i386_xmm_regnum_p (gdbarch, reg))
494         return reg - 21 + ymm0_regnum;
495       else
496         return reg - 21 + I387_XMM0_REGNUM (tdep);
497     }
498   else if (reg >= 29 && reg <= 36)
499     {
500       /* MMX registers.  */
501       return reg - 29 + I387_MM0_REGNUM (tdep);
502     }
503
504   /* This will hopefully provoke a warning.  */
505   return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
506 }
507
508 /* Convert SVR4 DWARF register number REG to the appropriate register number
509    used by GDB.  */
510
511 static int
512 i386_svr4_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
513 {
514   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
515
516   /* This implements the GCC register map that tries to be compatible
517      with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]).  */
518
519   /* The SVR4 register numbering includes %eip and %eflags, and
520      numbers the floating point registers differently.  */
521   if (reg >= 0 && reg <= 9)
522     {
523       /* General-purpose registers.  */
524       return reg;
525     }
526   else if (reg >= 11 && reg <= 18)
527     {
528       /* Floating-point registers.  */
529       return reg - 11 + I387_ST0_REGNUM (tdep);
530     }
531   else if (reg >= 21 && reg <= 36)
532     {
533       /* The SSE and MMX registers have the same numbers as with dbx.  */
534       return i386_dbx_reg_to_regnum (gdbarch, reg);
535     }
536
537   switch (reg)
538     {
539     case 37: return I387_FCTRL_REGNUM (tdep);
540     case 38: return I387_FSTAT_REGNUM (tdep);
541     case 39: return I387_MXCSR_REGNUM (tdep);
542     case 40: return I386_ES_REGNUM;
543     case 41: return I386_CS_REGNUM;
544     case 42: return I386_SS_REGNUM;
545     case 43: return I386_DS_REGNUM;
546     case 44: return I386_FS_REGNUM;
547     case 45: return I386_GS_REGNUM;
548     }
549
550   return -1;
551 }
552
553 /* Wrapper on i386_svr4_dwarf_reg_to_regnum to return
554    num_regs + num_pseudo_regs for other debug formats.  */
555
556 static int
557 i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
558 {
559   int regnum = i386_svr4_dwarf_reg_to_regnum (gdbarch, reg);
560
561   if (regnum == -1)
562     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
563   return regnum;
564 }
565
566 \f
567
568 /* This is the variable that is set with "set disassembly-flavor", and
569    its legitimate values.  */
570 static const char att_flavor[] = "att";
571 static const char intel_flavor[] = "intel";
572 static const char *const valid_flavors[] =
573 {
574   att_flavor,
575   intel_flavor,
576   NULL
577 };
578 static const char *disassembly_flavor = att_flavor;
579 \f
580
581 /* Use the program counter to determine the contents and size of a
582    breakpoint instruction.  Return a pointer to a string of bytes that
583    encode a breakpoint instruction, store the length of the string in
584    *LEN and optionally adjust *PC to point to the correct memory
585    location for inserting the breakpoint.
586
587    On the i386 we have a single breakpoint that fits in a single byte
588    and can be inserted anywhere.
589
590    This function is 64-bit safe.  */
591
592 constexpr gdb_byte i386_break_insn[] = { 0xcc }; /* int 3 */
593
594 typedef BP_MANIPULATION (i386_break_insn) i386_breakpoint;
595
596 \f
597 /* Displaced instruction handling.  */
598
599 /* Skip the legacy instruction prefixes in INSN.
600    Not all prefixes are valid for any particular insn
601    but we needn't care, the insn will fault if it's invalid.
602    The result is a pointer to the first opcode byte,
603    or NULL if we run off the end of the buffer.  */
604
605 static gdb_byte *
606 i386_skip_prefixes (gdb_byte *insn, size_t max_len)
607 {
608   gdb_byte *end = insn + max_len;
609
610   while (insn < end)
611     {
612       switch (*insn)
613         {
614         case DATA_PREFIX_OPCODE:
615         case ADDR_PREFIX_OPCODE:
616         case CS_PREFIX_OPCODE:
617         case DS_PREFIX_OPCODE:
618         case ES_PREFIX_OPCODE:
619         case FS_PREFIX_OPCODE:
620         case GS_PREFIX_OPCODE:
621         case SS_PREFIX_OPCODE:
622         case LOCK_PREFIX_OPCODE:
623         case REPE_PREFIX_OPCODE:
624         case REPNE_PREFIX_OPCODE:
625           ++insn;
626           continue;
627         default:
628           return insn;
629         }
630     }
631
632   return NULL;
633 }
634
635 static int
636 i386_absolute_jmp_p (const gdb_byte *insn)
637 {
638   /* jmp far (absolute address in operand).  */
639   if (insn[0] == 0xea)
640     return 1;
641
642   if (insn[0] == 0xff)
643     {
644       /* jump near, absolute indirect (/4).  */
645       if ((insn[1] & 0x38) == 0x20)
646         return 1;
647
648       /* jump far, absolute indirect (/5).  */
649       if ((insn[1] & 0x38) == 0x28)
650         return 1;
651     }
652
653   return 0;
654 }
655
656 /* Return non-zero if INSN is a jump, zero otherwise.  */
657
658 static int
659 i386_jmp_p (const gdb_byte *insn)
660 {
661   /* jump short, relative.  */
662   if (insn[0] == 0xeb)
663     return 1;
664
665   /* jump near, relative.  */
666   if (insn[0] == 0xe9)
667     return 1;
668
669   return i386_absolute_jmp_p (insn);
670 }
671
672 static int
673 i386_absolute_call_p (const gdb_byte *insn)
674 {
675   /* call far, absolute.  */
676   if (insn[0] == 0x9a)
677     return 1;
678
679   if (insn[0] == 0xff)
680     {
681       /* Call near, absolute indirect (/2).  */
682       if ((insn[1] & 0x38) == 0x10)
683         return 1;
684
685       /* Call far, absolute indirect (/3).  */
686       if ((insn[1] & 0x38) == 0x18)
687         return 1;
688     }
689
690   return 0;
691 }
692
693 static int
694 i386_ret_p (const gdb_byte *insn)
695 {
696   switch (insn[0])
697     {
698     case 0xc2: /* ret near, pop N bytes.  */
699     case 0xc3: /* ret near */
700     case 0xca: /* ret far, pop N bytes.  */
701     case 0xcb: /* ret far */
702     case 0xcf: /* iret */
703       return 1;
704
705     default:
706       return 0;
707     }
708 }
709
710 static int
711 i386_call_p (const gdb_byte *insn)
712 {
713   if (i386_absolute_call_p (insn))
714     return 1;
715
716   /* call near, relative.  */
717   if (insn[0] == 0xe8)
718     return 1;
719
720   return 0;
721 }
722
723 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
724    length in bytes.  Otherwise, return zero.  */
725
726 static int
727 i386_syscall_p (const gdb_byte *insn, int *lengthp)
728 {
729   /* Is it 'int $0x80'?  */
730   if ((insn[0] == 0xcd && insn[1] == 0x80)
731       /* Or is it 'sysenter'?  */
732       || (insn[0] == 0x0f && insn[1] == 0x34)
733       /* Or is it 'syscall'?  */
734       || (insn[0] == 0x0f && insn[1] == 0x05))
735     {
736       *lengthp = 2;
737       return 1;
738     }
739
740   return 0;
741 }
742
743 /* The gdbarch insn_is_call method.  */
744
745 static int
746 i386_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
747 {
748   gdb_byte buf[I386_MAX_INSN_LEN], *insn;
749
750   read_code (addr, buf, I386_MAX_INSN_LEN);
751   insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
752
753   return i386_call_p (insn);
754 }
755
756 /* The gdbarch insn_is_ret method.  */
757
758 static int
759 i386_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
760 {
761   gdb_byte buf[I386_MAX_INSN_LEN], *insn;
762
763   read_code (addr, buf, I386_MAX_INSN_LEN);
764   insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
765
766   return i386_ret_p (insn);
767 }
768
769 /* The gdbarch insn_is_jump method.  */
770
771 static int
772 i386_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
773 {
774   gdb_byte buf[I386_MAX_INSN_LEN], *insn;
775
776   read_code (addr, buf, I386_MAX_INSN_LEN);
777   insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
778
779   return i386_jmp_p (insn);
780 }
781
782 /* Some kernels may run one past a syscall insn, so we have to cope.
783    Otherwise this is just simple_displaced_step_copy_insn.  */
784
785 struct displaced_step_closure *
786 i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
787                                CORE_ADDR from, CORE_ADDR to,
788                                struct regcache *regs)
789 {
790   size_t len = gdbarch_max_insn_length (gdbarch);
791   gdb_byte *buf = (gdb_byte *) xmalloc (len);
792
793   read_memory (from, buf, len);
794
795   /* GDB may get control back after the insn after the syscall.
796      Presumably this is a kernel bug.
797      If this is a syscall, make sure there's a nop afterwards.  */
798   {
799     int syscall_length;
800     gdb_byte *insn;
801
802     insn = i386_skip_prefixes (buf, len);
803     if (insn != NULL && i386_syscall_p (insn, &syscall_length))
804       insn[syscall_length] = NOP_OPCODE;
805   }
806
807   write_memory (to, buf, len);
808
809   if (debug_displaced)
810     {
811       fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
812                           paddress (gdbarch, from), paddress (gdbarch, to));
813       displaced_step_dump_bytes (gdb_stdlog, buf, len);
814     }
815
816   return (struct displaced_step_closure *) buf;
817 }
818
819 /* Fix up the state of registers and memory after having single-stepped
820    a displaced instruction.  */
821
822 void
823 i386_displaced_step_fixup (struct gdbarch *gdbarch,
824                            struct displaced_step_closure *closure,
825                            CORE_ADDR from, CORE_ADDR to,
826                            struct regcache *regs)
827 {
828   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
829
830   /* The offset we applied to the instruction's address.
831      This could well be negative (when viewed as a signed 32-bit
832      value), but ULONGEST won't reflect that, so take care when
833      applying it.  */
834   ULONGEST insn_offset = to - from;
835
836   /* Since we use simple_displaced_step_copy_insn, our closure is a
837      copy of the instruction.  */
838   gdb_byte *insn = (gdb_byte *) closure;
839   /* The start of the insn, needed in case we see some prefixes.  */
840   gdb_byte *insn_start = insn;
841
842   if (debug_displaced)
843     fprintf_unfiltered (gdb_stdlog,
844                         "displaced: fixup (%s, %s), "
845                         "insn = 0x%02x 0x%02x ...\n",
846                         paddress (gdbarch, from), paddress (gdbarch, to),
847                         insn[0], insn[1]);
848
849   /* The list of issues to contend with here is taken from
850      resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
851      Yay for Free Software!  */
852
853   /* Relocate the %eip, if necessary.  */
854
855   /* The instruction recognizers we use assume any leading prefixes
856      have been skipped.  */
857   {
858     /* This is the size of the buffer in closure.  */
859     size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
860     gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
861     /* If there are too many prefixes, just ignore the insn.
862        It will fault when run.  */
863     if (opcode != NULL)
864       insn = opcode;
865   }
866
867   /* Except in the case of absolute or indirect jump or call
868      instructions, or a return instruction, the new eip is relative to
869      the displaced instruction; make it relative.  Well, signal
870      handler returns don't need relocation either, but we use the
871      value of %eip to recognize those; see below.  */
872   if (! i386_absolute_jmp_p (insn)
873       && ! i386_absolute_call_p (insn)
874       && ! i386_ret_p (insn))
875     {
876       ULONGEST orig_eip;
877       int insn_len;
878
879       regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
880
881       /* A signal trampoline system call changes the %eip, resuming
882          execution of the main program after the signal handler has
883          returned.  That makes them like 'return' instructions; we
884          shouldn't relocate %eip.
885
886          But most system calls don't, and we do need to relocate %eip.
887
888          Our heuristic for distinguishing these cases: if stepping
889          over the system call instruction left control directly after
890          the instruction, the we relocate --- control almost certainly
891          doesn't belong in the displaced copy.  Otherwise, we assume
892          the instruction has put control where it belongs, and leave
893          it unrelocated.  Goodness help us if there are PC-relative
894          system calls.  */
895       if (i386_syscall_p (insn, &insn_len)
896           && orig_eip != to + (insn - insn_start) + insn_len
897           /* GDB can get control back after the insn after the syscall.
898              Presumably this is a kernel bug.
899              i386_displaced_step_copy_insn ensures its a nop,
900              we add one to the length for it.  */
901           && orig_eip != to + (insn - insn_start) + insn_len + 1)
902         {
903           if (debug_displaced)
904             fprintf_unfiltered (gdb_stdlog,
905                                 "displaced: syscall changed %%eip; "
906                                 "not relocating\n");
907         }
908       else
909         {
910           ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
911
912           /* If we just stepped over a breakpoint insn, we don't backup
913              the pc on purpose; this is to match behaviour without
914              stepping.  */
915
916           regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
917
918           if (debug_displaced)
919             fprintf_unfiltered (gdb_stdlog,
920                                 "displaced: "
921                                 "relocated %%eip from %s to %s\n",
922                                 paddress (gdbarch, orig_eip),
923                                 paddress (gdbarch, eip));
924         }
925     }
926
927   /* If the instruction was PUSHFL, then the TF bit will be set in the
928      pushed value, and should be cleared.  We'll leave this for later,
929      since GDB already messes up the TF flag when stepping over a
930      pushfl.  */
931
932   /* If the instruction was a call, the return address now atop the
933      stack is the address following the copied instruction.  We need
934      to make it the address following the original instruction.  */
935   if (i386_call_p (insn))
936     {
937       ULONGEST esp;
938       ULONGEST retaddr;
939       const ULONGEST retaddr_len = 4;
940
941       regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
942       retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
943       retaddr = (retaddr - insn_offset) & 0xffffffffUL;
944       write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
945
946       if (debug_displaced)
947         fprintf_unfiltered (gdb_stdlog,
948                             "displaced: relocated return addr at %s to %s\n",
949                             paddress (gdbarch, esp),
950                             paddress (gdbarch, retaddr));
951     }
952 }
953
954 static void
955 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
956 {
957   target_write_memory (*to, buf, len);
958   *to += len;
959 }
960
961 static void
962 i386_relocate_instruction (struct gdbarch *gdbarch,
963                            CORE_ADDR *to, CORE_ADDR oldloc)
964 {
965   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
966   gdb_byte buf[I386_MAX_INSN_LEN];
967   int offset = 0, rel32, newrel;
968   int insn_length;
969   gdb_byte *insn = buf;
970
971   read_memory (oldloc, buf, I386_MAX_INSN_LEN);
972
973   insn_length = gdb_buffered_insn_length (gdbarch, insn,
974                                           I386_MAX_INSN_LEN, oldloc);
975
976   /* Get past the prefixes.  */
977   insn = i386_skip_prefixes (insn, I386_MAX_INSN_LEN);
978
979   /* Adjust calls with 32-bit relative addresses as push/jump, with
980      the address pushed being the location where the original call in
981      the user program would return to.  */
982   if (insn[0] == 0xe8)
983     {
984       gdb_byte push_buf[16];
985       unsigned int ret_addr;
986
987       /* Where "ret" in the original code will return to.  */
988       ret_addr = oldloc + insn_length;
989       push_buf[0] = 0x68; /* pushq $...  */
990       store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
991       /* Push the push.  */
992       append_insns (to, 5, push_buf);
993
994       /* Convert the relative call to a relative jump.  */
995       insn[0] = 0xe9;
996
997       /* Adjust the destination offset.  */
998       rel32 = extract_signed_integer (insn + 1, 4, byte_order);
999       newrel = (oldloc - *to) + rel32;
1000       store_signed_integer (insn + 1, 4, byte_order, newrel);
1001
1002       if (debug_displaced)
1003         fprintf_unfiltered (gdb_stdlog,
1004                             "Adjusted insn rel32=%s at %s to"
1005                             " rel32=%s at %s\n",
1006                             hex_string (rel32), paddress (gdbarch, oldloc),
1007                             hex_string (newrel), paddress (gdbarch, *to));
1008
1009       /* Write the adjusted jump into its displaced location.  */
1010       append_insns (to, 5, insn);
1011       return;
1012     }
1013
1014   /* Adjust jumps with 32-bit relative addresses.  Calls are already
1015      handled above.  */
1016   if (insn[0] == 0xe9)
1017     offset = 1;
1018   /* Adjust conditional jumps.  */
1019   else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1020     offset = 2;
1021
1022   if (offset)
1023     {
1024       rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1025       newrel = (oldloc - *to) + rel32;
1026       store_signed_integer (insn + offset, 4, byte_order, newrel);
1027       if (debug_displaced)
1028         fprintf_unfiltered (gdb_stdlog,
1029                             "Adjusted insn rel32=%s at %s to"
1030                             " rel32=%s at %s\n",
1031                             hex_string (rel32), paddress (gdbarch, oldloc),
1032                             hex_string (newrel), paddress (gdbarch, *to));
1033     }
1034
1035   /* Write the adjusted instructions into their displaced
1036      location.  */
1037   append_insns (to, insn_length, buf);
1038 }
1039
1040 \f
1041 #ifdef I386_REGNO_TO_SYMMETRY
1042 #error "The Sequent Symmetry is no longer supported."
1043 #endif
1044
1045 /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
1046    and %esp "belong" to the calling function.  Therefore these
1047    registers should be saved if they're going to be modified.  */
1048
1049 /* The maximum number of saved registers.  This should include all
1050    registers mentioned above, and %eip.  */
1051 #define I386_NUM_SAVED_REGS     I386_NUM_GREGS
1052
1053 struct i386_frame_cache
1054 {
1055   /* Base address.  */
1056   CORE_ADDR base;
1057   int base_p;
1058   LONGEST sp_offset;
1059   CORE_ADDR pc;
1060
1061   /* Saved registers.  */
1062   CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
1063   CORE_ADDR saved_sp;
1064   int saved_sp_reg;
1065   int pc_in_eax;
1066
1067   /* Stack space reserved for local variables.  */
1068   long locals;
1069 };
1070
1071 /* Allocate and initialize a frame cache.  */
1072
1073 static struct i386_frame_cache *
1074 i386_alloc_frame_cache (void)
1075 {
1076   struct i386_frame_cache *cache;
1077   int i;
1078
1079   cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
1080
1081   /* Base address.  */
1082   cache->base_p = 0;
1083   cache->base = 0;
1084   cache->sp_offset = -4;
1085   cache->pc = 0;
1086
1087   /* Saved registers.  We initialize these to -1 since zero is a valid
1088      offset (that's where %ebp is supposed to be stored).  */
1089   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
1090     cache->saved_regs[i] = -1;
1091   cache->saved_sp = 0;
1092   cache->saved_sp_reg = -1;
1093   cache->pc_in_eax = 0;
1094
1095   /* Frameless until proven otherwise.  */
1096   cache->locals = -1;
1097
1098   return cache;
1099 }
1100
1101 /* If the instruction at PC is a jump, return the address of its
1102    target.  Otherwise, return PC.  */
1103
1104 static CORE_ADDR
1105 i386_follow_jump (struct gdbarch *gdbarch, CORE_ADDR pc)
1106 {
1107   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1108   gdb_byte op;
1109   long delta = 0;
1110   int data16 = 0;
1111
1112   if (target_read_code (pc, &op, 1))
1113     return pc;
1114
1115   if (op == 0x66)
1116     {
1117       data16 = 1;
1118
1119       op = read_code_unsigned_integer (pc + 1, 1, byte_order);
1120     }
1121
1122   switch (op)
1123     {
1124     case 0xe9:
1125       /* Relative jump: if data16 == 0, disp32, else disp16.  */
1126       if (data16)
1127         {
1128           delta = read_memory_integer (pc + 2, 2, byte_order);
1129
1130           /* Include the size of the jmp instruction (including the
1131              0x66 prefix).  */
1132           delta += 4;
1133         }
1134       else
1135         {
1136           delta = read_memory_integer (pc + 1, 4, byte_order);
1137
1138           /* Include the size of the jmp instruction.  */
1139           delta += 5;
1140         }
1141       break;
1142     case 0xeb:
1143       /* Relative jump, disp8 (ignore data16).  */
1144       delta = read_memory_integer (pc + data16 + 1, 1, byte_order);
1145
1146       delta += data16 + 2;
1147       break;
1148     }
1149
1150   return pc + delta;
1151 }
1152
1153 /* Check whether PC points at a prologue for a function returning a
1154    structure or union.  If so, it updates CACHE and returns the
1155    address of the first instruction after the code sequence that
1156    removes the "hidden" argument from the stack or CURRENT_PC,
1157    whichever is smaller.  Otherwise, return PC.  */
1158
1159 static CORE_ADDR
1160 i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
1161                             struct i386_frame_cache *cache)
1162 {
1163   /* Functions that return a structure or union start with:
1164
1165         popl %eax             0x58
1166         xchgl %eax, (%esp)    0x87 0x04 0x24
1167      or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
1168
1169      (the System V compiler puts out the second `xchg' instruction,
1170      and the assembler doesn't try to optimize it, so the 'sib' form
1171      gets generated).  This sequence is used to get the address of the
1172      return buffer for a function that returns a structure.  */
1173   static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
1174   static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
1175   gdb_byte buf[4];
1176   gdb_byte op;
1177
1178   if (current_pc <= pc)
1179     return pc;
1180
1181   if (target_read_code (pc, &op, 1))
1182     return pc;
1183
1184   if (op != 0x58)               /* popl %eax */
1185     return pc;
1186
1187   if (target_read_code (pc + 1, buf, 4))
1188     return pc;
1189
1190   if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
1191     return pc;
1192
1193   if (current_pc == pc)
1194     {
1195       cache->sp_offset += 4;
1196       return current_pc;
1197     }
1198
1199   if (current_pc == pc + 1)
1200     {
1201       cache->pc_in_eax = 1;
1202       return current_pc;
1203     }
1204   
1205   if (buf[1] == proto1[1])
1206     return pc + 4;
1207   else
1208     return pc + 5;
1209 }
1210
1211 static CORE_ADDR
1212 i386_skip_probe (CORE_ADDR pc)
1213 {
1214   /* A function may start with
1215
1216         pushl constant
1217         call _probe
1218         addl $4, %esp
1219            
1220      followed by
1221
1222         pushl %ebp
1223
1224      etc.  */
1225   gdb_byte buf[8];
1226   gdb_byte op;
1227
1228   if (target_read_code (pc, &op, 1))
1229     return pc;
1230
1231   if (op == 0x68 || op == 0x6a)
1232     {
1233       int delta;
1234
1235       /* Skip past the `pushl' instruction; it has either a one-byte or a
1236          four-byte operand, depending on the opcode.  */
1237       if (op == 0x68)
1238         delta = 5;
1239       else
1240         delta = 2;
1241
1242       /* Read the following 8 bytes, which should be `call _probe' (6
1243          bytes) followed by `addl $4,%esp' (2 bytes).  */
1244       read_memory (pc + delta, buf, sizeof (buf));
1245       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
1246         pc += delta + sizeof (buf);
1247     }
1248
1249   return pc;
1250 }
1251
1252 /* GCC 4.1 and later, can put code in the prologue to realign the
1253    stack pointer.  Check whether PC points to such code, and update
1254    CACHE accordingly.  Return the first instruction after the code
1255    sequence or CURRENT_PC, whichever is smaller.  If we don't
1256    recognize the code, return PC.  */
1257
1258 static CORE_ADDR
1259 i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1260                           struct i386_frame_cache *cache)
1261 {
1262   /* There are 2 code sequences to re-align stack before the frame
1263      gets set up:
1264
1265         1. Use a caller-saved saved register:
1266
1267                 leal  4(%esp), %reg
1268                 andl  $-XXX, %esp
1269                 pushl -4(%reg)
1270
1271         2. Use a callee-saved saved register:
1272
1273                 pushl %reg
1274                 leal  8(%esp), %reg
1275                 andl  $-XXX, %esp
1276                 pushl -4(%reg)
1277
1278      "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
1279      
1280         0x83 0xe4 0xf0                  andl $-16, %esp
1281         0x81 0xe4 0x00 0xff 0xff 0xff   andl $-256, %esp
1282    */
1283
1284   gdb_byte buf[14];
1285   int reg;
1286   int offset, offset_and;
1287   static int regnums[8] = {
1288     I386_EAX_REGNUM,            /* %eax */
1289     I386_ECX_REGNUM,            /* %ecx */
1290     I386_EDX_REGNUM,            /* %edx */
1291     I386_EBX_REGNUM,            /* %ebx */
1292     I386_ESP_REGNUM,            /* %esp */
1293     I386_EBP_REGNUM,            /* %ebp */
1294     I386_ESI_REGNUM,            /* %esi */
1295     I386_EDI_REGNUM             /* %edi */
1296   };
1297
1298   if (target_read_code (pc, buf, sizeof buf))
1299     return pc;
1300
1301   /* Check caller-saved saved register.  The first instruction has
1302      to be "leal 4(%esp), %reg".  */
1303   if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
1304     {
1305       /* MOD must be binary 10 and R/M must be binary 100.  */
1306       if ((buf[1] & 0xc7) != 0x44)
1307         return pc;
1308
1309       /* REG has register number.  */
1310       reg = (buf[1] >> 3) & 7;
1311       offset = 4;
1312     }
1313   else
1314     {
1315       /* Check callee-saved saved register.  The first instruction
1316          has to be "pushl %reg".  */
1317       if ((buf[0] & 0xf8) != 0x50)
1318         return pc;
1319
1320       /* Get register.  */
1321       reg = buf[0] & 0x7;
1322
1323       /* The next instruction has to be "leal 8(%esp), %reg".  */
1324       if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
1325         return pc;
1326
1327       /* MOD must be binary 10 and R/M must be binary 100.  */
1328       if ((buf[2] & 0xc7) != 0x44)
1329         return pc;
1330       
1331       /* REG has register number.  Registers in pushl and leal have to
1332          be the same.  */
1333       if (reg != ((buf[2] >> 3) & 7))
1334         return pc;
1335
1336       offset = 5;
1337     }
1338
1339   /* Rigister can't be %esp nor %ebp.  */
1340   if (reg == 4 || reg == 5)
1341     return pc;
1342
1343   /* The next instruction has to be "andl $-XXX, %esp".  */
1344   if (buf[offset + 1] != 0xe4
1345       || (buf[offset] != 0x81 && buf[offset] != 0x83))
1346     return pc;
1347
1348   offset_and = offset;
1349   offset += buf[offset] == 0x81 ? 6 : 3;
1350
1351   /* The next instruction has to be "pushl -4(%reg)".  8bit -4 is
1352      0xfc.  REG must be binary 110 and MOD must be binary 01.  */
1353   if (buf[offset] != 0xff
1354       || buf[offset + 2] != 0xfc
1355       || (buf[offset + 1] & 0xf8) != 0x70)
1356     return pc;
1357
1358   /* R/M has register.  Registers in leal and pushl have to be the
1359      same.  */
1360   if (reg != (buf[offset + 1] & 7))
1361     return pc;
1362
1363   if (current_pc > pc + offset_and)
1364     cache->saved_sp_reg = regnums[reg];
1365
1366   return std::min (pc + offset + 3, current_pc);
1367 }
1368
1369 /* Maximum instruction length we need to handle.  */
1370 #define I386_MAX_MATCHED_INSN_LEN       6
1371
1372 /* Instruction description.  */
1373 struct i386_insn
1374 {
1375   size_t len;
1376   gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
1377   gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
1378 };
1379
1380 /* Return whether instruction at PC matches PATTERN.  */
1381
1382 static int
1383 i386_match_pattern (CORE_ADDR pc, struct i386_insn pattern)
1384 {
1385   gdb_byte op;
1386
1387   if (target_read_code (pc, &op, 1))
1388     return 0;
1389
1390   if ((op & pattern.mask[0]) == pattern.insn[0])
1391     {
1392       gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
1393       int insn_matched = 1;
1394       size_t i;
1395
1396       gdb_assert (pattern.len > 1);
1397       gdb_assert (pattern.len <= I386_MAX_MATCHED_INSN_LEN);
1398
1399       if (target_read_code (pc + 1, buf, pattern.len - 1))
1400         return 0;
1401
1402       for (i = 1; i < pattern.len; i++)
1403         {
1404           if ((buf[i - 1] & pattern.mask[i]) != pattern.insn[i])
1405             insn_matched = 0;
1406         }
1407       return insn_matched;
1408     }
1409   return 0;
1410 }
1411
1412 /* Search for the instruction at PC in the list INSN_PATTERNS.  Return
1413    the first instruction description that matches.  Otherwise, return
1414    NULL.  */
1415
1416 static struct i386_insn *
1417 i386_match_insn (CORE_ADDR pc, struct i386_insn *insn_patterns)
1418 {
1419   struct i386_insn *pattern;
1420
1421   for (pattern = insn_patterns; pattern->len > 0; pattern++)
1422     {
1423       if (i386_match_pattern (pc, *pattern))
1424         return pattern;
1425     }
1426
1427   return NULL;
1428 }
1429
1430 /* Return whether PC points inside a sequence of instructions that
1431    matches INSN_PATTERNS.  */
1432
1433 static int
1434 i386_match_insn_block (CORE_ADDR pc, struct i386_insn *insn_patterns)
1435 {
1436   CORE_ADDR current_pc;
1437   int ix, i;
1438   struct i386_insn *insn;
1439
1440   insn = i386_match_insn (pc, insn_patterns);
1441   if (insn == NULL)
1442     return 0;
1443
1444   current_pc = pc;
1445   ix = insn - insn_patterns;
1446   for (i = ix - 1; i >= 0; i--)
1447     {
1448       current_pc -= insn_patterns[i].len;
1449
1450       if (!i386_match_pattern (current_pc, insn_patterns[i]))
1451         return 0;
1452     }
1453
1454   current_pc = pc + insn->len;
1455   for (insn = insn_patterns + ix + 1; insn->len > 0; insn++)
1456     {
1457       if (!i386_match_pattern (current_pc, *insn))
1458         return 0;
1459
1460       current_pc += insn->len;
1461     }
1462
1463   return 1;
1464 }
1465
1466 /* Some special instructions that might be migrated by GCC into the
1467    part of the prologue that sets up the new stack frame.  Because the
1468    stack frame hasn't been setup yet, no registers have been saved
1469    yet, and only the scratch registers %eax, %ecx and %edx can be
1470    touched.  */
1471
1472 struct i386_insn i386_frame_setup_skip_insns[] =
1473 {
1474   /* Check for `movb imm8, r' and `movl imm32, r'.
1475     
1476      ??? Should we handle 16-bit operand-sizes here?  */
1477
1478   /* `movb imm8, %al' and `movb imm8, %ah' */
1479   /* `movb imm8, %cl' and `movb imm8, %ch' */
1480   { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
1481   /* `movb imm8, %dl' and `movb imm8, %dh' */
1482   { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
1483   /* `movl imm32, %eax' and `movl imm32, %ecx' */
1484   { 5, { 0xb8 }, { 0xfe } },
1485   /* `movl imm32, %edx' */
1486   { 5, { 0xba }, { 0xff } },
1487
1488   /* Check for `mov imm32, r32'.  Note that there is an alternative
1489      encoding for `mov m32, %eax'.
1490
1491      ??? Should we handle SIB adressing here?
1492      ??? Should we handle 16-bit operand-sizes here?  */
1493
1494   /* `movl m32, %eax' */
1495   { 5, { 0xa1 }, { 0xff } },
1496   /* `movl m32, %eax' and `mov; m32, %ecx' */
1497   { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
1498   /* `movl m32, %edx' */
1499   { 6, { 0x89, 0x15 }, {0xff, 0xff } },
1500
1501   /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
1502      Because of the symmetry, there are actually two ways to encode
1503      these instructions; opcode bytes 0x29 and 0x2b for `subl' and
1504      opcode bytes 0x31 and 0x33 for `xorl'.  */
1505
1506   /* `subl %eax, %eax' */
1507   { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
1508   /* `subl %ecx, %ecx' */
1509   { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
1510   /* `subl %edx, %edx' */
1511   { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
1512   /* `xorl %eax, %eax' */
1513   { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
1514   /* `xorl %ecx, %ecx' */
1515   { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
1516   /* `xorl %edx, %edx' */
1517   { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
1518   { 0 }
1519 };
1520
1521
1522 /* Check whether PC points to a no-op instruction.  */
1523 static CORE_ADDR
1524 i386_skip_noop (CORE_ADDR pc)
1525 {
1526   gdb_byte op;
1527   int check = 1;
1528
1529   if (target_read_code (pc, &op, 1))
1530     return pc;
1531
1532   while (check) 
1533     {
1534       check = 0;
1535       /* Ignore `nop' instruction.  */
1536       if (op == 0x90) 
1537         {
1538           pc += 1;
1539           if (target_read_code (pc, &op, 1))
1540             return pc;
1541           check = 1;
1542         }
1543       /* Ignore no-op instruction `mov %edi, %edi'.
1544          Microsoft system dlls often start with
1545          a `mov %edi,%edi' instruction.
1546          The 5 bytes before the function start are
1547          filled with `nop' instructions.
1548          This pattern can be used for hot-patching:
1549          The `mov %edi, %edi' instruction can be replaced by a
1550          near jump to the location of the 5 `nop' instructions
1551          which can be replaced by a 32-bit jump to anywhere
1552          in the 32-bit address space.  */
1553
1554       else if (op == 0x8b)
1555         {
1556           if (target_read_code (pc + 1, &op, 1))
1557             return pc;
1558
1559           if (op == 0xff)
1560             {
1561               pc += 2;
1562               if (target_read_code (pc, &op, 1))
1563                 return pc;
1564
1565               check = 1;
1566             }
1567         }
1568     }
1569   return pc; 
1570 }
1571
1572 /* Check whether PC points at a code that sets up a new stack frame.
1573    If so, it updates CACHE and returns the address of the first
1574    instruction after the sequence that sets up the frame or LIMIT,
1575    whichever is smaller.  If we don't recognize the code, return PC.  */
1576
1577 static CORE_ADDR
1578 i386_analyze_frame_setup (struct gdbarch *gdbarch,
1579                           CORE_ADDR pc, CORE_ADDR limit,
1580                           struct i386_frame_cache *cache)
1581 {
1582   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1583   struct i386_insn *insn;
1584   gdb_byte op;
1585   int skip = 0;
1586
1587   if (limit <= pc)
1588     return limit;
1589
1590   if (target_read_code (pc, &op, 1))
1591     return pc;
1592
1593   if (op == 0x55)               /* pushl %ebp */
1594     {
1595       /* Take into account that we've executed the `pushl %ebp' that
1596          starts this instruction sequence.  */
1597       cache->saved_regs[I386_EBP_REGNUM] = 0;
1598       cache->sp_offset += 4;
1599       pc++;
1600
1601       /* If that's all, return now.  */
1602       if (limit <= pc)
1603         return limit;
1604
1605       /* Check for some special instructions that might be migrated by
1606          GCC into the prologue and skip them.  At this point in the
1607          prologue, code should only touch the scratch registers %eax,
1608          %ecx and %edx, so while the number of posibilities is sheer,
1609          it is limited.
1610
1611          Make sure we only skip these instructions if we later see the
1612          `movl %esp, %ebp' that actually sets up the frame.  */
1613       while (pc + skip < limit)
1614         {
1615           insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1616           if (insn == NULL)
1617             break;
1618
1619           skip += insn->len;
1620         }
1621
1622       /* If that's all, return now.  */
1623       if (limit <= pc + skip)
1624         return limit;
1625
1626       if (target_read_code (pc + skip, &op, 1))
1627         return pc + skip;
1628
1629       /* The i386 prologue looks like
1630
1631          push   %ebp
1632          mov    %esp,%ebp
1633          sub    $0x10,%esp
1634
1635          and a different prologue can be generated for atom.
1636
1637          push   %ebp
1638          lea    (%esp),%ebp
1639          lea    -0x10(%esp),%esp
1640
1641          We handle both of them here.  */
1642
1643       switch (op)
1644         {
1645           /* Check for `movl %esp, %ebp' -- can be written in two ways.  */
1646         case 0x8b:
1647           if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
1648               != 0xec)
1649             return pc;
1650           pc += (skip + 2);
1651           break;
1652         case 0x89:
1653           if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
1654               != 0xe5)
1655             return pc;
1656           pc += (skip + 2);
1657           break;
1658         case 0x8d: /* Check for 'lea (%ebp), %ebp'.  */
1659           if (read_code_unsigned_integer (pc + skip + 1, 2, byte_order)
1660               != 0x242c)
1661             return pc;
1662           pc += (skip + 3);
1663           break;
1664         default:
1665           return pc;
1666         }
1667
1668       /* OK, we actually have a frame.  We just don't know how large
1669          it is yet.  Set its size to zero.  We'll adjust it if
1670          necessary.  We also now commit to skipping the special
1671          instructions mentioned before.  */
1672       cache->locals = 0;
1673
1674       /* If that's all, return now.  */
1675       if (limit <= pc)
1676         return limit;
1677
1678       /* Check for stack adjustment 
1679
1680             subl $XXX, %esp
1681          or
1682             lea -XXX(%esp),%esp
1683
1684          NOTE: You can't subtract a 16-bit immediate from a 32-bit
1685          reg, so we don't have to worry about a data16 prefix.  */
1686       if (target_read_code (pc, &op, 1))
1687         return pc;
1688       if (op == 0x83)
1689         {
1690           /* `subl' with 8-bit immediate.  */
1691           if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1692             /* Some instruction starting with 0x83 other than `subl'.  */
1693             return pc;
1694
1695           /* `subl' with signed 8-bit immediate (though it wouldn't
1696              make sense to be negative).  */
1697           cache->locals = read_code_integer (pc + 2, 1, byte_order);
1698           return pc + 3;
1699         }
1700       else if (op == 0x81)
1701         {
1702           /* Maybe it is `subl' with a 32-bit immediate.  */
1703           if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
1704             /* Some instruction starting with 0x81 other than `subl'.  */
1705             return pc;
1706
1707           /* It is `subl' with a 32-bit immediate.  */
1708           cache->locals = read_code_integer (pc + 2, 4, byte_order);
1709           return pc + 6;
1710         }
1711       else if (op == 0x8d)
1712         {
1713           /* The ModR/M byte is 0x64.  */
1714           if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0x64)
1715             return pc;
1716           /* 'lea' with 8-bit displacement.  */
1717           cache->locals = -1 * read_code_integer (pc + 3, 1, byte_order);
1718           return pc + 4;
1719         }
1720       else
1721         {
1722           /* Some instruction other than `subl' nor 'lea'.  */
1723           return pc;
1724         }
1725     }
1726   else if (op == 0xc8)          /* enter */
1727     {
1728       cache->locals = read_code_unsigned_integer (pc + 1, 2, byte_order);
1729       return pc + 4;
1730     }
1731
1732   return pc;
1733 }
1734
1735 /* Check whether PC points at code that saves registers on the stack.
1736    If so, it updates CACHE and returns the address of the first
1737    instruction after the register saves or CURRENT_PC, whichever is
1738    smaller.  Otherwise, return PC.  */
1739
1740 static CORE_ADDR
1741 i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1742                              struct i386_frame_cache *cache)
1743 {
1744   CORE_ADDR offset = 0;
1745   gdb_byte op;
1746   int i;
1747
1748   if (cache->locals > 0)
1749     offset -= cache->locals;
1750   for (i = 0; i < 8 && pc < current_pc; i++)
1751     {
1752       if (target_read_code (pc, &op, 1))
1753         return pc;
1754       if (op < 0x50 || op > 0x57)
1755         break;
1756
1757       offset -= 4;
1758       cache->saved_regs[op - 0x50] = offset;
1759       cache->sp_offset += 4;
1760       pc++;
1761     }
1762
1763   return pc;
1764 }
1765
1766 /* Do a full analysis of the prologue at PC and update CACHE
1767    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
1768    address where the analysis stopped.
1769
1770    We handle these cases:
1771
1772    The startup sequence can be at the start of the function, or the
1773    function can start with a branch to startup code at the end.
1774
1775    %ebp can be set up with either the 'enter' instruction, or "pushl
1776    %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
1777    once used in the System V compiler).
1778
1779    Local space is allocated just below the saved %ebp by either the
1780    'enter' instruction, or by "subl $<size>, %esp".  'enter' has a
1781    16-bit unsigned argument for space to allocate, and the 'addl'
1782    instruction could have either a signed byte, or 32-bit immediate.
1783
1784    Next, the registers used by this function are pushed.  With the
1785    System V compiler they will always be in the order: %edi, %esi,
1786    %ebx (and sometimes a harmless bug causes it to also save but not
1787    restore %eax); however, the code below is willing to see the pushes
1788    in any order, and will handle up to 8 of them.
1789  
1790    If the setup sequence is at the end of the function, then the next
1791    instruction will be a branch back to the start.  */
1792
1793 static CORE_ADDR
1794 i386_analyze_prologue (struct gdbarch *gdbarch,
1795                        CORE_ADDR pc, CORE_ADDR current_pc,
1796                        struct i386_frame_cache *cache)
1797 {
1798   pc = i386_skip_noop (pc);
1799   pc = i386_follow_jump (gdbarch, pc);
1800   pc = i386_analyze_struct_return (pc, current_pc, cache);
1801   pc = i386_skip_probe (pc);
1802   pc = i386_analyze_stack_align (pc, current_pc, cache);
1803   pc = i386_analyze_frame_setup (gdbarch, pc, current_pc, cache);
1804   return i386_analyze_register_saves (pc, current_pc, cache);
1805 }
1806
1807 /* Return PC of first real instruction.  */
1808
1809 static CORE_ADDR
1810 i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1811 {
1812   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1813
1814   static gdb_byte pic_pat[6] =
1815   {
1816     0xe8, 0, 0, 0, 0,           /* call 0x0 */
1817     0x5b,                       /* popl %ebx */
1818   };
1819   struct i386_frame_cache cache;
1820   CORE_ADDR pc;
1821   gdb_byte op;
1822   int i;
1823   CORE_ADDR func_addr;
1824
1825   if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1826     {
1827       CORE_ADDR post_prologue_pc
1828         = skip_prologue_using_sal (gdbarch, func_addr);
1829       struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
1830
1831       /* Clang always emits a line note before the prologue and another
1832          one after.  We trust clang to emit usable line notes.  */
1833       if (post_prologue_pc
1834           && (cust != NULL
1835               && COMPUNIT_PRODUCER (cust) != NULL
1836               && startswith (COMPUNIT_PRODUCER (cust), "clang ")))
1837         return std::max (start_pc, post_prologue_pc);
1838     }
1839  
1840   cache.locals = -1;
1841   pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
1842   if (cache.locals < 0)
1843     return start_pc;
1844
1845   /* Found valid frame setup.  */
1846
1847   /* The native cc on SVR4 in -K PIC mode inserts the following code
1848      to get the address of the global offset table (GOT) into register
1849      %ebx:
1850
1851         call    0x0
1852         popl    %ebx
1853         movl    %ebx,x(%ebp)    (optional)
1854         addl    y,%ebx
1855
1856      This code is with the rest of the prologue (at the end of the
1857      function), so we have to skip it to get to the first real
1858      instruction at the start of the function.  */
1859
1860   for (i = 0; i < 6; i++)
1861     {
1862       if (target_read_code (pc + i, &op, 1))
1863         return pc;
1864
1865       if (pic_pat[i] != op)
1866         break;
1867     }
1868   if (i == 6)
1869     {
1870       int delta = 6;
1871
1872       if (target_read_code (pc + delta, &op, 1))
1873         return pc;
1874
1875       if (op == 0x89)           /* movl %ebx, x(%ebp) */
1876         {
1877           op = read_code_unsigned_integer (pc + delta + 1, 1, byte_order);
1878
1879           if (op == 0x5d)       /* One byte offset from %ebp.  */
1880             delta += 3;
1881           else if (op == 0x9d)  /* Four byte offset from %ebp.  */
1882             delta += 6;
1883           else                  /* Unexpected instruction.  */
1884             delta = 0;
1885
1886           if (target_read_code (pc + delta, &op, 1))
1887             return pc;
1888         }
1889
1890       /* addl y,%ebx */
1891       if (delta > 0 && op == 0x81
1892           && read_code_unsigned_integer (pc + delta + 1, 1, byte_order)
1893              == 0xc3)
1894         {
1895           pc += delta + 6;
1896         }
1897     }
1898
1899   /* If the function starts with a branch (to startup code at the end)
1900      the last instruction should bring us back to the first
1901      instruction of the real code.  */
1902   if (i386_follow_jump (gdbarch, start_pc) != start_pc)
1903     pc = i386_follow_jump (gdbarch, pc);
1904
1905   return pc;
1906 }
1907
1908 /* Check that the code pointed to by PC corresponds to a call to
1909    __main, skip it if so.  Return PC otherwise.  */
1910
1911 CORE_ADDR
1912 i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1913 {
1914   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1915   gdb_byte op;
1916
1917   if (target_read_code (pc, &op, 1))
1918     return pc;
1919   if (op == 0xe8)
1920     {
1921       gdb_byte buf[4];
1922
1923       if (target_read_code (pc + 1, buf, sizeof buf) == 0)
1924         {
1925           /* Make sure address is computed correctly as a 32bit
1926              integer even if CORE_ADDR is 64 bit wide.  */
1927           struct bound_minimal_symbol s;
1928           CORE_ADDR call_dest;
1929
1930           call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
1931           call_dest = call_dest & 0xffffffffU;
1932           s = lookup_minimal_symbol_by_pc (call_dest);
1933           if (s.minsym != NULL
1934               && MSYMBOL_LINKAGE_NAME (s.minsym) != NULL
1935               && strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0)
1936             pc += 5;
1937         }
1938     }
1939
1940   return pc;
1941 }
1942
1943 /* This function is 64-bit safe.  */
1944
1945 static CORE_ADDR
1946 i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1947 {
1948   gdb_byte buf[8];
1949
1950   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
1951   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
1952 }
1953 \f
1954
1955 /* Normal frames.  */
1956
1957 static void
1958 i386_frame_cache_1 (struct frame_info *this_frame,
1959                     struct i386_frame_cache *cache)
1960 {
1961   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1962   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1963   gdb_byte buf[4];
1964   int i;
1965
1966   cache->pc = get_frame_func (this_frame);
1967
1968   /* In principle, for normal frames, %ebp holds the frame pointer,
1969      which holds the base address for the current stack frame.
1970      However, for functions that don't need it, the frame pointer is
1971      optional.  For these "frameless" functions the frame pointer is
1972      actually the frame pointer of the calling frame.  Signal
1973      trampolines are just a special case of a "frameless" function.
1974      They (usually) share their frame pointer with the frame that was
1975      in progress when the signal occurred.  */
1976
1977   get_frame_register (this_frame, I386_EBP_REGNUM, buf);
1978   cache->base = extract_unsigned_integer (buf, 4, byte_order);
1979   if (cache->base == 0)
1980     {
1981       cache->base_p = 1;
1982       return;
1983     }
1984
1985   /* For normal frames, %eip is stored at 4(%ebp).  */
1986   cache->saved_regs[I386_EIP_REGNUM] = 4;
1987
1988   if (cache->pc != 0)
1989     i386_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
1990                            cache);
1991
1992   if (cache->locals < 0)
1993     {
1994       /* We didn't find a valid frame, which means that CACHE->base
1995          currently holds the frame pointer for our calling frame.  If
1996          we're at the start of a function, or somewhere half-way its
1997          prologue, the function's frame probably hasn't been fully
1998          setup yet.  Try to reconstruct the base address for the stack
1999          frame by looking at the stack pointer.  For truly "frameless"
2000          functions this might work too.  */
2001
2002       if (cache->saved_sp_reg != -1)
2003         {
2004           /* Saved stack pointer has been saved.  */
2005           get_frame_register (this_frame, cache->saved_sp_reg, buf);
2006           cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
2007
2008           /* We're halfway aligning the stack.  */
2009           cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
2010           cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
2011
2012           /* This will be added back below.  */
2013           cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
2014         }
2015       else if (cache->pc != 0
2016                || target_read_code (get_frame_pc (this_frame), buf, 1))
2017         {
2018           /* We're in a known function, but did not find a frame
2019              setup.  Assume that the function does not use %ebp.
2020              Alternatively, we may have jumped to an invalid
2021              address; in that case there is definitely no new
2022              frame in %ebp.  */
2023           get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2024           cache->base = extract_unsigned_integer (buf, 4, byte_order)
2025                         + cache->sp_offset;
2026         }
2027       else
2028         /* We're in an unknown function.  We could not find the start
2029            of the function to analyze the prologue; our best option is
2030            to assume a typical frame layout with the caller's %ebp
2031            saved.  */
2032         cache->saved_regs[I386_EBP_REGNUM] = 0;
2033     }
2034
2035   if (cache->saved_sp_reg != -1)
2036     {
2037       /* Saved stack pointer has been saved (but the SAVED_SP_REG
2038          register may be unavailable).  */
2039       if (cache->saved_sp == 0
2040           && deprecated_frame_register_read (this_frame,
2041                                              cache->saved_sp_reg, buf))
2042         cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
2043     }
2044   /* Now that we have the base address for the stack frame we can
2045      calculate the value of %esp in the calling frame.  */
2046   else if (cache->saved_sp == 0)
2047     cache->saved_sp = cache->base + 8;
2048
2049   /* Adjust all the saved registers such that they contain addresses
2050      instead of offsets.  */
2051   for (i = 0; i < I386_NUM_SAVED_REGS; i++)
2052     if (cache->saved_regs[i] != -1)
2053       cache->saved_regs[i] += cache->base;
2054
2055   cache->base_p = 1;
2056 }
2057
2058 static struct i386_frame_cache *
2059 i386_frame_cache (struct frame_info *this_frame, void **this_cache)
2060 {
2061   struct i386_frame_cache *cache;
2062
2063   if (*this_cache)
2064     return (struct i386_frame_cache *) *this_cache;
2065
2066   cache = i386_alloc_frame_cache ();
2067   *this_cache = cache;
2068
2069   TRY
2070     {
2071       i386_frame_cache_1 (this_frame, cache);
2072     }
2073   CATCH (ex, RETURN_MASK_ERROR)
2074     {
2075       if (ex.error != NOT_AVAILABLE_ERROR)
2076         throw_exception (ex);
2077     }
2078   END_CATCH
2079
2080   return cache;
2081 }
2082
2083 static void
2084 i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
2085                     struct frame_id *this_id)
2086 {
2087   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2088
2089   if (!cache->base_p)
2090     (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2091   else if (cache->base == 0)
2092     {
2093       /* This marks the outermost frame.  */
2094     }
2095   else
2096     {
2097       /* See the end of i386_push_dummy_call.  */
2098       (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2099     }
2100 }
2101
2102 static enum unwind_stop_reason
2103 i386_frame_unwind_stop_reason (struct frame_info *this_frame,
2104                                void **this_cache)
2105 {
2106   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2107
2108   if (!cache->base_p)
2109     return UNWIND_UNAVAILABLE;
2110
2111   /* This marks the outermost frame.  */
2112   if (cache->base == 0)
2113     return UNWIND_OUTERMOST;
2114
2115   return UNWIND_NO_REASON;
2116 }
2117
2118 static struct value *
2119 i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
2120                           int regnum)
2121 {
2122   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2123
2124   gdb_assert (regnum >= 0);
2125
2126   /* The System V ABI says that:
2127
2128      "The flags register contains the system flags, such as the
2129      direction flag and the carry flag.  The direction flag must be
2130      set to the forward (that is, zero) direction before entry and
2131      upon exit from a function.  Other user flags have no specified
2132      role in the standard calling sequence and are not preserved."
2133
2134      To guarantee the "upon exit" part of that statement we fake a
2135      saved flags register that has its direction flag cleared.
2136
2137      Note that GCC doesn't seem to rely on the fact that the direction
2138      flag is cleared after a function return; it always explicitly
2139      clears the flag before operations where it matters.
2140
2141      FIXME: kettenis/20030316: I'm not quite sure whether this is the
2142      right thing to do.  The way we fake the flags register here makes
2143      it impossible to change it.  */
2144
2145   if (regnum == I386_EFLAGS_REGNUM)
2146     {
2147       ULONGEST val;
2148
2149       val = get_frame_register_unsigned (this_frame, regnum);
2150       val &= ~(1 << 10);
2151       return frame_unwind_got_constant (this_frame, regnum, val);
2152     }
2153
2154   if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
2155     return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
2156
2157   if (regnum == I386_ESP_REGNUM
2158       && (cache->saved_sp != 0 || cache->saved_sp_reg != -1))
2159     {
2160       /* If the SP has been saved, but we don't know where, then this
2161          means that SAVED_SP_REG register was found unavailable back
2162          when we built the cache.  */
2163       if (cache->saved_sp == 0)
2164         return frame_unwind_got_register (this_frame, regnum,
2165                                           cache->saved_sp_reg);
2166       else
2167         return frame_unwind_got_constant (this_frame, regnum,
2168                                           cache->saved_sp);
2169     }
2170
2171   if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2172     return frame_unwind_got_memory (this_frame, regnum,
2173                                     cache->saved_regs[regnum]);
2174
2175   return frame_unwind_got_register (this_frame, regnum, regnum);
2176 }
2177
2178 static const struct frame_unwind i386_frame_unwind =
2179 {
2180   NORMAL_FRAME,
2181   i386_frame_unwind_stop_reason,
2182   i386_frame_this_id,
2183   i386_frame_prev_register,
2184   NULL,
2185   default_frame_sniffer
2186 };
2187
2188 /* Normal frames, but in a function epilogue.  */
2189
2190 /* Implement the stack_frame_destroyed_p gdbarch method.
2191
2192    The epilogue is defined here as the 'ret' instruction, which will
2193    follow any instruction such as 'leave' or 'pop %ebp' that destroys
2194    the function's stack frame.  */
2195
2196 static int
2197 i386_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2198 {
2199   gdb_byte insn;
2200   struct compunit_symtab *cust;
2201
2202   cust = find_pc_compunit_symtab (pc);
2203   if (cust != NULL && COMPUNIT_EPILOGUE_UNWIND_VALID (cust))
2204     return 0;
2205
2206   if (target_read_memory (pc, &insn, 1))
2207     return 0;   /* Can't read memory at pc.  */
2208
2209   if (insn != 0xc3)     /* 'ret' instruction.  */
2210     return 0;
2211
2212   return 1;
2213 }
2214
2215 static int
2216 i386_epilogue_frame_sniffer (const struct frame_unwind *self,
2217                              struct frame_info *this_frame,
2218                              void **this_prologue_cache)
2219 {
2220   if (frame_relative_level (this_frame) == 0)
2221     return i386_stack_frame_destroyed_p (get_frame_arch (this_frame),
2222                                          get_frame_pc (this_frame));
2223   else
2224     return 0;
2225 }
2226
2227 static struct i386_frame_cache *
2228 i386_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2229 {
2230   struct i386_frame_cache *cache;
2231   CORE_ADDR sp;
2232
2233   if (*this_cache)
2234     return (struct i386_frame_cache *) *this_cache;
2235
2236   cache = i386_alloc_frame_cache ();
2237   *this_cache = cache;
2238
2239   TRY
2240     {
2241       cache->pc = get_frame_func (this_frame);
2242
2243       /* At this point the stack looks as if we just entered the
2244          function, with the return address at the top of the
2245          stack.  */
2246       sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
2247       cache->base = sp + cache->sp_offset;
2248       cache->saved_sp = cache->base + 8;
2249       cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
2250
2251       cache->base_p = 1;
2252     }
2253   CATCH (ex, RETURN_MASK_ERROR)
2254     {
2255       if (ex.error != NOT_AVAILABLE_ERROR)
2256         throw_exception (ex);
2257     }
2258   END_CATCH
2259
2260   return cache;
2261 }
2262
2263 static enum unwind_stop_reason
2264 i386_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
2265                                         void **this_cache)
2266 {
2267   struct i386_frame_cache *cache =
2268     i386_epilogue_frame_cache (this_frame, this_cache);
2269
2270   if (!cache->base_p)
2271     return UNWIND_UNAVAILABLE;
2272
2273   return UNWIND_NO_REASON;
2274 }
2275
2276 static void
2277 i386_epilogue_frame_this_id (struct frame_info *this_frame,
2278                              void **this_cache,
2279                              struct frame_id *this_id)
2280 {
2281   struct i386_frame_cache *cache =
2282     i386_epilogue_frame_cache (this_frame, this_cache);
2283
2284   if (!cache->base_p)
2285     (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2286   else
2287     (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2288 }
2289
2290 static struct value *
2291 i386_epilogue_frame_prev_register (struct frame_info *this_frame,
2292                                    void **this_cache, int regnum)
2293 {
2294   /* Make sure we've initialized the cache.  */
2295   i386_epilogue_frame_cache (this_frame, this_cache);
2296
2297   return i386_frame_prev_register (this_frame, this_cache, regnum);
2298 }
2299
2300 static const struct frame_unwind i386_epilogue_frame_unwind =
2301 {
2302   NORMAL_FRAME,
2303   i386_epilogue_frame_unwind_stop_reason,
2304   i386_epilogue_frame_this_id,
2305   i386_epilogue_frame_prev_register,
2306   NULL, 
2307   i386_epilogue_frame_sniffer
2308 };
2309 \f
2310
2311 /* Stack-based trampolines.  */
2312
2313 /* These trampolines are used on cross x86 targets, when taking the
2314    address of a nested function.  When executing these trampolines,
2315    no stack frame is set up, so we are in a similar situation as in
2316    epilogues and i386_epilogue_frame_this_id can be re-used.  */
2317
2318 /* Static chain passed in register.  */
2319
2320 struct i386_insn i386_tramp_chain_in_reg_insns[] =
2321 {
2322   /* `movl imm32, %eax' and `movl imm32, %ecx' */
2323   { 5, { 0xb8 }, { 0xfe } },
2324
2325   /* `jmp imm32' */
2326   { 5, { 0xe9 }, { 0xff } },
2327
2328   {0}
2329 };
2330
2331 /* Static chain passed on stack (when regparm=3).  */
2332
2333 struct i386_insn i386_tramp_chain_on_stack_insns[] =
2334 {
2335   /* `push imm32' */
2336   { 5, { 0x68 }, { 0xff } },
2337
2338   /* `jmp imm32' */
2339   { 5, { 0xe9 }, { 0xff } },
2340
2341   {0}
2342 };
2343
2344 /* Return whether PC points inside a stack trampoline.   */
2345
2346 static int
2347 i386_in_stack_tramp_p (CORE_ADDR pc)
2348 {
2349   gdb_byte insn;
2350   const char *name;
2351
2352   /* A stack trampoline is detected if no name is associated
2353     to the current pc and if it points inside a trampoline
2354     sequence.  */
2355
2356   find_pc_partial_function (pc, &name, NULL, NULL);
2357   if (name)
2358     return 0;
2359
2360   if (target_read_memory (pc, &insn, 1))
2361     return 0;
2362
2363   if (!i386_match_insn_block (pc, i386_tramp_chain_in_reg_insns)
2364       && !i386_match_insn_block (pc, i386_tramp_chain_on_stack_insns))
2365     return 0;
2366
2367   return 1;
2368 }
2369
2370 static int
2371 i386_stack_tramp_frame_sniffer (const struct frame_unwind *self,
2372                                 struct frame_info *this_frame,
2373                                 void **this_cache)
2374 {
2375   if (frame_relative_level (this_frame) == 0)
2376     return i386_in_stack_tramp_p (get_frame_pc (this_frame));
2377   else
2378     return 0;
2379 }
2380
2381 static const struct frame_unwind i386_stack_tramp_frame_unwind =
2382 {
2383   NORMAL_FRAME,
2384   i386_epilogue_frame_unwind_stop_reason,
2385   i386_epilogue_frame_this_id,
2386   i386_epilogue_frame_prev_register,
2387   NULL, 
2388   i386_stack_tramp_frame_sniffer
2389 };
2390 \f
2391 /* Generate a bytecode expression to get the value of the saved PC.  */
2392
2393 static void
2394 i386_gen_return_address (struct gdbarch *gdbarch,
2395                          struct agent_expr *ax, struct axs_value *value,
2396                          CORE_ADDR scope)
2397 {
2398   /* The following sequence assumes the traditional use of the base
2399      register.  */
2400   ax_reg (ax, I386_EBP_REGNUM);
2401   ax_const_l (ax, 4);
2402   ax_simple (ax, aop_add);
2403   value->type = register_type (gdbarch, I386_EIP_REGNUM);
2404   value->kind = axs_lvalue_memory;
2405 }
2406 \f
2407
2408 /* Signal trampolines.  */
2409
2410 static struct i386_frame_cache *
2411 i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
2412 {
2413   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2414   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2415   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2416   struct i386_frame_cache *cache;
2417   CORE_ADDR addr;
2418   gdb_byte buf[4];
2419
2420   if (*this_cache)
2421     return (struct i386_frame_cache *) *this_cache;
2422
2423   cache = i386_alloc_frame_cache ();
2424
2425   TRY
2426     {
2427       get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2428       cache->base = extract_unsigned_integer (buf, 4, byte_order) - 4;
2429
2430       addr = tdep->sigcontext_addr (this_frame);
2431       if (tdep->sc_reg_offset)
2432         {
2433           int i;
2434
2435           gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
2436
2437           for (i = 0; i < tdep->sc_num_regs; i++)
2438             if (tdep->sc_reg_offset[i] != -1)
2439               cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2440         }
2441       else
2442         {
2443           cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
2444           cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
2445         }
2446
2447       cache->base_p = 1;
2448     }
2449   CATCH (ex, RETURN_MASK_ERROR)
2450     {
2451       if (ex.error != NOT_AVAILABLE_ERROR)
2452         throw_exception (ex);
2453     }
2454   END_CATCH
2455
2456   *this_cache = cache;
2457   return cache;
2458 }
2459
2460 static enum unwind_stop_reason
2461 i386_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
2462                                         void **this_cache)
2463 {
2464   struct i386_frame_cache *cache =
2465     i386_sigtramp_frame_cache (this_frame, this_cache);
2466
2467   if (!cache->base_p)
2468     return UNWIND_UNAVAILABLE;
2469
2470   return UNWIND_NO_REASON;
2471 }
2472
2473 static void
2474 i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
2475                              struct frame_id *this_id)
2476 {
2477   struct i386_frame_cache *cache =
2478     i386_sigtramp_frame_cache (this_frame, this_cache);
2479
2480   if (!cache->base_p)
2481     (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2482   else
2483     {
2484       /* See the end of i386_push_dummy_call.  */
2485       (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
2486     }
2487 }
2488
2489 static struct value *
2490 i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
2491                                    void **this_cache, int regnum)
2492 {
2493   /* Make sure we've initialized the cache.  */
2494   i386_sigtramp_frame_cache (this_frame, this_cache);
2495
2496   return i386_frame_prev_register (this_frame, this_cache, regnum);
2497 }
2498
2499 static int
2500 i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
2501                              struct frame_info *this_frame,
2502                              void **this_prologue_cache)
2503 {
2504   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
2505
2506   /* We shouldn't even bother if we don't have a sigcontext_addr
2507      handler.  */
2508   if (tdep->sigcontext_addr == NULL)
2509     return 0;
2510
2511   if (tdep->sigtramp_p != NULL)
2512     {
2513       if (tdep->sigtramp_p (this_frame))
2514         return 1;
2515     }
2516
2517   if (tdep->sigtramp_start != 0)
2518     {
2519       CORE_ADDR pc = get_frame_pc (this_frame);
2520
2521       gdb_assert (tdep->sigtramp_end != 0);
2522       if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2523         return 1;
2524     }
2525
2526   return 0;
2527 }
2528
2529 static const struct frame_unwind i386_sigtramp_frame_unwind =
2530 {
2531   SIGTRAMP_FRAME,
2532   i386_sigtramp_frame_unwind_stop_reason,
2533   i386_sigtramp_frame_this_id,
2534   i386_sigtramp_frame_prev_register,
2535   NULL,
2536   i386_sigtramp_frame_sniffer
2537 };
2538 \f
2539
2540 static CORE_ADDR
2541 i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
2542 {
2543   struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
2544
2545   return cache->base;
2546 }
2547
2548 static const struct frame_base i386_frame_base =
2549 {
2550   &i386_frame_unwind,
2551   i386_frame_base_address,
2552   i386_frame_base_address,
2553   i386_frame_base_address
2554 };
2555
2556 static struct frame_id
2557 i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2558 {
2559   CORE_ADDR fp;
2560
2561   fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
2562
2563   /* See the end of i386_push_dummy_call.  */
2564   return frame_id_build (fp + 8, get_frame_pc (this_frame));
2565 }
2566
2567 /* _Decimal128 function return values need 16-byte alignment on the
2568    stack.  */
2569
2570 static CORE_ADDR
2571 i386_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2572 {
2573   return sp & -(CORE_ADDR)16;
2574 }
2575 \f
2576
2577 /* Figure out where the longjmp will land.  Slurp the args out of the
2578    stack.  We expect the first arg to be a pointer to the jmp_buf
2579    structure from which we extract the address that we will land at.
2580    This address is copied into PC.  This routine returns non-zero on
2581    success.  */
2582
2583 static int
2584 i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2585 {
2586   gdb_byte buf[4];
2587   CORE_ADDR sp, jb_addr;
2588   struct gdbarch *gdbarch = get_frame_arch (frame);
2589   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2590   int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
2591
2592   /* If JB_PC_OFFSET is -1, we have no way to find out where the
2593      longjmp will land.  */
2594   if (jb_pc_offset == -1)
2595     return 0;
2596
2597   get_frame_register (frame, I386_ESP_REGNUM, buf);
2598   sp = extract_unsigned_integer (buf, 4, byte_order);
2599   if (target_read_memory (sp + 4, buf, 4))
2600     return 0;
2601
2602   jb_addr = extract_unsigned_integer (buf, 4, byte_order);
2603   if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
2604     return 0;
2605
2606   *pc = extract_unsigned_integer (buf, 4, byte_order);
2607   return 1;
2608 }
2609 \f
2610
2611 /* Check whether TYPE must be 16-byte-aligned when passed as a
2612    function argument.  16-byte vectors, _Decimal128 and structures or
2613    unions containing such types must be 16-byte-aligned; other
2614    arguments are 4-byte-aligned.  */
2615
2616 static int
2617 i386_16_byte_align_p (struct type *type)
2618 {
2619   type = check_typedef (type);
2620   if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT
2621        || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
2622       && TYPE_LENGTH (type) == 16)
2623     return 1;
2624   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
2625     return i386_16_byte_align_p (TYPE_TARGET_TYPE (type));
2626   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2627       || TYPE_CODE (type) == TYPE_CODE_UNION)
2628     {
2629       int i;
2630       for (i = 0; i < TYPE_NFIELDS (type); i++)
2631         {
2632           if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
2633             return 1;
2634         }
2635     }
2636   return 0;
2637 }
2638
2639 /* Implementation for set_gdbarch_push_dummy_code.  */
2640
2641 static CORE_ADDR
2642 i386_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
2643                       struct value **args, int nargs, struct type *value_type,
2644                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
2645                       struct regcache *regcache)
2646 {
2647   /* Use 0xcc breakpoint - 1 byte.  */
2648   *bp_addr = sp - 1;
2649   *real_pc = funaddr;
2650
2651   /* Keep the stack aligned.  */
2652   return sp - 16;
2653 }
2654
2655 static CORE_ADDR
2656 i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2657                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
2658                       struct value **args, CORE_ADDR sp, int struct_return,
2659                       CORE_ADDR struct_addr)
2660 {
2661   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2662   gdb_byte buf[4];
2663   int i;
2664   int write_pass;
2665   int args_space = 0;
2666
2667   /* Determine the total space required for arguments and struct
2668      return address in a first pass (allowing for 16-byte-aligned
2669      arguments), then push arguments in a second pass.  */
2670
2671   for (write_pass = 0; write_pass < 2; write_pass++)
2672     {
2673       int args_space_used = 0;
2674
2675       if (struct_return)
2676         {
2677           if (write_pass)
2678             {
2679               /* Push value address.  */
2680               store_unsigned_integer (buf, 4, byte_order, struct_addr);
2681               write_memory (sp, buf, 4);
2682               args_space_used += 4;
2683             }
2684           else
2685             args_space += 4;
2686         }
2687
2688       for (i = 0; i < nargs; i++)
2689         {
2690           int len = TYPE_LENGTH (value_enclosing_type (args[i]));
2691
2692           if (write_pass)
2693             {
2694               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
2695                 args_space_used = align_up (args_space_used, 16);
2696
2697               write_memory (sp + args_space_used,
2698                             value_contents_all (args[i]), len);
2699               /* The System V ABI says that:
2700
2701               "An argument's size is increased, if necessary, to make it a
2702               multiple of [32-bit] words.  This may require tail padding,
2703               depending on the size of the argument."
2704
2705               This makes sure the stack stays word-aligned.  */
2706               args_space_used += align_up (len, 4);
2707             }
2708           else
2709             {
2710               if (i386_16_byte_align_p (value_enclosing_type (args[i])))
2711                 args_space = align_up (args_space, 16);
2712               args_space += align_up (len, 4);
2713             }
2714         }
2715
2716       if (!write_pass)
2717         {
2718           sp -= args_space;
2719
2720           /* The original System V ABI only requires word alignment,
2721              but modern incarnations need 16-byte alignment in order
2722              to support SSE.  Since wasting a few bytes here isn't
2723              harmful we unconditionally enforce 16-byte alignment.  */
2724           sp &= ~0xf;
2725         }
2726     }
2727
2728   /* Store return address.  */
2729   sp -= 4;
2730   store_unsigned_integer (buf, 4, byte_order, bp_addr);
2731   write_memory (sp, buf, 4);
2732
2733   /* Finally, update the stack pointer...  */
2734   store_unsigned_integer (buf, 4, byte_order, sp);
2735   regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
2736
2737   /* ...and fake a frame pointer.  */
2738   regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
2739
2740   /* MarkK wrote: This "+ 8" is all over the place:
2741      (i386_frame_this_id, i386_sigtramp_frame_this_id,
2742      i386_dummy_id).  It's there, since all frame unwinders for
2743      a given target have to agree (within a certain margin) on the
2744      definition of the stack address of a frame.  Otherwise frame id
2745      comparison might not work correctly.  Since DWARF2/GCC uses the
2746      stack address *before* the function call as a frame's CFA.  On
2747      the i386, when %ebp is used as a frame pointer, the offset
2748      between the contents %ebp and the CFA as defined by GCC.  */
2749   return sp + 8;
2750 }
2751
2752 /* These registers are used for returning integers (and on some
2753    targets also for returning `struct' and `union' values when their
2754    size and alignment match an integer type).  */
2755 #define LOW_RETURN_REGNUM       I386_EAX_REGNUM /* %eax */
2756 #define HIGH_RETURN_REGNUM      I386_EDX_REGNUM /* %edx */
2757
2758 /* Read, for architecture GDBARCH, a function return value of TYPE
2759    from REGCACHE, and copy that into VALBUF.  */
2760
2761 static void
2762 i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
2763                            struct regcache *regcache, gdb_byte *valbuf)
2764 {
2765   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2766   int len = TYPE_LENGTH (type);
2767   gdb_byte buf[I386_MAX_REGISTER_SIZE];
2768
2769   if (TYPE_CODE (type) == TYPE_CODE_FLT)
2770     {
2771       if (tdep->st0_regnum < 0)
2772         {
2773           warning (_("Cannot find floating-point return value."));
2774           memset (valbuf, 0, len);
2775           return;
2776         }
2777
2778       /* Floating-point return values can be found in %st(0).  Convert
2779          its contents to the desired type.  This is probably not
2780          exactly how it would happen on the target itself, but it is
2781          the best we can do.  */
2782       regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
2783       convert_typed_floating (buf, i387_ext_type (gdbarch), valbuf, type);
2784     }
2785   else
2786     {
2787       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2788       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
2789
2790       if (len <= low_size)
2791         {
2792           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
2793           memcpy (valbuf, buf, len);
2794         }
2795       else if (len <= (low_size + high_size))
2796         {
2797           regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
2798           memcpy (valbuf, buf, low_size);
2799           regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
2800           memcpy (valbuf + low_size, buf, len - low_size);
2801         }
2802       else
2803         internal_error (__FILE__, __LINE__,
2804                         _("Cannot extract return value of %d bytes long."),
2805                         len);
2806     }
2807 }
2808
2809 /* Write, for architecture GDBARCH, a function return value of TYPE
2810    from VALBUF into REGCACHE.  */
2811
2812 static void
2813 i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
2814                          struct regcache *regcache, const gdb_byte *valbuf)
2815 {
2816   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2817   int len = TYPE_LENGTH (type);
2818
2819   if (TYPE_CODE (type) == TYPE_CODE_FLT)
2820     {
2821       ULONGEST fstat;
2822       gdb_byte buf[I386_MAX_REGISTER_SIZE];
2823
2824       if (tdep->st0_regnum < 0)
2825         {
2826           warning (_("Cannot set floating-point return value."));
2827           return;
2828         }
2829
2830       /* Returning floating-point values is a bit tricky.  Apart from
2831          storing the return value in %st(0), we have to simulate the
2832          state of the FPU at function return point.  */
2833
2834       /* Convert the value found in VALBUF to the extended
2835          floating-point format used by the FPU.  This is probably
2836          not exactly how it would happen on the target itself, but
2837          it is the best we can do.  */
2838       convert_typed_floating (valbuf, type, buf, i387_ext_type (gdbarch));
2839       regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
2840
2841       /* Set the top of the floating-point register stack to 7.  The
2842          actual value doesn't really matter, but 7 is what a normal
2843          function return would end up with if the program started out
2844          with a freshly initialized FPU.  */
2845       regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
2846       fstat |= (7 << 11);
2847       regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
2848
2849       /* Mark %st(1) through %st(7) as empty.  Since we set the top of
2850          the floating-point register stack to 7, the appropriate value
2851          for the tag word is 0x3fff.  */
2852       regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
2853     }
2854   else
2855     {
2856       int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2857       int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
2858
2859       if (len <= low_size)
2860         regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
2861       else if (len <= (low_size + high_size))
2862         {
2863           regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
2864           regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
2865                                    len - low_size, valbuf + low_size);
2866         }
2867       else
2868         internal_error (__FILE__, __LINE__,
2869                         _("Cannot store return value of %d bytes long."), len);
2870     }
2871 }
2872 \f
2873
2874 /* This is the variable that is set with "set struct-convention", and
2875    its legitimate values.  */
2876 static const char default_struct_convention[] = "default";
2877 static const char pcc_struct_convention[] = "pcc";
2878 static const char reg_struct_convention[] = "reg";
2879 static const char *const valid_conventions[] =
2880 {
2881   default_struct_convention,
2882   pcc_struct_convention,
2883   reg_struct_convention,
2884   NULL
2885 };
2886 static const char *struct_convention = default_struct_convention;
2887
2888 /* Return non-zero if TYPE, which is assumed to be a structure,
2889    a union type, or an array type, should be returned in registers
2890    for architecture GDBARCH.  */
2891
2892 static int
2893 i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
2894 {
2895   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2896   enum type_code code = TYPE_CODE (type);
2897   int len = TYPE_LENGTH (type);
2898
2899   gdb_assert (code == TYPE_CODE_STRUCT
2900               || code == TYPE_CODE_UNION
2901               || code == TYPE_CODE_ARRAY);
2902
2903   if (struct_convention == pcc_struct_convention
2904       || (struct_convention == default_struct_convention
2905           && tdep->struct_return == pcc_struct_return))
2906     return 0;
2907
2908   /* Structures consisting of a single `float', `double' or 'long
2909      double' member are returned in %st(0).  */
2910   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2911     {
2912       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
2913       if (TYPE_CODE (type) == TYPE_CODE_FLT)
2914         return (len == 4 || len == 8 || len == 12);
2915     }
2916
2917   return (len == 1 || len == 2 || len == 4 || len == 8);
2918 }
2919
2920 /* Determine, for architecture GDBARCH, how a return value of TYPE
2921    should be returned.  If it is supposed to be returned in registers,
2922    and READBUF is non-zero, read the appropriate value from REGCACHE,
2923    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
2924    from WRITEBUF into REGCACHE.  */
2925
2926 static enum return_value_convention
2927 i386_return_value (struct gdbarch *gdbarch, struct value *function,
2928                    struct type *type, struct regcache *regcache,
2929                    gdb_byte *readbuf, const gdb_byte *writebuf)
2930 {
2931   enum type_code code = TYPE_CODE (type);
2932
2933   if (((code == TYPE_CODE_STRUCT
2934         || code == TYPE_CODE_UNION
2935         || code == TYPE_CODE_ARRAY)
2936        && !i386_reg_struct_return_p (gdbarch, type))
2937       /* Complex double and long double uses the struct return covention.  */
2938       || (code == TYPE_CODE_COMPLEX && TYPE_LENGTH (type) == 16)
2939       || (code == TYPE_CODE_COMPLEX && TYPE_LENGTH (type) == 24)
2940       /* 128-bit decimal float uses the struct return convention.  */
2941       || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
2942     {
2943       /* The System V ABI says that:
2944
2945          "A function that returns a structure or union also sets %eax
2946          to the value of the original address of the caller's area
2947          before it returns.  Thus when the caller receives control
2948          again, the address of the returned object resides in register
2949          %eax and can be used to access the object."
2950
2951          So the ABI guarantees that we can always find the return
2952          value just after the function has returned.  */
2953
2954       /* Note that the ABI doesn't mention functions returning arrays,
2955          which is something possible in certain languages such as Ada.
2956          In this case, the value is returned as if it was wrapped in
2957          a record, so the convention applied to records also applies
2958          to arrays.  */
2959
2960       if (readbuf)
2961         {
2962           ULONGEST addr;
2963
2964           regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
2965           read_memory (addr, readbuf, TYPE_LENGTH (type));
2966         }
2967
2968       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2969     }
2970
2971   /* This special case is for structures consisting of a single
2972      `float', `double' or 'long double' member.  These structures are
2973      returned in %st(0).  For these structures, we call ourselves
2974      recursively, changing TYPE into the type of the first member of
2975      the structure.  Since that should work for all structures that
2976      have only one member, we don't bother to check the member's type
2977      here.  */
2978   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2979     {
2980       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
2981       return i386_return_value (gdbarch, function, type, regcache,
2982                                 readbuf, writebuf);
2983     }
2984
2985   if (readbuf)
2986     i386_extract_return_value (gdbarch, type, regcache, readbuf);
2987   if (writebuf)
2988     i386_store_return_value (gdbarch, type, regcache, writebuf);
2989
2990   return RETURN_VALUE_REGISTER_CONVENTION;
2991 }
2992 \f
2993
2994 struct type *
2995 i387_ext_type (struct gdbarch *gdbarch)
2996 {
2997   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2998
2999   if (!tdep->i387_ext_type)
3000     {
3001       tdep->i387_ext_type = tdesc_find_type (gdbarch, "i387_ext");
3002       gdb_assert (tdep->i387_ext_type != NULL);
3003     }
3004
3005   return tdep->i387_ext_type;
3006 }
3007
3008 /* Construct type for pseudo BND registers.  We can't use
3009    tdesc_find_type since a complement of one value has to be used
3010    to describe the upper bound.  */
3011
3012 static struct type *
3013 i386_bnd_type (struct gdbarch *gdbarch)
3014 {
3015   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3016
3017
3018   if (!tdep->i386_bnd_type)
3019     {
3020       struct type *t;
3021       const struct builtin_type *bt = builtin_type (gdbarch);
3022
3023       /* The type we're building is described bellow:  */
3024 #if 0
3025       struct __bound128
3026       {
3027         void *lbound;
3028         void *ubound;           /* One complement of raw ubound field.  */
3029       };
3030 #endif
3031
3032       t = arch_composite_type (gdbarch,
3033                                "__gdb_builtin_type_bound128", TYPE_CODE_STRUCT);
3034
3035       append_composite_type_field (t, "lbound", bt->builtin_data_ptr);
3036       append_composite_type_field (t, "ubound", bt->builtin_data_ptr);
3037
3038       TYPE_NAME (t) = "builtin_type_bound128";
3039       tdep->i386_bnd_type = t;
3040     }
3041
3042   return tdep->i386_bnd_type;
3043 }
3044
3045 /* Construct vector type for pseudo ZMM registers.  We can't use
3046    tdesc_find_type since ZMM isn't described in target description.  */
3047
3048 static struct type *
3049 i386_zmm_type (struct gdbarch *gdbarch)
3050 {
3051   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3052
3053   if (!tdep->i386_zmm_type)
3054     {
3055       const struct builtin_type *bt = builtin_type (gdbarch);
3056
3057       /* The type we're building is this:  */
3058 #if 0
3059       union __gdb_builtin_type_vec512i
3060       {
3061         int128_t uint128[4];
3062         int64_t v4_int64[8];
3063         int32_t v8_int32[16];
3064         int16_t v16_int16[32];
3065         int8_t v32_int8[64];
3066         double v4_double[8];
3067         float v8_float[16];
3068       };
3069 #endif
3070
3071       struct type *t;
3072
3073       t = arch_composite_type (gdbarch,
3074                                "__gdb_builtin_type_vec512i", TYPE_CODE_UNION);
3075       append_composite_type_field (t, "v16_float",
3076                                    init_vector_type (bt->builtin_float, 16));
3077       append_composite_type_field (t, "v8_double",
3078                                    init_vector_type (bt->builtin_double, 8));
3079       append_composite_type_field (t, "v64_int8",
3080                                    init_vector_type (bt->builtin_int8, 64));
3081       append_composite_type_field (t, "v32_int16",
3082                                    init_vector_type (bt->builtin_int16, 32));
3083       append_composite_type_field (t, "v16_int32",
3084                                    init_vector_type (bt->builtin_int32, 16));
3085       append_composite_type_field (t, "v8_int64",
3086                                    init_vector_type (bt->builtin_int64, 8));
3087       append_composite_type_field (t, "v4_int128",
3088                                    init_vector_type (bt->builtin_int128, 4));
3089
3090       TYPE_VECTOR (t) = 1;
3091       TYPE_NAME (t) = "builtin_type_vec512i";
3092       tdep->i386_zmm_type = t;
3093     }
3094
3095   return tdep->i386_zmm_type;
3096 }
3097
3098 /* Construct vector type for pseudo YMM registers.  We can't use
3099    tdesc_find_type since YMM isn't described in target description.  */
3100
3101 static struct type *
3102 i386_ymm_type (struct gdbarch *gdbarch)
3103 {
3104   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3105
3106   if (!tdep->i386_ymm_type)
3107     {
3108       const struct builtin_type *bt = builtin_type (gdbarch);
3109
3110       /* The type we're building is this: */
3111 #if 0
3112       union __gdb_builtin_type_vec256i
3113       {
3114         int128_t uint128[2];
3115         int64_t v2_int64[4];
3116         int32_t v4_int32[8];
3117         int16_t v8_int16[16];
3118         int8_t v16_int8[32];
3119         double v2_double[4];
3120         float v4_float[8];
3121       };
3122 #endif
3123
3124       struct type *t;
3125
3126       t = arch_composite_type (gdbarch,
3127                                "__gdb_builtin_type_vec256i", TYPE_CODE_UNION);
3128       append_composite_type_field (t, "v8_float",
3129                                    init_vector_type (bt->builtin_float, 8));
3130       append_composite_type_field (t, "v4_double",
3131                                    init_vector_type (bt->builtin_double, 4));
3132       append_composite_type_field (t, "v32_int8",
3133                                    init_vector_type (bt->builtin_int8, 32));
3134       append_composite_type_field (t, "v16_int16",
3135                                    init_vector_type (bt->builtin_int16, 16));
3136       append_composite_type_field (t, "v8_int32",
3137                                    init_vector_type (bt->builtin_int32, 8));
3138       append_composite_type_field (t, "v4_int64",
3139                                    init_vector_type (bt->builtin_int64, 4));
3140       append_composite_type_field (t, "v2_int128",
3141                                    init_vector_type (bt->builtin_int128, 2));
3142
3143       TYPE_VECTOR (t) = 1;
3144       TYPE_NAME (t) = "builtin_type_vec256i";
3145       tdep->i386_ymm_type = t;
3146     }
3147
3148   return tdep->i386_ymm_type;
3149 }
3150
3151 /* Construct vector type for MMX registers.  */
3152 static struct type *
3153 i386_mmx_type (struct gdbarch *gdbarch)
3154 {
3155   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3156
3157   if (!tdep->i386_mmx_type)
3158     {
3159       const struct builtin_type *bt = builtin_type (gdbarch);
3160
3161       /* The type we're building is this: */
3162 #if 0
3163       union __gdb_builtin_type_vec64i
3164       {
3165         int64_t uint64;
3166         int32_t v2_int32[2];
3167         int16_t v4_int16[4];
3168         int8_t v8_int8[8];
3169       };
3170 #endif
3171
3172       struct type *t;
3173
3174       t = arch_composite_type (gdbarch,
3175                                "__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
3176
3177       append_composite_type_field (t, "uint64", bt->builtin_int64);
3178       append_composite_type_field (t, "v2_int32",
3179                                    init_vector_type (bt->builtin_int32, 2));
3180       append_composite_type_field (t, "v4_int16",
3181                                    init_vector_type (bt->builtin_int16, 4));
3182       append_composite_type_field (t, "v8_int8",
3183                                    init_vector_type (bt->builtin_int8, 8));
3184
3185       TYPE_VECTOR (t) = 1;
3186       TYPE_NAME (t) = "builtin_type_vec64i";
3187       tdep->i386_mmx_type = t;
3188     }
3189
3190   return tdep->i386_mmx_type;
3191 }
3192
3193 /* Return the GDB type object for the "standard" data type of data in
3194    register REGNUM.  */
3195
3196 struct type *
3197 i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3198 {
3199   if (i386_bnd_regnum_p (gdbarch, regnum))
3200     return i386_bnd_type (gdbarch);
3201   if (i386_mmx_regnum_p (gdbarch, regnum))
3202     return i386_mmx_type (gdbarch);
3203   else if (i386_ymm_regnum_p (gdbarch, regnum))
3204     return i386_ymm_type (gdbarch);
3205   else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3206     return i386_ymm_type (gdbarch);
3207   else if (i386_zmm_regnum_p (gdbarch, regnum))
3208     return i386_zmm_type (gdbarch);
3209   else
3210     {
3211       const struct builtin_type *bt = builtin_type (gdbarch);
3212       if (i386_byte_regnum_p (gdbarch, regnum))
3213         return bt->builtin_int8;
3214       else if (i386_word_regnum_p (gdbarch, regnum))
3215         return bt->builtin_int16;
3216       else if (i386_dword_regnum_p (gdbarch, regnum))
3217         return bt->builtin_int32;
3218       else if (i386_k_regnum_p (gdbarch, regnum))
3219         return bt->builtin_int64;
3220     }
3221
3222   internal_error (__FILE__, __LINE__, _("invalid regnum"));
3223 }
3224
3225 /* Map a cooked register onto a raw register or memory.  For the i386,
3226    the MMX registers need to be mapped onto floating point registers.  */
3227
3228 static int
3229 i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
3230 {
3231   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
3232   int mmxreg, fpreg;
3233   ULONGEST fstat;
3234   int tos;
3235
3236   mmxreg = regnum - tdep->mm0_regnum;
3237   regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
3238   tos = (fstat >> 11) & 0x7;
3239   fpreg = (mmxreg + tos) % 8;
3240
3241   return (I387_ST0_REGNUM (tdep) + fpreg);
3242 }
3243
3244 /* A helper function for us by i386_pseudo_register_read_value and
3245    amd64_pseudo_register_read_value.  It does all the work but reads
3246    the data into an already-allocated value.  */
3247
3248 void
3249 i386_pseudo_register_read_into_value (struct gdbarch *gdbarch,
3250                                       struct regcache *regcache,
3251                                       int regnum,
3252                                       struct value *result_value)
3253 {
3254   gdb_byte raw_buf[MAX_REGISTER_SIZE];
3255   enum register_status status;
3256   gdb_byte *buf = value_contents_raw (result_value);
3257
3258   if (i386_mmx_regnum_p (gdbarch, regnum))
3259     {
3260       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
3261
3262       /* Extract (always little endian).  */
3263       status = regcache_raw_read (regcache, fpnum, raw_buf);
3264       if (status != REG_VALID)
3265         mark_value_bytes_unavailable (result_value, 0,
3266                                       TYPE_LENGTH (value_type (result_value)));
3267       else
3268         memcpy (buf, raw_buf, register_size (gdbarch, regnum));
3269     }
3270   else
3271     {
3272       struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3273       if (i386_bnd_regnum_p (gdbarch, regnum))
3274         {
3275           regnum -= tdep->bnd0_regnum;
3276
3277           /* Extract (always little endian).  Read lower 128bits.  */
3278           status = regcache_raw_read (regcache,
3279                                       I387_BND0R_REGNUM (tdep) + regnum,
3280                                       raw_buf);
3281           if (status != REG_VALID)
3282             mark_value_bytes_unavailable (result_value, 0, 16);
3283           else
3284             {
3285               enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
3286               LONGEST upper, lower;
3287               int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
3288
3289               lower = extract_unsigned_integer (raw_buf, 8, byte_order);
3290               upper = extract_unsigned_integer (raw_buf + 8, 8, byte_order);
3291               upper = ~upper;
3292
3293               memcpy (buf, &lower, size);
3294               memcpy (buf + size, &upper, size);
3295             }
3296         }
3297       else if (i386_k_regnum_p (gdbarch, regnum))
3298         {
3299           regnum -= tdep->k0_regnum;
3300
3301           /* Extract (always little endian).  */
3302           status = regcache_raw_read (regcache,
3303                                       tdep->k0_regnum + regnum,
3304                                       raw_buf);
3305           if (status != REG_VALID)
3306             mark_value_bytes_unavailable (result_value, 0, 8);
3307           else
3308             memcpy (buf, raw_buf, 8);
3309         }
3310       else if (i386_zmm_regnum_p (gdbarch, regnum))
3311         {
3312           regnum -= tdep->zmm0_regnum;
3313
3314           if (regnum < num_lower_zmm_regs)
3315             {
3316               /* Extract (always little endian).  Read lower 128bits.  */
3317               status = regcache_raw_read (regcache,
3318                                           I387_XMM0_REGNUM (tdep) + regnum,
3319                                           raw_buf);
3320               if (status != REG_VALID)
3321                 mark_value_bytes_unavailable (result_value, 0, 16);
3322               else
3323                 memcpy (buf, raw_buf, 16);
3324
3325               /* Extract (always little endian).  Read upper 128bits.  */
3326               status = regcache_raw_read (regcache,
3327                                           tdep->ymm0h_regnum + regnum,
3328                                           raw_buf);
3329               if (status != REG_VALID)
3330                 mark_value_bytes_unavailable (result_value, 16, 16);
3331               else
3332                 memcpy (buf + 16, raw_buf, 16);
3333             }
3334           else
3335             {
3336               /* Extract (always little endian).  Read lower 128bits.  */
3337               status = regcache_raw_read (regcache,
3338                                           I387_XMM16_REGNUM (tdep) + regnum
3339                                           - num_lower_zmm_regs,
3340                                           raw_buf);
3341               if (status != REG_VALID)
3342                 mark_value_bytes_unavailable (result_value, 0, 16);
3343               else
3344                 memcpy (buf, raw_buf, 16);
3345
3346               /* Extract (always little endian).  Read upper 128bits.  */
3347               status = regcache_raw_read (regcache,
3348                                           I387_YMM16H_REGNUM (tdep) + regnum
3349                                           - num_lower_zmm_regs,
3350                                           raw_buf);
3351               if (status != REG_VALID)
3352                 mark_value_bytes_unavailable (result_value, 16, 16);
3353               else
3354                 memcpy (buf + 16, raw_buf, 16);
3355             }
3356
3357           /* Read upper 256bits.  */
3358           status = regcache_raw_read (regcache,
3359                                       tdep->zmm0h_regnum + regnum,
3360                                       raw_buf);
3361           if (status != REG_VALID)
3362             mark_value_bytes_unavailable (result_value, 32, 32);
3363           else
3364             memcpy (buf + 32, raw_buf, 32);
3365         }
3366       else if (i386_ymm_regnum_p (gdbarch, regnum))
3367         {
3368           regnum -= tdep->ymm0_regnum;
3369
3370           /* Extract (always little endian).  Read lower 128bits.  */
3371           status = regcache_raw_read (regcache,
3372                                       I387_XMM0_REGNUM (tdep) + regnum,
3373                                       raw_buf);
3374           if (status != REG_VALID)
3375             mark_value_bytes_unavailable (result_value, 0, 16);
3376           else
3377             memcpy (buf, raw_buf, 16);
3378           /* Read upper 128bits.  */
3379           status = regcache_raw_read (regcache,
3380                                       tdep->ymm0h_regnum + regnum,
3381                                       raw_buf);
3382           if (status != REG_VALID)
3383             mark_value_bytes_unavailable (result_value, 16, 32);
3384           else
3385             memcpy (buf + 16, raw_buf, 16);
3386         }
3387       else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3388         {
3389           regnum -= tdep->ymm16_regnum;
3390           /* Extract (always little endian).  Read lower 128bits.  */
3391           status = regcache_raw_read (regcache,
3392                                       I387_XMM16_REGNUM (tdep) + regnum,
3393                                       raw_buf);
3394           if (status != REG_VALID)
3395             mark_value_bytes_unavailable (result_value, 0, 16);
3396           else
3397             memcpy (buf, raw_buf, 16);
3398           /* Read upper 128bits.  */
3399           status = regcache_raw_read (regcache,
3400                                       tdep->ymm16h_regnum + regnum,
3401                                       raw_buf);
3402           if (status != REG_VALID)
3403             mark_value_bytes_unavailable (result_value, 16, 16);
3404           else
3405             memcpy (buf + 16, raw_buf, 16);
3406         }
3407       else if (i386_word_regnum_p (gdbarch, regnum))
3408         {
3409           int gpnum = regnum - tdep->ax_regnum;
3410
3411           /* Extract (always little endian).  */
3412           status = regcache_raw_read (regcache, gpnum, raw_buf);
3413           if (status != REG_VALID)
3414             mark_value_bytes_unavailable (result_value, 0,
3415                                           TYPE_LENGTH (value_type (result_value)));
3416           else
3417             memcpy (buf, raw_buf, 2);
3418         }
3419       else if (i386_byte_regnum_p (gdbarch, regnum))
3420         {
3421           int gpnum = regnum - tdep->al_regnum;
3422
3423           /* Extract (always little endian).  We read both lower and
3424              upper registers.  */
3425           status = regcache_raw_read (regcache, gpnum % 4, raw_buf);
3426           if (status != REG_VALID)
3427             mark_value_bytes_unavailable (result_value, 0,
3428                                           TYPE_LENGTH (value_type (result_value)));
3429           else if (gpnum >= 4)
3430             memcpy (buf, raw_buf + 1, 1);
3431           else
3432             memcpy (buf, raw_buf, 1);
3433         }
3434       else
3435         internal_error (__FILE__, __LINE__, _("invalid regnum"));
3436     }
3437 }
3438
3439 static struct value *
3440 i386_pseudo_register_read_value (struct gdbarch *gdbarch,
3441                                  struct regcache *regcache,
3442                                  int regnum)
3443 {
3444   struct value *result;
3445
3446   result = allocate_value (register_type (gdbarch, regnum));
3447   VALUE_LVAL (result) = lval_register;
3448   VALUE_REGNUM (result) = regnum;
3449
3450   i386_pseudo_register_read_into_value (gdbarch, regcache, regnum, result);
3451
3452   return result;
3453 }
3454
3455 void
3456 i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
3457                             int regnum, const gdb_byte *buf)
3458 {
3459   gdb_byte raw_buf[MAX_REGISTER_SIZE];
3460
3461   if (i386_mmx_regnum_p (gdbarch, regnum))
3462     {
3463       int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
3464
3465       /* Read ...  */
3466       regcache_raw_read (regcache, fpnum, raw_buf);
3467       /* ... Modify ... (always little endian).  */
3468       memcpy (raw_buf, buf, register_size (gdbarch, regnum));
3469       /* ... Write.  */
3470       regcache_raw_write (regcache, fpnum, raw_buf);
3471     }
3472   else
3473     {
3474       struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3475
3476       if (i386_bnd_regnum_p (gdbarch, regnum))
3477         {
3478           ULONGEST upper, lower;
3479           int size = TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr);
3480           enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
3481
3482           /* New values from input value.  */
3483           regnum -= tdep->bnd0_regnum;
3484           lower = extract_unsigned_integer (buf, size, byte_order);
3485           upper = extract_unsigned_integer (buf + size, size, byte_order);
3486
3487           /* Fetching register buffer.  */
3488           regcache_raw_read (regcache,
3489                              I387_BND0R_REGNUM (tdep) + regnum,
3490                              raw_buf);
3491
3492           upper = ~upper;
3493
3494           /* Set register bits.  */
3495           memcpy (raw_buf, &lower, 8);
3496           memcpy (raw_buf + 8, &upper, 8);
3497
3498
3499           regcache_raw_write (regcache,
3500                               I387_BND0R_REGNUM (tdep) + regnum,
3501                               raw_buf);
3502         }
3503       else if (i386_k_regnum_p (gdbarch, regnum))
3504         {
3505           regnum -= tdep->k0_regnum;
3506
3507           regcache_raw_write (regcache,
3508                               tdep->k0_regnum + regnum,
3509                               buf);
3510         }
3511       else if (i386_zmm_regnum_p (gdbarch, regnum))
3512         {
3513           regnum -= tdep->zmm0_regnum;
3514
3515           if (regnum < num_lower_zmm_regs)
3516             {
3517               /* Write lower 128bits.  */
3518               regcache_raw_write (regcache,
3519                                   I387_XMM0_REGNUM (tdep) + regnum,
3520                                   buf);
3521               /* Write upper 128bits.  */
3522               regcache_raw_write (regcache,
3523                                   I387_YMM0_REGNUM (tdep) + regnum,
3524                                   buf + 16);
3525             }
3526           else
3527             {
3528               /* Write lower 128bits.  */
3529               regcache_raw_write (regcache,
3530                                   I387_XMM16_REGNUM (tdep) + regnum
3531                                   - num_lower_zmm_regs,
3532                                   buf);
3533               /* Write upper 128bits.  */
3534               regcache_raw_write (regcache,
3535                                   I387_YMM16H_REGNUM (tdep) + regnum
3536                                   - num_lower_zmm_regs,
3537                                   buf + 16);
3538             }
3539           /* Write upper 256bits.  */
3540           regcache_raw_write (regcache,
3541                               tdep->zmm0h_regnum + regnum,
3542                               buf + 32);
3543         }
3544       else if (i386_ymm_regnum_p (gdbarch, regnum))
3545         {
3546           regnum -= tdep->ymm0_regnum;
3547
3548           /* ... Write lower 128bits.  */
3549           regcache_raw_write (regcache,
3550                              I387_XMM0_REGNUM (tdep) + regnum,
3551                              buf);
3552           /* ... Write upper 128bits.  */
3553           regcache_raw_write (regcache,
3554                              tdep->ymm0h_regnum + regnum,
3555                              buf + 16);
3556         }
3557       else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3558         {
3559           regnum -= tdep->ymm16_regnum;
3560
3561           /* ... Write lower 128bits.  */
3562           regcache_raw_write (regcache,
3563                               I387_XMM16_REGNUM (tdep) + regnum,
3564                               buf);
3565           /* ... Write upper 128bits.  */
3566           regcache_raw_write (regcache,
3567                               tdep->ymm16h_regnum + regnum,
3568                               buf + 16);
3569         }
3570       else if (i386_word_regnum_p (gdbarch, regnum))
3571         {
3572           int gpnum = regnum - tdep->ax_regnum;
3573
3574           /* Read ...  */
3575           regcache_raw_read (regcache, gpnum, raw_buf);
3576           /* ... Modify ... (always little endian).  */
3577           memcpy (raw_buf, buf, 2);
3578           /* ... Write.  */
3579           regcache_raw_write (regcache, gpnum, raw_buf);
3580         }
3581       else if (i386_byte_regnum_p (gdbarch, regnum))
3582         {
3583           int gpnum = regnum - tdep->al_regnum;
3584
3585           /* Read ...  We read both lower and upper registers.  */
3586           regcache_raw_read (regcache, gpnum % 4, raw_buf);
3587           /* ... Modify ... (always little endian).  */
3588           if (gpnum >= 4)
3589             memcpy (raw_buf + 1, buf, 1);
3590           else
3591             memcpy (raw_buf, buf, 1);
3592           /* ... Write.  */
3593           regcache_raw_write (regcache, gpnum % 4, raw_buf);
3594         }
3595       else
3596         internal_error (__FILE__, __LINE__, _("invalid regnum"));
3597     }
3598 }
3599
3600 /* Implement the 'ax_pseudo_register_collect' gdbarch method.  */
3601
3602 int
3603 i386_ax_pseudo_register_collect (struct gdbarch *gdbarch,
3604                                  struct agent_expr *ax, int regnum)
3605 {
3606   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3607
3608   if (i386_mmx_regnum_p (gdbarch, regnum))
3609     {
3610       /* MMX to FPU register mapping depends on current TOS.  Let's just
3611          not care and collect everything...  */
3612       int i;
3613
3614       ax_reg_mask (ax, I387_FSTAT_REGNUM (tdep));
3615       for (i = 0; i < 8; i++)
3616         ax_reg_mask (ax, I387_ST0_REGNUM (tdep) + i);
3617       return 0;
3618     }
3619   else if (i386_bnd_regnum_p (gdbarch, regnum))
3620     {
3621       regnum -= tdep->bnd0_regnum;
3622       ax_reg_mask (ax, I387_BND0R_REGNUM (tdep) + regnum);
3623       return 0;
3624     }
3625   else if (i386_k_regnum_p (gdbarch, regnum))
3626     {
3627       regnum -= tdep->k0_regnum;
3628       ax_reg_mask (ax, tdep->k0_regnum + regnum);
3629       return 0;
3630     }
3631   else if (i386_zmm_regnum_p (gdbarch, regnum))
3632     {
3633       regnum -= tdep->zmm0_regnum;
3634       if (regnum < num_lower_zmm_regs)
3635         {
3636           ax_reg_mask (ax, I387_XMM0_REGNUM (tdep) + regnum);
3637           ax_reg_mask (ax, tdep->ymm0h_regnum + regnum);
3638         }
3639       else
3640         {
3641           ax_reg_mask (ax, I387_XMM16_REGNUM (tdep) + regnum
3642                            - num_lower_zmm_regs);
3643           ax_reg_mask (ax, I387_YMM16H_REGNUM (tdep) + regnum
3644                            - num_lower_zmm_regs);
3645         }
3646       ax_reg_mask (ax, tdep->zmm0h_regnum + regnum);
3647       return 0;
3648     }
3649   else if (i386_ymm_regnum_p (gdbarch, regnum))
3650     {
3651       regnum -= tdep->ymm0_regnum;
3652       ax_reg_mask (ax, I387_XMM0_REGNUM (tdep) + regnum);
3653       ax_reg_mask (ax, tdep->ymm0h_regnum + regnum);
3654       return 0;
3655     }
3656   else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
3657     {
3658       regnum -= tdep->ymm16_regnum;
3659       ax_reg_mask (ax, I387_XMM16_REGNUM (tdep) + regnum);
3660       ax_reg_mask (ax, tdep->ymm16h_regnum + regnum);
3661       return 0;
3662     }
3663   else if (i386_word_regnum_p (gdbarch, regnum))
3664     {
3665       int gpnum = regnum - tdep->ax_regnum;
3666
3667       ax_reg_mask (ax, gpnum);
3668       return 0;
3669     }
3670   else if (i386_byte_regnum_p (gdbarch, regnum))
3671     {
3672       int gpnum = regnum - tdep->al_regnum;
3673
3674       ax_reg_mask (ax, gpnum % 4);
3675       return 0;
3676     }
3677   else
3678     internal_error (__FILE__, __LINE__, _("invalid regnum"));
3679   return 1;
3680 }
3681 \f
3682
3683 /* Return the register number of the register allocated by GCC after
3684    REGNUM, or -1 if there is no such register.  */
3685
3686 static int
3687 i386_next_regnum (int regnum)
3688 {
3689   /* GCC allocates the registers in the order:
3690
3691      %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
3692
3693      Since storing a variable in %esp doesn't make any sense we return
3694      -1 for %ebp and for %esp itself.  */
3695   static int next_regnum[] =
3696   {
3697     I386_EDX_REGNUM,            /* Slot for %eax.  */
3698     I386_EBX_REGNUM,            /* Slot for %ecx.  */
3699     I386_ECX_REGNUM,            /* Slot for %edx.  */
3700     I386_ESI_REGNUM,            /* Slot for %ebx.  */
3701     -1, -1,                     /* Slots for %esp and %ebp.  */
3702     I386_EDI_REGNUM,            /* Slot for %esi.  */
3703     I386_EBP_REGNUM             /* Slot for %edi.  */
3704   };
3705
3706   if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
3707     return next_regnum[regnum];
3708
3709   return -1;
3710 }
3711
3712 /* Return nonzero if a value of type TYPE stored in register REGNUM
3713    needs any special handling.  */
3714
3715 static int
3716 i386_convert_register_p (struct gdbarch *gdbarch,
3717                          int regnum, struct type *type)
3718 {
3719   int len = TYPE_LENGTH (type);
3720
3721   /* Values may be spread across multiple registers.  Most debugging
3722      formats aren't expressive enough to specify the locations, so
3723      some heuristics is involved.  Right now we only handle types that
3724      have a length that is a multiple of the word size, since GCC
3725      doesn't seem to put any other types into registers.  */
3726   if (len > 4 && len % 4 == 0)
3727     {
3728       int last_regnum = regnum;
3729
3730       while (len > 4)
3731         {
3732           last_regnum = i386_next_regnum (last_regnum);
3733           len -= 4;
3734         }
3735
3736       if (last_regnum != -1)
3737         return 1;
3738     }
3739
3740   return i387_convert_register_p (gdbarch, regnum, type);
3741 }
3742
3743 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
3744    return its contents in TO.  */
3745
3746 static int
3747 i386_register_to_value (struct frame_info *frame, int regnum,
3748                         struct type *type, gdb_byte *to,
3749                         int *optimizedp, int *unavailablep)
3750 {
3751   struct gdbarch *gdbarch = get_frame_arch (frame);
3752   int len = TYPE_LENGTH (type);
3753
3754   if (i386_fp_regnum_p (gdbarch, regnum))
3755     return i387_register_to_value (frame, regnum, type, to,
3756                                    optimizedp, unavailablep);
3757
3758   /* Read a value spread across multiple registers.  */
3759
3760   gdb_assert (len > 4 && len % 4 == 0);
3761
3762   while (len > 0)
3763     {
3764       gdb_assert (regnum != -1);
3765       gdb_assert (register_size (gdbarch, regnum) == 4);
3766
3767       if (!get_frame_register_bytes (frame, regnum, 0,
3768                                      register_size (gdbarch, regnum),
3769                                      to, optimizedp, unavailablep))
3770         return 0;
3771
3772       regnum = i386_next_regnum (regnum);
3773       len -= 4;
3774       to += 4;
3775     }
3776
3777   *optimizedp = *unavailablep = 0;
3778   return 1;
3779 }
3780
3781 /* Write the contents FROM of a value of type TYPE into register
3782    REGNUM in frame FRAME.  */
3783
3784 static void
3785 i386_value_to_register (struct frame_info *frame, int regnum,
3786                         struct type *type, const gdb_byte *from)
3787 {
3788   int len = TYPE_LENGTH (type);
3789
3790   if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
3791     {
3792       i387_value_to_register (frame, regnum, type, from);
3793       return;
3794     }
3795
3796   /* Write a value spread across multiple registers.  */
3797
3798   gdb_assert (len > 4 && len % 4 == 0);
3799
3800   while (len > 0)
3801     {
3802       gdb_assert (regnum != -1);
3803       gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
3804
3805       put_frame_register (frame, regnum, from);
3806       regnum = i386_next_regnum (regnum);
3807       len -= 4;
3808       from += 4;
3809     }
3810 }
3811 \f
3812 /* Supply register REGNUM from the buffer specified by GREGS and LEN
3813    in the general-purpose register set REGSET to register cache
3814    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
3815
3816 void
3817 i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
3818                      int regnum, const void *gregs, size_t len)
3819 {
3820   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3821   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3822   const gdb_byte *regs = (const gdb_byte *) gregs;
3823   int i;
3824
3825   gdb_assert (len >= tdep->sizeof_gregset);
3826
3827   for (i = 0; i < tdep->gregset_num_regs; i++)
3828     {
3829       if ((regnum == i || regnum == -1)
3830           && tdep->gregset_reg_offset[i] != -1)
3831         regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
3832     }
3833 }
3834
3835 /* Collect register REGNUM from the register cache REGCACHE and store
3836    it in the buffer specified by GREGS and LEN as described by the
3837    general-purpose register set REGSET.  If REGNUM is -1, do this for
3838    all registers in REGSET.  */
3839
3840 static void
3841 i386_collect_gregset (const struct regset *regset,
3842                       const struct regcache *regcache,
3843                       int regnum, void *gregs, size_t len)
3844 {
3845   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3846   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3847   gdb_byte *regs = (gdb_byte *) gregs;
3848   int i;
3849
3850   gdb_assert (len >= tdep->sizeof_gregset);
3851
3852   for (i = 0; i < tdep->gregset_num_regs; i++)
3853     {
3854       if ((regnum == i || regnum == -1)
3855           && tdep->gregset_reg_offset[i] != -1)
3856         regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]);
3857     }
3858 }
3859
3860 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
3861    in the floating-point register set REGSET to register cache
3862    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
3863
3864 static void
3865 i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
3866                       int regnum, const void *fpregs, size_t len)
3867 {
3868   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3869   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3870
3871   if (len == I387_SIZEOF_FXSAVE)
3872     {
3873       i387_supply_fxsave (regcache, regnum, fpregs);
3874       return;
3875     }
3876
3877   gdb_assert (len >= tdep->sizeof_fpregset);
3878   i387_supply_fsave (regcache, regnum, fpregs);
3879 }
3880
3881 /* Collect register REGNUM from the register cache REGCACHE and store
3882    it in the buffer specified by FPREGS and LEN as described by the
3883    floating-point register set REGSET.  If REGNUM is -1, do this for
3884    all registers in REGSET.  */
3885
3886 static void
3887 i386_collect_fpregset (const struct regset *regset,
3888                        const struct regcache *regcache,
3889                        int regnum, void *fpregs, size_t len)
3890 {
3891   struct gdbarch *gdbarch = get_regcache_arch (regcache);
3892   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3893
3894   if (len == I387_SIZEOF_FXSAVE)
3895     {
3896       i387_collect_fxsave (regcache, regnum, fpregs);
3897       return;
3898     }
3899
3900   gdb_assert (len >= tdep->sizeof_fpregset);
3901   i387_collect_fsave (regcache, regnum, fpregs);
3902 }
3903
3904 /* Register set definitions.  */
3905
3906 const struct regset i386_gregset =
3907   {
3908     NULL, i386_supply_gregset, i386_collect_gregset
3909   };
3910
3911 const struct regset i386_fpregset =
3912   {
3913     NULL, i386_supply_fpregset, i386_collect_fpregset
3914   };
3915
3916 /* Default iterator over core file register note sections.  */
3917
3918 void
3919 i386_iterate_over_regset_sections (struct gdbarch *gdbarch,
3920                                    iterate_over_regset_sections_cb *cb,
3921                                    void *cb_data,
3922                                    const struct regcache *regcache)
3923 {
3924   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3925
3926   cb (".reg", tdep->sizeof_gregset, &i386_gregset, NULL, cb_data);
3927   if (tdep->sizeof_fpregset)
3928     cb (".reg2", tdep->sizeof_fpregset, tdep->fpregset, NULL, cb_data);
3929 }
3930 \f
3931
3932 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
3933
3934 CORE_ADDR
3935 i386_pe_skip_trampoline_code (struct frame_info *frame,
3936                               CORE_ADDR pc, char *name)
3937 {
3938   struct gdbarch *gdbarch = get_frame_arch (frame);
3939   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3940
3941   /* jmp *(dest) */
3942   if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
3943     {
3944       unsigned long indirect =
3945         read_memory_unsigned_integer (pc + 2, 4, byte_order);
3946       struct minimal_symbol *indsym =
3947         indirect ? lookup_minimal_symbol_by_pc (indirect).minsym : 0;
3948       const char *symname = indsym ? MSYMBOL_LINKAGE_NAME (indsym) : 0;
3949
3950       if (symname)
3951         {
3952           if (startswith (symname, "__imp_")
3953               || startswith (symname, "_imp_"))
3954             return name ? 1 :
3955                    read_memory_unsigned_integer (indirect, 4, byte_order);
3956         }
3957     }
3958   return 0;                     /* Not a trampoline.  */
3959 }
3960 \f
3961
3962 /* Return whether the THIS_FRAME corresponds to a sigtramp
3963    routine.  */
3964
3965 int
3966 i386_sigtramp_p (struct frame_info *this_frame)
3967 {
3968   CORE_ADDR pc = get_frame_pc (this_frame);
3969   const char *name;
3970
3971   find_pc_partial_function (pc, &name, NULL, NULL);
3972   return (name && strcmp ("_sigtramp", name) == 0);
3973 }
3974 \f
3975
3976 /* We have two flavours of disassembly.  The machinery on this page
3977    deals with switching between those.  */
3978
3979 static int
3980 i386_print_insn (bfd_vma pc, struct disassemble_info *info)
3981 {
3982   gdb_assert (disassembly_flavor == att_flavor
3983               || disassembly_flavor == intel_flavor);
3984
3985   /* FIXME: kettenis/20020915: Until disassembler_options is properly
3986      constified, cast to prevent a compiler warning.  */
3987   info->disassembler_options = (char *) disassembly_flavor;
3988
3989   return print_insn_i386 (pc, info);
3990 }
3991 \f
3992
3993 /* There are a few i386 architecture variants that differ only
3994    slightly from the generic i386 target.  For now, we don't give them
3995    their own source file, but include them here.  As a consequence,
3996    they'll always be included.  */
3997
3998 /* System V Release 4 (SVR4).  */
3999
4000 /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
4001    routine.  */
4002
4003 static int
4004 i386_svr4_sigtramp_p (struct frame_info *this_frame)
4005 {
4006   CORE_ADDR pc = get_frame_pc (this_frame);
4007   const char *name;
4008
4009   /* The origin of these symbols is currently unknown.  */
4010   find_pc_partial_function (pc, &name, NULL, NULL);
4011   return (name && (strcmp ("_sigreturn", name) == 0
4012                    || strcmp ("sigvechandler", name) == 0));
4013 }
4014
4015 /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
4016    address of the associated sigcontext (ucontext) structure.  */
4017
4018 static CORE_ADDR
4019 i386_svr4_sigcontext_addr (struct frame_info *this_frame)
4020 {
4021   struct gdbarch *gdbarch = get_frame_arch (this_frame);
4022   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4023   gdb_byte buf[4];
4024   CORE_ADDR sp;
4025
4026   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
4027   sp = extract_unsigned_integer (buf, 4, byte_order);
4028
4029   return read_memory_unsigned_integer (sp + 8, 4, byte_order);
4030 }
4031
4032 \f
4033
4034 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
4035    gdbarch.h.  */
4036
4037 int
4038 i386_stap_is_single_operand (struct gdbarch *gdbarch, const char *s)
4039 {
4040   return (*s == '$' /* Literal number.  */
4041           || (isdigit (*s) && s[1] == '(' && s[2] == '%') /* Displacement.  */
4042           || (*s == '(' && s[1] == '%') /* Register indirection.  */
4043           || (*s == '%' && isalpha (s[1]))); /* Register access.  */
4044 }
4045
4046 /* Helper function for i386_stap_parse_special_token.
4047
4048    This function parses operands of the form `-8+3+1(%rbp)', which
4049    must be interpreted as `*(-8 + 3 - 1 + (void *) $eax)'.
4050
4051    Return 1 if the operand was parsed successfully, zero
4052    otherwise.  */
4053
4054 static int
4055 i386_stap_parse_special_token_triplet (struct gdbarch *gdbarch,
4056                                        struct stap_parse_info *p)
4057 {
4058   const char *s = p->arg;
4059
4060   if (isdigit (*s) || *s == '-' || *s == '+')
4061     {
4062       int got_minus[3];
4063       int i;
4064       long displacements[3];
4065       const char *start;
4066       char *regname;
4067       int len;
4068       struct stoken str;
4069       char *endp;
4070
4071       got_minus[0] = 0;
4072       if (*s == '+')
4073         ++s;
4074       else if (*s == '-')
4075         {
4076           ++s;
4077           got_minus[0] = 1;
4078         }
4079
4080       if (!isdigit ((unsigned char) *s))
4081         return 0;
4082
4083       displacements[0] = strtol (s, &endp, 10);
4084       s = endp;
4085
4086       if (*s != '+' && *s != '-')
4087         {
4088           /* We are not dealing with a triplet.  */
4089           return 0;
4090         }
4091
4092       got_minus[1] = 0;
4093       if (*s == '+')
4094         ++s;
4095       else
4096         {
4097           ++s;
4098           got_minus[1] = 1;
4099         }
4100
4101       if (!isdigit ((unsigned char) *s))
4102         return 0;
4103
4104       displacements[1] = strtol (s, &endp, 10);
4105       s = endp;
4106
4107       if (*s != '+' && *s != '-')
4108         {
4109           /* We are not dealing with a triplet.  */
4110           return 0;
4111         }
4112
4113       got_minus[2] = 0;
4114       if (*s == '+')
4115         ++s;
4116       else
4117         {
4118           ++s;
4119           got_minus[2] = 1;
4120         }
4121
4122       if (!isdigit ((unsigned char) *s))
4123         return 0;
4124
4125       displacements[2] = strtol (s, &endp, 10);
4126       s = endp;
4127
4128       if (*s != '(' || s[1] != '%')
4129         return 0;
4130
4131       s += 2;
4132       start = s;
4133
4134       while (isalnum (*s))
4135         ++s;
4136
4137       if (*s++ != ')')
4138         return 0;
4139
4140       len = s - start - 1;
4141       regname = (char *) alloca (len + 1);
4142
4143       strncpy (regname, start, len);
4144       regname[len] = '\0';
4145
4146       if (user_reg_map_name_to_regnum (gdbarch, regname, len) == -1)
4147         error (_("Invalid register name `%s' on expression `%s'."),
4148                regname, p->saved_arg);
4149
4150       for (i = 0; i < 3; i++)
4151         {
4152           write_exp_elt_opcode (&p->pstate, OP_LONG);
4153           write_exp_elt_type
4154             (&p->pstate, builtin_type (gdbarch)->builtin_long);
4155           write_exp_elt_longcst (&p->pstate, displacements[i]);
4156           write_exp_elt_opcode (&p->pstate, OP_LONG);
4157           if (got_minus[i])
4158             write_exp_elt_opcode (&p->pstate, UNOP_NEG);
4159         }
4160
4161       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4162       str.ptr = regname;
4163       str.length = len;
4164       write_exp_string (&p->pstate, str);
4165       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4166
4167       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4168       write_exp_elt_type (&p->pstate,
4169                           builtin_type (gdbarch)->builtin_data_ptr);
4170       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4171
4172       write_exp_elt_opcode (&p->pstate, BINOP_ADD);
4173       write_exp_elt_opcode (&p->pstate, BINOP_ADD);
4174       write_exp_elt_opcode (&p->pstate, BINOP_ADD);
4175
4176       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4177       write_exp_elt_type (&p->pstate,
4178                           lookup_pointer_type (p->arg_type));
4179       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4180
4181       write_exp_elt_opcode (&p->pstate, UNOP_IND);
4182
4183       p->arg = s;
4184
4185       return 1;
4186     }
4187
4188   return 0;
4189 }
4190
4191 /* Helper function for i386_stap_parse_special_token.
4192
4193    This function parses operands of the form `register base +
4194    (register index * size) + offset', as represented in
4195    `(%rcx,%rax,8)', or `[OFFSET](BASE_REG,INDEX_REG[,SIZE])'.
4196
4197    Return 1 if the operand was parsed successfully, zero
4198    otherwise.  */
4199
4200 static int
4201 i386_stap_parse_special_token_three_arg_disp (struct gdbarch *gdbarch,
4202                                               struct stap_parse_info *p)
4203 {
4204   const char *s = p->arg;
4205
4206   if (isdigit (*s) || *s == '(' || *s == '-' || *s == '+')
4207     {
4208       int offset_minus = 0;
4209       long offset = 0;
4210       int size_minus = 0;
4211       long size = 0;
4212       const char *start;
4213       char *base;
4214       int len_base;
4215       char *index;
4216       int len_index;
4217       struct stoken base_token, index_token;
4218
4219       if (*s == '+')
4220         ++s;
4221       else if (*s == '-')
4222         {
4223           ++s;
4224           offset_minus = 1;
4225         }
4226
4227       if (offset_minus && !isdigit (*s))
4228         return 0;
4229
4230       if (isdigit (*s))
4231         {
4232           char *endp;
4233
4234           offset = strtol (s, &endp, 10);
4235           s = endp;
4236         }
4237
4238       if (*s != '(' || s[1] != '%')
4239         return 0;
4240
4241       s += 2;
4242       start = s;
4243
4244       while (isalnum (*s))
4245         ++s;
4246
4247       if (*s != ',' || s[1] != '%')
4248         return 0;
4249
4250       len_base = s - start;
4251       base = (char *) alloca (len_base + 1);
4252       strncpy (base, start, len_base);
4253       base[len_base] = '\0';
4254
4255       if (user_reg_map_name_to_regnum (gdbarch, base, len_base) == -1)
4256         error (_("Invalid register name `%s' on expression `%s'."),
4257                base, p->saved_arg);
4258
4259       s += 2;
4260       start = s;
4261
4262       while (isalnum (*s))
4263         ++s;
4264
4265       len_index = s - start;
4266       index = (char *) alloca (len_index + 1);
4267       strncpy (index, start, len_index);
4268       index[len_index] = '\0';
4269
4270       if (user_reg_map_name_to_regnum (gdbarch, index, len_index) == -1)
4271         error (_("Invalid register name `%s' on expression `%s'."),
4272                index, p->saved_arg);
4273
4274       if (*s != ',' && *s != ')')
4275         return 0;
4276
4277       if (*s == ',')
4278         {
4279           char *endp;
4280
4281           ++s;
4282           if (*s == '+')
4283             ++s;
4284           else if (*s == '-')
4285             {
4286               ++s;
4287               size_minus = 1;
4288             }
4289
4290           size = strtol (s, &endp, 10);
4291           s = endp;
4292
4293           if (*s != ')')
4294             return 0;
4295         }
4296
4297       ++s;
4298
4299       if (offset)
4300         {
4301           write_exp_elt_opcode (&p->pstate, OP_LONG);
4302           write_exp_elt_type (&p->pstate,
4303                               builtin_type (gdbarch)->builtin_long);
4304           write_exp_elt_longcst (&p->pstate, offset);
4305           write_exp_elt_opcode (&p->pstate, OP_LONG);
4306           if (offset_minus)
4307             write_exp_elt_opcode (&p->pstate, UNOP_NEG);
4308         }
4309
4310       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4311       base_token.ptr = base;
4312       base_token.length = len_base;
4313       write_exp_string (&p->pstate, base_token);
4314       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4315
4316       if (offset)
4317         write_exp_elt_opcode (&p->pstate, BINOP_ADD);
4318
4319       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4320       index_token.ptr = index;
4321       index_token.length = len_index;
4322       write_exp_string (&p->pstate, index_token);
4323       write_exp_elt_opcode (&p->pstate, OP_REGISTER);
4324
4325       if (size)
4326         {
4327           write_exp_elt_opcode (&p->pstate, OP_LONG);
4328           write_exp_elt_type (&p->pstate,
4329                               builtin_type (gdbarch)->builtin_long);
4330           write_exp_elt_longcst (&p->pstate, size);
4331           write_exp_elt_opcode (&p->pstate, OP_LONG);
4332           if (size_minus)
4333             write_exp_elt_opcode (&p->pstate, UNOP_NEG);
4334           write_exp_elt_opcode (&p->pstate, BINOP_MUL);
4335         }
4336
4337       write_exp_elt_opcode (&p->pstate, BINOP_ADD);
4338
4339       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4340       write_exp_elt_type (&p->pstate,
4341                           lookup_pointer_type (p->arg_type));
4342       write_exp_elt_opcode (&p->pstate, UNOP_CAST);
4343
4344       write_exp_elt_opcode (&p->pstate, UNOP_IND);
4345
4346       p->arg = s;
4347
4348       return 1;
4349     }
4350
4351   return 0;
4352 }
4353
4354 /* Implementation of `gdbarch_stap_parse_special_token', as defined in
4355    gdbarch.h.  */
4356
4357 int
4358 i386_stap_parse_special_token (struct gdbarch *gdbarch,
4359                                struct stap_parse_info *p)
4360 {
4361   /* In order to parse special tokens, we use a state-machine that go
4362      through every known token and try to get a match.  */
4363   enum
4364     {
4365       TRIPLET,
4366       THREE_ARG_DISPLACEMENT,
4367       DONE
4368     };
4369   int current_state;
4370
4371   current_state = TRIPLET;
4372
4373   /* The special tokens to be parsed here are:
4374
4375      - `register base + (register index * size) + offset', as represented
4376      in `(%rcx,%rax,8)', or `[OFFSET](BASE_REG,INDEX_REG[,SIZE])'.
4377
4378      - Operands of the form `-8+3+1(%rbp)', which must be interpreted as
4379      `*(-8 + 3 - 1 + (void *) $eax)'.  */
4380
4381   while (current_state != DONE)
4382     {
4383       switch (current_state)
4384         {
4385         case TRIPLET:
4386           if (i386_stap_parse_special_token_triplet (gdbarch, p))
4387             return 1;
4388           break;
4389
4390         case THREE_ARG_DISPLACEMENT:
4391           if (i386_stap_parse_special_token_three_arg_disp (gdbarch, p))
4392             return 1;
4393           break;
4394         }
4395
4396       /* Advancing to the next state.  */
4397       ++current_state;
4398     }
4399
4400   return 0;
4401 }
4402
4403 \f
4404
4405 /* gdbarch gnu_triplet_regexp method.  Both arches are acceptable as GDB always
4406    also supplies -m64 or -m32 by gdbarch_gcc_target_options.  */
4407
4408 static const char *
4409 i386_gnu_triplet_regexp (struct gdbarch *gdbarch)
4410 {
4411   return "(x86_64|i.86)";
4412 }
4413
4414 \f
4415
4416 /* Generic ELF.  */
4417
4418 void
4419 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4420 {
4421   static const char *const stap_integer_prefixes[] = { "$", NULL };
4422   static const char *const stap_register_prefixes[] = { "%", NULL };
4423   static const char *const stap_register_indirection_prefixes[] = { "(",
4424                                                                     NULL };
4425   static const char *const stap_register_indirection_suffixes[] = { ")",
4426                                                                     NULL };
4427
4428   /* We typically use stabs-in-ELF with the SVR4 register numbering.  */
4429   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
4430
4431   /* Registering SystemTap handlers.  */
4432   set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
4433   set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
4434   set_gdbarch_stap_register_indirection_prefixes (gdbarch,
4435                                           stap_register_indirection_prefixes);
4436   set_gdbarch_stap_register_indirection_suffixes (gdbarch,
4437                                           stap_register_indirection_suffixes);
4438   set_gdbarch_stap_is_single_operand (gdbarch,
4439                                       i386_stap_is_single_operand);
4440   set_gdbarch_stap_parse_special_token (gdbarch,
4441                                         i386_stap_parse_special_token);
4442
4443   set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
4444 }
4445
4446 /* System V Release 4 (SVR4).  */
4447
4448 void
4449 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4450 {
4451   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4452
4453   /* System V Release 4 uses ELF.  */
4454   i386_elf_init_abi (info, gdbarch);
4455
4456   /* System V Release 4 has shared libraries.  */
4457   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
4458
4459   tdep->sigtramp_p = i386_svr4_sigtramp_p;
4460   tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
4461   tdep->sc_pc_offset = 36 + 14 * 4;
4462   tdep->sc_sp_offset = 36 + 17 * 4;
4463
4464   tdep->jb_pc_offset = 20;
4465 }
4466
4467 /* DJGPP.  */
4468
4469 static void
4470 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4471 {
4472   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4473
4474   /* DJGPP doesn't have any special frames for signal handlers.  */
4475   tdep->sigtramp_p = NULL;
4476
4477   tdep->jb_pc_offset = 36;
4478
4479   /* DJGPP does not support the SSE registers.  */
4480   if (! tdesc_has_registers (info.target_desc))
4481     tdep->tdesc = tdesc_i386_mmx;
4482
4483   /* Native compiler is GCC, which uses the SVR4 register numbering
4484      even in COFF and STABS.  See the comment in i386_gdbarch_init,
4485      before the calls to set_gdbarch_stab_reg_to_regnum and
4486      set_gdbarch_sdb_reg_to_regnum.  */
4487   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
4488   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
4489
4490   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
4491
4492   set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
4493 }
4494 \f
4495
4496 /* i386 register groups.  In addition to the normal groups, add "mmx"
4497    and "sse".  */
4498
4499 static struct reggroup *i386_sse_reggroup;
4500 static struct reggroup *i386_mmx_reggroup;
4501
4502 static void
4503 i386_init_reggroups (void)
4504 {
4505   i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
4506   i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
4507 }
4508
4509 static void
4510 i386_add_reggroups (struct gdbarch *gdbarch)
4511 {
4512   reggroup_add (gdbarch, i386_sse_reggroup);
4513   reggroup_add (gdbarch, i386_mmx_reggroup);
4514   reggroup_add (gdbarch, general_reggroup);
4515   reggroup_add (gdbarch, float_reggroup);
4516   reggroup_add (gdbarch, all_reggroup);
4517   reggroup_add (gdbarch, save_reggroup);
4518   reggroup_add (gdbarch, restore_reggroup);
4519   reggroup_add (gdbarch, vector_reggroup);
4520   reggroup_add (gdbarch, system_reggroup);
4521 }
4522
4523 int
4524 i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
4525                           struct reggroup *group)
4526 {
4527   const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4528   int fp_regnum_p, mmx_regnum_p, xmm_regnum_p, mxcsr_regnum_p,
4529       ymm_regnum_p, ymmh_regnum_p, ymm_avx512_regnum_p, ymmh_avx512_regnum_p,
4530       bndr_regnum_p, bnd_regnum_p, k_regnum_p, zmm_regnum_p, zmmh_regnum_p,
4531       zmm_avx512_regnum_p, mpx_ctrl_regnum_p, xmm_avx512_regnum_p,
4532       avx512_p, avx_p, sse_p;
4533
4534   /* Don't include pseudo registers, except for MMX, in any register
4535      groups.  */
4536   if (i386_byte_regnum_p (gdbarch, regnum))
4537     return 0;
4538
4539   if (i386_word_regnum_p (gdbarch, regnum))
4540     return 0;
4541
4542   if (i386_dword_regnum_p (gdbarch, regnum))
4543     return 0;
4544
4545   mmx_regnum_p = i386_mmx_regnum_p (gdbarch, regnum);
4546   if (group == i386_mmx_reggroup)
4547     return mmx_regnum_p;
4548
4549   xmm_regnum_p = i386_xmm_regnum_p (gdbarch, regnum);
4550   xmm_avx512_regnum_p = i386_xmm_avx512_regnum_p (gdbarch, regnum);
4551   mxcsr_regnum_p = i386_mxcsr_regnum_p (gdbarch, regnum);
4552   if (group == i386_sse_reggroup)
4553     return xmm_regnum_p || xmm_avx512_regnum_p || mxcsr_regnum_p;
4554
4555   ymm_regnum_p = i386_ymm_regnum_p (gdbarch, regnum);
4556   ymm_avx512_regnum_p = i386_ymm_avx512_regnum_p (gdbarch, regnum);
4557   zmm_regnum_p = i386_zmm_regnum_p (gdbarch, regnum);
4558
4559   avx512_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4560               == X86_XSTATE_AVX_AVX512_MASK);
4561   avx_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4562            == X86_XSTATE_AVX_MASK) && !avx512_p;
4563   sse_p = ((tdep->xcr0 & X86_XSTATE_AVX_AVX512_MASK)
4564            == X86_XSTATE_SSE_MASK) && !avx512_p && ! avx_p;
4565
4566   if (group == vector_reggroup)
4567     return (mmx_regnum_p
4568             || (zmm_regnum_p && avx512_p)
4569             || ((ymm_regnum_p || ymm_avx512_regnum_p) && avx_p)
4570             || ((xmm_regnum_p || xmm_avx512_regnum_p) && sse_p)
4571             || mxcsr_regnum_p);
4572
4573   fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
4574                  || i386_fpc_regnum_p (gdbarch, regnum));
4575   if (group == float_reggroup)
4576     return fp_regnum_p;
4577
4578   /* For "info reg all", don't include upper YMM registers nor XMM
4579      registers when AVX is supported.  */
4580   ymmh_regnum_p = i386_ymmh_regnum_p (gdbarch, regnum);
4581   ymmh_avx512_regnum_p = i386_ymmh_avx512_regnum_p (gdbarch, regnum);
4582   zmmh_regnum_p = i386_zmmh_regnum_p (gdbarch, regnum);
4583   if (group == all_reggroup
4584       && (((xmm_regnum_p || xmm_avx512_regnum_p) && !sse_p)
4585           || ((ymm_regnum_p || ymm_avx512_regnum_p) && !avx_p)
4586           || ymmh_regnum_p
4587           || ymmh_avx512_regnum_p
4588           || zmmh_regnum_p))
4589     return 0;
4590
4591   bnd_regnum_p = i386_bnd_regnum_p (gdbarch, regnum);
4592   if (group == all_reggroup
4593       && ((bnd_regnum_p && (tdep->xcr0 & X86_XSTATE_MPX_MASK))))
4594     return bnd_regnum_p;
4595
4596   bndr_regnum_p = i386_bndr_regnum_p (gdbarch, regnum);
4597   if (group == all_reggroup
4598       && ((bndr_regnum_p && (tdep->xcr0 & X86_XSTATE_MPX_MASK))))
4599     return 0;
4600
4601   mpx_ctrl_regnum_p = i386_mpx_ctrl_regnum_p (gdbarch, regnum);
4602   if (group == all_reggroup
4603       && ((mpx_ctrl_regnum_p && (tdep->xcr0 & X86_XSTATE_MPX_MASK))))
4604     return mpx_ctrl_regnum_p;
4605
4606   if (group == general_reggroup)
4607     return (!fp_regnum_p
4608             && !mmx_regnum_p
4609             && !mxcsr_regnum_p
4610             && !xmm_regnum_p
4611             && !xmm_avx512_regnum_p
4612             && !ymm_regnum_p
4613             && !ymmh_regnum_p
4614             && !ymm_avx512_regnum_p
4615             && !ymmh_avx512_regnum_p
4616             && !bndr_regnum_p
4617             && !bnd_regnum_p
4618             && !mpx_ctrl_regnum_p
4619             && !zmm_regnum_p
4620             && !zmmh_regnum_p);
4621
4622   return default_register_reggroup_p (gdbarch, regnum, group);
4623 }
4624 \f
4625
4626 /* Get the ARGIth function argument for the current function.  */
4627
4628 static CORE_ADDR
4629 i386_fetch_pointer_argument (struct frame_info *frame, int argi, 
4630                              struct type *type)
4631 {
4632   struct gdbarch *gdbarch = get_frame_arch (frame);
4633   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4634   CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
4635   return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4, byte_order);
4636 }
4637
4638 #define PREFIX_REPZ     0x01
4639 #define PREFIX_REPNZ    0x02
4640 #define PREFIX_LOCK     0x04
4641 #define PREFIX_DATA     0x08
4642 #define PREFIX_ADDR     0x10
4643
4644 /* operand size */
4645 enum
4646 {
4647   OT_BYTE = 0,
4648   OT_WORD,
4649   OT_LONG,
4650   OT_QUAD,
4651   OT_DQUAD,
4652 };
4653
4654 /* i386 arith/logic operations */
4655 enum
4656 {
4657   OP_ADDL,
4658   OP_ORL,
4659   OP_ADCL,
4660   OP_SBBL,
4661   OP_ANDL,
4662   OP_SUBL,
4663   OP_XORL,
4664   OP_CMPL,
4665 };
4666
4667 struct i386_record_s
4668 {
4669   struct gdbarch *gdbarch;
4670   struct regcache *regcache;
4671   CORE_ADDR orig_addr;
4672   CORE_ADDR addr;
4673   int aflag;
4674   int dflag;
4675   int override;
4676   uint8_t modrm;
4677   uint8_t mod, reg, rm;
4678   int ot;
4679   uint8_t rex_x;
4680   uint8_t rex_b;
4681   int rip_offset;
4682   int popl_esp_hack;
4683   const int *regmap;
4684 };
4685
4686 /* Parse the "modrm" part of the memory address irp->addr points at.
4687    Returns -1 if something goes wrong, 0 otherwise.  */
4688
4689 static int
4690 i386_record_modrm (struct i386_record_s *irp)
4691 {
4692   struct gdbarch *gdbarch = irp->gdbarch;
4693
4694   if (record_read_memory (gdbarch, irp->addr, &irp->modrm, 1))
4695     return -1;
4696
4697   irp->addr++;
4698   irp->mod = (irp->modrm >> 6) & 3;
4699   irp->reg = (irp->modrm >> 3) & 7;
4700   irp->rm = irp->modrm & 7;
4701
4702   return 0;
4703 }
4704
4705 /* Extract the memory address that the current instruction writes to,
4706    and return it in *ADDR.  Return -1 if something goes wrong.  */
4707
4708 static int
4709 i386_record_lea_modrm_addr (struct i386_record_s *irp, uint64_t *addr)
4710 {
4711   struct gdbarch *gdbarch = irp->gdbarch;
4712   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4713   gdb_byte buf[4];
4714   ULONGEST offset64;
4715
4716   *addr = 0;
4717   if (irp->aflag || irp->regmap[X86_RECORD_R8_REGNUM])
4718     {
4719       /* 32/64 bits */
4720       int havesib = 0;
4721       uint8_t scale = 0;
4722       uint8_t byte;
4723       uint8_t index = 0;
4724       uint8_t base = irp->rm;
4725
4726       if (base == 4)
4727         {
4728           havesib = 1;
4729           if (record_read_memory (gdbarch, irp->addr, &byte, 1))
4730             return -1;
4731           irp->addr++;
4732           scale = (byte >> 6) & 3;
4733           index = ((byte >> 3) & 7) | irp->rex_x;
4734           base = (byte & 7);
4735         }
4736       base |= irp->rex_b;
4737
4738       switch (irp->mod)
4739         {
4740         case 0:
4741           if ((base & 7) == 5)
4742             {
4743               base = 0xff;
4744               if (record_read_memory (gdbarch, irp->addr, buf, 4))
4745                 return -1;
4746               irp->addr += 4;
4747               *addr = extract_signed_integer (buf, 4, byte_order);
4748               if (irp->regmap[X86_RECORD_R8_REGNUM] && !havesib)
4749                 *addr += irp->addr + irp->rip_offset;
4750             }
4751           break;
4752         case 1:
4753           if (record_read_memory (gdbarch, irp->addr, buf, 1))
4754             return -1;
4755           irp->addr++;
4756           *addr = (int8_t) buf[0];
4757           break;
4758         case 2:
4759           if (record_read_memory (gdbarch, irp->addr, buf, 4))
4760             return -1;
4761           *addr = extract_signed_integer (buf, 4, byte_order);
4762           irp->addr += 4;
4763           break;
4764         }
4765
4766       offset64 = 0;
4767       if (base != 0xff)
4768         {
4769           if (base == 4 && irp->popl_esp_hack)
4770             *addr += irp->popl_esp_hack;
4771           regcache_raw_read_unsigned (irp->regcache, irp->regmap[base],
4772                                       &offset64);
4773         }
4774       if (irp->aflag == 2)
4775         {
4776           *addr += offset64;
4777         }
4778       else
4779         *addr = (uint32_t) (offset64 + *addr);
4780
4781       if (havesib && (index != 4 || scale != 0))
4782         {
4783           regcache_raw_read_unsigned (irp->regcache, irp->regmap[index],
4784                                       &offset64);
4785           if (irp->aflag == 2)
4786             *addr += offset64 << scale;
4787           else
4788             *addr = (uint32_t) (*addr + (offset64 << scale));
4789         }
4790
4791       if (!irp->aflag)
4792         {
4793           /* Since we are in 64-bit mode with ADDR32 prefix, zero-extend
4794              address from 32-bit to 64-bit.  */
4795             *addr = (uint32_t) *addr;
4796         }
4797     }
4798   else
4799     {
4800       /* 16 bits */
4801       switch (irp->mod)
4802         {
4803         case 0:
4804           if (irp->rm == 6)
4805             {
4806               if (record_read_memory (gdbarch, irp->addr, buf, 2))
4807                 return -1;
4808               irp->addr += 2;
4809               *addr = extract_signed_integer (buf, 2, byte_order);
4810               irp->rm = 0;
4811               goto no_rm;
4812             }
4813           break;
4814         case 1:
4815           if (record_read_memory (gdbarch, irp->addr, buf, 1))
4816             return -1;
4817           irp->addr++;
4818           *addr = (int8_t) buf[0];
4819           break;
4820         case 2:
4821           if (record_read_memory (gdbarch, irp->addr, buf, 2))
4822             return -1;
4823           irp->addr += 2;
4824           *addr = extract_signed_integer (buf, 2, byte_order);
4825           break;
4826         }
4827
4828       switch (irp->rm)
4829         {
4830         case 0:
4831           regcache_raw_read_unsigned (irp->regcache,
4832                                       irp->regmap[X86_RECORD_REBX_REGNUM],
4833                                       &offset64);
4834           *addr = (uint32_t) (*addr + offset64);
4835           regcache_raw_read_unsigned (irp->regcache,
4836                                       irp->regmap[X86_RECORD_RESI_REGNUM],
4837                                       &offset64);
4838           *addr = (uint32_t) (*addr + offset64);
4839           break;
4840         case 1:
4841           regcache_raw_read_unsigned (irp->regcache,
4842                                       irp->regmap[X86_RECORD_REBX_REGNUM],
4843                                       &offset64);
4844           *addr = (uint32_t) (*addr + offset64);
4845           regcache_raw_read_unsigned (irp->regcache,
4846                                       irp->regmap[X86_RECORD_REDI_REGNUM],
4847                                       &offset64);
4848           *addr = (uint32_t) (*addr + offset64);
4849           break;
4850         case 2:
4851           regcache_raw_read_unsigned (irp->regcache,
4852                                       irp->regmap[X86_RECORD_REBP_REGNUM],
4853                                       &offset64);
4854           *addr = (uint32_t) (*addr + offset64);
4855           regcache_raw_read_unsigned (irp->regcache,
4856                                       irp->regmap[X86_RECORD_RESI_REGNUM],
4857                                       &offset64);
4858           *addr = (uint32_t) (*addr + offset64);
4859           break;
4860         case 3:
4861           regcache_raw_read_unsigned (irp->regcache,
4862                                       irp->regmap[X86_RECORD_REBP_REGNUM],
4863                                       &offset64);
4864           *addr = (uint32_t) (*addr + offset64);
4865           regcache_raw_read_unsigned (irp->regcache,
4866                                       irp->regmap[X86_RECORD_REDI_REGNUM],
4867                                       &offset64);
4868           *addr = (uint32_t) (*addr + offset64);
4869           break;
4870         case 4:
4871           regcache_raw_read_unsigned (irp->regcache,
4872                                       irp->regmap[X86_RECORD_RESI_REGNUM],
4873                                       &offset64);
4874           *addr = (uint32_t) (*addr + offset64);
4875           break;
4876         case 5:
4877           regcache_raw_read_unsigned (irp->regcache,
4878                                       irp->regmap[X86_RECORD_REDI_REGNUM],
4879                                       &offset64);
4880           *addr = (uint32_t) (*addr + offset64);
4881           break;
4882         case 6:
4883           regcache_raw_read_unsigned (irp->regcache,
4884                                       irp->regmap[X86_RECORD_REBP_REGNUM],
4885                                       &offset64);
4886           *addr = (uint32_t) (*addr + offset64);
4887           break;
4888         case 7:
4889           regcache_raw_read_unsigned (irp->regcache,
4890                                       irp->regmap[X86_RECORD_REBX_REGNUM],
4891                                       &offset64);
4892           *addr = (uint32_t) (*addr + offset64);
4893           break;
4894         }
4895       *addr &= 0xffff;
4896     }
4897
4898  no_rm:
4899   return 0;
4900 }
4901
4902 /* Record the address and contents of the memory that will be changed
4903    by the current instruction.  Return -1 if something goes wrong, 0
4904    otherwise.  */
4905
4906 static int
4907 i386_record_lea_modrm (struct i386_record_s *irp)
4908 {
4909   struct gdbarch *gdbarch = irp->gdbarch;
4910   uint64_t addr;
4911
4912   if (irp->override >= 0)
4913     {
4914       if (record_full_memory_query)
4915         {
4916           if (yquery (_("\
4917 Process record ignores the memory change of instruction at address %s\n\
4918 because it can't get the value of the segment register.\n\
4919 Do you want to stop the program?"),
4920                       paddress (gdbarch, irp->orig_addr)))
4921             return -1;
4922         }
4923
4924       return 0;
4925     }
4926
4927   if (i386_record_lea_modrm_addr (irp, &addr))
4928     return -1;
4929
4930   if (record_full_arch_list_add_mem (addr, 1 << irp->ot))
4931     return -1;
4932
4933   return 0;
4934 }
4935
4936 /* Record the effects of a push operation.  Return -1 if something
4937    goes wrong, 0 otherwise.  */
4938
4939 static int
4940 i386_record_push (struct i386_record_s *irp, int size)
4941 {
4942   ULONGEST addr;
4943
4944   if (record_full_arch_list_add_reg (irp->regcache,
4945                                      irp->regmap[X86_RECORD_RESP_REGNUM]))
4946     return -1;
4947   regcache_raw_read_unsigned (irp->regcache,
4948                               irp->regmap[X86_RECORD_RESP_REGNUM],
4949                               &addr);
4950   if (record_full_arch_list_add_mem ((CORE_ADDR) addr - size, size))
4951     return -1;
4952
4953   return 0;
4954 }
4955
4956
4957 /* Defines contents to record.  */
4958 #define I386_SAVE_FPU_REGS              0xfffd
4959 #define I386_SAVE_FPU_ENV               0xfffe
4960 #define I386_SAVE_FPU_ENV_REG_STACK     0xffff
4961
4962 /* Record the values of the floating point registers which will be
4963    changed by the current instruction.  Returns -1 if something is
4964    wrong, 0 otherwise.  */
4965
4966 static int i386_record_floats (struct gdbarch *gdbarch,
4967                                struct i386_record_s *ir,
4968                                uint32_t iregnum)
4969 {
4970   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4971   int i;
4972
4973   /* Oza: Because of floating point insn push/pop of fpu stack is going to
4974      happen.  Currently we store st0-st7 registers, but we need not store all
4975      registers all the time, in future we use ftag register and record only
4976      those who are not marked as an empty.  */
4977
4978   if (I386_SAVE_FPU_REGS == iregnum)
4979     {
4980       for (i = I387_ST0_REGNUM (tdep); i <= I387_ST0_REGNUM (tdep) + 7; i++)
4981         {
4982           if (record_full_arch_list_add_reg (ir->regcache, i))
4983             return -1;
4984         }
4985     }
4986   else if (I386_SAVE_FPU_ENV == iregnum)
4987     {
4988       for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
4989               {
4990               if (record_full_arch_list_add_reg (ir->regcache, i))
4991                 return -1;
4992               }
4993     }
4994   else if (I386_SAVE_FPU_ENV_REG_STACK == iregnum)
4995     {
4996       for (i = I387_ST0_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
4997       {
4998         if (record_full_arch_list_add_reg (ir->regcache, i))
4999           return -1;
5000       }
5001     }
5002   else if ((iregnum >= I387_ST0_REGNUM (tdep)) &&
5003            (iregnum <= I387_FOP_REGNUM (tdep)))
5004     {
5005       if (record_full_arch_list_add_reg (ir->regcache,iregnum))
5006         return -1;
5007     }
5008   else
5009     {
5010       /* Parameter error.  */
5011       return -1;
5012     }
5013   if(I386_SAVE_FPU_ENV != iregnum)
5014     {
5015     for (i = I387_FCTRL_REGNUM (tdep); i <= I387_FOP_REGNUM (tdep); i++)
5016       {
5017       if (record_full_arch_list_add_reg (ir->regcache, i))
5018         return -1;
5019       }
5020     }
5021   return 0;
5022 }
5023
5024 /* Parse the current instruction, and record the values of the
5025    registers and memory that will be changed by the current
5026    instruction.  Returns -1 if something goes wrong, 0 otherwise.  */
5027
5028 #define I386_RECORD_FULL_ARCH_LIST_ADD_REG(regnum) \
5029     record_full_arch_list_add_reg (ir.regcache, ir.regmap[(regnum)])
5030
5031 int
5032 i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
5033                      CORE_ADDR input_addr)
5034 {
5035   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5036   int prefixes = 0;
5037   int regnum = 0;
5038   uint32_t opcode;
5039   uint8_t opcode8;
5040   ULONGEST addr;
5041   gdb_byte buf[MAX_REGISTER_SIZE];
5042   struct i386_record_s ir;
5043   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5044   uint8_t rex_w = -1;
5045   uint8_t rex_r = 0;
5046
5047   memset (&ir, 0, sizeof (struct i386_record_s));
5048   ir.regcache = regcache;
5049   ir.addr = input_addr;
5050   ir.orig_addr = input_addr;
5051   ir.aflag = 1;
5052   ir.dflag = 1;
5053   ir.override = -1;
5054   ir.popl_esp_hack = 0;
5055   ir.regmap = tdep->record_regmap;
5056   ir.gdbarch = gdbarch;
5057
5058   if (record_debug > 1)
5059     fprintf_unfiltered (gdb_stdlog, "Process record: i386_process_record "
5060                                     "addr = %s\n",
5061                         paddress (gdbarch, ir.addr));
5062
5063   /* prefixes */
5064   while (1)
5065     {
5066       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5067         return -1;
5068       ir.addr++;
5069       switch (opcode8)  /* Instruction prefixes */
5070         {
5071         case REPE_PREFIX_OPCODE:
5072           prefixes |= PREFIX_REPZ;
5073           break;
5074         case REPNE_PREFIX_OPCODE:
5075           prefixes |= PREFIX_REPNZ;
5076           break;
5077         case LOCK_PREFIX_OPCODE:
5078           prefixes |= PREFIX_LOCK;
5079           break;
5080         case CS_PREFIX_OPCODE:
5081           ir.override = X86_RECORD_CS_REGNUM;
5082           break;
5083         case SS_PREFIX_OPCODE:
5084           ir.override = X86_RECORD_SS_REGNUM;
5085           break;
5086         case DS_PREFIX_OPCODE:
5087           ir.override = X86_RECORD_DS_REGNUM;
5088           break;
5089         case ES_PREFIX_OPCODE:
5090           ir.override = X86_RECORD_ES_REGNUM;
5091           break;
5092         case FS_PREFIX_OPCODE:
5093           ir.override = X86_RECORD_FS_REGNUM;
5094           break;
5095         case GS_PREFIX_OPCODE:
5096           ir.override = X86_RECORD_GS_REGNUM;
5097           break;
5098         case DATA_PREFIX_OPCODE:
5099           prefixes |= PREFIX_DATA;
5100           break;
5101         case ADDR_PREFIX_OPCODE:
5102           prefixes |= PREFIX_ADDR;
5103           break;
5104         case 0x40:      /* i386 inc %eax */
5105         case 0x41:      /* i386 inc %ecx */
5106         case 0x42:      /* i386 inc %edx */
5107         case 0x43:      /* i386 inc %ebx */
5108         case 0x44:      /* i386 inc %esp */
5109         case 0x45:      /* i386 inc %ebp */
5110         case 0x46:      /* i386 inc %esi */
5111         case 0x47:      /* i386 inc %edi */
5112         case 0x48:      /* i386 dec %eax */
5113         case 0x49:      /* i386 dec %ecx */
5114         case 0x4a:      /* i386 dec %edx */
5115         case 0x4b:      /* i386 dec %ebx */
5116         case 0x4c:      /* i386 dec %esp */
5117         case 0x4d:      /* i386 dec %ebp */
5118         case 0x4e:      /* i386 dec %esi */
5119         case 0x4f:      /* i386 dec %edi */
5120           if (ir.regmap[X86_RECORD_R8_REGNUM])  /* 64 bit target */
5121             {
5122                /* REX */
5123                rex_w = (opcode8 >> 3) & 1;
5124                rex_r = (opcode8 & 0x4) << 1;
5125                ir.rex_x = (opcode8 & 0x2) << 2;
5126                ir.rex_b = (opcode8 & 0x1) << 3;
5127             }
5128           else                                  /* 32 bit target */
5129             goto out_prefixes;
5130           break;
5131         default:
5132           goto out_prefixes;
5133           break;
5134         }
5135     }
5136  out_prefixes:
5137   if (ir.regmap[X86_RECORD_R8_REGNUM] && rex_w == 1)
5138     {
5139       ir.dflag = 2;
5140     }
5141   else
5142     {
5143       if (prefixes & PREFIX_DATA)
5144         ir.dflag ^= 1;
5145     }
5146   if (prefixes & PREFIX_ADDR)
5147     ir.aflag ^= 1;
5148   else if (ir.regmap[X86_RECORD_R8_REGNUM])
5149     ir.aflag = 2;
5150
5151   /* Now check op code.  */
5152   opcode = (uint32_t) opcode8;
5153  reswitch:
5154   switch (opcode)
5155     {
5156     case 0x0f:
5157       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5158         return -1;
5159       ir.addr++;
5160       opcode = (uint32_t) opcode8 | 0x0f00;
5161       goto reswitch;
5162       break;
5163
5164     case 0x00:    /* arith & logic */
5165     case 0x01:
5166     case 0x02:
5167     case 0x03:
5168     case 0x04:
5169     case 0x05:
5170     case 0x08:
5171     case 0x09:
5172     case 0x0a:
5173     case 0x0b:
5174     case 0x0c:
5175     case 0x0d:
5176     case 0x10:
5177     case 0x11:
5178     case 0x12:
5179     case 0x13:
5180     case 0x14:
5181     case 0x15:
5182     case 0x18:
5183     case 0x19:
5184     case 0x1a:
5185     case 0x1b:
5186     case 0x1c:
5187     case 0x1d:
5188     case 0x20:
5189     case 0x21:
5190     case 0x22:
5191     case 0x23:
5192     case 0x24:
5193     case 0x25:
5194     case 0x28:
5195     case 0x29:
5196     case 0x2a:
5197     case 0x2b:
5198     case 0x2c:
5199     case 0x2d:
5200     case 0x30:
5201     case 0x31:
5202     case 0x32:
5203     case 0x33:
5204     case 0x34:
5205     case 0x35:
5206     case 0x38:
5207     case 0x39:
5208     case 0x3a:
5209     case 0x3b:
5210     case 0x3c:
5211     case 0x3d:
5212       if (((opcode >> 3) & 7) != OP_CMPL)
5213         {
5214           if ((opcode & 1) == 0)
5215             ir.ot = OT_BYTE;
5216           else
5217             ir.ot = ir.dflag + OT_WORD;
5218
5219           switch ((opcode >> 1) & 3)
5220             {
5221             case 0:    /* OP Ev, Gv */
5222               if (i386_record_modrm (&ir))
5223                 return -1;
5224               if (ir.mod != 3)
5225                 {
5226                   if (i386_record_lea_modrm (&ir))
5227                     return -1;
5228                 }
5229               else
5230                 {
5231                   ir.rm |= ir.rex_b;
5232                   if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5233                     ir.rm &= 0x3;
5234                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5235                 }
5236               break;
5237             case 1:    /* OP Gv, Ev */
5238               if (i386_record_modrm (&ir))
5239                 return -1;
5240               ir.reg |= rex_r;
5241               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5242                 ir.reg &= 0x3;
5243               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5244               break;
5245             case 2:    /* OP A, Iv */
5246               I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5247               break;
5248             }
5249         }
5250       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5251       break;
5252
5253     case 0x80:    /* GRP1 */
5254     case 0x81:
5255     case 0x82:
5256     case 0x83:
5257       if (i386_record_modrm (&ir))
5258         return -1;
5259
5260       if (ir.reg != OP_CMPL)
5261         {
5262           if ((opcode & 1) == 0)
5263             ir.ot = OT_BYTE;
5264           else
5265             ir.ot = ir.dflag + OT_WORD;
5266
5267           if (ir.mod != 3)
5268             {
5269               if (opcode == 0x83)
5270                 ir.rip_offset = 1;
5271               else
5272                 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5273               if (i386_record_lea_modrm (&ir))
5274                 return -1;
5275             }
5276           else
5277             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5278         }
5279       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5280       break;
5281
5282     case 0x40:      /* inc */
5283     case 0x41:
5284     case 0x42:
5285     case 0x43:
5286     case 0x44:
5287     case 0x45:
5288     case 0x46:
5289     case 0x47:
5290
5291     case 0x48:      /* dec */
5292     case 0x49:
5293     case 0x4a:
5294     case 0x4b:
5295     case 0x4c:
5296     case 0x4d:
5297     case 0x4e:
5298     case 0x4f:
5299
5300       I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 7);
5301       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5302       break;
5303
5304     case 0xf6:    /* GRP3 */
5305     case 0xf7:
5306       if ((opcode & 1) == 0)
5307         ir.ot = OT_BYTE;
5308       else
5309         ir.ot = ir.dflag + OT_WORD;
5310       if (i386_record_modrm (&ir))
5311         return -1;
5312
5313       if (ir.mod != 3 && ir.reg == 0)
5314         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5315
5316       switch (ir.reg)
5317         {
5318         case 0:    /* test */
5319           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5320           break;
5321         case 2:    /* not */
5322         case 3:    /* neg */
5323           if (ir.mod != 3)
5324             {
5325               if (i386_record_lea_modrm (&ir))
5326                 return -1;
5327             }
5328           else
5329             {
5330               ir.rm |= ir.rex_b;
5331               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5332                 ir.rm &= 0x3;
5333               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5334             }
5335           if (ir.reg == 3)  /* neg */
5336             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5337           break;
5338         case 4:    /* mul  */
5339         case 5:    /* imul */
5340         case 6:    /* div  */
5341         case 7:    /* idiv */
5342           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5343           if (ir.ot != OT_BYTE)
5344             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5345           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5346           break;
5347         default:
5348           ir.addr -= 2;
5349           opcode = opcode << 8 | ir.modrm;
5350           goto no_support;
5351           break;
5352         }
5353       break;
5354
5355     case 0xfe:    /* GRP4 */
5356     case 0xff:    /* GRP5 */
5357       if (i386_record_modrm (&ir))
5358         return -1;
5359       if (ir.reg >= 2 && opcode == 0xfe)
5360         {
5361           ir.addr -= 2;
5362           opcode = opcode << 8 | ir.modrm;
5363           goto no_support;
5364         }
5365       switch (ir.reg)
5366         {
5367         case 0:    /* inc */
5368         case 1:    /* dec */
5369           if ((opcode & 1) == 0)
5370             ir.ot = OT_BYTE;
5371           else
5372             ir.ot = ir.dflag + OT_WORD;
5373           if (ir.mod != 3)
5374             {
5375               if (i386_record_lea_modrm (&ir))
5376                 return -1;
5377             }
5378           else
5379             {
5380               ir.rm |= ir.rex_b;
5381               if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5382                 ir.rm &= 0x3;
5383               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5384             }
5385           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5386           break;
5387         case 2:    /* call */
5388           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5389             ir.dflag = 2;
5390           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5391             return -1;
5392           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5393           break;
5394         case 3:    /* lcall */
5395           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
5396           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5397             return -1;
5398           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5399           break;
5400         case 4:    /* jmp  */
5401         case 5:    /* ljmp */
5402           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5403           break;
5404         case 6:    /* push */
5405           if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5406             ir.dflag = 2;
5407           if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5408             return -1;
5409           break;
5410         default:
5411           ir.addr -= 2;
5412           opcode = opcode << 8 | ir.modrm;
5413           goto no_support;
5414           break;
5415         }
5416       break;
5417
5418     case 0x84:    /* test */
5419     case 0x85:
5420     case 0xa8:
5421     case 0xa9:
5422       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5423       break;
5424
5425     case 0x98:    /* CWDE/CBW */
5426       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5427       break;
5428
5429     case 0x99:    /* CDQ/CWD */
5430       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5431       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5432       break;
5433
5434     case 0x0faf:  /* imul */
5435     case 0x69:
5436     case 0x6b:
5437       ir.ot = ir.dflag + OT_WORD;
5438       if (i386_record_modrm (&ir))
5439         return -1;
5440       if (opcode == 0x69)
5441         ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5442       else if (opcode == 0x6b)
5443         ir.rip_offset = 1;
5444       ir.reg |= rex_r;
5445       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5446         ir.reg &= 0x3;
5447       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5448       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5449       break;
5450
5451     case 0x0fc0:  /* xadd */
5452     case 0x0fc1:
5453       if ((opcode & 1) == 0)
5454         ir.ot = OT_BYTE;
5455       else
5456         ir.ot = ir.dflag + OT_WORD;
5457       if (i386_record_modrm (&ir))
5458         return -1;
5459       ir.reg |= rex_r;
5460       if (ir.mod == 3)
5461         {
5462           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5463             ir.reg &= 0x3;
5464           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5465           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5466             ir.rm &= 0x3;
5467           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5468         }
5469       else
5470         {
5471           if (i386_record_lea_modrm (&ir))
5472             return -1;
5473           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5474             ir.reg &= 0x3;
5475           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5476         }
5477       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5478       break;
5479
5480     case 0x0fb0:  /* cmpxchg */
5481     case 0x0fb1:
5482       if ((opcode & 1) == 0)
5483         ir.ot = OT_BYTE;
5484       else
5485         ir.ot = ir.dflag + OT_WORD;
5486       if (i386_record_modrm (&ir))
5487         return -1;
5488       if (ir.mod == 3)
5489         {
5490           ir.reg |= rex_r;
5491           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5492           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5493             ir.reg &= 0x3;
5494           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5495         }
5496       else
5497         {
5498           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5499           if (i386_record_lea_modrm (&ir))
5500             return -1;
5501         }
5502       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5503       break;
5504
5505     case 0x0fc7:    /* cmpxchg8b / rdrand / rdseed */
5506       if (i386_record_modrm (&ir))
5507         return -1;
5508       if (ir.mod == 3)
5509         {
5510           /* rdrand and rdseed use the 3 bits of the REG field of ModR/M as
5511              an extended opcode.  rdrand has bits 110 (/6) and rdseed
5512              has bits 111 (/7).  */
5513           if (ir.reg == 6 || ir.reg == 7)
5514             {
5515               /* The storage register is described by the 3 R/M bits, but the
5516                  REX.B prefix may be used to give access to registers
5517                  R8~R15.  In this case ir.rex_b + R/M will give us the register
5518                  in the range R8~R15.
5519
5520                  REX.W may also be used to access 64-bit registers, but we
5521                  already record entire registers and not just partial bits
5522                  of them.  */
5523               I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b + ir.rm);
5524               /* These instructions also set conditional bits.  */
5525               I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5526               break;
5527             }
5528           else
5529             {
5530               /* We don't handle this particular instruction yet.  */
5531               ir.addr -= 2;
5532               opcode = opcode << 8 | ir.modrm;
5533               goto no_support;
5534             }
5535         }
5536       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5537       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5538       if (i386_record_lea_modrm (&ir))
5539         return -1;
5540       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5541       break;
5542
5543     case 0x50:    /* push */
5544     case 0x51:
5545     case 0x52:
5546     case 0x53:
5547     case 0x54:
5548     case 0x55:
5549     case 0x56:
5550     case 0x57:
5551     case 0x68:
5552     case 0x6a:
5553       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5554         ir.dflag = 2;
5555       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5556         return -1;
5557       break;
5558
5559     case 0x06:    /* push es */
5560     case 0x0e:    /* push cs */
5561     case 0x16:    /* push ss */
5562     case 0x1e:    /* push ds */
5563       if (ir.regmap[X86_RECORD_R8_REGNUM])
5564         {
5565           ir.addr -= 1;
5566           goto no_support;
5567         }
5568       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5569         return -1;
5570       break;
5571
5572     case 0x0fa0:    /* push fs */
5573     case 0x0fa8:    /* push gs */
5574       if (ir.regmap[X86_RECORD_R8_REGNUM])
5575         {
5576           ir.addr -= 2;
5577           goto no_support;
5578         }
5579       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5580         return -1;
5581       break;
5582
5583     case 0x60:    /* pusha */
5584       if (ir.regmap[X86_RECORD_R8_REGNUM])
5585         {
5586           ir.addr -= 1;
5587           goto no_support;
5588         }
5589       if (i386_record_push (&ir, 1 << (ir.dflag + 4)))
5590         return -1;
5591       break;
5592
5593     case 0x58:    /* pop */
5594     case 0x59:
5595     case 0x5a:
5596     case 0x5b:
5597     case 0x5c:
5598     case 0x5d:
5599     case 0x5e:
5600     case 0x5f:
5601       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5602       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
5603       break;
5604
5605     case 0x61:    /* popa */
5606       if (ir.regmap[X86_RECORD_R8_REGNUM])
5607         {
5608           ir.addr -= 1;
5609           goto no_support;
5610         }
5611       for (regnum = X86_RECORD_REAX_REGNUM; 
5612            regnum <= X86_RECORD_REDI_REGNUM;
5613            regnum++)
5614         I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5615       break;
5616
5617     case 0x8f:    /* pop */
5618       if (ir.regmap[X86_RECORD_R8_REGNUM])
5619         ir.ot = ir.dflag ? OT_QUAD : OT_WORD;
5620       else
5621         ir.ot = ir.dflag + OT_WORD;
5622       if (i386_record_modrm (&ir))
5623         return -1;
5624       if (ir.mod == 3)
5625         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
5626       else
5627         {
5628           ir.popl_esp_hack = 1 << ir.ot;
5629           if (i386_record_lea_modrm (&ir))
5630             return -1;
5631         }
5632       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5633       break;
5634
5635     case 0xc8:    /* enter */
5636       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
5637       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5638         ir.dflag = 2;
5639       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
5640         return -1;
5641       break;
5642
5643     case 0xc9:    /* leave */
5644       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5645       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
5646       break;
5647
5648     case 0x07:    /* pop es */
5649       if (ir.regmap[X86_RECORD_R8_REGNUM])
5650         {
5651           ir.addr -= 1;
5652           goto no_support;
5653         }
5654       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5655       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_ES_REGNUM);
5656       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5657       break;
5658
5659     case 0x17:    /* pop ss */
5660       if (ir.regmap[X86_RECORD_R8_REGNUM])
5661         {
5662           ir.addr -= 1;
5663           goto no_support;
5664         }
5665       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5666       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_SS_REGNUM);
5667       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5668       break;
5669
5670     case 0x1f:    /* pop ds */
5671       if (ir.regmap[X86_RECORD_R8_REGNUM])
5672         {
5673           ir.addr -= 1;
5674           goto no_support;
5675         }
5676       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5677       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_DS_REGNUM);
5678       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5679       break;
5680
5681     case 0x0fa1:    /* pop fs */
5682       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5683       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_FS_REGNUM);
5684       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5685       break;
5686
5687     case 0x0fa9:    /* pop gs */
5688       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5689       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
5690       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5691       break;
5692
5693     case 0x88:    /* mov */
5694     case 0x89:
5695     case 0xc6:
5696     case 0xc7:
5697       if ((opcode & 1) == 0)
5698         ir.ot = OT_BYTE;
5699       else
5700         ir.ot = ir.dflag + OT_WORD;
5701
5702       if (i386_record_modrm (&ir))
5703         return -1;
5704
5705       if (ir.mod != 3)
5706         {
5707           if (opcode == 0xc6 || opcode == 0xc7)
5708             ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5709           if (i386_record_lea_modrm (&ir))
5710             return -1;
5711         }
5712       else
5713         {
5714           if (opcode == 0xc6 || opcode == 0xc7)
5715             ir.rm |= ir.rex_b;
5716           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5717             ir.rm &= 0x3;
5718           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5719         }
5720       break;
5721
5722     case 0x8a:    /* mov */
5723     case 0x8b:
5724       if ((opcode & 1) == 0)
5725         ir.ot = OT_BYTE;
5726       else
5727         ir.ot = ir.dflag + OT_WORD;
5728       if (i386_record_modrm (&ir))
5729         return -1;
5730       ir.reg |= rex_r;
5731       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5732         ir.reg &= 0x3;
5733       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5734       break;
5735
5736     case 0x8c:    /* mov seg */
5737       if (i386_record_modrm (&ir))
5738         return -1;
5739       if (ir.reg > 5)
5740         {
5741           ir.addr -= 2;
5742           opcode = opcode << 8 | ir.modrm;
5743           goto no_support;
5744         }
5745
5746       if (ir.mod == 3)
5747         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5748       else
5749         {
5750           ir.ot = OT_WORD;
5751           if (i386_record_lea_modrm (&ir))
5752             return -1;
5753         }
5754       break;
5755
5756     case 0x8e:    /* mov seg */
5757       if (i386_record_modrm (&ir))
5758         return -1;
5759       switch (ir.reg)
5760         {
5761         case 0:
5762           regnum = X86_RECORD_ES_REGNUM;
5763           break;
5764         case 2:
5765           regnum = X86_RECORD_SS_REGNUM;
5766           break;
5767         case 3:
5768           regnum = X86_RECORD_DS_REGNUM;
5769           break;
5770         case 4:
5771           regnum = X86_RECORD_FS_REGNUM;
5772           break;
5773         case 5:
5774           regnum = X86_RECORD_GS_REGNUM;
5775           break;
5776         default:
5777           ir.addr -= 2;
5778           opcode = opcode << 8 | ir.modrm;
5779           goto no_support;
5780           break;
5781         }
5782       I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5783       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5784       break;
5785
5786     case 0x0fb6:    /* movzbS */
5787     case 0x0fb7:    /* movzwS */
5788     case 0x0fbe:    /* movsbS */
5789     case 0x0fbf:    /* movswS */
5790       if (i386_record_modrm (&ir))
5791         return -1;
5792       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
5793       break;
5794
5795     case 0x8d:      /* lea */
5796       if (i386_record_modrm (&ir))
5797         return -1;
5798       if (ir.mod == 3)
5799         {
5800           ir.addr -= 2;
5801           opcode = opcode << 8 | ir.modrm;
5802           goto no_support;
5803         }
5804       ir.ot = ir.dflag;
5805       ir.reg |= rex_r;
5806       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5807         ir.reg &= 0x3;
5808       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5809       break;
5810
5811     case 0xa0:    /* mov EAX */
5812     case 0xa1:
5813
5814     case 0xd7:    /* xlat */
5815       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5816       break;
5817
5818     case 0xa2:    /* mov EAX */
5819     case 0xa3:
5820       if (ir.override >= 0)
5821         {
5822           if (record_full_memory_query)
5823             {
5824               if (yquery (_("\
5825 Process record ignores the memory change of instruction at address %s\n\
5826 because it can't get the value of the segment register.\n\
5827 Do you want to stop the program?"),
5828                           paddress (gdbarch, ir.orig_addr)))
5829                 return -1;
5830             }
5831         }
5832       else
5833         {
5834           if ((opcode & 1) == 0)
5835             ir.ot = OT_BYTE;
5836           else
5837             ir.ot = ir.dflag + OT_WORD;
5838           if (ir.aflag == 2)
5839             {
5840               if (record_read_memory (gdbarch, ir.addr, buf, 8))
5841                 return -1;
5842               ir.addr += 8;
5843               addr = extract_unsigned_integer (buf, 8, byte_order);
5844             }
5845           else if (ir.aflag)
5846             {
5847               if (record_read_memory (gdbarch, ir.addr, buf, 4))
5848                 return -1;
5849               ir.addr += 4;
5850               addr = extract_unsigned_integer (buf, 4, byte_order);
5851             }
5852           else
5853             {
5854               if (record_read_memory (gdbarch, ir.addr, buf, 2))
5855                 return -1;
5856               ir.addr += 2;
5857               addr = extract_unsigned_integer (buf, 2, byte_order);
5858             }
5859           if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
5860             return -1;
5861         }
5862       break;
5863
5864     case 0xb0:    /* mov R, Ib */
5865     case 0xb1:
5866     case 0xb2:
5867     case 0xb3:
5868     case 0xb4:
5869     case 0xb5:
5870     case 0xb6:
5871     case 0xb7:
5872       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((ir.regmap[X86_RECORD_R8_REGNUM])
5873                                           ? ((opcode & 0x7) | ir.rex_b)
5874                                           : ((opcode & 0x7) & 0x3));
5875       break;
5876
5877     case 0xb8:    /* mov R, Iv */
5878     case 0xb9:
5879     case 0xba:
5880     case 0xbb:
5881     case 0xbc:
5882     case 0xbd:
5883     case 0xbe:
5884     case 0xbf:
5885       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
5886       break;
5887
5888     case 0x91:    /* xchg R, EAX */
5889     case 0x92:
5890     case 0x93:
5891     case 0x94:
5892     case 0x95:
5893     case 0x96:
5894     case 0x97:
5895       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5896       I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 0x7);
5897       break;
5898
5899     case 0x86:    /* xchg Ev, Gv */
5900     case 0x87:
5901       if ((opcode & 1) == 0)
5902         ir.ot = OT_BYTE;
5903       else
5904         ir.ot = ir.dflag + OT_WORD;
5905       if (i386_record_modrm (&ir))
5906         return -1;
5907       if (ir.mod == 3)
5908         {
5909           ir.rm |= ir.rex_b;
5910           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5911             ir.rm &= 0x3;
5912           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5913         }
5914       else
5915         {
5916           if (i386_record_lea_modrm (&ir))
5917             return -1;
5918         }
5919       ir.reg |= rex_r;
5920       if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5921         ir.reg &= 0x3;
5922       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5923       break;
5924
5925     case 0xc4:    /* les Gv */
5926     case 0xc5:    /* lds Gv */
5927       if (ir.regmap[X86_RECORD_R8_REGNUM])
5928         {
5929           ir.addr -= 1;
5930           goto no_support;
5931         }
5932       /* FALLTHROUGH */
5933     case 0x0fb2:    /* lss Gv */
5934     case 0x0fb4:    /* lfs Gv */
5935     case 0x0fb5:    /* lgs Gv */
5936       if (i386_record_modrm (&ir))
5937         return -1;
5938       if (ir.mod == 3)
5939         {
5940           if (opcode > 0xff)
5941             ir.addr -= 3;
5942           else
5943             ir.addr -= 2;
5944           opcode = opcode << 8 | ir.modrm;
5945           goto no_support;
5946         }
5947       switch (opcode)
5948         {
5949         case 0xc4:    /* les Gv */
5950           regnum = X86_RECORD_ES_REGNUM;
5951           break;
5952         case 0xc5:    /* lds Gv */
5953           regnum = X86_RECORD_DS_REGNUM;
5954           break;
5955         case 0x0fb2:  /* lss Gv */
5956           regnum = X86_RECORD_SS_REGNUM;
5957           break;
5958         case 0x0fb4:  /* lfs Gv */
5959           regnum = X86_RECORD_FS_REGNUM;
5960           break;
5961         case 0x0fb5:  /* lgs Gv */
5962           regnum = X86_RECORD_GS_REGNUM;
5963           break;
5964         }
5965       I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5966       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
5967       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5968       break;
5969
5970     case 0xc0:    /* shifts */
5971     case 0xc1:
5972     case 0xd0:
5973     case 0xd1:
5974     case 0xd2:
5975     case 0xd3:
5976       if ((opcode & 1) == 0)
5977         ir.ot = OT_BYTE;
5978       else
5979         ir.ot = ir.dflag + OT_WORD;
5980       if (i386_record_modrm (&ir))
5981         return -1;
5982       if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
5983         {
5984           if (i386_record_lea_modrm (&ir))
5985             return -1;
5986         }
5987       else
5988         {
5989           ir.rm |= ir.rex_b;
5990           if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5991             ir.rm &= 0x3;
5992           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
5993         }
5994       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
5995       break;
5996
5997     case 0x0fa4:
5998     case 0x0fa5:
5999     case 0x0fac:
6000     case 0x0fad:
6001       if (i386_record_modrm (&ir))
6002         return -1;
6003       if (ir.mod == 3)
6004         {
6005           if (record_full_arch_list_add_reg (ir.regcache, ir.rm))
6006             return -1;
6007         }
6008       else
6009         {
6010           if (i386_record_lea_modrm (&ir))
6011             return -1;
6012         }
6013       break;
6014
6015     case 0xd8:    /* Floats.  */
6016     case 0xd9:
6017     case 0xda:
6018     case 0xdb:
6019     case 0xdc:
6020     case 0xdd:
6021     case 0xde:
6022     case 0xdf:
6023       if (i386_record_modrm (&ir))
6024         return -1;
6025       ir.reg |= ((opcode & 7) << 3);
6026       if (ir.mod != 3)
6027         {
6028           /* Memory.  */
6029           uint64_t addr64;
6030
6031           if (i386_record_lea_modrm_addr (&ir, &addr64))
6032             return -1;
6033           switch (ir.reg)
6034             {
6035             case 0x02:
6036             case 0x12:
6037             case 0x22:
6038             case 0x32:
6039               /* For fcom, ficom nothing to do.  */
6040               break;
6041             case 0x03:
6042             case 0x13:
6043             case 0x23:
6044             case 0x33:
6045               /* For fcomp, ficomp pop FPU stack, store all.  */
6046               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6047                 return -1;
6048               break;
6049             case 0x00:
6050             case 0x01:
6051             case 0x04:
6052             case 0x05:
6053             case 0x06:
6054             case 0x07:
6055             case 0x10:
6056             case 0x11:
6057             case 0x14:
6058             case 0x15:
6059             case 0x16:
6060             case 0x17:
6061             case 0x20:
6062             case 0x21:
6063             case 0x24:
6064             case 0x25:
6065             case 0x26:
6066             case 0x27:
6067             case 0x30:
6068             case 0x31:
6069             case 0x34:
6070             case 0x35:
6071             case 0x36:
6072             case 0x37:
6073               /* For fadd, fmul, fsub, fsubr, fdiv, fdivr, fiadd, fimul,
6074                  fisub, fisubr, fidiv, fidivr, modR/M.reg is an extension
6075                  of code,  always affects st(0) register.  */
6076               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
6077                 return -1;
6078               break;
6079             case 0x08:
6080             case 0x0a:
6081             case 0x0b:
6082             case 0x18:
6083             case 0x19:
6084             case 0x1a:
6085             case 0x1b:
6086             case 0x1d:
6087             case 0x28:
6088             case 0x29:
6089             case 0x2a:
6090             case 0x2b:
6091             case 0x38:
6092             case 0x39:
6093             case 0x3a:
6094             case 0x3b:
6095             case 0x3c:
6096             case 0x3d:
6097               switch (ir.reg & 7)
6098                 {
6099                 case 0:
6100                   /* Handling fld, fild.  */
6101                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6102                     return -1;
6103                   break;
6104                 case 1:
6105                   switch (ir.reg >> 4)
6106                     {
6107                     case 0:
6108                       if (record_full_arch_list_add_mem (addr64, 4))
6109                         return -1;
6110                       break;
6111                     case 2:
6112                       if (record_full_arch_list_add_mem (addr64, 8))
6113                         return -1;
6114                       break;
6115                     case 3:
6116                       break;
6117                     default:
6118                       if (record_full_arch_list_add_mem (addr64, 2))
6119                         return -1;
6120                       break;
6121                     }
6122                   break;
6123                 default:
6124                   switch (ir.reg >> 4)
6125                     {
6126                     case 0:
6127                       if (record_full_arch_list_add_mem (addr64, 4))
6128                         return -1;
6129                       if (3 == (ir.reg & 7))
6130                         {
6131                           /* For fstp m32fp.  */
6132                           if (i386_record_floats (gdbarch, &ir,
6133                                                   I386_SAVE_FPU_REGS))
6134                             return -1;
6135                         }
6136                       break;
6137                     case 1:
6138                       if (record_full_arch_list_add_mem (addr64, 4))
6139                         return -1;
6140                       if ((3 == (ir.reg & 7))
6141                           || (5 == (ir.reg & 7))
6142                           || (7 == (ir.reg & 7)))
6143                         {
6144                           /* For fstp insn.  */
6145                           if (i386_record_floats (gdbarch, &ir,
6146                                                   I386_SAVE_FPU_REGS))
6147                             return -1;
6148                         }
6149                       break;
6150                     case 2:
6151                       if (record_full_arch_list_add_mem (addr64, 8))
6152                         return -1;
6153                       if (3 == (ir.reg & 7))
6154                         {
6155                           /* For fstp m64fp.  */
6156                           if (i386_record_floats (gdbarch, &ir,
6157                                                   I386_SAVE_FPU_REGS))
6158                             return -1;
6159                         }
6160                       break;
6161                     case 3:
6162                       if ((3 <= (ir.reg & 7)) && (6 <= (ir.reg & 7)))
6163                         {
6164                           /* For fistp, fbld, fild, fbstp.  */
6165                           if (i386_record_floats (gdbarch, &ir,
6166                                                   I386_SAVE_FPU_REGS))
6167                             return -1;
6168                         }
6169                       /* Fall through */
6170                     default:
6171                       if (record_full_arch_list_add_mem (addr64, 2))
6172                         return -1;
6173                       break;
6174                     }
6175                   break;
6176                 }
6177               break;
6178             case 0x0c:
6179               /* Insn fldenv.  */
6180               if (i386_record_floats (gdbarch, &ir,
6181                                       I386_SAVE_FPU_ENV_REG_STACK))
6182                 return -1;
6183               break;
6184             case 0x0d:
6185               /* Insn fldcw.  */
6186               if (i386_record_floats (gdbarch, &ir, I387_FCTRL_REGNUM (tdep)))
6187                 return -1;
6188               break;
6189             case 0x2c:
6190               /* Insn frstor.  */
6191               if (i386_record_floats (gdbarch, &ir,
6192                                       I386_SAVE_FPU_ENV_REG_STACK))
6193                 return -1;
6194               break;
6195             case 0x0e:
6196               if (ir.dflag)
6197                 {
6198                   if (record_full_arch_list_add_mem (addr64, 28))
6199                     return -1;
6200                 }
6201               else
6202                 {
6203                   if (record_full_arch_list_add_mem (addr64, 14))
6204                     return -1;
6205                 }
6206               break;
6207             case 0x0f:
6208             case 0x2f:
6209               if (record_full_arch_list_add_mem (addr64, 2))
6210                 return -1;
6211               /* Insn fstp, fbstp.  */
6212               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6213                 return -1;
6214               break;
6215             case 0x1f:
6216             case 0x3e:
6217               if (record_full_arch_list_add_mem (addr64, 10))
6218                 return -1;
6219               break;
6220             case 0x2e:
6221               if (ir.dflag)
6222                 {
6223                   if (record_full_arch_list_add_mem (addr64, 28))
6224                     return -1;
6225                   addr64 += 28;
6226                 }
6227               else
6228                 {
6229                   if (record_full_arch_list_add_mem (addr64, 14))
6230                     return -1;
6231                   addr64 += 14;
6232                 }
6233               if (record_full_arch_list_add_mem (addr64, 80))
6234                 return -1;
6235               /* Insn fsave.  */
6236               if (i386_record_floats (gdbarch, &ir,
6237                                       I386_SAVE_FPU_ENV_REG_STACK))
6238                 return -1;
6239               break;
6240             case 0x3f:
6241               if (record_full_arch_list_add_mem (addr64, 8))
6242                 return -1;
6243               /* Insn fistp.  */
6244               if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6245                 return -1;
6246               break;
6247             default:
6248               ir.addr -= 2;
6249               opcode = opcode << 8 | ir.modrm;
6250               goto no_support;
6251               break;
6252             }
6253         }
6254       /* Opcode is an extension of modR/M byte.  */
6255       else
6256         {
6257           switch (opcode)
6258             {
6259             case 0xd8:
6260               if (i386_record_floats (gdbarch, &ir, I387_ST0_REGNUM (tdep)))
6261                 return -1;
6262               break;
6263             case 0xd9:
6264               if (0x0c == (ir.modrm >> 4))
6265                 {
6266                   if ((ir.modrm & 0x0f) <= 7)
6267                     {
6268                       if (i386_record_floats (gdbarch, &ir,
6269                                               I386_SAVE_FPU_REGS))
6270                         return -1;
6271                     }
6272                   else
6273                     {
6274                       if (i386_record_floats (gdbarch, &ir,
6275                                               I387_ST0_REGNUM (tdep)))
6276                         return -1;
6277                       /* If only st(0) is changing, then we have already
6278                          recorded.  */
6279                       if ((ir.modrm & 0x0f) - 0x08)
6280                         {
6281                           if (i386_record_floats (gdbarch, &ir,
6282                                                   I387_ST0_REGNUM (tdep) +
6283                                                   ((ir.modrm & 0x0f) - 0x08)))
6284                             return -1;
6285                         }
6286                     }
6287                 }
6288               else
6289                 {
6290                   switch (ir.modrm)
6291                     {
6292                     case 0xe0:
6293                     case 0xe1:
6294                     case 0xf0:
6295                     case 0xf5:
6296                     case 0xf8:
6297                     case 0xfa:
6298                     case 0xfc:
6299                     case 0xfe:
6300                     case 0xff:
6301                       if (i386_record_floats (gdbarch, &ir,
6302                                               I387_ST0_REGNUM (tdep)))
6303                         return -1;
6304                       break;
6305                     case 0xf1:
6306                     case 0xf2:
6307                     case 0xf3:
6308                     case 0xf4:
6309                     case 0xf6:
6310                     case 0xf7:
6311                     case 0xe8:
6312                     case 0xe9:
6313                     case 0xea:
6314                     case 0xeb:
6315                     case 0xec:
6316                     case 0xed:
6317                     case 0xee:
6318                     case 0xf9:
6319                     case 0xfb:
6320                       if (i386_record_floats (gdbarch, &ir,
6321                                               I386_SAVE_FPU_REGS))
6322                         return -1;
6323                       break;
6324                     case 0xfd:
6325                       if (i386_record_floats (gdbarch, &ir,
6326                                               I387_ST0_REGNUM (tdep)))
6327                         return -1;
6328                       if (i386_record_floats (gdbarch, &ir,
6329                                               I387_ST0_REGNUM (tdep) + 1))
6330                         return -1;
6331                       break;
6332                     }
6333                 }
6334               break;
6335             case 0xda:
6336               if (0xe9 == ir.modrm)
6337                 {
6338                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6339                     return -1;
6340                 }
6341               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
6342                 {
6343                   if (i386_record_floats (gdbarch, &ir,
6344                                           I387_ST0_REGNUM (tdep)))
6345                     return -1;
6346                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
6347                     {
6348                       if (i386_record_floats (gdbarch, &ir,
6349                                               I387_ST0_REGNUM (tdep) +
6350                                               (ir.modrm & 0x0f)))
6351                         return -1;
6352                     }
6353                   else if ((ir.modrm & 0x0f) - 0x08)
6354                     {
6355                       if (i386_record_floats (gdbarch, &ir,
6356                                               I387_ST0_REGNUM (tdep) +
6357                                               ((ir.modrm & 0x0f) - 0x08)))
6358                         return -1;
6359                     }
6360                 }
6361               break;
6362             case 0xdb:
6363               if (0xe3 == ir.modrm)
6364                 {
6365                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_ENV))
6366                     return -1;
6367                 }
6368               else if ((0x0c == ir.modrm >> 4) || (0x0d == ir.modrm >> 4))
6369                 {
6370                   if (i386_record_floats (gdbarch, &ir,
6371                                           I387_ST0_REGNUM (tdep)))
6372                     return -1;
6373                   if (((ir.modrm & 0x0f) > 0) && ((ir.modrm & 0x0f) <= 7))
6374                     {
6375                       if (i386_record_floats (gdbarch, &ir,
6376                                               I387_ST0_REGNUM (tdep) +
6377                                               (ir.modrm & 0x0f)))
6378                         return -1;
6379                     }
6380                   else if ((ir.modrm & 0x0f) - 0x08)
6381                     {
6382                       if (i386_record_floats (gdbarch, &ir,
6383                                               I387_ST0_REGNUM (tdep) +
6384                                               ((ir.modrm & 0x0f) - 0x08)))
6385                         return -1;
6386                     }
6387                 }
6388               break;
6389             case 0xdc:
6390               if ((0x0c == ir.modrm >> 4)
6391                   || (0x0d == ir.modrm >> 4)
6392                   || (0x0f == ir.modrm >> 4))
6393                 {
6394                   if ((ir.modrm & 0x0f) <= 7)
6395                     {
6396                       if (i386_record_floats (gdbarch, &ir,
6397                                               I387_ST0_REGNUM (tdep) +
6398                                               (ir.modrm & 0x0f)))
6399                         return -1;
6400                     }
6401                   else
6402                     {
6403                       if (i386_record_floats (gdbarch, &ir,
6404                                               I387_ST0_REGNUM (tdep) +
6405                                               ((ir.modrm & 0x0f) - 0x08)))
6406                         return -1;
6407                     }
6408                 }
6409               break;
6410             case 0xdd:
6411               if (0x0c == ir.modrm >> 4)
6412                 {
6413                   if (i386_record_floats (gdbarch, &ir,
6414                                           I387_FTAG_REGNUM (tdep)))
6415                     return -1;
6416                 }
6417               else if ((0x0d == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
6418                 {
6419                   if ((ir.modrm & 0x0f) <= 7)
6420                     {
6421                       if (i386_record_floats (gdbarch, &ir,
6422                                               I387_ST0_REGNUM (tdep) +
6423                                               (ir.modrm & 0x0f)))
6424                         return -1;
6425                     }
6426                   else
6427                     {
6428                       if (i386_record_floats (gdbarch, &ir,
6429                                               I386_SAVE_FPU_REGS))
6430                         return -1;
6431                     }
6432                 }
6433               break;
6434             case 0xde:
6435               if ((0x0c == ir.modrm >> 4)
6436                   || (0x0e == ir.modrm >> 4)
6437                   || (0x0f == ir.modrm >> 4)
6438                   || (0xd9 == ir.modrm))
6439                 {
6440                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6441                     return -1;
6442                 }
6443               break;
6444             case 0xdf:
6445               if (0xe0 == ir.modrm)
6446                 {
6447                   if (record_full_arch_list_add_reg (ir.regcache,
6448                                                      I386_EAX_REGNUM))
6449                     return -1;
6450                 }
6451               else if ((0x0f == ir.modrm >> 4) || (0x0e == ir.modrm >> 4))
6452                 {
6453                   if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6454                     return -1;
6455                 }
6456               break;
6457             }
6458         }
6459       break;
6460       /* string ops */
6461     case 0xa4:    /* movsS */
6462     case 0xa5:
6463     case 0xaa:    /* stosS */
6464     case 0xab:
6465     case 0x6c:    /* insS */
6466     case 0x6d:
6467       regcache_raw_read_unsigned (ir.regcache,
6468                                   ir.regmap[X86_RECORD_RECX_REGNUM],
6469                                   &addr);
6470       if (addr)
6471         {
6472           ULONGEST es, ds;
6473
6474           if ((opcode & 1) == 0)
6475             ir.ot = OT_BYTE;
6476           else
6477             ir.ot = ir.dflag + OT_WORD;
6478           regcache_raw_read_unsigned (ir.regcache,
6479                                       ir.regmap[X86_RECORD_REDI_REGNUM],
6480                                       &addr);
6481
6482           regcache_raw_read_unsigned (ir.regcache,
6483                                       ir.regmap[X86_RECORD_ES_REGNUM],
6484                                       &es);
6485           regcache_raw_read_unsigned (ir.regcache,
6486                                       ir.regmap[X86_RECORD_DS_REGNUM],
6487                                       &ds);
6488           if (ir.aflag && (es != ds))
6489             {
6490               /* addr += ((uint32_t) read_register (I386_ES_REGNUM)) << 4; */
6491               if (record_full_memory_query)
6492                 {
6493                   if (yquery (_("\
6494 Process record ignores the memory change of instruction at address %s\n\
6495 because it can't get the value of the segment register.\n\
6496 Do you want to stop the program?"),
6497                               paddress (gdbarch, ir.orig_addr)))
6498                     return -1;
6499                 }
6500             }
6501           else
6502             {
6503               if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
6504                 return -1;
6505             }
6506
6507           if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6508             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6509           if (opcode == 0xa4 || opcode == 0xa5)
6510             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6511           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6512           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6513         }
6514       break;
6515
6516     case 0xa6:    /* cmpsS */
6517     case 0xa7:
6518       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6519       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6520       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6521         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6522       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6523       break;
6524
6525     case 0xac:    /* lodsS */
6526     case 0xad:
6527       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6528       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6529       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6530         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6531       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6532       break;
6533
6534     case 0xae:    /* scasS */
6535     case 0xaf:
6536       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6537       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6538         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6539       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6540       break;
6541
6542     case 0x6e:    /* outsS */
6543     case 0x6f:
6544       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
6545       if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
6546         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6547       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6548       break;
6549
6550     case 0xe4:    /* port I/O */
6551     case 0xe5:
6552     case 0xec:
6553     case 0xed:
6554       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6555       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6556       break;
6557
6558     case 0xe6:
6559     case 0xe7:
6560     case 0xee:
6561     case 0xef:
6562       break;
6563
6564       /* control */
6565     case 0xc2:    /* ret im */
6566     case 0xc3:    /* ret */
6567       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6568       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6569       break;
6570
6571     case 0xca:    /* lret im */
6572     case 0xcb:    /* lret */
6573     case 0xcf:    /* iret */
6574       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
6575       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6576       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6577       break;
6578
6579     case 0xe8:    /* call im */
6580       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
6581         ir.dflag = 2;
6582       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6583         return -1;
6584       break;
6585
6586     case 0x9a:    /* lcall im */
6587       if (ir.regmap[X86_RECORD_R8_REGNUM])
6588         {
6589           ir.addr -= 1;
6590           goto no_support;
6591         }
6592       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
6593       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6594         return -1;
6595       break;
6596
6597     case 0xe9:    /* jmp im */
6598     case 0xea:    /* ljmp im */
6599     case 0xeb:    /* jmp Jb */
6600     case 0x70:    /* jcc Jb */
6601     case 0x71:
6602     case 0x72:
6603     case 0x73:
6604     case 0x74:
6605     case 0x75:
6606     case 0x76:
6607     case 0x77:
6608     case 0x78:
6609     case 0x79:
6610     case 0x7a:
6611     case 0x7b:
6612     case 0x7c:
6613     case 0x7d:
6614     case 0x7e:
6615     case 0x7f:
6616     case 0x0f80:  /* jcc Jv */
6617     case 0x0f81:
6618     case 0x0f82:
6619     case 0x0f83:
6620     case 0x0f84:
6621     case 0x0f85:
6622     case 0x0f86:
6623     case 0x0f87:
6624     case 0x0f88:
6625     case 0x0f89:
6626     case 0x0f8a:
6627     case 0x0f8b:
6628     case 0x0f8c:
6629     case 0x0f8d:
6630     case 0x0f8e:
6631     case 0x0f8f:
6632       break;
6633
6634     case 0x0f90:  /* setcc Gv */
6635     case 0x0f91:
6636     case 0x0f92:
6637     case 0x0f93:
6638     case 0x0f94:
6639     case 0x0f95:
6640     case 0x0f96:
6641     case 0x0f97:
6642     case 0x0f98:
6643     case 0x0f99:
6644     case 0x0f9a:
6645     case 0x0f9b:
6646     case 0x0f9c:
6647     case 0x0f9d:
6648     case 0x0f9e:
6649     case 0x0f9f:
6650       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6651       ir.ot = OT_BYTE;
6652       if (i386_record_modrm (&ir))
6653         return -1;
6654       if (ir.mod == 3)
6655         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b ? (ir.rm | ir.rex_b)
6656                                             : (ir.rm & 0x3));
6657       else
6658         {
6659           if (i386_record_lea_modrm (&ir))
6660             return -1;
6661         }
6662       break;
6663
6664     case 0x0f40:    /* cmov Gv, Ev */
6665     case 0x0f41:
6666     case 0x0f42:
6667     case 0x0f43:
6668     case 0x0f44:
6669     case 0x0f45:
6670     case 0x0f46:
6671     case 0x0f47:
6672     case 0x0f48:
6673     case 0x0f49:
6674     case 0x0f4a:
6675     case 0x0f4b:
6676     case 0x0f4c:
6677     case 0x0f4d:
6678     case 0x0f4e:
6679     case 0x0f4f:
6680       if (i386_record_modrm (&ir))
6681         return -1;
6682       ir.reg |= rex_r;
6683       if (ir.dflag == OT_BYTE)
6684         ir.reg &= 0x3;
6685       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
6686       break;
6687
6688       /* flags */
6689     case 0x9c:    /* pushf */
6690       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6691       if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
6692         ir.dflag = 2;
6693       if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6694         return -1;
6695       break;
6696
6697     case 0x9d:    /* popf */
6698       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6699       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6700       break;
6701
6702     case 0x9e:    /* sahf */
6703       if (ir.regmap[X86_RECORD_R8_REGNUM])
6704         {
6705           ir.addr -= 1;
6706           goto no_support;
6707         }
6708       /* FALLTHROUGH */
6709     case 0xf5:    /* cmc */
6710     case 0xf8:    /* clc */
6711     case 0xf9:    /* stc */
6712     case 0xfc:    /* cld */
6713     case 0xfd:    /* std */
6714       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6715       break;
6716
6717     case 0x9f:    /* lahf */
6718       if (ir.regmap[X86_RECORD_R8_REGNUM])
6719         {
6720           ir.addr -= 1;
6721           goto no_support;
6722         }
6723       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6724       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6725       break;
6726
6727       /* bit operations */
6728     case 0x0fba:    /* bt/bts/btr/btc Gv, im */
6729       ir.ot = ir.dflag + OT_WORD;
6730       if (i386_record_modrm (&ir))
6731         return -1;
6732       if (ir.reg < 4)
6733         {
6734           ir.addr -= 2;
6735           opcode = opcode << 8 | ir.modrm;
6736           goto no_support;
6737         }
6738       if (ir.reg != 4)
6739         {
6740           if (ir.mod == 3)
6741             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
6742           else
6743             {
6744               if (i386_record_lea_modrm (&ir))
6745                 return -1;
6746             }
6747         }
6748       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6749       break;
6750
6751     case 0x0fa3:    /* bt Gv, Ev */
6752       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6753       break;
6754
6755     case 0x0fab:    /* bts */
6756     case 0x0fb3:    /* btr */
6757     case 0x0fbb:    /* btc */
6758       ir.ot = ir.dflag + OT_WORD;
6759       if (i386_record_modrm (&ir))
6760         return -1;
6761       if (ir.mod == 3)
6762         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
6763       else
6764         {
6765           uint64_t addr64;
6766           if (i386_record_lea_modrm_addr (&ir, &addr64))
6767             return -1;
6768           regcache_raw_read_unsigned (ir.regcache,
6769                                       ir.regmap[ir.reg | rex_r],
6770                                       &addr);
6771           switch (ir.dflag)
6772             {
6773             case 0:
6774               addr64 += ((int16_t) addr >> 4) << 4;
6775               break;
6776             case 1:
6777               addr64 += ((int32_t) addr >> 5) << 5;
6778               break;
6779             case 2:
6780               addr64 += ((int64_t) addr >> 6) << 6;
6781               break;
6782             }
6783           if (record_full_arch_list_add_mem (addr64, 1 << ir.ot))
6784             return -1;
6785           if (i386_record_lea_modrm (&ir))
6786             return -1;
6787         }
6788       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6789       break;
6790
6791     case 0x0fbc:    /* bsf */
6792     case 0x0fbd:    /* bsr */
6793       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
6794       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6795       break;
6796
6797       /* bcd */
6798     case 0x27:    /* daa */
6799     case 0x2f:    /* das */
6800     case 0x37:    /* aaa */
6801     case 0x3f:    /* aas */
6802     case 0xd4:    /* aam */
6803     case 0xd5:    /* aad */
6804       if (ir.regmap[X86_RECORD_R8_REGNUM])
6805         {
6806           ir.addr -= 1;
6807           goto no_support;
6808         }
6809       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6810       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6811       break;
6812
6813       /* misc */
6814     case 0x90:    /* nop */
6815       if (prefixes & PREFIX_LOCK)
6816         {
6817           ir.addr -= 1;
6818           goto no_support;
6819         }
6820       break;
6821
6822     case 0x9b:    /* fwait */
6823       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
6824         return -1;
6825       opcode = (uint32_t) opcode8;
6826       ir.addr++;
6827       goto reswitch;
6828       break;
6829
6830       /* XXX */
6831     case 0xcc:    /* int3 */
6832       printf_unfiltered (_("Process record does not support instruction "
6833                            "int3.\n"));
6834       ir.addr -= 1;
6835       goto no_support;
6836       break;
6837
6838       /* XXX */
6839     case 0xcd:    /* int */
6840       {
6841         int ret;
6842         uint8_t interrupt;
6843         if (record_read_memory (gdbarch, ir.addr, &interrupt, 1))
6844           return -1;
6845         ir.addr++;
6846         if (interrupt != 0x80
6847             || tdep->i386_intx80_record == NULL)
6848           {
6849             printf_unfiltered (_("Process record does not support "
6850                                  "instruction int 0x%02x.\n"),
6851                                interrupt);
6852             ir.addr -= 2;
6853             goto no_support;
6854           }
6855         ret = tdep->i386_intx80_record (ir.regcache);
6856         if (ret)
6857           return ret;
6858       }
6859       break;
6860
6861       /* XXX */
6862     case 0xce:    /* into */
6863       printf_unfiltered (_("Process record does not support "
6864                            "instruction into.\n"));
6865       ir.addr -= 1;
6866       goto no_support;
6867       break;
6868
6869     case 0xfa:    /* cli */
6870     case 0xfb:    /* sti */
6871       break;
6872
6873     case 0x62:    /* bound */
6874       printf_unfiltered (_("Process record does not support "
6875                            "instruction bound.\n"));
6876       ir.addr -= 1;
6877       goto no_support;
6878       break;
6879
6880     case 0x0fc8:    /* bswap reg */
6881     case 0x0fc9:
6882     case 0x0fca:
6883     case 0x0fcb:
6884     case 0x0fcc:
6885     case 0x0fcd:
6886     case 0x0fce:
6887     case 0x0fcf:
6888       I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 7) | ir.rex_b);
6889       break;
6890
6891     case 0xd6:    /* salc */
6892       if (ir.regmap[X86_RECORD_R8_REGNUM])
6893         {
6894           ir.addr -= 1;
6895           goto no_support;
6896         }
6897       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6898       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6899       break;
6900
6901     case 0xe0:    /* loopnz */
6902     case 0xe1:    /* loopz */
6903     case 0xe2:    /* loop */
6904     case 0xe3:    /* jecxz */
6905       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6906       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6907       break;
6908
6909     case 0x0f30:    /* wrmsr */
6910       printf_unfiltered (_("Process record does not support "
6911                            "instruction wrmsr.\n"));
6912       ir.addr -= 2;
6913       goto no_support;
6914       break;
6915
6916     case 0x0f32:    /* rdmsr */
6917       printf_unfiltered (_("Process record does not support "
6918                            "instruction rdmsr.\n"));
6919       ir.addr -= 2;
6920       goto no_support;
6921       break;
6922
6923     case 0x0f31:    /* rdtsc */
6924       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6925       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
6926       break;
6927
6928     case 0x0f34:    /* sysenter */
6929       {
6930         int ret;
6931         if (ir.regmap[X86_RECORD_R8_REGNUM])
6932           {
6933             ir.addr -= 2;
6934             goto no_support;
6935           }
6936         if (tdep->i386_sysenter_record == NULL)
6937           {
6938             printf_unfiltered (_("Process record does not support "
6939                                  "instruction sysenter.\n"));
6940             ir.addr -= 2;
6941             goto no_support;
6942           }
6943         ret = tdep->i386_sysenter_record (ir.regcache);
6944         if (ret)
6945           return ret;
6946       }
6947       break;
6948
6949     case 0x0f35:    /* sysexit */
6950       printf_unfiltered (_("Process record does not support "
6951                            "instruction sysexit.\n"));
6952       ir.addr -= 2;
6953       goto no_support;
6954       break;
6955
6956     case 0x0f05:    /* syscall */
6957       {
6958         int ret;
6959         if (tdep->i386_syscall_record == NULL)
6960           {
6961             printf_unfiltered (_("Process record does not support "
6962                                  "instruction syscall.\n"));
6963             ir.addr -= 2;
6964             goto no_support;
6965           }
6966         ret = tdep->i386_syscall_record (ir.regcache);
6967         if (ret)
6968           return ret;
6969       }
6970       break;
6971
6972     case 0x0f07:    /* sysret */
6973       printf_unfiltered (_("Process record does not support "
6974                            "instruction sysret.\n"));
6975       ir.addr -= 2;
6976       goto no_support;
6977       break;
6978
6979     case 0x0fa2:    /* cpuid */
6980       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6981       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6982       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
6983       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
6984       break;
6985
6986     case 0xf4:    /* hlt */
6987       printf_unfiltered (_("Process record does not support "
6988                            "instruction hlt.\n"));
6989       ir.addr -= 1;
6990       goto no_support;
6991       break;
6992
6993     case 0x0f00:
6994       if (i386_record_modrm (&ir))
6995         return -1;
6996       switch (ir.reg)
6997         {
6998         case 0:  /* sldt */
6999         case 1:  /* str  */
7000           if (ir.mod == 3)
7001             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7002           else
7003             {
7004               ir.ot = OT_WORD;
7005               if (i386_record_lea_modrm (&ir))
7006                 return -1;
7007             }
7008           break;
7009         case 2:  /* lldt */
7010         case 3:  /* ltr */
7011           break;
7012         case 4:  /* verr */
7013         case 5:  /* verw */
7014           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7015           break;
7016         default:
7017           ir.addr -= 3;
7018           opcode = opcode << 8 | ir.modrm;
7019           goto no_support;
7020           break;
7021         }
7022       break;
7023
7024     case 0x0f01:
7025       if (i386_record_modrm (&ir))
7026         return -1;
7027       switch (ir.reg)
7028         {
7029         case 0:  /* sgdt */
7030           {
7031             uint64_t addr64;
7032
7033             if (ir.mod == 3)
7034               {
7035                 ir.addr -= 3;
7036                 opcode = opcode << 8 | ir.modrm;
7037                 goto no_support;
7038               }
7039             if (ir.override >= 0)
7040               {
7041                 if (record_full_memory_query)
7042                   {
7043                     if (yquery (_("\
7044 Process record ignores the memory change of instruction at address %s\n\
7045 because it can't get the value of the segment register.\n\
7046 Do you want to stop the program?"),
7047                                 paddress (gdbarch, ir.orig_addr)))
7048                       return -1;
7049                   }
7050               }
7051             else
7052               {
7053                 if (i386_record_lea_modrm_addr (&ir, &addr64))
7054                   return -1;
7055                 if (record_full_arch_list_add_mem (addr64, 2))
7056                   return -1;
7057                 addr64 += 2;
7058                 if (ir.regmap[X86_RECORD_R8_REGNUM])
7059                   {
7060                     if (record_full_arch_list_add_mem (addr64, 8))
7061                       return -1;
7062                   }
7063                 else
7064                   {
7065                     if (record_full_arch_list_add_mem (addr64, 4))
7066                       return -1;
7067                   }
7068               }
7069           }
7070           break;
7071         case 1:
7072           if (ir.mod == 3)
7073             {
7074               switch (ir.rm)
7075                 {
7076                 case 0:  /* monitor */
7077                   break;
7078                 case 1:  /* mwait */
7079                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7080                   break;
7081                 default:
7082                   ir.addr -= 3;
7083                   opcode = opcode << 8 | ir.modrm;
7084                   goto no_support;
7085                   break;
7086                 }
7087             }
7088           else
7089             {
7090               /* sidt */
7091               if (ir.override >= 0)
7092                 {
7093                   if (record_full_memory_query)
7094                     {
7095                       if (yquery (_("\
7096 Process record ignores the memory change of instruction at address %s\n\
7097 because it can't get the value of the segment register.\n\
7098 Do you want to stop the program?"),
7099                                   paddress (gdbarch, ir.orig_addr)))
7100                         return -1;
7101                     }
7102                 }
7103               else
7104                 {
7105                   uint64_t addr64;
7106
7107                   if (i386_record_lea_modrm_addr (&ir, &addr64))
7108                     return -1;
7109                   if (record_full_arch_list_add_mem (addr64, 2))
7110                     return -1;
7111                   addr64 += 2;
7112                   if (ir.regmap[X86_RECORD_R8_REGNUM])
7113                     {
7114                       if (record_full_arch_list_add_mem (addr64, 8))
7115                         return -1;
7116                     }
7117                   else
7118                     {
7119                       if (record_full_arch_list_add_mem (addr64, 4))
7120                         return -1;
7121                     }
7122                 }
7123             }
7124           break;
7125         case 2:  /* lgdt */
7126           if (ir.mod == 3)
7127             {
7128               /* xgetbv */
7129               if (ir.rm == 0)
7130                 {
7131                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7132                   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7133                   break;
7134                 }
7135               /* xsetbv */
7136               else if (ir.rm == 1)
7137                 break;
7138             }
7139         case 3:  /* lidt */
7140           if (ir.mod == 3)
7141             {
7142               ir.addr -= 3;
7143               opcode = opcode << 8 | ir.modrm;
7144               goto no_support;
7145             }
7146           break;
7147         case 4:  /* smsw */
7148           if (ir.mod == 3)
7149             {
7150               if (record_full_arch_list_add_reg (ir.regcache, ir.rm | ir.rex_b))
7151                 return -1;
7152             }
7153           else
7154             {
7155               ir.ot = OT_WORD;
7156               if (i386_record_lea_modrm (&ir))
7157                 return -1;
7158             }
7159           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7160           break;
7161         case 6:  /* lmsw */
7162           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7163           break;
7164         case 7:  /* invlpg */
7165           if (ir.mod == 3)
7166             {
7167               if (ir.rm == 0 && ir.regmap[X86_RECORD_R8_REGNUM])
7168                 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
7169               else
7170                 {
7171                   ir.addr -= 3;
7172                   opcode = opcode << 8 | ir.modrm;
7173                   goto no_support;
7174                 }
7175             }
7176           else
7177             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7178           break;
7179         default:
7180           ir.addr -= 3;
7181           opcode = opcode << 8 | ir.modrm;
7182           goto no_support;
7183           break;
7184         }
7185       break;
7186
7187     case 0x0f08:    /* invd */
7188     case 0x0f09:    /* wbinvd */
7189       break;
7190
7191     case 0x63:    /* arpl */
7192       if (i386_record_modrm (&ir))
7193         return -1;
7194       if (ir.mod == 3 || ir.regmap[X86_RECORD_R8_REGNUM])
7195         {
7196           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.regmap[X86_RECORD_R8_REGNUM]
7197                                               ? (ir.reg | rex_r) : ir.rm);
7198         }
7199       else
7200         {
7201           ir.ot = ir.dflag ? OT_LONG : OT_WORD;
7202           if (i386_record_lea_modrm (&ir))
7203             return -1;
7204         }
7205       if (!ir.regmap[X86_RECORD_R8_REGNUM])
7206         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7207       break;
7208
7209     case 0x0f02:    /* lar */
7210     case 0x0f03:    /* lsl */
7211       if (i386_record_modrm (&ir))
7212         return -1;
7213       I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7214       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7215       break;
7216
7217     case 0x0f18:
7218       if (i386_record_modrm (&ir))
7219         return -1;
7220       if (ir.mod == 3 && ir.reg == 3)
7221         {
7222           ir.addr -= 3;
7223           opcode = opcode << 8 | ir.modrm;
7224           goto no_support;
7225         }
7226       break;
7227
7228     case 0x0f19:
7229     case 0x0f1a:
7230     case 0x0f1b:
7231     case 0x0f1c:
7232     case 0x0f1d:
7233     case 0x0f1e:
7234     case 0x0f1f:
7235       /* nop (multi byte) */
7236       break;
7237
7238     case 0x0f20:    /* mov reg, crN */
7239     case 0x0f22:    /* mov crN, reg */
7240       if (i386_record_modrm (&ir))
7241         return -1;
7242       if ((ir.modrm & 0xc0) != 0xc0)
7243         {
7244           ir.addr -= 3;
7245           opcode = opcode << 8 | ir.modrm;
7246           goto no_support;
7247         }
7248       switch (ir.reg)
7249         {
7250         case 0:
7251         case 2:
7252         case 3:
7253         case 4:
7254         case 8:
7255           if (opcode & 2)
7256             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7257           else
7258             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7259           break;
7260         default:
7261           ir.addr -= 3;
7262           opcode = opcode << 8 | ir.modrm;
7263           goto no_support;
7264           break;
7265         }
7266       break;
7267
7268     case 0x0f21:    /* mov reg, drN */
7269     case 0x0f23:    /* mov drN, reg */
7270       if (i386_record_modrm (&ir))
7271         return -1;
7272       if ((ir.modrm & 0xc0) != 0xc0 || ir.reg == 4
7273           || ir.reg == 5 || ir.reg >= 8)
7274         {
7275           ir.addr -= 3;
7276           opcode = opcode << 8 | ir.modrm;
7277           goto no_support;
7278         }
7279       if (opcode & 2)
7280         I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7281       else
7282         I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7283       break;
7284
7285     case 0x0f06:    /* clts */
7286       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7287       break;
7288
7289     /* MMX 3DNow! SSE SSE2 SSE3 SSSE3 SSE4 */
7290
7291     case 0x0f0d:    /* 3DNow! prefetch */
7292       break;
7293
7294     case 0x0f0e:    /* 3DNow! femms */
7295     case 0x0f77:    /* emms */
7296       if (i386_fpc_regnum_p (gdbarch, I387_FTAG_REGNUM(tdep)))
7297         goto no_support;
7298       record_full_arch_list_add_reg (ir.regcache, I387_FTAG_REGNUM(tdep));
7299       break;
7300
7301     case 0x0f0f:    /* 3DNow! data */
7302       if (i386_record_modrm (&ir))
7303         return -1;
7304       if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7305         return -1;
7306       ir.addr++;
7307       switch (opcode8)
7308         {
7309         case 0x0c:    /* 3DNow! pi2fw */
7310         case 0x0d:    /* 3DNow! pi2fd */
7311         case 0x1c:    /* 3DNow! pf2iw */
7312         case 0x1d:    /* 3DNow! pf2id */
7313         case 0x8a:    /* 3DNow! pfnacc */
7314         case 0x8e:    /* 3DNow! pfpnacc */
7315         case 0x90:    /* 3DNow! pfcmpge */
7316         case 0x94:    /* 3DNow! pfmin */
7317         case 0x96:    /* 3DNow! pfrcp */
7318         case 0x97:    /* 3DNow! pfrsqrt */
7319         case 0x9a:    /* 3DNow! pfsub */
7320         case 0x9e:    /* 3DNow! pfadd */
7321         case 0xa0:    /* 3DNow! pfcmpgt */
7322         case 0xa4:    /* 3DNow! pfmax */
7323         case 0xa6:    /* 3DNow! pfrcpit1 */
7324         case 0xa7:    /* 3DNow! pfrsqit1 */
7325         case 0xaa:    /* 3DNow! pfsubr */
7326         case 0xae:    /* 3DNow! pfacc */
7327         case 0xb0:    /* 3DNow! pfcmpeq */
7328         case 0xb4:    /* 3DNow! pfmul */
7329         case 0xb6:    /* 3DNow! pfrcpit2 */
7330         case 0xb7:    /* 3DNow! pmulhrw */
7331         case 0xbb:    /* 3DNow! pswapd */
7332         case 0xbf:    /* 3DNow! pavgusb */
7333           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
7334             goto no_support_3dnow_data;
7335           record_full_arch_list_add_reg (ir.regcache, ir.reg);
7336           break;
7337
7338         default:
7339 no_support_3dnow_data:
7340           opcode = (opcode << 8) | opcode8;
7341           goto no_support;
7342           break;
7343         }
7344       break;
7345
7346     case 0x0faa:    /* rsm */
7347       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7348       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7349       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
7350       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7351       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBX_REGNUM);
7352       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
7353       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
7354       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
7355       I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
7356       break;
7357
7358     case 0x0fae:
7359       if (i386_record_modrm (&ir))
7360         return -1;
7361       switch(ir.reg)
7362         {
7363         case 0:    /* fxsave */
7364           {
7365             uint64_t tmpu64;
7366
7367             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7368             if (i386_record_lea_modrm_addr (&ir, &tmpu64))
7369               return -1;
7370             if (record_full_arch_list_add_mem (tmpu64, 512))
7371               return -1;
7372           }
7373           break;
7374
7375         case 1:    /* fxrstor */
7376           {
7377             int i;
7378
7379             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7380
7381             for (i = I387_MM0_REGNUM (tdep);
7382                  i386_mmx_regnum_p (gdbarch, i); i++)
7383               record_full_arch_list_add_reg (ir.regcache, i);
7384
7385             for (i = I387_XMM0_REGNUM (tdep);
7386                  i386_xmm_regnum_p (gdbarch, i); i++)
7387               record_full_arch_list_add_reg (ir.regcache, i);
7388
7389             if (i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
7390               record_full_arch_list_add_reg (ir.regcache,
7391                                              I387_MXCSR_REGNUM(tdep));
7392
7393             for (i = I387_ST0_REGNUM (tdep);
7394                  i386_fp_regnum_p (gdbarch, i); i++)
7395               record_full_arch_list_add_reg (ir.regcache, i);
7396
7397             for (i = I387_FCTRL_REGNUM (tdep);
7398                  i386_fpc_regnum_p (gdbarch, i); i++)
7399               record_full_arch_list_add_reg (ir.regcache, i);
7400           }
7401           break;
7402
7403         case 2:    /* ldmxcsr */
7404           if (!i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
7405             goto no_support;
7406           record_full_arch_list_add_reg (ir.regcache, I387_MXCSR_REGNUM(tdep));
7407           break;
7408
7409         case 3:    /* stmxcsr */
7410           ir.ot = OT_LONG;
7411           if (i386_record_lea_modrm (&ir))
7412             return -1;
7413           break;
7414
7415         case 5:    /* lfence */
7416         case 6:    /* mfence */
7417         case 7:    /* sfence clflush */
7418           break;
7419
7420         default:
7421           opcode = (opcode << 8) | ir.modrm;
7422           goto no_support;
7423           break;
7424         }
7425       break;
7426
7427     case 0x0fc3:    /* movnti */
7428       ir.ot = (ir.dflag == 2) ? OT_QUAD : OT_LONG;
7429       if (i386_record_modrm (&ir))
7430         return -1;
7431       if (ir.mod == 3)
7432         goto no_support;
7433       ir.reg |= rex_r;
7434       if (i386_record_lea_modrm (&ir))
7435         return -1;
7436       break;
7437
7438     /* Add prefix to opcode.  */
7439     case 0x0f10:
7440     case 0x0f11:
7441     case 0x0f12:
7442     case 0x0f13:
7443     case 0x0f14:
7444     case 0x0f15:
7445     case 0x0f16:
7446     case 0x0f17:
7447     case 0x0f28:
7448     case 0x0f29:
7449     case 0x0f2a:
7450     case 0x0f2b:
7451     case 0x0f2c:
7452     case 0x0f2d:
7453     case 0x0f2e:
7454     case 0x0f2f:
7455     case 0x0f38:
7456     case 0x0f39:
7457     case 0x0f3a:
7458     case 0x0f50:
7459     case 0x0f51:
7460     case 0x0f52:
7461     case 0x0f53:
7462     case 0x0f54:
7463     case 0x0f55:
7464     case 0x0f56:
7465     case 0x0f57:
7466     case 0x0f58:
7467     case 0x0f59:
7468     case 0x0f5a:
7469     case 0x0f5b:
7470     case 0x0f5c:
7471     case 0x0f5d:
7472     case 0x0f5e:
7473     case 0x0f5f:
7474     case 0x0f60:
7475     case 0x0f61:
7476     case 0x0f62:
7477     case 0x0f63:
7478     case 0x0f64:
7479     case 0x0f65:
7480     case 0x0f66:
7481     case 0x0f67:
7482     case 0x0f68:
7483     case 0x0f69:
7484     case 0x0f6a:
7485     case 0x0f6b:
7486     case 0x0f6c:
7487     case 0x0f6d:
7488     case 0x0f6e:
7489     case 0x0f6f:
7490     case 0x0f70:
7491     case 0x0f71:
7492     case 0x0f72:
7493     case 0x0f73:
7494     case 0x0f74:
7495     case 0x0f75:
7496     case 0x0f76:
7497     case 0x0f7c:
7498     case 0x0f7d:
7499     case 0x0f7e:
7500     case 0x0f7f:
7501     case 0x0fb8:
7502     case 0x0fc2:
7503     case 0x0fc4:
7504     case 0x0fc5:
7505     case 0x0fc6:
7506     case 0x0fd0:
7507     case 0x0fd1:
7508     case 0x0fd2:
7509     case 0x0fd3:
7510     case 0x0fd4:
7511     case 0x0fd5:
7512     case 0x0fd6:
7513     case 0x0fd7:
7514     case 0x0fd8:
7515     case 0x0fd9:
7516     case 0x0fda:
7517     case 0x0fdb:
7518     case 0x0fdc:
7519     case 0x0fdd:
7520     case 0x0fde:
7521     case 0x0fdf:
7522     case 0x0fe0:
7523     case 0x0fe1:
7524     case 0x0fe2:
7525     case 0x0fe3:
7526     case 0x0fe4:
7527     case 0x0fe5:
7528     case 0x0fe6:
7529     case 0x0fe7:
7530     case 0x0fe8:
7531     case 0x0fe9:
7532     case 0x0fea:
7533     case 0x0feb:
7534     case 0x0fec:
7535     case 0x0fed:
7536     case 0x0fee:
7537     case 0x0fef:
7538     case 0x0ff0:
7539     case 0x0ff1:
7540     case 0x0ff2:
7541     case 0x0ff3:
7542     case 0x0ff4:
7543     case 0x0ff5:
7544     case 0x0ff6:
7545     case 0x0ff7:
7546     case 0x0ff8:
7547     case 0x0ff9:
7548     case 0x0ffa:
7549     case 0x0ffb:
7550     case 0x0ffc:
7551     case 0x0ffd:
7552     case 0x0ffe:
7553       /* Mask out PREFIX_ADDR.  */
7554       switch ((prefixes & ~PREFIX_ADDR))
7555         {
7556         case PREFIX_REPNZ:
7557           opcode |= 0xf20000;
7558           break;
7559         case PREFIX_DATA:
7560           opcode |= 0x660000;
7561           break;
7562         case PREFIX_REPZ:
7563           opcode |= 0xf30000;
7564           break;
7565         }
7566 reswitch_prefix_add:
7567       switch (opcode)
7568         {
7569         case 0x0f38:
7570         case 0x660f38:
7571         case 0xf20f38:
7572         case 0x0f3a:
7573         case 0x660f3a:
7574           if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7575             return -1;
7576           ir.addr++;
7577           opcode = (uint32_t) opcode8 | opcode << 8;
7578           goto reswitch_prefix_add;
7579           break;
7580
7581         case 0x0f10:        /* movups */
7582         case 0x660f10:      /* movupd */
7583         case 0xf30f10:      /* movss */
7584         case 0xf20f10:      /* movsd */
7585         case 0x0f12:        /* movlps */
7586         case 0x660f12:      /* movlpd */
7587         case 0xf30f12:      /* movsldup */
7588         case 0xf20f12:      /* movddup */
7589         case 0x0f14:        /* unpcklps */
7590         case 0x660f14:      /* unpcklpd */
7591         case 0x0f15:        /* unpckhps */
7592         case 0x660f15:      /* unpckhpd */
7593         case 0x0f16:        /* movhps */
7594         case 0x660f16:      /* movhpd */
7595         case 0xf30f16:      /* movshdup */
7596         case 0x0f28:        /* movaps */
7597         case 0x660f28:      /* movapd */
7598         case 0x0f2a:        /* cvtpi2ps */
7599         case 0x660f2a:      /* cvtpi2pd */
7600         case 0xf30f2a:      /* cvtsi2ss */
7601         case 0xf20f2a:      /* cvtsi2sd */
7602         case 0x0f2c:        /* cvttps2pi */
7603         case 0x660f2c:      /* cvttpd2pi */
7604         case 0x0f2d:        /* cvtps2pi */
7605         case 0x660f2d:      /* cvtpd2pi */
7606         case 0x660f3800:    /* pshufb */
7607         case 0x660f3801:    /* phaddw */
7608         case 0x660f3802:    /* phaddd */
7609         case 0x660f3803:    /* phaddsw */
7610         case 0x660f3804:    /* pmaddubsw */
7611         case 0x660f3805:    /* phsubw */
7612         case 0x660f3806:    /* phsubd */
7613         case 0x660f3807:    /* phsubsw */
7614         case 0x660f3808:    /* psignb */
7615         case 0x660f3809:    /* psignw */
7616         case 0x660f380a:    /* psignd */
7617         case 0x660f380b:    /* pmulhrsw */
7618         case 0x660f3810:    /* pblendvb */
7619         case 0x660f3814:    /* blendvps */
7620         case 0x660f3815:    /* blendvpd */
7621         case 0x660f381c:    /* pabsb */
7622         case 0x660f381d:    /* pabsw */
7623         case 0x660f381e:    /* pabsd */
7624         case 0x660f3820:    /* pmovsxbw */
7625         case 0x660f3821:    /* pmovsxbd */
7626         case 0x660f3822:    /* pmovsxbq */
7627         case 0x660f3823:    /* pmovsxwd */
7628         case 0x660f3824:    /* pmovsxwq */
7629         case 0x660f3825:    /* pmovsxdq */
7630         case 0x660f3828:    /* pmuldq */
7631         case 0x660f3829:    /* pcmpeqq */
7632         case 0x660f382a:    /* movntdqa */
7633         case 0x660f3a08:    /* roundps */
7634         case 0x660f3a09:    /* roundpd */
7635         case 0x660f3a0a:    /* roundss */
7636         case 0x660f3a0b:    /* roundsd */
7637         case 0x660f3a0c:    /* blendps */
7638         case 0x660f3a0d:    /* blendpd */
7639         case 0x660f3a0e:    /* pblendw */
7640         case 0x660f3a0f:    /* palignr */
7641         case 0x660f3a20:    /* pinsrb */
7642         case 0x660f3a21:    /* insertps */
7643         case 0x660f3a22:    /* pinsrd pinsrq */
7644         case 0x660f3a40:    /* dpps */
7645         case 0x660f3a41:    /* dppd */
7646         case 0x660f3a42:    /* mpsadbw */
7647         case 0x660f3a60:    /* pcmpestrm */
7648         case 0x660f3a61:    /* pcmpestri */
7649         case 0x660f3a62:    /* pcmpistrm */
7650         case 0x660f3a63:    /* pcmpistri */
7651         case 0x0f51:        /* sqrtps */
7652         case 0x660f51:      /* sqrtpd */
7653         case 0xf20f51:      /* sqrtsd */
7654         case 0xf30f51:      /* sqrtss */
7655         case 0x0f52:        /* rsqrtps */
7656         case 0xf30f52:      /* rsqrtss */
7657         case 0x0f53:        /* rcpps */
7658         case 0xf30f53:      /* rcpss */
7659         case 0x0f54:        /* andps */
7660         case 0x660f54:      /* andpd */
7661         case 0x0f55:        /* andnps */
7662         case 0x660f55:      /* andnpd */
7663         case 0x0f56:        /* orps */
7664         case 0x660f56:      /* orpd */
7665         case 0x0f57:        /* xorps */
7666         case 0x660f57:      /* xorpd */
7667         case 0x0f58:        /* addps */
7668         case 0x660f58:      /* addpd */
7669         case 0xf20f58:      /* addsd */
7670         case 0xf30f58:      /* addss */
7671         case 0x0f59:        /* mulps */
7672         case 0x660f59:      /* mulpd */
7673         case 0xf20f59:      /* mulsd */
7674         case 0xf30f59:      /* mulss */
7675         case 0x0f5a:        /* cvtps2pd */
7676         case 0x660f5a:      /* cvtpd2ps */
7677         case 0xf20f5a:      /* cvtsd2ss */
7678         case 0xf30f5a:      /* cvtss2sd */
7679         case 0x0f5b:        /* cvtdq2ps */
7680         case 0x660f5b:      /* cvtps2dq */
7681         case 0xf30f5b:      /* cvttps2dq */
7682         case 0x0f5c:        /* subps */
7683         case 0x660f5c:      /* subpd */
7684         case 0xf20f5c:      /* subsd */
7685         case 0xf30f5c:      /* subss */
7686         case 0x0f5d:        /* minps */
7687         case 0x660f5d:      /* minpd */
7688         case 0xf20f5d:      /* minsd */
7689         case 0xf30f5d:      /* minss */
7690         case 0x0f5e:        /* divps */
7691         case 0x660f5e:      /* divpd */
7692         case 0xf20f5e:      /* divsd */
7693         case 0xf30f5e:      /* divss */
7694         case 0x0f5f:        /* maxps */
7695         case 0x660f5f:      /* maxpd */
7696         case 0xf20f5f:      /* maxsd */
7697         case 0xf30f5f:      /* maxss */
7698         case 0x660f60:      /* punpcklbw */
7699         case 0x660f61:      /* punpcklwd */
7700         case 0x660f62:      /* punpckldq */
7701         case 0x660f63:      /* packsswb */
7702         case 0x660f64:      /* pcmpgtb */
7703         case 0x660f65:      /* pcmpgtw */
7704         case 0x660f66:      /* pcmpgtd */
7705         case 0x660f67:      /* packuswb */
7706         case 0x660f68:      /* punpckhbw */
7707         case 0x660f69:      /* punpckhwd */
7708         case 0x660f6a:      /* punpckhdq */
7709         case 0x660f6b:      /* packssdw */
7710         case 0x660f6c:      /* punpcklqdq */
7711         case 0x660f6d:      /* punpckhqdq */
7712         case 0x660f6e:      /* movd */
7713         case 0x660f6f:      /* movdqa */
7714         case 0xf30f6f:      /* movdqu */
7715         case 0x660f70:      /* pshufd */
7716         case 0xf20f70:      /* pshuflw */
7717         case 0xf30f70:      /* pshufhw */
7718         case 0x660f74:      /* pcmpeqb */
7719         case 0x660f75:      /* pcmpeqw */
7720         case 0x660f76:      /* pcmpeqd */
7721         case 0x660f7c:      /* haddpd */
7722         case 0xf20f7c:      /* haddps */
7723         case 0x660f7d:      /* hsubpd */
7724         case 0xf20f7d:      /* hsubps */
7725         case 0xf30f7e:      /* movq */
7726         case 0x0fc2:        /* cmpps */
7727         case 0x660fc2:      /* cmppd */
7728         case 0xf20fc2:      /* cmpsd */
7729         case 0xf30fc2:      /* cmpss */
7730         case 0x660fc4:      /* pinsrw */
7731         case 0x0fc6:        /* shufps */
7732         case 0x660fc6:      /* shufpd */
7733         case 0x660fd0:      /* addsubpd */
7734         case 0xf20fd0:      /* addsubps */
7735         case 0x660fd1:      /* psrlw */
7736         case 0x660fd2:      /* psrld */
7737         case 0x660fd3:      /* psrlq */
7738         case 0x660fd4:      /* paddq */
7739         case 0x660fd5:      /* pmullw */
7740         case 0xf30fd6:      /* movq2dq */
7741         case 0x660fd8:      /* psubusb */
7742         case 0x660fd9:      /* psubusw */
7743         case 0x660fda:      /* pminub */
7744         case 0x660fdb:      /* pand */
7745         case 0x660fdc:      /* paddusb */
7746         case 0x660fdd:      /* paddusw */
7747         case 0x660fde:      /* pmaxub */
7748         case 0x660fdf:      /* pandn */
7749         case 0x660fe0:      /* pavgb */
7750         case 0x660fe1:      /* psraw */
7751         case 0x660fe2:      /* psrad */
7752         case 0x660fe3:      /* pavgw */
7753         case 0x660fe4:      /* pmulhuw */
7754         case 0x660fe5:      /* pmulhw */
7755         case 0x660fe6:      /* cvttpd2dq */
7756         case 0xf20fe6:      /* cvtpd2dq */
7757         case 0xf30fe6:      /* cvtdq2pd */
7758         case 0x660fe8:      /* psubsb */
7759         case 0x660fe9:      /* psubsw */
7760         case 0x660fea:      /* pminsw */
7761         case 0x660feb:      /* por */
7762         case 0x660fec:      /* paddsb */
7763         case 0x660fed:      /* paddsw */
7764         case 0x660fee:      /* pmaxsw */
7765         case 0x660fef:      /* pxor */
7766         case 0xf20ff0:      /* lddqu */
7767         case 0x660ff1:      /* psllw */
7768         case 0x660ff2:      /* pslld */
7769         case 0x660ff3:      /* psllq */
7770         case 0x660ff4:      /* pmuludq */
7771         case 0x660ff5:      /* pmaddwd */
7772         case 0x660ff6:      /* psadbw */
7773         case 0x660ff8:      /* psubb */
7774         case 0x660ff9:      /* psubw */
7775         case 0x660ffa:      /* psubd */
7776         case 0x660ffb:      /* psubq */
7777         case 0x660ffc:      /* paddb */
7778         case 0x660ffd:      /* paddw */
7779         case 0x660ffe:      /* paddd */
7780           if (i386_record_modrm (&ir))
7781             return -1;
7782           ir.reg |= rex_r;
7783           if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.reg))
7784             goto no_support;
7785           record_full_arch_list_add_reg (ir.regcache,
7786                                          I387_XMM0_REGNUM (tdep) + ir.reg);
7787           if ((opcode & 0xfffffffc) == 0x660f3a60)
7788             I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7789           break;
7790
7791         case 0x0f11:        /* movups */
7792         case 0x660f11:      /* movupd */
7793         case 0xf30f11:      /* movss */
7794         case 0xf20f11:      /* movsd */
7795         case 0x0f13:        /* movlps */
7796         case 0x660f13:      /* movlpd */
7797         case 0x0f17:        /* movhps */
7798         case 0x660f17:      /* movhpd */
7799         case 0x0f29:        /* movaps */
7800         case 0x660f29:      /* movapd */
7801         case 0x660f3a14:    /* pextrb */
7802         case 0x660f3a15:    /* pextrw */
7803         case 0x660f3a16:    /* pextrd pextrq */
7804         case 0x660f3a17:    /* extractps */
7805         case 0x660f7f:      /* movdqa */
7806         case 0xf30f7f:      /* movdqu */
7807           if (i386_record_modrm (&ir))
7808             return -1;
7809           if (ir.mod == 3)
7810             {
7811               if (opcode == 0x0f13 || opcode == 0x660f13
7812                   || opcode == 0x0f17 || opcode == 0x660f17)
7813                 goto no_support;
7814               ir.rm |= ir.rex_b;
7815               if (!i386_xmm_regnum_p (gdbarch,
7816                                       I387_XMM0_REGNUM (tdep) + ir.rm))
7817                 goto no_support;
7818               record_full_arch_list_add_reg (ir.regcache,
7819                                              I387_XMM0_REGNUM (tdep) + ir.rm);
7820             }
7821           else
7822             {
7823               switch (opcode)
7824                 {
7825                   case 0x660f3a14:
7826                     ir.ot = OT_BYTE;
7827                     break;
7828                   case 0x660f3a15:
7829                     ir.ot = OT_WORD;
7830                     break;
7831                   case 0x660f3a16:
7832                     ir.ot = OT_LONG;
7833                     break;
7834                   case 0x660f3a17:
7835                     ir.ot = OT_QUAD;
7836                     break;
7837                   default:
7838                     ir.ot = OT_DQUAD;
7839                     break;
7840                 }
7841               if (i386_record_lea_modrm (&ir))
7842                 return -1;
7843             }
7844           break;
7845
7846         case 0x0f2b:      /* movntps */
7847         case 0x660f2b:    /* movntpd */
7848         case 0x0fe7:      /* movntq */
7849         case 0x660fe7:    /* movntdq */
7850           if (ir.mod == 3)
7851             goto no_support;
7852           if (opcode == 0x0fe7)
7853             ir.ot = OT_QUAD;
7854           else
7855             ir.ot = OT_DQUAD;
7856           if (i386_record_lea_modrm (&ir))
7857             return -1;
7858           break;
7859
7860         case 0xf30f2c:      /* cvttss2si */
7861         case 0xf20f2c:      /* cvttsd2si */
7862         case 0xf30f2d:      /* cvtss2si */
7863         case 0xf20f2d:      /* cvtsd2si */
7864         case 0xf20f38f0:    /* crc32 */
7865         case 0xf20f38f1:    /* crc32 */
7866         case 0x0f50:        /* movmskps */
7867         case 0x660f50:      /* movmskpd */
7868         case 0x0fc5:        /* pextrw */
7869         case 0x660fc5:      /* pextrw */
7870         case 0x0fd7:        /* pmovmskb */
7871         case 0x660fd7:      /* pmovmskb */
7872           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7873           break;
7874
7875         case 0x0f3800:    /* pshufb */
7876         case 0x0f3801:    /* phaddw */
7877         case 0x0f3802:    /* phaddd */
7878         case 0x0f3803:    /* phaddsw */
7879         case 0x0f3804:    /* pmaddubsw */
7880         case 0x0f3805:    /* phsubw */
7881         case 0x0f3806:    /* phsubd */
7882         case 0x0f3807:    /* phsubsw */
7883         case 0x0f3808:    /* psignb */
7884         case 0x0f3809:    /* psignw */
7885         case 0x0f380a:    /* psignd */
7886         case 0x0f380b:    /* pmulhrsw */
7887         case 0x0f381c:    /* pabsb */
7888         case 0x0f381d:    /* pabsw */
7889         case 0x0f381e:    /* pabsd */
7890         case 0x0f382b:    /* packusdw */
7891         case 0x0f3830:    /* pmovzxbw */
7892         case 0x0f3831:    /* pmovzxbd */
7893         case 0x0f3832:    /* pmovzxbq */
7894         case 0x0f3833:    /* pmovzxwd */
7895         case 0x0f3834:    /* pmovzxwq */
7896         case 0x0f3835:    /* pmovzxdq */
7897         case 0x0f3837:    /* pcmpgtq */
7898         case 0x0f3838:    /* pminsb */
7899         case 0x0f3839:    /* pminsd */
7900         case 0x0f383a:    /* pminuw */
7901         case 0x0f383b:    /* pminud */
7902         case 0x0f383c:    /* pmaxsb */
7903         case 0x0f383d:    /* pmaxsd */
7904         case 0x0f383e:    /* pmaxuw */
7905         case 0x0f383f:    /* pmaxud */
7906         case 0x0f3840:    /* pmulld */
7907         case 0x0f3841:    /* phminposuw */
7908         case 0x0f3a0f:    /* palignr */
7909         case 0x0f60:      /* punpcklbw */
7910         case 0x0f61:      /* punpcklwd */
7911         case 0x0f62:      /* punpckldq */
7912         case 0x0f63:      /* packsswb */
7913         case 0x0f64:      /* pcmpgtb */
7914         case 0x0f65:      /* pcmpgtw */
7915         case 0x0f66:      /* pcmpgtd */
7916         case 0x0f67:      /* packuswb */
7917         case 0x0f68:      /* punpckhbw */
7918         case 0x0f69:      /* punpckhwd */
7919         case 0x0f6a:      /* punpckhdq */
7920         case 0x0f6b:      /* packssdw */
7921         case 0x0f6e:      /* movd */
7922         case 0x0f6f:      /* movq */
7923         case 0x0f70:      /* pshufw */
7924         case 0x0f74:      /* pcmpeqb */
7925         case 0x0f75:      /* pcmpeqw */
7926         case 0x0f76:      /* pcmpeqd */
7927         case 0x0fc4:      /* pinsrw */
7928         case 0x0fd1:      /* psrlw */
7929         case 0x0fd2:      /* psrld */
7930         case 0x0fd3:      /* psrlq */
7931         case 0x0fd4:      /* paddq */
7932         case 0x0fd5:      /* pmullw */
7933         case 0xf20fd6:    /* movdq2q */
7934         case 0x0fd8:      /* psubusb */
7935         case 0x0fd9:      /* psubusw */
7936         case 0x0fda:      /* pminub */
7937         case 0x0fdb:      /* pand */
7938         case 0x0fdc:      /* paddusb */
7939         case 0x0fdd:      /* paddusw */
7940         case 0x0fde:      /* pmaxub */
7941         case 0x0fdf:      /* pandn */
7942         case 0x0fe0:      /* pavgb */
7943         case 0x0fe1:      /* psraw */
7944         case 0x0fe2:      /* psrad */
7945         case 0x0fe3:      /* pavgw */
7946         case 0x0fe4:      /* pmulhuw */
7947         case 0x0fe5:      /* pmulhw */
7948         case 0x0fe8:      /* psubsb */
7949         case 0x0fe9:      /* psubsw */
7950         case 0x0fea:      /* pminsw */
7951         case 0x0feb:      /* por */
7952         case 0x0fec:      /* paddsb */
7953         case 0x0fed:      /* paddsw */
7954         case 0x0fee:      /* pmaxsw */
7955         case 0x0fef:      /* pxor */
7956         case 0x0ff1:      /* psllw */
7957         case 0x0ff2:      /* pslld */
7958         case 0x0ff3:      /* psllq */
7959         case 0x0ff4:      /* pmuludq */
7960         case 0x0ff5:      /* pmaddwd */
7961         case 0x0ff6:      /* psadbw */
7962         case 0x0ff8:      /* psubb */
7963         case 0x0ff9:      /* psubw */
7964         case 0x0ffa:      /* psubd */
7965         case 0x0ffb:      /* psubq */
7966         case 0x0ffc:      /* paddb */
7967         case 0x0ffd:      /* paddw */
7968         case 0x0ffe:      /* paddd */
7969           if (i386_record_modrm (&ir))
7970             return -1;
7971           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.reg))
7972             goto no_support;
7973           record_full_arch_list_add_reg (ir.regcache,
7974                                          I387_MM0_REGNUM (tdep) + ir.reg);
7975           break;
7976
7977         case 0x0f71:    /* psllw */
7978         case 0x0f72:    /* pslld */
7979         case 0x0f73:    /* psllq */
7980           if (i386_record_modrm (&ir))
7981             return -1;
7982           if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
7983             goto no_support;
7984           record_full_arch_list_add_reg (ir.regcache,
7985                                          I387_MM0_REGNUM (tdep) + ir.rm);
7986           break;
7987
7988         case 0x660f71:    /* psllw */
7989         case 0x660f72:    /* pslld */
7990         case 0x660f73:    /* psllq */
7991           if (i386_record_modrm (&ir))
7992             return -1;
7993           ir.rm |= ir.rex_b;
7994           if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.rm))
7995             goto no_support;
7996           record_full_arch_list_add_reg (ir.regcache,
7997                                          I387_XMM0_REGNUM (tdep) + ir.rm);
7998           break;
7999
8000         case 0x0f7e:      /* movd */
8001         case 0x660f7e:    /* movd */
8002           if (i386_record_modrm (&ir))
8003             return -1;
8004           if (ir.mod == 3)
8005             I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
8006           else
8007             {
8008               if (ir.dflag == 2)
8009                 ir.ot = OT_QUAD;
8010               else
8011                 ir.ot = OT_LONG;
8012               if (i386_record_lea_modrm (&ir))
8013                 return -1;
8014             }
8015           break;
8016
8017         case 0x0f7f:    /* movq */
8018           if (i386_record_modrm (&ir))
8019             return -1;
8020           if (ir.mod == 3)
8021             {
8022               if (!i386_mmx_regnum_p (gdbarch, I387_MM0_REGNUM (tdep) + ir.rm))
8023                 goto no_support;
8024               record_full_arch_list_add_reg (ir.regcache,
8025                                              I387_MM0_REGNUM (tdep) + ir.rm);
8026             }
8027           else
8028             {
8029               ir.ot = OT_QUAD;
8030               if (i386_record_lea_modrm (&ir))
8031                 return -1;
8032             }
8033           break;
8034
8035         case 0xf30fb8:    /* popcnt */
8036           if (i386_record_modrm (&ir))
8037             return -1;
8038           I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
8039           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
8040           break;
8041
8042         case 0x660fd6:    /* movq */
8043           if (i386_record_modrm (&ir))
8044             return -1;
8045           if (ir.mod == 3)
8046             {
8047               ir.rm |= ir.rex_b;
8048               if (!i386_xmm_regnum_p (gdbarch,
8049                                       I387_XMM0_REGNUM (tdep) + ir.rm))
8050                 goto no_support;
8051               record_full_arch_list_add_reg (ir.regcache,
8052                                              I387_XMM0_REGNUM (tdep) + ir.rm);
8053             }
8054           else
8055             {
8056               ir.ot = OT_QUAD;
8057               if (i386_record_lea_modrm (&ir))
8058                 return -1;
8059             }
8060           break;
8061
8062         case 0x660f3817:    /* ptest */
8063         case 0x0f2e:        /* ucomiss */
8064         case 0x660f2e:      /* ucomisd */
8065         case 0x0f2f:        /* comiss */
8066         case 0x660f2f:      /* comisd */
8067           I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
8068           break;
8069
8070         case 0x0ff7:    /* maskmovq */
8071           regcache_raw_read_unsigned (ir.regcache,
8072                                       ir.regmap[X86_RECORD_REDI_REGNUM],
8073                                       &addr);
8074           if (record_full_arch_list_add_mem (addr, 64))
8075             return -1;
8076           break;
8077
8078         case 0x660ff7:    /* maskmovdqu */
8079           regcache_raw_read_unsigned (ir.regcache,
8080                                       ir.regmap[X86_RECORD_REDI_REGNUM],
8081                                       &addr);
8082           if (record_full_arch_list_add_mem (addr, 128))
8083             return -1;
8084           break;
8085
8086         default:
8087           goto no_support;
8088           break;
8089         }
8090       break;
8091
8092     default:
8093       goto no_support;
8094       break;
8095     }
8096
8097   /* In the future, maybe still need to deal with need_dasm.  */
8098   I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REIP_REGNUM);
8099   if (record_full_arch_list_add_end ())
8100     return -1;
8101
8102   return 0;
8103
8104  no_support:
8105   printf_unfiltered (_("Process record does not support instruction 0x%02x "
8106                        "at address %s.\n"),
8107                      (unsigned int) (opcode),
8108                      paddress (gdbarch, ir.orig_addr));
8109   return -1;
8110 }
8111
8112 static const int i386_record_regmap[] =
8113 {
8114   I386_EAX_REGNUM, I386_ECX_REGNUM, I386_EDX_REGNUM, I386_EBX_REGNUM,
8115   I386_ESP_REGNUM, I386_EBP_REGNUM, I386_ESI_REGNUM, I386_EDI_REGNUM,
8116   0, 0, 0, 0, 0, 0, 0, 0,
8117   I386_EIP_REGNUM, I386_EFLAGS_REGNUM, I386_CS_REGNUM, I386_SS_REGNUM,
8118   I386_DS_REGNUM, I386_ES_REGNUM, I386_FS_REGNUM, I386_GS_REGNUM
8119 };
8120
8121 /* Check that the given address appears suitable for a fast
8122    tracepoint, which on x86-64 means that we need an instruction of at
8123    least 5 bytes, so that we can overwrite it with a 4-byte-offset
8124    jump and not have to worry about program jumps to an address in the
8125    middle of the tracepoint jump.  On x86, it may be possible to use
8126    4-byte jumps with a 2-byte offset to a trampoline located in the
8127    bottom 64 KiB of memory.  Returns 1 if OK, and writes a size
8128    of instruction to replace, and 0 if not, plus an explanatory
8129    string.  */
8130
8131 static int
8132 i386_fast_tracepoint_valid_at (struct gdbarch *gdbarch, CORE_ADDR addr,
8133                                char **msg)
8134 {
8135   int len, jumplen;
8136
8137   /*  Ask the target for the minimum instruction length supported.  */
8138   jumplen = target_get_min_fast_tracepoint_insn_len ();
8139
8140   if (jumplen < 0)
8141     {
8142       /* If the target does not support the get_min_fast_tracepoint_insn_len
8143          operation, assume that fast tracepoints will always be implemented
8144          using 4-byte relative jumps on both x86 and x86-64.  */
8145       jumplen = 5;
8146     }
8147   else if (jumplen == 0)
8148     {
8149       /* If the target does support get_min_fast_tracepoint_insn_len but
8150          returns zero, then the IPA has not loaded yet.  In this case,
8151          we optimistically assume that truncated 2-byte relative jumps
8152          will be available on x86, and compensate later if this assumption
8153          turns out to be incorrect.  On x86-64 architectures, 4-byte relative
8154          jumps will always be used.  */
8155       jumplen = (register_size (gdbarch, 0) == 8) ? 5 : 4;
8156     }
8157
8158   /* Check for fit.  */
8159   len = gdb_insn_length (gdbarch, addr);
8160
8161   if (len < jumplen)
8162     {
8163       /* Return a bit of target-specific detail to add to the caller's
8164          generic failure message.  */
8165       if (msg)
8166         *msg = xstrprintf (_("; instruction is only %d bytes long, "
8167                              "need at least %d bytes for the jump"),
8168                            len, jumplen);
8169       return 0;
8170     }
8171   else
8172     {
8173       if (msg)
8174         *msg = NULL;
8175       return 1;
8176     }
8177 }
8178
8179 /* Return a floating-point format for a floating-point variable of
8180    length LEN in bits.  If non-NULL, NAME is the name of its type.
8181    If no suitable type is found, return NULL.  */
8182
8183 const struct floatformat **
8184 i386_floatformat_for_type (struct gdbarch *gdbarch,
8185                            const char *name, int len)
8186 {
8187   if (len == 128 && name)
8188     if (strcmp (name, "__float128") == 0
8189         || strcmp (name, "_Float128") == 0
8190         || strcmp (name, "complex _Float128") == 0)
8191       return floatformats_ia64_quad;
8192
8193   return default_floatformat_for_type (gdbarch, name, len);
8194 }
8195
8196 static int
8197 i386_validate_tdesc_p (struct gdbarch_tdep *tdep,
8198                        struct tdesc_arch_data *tdesc_data)
8199 {
8200   const struct target_desc *tdesc = tdep->tdesc;
8201   const struct tdesc_feature *feature_core;
8202
8203   const struct tdesc_feature *feature_sse, *feature_avx, *feature_mpx,
8204                              *feature_avx512;
8205   int i, num_regs, valid_p;
8206
8207   if (! tdesc_has_registers (tdesc))
8208     return 0;
8209
8210   /* Get core registers.  */
8211   feature_core = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.core");
8212   if (feature_core == NULL)
8213     return 0;
8214
8215   /* Get SSE registers.  */
8216   feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
8217
8218   /* Try AVX registers.  */
8219   feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
8220
8221   /* Try MPX registers.  */
8222   feature_mpx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx");
8223
8224   /* Try AVX512 registers.  */
8225   feature_avx512 = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512");
8226
8227   valid_p = 1;
8228
8229   /* The XCR0 bits.  */
8230   if (feature_avx512)
8231     {
8232       /* AVX512 register description requires AVX register description.  */
8233       if (!feature_avx)
8234         return 0;
8235
8236       tdep->xcr0 = X86_XSTATE_AVX_AVX512_MASK;
8237
8238       /* It may have been set by OSABI initialization function.  */
8239       if (tdep->k0_regnum < 0)
8240         {
8241           tdep->k_register_names = i386_k_names;
8242           tdep->k0_regnum = I386_K0_REGNUM;
8243         }
8244
8245       for (i = 0; i < I387_NUM_K_REGS; i++)
8246         valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8247                                             tdep->k0_regnum + i,
8248                                             i386_k_names[i]);
8249
8250       if (tdep->num_zmm_regs == 0)
8251         {
8252           tdep->zmmh_register_names = i386_zmmh_names;
8253           tdep->num_zmm_regs = 8;
8254           tdep->zmm0h_regnum = I386_ZMM0H_REGNUM;
8255         }
8256
8257       for (i = 0; i < tdep->num_zmm_regs; i++)
8258         valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8259                                             tdep->zmm0h_regnum + i,
8260                                             tdep->zmmh_register_names[i]);
8261
8262       for (i = 0; i < tdep->num_xmm_avx512_regs; i++)
8263         valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8264                                             tdep->xmm16_regnum + i,
8265                                             tdep->xmm_avx512_register_names[i]);
8266
8267       for (i = 0; i < tdep->num_ymm_avx512_regs; i++)
8268         valid_p &= tdesc_numbered_register (feature_avx512, tdesc_data,
8269                                             tdep->ymm16h_regnum + i,
8270                                             tdep->ymm16h_register_names[i]);
8271     }
8272   if (feature_avx)
8273     {
8274       /* AVX register description requires SSE register description.  */
8275       if (!feature_sse)
8276         return 0;
8277
8278       if (!feature_avx512)
8279         tdep->xcr0 = X86_XSTATE_AVX_MASK;
8280
8281       /* It may have been set by OSABI initialization function.  */
8282       if (tdep->num_ymm_regs == 0)
8283         {
8284           tdep->ymmh_register_names = i386_ymmh_names;
8285           tdep->num_ymm_regs = 8;
8286           tdep->ymm0h_regnum = I386_YMM0H_REGNUM;
8287         }
8288
8289       for (i = 0; i < tdep->num_ymm_regs; i++)
8290         valid_p &= tdesc_numbered_register (feature_avx, tdesc_data,
8291                                             tdep->ymm0h_regnum + i,
8292                                             tdep->ymmh_register_names[i]);
8293     }
8294   else if (feature_sse)
8295     tdep->xcr0 = X86_XSTATE_SSE_MASK;
8296   else
8297     {
8298       tdep->xcr0 = X86_XSTATE_X87_MASK;
8299       tdep->num_xmm_regs = 0;
8300     }
8301
8302   num_regs = tdep->num_core_regs;
8303   for (i = 0; i < num_regs; i++)
8304     valid_p &= tdesc_numbered_register (feature_core, tdesc_data, i,
8305                                         tdep->register_names[i]);
8306
8307   if (feature_sse)
8308     {
8309       /* Need to include %mxcsr, so add one.  */
8310       num_regs += tdep->num_xmm_regs + 1;
8311       for (; i < num_regs; i++)
8312         valid_p &= tdesc_numbered_register (feature_sse, tdesc_data, i,
8313                                             tdep->register_names[i]);
8314     }
8315
8316   if (feature_mpx)
8317     {
8318       tdep->xcr0 |= X86_XSTATE_MPX_MASK;
8319
8320       if (tdep->bnd0r_regnum < 0)
8321         {
8322           tdep->mpx_register_names = i386_mpx_names;
8323           tdep->bnd0r_regnum = I386_BND0R_REGNUM;
8324           tdep->bndcfgu_regnum = I386_BNDCFGU_REGNUM;
8325         }
8326
8327       for (i = 0; i < I387_NUM_MPX_REGS; i++)
8328         valid_p &= tdesc_numbered_register (feature_mpx, tdesc_data,
8329             I387_BND0R_REGNUM (tdep) + i,
8330             tdep->mpx_register_names[i]);
8331     }
8332
8333   return valid_p;
8334 }
8335
8336 \f
8337 /* Note: This is called for both i386 and amd64.  */
8338
8339 static struct gdbarch *
8340 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8341 {
8342   struct gdbarch_tdep *tdep;
8343   struct gdbarch *gdbarch;
8344   struct tdesc_arch_data *tdesc_data;
8345   const struct target_desc *tdesc;
8346   int mm0_regnum;
8347   int ymm0_regnum;
8348   int bnd0_regnum;
8349   int num_bnd_cooked;
8350
8351   /* If there is already a candidate, use it.  */
8352   arches = gdbarch_list_lookup_by_info (arches, &info);
8353   if (arches != NULL)
8354     return arches->gdbarch;
8355
8356   /* Allocate space for the new architecture.  Assume i386 for now.  */
8357   tdep = XCNEW (struct gdbarch_tdep);
8358   gdbarch = gdbarch_alloc (&info, tdep);
8359
8360   /* General-purpose registers.  */
8361   tdep->gregset_reg_offset = NULL;
8362   tdep->gregset_num_regs = I386_NUM_GREGS;
8363   tdep->sizeof_gregset = 0;
8364
8365   /* Floating-point registers.  */
8366   tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
8367   tdep->fpregset = &i386_fpregset;
8368
8369   /* The default settings include the FPU registers, the MMX registers
8370      and the SSE registers.  This can be overridden for a specific ABI
8371      by adjusting the members `st0_regnum', `mm0_regnum' and
8372      `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
8373      will show up in the output of "info all-registers".  */
8374
8375   tdep->st0_regnum = I386_ST0_REGNUM;
8376
8377   /* I386_NUM_XREGS includes %mxcsr, so substract one.  */
8378   tdep->num_xmm_regs = I386_NUM_XREGS - 1;
8379
8380   tdep->jb_pc_offset = -1;
8381   tdep->struct_return = pcc_struct_return;
8382   tdep->sigtramp_start = 0;
8383   tdep->sigtramp_end = 0;
8384   tdep->sigtramp_p = i386_sigtramp_p;
8385   tdep->sigcontext_addr = NULL;
8386   tdep->sc_reg_offset = NULL;
8387   tdep->sc_pc_offset = -1;
8388   tdep->sc_sp_offset = -1;
8389
8390   tdep->xsave_xcr0_offset = -1;
8391
8392   tdep->record_regmap = i386_record_regmap;
8393
8394   set_gdbarch_long_long_align_bit (gdbarch, 32);
8395
8396   /* The format used for `long double' on almost all i386 targets is
8397      the i387 extended floating-point format.  In fact, of all targets
8398      in the GCC 2.95 tree, only OSF/1 does it different, and insists
8399      on having a `long double' that's not `long' at all.  */
8400   set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
8401
8402   /* Although the i387 extended floating-point has only 80 significant
8403      bits, a `long double' actually takes up 96, probably to enforce
8404      alignment.  */
8405   set_gdbarch_long_double_bit (gdbarch, 96);
8406
8407   /* Support for floating-point data type variants.  */
8408   set_gdbarch_floatformat_for_type (gdbarch, i386_floatformat_for_type);
8409
8410   /* Register numbers of various important registers.  */
8411   set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
8412   set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
8413   set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
8414   set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
8415
8416   /* NOTE: kettenis/20040418: GCC does have two possible register
8417      numbering schemes on the i386: dbx and SVR4.  These schemes
8418      differ in how they number %ebp, %esp, %eflags, and the
8419      floating-point registers, and are implemented by the arrays
8420      dbx_register_map[] and svr4_dbx_register_map in
8421      gcc/config/i386.c.  GCC also defines a third numbering scheme in
8422      gcc/config/i386.c, which it designates as the "default" register
8423      map used in 64bit mode.  This last register numbering scheme is
8424      implemented in dbx64_register_map, and is used for AMD64; see
8425      amd64-tdep.c.
8426
8427      Currently, each GCC i386 target always uses the same register
8428      numbering scheme across all its supported debugging formats
8429      i.e. SDB (COFF), stabs and DWARF 2.  This is because
8430      gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
8431      DBX_REGISTER_NUMBER macro which is defined by each target's
8432      respective config header in a manner independent of the requested
8433      output debugging format.
8434
8435      This does not match the arrangement below, which presumes that
8436      the SDB and stabs numbering schemes differ from the DWARF and
8437      DWARF 2 ones.  The reason for this arrangement is that it is
8438      likely to get the numbering scheme for the target's
8439      default/native debug format right.  For targets where GCC is the
8440      native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
8441      targets where the native toolchain uses a different numbering
8442      scheme for a particular debug format (stabs-in-ELF on Solaris)
8443      the defaults below will have to be overridden, like
8444      i386_elf_init_abi() does.  */
8445
8446   /* Use the dbx register numbering scheme for stabs and COFF.  */
8447   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
8448   set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
8449
8450   /* Use the SVR4 register numbering scheme for DWARF 2.  */
8451   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_dwarf_reg_to_regnum);
8452
8453   /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
8454      be in use on any of the supported i386 targets.  */
8455
8456   set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
8457
8458   set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
8459
8460   /* Call dummy code.  */
8461   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
8462   set_gdbarch_push_dummy_code (gdbarch, i386_push_dummy_code);
8463   set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
8464   set_gdbarch_frame_align (gdbarch, i386_frame_align);
8465
8466   set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
8467   set_gdbarch_register_to_value (gdbarch,  i386_register_to_value);
8468   set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
8469
8470   set_gdbarch_return_value (gdbarch, i386_return_value);
8471
8472   set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
8473
8474   /* Stack grows downward.  */
8475   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
8476
8477   set_gdbarch_breakpoint_kind_from_pc (gdbarch, i386_breakpoint::kind_from_pc);
8478   set_gdbarch_sw_breakpoint_from_kind (gdbarch, i386_breakpoint::bp_from_kind);
8479
8480   set_gdbarch_decr_pc_after_break (gdbarch, 1);
8481   set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
8482
8483   set_gdbarch_frame_args_skip (gdbarch, 8);
8484
8485   set_gdbarch_print_insn (gdbarch, i386_print_insn);
8486
8487   set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
8488
8489   set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
8490
8491   /* Add the i386 register groups.  */
8492   i386_add_reggroups (gdbarch);
8493   tdep->register_reggroup_p = i386_register_reggroup_p;
8494
8495   /* Helper for function argument information.  */
8496   set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
8497
8498   /* Hook the function epilogue frame unwinder.  This unwinder is
8499      appended to the list first, so that it supercedes the DWARF
8500      unwinder in function epilogues (where the DWARF unwinder
8501      currently fails).  */
8502   frame_unwind_append_unwinder (gdbarch, &i386_epilogue_frame_unwind);
8503
8504   /* Hook in the DWARF CFI frame unwinder.  This unwinder is appended
8505      to the list before the prologue-based unwinders, so that DWARF
8506      CFI info will be used if it is available.  */
8507   dwarf2_append_unwinders (gdbarch);
8508
8509   frame_base_set_default (gdbarch, &i386_frame_base);
8510
8511   /* Pseudo registers may be changed by amd64_init_abi.  */
8512   set_gdbarch_pseudo_register_read_value (gdbarch,
8513                                           i386_pseudo_register_read_value);
8514   set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
8515   set_gdbarch_ax_pseudo_register_collect (gdbarch,
8516                                           i386_ax_pseudo_register_collect);
8517
8518   set_tdesc_pseudo_register_type (gdbarch, i386_pseudo_register_type);
8519   set_tdesc_pseudo_register_name (gdbarch, i386_pseudo_register_name);
8520
8521   /* Override the normal target description method to make the AVX
8522      upper halves anonymous.  */
8523   set_gdbarch_register_name (gdbarch, i386_register_name);
8524
8525   /* Even though the default ABI only includes general-purpose registers,
8526      floating-point registers and the SSE registers, we have to leave a
8527      gap for the upper AVX, MPX and AVX512 registers.  */
8528   set_gdbarch_num_regs (gdbarch, I386_AVX512_NUM_REGS);
8529
8530   set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
8531
8532   /* Get the x86 target description from INFO.  */
8533   tdesc = info.target_desc;
8534   if (! tdesc_has_registers (tdesc))
8535     tdesc = tdesc_i386;
8536   tdep->tdesc = tdesc;
8537
8538   tdep->num_core_regs = I386_NUM_GREGS + I387_NUM_REGS;
8539   tdep->register_names = i386_register_names;
8540
8541   /* No upper YMM registers.  */
8542   tdep->ymmh_register_names = NULL;
8543   tdep->ymm0h_regnum = -1;
8544
8545   /* No upper ZMM registers.  */
8546   tdep->zmmh_register_names = NULL;
8547   tdep->zmm0h_regnum = -1;
8548
8549   /* No high XMM registers.  */
8550   tdep->xmm_avx512_register_names = NULL;
8551   tdep->xmm16_regnum = -1;
8552
8553   /* No upper YMM16-31 registers.  */
8554   tdep->ymm16h_register_names = NULL;
8555   tdep->ymm16h_regnum = -1;
8556
8557   tdep->num_byte_regs = 8;
8558   tdep->num_word_regs = 8;
8559   tdep->num_dword_regs = 0;
8560   tdep->num_mmx_regs = 8;
8561   tdep->num_ymm_regs = 0;
8562
8563   /* No MPX registers.  */
8564   tdep->bnd0r_regnum = -1;
8565   tdep->bndcfgu_regnum = -1;
8566
8567   /* No AVX512 registers.  */
8568   tdep->k0_regnum = -1;
8569   tdep->num_zmm_regs = 0;
8570   tdep->num_ymm_avx512_regs = 0;
8571   tdep->num_xmm_avx512_regs = 0;
8572
8573   tdesc_data = tdesc_data_alloc ();
8574
8575   set_gdbarch_relocate_instruction (gdbarch, i386_relocate_instruction);
8576
8577   set_gdbarch_gen_return_address (gdbarch, i386_gen_return_address);
8578
8579   set_gdbarch_insn_is_call (gdbarch, i386_insn_is_call);
8580   set_gdbarch_insn_is_ret (gdbarch, i386_insn_is_ret);
8581   set_gdbarch_insn_is_jump (gdbarch, i386_insn_is_jump);
8582
8583   /* Hook in ABI-specific overrides, if they have been registered.
8584      Note: If INFO specifies a 64 bit arch, this is where we turn
8585      a 32-bit i386 into a 64-bit amd64.  */
8586   info.tdep_info = tdesc_data;
8587   gdbarch_init_osabi (info, gdbarch);
8588
8589   if (!i386_validate_tdesc_p (tdep, tdesc_data))
8590     {
8591       tdesc_data_cleanup (tdesc_data);
8592       xfree (tdep);
8593       gdbarch_free (gdbarch);
8594       return NULL;
8595     }
8596
8597   num_bnd_cooked = (tdep->bnd0r_regnum > 0 ? I387_NUM_BND_REGS : 0);
8598
8599   /* Wire in pseudo registers.  Number of pseudo registers may be
8600      changed.  */
8601   set_gdbarch_num_pseudo_regs (gdbarch, (tdep->num_byte_regs
8602                                          + tdep->num_word_regs
8603                                          + tdep->num_dword_regs
8604                                          + tdep->num_mmx_regs
8605                                          + tdep->num_ymm_regs
8606                                          + num_bnd_cooked
8607                                          + tdep->num_ymm_avx512_regs
8608                                          + tdep->num_zmm_regs));
8609
8610   /* Target description may be changed.  */
8611   tdesc = tdep->tdesc;
8612
8613   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
8614
8615   /* Override gdbarch_register_reggroup_p set in tdesc_use_registers.  */
8616   set_gdbarch_register_reggroup_p (gdbarch, tdep->register_reggroup_p);
8617
8618   /* Make %al the first pseudo-register.  */
8619   tdep->al_regnum = gdbarch_num_regs (gdbarch);
8620   tdep->ax_regnum = tdep->al_regnum + tdep->num_byte_regs;
8621
8622   ymm0_regnum = tdep->ax_regnum + tdep->num_word_regs;
8623   if (tdep->num_dword_regs)
8624     {
8625       /* Support dword pseudo-register if it hasn't been disabled.  */
8626       tdep->eax_regnum = ymm0_regnum;
8627       ymm0_regnum += tdep->num_dword_regs;
8628     }
8629   else
8630     tdep->eax_regnum = -1;
8631
8632   mm0_regnum = ymm0_regnum;
8633   if (tdep->num_ymm_regs)
8634     {
8635       /* Support YMM pseudo-register if it is available.  */
8636       tdep->ymm0_regnum = ymm0_regnum;
8637       mm0_regnum += tdep->num_ymm_regs;
8638     }
8639   else
8640     tdep->ymm0_regnum = -1;
8641
8642   if (tdep->num_ymm_avx512_regs)
8643     {
8644       /* Support YMM16-31 pseudo registers if available.  */
8645       tdep->ymm16_regnum = mm0_regnum;
8646       mm0_regnum += tdep->num_ymm_avx512_regs;
8647     }
8648   else
8649     tdep->ymm16_regnum = -1;
8650
8651   if (tdep->num_zmm_regs)
8652     {
8653       /* Support ZMM pseudo-register if it is available.  */
8654       tdep->zmm0_regnum = mm0_regnum;
8655       mm0_regnum += tdep->num_zmm_regs;
8656     }
8657   else
8658     tdep->zmm0_regnum = -1;
8659
8660   bnd0_regnum = mm0_regnum;
8661   if (tdep->num_mmx_regs != 0)
8662     {
8663       /* Support MMX pseudo-register if MMX hasn't been disabled.  */
8664       tdep->mm0_regnum = mm0_regnum;
8665       bnd0_regnum += tdep->num_mmx_regs;
8666     }
8667   else
8668     tdep->mm0_regnum = -1;
8669
8670   if (tdep->bnd0r_regnum > 0)
8671       tdep->bnd0_regnum = bnd0_regnum;
8672   else
8673     tdep-> bnd0_regnum = -1;
8674
8675   /* Hook in the legacy prologue-based unwinders last (fallback).  */
8676   frame_unwind_append_unwinder (gdbarch, &i386_stack_tramp_frame_unwind);
8677   frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
8678   frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
8679
8680   /* If we have a register mapping, enable the generic core file
8681      support, unless it has already been enabled.  */
8682   if (tdep->gregset_reg_offset
8683       && !gdbarch_iterate_over_regset_sections_p (gdbarch))
8684     set_gdbarch_iterate_over_regset_sections
8685       (gdbarch, i386_iterate_over_regset_sections);
8686
8687   set_gdbarch_fast_tracepoint_valid_at (gdbarch,
8688                                         i386_fast_tracepoint_valid_at);
8689
8690   return gdbarch;
8691 }
8692
8693 static enum gdb_osabi
8694 i386_coff_osabi_sniffer (bfd *abfd)
8695 {
8696   if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
8697       || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
8698     return GDB_OSABI_GO32;
8699
8700   return GDB_OSABI_UNKNOWN;
8701 }
8702 \f
8703
8704 /* Return the target description for a specified XSAVE feature mask.  */
8705
8706 const struct target_desc *
8707 i386_target_description (uint64_t xcr0)
8708 {
8709   switch (xcr0 & X86_XSTATE_ALL_MASK)
8710     {
8711     case X86_XSTATE_AVX_MPX_AVX512_MASK:
8712       return tdesc_i386_avx_mpx_avx512;
8713     case X86_XSTATE_AVX_AVX512_MASK:
8714       return tdesc_i386_avx_avx512;
8715     case X86_XSTATE_AVX_MPX_MASK:
8716       return tdesc_i386_avx_mpx;
8717     case X86_XSTATE_MPX_MASK:
8718       return tdesc_i386_mpx;
8719     case X86_XSTATE_AVX_MASK:
8720       return tdesc_i386_avx;
8721     default:
8722       return tdesc_i386;
8723     }
8724 }
8725
8726 #define MPX_BASE_MASK (~(ULONGEST) 0xfff)
8727
8728 /* Find the bound directory base address.  */
8729
8730 static unsigned long
8731 i386_mpx_bd_base (void)
8732 {
8733   struct regcache *rcache;
8734   struct gdbarch_tdep *tdep;
8735   ULONGEST ret;
8736   enum register_status regstatus;
8737
8738   rcache = get_current_regcache ();
8739   tdep = gdbarch_tdep (get_regcache_arch (rcache));
8740
8741   regstatus = regcache_raw_read_unsigned (rcache, tdep->bndcfgu_regnum, &ret);
8742
8743   if (regstatus != REG_VALID)
8744     error (_("BNDCFGU register invalid, read status %d."), regstatus);
8745
8746   return ret & MPX_BASE_MASK;
8747 }
8748
8749 int
8750 i386_mpx_enabled (void)
8751 {
8752   const struct gdbarch_tdep *tdep = gdbarch_tdep (get_current_arch ());
8753   const struct target_desc *tdesc = tdep->tdesc;
8754
8755   return (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL);
8756 }
8757
8758 #define MPX_BD_MASK     0xfffffff00000ULL       /* select bits [47:20]  */
8759 #define MPX_BT_MASK     0x0000000ffff8          /* select bits [19:3]   */
8760 #define MPX_BD_MASK_32  0xfffff000              /* select bits [31:12]  */
8761 #define MPX_BT_MASK_32  0x00000ffc              /* select bits [11:2]   */
8762
8763 /* Find the bound table entry given the pointer location and the base
8764    address of the table.  */
8765
8766 static CORE_ADDR
8767 i386_mpx_get_bt_entry (CORE_ADDR ptr, CORE_ADDR bd_base)
8768 {
8769   CORE_ADDR offset1;
8770   CORE_ADDR offset2;
8771   CORE_ADDR mpx_bd_mask, bd_ptr_r_shift, bd_ptr_l_shift;
8772   CORE_ADDR bt_mask, bt_select_r_shift, bt_select_l_shift;
8773   CORE_ADDR bd_entry_addr;
8774   CORE_ADDR bt_addr;
8775   CORE_ADDR bd_entry;
8776   struct gdbarch *gdbarch = get_current_arch ();
8777   struct type *data_ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
8778
8779
8780   if (gdbarch_ptr_bit (gdbarch) == 64)
8781     {
8782       mpx_bd_mask = (CORE_ADDR) MPX_BD_MASK;
8783       bd_ptr_r_shift = 20;
8784       bd_ptr_l_shift = 3;
8785       bt_select_r_shift = 3;
8786       bt_select_l_shift = 5;
8787       bt_mask = (CORE_ADDR) MPX_BT_MASK;
8788
8789       if ( sizeof (CORE_ADDR) == 4)
8790         error (_("bound table examination not supported\
8791  for 64-bit process with 32-bit GDB"));
8792     }
8793   else
8794     {
8795       mpx_bd_mask = MPX_BD_MASK_32;
8796       bd_ptr_r_shift = 12;
8797       bd_ptr_l_shift = 2;
8798       bt_select_r_shift = 2;
8799       bt_select_l_shift = 4;
8800       bt_mask = MPX_BT_MASK_32;
8801     }
8802
8803   offset1 = ((ptr & mpx_bd_mask) >> bd_ptr_r_shift) << bd_ptr_l_shift;
8804   bd_entry_addr = bd_base + offset1;
8805   bd_entry = read_memory_typed_address (bd_entry_addr, data_ptr_type);
8806
8807   if ((bd_entry & 0x1) == 0)
8808     error (_("Invalid bounds directory entry at %s."),
8809            paddress (get_current_arch (), bd_entry_addr));
8810
8811   /* Clearing status bit.  */
8812   bd_entry--;
8813   bt_addr = bd_entry & ~bt_select_r_shift;
8814   offset2 = ((ptr & bt_mask) >> bt_select_r_shift) << bt_select_l_shift;
8815
8816   return bt_addr + offset2;
8817 }
8818
8819 /* Print routine for the mpx bounds.  */
8820
8821 static void
8822 i386_mpx_print_bounds (const CORE_ADDR bt_entry[4])
8823 {
8824   struct ui_out *uiout = current_uiout;
8825   LONGEST size;
8826   struct gdbarch *gdbarch = get_current_arch ();
8827   CORE_ADDR onecompl = ~((CORE_ADDR) 0);
8828   int bounds_in_map = ((~bt_entry[1] == 0 && bt_entry[0] == onecompl) ? 1 : 0);
8829
8830   if (bounds_in_map == 1)
8831     {
8832       uiout->text ("Null bounds on map:");
8833       uiout->text (" pointer value = ");
8834       uiout->field_core_addr ("pointer-value", gdbarch, bt_entry[2]);
8835       uiout->text (".");
8836       uiout->text ("\n");
8837     }
8838   else
8839     {
8840       uiout->text ("{lbound = ");
8841       uiout->field_core_addr ("lower-bound", gdbarch, bt_entry[0]);
8842       uiout->text (", ubound = ");
8843
8844       /* The upper bound is stored in 1's complement.  */
8845       uiout->field_core_addr ("upper-bound", gdbarch, ~bt_entry[1]);
8846       uiout->text ("}: pointer value = ");
8847       uiout->field_core_addr ("pointer-value", gdbarch, bt_entry[2]);
8848
8849       if (gdbarch_ptr_bit (gdbarch) == 64)
8850         size = ( (~(int64_t) bt_entry[1]) - (int64_t) bt_entry[0]);
8851       else
8852         size = ( ~((int32_t) bt_entry[1]) - (int32_t) bt_entry[0]);
8853
8854       /* In case the bounds are 0x0 and 0xffff... the difference will be -1.
8855          -1 represents in this sense full memory access, and there is no need
8856          one to the size.  */
8857
8858       size = (size > -1 ? size + 1 : size);
8859       uiout->text (", size = ");
8860       uiout->field_fmt ("size", "%s", plongest (size));
8861
8862       uiout->text (", metadata = ");
8863       uiout->field_core_addr ("metadata", gdbarch, bt_entry[3]);
8864       uiout->text ("\n");
8865     }
8866 }
8867
8868 /* Implement the command "show mpx bound".  */
8869
8870 static void
8871 i386_mpx_info_bounds (char *args, int from_tty)
8872 {
8873   CORE_ADDR bd_base = 0;
8874   CORE_ADDR addr;
8875   CORE_ADDR bt_entry_addr = 0;
8876   CORE_ADDR bt_entry[4];
8877   int i;
8878   struct gdbarch *gdbarch = get_current_arch ();
8879   struct type *data_ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
8880
8881   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_i386
8882       || !i386_mpx_enabled ())
8883     {
8884       printf_unfiltered (_("Intel Memory Protection Extensions not "
8885                            "supported on this target.\n"));
8886       return;
8887     }
8888
8889   if (args == NULL)
8890     {
8891       printf_unfiltered (_("Address of pointer variable expected.\n"));
8892       return;
8893     }
8894
8895   addr = parse_and_eval_address (args);
8896
8897   bd_base = i386_mpx_bd_base ();
8898   bt_entry_addr = i386_mpx_get_bt_entry (addr, bd_base);
8899
8900   memset (bt_entry, 0, sizeof (bt_entry));
8901
8902   for (i = 0; i < 4; i++)
8903     bt_entry[i] = read_memory_typed_address (bt_entry_addr
8904                                              + i * TYPE_LENGTH (data_ptr_type),
8905                                              data_ptr_type);
8906
8907   i386_mpx_print_bounds (bt_entry);
8908 }
8909
8910 /* Implement the command "set mpx bound".  */
8911
8912 static void
8913 i386_mpx_set_bounds (char *args, int from_tty)
8914 {
8915   CORE_ADDR bd_base = 0;
8916   CORE_ADDR addr, lower, upper;
8917   CORE_ADDR bt_entry_addr = 0;
8918   CORE_ADDR bt_entry[2];
8919   const char *input = args;
8920   int i;
8921   struct gdbarch *gdbarch = get_current_arch ();
8922   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
8923   struct type *data_ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
8924
8925   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_i386
8926       || !i386_mpx_enabled ())
8927     error (_("Intel Memory Protection Extensions not supported\
8928  on this target."));
8929
8930   if (args == NULL)
8931     error (_("Pointer value expected."));
8932
8933   addr = value_as_address (parse_to_comma_and_eval (&input));
8934
8935   if (input[0] == ',')
8936     ++input;
8937   if (input[0] == '\0')
8938     error (_("wrong number of arguments: missing lower and upper bound."));
8939   lower = value_as_address (parse_to_comma_and_eval (&input));
8940
8941   if (input[0] == ',')
8942     ++input;
8943   if (input[0] == '\0')
8944     error (_("Wrong number of arguments; Missing upper bound."));
8945   upper = value_as_address (parse_to_comma_and_eval (&input));
8946
8947   bd_base = i386_mpx_bd_base ();
8948   bt_entry_addr = i386_mpx_get_bt_entry (addr, bd_base);
8949   for (i = 0; i < 2; i++)
8950     bt_entry[i] = read_memory_typed_address (bt_entry_addr
8951                                              + i * TYPE_LENGTH (data_ptr_type),
8952                                              data_ptr_type);
8953   bt_entry[0] = (uint64_t) lower;
8954   bt_entry[1] = ~(uint64_t) upper;
8955
8956   for (i = 0; i < 2; i++)
8957     write_memory_unsigned_integer (bt_entry_addr
8958                                    + i * TYPE_LENGTH (data_ptr_type),
8959                                    TYPE_LENGTH (data_ptr_type), byte_order,
8960                                    bt_entry[i]);
8961 }
8962
8963 static struct cmd_list_element *mpx_set_cmdlist, *mpx_show_cmdlist;
8964
8965 /* Helper function for the CLI commands.  */
8966
8967 static void
8968 set_mpx_cmd (char *args, int from_tty)
8969 {
8970   help_list (mpx_set_cmdlist, "set mpx ", all_commands, gdb_stdout);
8971 }
8972
8973 /* Helper function for the CLI commands.  */
8974
8975 static void
8976 show_mpx_cmd (char *args, int from_tty)
8977 {
8978   cmd_show_list (mpx_show_cmdlist, from_tty, "");
8979 }
8980
8981 /* Provide a prototype to silence -Wmissing-prototypes.  */
8982 void _initialize_i386_tdep (void);
8983
8984 void
8985 _initialize_i386_tdep (void)
8986 {
8987   register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
8988
8989   /* Add the variable that controls the disassembly flavor.  */
8990   add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
8991                         &disassembly_flavor, _("\
8992 Set the disassembly flavor."), _("\
8993 Show the disassembly flavor."), _("\
8994 The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
8995                         NULL,
8996                         NULL, /* FIXME: i18n: */
8997                         &setlist, &showlist);
8998
8999   /* Add the variable that controls the convention for returning
9000      structs.  */
9001   add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
9002                         &struct_convention, _("\
9003 Set the convention for returning small structs."), _("\
9004 Show the convention for returning small structs."), _("\
9005 Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
9006 is \"default\"."),
9007                         NULL,
9008                         NULL, /* FIXME: i18n: */
9009                         &setlist, &showlist);
9010
9011   /* Add "mpx" prefix for the set commands.  */
9012
9013   add_prefix_cmd ("mpx", class_support, set_mpx_cmd, _("\
9014 Set Intel Memory Protection Extensions specific variables."),
9015                   &mpx_set_cmdlist, "set mpx ",
9016                   0 /* allow-unknown */, &setlist);
9017
9018   /* Add "mpx" prefix for the show commands.  */
9019
9020   add_prefix_cmd ("mpx", class_support, show_mpx_cmd, _("\
9021 Show Intel Memory Protection Extensions specific variables."),
9022                   &mpx_show_cmdlist, "show mpx ",
9023                   0 /* allow-unknown */, &showlist);
9024
9025   /* Add "bound" command for the show mpx commands list.  */
9026
9027   add_cmd ("bound", no_class, i386_mpx_info_bounds,
9028            "Show the memory bounds for a given array/pointer storage\
9029  in the bound table.",
9030            &mpx_show_cmdlist);
9031
9032   /* Add "bound" command for the set mpx commands list.  */
9033
9034   add_cmd ("bound", no_class, i386_mpx_set_bounds,
9035            "Set the memory bounds for a given array/pointer storage\
9036  in the bound table.",
9037            &mpx_set_cmdlist);
9038
9039   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
9040                                   i386_coff_osabi_sniffer);
9041
9042   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
9043                           i386_svr4_init_abi);
9044   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
9045                           i386_go32_init_abi);
9046
9047   /* Initialize the i386-specific register groups.  */
9048   i386_init_reggroups ();
9049
9050   /* Initialize the standard target descriptions.  */
9051   initialize_tdesc_i386 ();
9052   initialize_tdesc_i386_mmx ();
9053   initialize_tdesc_i386_avx ();
9054   initialize_tdesc_i386_mpx ();
9055   initialize_tdesc_i386_avx_mpx ();
9056   initialize_tdesc_i386_avx_avx512 ();
9057   initialize_tdesc_i386_avx_mpx_avx512 ();
9058
9059   /* Tell remote stub that we support XML target description.  */
9060   register_remote_support_xml ("i386");
9061 }