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