| 1 | /* Intel 387 floating point stuff. |
| 2 | Copyright (C) 1988, 1989, 1991, 1998 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "frame.h" |
| 23 | #include "inferior.h" |
| 24 | #include "language.h" |
| 25 | #include "value.h" |
| 26 | #include "gdbcore.h" |
| 27 | #include "floatformat.h" |
| 28 | |
| 29 | void i387_to_double PARAMS ((char *, char *)); |
| 30 | void double_to_i387 PARAMS ((char *, char *)); |
| 31 | |
| 32 | static void print_387_control_bits PARAMS ((unsigned int control)); |
| 33 | static void print_387_status_bits PARAMS ((unsigned int status)); |
| 34 | |
| 35 | /* FIXME: Eliminate these routines when we have the time to change all |
| 36 | the callers. */ |
| 37 | |
| 38 | void |
| 39 | i387_to_double (from, to) |
| 40 | char *from; |
| 41 | char *to; |
| 42 | { |
| 43 | floatformat_to_double (&floatformat_i387_ext, from, (double *) to); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | double_to_i387 (from, to) |
| 48 | char *from; |
| 49 | char *to; |
| 50 | { |
| 51 | floatformat_from_double (&floatformat_i387_ext, (double *) from, to); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | print_387_control_bits (control) |
| 56 | unsigned int control; |
| 57 | { |
| 58 | switch ((control >> 8) & 3) |
| 59 | { |
| 60 | case 0: |
| 61 | puts_unfiltered (" 24 bit; "); |
| 62 | break; |
| 63 | case 1: |
| 64 | puts_unfiltered (" (bad); "); |
| 65 | break; |
| 66 | case 2: |
| 67 | puts_unfiltered (" 53 bit; "); |
| 68 | break; |
| 69 | case 3: |
| 70 | puts_unfiltered (" 64 bit; "); |
| 71 | break; |
| 72 | } |
| 73 | switch ((control >> 10) & 3) |
| 74 | { |
| 75 | case 0: |
| 76 | puts_unfiltered ("NEAR; "); |
| 77 | break; |
| 78 | case 1: |
| 79 | puts_unfiltered ("DOWN; "); |
| 80 | break; |
| 81 | case 2: |
| 82 | puts_unfiltered ("UP; "); |
| 83 | break; |
| 84 | case 3: |
| 85 | puts_unfiltered ("CHOP; "); |
| 86 | break; |
| 87 | } |
| 88 | if (control & 0x3f) |
| 89 | { |
| 90 | puts_unfiltered ("mask"); |
| 91 | if (control & 0x0001) |
| 92 | puts_unfiltered (" INVAL"); |
| 93 | if (control & 0x0002) |
| 94 | puts_unfiltered (" DENOR"); |
| 95 | if (control & 0x0004) |
| 96 | puts_unfiltered (" DIVZ"); |
| 97 | if (control & 0x0008) |
| 98 | puts_unfiltered (" OVERF"); |
| 99 | if (control & 0x0010) |
| 100 | puts_unfiltered (" UNDER"); |
| 101 | if (control & 0x0020) |
| 102 | puts_unfiltered (" LOS"); |
| 103 | puts_unfiltered (";"); |
| 104 | } |
| 105 | printf_unfiltered ("\n"); |
| 106 | if (control & 0xe080) |
| 107 | warning ("\nreserved bits on: %s", |
| 108 | local_hex_string (control & 0xe080)); |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | print_387_control_word (control) |
| 113 | unsigned int control; |
| 114 | { |
| 115 | printf_filtered ("control %s:", local_hex_string(control & 0xffff)); |
| 116 | print_387_control_bits (control); |
| 117 | puts_unfiltered ("\n"); |
| 118 | } |
| 119 | |
| 120 | static void |
| 121 | print_387_status_bits (status) |
| 122 | unsigned int status; |
| 123 | { |
| 124 | printf_unfiltered (" flags %d%d%d%d; ", |
| 125 | (status & 0x4000) != 0, |
| 126 | (status & 0x0400) != 0, |
| 127 | (status & 0x0200) != 0, |
| 128 | (status & 0x0100) != 0); |
| 129 | printf_unfiltered ("top %d; ", (status >> 11) & 7); |
| 130 | if (status & 0xff) |
| 131 | { |
| 132 | puts_unfiltered ("excep"); |
| 133 | if (status & 0x0001) puts_unfiltered (" INVAL"); |
| 134 | if (status & 0x0002) puts_unfiltered (" DENOR"); |
| 135 | if (status & 0x0004) puts_unfiltered (" DIVZ"); |
| 136 | if (status & 0x0008) puts_unfiltered (" OVERF"); |
| 137 | if (status & 0x0010) puts_unfiltered (" UNDER"); |
| 138 | if (status & 0x0020) puts_unfiltered (" LOS"); |
| 139 | if (status & 0x0040) puts_unfiltered (" STACK"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | print_387_status_word (status) |
| 145 | unsigned int status; |
| 146 | { |
| 147 | printf_filtered ("status %s:", local_hex_string (status & 0xffff)); |
| 148 | print_387_status_bits (status); |
| 149 | puts_unfiltered ("\n"); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | i387_print_register (raw_regs, regnum) |
| 154 | char *raw_regs; |
| 155 | int regnum; |
| 156 | { |
| 157 | unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE]; |
| 158 | unsigned long val; |
| 159 | int j, sign, special; |
| 160 | unsigned swd, tags, expon, top, norm, ls, ms; |
| 161 | char string[12]; |
| 162 | |
| 163 | #if (FPREG_RAW_SIZE != 10) |
| 164 | #error "Bad FPREG_RAW_SIZE" |
| 165 | #endif |
| 166 | |
| 167 | printf_filtered ("%8.8s: ", REGISTER_NAME (regnum)); |
| 168 | if (regnum < FPDATA_REGNUM) |
| 169 | { |
| 170 | val = extract_unsigned_integer (raw_regs + REGISTER_BYTE (regnum), 4); |
| 171 | if ( (regnum < FPSTART_REGNUM + 3) || |
| 172 | (regnum == FPSTART_REGNUM + 6) ) |
| 173 | /* Don't print the un-modifiable bytes. */ |
| 174 | sprintf(string, "0x%04x", val & 0xffff); |
| 175 | else |
| 176 | sprintf(string, "0x%08x", val); |
| 177 | |
| 178 | printf_unfiltered ("%10.10s", string); |
| 179 | |
| 180 | if (regnum == FPCONTROL_REGNUM) |
| 181 | print_387_control_bits (val); |
| 182 | else if (regnum == FPSTATUS_REGNUM) |
| 183 | print_387_status_bits (val); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | /* An FPU stack register. */ |
| 188 | if ( REGISTER_RAW_SIZE (regnum) != FPREG_RAW_SIZE ) |
| 189 | error ("GDB bug: i387-tdep.c (i387_print_register): wrong size for FPU stack register"); |
| 190 | |
| 191 | /* Put the data in the buffer. No conversions are ever necessary. */ |
| 192 | memcpy (virtual_buffer, raw_regs + REGISTER_BYTE (regnum), |
| 193 | FPREG_RAW_SIZE); |
| 194 | |
| 195 | swd = extract_signed_integer (raw_regs + REGISTER_BYTE (FPSTATUS_REGNUM), |
| 196 | 4); |
| 197 | top = (swd >> 11) & 7; |
| 198 | tags = extract_signed_integer (raw_regs + REGISTER_BYTE (FPTAG_REGNUM), |
| 199 | 4); |
| 200 | |
| 201 | puts_unfiltered ("0x"); |
| 202 | for (j = 0; j < FPREG_RAW_SIZE; j++) |
| 203 | printf_unfiltered ("%02x", |
| 204 | (unsigned char)raw_regs[REGISTER_BYTE (regnum) |
| 205 | + FPREG_RAW_SIZE - 1 - j]); |
| 206 | |
| 207 | puts_unfiltered (" "); |
| 208 | special = 0; |
| 209 | switch ((tags >> (((regnum - FPDATA_REGNUM + top) & 7) * 2)) & 3) |
| 210 | { |
| 211 | case 0: puts_unfiltered ("Valid "); break; |
| 212 | case 1: puts_unfiltered ("Zero "); break; |
| 213 | case 2: puts_unfiltered ("Spec "); |
| 214 | special = 1; |
| 215 | break; |
| 216 | case 3: puts_unfiltered ("Empty "); break; |
| 217 | } |
| 218 | |
| 219 | expon = extract_unsigned_integer (raw_regs + REGISTER_BYTE (regnum) |
| 220 | + FPREG_RAW_SIZE - 2, 2); |
| 221 | sign = expon & 0x8000; |
| 222 | expon &= 0x7fff; |
| 223 | ms = extract_unsigned_integer (raw_regs + REGISTER_BYTE (regnum) + 4, 4); |
| 224 | ls = extract_signed_integer (raw_regs + REGISTER_BYTE (regnum), 4); |
| 225 | norm = ms & 0x80000000; |
| 226 | |
| 227 | if ( expon == 0 ) |
| 228 | { |
| 229 | if ( ms | ls ) |
| 230 | { |
| 231 | /* Denormal or Pseudodenormal. */ |
| 232 | if ( norm ) |
| 233 | puts_unfiltered ("Pseudo "); |
| 234 | else |
| 235 | puts_unfiltered ("Denorm "); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | /* Zero. */ |
| 240 | puts_unfiltered ("Zero "); |
| 241 | } |
| 242 | } |
| 243 | else if ( expon == 0x7fff ) |
| 244 | { |
| 245 | /* Infinity, NaN or unsupported. */ |
| 246 | if ( (ms == 0x80000000) && |
| 247 | (ls == 0) ) |
| 248 | { |
| 249 | puts_unfiltered ("Infty "); |
| 250 | } |
| 251 | else if ( norm ) |
| 252 | { |
| 253 | if ( ms & 0x40000000 ) |
| 254 | puts_unfiltered ("QNaN "); |
| 255 | else |
| 256 | puts_unfiltered ("SNaN "); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | puts_unfiltered ("Unsupp "); |
| 261 | } |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | /* Normal or unsupported. */ |
| 266 | if ( norm ) |
| 267 | puts_unfiltered ("Normal "); |
| 268 | else |
| 269 | puts_unfiltered ("Unsupp "); |
| 270 | } |
| 271 | |
| 272 | val_print (REGISTER_VIRTUAL_TYPE (regnum), virtual_buffer, 0, 0, |
| 273 | gdb_stdout, 0, |
| 274 | 1, 0, Val_pretty_default); |
| 275 | } |
| 276 | puts_filtered ("\n"); |
| 277 | } |
| 278 | |
| 279 | void i387_float_info(void) |
| 280 | { |
| 281 | char raw_regs [REGISTER_BYTES]; |
| 282 | int i; |
| 283 | |
| 284 | for (i = FPSTART_REGNUM; i <= FPEND_REGNUM; i++) |
| 285 | read_relative_register_raw_bytes (i, raw_regs + REGISTER_BYTE (i)); |
| 286 | |
| 287 | for (i = FPSTART_REGNUM; i <= FPEND_REGNUM; i++) |
| 288 | i387_print_register (raw_regs, i); |
| 289 | } |
| 290 | |
| 291 | #ifdef LD_I387 |
| 292 | int |
| 293 | i387_extract_floating (PTR addr, int len, DOUBLEST *dretptr) |
| 294 | { |
| 295 | if (len == TARGET_LONG_DOUBLE_BIT / 8) |
| 296 | { |
| 297 | if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT) |
| 298 | { |
| 299 | DOUBLEST retval; |
| 300 | |
| 301 | memcpy (dretptr, addr, sizeof (retval)); |
| 302 | } |
| 303 | else |
| 304 | floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, dretptr); |
| 305 | |
| 306 | return 1; |
| 307 | } |
| 308 | else |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int |
| 313 | i387_store_floating (PTR addr, int len, DOUBLEST val) |
| 314 | { |
| 315 | if (len == TARGET_LONG_DOUBLE_BIT / 8) |
| 316 | { |
| 317 | /* This `if' may be totally stupid. I just put it in here to be |
| 318 | absolutely sure I'm preserving the semantics of the code I'm |
| 319 | frobbing, while I try to maintain portability boundaries; I |
| 320 | don't actually know exactly what it's doing. -JimB, May 1999 */ |
| 321 | if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT) |
| 322 | memcpy (addr, &val, sizeof (val)); |
| 323 | else |
| 324 | floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr); |
| 325 | |
| 326 | return 1; |
| 327 | } |
| 328 | else |
| 329 | return 0; |
| 330 | } |
| 331 | #endif /* LD_I387 */ |