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