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