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