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