OSDN Git Service

82baa178101de092097acc6f90c310eb35e6c9c4
[pf3gnuchains/pf3gnuchains3x.git] / gdb / sparc64-tdep.c
1 /* Target-dependent code for UltraSPARC.
2
3    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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 3 of the License, or
11    (at your option) any later version.
12
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.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "dwarf2-frame.h"
24 #include "floatformat.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbcore.h"
29 #include "gdbtypes.h"
30 #include "inferior.h"
31 #include "symtab.h"
32 #include "objfiles.h"
33 #include "osabi.h"
34 #include "regcache.h"
35 #include "target.h"
36 #include "value.h"
37
38 #include "gdb_assert.h"
39 #include "gdb_string.h"
40
41 #include "sparc64-tdep.h"
42
43 /* This file implements the The SPARC 64-bit ABI as defined by the
44    section "Low-Level System Information" of the SPARC Compliance
45    Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
46    SPARC.  */
47
48 /* Please use the sparc32_-prefix for 32-bit specific code, the
49    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
50    code can handle both.  */
51 \f
52 /* The functions on this page are intended to be used to classify
53    function arguments.  */
54
55 /* Check whether TYPE is "Integral or Pointer".  */
56
57 static int
58 sparc64_integral_or_pointer_p (const struct type *type)
59 {
60   switch (TYPE_CODE (type))
61     {
62     case TYPE_CODE_INT:
63     case TYPE_CODE_BOOL:
64     case TYPE_CODE_CHAR:
65     case TYPE_CODE_ENUM:
66     case TYPE_CODE_RANGE:
67       {
68         int len = TYPE_LENGTH (type);
69         gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
70       }
71       return 1;
72     case TYPE_CODE_PTR:
73     case TYPE_CODE_REF:
74       {
75         int len = TYPE_LENGTH (type);
76         gdb_assert (len == 8);
77       }
78       return 1;
79     default:
80       break;
81     }
82
83   return 0;
84 }
85
86 /* Check whether TYPE is "Floating".  */
87
88 static int
89 sparc64_floating_p (const struct type *type)
90 {
91   switch (TYPE_CODE (type))
92     {
93     case TYPE_CODE_FLT:
94       {
95         int len = TYPE_LENGTH (type);
96         gdb_assert (len == 4 || len == 8 || len == 16);
97       }
98       return 1;
99     default:
100       break;
101     }
102
103   return 0;
104 }
105
106 /* Check whether TYPE is "Structure or Union".  */
107
108 static int
109 sparc64_structure_or_union_p (const struct type *type)
110 {
111   switch (TYPE_CODE (type))
112     {
113     case TYPE_CODE_STRUCT:
114     case TYPE_CODE_UNION:
115       return 1;
116     default:
117       break;
118     }
119
120   return 0;
121 }
122 \f
123
124 /* Construct types for ISA-specific registers.  */
125
126 static struct type *
127 sparc64_pstate_type (struct gdbarch *gdbarch)
128 {
129   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
130
131   if (!tdep->sparc64_pstate_type)
132     {
133       struct type *type;
134
135       type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 8);
136       append_flags_type_flag (type, 0, "AG");
137       append_flags_type_flag (type, 1, "IE");
138       append_flags_type_flag (type, 2, "PRIV");
139       append_flags_type_flag (type, 3, "AM");
140       append_flags_type_flag (type, 4, "PEF");
141       append_flags_type_flag (type, 5, "RED");
142       append_flags_type_flag (type, 8, "TLE");
143       append_flags_type_flag (type, 9, "CLE");
144       append_flags_type_flag (type, 10, "PID0");
145       append_flags_type_flag (type, 11, "PID1");
146
147       tdep->sparc64_pstate_type = type;
148     }
149
150   return tdep->sparc64_pstate_type;
151 }
152
153 static struct type *
154 sparc64_fsr_type (struct gdbarch *gdbarch)
155 {
156   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
157
158   if (!tdep->sparc64_fsr_type)
159     {
160       struct type *type;
161
162       type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 8);
163       append_flags_type_flag (type, 0, "NXA");
164       append_flags_type_flag (type, 1, "DZA");
165       append_flags_type_flag (type, 2, "UFA");
166       append_flags_type_flag (type, 3, "OFA");
167       append_flags_type_flag (type, 4, "NVA");
168       append_flags_type_flag (type, 5, "NXC");
169       append_flags_type_flag (type, 6, "DZC");
170       append_flags_type_flag (type, 7, "UFC");
171       append_flags_type_flag (type, 8, "OFC");
172       append_flags_type_flag (type, 9, "NVC");
173       append_flags_type_flag (type, 22, "NS");
174       append_flags_type_flag (type, 23, "NXM");
175       append_flags_type_flag (type, 24, "DZM");
176       append_flags_type_flag (type, 25, "UFM");
177       append_flags_type_flag (type, 26, "OFM");
178       append_flags_type_flag (type, 27, "NVM");
179
180       tdep->sparc64_fsr_type = type;
181     }
182
183   return tdep->sparc64_fsr_type;
184 }
185
186 static struct type *
187 sparc64_fprs_type (struct gdbarch *gdbarch)
188 {
189   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
190
191   if (!tdep->sparc64_fprs_type)
192     {
193       struct type *type;
194
195       type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 8);
196       append_flags_type_flag (type, 0, "DL");
197       append_flags_type_flag (type, 1, "DU");
198       append_flags_type_flag (type, 2, "FEF");
199
200       tdep->sparc64_fprs_type = type;
201     }
202
203   return tdep->sparc64_fprs_type;
204 }
205
206
207 /* Register information.  */
208
209 static const char *sparc64_register_names[] =
210 {
211   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
212   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
213   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
214   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
215
216   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
217   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
218   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
219   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
220   "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",
221   "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",
222
223   "pc", "npc",
224   
225   /* FIXME: Give "state" a name until we start using register groups.  */
226   "state",
227   "fsr",
228   "fprs",
229   "y",
230 };
231
232 /* Total number of registers.  */
233 #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)
234
235 /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
236    registers as "psuedo" registers.  */
237
238 static const char *sparc64_pseudo_register_names[] =
239 {
240   "cwp", "pstate", "asi", "ccr",
241
242   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
243   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
244   "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
245   "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",
246
247   "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
248   "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
249 };
250
251 /* Total number of pseudo registers.  */
252 #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)
253
254 /* Return the name of register REGNUM.  */
255
256 static const char *
257 sparc64_register_name (struct gdbarch *gdbarch, int regnum)
258 {
259   if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
260     return sparc64_register_names[regnum];
261
262   if (regnum >= SPARC64_NUM_REGS
263       && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
264     return sparc64_pseudo_register_names[regnum - SPARC64_NUM_REGS];
265
266   return NULL;
267 }
268
269 /* Return the GDB type object for the "standard" data type of data in
270    register REGNUM. */
271
272 static struct type *
273 sparc64_register_type (struct gdbarch *gdbarch, int regnum)
274 {
275   /* Raw registers.  */
276
277   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
278     return builtin_type (gdbarch)->builtin_data_ptr;
279   if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
280     return builtin_type (gdbarch)->builtin_int64;
281   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
282     return builtin_type (gdbarch)->builtin_float;
283   if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
284     return builtin_type (gdbarch)->builtin_double;
285   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
286     return builtin_type (gdbarch)->builtin_func_ptr;
287   /* This raw register contains the contents of %cwp, %pstate, %asi
288      and %ccr as laid out in a %tstate register.  */
289   if (regnum == SPARC64_STATE_REGNUM)
290     return builtin_type (gdbarch)->builtin_int64;
291   if (regnum == SPARC64_FSR_REGNUM)
292     return sparc64_fsr_type (gdbarch);
293   if (regnum == SPARC64_FPRS_REGNUM)
294     return sparc64_fprs_type (gdbarch);
295   /* "Although Y is a 64-bit register, its high-order 32 bits are
296      reserved and always read as 0."  */
297   if (regnum == SPARC64_Y_REGNUM)
298     return builtin_type (gdbarch)->builtin_int64;
299
300   /* Pseudo registers.  */
301
302   if (regnum == SPARC64_CWP_REGNUM)
303     return builtin_type (gdbarch)->builtin_int64;
304   if (regnum == SPARC64_PSTATE_REGNUM)
305     return sparc64_pstate_type (gdbarch);
306   if (regnum == SPARC64_ASI_REGNUM)
307     return builtin_type (gdbarch)->builtin_int64;
308   if (regnum == SPARC64_CCR_REGNUM)
309     return builtin_type (gdbarch)->builtin_int64;
310   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
311     return builtin_type (gdbarch)->builtin_double;
312   if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
313     return builtin_type (gdbarch)->builtin_long_double;
314
315   internal_error (__FILE__, __LINE__, _("invalid regnum"));
316 }
317
318 static void
319 sparc64_pseudo_register_read (struct gdbarch *gdbarch,
320                               struct regcache *regcache,
321                               int regnum, gdb_byte *buf)
322 {
323   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
324   gdb_assert (regnum >= SPARC64_NUM_REGS);
325
326   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
327     {
328       regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
329       regcache_raw_read (regcache, regnum, buf);
330       regcache_raw_read (regcache, regnum + 1, buf + 4);
331     }
332   else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
333     {
334       regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
335       regcache_raw_read (regcache, regnum, buf);
336     }
337   else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
338     {
339       regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
340       regcache_raw_read (regcache, regnum, buf);
341       regcache_raw_read (regcache, regnum + 1, buf + 4);
342       regcache_raw_read (regcache, regnum + 2, buf + 8);
343       regcache_raw_read (regcache, regnum + 3, buf + 12);
344     }
345   else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
346     {
347       regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
348       regcache_raw_read (regcache, regnum, buf);
349       regcache_raw_read (regcache, regnum + 1, buf + 8);
350     }
351   else if (regnum == SPARC64_CWP_REGNUM
352            || regnum == SPARC64_PSTATE_REGNUM
353            || regnum == SPARC64_ASI_REGNUM
354            || regnum == SPARC64_CCR_REGNUM)
355     {
356       ULONGEST state;
357
358       regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
359       switch (regnum)
360         {
361         case SPARC64_CWP_REGNUM:
362           state = (state >> 0) & ((1 << 5) - 1);
363           break;
364         case SPARC64_PSTATE_REGNUM:
365           state = (state >> 8) & ((1 << 12) - 1);
366           break;
367         case SPARC64_ASI_REGNUM:
368           state = (state >> 24) & ((1 << 8) - 1);
369           break;
370         case SPARC64_CCR_REGNUM:
371           state = (state >> 32) & ((1 << 8) - 1);
372           break;
373         }
374       store_unsigned_integer (buf, 8, byte_order, state);
375     }
376 }
377
378 static void
379 sparc64_pseudo_register_write (struct gdbarch *gdbarch,
380                                struct regcache *regcache,
381                                int regnum, const gdb_byte *buf)
382 {
383   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
384   gdb_assert (regnum >= SPARC64_NUM_REGS);
385
386   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
387     {
388       regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
389       regcache_raw_write (regcache, regnum, buf);
390       regcache_raw_write (regcache, regnum + 1, buf + 4);
391     }
392   else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
393     {
394       regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
395       regcache_raw_write (regcache, regnum, buf);
396     }
397   else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
398     {
399       regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
400       regcache_raw_write (regcache, regnum, buf);
401       regcache_raw_write (regcache, regnum + 1, buf + 4);
402       regcache_raw_write (regcache, regnum + 2, buf + 8);
403       regcache_raw_write (regcache, regnum + 3, buf + 12);
404     }
405   else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
406     {
407       regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
408       regcache_raw_write (regcache, regnum, buf);
409       regcache_raw_write (regcache, regnum + 1, buf + 8);
410     }
411   else if (regnum == SPARC64_CWP_REGNUM
412            || regnum == SPARC64_PSTATE_REGNUM
413            || regnum == SPARC64_ASI_REGNUM
414            || regnum == SPARC64_CCR_REGNUM)
415     {
416       ULONGEST state, bits;
417
418       regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
419       bits = extract_unsigned_integer (buf, 8, byte_order);
420       switch (regnum)
421         {
422         case SPARC64_CWP_REGNUM:
423           state |= ((bits & ((1 << 5) - 1)) << 0);
424           break;
425         case SPARC64_PSTATE_REGNUM:
426           state |= ((bits & ((1 << 12) - 1)) << 8);
427           break;
428         case SPARC64_ASI_REGNUM:
429           state |= ((bits & ((1 << 8) - 1)) << 24);
430           break;
431         case SPARC64_CCR_REGNUM:
432           state |= ((bits & ((1 << 8) - 1)) << 32);
433           break;
434         }
435       regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
436     }
437 }
438 \f
439
440 /* Return PC of first real instruction of the function starting at
441    START_PC.  */
442
443 static CORE_ADDR
444 sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
445 {
446   struct symtab_and_line sal;
447   CORE_ADDR func_start, func_end;
448   struct sparc_frame_cache cache;
449
450   /* This is the preferred method, find the end of the prologue by
451      using the debugging information.  */
452   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
453     {
454       sal = find_pc_line (func_start, 0);
455
456       if (sal.end < func_end
457           && start_pc <= sal.end)
458         return sal.end;
459     }
460
461   return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
462                                  &cache);
463 }
464
465 /* Normal frames.  */
466
467 static struct sparc_frame_cache *
468 sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
469 {
470   return sparc_frame_cache (this_frame, this_cache);
471 }
472
473 static void
474 sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
475                        struct frame_id *this_id)
476 {
477   struct sparc_frame_cache *cache =
478     sparc64_frame_cache (this_frame, this_cache);
479
480   /* This marks the outermost frame.  */
481   if (cache->base == 0)
482     return;
483
484   (*this_id) = frame_id_build (cache->base, cache->pc);
485 }
486
487 static struct value *
488 sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
489                              int regnum)
490 {
491   struct gdbarch *gdbarch = get_frame_arch (this_frame);
492   struct sparc_frame_cache *cache =
493     sparc64_frame_cache (this_frame, this_cache);
494
495   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
496     {
497       CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;
498
499       regnum = cache->frameless_p ? SPARC_O7_REGNUM : SPARC_I7_REGNUM;
500       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
501       return frame_unwind_got_constant (this_frame, regnum, pc);
502     }
503
504   /* Handle StackGhost.  */
505   {
506     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
507
508     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
509       {
510         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
511         ULONGEST i7;
512
513         /* Read the value in from memory.  */
514         i7 = get_frame_memory_unsigned (this_frame, addr, 8);
515         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
516       }
517   }
518
519   /* The previous frame's `local' and `in' registers have been saved
520      in the register save area.  */
521   if (!cache->frameless_p
522       && regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM)
523     {
524       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
525
526       return frame_unwind_got_memory (this_frame, regnum, addr);
527     }
528
529   /* The previous frame's `out' registers are accessable as the
530      current frame's `in' registers.  */
531   if (!cache->frameless_p
532       && regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM)
533     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
534
535   return frame_unwind_got_register (this_frame, regnum, regnum);
536 }
537
538 static const struct frame_unwind sparc64_frame_unwind =
539 {
540   NORMAL_FRAME,
541   sparc64_frame_this_id,
542   sparc64_frame_prev_register,
543   NULL,
544   default_frame_sniffer
545 };
546 \f
547
548 static CORE_ADDR
549 sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
550 {
551   struct sparc_frame_cache *cache =
552     sparc64_frame_cache (this_frame, this_cache);
553
554   return cache->base;
555 }
556
557 static const struct frame_base sparc64_frame_base =
558 {
559   &sparc64_frame_unwind,
560   sparc64_frame_base_address,
561   sparc64_frame_base_address,
562   sparc64_frame_base_address
563 };
564 \f
565 /* Check whether TYPE must be 16-byte aligned.  */
566
567 static int
568 sparc64_16_byte_align_p (struct type *type)
569 {
570   if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
571     return 1;
572
573   if (sparc64_structure_or_union_p (type))
574     {
575       int i;
576
577       for (i = 0; i < TYPE_NFIELDS (type); i++)
578         {
579           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
580
581           if (sparc64_16_byte_align_p (subtype))
582             return 1;
583         }
584     }
585
586   return 0;
587 }
588
589 /* Store floating fields of element ELEMENT of an "parameter array"
590    that has type TYPE and is stored at BITPOS in VALBUF in the
591    apropriate registers of REGCACHE.  This function can be called
592    recursively and therefore handles floating types in addition to
593    structures.  */
594
595 static void
596 sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
597                                const gdb_byte *valbuf, int element, int bitpos)
598 {
599   gdb_assert (element < 16);
600
601   if (sparc64_floating_p (type))
602     {
603       int len = TYPE_LENGTH (type);
604       int regnum;
605
606       if (len == 16)
607         {
608           gdb_assert (bitpos == 0);
609           gdb_assert ((element % 2) == 0);
610
611           regnum = SPARC64_Q0_REGNUM + element / 2;
612           regcache_cooked_write (regcache, regnum, valbuf);
613         }
614       else if (len == 8)
615         {
616           gdb_assert (bitpos == 0 || bitpos == 64);
617
618           regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
619           regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
620         }
621       else
622         {
623           gdb_assert (len == 4);
624           gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);
625
626           regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
627           regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
628         }
629     }
630   else if (sparc64_structure_or_union_p (type))
631     {
632       int i;
633
634       for (i = 0; i < TYPE_NFIELDS (type); i++)
635         {
636           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
637           int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
638
639           sparc64_store_floating_fields (regcache, subtype, valbuf,
640                                          element, subpos);
641         }
642
643       /* GCC has an interesting bug.  If TYPE is a structure that has
644          a single `float' member, GCC doesn't treat it as a structure
645          at all, but rather as an ordinary `float' argument.  This
646          argument will be stored in %f1, as required by the psABI.
647          However, as a member of a structure the psABI requires it to
648          be stored in %f0.  This bug is present in GCC 3.3.2, but
649          probably in older releases to.  To appease GCC, if a
650          structure has only a single `float' member, we store its
651          value in %f1 too (we already have stored in %f0).  */
652       if (TYPE_NFIELDS (type) == 1)
653         {
654           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));
655
656           if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
657             regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
658         }
659     }
660 }
661
662 /* Fetch floating fields from a variable of type TYPE from the
663    appropriate registers for BITPOS in REGCACHE and store it at BITPOS
664    in VALBUF.  This function can be called recursively and therefore
665    handles floating types in addition to structures.  */
666
667 static void
668 sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
669                                  gdb_byte *valbuf, int bitpos)
670 {
671   if (sparc64_floating_p (type))
672     {
673       int len = TYPE_LENGTH (type);
674       int regnum;
675
676       if (len == 16)
677         {
678           gdb_assert (bitpos == 0 || bitpos == 128);
679
680           regnum = SPARC64_Q0_REGNUM + bitpos / 128;
681           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
682         }
683       else if (len == 8)
684         {
685           gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);
686
687           regnum = SPARC64_D0_REGNUM + bitpos / 64;
688           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
689         }
690       else
691         {
692           gdb_assert (len == 4);
693           gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);
694
695           regnum = SPARC_F0_REGNUM + bitpos / 32;
696           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
697         }
698     }
699   else if (sparc64_structure_or_union_p (type))
700     {
701       int i;
702
703       for (i = 0; i < TYPE_NFIELDS (type); i++)
704         {
705           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
706           int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);
707
708           sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
709         }
710     }
711 }
712
713 /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
714    non-zero) in REGCACHE and on the stack (starting from address SP).  */
715
716 static CORE_ADDR
717 sparc64_store_arguments (struct regcache *regcache, int nargs,
718                          struct value **args, CORE_ADDR sp,
719                          int struct_return, CORE_ADDR struct_addr)
720 {
721   struct gdbarch *gdbarch = get_regcache_arch (regcache);
722   /* Number of extended words in the "parameter array".  */
723   int num_elements = 0;
724   int element = 0;
725   int i;
726
727   /* Take BIAS into account.  */
728   sp += BIAS;
729
730   /* First we calculate the number of extended words in the "parameter
731      array".  While doing so we also convert some of the arguments.  */
732
733   if (struct_return)
734     num_elements++;
735
736   for (i = 0; i < nargs; i++)
737     {
738       struct type *type = value_type (args[i]);
739       int len = TYPE_LENGTH (type);
740
741       if (sparc64_structure_or_union_p (type))
742         {
743           /* Structure or Union arguments.  */
744           if (len <= 16)
745             {
746               if (num_elements % 2 && sparc64_16_byte_align_p (type))
747                 num_elements++;
748               num_elements += ((len + 7) / 8);
749             }
750           else
751             {
752               /* The psABI says that "Structures or unions larger than
753                  sixteen bytes are copied by the caller and passed
754                  indirectly; the caller will pass the address of a
755                  correctly aligned structure value.  This sixty-four
756                  bit address will occupy one word in the parameter
757                  array, and may be promoted to an %o register like any
758                  other pointer value."  Allocate memory for these
759                  values on the stack.  */
760               sp -= len;
761
762               /* Use 16-byte alignment for these values.  That's
763                  always correct, and wasting a few bytes shouldn't be
764                  a problem.  */
765               sp &= ~0xf;
766
767               write_memory (sp, value_contents (args[i]), len);
768               args[i] = value_from_pointer (lookup_pointer_type (type), sp);
769               num_elements++;
770             }
771         }
772       else if (sparc64_floating_p (type))
773         {
774           /* Floating arguments.  */
775
776           if (len == 16)
777             {
778               /* The psABI says that "Each quad-precision parameter
779                  value will be assigned to two extended words in the
780                  parameter array.  */
781               num_elements += 2;
782
783               /* The psABI says that "Long doubles must be
784                  quad-aligned, and thus a hole might be introduced
785                  into the parameter array to force alignment."  Skip
786                  an element if necessary.  */
787               if (num_elements % 2)
788                 num_elements++;
789             }
790           else
791             num_elements++;
792         }
793       else
794         {
795           /* Integral and pointer arguments.  */
796           gdb_assert (sparc64_integral_or_pointer_p (type));
797
798           /* The psABI says that "Each argument value of integral type
799              smaller than an extended word will be widened by the
800              caller to an extended word according to the signed-ness
801              of the argument type."  */
802           if (len < 8)
803             args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
804                                   args[i]);
805           num_elements++;
806         }
807     }
808
809   /* Allocate the "parameter array".  */
810   sp -= num_elements * 8;
811
812   /* The psABI says that "Every stack frame must be 16-byte aligned."  */
813   sp &= ~0xf;
814
815   /* Now we store the arguments in to the "paramater array".  Some
816      Integer or Pointer arguments and Structure or Union arguments
817      will be passed in %o registers.  Some Floating arguments and
818      floating members of structures are passed in floating-point
819      registers.  However, for functions with variable arguments,
820      floating arguments are stored in an %0 register, and for
821      functions without a prototype floating arguments are stored in
822      both a floating-point and an %o registers, or a floating-point
823      register and memory.  To simplify the logic here we always pass
824      arguments in memory, an %o register, and a floating-point
825      register if appropriate.  This should be no problem since the
826      contents of any unused memory or registers in the "parameter
827      array" are undefined.  */
828
829   if (struct_return)
830     {
831       regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
832       element++;
833     }
834
835   for (i = 0; i < nargs; i++)
836     {
837       const gdb_byte *valbuf = value_contents (args[i]);
838       struct type *type = value_type (args[i]);
839       int len = TYPE_LENGTH (type);
840       int regnum = -1;
841       gdb_byte buf[16];
842
843       if (sparc64_structure_or_union_p (type))
844         {
845           /* Structure or Union arguments.  */
846           gdb_assert (len <= 16);
847           memset (buf, 0, sizeof (buf));
848           valbuf = memcpy (buf, valbuf, len);
849
850           if (element % 2 && sparc64_16_byte_align_p (type))
851             element++;
852
853           if (element < 6)
854             {
855               regnum = SPARC_O0_REGNUM + element;
856               if (len > 8 && element < 5)
857                 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
858             }
859
860           if (element < 16)
861             sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
862         }
863       else if (sparc64_floating_p (type))
864         {
865           /* Floating arguments.  */
866           if (len == 16)
867             {
868               if (element % 2)
869                 element++;
870               if (element < 16)
871                 regnum = SPARC64_Q0_REGNUM + element / 2;
872             }
873           else if (len == 8)
874             {
875               if (element < 16)
876                 regnum = SPARC64_D0_REGNUM + element;
877             }
878           else
879             {
880               /* The psABI says "Each single-precision parameter value
881                  will be assigned to one extended word in the
882                  parameter array, and right-justified within that
883                  word; the left half (even floatregister) is
884                  undefined."  Even though the psABI says that "the
885                  left half is undefined", set it to zero here.  */
886               memset (buf, 0, 4);
887               memcpy (buf + 4, valbuf, 4);
888               valbuf = buf;
889               len = 8;
890               if (element < 16)
891                 regnum = SPARC64_D0_REGNUM + element;
892             }
893         }
894       else
895         {
896           /* Integral and pointer arguments.  */
897           gdb_assert (len == 8);
898           if (element < 6)
899             regnum = SPARC_O0_REGNUM + element;
900         }
901
902       if (regnum != -1)
903         {
904           regcache_cooked_write (regcache, regnum, valbuf);
905
906           /* If we're storing the value in a floating-point register,
907              also store it in the corresponding %0 register(s).  */
908           if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
909             {
910               gdb_assert (element < 6);
911               regnum = SPARC_O0_REGNUM + element;
912               regcache_cooked_write (regcache, regnum, valbuf);
913             }
914           else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
915             {
916               gdb_assert (element < 6);
917               regnum = SPARC_O0_REGNUM + element;
918               regcache_cooked_write (regcache, regnum, valbuf);
919               regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
920             }
921         }
922
923       /* Always store the argument in memory.  */
924       write_memory (sp + element * 8, valbuf, len);
925       element += ((len + 7) / 8);
926     }
927
928   gdb_assert (element == num_elements);
929
930   /* Take BIAS into account.  */
931   sp -= BIAS;
932   return sp;
933 }
934
935 static CORE_ADDR
936 sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
937                          struct regcache *regcache, CORE_ADDR bp_addr,
938                          int nargs, struct value **args, CORE_ADDR sp,
939                          int struct_return, CORE_ADDR struct_addr)
940 {
941   /* Set return address.  */
942   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);
943
944   /* Set up function arguments.  */
945   sp = sparc64_store_arguments (regcache, nargs, args, sp,
946                                 struct_return, struct_addr);
947
948   /* Allocate the register save area.  */
949   sp -= 16 * 8;
950
951   /* Stack should be 16-byte aligned at this point.  */
952   gdb_assert ((sp + BIAS) % 16 == 0);
953
954   /* Finally, update the stack pointer.  */
955   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
956
957   return sp + BIAS;
958 }
959 \f
960
961 /* Extract from an array REGBUF containing the (raw) register state, a
962    function return value of TYPE, and copy that into VALBUF.  */
963
964 static void
965 sparc64_extract_return_value (struct type *type, struct regcache *regcache,
966                               gdb_byte *valbuf)
967 {
968   int len = TYPE_LENGTH (type);
969   gdb_byte buf[32];
970   int i;
971
972   if (sparc64_structure_or_union_p (type))
973     {
974       /* Structure or Union return values.  */
975       gdb_assert (len <= 32);
976
977       for (i = 0; i < ((len + 7) / 8); i++)
978         regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
979       if (TYPE_CODE (type) != TYPE_CODE_UNION)
980         sparc64_extract_floating_fields (regcache, type, buf, 0);
981       memcpy (valbuf, buf, len);
982     }
983   else if (sparc64_floating_p (type))
984     {
985       /* Floating return values.  */
986       for (i = 0; i < len / 4; i++)
987         regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
988       memcpy (valbuf, buf, len);
989     }
990   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
991     {
992       /* Small arrays are returned the same way as small structures.  */
993       gdb_assert (len <= 32);
994
995       for (i = 0; i < ((len + 7) / 8); i++)
996         regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
997       memcpy (valbuf, buf, len);
998     }
999   else
1000     {
1001       /* Integral and pointer return values.  */
1002       gdb_assert (sparc64_integral_or_pointer_p (type));
1003
1004       /* Just stripping off any unused bytes should preserve the
1005          signed-ness just fine.  */
1006       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
1007       memcpy (valbuf, buf + 8 - len, len);
1008     }
1009 }
1010
1011 /* Write into the appropriate registers a function return value stored
1012    in VALBUF of type TYPE.  */
1013
1014 static void
1015 sparc64_store_return_value (struct type *type, struct regcache *regcache,
1016                             const gdb_byte *valbuf)
1017 {
1018   int len = TYPE_LENGTH (type);
1019   gdb_byte buf[16];
1020   int i;
1021
1022   if (sparc64_structure_or_union_p (type))
1023     {
1024       /* Structure or Union return values.  */
1025       gdb_assert (len <= 32);
1026
1027       /* Simplify matters by storing the complete value (including
1028          floating members) into %o0 and %o1.  Floating members are
1029          also store in the appropriate floating-point registers.  */
1030       memset (buf, 0, sizeof (buf));
1031       memcpy (buf, valbuf, len);
1032       for (i = 0; i < ((len + 7) / 8); i++)
1033         regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1034       if (TYPE_CODE (type) != TYPE_CODE_UNION)
1035         sparc64_store_floating_fields (regcache, type, buf, 0, 0);
1036     }
1037   else if (sparc64_floating_p (type))
1038     {
1039       /* Floating return values.  */
1040       memcpy (buf, valbuf, len);
1041       for (i = 0; i < len / 4; i++)
1042         regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
1043     }
1044   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1045     {
1046       /* Small arrays are returned the same way as small structures.  */
1047       gdb_assert (len <= 32);
1048
1049       memset (buf, 0, sizeof (buf));
1050       memcpy (buf, valbuf, len);
1051       for (i = 0; i < ((len + 7) / 8); i++)
1052         regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
1053     }
1054   else
1055     {
1056       /* Integral and pointer return values.  */
1057       gdb_assert (sparc64_integral_or_pointer_p (type));
1058
1059       /* ??? Do we need to do any sign-extension here?  */
1060       memset (buf, 0, 8);
1061       memcpy (buf + 8 - len, valbuf, len);
1062       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
1063     }
1064 }
1065
1066 static enum return_value_convention
1067 sparc64_return_value (struct gdbarch *gdbarch, struct type *func_type,
1068                       struct type *type, struct regcache *regcache,
1069                       gdb_byte *readbuf, const gdb_byte *writebuf)
1070 {
1071   if (TYPE_LENGTH (type) > 32)
1072     return RETURN_VALUE_STRUCT_CONVENTION;
1073
1074   if (readbuf)
1075     sparc64_extract_return_value (type, regcache, readbuf);
1076   if (writebuf)
1077     sparc64_store_return_value (type, regcache, writebuf);
1078
1079   return RETURN_VALUE_REGISTER_CONVENTION;
1080 }
1081 \f
1082
1083 static void
1084 sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1085                                struct dwarf2_frame_state_reg *reg,
1086                                struct frame_info *this_frame)
1087 {
1088   switch (regnum)
1089     {
1090     case SPARC_G0_REGNUM:
1091       /* Since %g0 is always zero, there is no point in saving it, and
1092          people will be inclined omit it from the CFI.  Make sure we
1093          don't warn about that.  */
1094       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1095       break;
1096     case SPARC_SP_REGNUM:
1097       reg->how = DWARF2_FRAME_REG_CFA;
1098       break;
1099     case SPARC64_PC_REGNUM:
1100       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1101       reg->loc.offset = 8;
1102       break;
1103     case SPARC64_NPC_REGNUM:
1104       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1105       reg->loc.offset = 12;
1106       break;
1107     }
1108 }
1109
1110 void
1111 sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1112 {
1113   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1114
1115   tdep->pc_regnum = SPARC64_PC_REGNUM;
1116   tdep->npc_regnum = SPARC64_NPC_REGNUM;
1117
1118   /* This is what all the fuss is about.  */
1119   set_gdbarch_long_bit (gdbarch, 64);
1120   set_gdbarch_long_long_bit (gdbarch, 64);
1121   set_gdbarch_ptr_bit (gdbarch, 64);
1122
1123   set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
1124   set_gdbarch_register_name (gdbarch, sparc64_register_name);
1125   set_gdbarch_register_type (gdbarch, sparc64_register_type);
1126   set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
1127   set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
1128   set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);
1129
1130   /* Register numbers of various important registers.  */
1131   set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */
1132
1133   /* Call dummy code.  */
1134   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1135   set_gdbarch_push_dummy_code (gdbarch, NULL);
1136   set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);
1137
1138   set_gdbarch_return_value (gdbarch, sparc64_return_value);
1139   set_gdbarch_stabs_argument_has_addr
1140     (gdbarch, default_stabs_argument_has_addr);
1141
1142   set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
1143
1144   /* Hook in the DWARF CFI frame unwinder.  */
1145   dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
1146   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1147      StackGhost issues have been resolved.  */
1148
1149   frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
1150   frame_base_set_default (gdbarch, &sparc64_frame_base);
1151 }
1152 \f
1153
1154 /* Helper functions for dealing with register sets.  */
1155
1156 #define TSTATE_CWP      0x000000000000001fULL
1157 #define TSTATE_ICC      0x0000000f00000000ULL
1158 #define TSTATE_XCC      0x000000f000000000ULL
1159
1160 #define PSR_S           0x00000080
1161 #define PSR_ICC         0x00f00000
1162 #define PSR_VERS        0x0f000000
1163 #define PSR_IMPL        0xf0000000
1164 #define PSR_V8PLUS      0xff000000
1165 #define PSR_XCC         0x000f0000
1166
1167 void
1168 sparc64_supply_gregset (const struct sparc_gregset *gregset,
1169                         struct regcache *regcache,
1170                         int regnum, const void *gregs)
1171 {
1172   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1173   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1174   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1175   const gdb_byte *regs = gregs;
1176   int i;
1177
1178   if (sparc32)
1179     {
1180       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1181         {
1182           int offset = gregset->r_tstate_offset;
1183           ULONGEST tstate, psr;
1184           gdb_byte buf[4];
1185
1186           tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1187           psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
1188                  | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
1189           store_unsigned_integer (buf, 4, byte_order, psr);
1190           regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
1191         }
1192
1193       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1194         regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
1195                              regs + gregset->r_pc_offset + 4);
1196
1197       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1198         regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
1199                              regs + gregset->r_npc_offset + 4);
1200
1201       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1202         {
1203           int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1204           regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
1205         }
1206     }
1207   else
1208     {
1209       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1210         regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
1211                              regs + gregset->r_tstate_offset);
1212
1213       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1214         regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
1215                              regs + gregset->r_pc_offset);
1216
1217       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1218         regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
1219                              regs + gregset->r_npc_offset);
1220
1221       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1222         {
1223           gdb_byte buf[8];
1224
1225           memset (buf, 0, 8);
1226           memcpy (buf + 8 - gregset->r_y_size,
1227                   regs + gregset->r_y_offset, gregset->r_y_size);
1228           regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
1229         }
1230
1231       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1232           && gregset->r_fprs_offset != -1)
1233         regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
1234                              regs + gregset->r_fprs_offset);
1235     }
1236
1237   if (regnum == SPARC_G0_REGNUM || regnum == -1)
1238     regcache_raw_supply (regcache, SPARC_G0_REGNUM, NULL);
1239
1240   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1241     {
1242       int offset = gregset->r_g1_offset;
1243
1244       if (sparc32)
1245         offset += 4;
1246
1247       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1248         {
1249           if (regnum == i || regnum == -1)
1250             regcache_raw_supply (regcache, i, regs + offset);
1251           offset += 8;
1252         }
1253     }
1254
1255   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1256     {
1257       /* Not all of the register set variants include Locals and
1258          Inputs.  For those that don't, we read them off the stack.  */
1259       if (gregset->r_l0_offset == -1)
1260         {
1261           ULONGEST sp;
1262
1263           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1264           sparc_supply_rwindow (regcache, sp, regnum);
1265         }
1266       else
1267         {
1268           int offset = gregset->r_l0_offset;
1269
1270           if (sparc32)
1271             offset += 4;
1272
1273           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1274             {
1275               if (regnum == i || regnum == -1)
1276                 regcache_raw_supply (regcache, i, regs + offset);
1277               offset += 8;
1278             }
1279         }
1280     }
1281 }
1282
1283 void
1284 sparc64_collect_gregset (const struct sparc_gregset *gregset,
1285                          const struct regcache *regcache,
1286                          int regnum, void *gregs)
1287 {
1288   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1289   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1290   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
1291   gdb_byte *regs = gregs;
1292   int i;
1293
1294   if (sparc32)
1295     {
1296       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
1297         {
1298           int offset = gregset->r_tstate_offset;
1299           ULONGEST tstate, psr;
1300           gdb_byte buf[8];
1301
1302           tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
1303           regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
1304           psr = extract_unsigned_integer (buf, 4, byte_order);
1305           tstate |= (psr & PSR_ICC) << 12;
1306           if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
1307             tstate |= (psr & PSR_XCC) << 20;
1308           store_unsigned_integer (buf, 8, byte_order, tstate);
1309           memcpy (regs + offset, buf, 8);
1310         }
1311
1312       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
1313         regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
1314                               regs + gregset->r_pc_offset + 4);
1315
1316       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
1317         regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
1318                               regs + gregset->r_npc_offset + 4);
1319
1320       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
1321         {
1322           int offset = gregset->r_y_offset + 8 - gregset->r_y_size;
1323           regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
1324         }
1325     }
1326   else
1327     {
1328       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
1329         regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
1330                               regs + gregset->r_tstate_offset);
1331
1332       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
1333         regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
1334                               regs + gregset->r_pc_offset);
1335
1336       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
1337         regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
1338                               regs + gregset->r_npc_offset);
1339
1340       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
1341         {
1342           gdb_byte buf[8];
1343
1344           regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
1345           memcpy (regs + gregset->r_y_offset,
1346                   buf + 8 - gregset->r_y_size, gregset->r_y_size);
1347         }
1348
1349       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
1350           && gregset->r_fprs_offset != -1)
1351         regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
1352                               regs + gregset->r_fprs_offset);
1353
1354     }
1355
1356   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
1357     {
1358       int offset = gregset->r_g1_offset;
1359
1360       if (sparc32)
1361         offset += 4;
1362
1363       /* %g0 is always zero.  */
1364       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
1365         {
1366           if (regnum == i || regnum == -1)
1367             regcache_raw_collect (regcache, i, regs + offset);
1368           offset += 8;
1369         }
1370     }
1371
1372   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
1373     {
1374       /* Not all of the register set variants include Locals and
1375          Inputs.  For those that don't, we read them off the stack.  */
1376       if (gregset->r_l0_offset != -1)
1377         {
1378           int offset = gregset->r_l0_offset;
1379
1380           if (sparc32)
1381             offset += 4;
1382
1383           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1384             {
1385               if (regnum == i || regnum == -1)
1386                 regcache_raw_collect (regcache, i, regs + offset);
1387               offset += 8;
1388             }
1389         }
1390     }
1391 }
1392
1393 void
1394 sparc64_supply_fpregset (struct regcache *regcache,
1395                          int regnum, const void *fpregs)
1396 {
1397   int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1398   const gdb_byte *regs = fpregs;
1399   int i;
1400
1401   for (i = 0; i < 32; i++)
1402     {
1403       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1404         regcache_raw_supply (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1405     }
1406
1407   if (sparc32)
1408     {
1409       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1410         regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
1411                              regs + (32 * 4) + (16 * 8) + 4);
1412     }
1413   else
1414     {
1415       for (i = 0; i < 16; i++)
1416         {
1417           if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1418             regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
1419                                  regs + (32 * 4) + (i * 8));
1420         }
1421
1422       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1423         regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
1424                              regs + (32 * 4) + (16 * 8));
1425     }
1426 }
1427
1428 void
1429 sparc64_collect_fpregset (const struct regcache *regcache,
1430                           int regnum, void *fpregs)
1431 {
1432   int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
1433   gdb_byte *regs = fpregs;
1434   int i;
1435
1436   for (i = 0; i < 32; i++)
1437     {
1438       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
1439         regcache_raw_collect (regcache, SPARC_F0_REGNUM + i, regs + (i * 4));
1440     }
1441
1442   if (sparc32)
1443     {
1444       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
1445         regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
1446                               regs + (32 * 4) + (16 * 8) + 4);
1447     }
1448   else
1449     {
1450       for (i = 0; i < 16; i++)
1451         {
1452           if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
1453             regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
1454                                   regs + (32 * 4) + (i * 8));
1455         }
1456
1457       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
1458         regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
1459                               regs + (32 * 4) + (16 * 8));
1460     }
1461 }
1462