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