Fix outputdots so that it doesn't errantly discard the first line of, e.g.,
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
c906108c 1/* Intel 386 target-dependent stuff.
349c5d5f 2
ecd75fc8 3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
1903f0e6 21#include "opcode/i386.h"
acd5c798
MK
22#include "arch-utils.h"
23#include "command.h"
24#include "dummy-frame.h"
6405b0a6 25#include "dwarf2-frame.h"
acd5c798 26#include "doublest.h"
c906108c 27#include "frame.h"
acd5c798
MK
28#include "frame-base.h"
29#include "frame-unwind.h"
c906108c 30#include "inferior.h"
45741a9c 31#include "infrun.h"
acd5c798 32#include "gdbcmd.h"
c906108c 33#include "gdbcore.h"
e6bb342a 34#include "gdbtypes.h"
dfe01d39 35#include "objfiles.h"
acd5c798
MK
36#include "osabi.h"
37#include "regcache.h"
38#include "reggroups.h"
473f17b0 39#include "regset.h"
c0d1d883 40#include "symfile.h"
c906108c 41#include "symtab.h"
acd5c798 42#include "target.h"
fd0407d6 43#include "value.h"
a89aa300 44#include "dis-asm.h"
7a697b8d 45#include "disasm.h"
c8d5aac9 46#include "remote.h"
8fbca658 47#include "exceptions.h"
3d261580 48#include "gdb_assert.h"
0e9f083f 49#include <string.h>
3d261580 50
d2a7c97a 51#include "i386-tdep.h"
61113f8b 52#include "i387-tdep.h"
c131fcee 53#include "i386-xstate.h"
d2a7c97a 54
7ad10968 55#include "record.h"
d02ed0bb 56#include "record-full.h"
7ad10968
HZ
57#include <stdint.h>
58
90884b2b 59#include "features/i386/i386.c"
c131fcee 60#include "features/i386/i386-avx.c"
1dbcd68c 61#include "features/i386/i386-mpx.c"
01f9f808 62#include "features/i386/i386-avx512.c"
3a13a53b 63#include "features/i386/i386-mmx.c"
90884b2b 64
6710bf39
SS
65#include "ax.h"
66#include "ax-gdb.h"
67
55aa24fb
SDJ
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
c4fc7f1b 75/* Register names. */
c40e1eab 76
90884b2b 77static const char *i386_register_names[] =
fc633446
MK
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
01f9f808
MS
92static const char *i386_zmm_names[] =
93{
94 "zmm0", "zmm1", "zmm2", "zmm3",
95 "zmm4", "zmm5", "zmm6", "zmm7"
96};
97
98static const char *i386_zmmh_names[] =
99{
100 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
101 "zmm4h", "zmm5h", "zmm6h", "zmm7h"
102};
103
104static const char *i386_k_names[] =
105{
106 "k0", "k1", "k2", "k3",
107 "k4", "k5", "k6", "k7"
108};
109
c131fcee
L
110static const char *i386_ymm_names[] =
111{
112 "ymm0", "ymm1", "ymm2", "ymm3",
113 "ymm4", "ymm5", "ymm6", "ymm7",
114};
115
116static const char *i386_ymmh_names[] =
117{
118 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
119 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
120};
121
1dbcd68c
WT
122static const char *i386_mpx_names[] =
123{
124 "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
125};
126
127/* Register names for MPX pseudo-registers. */
128
129static const char *i386_bnd_names[] =
130{
131 "bnd0", "bnd1", "bnd2", "bnd3"
132};
133
c4fc7f1b 134/* Register names for MMX pseudo-registers. */
28fc6740 135
90884b2b 136static const char *i386_mmx_names[] =
28fc6740
AC
137{
138 "mm0", "mm1", "mm2", "mm3",
139 "mm4", "mm5", "mm6", "mm7"
140};
c40e1eab 141
1ba53b71
L
142/* Register names for byte pseudo-registers. */
143
144static 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
152static const char *i386_word_names[] =
153{
154 "ax", "cx", "dx", "bx",
9cad29ac 155 "", "bp", "si", "di"
1ba53b71
L
156};
157
01f9f808
MS
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
162const int num_lower_zmm_regs = 16;
163
1ba53b71 164/* MMX register? */
c40e1eab 165
28fc6740 166static int
5716833c 167i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
28fc6740 168{
1ba53b71
L
169 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
170 int mm0_regnum = tdep->mm0_regnum;
5716833c
MK
171
172 if (mm0_regnum < 0)
173 return 0;
174
1ba53b71
L
175 regnum -= mm0_regnum;
176 return regnum >= 0 && regnum < tdep->num_mmx_regs;
177}
178
179/* Byte register? */
180
181int
182i386_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
192int
193i386_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
203int
204i386_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;
28fc6740
AC
214}
215
01f9f808
MS
216/* AVX512 register? */
217
218int
219i386_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
231int
232i386_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
244int
245i386_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
9191d390 257static int
c131fcee
L
258i386_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
272int
273i386_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
01f9f808
MS
285static int
286i386_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
298int
299i386_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
1dbcd68c
WT
311/* BND register? */
312
313int
314i386_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
5716833c 326/* SSE register? */
23a34459 327
c131fcee
L
328int
329i386_xmm_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 330{
5716833c 331 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
c131fcee 332 int num_xmm_regs = I387_NUM_XMM_REGS (tdep);
5716833c 333
c131fcee 334 if (num_xmm_regs == 0)
5716833c
MK
335 return 0;
336
c131fcee
L
337 regnum -= I387_XMM0_REGNUM (tdep);
338 return regnum >= 0 && regnum < num_xmm_regs;
23a34459
AC
339}
340
01f9f808
MS
341/* XMM_512 register? */
342
343int
344i386_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
5716833c
MK
356static int
357i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 358{
5716833c
MK
359 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
360
20a6ec49 361 if (I387_NUM_XMM_REGS (tdep) == 0)
5716833c
MK
362 return 0;
363
20a6ec49 364 return (regnum == I387_MXCSR_REGNUM (tdep));
23a34459
AC
365}
366
5716833c 367/* FP register? */
23a34459
AC
368
369int
20a6ec49 370i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 371{
20a6ec49
MD
372 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
373
374 if (I387_ST0_REGNUM (tdep) < 0)
5716833c
MK
375 return 0;
376
20a6ec49
MD
377 return (I387_ST0_REGNUM (tdep) <= regnum
378 && regnum < I387_FCTRL_REGNUM (tdep));
23a34459
AC
379}
380
381int
20a6ec49 382i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 383{
20a6ec49
MD
384 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
385
386 if (I387_ST0_REGNUM (tdep) < 0)
5716833c
MK
387 return 0;
388
20a6ec49
MD
389 return (I387_FCTRL_REGNUM (tdep) <= regnum
390 && regnum < I387_XMM0_REGNUM (tdep));
23a34459
AC
391}
392
1dbcd68c
WT
393/* BNDr (raw) register? */
394
395static int
396i386_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
409static int
410i386_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
c131fcee
L
421/* Return the name of register REGNUM, or the empty string if it is
422 an anonymous register. */
423
424static const char *
425i386_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
01f9f808
MS
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
c131fcee
L
439 return tdesc_register_name (gdbarch, regnum);
440}
441
30b0e2d8 442/* Return the name of register REGNUM. */
fc633446 443
1ba53b71 444const char *
90884b2b 445i386_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
fc633446 446{
1ba53b71 447 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1dbcd68c
WT
448 if (i386_bnd_regnum_p (gdbarch, regnum))
449 return i386_bnd_names[regnum - tdep->bnd0_regnum];
1ba53b71
L
450 if (i386_mmx_regnum_p (gdbarch, regnum))
451 return i386_mmx_names[regnum - I387_MM0_REGNUM (tdep)];
c131fcee
L
452 else if (i386_ymm_regnum_p (gdbarch, regnum))
453 return i386_ymm_names[regnum - tdep->ymm0_regnum];
01f9f808
MS
454 else if (i386_zmm_regnum_p (gdbarch, regnum))
455 return i386_zmm_names[regnum - tdep->zmm0_regnum];
1ba53b71
L
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"));
fc633446
MK
462}
463
c4fc7f1b 464/* Convert a dbx register number REG to the appropriate register
85540d8c
MK
465 number used by GDB. */
466
8201327c 467static int
d3f73121 468i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
85540d8c 469{
20a6ec49
MD
470 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
471
c4fc7f1b
MK
472 /* This implements what GCC calls the "default" register map
473 (dbx_register_map[]). */
474
85540d8c
MK
475 if (reg >= 0 && reg <= 7)
476 {
9872ad24
JB
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;
85540d8c
MK
484 }
485 else if (reg >= 12 && reg <= 19)
486 {
487 /* Floating-point registers. */
20a6ec49 488 return reg - 12 + I387_ST0_REGNUM (tdep);
85540d8c
MK
489 }
490 else if (reg >= 21 && reg <= 28)
491 {
492 /* SSE registers. */
c131fcee
L
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);
85540d8c
MK
500 }
501 else if (reg >= 29 && reg <= 36)
502 {
503 /* MMX registers. */
20a6ec49 504 return reg - 29 + I387_MM0_REGNUM (tdep);
85540d8c
MK
505 }
506
507 /* This will hopefully provoke a warning. */
d3f73121 508 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
85540d8c
MK
509}
510
c4fc7f1b
MK
511/* Convert SVR4 register number REG to the appropriate register number
512 used by GDB. */
85540d8c 513
8201327c 514static int
d3f73121 515i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
85540d8c 516{
20a6ec49
MD
517 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
518
c4fc7f1b
MK
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
85540d8c
MK
523 numbers the floating point registers differently. */
524 if (reg >= 0 && reg <= 9)
525 {
acd5c798 526 /* General-purpose registers. */
85540d8c
MK
527 return reg;
528 }
529 else if (reg >= 11 && reg <= 18)
530 {
531 /* Floating-point registers. */
20a6ec49 532 return reg - 11 + I387_ST0_REGNUM (tdep);
85540d8c 533 }
c6f4c129 534 else if (reg >= 21 && reg <= 36)
85540d8c 535 {
c4fc7f1b 536 /* The SSE and MMX registers have the same numbers as with dbx. */
d3f73121 537 return i386_dbx_reg_to_regnum (gdbarch, reg);
85540d8c
MK
538 }
539
c6f4c129
JB
540 switch (reg)
541 {
20a6ec49
MD
542 case 37: return I387_FCTRL_REGNUM (tdep);
543 case 38: return I387_FSTAT_REGNUM (tdep);
544 case 39: return I387_MXCSR_REGNUM (tdep);
c6f4c129
JB
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
85540d8c 553 /* This will hopefully provoke a warning. */
d3f73121 554 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
85540d8c 555}
5716833c 556
fc338970 557\f
917317f4 558
fc338970
MK
559/* This is the variable that is set with "set disassembly-flavor", and
560 its legitimate values. */
53904c9e
AC
561static const char att_flavor[] = "att";
562static const char intel_flavor[] = "intel";
40478521 563static const char *const valid_flavors[] =
c5aa993b 564{
c906108c
SS
565 att_flavor,
566 intel_flavor,
567 NULL
568};
53904c9e 569static const char *disassembly_flavor = att_flavor;
acd5c798 570\f
c906108c 571
acd5c798
MK
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.
c906108c 577
acd5c798
MK
578 On the i386 we have a single breakpoint that fits in a single byte
579 and can be inserted anywhere.
c906108c 580
acd5c798 581 This function is 64-bit safe. */
63c0089f
MK
582
583static const gdb_byte *
67d57894 584i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
c906108c 585{
63c0089f
MK
586 static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
587
acd5c798
MK
588 *len = sizeof (break_insn);
589 return break_insn;
c906108c 590}
237fc4c9
PA
591\f
592/* Displaced instruction handling. */
593
1903f0e6
DE
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
600static gdb_byte *
601i386_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}
237fc4c9
PA
629
630static int
1903f0e6 631i386_absolute_jmp_p (const gdb_byte *insn)
237fc4c9 632{
1777feb0 633 /* jmp far (absolute address in operand). */
237fc4c9
PA
634 if (insn[0] == 0xea)
635 return 1;
636
637 if (insn[0] == 0xff)
638 {
1777feb0 639 /* jump near, absolute indirect (/4). */
237fc4c9
PA
640 if ((insn[1] & 0x38) == 0x20)
641 return 1;
642
1777feb0 643 /* jump far, absolute indirect (/5). */
237fc4c9
PA
644 if ((insn[1] & 0x38) == 0x28)
645 return 1;
646 }
647
648 return 0;
649}
650
c2170eef
MM
651/* Return non-zero if INSN is a jump, zero otherwise. */
652
653static int
654i386_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
237fc4c9 667static int
1903f0e6 668i386_absolute_call_p (const gdb_byte *insn)
237fc4c9 669{
1777feb0 670 /* call far, absolute. */
237fc4c9
PA
671 if (insn[0] == 0x9a)
672 return 1;
673
674 if (insn[0] == 0xff)
675 {
1777feb0 676 /* Call near, absolute indirect (/2). */
237fc4c9
PA
677 if ((insn[1] & 0x38) == 0x10)
678 return 1;
679
1777feb0 680 /* Call far, absolute indirect (/3). */
237fc4c9
PA
681 if ((insn[1] & 0x38) == 0x18)
682 return 1;
683 }
684
685 return 0;
686}
687
688static int
1903f0e6 689i386_ret_p (const gdb_byte *insn)
237fc4c9
PA
690{
691 switch (insn[0])
692 {
1777feb0 693 case 0xc2: /* ret near, pop N bytes. */
237fc4c9 694 case 0xc3: /* ret near */
1777feb0 695 case 0xca: /* ret far, pop N bytes. */
237fc4c9
PA
696 case 0xcb: /* ret far */
697 case 0xcf: /* iret */
698 return 1;
699
700 default:
701 return 0;
702 }
703}
704
705static int
1903f0e6 706i386_call_p (const gdb_byte *insn)
237fc4c9
PA
707{
708 if (i386_absolute_call_p (insn))
709 return 1;
710
1777feb0 711 /* call near, relative. */
237fc4c9
PA
712 if (insn[0] == 0xe8)
713 return 1;
714
715 return 0;
716}
717
237fc4c9
PA
718/* Return non-zero if INSN is a system call, and set *LENGTHP to its
719 length in bytes. Otherwise, return zero. */
1903f0e6 720
237fc4c9 721static int
b55078be 722i386_syscall_p (const gdb_byte *insn, int *lengthp)
237fc4c9 723{
9a7f938f
JK
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))
237fc4c9
PA
730 {
731 *lengthp = 2;
732 return 1;
733 }
734
735 return 0;
736}
737
c2170eef
MM
738/* The gdbarch insn_is_call method. */
739
740static int
741i386_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
753static int
754i386_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
766static int
767i386_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
b55078be
DE
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
780struct displaced_step_closure *
781i386_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
237fc4c9
PA
814/* Fix up the state of registers and memory after having single-stepped
815 a displaced instruction. */
1903f0e6 816
237fc4c9
PA
817void
818i386_displaced_step_fixup (struct gdbarch *gdbarch,
819 struct displaced_step_closure *closure,
820 CORE_ADDR from, CORE_ADDR to,
821 struct regcache *regs)
822{
e17a4113
UW
823 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
824
237fc4c9
PA
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;
1903f0e6
DE
834 /* The start of the insn, needed in case we see some prefixes. */
835 gdb_byte *insn_start = insn;
237fc4c9
PA
836
837 if (debug_displaced)
838 fprintf_unfiltered (gdb_stdlog,
5af949e3 839 "displaced: fixup (%s, %s), "
237fc4c9 840 "insn = 0x%02x 0x%02x ...\n",
5af949e3
UW
841 paddress (gdbarch, from), paddress (gdbarch, to),
842 insn[0], insn[1]);
237fc4c9
PA
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
1903f0e6
DE
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
237fc4c9
PA
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;
b55078be 872 int insn_len;
237fc4c9
PA
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)
b55078be
DE
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)
237fc4c9
PA
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
1903f0e6
DE
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. */
237fc4c9
PA
910
911 regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
912
913 if (debug_displaced)
914 fprintf_unfiltered (gdb_stdlog,
915 "displaced: "
5af949e3
UW
916 "relocated %%eip from %s to %s\n",
917 paddress (gdbarch, orig_eip),
918 paddress (gdbarch, eip));
237fc4c9
PA
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);
b75f0b83 937 retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
237fc4c9 938 retaddr = (retaddr - insn_offset) & 0xffffffffUL;
e17a4113 939 write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
237fc4c9
PA
940
941 if (debug_displaced)
942 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
943 "displaced: relocated return addr at %s to %s\n",
944 paddress (gdbarch, esp),
945 paddress (gdbarch, retaddr));
237fc4c9
PA
946 }
947}
dde08ee1
PA
948
949static void
950append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
951{
952 target_write_memory (*to, buf, len);
953 *to += len;
954}
955
956static void
957i386_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;
1777feb0 984 push_buf[0] = 0x68; /* pushq $... */
144db827 985 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
dde08ee1
PA
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;
f4a1794a
KY
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));
dde08ee1
PA
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;
f4a1794a 1021 store_signed_integer (insn + offset, 4, byte_order, newrel);
dde08ee1
PA
1022 if (debug_displaced)
1023 fprintf_unfiltered (gdb_stdlog,
f4a1794a
KY
1024 "Adjusted insn rel32=%s at %s to"
1025 " rel32=%s at %s\n",
dde08ee1
PA
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
fc338970 1035\f
acd5c798
MK
1036#ifdef I386_REGNO_TO_SYMMETRY
1037#error "The Sequent Symmetry is no longer supported."
1038#endif
c906108c 1039
acd5c798
MK
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. */
c906108c 1043
acd5c798
MK
1044/* The maximum number of saved registers. This should include all
1045 registers mentioned above, and %eip. */
a3386186 1046#define I386_NUM_SAVED_REGS I386_NUM_GREGS
acd5c798
MK
1047
1048struct i386_frame_cache
c906108c 1049{
acd5c798
MK
1050 /* Base address. */
1051 CORE_ADDR base;
8fbca658 1052 int base_p;
772562f8 1053 LONGEST sp_offset;
acd5c798
MK
1054 CORE_ADDR pc;
1055
fd13a04a
AC
1056 /* Saved registers. */
1057 CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
acd5c798 1058 CORE_ADDR saved_sp;
e0c62198 1059 int saved_sp_reg;
acd5c798
MK
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
1068static struct i386_frame_cache *
fd13a04a 1069i386_alloc_frame_cache (void)
acd5c798
MK
1070{
1071 struct i386_frame_cache *cache;
1072 int i;
1073
1074 cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
1075
1076 /* Base address. */
8fbca658 1077 cache->base_p = 0;
acd5c798
MK
1078 cache->base = 0;
1079 cache->sp_offset = -4;
1080 cache->pc = 0;
1081
fd13a04a
AC
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;
acd5c798 1086 cache->saved_sp = 0;
e0c62198 1087 cache->saved_sp_reg = -1;
acd5c798
MK
1088 cache->pc_in_eax = 0;
1089
1090 /* Frameless until proven otherwise. */
1091 cache->locals = -1;
1092
1093 return cache;
1094}
c906108c 1095
acd5c798
MK
1096/* If the instruction at PC is a jump, return the address of its
1097 target. Otherwise, return PC. */
c906108c 1098
acd5c798 1099static CORE_ADDR
e17a4113 1100i386_follow_jump (struct gdbarch *gdbarch, CORE_ADDR pc)
acd5c798 1101{
e17a4113 1102 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
63c0089f 1103 gdb_byte op;
acd5c798
MK
1104 long delta = 0;
1105 int data16 = 0;
c906108c 1106
0865b04a 1107 if (target_read_code (pc, &op, 1))
3dcabaa8
MS
1108 return pc;
1109
acd5c798 1110 if (op == 0x66)
c906108c 1111 {
c906108c 1112 data16 = 1;
0865b04a
YQ
1113
1114 op = read_code_unsigned_integer (pc + 1, 1, byte_order);
c906108c
SS
1115 }
1116
acd5c798 1117 switch (op)
c906108c
SS
1118 {
1119 case 0xe9:
fc338970 1120 /* Relative jump: if data16 == 0, disp32, else disp16. */
c906108c
SS
1121 if (data16)
1122 {
e17a4113 1123 delta = read_memory_integer (pc + 2, 2, byte_order);
c906108c 1124
fc338970
MK
1125 /* Include the size of the jmp instruction (including the
1126 0x66 prefix). */
acd5c798 1127 delta += 4;
c906108c
SS
1128 }
1129 else
1130 {
e17a4113 1131 delta = read_memory_integer (pc + 1, 4, byte_order);
c906108c 1132
acd5c798
MK
1133 /* Include the size of the jmp instruction. */
1134 delta += 5;
c906108c
SS
1135 }
1136 break;
1137 case 0xeb:
fc338970 1138 /* Relative jump, disp8 (ignore data16). */
e17a4113 1139 delta = read_memory_integer (pc + data16 + 1, 1, byte_order);
c906108c 1140
acd5c798 1141 delta += data16 + 2;
c906108c
SS
1142 break;
1143 }
c906108c 1144
acd5c798
MK
1145 return pc + delta;
1146}
fc338970 1147
acd5c798
MK
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. */
c906108c 1153
acd5c798
MK
1154static CORE_ADDR
1155i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
1156 struct i386_frame_cache *cache)
c906108c 1157{
acd5c798
MK
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. */
63c0089f
MK
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;
c906108c 1172
acd5c798
MK
1173 if (current_pc <= pc)
1174 return pc;
1175
0865b04a 1176 if (target_read_code (pc, &op, 1))
3dcabaa8 1177 return pc;
c906108c 1178
acd5c798
MK
1179 if (op != 0x58) /* popl %eax */
1180 return pc;
c906108c 1181
0865b04a 1182 if (target_read_code (pc + 1, buf, 4))
3dcabaa8
MS
1183 return pc;
1184
acd5c798
MK
1185 if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
1186 return pc;
c906108c 1187
acd5c798 1188 if (current_pc == pc)
c906108c 1189 {
acd5c798
MK
1190 cache->sp_offset += 4;
1191 return current_pc;
c906108c
SS
1192 }
1193
acd5c798 1194 if (current_pc == pc + 1)
c906108c 1195 {
acd5c798
MK
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
1206static CORE_ADDR
1207i386_skip_probe (CORE_ADDR pc)
1208{
1209 /* A function may start with
fc338970 1210
acd5c798
MK
1211 pushl constant
1212 call _probe
1213 addl $4, %esp
fc338970 1214
acd5c798
MK
1215 followed by
1216
1217 pushl %ebp
fc338970 1218
acd5c798 1219 etc. */
63c0089f
MK
1220 gdb_byte buf[8];
1221 gdb_byte op;
fc338970 1222
0865b04a 1223 if (target_read_code (pc, &op, 1))
3dcabaa8 1224 return pc;
acd5c798
MK
1225
1226 if (op == 0x68 || op == 0x6a)
1227 {
1228 int delta;
c906108c 1229
acd5c798
MK
1230 /* Skip past the `pushl' instruction; it has either a one-byte or a
1231 four-byte operand, depending on the opcode. */
c906108c 1232 if (op == 0x68)
acd5c798 1233 delta = 5;
c906108c 1234 else
acd5c798 1235 delta = 2;
c906108c 1236
acd5c798
MK
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));
c906108c 1240 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
acd5c798 1241 pc += delta + sizeof (buf);
c906108c
SS
1242 }
1243
acd5c798
MK
1244 return pc;
1245}
1246
92dd43fa
MK
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
1253static CORE_ADDR
1254i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1255 struct i386_frame_cache *cache)
1256{
e0c62198
L
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 */
92dd43fa 1291 };
92dd43fa 1292
0865b04a 1293 if (target_read_code (pc, buf, sizeof buf))
e0c62198
L
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))
92dd43fa
MK
1356 return pc;
1357
e0c62198
L
1358 if (current_pc > pc + offset_and)
1359 cache->saved_sp_reg = regnums[reg];
92dd43fa 1360
e0c62198 1361 return min (pc + offset + 3, current_pc);
92dd43fa
MK
1362}
1363
37bdc87e 1364/* Maximum instruction length we need to handle. */
237fc4c9 1365#define I386_MAX_MATCHED_INSN_LEN 6
37bdc87e
MK
1366
1367/* Instruction description. */
1368struct i386_insn
1369{
1370 size_t len;
237fc4c9
PA
1371 gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
1372 gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
37bdc87e
MK
1373};
1374
a3fcb948 1375/* Return whether instruction at PC matches PATTERN. */
37bdc87e 1376
a3fcb948
JG
1377static int
1378i386_match_pattern (CORE_ADDR pc, struct i386_insn pattern)
37bdc87e 1379{
63c0089f 1380 gdb_byte op;
37bdc87e 1381
0865b04a 1382 if (target_read_code (pc, &op, 1))
a3fcb948 1383 return 0;
37bdc87e 1384
a3fcb948 1385 if ((op & pattern.mask[0]) == pattern.insn[0])
37bdc87e 1386 {
a3fcb948
JG
1387 gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
1388 int insn_matched = 1;
1389 size_t i;
37bdc87e 1390
a3fcb948
JG
1391 gdb_assert (pattern.len > 1);
1392 gdb_assert (pattern.len <= I386_MAX_MATCHED_INSN_LEN);
3dcabaa8 1393
0865b04a 1394 if (target_read_code (pc + 1, buf, pattern.len - 1))
a3fcb948 1395 return 0;
613e8135 1396
a3fcb948
JG
1397 for (i = 1; i < pattern.len; i++)
1398 {
1399 if ((buf[i - 1] & pattern.mask[i]) != pattern.insn[i])
1400 insn_matched = 0;
37bdc87e 1401 }
a3fcb948
JG
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
1411static struct i386_insn *
1412i386_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;
37bdc87e
MK
1420 }
1421
1422 return NULL;
1423}
1424
a3fcb948
JG
1425/* Return whether PC points inside a sequence of instructions that
1426 matches INSN_PATTERNS. */
1427
1428static int
1429i386_match_insn_block (CORE_ADDR pc, struct i386_insn *insn_patterns)
1430{
1431 CORE_ADDR current_pc;
1432 int ix, i;
a3fcb948
JG
1433 struct i386_insn *insn;
1434
1435 insn = i386_match_insn (pc, insn_patterns);
1436 if (insn == NULL)
1437 return 0;
1438
8bbdd3f4 1439 current_pc = pc;
a3fcb948
JG
1440 ix = insn - insn_patterns;
1441 for (i = ix - 1; i >= 0; i--)
1442 {
8bbdd3f4
MK
1443 current_pc -= insn_patterns[i].len;
1444
a3fcb948
JG
1445 if (!i386_match_pattern (current_pc, insn_patterns[i]))
1446 return 0;
a3fcb948
JG
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
37bdc87e
MK
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
1467struct i386_insn i386_frame_setup_skip_insns[] =
1468{
1777feb0 1469 /* Check for `movb imm8, r' and `movl imm32, r'.
37bdc87e
MK
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
e11481da
PM
1516
1517/* Check whether PC points to a no-op instruction. */
1518static CORE_ADDR
1519i386_skip_noop (CORE_ADDR pc)
1520{
1521 gdb_byte op;
1522 int check = 1;
1523
0865b04a 1524 if (target_read_code (pc, &op, 1))
3dcabaa8 1525 return pc;
e11481da
PM
1526
1527 while (check)
1528 {
1529 check = 0;
1530 /* Ignore `nop' instruction. */
1531 if (op == 0x90)
1532 {
1533 pc += 1;
0865b04a 1534 if (target_read_code (pc, &op, 1))
3dcabaa8 1535 return pc;
e11481da
PM
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 {
0865b04a 1551 if (target_read_code (pc + 1, &op, 1))
3dcabaa8
MS
1552 return pc;
1553
e11481da
PM
1554 if (op == 0xff)
1555 {
1556 pc += 2;
0865b04a 1557 if (target_read_code (pc, &op, 1))
3dcabaa8
MS
1558 return pc;
1559
e11481da
PM
1560 check = 1;
1561 }
1562 }
1563 }
1564 return pc;
1565}
1566
acd5c798
MK
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
37bdc87e
MK
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. */
acd5c798
MK
1571
1572static CORE_ADDR
e17a4113
UW
1573i386_analyze_frame_setup (struct gdbarch *gdbarch,
1574 CORE_ADDR pc, CORE_ADDR limit,
acd5c798
MK
1575 struct i386_frame_cache *cache)
1576{
e17a4113 1577 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
37bdc87e 1578 struct i386_insn *insn;
63c0089f 1579 gdb_byte op;
26604a34 1580 int skip = 0;
acd5c798 1581
37bdc87e
MK
1582 if (limit <= pc)
1583 return limit;
acd5c798 1584
0865b04a 1585 if (target_read_code (pc, &op, 1))
3dcabaa8 1586 return pc;
acd5c798 1587
c906108c 1588 if (op == 0x55) /* pushl %ebp */
c5aa993b 1589 {
acd5c798
MK
1590 /* Take into account that we've executed the `pushl %ebp' that
1591 starts this instruction sequence. */
fd13a04a 1592 cache->saved_regs[I386_EBP_REGNUM] = 0;
acd5c798 1593 cache->sp_offset += 4;
37bdc87e 1594 pc++;
acd5c798
MK
1595
1596 /* If that's all, return now. */
37bdc87e
MK
1597 if (limit <= pc)
1598 return limit;
26604a34 1599
b4632131 1600 /* Check for some special instructions that might be migrated by
37bdc87e
MK
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.
5daa5b4e 1605
26604a34
MK
1606 Make sure we only skip these instructions if we later see the
1607 `movl %esp, %ebp' that actually sets up the frame. */
37bdc87e 1608 while (pc + skip < limit)
26604a34 1609 {
37bdc87e
MK
1610 insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1611 if (insn == NULL)
1612 break;
b4632131 1613
37bdc87e 1614 skip += insn->len;
26604a34
MK
1615 }
1616
37bdc87e
MK
1617 /* If that's all, return now. */
1618 if (limit <= pc + skip)
1619 return limit;
1620
0865b04a 1621 if (target_read_code (pc + skip, &op, 1))
3dcabaa8 1622 return pc + skip;
37bdc87e 1623
30f8135b
YQ
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
acd5c798 1638 switch (op)
c906108c 1639 {
30f8135b 1640 /* Check for `movl %esp, %ebp' -- can be written in two ways. */
c906108c 1641 case 0x8b:
0865b04a 1642 if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
e17a4113 1643 != 0xec)
37bdc87e 1644 return pc;
30f8135b 1645 pc += (skip + 2);
c906108c
SS
1646 break;
1647 case 0x89:
0865b04a 1648 if (read_code_unsigned_integer (pc + skip + 1, 1, byte_order)
e17a4113 1649 != 0xe5)
37bdc87e 1650 return pc;
30f8135b
YQ
1651 pc += (skip + 2);
1652 break;
1653 case 0x8d: /* Check for 'lea (%ebp), %ebp'. */
0865b04a 1654 if (read_code_unsigned_integer (pc + skip + 1, 2, byte_order)
30f8135b
YQ
1655 != 0x242c)
1656 return pc;
1657 pc += (skip + 3);
c906108c
SS
1658 break;
1659 default:
37bdc87e 1660 return pc;
c906108c 1661 }
acd5c798 1662
26604a34
MK
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. */
acd5c798
MK
1667 cache->locals = 0;
1668
1669 /* If that's all, return now. */
37bdc87e
MK
1670 if (limit <= pc)
1671 return limit;
acd5c798 1672
fc338970
MK
1673 /* Check for stack adjustment
1674
acd5c798 1675 subl $XXX, %esp
30f8135b
YQ
1676 or
1677 lea -XXX(%esp),%esp
fc338970 1678
fd35795f 1679 NOTE: You can't subtract a 16-bit immediate from a 32-bit
fc338970 1680 reg, so we don't have to worry about a data16 prefix. */
0865b04a 1681 if (target_read_code (pc, &op, 1))
3dcabaa8 1682 return pc;
c906108c
SS
1683 if (op == 0x83)
1684 {
fd35795f 1685 /* `subl' with 8-bit immediate. */
0865b04a 1686 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
fc338970 1687 /* Some instruction starting with 0x83 other than `subl'. */
37bdc87e 1688 return pc;
acd5c798 1689
37bdc87e
MK
1690 /* `subl' with signed 8-bit immediate (though it wouldn't
1691 make sense to be negative). */
0865b04a 1692 cache->locals = read_code_integer (pc + 2, 1, byte_order);
37bdc87e 1693 return pc + 3;
c906108c
SS
1694 }
1695 else if (op == 0x81)
1696 {
fd35795f 1697 /* Maybe it is `subl' with a 32-bit immediate. */
0865b04a 1698 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0xec)
fc338970 1699 /* Some instruction starting with 0x81 other than `subl'. */
37bdc87e 1700 return pc;
acd5c798 1701
fd35795f 1702 /* It is `subl' with a 32-bit immediate. */
0865b04a 1703 cache->locals = read_code_integer (pc + 2, 4, byte_order);
37bdc87e 1704 return pc + 6;
c906108c 1705 }
30f8135b
YQ
1706 else if (op == 0x8d)
1707 {
1708 /* The ModR/M byte is 0x64. */
0865b04a 1709 if (read_code_unsigned_integer (pc + 1, 1, byte_order) != 0x64)
30f8135b
YQ
1710 return pc;
1711 /* 'lea' with 8-bit displacement. */
0865b04a 1712 cache->locals = -1 * read_code_integer (pc + 3, 1, byte_order);
30f8135b
YQ
1713 return pc + 4;
1714 }
c906108c
SS
1715 else
1716 {
30f8135b 1717 /* Some instruction other than `subl' nor 'lea'. */
37bdc87e 1718 return pc;
c906108c
SS
1719 }
1720 }
37bdc87e 1721 else if (op == 0xc8) /* enter */
c906108c 1722 {
0865b04a 1723 cache->locals = read_code_unsigned_integer (pc + 1, 2, byte_order);
acd5c798 1724 return pc + 4;
c906108c 1725 }
21d0e8a4 1726
acd5c798 1727 return pc;
21d0e8a4
MK
1728}
1729
acd5c798
MK
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. */
6bff26de
MK
1734
1735static CORE_ADDR
acd5c798
MK
1736i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1737 struct i386_frame_cache *cache)
6bff26de 1738{
99ab4326 1739 CORE_ADDR offset = 0;
63c0089f 1740 gdb_byte op;
99ab4326 1741 int i;
c0d1d883 1742
99ab4326
MK
1743 if (cache->locals > 0)
1744 offset -= cache->locals;
1745 for (i = 0; i < 8 && pc < current_pc; i++)
1746 {
0865b04a 1747 if (target_read_code (pc, &op, 1))
3dcabaa8 1748 return pc;
99ab4326
MK
1749 if (op < 0x50 || op > 0x57)
1750 break;
0d17c81d 1751
99ab4326
MK
1752 offset -= 4;
1753 cache->saved_regs[op - 0x50] = offset;
1754 cache->sp_offset += 4;
1755 pc++;
6bff26de
MK
1756 }
1757
acd5c798 1758 return pc;
22797942
AC
1759}
1760
acd5c798
MK
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.
ed84f6c1 1764
fc338970
MK
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
fd35795f
MK
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.
fc338970
MK
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. */
c906108c 1787
acd5c798 1788static CORE_ADDR
e17a4113
UW
1789i386_analyze_prologue (struct gdbarch *gdbarch,
1790 CORE_ADDR pc, CORE_ADDR current_pc,
acd5c798 1791 struct i386_frame_cache *cache)
c906108c 1792{
e11481da 1793 pc = i386_skip_noop (pc);
e17a4113 1794 pc = i386_follow_jump (gdbarch, pc);
acd5c798
MK
1795 pc = i386_analyze_struct_return (pc, current_pc, cache);
1796 pc = i386_skip_probe (pc);
92dd43fa 1797 pc = i386_analyze_stack_align (pc, current_pc, cache);
e17a4113 1798 pc = i386_analyze_frame_setup (gdbarch, pc, current_pc, cache);
acd5c798 1799 return i386_analyze_register_saves (pc, current_pc, cache);
c906108c
SS
1800}
1801
fc338970 1802/* Return PC of first real instruction. */
c906108c 1803
3a1e71e3 1804static CORE_ADDR
6093d2eb 1805i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
c906108c 1806{
e17a4113
UW
1807 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1808
63c0089f 1809 static gdb_byte pic_pat[6] =
acd5c798
MK
1810 {
1811 0xe8, 0, 0, 0, 0, /* call 0x0 */
1812 0x5b, /* popl %ebx */
c5aa993b 1813 };
acd5c798
MK
1814 struct i386_frame_cache cache;
1815 CORE_ADDR pc;
63c0089f 1816 gdb_byte op;
acd5c798 1817 int i;
56bf0743 1818 CORE_ADDR func_addr;
4e879fc2 1819
56bf0743
KB
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
e0f33b1f 1835 cache.locals = -1;
e17a4113 1836 pc = i386_analyze_prologue (gdbarch, start_pc, 0xffffffff, &cache);
acd5c798
MK
1837 if (cache.locals < 0)
1838 return start_pc;
c5aa993b 1839
acd5c798 1840 /* Found valid frame setup. */
c906108c 1841
fc338970
MK
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
acd5c798
MK
1844 %ebx:
1845
fc338970
MK
1846 call 0x0
1847 popl %ebx
1848 movl %ebx,x(%ebp) (optional)
1849 addl y,%ebx
1850
c906108c
SS
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. */
c5aa993b 1854
c906108c
SS
1855 for (i = 0; i < 6; i++)
1856 {
0865b04a 1857 if (target_read_code (pc + i, &op, 1))
3dcabaa8
MS
1858 return pc;
1859
c5aa993b 1860 if (pic_pat[i] != op)
c906108c
SS
1861 break;
1862 }
1863 if (i == 6)
1864 {
acd5c798
MK
1865 int delta = 6;
1866
0865b04a 1867 if (target_read_code (pc + delta, &op, 1))
3dcabaa8 1868 return pc;
c906108c 1869
c5aa993b 1870 if (op == 0x89) /* movl %ebx, x(%ebp) */
c906108c 1871 {
0865b04a 1872 op = read_code_unsigned_integer (pc + delta + 1, 1, byte_order);
acd5c798 1873
fc338970 1874 if (op == 0x5d) /* One byte offset from %ebp. */
acd5c798 1875 delta += 3;
fc338970 1876 else if (op == 0x9d) /* Four byte offset from %ebp. */
acd5c798 1877 delta += 6;
fc338970 1878 else /* Unexpected instruction. */
acd5c798
MK
1879 delta = 0;
1880
0865b04a 1881 if (target_read_code (pc + delta, &op, 1))
3dcabaa8 1882 return pc;
c906108c 1883 }
acd5c798 1884
c5aa993b 1885 /* addl y,%ebx */
acd5c798 1886 if (delta > 0 && op == 0x81
0865b04a 1887 && read_code_unsigned_integer (pc + delta + 1, 1, byte_order)
e17a4113 1888 == 0xc3)
c906108c 1889 {
acd5c798 1890 pc += delta + 6;
c906108c
SS
1891 }
1892 }
c5aa993b 1893
e63bbc88
MK
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. */
e17a4113
UW
1897 if (i386_follow_jump (gdbarch, start_pc) != start_pc)
1898 pc = i386_follow_jump (gdbarch, pc);
e63bbc88
MK
1899
1900 return pc;
c906108c
SS
1901}
1902
4309257c
PM
1903/* Check that the code pointed to by PC corresponds to a call to
1904 __main, skip it if so. Return PC otherwise. */
1905
1906CORE_ADDR
1907i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1908{
e17a4113 1909 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4309257c
PM
1910 gdb_byte op;
1911
0865b04a 1912 if (target_read_code (pc, &op, 1))
3dcabaa8 1913 return pc;
4309257c
PM
1914 if (op == 0xe8)
1915 {
1916 gdb_byte buf[4];
1917
0865b04a 1918 if (target_read_code (pc + 1, buf, sizeof buf) == 0)
4309257c
PM
1919 {
1920 /* Make sure address is computed correctly as a 32bit
1921 integer even if CORE_ADDR is 64 bit wide. */
7cbd4a93 1922 struct bound_minimal_symbol s;
e17a4113 1923 CORE_ADDR call_dest;
4309257c 1924
e17a4113 1925 call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
4309257c
PM
1926 call_dest = call_dest & 0xffffffffU;
1927 s = lookup_minimal_symbol_by_pc (call_dest);
7cbd4a93 1928 if (s.minsym != NULL
efd66ac6
TT
1929 && MSYMBOL_LINKAGE_NAME (s.minsym) != NULL
1930 && strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0)
4309257c
PM
1931 pc += 5;
1932 }
1933 }
1934
1935 return pc;
1936}
1937
acd5c798 1938/* This function is 64-bit safe. */
93924b6b 1939
acd5c798
MK
1940static CORE_ADDR
1941i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
93924b6b 1942{
63c0089f 1943 gdb_byte buf[8];
acd5c798 1944
875f8d0e 1945 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
0dfff4cb 1946 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
93924b6b 1947}
acd5c798 1948\f
93924b6b 1949
acd5c798 1950/* Normal frames. */
c5aa993b 1951
8fbca658
PA
1952static void
1953i386_frame_cache_1 (struct frame_info *this_frame,
1954 struct i386_frame_cache *cache)
a7769679 1955{
e17a4113
UW
1956 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1957 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
63c0089f 1958 gdb_byte buf[4];
acd5c798
MK
1959 int i;
1960
8fbca658 1961 cache->pc = get_frame_func (this_frame);
acd5c798
MK
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
10458914 1972 get_frame_register (this_frame, I386_EBP_REGNUM, buf);
e17a4113 1973 cache->base = extract_unsigned_integer (buf, 4, byte_order);
acd5c798 1974 if (cache->base == 0)
620fa63a
PA
1975 {
1976 cache->base_p = 1;
1977 return;
1978 }
acd5c798
MK
1979
1980 /* For normal frames, %eip is stored at 4(%ebp). */
fd13a04a 1981 cache->saved_regs[I386_EIP_REGNUM] = 4;
acd5c798 1982
acd5c798 1983 if (cache->pc != 0)
e17a4113
UW
1984 i386_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
1985 cache);
acd5c798
MK
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
e0c62198 1997 if (cache->saved_sp_reg != -1)
92dd43fa 1998 {
8fbca658
PA
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
92dd43fa
MK
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 }
7618e12b 2010 else if (cache->pc != 0
0865b04a 2011 || target_read_code (get_frame_pc (this_frame), buf, 1))
92dd43fa 2012 {
7618e12b
DJ
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. */
10458914 2018 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
e17a4113
UW
2019 cache->base = extract_unsigned_integer (buf, 4, byte_order)
2020 + cache->sp_offset;
92dd43fa 2021 }
7618e12b
DJ
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;
acd5c798
MK
2028 }
2029
8fbca658
PA
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
ca9d61b9
JB
2035 && deprecated_frame_register_read (this_frame,
2036 cache->saved_sp_reg, buf))
8fbca658
PA
2037 cache->saved_sp = extract_unsigned_integer (buf, 4, byte_order);
2038 }
acd5c798
MK
2039 /* Now that we have the base address for the stack frame we can
2040 calculate the value of %esp in the calling frame. */
8fbca658 2041 else if (cache->saved_sp == 0)
92dd43fa 2042 cache->saved_sp = cache->base + 8;
a7769679 2043
acd5c798
MK
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++)
fd13a04a
AC
2047 if (cache->saved_regs[i] != -1)
2048 cache->saved_regs[i] += cache->base;
acd5c798 2049
8fbca658
PA
2050 cache->base_p = 1;
2051}
2052
2053static struct i386_frame_cache *
2054i386_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
acd5c798 2072 return cache;
a7769679
MK
2073}
2074
3a1e71e3 2075static void
10458914 2076i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
acd5c798 2077 struct frame_id *this_id)
c906108c 2078{
10458914 2079 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798 2080
5ce0145d
PA
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 }
acd5c798
MK
2092}
2093
8fbca658
PA
2094static enum unwind_stop_reason
2095i386_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
10458914
DJ
2110static struct value *
2111i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
2112 int regnum)
acd5c798 2113{
10458914 2114 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798
MK
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 {
10458914 2139 ULONGEST val;
c5aa993b 2140
10458914
DJ
2141 val = get_frame_register_unsigned (this_frame, regnum);
2142 val &= ~(1 << 10);
2143 return frame_unwind_got_constant (this_frame, regnum, val);
acd5c798 2144 }
1211c4e4 2145
acd5c798 2146 if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
10458914 2147 return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
acd5c798 2148
fcf250e2
UW
2149 if (regnum == I386_ESP_REGNUM
2150 && (cache->saved_sp != 0 || cache->saved_sp_reg != -1))
8fbca658
PA
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. */
fcf250e2 2155 if (cache->saved_sp == 0)
8fbca658
PA
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 }
acd5c798 2162
fd13a04a 2163 if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
10458914
DJ
2164 return frame_unwind_got_memory (this_frame, regnum,
2165 cache->saved_regs[regnum]);
fd13a04a 2166
10458914 2167 return frame_unwind_got_register (this_frame, regnum, regnum);
acd5c798
MK
2168}
2169
2170static const struct frame_unwind i386_frame_unwind =
2171{
2172 NORMAL_FRAME,
8fbca658 2173 i386_frame_unwind_stop_reason,
acd5c798 2174 i386_frame_this_id,
10458914
DJ
2175 i386_frame_prev_register,
2176 NULL,
2177 default_frame_sniffer
acd5c798 2178};
06da04c6
MS
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
2186static int
2187i386_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2188{
2189 gdb_byte insn;
e0d00bc7
JK
2190 struct symtab *symtab;
2191
2192 symtab = find_pc_symtab (pc);
2193 if (symtab && symtab->epilogue_unwind_valid)
2194 return 0;
06da04c6
MS
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
2205static int
2206i386_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
2217static struct i386_frame_cache *
2218i386_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2219{
8fbca658 2220 volatile struct gdb_exception ex;
06da04c6 2221 struct i386_frame_cache *cache;
0d6c2135 2222 CORE_ADDR sp;
06da04c6
MS
2223
2224 if (*this_cache)
2225 return *this_cache;
2226
2227 cache = i386_alloc_frame_cache ();
2228 *this_cache = cache;
2229
8fbca658
PA
2230 TRY_CATCH (ex, RETURN_MASK_ERROR)
2231 {
0d6c2135 2232 cache->pc = get_frame_func (this_frame);
06da04c6 2233
0d6c2135
MK
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;
8fbca658 2239 cache->saved_sp = cache->base + 8;
8fbca658 2240 cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
06da04c6 2241
8fbca658
PA
2242 cache->base_p = 1;
2243 }
2244 if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
2245 throw_exception (ex);
06da04c6
MS
2246
2247 return cache;
2248}
2249
8fbca658
PA
2250static enum unwind_stop_reason
2251i386_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
2252 void **this_cache)
2253{
0d6c2135
MK
2254 struct i386_frame_cache *cache =
2255 i386_epilogue_frame_cache (this_frame, this_cache);
8fbca658
PA
2256
2257 if (!cache->base_p)
2258 return UNWIND_UNAVAILABLE;
2259
2260 return UNWIND_NO_REASON;
2261}
2262
06da04c6
MS
2263static void
2264i386_epilogue_frame_this_id (struct frame_info *this_frame,
2265 void **this_cache,
2266 struct frame_id *this_id)
2267{
0d6c2135
MK
2268 struct i386_frame_cache *cache =
2269 i386_epilogue_frame_cache (this_frame, this_cache);
06da04c6 2270
8fbca658 2271 if (!cache->base_p)
5ce0145d
PA
2272 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2273 else
2274 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
06da04c6
MS
2275}
2276
0d6c2135
MK
2277static struct value *
2278i386_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
06da04c6
MS
2287static const struct frame_unwind i386_epilogue_frame_unwind =
2288{
2289 NORMAL_FRAME,
8fbca658 2290 i386_epilogue_frame_unwind_stop_reason,
06da04c6 2291 i386_epilogue_frame_this_id,
0d6c2135 2292 i386_epilogue_frame_prev_register,
06da04c6
MS
2293 NULL,
2294 i386_epilogue_frame_sniffer
2295};
acd5c798
MK
2296\f
2297
a3fcb948
JG
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
2307struct 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
2320struct 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
2333static int
6df81a63 2334i386_in_stack_tramp_p (CORE_ADDR pc)
a3fcb948
JG
2335{
2336 gdb_byte insn;
2c02bd72 2337 const char *name;
a3fcb948
JG
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
2357static int
2358i386_stack_tramp_frame_sniffer (const struct frame_unwind *self,
0d6c2135
MK
2359 struct frame_info *this_frame,
2360 void **this_cache)
a3fcb948
JG
2361{
2362 if (frame_relative_level (this_frame) == 0)
6df81a63 2363 return i386_in_stack_tramp_p (get_frame_pc (this_frame));
a3fcb948
JG
2364 else
2365 return 0;
2366}
2367
2368static 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,
0d6c2135 2373 i386_epilogue_frame_prev_register,
a3fcb948
JG
2374 NULL,
2375 i386_stack_tramp_frame_sniffer
2376};
2377\f
6710bf39
SS
2378/* Generate a bytecode expression to get the value of the saved PC. */
2379
2380static void
2381i386_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
a3fcb948 2394
acd5c798
MK
2395/* Signal trampolines. */
2396
2397static struct i386_frame_cache *
10458914 2398i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
acd5c798 2399{
e17a4113
UW
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);
8fbca658 2403 volatile struct gdb_exception ex;
acd5c798 2404 struct i386_frame_cache *cache;
acd5c798 2405 CORE_ADDR addr;
63c0089f 2406 gdb_byte buf[4];
acd5c798
MK
2407
2408 if (*this_cache)
2409 return *this_cache;
2410
fd13a04a 2411 cache = i386_alloc_frame_cache ();
acd5c798 2412
8fbca658 2413 TRY_CATCH (ex, RETURN_MASK_ERROR)
a3386186 2414 {
8fbca658
PA
2415 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
2416 cache->base = extract_unsigned_integer (buf, 4, byte_order) - 4;
a3386186 2417
8fbca658
PA
2418 addr = tdep->sigcontext_addr (this_frame);
2419 if (tdep->sc_reg_offset)
2420 {
2421 int i;
a3386186 2422
8fbca658
PA
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;
a3386186 2436 }
8fbca658
PA
2437 if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
2438 throw_exception (ex);
acd5c798
MK
2439
2440 *this_cache = cache;
2441 return cache;
2442}
2443
8fbca658
PA
2444static enum unwind_stop_reason
2445i386_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
acd5c798 2457static void
10458914 2458i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
acd5c798
MK
2459 struct frame_id *this_id)
2460{
2461 struct i386_frame_cache *cache =
10458914 2462 i386_sigtramp_frame_cache (this_frame, this_cache);
acd5c798 2463
8fbca658 2464 if (!cache->base_p)
5ce0145d
PA
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 }
acd5c798
MK
2471}
2472
10458914
DJ
2473static struct value *
2474i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
2475 void **this_cache, int regnum)
acd5c798
MK
2476{
2477 /* Make sure we've initialized the cache. */
10458914 2478 i386_sigtramp_frame_cache (this_frame, this_cache);
acd5c798 2479
10458914 2480 return i386_frame_prev_register (this_frame, this_cache, regnum);
c906108c 2481}
c0d1d883 2482
10458914
DJ
2483static int
2484i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
2485 struct frame_info *this_frame,
2486 void **this_prologue_cache)
acd5c798 2487{
10458914 2488 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
acd5c798 2489
911bc6ee
MK
2490 /* We shouldn't even bother if we don't have a sigcontext_addr
2491 handler. */
2492 if (tdep->sigcontext_addr == NULL)
10458914 2493 return 0;
1c3545ae 2494
911bc6ee
MK
2495 if (tdep->sigtramp_p != NULL)
2496 {
10458914
DJ
2497 if (tdep->sigtramp_p (this_frame))
2498 return 1;
911bc6ee
MK
2499 }
2500
2501 if (tdep->sigtramp_start != 0)
2502 {
10458914 2503 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
2504
2505 gdb_assert (tdep->sigtramp_end != 0);
2506 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
10458914 2507 return 1;
911bc6ee 2508 }
acd5c798 2509
10458914 2510 return 0;
acd5c798 2511}
10458914
DJ
2512
2513static const struct frame_unwind i386_sigtramp_frame_unwind =
2514{
2515 SIGTRAMP_FRAME,
8fbca658 2516 i386_sigtramp_frame_unwind_stop_reason,
10458914
DJ
2517 i386_sigtramp_frame_this_id,
2518 i386_sigtramp_frame_prev_register,
2519 NULL,
2520 i386_sigtramp_frame_sniffer
2521};
acd5c798
MK
2522\f
2523
2524static CORE_ADDR
10458914 2525i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
acd5c798 2526{
10458914 2527 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798
MK
2528
2529 return cache->base;
2530}
2531
2532static 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
acd5c798 2540static struct frame_id
10458914 2541i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
acd5c798 2542{
acd5c798
MK
2543 CORE_ADDR fp;
2544
10458914 2545 fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
acd5c798 2546
3e210248 2547 /* See the end of i386_push_dummy_call. */
10458914 2548 return frame_id_build (fp + 8, get_frame_pc (this_frame));
c0d1d883 2549}
e04e5beb
JM
2550
2551/* _Decimal128 function return values need 16-byte alignment on the
2552 stack. */
2553
2554static CORE_ADDR
2555i386_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2556{
2557 return sp & -(CORE_ADDR)16;
2558}
fc338970 2559\f
c906108c 2560
fc338970
MK
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
8201327c 2563 structure from which we extract the address that we will land at.
28bcfd30 2564 This address is copied into PC. This routine returns non-zero on
436675d3 2565 success. */
c906108c 2566
8201327c 2567static int
60ade65d 2568i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
c906108c 2569{
436675d3 2570 gdb_byte buf[4];
c906108c 2571 CORE_ADDR sp, jb_addr;
20a6ec49 2572 struct gdbarch *gdbarch = get_frame_arch (frame);
e17a4113 2573 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
20a6ec49 2574 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
c906108c 2575
8201327c
MK
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)
c906108c
SS
2579 return 0;
2580
436675d3 2581 get_frame_register (frame, I386_ESP_REGNUM, buf);
e17a4113 2582 sp = extract_unsigned_integer (buf, 4, byte_order);
436675d3 2583 if (target_read_memory (sp + 4, buf, 4))
c906108c
SS
2584 return 0;
2585
e17a4113 2586 jb_addr = extract_unsigned_integer (buf, 4, byte_order);
436675d3 2587 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
8201327c 2588 return 0;
c906108c 2589
e17a4113 2590 *pc = extract_unsigned_integer (buf, 4, byte_order);
c906108c
SS
2591 return 1;
2592}
fc338970 2593\f
c906108c 2594
7ccc1c74
JM
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
2600static int
2601i386_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
a9b8d892
JK
2623/* Implementation for set_gdbarch_push_dummy_code. */
2624
2625static CORE_ADDR
2626i386_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
3a1e71e3 2639static CORE_ADDR
7d9b040b 2640i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
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)
22f8ba57 2644{
e17a4113 2645 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
63c0089f 2646 gdb_byte buf[4];
acd5c798 2647 int i;
7ccc1c74
JM
2648 int write_pass;
2649 int args_space = 0;
acd5c798 2650
7ccc1c74
JM
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++)
22f8ba57 2656 {
7ccc1c74 2657 int args_space_used = 0;
7ccc1c74
JM
2658
2659 if (struct_return)
2660 {
2661 if (write_pass)
2662 {
2663 /* Push value address. */
e17a4113 2664 store_unsigned_integer (buf, 4, byte_order, struct_addr);
7ccc1c74
JM
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]));
acd5c798 2675
7ccc1c74
JM
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);
acd5c798 2680
7ccc1c74
JM
2681 write_memory (sp + args_space_used,
2682 value_contents_all (args[i]), len);
2683 /* The System V ABI says that:
acd5c798 2684
7ccc1c74
JM
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."
22f8ba57 2688
7ccc1c74
JM
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])))
284c5a60 2695 args_space = align_up (args_space, 16);
7ccc1c74
JM
2696 args_space += align_up (len, 4);
2697 }
2698 }
2699
2700 if (!write_pass)
2701 {
7ccc1c74 2702 sp -= args_space;
284c5a60
MK
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;
7ccc1c74 2709 }
22f8ba57
MK
2710 }
2711
acd5c798
MK
2712 /* Store return address. */
2713 sp -= 4;
e17a4113 2714 store_unsigned_integer (buf, 4, byte_order, bp_addr);
acd5c798
MK
2715 write_memory (sp, buf, 4);
2716
2717 /* Finally, update the stack pointer... */
e17a4113 2718 store_unsigned_integer (buf, 4, byte_order, sp);
acd5c798
MK
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
3e210248
AC
2724 /* MarkK wrote: This "+ 8" is all over the place:
2725 (i386_frame_this_id, i386_sigtramp_frame_this_id,
10458914 2726 i386_dummy_id). It's there, since all frame unwinders for
3e210248 2727 a given target have to agree (within a certain margin) on the
a45ae3ed
UW
2728 definition of the stack address of a frame. Otherwise frame id
2729 comparison might not work correctly. Since DWARF2/GCC uses the
3e210248
AC
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;
22f8ba57
MK
2734}
2735
1a309862
MK
2736/* These registers are used for returning integers (and on some
2737 targets also for returning `struct' and `union' values when their
ef9dff19 2738 size and alignment match an integer type). */
acd5c798
MK
2739#define LOW_RETURN_REGNUM I386_EAX_REGNUM /* %eax */
2740#define HIGH_RETURN_REGNUM I386_EDX_REGNUM /* %edx */
1a309862 2741
c5e656c1
MK
2742/* Read, for architecture GDBARCH, a function return value of TYPE
2743 from REGCACHE, and copy that into VALBUF. */
1a309862 2744
3a1e71e3 2745static void
c5e656c1 2746i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
63c0089f 2747 struct regcache *regcache, gdb_byte *valbuf)
c906108c 2748{
c5e656c1 2749 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1a309862 2750 int len = TYPE_LENGTH (type);
63c0089f 2751 gdb_byte buf[I386_MAX_REGISTER_SIZE];
1a309862 2752
1e8d0a7b 2753 if (TYPE_CODE (type) == TYPE_CODE_FLT)
c906108c 2754 {
5716833c 2755 if (tdep->st0_regnum < 0)
1a309862 2756 {
8a3fe4f8 2757 warning (_("Cannot find floating-point return value."));
1a309862 2758 memset (valbuf, 0, len);
ef9dff19 2759 return;
1a309862
MK
2760 }
2761
c6ba6f0d
MK
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. */
acd5c798 2766 regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
27067745 2767 convert_typed_floating (buf, i387_ext_type (gdbarch), valbuf, type);
c906108c
SS
2768 }
2769 else
c5aa993b 2770 {
875f8d0e
UW
2771 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2772 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
d4f3574e
SS
2773
2774 if (len <= low_size)
00f8375e 2775 {
0818c12a 2776 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e
MK
2777 memcpy (valbuf, buf, len);
2778 }
d4f3574e
SS
2779 else if (len <= (low_size + high_size))
2780 {
0818c12a 2781 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e 2782 memcpy (valbuf, buf, low_size);
0818c12a 2783 regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
63c0089f 2784 memcpy (valbuf + low_size, buf, len - low_size);
d4f3574e
SS
2785 }
2786 else
8e65ff28 2787 internal_error (__FILE__, __LINE__,
1777feb0
MS
2788 _("Cannot extract return value of %d bytes long."),
2789 len);
c906108c
SS
2790 }
2791}
2792
c5e656c1
MK
2793/* Write, for architecture GDBARCH, a function return value of TYPE
2794 from VALBUF into REGCACHE. */
ef9dff19 2795
3a1e71e3 2796static void
c5e656c1 2797i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
63c0089f 2798 struct regcache *regcache, const gdb_byte *valbuf)
ef9dff19 2799{
c5e656c1 2800 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
ef9dff19
MK
2801 int len = TYPE_LENGTH (type);
2802
1e8d0a7b 2803 if (TYPE_CODE (type) == TYPE_CODE_FLT)
ef9dff19 2804 {
3d7f4f49 2805 ULONGEST fstat;
63c0089f 2806 gdb_byte buf[I386_MAX_REGISTER_SIZE];
ccb945b8 2807
5716833c 2808 if (tdep->st0_regnum < 0)
ef9dff19 2809 {
8a3fe4f8 2810 warning (_("Cannot set floating-point return value."));
ef9dff19
MK
2811 return;
2812 }
2813
635b0cc1
MK
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
c6ba6f0d
MK
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. */
27067745 2822 convert_typed_floating (valbuf, type, buf, i387_ext_type (gdbarch));
acd5c798 2823 regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
ccb945b8 2824
635b0cc1
MK
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. */
20a6ec49 2829 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
ccb945b8 2830 fstat |= (7 << 11);
20a6ec49 2831 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
ccb945b8 2832
635b0cc1
MK
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. */
20a6ec49 2836 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
ef9dff19
MK
2837 }
2838 else
2839 {
875f8d0e
UW
2840 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
2841 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
ef9dff19
MK
2842
2843 if (len <= low_size)
3d7f4f49 2844 regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
ef9dff19
MK
2845 else if (len <= (low_size + high_size))
2846 {
3d7f4f49
MK
2847 regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
2848 regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
63c0089f 2849 len - low_size, valbuf + low_size);
ef9dff19
MK
2850 }
2851 else
8e65ff28 2852 internal_error (__FILE__, __LINE__,
e2e0b3e5 2853 _("Cannot store return value of %d bytes long."), len);
ef9dff19
MK
2854 }
2855}
fc338970 2856\f
ef9dff19 2857
8201327c
MK
2858/* This is the variable that is set with "set struct-convention", and
2859 its legitimate values. */
2860static const char default_struct_convention[] = "default";
2861static const char pcc_struct_convention[] = "pcc";
2862static const char reg_struct_convention[] = "reg";
40478521 2863static const char *const valid_conventions[] =
8201327c
MK
2864{
2865 default_struct_convention,
2866 pcc_struct_convention,
2867 reg_struct_convention,
2868 NULL
2869};
2870static const char *struct_convention = default_struct_convention;
2871
0e4377e1
JB
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. */
c5e656c1 2875
8201327c 2876static int
c5e656c1 2877i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
8201327c 2878{
c5e656c1
MK
2879 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2880 enum type_code code = TYPE_CODE (type);
2881 int len = TYPE_LENGTH (type);
8201327c 2882
0e4377e1
JB
2883 gdb_assert (code == TYPE_CODE_STRUCT
2884 || code == TYPE_CODE_UNION
2885 || code == TYPE_CODE_ARRAY);
c5e656c1
MK
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
9edde48e
MK
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
c5e656c1
MK
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
2910static enum return_value_convention
6a3a010b 2911i386_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
2912 struct type *type, struct regcache *regcache,
2913 gdb_byte *readbuf, const gdb_byte *writebuf)
c5e656c1
MK
2914{
2915 enum type_code code = TYPE_CODE (type);
2916
5daa78cc
TJB
2917 if (((code == TYPE_CODE_STRUCT
2918 || code == TYPE_CODE_UNION
2919 || code == TYPE_CODE_ARRAY)
2920 && !i386_reg_struct_return_p (gdbarch, type))
2445fd7b
MK
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)
5daa78cc
TJB
2924 /* 128-bit decimal float uses the struct return convention. */
2925 || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
31db7b6c
MK
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
0e4377e1
JB
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
31db7b6c
MK
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 }
c5e656c1
MK
2954
2955 /* This special case is for structures consisting of a single
9edde48e
MK
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. */
c5e656c1
MK
2962 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
2963 {
2964 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
6a3a010b 2965 return i386_return_value (gdbarch, function, type, regcache,
c055b101 2966 readbuf, writebuf);
c5e656c1
MK
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);
8201327c 2973
c5e656c1 2974 return RETURN_VALUE_REGISTER_CONVENTION;
8201327c
MK
2975}
2976\f
2977
27067745
UW
2978struct type *
2979i387_ext_type (struct gdbarch *gdbarch)
2980{
2981 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2982
2983 if (!tdep->i387_ext_type)
90884b2b
L
2984 {
2985 tdep->i387_ext_type = tdesc_find_type (gdbarch, "i387_ext");
2986 gdb_assert (tdep->i387_ext_type != NULL);
2987 }
27067745
UW
2988
2989 return tdep->i387_ext_type;
2990}
2991
1dbcd68c
WT
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
2996static struct type *
2997i386_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
01f9f808
MS
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
3032static struct type *
3033i386_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
c131fcee
L
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
3085static struct type *
3086i386_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;
0c5acf93 3128 TYPE_NAME (t) = "builtin_type_vec256i";
c131fcee
L
3129 tdep->i386_ymm_type = t;
3130 }
3131
3132 return tdep->i386_ymm_type;
3133}
3134
794ac428 3135/* Construct vector type for MMX registers. */
90884b2b 3136static struct type *
794ac428
UW
3137i386_mmx_type (struct gdbarch *gdbarch)
3138{
3139 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3140
3141 if (!tdep->i386_mmx_type)
3142 {
df4df182
UW
3143 const struct builtin_type *bt = builtin_type (gdbarch);
3144
794ac428
UW
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
e9bb382b
UW
3158 t = arch_composite_type (gdbarch,
3159 "__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
df4df182
UW
3160
3161 append_composite_type_field (t, "uint64", bt->builtin_int64);
794ac428 3162 append_composite_type_field (t, "v2_int32",
df4df182 3163 init_vector_type (bt->builtin_int32, 2));
794ac428 3164 append_composite_type_field (t, "v4_int16",
df4df182 3165 init_vector_type (bt->builtin_int16, 4));
794ac428 3166 append_composite_type_field (t, "v8_int8",
df4df182 3167 init_vector_type (bt->builtin_int8, 8));
794ac428 3168
876cecd0 3169 TYPE_VECTOR (t) = 1;
794ac428
UW
3170 TYPE_NAME (t) = "builtin_type_vec64i";
3171 tdep->i386_mmx_type = t;
3172 }
3173
3174 return tdep->i386_mmx_type;
3175}
3176
d7a0d72c 3177/* Return the GDB type object for the "standard" data type of data in
1777feb0 3178 register REGNUM. */
d7a0d72c 3179
fff4548b 3180struct type *
90884b2b 3181i386_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
d7a0d72c 3182{
1dbcd68c
WT
3183 if (i386_bnd_regnum_p (gdbarch, regnum))
3184 return i386_bnd_type (gdbarch);
1ba53b71
L
3185 if (i386_mmx_regnum_p (gdbarch, regnum))
3186 return i386_mmx_type (gdbarch);
c131fcee
L
3187 else if (i386_ymm_regnum_p (gdbarch, regnum))
3188 return i386_ymm_type (gdbarch);
01f9f808
MS
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);
1ba53b71
L
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;
01f9f808
MS
3202 else if (i386_k_regnum_p (gdbarch, regnum))
3203 return bt->builtin_int64;
1ba53b71
L
3204 }
3205
3206 internal_error (__FILE__, __LINE__, _("invalid regnum"));
d7a0d72c
MK
3207}
3208
28fc6740 3209/* Map a cooked register onto a raw register or memory. For the i386,
acd5c798 3210 the MMX registers need to be mapped onto floating point registers. */
28fc6740
AC
3211
3212static int
c86c27af 3213i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
28fc6740 3214{
5716833c
MK
3215 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
3216 int mmxreg, fpreg;
28fc6740
AC
3217 ULONGEST fstat;
3218 int tos;
c86c27af 3219
5716833c 3220 mmxreg = regnum - tdep->mm0_regnum;
20a6ec49 3221 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
28fc6740 3222 tos = (fstat >> 11) & 0x7;
5716833c
MK
3223 fpreg = (mmxreg + tos) % 8;
3224
20a6ec49 3225 return (I387_ST0_REGNUM (tdep) + fpreg);
28fc6740
AC
3226}
3227
3543a589
TT
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
3232void
3233i386_pseudo_register_read_into_value (struct gdbarch *gdbarch,
3234 struct regcache *regcache,
3235 int regnum,
3236 struct value *result_value)
28fc6740 3237{
1ba53b71 3238 gdb_byte raw_buf[MAX_REGISTER_SIZE];
05d1431c 3239 enum register_status status;
3543a589 3240 gdb_byte *buf = value_contents_raw (result_value);
1ba53b71 3241
5716833c 3242 if (i386_mmx_regnum_p (gdbarch, regnum))
28fc6740 3243 {
c86c27af
MK
3244 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
3245
28fc6740 3246 /* Extract (always little endian). */
05d1431c
PA
3247 status = regcache_raw_read (regcache, fpnum, raw_buf);
3248 if (status != REG_VALID)
3543a589
TT
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));
28fc6740
AC
3253 }
3254 else
1ba53b71
L
3255 {
3256 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1dbcd68c
WT
3257 if (i386_bnd_regnum_p (gdbarch, regnum))
3258 {
3259 regnum -= tdep->bnd0_regnum;
1ba53b71 3260
1dbcd68c
WT
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 }
01f9f808
MS
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 }
1dbcd68c 3350 else if (i386_ymm_regnum_p (gdbarch, regnum))
c131fcee
L
3351 {
3352 regnum -= tdep->ymm0_regnum;
3353
1777feb0 3354 /* Extract (always little endian). Read lower 128bits. */
05d1431c
PA
3355 status = regcache_raw_read (regcache,
3356 I387_XMM0_REGNUM (tdep) + regnum,
3357 raw_buf);
3358 if (status != REG_VALID)
3543a589
TT
3359 mark_value_bytes_unavailable (result_value, 0, 16);
3360 else
3361 memcpy (buf, raw_buf, 16);
c131fcee 3362 /* Read upper 128bits. */
05d1431c
PA
3363 status = regcache_raw_read (regcache,
3364 tdep->ymm0h_regnum + regnum,
3365 raw_buf);
3366 if (status != REG_VALID)
3543a589
TT
3367 mark_value_bytes_unavailable (result_value, 16, 32);
3368 else
3369 memcpy (buf + 16, raw_buf, 16);
c131fcee 3370 }
01f9f808
MS
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 }
c131fcee 3391 else if (i386_word_regnum_p (gdbarch, regnum))
1ba53b71
L
3392 {
3393 int gpnum = regnum - tdep->ax_regnum;
3394
3395 /* Extract (always little endian). */
05d1431c
PA
3396 status = regcache_raw_read (regcache, gpnum, raw_buf);
3397 if (status != REG_VALID)
3543a589
TT
3398 mark_value_bytes_unavailable (result_value, 0,
3399 TYPE_LENGTH (value_type (result_value)));
3400 else
3401 memcpy (buf, raw_buf, 2);
1ba53b71
L
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. */
05d1431c
PA
3412 status = regcache_raw_read (regcache, gpnum % 4, raw_buf);
3413 if (status != REG_VALID)
3543a589
TT
3414 mark_value_bytes_unavailable (result_value, 0,
3415 TYPE_LENGTH (value_type (result_value)));
3416 else if (gpnum >= 4)
1ba53b71
L
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 }
3543a589
TT
3424}
3425
3426static struct value *
3427i386_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);
05d1431c 3438
3543a589 3439 return result;
28fc6740
AC
3440}
3441
1ba53b71 3442void
28fc6740 3443i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
42835c2b 3444 int regnum, const gdb_byte *buf)
28fc6740 3445{
1ba53b71
L
3446 gdb_byte raw_buf[MAX_REGISTER_SIZE];
3447
5716833c 3448 if (i386_mmx_regnum_p (gdbarch, regnum))
28fc6740 3449 {
c86c27af
MK
3450 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
3451
28fc6740 3452 /* Read ... */
1ba53b71 3453 regcache_raw_read (regcache, fpnum, raw_buf);
28fc6740 3454 /* ... Modify ... (always little endian). */
1ba53b71 3455 memcpy (raw_buf, buf, register_size (gdbarch, regnum));
28fc6740 3456 /* ... Write. */
1ba53b71 3457 regcache_raw_write (regcache, fpnum, raw_buf);
28fc6740
AC
3458 }
3459 else
1ba53b71
L
3460 {
3461 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3462
1dbcd68c
WT
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 }
01f9f808
MS
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 }
1dbcd68c 3531 else if (i386_ymm_regnum_p (gdbarch, regnum))
c131fcee
L
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 }
01f9f808
MS
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 }
c131fcee 3557 else if (i386_word_regnum_p (gdbarch, regnum))
1ba53b71
L
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 }
28fc6740 3588}
ff2e87ac
AC
3589\f
3590
ff2e87ac
AC
3591/* Return the register number of the register allocated by GCC after
3592 REGNUM, or -1 if there is no such register. */
3593
3594static int
3595i386_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
de5b9bb9 3614 if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
ff2e87ac 3615 return next_regnum[regnum];
28fc6740 3616
ff2e87ac
AC
3617 return -1;
3618}
3619
3620/* Return nonzero if a value of type TYPE stored in register REGNUM
3621 needs any special handling. */
d7a0d72c 3622
3a1e71e3 3623static int
1777feb0
MS
3624i386_convert_register_p (struct gdbarch *gdbarch,
3625 int regnum, struct type *type)
d7a0d72c 3626{
de5b9bb9
MK
3627 int len = TYPE_LENGTH (type);
3628
ff2e87ac
AC
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
de5b9bb9
MK
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 }
ff2e87ac 3647
0abe36f5 3648 return i387_convert_register_p (gdbarch, regnum, type);
d7a0d72c
MK
3649}
3650
ff2e87ac
AC
3651/* Read a value of type TYPE from register REGNUM in frame FRAME, and
3652 return its contents in TO. */
ac27f131 3653
8dccd430 3654static int
ff2e87ac 3655i386_register_to_value (struct frame_info *frame, int regnum,
8dccd430
PA
3656 struct type *type, gdb_byte *to,
3657 int *optimizedp, int *unavailablep)
ac27f131 3658{
20a6ec49 3659 struct gdbarch *gdbarch = get_frame_arch (frame);
de5b9bb9 3660 int len = TYPE_LENGTH (type);
de5b9bb9 3661
20a6ec49 3662 if (i386_fp_regnum_p (gdbarch, regnum))
8dccd430
PA
3663 return i387_register_to_value (frame, regnum, type, to,
3664 optimizedp, unavailablep);
ff2e87ac 3665
fd35795f 3666 /* Read a value spread across multiple registers. */
de5b9bb9
MK
3667
3668 gdb_assert (len > 4 && len % 4 == 0);
3d261580 3669
de5b9bb9
MK
3670 while (len > 0)
3671 {
3672 gdb_assert (regnum != -1);
20a6ec49 3673 gdb_assert (register_size (gdbarch, regnum) == 4);
d532c08f 3674
8dccd430
PA
3675 if (!get_frame_register_bytes (frame, regnum, 0,
3676 register_size (gdbarch, regnum),
3677 to, optimizedp, unavailablep))
3678 return 0;
3679
de5b9bb9
MK
3680 regnum = i386_next_regnum (regnum);
3681 len -= 4;
42835c2b 3682 to += 4;
de5b9bb9 3683 }
8dccd430
PA
3684
3685 *optimizedp = *unavailablep = 0;
3686 return 1;
ac27f131
MK
3687}
3688
ff2e87ac
AC
3689/* Write the contents FROM of a value of type TYPE into register
3690 REGNUM in frame FRAME. */
ac27f131 3691
3a1e71e3 3692static void
ff2e87ac 3693i386_value_to_register (struct frame_info *frame, int regnum,
42835c2b 3694 struct type *type, const gdb_byte *from)
ac27f131 3695{
de5b9bb9 3696 int len = TYPE_LENGTH (type);
de5b9bb9 3697
20a6ec49 3698 if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
c6ba6f0d 3699 {
d532c08f
MK
3700 i387_value_to_register (frame, regnum, type, from);
3701 return;
3702 }
3d261580 3703
fd35795f 3704 /* Write a value spread across multiple registers. */
de5b9bb9
MK
3705
3706 gdb_assert (len > 4 && len % 4 == 0);
ff2e87ac 3707
de5b9bb9
MK
3708 while (len > 0)
3709 {
3710 gdb_assert (regnum != -1);
875f8d0e 3711 gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
d532c08f 3712
42835c2b 3713 put_frame_register (frame, regnum, from);
de5b9bb9
MK
3714 regnum = i386_next_regnum (regnum);
3715 len -= 4;
42835c2b 3716 from += 4;
de5b9bb9 3717 }
ac27f131 3718}
ff2e87ac 3719\f
7fdafb5a
MK
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. */
ff2e87ac 3723
20187ed5 3724void
473f17b0
MK
3725i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
3726 int regnum, const void *gregs, size_t len)
3727{
09424cff
AA
3728 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3729 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
156cdbee 3730 const gdb_byte *regs = gregs;
473f17b0
MK
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
7fdafb5a
MK
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
ecc37a5a 3748static void
7fdafb5a
MK
3749i386_collect_gregset (const struct regset *regset,
3750 const struct regcache *regcache,
3751 int regnum, void *gregs, size_t len)
3752{
09424cff
AA
3753 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3754 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
156cdbee 3755 gdb_byte *regs = gregs;
7fdafb5a
MK
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. */
473f17b0
MK
3771
3772static void
3773i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
3774 int regnum, const void *fpregs, size_t len)
3775{
09424cff
AA
3776 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3777 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
473f17b0 3778
66a72d25
MK
3779 if (len == I387_SIZEOF_FXSAVE)
3780 {
3781 i387_supply_fxsave (regcache, regnum, fpregs);
3782 return;
3783 }
3784
473f17b0
MK
3785 gdb_assert (len == tdep->sizeof_fpregset);
3786 i387_supply_fsave (regcache, regnum, fpregs);
3787}
8446b36a 3788
2f305df1
MK
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. */
7fdafb5a
MK
3793
3794static void
3795i386_collect_fpregset (const struct regset *regset,
3796 const struct regcache *regcache,
3797 int regnum, void *fpregs, size_t len)
3798{
09424cff
AA
3799 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3800 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7fdafb5a
MK
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
c131fcee
L
3812/* Similar to i386_supply_fpregset, but use XSAVE extended state. */
3813
3814static void
3815i386_supply_xstateregset (const struct regset *regset,
3816 struct regcache *regcache, int regnum,
3817 const void *xstateregs, size_t len)
3818{
c131fcee
L
3819 i387_supply_xsave (regcache, regnum, xstateregs);
3820}
3821
3822/* Similar to i386_collect_fpregset , but use XSAVE extended state. */
3823
3824static void
3825i386_collect_xstateregset (const struct regset *regset,
3826 const struct regcache *regcache,
3827 int regnum, void *xstateregs, size_t len)
3828{
c131fcee
L
3829 i387_collect_xsave (regcache, regnum, xstateregs, 1);
3830}
3831
ecc37a5a
AA
3832/* Register set definitions. */
3833
3834const struct regset i386_gregset =
3835 {
3836 NULL, i386_supply_gregset, i386_collect_gregset
3837 };
3838
3839static const struct regset i386_fpregset =
3840 {
3841 NULL, i386_supply_fpregset, i386_collect_fpregset
3842 };
3843
3844static const struct regset i386_xstateregset =
3845 {
3846 NULL, i386_supply_xstateregset, i386_collect_xstateregset
3847 };
3848
8446b36a
MK
3849/* Return the appropriate register set for the core section identified
3850 by SECT_NAME and SECT_SIZE. */
3851
3852const struct regset *
3853i386_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)
ecc37a5a 3859 return &i386_gregset;
8446b36a 3860
66a72d25
MK
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))
ecc37a5a 3864 return &i386_fpregset;
8446b36a 3865
c131fcee 3866 if (strcmp (sect_name, ".reg-xstate") == 0)
ecc37a5a 3867 return &i386_xstateregset;
c131fcee 3868
8446b36a
MK
3869 return NULL;
3870}
473f17b0 3871\f
fc338970 3872
fc338970 3873/* Stuff for WIN32 PE style DLL's but is pretty generic really. */
c906108c
SS
3874
3875CORE_ADDR
e17a4113
UW
3876i386_pe_skip_trampoline_code (struct frame_info *frame,
3877 CORE_ADDR pc, char *name)
c906108c 3878{
e17a4113
UW
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)
c906108c 3884 {
e17a4113
UW
3885 unsigned long indirect =
3886 read_memory_unsigned_integer (pc + 2, 4, byte_order);
c906108c 3887 struct minimal_symbol *indsym =
7cbd4a93 3888 indirect ? lookup_minimal_symbol_by_pc (indirect).minsym : 0;
efd66ac6 3889 const char *symname = indsym ? MSYMBOL_LINKAGE_NAME (indsym) : 0;
c906108c 3890
c5aa993b 3891 if (symname)
c906108c 3892 {
c5aa993b
JM
3893 if (strncmp (symname, "__imp_", 6) == 0
3894 || strncmp (symname, "_imp_", 5) == 0)
e17a4113
UW
3895 return name ? 1 :
3896 read_memory_unsigned_integer (indirect, 4, byte_order);
c906108c
SS
3897 }
3898 }
fc338970 3899 return 0; /* Not a trampoline. */
c906108c 3900}
fc338970
MK
3901\f
3902
10458914
DJ
3903/* Return whether the THIS_FRAME corresponds to a sigtramp
3904 routine. */
8201327c 3905
4bd207ef 3906int
10458914 3907i386_sigtramp_p (struct frame_info *this_frame)
8201327c 3908{
10458914 3909 CORE_ADDR pc = get_frame_pc (this_frame);
2c02bd72 3910 const char *name;
911bc6ee
MK
3911
3912 find_pc_partial_function (pc, &name, NULL, NULL);
8201327c
MK
3913 return (name && strcmp ("_sigtramp", name) == 0);
3914}
3915\f
3916
fc338970
MK
3917/* We have two flavours of disassembly. The machinery on this page
3918 deals with switching between those. */
c906108c
SS
3919
3920static int
a89aa300 3921i386_print_insn (bfd_vma pc, struct disassemble_info *info)
c906108c 3922{
5e3397bb
MK
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;
5e3397bb
MK
3929
3930 return print_insn_i386 (pc, info);
7a292a7a 3931}
fc338970 3932\f
3ce1502b 3933
8201327c
MK
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. */
3ce1502b 3938
8201327c 3939/* System V Release 4 (SVR4). */
3ce1502b 3940
10458914
DJ
3941/* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
3942 routine. */
911bc6ee 3943
8201327c 3944static int
10458914 3945i386_svr4_sigtramp_p (struct frame_info *this_frame)
d2a7c97a 3946{
10458914 3947 CORE_ADDR pc = get_frame_pc (this_frame);
2c02bd72 3948 const char *name;
911bc6ee 3949
05b4bd79 3950 /* The origin of these symbols is currently unknown. */
911bc6ee 3951 find_pc_partial_function (pc, &name, NULL, NULL);
8201327c 3952 return (name && (strcmp ("_sigreturn", name) == 0
8201327c
MK
3953 || strcmp ("sigvechandler", name) == 0));
3954}
d2a7c97a 3955
10458914
DJ
3956/* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
3957 address of the associated sigcontext (ucontext) structure. */
3ce1502b 3958
3a1e71e3 3959static CORE_ADDR
10458914 3960i386_svr4_sigcontext_addr (struct frame_info *this_frame)
8201327c 3961{
e17a4113
UW
3962 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3963 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
63c0089f 3964 gdb_byte buf[4];
acd5c798 3965 CORE_ADDR sp;
3ce1502b 3966
10458914 3967 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
e17a4113 3968 sp = extract_unsigned_integer (buf, 4, byte_order);
21d0e8a4 3969
e17a4113 3970 return read_memory_unsigned_integer (sp + 8, 4, byte_order);
8201327c 3971}
55aa24fb
SDJ
3972
3973\f
3974
3975/* Implementation of `gdbarch_stap_is_single_operand', as defined in
3976 gdbarch.h. */
3977
3978int
3979i386_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
5acfdbae
SDJ
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
3995static int
3996i386_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
d7b30f67
SDJ
4021 if (!isdigit ((unsigned char) *s))
4022 return 0;
4023
5acfdbae
SDJ
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
d7b30f67
SDJ
4042 if (!isdigit ((unsigned char) *s))
4043 return 0;
4044
5acfdbae
SDJ
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
d7b30f67
SDJ
4063 if (!isdigit ((unsigned char) *s))
4064 return 0;
4065
5acfdbae
SDJ
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
d7b30f67 4081 len = s - start - 1;
5acfdbae
SDJ
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 {
410a0ff2
SDJ
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);
5acfdbae 4098 if (got_minus[i])
410a0ff2 4099 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
5acfdbae
SDJ
4100 }
4101
410a0ff2 4102 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae
SDJ
4103 str.ptr = regname;
4104 str.length = len;
410a0ff2
SDJ
4105 write_exp_string (&p->pstate, str);
4106 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae 4107
410a0ff2
SDJ
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);
5acfdbae 4112
410a0ff2
SDJ
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);
5acfdbae 4116
410a0ff2
SDJ
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);
5acfdbae 4121
410a0ff2 4122 write_exp_elt_opcode (&p->pstate, UNOP_IND);
5acfdbae
SDJ
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
4141static int
4142i386_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 {
410a0ff2
SDJ
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);
5acfdbae 4247 if (offset_minus)
410a0ff2 4248 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
5acfdbae
SDJ
4249 }
4250
410a0ff2 4251 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae
SDJ
4252 base_token.ptr = base;
4253 base_token.length = len_base;
410a0ff2
SDJ
4254 write_exp_string (&p->pstate, base_token);
4255 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae
SDJ
4256
4257 if (offset)
410a0ff2 4258 write_exp_elt_opcode (&p->pstate, BINOP_ADD);
5acfdbae 4259
410a0ff2 4260 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae
SDJ
4261 index_token.ptr = index;
4262 index_token.length = len_index;
410a0ff2
SDJ
4263 write_exp_string (&p->pstate, index_token);
4264 write_exp_elt_opcode (&p->pstate, OP_REGISTER);
5acfdbae
SDJ
4265
4266 if (size)
4267 {
410a0ff2
SDJ
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);
5acfdbae 4273 if (size_minus)
410a0ff2
SDJ
4274 write_exp_elt_opcode (&p->pstate, UNOP_NEG);
4275 write_exp_elt_opcode (&p->pstate, BINOP_MUL);
5acfdbae
SDJ
4276 }
4277
410a0ff2 4278 write_exp_elt_opcode (&p->pstate, BINOP_ADD);
5acfdbae 4279
410a0ff2
SDJ
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);
5acfdbae 4284
410a0ff2 4285 write_exp_elt_opcode (&p->pstate, UNOP_IND);
5acfdbae
SDJ
4286
4287 p->arg = s;
4288
4289 return 1;
4290 }
4291
4292 return 0;
4293}
4294
55aa24fb
SDJ
4295/* Implementation of `gdbarch_stap_parse_special_token', as defined in
4296 gdbarch.h. */
4297
4298int
4299i386_stap_parse_special_token (struct gdbarch *gdbarch,
4300 struct stap_parse_info *p)
4301{
55aa24fb
SDJ
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 {
55aa24fb
SDJ
4323 switch (current_state)
4324 {
4325 case TRIPLET:
5acfdbae
SDJ
4326 if (i386_stap_parse_special_token_triplet (gdbarch, p))
4327 return 1;
4328 break;
4329
55aa24fb 4330 case THREE_ARG_DISPLACEMENT:
5acfdbae
SDJ
4331 if (i386_stap_parse_special_token_three_arg_disp (gdbarch, p))
4332 return 1;
4333 break;
55aa24fb
SDJ
4334 }
4335
4336 /* Advancing to the next state. */
4337 ++current_state;
4338 }
4339
4340 return 0;
4341}
4342
8201327c 4343\f
3ce1502b 4344
8201327c 4345/* Generic ELF. */
d2a7c97a 4346
8201327c
MK
4347void
4348i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4349{
05c0465e
SDJ
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
c4fc7f1b
MK
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);
55aa24fb
SDJ
4359
4360 /* Registering SystemTap handlers. */
05c0465e
SDJ
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);
55aa24fb
SDJ
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);
8201327c 4371}
3ce1502b 4372
8201327c 4373/* System V Release 4 (SVR4). */
3ce1502b 4374
8201327c
MK
4375void
4376i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
4377{
4378 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 4379
8201327c
MK
4380 /* System V Release 4 uses ELF. */
4381 i386_elf_init_abi (info, gdbarch);
3ce1502b 4382
dfe01d39 4383 /* System V Release 4 has shared libraries. */
dfe01d39
MK
4384 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
4385
911bc6ee 4386 tdep->sigtramp_p = i386_svr4_sigtramp_p;
21d0e8a4 4387 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
acd5c798
MK
4388 tdep->sc_pc_offset = 36 + 14 * 4;
4389 tdep->sc_sp_offset = 36 + 17 * 4;
3ce1502b 4390
8201327c 4391 tdep->jb_pc_offset = 20;
3ce1502b
MK
4392}
4393
8201327c 4394/* DJGPP. */
3ce1502b 4395
3a1e71e3 4396static void
8201327c 4397i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
3ce1502b 4398{
8201327c 4399 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 4400
911bc6ee
MK
4401 /* DJGPP doesn't have any special frames for signal handlers. */
4402 tdep->sigtramp_p = NULL;
3ce1502b 4403
8201327c 4404 tdep->jb_pc_offset = 36;
15430fc0
EZ
4405
4406 /* DJGPP does not support the SSE registers. */
3a13a53b
L
4407 if (! tdesc_has_registers (info.target_desc))
4408 tdep->tdesc = tdesc_i386_mmx;
3d22076f
EZ
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);
ab38a727
PA
4416
4417 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
3ce1502b 4418}
8201327c 4419\f
2acceee2 4420
38c968cf
AC
4421/* i386 register groups. In addition to the normal groups, add "mmx"
4422 and "sse". */
4423
4424static struct reggroup *i386_sse_reggroup;
4425static struct reggroup *i386_mmx_reggroup;
4426
4427static void
4428i386_init_reggroups (void)
4429{
4430 i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
4431 i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
4432}
4433
4434static void
4435i386_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
4448int
4449i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
4450 struct reggroup *group)
4451{
c131fcee
L
4452 const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4453 int fp_regnum_p, mmx_regnum_p, xmm_regnum_p, mxcsr_regnum_p,
01f9f808
MS
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;
acd5c798 4458
1ba53b71
L
4459 /* Don't include pseudo registers, except for MMX, in any register
4460 groups. */
c131fcee 4461 if (i386_byte_regnum_p (gdbarch, regnum))
1ba53b71
L
4462 return 0;
4463
c131fcee 4464 if (i386_word_regnum_p (gdbarch, regnum))
1ba53b71
L
4465 return 0;
4466
c131fcee 4467 if (i386_dword_regnum_p (gdbarch, regnum))
1ba53b71
L
4468 return 0;
4469
4470 mmx_regnum_p = i386_mmx_regnum_p (gdbarch, regnum);
38c968cf
AC
4471 if (group == i386_mmx_reggroup)
4472 return mmx_regnum_p;
1ba53b71 4473
c131fcee 4474 xmm_regnum_p = i386_xmm_regnum_p (gdbarch, regnum);
01f9f808 4475 xmm_avx512_regnum_p = i386_xmm_avx512_regnum_p (gdbarch, regnum);
c131fcee 4476 mxcsr_regnum_p = i386_mxcsr_regnum_p (gdbarch, regnum);
38c968cf 4477 if (group == i386_sse_reggroup)
01f9f808 4478 return xmm_regnum_p || xmm_avx512_regnum_p || mxcsr_regnum_p;
c131fcee
L
4479
4480 ymm_regnum_p = i386_ymm_regnum_p (gdbarch, regnum);
01f9f808
MS
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
38c968cf 4491 if (group == vector_reggroup)
c131fcee 4492 return (mmx_regnum_p
01f9f808
MS
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);
1ba53b71
L
4497
4498 fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
4499 || i386_fpc_regnum_p (gdbarch, regnum));
38c968cf
AC
4500 if (group == float_reggroup)
4501 return fp_regnum_p;
1ba53b71 4502
c131fcee
L
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);
01f9f808
MS
4506 ymmh_avx512_regnum_p = i386_ymmh_avx512_regnum_p (gdbarch, regnum);
4507 zmmh_regnum_p = i386_zmmh_regnum_p (gdbarch, regnum);
c131fcee 4508 if (group == all_reggroup
01f9f808
MS
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))
c131fcee
L
4514 return 0;
4515
1dbcd68c
WT
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
38c968cf 4531 if (group == general_reggroup)
1ba53b71
L
4532 return (!fp_regnum_p
4533 && !mmx_regnum_p
c131fcee
L
4534 && !mxcsr_regnum_p
4535 && !xmm_regnum_p
01f9f808 4536 && !xmm_avx512_regnum_p
c131fcee 4537 && !ymm_regnum_p
1dbcd68c 4538 && !ymmh_regnum_p
01f9f808
MS
4539 && !ymm_avx512_regnum_p
4540 && !ymmh_avx512_regnum_p
1dbcd68c
WT
4541 && !bndr_regnum_p
4542 && !bnd_regnum_p
01f9f808
MS
4543 && !mpx_ctrl_regnum_p
4544 && !zmm_regnum_p
4545 && !zmmh_regnum_p);
acd5c798 4546
38c968cf
AC
4547 return default_register_reggroup_p (gdbarch, regnum, group);
4548}
38c968cf 4549\f
acd5c798 4550
f837910f
MK
4551/* Get the ARGIth function argument for the current function. */
4552
42c466d7 4553static CORE_ADDR
143985b7
AF
4554i386_fetch_pointer_argument (struct frame_info *frame, int argi,
4555 struct type *type)
4556{
e17a4113
UW
4557 struct gdbarch *gdbarch = get_frame_arch (frame);
4558 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
f4644a3f 4559 CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
e17a4113 4560 return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4, byte_order);
143985b7
AF
4561}
4562
514f746b
AR
4563static void
4564i386_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
7ad10968
HZ
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
473f17b0 4580
7ad10968
HZ
4581/* operand size */
4582enum
4583{
4584 OT_BYTE = 0,
4585 OT_WORD,
4586 OT_LONG,
cf648174 4587 OT_QUAD,
a3c4230a 4588 OT_DQUAD,
7ad10968 4589};
473f17b0 4590
7ad10968
HZ
4591/* i386 arith/logic operations */
4592enum
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};
5716833c 4603
7ad10968
HZ
4604struct i386_record_s
4605{
cf648174 4606 struct gdbarch *gdbarch;
7ad10968 4607 struct regcache *regcache;
df61f520 4608 CORE_ADDR orig_addr;
7ad10968
HZ
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;
cf648174
HZ
4616 uint8_t rex_x;
4617 uint8_t rex_b;
4618 int rip_offset;
4619 int popl_esp_hack;
4620 const int *regmap;
7ad10968 4621};
5716833c 4622
99c1624c
PA
4623/* Parse the "modrm" part of the memory address irp->addr points at.
4624 Returns -1 if something goes wrong, 0 otherwise. */
5716833c 4625
7ad10968
HZ
4626static int
4627i386_record_modrm (struct i386_record_s *irp)
4628{
cf648174 4629 struct gdbarch *gdbarch = irp->gdbarch;
5af949e3 4630
4ffa4fc7
PA
4631 if (record_read_memory (gdbarch, irp->addr, &irp->modrm, 1))
4632 return -1;
4633
7ad10968
HZ
4634 irp->addr++;
4635 irp->mod = (irp->modrm >> 6) & 3;
4636 irp->reg = (irp->modrm >> 3) & 7;
4637 irp->rm = irp->modrm & 7;
5716833c 4638
7ad10968
HZ
4639 return 0;
4640}
d2a7c97a 4641
99c1624c
PA
4642/* Extract the memory address that the current instruction writes to,
4643 and return it in *ADDR. Return -1 if something goes wrong. */
8201327c 4644
7ad10968 4645static int
cf648174 4646i386_record_lea_modrm_addr (struct i386_record_s *irp, uint64_t *addr)
7ad10968 4647{
cf648174 4648 struct gdbarch *gdbarch = irp->gdbarch;
60a1502a
MS
4649 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4650 gdb_byte buf[4];
4651 ULONGEST offset64;
21d0e8a4 4652
7ad10968 4653 *addr = 0;
1e87984a 4654 if (irp->aflag || irp->regmap[X86_RECORD_R8_REGNUM])
7ad10968 4655 {
1e87984a 4656 /* 32/64 bits */
7ad10968
HZ
4657 int havesib = 0;
4658 uint8_t scale = 0;
648d0c8b 4659 uint8_t byte;
7ad10968
HZ
4660 uint8_t index = 0;
4661 uint8_t base = irp->rm;
896fb97d 4662
7ad10968
HZ
4663 if (base == 4)
4664 {
4665 havesib = 1;
4ffa4fc7
PA
4666 if (record_read_memory (gdbarch, irp->addr, &byte, 1))
4667 return -1;
7ad10968 4668 irp->addr++;
648d0c8b
MS
4669 scale = (byte >> 6) & 3;
4670 index = ((byte >> 3) & 7) | irp->rex_x;
4671 base = (byte & 7);
7ad10968 4672 }
cf648174 4673 base |= irp->rex_b;
21d0e8a4 4674
7ad10968
HZ
4675 switch (irp->mod)
4676 {
4677 case 0:
4678 if ((base & 7) == 5)
4679 {
4680 base = 0xff;
4ffa4fc7
PA
4681 if (record_read_memory (gdbarch, irp->addr, buf, 4))
4682 return -1;
7ad10968 4683 irp->addr += 4;
60a1502a 4684 *addr = extract_signed_integer (buf, 4, byte_order);
cf648174
HZ
4685 if (irp->regmap[X86_RECORD_R8_REGNUM] && !havesib)
4686 *addr += irp->addr + irp->rip_offset;
7ad10968 4687 }
7ad10968
HZ
4688 break;
4689 case 1:
4ffa4fc7
PA
4690 if (record_read_memory (gdbarch, irp->addr, buf, 1))
4691 return -1;
7ad10968 4692 irp->addr++;
60a1502a 4693 *addr = (int8_t) buf[0];
7ad10968
HZ
4694 break;
4695 case 2:
4ffa4fc7
PA
4696 if (record_read_memory (gdbarch, irp->addr, buf, 4))
4697 return -1;
60a1502a 4698 *addr = extract_signed_integer (buf, 4, byte_order);
7ad10968
HZ
4699 irp->addr += 4;
4700 break;
4701 }
356a6b3e 4702
60a1502a 4703 offset64 = 0;
7ad10968 4704 if (base != 0xff)
cf648174
HZ
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],
60a1502a 4709 &offset64);
7ad10968 4710 }
cf648174
HZ
4711 if (irp->aflag == 2)
4712 {
60a1502a 4713 *addr += offset64;
cf648174
HZ
4714 }
4715 else
60a1502a 4716 *addr = (uint32_t) (offset64 + *addr);
c4fc7f1b 4717
7ad10968
HZ
4718 if (havesib && (index != 4 || scale != 0))
4719 {
cf648174 4720 regcache_raw_read_unsigned (irp->regcache, irp->regmap[index],
60a1502a 4721 &offset64);
cf648174 4722 if (irp->aflag == 2)
60a1502a 4723 *addr += offset64 << scale;
cf648174 4724 else
60a1502a 4725 *addr = (uint32_t) (*addr + (offset64 << scale));
7ad10968 4726 }
e85596e0
L
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 }
7ad10968
HZ
4734 }
4735 else
4736 {
4737 /* 16 bits */
4738 switch (irp->mod)
4739 {
4740 case 0:
4741 if (irp->rm == 6)
4742 {
4ffa4fc7
PA
4743 if (record_read_memory (gdbarch, irp->addr, buf, 2))
4744 return -1;
7ad10968 4745 irp->addr += 2;
60a1502a 4746 *addr = extract_signed_integer (buf, 2, byte_order);
7ad10968
HZ
4747 irp->rm = 0;
4748 goto no_rm;
4749 }
7ad10968
HZ
4750 break;
4751 case 1:
4ffa4fc7
PA
4752 if (record_read_memory (gdbarch, irp->addr, buf, 1))
4753 return -1;
7ad10968 4754 irp->addr++;
60a1502a 4755 *addr = (int8_t) buf[0];
7ad10968
HZ
4756 break;
4757 case 2:
4ffa4fc7
PA
4758 if (record_read_memory (gdbarch, irp->addr, buf, 2))
4759 return -1;
7ad10968 4760 irp->addr += 2;
60a1502a 4761 *addr = extract_signed_integer (buf, 2, byte_order);
7ad10968
HZ
4762 break;
4763 }
c4fc7f1b 4764
7ad10968
HZ
4765 switch (irp->rm)
4766 {
4767 case 0:
cf648174
HZ
4768 regcache_raw_read_unsigned (irp->regcache,
4769 irp->regmap[X86_RECORD_REBX_REGNUM],
60a1502a
MS
4770 &offset64);
4771 *addr = (uint32_t) (*addr + offset64);
cf648174
HZ
4772 regcache_raw_read_unsigned (irp->regcache,
4773 irp->regmap[X86_RECORD_RESI_REGNUM],
60a1502a
MS
4774 &offset64);
4775 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4776 break;
4777 case 1:
cf648174
HZ
4778 regcache_raw_read_unsigned (irp->regcache,
4779 irp->regmap[X86_RECORD_REBX_REGNUM],
60a1502a
MS
4780 &offset64);
4781 *addr = (uint32_t) (*addr + offset64);
cf648174
HZ
4782 regcache_raw_read_unsigned (irp->regcache,
4783 irp->regmap[X86_RECORD_REDI_REGNUM],
60a1502a
MS
4784 &offset64);
4785 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4786 break;
4787 case 2:
cf648174
HZ
4788 regcache_raw_read_unsigned (irp->regcache,
4789 irp->regmap[X86_RECORD_REBP_REGNUM],
60a1502a
MS
4790 &offset64);
4791 *addr = (uint32_t) (*addr + offset64);
cf648174
HZ
4792 regcache_raw_read_unsigned (irp->regcache,
4793 irp->regmap[X86_RECORD_RESI_REGNUM],
60a1502a
MS
4794 &offset64);
4795 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4796 break;
4797 case 3:
cf648174
HZ
4798 regcache_raw_read_unsigned (irp->regcache,
4799 irp->regmap[X86_RECORD_REBP_REGNUM],
60a1502a
MS
4800 &offset64);
4801 *addr = (uint32_t) (*addr + offset64);
cf648174
HZ
4802 regcache_raw_read_unsigned (irp->regcache,
4803 irp->regmap[X86_RECORD_REDI_REGNUM],
60a1502a
MS
4804 &offset64);
4805 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4806 break;
4807 case 4:
cf648174
HZ
4808 regcache_raw_read_unsigned (irp->regcache,
4809 irp->regmap[X86_RECORD_RESI_REGNUM],
60a1502a
MS
4810 &offset64);
4811 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4812 break;
4813 case 5:
cf648174
HZ
4814 regcache_raw_read_unsigned (irp->regcache,
4815 irp->regmap[X86_RECORD_REDI_REGNUM],
60a1502a
MS
4816 &offset64);
4817 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4818 break;
4819 case 6:
cf648174
HZ
4820 regcache_raw_read_unsigned (irp->regcache,
4821 irp->regmap[X86_RECORD_REBP_REGNUM],
60a1502a
MS
4822 &offset64);
4823 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4824 break;
4825 case 7:
cf648174
HZ
4826 regcache_raw_read_unsigned (irp->regcache,
4827 irp->regmap[X86_RECORD_REBX_REGNUM],
60a1502a
MS
4828 &offset64);
4829 *addr = (uint32_t) (*addr + offset64);
7ad10968
HZ
4830 break;
4831 }
4832 *addr &= 0xffff;
4833 }
c4fc7f1b 4834
01fe1b41 4835 no_rm:
7ad10968
HZ
4836 return 0;
4837}
c4fc7f1b 4838
99c1624c
PA
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. */
356a6b3e 4842
7ad10968
HZ
4843static int
4844i386_record_lea_modrm (struct i386_record_s *irp)
4845{
cf648174
HZ
4846 struct gdbarch *gdbarch = irp->gdbarch;
4847 uint64_t addr;
356a6b3e 4848
d7877f7e 4849 if (irp->override >= 0)
7ad10968 4850 {
25ea693b 4851 if (record_full_memory_query)
bb08c432
HZ
4852 {
4853 int q;
4854
4855 target_terminal_ours ();
4856 q = yquery (_("\
4857Process record ignores the memory change of instruction at address %s\n\
4858because it can't get the value of the segment register.\n\
4859Do you want to stop the program?"),
4860 paddress (gdbarch, irp->orig_addr));
4861 target_terminal_inferior ();
4862 if (q)
4863 return -1;
4864 }
4865
7ad10968
HZ
4866 return 0;
4867 }
61113f8b 4868
7ad10968
HZ
4869 if (i386_record_lea_modrm_addr (irp, &addr))
4870 return -1;
96297dab 4871
25ea693b 4872 if (record_full_arch_list_add_mem (addr, 1 << irp->ot))
7ad10968 4873 return -1;
a62cc96e 4874
7ad10968
HZ
4875 return 0;
4876}
b6197528 4877
99c1624c
PA
4878/* Record the effects of a push operation. Return -1 if something
4879 goes wrong, 0 otherwise. */
cf648174
HZ
4880
4881static int
4882i386_record_push (struct i386_record_s *irp, int size)
4883{
648d0c8b 4884 ULONGEST addr;
cf648174 4885
25ea693b
MM
4886 if (record_full_arch_list_add_reg (irp->regcache,
4887 irp->regmap[X86_RECORD_RESP_REGNUM]))
cf648174
HZ
4888 return -1;
4889 regcache_raw_read_unsigned (irp->regcache,
4890 irp->regmap[X86_RECORD_RESP_REGNUM],
648d0c8b 4891 &addr);
25ea693b 4892 if (record_full_arch_list_add_mem ((CORE_ADDR) addr - size, size))
cf648174
HZ
4893 return -1;
4894
4895 return 0;
4896}
4897
0289bdd7
MS
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
99c1624c
PA
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. */
0289bdd7
MS
4907
4908static 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 {
25ea693b 4924 if (record_full_arch_list_add_reg (ir->regcache, i))
0289bdd7
MS
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 {
25ea693b 4932 if (record_full_arch_list_add_reg (ir->regcache, i))
0289bdd7
MS
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 {
25ea693b 4940 if (record_full_arch_list_add_reg (ir->regcache, i))
0289bdd7
MS
4941 return -1;
4942 }
4943 }
4944 else if ((iregnum >= I387_ST0_REGNUM (tdep)) &&
4945 (iregnum <= I387_FOP_REGNUM (tdep)))
4946 {
25ea693b 4947 if (record_full_arch_list_add_reg (ir->regcache,iregnum))
0289bdd7
MS
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 {
25ea693b 4959 if (record_full_arch_list_add_reg (ir->regcache, i))
0289bdd7
MS
4960 return -1;
4961 }
4962 }
4963 return 0;
4964}
4965
99c1624c
PA
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. */
8201327c 4969
25ea693b
MM
4970#define I386_RECORD_FULL_ARCH_LIST_ADD_REG(regnum) \
4971 record_full_arch_list_add_reg (ir.regcache, ir.regmap[(regnum)])
cf648174 4972
a6b808b4 4973int
7ad10968 4974i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
648d0c8b 4975 CORE_ADDR input_addr)
7ad10968 4976{
60a1502a 4977 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7ad10968 4978 int prefixes = 0;
580879fc 4979 int regnum = 0;
425b824a 4980 uint32_t opcode;
f4644a3f 4981 uint8_t opcode8;
648d0c8b 4982 ULONGEST addr;
60a1502a 4983 gdb_byte buf[MAX_REGISTER_SIZE];
7ad10968 4984 struct i386_record_s ir;
0289bdd7 4985 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
cf648174
HZ
4986 uint8_t rex_w = -1;
4987 uint8_t rex_r = 0;
7ad10968 4988
8408d274 4989 memset (&ir, 0, sizeof (struct i386_record_s));
7ad10968 4990 ir.regcache = regcache;
648d0c8b
MS
4991 ir.addr = input_addr;
4992 ir.orig_addr = input_addr;
7ad10968
HZ
4993 ir.aflag = 1;
4994 ir.dflag = 1;
cf648174
HZ
4995 ir.override = -1;
4996 ir.popl_esp_hack = 0;
a3c4230a 4997 ir.regmap = tdep->record_regmap;
cf648174 4998 ir.gdbarch = gdbarch;
7ad10968
HZ
4999
5000 if (record_debug > 1)
5001 fprintf_unfiltered (gdb_stdlog, "Process record: i386_process_record "
5af949e3
UW
5002 "addr = %s\n",
5003 paddress (gdbarch, ir.addr));
7ad10968
HZ
5004
5005 /* prefixes */
5006 while (1)
5007 {
4ffa4fc7
PA
5008 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5009 return -1;
7ad10968 5010 ir.addr++;
425b824a 5011 switch (opcode8) /* Instruction prefixes */
7ad10968 5012 {
01fe1b41 5013 case REPE_PREFIX_OPCODE:
7ad10968
HZ
5014 prefixes |= PREFIX_REPZ;
5015 break;
01fe1b41 5016 case REPNE_PREFIX_OPCODE:
7ad10968
HZ
5017 prefixes |= PREFIX_REPNZ;
5018 break;
01fe1b41 5019 case LOCK_PREFIX_OPCODE:
7ad10968
HZ
5020 prefixes |= PREFIX_LOCK;
5021 break;
01fe1b41 5022 case CS_PREFIX_OPCODE:
cf648174 5023 ir.override = X86_RECORD_CS_REGNUM;
7ad10968 5024 break;
01fe1b41 5025 case SS_PREFIX_OPCODE:
cf648174 5026 ir.override = X86_RECORD_SS_REGNUM;
7ad10968 5027 break;
01fe1b41 5028 case DS_PREFIX_OPCODE:
cf648174 5029 ir.override = X86_RECORD_DS_REGNUM;
7ad10968 5030 break;
01fe1b41 5031 case ES_PREFIX_OPCODE:
cf648174 5032 ir.override = X86_RECORD_ES_REGNUM;
7ad10968 5033 break;
01fe1b41 5034 case FS_PREFIX_OPCODE:
cf648174 5035 ir.override = X86_RECORD_FS_REGNUM;
7ad10968 5036 break;
01fe1b41 5037 case GS_PREFIX_OPCODE:
cf648174 5038 ir.override = X86_RECORD_GS_REGNUM;
7ad10968 5039 break;
01fe1b41 5040 case DATA_PREFIX_OPCODE:
7ad10968
HZ
5041 prefixes |= PREFIX_DATA;
5042 break;
01fe1b41 5043 case ADDR_PREFIX_OPCODE:
7ad10968
HZ
5044 prefixes |= PREFIX_ADDR;
5045 break;
d691bec7
MS
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 */
cf648174
HZ
5063 {
5064 /* REX */
425b824a
MS
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;
cf648174 5069 }
d691bec7
MS
5070 else /* 32 bit target */
5071 goto out_prefixes;
cf648174 5072 break;
7ad10968
HZ
5073 default:
5074 goto out_prefixes;
5075 break;
5076 }
5077 }
01fe1b41 5078 out_prefixes:
cf648174
HZ
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 }
7ad10968
HZ
5088 if (prefixes & PREFIX_ADDR)
5089 ir.aflag ^= 1;
cf648174
HZ
5090 else if (ir.regmap[X86_RECORD_R8_REGNUM])
5091 ir.aflag = 2;
7ad10968 5092
1777feb0 5093 /* Now check op code. */
425b824a 5094 opcode = (uint32_t) opcode8;
01fe1b41 5095 reswitch:
7ad10968
HZ
5096 switch (opcode)
5097 {
5098 case 0x0f:
4ffa4fc7
PA
5099 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
5100 return -1;
7ad10968 5101 ir.addr++;
a3c4230a 5102 opcode = (uint32_t) opcode8 | 0x0f00;
7ad10968
HZ
5103 goto reswitch;
5104 break;
93924b6b 5105
a38bba38 5106 case 0x00: /* arith & logic */
7ad10968
HZ
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;
93924b6b 5160
7ad10968
HZ
5161 switch ((opcode >> 1) & 3)
5162 {
a38bba38 5163 case 0: /* OP Ev, Gv */
7ad10968
HZ
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 {
cf648174
HZ
5173 ir.rm |= ir.rex_b;
5174 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5175 ir.rm &= 0x3;
25ea693b 5176 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968
HZ
5177 }
5178 break;
a38bba38 5179 case 1: /* OP Gv, Ev */
7ad10968
HZ
5180 if (i386_record_modrm (&ir))
5181 return -1;
cf648174
HZ
5182 ir.reg |= rex_r;
5183 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5184 ir.reg &= 0x3;
25ea693b 5185 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968 5186 break;
a38bba38 5187 case 2: /* OP A, Iv */
25ea693b 5188 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
5189 break;
5190 }
5191 }
25ea693b 5192 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5193 break;
42fdc8df 5194
a38bba38 5195 case 0x80: /* GRP1 */
7ad10968
HZ
5196 case 0x81:
5197 case 0x82:
5198 case 0x83:
5199 if (i386_record_modrm (&ir))
5200 return -1;
8201327c 5201
7ad10968
HZ
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;
28fc6740 5208
7ad10968
HZ
5209 if (ir.mod != 3)
5210 {
cf648174
HZ
5211 if (opcode == 0x83)
5212 ir.rip_offset = 1;
5213 else
5214 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
7ad10968
HZ
5215 if (i386_record_lea_modrm (&ir))
5216 return -1;
5217 }
5218 else
25ea693b 5219 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968 5220 }
25ea693b 5221 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5222 break;
5e3397bb 5223
a38bba38 5224 case 0x40: /* inc */
7ad10968
HZ
5225 case 0x41:
5226 case 0x42:
5227 case 0x43:
5228 case 0x44:
5229 case 0x45:
5230 case 0x46:
5231 case 0x47:
a38bba38
MS
5232
5233 case 0x48: /* dec */
7ad10968
HZ
5234 case 0x49:
5235 case 0x4a:
5236 case 0x4b:
5237 case 0x4c:
5238 case 0x4d:
5239 case 0x4e:
5240 case 0x4f:
a38bba38 5241
25ea693b
MM
5242 I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 7);
5243 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5244 break;
acd5c798 5245
a38bba38 5246 case 0xf6: /* GRP3 */
7ad10968
HZ
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;
acd5c798 5254
cf648174
HZ
5255 if (ir.mod != 3 && ir.reg == 0)
5256 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
5257
7ad10968
HZ
5258 switch (ir.reg)
5259 {
a38bba38 5260 case 0: /* test */
25ea693b 5261 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5262 break;
a38bba38
MS
5263 case 2: /* not */
5264 case 3: /* neg */
7ad10968
HZ
5265 if (ir.mod != 3)
5266 {
5267 if (i386_record_lea_modrm (&ir))
5268 return -1;
5269 }
5270 else
5271 {
cf648174
HZ
5272 ir.rm |= ir.rex_b;
5273 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5274 ir.rm &= 0x3;
25ea693b 5275 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968 5276 }
a38bba38 5277 if (ir.reg == 3) /* neg */
25ea693b 5278 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5279 break;
a38bba38
MS
5280 case 4: /* mul */
5281 case 5: /* imul */
5282 case 6: /* div */
5283 case 7: /* idiv */
25ea693b 5284 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968 5285 if (ir.ot != OT_BYTE)
25ea693b
MM
5286 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
5287 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5288 break;
5289 default:
5290 ir.addr -= 2;
5291 opcode = opcode << 8 | ir.modrm;
5292 goto no_support;
5293 break;
5294 }
5295 break;
5296
a38bba38
MS
5297 case 0xfe: /* GRP4 */
5298 case 0xff: /* GRP5 */
7ad10968
HZ
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 }
7ad10968
HZ
5307 switch (ir.reg)
5308 {
a38bba38
MS
5309 case 0: /* inc */
5310 case 1: /* dec */
cf648174
HZ
5311 if ((opcode & 1) == 0)
5312 ir.ot = OT_BYTE;
5313 else
5314 ir.ot = ir.dflag + OT_WORD;
7ad10968
HZ
5315 if (ir.mod != 3)
5316 {
5317 if (i386_record_lea_modrm (&ir))
5318 return -1;
5319 }
5320 else
5321 {
cf648174
HZ
5322 ir.rm |= ir.rex_b;
5323 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5324 ir.rm &= 0x3;
25ea693b 5325 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968 5326 }
25ea693b 5327 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5328 break;
a38bba38 5329 case 2: /* call */
cf648174
HZ
5330 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5331 ir.dflag = 2;
5332 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
7ad10968 5333 return -1;
25ea693b 5334 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5335 break;
a38bba38 5336 case 3: /* lcall */
25ea693b 5337 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
cf648174 5338 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
7ad10968 5339 return -1;
25ea693b 5340 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 5341 break;
a38bba38
MS
5342 case 4: /* jmp */
5343 case 5: /* ljmp */
25ea693b 5344 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174 5345 break;
a38bba38 5346 case 6: /* push */
cf648174
HZ
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;
7ad10968
HZ
5351 break;
5352 default:
5353 ir.addr -= 2;
5354 opcode = opcode << 8 | ir.modrm;
5355 goto no_support;
5356 break;
5357 }
5358 break;
5359
a38bba38 5360 case 0x84: /* test */
7ad10968
HZ
5361 case 0x85:
5362 case 0xa8:
5363 case 0xa9:
25ea693b 5364 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5365 break;
5366
a38bba38 5367 case 0x98: /* CWDE/CBW */
25ea693b 5368 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
5369 break;
5370
a38bba38 5371 case 0x99: /* CDQ/CWD */
25ea693b
MM
5372 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5373 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7ad10968
HZ
5374 break;
5375
a38bba38 5376 case 0x0faf: /* imul */
7ad10968
HZ
5377 case 0x69:
5378 case 0x6b:
5379 ir.ot = ir.dflag + OT_WORD;
5380 if (i386_record_modrm (&ir))
5381 return -1;
cf648174
HZ
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])
7ad10968 5388 ir.reg &= 0x3;
25ea693b
MM
5389 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
5390 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5391 break;
5392
a38bba38 5393 case 0x0fc0: /* xadd */
7ad10968
HZ
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;
cf648174 5401 ir.reg |= rex_r;
7ad10968
HZ
5402 if (ir.mod == 3)
5403 {
cf648174 5404 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5405 ir.reg &= 0x3;
25ea693b 5406 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
cf648174 5407 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5408 ir.rm &= 0x3;
25ea693b 5409 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968
HZ
5410 }
5411 else
5412 {
5413 if (i386_record_lea_modrm (&ir))
5414 return -1;
cf648174 5415 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5416 ir.reg &= 0x3;
25ea693b 5417 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968 5418 }
25ea693b 5419 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5420 break;
5421
a38bba38 5422 case 0x0fb0: /* cmpxchg */
7ad10968
HZ
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 {
cf648174 5432 ir.reg |= rex_r;
25ea693b 5433 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
cf648174 5434 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5435 ir.reg &= 0x3;
25ea693b 5436 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968
HZ
5437 }
5438 else
5439 {
25ea693b 5440 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
5441 if (i386_record_lea_modrm (&ir))
5442 return -1;
5443 }
25ea693b 5444 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5445 break;
5446
a38bba38 5447 case 0x0fc7: /* cmpxchg8b */
7ad10968
HZ
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 }
25ea693b
MM
5456 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5457 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7ad10968
HZ
5458 if (i386_record_lea_modrm (&ir))
5459 return -1;
25ea693b 5460 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5461 break;
5462
a38bba38 5463 case 0x50: /* push */
7ad10968
HZ
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:
cf648174
HZ
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
a38bba38
MS
5479 case 0x06: /* push es */
5480 case 0x0e: /* push cs */
5481 case 0x16: /* push ss */
5482 case 0x1e: /* push ds */
cf648174
HZ
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
a38bba38
MS
5492 case 0x0fa0: /* push fs */
5493 case 0x0fa8: /* push gs */
cf648174
HZ
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)))
7ad10968 5500 return -1;
cf648174
HZ
5501 break;
5502
a38bba38 5503 case 0x60: /* pusha */
cf648174
HZ
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)))
7ad10968
HZ
5510 return -1;
5511 break;
5512
a38bba38 5513 case 0x58: /* pop */
7ad10968
HZ
5514 case 0x59:
5515 case 0x5a:
5516 case 0x5b:
5517 case 0x5c:
5518 case 0x5d:
5519 case 0x5e:
5520 case 0x5f:
25ea693b
MM
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);
7ad10968
HZ
5523 break;
5524
a38bba38 5525 case 0x61: /* popa */
cf648174
HZ
5526 if (ir.regmap[X86_RECORD_R8_REGNUM])
5527 {
5528 ir.addr -= 1;
5529 goto no_support;
7ad10968 5530 }
425b824a
MS
5531 for (regnum = X86_RECORD_REAX_REGNUM;
5532 regnum <= X86_RECORD_REDI_REGNUM;
5533 regnum++)
25ea693b 5534 I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
7ad10968
HZ
5535 break;
5536
a38bba38 5537 case 0x8f: /* pop */
cf648174
HZ
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;
7ad10968
HZ
5542 if (i386_record_modrm (&ir))
5543 return -1;
5544 if (ir.mod == 3)
25ea693b 5545 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968
HZ
5546 else
5547 {
cf648174 5548 ir.popl_esp_hack = 1 << ir.ot;
7ad10968
HZ
5549 if (i386_record_lea_modrm (&ir))
5550 return -1;
5551 }
25ea693b 5552 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
7ad10968
HZ
5553 break;
5554
a38bba38 5555 case 0xc8: /* enter */
25ea693b 5556 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
cf648174
HZ
5557 if (ir.regmap[X86_RECORD_R8_REGNUM] && ir.dflag)
5558 ir.dflag = 2;
5559 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
7ad10968
HZ
5560 return -1;
5561 break;
5562
a38bba38 5563 case 0xc9: /* leave */
25ea693b
MM
5564 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
5565 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REBP_REGNUM);
7ad10968
HZ
5566 break;
5567
a38bba38 5568 case 0x07: /* pop es */
cf648174
HZ
5569 if (ir.regmap[X86_RECORD_R8_REGNUM])
5570 {
5571 ir.addr -= 1;
5572 goto no_support;
5573 }
25ea693b
MM
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);
7ad10968
HZ
5577 break;
5578
a38bba38 5579 case 0x17: /* pop ss */
cf648174
HZ
5580 if (ir.regmap[X86_RECORD_R8_REGNUM])
5581 {
5582 ir.addr -= 1;
5583 goto no_support;
5584 }
25ea693b
MM
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);
7ad10968
HZ
5588 break;
5589
a38bba38 5590 case 0x1f: /* pop ds */
cf648174
HZ
5591 if (ir.regmap[X86_RECORD_R8_REGNUM])
5592 {
5593 ir.addr -= 1;
5594 goto no_support;
5595 }
25ea693b
MM
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);
7ad10968
HZ
5599 break;
5600
a38bba38 5601 case 0x0fa1: /* pop fs */
25ea693b
MM
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);
7ad10968
HZ
5605 break;
5606
a38bba38 5607 case 0x0fa9: /* pop gs */
25ea693b
MM
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);
7ad10968
HZ
5611 break;
5612
a38bba38 5613 case 0x88: /* mov */
7ad10968
HZ
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 {
cf648174
HZ
5627 if (opcode == 0xc6 || opcode == 0xc7)
5628 ir.rip_offset = (ir.ot > OT_LONG) ? 4 : (1 << ir.ot);
7ad10968
HZ
5629 if (i386_record_lea_modrm (&ir))
5630 return -1;
5631 }
5632 else
5633 {
cf648174
HZ
5634 if (opcode == 0xc6 || opcode == 0xc7)
5635 ir.rm |= ir.rex_b;
5636 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5637 ir.rm &= 0x3;
25ea693b 5638 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968 5639 }
7ad10968 5640 break;
cf648174 5641
a38bba38 5642 case 0x8a: /* mov */
7ad10968
HZ
5643 case 0x8b:
5644 if ((opcode & 1) == 0)
5645 ir.ot = OT_BYTE;
5646 else
5647 ir.ot = ir.dflag + OT_WORD;
7ad10968
HZ
5648 if (i386_record_modrm (&ir))
5649 return -1;
cf648174
HZ
5650 ir.reg |= rex_r;
5651 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5652 ir.reg &= 0x3;
25ea693b 5653 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
cf648174 5654 break;
7ad10968 5655
a38bba38 5656 case 0x8c: /* mov seg */
cf648174 5657 if (i386_record_modrm (&ir))
7ad10968 5658 return -1;
cf648174
HZ
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)
25ea693b 5667 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
cf648174
HZ
5668 else
5669 {
5670 ir.ot = OT_WORD;
5671 if (i386_record_lea_modrm (&ir))
5672 return -1;
5673 }
7ad10968
HZ
5674 break;
5675
a38bba38 5676 case 0x8e: /* mov seg */
7ad10968
HZ
5677 if (i386_record_modrm (&ir))
5678 return -1;
7ad10968
HZ
5679 switch (ir.reg)
5680 {
5681 case 0:
425b824a 5682 regnum = X86_RECORD_ES_REGNUM;
7ad10968
HZ
5683 break;
5684 case 2:
425b824a 5685 regnum = X86_RECORD_SS_REGNUM;
7ad10968
HZ
5686 break;
5687 case 3:
425b824a 5688 regnum = X86_RECORD_DS_REGNUM;
7ad10968
HZ
5689 break;
5690 case 4:
425b824a 5691 regnum = X86_RECORD_FS_REGNUM;
7ad10968
HZ
5692 break;
5693 case 5:
425b824a 5694 regnum = X86_RECORD_GS_REGNUM;
7ad10968
HZ
5695 break;
5696 default:
5697 ir.addr -= 2;
5698 opcode = opcode << 8 | ir.modrm;
5699 goto no_support;
5700 break;
5701 }
25ea693b
MM
5702 I386_RECORD_FULL_ARCH_LIST_ADD_REG (regnum);
5703 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
5704 break;
5705
a38bba38
MS
5706 case 0x0fb6: /* movzbS */
5707 case 0x0fb7: /* movzwS */
5708 case 0x0fbe: /* movsbS */
5709 case 0x0fbf: /* movswS */
7ad10968
HZ
5710 if (i386_record_modrm (&ir))
5711 return -1;
25ea693b 5712 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7ad10968
HZ
5713 break;
5714
a38bba38 5715 case 0x8d: /* lea */
7ad10968
HZ
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 }
7ad10968 5724 ir.ot = ir.dflag;
cf648174
HZ
5725 ir.reg |= rex_r;
5726 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5727 ir.reg &= 0x3;
25ea693b 5728 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968
HZ
5729 break;
5730
a38bba38 5731 case 0xa0: /* mov EAX */
7ad10968 5732 case 0xa1:
a38bba38
MS
5733
5734 case 0xd7: /* xlat */
25ea693b 5735 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
5736 break;
5737
a38bba38 5738 case 0xa2: /* mov EAX */
7ad10968 5739 case 0xa3:
d7877f7e 5740 if (ir.override >= 0)
cf648174 5741 {
25ea693b 5742 if (record_full_memory_query)
bb08c432
HZ
5743 {
5744 int q;
5745
5746 target_terminal_ours ();
5747 q = yquery (_("\
5748Process record ignores the memory change of instruction at address %s\n\
5749because it can't get the value of the segment register.\n\
5750Do you want to stop the program?"),
5751 paddress (gdbarch, ir.orig_addr));
5752 target_terminal_inferior ();
5753 if (q)
5754 return -1;
5755 }
cf648174
HZ
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 {
4ffa4fc7
PA
5765 if (record_read_memory (gdbarch, ir.addr, buf, 8))
5766 return -1;
cf648174 5767 ir.addr += 8;
60a1502a 5768 addr = extract_unsigned_integer (buf, 8, byte_order);
cf648174
HZ
5769 }
5770 else if (ir.aflag)
5771 {
4ffa4fc7
PA
5772 if (record_read_memory (gdbarch, ir.addr, buf, 4))
5773 return -1;
cf648174 5774 ir.addr += 4;
60a1502a 5775 addr = extract_unsigned_integer (buf, 4, byte_order);
cf648174
HZ
5776 }
5777 else
5778 {
4ffa4fc7
PA
5779 if (record_read_memory (gdbarch, ir.addr, buf, 2))
5780 return -1;
cf648174 5781 ir.addr += 2;
60a1502a 5782 addr = extract_unsigned_integer (buf, 2, byte_order);
cf648174 5783 }
25ea693b 5784 if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
cf648174
HZ
5785 return -1;
5786 }
7ad10968
HZ
5787 break;
5788
a38bba38 5789 case 0xb0: /* mov R, Ib */
7ad10968
HZ
5790 case 0xb1:
5791 case 0xb2:
5792 case 0xb3:
5793 case 0xb4:
5794 case 0xb5:
5795 case 0xb6:
5796 case 0xb7:
25ea693b
MM
5797 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((ir.regmap[X86_RECORD_R8_REGNUM])
5798 ? ((opcode & 0x7) | ir.rex_b)
5799 : ((opcode & 0x7) & 0x3));
7ad10968
HZ
5800 break;
5801
a38bba38 5802 case 0xb8: /* mov R, Iv */
7ad10968
HZ
5803 case 0xb9:
5804 case 0xba:
5805 case 0xbb:
5806 case 0xbc:
5807 case 0xbd:
5808 case 0xbe:
5809 case 0xbf:
25ea693b 5810 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 0x7) | ir.rex_b);
7ad10968
HZ
5811 break;
5812
a38bba38 5813 case 0x91: /* xchg R, EAX */
7ad10968
HZ
5814 case 0x92:
5815 case 0x93:
5816 case 0x94:
5817 case 0x95:
5818 case 0x96:
5819 case 0x97:
25ea693b
MM
5820 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
5821 I386_RECORD_FULL_ARCH_LIST_ADD_REG (opcode & 0x7);
7ad10968
HZ
5822 break;
5823
a38bba38 5824 case 0x86: /* xchg Ev, Gv */
7ad10968
HZ
5825 case 0x87:
5826 if ((opcode & 1) == 0)
5827 ir.ot = OT_BYTE;
5828 else
5829 ir.ot = ir.dflag + OT_WORD;
7ad10968
HZ
5830 if (i386_record_modrm (&ir))
5831 return -1;
7ad10968
HZ
5832 if (ir.mod == 3)
5833 {
86839d38 5834 ir.rm |= ir.rex_b;
cf648174
HZ
5835 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
5836 ir.rm &= 0x3;
25ea693b 5837 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968
HZ
5838 }
5839 else
5840 {
5841 if (i386_record_lea_modrm (&ir))
5842 return -1;
5843 }
cf648174
HZ
5844 ir.reg |= rex_r;
5845 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5846 ir.reg &= 0x3;
25ea693b 5847 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968
HZ
5848 break;
5849
a38bba38
MS
5850 case 0xc4: /* les Gv */
5851 case 0xc5: /* lds Gv */
cf648174
HZ
5852 if (ir.regmap[X86_RECORD_R8_REGNUM])
5853 {
5854 ir.addr -= 1;
5855 goto no_support;
5856 }
d3f323f3 5857 /* FALLTHROUGH */
a38bba38
MS
5858 case 0x0fb2: /* lss Gv */
5859 case 0x0fb4: /* lfs Gv */
5860 case 0x0fb5: /* lgs Gv */
7ad10968
HZ
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 }
7ad10968
HZ
5872 switch (opcode)
5873 {
a38bba38 5874 case 0xc4: /* les Gv */
425b824a 5875 regnum = X86_RECORD_ES_REGNUM;
7ad10968 5876 break;
a38bba38 5877 case 0xc5: /* lds Gv */
425b824a 5878 regnum = X86_RECORD_DS_REGNUM;
7ad10968 5879 break;
a38bba38 5880 case 0x0fb2: /* lss Gv */
425b824a 5881 regnum = X86_RECORD_SS_REGNUM;
7ad10968 5882 break;
a38bba38 5883 case 0x0fb4: /* lfs Gv */
425b824a 5884 regnum = X86_RECORD_FS_REGNUM;
7ad10968 5885 break;
a38bba38 5886 case 0x0fb5: /* lgs Gv */
425b824a 5887 regnum = X86_RECORD_GS_REGNUM;
7ad10968
HZ
5888 break;
5889 }
25ea693b
MM
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);
7ad10968
HZ
5893 break;
5894
a38bba38 5895 case 0xc0: /* shifts */
7ad10968
HZ
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;
7ad10968
HZ
5905 if (i386_record_modrm (&ir))
5906 return -1;
7ad10968
HZ
5907 if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
5908 {
5909 if (i386_record_lea_modrm (&ir))
5910 return -1;
5911 }
5912 else
5913 {
cf648174
HZ
5914 ir.rm |= ir.rex_b;
5915 if (ir.ot == OT_BYTE && !ir.regmap[X86_RECORD_R8_REGNUM])
7ad10968 5916 ir.rm &= 0x3;
25ea693b 5917 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm);
7ad10968 5918 }
25ea693b 5919 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
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 {
25ea693b 5930 if (record_full_arch_list_add_reg (ir.regcache, ir.rm))
7ad10968
HZ
5931 return -1;
5932 }
5933 else
5934 {
5935 if (i386_record_lea_modrm (&ir))
5936 return -1;
5937 }
5938 break;
5939
a38bba38 5940 case 0xd8: /* Floats. */
7ad10968
HZ
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 {
1777feb0 5953 /* Memory. */
955db0c0 5954 uint64_t addr64;
7ad10968 5955
955db0c0 5956 if (i386_record_lea_modrm_addr (&ir, &addr64))
7ad10968
HZ
5957 return -1;
5958 switch (ir.reg)
5959 {
7ad10968 5960 case 0x02:
0289bdd7
MS
5961 case 0x12:
5962 case 0x22:
5963 case 0x32:
5964 /* For fcom, ficom nothing to do. */
5965 break;
7ad10968 5966 case 0x03:
0289bdd7
MS
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:
7ad10968
HZ
5976 case 0x04:
5977 case 0x05:
5978 case 0x06:
5979 case 0x07:
5980 case 0x10:
5981 case 0x11:
7ad10968
HZ
5982 case 0x14:
5983 case 0x15:
5984 case 0x16:
5985 case 0x17:
5986 case 0x20:
5987 case 0x21:
7ad10968
HZ
5988 case 0x24:
5989 case 0x25:
5990 case 0x26:
5991 case 0x27:
5992 case 0x30:
5993 case 0x31:
7ad10968
HZ
5994 case 0x34:
5995 case 0x35:
5996 case 0x36:
5997 case 0x37:
0289bdd7
MS
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;
7ad10968
HZ
6003 break;
6004 case 0x08:
6005 case 0x0a:
6006 case 0x0b:
6007 case 0x18:
6008 case 0x19:
6009 case 0x1a:
6010 case 0x1b:
0289bdd7 6011 case 0x1d:
7ad10968
HZ
6012 case 0x28:
6013 case 0x29:
6014 case 0x2a:
6015 case 0x2b:
6016 case 0x38:
6017 case 0x39:
6018 case 0x3a:
6019 case 0x3b:
0289bdd7
MS
6020 case 0x3c:
6021 case 0x3d:
7ad10968
HZ
6022 switch (ir.reg & 7)
6023 {
6024 case 0:
0289bdd7
MS
6025 /* Handling fld, fild. */
6026 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6027 return -1;
7ad10968
HZ
6028 break;
6029 case 1:
6030 switch (ir.reg >> 4)
6031 {
6032 case 0:
25ea693b 6033 if (record_full_arch_list_add_mem (addr64, 4))
7ad10968
HZ
6034 return -1;
6035 break;
6036 case 2:
25ea693b 6037 if (record_full_arch_list_add_mem (addr64, 8))
7ad10968
HZ
6038 return -1;
6039 break;
6040 case 3:
0289bdd7 6041 break;
7ad10968 6042 default:
25ea693b 6043 if (record_full_arch_list_add_mem (addr64, 2))
7ad10968
HZ
6044 return -1;
6045 break;
6046 }
6047 break;
6048 default:
6049 switch (ir.reg >> 4)
6050 {
6051 case 0:
25ea693b 6052 if (record_full_arch_list_add_mem (addr64, 4))
0289bdd7
MS
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;
7ad10968 6062 case 1:
25ea693b 6063 if (record_full_arch_list_add_mem (addr64, 4))
7ad10968 6064 return -1;
0289bdd7
MS
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 }
7ad10968
HZ
6074 break;
6075 case 2:
25ea693b 6076 if (record_full_arch_list_add_mem (addr64, 8))
7ad10968 6077 return -1;
0289bdd7
MS
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 }
7ad10968
HZ
6085 break;
6086 case 3:
0289bdd7
MS
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 */
7ad10968 6095 default:
25ea693b 6096 if (record_full_arch_list_add_mem (addr64, 2))
7ad10968
HZ
6097 return -1;
6098 break;
6099 }
6100 break;
6101 }
6102 break;
6103 case 0x0c:
0289bdd7
MS
6104 /* Insn fldenv. */
6105 if (i386_record_floats (gdbarch, &ir,
6106 I386_SAVE_FPU_ENV_REG_STACK))
6107 return -1;
6108 break;
7ad10968 6109 case 0x0d:
0289bdd7
MS
6110 /* Insn fldcw. */
6111 if (i386_record_floats (gdbarch, &ir, I387_FCTRL_REGNUM (tdep)))
6112 return -1;
6113 break;
7ad10968 6114 case 0x2c:
0289bdd7
MS
6115 /* Insn frstor. */
6116 if (i386_record_floats (gdbarch, &ir,
6117 I386_SAVE_FPU_ENV_REG_STACK))
6118 return -1;
7ad10968
HZ
6119 break;
6120 case 0x0e:
6121 if (ir.dflag)
6122 {
25ea693b 6123 if (record_full_arch_list_add_mem (addr64, 28))
7ad10968
HZ
6124 return -1;
6125 }
6126 else
6127 {
25ea693b 6128 if (record_full_arch_list_add_mem (addr64, 14))
7ad10968
HZ
6129 return -1;
6130 }
6131 break;
6132 case 0x0f:
6133 case 0x2f:
25ea693b 6134 if (record_full_arch_list_add_mem (addr64, 2))
7ad10968 6135 return -1;
0289bdd7
MS
6136 /* Insn fstp, fbstp. */
6137 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6138 return -1;
7ad10968
HZ
6139 break;
6140 case 0x1f:
6141 case 0x3e:
25ea693b 6142 if (record_full_arch_list_add_mem (addr64, 10))
7ad10968
HZ
6143 return -1;
6144 break;
6145 case 0x2e:
6146 if (ir.dflag)
6147 {
25ea693b 6148 if (record_full_arch_list_add_mem (addr64, 28))
7ad10968 6149 return -1;
955db0c0 6150 addr64 += 28;
7ad10968
HZ
6151 }
6152 else
6153 {
25ea693b 6154 if (record_full_arch_list_add_mem (addr64, 14))
7ad10968 6155 return -1;
955db0c0 6156 addr64 += 14;
7ad10968 6157 }
25ea693b 6158 if (record_full_arch_list_add_mem (addr64, 80))
7ad10968 6159 return -1;
0289bdd7
MS
6160 /* Insn fsave. */
6161 if (i386_record_floats (gdbarch, &ir,
6162 I386_SAVE_FPU_ENV_REG_STACK))
6163 return -1;
7ad10968
HZ
6164 break;
6165 case 0x3f:
25ea693b 6166 if (record_full_arch_list_add_mem (addr64, 8))
7ad10968 6167 return -1;
0289bdd7
MS
6168 /* Insn fistp. */
6169 if (i386_record_floats (gdbarch, &ir, I386_SAVE_FPU_REGS))
6170 return -1;
7ad10968
HZ
6171 break;
6172 default:
6173 ir.addr -= 2;
6174 opcode = opcode << 8 | ir.modrm;
6175 goto no_support;
6176 break;
6177 }
6178 }
0289bdd7
MS
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 {
25ea693b
MM
6372 if (record_full_arch_list_add_reg (ir.regcache,
6373 I386_EAX_REGNUM))
0289bdd7
MS
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 }
7ad10968 6384 break;
7ad10968 6385 /* string ops */
a38bba38 6386 case 0xa4: /* movsS */
7ad10968 6387 case 0xa5:
a38bba38 6388 case 0xaa: /* stosS */
7ad10968 6389 case 0xab:
a38bba38 6390 case 0x6c: /* insS */
7ad10968 6391 case 0x6d:
cf648174 6392 regcache_raw_read_unsigned (ir.regcache,
77d7dc92 6393 ir.regmap[X86_RECORD_RECX_REGNUM],
648d0c8b
MS
6394 &addr);
6395 if (addr)
cf648174 6396 {
77d7dc92
HZ
6397 ULONGEST es, ds;
6398
6399 if ((opcode & 1) == 0)
6400 ir.ot = OT_BYTE;
6401 else
6402 ir.ot = ir.dflag + OT_WORD;
cf648174
HZ
6403 regcache_raw_read_unsigned (ir.regcache,
6404 ir.regmap[X86_RECORD_REDI_REGNUM],
648d0c8b 6405 &addr);
77d7dc92 6406
d7877f7e
HZ
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))
77d7dc92
HZ
6414 {
6415 /* addr += ((uint32_t) read_register (I386_ES_REGNUM)) << 4; */
25ea693b 6416 if (record_full_memory_query)
bb08c432
HZ
6417 {
6418 int q;
6419
6420 target_terminal_ours ();
6421 q = yquery (_("\
6422Process record ignores the memory change of instruction at address %s\n\
6423because it can't get the value of the segment register.\n\
6424Do you want to stop the program?"),
6425 paddress (gdbarch, ir.orig_addr));
6426 target_terminal_inferior ();
6427 if (q)
6428 return -1;
6429 }
df61f520
HZ
6430 }
6431 else
6432 {
25ea693b 6433 if (record_full_arch_list_add_mem (addr, 1 << ir.ot))
df61f520 6434 return -1;
77d7dc92
HZ
6435 }
6436
6437 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
25ea693b 6438 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
77d7dc92 6439 if (opcode == 0xa4 || opcode == 0xa5)
25ea693b
MM
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);
77d7dc92 6443 }
cf648174 6444 break;
7ad10968 6445
a38bba38 6446 case 0xa6: /* cmpsS */
cf648174 6447 case 0xa7:
25ea693b
MM
6448 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
6449 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
cf648174 6450 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
25ea693b
MM
6451 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6452 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6453 break;
6454
a38bba38 6455 case 0xac: /* lodsS */
7ad10968 6456 case 0xad:
25ea693b
MM
6457 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6458 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
7ad10968 6459 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
25ea693b
MM
6460 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6461 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6462 break;
6463
a38bba38 6464 case 0xae: /* scasS */
7ad10968 6465 case 0xaf:
25ea693b 6466 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDI_REGNUM);
7ad10968 6467 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
25ea693b
MM
6468 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6469 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6470 break;
6471
a38bba38 6472 case 0x6e: /* outsS */
cf648174 6473 case 0x6f:
25ea693b 6474 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESI_REGNUM);
7ad10968 6475 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
25ea693b
MM
6476 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6477 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6478 break;
6479
a38bba38 6480 case 0xe4: /* port I/O */
7ad10968
HZ
6481 case 0xe5:
6482 case 0xec:
6483 case 0xed:
25ea693b
MM
6484 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6485 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
6486 break;
6487
6488 case 0xe6:
6489 case 0xe7:
6490 case 0xee:
6491 case 0xef:
6492 break;
6493
6494 /* control */
a38bba38
MS
6495 case 0xc2: /* ret im */
6496 case 0xc3: /* ret */
25ea693b
MM
6497 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6498 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174
HZ
6499 break;
6500
a38bba38
MS
6501 case 0xca: /* lret im */
6502 case 0xcb: /* lret */
6503 case 0xcf: /* iret */
25ea693b
MM
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);
7ad10968
HZ
6507 break;
6508
a38bba38 6509 case 0xe8: /* call im */
cf648174
HZ
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;
7ad10968
HZ
6514 break;
6515
a38bba38 6516 case 0x9a: /* lcall im */
cf648174
HZ
6517 if (ir.regmap[X86_RECORD_R8_REGNUM])
6518 {
6519 ir.addr -= 1;
6520 goto no_support;
6521 }
25ea693b 6522 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_CS_REGNUM);
cf648174
HZ
6523 if (i386_record_push (&ir, 1 << (ir.dflag + 1)))
6524 return -1;
7ad10968
HZ
6525 break;
6526
a38bba38
MS
6527 case 0xe9: /* jmp im */
6528 case 0xea: /* ljmp im */
6529 case 0xeb: /* jmp Jb */
6530 case 0x70: /* jcc Jb */
7ad10968
HZ
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:
a38bba38 6546 case 0x0f80: /* jcc Jv */
7ad10968
HZ
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
a38bba38 6564 case 0x0f90: /* setcc Gv */
7ad10968
HZ
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:
25ea693b 6580 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6581 ir.ot = OT_BYTE;
6582 if (i386_record_modrm (&ir))
6583 return -1;
6584 if (ir.mod == 3)
25ea693b
MM
6585 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rex_b ? (ir.rm | ir.rex_b)
6586 : (ir.rm & 0x3));
7ad10968
HZ
6587 else
6588 {
6589 if (i386_record_lea_modrm (&ir))
6590 return -1;
6591 }
6592 break;
6593
a38bba38 6594 case 0x0f40: /* cmov Gv, Ev */
7ad10968
HZ
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;
cf648174 6612 ir.reg |= rex_r;
7ad10968
HZ
6613 if (ir.dflag == OT_BYTE)
6614 ir.reg &= 0x3;
25ea693b 6615 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7ad10968
HZ
6616 break;
6617
6618 /* flags */
a38bba38 6619 case 0x9c: /* pushf */
25ea693b 6620 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174
HZ
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;
7ad10968
HZ
6625 break;
6626
a38bba38 6627 case 0x9d: /* popf */
25ea693b
MM
6628 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RESP_REGNUM);
6629 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6630 break;
6631
a38bba38 6632 case 0x9e: /* sahf */
cf648174
HZ
6633 if (ir.regmap[X86_RECORD_R8_REGNUM])
6634 {
6635 ir.addr -= 1;
6636 goto no_support;
6637 }
d3f323f3 6638 /* FALLTHROUGH */
a38bba38
MS
6639 case 0xf5: /* cmc */
6640 case 0xf8: /* clc */
6641 case 0xf9: /* stc */
6642 case 0xfc: /* cld */
6643 case 0xfd: /* std */
25ea693b 6644 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6645 break;
6646
a38bba38 6647 case 0x9f: /* lahf */
cf648174
HZ
6648 if (ir.regmap[X86_RECORD_R8_REGNUM])
6649 {
6650 ir.addr -= 1;
6651 goto no_support;
6652 }
25ea693b
MM
6653 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
6654 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7ad10968
HZ
6655 break;
6656
6657 /* bit operations */
a38bba38 6658 case 0x0fba: /* bt/bts/btr/btc Gv, im */
7ad10968
HZ
6659 ir.ot = ir.dflag + OT_WORD;
6660 if (i386_record_modrm (&ir))
6661 return -1;
6662 if (ir.reg < 4)
6663 {
cf648174 6664 ir.addr -= 2;
7ad10968
HZ
6665 opcode = opcode << 8 | ir.modrm;
6666 goto no_support;
6667 }
cf648174 6668 if (ir.reg != 4)
7ad10968 6669 {
cf648174 6670 if (ir.mod == 3)
25ea693b 6671 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968
HZ
6672 else
6673 {
cf648174 6674 if (i386_record_lea_modrm (&ir))
7ad10968
HZ
6675 return -1;
6676 }
6677 }
25ea693b 6678 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6679 break;
6680
a38bba38 6681 case 0x0fa3: /* bt Gv, Ev */
25ea693b 6682 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174
HZ
6683 break;
6684
a38bba38
MS
6685 case 0x0fab: /* bts */
6686 case 0x0fb3: /* btr */
6687 case 0x0fbb: /* btc */
cf648174
HZ
6688 ir.ot = ir.dflag + OT_WORD;
6689 if (i386_record_modrm (&ir))
6690 return -1;
6691 if (ir.mod == 3)
25ea693b 6692 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
cf648174
HZ
6693 else
6694 {
955db0c0
MS
6695 uint64_t addr64;
6696 if (i386_record_lea_modrm_addr (&ir, &addr64))
cf648174
HZ
6697 return -1;
6698 regcache_raw_read_unsigned (ir.regcache,
6699 ir.regmap[ir.reg | rex_r],
648d0c8b 6700 &addr);
cf648174
HZ
6701 switch (ir.dflag)
6702 {
6703 case 0:
648d0c8b 6704 addr64 += ((int16_t) addr >> 4) << 4;
cf648174
HZ
6705 break;
6706 case 1:
648d0c8b 6707 addr64 += ((int32_t) addr >> 5) << 5;
cf648174
HZ
6708 break;
6709 case 2:
648d0c8b 6710 addr64 += ((int64_t) addr >> 6) << 6;
cf648174
HZ
6711 break;
6712 }
25ea693b 6713 if (record_full_arch_list_add_mem (addr64, 1 << ir.ot))
cf648174
HZ
6714 return -1;
6715 if (i386_record_lea_modrm (&ir))
6716 return -1;
6717 }
25ea693b 6718 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6719 break;
6720
a38bba38
MS
6721 case 0x0fbc: /* bsf */
6722 case 0x0fbd: /* bsr */
25ea693b
MM
6723 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
6724 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6725 break;
6726
6727 /* bcd */
a38bba38
MS
6728 case 0x27: /* daa */
6729 case 0x2f: /* das */
6730 case 0x37: /* aaa */
6731 case 0x3f: /* aas */
6732 case 0xd4: /* aam */
6733 case 0xd5: /* aad */
cf648174
HZ
6734 if (ir.regmap[X86_RECORD_R8_REGNUM])
6735 {
6736 ir.addr -= 1;
6737 goto no_support;
6738 }
25ea693b
MM
6739 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6740 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6741 break;
6742
6743 /* misc */
a38bba38 6744 case 0x90: /* nop */
7ad10968
HZ
6745 if (prefixes & PREFIX_LOCK)
6746 {
6747 ir.addr -= 1;
6748 goto no_support;
6749 }
6750 break;
6751
a38bba38 6752 case 0x9b: /* fwait */
4ffa4fc7
PA
6753 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
6754 return -1;
425b824a 6755 opcode = (uint32_t) opcode8;
0289bdd7
MS
6756 ir.addr++;
6757 goto reswitch;
7ad10968
HZ
6758 break;
6759
7ad10968 6760 /* XXX */
a38bba38 6761 case 0xcc: /* int3 */
a3c4230a 6762 printf_unfiltered (_("Process record does not support instruction "
7ad10968
HZ
6763 "int3.\n"));
6764 ir.addr -= 1;
6765 goto no_support;
6766 break;
6767
7ad10968 6768 /* XXX */
a38bba38 6769 case 0xcd: /* int */
7ad10968
HZ
6770 {
6771 int ret;
425b824a 6772 uint8_t interrupt;
4ffa4fc7
PA
6773 if (record_read_memory (gdbarch, ir.addr, &interrupt, 1))
6774 return -1;
7ad10968 6775 ir.addr++;
425b824a 6776 if (interrupt != 0x80
a3c4230a 6777 || tdep->i386_intx80_record == NULL)
7ad10968 6778 {
a3c4230a 6779 printf_unfiltered (_("Process record does not support "
7ad10968 6780 "instruction int 0x%02x.\n"),
425b824a 6781 interrupt);
7ad10968
HZ
6782 ir.addr -= 2;
6783 goto no_support;
6784 }
a3c4230a 6785 ret = tdep->i386_intx80_record (ir.regcache);
7ad10968
HZ
6786 if (ret)
6787 return ret;
6788 }
6789 break;
6790
7ad10968 6791 /* XXX */
a38bba38 6792 case 0xce: /* into */
a3c4230a 6793 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6794 "instruction into.\n"));
6795 ir.addr -= 1;
6796 goto no_support;
6797 break;
6798
a38bba38
MS
6799 case 0xfa: /* cli */
6800 case 0xfb: /* sti */
7ad10968
HZ
6801 break;
6802
a38bba38 6803 case 0x62: /* bound */
a3c4230a 6804 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6805 "instruction bound.\n"));
6806 ir.addr -= 1;
6807 goto no_support;
6808 break;
6809
a38bba38 6810 case 0x0fc8: /* bswap reg */
7ad10968
HZ
6811 case 0x0fc9:
6812 case 0x0fca:
6813 case 0x0fcb:
6814 case 0x0fcc:
6815 case 0x0fcd:
6816 case 0x0fce:
6817 case 0x0fcf:
25ea693b 6818 I386_RECORD_FULL_ARCH_LIST_ADD_REG ((opcode & 7) | ir.rex_b);
7ad10968
HZ
6819 break;
6820
a38bba38 6821 case 0xd6: /* salc */
cf648174
HZ
6822 if (ir.regmap[X86_RECORD_R8_REGNUM])
6823 {
6824 ir.addr -= 1;
6825 goto no_support;
6826 }
25ea693b
MM
6827 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6828 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6829 break;
6830
a38bba38
MS
6831 case 0xe0: /* loopnz */
6832 case 0xe1: /* loopz */
6833 case 0xe2: /* loop */
6834 case 0xe3: /* jecxz */
25ea693b
MM
6835 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_RECX_REGNUM);
6836 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
6837 break;
6838
a38bba38 6839 case 0x0f30: /* wrmsr */
a3c4230a 6840 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6841 "instruction wrmsr.\n"));
6842 ir.addr -= 2;
6843 goto no_support;
6844 break;
6845
a38bba38 6846 case 0x0f32: /* rdmsr */
a3c4230a 6847 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6848 "instruction rdmsr.\n"));
6849 ir.addr -= 2;
6850 goto no_support;
6851 break;
6852
a38bba38 6853 case 0x0f31: /* rdtsc */
25ea693b
MM
6854 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
6855 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
7ad10968
HZ
6856 break;
6857
a38bba38 6858 case 0x0f34: /* sysenter */
7ad10968
HZ
6859 {
6860 int ret;
cf648174
HZ
6861 if (ir.regmap[X86_RECORD_R8_REGNUM])
6862 {
6863 ir.addr -= 2;
6864 goto no_support;
6865 }
a3c4230a 6866 if (tdep->i386_sysenter_record == NULL)
7ad10968 6867 {
a3c4230a 6868 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6869 "instruction sysenter.\n"));
6870 ir.addr -= 2;
6871 goto no_support;
6872 }
a3c4230a 6873 ret = tdep->i386_sysenter_record (ir.regcache);
7ad10968
HZ
6874 if (ret)
6875 return ret;
6876 }
6877 break;
6878
a38bba38 6879 case 0x0f35: /* sysexit */
a3c4230a 6880 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
6881 "instruction sysexit.\n"));
6882 ir.addr -= 2;
6883 goto no_support;
6884 break;
6885
a38bba38 6886 case 0x0f05: /* syscall */
cf648174
HZ
6887 {
6888 int ret;
a3c4230a 6889 if (tdep->i386_syscall_record == NULL)
cf648174 6890 {
a3c4230a 6891 printf_unfiltered (_("Process record does not support "
cf648174
HZ
6892 "instruction syscall.\n"));
6893 ir.addr -= 2;
6894 goto no_support;
6895 }
a3c4230a 6896 ret = tdep->i386_syscall_record (ir.regcache);
cf648174
HZ
6897 if (ret)
6898 return ret;
6899 }
6900 break;
6901
a38bba38 6902 case 0x0f07: /* sysret */
a3c4230a 6903 printf_unfiltered (_("Process record does not support "
cf648174
HZ
6904 "instruction sysret.\n"));
6905 ir.addr -= 2;
6906 goto no_support;
6907 break;
6908
a38bba38 6909 case 0x0fa2: /* cpuid */
25ea693b
MM
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);
7ad10968
HZ
6914 break;
6915
a38bba38 6916 case 0xf4: /* hlt */
a3c4230a 6917 printf_unfiltered (_("Process record does not support "
7ad10968
HZ
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 {
a38bba38
MS
6928 case 0: /* sldt */
6929 case 1: /* str */
7ad10968 6930 if (ir.mod == 3)
25ea693b 6931 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968
HZ
6932 else
6933 {
6934 ir.ot = OT_WORD;
6935 if (i386_record_lea_modrm (&ir))
6936 return -1;
6937 }
6938 break;
a38bba38
MS
6939 case 2: /* lldt */
6940 case 3: /* ltr */
7ad10968 6941 break;
a38bba38
MS
6942 case 4: /* verr */
6943 case 5: /* verw */
25ea693b 6944 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
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 {
a38bba38 6959 case 0: /* sgdt */
7ad10968 6960 {
955db0c0 6961 uint64_t addr64;
7ad10968
HZ
6962
6963 if (ir.mod == 3)
6964 {
6965 ir.addr -= 3;
6966 opcode = opcode << 8 | ir.modrm;
6967 goto no_support;
6968 }
d7877f7e 6969 if (ir.override >= 0)
7ad10968 6970 {
25ea693b 6971 if (record_full_memory_query)
bb08c432
HZ
6972 {
6973 int q;
6974
6975 target_terminal_ours ();
6976 q = yquery (_("\
6977Process record ignores the memory change of instruction at address %s\n\
6978because it can't get the value of the segment register.\n\
6979Do you want to stop the program?"),
6980 paddress (gdbarch, ir.orig_addr));
6981 target_terminal_inferior ();
6982 if (q)
6983 return -1;
6984 }
7ad10968
HZ
6985 }
6986 else
6987 {
955db0c0 6988 if (i386_record_lea_modrm_addr (&ir, &addr64))
7ad10968 6989 return -1;
25ea693b 6990 if (record_full_arch_list_add_mem (addr64, 2))
7ad10968 6991 return -1;
955db0c0 6992 addr64 += 2;
cf648174
HZ
6993 if (ir.regmap[X86_RECORD_R8_REGNUM])
6994 {
25ea693b 6995 if (record_full_arch_list_add_mem (addr64, 8))
cf648174
HZ
6996 return -1;
6997 }
6998 else
6999 {
25ea693b 7000 if (record_full_arch_list_add_mem (addr64, 4))
cf648174
HZ
7001 return -1;
7002 }
7ad10968
HZ
7003 }
7004 }
7005 break;
7006 case 1:
7007 if (ir.mod == 3)
7008 {
7009 switch (ir.rm)
7010 {
a38bba38 7011 case 0: /* monitor */
7ad10968 7012 break;
a38bba38 7013 case 1: /* mwait */
25ea693b 7014 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
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 */
d7877f7e 7026 if (ir.override >= 0)
7ad10968 7027 {
25ea693b 7028 if (record_full_memory_query)
bb08c432
HZ
7029 {
7030 int q;
7031
7032 target_terminal_ours ();
7033 q = yquery (_("\
7034Process record ignores the memory change of instruction at address %s\n\
7035because it can't get the value of the segment register.\n\
7036Do you want to stop the program?"),
7037 paddress (gdbarch, ir.orig_addr));
7038 target_terminal_inferior ();
7039 if (q)
7040 return -1;
7041 }
7ad10968
HZ
7042 }
7043 else
7044 {
955db0c0 7045 uint64_t addr64;
7ad10968 7046
955db0c0 7047 if (i386_record_lea_modrm_addr (&ir, &addr64))
7ad10968 7048 return -1;
25ea693b 7049 if (record_full_arch_list_add_mem (addr64, 2))
7ad10968 7050 return -1;
955db0c0 7051 addr64 += 2;
cf648174
HZ
7052 if (ir.regmap[X86_RECORD_R8_REGNUM])
7053 {
25ea693b 7054 if (record_full_arch_list_add_mem (addr64, 8))
cf648174
HZ
7055 return -1;
7056 }
7057 else
7058 {
25ea693b 7059 if (record_full_arch_list_add_mem (addr64, 4))
cf648174
HZ
7060 return -1;
7061 }
7ad10968
HZ
7062 }
7063 }
7064 break;
a38bba38 7065 case 2: /* lgdt */
3800e645
MS
7066 if (ir.mod == 3)
7067 {
7068 /* xgetbv */
7069 if (ir.rm == 0)
7070 {
25ea693b
MM
7071 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REAX_REGNUM);
7072 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REDX_REGNUM);
3800e645
MS
7073 break;
7074 }
7075 /* xsetbv */
7076 else if (ir.rm == 1)
7077 break;
7078 }
a38bba38 7079 case 3: /* lidt */
7ad10968
HZ
7080 if (ir.mod == 3)
7081 {
7082 ir.addr -= 3;
7083 opcode = opcode << 8 | ir.modrm;
7084 goto no_support;
7085 }
7086 break;
a38bba38 7087 case 4: /* smsw */
7ad10968
HZ
7088 if (ir.mod == 3)
7089 {
25ea693b 7090 if (record_full_arch_list_add_reg (ir.regcache, ir.rm | ir.rex_b))
7ad10968
HZ
7091 return -1;
7092 }
7093 else
7094 {
7095 ir.ot = OT_WORD;
7096 if (i386_record_lea_modrm (&ir))
7097 return -1;
7098 }
25ea693b 7099 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 7100 break;
a38bba38 7101 case 6: /* lmsw */
25ea693b 7102 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174 7103 break;
a38bba38 7104 case 7: /* invlpg */
cf648174
HZ
7105 if (ir.mod == 3)
7106 {
7107 if (ir.rm == 0 && ir.regmap[X86_RECORD_R8_REGNUM])
25ea693b 7108 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_GS_REGNUM);
cf648174
HZ
7109 else
7110 {
7111 ir.addr -= 3;
7112 opcode = opcode << 8 | ir.modrm;
7113 goto no_support;
7114 }
7115 }
7116 else
25ea693b 7117 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
cf648174
HZ
7118 break;
7119 default:
7120 ir.addr -= 3;
7121 opcode = opcode << 8 | ir.modrm;
7122 goto no_support;
7ad10968
HZ
7123 break;
7124 }
7125 break;
7126
a38bba38
MS
7127 case 0x0f08: /* invd */
7128 case 0x0f09: /* wbinvd */
7ad10968
HZ
7129 break;
7130
a38bba38 7131 case 0x63: /* arpl */
7ad10968
HZ
7132 if (i386_record_modrm (&ir))
7133 return -1;
cf648174
HZ
7134 if (ir.mod == 3 || ir.regmap[X86_RECORD_R8_REGNUM])
7135 {
25ea693b
MM
7136 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.regmap[X86_RECORD_R8_REGNUM]
7137 ? (ir.reg | rex_r) : ir.rm);
cf648174 7138 }
7ad10968 7139 else
cf648174
HZ
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])
25ea693b 7146 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
7147 break;
7148
a38bba38
MS
7149 case 0x0f02: /* lar */
7150 case 0x0f03: /* lsl */
7ad10968
HZ
7151 if (i386_record_modrm (&ir))
7152 return -1;
25ea693b
MM
7153 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
7154 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
7155 break;
7156
7157 case 0x0f18:
cf648174
HZ
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 }
7ad10968
HZ
7166 break;
7167
7ad10968
HZ
7168 case 0x0f19:
7169 case 0x0f1a:
7170 case 0x0f1b:
7171 case 0x0f1c:
7172 case 0x0f1d:
7173 case 0x0f1e:
7174 case 0x0f1f:
a38bba38 7175 /* nop (multi byte) */
7ad10968
HZ
7176 break;
7177
a38bba38
MS
7178 case 0x0f20: /* mov reg, crN */
7179 case 0x0f22: /* mov crN, reg */
7ad10968
HZ
7180 if (i386_record_modrm (&ir))
7181 return -1;
7182 if ((ir.modrm & 0xc0) != 0xc0)
7183 {
cf648174 7184 ir.addr -= 3;
7ad10968
HZ
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)
25ea693b 7196 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 7197 else
25ea693b 7198 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968
HZ
7199 break;
7200 default:
cf648174 7201 ir.addr -= 3;
7ad10968
HZ
7202 opcode = opcode << 8 | ir.modrm;
7203 goto no_support;
7204 break;
7205 }
7206 break;
7207
a38bba38
MS
7208 case 0x0f21: /* mov reg, drN */
7209 case 0x0f23: /* mov drN, reg */
7ad10968
HZ
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 {
cf648174 7215 ir.addr -= 3;
7ad10968
HZ
7216 opcode = opcode << 8 | ir.modrm;
7217 goto no_support;
7218 }
7219 if (opcode & 2)
25ea693b 7220 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968 7221 else
25ea693b 7222 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
7ad10968
HZ
7223 break;
7224
a38bba38 7225 case 0x0f06: /* clts */
25ea693b 7226 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
7ad10968
HZ
7227 break;
7228
a3c4230a
HZ
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;
25ea693b 7238 record_full_arch_list_add_reg (ir.regcache, I387_FTAG_REGNUM(tdep));
a3c4230a
HZ
7239 break;
7240
7241 case 0x0f0f: /* 3DNow! data */
7242 if (i386_record_modrm (&ir))
7243 return -1;
4ffa4fc7
PA
7244 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7245 return -1;
a3c4230a
HZ
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;
25ea693b 7275 record_full_arch_list_add_reg (ir.regcache, ir.reg);
a3c4230a
HZ
7276 break;
7277
7278 default:
7279no_support_3dnow_data:
7280 opcode = (opcode << 8) | opcode8;
7281 goto no_support;
7282 break;
7283 }
7284 break;
7285
7286 case 0x0faa: /* rsm */
25ea693b
MM
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);
a3c4230a
HZ
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
25ea693b 7307 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
a3c4230a
HZ
7308 if (i386_record_lea_modrm_addr (&ir, &tmpu64))
7309 return -1;
25ea693b 7310 if (record_full_arch_list_add_mem (tmpu64, 512))
a3c4230a
HZ
7311 return -1;
7312 }
7313 break;
7314
7315 case 1: /* fxrstor */
7316 {
7317 int i;
7318
25ea693b 7319 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
a3c4230a
HZ
7320
7321 for (i = I387_MM0_REGNUM (tdep);
7322 i386_mmx_regnum_p (gdbarch, i); i++)
25ea693b 7323 record_full_arch_list_add_reg (ir.regcache, i);
a3c4230a
HZ
7324
7325 for (i = I387_XMM0_REGNUM (tdep);
c131fcee 7326 i386_xmm_regnum_p (gdbarch, i); i++)
25ea693b 7327 record_full_arch_list_add_reg (ir.regcache, i);
a3c4230a
HZ
7328
7329 if (i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
25ea693b
MM
7330 record_full_arch_list_add_reg (ir.regcache,
7331 I387_MXCSR_REGNUM(tdep));
a3c4230a
HZ
7332
7333 for (i = I387_ST0_REGNUM (tdep);
7334 i386_fp_regnum_p (gdbarch, i); i++)
25ea693b 7335 record_full_arch_list_add_reg (ir.regcache, i);
a3c4230a
HZ
7336
7337 for (i = I387_FCTRL_REGNUM (tdep);
7338 i386_fpc_regnum_p (gdbarch, i); i++)
25ea693b 7339 record_full_arch_list_add_reg (ir.regcache, i);
a3c4230a
HZ
7340 }
7341 break;
7342
7343 case 2: /* ldmxcsr */
7344 if (!i386_mxcsr_regnum_p (gdbarch, I387_MXCSR_REGNUM(tdep)))
7345 goto no_support;
25ea693b 7346 record_full_arch_list_add_reg (ir.regcache, I387_MXCSR_REGNUM(tdep));
a3c4230a
HZ
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:
f9fda3f5
L
7493 /* Mask out PREFIX_ADDR. */
7494 switch ((prefixes & ~PREFIX_ADDR))
a3c4230a
HZ
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 }
7506reswitch_prefix_add:
7507 switch (opcode)
7508 {
7509 case 0x0f38:
7510 case 0x660f38:
7511 case 0xf20f38:
7512 case 0x0f3a:
7513 case 0x660f3a:
4ffa4fc7
PA
7514 if (record_read_memory (gdbarch, ir.addr, &opcode8, 1))
7515 return -1;
a3c4230a
HZ
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 */
4f7d61a8 7553 case 0x660f3807: /* phsubsw */
a3c4230a
HZ
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 */
56d2815c 7644 case 0x660f66: /* pcmpgtd */
a3c4230a
HZ
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 */
56d2815c 7660 case 0x660f76: /* pcmpeqd */
a3c4230a
HZ
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 */
4f7d61a8 7706 case 0xf20ff0: /* lddqu */
a3c4230a
HZ
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 */
56d2815c 7715 case 0x660ffa: /* psubd */
a3c4230a
HZ
7716 case 0x660ffb: /* psubq */
7717 case 0x660ffc: /* paddb */
7718 case 0x660ffd: /* paddw */
56d2815c 7719 case 0x660ffe: /* paddd */
a3c4230a
HZ
7720 if (i386_record_modrm (&ir))
7721 return -1;
7722 ir.reg |= rex_r;
c131fcee 7723 if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.reg))
a3c4230a 7724 goto no_support;
25ea693b
MM
7725 record_full_arch_list_add_reg (ir.regcache,
7726 I387_XMM0_REGNUM (tdep) + ir.reg);
a3c4230a 7727 if ((opcode & 0xfffffffc) == 0x660f3a60)
25ea693b 7728 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
a3c4230a
HZ
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;
1777feb0
MS
7755 if (!i386_xmm_regnum_p (gdbarch,
7756 I387_XMM0_REGNUM (tdep) + ir.rm))
a3c4230a 7757 goto no_support;
25ea693b
MM
7758 record_full_arch_list_add_reg (ir.regcache,
7759 I387_XMM0_REGNUM (tdep) + ir.rm);
a3c4230a
HZ
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 */
25ea693b 7812 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg | rex_r);
a3c4230a
HZ
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 */
4f7d61a8 7822 case 0x0f3807: /* phsubsw */
a3c4230a
HZ
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 */
56d2815c 7855 case 0x0f66: /* pcmpgtd */
a3c4230a
HZ
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 */
56d2815c 7866 case 0x0f76: /* pcmpeqd */
a3c4230a
HZ
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 */
56d2815c 7904 case 0x0ffa: /* psubd */
a3c4230a
HZ
7905 case 0x0ffb: /* psubq */
7906 case 0x0ffc: /* paddb */
7907 case 0x0ffd: /* paddw */
56d2815c 7908 case 0x0ffe: /* paddd */
a3c4230a
HZ
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;
25ea693b
MM
7913 record_full_arch_list_add_reg (ir.regcache,
7914 I387_MM0_REGNUM (tdep) + ir.reg);
a3c4230a
HZ
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;
25ea693b
MM
7924 record_full_arch_list_add_reg (ir.regcache,
7925 I387_MM0_REGNUM (tdep) + ir.rm);
a3c4230a
HZ
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;
c131fcee 7934 if (!i386_xmm_regnum_p (gdbarch, I387_XMM0_REGNUM (tdep) + ir.rm))
a3c4230a 7935 goto no_support;
25ea693b
MM
7936 record_full_arch_list_add_reg (ir.regcache,
7937 I387_XMM0_REGNUM (tdep) + ir.rm);
a3c4230a
HZ
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)
25ea693b 7945 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.rm | ir.rex_b);
a3c4230a
HZ
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;
25ea693b
MM
7964 record_full_arch_list_add_reg (ir.regcache,
7965 I387_MM0_REGNUM (tdep) + ir.rm);
a3c4230a
HZ
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;
25ea693b
MM
7978 I386_RECORD_FULL_ARCH_LIST_ADD_REG (ir.reg);
7979 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
a3c4230a
HZ
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;
1777feb0
MS
7988 if (!i386_xmm_regnum_p (gdbarch,
7989 I387_XMM0_REGNUM (tdep) + ir.rm))
a3c4230a 7990 goto no_support;
25ea693b
MM
7991 record_full_arch_list_add_reg (ir.regcache,
7992 I387_XMM0_REGNUM (tdep) + ir.rm);
a3c4230a
HZ
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 */
25ea693b 8007 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_EFLAGS_REGNUM);
a3c4230a
HZ
8008 break;
8009
8010 case 0x0ff7: /* maskmovq */
8011 regcache_raw_read_unsigned (ir.regcache,
8012 ir.regmap[X86_RECORD_REDI_REGNUM],
8013 &addr);
25ea693b 8014 if (record_full_arch_list_add_mem (addr, 64))
a3c4230a
HZ
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);
25ea693b 8022 if (record_full_arch_list_add_mem (addr, 128))
a3c4230a
HZ
8023 return -1;
8024 break;
8025
8026 default:
8027 goto no_support;
8028 break;
8029 }
8030 break;
7ad10968
HZ
8031
8032 default:
7ad10968
HZ
8033 goto no_support;
8034 break;
8035 }
8036
cf648174 8037 /* In the future, maybe still need to deal with need_dasm. */
25ea693b
MM
8038 I386_RECORD_FULL_ARCH_LIST_ADD_REG (X86_RECORD_REIP_REGNUM);
8039 if (record_full_arch_list_add_end ())
7ad10968
HZ
8040 return -1;
8041
8042 return 0;
8043
01fe1b41 8044 no_support:
a3c4230a
HZ
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));
7ad10968
HZ
8049 return -1;
8050}
8051
cf648174
HZ
8052static 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
7a697b8d 8061/* Check that the given address appears suitable for a fast
405f8e94 8062 tracepoint, which on x86-64 means that we need an instruction of at
7a697b8d
SS
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
405f8e94
SS
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
7a697b8d
SS
8068 of instruction to replace, and 0 if not, plus an explanatory
8069 string. */
8070
8071static int
8072i386_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
405f8e94
SS
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 }
7a697b8d
SS
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);
405f8e94
SS
8105 if (isize)
8106 *isize = len;
8107
7a697b8d
SS
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)
1777feb0
MS
8113 *msg = xstrprintf (_("; instruction is only %d bytes long, "
8114 "need at least %d bytes for the jump"),
7a697b8d
SS
8115 len, jumplen);
8116 return 0;
8117 }
405f8e94
SS
8118 else
8119 {
8120 if (msg)
8121 *msg = NULL;
8122 return 1;
8123 }
7a697b8d
SS
8124}
8125
90884b2b
L
8126static int
8127i386_validate_tdesc_p (struct gdbarch_tdep *tdep,
8128 struct tdesc_arch_data *tdesc_data)
8129{
8130 const struct target_desc *tdesc = tdep->tdesc;
c131fcee 8131 const struct tdesc_feature *feature_core;
01f9f808
MS
8132
8133 const struct tdesc_feature *feature_sse, *feature_avx, *feature_mpx,
8134 *feature_avx512;
90884b2b
L
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");
3a13a53b
L
8142 if (feature_core == NULL)
8143 return 0;
90884b2b
L
8144
8145 /* Get SSE registers. */
c131fcee 8146 feature_sse = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.sse");
90884b2b 8147
c131fcee
L
8148 /* Try AVX registers. */
8149 feature_avx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx");
8150
1dbcd68c
WT
8151 /* Try MPX registers. */
8152 feature_mpx = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx");
8153
01f9f808
MS
8154 /* Try AVX512 registers. */
8155 feature_avx512 = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512");
8156
90884b2b
L
8157 valid_p = 1;
8158
c131fcee 8159 /* The XCR0 bits. */
01f9f808
MS
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 }
c131fcee
L
8202 if (feature_avx)
8203 {
3a13a53b
L
8204 /* AVX register description requires SSE register description. */
8205 if (!feature_sse)
8206 return 0;
8207
01f9f808
MS
8208 if (!feature_avx512)
8209 tdep->xcr0 = I386_XSTATE_AVX_MASK;
c131fcee
L
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 }
3a13a53b 8224 else if (feature_sse)
c131fcee 8225 tdep->xcr0 = I386_XSTATE_SSE_MASK;
3a13a53b
L
8226 else
8227 {
8228 tdep->xcr0 = I386_XSTATE_X87_MASK;
8229 tdep->num_xmm_regs = 0;
8230 }
c131fcee 8231
90884b2b
L
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
3a13a53b
L
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 }
90884b2b 8245
1dbcd68c
WT
8246 if (feature_mpx)
8247 {
01f9f808 8248 tdep->xcr0 |= I386_XSTATE_MPX_MASK;
1dbcd68c
WT
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
90884b2b
L
8263 return valid_p;
8264}
8265
7ad10968
HZ
8266\f
8267static struct gdbarch *
8268i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8269{
8270 struct gdbarch_tdep *tdep;
8271 struct gdbarch *gdbarch;
90884b2b
L
8272 struct tdesc_arch_data *tdesc_data;
8273 const struct target_desc *tdesc;
1ba53b71 8274 int mm0_regnum;
c131fcee 8275 int ymm0_regnum;
1dbcd68c
WT
8276 int bnd0_regnum;
8277 int num_bnd_cooked;
01f9f808
MS
8278 int k0_regnum;
8279 int zmm0_regnum;
7ad10968
HZ
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. */
fc270c35 8287 tdep = XCNEW (struct gdbarch_tdep);
7ad10968
HZ
8288 gdbarch = gdbarch_alloc (&info, tdep);
8289
8290 /* General-purpose registers. */
7ad10968
HZ
8291 tdep->gregset_reg_offset = NULL;
8292 tdep->gregset_num_regs = I386_NUM_GREGS;
8293 tdep->sizeof_gregset = 0;
8294
8295 /* Floating-point registers. */
7ad10968
HZ
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
3a13a53b 8302 will show up in the output of "info all-registers". */
7ad10968
HZ
8303
8304 tdep->st0_regnum = I386_ST0_REGNUM;
8305
7ad10968
HZ
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
c131fcee
L
8319 tdep->xsave_xcr0_offset = -1;
8320
cf648174
HZ
8321 tdep->record_regmap = i386_record_regmap;
8322
205c306f
DM
8323 set_gdbarch_long_long_align_bit (gdbarch, 32);
8324
7ad10968
HZ
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
7ad10968
HZ
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. */
a9b8d892
JK
8387 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
8388 set_gdbarch_push_dummy_code (gdbarch, i386_push_dummy_code);
7ad10968 8389 set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
e04e5beb 8390 set_gdbarch_frame_align (gdbarch, i386_frame_align);
7ad10968
HZ
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
7ad10968
HZ
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);
90884b2b 8417 tdep->register_reggroup_p = i386_register_reggroup_p;
38c968cf 8418
143985b7
AF
8419 /* Helper for function argument information. */
8420 set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
8421
06da04c6 8422 /* Hook the function epilogue frame unwinder. This unwinder is
0d6c2135
MK
8423 appended to the list first, so that it supercedes the DWARF
8424 unwinder in function epilogues (where the DWARF unwinder
06da04c6
MS
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
0d6c2135 8429 to the list before the prologue-based unwinders, so that DWARF
06da04c6 8430 CFI info will be used if it is available. */
10458914 8431 dwarf2_append_unwinders (gdbarch);
6405b0a6 8432
acd5c798 8433 frame_base_set_default (gdbarch, &i386_frame_base);
6c0e89ed 8434
1ba53b71 8435 /* Pseudo registers may be changed by amd64_init_abi. */
3543a589
TT
8436 set_gdbarch_pseudo_register_read_value (gdbarch,
8437 i386_pseudo_register_read_value);
90884b2b
L
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
c131fcee
L
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
01f9f808
MS
8449 gap for the upper AVX, MPX and AVX512 registers. */
8450 set_gdbarch_num_regs (gdbarch, I386_AVX512_NUM_REGS);
90884b2b
L
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
c131fcee
L
8461 /* No upper YMM registers. */
8462 tdep->ymmh_register_names = NULL;
8463 tdep->ymm0h_regnum = -1;
8464
01f9f808
MS
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
1ba53b71
L
8477 tdep->num_byte_regs = 8;
8478 tdep->num_word_regs = 8;
8479 tdep->num_dword_regs = 0;
8480 tdep->num_mmx_regs = 8;
c131fcee 8481 tdep->num_ymm_regs = 0;
1ba53b71 8482
1dbcd68c
WT
8483 /* No MPX registers. */
8484 tdep->bnd0r_regnum = -1;
8485 tdep->bndcfgu_regnum = -1;
8486
01f9f808
MS
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
90884b2b
L
8493 tdesc_data = tdesc_data_alloc ();
8494
dde08ee1
PA
8495 set_gdbarch_relocate_instruction (gdbarch, i386_relocate_instruction);
8496
6710bf39
SS
8497 set_gdbarch_gen_return_address (gdbarch, i386_gen_return_address);
8498
c2170eef
MM
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
3ce1502b 8503 /* Hook in ABI-specific overrides, if they have been registered. */
90884b2b 8504 info.tdep_info = (void *) tdesc_data;
4be87837 8505 gdbarch_init_osabi (info, gdbarch);
3ce1502b 8506
c131fcee
L
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
1dbcd68c
WT
8515 num_bnd_cooked = (tdep->bnd0r_regnum > 0 ? I387_NUM_BND_REGS : 0);
8516
1ba53b71
L
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
c131fcee 8522 + tdep->num_mmx_regs
1dbcd68c 8523 + tdep->num_ymm_regs
01f9f808
MS
8524 + num_bnd_cooked
8525 + tdep->num_ymm_avx512_regs
8526 + tdep->num_zmm_regs));
1ba53b71 8527
90884b2b
L
8528 /* Target description may be changed. */
8529 tdesc = tdep->tdesc;
8530
90884b2b
L
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
1ba53b71
L
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
c131fcee 8540 ymm0_regnum = tdep->ax_regnum + tdep->num_word_regs;
1ba53b71
L
8541 if (tdep->num_dword_regs)
8542 {
1c6272a6 8543 /* Support dword pseudo-register if it hasn't been disabled. */
c131fcee
L
8544 tdep->eax_regnum = ymm0_regnum;
8545 ymm0_regnum += tdep->num_dword_regs;
1ba53b71
L
8546 }
8547 else
8548 tdep->eax_regnum = -1;
8549
c131fcee
L
8550 mm0_regnum = ymm0_regnum;
8551 if (tdep->num_ymm_regs)
8552 {
1c6272a6 8553 /* Support YMM pseudo-register if it is available. */
c131fcee
L
8554 tdep->ymm0_regnum = ymm0_regnum;
8555 mm0_regnum += tdep->num_ymm_regs;
8556 }
8557 else
8558 tdep->ymm0_regnum = -1;
8559
01f9f808
MS
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
1dbcd68c 8578 bnd0_regnum = mm0_regnum;
1ba53b71
L
8579 if (tdep->num_mmx_regs != 0)
8580 {
1c6272a6 8581 /* Support MMX pseudo-register if MMX hasn't been disabled. */
1ba53b71 8582 tdep->mm0_regnum = mm0_regnum;
1dbcd68c 8583 bnd0_regnum += tdep->num_mmx_regs;
1ba53b71
L
8584 }
8585 else
8586 tdep->mm0_regnum = -1;
8587
1dbcd68c
WT
8588 if (tdep->bnd0r_regnum > 0)
8589 tdep->bnd0_regnum = bnd0_regnum;
8590 else
8591 tdep-> bnd0_regnum = -1;
8592
06da04c6 8593 /* Hook in the legacy prologue-based unwinders last (fallback). */
a3fcb948 8594 frame_unwind_append_unwinder (gdbarch, &i386_stack_tramp_frame_unwind);
10458914
DJ
8595 frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
8596 frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
acd5c798 8597
8446b36a
MK
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
514f746b
AR
8605 set_gdbarch_skip_permanent_breakpoint (gdbarch,
8606 i386_skip_permanent_breakpoint);
8607
7a697b8d
SS
8608 set_gdbarch_fast_tracepoint_valid_at (gdbarch,
8609 i386_fast_tracepoint_valid_at);
8610
a62cc96e
AC
8611 return gdbarch;
8612}
8613
8201327c
MK
8614static enum gdb_osabi
8615i386_coff_osabi_sniffer (bfd *abfd)
8616{
762c5349
MK
8617 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
8618 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
8201327c
MK
8619 return GDB_OSABI_GO32;
8620
8621 return GDB_OSABI_UNKNOWN;
8622}
8201327c
MK
8623\f
8624
28e9e0f0
MK
8625/* Provide a prototype to silence -Wmissing-prototypes. */
8626void _initialize_i386_tdep (void);
8627
c906108c 8628void
fba45db2 8629_initialize_i386_tdep (void)
c906108c 8630{
a62cc96e
AC
8631 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
8632
fc338970 8633 /* Add the variable that controls the disassembly flavor. */
7ab04401
AC
8634 add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
8635 &disassembly_flavor, _("\
8636Set the disassembly flavor."), _("\
8637Show the disassembly flavor."), _("\
8638The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
8639 NULL,
8640 NULL, /* FIXME: i18n: */
8641 &setlist, &showlist);
8201327c
MK
8642
8643 /* Add the variable that controls the convention for returning
8644 structs. */
7ab04401
AC
8645 add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
8646 &struct_convention, _("\
8647Set the convention for returning small structs."), _("\
8648Show the convention for returning small structs."), _("\
8649Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
8650is \"default\"."),
8651 NULL,
8652 NULL, /* FIXME: i18n: */
8653 &setlist, &showlist);
8201327c
MK
8654
8655 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
8656 i386_coff_osabi_sniffer);
8201327c 8657
05816f70 8658 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
8201327c 8659 i386_svr4_init_abi);
05816f70 8660 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
8201327c 8661 i386_go32_init_abi);
38c968cf 8662
209bd28e 8663 /* Initialize the i386-specific register groups. */
38c968cf 8664 i386_init_reggroups ();
90884b2b
L
8665
8666 /* Initialize the standard target descriptions. */
8667 initialize_tdesc_i386 ();
3a13a53b 8668 initialize_tdesc_i386_mmx ();
c131fcee 8669 initialize_tdesc_i386_avx ();
1dbcd68c 8670 initialize_tdesc_i386_mpx ();
01f9f808 8671 initialize_tdesc_i386_avx512 ();
c8d5aac9
L
8672
8673 /* Tell remote stub that we support XML target description. */
8674 register_remote_support_xml ("i386");
c906108c 8675}
This page took 3.280754 seconds and 4 git commands to generate.