2002-05-10 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i387-nat.c
1 /* Native-dependent code for the i387.
2 Copyright 2000, 2001 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 "inferior.h"
23 #include "value.h"
24 #include "regcache.h"
25
26 #include "i387-nat.h"
27 #include "i386-tdep.h"
28
29 /* FIXME: kettenis/2000-05-21: Right now more than a few i386 targets
30 define their own routines to manage the floating-point registers in
31 GDB's register array. Most (if not all) of these targets use the
32 format used by the "fsave" instruction in their communication with
33 the OS. They should all be converted to use the routines below. */
34
35 /* At fsave_offset[REGNUM] you'll find the offset to the location in
36 the data structure used by the "fsave" instruction where GDB
37 register REGNUM is stored. */
38
39 static int fsave_offset[] =
40 {
41 28 + 0 * FPU_REG_RAW_SIZE, /* FP0_REGNUM through ... */
42 28 + 1 * FPU_REG_RAW_SIZE,
43 28 + 2 * FPU_REG_RAW_SIZE,
44 28 + 3 * FPU_REG_RAW_SIZE,
45 28 + 4 * FPU_REG_RAW_SIZE,
46 28 + 5 * FPU_REG_RAW_SIZE,
47 28 + 6 * FPU_REG_RAW_SIZE,
48 28 + 7 * FPU_REG_RAW_SIZE, /* ... FP7_REGNUM. */
49 0, /* FCTRL_REGNUM (16 bits). */
50 4, /* FSTAT_REGNUM (16 bits). */
51 8, /* FTAG_REGNUM (16 bits). */
52 16, /* FISEG_REGNUM (16 bits). */
53 12, /* FIOFF_REGNUM. */
54 24, /* FOSEG_REGNUM. */
55 20, /* FOOFF_REGNUM. */
56 18 /* FOP_REGNUM (bottom 11 bits). */
57 };
58
59 #define FSAVE_ADDR(fsave, regnum) (fsave + fsave_offset[regnum - FP0_REGNUM])
60 \f
61
62 /* Fill register REGNUM in GDB's register array with the appropriate
63 value from *FSAVE. This function masks off any of the reserved
64 bits in *FSAVE. */
65
66 void
67 i387_supply_register (int regnum, char *fsave)
68 {
69 /* Most of the FPU control registers occupy only 16 bits in
70 the fsave area. Give those a special treatment. */
71 if (regnum >= FPC_REGNUM
72 && regnum != FIOFF_REGNUM && regnum != FOOFF_REGNUM)
73 {
74 unsigned int val = *(unsigned short *) (FSAVE_ADDR (fsave, regnum));
75
76 if (regnum == FOP_REGNUM)
77 {
78 val &= ((1 << 11) - 1);
79 supply_register (regnum, (char *) &val);
80 }
81 else
82 supply_register (regnum, (char *) &val);
83 }
84 else
85 supply_register (regnum, FSAVE_ADDR (fsave, regnum));
86 }
87
88 /* Fill GDB's register array with the floating-point register values
89 in *FSAVE. This function masks off any of the reserved
90 bits in *FSAVE. */
91
92 void
93 i387_supply_fsave (char *fsave)
94 {
95 int i;
96
97 for (i = FP0_REGNUM; i < XMM0_REGNUM; i++)
98 i387_supply_register (i, fsave);
99 }
100
101 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
102 with the value in GDB's register array. If REGNUM is -1, do this
103 for all registers. This function doesn't touch any of the reserved
104 bits in *FSAVE. */
105
106 void
107 i387_fill_fsave (char *fsave, int regnum)
108 {
109 int i;
110
111 for (i = FP0_REGNUM; i < XMM0_REGNUM; i++)
112 if (regnum == -1 || regnum == i)
113 {
114 /* Most of the FPU control registers occupy only 16 bits in
115 the fsave area. Give those a special treatment. */
116 if (i >= FPC_REGNUM
117 && i != FIOFF_REGNUM && i != FOOFF_REGNUM)
118 {
119 char buf[4];
120
121 regcache_collect (i, buf);
122
123 if (i == FOP_REGNUM)
124 {
125 unsigned short oldval, newval;
126
127 /* The opcode occupies only 11 bits. */
128 oldval = (*(unsigned short *) (FSAVE_ADDR (fsave, i)));
129 newval = *(unsigned short *) buf;
130 newval &= ((1 << 11) - 1);
131 newval |= oldval & ~((1 << 11) - 1);
132 memcpy (FSAVE_ADDR (fsave, i), &newval, 2);
133 }
134 else
135 memcpy (FSAVE_ADDR (fsave, i), buf, 2);
136 }
137 else
138 regcache_collect (i, FSAVE_ADDR (fsave, i));
139 }
140 }
141 \f
142
143 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
144 the data structure used by the "fxsave" instruction where GDB
145 register REGNUM is stored. */
146
147 static int fxsave_offset[] =
148 {
149 32, /* FP0_REGNUM through ... */
150 48,
151 64,
152 80,
153 96,
154 112,
155 128,
156 144, /* ... FP7_REGNUM (80 bits each). */
157 0, /* FCTRL_REGNUM (16 bits). */
158 2, /* FSTAT_REGNUM (16 bits). */
159 4, /* FTAG_REGNUM (16 bits). */
160 12, /* FISEG_REGNUM (16 bits). */
161 8, /* FIOFF_REGNUM. */
162 20, /* FOSEG_REGNUM (16 bits). */
163 16, /* FOOFF_REGNUM. */
164 6, /* FOP_REGNUM (bottom 11 bits). */
165 160, /* XMM0_REGNUM through ... */
166 176,
167 192,
168 208,
169 224,
170 240,
171 256,
172 272, /* ... XMM7_REGNUM (128 bits each). */
173 24, /* MXCSR_REGNUM. */
174 };
175
176 #define FXSAVE_ADDR(fxsave, regnum) \
177 (fxsave + fxsave_offset[regnum - FP0_REGNUM])
178
179 static int i387_tag (unsigned char *raw);
180 \f
181
182 /* Fill GDB's register array with the floating-point and SSE register
183 values in *FXSAVE. This function masks off any of the reserved
184 bits in *FXSAVE. */
185
186 void
187 i387_supply_fxsave (char *fxsave)
188 {
189 int i;
190
191 for (i = FP0_REGNUM; i <= MXCSR_REGNUM; i++)
192 {
193 /* Most of the FPU control registers occupy only 16 bits in
194 the fxsave area. Give those a special treatment. */
195 if (i >= FPC_REGNUM && i < XMM0_REGNUM
196 && i != FIOFF_REGNUM && i != FOOFF_REGNUM)
197 {
198 unsigned long val = *(unsigned short *) (FXSAVE_ADDR (fxsave, i));
199
200 if (i == FOP_REGNUM)
201 {
202 val &= ((1 << 11) - 1);
203 supply_register (i, (char *) &val);
204 }
205 else if (i== FTAG_REGNUM)
206 {
207 /* The fxsave area contains a simplified version of the
208 tag word. We have to look at the actual 80-bit FP
209 data to recreate the traditional i387 tag word. */
210
211 unsigned long ftag = 0;
212 unsigned long fstat;
213 int fpreg;
214 int top;
215
216 fstat = *(unsigned short *) (FXSAVE_ADDR (fxsave, FSTAT_REGNUM));
217 top = ((fstat >> 11) & 0x7);
218
219 for (fpreg = 7; fpreg >= 0; fpreg--)
220 {
221 int tag;
222
223 if (val & (1 << fpreg))
224 {
225 int regnum = (fpreg + 8 - top) % 8 + FP0_REGNUM;
226 tag = i387_tag (FXSAVE_ADDR (fxsave, regnum));
227 }
228 else
229 tag = 3; /* Empty */
230
231 ftag |= tag << (2 * fpreg);
232 }
233 supply_register (i, (char *) &ftag);
234 }
235 else
236 supply_register (i, (char *) &val);
237 }
238 else
239 supply_register (i, FXSAVE_ADDR (fxsave, i));
240 }
241 }
242
243 /* Fill register REGNUM (if it is a floating-point or SSE register) in
244 *FXSAVE with the value in GDB's register array. If REGNUM is -1, do
245 this for all registers. This function doesn't touch any of the
246 reserved bits in *FXSAVE. */
247
248 void
249 i387_fill_fxsave (char *fxsave, int regnum)
250 {
251 int i;
252
253 for (i = FP0_REGNUM; i <= MXCSR_REGNUM; i++)
254 if (regnum == -1 || regnum == i)
255 {
256 /* Most of the FPU control registers occupy only 16 bits in
257 the fxsave area. Give those a special treatment. */
258 if (i >= FPC_REGNUM && i < XMM0_REGNUM
259 && i != FIOFF_REGNUM && i != FDOFF_REGNUM)
260 {
261 char buf[4];
262
263 regcache_collect (i, buf);
264
265 if (i == FOP_REGNUM)
266 {
267 unsigned short oldval, newval;
268
269 /* The opcode occupies only 11 bits. */
270 oldval = (*(unsigned short *) (FXSAVE_ADDR (fxsave, i)));
271 newval = *(unsigned short *) buf;
272 newval &= ((1 << 11) - 1);
273 newval |= oldval & ~((1 << 11) - 1);
274 memcpy (FXSAVE_ADDR (fxsave, i), &newval, 2);
275 }
276 else if (i == FTAG_REGNUM)
277 {
278 /* Converting back is much easier. */
279
280 unsigned short val = 0;
281 unsigned short ftag;
282 int fpreg;
283
284 ftag = *(unsigned short *) buf;
285
286 for (fpreg = 7; fpreg >= 0; fpreg--)
287 {
288 int tag = (ftag >> (fpreg * 2)) & 3;
289
290 if (tag != 3)
291 val |= (1 << fpreg);
292 }
293
294 memcpy (FXSAVE_ADDR (fxsave, i), &val, 2);
295 }
296 else
297 memcpy (FXSAVE_ADDR (fxsave, i), buf, 2);
298 }
299 else
300 regcache_collect (i, FXSAVE_ADDR (fxsave, i));
301 }
302 }
303
304 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
305 *RAW. */
306
307 static int
308 i387_tag (unsigned char *raw)
309 {
310 int integer;
311 unsigned int exponent;
312 unsigned long fraction[2];
313
314 integer = raw[7] & 0x80;
315 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
316 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
317 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
318 | (raw[5] << 8) | raw[4]);
319
320 if (exponent == 0x7fff)
321 {
322 /* Special. */
323 return (2);
324 }
325 else if (exponent == 0x0000)
326 {
327 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
328 {
329 /* Zero. */
330 return (1);
331 }
332 else
333 {
334 /* Special. */
335 return (2);
336 }
337 }
338 else
339 {
340 if (integer)
341 {
342 /* Valid. */
343 return (0);
344 }
345 else
346 {
347 /* Special. */
348 return (2);
349 }
350 }
351 }
This page took 0.036343 seconds and 4 git commands to generate.