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