Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Intel 387 floating point stuff. |
38edeab8 | 2 | |
6aba47ca DJ |
3 | Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1998, 1999, 2000, 2001, |
4 | 2002, 2003, 2004, 2005, 2007 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 | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
786a90bb MK |
24 | #include "doublest.h" |
25 | #include "floatformat.h" | |
c906108c | 26 | #include "frame.h" |
786a90bb | 27 | #include "gdbcore.h" |
c906108c SS |
28 | #include "inferior.h" |
29 | #include "language.h" | |
4e052eda | 30 | #include "regcache.h" |
786a90bb MK |
31 | #include "value.h" |
32 | ||
d0df8472 | 33 | #include "gdb_assert.h" |
309367d4 | 34 | #include "gdb_string.h" |
c906108c | 35 | |
9a82579f | 36 | #include "i386-tdep.h" |
42c466d7 | 37 | #include "i387-tdep.h" |
c906108c | 38 | |
de57eccd | 39 | /* Print the floating point number specified by RAW. */ |
786a90bb | 40 | |
de57eccd | 41 | static void |
b4ad899f | 42 | print_i387_value (const gdb_byte *raw, struct ui_file *file) |
de57eccd JM |
43 | { |
44 | DOUBLEST value; | |
4583280c MK |
45 | |
46 | /* Using extract_typed_floating here might affect the representation | |
47 | of certain numbers such as NaNs, even if GDB is running natively. | |
48 | This is fine since our caller already detects such special | |
49 | numbers and we print the hexadecimal representation anyway. */ | |
50 | value = extract_typed_floating (raw, builtin_type_i387_ext); | |
de57eccd JM |
51 | |
52 | /* We try to print 19 digits. The last digit may or may not contain | |
53 | garbage, but we'd better print one too many. We need enough room | |
54 | to print the value, 1 position for the sign, 1 for the decimal | |
55 | point, 19 for the digits and 6 for the exponent adds up to 27. */ | |
56 | #ifdef PRINTF_HAS_LONG_DOUBLE | |
61113f8b | 57 | fprintf_filtered (file, " %-+27.19Lg", (long double) value); |
de57eccd | 58 | #else |
61113f8b | 59 | fprintf_filtered (file, " %-+27.19g", (double) value); |
de57eccd JM |
60 | #endif |
61 | } | |
62 | ||
63 | /* Print the classification for the register contents RAW. */ | |
786a90bb | 64 | |
de57eccd | 65 | static void |
b4ad899f | 66 | print_i387_ext (const gdb_byte *raw, struct ui_file *file) |
de57eccd JM |
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. */ | |
61113f8b | 84 | fprintf_filtered (file, " %cInf", (sign ? '-' : '+')); |
de57eccd JM |
85 | else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000) |
86 | /* Real Indefinite (QNaN). */ | |
61113f8b | 87 | fputs_unfiltered (" Real Indefinite (QNaN)", file); |
de57eccd JM |
88 | else if (fraction[1] & 0x40000000) |
89 | /* QNaN. */ | |
61113f8b | 90 | fputs_filtered (" QNaN", file); |
de57eccd JM |
91 | else |
92 | /* SNaN. */ | |
61113f8b | 93 | fputs_filtered (" SNaN", file); |
de57eccd JM |
94 | } |
95 | else if (exponent < 0x7fff && exponent > 0x0000 && integer) | |
96 | /* Normal. */ | |
61113f8b | 97 | print_i387_value (raw, file); |
de57eccd JM |
98 | else if (exponent == 0x0000) |
99 | { | |
100 | /* Denormal or zero. */ | |
61113f8b | 101 | print_i387_value (raw, file); |
de57eccd JM |
102 | |
103 | if (integer) | |
104 | /* Pseudo-denormal. */ | |
61113f8b | 105 | fputs_filtered (" Pseudo-denormal", file); |
de57eccd JM |
106 | else if (fraction[0] || fraction[1]) |
107 | /* Denormal. */ | |
61113f8b | 108 | fputs_filtered (" Denormal", file); |
de57eccd JM |
109 | } |
110 | else | |
111 | /* Unsupported. */ | |
61113f8b | 112 | fputs_filtered (" Unsupported", file); |
de57eccd JM |
113 | } |
114 | ||
115 | /* Print the status word STATUS. */ | |
786a90bb | 116 | |
de57eccd | 117 | static void |
61113f8b | 118 | print_i387_status_word (unsigned int status, struct ui_file *file) |
de57eccd | 119 | { |
61113f8b | 120 | fprintf_filtered (file, "Status Word: %s", |
bb599908 | 121 | hex_string_custom (status, 4)); |
61113f8b MK |
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)); | |
de57eccd JM |
143 | } |
144 | ||
145 | /* Print the control word CONTROL. */ | |
786a90bb | 146 | |
de57eccd | 147 | static void |
61113f8b | 148 | print_i387_control_word (unsigned int control, struct ui_file *file) |
de57eccd | 149 | { |
61113f8b | 150 | fprintf_filtered (file, "Control Word: %s", |
bb599908 | 151 | hex_string_custom (control, 4)); |
61113f8b MK |
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" : " "); | |
de57eccd | 159 | |
61113f8b | 160 | fputs_filtered ("\n", file); |
de57eccd | 161 | |
61113f8b | 162 | fputs_filtered (" PC: ", file); |
de57eccd JM |
163 | switch ((control >> 8) & 3) |
164 | { | |
165 | case 0: | |
61113f8b | 166 | fputs_filtered ("Single Precision (24-bits)\n", file); |
de57eccd JM |
167 | break; |
168 | case 1: | |
61113f8b | 169 | fputs_filtered ("Reserved\n", file); |
de57eccd JM |
170 | break; |
171 | case 2: | |
61113f8b | 172 | fputs_filtered ("Double Precision (53-bits)\n", file); |
de57eccd JM |
173 | break; |
174 | case 3: | |
61113f8b | 175 | fputs_filtered ("Extended Precision (64-bits)\n", file); |
de57eccd JM |
176 | break; |
177 | } | |
178 | ||
61113f8b | 179 | fputs_filtered (" RC: ", file); |
de57eccd JM |
180 | switch ((control >> 10) & 3) |
181 | { | |
182 | case 0: | |
61113f8b | 183 | fputs_filtered ("Round to nearest\n", file); |
de57eccd JM |
184 | break; |
185 | case 1: | |
61113f8b | 186 | fputs_filtered ("Round down\n", file); |
de57eccd JM |
187 | break; |
188 | case 2: | |
61113f8b | 189 | fputs_filtered ("Round up\n", file); |
de57eccd JM |
190 | break; |
191 | case 3: | |
61113f8b | 192 | fputs_filtered ("Round toward zero\n", file); |
de57eccd JM |
193 | break; |
194 | } | |
195 | } | |
196 | ||
9b949a49 | 197 | /* Print out the i387 floating point state. Note that we ignore FRAME |
7d8d2918 MK |
198 | in the code below. That's OK since floating-point registers are |
199 | never saved on the stack. */ | |
200 | ||
de57eccd | 201 | void |
61113f8b | 202 | i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file, |
8e186fd6 | 203 | struct frame_info *frame, const char *args) |
de57eccd | 204 | { |
5716833c | 205 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame)); |
b4ad899f | 206 | gdb_byte buf[4]; |
1d70089a MK |
207 | ULONGEST fctrl; |
208 | ULONGEST fstat; | |
209 | ULONGEST ftag; | |
210 | ULONGEST fiseg; | |
211 | ULONGEST fioff; | |
212 | ULONGEST foseg; | |
213 | ULONGEST fooff; | |
214 | ULONGEST fop; | |
de57eccd JM |
215 | int fpreg; |
216 | int top; | |
217 | ||
5716833c MK |
218 | gdb_assert (gdbarch == get_frame_arch (frame)); |
219 | ||
220 | /* Define I387_ST0_REGNUM such that we use the proper definitions | |
221 | for FRAME's architecture. */ | |
222 | #define I387_ST0_REGNUM tdep->st0_regnum | |
223 | ||
224 | fctrl = get_frame_register_unsigned (frame, I387_FCTRL_REGNUM); | |
225 | fstat = get_frame_register_unsigned (frame, I387_FSTAT_REGNUM); | |
226 | ftag = get_frame_register_unsigned (frame, I387_FTAG_REGNUM); | |
227 | fiseg = get_frame_register_unsigned (frame, I387_FISEG_REGNUM); | |
228 | fioff = get_frame_register_unsigned (frame, I387_FIOFF_REGNUM); | |
229 | foseg = get_frame_register_unsigned (frame, I387_FOSEG_REGNUM); | |
230 | fooff = get_frame_register_unsigned (frame, I387_FOOFF_REGNUM); | |
231 | fop = get_frame_register_unsigned (frame, I387_FOP_REGNUM); | |
1d70089a | 232 | |
de57eccd JM |
233 | top = ((fstat >> 11) & 7); |
234 | ||
235 | for (fpreg = 7; fpreg >= 0; fpreg--) | |
236 | { | |
b4ad899f | 237 | gdb_byte raw[I386_MAX_REGISTER_SIZE]; |
de57eccd JM |
238 | int tag = (ftag >> (fpreg * 2)) & 3; |
239 | int i; | |
240 | ||
61113f8b | 241 | fprintf_filtered (file, "%sR%d: ", fpreg == top ? "=>" : " ", fpreg); |
de57eccd JM |
242 | |
243 | switch (tag) | |
244 | { | |
245 | case 0: | |
61113f8b | 246 | fputs_filtered ("Valid ", file); |
de57eccd JM |
247 | break; |
248 | case 1: | |
61113f8b | 249 | fputs_filtered ("Zero ", file); |
de57eccd JM |
250 | break; |
251 | case 2: | |
61113f8b | 252 | fputs_filtered ("Special ", file); |
de57eccd JM |
253 | break; |
254 | case 3: | |
61113f8b | 255 | fputs_filtered ("Empty ", file); |
de57eccd JM |
256 | break; |
257 | } | |
258 | ||
5716833c | 259 | get_frame_register (frame, (fpreg + 8 - top) % 8 + I387_ST0_REGNUM, raw); |
de57eccd | 260 | |
61113f8b | 261 | fputs_filtered ("0x", file); |
de57eccd | 262 | for (i = 9; i >= 0; i--) |
61113f8b | 263 | fprintf_filtered (file, "%02x", raw[i]); |
de57eccd JM |
264 | |
265 | if (tag != 3) | |
61113f8b | 266 | print_i387_ext (raw, file); |
de57eccd | 267 | |
61113f8b | 268 | fputs_filtered ("\n", file); |
de57eccd JM |
269 | } |
270 | ||
f16a25ae | 271 | fputs_filtered ("\n", file); |
de57eccd | 272 | |
61113f8b MK |
273 | print_i387_status_word (fstat, file); |
274 | print_i387_control_word (fctrl, file); | |
275 | fprintf_filtered (file, "Tag Word: %s\n", | |
bb599908 | 276 | hex_string_custom (ftag, 4)); |
61113f8b | 277 | fprintf_filtered (file, "Instruction Pointer: %s:", |
bb599908 PH |
278 | hex_string_custom (fiseg, 2)); |
279 | fprintf_filtered (file, "%s\n", hex_string_custom (fioff, 8)); | |
61113f8b | 280 | fprintf_filtered (file, "Operand Pointer: %s:", |
bb599908 PH |
281 | hex_string_custom (foseg, 2)); |
282 | fprintf_filtered (file, "%s\n", hex_string_custom (fooff, 8)); | |
61113f8b | 283 | fprintf_filtered (file, "Opcode: %s\n", |
bb599908 | 284 | hex_string_custom (fop ? (fop | 0xd800) : 0, 4)); |
5716833c MK |
285 | |
286 | #undef I387_ST0_REGNUM | |
de57eccd | 287 | } |
d532c08f MK |
288 | \f |
289 | ||
290 | /* Read a value of type TYPE from register REGNUM in frame FRAME, and | |
291 | return its contents in TO. */ | |
292 | ||
293 | void | |
294 | i387_register_to_value (struct frame_info *frame, int regnum, | |
42835c2b | 295 | struct type *type, gdb_byte *to) |
d532c08f | 296 | { |
b4ad899f | 297 | gdb_byte from[I386_MAX_REGISTER_SIZE]; |
d532c08f MK |
298 | |
299 | gdb_assert (i386_fp_regnum_p (regnum)); | |
300 | ||
301 | /* We only support floating-point values. */ | |
302 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
303 | { | |
8a3fe4f8 AC |
304 | warning (_("Cannot convert floating-point register value " |
305 | "to non-floating-point type.")); | |
d532c08f MK |
306 | return; |
307 | } | |
308 | ||
309 | /* Convert to TYPE. This should be a no-op if TYPE is equivalent to | |
310 | the extended floating-point format used by the FPU. */ | |
192285c6 | 311 | get_frame_register (frame, regnum, from); |
d532c08f MK |
312 | convert_typed_floating (from, builtin_type_i387_ext, to, type); |
313 | } | |
314 | ||
315 | /* Write the contents FROM of a value of type TYPE into register | |
316 | REGNUM in frame FRAME. */ | |
317 | ||
318 | void | |
319 | i387_value_to_register (struct frame_info *frame, int regnum, | |
42835c2b | 320 | struct type *type, const gdb_byte *from) |
d532c08f | 321 | { |
b4ad899f | 322 | gdb_byte to[I386_MAX_REGISTER_SIZE]; |
d532c08f MK |
323 | |
324 | gdb_assert (i386_fp_regnum_p (regnum)); | |
325 | ||
326 | /* We only support floating-point values. */ | |
327 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
328 | { | |
8a3fe4f8 AC |
329 | warning (_("Cannot convert non-floating-point type " |
330 | "to floating-point register value.")); | |
d532c08f MK |
331 | return; |
332 | } | |
333 | ||
334 | /* Convert from TYPE. This should be a no-op if TYPE is equivalent | |
335 | to the extended floating-point format used by the FPU. */ | |
336 | convert_typed_floating (from, type, to, builtin_type_i387_ext); | |
337 | put_frame_register (frame, regnum, to); | |
338 | } | |
339 | \f | |
e750d25e | 340 | |
786a90bb | 341 | /* Handle FSAVE and FXSAVE formats. */ |
e750d25e JT |
342 | |
343 | /* At fsave_offset[REGNUM] you'll find the offset to the location in | |
344 | the data structure used by the "fsave" instruction where GDB | |
345 | register REGNUM is stored. */ | |
346 | ||
347 | static int fsave_offset[] = | |
348 | { | |
5716833c MK |
349 | 28 + 0 * 10, /* %st(0) ... */ |
350 | 28 + 1 * 10, | |
351 | 28 + 2 * 10, | |
352 | 28 + 3 * 10, | |
353 | 28 + 4 * 10, | |
354 | 28 + 5 * 10, | |
355 | 28 + 6 * 10, | |
356 | 28 + 7 * 10, /* ... %st(7). */ | |
357 | 0, /* `fctrl' (16 bits). */ | |
358 | 4, /* `fstat' (16 bits). */ | |
359 | 8, /* `ftag' (16 bits). */ | |
360 | 16, /* `fiseg' (16 bits). */ | |
361 | 12, /* `fioff'. */ | |
362 | 24, /* `foseg' (16 bits). */ | |
363 | 20, /* `fooff'. */ | |
364 | 18 /* `fop' (bottom 11 bits). */ | |
e750d25e JT |
365 | }; |
366 | ||
5716833c MK |
367 | #define FSAVE_ADDR(fsave, regnum) \ |
368 | (fsave + fsave_offset[regnum - I387_ST0_REGNUM]) | |
e750d25e JT |
369 | \f |
370 | ||
41d041d6 MK |
371 | /* Fill register REGNUM in REGCACHE with the appropriate value from |
372 | *FSAVE. This function masks off any of the reserved bits in | |
373 | *FSAVE. */ | |
e750d25e JT |
374 | |
375 | void | |
41d041d6 | 376 | i387_supply_fsave (struct regcache *regcache, int regnum, const void *fsave) |
e750d25e | 377 | { |
41d041d6 | 378 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache)); |
b4ad899f | 379 | const gdb_byte *regs = fsave; |
e750d25e JT |
380 | int i; |
381 | ||
5716833c MK |
382 | gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM); |
383 | ||
b87bc0d8 MK |
384 | /* Define I387_ST0_REGNUM and I387_NUM_XMM_REGS such that we use the |
385 | proper definitions for REGCACHE's architecture. */ | |
386 | ||
5716833c | 387 | #define I387_ST0_REGNUM tdep->st0_regnum |
b87bc0d8 | 388 | #define I387_NUM_XMM_REGS tdep->num_xmm_regs |
5716833c MK |
389 | |
390 | for (i = I387_ST0_REGNUM; i < I387_XMM0_REGNUM; i++) | |
ed504bdf MK |
391 | if (regnum == -1 || regnum == i) |
392 | { | |
393 | if (fsave == NULL) | |
394 | { | |
5716833c MK |
395 | regcache_raw_supply (regcache, i, NULL); |
396 | continue; | |
ed504bdf MK |
397 | } |
398 | ||
399 | /* Most of the FPU control registers occupy only 16 bits in the | |
400 | fsave area. Give those a special treatment. */ | |
5716833c MK |
401 | if (i >= I387_FCTRL_REGNUM |
402 | && i != I387_FIOFF_REGNUM && i != I387_FOOFF_REGNUM) | |
ed504bdf | 403 | { |
b4ad899f | 404 | gdb_byte val[4]; |
ed504bdf | 405 | |
5716833c | 406 | memcpy (val, FSAVE_ADDR (regs, i), 2); |
ed504bdf | 407 | val[2] = val[3] = 0; |
5716833c | 408 | if (i == I387_FOP_REGNUM) |
ed504bdf | 409 | val[1] &= ((1 << 3) - 1); |
5716833c | 410 | regcache_raw_supply (regcache, i, val); |
ed504bdf MK |
411 | } |
412 | else | |
5716833c | 413 | regcache_raw_supply (regcache, i, FSAVE_ADDR (regs, i)); |
ed504bdf | 414 | } |
b87bc0d8 MK |
415 | |
416 | /* Provide dummy values for the SSE registers. */ | |
417 | for (i = I387_XMM0_REGNUM; i < I387_MXCSR_REGNUM; i++) | |
418 | if (regnum == -1 || regnum == i) | |
419 | regcache_raw_supply (regcache, i, NULL); | |
420 | if (regnum == -1 || regnum == I387_MXCSR_REGNUM) | |
421 | { | |
b4ad899f | 422 | gdb_byte buf[4]; |
b87bc0d8 MK |
423 | |
424 | store_unsigned_integer (buf, 4, 0x1f80); | |
425 | regcache_raw_supply (regcache, I387_MXCSR_REGNUM, buf); | |
426 | } | |
427 | ||
5716833c | 428 | #undef I387_ST0_REGNUM |
b87bc0d8 | 429 | #undef I387_NUM_XMM_REGS |
e750d25e JT |
430 | } |
431 | ||
432 | /* Fill register REGNUM (if it is a floating-point register) in *FSAVE | |
63b6c53f MK |
433 | with the value from REGCACHE. If REGNUM is -1, do this for all |
434 | registers. This function doesn't touch any of the reserved bits in | |
435 | *FSAVE. */ | |
e750d25e JT |
436 | |
437 | void | |
63b6c53f | 438 | i387_collect_fsave (const struct regcache *regcache, int regnum, void *fsave) |
e750d25e | 439 | { |
5716833c | 440 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); |
b4ad899f | 441 | gdb_byte *regs = fsave; |
e750d25e JT |
442 | int i; |
443 | ||
5716833c MK |
444 | gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM); |
445 | ||
446 | /* Define I387_ST0_REGNUM such that we use the proper definitions | |
447 | for REGCACHE's architecture. */ | |
448 | #define I387_ST0_REGNUM tdep->st0_regnum | |
449 | ||
450 | for (i = I387_ST0_REGNUM; i < I387_XMM0_REGNUM; i++) | |
e750d25e JT |
451 | if (regnum == -1 || regnum == i) |
452 | { | |
453 | /* Most of the FPU control registers occupy only 16 bits in | |
454 | the fsave area. Give those a special treatment. */ | |
5716833c MK |
455 | if (i >= I387_FCTRL_REGNUM |
456 | && i != I387_FIOFF_REGNUM && i != I387_FOOFF_REGNUM) | |
e750d25e | 457 | { |
b4ad899f | 458 | gdb_byte buf[4]; |
e750d25e | 459 | |
5716833c | 460 | regcache_raw_collect (regcache, i, buf); |
e750d25e | 461 | |
5716833c | 462 | if (i == I387_FOP_REGNUM) |
e750d25e JT |
463 | { |
464 | /* The opcode occupies only 11 bits. Make sure we | |
465 | don't touch the other bits. */ | |
466 | buf[1] &= ((1 << 3) - 1); | |
5716833c | 467 | buf[1] |= ((FSAVE_ADDR (regs, i))[1] & ~((1 << 3) - 1)); |
e750d25e | 468 | } |
5716833c | 469 | memcpy (FSAVE_ADDR (regs, i), buf, 2); |
e750d25e JT |
470 | } |
471 | else | |
5716833c | 472 | regcache_raw_collect (regcache, i, FSAVE_ADDR (regs, i)); |
e750d25e | 473 | } |
5716833c | 474 | #undef I387_ST0_REGNUM |
e750d25e JT |
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 | { | |
5716833c | 484 | 32, /* %st(0) through ... */ |
e750d25e JT |
485 | 48, |
486 | 64, | |
487 | 80, | |
488 | 96, | |
489 | 112, | |
490 | 128, | |
5716833c MK |
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 ... */ | |
04c8243f MK |
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, | |
5716833c | 515 | 160 + 15 * 16, /* ... %xmm15 (128 bits each). */ |
e750d25e JT |
516 | }; |
517 | ||
518 | #define FXSAVE_ADDR(fxsave, regnum) \ | |
5716833c MK |
519 | (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM]) |
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) | |
e750d25e | 527 | |
b4ad899f | 528 | static int i387_tag (const gdb_byte *raw); |
e750d25e JT |
529 | \f |
530 | ||
41d041d6 | 531 | /* Fill register REGNUM in REGCACHE with the appropriate |
ed504bdf MK |
532 | floating-point or SSE register value from *FXSAVE. This function |
533 | masks off any of the reserved bits in *FXSAVE. */ | |
e750d25e JT |
534 | |
535 | void | |
41d041d6 | 536 | i387_supply_fxsave (struct regcache *regcache, int regnum, const void *fxsave) |
e750d25e | 537 | { |
41d041d6 | 538 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache)); |
b4ad899f | 539 | const gdb_byte *regs = fxsave; |
5716833c MK |
540 | int i; |
541 | ||
542 | gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM); | |
543 | gdb_assert (tdep->num_xmm_regs > 0); | |
dff95cc7 | 544 | |
5716833c MK |
545 | /* Define I387_ST0_REGNUM and I387_NUM_XMM_REGS such that we use the |
546 | proper definitions for REGCACHE's architecture. */ | |
e750d25e | 547 | |
5716833c MK |
548 | #define I387_ST0_REGNUM tdep->st0_regnum |
549 | #define I387_NUM_XMM_REGS tdep->num_xmm_regs | |
550 | ||
551 | for (i = I387_ST0_REGNUM; i < I387_MXCSR_REGNUM; i++) | |
ed504bdf MK |
552 | if (regnum == -1 || regnum == i) |
553 | { | |
5716833c | 554 | if (regs == NULL) |
ed504bdf | 555 | { |
5716833c | 556 | regcache_raw_supply (regcache, i, NULL); |
ed504bdf MK |
557 | continue; |
558 | } | |
932bb524 | 559 | |
ed504bdf MK |
560 | /* Most of the FPU control registers occupy only 16 bits in |
561 | the fxsave area. Give those a special treatment. */ | |
5716833c MK |
562 | if (i >= I387_FCTRL_REGNUM && i < I387_XMM0_REGNUM |
563 | && i != I387_FIOFF_REGNUM && i != I387_FOOFF_REGNUM) | |
ed504bdf | 564 | { |
b4ad899f | 565 | gdb_byte val[4]; |
ed504bdf | 566 | |
5716833c | 567 | memcpy (val, FXSAVE_ADDR (regs, i), 2); |
ed504bdf | 568 | val[2] = val[3] = 0; |
5716833c | 569 | if (i == I387_FOP_REGNUM) |
ed504bdf | 570 | val[1] &= ((1 << 3) - 1); |
5716833c | 571 | else if (i== I387_FTAG_REGNUM) |
ed504bdf MK |
572 | { |
573 | /* The fxsave area contains a simplified version of | |
574 | the tag word. We have to look at the actual 80-bit | |
575 | FP data to recreate the traditional i387 tag word. */ | |
576 | ||
577 | unsigned long ftag = 0; | |
578 | int fpreg; | |
579 | int top; | |
580 | ||
5716833c MK |
581 | top = ((FXSAVE_ADDR (regs, I387_FSTAT_REGNUM))[1] >> 3); |
582 | top &= 0x7; | |
ed504bdf MK |
583 | |
584 | for (fpreg = 7; fpreg >= 0; fpreg--) | |
585 | { | |
586 | int tag; | |
587 | ||
588 | if (val[0] & (1 << fpreg)) | |
589 | { | |
5716833c MK |
590 | int regnum = (fpreg + 8 - top) % 8 + I387_ST0_REGNUM; |
591 | tag = i387_tag (FXSAVE_ADDR (regs, regnum)); | |
ed504bdf MK |
592 | } |
593 | else | |
594 | tag = 3; /* Empty */ | |
595 | ||
596 | ftag |= tag << (2 * fpreg); | |
597 | } | |
598 | val[0] = ftag & 0xff; | |
599 | val[1] = (ftag >> 8) & 0xff; | |
600 | } | |
5716833c | 601 | regcache_raw_supply (regcache, i, val); |
ed504bdf MK |
602 | } |
603 | else | |
5716833c | 604 | regcache_raw_supply (regcache, i, FXSAVE_ADDR (regs, i)); |
ed504bdf | 605 | } |
5716833c MK |
606 | |
607 | if (regnum == I387_MXCSR_REGNUM || regnum == -1) | |
608 | { | |
609 | if (regs == NULL) | |
610 | regcache_raw_supply (regcache, I387_MXCSR_REGNUM, NULL); | |
611 | else | |
612 | regcache_raw_supply (regcache, I387_MXCSR_REGNUM, | |
613 | FXSAVE_MXCSR_ADDR (regs)); | |
614 | } | |
615 | ||
616 | #undef I387_ST0_REGNUM | |
617 | #undef I387_NUM_XMM_REGS | |
e750d25e JT |
618 | } |
619 | ||
620 | /* Fill register REGNUM (if it is a floating-point or SSE register) in | |
80571bff MK |
621 | *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for |
622 | all registers. This function doesn't touch any of the reserved | |
623 | bits in *FXSAVE. */ | |
e750d25e JT |
624 | |
625 | void | |
80571bff | 626 | i387_collect_fxsave (const struct regcache *regcache, int regnum, void *fxsave) |
e750d25e | 627 | { |
5716833c | 628 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); |
b4ad899f | 629 | gdb_byte *regs = fxsave; |
5716833c MK |
630 | int i; |
631 | ||
632 | gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM); | |
633 | gdb_assert (tdep->num_xmm_regs > 0); | |
dff95cc7 | 634 | |
5716833c MK |
635 | /* Define I387_ST0_REGNUM and I387_NUM_XMM_REGS such that we use the |
636 | proper definitions for REGCACHE's architecture. */ | |
e750d25e | 637 | |
5716833c MK |
638 | #define I387_ST0_REGNUM tdep->st0_regnum |
639 | #define I387_NUM_XMM_REGS tdep->num_xmm_regs | |
640 | ||
641 | for (i = I387_ST0_REGNUM; i < I387_MXCSR_REGNUM; i++) | |
e750d25e JT |
642 | if (regnum == -1 || regnum == i) |
643 | { | |
644 | /* Most of the FPU control registers occupy only 16 bits in | |
645 | the fxsave area. Give those a special treatment. */ | |
5716833c MK |
646 | if (i >= I387_FCTRL_REGNUM && i < I387_XMM0_REGNUM |
647 | && i != I387_FIOFF_REGNUM && i != I387_FOOFF_REGNUM) | |
e750d25e | 648 | { |
b4ad899f | 649 | gdb_byte buf[4]; |
e750d25e | 650 | |
5716833c | 651 | regcache_raw_collect (regcache, i, buf); |
e750d25e | 652 | |
5716833c | 653 | if (i == I387_FOP_REGNUM) |
e750d25e JT |
654 | { |
655 | /* The opcode occupies only 11 bits. Make sure we | |
656 | don't touch the other bits. */ | |
657 | buf[1] &= ((1 << 3) - 1); | |
5716833c | 658 | buf[1] |= ((FXSAVE_ADDR (regs, i))[1] & ~((1 << 3) - 1)); |
e750d25e | 659 | } |
5716833c | 660 | else if (i == I387_FTAG_REGNUM) |
e750d25e JT |
661 | { |
662 | /* Converting back is much easier. */ | |
663 | ||
664 | unsigned short ftag; | |
665 | int fpreg; | |
666 | ||
667 | ftag = (buf[1] << 8) | buf[0]; | |
668 | buf[0] = 0; | |
669 | buf[1] = 0; | |
670 | ||
671 | for (fpreg = 7; fpreg >= 0; fpreg--) | |
672 | { | |
673 | int tag = (ftag >> (fpreg * 2)) & 3; | |
674 | ||
675 | if (tag != 3) | |
676 | buf[0] |= (1 << fpreg); | |
677 | } | |
678 | } | |
5716833c | 679 | memcpy (FXSAVE_ADDR (regs, i), buf, 2); |
e750d25e JT |
680 | } |
681 | else | |
5716833c | 682 | regcache_raw_collect (regcache, i, FXSAVE_ADDR (regs, i)); |
e750d25e | 683 | } |
5716833c MK |
684 | |
685 | if (regnum == I387_MXCSR_REGNUM || regnum == -1) | |
686 | regcache_raw_collect (regcache, I387_MXCSR_REGNUM, | |
687 | FXSAVE_MXCSR_ADDR (regs)); | |
688 | ||
689 | #undef I387_ST0_REGNUM | |
690 | #undef I387_NUM_XMM_REGS | |
e750d25e JT |
691 | } |
692 | ||
693 | /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in | |
694 | *RAW. */ | |
695 | ||
696 | static int | |
b4ad899f | 697 | i387_tag (const gdb_byte *raw) |
e750d25e JT |
698 | { |
699 | int integer; | |
700 | unsigned int exponent; | |
701 | unsigned long fraction[2]; | |
702 | ||
703 | integer = raw[7] & 0x80; | |
704 | exponent = (((raw[9] & 0x7f) << 8) | raw[8]); | |
705 | fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]); | |
706 | fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16) | |
707 | | (raw[5] << 8) | raw[4]); | |
708 | ||
709 | if (exponent == 0x7fff) | |
710 | { | |
711 | /* Special. */ | |
712 | return (2); | |
713 | } | |
714 | else if (exponent == 0x0000) | |
715 | { | |
716 | if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer) | |
717 | { | |
718 | /* Zero. */ | |
719 | return (1); | |
720 | } | |
721 | else | |
722 | { | |
723 | /* Special. */ | |
724 | return (2); | |
725 | } | |
726 | } | |
727 | else | |
728 | { | |
729 | if (integer) | |
730 | { | |
731 | /* Valid. */ | |
732 | return (0); | |
733 | } | |
734 | else | |
735 | { | |
736 | /* Special. */ | |
737 | return (2); | |
738 | } | |
739 | } | |
740 | } | |
efb1c01c MK |
741 | |
742 | /* Prepare the FPU stack in REGCACHE for a function return. */ | |
743 | ||
744 | void | |
745 | i387_return_value (struct gdbarch *gdbarch, struct regcache *regcache) | |
746 | { | |
747 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
748 | ULONGEST fstat; | |
749 | ||
750 | /* Define I387_ST0_REGNUM such that we use the proper | |
751 | definitions for the architecture. */ | |
752 | #define I387_ST0_REGNUM tdep->st0_regnum | |
753 | ||
754 | /* Set the top of the floating-point register stack to 7. The | |
755 | actual value doesn't really matter, but 7 is what a normal | |
756 | function return would end up with if the program started out with | |
757 | a freshly initialized FPU. */ | |
758 | regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM, &fstat); | |
759 | fstat |= (7 << 11); | |
760 | regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM, fstat); | |
761 | ||
762 | /* Mark %st(1) through %st(7) as empty. Since we set the top of the | |
763 | floating-point register stack to 7, the appropriate value for the | |
764 | tag word is 0x3fff. */ | |
765 | regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM, 0x3fff); | |
766 | ||
767 | #undef I387_ST0_REGNUM | |
768 | } |