Replace CONST with const
[deliverable/binutils-gdb.git] / gdb / i387-nat.c
CommitLineData
b2450fc5 1/* Native-dependent code for the i387.
f31e928c 2 Copyright 2000, 2001 Free Software Foundation, Inc.
b2450fc5
MK
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"
4e052eda 24#include "regcache.h"
b2450fc5 25
f31e928c
MK
26#include "i387-nat.h"
27
9a82579f
JS
28#if GDB_MULTI_ARCH > 0
29#include "i386-tdep.h"
30#endif
31
b2450fc5
MK
32/* FIXME: kettenis/2000-05-21: Right now more than a few i386 targets
33 define their own routines to manage the floating-point registers in
34 GDB's register array. Most (if not all) of these targets use the
35 format used by the "fsave" instruction in their communication with
36 the OS. They should all be converted to use the routines below. */
37
f31e928c 38/* At fsave_offset[REGNUM] you'll find the offset to the location in
b2450fc5 39 the data structure used by the "fsave" instruction where GDB
f31e928c 40 register REGNUM is stored. */
b2450fc5
MK
41
42static int fsave_offset[] =
43{
44 28 + 0 * FPU_REG_RAW_SIZE, /* FP0_REGNUM through ... */
45 28 + 1 * FPU_REG_RAW_SIZE,
46 28 + 2 * FPU_REG_RAW_SIZE,
47 28 + 3 * FPU_REG_RAW_SIZE,
48 28 + 4 * FPU_REG_RAW_SIZE,
49 28 + 5 * FPU_REG_RAW_SIZE,
50 28 + 6 * FPU_REG_RAW_SIZE,
51 28 + 7 * FPU_REG_RAW_SIZE, /* ... FP7_REGNUM. */
52 0, /* FCTRL_REGNUM (16 bits). */
53 4, /* FSTAT_REGNUM (16 bits). */
54 8, /* FTAG_REGNUM (16 bits). */
55 16, /* FCS_REGNUM (16 bits). */
56 12, /* FCOFF_REGNUM. */
57 24, /* FDS_REGNUM. */
58 20, /* FDOFF_REGNUM. */
59 18 /* FOP_REGNUM (bottom 11 bits). */
60};
61
62#define FSAVE_ADDR(fsave, regnum) (fsave + fsave_offset[regnum - FP0_REGNUM])
63\f
64
f31e928c
MK
65/* Fill register REGNUM in GDB's register array with the appropriate
66 value from *FSAVE. This function masks off any of the reserved
67 bits in *FSAVE. */
68
69void
70i387_supply_register (int regnum, char *fsave)
71{
72 /* Most of the FPU control registers occupy only 16 bits in
73 the fsave area. Give those a special treatment. */
74 if (regnum >= FIRST_FPU_CTRL_REGNUM
75 && regnum != FCOFF_REGNUM && regnum != FDOFF_REGNUM)
76 {
77 unsigned int val = *(unsigned short *) (FSAVE_ADDR (fsave, regnum));
78
79 if (regnum == FOP_REGNUM)
80 {
81 val &= ((1 << 11) - 1);
82 supply_register (regnum, (char *) &val);
83 }
84 else
85 supply_register (regnum, (char *) &val);
86 }
87 else
88 supply_register (regnum, FSAVE_ADDR (fsave, regnum));
89}
90
b2450fc5
MK
91/* Fill GDB's register array with the floating-point register values
92 in *FSAVE. This function masks off any of the reserved
93 bits in *FSAVE. */
94
95void
96i387_supply_fsave (char *fsave)
97{
98 int i;
99
100 for (i = FP0_REGNUM; i <= LAST_FPU_CTRL_REGNUM; i++)
f31e928c 101 i387_supply_register (i, fsave);
b2450fc5
MK
102}
103
f31e928c
MK
104/* Fill register REGNUM (if it is a floating-point register) in *FSAVE
105 with the value in GDB's register array. If REGNUM is -1, do this
b2450fc5
MK
106 for all registers. This function doesn't touch any of the reserved
107 bits in *FSAVE. */
108
109void
f31e928c 110i387_fill_fsave (char *fsave, int regnum)
b2450fc5
MK
111{
112 int i;
113
114 for (i = FP0_REGNUM; i <= LAST_FPU_CTRL_REGNUM; i++)
f31e928c 115 if (regnum == -1 || regnum == i)
b2450fc5
MK
116 {
117 /* Most of the FPU control registers occupy only 16 bits in
118 the fsave area. Give those a special treatment. */
119 if (i >= FIRST_FPU_CTRL_REGNUM
120 && i != FCOFF_REGNUM && i != FDOFF_REGNUM)
121 {
122 if (i == FOP_REGNUM)
123 {
124 unsigned short oldval, newval;
125
126 /* The opcode occupies only 11 bits. */
127 oldval = (*(unsigned short *) (FSAVE_ADDR (fsave, i)));
128 newval = *(unsigned short *) &registers[REGISTER_BYTE (i)];
129 newval &= ((1 << 11) - 1);
130 newval |= oldval & ~((1 << 11) - 1);
131 memcpy (FSAVE_ADDR (fsave, i), &newval, 2);
132 }
133 else
134 memcpy (FSAVE_ADDR (fsave, i), &registers[REGISTER_BYTE (i)], 2);
135 }
136 else
137 memcpy (FSAVE_ADDR (fsave, i), &registers[REGISTER_BYTE (i)],
138 REGISTER_RAW_SIZE (i));
139 }
140}
e2890f08
MK
141\f
142
f31e928c 143/* At fxsave_offset[REGNUM] you'll find the offset to the location in
e2890f08 144 the data structure used by the "fxsave" instruction where GDB
f31e928c 145 register REGNUM is stored. */
e2890f08
MK
146
147static 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, /* FCS_REGNUM (16 bits). */
161 8, /* FCOFF_REGNUM. */
162 20, /* FDS_REGNUM (16 bits). */
163 16, /* FDOFF_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
179static 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
186void
187i387_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 >= FIRST_FPU_CTRL_REGNUM && i < XMM0_REGNUM
196 && i != FCOFF_REGNUM && i != FDOFF_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));
128437e6 217 top = ((fstat >> 11) & 0x7);
e2890f08
MK
218
219 for (fpreg = 7; fpreg >= 0; fpreg--)
220 {
128437e6 221 int tag;
e2890f08
MK
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 }
128437e6
DH
228 else
229 tag = 3; /* Empty */
e2890f08
MK
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
f31e928c
MK
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
e2890f08
MK
245 this for all registers. This function doesn't touch any of the
246 reserved bits in *FXSAVE. */
247
248void
f31e928c 249i387_fill_fxsave (char *fxsave, int regnum)
e2890f08
MK
250{
251 int i;
252
253 for (i = FP0_REGNUM; i <= MXCSR_REGNUM; i++)
f31e928c 254 if (regnum == -1 || regnum == i)
e2890f08
MK
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 >= FIRST_FPU_CTRL_REGNUM && i < XMM0_REGNUM
259 && i != FCOFF_REGNUM && i != FDOFF_REGNUM)
260 {
261 if (i == FOP_REGNUM)
262 {
263 unsigned short oldval, newval;
264
265 /* The opcode occupies only 11 bits. */
266 oldval = (*(unsigned short *) (FXSAVE_ADDR (fxsave, i)));
267 newval = *(unsigned short *) &registers[REGISTER_BYTE (i)];
268 newval &= ((1 << 11) - 1);
269 newval |= oldval & ~((1 << 11) - 1);
270 memcpy (FXSAVE_ADDR (fxsave, i), &newval, 2);
271 }
272 else if (i == FTAG_REGNUM)
273 {
274 /* Converting back is much easier. */
275
276 unsigned char val = 0;
277 unsigned short ftag;
278 int fpreg;
279
280 ftag = *(unsigned short *) &registers[REGISTER_BYTE (i)];
281
282 for (fpreg = 7; fpreg >= 0; fpreg--)
283 {
128437e6 284 int tag = (ftag >> (fpreg * 2)) & 3;
e2890f08 285
128437e6
DH
286 if (tag != 3)
287 val |= (1 << (fpreg * 2));
e2890f08
MK
288 }
289
290 memcpy (FXSAVE_ADDR (fxsave, i), &val, 2);
291 }
292 else
293 memcpy (FXSAVE_ADDR (fxsave, i),
294 &registers[REGISTER_BYTE (i)], 2);
295 }
296 else
297 memcpy (FXSAVE_ADDR (fxsave, i), &registers[REGISTER_BYTE (i)],
298 REGISTER_RAW_SIZE (i));
299 }
300}
301
302/* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
303 *RAW. */
304
305static int
306i387_tag (unsigned char *raw)
307{
308 int integer;
309 unsigned int exponent;
310 unsigned long fraction[2];
311
312 integer = raw[7] & 0x80;
313 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
314 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
315 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
316 | (raw[5] << 8) | raw[4]);
317
318 if (exponent == 0x7fff)
319 {
320 /* Special. */
128437e6 321 return (2);
e2890f08
MK
322 }
323 else if (exponent == 0x0000)
324 {
128437e6 325 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
e2890f08 326 {
128437e6
DH
327 /* Zero. */
328 return (1);
e2890f08
MK
329 }
330 else
331 {
332 /* Special. */
128437e6 333 return (2);
e2890f08
MK
334 }
335 }
336 else
337 {
128437e6 338 if (integer)
e2890f08 339 {
128437e6
DH
340 /* Valid. */
341 return (0);
e2890f08
MK
342 }
343 else
344 {
345 /* Special. */
128437e6 346 return (2);
e2890f08
MK
347 }
348 }
349}
This page took 0.101605 seconds and 4 git commands to generate.