libiberty/ChangeLog:
[deliverable/binutils-gdb.git] / gdb / i387-tdep.c
CommitLineData
c906108c 1/* Intel 387 floating point stuff.
38edeab8 2
dff95cc7 3 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1998, 1999, 2000,
38edeab8 4 2001, 2002, 2003 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "language.h"
d4f3574e 27#include "value.h"
c906108c
SS
28#include "gdbcore.h"
29#include "floatformat.h"
4e052eda 30#include "regcache.h"
d0df8472 31#include "gdb_assert.h"
309367d4 32#include "gdb_string.h"
d16aafd8 33#include "doublest.h"
c906108c 34
9a82579f 35#include "i386-tdep.h"
c906108c 36
de57eccd
JM
37\f
38/* FIXME: The functions on this page are used by the old `info float'
39 implementations that a few of the i386 targets provide. These
40 functions should be removed if all of these have been converted to
41 use the generic implementation based on the new register file
42 layout. */
43
a14ed312
KB
44static void print_387_control_bits (unsigned int control);
45static void print_387_status_bits (unsigned int status);
de57eccd 46
d4f3574e 47static void
fba45db2 48print_387_control_bits (unsigned int control)
c906108c 49{
c5aa993b 50 switch ((control >> 8) & 3)
c906108c 51 {
c5aa993b 52 case 0:
d4f3574e 53 puts_unfiltered (" 24 bit; ");
c5aa993b
JM
54 break;
55 case 1:
d4f3574e 56 puts_unfiltered (" (bad); ");
c5aa993b
JM
57 break;
58 case 2:
d4f3574e 59 puts_unfiltered (" 53 bit; ");
c5aa993b
JM
60 break;
61 case 3:
d4f3574e 62 puts_unfiltered (" 64 bit; ");
c5aa993b 63 break;
c906108c 64 }
c5aa993b 65 switch ((control >> 10) & 3)
c906108c 66 {
c5aa993b 67 case 0:
d4f3574e 68 puts_unfiltered ("NEAR; ");
c5aa993b
JM
69 break;
70 case 1:
d4f3574e 71 puts_unfiltered ("DOWN; ");
c5aa993b
JM
72 break;
73 case 2:
d4f3574e 74 puts_unfiltered ("UP; ");
c5aa993b
JM
75 break;
76 case 3:
d4f3574e 77 puts_unfiltered ("CHOP; ");
c5aa993b 78 break;
c906108c 79 }
c5aa993b 80 if (control & 0x3f)
c906108c 81 {
d4f3574e 82 puts_unfiltered ("mask");
c5aa993b 83 if (control & 0x0001)
d4f3574e 84 puts_unfiltered (" INVAL");
c5aa993b 85 if (control & 0x0002)
d4f3574e 86 puts_unfiltered (" DENOR");
c5aa993b 87 if (control & 0x0004)
d4f3574e 88 puts_unfiltered (" DIVZ");
c5aa993b 89 if (control & 0x0008)
d4f3574e 90 puts_unfiltered (" OVERF");
c5aa993b 91 if (control & 0x0010)
d4f3574e 92 puts_unfiltered (" UNDER");
c5aa993b 93 if (control & 0x0020)
d4f3574e
SS
94 puts_unfiltered (" LOS");
95 puts_unfiltered (";");
c906108c 96 }
cff3e48b 97
c5aa993b 98 if (control & 0xe080)
d4f3574e 99 warning ("\nreserved bits on: %s",
c5aa993b 100 local_hex_string (control & 0xe080));
c906108c
SS
101}
102
103void
fba45db2 104print_387_control_word (unsigned int control)
d4f3574e
SS
105{
106 printf_filtered ("control %s:", local_hex_string(control & 0xffff));
107 print_387_control_bits (control);
108 puts_unfiltered ("\n");
109}
110
111static void
fba45db2 112print_387_status_bits (unsigned int status)
c906108c 113{
d4f3574e 114 printf_unfiltered (" flags %d%d%d%d; ",
c5aa993b
JM
115 (status & 0x4000) != 0,
116 (status & 0x0400) != 0,
117 (status & 0x0200) != 0,
118 (status & 0x0100) != 0);
d4f3574e
SS
119 printf_unfiltered ("top %d; ", (status >> 11) & 7);
120 if (status & 0xff)
121 {
122 puts_unfiltered ("excep");
123 if (status & 0x0001) puts_unfiltered (" INVAL");
124 if (status & 0x0002) puts_unfiltered (" DENOR");
125 if (status & 0x0004) puts_unfiltered (" DIVZ");
126 if (status & 0x0008) puts_unfiltered (" OVERF");
127 if (status & 0x0010) puts_unfiltered (" UNDER");
128 if (status & 0x0020) puts_unfiltered (" LOS");
129 if (status & 0x0040) puts_unfiltered (" STACK");
130 }
131}
132
133void
fba45db2 134print_387_status_word (unsigned int status)
d4f3574e
SS
135{
136 printf_filtered ("status %s:", local_hex_string (status & 0xffff));
137 print_387_status_bits (status);
138 puts_unfiltered ("\n");
139}
140
de57eccd
JM
141\f
142/* Implement the `info float' layout based on the register definitions
143 in `tm-i386.h'. */
144
145/* Print the floating point number specified by RAW. */
146static void
61113f8b 147print_i387_value (char *raw, struct ui_file *file)
de57eccd
JM
148{
149 DOUBLEST value;
4583280c
MK
150
151 /* Using extract_typed_floating here might affect the representation
152 of certain numbers such as NaNs, even if GDB is running natively.
153 This is fine since our caller already detects such special
154 numbers and we print the hexadecimal representation anyway. */
155 value = extract_typed_floating (raw, builtin_type_i387_ext);
de57eccd
JM
156
157 /* We try to print 19 digits. The last digit may or may not contain
158 garbage, but we'd better print one too many. We need enough room
159 to print the value, 1 position for the sign, 1 for the decimal
160 point, 19 for the digits and 6 for the exponent adds up to 27. */
161#ifdef PRINTF_HAS_LONG_DOUBLE
61113f8b 162 fprintf_filtered (file, " %-+27.19Lg", (long double) value);
de57eccd 163#else
61113f8b 164 fprintf_filtered (file, " %-+27.19g", (double) value);
de57eccd
JM
165#endif
166}
167
168/* Print the classification for the register contents RAW. */
169static void
61113f8b 170print_i387_ext (unsigned char *raw, struct ui_file *file)
de57eccd
JM
171{
172 int sign;
173 int integer;
174 unsigned int exponent;
175 unsigned long fraction[2];
176
177 sign = raw[9] & 0x80;
178 integer = raw[7] & 0x80;
179 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
180 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
181 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
182 | (raw[5] << 8) | raw[4]);
183
184 if (exponent == 0x7fff && integer)
185 {
186 if (fraction[0] == 0x00000000 && fraction[1] == 0x00000000)
187 /* Infinity. */
61113f8b 188 fprintf_filtered (file, " %cInf", (sign ? '-' : '+'));
de57eccd
JM
189 else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000)
190 /* Real Indefinite (QNaN). */
61113f8b 191 fputs_unfiltered (" Real Indefinite (QNaN)", file);
de57eccd
JM
192 else if (fraction[1] & 0x40000000)
193 /* QNaN. */
61113f8b 194 fputs_filtered (" QNaN", file);
de57eccd
JM
195 else
196 /* SNaN. */
61113f8b 197 fputs_filtered (" SNaN", file);
de57eccd
JM
198 }
199 else if (exponent < 0x7fff && exponent > 0x0000 && integer)
200 /* Normal. */
61113f8b 201 print_i387_value (raw, file);
de57eccd
JM
202 else if (exponent == 0x0000)
203 {
204 /* Denormal or zero. */
61113f8b 205 print_i387_value (raw, file);
de57eccd
JM
206
207 if (integer)
208 /* Pseudo-denormal. */
61113f8b 209 fputs_filtered (" Pseudo-denormal", file);
de57eccd
JM
210 else if (fraction[0] || fraction[1])
211 /* Denormal. */
61113f8b 212 fputs_filtered (" Denormal", file);
de57eccd
JM
213 }
214 else
215 /* Unsupported. */
61113f8b 216 fputs_filtered (" Unsupported", file);
de57eccd
JM
217}
218
219/* Print the status word STATUS. */
220static void
61113f8b 221print_i387_status_word (unsigned int status, struct ui_file *file)
de57eccd 222{
61113f8b 223 fprintf_filtered (file, "Status Word: %s",
de57eccd 224 local_hex_string_custom (status, "04"));
61113f8b
MK
225 fputs_filtered (" ", file);
226 fprintf_filtered (file, " %s", (status & 0x0001) ? "IE" : " ");
227 fprintf_filtered (file, " %s", (status & 0x0002) ? "DE" : " ");
228 fprintf_filtered (file, " %s", (status & 0x0004) ? "ZE" : " ");
229 fprintf_filtered (file, " %s", (status & 0x0008) ? "OE" : " ");
230 fprintf_filtered (file, " %s", (status & 0x0010) ? "UE" : " ");
231 fprintf_filtered (file, " %s", (status & 0x0020) ? "PE" : " ");
232 fputs_filtered (" ", file);
233 fprintf_filtered (file, " %s", (status & 0x0080) ? "ES" : " ");
234 fputs_filtered (" ", file);
235 fprintf_filtered (file, " %s", (status & 0x0040) ? "SF" : " ");
236 fputs_filtered (" ", file);
237 fprintf_filtered (file, " %s", (status & 0x0100) ? "C0" : " ");
238 fprintf_filtered (file, " %s", (status & 0x0200) ? "C1" : " ");
239 fprintf_filtered (file, " %s", (status & 0x0400) ? "C2" : " ");
240 fprintf_filtered (file, " %s", (status & 0x4000) ? "C3" : " ");
241
242 fputs_filtered ("\n", file);
243
244 fprintf_filtered (file,
245 " TOP: %d\n", ((status >> 11) & 7));
de57eccd
JM
246}
247
248/* Print the control word CONTROL. */
249static void
61113f8b 250print_i387_control_word (unsigned int control, struct ui_file *file)
de57eccd 251{
61113f8b 252 fprintf_filtered (file, "Control Word: %s",
de57eccd 253 local_hex_string_custom (control, "04"));
61113f8b
MK
254 fputs_filtered (" ", file);
255 fprintf_filtered (file, " %s", (control & 0x0001) ? "IM" : " ");
256 fprintf_filtered (file, " %s", (control & 0x0002) ? "DM" : " ");
257 fprintf_filtered (file, " %s", (control & 0x0004) ? "ZM" : " ");
258 fprintf_filtered (file, " %s", (control & 0x0008) ? "OM" : " ");
259 fprintf_filtered (file, " %s", (control & 0x0010) ? "UM" : " ");
260 fprintf_filtered (file, " %s", (control & 0x0020) ? "PM" : " ");
de57eccd 261
61113f8b 262 fputs_filtered ("\n", file);
de57eccd 263
61113f8b 264 fputs_filtered (" PC: ", file);
de57eccd
JM
265 switch ((control >> 8) & 3)
266 {
267 case 0:
61113f8b 268 fputs_filtered ("Single Precision (24-bits)\n", file);
de57eccd
JM
269 break;
270 case 1:
61113f8b 271 fputs_filtered ("Reserved\n", file);
de57eccd
JM
272 break;
273 case 2:
61113f8b 274 fputs_filtered ("Double Precision (53-bits)\n", file);
de57eccd
JM
275 break;
276 case 3:
61113f8b 277 fputs_filtered ("Extended Precision (64-bits)\n", file);
de57eccd
JM
278 break;
279 }
280
61113f8b 281 fputs_filtered (" RC: ", file);
de57eccd
JM
282 switch ((control >> 10) & 3)
283 {
284 case 0:
61113f8b 285 fputs_filtered ("Round to nearest\n", file);
de57eccd
JM
286 break;
287 case 1:
61113f8b 288 fputs_filtered ("Round down\n", file);
de57eccd
JM
289 break;
290 case 2:
61113f8b 291 fputs_filtered ("Round up\n", file);
de57eccd
JM
292 break;
293 case 3:
61113f8b 294 fputs_filtered ("Round toward zero\n", file);
de57eccd
JM
295 break;
296 }
297}
298
9b949a49 299/* Print out the i387 floating point state. Note that we ignore FRAME
7d8d2918
MK
300 in the code below. That's OK since floating-point registers are
301 never saved on the stack. */
302
de57eccd 303void
61113f8b 304i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
8e186fd6 305 struct frame_info *frame, const char *args)
de57eccd 306{
1d70089a
MK
307 char buf[4];
308 ULONGEST fctrl;
309 ULONGEST fstat;
310 ULONGEST ftag;
311 ULONGEST fiseg;
312 ULONGEST fioff;
313 ULONGEST foseg;
314 ULONGEST fooff;
315 ULONGEST fop;
de57eccd
JM
316 int fpreg;
317 int top;
318
1d70089a
MK
319 frame_register_read (frame, FCTRL_REGNUM, buf);
320 fctrl = extract_unsigned_integer (buf, 4);
321 frame_register_read (frame, FSTAT_REGNUM, buf);
322 fstat = extract_unsigned_integer (buf, 4);
323 frame_register_read (frame, FTAG_REGNUM, buf);
324 ftag = extract_unsigned_integer (buf, 4);
325 frame_register_read (frame, FISEG_REGNUM, buf);
326 fiseg = extract_unsigned_integer (buf, 4);
327 frame_register_read (frame, FIOFF_REGNUM, buf);
328 fioff = extract_unsigned_integer (buf, 4);
329 frame_register_read (frame, FOSEG_REGNUM, buf);
330 foseg = extract_unsigned_integer (buf, 4);
331 frame_register_read (frame, FOOFF_REGNUM, buf);
332 fooff = extract_unsigned_integer (buf, 4);
333 frame_register_read (frame, FOP_REGNUM, buf);
334 fop = extract_unsigned_integer (buf, 4);
335
de57eccd
JM
336 top = ((fstat >> 11) & 7);
337
338 for (fpreg = 7; fpreg >= 0; fpreg--)
339 {
340 unsigned char raw[FPU_REG_RAW_SIZE];
341 int tag = (ftag >> (fpreg * 2)) & 3;
342 int i;
343
61113f8b 344 fprintf_filtered (file, "%sR%d: ", fpreg == top ? "=>" : " ", fpreg);
de57eccd
JM
345
346 switch (tag)
347 {
348 case 0:
61113f8b 349 fputs_filtered ("Valid ", file);
de57eccd
JM
350 break;
351 case 1:
61113f8b 352 fputs_filtered ("Zero ", file);
de57eccd
JM
353 break;
354 case 2:
61113f8b 355 fputs_filtered ("Special ", file);
de57eccd
JM
356 break;
357 case 3:
61113f8b 358 fputs_filtered ("Empty ", file);
de57eccd
JM
359 break;
360 }
361
1d70089a 362 frame_register_read (frame, (fpreg + 8 - top) % 8 + FP0_REGNUM, raw);
de57eccd 363
61113f8b 364 fputs_filtered ("0x", file);
de57eccd 365 for (i = 9; i >= 0; i--)
61113f8b 366 fprintf_filtered (file, "%02x", raw[i]);
de57eccd
JM
367
368 if (tag != 3)
61113f8b 369 print_i387_ext (raw, file);
de57eccd 370
61113f8b 371 fputs_filtered ("\n", file);
de57eccd
JM
372 }
373
f16a25ae 374 fputs_filtered ("\n", file);
de57eccd 375
61113f8b
MK
376 print_i387_status_word (fstat, file);
377 print_i387_control_word (fctrl, file);
378 fprintf_filtered (file, "Tag Word: %s\n",
379 local_hex_string_custom (ftag, "04"));
380 fprintf_filtered (file, "Instruction Pointer: %s:",
381 local_hex_string_custom (fiseg, "02"));
382 fprintf_filtered (file, "%s\n", local_hex_string_custom (fioff, "08"));
383 fprintf_filtered (file, "Operand Pointer: %s:",
384 local_hex_string_custom (foseg, "02"));
385 fprintf_filtered (file, "%s\n", local_hex_string_custom (fooff, "08"));
386 fprintf_filtered (file, "Opcode: %s\n",
387 local_hex_string_custom (fop ? (fop | 0xd800) : 0, "04"));
de57eccd 388}
e750d25e
JT
389
390/* FIXME: kettenis/2000-05-21: Right now more than a few i386 targets
391 define their own routines to manage the floating-point registers in
392 GDB's register array. Most (if not all) of these targets use the
393 format used by the "fsave" instruction in their communication with
394 the OS. They should all be converted to use the routines below. */
395
396/* At fsave_offset[REGNUM] you'll find the offset to the location in
397 the data structure used by the "fsave" instruction where GDB
398 register REGNUM is stored. */
399
400static int fsave_offset[] =
401{
402 28 + 0 * FPU_REG_RAW_SIZE, /* FP0_REGNUM through ... */
403 28 + 1 * FPU_REG_RAW_SIZE,
404 28 + 2 * FPU_REG_RAW_SIZE,
405 28 + 3 * FPU_REG_RAW_SIZE,
406 28 + 4 * FPU_REG_RAW_SIZE,
407 28 + 5 * FPU_REG_RAW_SIZE,
408 28 + 6 * FPU_REG_RAW_SIZE,
409 28 + 7 * FPU_REG_RAW_SIZE, /* ... FP7_REGNUM. */
410 0, /* FCTRL_REGNUM (16 bits). */
411 4, /* FSTAT_REGNUM (16 bits). */
412 8, /* FTAG_REGNUM (16 bits). */
413 16, /* FISEG_REGNUM (16 bits). */
414 12, /* FIOFF_REGNUM. */
415 24, /* FOSEG_REGNUM. */
416 20, /* FOOFF_REGNUM. */
417 18 /* FOP_REGNUM (bottom 11 bits). */
418};
419
420#define FSAVE_ADDR(fsave, regnum) (fsave + fsave_offset[regnum - FP0_REGNUM])
421\f
422
423/* Fill register REGNUM in GDB's register array with the appropriate
424 value from *FSAVE. This function masks off any of the reserved
425 bits in *FSAVE. */
426
427void
428i387_supply_register (int regnum, char *fsave)
429{
932bb524
KD
430 if (fsave == NULL)
431 {
432 supply_register (regnum, NULL);
433 return;
434 }
435
e750d25e
JT
436 /* Most of the FPU control registers occupy only 16 bits in
437 the fsave area. Give those a special treatment. */
438 if (regnum >= FPC_REGNUM
439 && regnum != FIOFF_REGNUM && regnum != FOOFF_REGNUM)
440 {
441 unsigned char val[4];
442
443 memcpy (val, FSAVE_ADDR (fsave, regnum), 2);
444 val[2] = val[3] = 0;
445 if (regnum == FOP_REGNUM)
446 val[1] &= ((1 << 3) - 1);
447 supply_register (regnum, val);
448 }
449 else
450 supply_register (regnum, FSAVE_ADDR (fsave, regnum));
451}
452
453/* Fill GDB's register array with the floating-point register values
454 in *FSAVE. This function masks off any of the reserved
455 bits in *FSAVE. */
456
457void
458i387_supply_fsave (char *fsave)
459{
460 int i;
461
462 for (i = FP0_REGNUM; i < XMM0_REGNUM; i++)
463 i387_supply_register (i, fsave);
464}
465
466/* Fill register REGNUM (if it is a floating-point register) in *FSAVE
467 with the value in GDB's register array. If REGNUM is -1, do this
468 for all registers. This function doesn't touch any of the reserved
469 bits in *FSAVE. */
470
471void
472i387_fill_fsave (char *fsave, int regnum)
473{
474 int i;
475
476 for (i = FP0_REGNUM; i < XMM0_REGNUM; i++)
477 if (regnum == -1 || regnum == i)
478 {
479 /* Most of the FPU control registers occupy only 16 bits in
480 the fsave area. Give those a special treatment. */
481 if (i >= FPC_REGNUM
482 && i != FIOFF_REGNUM && i != FOOFF_REGNUM)
483 {
484 unsigned char buf[4];
485
486 regcache_collect (i, buf);
487
488 if (i == FOP_REGNUM)
489 {
490 /* The opcode occupies only 11 bits. Make sure we
491 don't touch the other bits. */
492 buf[1] &= ((1 << 3) - 1);
493 buf[1] |= ((FSAVE_ADDR (fsave, i))[1] & ~((1 << 3) - 1));
494 }
495 memcpy (FSAVE_ADDR (fsave, i), buf, 2);
496 }
497 else
498 regcache_collect (i, FSAVE_ADDR (fsave, i));
499 }
500}
501\f
502
503/* At fxsave_offset[REGNUM] you'll find the offset to the location in
504 the data structure used by the "fxsave" instruction where GDB
505 register REGNUM is stored. */
506
507static int fxsave_offset[] =
508{
509 32, /* FP0_REGNUM through ... */
510 48,
511 64,
512 80,
513 96,
514 112,
515 128,
516 144, /* ... FP7_REGNUM (80 bits each). */
517 0, /* FCTRL_REGNUM (16 bits). */
518 2, /* FSTAT_REGNUM (16 bits). */
519 4, /* FTAG_REGNUM (16 bits). */
520 12, /* FISEG_REGNUM (16 bits). */
521 8, /* FIOFF_REGNUM. */
522 20, /* FOSEG_REGNUM (16 bits). */
523 16, /* FOOFF_REGNUM. */
524 6, /* FOP_REGNUM (bottom 11 bits). */
04c8243f
MK
525 160 + 0 * 16, /* XMM0_REGNUM through ... */
526 160 + 1 * 16,
527 160 + 2 * 16,
528 160 + 3 * 16,
529 160 + 4 * 16,
530 160 + 5 * 16,
531 160 + 6 * 16,
532 160 + 7 * 16,
533 160 + 8 * 16,
534 160 + 9 * 16,
535 160 + 10 * 16,
536 160 + 11 * 16,
537 160 + 12 * 16,
538 160 + 13 * 16,
539 160 + 14 * 16,
540 160 + 15 * 16, /* ... XMM15_REGNUM (128 bits each). */
541 24 /* MXCSR_REGNUM. */
e750d25e
JT
542};
543
04c8243f
MK
544/* FIXME: kettenis/20030430: We made an unfortunate choice in putting
545 %mxcsr after the SSE registers %xmm0-%xmm7 instead of before, since
546 it makes supporting the registers %xmm8-%xmm15 on x86-64 a bit
547 involved. Hack around it by explicitly overriding the offset for
548 %mxcsr here. */
549
e750d25e 550#define FXSAVE_ADDR(fxsave, regnum) \
04c8243f
MK
551 ((regnum == MXCSR_REGNUM) ? (fxsave + 24) : \
552 (fxsave + fxsave_offset[regnum - FP0_REGNUM]))
e750d25e
JT
553
554static int i387_tag (unsigned char *raw);
555\f
556
557/* Fill GDB's register array with the floating-point and SSE register
558 values in *FXSAVE. This function masks off any of the reserved
559 bits in *FXSAVE. */
560
561void
562i387_supply_fxsave (char *fxsave)
563{
dff95cc7
MK
564 int i, last_regnum = MXCSR_REGNUM;
565
566 if (gdbarch_tdep (current_gdbarch)->num_xmm_regs == 0)
567 last_regnum = FOP_REGNUM;
e750d25e 568
dff95cc7 569 for (i = FP0_REGNUM; i <= last_regnum; i++)
e750d25e 570 {
932bb524
KD
571 if (fxsave == NULL)
572 {
573 supply_register (i, NULL);
574 continue;
575 }
576
e750d25e
JT
577 /* Most of the FPU control registers occupy only 16 bits in
578 the fxsave area. Give those a special treatment. */
579 if (i >= FPC_REGNUM && i < XMM0_REGNUM
580 && i != FIOFF_REGNUM && i != FOOFF_REGNUM)
581 {
582 unsigned char val[4];
583
584 memcpy (val, FXSAVE_ADDR (fxsave, i), 2);
585 val[2] = val[3] = 0;
586 if (i == FOP_REGNUM)
587 val[1] &= ((1 << 3) - 1);
588 else if (i== FTAG_REGNUM)
589 {
590 /* The fxsave area contains a simplified version of the
591 tag word. We have to look at the actual 80-bit FP
592 data to recreate the traditional i387 tag word. */
593
594 unsigned long ftag = 0;
595 int fpreg;
596 int top;
597
598 top = (((FXSAVE_ADDR (fxsave, FSTAT_REGNUM))[1] >> 3) & 0x7);
599
600 for (fpreg = 7; fpreg >= 0; fpreg--)
601 {
602 int tag;
603
604 if (val[0] & (1 << fpreg))
605 {
606 int regnum = (fpreg + 8 - top) % 8 + FP0_REGNUM;
607 tag = i387_tag (FXSAVE_ADDR (fxsave, regnum));
608 }
609 else
610 tag = 3; /* Empty */
611
612 ftag |= tag << (2 * fpreg);
613 }
614 val[0] = ftag & 0xff;
615 val[1] = (ftag >> 8) & 0xff;
616 }
617 supply_register (i, val);
618 }
619 else
620 supply_register (i, FXSAVE_ADDR (fxsave, i));
621 }
622}
623
624/* Fill register REGNUM (if it is a floating-point or SSE register) in
625 *FXSAVE with the value in GDB's register array. If REGNUM is -1, do
626 this for all registers. This function doesn't touch any of the
627 reserved bits in *FXSAVE. */
628
629void
630i387_fill_fxsave (char *fxsave, int regnum)
631{
dff95cc7
MK
632 int i, last_regnum = MXCSR_REGNUM;
633
634 if (gdbarch_tdep (current_gdbarch)->num_xmm_regs == 0)
635 last_regnum = FOP_REGNUM;
e750d25e 636
dff95cc7 637 for (i = FP0_REGNUM; i <= last_regnum; i++)
e750d25e
JT
638 if (regnum == -1 || regnum == i)
639 {
640 /* Most of the FPU control registers occupy only 16 bits in
641 the fxsave area. Give those a special treatment. */
642 if (i >= FPC_REGNUM && i < XMM0_REGNUM
19e33363 643 && i != FIOFF_REGNUM && i != FOOFF_REGNUM)
e750d25e
JT
644 {
645 unsigned char buf[4];
646
647 regcache_collect (i, buf);
648
649 if (i == FOP_REGNUM)
650 {
651 /* The opcode occupies only 11 bits. Make sure we
652 don't touch the other bits. */
653 buf[1] &= ((1 << 3) - 1);
654 buf[1] |= ((FXSAVE_ADDR (fxsave, i))[1] & ~((1 << 3) - 1));
655 }
656 else if (i == FTAG_REGNUM)
657 {
658 /* Converting back is much easier. */
659
660 unsigned short ftag;
661 int fpreg;
662
663 ftag = (buf[1] << 8) | buf[0];
664 buf[0] = 0;
665 buf[1] = 0;
666
667 for (fpreg = 7; fpreg >= 0; fpreg--)
668 {
669 int tag = (ftag >> (fpreg * 2)) & 3;
670
671 if (tag != 3)
672 buf[0] |= (1 << fpreg);
673 }
674 }
675 memcpy (FXSAVE_ADDR (fxsave, i), buf, 2);
676 }
677 else
678 regcache_collect (i, FXSAVE_ADDR (fxsave, i));
679 }
680}
681
682/* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
683 *RAW. */
684
685static int
686i387_tag (unsigned char *raw)
687{
688 int integer;
689 unsigned int exponent;
690 unsigned long fraction[2];
691
692 integer = raw[7] & 0x80;
693 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
694 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
695 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
696 | (raw[5] << 8) | raw[4]);
697
698 if (exponent == 0x7fff)
699 {
700 /* Special. */
701 return (2);
702 }
703 else if (exponent == 0x0000)
704 {
705 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
706 {
707 /* Zero. */
708 return (1);
709 }
710 else
711 {
712 /* Special. */
713 return (2);
714 }
715 }
716 else
717 {
718 if (integer)
719 {
720 /* Valid. */
721 return (0);
722 }
723 else
724 {
725 /* Special. */
726 return (2);
727 }
728 }
729}
This page took 0.295761 seconds and 4 git commands to generate.