* gdbtypes.h (builtin_type_ieee_single, builtin_type_ieee_double,
[deliverable/binutils-gdb.git] / gdb / i387-tdep.c
1 /* Intel 387 floating point stuff.
2
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "doublest.h"
23 #include "floatformat.h"
24 #include "frame.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "language.h"
28 #include "regcache.h"
29 #include "value.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 #include "i386-tdep.h"
35 #include "i387-tdep.h"
36
37 /* Print the floating point number specified by RAW. */
38
39 static void
40 print_i387_value (struct gdbarch *gdbarch,
41 const gdb_byte *raw, struct ui_file *file)
42 {
43 DOUBLEST value;
44
45 /* Using extract_typed_floating here might affect the representation
46 of certain numbers such as NaNs, even if GDB is running natively.
47 This is fine since our caller already detects such special
48 numbers and we print the hexadecimal representation anyway. */
49 value = extract_typed_floating (raw, i387_ext_type (gdbarch));
50
51 /* We try to print 19 digits. The last digit may or may not contain
52 garbage, but we'd better print one too many. We need enough room
53 to print the value, 1 position for the sign, 1 for the decimal
54 point, 19 for the digits and 6 for the exponent adds up to 27. */
55 #ifdef PRINTF_HAS_LONG_DOUBLE
56 fprintf_filtered (file, " %-+27.19Lg", (long double) value);
57 #else
58 fprintf_filtered (file, " %-+27.19g", (double) value);
59 #endif
60 }
61
62 /* Print the classification for the register contents RAW. */
63
64 static void
65 print_i387_ext (struct gdbarch *gdbarch,
66 const gdb_byte *raw, struct ui_file *file)
67 {
68 int sign;
69 int integer;
70 unsigned int exponent;
71 unsigned long fraction[2];
72
73 sign = raw[9] & 0x80;
74 integer = raw[7] & 0x80;
75 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
76 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
77 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
78 | (raw[5] << 8) | raw[4]);
79
80 if (exponent == 0x7fff && integer)
81 {
82 if (fraction[0] == 0x00000000 && fraction[1] == 0x00000000)
83 /* Infinity. */
84 fprintf_filtered (file, " %cInf", (sign ? '-' : '+'));
85 else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000)
86 /* Real Indefinite (QNaN). */
87 fputs_unfiltered (" Real Indefinite (QNaN)", file);
88 else if (fraction[1] & 0x40000000)
89 /* QNaN. */
90 fputs_filtered (" QNaN", file);
91 else
92 /* SNaN. */
93 fputs_filtered (" SNaN", file);
94 }
95 else if (exponent < 0x7fff && exponent > 0x0000 && integer)
96 /* Normal. */
97 print_i387_value (gdbarch, raw, file);
98 else if (exponent == 0x0000)
99 {
100 /* Denormal or zero. */
101 print_i387_value (gdbarch, raw, file);
102
103 if (integer)
104 /* Pseudo-denormal. */
105 fputs_filtered (" Pseudo-denormal", file);
106 else if (fraction[0] || fraction[1])
107 /* Denormal. */
108 fputs_filtered (" Denormal", file);
109 }
110 else
111 /* Unsupported. */
112 fputs_filtered (" Unsupported", file);
113 }
114
115 /* Print the status word STATUS. */
116
117 static void
118 print_i387_status_word (unsigned int status, struct ui_file *file)
119 {
120 fprintf_filtered (file, "Status Word: %s",
121 hex_string_custom (status, 4));
122 fputs_filtered (" ", file);
123 fprintf_filtered (file, " %s", (status & 0x0001) ? "IE" : " ");
124 fprintf_filtered (file, " %s", (status & 0x0002) ? "DE" : " ");
125 fprintf_filtered (file, " %s", (status & 0x0004) ? "ZE" : " ");
126 fprintf_filtered (file, " %s", (status & 0x0008) ? "OE" : " ");
127 fprintf_filtered (file, " %s", (status & 0x0010) ? "UE" : " ");
128 fprintf_filtered (file, " %s", (status & 0x0020) ? "PE" : " ");
129 fputs_filtered (" ", file);
130 fprintf_filtered (file, " %s", (status & 0x0080) ? "ES" : " ");
131 fputs_filtered (" ", file);
132 fprintf_filtered (file, " %s", (status & 0x0040) ? "SF" : " ");
133 fputs_filtered (" ", file);
134 fprintf_filtered (file, " %s", (status & 0x0100) ? "C0" : " ");
135 fprintf_filtered (file, " %s", (status & 0x0200) ? "C1" : " ");
136 fprintf_filtered (file, " %s", (status & 0x0400) ? "C2" : " ");
137 fprintf_filtered (file, " %s", (status & 0x4000) ? "C3" : " ");
138
139 fputs_filtered ("\n", file);
140
141 fprintf_filtered (file,
142 " TOP: %d\n", ((status >> 11) & 7));
143 }
144
145 /* Print the control word CONTROL. */
146
147 static void
148 print_i387_control_word (unsigned int control, struct ui_file *file)
149 {
150 fprintf_filtered (file, "Control Word: %s",
151 hex_string_custom (control, 4));
152 fputs_filtered (" ", file);
153 fprintf_filtered (file, " %s", (control & 0x0001) ? "IM" : " ");
154 fprintf_filtered (file, " %s", (control & 0x0002) ? "DM" : " ");
155 fprintf_filtered (file, " %s", (control & 0x0004) ? "ZM" : " ");
156 fprintf_filtered (file, " %s", (control & 0x0008) ? "OM" : " ");
157 fprintf_filtered (file, " %s", (control & 0x0010) ? "UM" : " ");
158 fprintf_filtered (file, " %s", (control & 0x0020) ? "PM" : " ");
159
160 fputs_filtered ("\n", file);
161
162 fputs_filtered (" PC: ", file);
163 switch ((control >> 8) & 3)
164 {
165 case 0:
166 fputs_filtered ("Single Precision (24-bits)\n", file);
167 break;
168 case 1:
169 fputs_filtered ("Reserved\n", file);
170 break;
171 case 2:
172 fputs_filtered ("Double Precision (53-bits)\n", file);
173 break;
174 case 3:
175 fputs_filtered ("Extended Precision (64-bits)\n", file);
176 break;
177 }
178
179 fputs_filtered (" RC: ", file);
180 switch ((control >> 10) & 3)
181 {
182 case 0:
183 fputs_filtered ("Round to nearest\n", file);
184 break;
185 case 1:
186 fputs_filtered ("Round down\n", file);
187 break;
188 case 2:
189 fputs_filtered ("Round up\n", file);
190 break;
191 case 3:
192 fputs_filtered ("Round toward zero\n", file);
193 break;
194 }
195 }
196
197 /* Print out the i387 floating point state. Note that we ignore FRAME
198 in the code below. That's OK since floating-point registers are
199 never saved on the stack. */
200
201 void
202 i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
203 struct frame_info *frame, const char *args)
204 {
205 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
206 gdb_byte buf[4];
207 ULONGEST fctrl;
208 ULONGEST fstat;
209 ULONGEST ftag;
210 ULONGEST fiseg;
211 ULONGEST fioff;
212 ULONGEST foseg;
213 ULONGEST fooff;
214 ULONGEST fop;
215 int fpreg;
216 int top;
217
218 gdb_assert (gdbarch == get_frame_arch (frame));
219
220 fctrl = get_frame_register_unsigned (frame, I387_FCTRL_REGNUM (tdep));
221 fstat = get_frame_register_unsigned (frame, I387_FSTAT_REGNUM (tdep));
222 ftag = get_frame_register_unsigned (frame, I387_FTAG_REGNUM (tdep));
223 fiseg = get_frame_register_unsigned (frame, I387_FISEG_REGNUM (tdep));
224 fioff = get_frame_register_unsigned (frame, I387_FIOFF_REGNUM (tdep));
225 foseg = get_frame_register_unsigned (frame, I387_FOSEG_REGNUM (tdep));
226 fooff = get_frame_register_unsigned (frame, I387_FOOFF_REGNUM (tdep));
227 fop = get_frame_register_unsigned (frame, I387_FOP_REGNUM (tdep));
228
229 top = ((fstat >> 11) & 7);
230
231 for (fpreg = 7; fpreg >= 0; fpreg--)
232 {
233 gdb_byte raw[I386_MAX_REGISTER_SIZE];
234 int tag = (ftag >> (fpreg * 2)) & 3;
235 int i;
236
237 fprintf_filtered (file, "%sR%d: ", fpreg == top ? "=>" : " ", fpreg);
238
239 switch (tag)
240 {
241 case 0:
242 fputs_filtered ("Valid ", file);
243 break;
244 case 1:
245 fputs_filtered ("Zero ", file);
246 break;
247 case 2:
248 fputs_filtered ("Special ", file);
249 break;
250 case 3:
251 fputs_filtered ("Empty ", file);
252 break;
253 }
254
255 get_frame_register (frame, (fpreg + 8 - top) % 8 + I387_ST0_REGNUM (tdep),
256 raw);
257
258 fputs_filtered ("0x", file);
259 for (i = 9; i >= 0; i--)
260 fprintf_filtered (file, "%02x", raw[i]);
261
262 if (tag != 3)
263 print_i387_ext (gdbarch, raw, file);
264
265 fputs_filtered ("\n", file);
266 }
267
268 fputs_filtered ("\n", file);
269
270 print_i387_status_word (fstat, file);
271 print_i387_control_word (fctrl, file);
272 fprintf_filtered (file, "Tag Word: %s\n",
273 hex_string_custom (ftag, 4));
274 fprintf_filtered (file, "Instruction Pointer: %s:",
275 hex_string_custom (fiseg, 2));
276 fprintf_filtered (file, "%s\n", hex_string_custom (fioff, 8));
277 fprintf_filtered (file, "Operand Pointer: %s:",
278 hex_string_custom (foseg, 2));
279 fprintf_filtered (file, "%s\n", hex_string_custom (fooff, 8));
280 fprintf_filtered (file, "Opcode: %s\n",
281 hex_string_custom (fop ? (fop | 0xd800) : 0, 4));
282 }
283 \f
284
285 /* Return nonzero if a value of type TYPE stored in register REGNUM
286 needs any special handling. */
287
288 int
289 i387_convert_register_p (struct gdbarch *gdbarch, int regnum, struct type *type)
290 {
291 if (i386_fp_regnum_p (gdbarch, regnum))
292 {
293 /* Floating point registers must be converted unless we are
294 accessing them in their hardware type. */
295 if (type == i387_ext_type (gdbarch))
296 return 0;
297 else
298 return 1;
299 }
300
301 return 0;
302 }
303
304 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
305 return its contents in TO. */
306
307 void
308 i387_register_to_value (struct frame_info *frame, int regnum,
309 struct type *type, gdb_byte *to)
310 {
311 struct gdbarch *gdbarch = get_frame_arch (frame);
312 gdb_byte from[I386_MAX_REGISTER_SIZE];
313
314 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
315
316 /* We only support floating-point values. */
317 if (TYPE_CODE (type) != TYPE_CODE_FLT)
318 {
319 warning (_("Cannot convert floating-point register value "
320 "to non-floating-point type."));
321 return;
322 }
323
324 /* Convert to TYPE. */
325 get_frame_register (frame, regnum, from);
326 convert_typed_floating (from, i387_ext_type (gdbarch), to, type);
327 }
328
329 /* Write the contents FROM of a value of type TYPE into register
330 REGNUM in frame FRAME. */
331
332 void
333 i387_value_to_register (struct frame_info *frame, int regnum,
334 struct type *type, const gdb_byte *from)
335 {
336 struct gdbarch *gdbarch = get_frame_arch (frame);
337 gdb_byte to[I386_MAX_REGISTER_SIZE];
338
339 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
340
341 /* We only support floating-point values. */
342 if (TYPE_CODE (type) != TYPE_CODE_FLT)
343 {
344 warning (_("Cannot convert non-floating-point type "
345 "to floating-point register value."));
346 return;
347 }
348
349 /* Convert from TYPE. */
350 convert_typed_floating (from, type, to, i387_ext_type (gdbarch));
351 put_frame_register (frame, regnum, to);
352 }
353 \f
354
355 /* Handle FSAVE and FXSAVE formats. */
356
357 /* At fsave_offset[REGNUM] you'll find the offset to the location in
358 the data structure used by the "fsave" instruction where GDB
359 register REGNUM is stored. */
360
361 static int fsave_offset[] =
362 {
363 28 + 0 * 10, /* %st(0) ... */
364 28 + 1 * 10,
365 28 + 2 * 10,
366 28 + 3 * 10,
367 28 + 4 * 10,
368 28 + 5 * 10,
369 28 + 6 * 10,
370 28 + 7 * 10, /* ... %st(7). */
371 0, /* `fctrl' (16 bits). */
372 4, /* `fstat' (16 bits). */
373 8, /* `ftag' (16 bits). */
374 16, /* `fiseg' (16 bits). */
375 12, /* `fioff'. */
376 24, /* `foseg' (16 bits). */
377 20, /* `fooff'. */
378 18 /* `fop' (bottom 11 bits). */
379 };
380
381 #define FSAVE_ADDR(tdep, fsave, regnum) \
382 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
383 \f
384
385 /* Fill register REGNUM in REGCACHE with the appropriate value from
386 *FSAVE. This function masks off any of the reserved bits in
387 *FSAVE. */
388
389 void
390 i387_supply_fsave (struct regcache *regcache, int regnum, const void *fsave)
391 {
392 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
393 const gdb_byte *regs = fsave;
394 int i;
395
396 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
397
398 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
399 if (regnum == -1 || regnum == i)
400 {
401 if (fsave == NULL)
402 {
403 regcache_raw_supply (regcache, i, NULL);
404 continue;
405 }
406
407 /* Most of the FPU control registers occupy only 16 bits in the
408 fsave area. Give those a special treatment. */
409 if (i >= I387_FCTRL_REGNUM (tdep)
410 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
411 {
412 gdb_byte val[4];
413
414 memcpy (val, FSAVE_ADDR (tdep, regs, i), 2);
415 val[2] = val[3] = 0;
416 if (i == I387_FOP_REGNUM (tdep))
417 val[1] &= ((1 << 3) - 1);
418 regcache_raw_supply (regcache, i, val);
419 }
420 else
421 regcache_raw_supply (regcache, i, FSAVE_ADDR (tdep, regs, i));
422 }
423
424 /* Provide dummy values for the SSE registers. */
425 for (i = I387_XMM0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
426 if (regnum == -1 || regnum == i)
427 regcache_raw_supply (regcache, i, NULL);
428 if (regnum == -1 || regnum == I387_MXCSR_REGNUM (tdep))
429 {
430 gdb_byte buf[4];
431
432 store_unsigned_integer (buf, 4, 0x1f80);
433 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), buf);
434 }
435 }
436
437 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
438 with the value from REGCACHE. If REGNUM is -1, do this for all
439 registers. This function doesn't touch any of the reserved bits in
440 *FSAVE. */
441
442 void
443 i387_collect_fsave (const struct regcache *regcache, int regnum, void *fsave)
444 {
445 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
446 gdb_byte *regs = fsave;
447 int i;
448
449 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
450
451 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
452 if (regnum == -1 || regnum == i)
453 {
454 /* Most of the FPU control registers occupy only 16 bits in
455 the fsave area. Give those a special treatment. */
456 if (i >= I387_FCTRL_REGNUM (tdep)
457 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
458 {
459 gdb_byte buf[4];
460
461 regcache_raw_collect (regcache, i, buf);
462
463 if (i == I387_FOP_REGNUM (tdep))
464 {
465 /* The opcode occupies only 11 bits. Make sure we
466 don't touch the other bits. */
467 buf[1] &= ((1 << 3) - 1);
468 buf[1] |= ((FSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
469 }
470 memcpy (FSAVE_ADDR (tdep, regs, i), buf, 2);
471 }
472 else
473 regcache_raw_collect (regcache, i, FSAVE_ADDR (tdep, regs, i));
474 }
475 }
476 \f
477
478 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
479 the data structure used by the "fxsave" instruction where GDB
480 register REGNUM is stored. */
481
482 static int fxsave_offset[] =
483 {
484 32, /* %st(0) through ... */
485 48,
486 64,
487 80,
488 96,
489 112,
490 128,
491 144, /* ... %st(7) (80 bits each). */
492 0, /* `fctrl' (16 bits). */
493 2, /* `fstat' (16 bits). */
494 4, /* `ftag' (16 bits). */
495 12, /* `fiseg' (16 bits). */
496 8, /* `fioff'. */
497 20, /* `foseg' (16 bits). */
498 16, /* `fooff'. */
499 6, /* `fop' (bottom 11 bits). */
500 160 + 0 * 16, /* %xmm0 through ... */
501 160 + 1 * 16,
502 160 + 2 * 16,
503 160 + 3 * 16,
504 160 + 4 * 16,
505 160 + 5 * 16,
506 160 + 6 * 16,
507 160 + 7 * 16,
508 160 + 8 * 16,
509 160 + 9 * 16,
510 160 + 10 * 16,
511 160 + 11 * 16,
512 160 + 12 * 16,
513 160 + 13 * 16,
514 160 + 14 * 16,
515 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
516 };
517
518 #define FXSAVE_ADDR(tdep, fxsave, regnum) \
519 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
520
521 /* We made an unfortunate choice in putting %mxcsr after the SSE
522 registers %xmm0-%xmm7 instead of before, since it makes supporting
523 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
524 don't include the offset for %mxcsr here above. */
525
526 #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
527
528 static int i387_tag (const gdb_byte *raw);
529 \f
530
531 /* Fill register REGNUM in REGCACHE with the appropriate
532 floating-point or SSE register value from *FXSAVE. This function
533 masks off any of the reserved bits in *FXSAVE. */
534
535 void
536 i387_supply_fxsave (struct regcache *regcache, int regnum, const void *fxsave)
537 {
538 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
539 const gdb_byte *regs = fxsave;
540 int i;
541
542 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
543 gdb_assert (tdep->num_xmm_regs > 0);
544
545 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
546 if (regnum == -1 || regnum == i)
547 {
548 if (regs == NULL)
549 {
550 regcache_raw_supply (regcache, i, NULL);
551 continue;
552 }
553
554 /* Most of the FPU control registers occupy only 16 bits in
555 the fxsave area. Give those a special treatment. */
556 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
557 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
558 {
559 gdb_byte val[4];
560
561 memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
562 val[2] = val[3] = 0;
563 if (i == I387_FOP_REGNUM (tdep))
564 val[1] &= ((1 << 3) - 1);
565 else if (i== I387_FTAG_REGNUM (tdep))
566 {
567 /* The fxsave area contains a simplified version of
568 the tag word. We have to look at the actual 80-bit
569 FP data to recreate the traditional i387 tag word. */
570
571 unsigned long ftag = 0;
572 int fpreg;
573 int top;
574
575 top = ((FXSAVE_ADDR (tdep, regs,
576 I387_FSTAT_REGNUM (tdep)))[1] >> 3);
577 top &= 0x7;
578
579 for (fpreg = 7; fpreg >= 0; fpreg--)
580 {
581 int tag;
582
583 if (val[0] & (1 << fpreg))
584 {
585 int regnum = (fpreg + 8 - top) % 8
586 + I387_ST0_REGNUM (tdep);
587 tag = i387_tag (FXSAVE_ADDR (tdep, regs, regnum));
588 }
589 else
590 tag = 3; /* Empty */
591
592 ftag |= tag << (2 * fpreg);
593 }
594 val[0] = ftag & 0xff;
595 val[1] = (ftag >> 8) & 0xff;
596 }
597 regcache_raw_supply (regcache, i, val);
598 }
599 else
600 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
601 }
602
603 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
604 {
605 if (regs == NULL)
606 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), NULL);
607 else
608 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep),
609 FXSAVE_MXCSR_ADDR (regs));
610 }
611 }
612
613 /* Fill register REGNUM (if it is a floating-point or SSE register) in
614 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
615 all registers. This function doesn't touch any of the reserved
616 bits in *FXSAVE. */
617
618 void
619 i387_collect_fxsave (const struct regcache *regcache, int regnum, void *fxsave)
620 {
621 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
622 gdb_byte *regs = fxsave;
623 int i;
624
625 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
626 gdb_assert (tdep->num_xmm_regs > 0);
627
628 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
629 if (regnum == -1 || regnum == i)
630 {
631 /* Most of the FPU control registers occupy only 16 bits in
632 the fxsave area. Give those a special treatment. */
633 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
634 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
635 {
636 gdb_byte buf[4];
637
638 regcache_raw_collect (regcache, i, buf);
639
640 if (i == I387_FOP_REGNUM (tdep))
641 {
642 /* The opcode occupies only 11 bits. Make sure we
643 don't touch the other bits. */
644 buf[1] &= ((1 << 3) - 1);
645 buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
646 }
647 else if (i == I387_FTAG_REGNUM (tdep))
648 {
649 /* Converting back is much easier. */
650
651 unsigned short ftag;
652 int fpreg;
653
654 ftag = (buf[1] << 8) | buf[0];
655 buf[0] = 0;
656 buf[1] = 0;
657
658 for (fpreg = 7; fpreg >= 0; fpreg--)
659 {
660 int tag = (ftag >> (fpreg * 2)) & 3;
661
662 if (tag != 3)
663 buf[0] |= (1 << fpreg);
664 }
665 }
666 memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
667 }
668 else
669 regcache_raw_collect (regcache, i, FXSAVE_ADDR (tdep, regs, i));
670 }
671
672 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
673 regcache_raw_collect (regcache, I387_MXCSR_REGNUM (tdep),
674 FXSAVE_MXCSR_ADDR (regs));
675 }
676
677 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
678 *RAW. */
679
680 static int
681 i387_tag (const gdb_byte *raw)
682 {
683 int integer;
684 unsigned int exponent;
685 unsigned long fraction[2];
686
687 integer = raw[7] & 0x80;
688 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
689 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
690 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
691 | (raw[5] << 8) | raw[4]);
692
693 if (exponent == 0x7fff)
694 {
695 /* Special. */
696 return (2);
697 }
698 else if (exponent == 0x0000)
699 {
700 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
701 {
702 /* Zero. */
703 return (1);
704 }
705 else
706 {
707 /* Special. */
708 return (2);
709 }
710 }
711 else
712 {
713 if (integer)
714 {
715 /* Valid. */
716 return (0);
717 }
718 else
719 {
720 /* Special. */
721 return (2);
722 }
723 }
724 }
725
726 /* Prepare the FPU stack in REGCACHE for a function return. */
727
728 void
729 i387_return_value (struct gdbarch *gdbarch, struct regcache *regcache)
730 {
731 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
732 ULONGEST fstat;
733
734 /* Set the top of the floating-point register stack to 7. The
735 actual value doesn't really matter, but 7 is what a normal
736 function return would end up with if the program started out with
737 a freshly initialized FPU. */
738 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
739 fstat |= (7 << 11);
740 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
741
742 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
743 floating-point register stack to 7, the appropriate value for the
744 tag word is 0x3fff. */
745 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
746
747 }
This page took 0.077499 seconds and 5 git commands to generate.