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