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