1 /* Intel 387 floating point stuff.
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "floatformat.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 #include "i386-tdep.h"
36 #include "i387-tdep.h"
37 #include "i386-xstate.h"
39 /* Print the floating point number specified by RAW. */
42 print_i387_value (struct gdbarch
*gdbarch
,
43 const gdb_byte
*raw
, struct ui_file
*file
)
47 /* Using extract_typed_floating here might affect the representation
48 of certain numbers such as NaNs, even if GDB is running natively.
49 This is fine since our caller already detects such special
50 numbers and we print the hexadecimal representation anyway. */
51 value
= extract_typed_floating (raw
, i387_ext_type (gdbarch
));
53 /* We try to print 19 digits. The last digit may or may not contain
54 garbage, but we'd better print one too many. We need enough room
55 to print the value, 1 position for the sign, 1 for the decimal
56 point, 19 for the digits and 6 for the exponent adds up to 27. */
57 #ifdef PRINTF_HAS_LONG_DOUBLE
58 fprintf_filtered (file
, " %-+27.19Lg", (long double) value
);
60 fprintf_filtered (file
, " %-+27.19g", (double) value
);
64 /* Print the classification for the register contents RAW. */
67 print_i387_ext (struct gdbarch
*gdbarch
,
68 const gdb_byte
*raw
, struct ui_file
*file
)
72 unsigned int exponent
;
73 unsigned long fraction
[2];
76 integer
= raw
[7] & 0x80;
77 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
78 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
79 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
80 | (raw
[5] << 8) | raw
[4]);
82 if (exponent
== 0x7fff && integer
)
84 if (fraction
[0] == 0x00000000 && fraction
[1] == 0x00000000)
86 fprintf_filtered (file
, " %cInf", (sign
? '-' : '+'));
87 else if (sign
&& fraction
[0] == 0x00000000 && fraction
[1] == 0x40000000)
88 /* Real Indefinite (QNaN). */
89 fputs_unfiltered (" Real Indefinite (QNaN)", file
);
90 else if (fraction
[1] & 0x40000000)
92 fputs_filtered (" QNaN", file
);
95 fputs_filtered (" SNaN", file
);
97 else if (exponent
< 0x7fff && exponent
> 0x0000 && integer
)
99 print_i387_value (gdbarch
, raw
, file
);
100 else if (exponent
== 0x0000)
102 /* Denormal or zero. */
103 print_i387_value (gdbarch
, raw
, file
);
106 /* Pseudo-denormal. */
107 fputs_filtered (" Pseudo-denormal", file
);
108 else if (fraction
[0] || fraction
[1])
110 fputs_filtered (" Denormal", file
);
114 fputs_filtered (" Unsupported", file
);
117 /* Print the status word STATUS. */
120 print_i387_status_word (unsigned int status
, struct ui_file
*file
)
122 fprintf_filtered (file
, "Status Word: %s",
123 hex_string_custom (status
, 4));
124 fputs_filtered (" ", file
);
125 fprintf_filtered (file
, " %s", (status
& 0x0001) ? "IE" : " ");
126 fprintf_filtered (file
, " %s", (status
& 0x0002) ? "DE" : " ");
127 fprintf_filtered (file
, " %s", (status
& 0x0004) ? "ZE" : " ");
128 fprintf_filtered (file
, " %s", (status
& 0x0008) ? "OE" : " ");
129 fprintf_filtered (file
, " %s", (status
& 0x0010) ? "UE" : " ");
130 fprintf_filtered (file
, " %s", (status
& 0x0020) ? "PE" : " ");
131 fputs_filtered (" ", file
);
132 fprintf_filtered (file
, " %s", (status
& 0x0080) ? "ES" : " ");
133 fputs_filtered (" ", file
);
134 fprintf_filtered (file
, " %s", (status
& 0x0040) ? "SF" : " ");
135 fputs_filtered (" ", file
);
136 fprintf_filtered (file
, " %s", (status
& 0x0100) ? "C0" : " ");
137 fprintf_filtered (file
, " %s", (status
& 0x0200) ? "C1" : " ");
138 fprintf_filtered (file
, " %s", (status
& 0x0400) ? "C2" : " ");
139 fprintf_filtered (file
, " %s", (status
& 0x4000) ? "C3" : " ");
141 fputs_filtered ("\n", file
);
143 fprintf_filtered (file
,
144 " TOP: %d\n", ((status
>> 11) & 7));
147 /* Print the control word CONTROL. */
150 print_i387_control_word (unsigned int control
, struct ui_file
*file
)
152 fprintf_filtered (file
, "Control Word: %s",
153 hex_string_custom (control
, 4));
154 fputs_filtered (" ", file
);
155 fprintf_filtered (file
, " %s", (control
& 0x0001) ? "IM" : " ");
156 fprintf_filtered (file
, " %s", (control
& 0x0002) ? "DM" : " ");
157 fprintf_filtered (file
, " %s", (control
& 0x0004) ? "ZM" : " ");
158 fprintf_filtered (file
, " %s", (control
& 0x0008) ? "OM" : " ");
159 fprintf_filtered (file
, " %s", (control
& 0x0010) ? "UM" : " ");
160 fprintf_filtered (file
, " %s", (control
& 0x0020) ? "PM" : " ");
162 fputs_filtered ("\n", file
);
164 fputs_filtered (" PC: ", file
);
165 switch ((control
>> 8) & 3)
168 fputs_filtered ("Single Precision (24-bits)\n", file
);
171 fputs_filtered ("Reserved\n", file
);
174 fputs_filtered ("Double Precision (53-bits)\n", file
);
177 fputs_filtered ("Extended Precision (64-bits)\n", file
);
181 fputs_filtered (" RC: ", file
);
182 switch ((control
>> 10) & 3)
185 fputs_filtered ("Round to nearest\n", file
);
188 fputs_filtered ("Round down\n", file
);
191 fputs_filtered ("Round up\n", file
);
194 fputs_filtered ("Round toward zero\n", file
);
199 /* Print out the i387 floating point state. Note that we ignore FRAME
200 in the code below. That's OK since floating-point registers are
201 never saved on the stack. */
204 i387_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
205 struct frame_info
*frame
, const char *args
)
207 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_frame_arch (frame
));
219 gdb_assert (gdbarch
== get_frame_arch (frame
));
221 fctrl
= get_frame_register_unsigned (frame
, I387_FCTRL_REGNUM (tdep
));
222 fstat
= get_frame_register_unsigned (frame
, I387_FSTAT_REGNUM (tdep
));
223 ftag
= get_frame_register_unsigned (frame
, I387_FTAG_REGNUM (tdep
));
224 fiseg
= get_frame_register_unsigned (frame
, I387_FISEG_REGNUM (tdep
));
225 fioff
= get_frame_register_unsigned (frame
, I387_FIOFF_REGNUM (tdep
));
226 foseg
= get_frame_register_unsigned (frame
, I387_FOSEG_REGNUM (tdep
));
227 fooff
= get_frame_register_unsigned (frame
, I387_FOOFF_REGNUM (tdep
));
228 fop
= get_frame_register_unsigned (frame
, I387_FOP_REGNUM (tdep
));
230 top
= ((fstat
>> 11) & 7);
232 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
234 gdb_byte raw
[I386_MAX_REGISTER_SIZE
];
235 int tag
= (ftag
>> (fpreg
* 2)) & 3;
238 fprintf_filtered (file
, "%sR%d: ", fpreg
== top
? "=>" : " ", fpreg
);
243 fputs_filtered ("Valid ", file
);
246 fputs_filtered ("Zero ", file
);
249 fputs_filtered ("Special ", file
);
252 fputs_filtered ("Empty ", file
);
256 get_frame_register (frame
, (fpreg
+ 8 - top
) % 8 + I387_ST0_REGNUM (tdep
),
259 fputs_filtered ("0x", file
);
260 for (i
= 9; i
>= 0; i
--)
261 fprintf_filtered (file
, "%02x", raw
[i
]);
264 print_i387_ext (gdbarch
, raw
, file
);
266 fputs_filtered ("\n", file
);
269 fputs_filtered ("\n", file
);
271 print_i387_status_word (fstat
, file
);
272 print_i387_control_word (fctrl
, file
);
273 fprintf_filtered (file
, "Tag Word: %s\n",
274 hex_string_custom (ftag
, 4));
275 fprintf_filtered (file
, "Instruction Pointer: %s:",
276 hex_string_custom (fiseg
, 2));
277 fprintf_filtered (file
, "%s\n", hex_string_custom (fioff
, 8));
278 fprintf_filtered (file
, "Operand Pointer: %s:",
279 hex_string_custom (foseg
, 2));
280 fprintf_filtered (file
, "%s\n", hex_string_custom (fooff
, 8));
281 fprintf_filtered (file
, "Opcode: %s\n",
282 hex_string_custom (fop
? (fop
| 0xd800) : 0, 4));
286 /* Return nonzero if a value of type TYPE stored in register REGNUM
287 needs any special handling. */
290 i387_convert_register_p (struct gdbarch
*gdbarch
, int regnum
, struct type
*type
)
292 if (i386_fp_regnum_p (gdbarch
, regnum
))
294 /* Floating point registers must be converted unless we are
295 accessing them in their hardware type. */
296 if (type
== i387_ext_type (gdbarch
))
305 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
306 return its contents in TO. */
309 i387_register_to_value (struct frame_info
*frame
, int regnum
,
310 struct type
*type
, gdb_byte
*to
)
312 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
313 gdb_byte from
[I386_MAX_REGISTER_SIZE
];
315 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
317 /* We only support floating-point values. */
318 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
320 warning (_("Cannot convert floating-point register value "
321 "to non-floating-point type."));
325 /* Convert to TYPE. */
326 get_frame_register (frame
, regnum
, from
);
327 convert_typed_floating (from
, i387_ext_type (gdbarch
), to
, type
);
330 /* Write the contents FROM of a value of type TYPE into register
331 REGNUM in frame FRAME. */
334 i387_value_to_register (struct frame_info
*frame
, int regnum
,
335 struct type
*type
, const gdb_byte
*from
)
337 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
338 gdb_byte to
[I386_MAX_REGISTER_SIZE
];
340 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
342 /* We only support floating-point values. */
343 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
345 warning (_("Cannot convert non-floating-point type "
346 "to floating-point register value."));
350 /* Convert from TYPE. */
351 convert_typed_floating (from
, type
, to
, i387_ext_type (gdbarch
));
352 put_frame_register (frame
, regnum
, to
);
356 /* Handle FSAVE and FXSAVE formats. */
358 /* At fsave_offset[REGNUM] you'll find the offset to the location in
359 the data structure used by the "fsave" instruction where GDB
360 register REGNUM is stored. */
362 static int fsave_offset
[] =
364 28 + 0 * 10, /* %st(0) ... */
371 28 + 7 * 10, /* ... %st(7). */
372 0, /* `fctrl' (16 bits). */
373 4, /* `fstat' (16 bits). */
374 8, /* `ftag' (16 bits). */
375 16, /* `fiseg' (16 bits). */
377 24, /* `foseg' (16 bits). */
379 18 /* `fop' (bottom 11 bits). */
382 #define FSAVE_ADDR(tdep, fsave, regnum) \
383 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
386 /* Fill register REGNUM in REGCACHE with the appropriate value from
387 *FSAVE. This function masks off any of the reserved bits in
391 i387_supply_fsave (struct regcache
*regcache
, int regnum
, const void *fsave
)
393 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
394 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
395 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
396 const gdb_byte
*regs
= fsave
;
399 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
401 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
402 if (regnum
== -1 || regnum
== i
)
406 regcache_raw_supply (regcache
, i
, NULL
);
410 /* Most of the FPU control registers occupy only 16 bits in the
411 fsave area. Give those a special treatment. */
412 if (i
>= I387_FCTRL_REGNUM (tdep
)
413 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
417 memcpy (val
, FSAVE_ADDR (tdep
, regs
, i
), 2);
419 if (i
== I387_FOP_REGNUM (tdep
))
420 val
[1] &= ((1 << 3) - 1);
421 regcache_raw_supply (regcache
, i
, val
);
424 regcache_raw_supply (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
427 /* Provide dummy values for the SSE registers. */
428 for (i
= I387_XMM0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
429 if (regnum
== -1 || regnum
== i
)
430 regcache_raw_supply (regcache
, i
, NULL
);
431 if (regnum
== -1 || regnum
== I387_MXCSR_REGNUM (tdep
))
435 store_unsigned_integer (buf
, 4, byte_order
, 0x1f80);
436 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), buf
);
440 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
441 with the value from REGCACHE. If REGNUM is -1, do this for all
442 registers. This function doesn't touch any of the reserved bits in
446 i387_collect_fsave (const struct regcache
*regcache
, int regnum
, void *fsave
)
448 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
449 gdb_byte
*regs
= fsave
;
452 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
454 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
455 if (regnum
== -1 || regnum
== i
)
457 /* Most of the FPU control registers occupy only 16 bits in
458 the fsave area. Give those a special treatment. */
459 if (i
>= I387_FCTRL_REGNUM (tdep
)
460 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
464 regcache_raw_collect (regcache
, i
, buf
);
466 if (i
== I387_FOP_REGNUM (tdep
))
468 /* The opcode occupies only 11 bits. Make sure we
469 don't touch the other bits. */
470 buf
[1] &= ((1 << 3) - 1);
471 buf
[1] |= ((FSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
473 memcpy (FSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
476 regcache_raw_collect (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
481 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
482 the data structure used by the "fxsave" instruction where GDB
483 register REGNUM is stored. */
485 static int fxsave_offset
[] =
487 32, /* %st(0) through ... */
494 144, /* ... %st(7) (80 bits each). */
495 0, /* `fctrl' (16 bits). */
496 2, /* `fstat' (16 bits). */
497 4, /* `ftag' (16 bits). */
498 12, /* `fiseg' (16 bits). */
500 20, /* `foseg' (16 bits). */
502 6, /* `fop' (bottom 11 bits). */
503 160 + 0 * 16, /* %xmm0 through ... */
518 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
521 #define FXSAVE_ADDR(tdep, fxsave, regnum) \
522 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
524 /* We made an unfortunate choice in putting %mxcsr after the SSE
525 registers %xmm0-%xmm7 instead of before, since it makes supporting
526 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
527 don't include the offset for %mxcsr here above. */
529 #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
531 static int i387_tag (const gdb_byte
*raw
);
534 /* Fill register REGNUM in REGCACHE with the appropriate
535 floating-point or SSE register value from *FXSAVE. This function
536 masks off any of the reserved bits in *FXSAVE. */
539 i387_supply_fxsave (struct regcache
*regcache
, int regnum
, const void *fxsave
)
541 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
542 const gdb_byte
*regs
= fxsave
;
545 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
546 gdb_assert (tdep
->num_xmm_regs
> 0);
548 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
549 if (regnum
== -1 || regnum
== i
)
553 regcache_raw_supply (regcache
, i
, NULL
);
557 /* Most of the FPU control registers occupy only 16 bits in
558 the fxsave area. Give those a special treatment. */
559 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
560 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
564 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
566 if (i
== I387_FOP_REGNUM (tdep
))
567 val
[1] &= ((1 << 3) - 1);
568 else if (i
== I387_FTAG_REGNUM (tdep
))
570 /* The fxsave area contains a simplified version of
571 the tag word. We have to look at the actual 80-bit
572 FP data to recreate the traditional i387 tag word. */
574 unsigned long ftag
= 0;
578 top
= ((FXSAVE_ADDR (tdep
, regs
,
579 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
582 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
586 if (val
[0] & (1 << fpreg
))
588 int regnum
= (fpreg
+ 8 - top
) % 8
589 + I387_ST0_REGNUM (tdep
);
590 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, regnum
));
595 ftag
|= tag
<< (2 * fpreg
);
597 val
[0] = ftag
& 0xff;
598 val
[1] = (ftag
>> 8) & 0xff;
600 regcache_raw_supply (regcache
, i
, val
);
603 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
606 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
609 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), NULL
);
611 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
),
612 FXSAVE_MXCSR_ADDR (regs
));
616 /* Fill register REGNUM (if it is a floating-point or SSE register) in
617 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
618 all registers. This function doesn't touch any of the reserved
622 i387_collect_fxsave (const struct regcache
*regcache
, int regnum
, void *fxsave
)
624 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
625 gdb_byte
*regs
= fxsave
;
628 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
629 gdb_assert (tdep
->num_xmm_regs
> 0);
631 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
632 if (regnum
== -1 || regnum
== i
)
634 /* Most of the FPU control registers occupy only 16 bits in
635 the fxsave area. Give those a special treatment. */
636 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
637 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
641 regcache_raw_collect (regcache
, i
, buf
);
643 if (i
== I387_FOP_REGNUM (tdep
))
645 /* The opcode occupies only 11 bits. Make sure we
646 don't touch the other bits. */
647 buf
[1] &= ((1 << 3) - 1);
648 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
650 else if (i
== I387_FTAG_REGNUM (tdep
))
652 /* Converting back is much easier. */
657 ftag
= (buf
[1] << 8) | buf
[0];
661 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
663 int tag
= (ftag
>> (fpreg
* 2)) & 3;
666 buf
[0] |= (1 << fpreg
);
669 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
672 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
675 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
676 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
677 FXSAVE_MXCSR_ADDR (regs
));
680 /* `xstate_bv' is at byte offset 512. */
681 #define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
683 /* At xsave_avxh_offset[REGNUM] you'll find the offset to the location in
684 the upper 128bit of AVX register data structure used by the "xsave"
685 instruction where GDB register REGNUM is stored. */
687 static int xsave_avxh_offset
[] =
689 576 + 0 * 16, /* Upper 128bit of %ymm0 through ... */
704 576 + 15 * 16 /* Upper 128bit of ... %ymm15 (128 bits each). */
707 #define XSAVE_AVXH_ADDR(tdep, xsave, regnum) \
708 (xsave + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
710 /* Similar to i387_supply_fxsave, but use XSAVE extended state. */
713 i387_supply_xsave (struct regcache
*regcache
, int regnum
,
716 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
717 const gdb_byte
*regs
= xsave
;
719 unsigned int clear_bv
;
727 all
= x87
| sse
| avxh
730 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
731 gdb_assert (tdep
->num_xmm_regs
> 0);
735 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
736 && regnum
< I387_YMMENDH_REGNUM (tdep
))
738 else if (regnum
>= I387_XMM0_REGNUM(tdep
)
739 && regnum
< I387_MXCSR_REGNUM (tdep
))
741 else if (regnum
>= I387_ST0_REGNUM (tdep
)
742 && regnum
< I387_FCTRL_REGNUM (tdep
))
747 if (regs
!= NULL
&& regclass
!= none
)
749 /* Get `xstat_bv'. */
750 const gdb_byte
*xstate_bv_p
= XSAVE_XSTATE_BV_ADDR (regs
);
752 /* The supported bits in `xstat_bv' are 1 byte. Clear part in
753 vector registers if its bit in xstat_bv is zero. */
754 clear_bv
= (~(*xstate_bv_p
)) & tdep
->xcr0
;
757 clear_bv
= I386_XSTATE_AVX_MASK
;
765 if ((clear_bv
& I386_XSTATE_AVX
))
768 p
= XSAVE_AVXH_ADDR (tdep
, regs
, regnum
);
769 regcache_raw_supply (regcache
, regnum
, p
);
773 if ((clear_bv
& I386_XSTATE_SSE
))
776 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
777 regcache_raw_supply (regcache
, regnum
, p
);
781 if ((clear_bv
& I386_XSTATE_X87
))
784 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
785 regcache_raw_supply (regcache
, regnum
, p
);
789 /* Hanle the upper YMM registers. */
790 if ((tdep
->xcr0
& I386_XSTATE_AVX
))
792 if ((clear_bv
& I386_XSTATE_AVX
))
797 for (i
= I387_YMM0H_REGNUM (tdep
);
798 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
801 p
= XSAVE_AVXH_ADDR (tdep
, regs
, i
);
802 regcache_raw_supply (regcache
, i
, p
);
806 /* Handle the XMM registers. */
807 if ((tdep
->xcr0
& I386_XSTATE_SSE
))
809 if ((clear_bv
& I386_XSTATE_SSE
))
814 for (i
= I387_XMM0_REGNUM (tdep
);
815 i
< I387_MXCSR_REGNUM (tdep
); i
++)
818 p
= FXSAVE_ADDR (tdep
, regs
, i
);
819 regcache_raw_supply (regcache
, i
, p
);
823 /* Handle the x87 registers. */
824 if ((tdep
->xcr0
& I386_XSTATE_X87
))
826 if ((clear_bv
& I386_XSTATE_X87
))
831 for (i
= I387_ST0_REGNUM (tdep
);
832 i
< I387_FCTRL_REGNUM (tdep
); i
++)
835 p
= FXSAVE_ADDR (tdep
, regs
, i
);
836 regcache_raw_supply (regcache
, i
, p
);
842 /* Only handle x87 control registers. */
843 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
844 if (regnum
== -1 || regnum
== i
)
848 regcache_raw_supply (regcache
, i
, NULL
);
852 /* Most of the FPU control registers occupy only 16 bits in
853 the xsave extended state. Give those a special treatment. */
854 if (i
!= I387_FIOFF_REGNUM (tdep
)
855 && i
!= I387_FOOFF_REGNUM (tdep
))
859 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
861 if (i
== I387_FOP_REGNUM (tdep
))
862 val
[1] &= ((1 << 3) - 1);
863 else if (i
== I387_FTAG_REGNUM (tdep
))
865 /* The fxsave area contains a simplified version of
866 the tag word. We have to look at the actual 80-bit
867 FP data to recreate the traditional i387 tag word. */
869 unsigned long ftag
= 0;
873 top
= ((FXSAVE_ADDR (tdep
, regs
,
874 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
877 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
881 if (val
[0] & (1 << fpreg
))
883 int regnum
= (fpreg
+ 8 - top
) % 8
884 + I387_ST0_REGNUM (tdep
);
885 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, regnum
));
890 ftag
|= tag
<< (2 * fpreg
);
892 val
[0] = ftag
& 0xff;
893 val
[1] = (ftag
>> 8) & 0xff;
895 regcache_raw_supply (regcache
, i
, val
);
898 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
901 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
903 p
= regs
== NULL
? NULL
: FXSAVE_MXCSR_ADDR (regs
);
904 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), p
);
908 /* Similar to i387_collect_fxsave, but use XSAVE extended state. */
911 i387_collect_xsave (const struct regcache
*regcache
, int regnum
,
912 void *xsave
, int gcore
)
914 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
915 gdb_byte
*regs
= xsave
;
924 all
= x87
| sse
| avxh
927 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
928 gdb_assert (tdep
->num_xmm_regs
> 0);
932 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
933 && regnum
< I387_YMMENDH_REGNUM (tdep
))
935 else if (regnum
>= I387_XMM0_REGNUM(tdep
)
936 && regnum
< I387_MXCSR_REGNUM (tdep
))
938 else if (regnum
>= I387_ST0_REGNUM (tdep
)
939 && regnum
< I387_FCTRL_REGNUM (tdep
))
946 /* Clear XSAVE extended state. */
947 memset (regs
, 0, I386_XSTATE_SIZE (tdep
->xcr0
));
949 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
950 if (tdep
->xsave_xcr0_offset
!= -1)
951 memcpy (regs
+ tdep
->xsave_xcr0_offset
, &tdep
->xcr0
, 8);
952 memcpy (XSAVE_XSTATE_BV_ADDR (regs
), &tdep
->xcr0
, 8);
955 if ((regclass
& check
))
957 gdb_byte raw
[I386_MAX_REGISTER_SIZE
];
958 gdb_byte
*xstate_bv_p
= XSAVE_XSTATE_BV_ADDR (regs
);
959 unsigned int xstate_bv
= 0;
960 /* The supported bits in `xstat_bv' are 1 byte. */
961 unsigned int clear_bv
= (~(*xstate_bv_p
)) & tdep
->xcr0
;
964 /* Clear register set if its bit in xstat_bv is zero. */
967 if ((clear_bv
& I386_XSTATE_AVX
))
968 for (i
= I387_YMM0H_REGNUM (tdep
);
969 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
970 memset (XSAVE_AVXH_ADDR (tdep
, regs
, i
), 0, 16);
972 if ((clear_bv
& I386_XSTATE_SSE
))
973 for (i
= I387_XMM0_REGNUM (tdep
);
974 i
< I387_MXCSR_REGNUM (tdep
); i
++)
975 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 16);
977 if ((clear_bv
& I386_XSTATE_X87
))
978 for (i
= I387_ST0_REGNUM (tdep
);
979 i
< I387_FCTRL_REGNUM (tdep
); i
++)
980 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 10);
985 /* Check if any upper YMM registers are changed. */
986 if ((tdep
->xcr0
& I386_XSTATE_AVX
))
987 for (i
= I387_YMM0H_REGNUM (tdep
);
988 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
990 regcache_raw_collect (regcache
, i
, raw
);
991 p
= XSAVE_AVXH_ADDR (tdep
, regs
, i
);
992 if (memcmp (raw
, p
, 16))
994 xstate_bv
|= I386_XSTATE_AVX
;
999 /* Check if any SSE registers are changed. */
1000 if ((tdep
->xcr0
& I386_XSTATE_SSE
))
1001 for (i
= I387_XMM0_REGNUM (tdep
);
1002 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1004 regcache_raw_collect (regcache
, i
, raw
);
1005 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1006 if (memcmp (raw
, p
, 16))
1008 xstate_bv
|= I386_XSTATE_SSE
;
1009 memcpy (p
, raw
, 16);
1013 /* Check if any X87 registers are changed. */
1014 if ((tdep
->xcr0
& I386_XSTATE_X87
))
1015 for (i
= I387_ST0_REGNUM (tdep
);
1016 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1018 regcache_raw_collect (regcache
, i
, raw
);
1019 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1020 if (memcmp (raw
, p
, 10))
1022 xstate_bv
|= I386_XSTATE_X87
;
1023 memcpy (p
, raw
, 10);
1029 /* Check if REGNUM is changed. */
1030 regcache_raw_collect (regcache
, regnum
, raw
);
1035 internal_error (__FILE__
, __LINE__
,
1036 _("invalid i387 regclass"));
1039 /* This is an upper YMM register. */
1040 p
= XSAVE_AVXH_ADDR (tdep
, regs
, regnum
);
1041 if (memcmp (raw
, p
, 16))
1043 xstate_bv
|= I386_XSTATE_AVX
;
1044 memcpy (p
, raw
, 16);
1049 /* This is an SSE register. */
1050 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1051 if (memcmp (raw
, p
, 16))
1053 xstate_bv
|= I386_XSTATE_SSE
;
1054 memcpy (p
, raw
, 16);
1059 /* This is an x87 register. */
1060 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1061 if (memcmp (raw
, p
, 10))
1063 xstate_bv
|= I386_XSTATE_X87
;
1064 memcpy (p
, raw
, 10);
1070 /* Update the corresponding bits in `xstate_bv' if any SSE/AVX
1071 registers are changed. */
1074 /* The supported bits in `xstat_bv' are 1 byte. */
1075 *xstate_bv_p
|= (gdb_byte
) xstate_bv
;
1080 internal_error (__FILE__
, __LINE__
,
1081 _("invalid i387 regclass"));
1089 /* Register REGNUM has been updated. Return. */
1095 /* Return if REGNUM isn't changed. */
1096 if (regclass
!= all
)
1101 /* Only handle x87 control registers. */
1102 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1103 if (regnum
== -1 || regnum
== i
)
1105 /* Most of the FPU control registers occupy only 16 bits in
1106 the xsave extended state. Give those a special treatment. */
1107 if (i
!= I387_FIOFF_REGNUM (tdep
)
1108 && i
!= I387_FOOFF_REGNUM (tdep
))
1112 regcache_raw_collect (regcache
, i
, buf
);
1114 if (i
== I387_FOP_REGNUM (tdep
))
1116 /* The opcode occupies only 11 bits. Make sure we
1117 don't touch the other bits. */
1118 buf
[1] &= ((1 << 3) - 1);
1119 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
1121 else if (i
== I387_FTAG_REGNUM (tdep
))
1123 /* Converting back is much easier. */
1125 unsigned short ftag
;
1128 ftag
= (buf
[1] << 8) | buf
[0];
1132 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1134 int tag
= (ftag
>> (fpreg
* 2)) & 3;
1137 buf
[0] |= (1 << fpreg
);
1140 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
1143 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1146 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
1147 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
1148 FXSAVE_MXCSR_ADDR (regs
));
1151 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1155 i387_tag (const gdb_byte
*raw
)
1158 unsigned int exponent
;
1159 unsigned long fraction
[2];
1161 integer
= raw
[7] & 0x80;
1162 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
1163 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
1164 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
1165 | (raw
[5] << 8) | raw
[4]);
1167 if (exponent
== 0x7fff)
1172 else if (exponent
== 0x0000)
1174 if (fraction
[0] == 0x0000 && fraction
[1] == 0x0000 && !integer
)
1200 /* Prepare the FPU stack in REGCACHE for a function return. */
1203 i387_return_value (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1205 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1208 /* Set the top of the floating-point register stack to 7. The
1209 actual value doesn't really matter, but 7 is what a normal
1210 function return would end up with if the program started out with
1211 a freshly initialized FPU. */
1212 regcache_raw_read_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), &fstat
);
1214 regcache_raw_write_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), fstat
);
1216 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1217 floating-point register stack to 7, the appropriate value for the
1218 tag word is 0x3fff. */
1219 regcache_raw_write_unsigned (regcache
, I387_FTAG_REGNUM (tdep
), 0x3fff);