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