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