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