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