gdb/sparc64-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for UltraSPARC.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dwarf2-frame.h"
  17. #include "floatformat.h"
  18. #include "frame.h"
  19. #include "frame-base.h"
  20. #include "frame-unwind.h"
  21. #include "gdbcore.h"
  22. #include "gdbtypes.h"
  23. #include "inferior.h"
  24. #include "symtab.h"
  25. #include "objfiles.h"
  26. #include "osabi.h"
  27. #include "regcache.h"
  28. #include "target.h"
  29. #include "value.h"

  30. #include "sparc64-tdep.h"

  31. /* This file implements the SPARC 64-bit ABI as defined by the
  32.    section "Low-Level System Information" of the SPARC Compliance
  33.    Definition (SCD) 2.4.1, which is the 64-bit System V psABI for
  34.    SPARC.  */

  35. /* Please use the sparc32_-prefix for 32-bit specific code, the
  36.    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
  37.    code can handle both.  */

  38. /* The functions on this page are intended to be used to classify
  39.    function arguments.  */

  40. /* Check whether TYPE is "Integral or Pointer".  */

  41. static int
  42. sparc64_integral_or_pointer_p (const struct type *type)
  43. {
  44.   switch (TYPE_CODE (type))
  45.     {
  46.     case TYPE_CODE_INT:
  47.     case TYPE_CODE_BOOL:
  48.     case TYPE_CODE_CHAR:
  49.     case TYPE_CODE_ENUM:
  50.     case TYPE_CODE_RANGE:
  51.       {
  52.         int len = TYPE_LENGTH (type);
  53.         gdb_assert (len == 1 || len == 2 || len == 4 || len == 8);
  54.       }
  55.       return 1;
  56.     case TYPE_CODE_PTR:
  57.     case TYPE_CODE_REF:
  58.       {
  59.         int len = TYPE_LENGTH (type);
  60.         gdb_assert (len == 8);
  61.       }
  62.       return 1;
  63.     default:
  64.       break;
  65.     }

  66.   return 0;
  67. }

  68. /* Check whether TYPE is "Floating".  */

  69. static int
  70. sparc64_floating_p (const struct type *type)
  71. {
  72.   switch (TYPE_CODE (type))
  73.     {
  74.     case TYPE_CODE_FLT:
  75.       {
  76.         int len = TYPE_LENGTH (type);
  77.         gdb_assert (len == 4 || len == 8 || len == 16);
  78.       }
  79.       return 1;
  80.     default:
  81.       break;
  82.     }

  83.   return 0;
  84. }

  85. /* Check whether TYPE is "Complex Floating".  */

  86. static int
  87. sparc64_complex_floating_p (const struct type *type)
  88. {
  89.   switch (TYPE_CODE (type))
  90.     {
  91.     case TYPE_CODE_COMPLEX:
  92.       {
  93.         int len = TYPE_LENGTH (type);
  94.         gdb_assert (len == 8 || len == 16 || len == 32);
  95.       }
  96.       return 1;
  97.     default:
  98.       break;
  99.     }

  100.   return 0;
  101. }

  102. /* Check whether TYPE is "Structure or Union".

  103.    In terms of Ada subprogram calls, arrays are treated the same as
  104.    struct and union types.  So this function also returns non-zero
  105.    for array types.  */

  106. static int
  107. sparc64_structure_or_union_p (const struct type *type)
  108. {
  109.   switch (TYPE_CODE (type))
  110.     {
  111.     case TYPE_CODE_STRUCT:
  112.     case TYPE_CODE_UNION:
  113.     case TYPE_CODE_ARRAY:
  114.       return 1;
  115.     default:
  116.       break;
  117.     }

  118.   return 0;
  119. }


  120. /* Construct types for ISA-specific registers.  */

  121. static struct type *
  122. sparc64_pstate_type (struct gdbarch *gdbarch)
  123. {
  124.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  125.   if (!tdep->sparc64_pstate_type)
  126.     {
  127.       struct type *type;

  128.       type = arch_flags_type (gdbarch, "builtin_type_sparc64_pstate", 8);
  129.       append_flags_type_flag (type, 0, "AG");
  130.       append_flags_type_flag (type, 1, "IE");
  131.       append_flags_type_flag (type, 2, "PRIV");
  132.       append_flags_type_flag (type, 3, "AM");
  133.       append_flags_type_flag (type, 4, "PEF");
  134.       append_flags_type_flag (type, 5, "RED");
  135.       append_flags_type_flag (type, 8, "TLE");
  136.       append_flags_type_flag (type, 9, "CLE");
  137.       append_flags_type_flag (type, 10, "PID0");
  138.       append_flags_type_flag (type, 11, "PID1");

  139.       tdep->sparc64_pstate_type = type;
  140.     }

  141.   return tdep->sparc64_pstate_type;
  142. }

  143. static struct type *
  144. sparc64_fsr_type (struct gdbarch *gdbarch)
  145. {
  146.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  147.   if (!tdep->sparc64_fsr_type)
  148.     {
  149.       struct type *type;

  150.       type = arch_flags_type (gdbarch, "builtin_type_sparc64_fsr", 8);
  151.       append_flags_type_flag (type, 0, "NXA");
  152.       append_flags_type_flag (type, 1, "DZA");
  153.       append_flags_type_flag (type, 2, "UFA");
  154.       append_flags_type_flag (type, 3, "OFA");
  155.       append_flags_type_flag (type, 4, "NVA");
  156.       append_flags_type_flag (type, 5, "NXC");
  157.       append_flags_type_flag (type, 6, "DZC");
  158.       append_flags_type_flag (type, 7, "UFC");
  159.       append_flags_type_flag (type, 8, "OFC");
  160.       append_flags_type_flag (type, 9, "NVC");
  161.       append_flags_type_flag (type, 22, "NS");
  162.       append_flags_type_flag (type, 23, "NXM");
  163.       append_flags_type_flag (type, 24, "DZM");
  164.       append_flags_type_flag (type, 25, "UFM");
  165.       append_flags_type_flag (type, 26, "OFM");
  166.       append_flags_type_flag (type, 27, "NVM");

  167.       tdep->sparc64_fsr_type = type;
  168.     }

  169.   return tdep->sparc64_fsr_type;
  170. }

  171. static struct type *
  172. sparc64_fprs_type (struct gdbarch *gdbarch)
  173. {
  174.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  175.   if (!tdep->sparc64_fprs_type)
  176.     {
  177.       struct type *type;

  178.       type = arch_flags_type (gdbarch, "builtin_type_sparc64_fprs", 8);
  179.       append_flags_type_flag (type, 0, "DL");
  180.       append_flags_type_flag (type, 1, "DU");
  181.       append_flags_type_flag (type, 2, "FEF");

  182.       tdep->sparc64_fprs_type = type;
  183.     }

  184.   return tdep->sparc64_fprs_type;
  185. }


  186. /* Register information.  */

  187. static const char *sparc64_register_names[] =
  188. {
  189.   "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
  190.   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
  191.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
  192.   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",

  193.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  194.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  195.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  196.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  197.   "f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",
  198.   "f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",

  199.   "pc", "npc",

  200.   /* FIXME: Give "state" a name until we start using register groups.  */
  201.   "state",
  202.   "fsr",
  203.   "fprs",
  204.   "y",
  205. };

  206. /* Total number of registers.  */
  207. #define SPARC64_NUM_REGS ARRAY_SIZE (sparc64_register_names)

  208. /* We provide the aliases %d0..%d62 and %q0..%q60 for the floating
  209.    registers as "psuedo" registers.  */

  210. static const char *sparc64_pseudo_register_names[] =
  211. {
  212.   "cwp", "pstate", "asi", "ccr",

  213.   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
  214.   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30",
  215.   "d32", "d34", "d36", "d38", "d40", "d42", "d44", "d46",
  216.   "d48", "d50", "d52", "d54", "d56", "d58", "d60", "d62",

  217.   "q0", "q4", "q8", "q12", "q16", "q20", "q24", "q28",
  218.   "q32", "q36", "q40", "q44", "q48", "q52", "q56", "q60",
  219. };

  220. /* Total number of pseudo registers.  */
  221. #define SPARC64_NUM_PSEUDO_REGS ARRAY_SIZE (sparc64_pseudo_register_names)

  222. /* Return the name of register REGNUM.  */

  223. static const char *
  224. sparc64_register_name (struct gdbarch *gdbarch, int regnum)
  225. {
  226.   if (regnum >= 0 && regnum < SPARC64_NUM_REGS)
  227.     return sparc64_register_names[regnum];

  228.   if (regnum >= SPARC64_NUM_REGS
  229.       && regnum < SPARC64_NUM_REGS + SPARC64_NUM_PSEUDO_REGS)
  230.     return sparc64_pseudo_register_names[regnum - SPARC64_NUM_REGS];

  231.   return NULL;
  232. }

  233. /* Return the GDB type object for the "standard" data type of data in
  234.    register REGNUM.  */

  235. static struct type *
  236. sparc64_register_type (struct gdbarch *gdbarch, int regnum)
  237. {
  238.   /* Raw registers.  */

  239.   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
  240.     return builtin_type (gdbarch)->builtin_data_ptr;
  241.   if (regnum >= SPARC_G0_REGNUM && regnum <= SPARC_I7_REGNUM)
  242.     return builtin_type (gdbarch)->builtin_int64;
  243.   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
  244.     return builtin_type (gdbarch)->builtin_float;
  245.   if (regnum >= SPARC64_F32_REGNUM && regnum <= SPARC64_F62_REGNUM)
  246.     return builtin_type (gdbarch)->builtin_double;
  247.   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  248.     return builtin_type (gdbarch)->builtin_func_ptr;
  249.   /* This raw register contains the contents of %cwp, %pstate, %asi
  250.      and %ccr as laid out in a %tstate register.  */
  251.   if (regnum == SPARC64_STATE_REGNUM)
  252.     return builtin_type (gdbarch)->builtin_int64;
  253.   if (regnum == SPARC64_FSR_REGNUM)
  254.     return sparc64_fsr_type (gdbarch);
  255.   if (regnum == SPARC64_FPRS_REGNUM)
  256.     return sparc64_fprs_type (gdbarch);
  257.   /* "Although Y is a 64-bit register, its high-order 32 bits are
  258.      reserved and always read as 0."  */
  259.   if (regnum == SPARC64_Y_REGNUM)
  260.     return builtin_type (gdbarch)->builtin_int64;

  261.   /* Pseudo registers.  */

  262.   if (regnum == SPARC64_CWP_REGNUM)
  263.     return builtin_type (gdbarch)->builtin_int64;
  264.   if (regnum == SPARC64_PSTATE_REGNUM)
  265.     return sparc64_pstate_type (gdbarch);
  266.   if (regnum == SPARC64_ASI_REGNUM)
  267.     return builtin_type (gdbarch)->builtin_int64;
  268.   if (regnum == SPARC64_CCR_REGNUM)
  269.     return builtin_type (gdbarch)->builtin_int64;
  270.   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D62_REGNUM)
  271.     return builtin_type (gdbarch)->builtin_double;
  272.   if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  273.     return builtin_type (gdbarch)->builtin_long_double;

  274.   internal_error (__FILE__, __LINE__, _("invalid regnum"));
  275. }

  276. static enum register_status
  277. sparc64_pseudo_register_read (struct gdbarch *gdbarch,
  278.                               struct regcache *regcache,
  279.                               int regnum, gdb_byte *buf)
  280. {
  281.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  282.   enum register_status status;

  283.   gdb_assert (regnum >= SPARC64_NUM_REGS);

  284.   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
  285.     {
  286.       regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
  287.       status = regcache_raw_read (regcache, regnum, buf);
  288.       if (status == REG_VALID)
  289.         status = regcache_raw_read (regcache, regnum + 1, buf + 4);
  290.       return status;
  291.     }
  292.   else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
  293.     {
  294.       regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
  295.       return regcache_raw_read (regcache, regnum, buf);
  296.     }
  297.   else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
  298.     {
  299.       regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);

  300.       status = regcache_raw_read (regcache, regnum, buf);
  301.       if (status == REG_VALID)
  302.         status = regcache_raw_read (regcache, regnum + 1, buf + 4);
  303.       if (status == REG_VALID)
  304.         status = regcache_raw_read (regcache, regnum + 2, buf + 8);
  305.       if (status == REG_VALID)
  306.         status = regcache_raw_read (regcache, regnum + 3, buf + 12);

  307.       return status;
  308.     }
  309.   else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  310.     {
  311.       regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);

  312.       status = regcache_raw_read (regcache, regnum, buf);
  313.       if (status == REG_VALID)
  314.         status = regcache_raw_read (regcache, regnum + 1, buf + 8);

  315.       return status;
  316.     }
  317.   else if (regnum == SPARC64_CWP_REGNUM
  318.            || regnum == SPARC64_PSTATE_REGNUM
  319.            || regnum == SPARC64_ASI_REGNUM
  320.            || regnum == SPARC64_CCR_REGNUM)
  321.     {
  322.       ULONGEST state;

  323.       status = regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
  324.       if (status != REG_VALID)
  325.         return status;

  326.       switch (regnum)
  327.         {
  328.         case SPARC64_CWP_REGNUM:
  329.           state = (state >> 0) & ((1 << 5) - 1);
  330.           break;
  331.         case SPARC64_PSTATE_REGNUM:
  332.           state = (state >> 8) & ((1 << 12) - 1);
  333.           break;
  334.         case SPARC64_ASI_REGNUM:
  335.           state = (state >> 24) & ((1 << 8) - 1);
  336.           break;
  337.         case SPARC64_CCR_REGNUM:
  338.           state = (state >> 32) & ((1 << 8) - 1);
  339.           break;
  340.         }
  341.       store_unsigned_integer (buf, 8, byte_order, state);
  342.     }

  343.   return REG_VALID;
  344. }

  345. static void
  346. sparc64_pseudo_register_write (struct gdbarch *gdbarch,
  347.                                struct regcache *regcache,
  348.                                int regnum, const gdb_byte *buf)
  349. {
  350.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  351.   gdb_assert (regnum >= SPARC64_NUM_REGS);

  352.   if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D30_REGNUM)
  353.     {
  354.       regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC64_D0_REGNUM);
  355.       regcache_raw_write (regcache, regnum, buf);
  356.       regcache_raw_write (regcache, regnum + 1, buf + 4);
  357.     }
  358.   else if (regnum >= SPARC64_D32_REGNUM && regnum <= SPARC64_D62_REGNUM)
  359.     {
  360.       regnum = SPARC64_F32_REGNUM + (regnum - SPARC64_D32_REGNUM);
  361.       regcache_raw_write (regcache, regnum, buf);
  362.     }
  363.   else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q28_REGNUM)
  364.     {
  365.       regnum = SPARC_F0_REGNUM + 4 * (regnum - SPARC64_Q0_REGNUM);
  366.       regcache_raw_write (regcache, regnum, buf);
  367.       regcache_raw_write (regcache, regnum + 1, buf + 4);
  368.       regcache_raw_write (regcache, regnum + 2, buf + 8);
  369.       regcache_raw_write (regcache, regnum + 3, buf + 12);
  370.     }
  371.   else if (regnum >= SPARC64_Q32_REGNUM && regnum <= SPARC64_Q60_REGNUM)
  372.     {
  373.       regnum = SPARC64_F32_REGNUM + 2 * (regnum - SPARC64_Q32_REGNUM);
  374.       regcache_raw_write (regcache, regnum, buf);
  375.       regcache_raw_write (regcache, regnum + 1, buf + 8);
  376.     }
  377.   else if (regnum == SPARC64_CWP_REGNUM
  378.            || regnum == SPARC64_PSTATE_REGNUM
  379.            || regnum == SPARC64_ASI_REGNUM
  380.            || regnum == SPARC64_CCR_REGNUM)
  381.     {
  382.       ULONGEST state, bits;

  383.       regcache_raw_read_unsigned (regcache, SPARC64_STATE_REGNUM, &state);
  384.       bits = extract_unsigned_integer (buf, 8, byte_order);
  385.       switch (regnum)
  386.         {
  387.         case SPARC64_CWP_REGNUM:
  388.           state |= ((bits & ((1 << 5) - 1)) << 0);
  389.           break;
  390.         case SPARC64_PSTATE_REGNUM:
  391.           state |= ((bits & ((1 << 12) - 1)) << 8);
  392.           break;
  393.         case SPARC64_ASI_REGNUM:
  394.           state |= ((bits & ((1 << 8) - 1)) << 24);
  395.           break;
  396.         case SPARC64_CCR_REGNUM:
  397.           state |= ((bits & ((1 << 8) - 1)) << 32);
  398.           break;
  399.         }
  400.       regcache_raw_write_unsigned (regcache, SPARC64_STATE_REGNUM, state);
  401.     }
  402. }


  403. /* Return PC of first real instruction of the function starting at
  404.    START_PC.  */

  405. static CORE_ADDR
  406. sparc64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  407. {
  408.   struct symtab_and_line sal;
  409.   CORE_ADDR func_start, func_end;
  410.   struct sparc_frame_cache cache;

  411.   /* This is the preferred method, find the end of the prologue by
  412.      using the debugging information.  */
  413.   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
  414.     {
  415.       sal = find_pc_line (func_start, 0);

  416.       if (sal.end < func_end
  417.           && start_pc <= sal.end)
  418.         return sal.end;
  419.     }

  420.   return sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffULL,
  421.                                  &cache);
  422. }

  423. /* Normal frames.  */

  424. static struct sparc_frame_cache *
  425. sparc64_frame_cache (struct frame_info *this_frame, void **this_cache)
  426. {
  427.   return sparc_frame_cache (this_frame, this_cache);
  428. }

  429. static void
  430. sparc64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  431.                        struct frame_id *this_id)
  432. {
  433.   struct sparc_frame_cache *cache =
  434.     sparc64_frame_cache (this_frame, this_cache);

  435.   /* This marks the outermost frame.  */
  436.   if (cache->base == 0)
  437.     return;

  438.   (*this_id) = frame_id_build (cache->base, cache->pc);
  439. }

  440. static struct value *
  441. sparc64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  442.                              int regnum)
  443. {
  444.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  445.   struct sparc_frame_cache *cache =
  446.     sparc64_frame_cache (this_frame, this_cache);

  447.   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  448.     {
  449.       CORE_ADDR pc = (regnum == SPARC64_NPC_REGNUM) ? 4 : 0;

  450.       regnum =
  451.         (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
  452.       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
  453.       return frame_unwind_got_constant (this_frame, regnum, pc);
  454.     }

  455.   /* Handle StackGhost.  */
  456.   {
  457.     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);

  458.     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
  459.       {
  460.         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;
  461.         ULONGEST i7;

  462.         /* Read the value in from memory.  */
  463.         i7 = get_frame_memory_unsigned (this_frame, addr, 8);
  464.         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
  465.       }
  466.   }

  467.   /* The previous frame's `local' and `in' registers may have been saved
  468.      in the register save area.  */
  469.   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  470.       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
  471.     {
  472.       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 8;

  473.       return frame_unwind_got_memory (this_frame, regnum, addr);
  474.     }

  475.   /* The previous frame's `out' registers may be accessible as the current
  476.      frame's `in' registers.  */
  477.   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
  478.       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
  479.     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);

  480.   return frame_unwind_got_register (this_frame, regnum, regnum);
  481. }

  482. static const struct frame_unwind sparc64_frame_unwind =
  483. {
  484.   NORMAL_FRAME,
  485.   default_frame_unwind_stop_reason,
  486.   sparc64_frame_this_id,
  487.   sparc64_frame_prev_register,
  488.   NULL,
  489.   default_frame_sniffer
  490. };


  491. static CORE_ADDR
  492. sparc64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  493. {
  494.   struct sparc_frame_cache *cache =
  495.     sparc64_frame_cache (this_frame, this_cache);

  496.   return cache->base;
  497. }

  498. static const struct frame_base sparc64_frame_base =
  499. {
  500.   &sparc64_frame_unwind,
  501.   sparc64_frame_base_address,
  502.   sparc64_frame_base_address,
  503.   sparc64_frame_base_address
  504. };

  505. /* Check whether TYPE must be 16-byte aligned.  */

  506. static int
  507. sparc64_16_byte_align_p (struct type *type)
  508. {
  509.   if (sparc64_floating_p (type) && TYPE_LENGTH (type) == 16)
  510.     return 1;

  511.   if (sparc64_structure_or_union_p (type))
  512.     {
  513.       int i;

  514.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  515.         {
  516.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));

  517.           if (sparc64_16_byte_align_p (subtype))
  518.             return 1;
  519.         }
  520.     }

  521.   return 0;
  522. }

  523. /* Store floating fields of element ELEMENT of an "parameter array"
  524.    that has type TYPE and is stored at BITPOS in VALBUF in the
  525.    apropriate registers of REGCACHE.  This function can be called
  526.    recursively and therefore handles floating types in addition to
  527.    structures.  */

  528. static void
  529. sparc64_store_floating_fields (struct regcache *regcache, struct type *type,
  530.                                const gdb_byte *valbuf, int element, int bitpos)
  531. {
  532.   int len = TYPE_LENGTH (type);

  533.   gdb_assert (element < 16);

  534.   if (sparc64_floating_p (type)
  535.       || (sparc64_complex_floating_p (type) && len <= 16))
  536.     {
  537.       int regnum;

  538.       if (len == 16)
  539.         {
  540.           gdb_assert (bitpos == 0);
  541.           gdb_assert ((element % 2) == 0);

  542.           regnum = SPARC64_Q0_REGNUM + element / 2;
  543.           regcache_cooked_write (regcache, regnum, valbuf);
  544.         }
  545.       else if (len == 8)
  546.         {
  547.           gdb_assert (bitpos == 0 || bitpos == 64);

  548.           regnum = SPARC64_D0_REGNUM + element + bitpos / 64;
  549.           regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
  550.         }
  551.       else
  552.         {
  553.           gdb_assert (len == 4);
  554.           gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 128);

  555.           regnum = SPARC_F0_REGNUM + element * 2 + bitpos / 32;
  556.           regcache_cooked_write (regcache, regnum, valbuf + (bitpos / 8));
  557.         }
  558.     }
  559.   else if (sparc64_structure_or_union_p (type))
  560.     {
  561.       int i;

  562.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  563.         {
  564.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
  565.           int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);

  566.           sparc64_store_floating_fields (regcache, subtype, valbuf,
  567.                                          element, subpos);
  568.         }

  569.       /* GCC has an interesting bug.  If TYPE is a structure that has
  570.          a single `float' member, GCC doesn't treat it as a structure
  571.          at all, but rather as an ordinary `float' argument.  This
  572.          argument will be stored in %f1, as required by the psABI.
  573.          However, as a member of a structure the psABI requires it to
  574.          be stored in %f0.  This bug is present in GCC 3.3.2, but
  575.          probably in older releases to.  To appease GCC, if a
  576.          structure has only a single `float' member, we store its
  577.          value in %f1 too (we already have stored in %f0).  */
  578.       if (TYPE_NFIELDS (type) == 1)
  579.         {
  580.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, 0));

  581.           if (sparc64_floating_p (subtype) && TYPE_LENGTH (subtype) == 4)
  582.             regcache_cooked_write (regcache, SPARC_F1_REGNUM, valbuf);
  583.         }
  584.     }
  585. }

  586. /* Fetch floating fields from a variable of type TYPE from the
  587.    appropriate registers for BITPOS in REGCACHE and store it at BITPOS
  588.    in VALBUF.  This function can be called recursively and therefore
  589.    handles floating types in addition to structures.  */

  590. static void
  591. sparc64_extract_floating_fields (struct regcache *regcache, struct type *type,
  592.                                  gdb_byte *valbuf, int bitpos)
  593. {
  594.   if (sparc64_floating_p (type))
  595.     {
  596.       int len = TYPE_LENGTH (type);
  597.       int regnum;

  598.       if (len == 16)
  599.         {
  600.           gdb_assert (bitpos == 0 || bitpos == 128);

  601.           regnum = SPARC64_Q0_REGNUM + bitpos / 128;
  602.           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
  603.         }
  604.       else if (len == 8)
  605.         {
  606.           gdb_assert (bitpos % 64 == 0 && bitpos >= 0 && bitpos < 256);

  607.           regnum = SPARC64_D0_REGNUM + bitpos / 64;
  608.           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
  609.         }
  610.       else
  611.         {
  612.           gdb_assert (len == 4);
  613.           gdb_assert (bitpos % 32 == 0 && bitpos >= 0 && bitpos < 256);

  614.           regnum = SPARC_F0_REGNUM + bitpos / 32;
  615.           regcache_cooked_read (regcache, regnum, valbuf + (bitpos / 8));
  616.         }
  617.     }
  618.   else if (sparc64_structure_or_union_p (type))
  619.     {
  620.       int i;

  621.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  622.         {
  623.           struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
  624.           int subpos = bitpos + TYPE_FIELD_BITPOS (type, i);

  625.           sparc64_extract_floating_fields (regcache, subtype, valbuf, subpos);
  626.         }
  627.     }
  628. }

  629. /* Store the NARGS arguments ARGS and STRUCT_ADDR (if STRUCT_RETURN is
  630.    non-zero) in REGCACHE and on the stack (starting from address SP).  */

  631. static CORE_ADDR
  632. sparc64_store_arguments (struct regcache *regcache, int nargs,
  633.                          struct value **args, CORE_ADDR sp,
  634.                          int struct_return, CORE_ADDR struct_addr)
  635. {
  636.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  637.   /* Number of extended words in the "parameter array".  */
  638.   int num_elements = 0;
  639.   int element = 0;
  640.   int i;

  641.   /* Take BIAS into account.  */
  642.   sp += BIAS;

  643.   /* First we calculate the number of extended words in the "parameter
  644.      array".  While doing so we also convert some of the arguments.  */

  645.   if (struct_return)
  646.     num_elements++;

  647.   for (i = 0; i < nargs; i++)
  648.     {
  649.       struct type *type = value_type (args[i]);
  650.       int len = TYPE_LENGTH (type);

  651.       if (sparc64_structure_or_union_p (type)
  652.           || (sparc64_complex_floating_p (type) && len == 32))
  653.         {
  654.           /* Structure or Union arguments.  */
  655.           if (len <= 16)
  656.             {
  657.               if (num_elements % 2 && sparc64_16_byte_align_p (type))
  658.                 num_elements++;
  659.               num_elements += ((len + 7) / 8);
  660.             }
  661.           else
  662.             {
  663.               /* The psABI says that "Structures or unions larger than
  664.                  sixteen bytes are copied by the caller and passed
  665.                  indirectly; the caller will pass the address of a
  666.                  correctly aligned structure value.  This sixty-four
  667.                  bit address will occupy one word in the parameter
  668.                  array, and may be promoted to an %o register like any
  669.                  other pointer value."  Allocate memory for these
  670.                  values on the stack.  */
  671.               sp -= len;

  672.               /* Use 16-byte alignment for these values.  That's
  673.                  always correct, and wasting a few bytes shouldn't be
  674.                  a problem.  */
  675.               sp &= ~0xf;

  676.               write_memory (sp, value_contents (args[i]), len);
  677.               args[i] = value_from_pointer (lookup_pointer_type (type), sp);
  678.               num_elements++;
  679.             }
  680.         }
  681.       else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  682.         {
  683.           /* Floating arguments.  */
  684.           if (len == 16)
  685.             {
  686.               /* The psABI says that "Each quad-precision parameter
  687.                  value will be assigned to two extended words in the
  688.                  parameter array.  */
  689.               num_elements += 2;

  690.               /* The psABI says that "Long doubles must be
  691.                  quad-aligned, and thus a hole might be introduced
  692.                  into the parameter array to force alignment."  Skip
  693.                  an element if necessary.  */
  694.               if ((num_elements % 2) && sparc64_16_byte_align_p (type))
  695.                 num_elements++;
  696.             }
  697.           else
  698.             num_elements++;
  699.         }
  700.       else
  701.         {
  702.           /* Integral and pointer arguments.  */
  703.           gdb_assert (sparc64_integral_or_pointer_p (type));

  704.           /* The psABI says that "Each argument value of integral type
  705.              smaller than an extended word will be widened by the
  706.              caller to an extended word according to the signed-ness
  707.              of the argument type."  */
  708.           if (len < 8)
  709.             args[i] = value_cast (builtin_type (gdbarch)->builtin_int64,
  710.                                   args[i]);
  711.           num_elements++;
  712.         }
  713.     }

  714.   /* Allocate the "parameter array".  */
  715.   sp -= num_elements * 8;

  716.   /* The psABI says that "Every stack frame must be 16-byte aligned."  */
  717.   sp &= ~0xf;

  718.   /* Now we store the arguments in to the "paramater array".  Some
  719.      Integer or Pointer arguments and Structure or Union arguments
  720.      will be passed in %o registers.  Some Floating arguments and
  721.      floating members of structures are passed in floating-point
  722.      registers.  However, for functions with variable arguments,
  723.      floating arguments are stored in an %0 register, and for
  724.      functions without a prototype floating arguments are stored in
  725.      both a floating-point and an %o registers, or a floating-point
  726.      register and memory.  To simplify the logic here we always pass
  727.      arguments in memory, an %o register, and a floating-point
  728.      register if appropriate.  This should be no problem since the
  729.      contents of any unused memory or registers in the "parameter
  730.      array" are undefined.  */

  731.   if (struct_return)
  732.     {
  733.       regcache_cooked_write_unsigned (regcache, SPARC_O0_REGNUM, struct_addr);
  734.       element++;
  735.     }

  736.   for (i = 0; i < nargs; i++)
  737.     {
  738.       const gdb_byte *valbuf = value_contents (args[i]);
  739.       struct type *type = value_type (args[i]);
  740.       int len = TYPE_LENGTH (type);
  741.       int regnum = -1;
  742.       gdb_byte buf[16];

  743.       if (sparc64_structure_or_union_p (type)
  744.           || (sparc64_complex_floating_p (type) && len == 32))
  745.         {
  746.           /* Structure, Union or long double Complex arguments.  */
  747.           gdb_assert (len <= 16);
  748.           memset (buf, 0, sizeof (buf));
  749.           valbuf = memcpy (buf, valbuf, len);

  750.           if (element % 2 && sparc64_16_byte_align_p (type))
  751.             element++;

  752.           if (element < 6)
  753.             {
  754.               regnum = SPARC_O0_REGNUM + element;
  755.               if (len > 8 && element < 5)
  756.                 regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
  757.             }

  758.           if (element < 16)
  759.             sparc64_store_floating_fields (regcache, type, valbuf, element, 0);
  760.         }
  761.       else if (sparc64_complex_floating_p (type))
  762.         {
  763.           /* Float Complex or double Complex arguments.  */
  764.           if (element < 16)
  765.             {
  766.               regnum = SPARC64_D0_REGNUM + element;

  767.               if (len == 16)
  768.                 {
  769.                   if (regnum < SPARC64_D30_REGNUM)
  770.                     regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
  771.                   if (regnum < SPARC64_D10_REGNUM)
  772.                     regcache_cooked_write (regcache,
  773.                                            SPARC_O0_REGNUM + element + 1,
  774.                                            valbuf + 8);
  775.                 }
  776.             }
  777.         }
  778.       else if (sparc64_floating_p (type))
  779.         {
  780.           /* Floating arguments.  */
  781.           if (len == 16)
  782.             {
  783.               if (element % 2)
  784.                 element++;
  785.               if (element < 16)
  786.                 regnum = SPARC64_Q0_REGNUM + element / 2;
  787.             }
  788.           else if (len == 8)
  789.             {
  790.               if (element < 16)
  791.                 regnum = SPARC64_D0_REGNUM + element;
  792.             }
  793.           else if (len == 4)
  794.             {
  795.               /* The psABI says "Each single-precision parameter value
  796.                  will be assigned to one extended word in the
  797.                  parameter array, and right-justified within that
  798.                  word; the left half (even float register) is
  799.                  undefined."  Even though the psABI says that "the
  800.                  left half is undefined", set it to zero here.  */
  801.               memset (buf, 0, 4);
  802.               memcpy (buf + 4, valbuf, 4);
  803.               valbuf = buf;
  804.               len = 8;
  805.               if (element < 16)
  806.                 regnum = SPARC64_D0_REGNUM + element;
  807.             }
  808.         }
  809.       else
  810.         {
  811.           /* Integral and pointer arguments.  */
  812.           gdb_assert (len == 8);
  813.           if (element < 6)
  814.             regnum = SPARC_O0_REGNUM + element;
  815.         }

  816.       if (regnum != -1)
  817.         {
  818.           regcache_cooked_write (regcache, regnum, valbuf);

  819.           /* If we're storing the value in a floating-point register,
  820.              also store it in the corresponding %0 register(s).  */
  821.           if (regnum >= SPARC64_D0_REGNUM && regnum <= SPARC64_D10_REGNUM)
  822.             {
  823.               gdb_assert (element < 6);
  824.               regnum = SPARC_O0_REGNUM + element;
  825.               regcache_cooked_write (regcache, regnum, valbuf);
  826.             }
  827.           else if (regnum >= SPARC64_Q0_REGNUM && regnum <= SPARC64_Q8_REGNUM)
  828.             {
  829.               gdb_assert (element < 5);
  830.               regnum = SPARC_O0_REGNUM + element;
  831.               regcache_cooked_write (regcache, regnum, valbuf);
  832.               regcache_cooked_write (regcache, regnum + 1, valbuf + 8);
  833.             }
  834.         }

  835.       /* Always store the argument in memory.  */
  836.       write_memory (sp + element * 8, valbuf, len);
  837.       element += ((len + 7) / 8);
  838.     }

  839.   gdb_assert (element == num_elements);

  840.   /* Take BIAS into account.  */
  841.   sp -= BIAS;
  842.   return sp;
  843. }

  844. static CORE_ADDR
  845. sparc64_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  846. {
  847.   /* The ABI requires 16-byte alignment.  */
  848.   return address & ~0xf;
  849. }

  850. static CORE_ADDR
  851. sparc64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  852.                          struct regcache *regcache, CORE_ADDR bp_addr,
  853.                          int nargs, struct value **args, CORE_ADDR sp,
  854.                          int struct_return, CORE_ADDR struct_addr)
  855. {
  856.   /* Set return address.  */
  857.   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, bp_addr - 8);

  858.   /* Set up function arguments.  */
  859.   sp = sparc64_store_arguments (regcache, nargs, args, sp,
  860.                                 struct_return, struct_addr);

  861.   /* Allocate the register save area.  */
  862.   sp -= 16 * 8;

  863.   /* Stack should be 16-byte aligned at this point.  */
  864.   gdb_assert ((sp + BIAS) % 16 == 0);

  865.   /* Finally, update the stack pointer.  */
  866.   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);

  867.   return sp + BIAS;
  868. }


  869. /* Extract from an array REGBUF containing the (raw) register state, a
  870.    function return value of TYPE, and copy that into VALBUF.  */

  871. static void
  872. sparc64_extract_return_value (struct type *type, struct regcache *regcache,
  873.                               gdb_byte *valbuf)
  874. {
  875.   int len = TYPE_LENGTH (type);
  876.   gdb_byte buf[32];
  877.   int i;

  878.   if (sparc64_structure_or_union_p (type))
  879.     {
  880.       /* Structure or Union return values.  */
  881.       gdb_assert (len <= 32);

  882.       for (i = 0; i < ((len + 7) / 8); i++)
  883.         regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
  884.       if (TYPE_CODE (type) != TYPE_CODE_UNION)
  885.         sparc64_extract_floating_fields (regcache, type, buf, 0);
  886.       memcpy (valbuf, buf, len);
  887.     }
  888.   else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  889.     {
  890.       /* Floating return values.  */
  891.       for (i = 0; i < len / 4; i++)
  892.         regcache_cooked_read (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
  893.       memcpy (valbuf, buf, len);
  894.     }
  895.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  896.     {
  897.       /* Small arrays are returned the same way as small structures.  */
  898.       gdb_assert (len <= 32);

  899.       for (i = 0; i < ((len + 7) / 8); i++)
  900.         regcache_cooked_read (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
  901.       memcpy (valbuf, buf, len);
  902.     }
  903.   else
  904.     {
  905.       /* Integral and pointer return values.  */
  906.       gdb_assert (sparc64_integral_or_pointer_p (type));

  907.       /* Just stripping off any unused bytes should preserve the
  908.          signed-ness just fine.  */
  909.       regcache_cooked_read (regcache, SPARC_O0_REGNUM, buf);
  910.       memcpy (valbuf, buf + 8 - len, len);
  911.     }
  912. }

  913. /* Write into the appropriate registers a function return value stored
  914.    in VALBUF of type TYPE.  */

  915. static void
  916. sparc64_store_return_value (struct type *type, struct regcache *regcache,
  917.                             const gdb_byte *valbuf)
  918. {
  919.   int len = TYPE_LENGTH (type);
  920.   gdb_byte buf[16];
  921.   int i;

  922.   if (sparc64_structure_or_union_p (type))
  923.     {
  924.       /* Structure or Union return values.  */
  925.       gdb_assert (len <= 32);

  926.       /* Simplify matters by storing the complete value (including
  927.          floating members) into %o0 and %o1.  Floating members are
  928.          also store in the appropriate floating-point registers.  */
  929.       memset (buf, 0, sizeof (buf));
  930.       memcpy (buf, valbuf, len);
  931.       for (i = 0; i < ((len + 7) / 8); i++)
  932.         regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
  933.       if (TYPE_CODE (type) != TYPE_CODE_UNION)
  934.         sparc64_store_floating_fields (regcache, type, buf, 0, 0);
  935.     }
  936.   else if (sparc64_floating_p (type) || sparc64_complex_floating_p (type))
  937.     {
  938.       /* Floating return values.  */
  939.       memcpy (buf, valbuf, len);
  940.       for (i = 0; i < len / 4; i++)
  941.         regcache_cooked_write (regcache, SPARC_F0_REGNUM + i, buf + i * 4);
  942.     }
  943.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  944.     {
  945.       /* Small arrays are returned the same way as small structures.  */
  946.       gdb_assert (len <= 32);

  947.       memset (buf, 0, sizeof (buf));
  948.       memcpy (buf, valbuf, len);
  949.       for (i = 0; i < ((len + 7) / 8); i++)
  950.         regcache_cooked_write (regcache, SPARC_O0_REGNUM + i, buf + i * 8);
  951.     }
  952.   else
  953.     {
  954.       /* Integral and pointer return values.  */
  955.       gdb_assert (sparc64_integral_or_pointer_p (type));

  956.       /* ??? Do we need to do any sign-extension here?  */
  957.       memset (buf, 0, 8);
  958.       memcpy (buf + 8 - len, valbuf, len);
  959.       regcache_cooked_write (regcache, SPARC_O0_REGNUM, buf);
  960.     }
  961. }

  962. static enum return_value_convention
  963. sparc64_return_value (struct gdbarch *gdbarch, struct value *function,
  964.                       struct type *type, struct regcache *regcache,
  965.                       gdb_byte *readbuf, const gdb_byte *writebuf)
  966. {
  967.   if (TYPE_LENGTH (type) > 32)
  968.     return RETURN_VALUE_STRUCT_CONVENTION;

  969.   if (readbuf)
  970.     sparc64_extract_return_value (type, regcache, readbuf);
  971.   if (writebuf)
  972.     sparc64_store_return_value (type, regcache, writebuf);

  973.   return RETURN_VALUE_REGISTER_CONVENTION;
  974. }


  975. static void
  976. sparc64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  977.                                struct dwarf2_frame_state_reg *reg,
  978.                                struct frame_info *this_frame)
  979. {
  980.   switch (regnum)
  981.     {
  982.     case SPARC_G0_REGNUM:
  983.       /* Since %g0 is always zero, there is no point in saving it, and
  984.          people will be inclined omit it from the CFI.  Make sure we
  985.          don't warn about that.  */
  986.       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
  987.       break;
  988.     case SPARC_SP_REGNUM:
  989.       reg->how = DWARF2_FRAME_REG_CFA;
  990.       break;
  991.     case SPARC64_PC_REGNUM:
  992.       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  993.       reg->loc.offset = 8;
  994.       break;
  995.     case SPARC64_NPC_REGNUM:
  996.       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  997.       reg->loc.offset = 12;
  998.       break;
  999.     }
  1000. }

  1001. void
  1002. sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  1003. {
  1004.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1005.   tdep->pc_regnum = SPARC64_PC_REGNUM;
  1006.   tdep->npc_regnum = SPARC64_NPC_REGNUM;

  1007.   /* This is what all the fuss is about.  */
  1008.   set_gdbarch_long_bit (gdbarch, 64);
  1009.   set_gdbarch_long_long_bit (gdbarch, 64);
  1010.   set_gdbarch_ptr_bit (gdbarch, 64);

  1011.   set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
  1012.   set_gdbarch_register_name (gdbarch, sparc64_register_name);
  1013.   set_gdbarch_register_type (gdbarch, sparc64_register_type);
  1014.   set_gdbarch_num_pseudo_regs (gdbarch, SPARC64_NUM_PSEUDO_REGS);
  1015.   set_gdbarch_pseudo_register_read (gdbarch, sparc64_pseudo_register_read);
  1016.   set_gdbarch_pseudo_register_write (gdbarch, sparc64_pseudo_register_write);

  1017.   /* Register numbers of various important registers.  */
  1018.   set_gdbarch_pc_regnum (gdbarch, SPARC64_PC_REGNUM); /* %pc */

  1019.   /* Call dummy code.  */
  1020.   set_gdbarch_frame_align (gdbarch, sparc64_frame_align);
  1021.   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
  1022.   set_gdbarch_push_dummy_code (gdbarch, NULL);
  1023.   set_gdbarch_push_dummy_call (gdbarch, sparc64_push_dummy_call);

  1024.   set_gdbarch_return_value (gdbarch, sparc64_return_value);
  1025.   set_gdbarch_stabs_argument_has_addr
  1026.     (gdbarch, default_stabs_argument_has_addr);

  1027.   set_gdbarch_skip_prologue (gdbarch, sparc64_skip_prologue);
  1028.   set_gdbarch_in_function_epilogue_p (gdbarch, sparc_in_function_epilogue_p);

  1029.   /* Hook in the DWARF CFI frame unwinder.  */
  1030.   dwarf2_frame_set_init_reg (gdbarch, sparc64_dwarf2_frame_init_reg);
  1031.   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
  1032.      StackGhost issues have been resolved.  */

  1033.   frame_unwind_append_unwinder (gdbarch, &sparc64_frame_unwind);
  1034.   frame_base_set_default (gdbarch, &sparc64_frame_base);
  1035. }


  1036. /* Helper functions for dealing with register sets.  */

  1037. #define TSTATE_CWP        0x000000000000001fULL
  1038. #define TSTATE_ICC        0x0000000f00000000ULL
  1039. #define TSTATE_XCC        0x000000f000000000ULL

  1040. #define PSR_S                0x00000080
  1041. #define PSR_ICC                0x00f00000
  1042. #define PSR_VERS        0x0f000000
  1043. #define PSR_IMPL        0xf0000000
  1044. #define PSR_V8PLUS        0xff000000
  1045. #define PSR_XCC                0x000f0000

  1046. void
  1047. sparc64_supply_gregset (const struct sparc_gregmap *gregmap,
  1048.                         struct regcache *regcache,
  1049.                         int regnum, const void *gregs)
  1050. {
  1051.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1052.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1053.   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
  1054.   const gdb_byte *regs = gregs;
  1055.   gdb_byte zero[8] = { 0 };
  1056.   int i;

  1057.   if (sparc32)
  1058.     {
  1059.       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1060.         {
  1061.           int offset = gregmap->r_tstate_offset;
  1062.           ULONGEST tstate, psr;
  1063.           gdb_byte buf[4];

  1064.           tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
  1065.           psr = ((tstate & TSTATE_CWP) | PSR_S | ((tstate & TSTATE_ICC) >> 12)
  1066.                  | ((tstate & TSTATE_XCC) >> 20) | PSR_V8PLUS);
  1067.           store_unsigned_integer (buf, 4, byte_order, psr);
  1068.           regcache_raw_supply (regcache, SPARC32_PSR_REGNUM, buf);
  1069.         }

  1070.       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1071.         regcache_raw_supply (regcache, SPARC32_PC_REGNUM,
  1072.                              regs + gregmap->r_pc_offset + 4);

  1073.       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1074.         regcache_raw_supply (regcache, SPARC32_NPC_REGNUM,
  1075.                              regs + gregmap->r_npc_offset + 4);

  1076.       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1077.         {
  1078.           int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
  1079.           regcache_raw_supply (regcache, SPARC32_Y_REGNUM, regs + offset);
  1080.         }
  1081.     }
  1082.   else
  1083.     {
  1084.       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
  1085.         regcache_raw_supply (regcache, SPARC64_STATE_REGNUM,
  1086.                              regs + gregmap->r_tstate_offset);

  1087.       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  1088.         regcache_raw_supply (regcache, SPARC64_PC_REGNUM,
  1089.                              regs + gregmap->r_pc_offset);

  1090.       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  1091.         regcache_raw_supply (regcache, SPARC64_NPC_REGNUM,
  1092.                              regs + gregmap->r_npc_offset);

  1093.       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
  1094.         {
  1095.           gdb_byte buf[8];

  1096.           memset (buf, 0, 8);
  1097.           memcpy (buf + 8 - gregmap->r_y_size,
  1098.                   regs + gregmap->r_y_offset, gregmap->r_y_size);
  1099.           regcache_raw_supply (regcache, SPARC64_Y_REGNUM, buf);
  1100.         }

  1101.       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
  1102.           && gregmap->r_fprs_offset != -1)
  1103.         regcache_raw_supply (regcache, SPARC64_FPRS_REGNUM,
  1104.                              regs + gregmap->r_fprs_offset);
  1105.     }

  1106.   if (regnum == SPARC_G0_REGNUM || regnum == -1)
  1107.     regcache_raw_supply (regcache, SPARC_G0_REGNUM, &zero);

  1108.   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1109.     {
  1110.       int offset = gregmap->r_g1_offset;

  1111.       if (sparc32)
  1112.         offset += 4;

  1113.       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1114.         {
  1115.           if (regnum == i || regnum == -1)
  1116.             regcache_raw_supply (regcache, i, regs + offset);
  1117.           offset += 8;
  1118.         }
  1119.     }

  1120.   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1121.     {
  1122.       /* Not all of the register set variants include Locals and
  1123.          Inputs.  For those that don't, we read them off the stack.  */
  1124.       if (gregmap->r_l0_offset == -1)
  1125.         {
  1126.           ULONGEST sp;

  1127.           regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1128.           sparc_supply_rwindow (regcache, sp, regnum);
  1129.         }
  1130.       else
  1131.         {
  1132.           int offset = gregmap->r_l0_offset;

  1133.           if (sparc32)
  1134.             offset += 4;

  1135.           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1136.             {
  1137.               if (regnum == i || regnum == -1)
  1138.                 regcache_raw_supply (regcache, i, regs + offset);
  1139.               offset += 8;
  1140.             }
  1141.         }
  1142.     }
  1143. }

  1144. void
  1145. sparc64_collect_gregset (const struct sparc_gregmap *gregmap,
  1146.                          const struct regcache *regcache,
  1147.                          int regnum, void *gregs)
  1148. {
  1149.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1150.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1151.   int sparc32 = (gdbarch_ptr_bit (gdbarch) == 32);
  1152.   gdb_byte *regs = gregs;
  1153.   int i;

  1154.   if (sparc32)
  1155.     {
  1156.       if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1157.         {
  1158.           int offset = gregmap->r_tstate_offset;
  1159.           ULONGEST tstate, psr;
  1160.           gdb_byte buf[8];

  1161.           tstate = extract_unsigned_integer (regs + offset, 8, byte_order);
  1162.           regcache_raw_collect (regcache, SPARC32_PSR_REGNUM, buf);
  1163.           psr = extract_unsigned_integer (buf, 4, byte_order);
  1164.           tstate |= (psr & PSR_ICC) << 12;
  1165.           if ((psr & (PSR_VERS | PSR_IMPL)) == PSR_V8PLUS)
  1166.             tstate |= (psr & PSR_XCC) << 20;
  1167.           store_unsigned_integer (buf, 8, byte_order, tstate);
  1168.           memcpy (regs + offset, buf, 8);
  1169.         }

  1170.       if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1171.         regcache_raw_collect (regcache, SPARC32_PC_REGNUM,
  1172.                               regs + gregmap->r_pc_offset + 4);

  1173.       if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1174.         regcache_raw_collect (regcache, SPARC32_NPC_REGNUM,
  1175.                               regs + gregmap->r_npc_offset + 4);

  1176.       if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1177.         {
  1178.           int offset = gregmap->r_y_offset + 8 - gregmap->r_y_size;
  1179.           regcache_raw_collect (regcache, SPARC32_Y_REGNUM, regs + offset);
  1180.         }
  1181.     }
  1182.   else
  1183.     {
  1184.       if (regnum == SPARC64_STATE_REGNUM || regnum == -1)
  1185.         regcache_raw_collect (regcache, SPARC64_STATE_REGNUM,
  1186.                               regs + gregmap->r_tstate_offset);

  1187.       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  1188.         regcache_raw_collect (regcache, SPARC64_PC_REGNUM,
  1189.                               regs + gregmap->r_pc_offset);

  1190.       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  1191.         regcache_raw_collect (regcache, SPARC64_NPC_REGNUM,
  1192.                               regs + gregmap->r_npc_offset);

  1193.       if (regnum == SPARC64_Y_REGNUM || regnum == -1)
  1194.         {
  1195.           gdb_byte buf[8];

  1196.           regcache_raw_collect (regcache, SPARC64_Y_REGNUM, buf);
  1197.           memcpy (regs + gregmap->r_y_offset,
  1198.                   buf + 8 - gregmap->r_y_size, gregmap->r_y_size);
  1199.         }

  1200.       if ((regnum == SPARC64_FPRS_REGNUM || regnum == -1)
  1201.           && gregmap->r_fprs_offset != -1)
  1202.         regcache_raw_collect (regcache, SPARC64_FPRS_REGNUM,
  1203.                               regs + gregmap->r_fprs_offset);

  1204.     }

  1205.   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1206.     {
  1207.       int offset = gregmap->r_g1_offset;

  1208.       if (sparc32)
  1209.         offset += 4;

  1210.       /* %g0 is always zero.  */
  1211.       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1212.         {
  1213.           if (regnum == i || regnum == -1)
  1214.             regcache_raw_collect (regcache, i, regs + offset);
  1215.           offset += 8;
  1216.         }
  1217.     }

  1218.   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1219.     {
  1220.       /* Not all of the register set variants include Locals and
  1221.          Inputs.  For those that don't, we read them off the stack.  */
  1222.       if (gregmap->r_l0_offset != -1)
  1223.         {
  1224.           int offset = gregmap->r_l0_offset;

  1225.           if (sparc32)
  1226.             offset += 4;

  1227.           for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1228.             {
  1229.               if (regnum == i || regnum == -1)
  1230.                 regcache_raw_collect (regcache, i, regs + offset);
  1231.               offset += 8;
  1232.             }
  1233.         }
  1234.     }
  1235. }

  1236. void
  1237. sparc64_supply_fpregset (const struct sparc_fpregmap *fpregmap,
  1238.                          struct regcache *regcache,
  1239.                          int regnum, const void *fpregs)
  1240. {
  1241.   int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
  1242.   const gdb_byte *regs = fpregs;
  1243.   int i;

  1244.   for (i = 0; i < 32; i++)
  1245.     {
  1246.       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1247.         regcache_raw_supply (regcache, SPARC_F0_REGNUM + i,
  1248.                              regs + fpregmap->r_f0_offset + (i * 4));
  1249.     }

  1250.   if (sparc32)
  1251.     {
  1252.       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1253.         regcache_raw_supply (regcache, SPARC32_FSR_REGNUM,
  1254.                              regs + fpregmap->r_fsr_offset);
  1255.     }
  1256.   else
  1257.     {
  1258.       for (i = 0; i < 16; i++)
  1259.         {
  1260.           if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
  1261.             regcache_raw_supply (regcache, SPARC64_F32_REGNUM + i,
  1262.                                  (regs + fpregmap->r_f0_offset
  1263.                                   + (32 * 4) + (i * 8)));
  1264.         }

  1265.       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
  1266.         regcache_raw_supply (regcache, SPARC64_FSR_REGNUM,
  1267.                              regs + fpregmap->r_fsr_offset);
  1268.     }
  1269. }

  1270. void
  1271. sparc64_collect_fpregset (const struct sparc_fpregmap *fpregmap,
  1272.                           const struct regcache *regcache,
  1273.                           int regnum, void *fpregs)
  1274. {
  1275.   int sparc32 = (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32);
  1276.   gdb_byte *regs = fpregs;
  1277.   int i;

  1278.   for (i = 0; i < 32; i++)
  1279.     {
  1280.       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1281.         regcache_raw_collect (regcache, SPARC_F0_REGNUM + i,
  1282.                               regs + fpregmap->r_f0_offset + (i * 4));
  1283.     }

  1284.   if (sparc32)
  1285.     {
  1286.       if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1287.         regcache_raw_collect (regcache, SPARC32_FSR_REGNUM,
  1288.                               regs + fpregmap->r_fsr_offset);
  1289.     }
  1290.   else
  1291.     {
  1292.       for (i = 0; i < 16; i++)
  1293.         {
  1294.           if (regnum == (SPARC64_F32_REGNUM + i) || regnum == -1)
  1295.             regcache_raw_collect (regcache, SPARC64_F32_REGNUM + i,
  1296.                                   (regs + fpregmap->r_f0_offset
  1297.                                    + (32 * 4) + (i * 8)));
  1298.         }

  1299.       if (regnum == SPARC64_FSR_REGNUM || regnum == -1)
  1300.         regcache_raw_collect (regcache, SPARC64_FSR_REGNUM,
  1301.                               regs + fpregmap->r_fsr_offset);
  1302.     }
  1303. }

  1304. const struct sparc_fpregmap sparc64_bsd_fpregmap =
  1305. {
  1306.   0 * 8,                        /* %f0 */
  1307.   32 * 8,                        /* %fsr */
  1308. };