Home | History | Annotate | Line # | Download | only in gdb
sparc-tdep.c revision 1.9
      1  1.1  christos /* Target-dependent code for SPARC.
      2  1.1  christos 
      3  1.9  christos    Copyright (C) 2003-2020 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This file is part of GDB.
      6  1.1  christos 
      7  1.1  christos    This program is free software; you can redistribute it and/or modify
      8  1.1  christos    it under the terms of the GNU General Public License as published by
      9  1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10  1.1  christos    (at your option) any later version.
     11  1.1  christos 
     12  1.1  christos    This program is distributed in the hope that it will be useful,
     13  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  christos    GNU General Public License for more details.
     16  1.1  christos 
     17  1.1  christos    You should have received a copy of the GNU General Public License
     18  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19  1.1  christos 
     20  1.1  christos #include "defs.h"
     21  1.1  christos #include "arch-utils.h"
     22  1.1  christos #include "dis-asm.h"
     23  1.8  christos #include "dwarf2.h"
     24  1.9  christos #include "dwarf2/frame.h"
     25  1.1  christos #include "frame.h"
     26  1.1  christos #include "frame-base.h"
     27  1.1  christos #include "frame-unwind.h"
     28  1.1  christos #include "gdbcore.h"
     29  1.1  christos #include "gdbtypes.h"
     30  1.1  christos #include "inferior.h"
     31  1.1  christos #include "symtab.h"
     32  1.1  christos #include "objfiles.h"
     33  1.1  christos #include "osabi.h"
     34  1.1  christos #include "regcache.h"
     35  1.1  christos #include "target.h"
     36  1.7  christos #include "target-descriptions.h"
     37  1.1  christos #include "value.h"
     38  1.1  christos 
     39  1.1  christos #include "sparc-tdep.h"
     40  1.1  christos #include "sparc-ravenscar-thread.h"
     41  1.7  christos #include <algorithm>
     42  1.1  christos 
     43  1.1  christos struct regset;
     44  1.1  christos 
     45  1.1  christos /* This file implements the SPARC 32-bit ABI as defined by the section
     46  1.1  christos    "Low-Level System Information" of the SPARC Compliance Definition
     47  1.1  christos    (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC.  The SCD
     48  1.1  christos    lists changes with respect to the original 32-bit psABI as defined
     49  1.1  christos    in the "System V ABI, SPARC Processor Supplement".
     50  1.1  christos 
     51  1.1  christos    Note that if we talk about SunOS, we mean SunOS 4.x, which was
     52  1.1  christos    BSD-based, which is sometimes (retroactively?) referred to as
     53  1.1  christos    Solaris 1.x.  If we talk about Solaris we mean Solaris 2.x and
     54  1.1  christos    above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
     55  1.1  christos    suffering from severe version number inflation).  Solaris 2.x is
     56  1.1  christos    also known as SunOS 5.x, since that's what uname(1) says.  Solaris
     57  1.1  christos    2.x is SVR4-based.  */
     58  1.1  christos 
     59  1.1  christos /* Please use the sparc32_-prefix for 32-bit specific code, the
     60  1.1  christos    sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
     61  1.1  christos    code that can handle both.  The 64-bit specific code lives in
     62  1.1  christos    sparc64-tdep.c; don't add any here.  */
     63  1.1  christos 
     64  1.1  christos /* The SPARC Floating-Point Quad-Precision format is similar to
     65  1.1  christos    big-endian IA-64 Quad-Precision format.  */
     66  1.1  christos #define floatformats_sparc_quad floatformats_ia64_quad
     67  1.1  christos 
     68  1.1  christos /* The stack pointer is offset from the stack frame by a BIAS of 2047
     69  1.1  christos    (0x7ff) for 64-bit code.  BIAS is likely to be defined on SPARC
     70  1.1  christos    hosts, so undefine it first.  */
     71  1.1  christos #undef BIAS
     72  1.1  christos #define BIAS 2047
     73  1.1  christos 
     74  1.1  christos /* Macros to extract fields from SPARC instructions.  */
     75  1.1  christos #define X_OP(i) (((i) >> 30) & 0x3)
     76  1.1  christos #define X_RD(i) (((i) >> 25) & 0x1f)
     77  1.1  christos #define X_A(i) (((i) >> 29) & 1)
     78  1.1  christos #define X_COND(i) (((i) >> 25) & 0xf)
     79  1.1  christos #define X_OP2(i) (((i) >> 22) & 0x7)
     80  1.1  christos #define X_IMM22(i) ((i) & 0x3fffff)
     81  1.1  christos #define X_OP3(i) (((i) >> 19) & 0x3f)
     82  1.1  christos #define X_RS1(i) (((i) >> 14) & 0x1f)
     83  1.1  christos #define X_RS2(i) ((i) & 0x1f)
     84  1.1  christos #define X_I(i) (((i) >> 13) & 1)
     85  1.1  christos /* Sign extension macros.  */
     86  1.1  christos #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
     87  1.1  christos #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
     88  1.1  christos #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
     89  1.1  christos #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
     90  1.3  christos /* Macros to identify some instructions.  */
     91  1.3  christos /* RETURN (RETT in V8) */
     92  1.3  christos #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
     93  1.1  christos 
     94  1.1  christos /* Fetch the instruction at PC.  Instructions are always big-endian
     95  1.1  christos    even if the processor operates in little-endian mode.  */
     96  1.1  christos 
     97  1.1  christos unsigned long
     98  1.1  christos sparc_fetch_instruction (CORE_ADDR pc)
     99  1.1  christos {
    100  1.1  christos   gdb_byte buf[4];
    101  1.1  christos   unsigned long insn;
    102  1.1  christos   int i;
    103  1.1  christos 
    104  1.1  christos   /* If we can't read the instruction at PC, return zero.  */
    105  1.1  christos   if (target_read_memory (pc, buf, sizeof (buf)))
    106  1.1  christos     return 0;
    107  1.1  christos 
    108  1.1  christos   insn = 0;
    109  1.1  christos   for (i = 0; i < sizeof (buf); i++)
    110  1.1  christos     insn = (insn << 8) | buf[i];
    111  1.1  christos   return insn;
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos 
    116  1.1  christos /* Return non-zero if the instruction corresponding to PC is an "unimp"
    117  1.1  christos    instruction.  */
    118  1.1  christos 
    119  1.1  christos static int
    120  1.1  christos sparc_is_unimp_insn (CORE_ADDR pc)
    121  1.1  christos {
    122  1.1  christos   const unsigned long insn = sparc_fetch_instruction (pc);
    123  1.1  christos 
    124  1.1  christos   return ((insn & 0xc1c00000) == 0);
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos /* Return non-zero if the instruction corresponding to PC is an
    128  1.1  christos    "annulled" branch, i.e. the annul bit is set.  */
    129  1.1  christos 
    130  1.1  christos int
    131  1.1  christos sparc_is_annulled_branch_insn (CORE_ADDR pc)
    132  1.1  christos {
    133  1.1  christos   /* The branch instructions featuring an annul bit can be identified
    134  1.1  christos      by the following bit patterns:
    135  1.1  christos 
    136  1.1  christos      OP=0
    137  1.1  christos       OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
    138  1.1  christos       OP2=2: Branch on Integer Condition Codes (Bcc).
    139  1.1  christos       OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
    140  1.1  christos       OP2=6: Branch on FP Condition Codes (FBcc).
    141  1.1  christos       OP2=3 && Bit28=0:
    142  1.1  christos              Branch on Integer Register with Prediction (BPr).
    143  1.1  christos 
    144  1.1  christos      This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
    145  1.1  christos      coprocessor branch instructions (Op2=7).  */
    146  1.1  christos 
    147  1.1  christos   const unsigned long insn = sparc_fetch_instruction (pc);
    148  1.1  christos   const unsigned op2 = X_OP2 (insn);
    149  1.1  christos 
    150  1.1  christos   if ((X_OP (insn) == 0)
    151  1.1  christos       && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
    152  1.1  christos 	  || ((op2 == 3) && ((insn & 0x10000000) == 0))))
    153  1.1  christos     return X_A (insn);
    154  1.1  christos   else
    155  1.1  christos     return 0;
    156  1.1  christos }
    157  1.1  christos 
    158  1.1  christos /* OpenBSD/sparc includes StackGhost, which according to the author's
    159  1.1  christos    website http://stackghost.cerias.purdue.edu "... transparently and
    160  1.1  christos    automatically protects applications' stack frames; more
    161  1.1  christos    specifically, it guards the return pointers.  The protection
    162  1.1  christos    mechanisms require no application source or binary modification and
    163  1.1  christos    imposes only a negligible performance penalty."
    164  1.1  christos 
    165  1.1  christos    The same website provides the following description of how
    166  1.1  christos    StackGhost works:
    167  1.1  christos 
    168  1.1  christos    "StackGhost interfaces with the kernel trap handler that would
    169  1.1  christos    normally write out registers to the stack and the handler that
    170  1.1  christos    would read them back in.  By XORing a cookie into the
    171  1.1  christos    return-address saved in the user stack when it is actually written
    172  1.1  christos    to the stack, and then XOR it out when the return-address is pulled
    173  1.1  christos    from the stack, StackGhost can cause attacker corrupted return
    174  1.1  christos    pointers to behave in a manner the attacker cannot predict.
    175  1.1  christos    StackGhost can also use several unused bits in the return pointer
    176  1.1  christos    to detect a smashed return pointer and abort the process."
    177  1.1  christos 
    178  1.1  christos    For GDB this means that whenever we're reading %i7 from a stack
    179  1.1  christos    frame's window save area, we'll have to XOR the cookie.
    180  1.1  christos 
    181  1.1  christos    More information on StackGuard can be found on in:
    182  1.1  christos 
    183  1.1  christos    Mike Frantzen and Mike Shuey.  "StackGhost: Hardware Facilitated
    184  1.1  christos    Stack Protection."  2001.  Published in USENIX Security Symposium
    185  1.1  christos    '01.  */
    186  1.1  christos 
    187  1.1  christos /* Fetch StackGhost Per-Process XOR cookie.  */
    188  1.1  christos 
    189  1.1  christos ULONGEST
    190  1.1  christos sparc_fetch_wcookie (struct gdbarch *gdbarch)
    191  1.1  christos {
    192  1.8  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    193  1.1  christos   struct target_ops *ops = current_top_target ();
    194  1.1  christos   gdb_byte buf[8];
    195  1.1  christos   int len;
    196  1.1  christos 
    197  1.1  christos   len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
    198  1.1  christos   if (len == -1)
    199  1.1  christos     return 0;
    200  1.1  christos 
    201  1.1  christos   /* We should have either an 32-bit or an 64-bit cookie.  */
    202  1.1  christos   gdb_assert (len == 4 || len == 8);
    203  1.1  christos 
    204  1.1  christos   return extract_unsigned_integer (buf, len, byte_order);
    205  1.1  christos }
    206  1.1  christos 
    207  1.1  christos 
    209  1.1  christos /* The functions on this page are intended to be used to classify
    210  1.1  christos    function arguments.  */
    211  1.1  christos 
    212  1.1  christos /* Check whether TYPE is "Integral or Pointer".  */
    213  1.1  christos 
    214  1.1  christos static int
    215  1.1  christos sparc_integral_or_pointer_p (const struct type *type)
    216  1.1  christos {
    217  1.9  christos   int len = TYPE_LENGTH (type);
    218  1.1  christos 
    219  1.1  christos   switch (type->code ())
    220  1.1  christos     {
    221  1.1  christos     case TYPE_CODE_INT:
    222  1.1  christos     case TYPE_CODE_BOOL:
    223  1.1  christos     case TYPE_CODE_CHAR:
    224  1.1  christos     case TYPE_CODE_ENUM:
    225  1.1  christos     case TYPE_CODE_RANGE:
    226  1.1  christos       /* We have byte, half-word, word and extended-word/doubleword
    227  1.1  christos 	 integral types.  The doubleword is an extension to the
    228  1.1  christos 	 original 32-bit ABI by the SCD 2.4.x.  */
    229  1.1  christos       return (len == 1 || len == 2 || len == 4 || len == 8);
    230  1.7  christos     case TYPE_CODE_PTR:
    231  1.1  christos     case TYPE_CODE_REF:
    232  1.1  christos     case TYPE_CODE_RVALUE_REF:
    233  1.1  christos       /* Allow either 32-bit or 64-bit pointers.  */
    234  1.1  christos       return (len == 4 || len == 8);
    235  1.1  christos     default:
    236  1.1  christos       break;
    237  1.1  christos     }
    238  1.1  christos 
    239  1.1  christos   return 0;
    240  1.1  christos }
    241  1.1  christos 
    242  1.1  christos /* Check whether TYPE is "Floating".  */
    243  1.1  christos 
    244  1.1  christos static int
    245  1.9  christos sparc_floating_p (const struct type *type)
    246  1.1  christos {
    247  1.1  christos   switch (type->code ())
    248  1.1  christos     {
    249  1.1  christos     case TYPE_CODE_FLT:
    250  1.1  christos       {
    251  1.1  christos 	int len = TYPE_LENGTH (type);
    252  1.1  christos 	return (len == 4 || len == 8 || len == 16);
    253  1.1  christos       }
    254  1.1  christos     default:
    255  1.1  christos       break;
    256  1.1  christos     }
    257  1.1  christos 
    258  1.1  christos   return 0;
    259  1.1  christos }
    260  1.1  christos 
    261  1.1  christos /* Check whether TYPE is "Complex Floating".  */
    262  1.1  christos 
    263  1.1  christos static int
    264  1.9  christos sparc_complex_floating_p (const struct type *type)
    265  1.1  christos {
    266  1.1  christos   switch (type->code ())
    267  1.1  christos     {
    268  1.1  christos     case TYPE_CODE_COMPLEX:
    269  1.1  christos       {
    270  1.1  christos 	int len = TYPE_LENGTH (type);
    271  1.1  christos 	return (len == 8 || len == 16 || len == 32);
    272  1.1  christos       }
    273  1.1  christos     default:
    274  1.1  christos       break;
    275  1.1  christos     }
    276  1.1  christos 
    277  1.1  christos   return 0;
    278  1.1  christos }
    279  1.1  christos 
    280  1.1  christos /* Check whether TYPE is "Structure or Union".
    281  1.1  christos 
    282  1.1  christos    In terms of Ada subprogram calls, arrays are treated the same as
    283  1.1  christos    struct and union types.  So this function also returns non-zero
    284  1.1  christos    for array types.  */
    285  1.1  christos 
    286  1.1  christos static int
    287  1.9  christos sparc_structure_or_union_p (const struct type *type)
    288  1.1  christos {
    289  1.1  christos   switch (type->code ())
    290  1.1  christos     {
    291  1.1  christos     case TYPE_CODE_STRUCT:
    292  1.1  christos     case TYPE_CODE_UNION:
    293  1.1  christos     case TYPE_CODE_ARRAY:
    294  1.1  christos       return 1;
    295  1.1  christos     default:
    296  1.1  christos       break;
    297  1.1  christos     }
    298  1.1  christos 
    299  1.1  christos   return 0;
    300  1.8  christos }
    301  1.8  christos 
    302  1.8  christos /* Return true if TYPE is returned by memory, false if returned by
    303  1.8  christos    register.  */
    304  1.8  christos 
    305  1.8  christos static bool
    306  1.9  christos sparc_structure_return_p (const struct type *type)
    307  1.8  christos {
    308  1.8  christos   if (type->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
    309  1.8  christos     {
    310  1.8  christos       /* Float vectors are always returned by memory.  */
    311  1.8  christos       if (sparc_floating_p (check_typedef (TYPE_TARGET_TYPE (type))))
    312  1.8  christos 	return true;
    313  1.8  christos       /* Integer vectors are returned by memory if the vector size
    314  1.8  christos 	 is greater than 8 bytes long.  */
    315  1.8  christos       return (TYPE_LENGTH (type) > 8);
    316  1.8  christos     }
    317  1.8  christos 
    318  1.8  christos   if (sparc_floating_p (type))
    319  1.8  christos     {
    320  1.8  christos       /* Floating point types are passed by register for size 4 and
    321  1.8  christos 	 8 bytes, and by memory for size 16 bytes.  */
    322  1.8  christos       return (TYPE_LENGTH (type) == 16);
    323  1.8  christos     }
    324  1.8  christos 
    325  1.8  christos   /* Other than that, only aggregates of all sizes get returned by
    326  1.8  christos      memory.  */
    327  1.8  christos   return sparc_structure_or_union_p (type);
    328  1.8  christos }
    329  1.8  christos 
    330  1.8  christos /* Return true if arguments of the given TYPE are passed by
    331  1.8  christos    memory; false if returned by register.  */
    332  1.8  christos 
    333  1.8  christos static bool
    334  1.9  christos sparc_arg_by_memory_p (const struct type *type)
    335  1.8  christos {
    336  1.8  christos   if (type->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (type))
    337  1.8  christos     {
    338  1.8  christos       /* Float vectors are always passed by memory.  */
    339  1.8  christos       if (sparc_floating_p (check_typedef (TYPE_TARGET_TYPE (type))))
    340  1.8  christos 	return true;
    341  1.8  christos       /* Integer vectors are passed by memory if the vector size
    342  1.8  christos 	 is greater than 8 bytes long.  */
    343  1.8  christos       return (TYPE_LENGTH (type) > 8);
    344  1.8  christos     }
    345  1.8  christos 
    346  1.8  christos   /* Floats are passed by register for size 4 and 8 bytes, and by memory
    347  1.8  christos      for size 16 bytes.  */
    348  1.8  christos   if (sparc_floating_p (type))
    349  1.8  christos     return (TYPE_LENGTH (type) == 16);
    350  1.8  christos 
    351  1.8  christos   /* Complex floats and aggregates of all sizes are passed by memory.  */
    352  1.8  christos   if (sparc_complex_floating_p (type) || sparc_structure_or_union_p (type))
    353  1.8  christos     return true;
    354  1.8  christos 
    355  1.8  christos   /* Everything else gets passed by register.  */
    356  1.8  christos   return false;
    357  1.1  christos }
    358  1.7  christos 
    359  1.7  christos /* Register information.  */
    360  1.7  christos #define SPARC32_FPU_REGISTERS                             \
    361  1.7  christos   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",         \
    362  1.7  christos   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",   \
    363  1.7  christos   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
    364  1.7  christos   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
    365  1.7  christos #define SPARC32_CP0_REGISTERS \
    366  1.7  christos   "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
    367  1.7  christos 
    368  1.7  christos static const char *sparc_core_register_names[] = { SPARC_CORE_REGISTERS };
    369  1.1  christos static const char *sparc32_fpu_register_names[] = { SPARC32_FPU_REGISTERS };
    370  1.1  christos static const char *sparc32_cp0_register_names[] = { SPARC32_CP0_REGISTERS };
    371  1.1  christos 
    372  1.7  christos static const char *sparc32_register_names[] =
    373  1.7  christos {
    374  1.7  christos   SPARC_CORE_REGISTERS,
    375  1.1  christos   SPARC32_FPU_REGISTERS,
    376  1.1  christos   SPARC32_CP0_REGISTERS
    377  1.1  christos };
    378  1.1  christos 
    379  1.1  christos /* Total number of registers.  */
    380  1.1  christos #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
    381  1.1  christos 
    382  1.1  christos /* We provide the aliases %d0..%d30 for the floating registers as
    383  1.1  christos    "psuedo" registers.  */
    384  1.1  christos 
    385  1.1  christos static const char *sparc32_pseudo_register_names[] =
    386  1.1  christos {
    387  1.1  christos   "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
    388  1.1  christos   "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
    389  1.1  christos };
    390  1.1  christos 
    391  1.1  christos /* Total number of pseudo registers.  */
    392  1.7  christos #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
    393  1.7  christos 
    394  1.7  christos /* Return the name of pseudo register REGNUM.  */
    395  1.7  christos 
    396  1.7  christos static const char *
    397  1.7  christos sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
    398  1.7  christos {
    399  1.7  christos   regnum -= gdbarch_num_regs (gdbarch);
    400  1.7  christos 
    401  1.7  christos   if (regnum < SPARC32_NUM_PSEUDO_REGS)
    402  1.7  christos     return sparc32_pseudo_register_names[regnum];
    403  1.7  christos 
    404  1.7  christos   internal_error (__FILE__, __LINE__,
    405  1.7  christos                   _("sparc32_pseudo_register_name: bad register number %d"),
    406  1.7  christos                   regnum);
    407  1.1  christos }
    408  1.1  christos 
    409  1.1  christos /* Return the name of register REGNUM.  */
    410  1.1  christos 
    411  1.1  christos static const char *
    412  1.7  christos sparc32_register_name (struct gdbarch *gdbarch, int regnum)
    413  1.7  christos {
    414  1.7  christos   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
    415  1.7  christos     return tdesc_register_name (gdbarch, regnum);
    416  1.1  christos 
    417  1.1  christos   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
    418  1.7  christos     return sparc32_register_names[regnum];
    419  1.1  christos 
    420  1.1  christos   return sparc32_pseudo_register_name (gdbarch, regnum);
    421  1.1  christos }
    422  1.1  christos 
    423  1.1  christos /* Construct types for ISA-specific registers.  */
    425  1.1  christos 
    426  1.1  christos static struct type *
    427  1.1  christos sparc_psr_type (struct gdbarch *gdbarch)
    428  1.1  christos {
    429  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    430  1.1  christos 
    431  1.1  christos   if (!tdep->sparc_psr_type)
    432  1.8  christos     {
    433  1.1  christos       struct type *type;
    434  1.1  christos 
    435  1.1  christos       type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 32);
    436  1.1  christos       append_flags_type_flag (type, 5, "ET");
    437  1.1  christos       append_flags_type_flag (type, 6, "PS");
    438  1.1  christos       append_flags_type_flag (type, 7, "S");
    439  1.1  christos       append_flags_type_flag (type, 12, "EF");
    440  1.1  christos       append_flags_type_flag (type, 13, "EC");
    441  1.1  christos 
    442  1.1  christos       tdep->sparc_psr_type = type;
    443  1.1  christos     }
    444  1.1  christos 
    445  1.1  christos   return tdep->sparc_psr_type;
    446  1.1  christos }
    447  1.1  christos 
    448  1.1  christos static struct type *
    449  1.1  christos sparc_fsr_type (struct gdbarch *gdbarch)
    450  1.1  christos {
    451  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    452  1.1  christos 
    453  1.1  christos   if (!tdep->sparc_fsr_type)
    454  1.8  christos     {
    455  1.1  christos       struct type *type;
    456  1.1  christos 
    457  1.1  christos       type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 32);
    458  1.1  christos       append_flags_type_flag (type, 0, "NXA");
    459  1.1  christos       append_flags_type_flag (type, 1, "DZA");
    460  1.1  christos       append_flags_type_flag (type, 2, "UFA");
    461  1.1  christos       append_flags_type_flag (type, 3, "OFA");
    462  1.1  christos       append_flags_type_flag (type, 4, "NVA");
    463  1.1  christos       append_flags_type_flag (type, 5, "NXC");
    464  1.1  christos       append_flags_type_flag (type, 6, "DZC");
    465  1.1  christos       append_flags_type_flag (type, 7, "UFC");
    466  1.1  christos       append_flags_type_flag (type, 8, "OFC");
    467  1.1  christos       append_flags_type_flag (type, 9, "NVC");
    468  1.1  christos       append_flags_type_flag (type, 22, "NS");
    469  1.1  christos       append_flags_type_flag (type, 23, "NXM");
    470  1.1  christos       append_flags_type_flag (type, 24, "DZM");
    471  1.1  christos       append_flags_type_flag (type, 25, "UFM");
    472  1.1  christos       append_flags_type_flag (type, 26, "OFM");
    473  1.1  christos       append_flags_type_flag (type, 27, "NVM");
    474  1.1  christos 
    475  1.1  christos       tdep->sparc_fsr_type = type;
    476  1.1  christos     }
    477  1.1  christos 
    478  1.1  christos   return tdep->sparc_fsr_type;
    479  1.7  christos }
    480  1.7  christos 
    481  1.7  christos /* Return the GDB type object for the "standard" data type of data in
    482  1.7  christos    pseudo register REGNUM.  */
    483  1.7  christos 
    484  1.7  christos static struct type *
    485  1.7  christos sparc32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
    486  1.7  christos {
    487  1.7  christos   regnum -= gdbarch_num_regs (gdbarch);
    488  1.7  christos 
    489  1.7  christos   if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
    490  1.7  christos     return builtin_type (gdbarch)->builtin_double;
    491  1.7  christos 
    492  1.7  christos   internal_error (__FILE__, __LINE__,
    493  1.7  christos                   _("sparc32_pseudo_register_type: bad register number %d"),
    494  1.7  christos                   regnum);
    495  1.1  christos }
    496  1.1  christos 
    497  1.1  christos /* Return the GDB type object for the "standard" data type of data in
    498  1.1  christos    register REGNUM.  */
    499  1.1  christos 
    500  1.7  christos static struct type *
    501  1.7  christos sparc32_register_type (struct gdbarch *gdbarch, int regnum)
    502  1.7  christos {
    503  1.1  christos   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
    504  1.1  christos     return tdesc_register_type (gdbarch, regnum);
    505  1.1  christos 
    506  1.1  christos   if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
    507  1.1  christos     return builtin_type (gdbarch)->builtin_float;
    508  1.1  christos 
    509  1.1  christos   if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
    510  1.1  christos     return builtin_type (gdbarch)->builtin_data_ptr;
    511  1.1  christos 
    512  1.1  christos   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
    513  1.1  christos     return builtin_type (gdbarch)->builtin_func_ptr;
    514  1.1  christos 
    515  1.1  christos   if (regnum == SPARC32_PSR_REGNUM)
    516  1.1  christos     return sparc_psr_type (gdbarch);
    517  1.1  christos 
    518  1.7  christos   if (regnum == SPARC32_FSR_REGNUM)
    519  1.7  christos     return sparc_fsr_type (gdbarch);
    520  1.7  christos 
    521  1.1  christos   if (regnum >= gdbarch_num_regs (gdbarch))
    522  1.1  christos     return sparc32_pseudo_register_type (gdbarch, regnum);
    523  1.1  christos 
    524  1.1  christos   return builtin_type (gdbarch)->builtin_int32;
    525  1.1  christos }
    526  1.8  christos 
    527  1.1  christos static enum register_status
    528  1.1  christos sparc32_pseudo_register_read (struct gdbarch *gdbarch,
    529  1.1  christos 			      readable_regcache *regcache,
    530  1.1  christos 			      int regnum, gdb_byte *buf)
    531  1.7  christos {
    532  1.1  christos   enum register_status status;
    533  1.1  christos 
    534  1.1  christos   regnum -= gdbarch_num_regs (gdbarch);
    535  1.8  christos   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
    536  1.1  christos 
    537  1.8  christos   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
    538  1.1  christos   status = regcache->raw_read (regnum, buf);
    539  1.1  christos   if (status == REG_VALID)
    540  1.1  christos     status = regcache->raw_read (regnum + 1, buf + 4);
    541  1.1  christos   return status;
    542  1.1  christos }
    543  1.1  christos 
    544  1.1  christos static void
    545  1.1  christos sparc32_pseudo_register_write (struct gdbarch *gdbarch,
    546  1.7  christos 			       struct regcache *regcache,
    547  1.1  christos 			       int regnum, const gdb_byte *buf)
    548  1.1  christos {
    549  1.1  christos   regnum -= gdbarch_num_regs (gdbarch);
    550  1.8  christos   gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
    551  1.8  christos 
    552  1.1  christos   regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
    553  1.1  christos   regcache->raw_write (regnum, buf);
    554  1.5  christos   regcache->raw_write (regnum + 1, buf + 4);
    555  1.3  christos }
    556  1.3  christos 
    557  1.5  christos /* Implement the stack_frame_destroyed_p gdbarch method.  */
    559  1.3  christos 
    560  1.3  christos int
    561  1.3  christos sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
    562  1.3  christos {
    563  1.3  christos   /* This function must return true if we are one instruction after an
    564  1.3  christos      instruction that destroyed the stack frame of the current
    565  1.3  christos      function.  The SPARC instructions used to restore the callers
    566  1.3  christos      stack frame are RESTORE and RETURN/RETT.
    567  1.3  christos 
    568  1.3  christos      Of these RETURN/RETT is a branch instruction and thus we return
    569  1.3  christos      true if we are in its delay slot.
    570  1.3  christos 
    571  1.3  christos      RESTORE is almost always found in the delay slot of a branch
    572  1.3  christos      instruction that transfers control to the caller, such as JMPL.
    573  1.3  christos      Thus the next instruction is in the caller frame and we don't
    574  1.3  christos      need to do anything about it.  */
    575  1.3  christos 
    576  1.3  christos   unsigned int insn = sparc_fetch_instruction (pc - 4);
    577  1.1  christos 
    578  1.1  christos   return X_RETTURN (insn);
    579  1.1  christos }
    580  1.1  christos 
    581  1.1  christos 
    583  1.1  christos static CORE_ADDR
    584  1.1  christos sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
    585  1.1  christos {
    586  1.1  christos   /* The ABI requires double-word alignment.  */
    587  1.1  christos   return address & ~0x7;
    588  1.1  christos }
    589  1.1  christos 
    590  1.1  christos static CORE_ADDR
    591  1.1  christos sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
    592  1.1  christos 			 CORE_ADDR funcaddr,
    593  1.1  christos 			 struct value **args, int nargs,
    594  1.1  christos 			 struct type *value_type,
    595  1.1  christos 			 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
    596  1.1  christos 			 struct regcache *regcache)
    597  1.1  christos {
    598  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    599  1.1  christos 
    600  1.1  christos   *bp_addr = sp - 4;
    601  1.1  christos   *real_pc = funcaddr;
    602  1.1  christos 
    603  1.1  christos   if (using_struct_return (gdbarch, NULL, value_type))
    604  1.1  christos     {
    605  1.1  christos       gdb_byte buf[4];
    606  1.1  christos 
    607  1.1  christos       /* This is an UNIMP instruction.  */
    608  1.1  christos       store_unsigned_integer (buf, 4, byte_order,
    609  1.1  christos 			      TYPE_LENGTH (value_type) & 0x1fff);
    610  1.1  christos       write_memory (sp - 8, buf, 4);
    611  1.1  christos       return sp - 8;
    612  1.1  christos     }
    613  1.1  christos 
    614  1.1  christos   return sp - 4;
    615  1.8  christos }
    616  1.8  christos 
    617  1.1  christos static CORE_ADDR
    618  1.8  christos sparc32_store_arguments (struct regcache *regcache, int nargs,
    619  1.1  christos 			 struct value **args, CORE_ADDR sp,
    620  1.1  christos 			 function_call_return_method return_method,
    621  1.1  christos 			 CORE_ADDR struct_addr)
    622  1.1  christos {
    623  1.1  christos   struct gdbarch *gdbarch = regcache->arch ();
    624  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    625  1.1  christos   /* Number of words in the "parameter array".  */
    626  1.1  christos   int num_elements = 0;
    627  1.1  christos   int element = 0;
    628  1.1  christos   int i;
    629  1.1  christos 
    630  1.8  christos   for (i = 0; i < nargs; i++)
    631  1.1  christos     {
    632  1.1  christos       struct type *type = value_type (args[i]);
    633  1.1  christos       int len = TYPE_LENGTH (type);
    634  1.1  christos 
    635  1.1  christos       if (sparc_arg_by_memory_p (type))
    636  1.1  christos 	{
    637  1.1  christos 	  /* Structure, Union and Quad-Precision Arguments.  */
    638  1.1  christos 	  sp -= len;
    639  1.1  christos 
    640  1.1  christos 	  /* Use doubleword alignment for these values.  That's always
    641  1.1  christos              correct, and wasting a few bytes shouldn't be a problem.  */
    642  1.1  christos 	  sp &= ~0x7;
    643  1.1  christos 
    644  1.1  christos 	  write_memory (sp, value_contents (args[i]), len);
    645  1.1  christos 	  args[i] = value_from_pointer (lookup_pointer_type (type), sp);
    646  1.1  christos 	  num_elements++;
    647  1.1  christos 	}
    648  1.1  christos       else if (sparc_floating_p (type))
    649  1.1  christos 	{
    650  1.1  christos 	  /* Floating arguments.  */
    651  1.8  christos 	  gdb_assert (len == 4 || len == 8);
    652  1.1  christos 	  num_elements += (len / 4);
    653  1.1  christos 	}
    654  1.1  christos       else
    655  1.1  christos 	{
    656  1.1  christos 	  /* Arguments passed via the General Purpose Registers.  */
    657  1.7  christos 	  num_elements += ((len + 3) / 4);
    658  1.1  christos 	}
    659  1.1  christos     }
    660  1.1  christos 
    661  1.1  christos   /* Always allocate at least six words.  */
    662  1.1  christos   sp -= std::max (6, num_elements) * 4;
    663  1.1  christos 
    664  1.1  christos   /* The psABI says that "Software convention requires space for the
    665  1.1  christos      struct/union return value pointer, even if the word is unused."  */
    666  1.1  christos   sp -= 4;
    667  1.1  christos 
    668  1.1  christos   /* The psABI says that "Although software convention and the
    669  1.1  christos      operating system require every stack frame to be doubleword
    670  1.1  christos      aligned."  */
    671  1.1  christos   sp &= ~0x7;
    672  1.1  christos 
    673  1.8  christos   for (i = 0; i < nargs; i++)
    674  1.8  christos     {
    675  1.8  christos       const bfd_byte *valbuf = value_contents (args[i]);
    676  1.8  christos       struct type *type = value_type (args[i]);
    677  1.8  christos       int len = TYPE_LENGTH (type);
    678  1.8  christos       gdb_byte buf[4];
    679  1.8  christos 
    680  1.8  christos       if (len < 4)
    681  1.8  christos         {
    682  1.1  christos           memset (buf, 0, 4 - len);
    683  1.1  christos           memcpy (buf + 4 - len, valbuf, len);
    684  1.1  christos           valbuf = buf;
    685  1.1  christos           len = 4;
    686  1.1  christos         }
    687  1.1  christos 
    688  1.1  christos       gdb_assert (len == 4 || len == 8);
    689  1.8  christos 
    690  1.1  christos       if (element < 6)
    691  1.8  christos 	{
    692  1.1  christos 	  int regnum = SPARC_O0_REGNUM + element;
    693  1.1  christos 
    694  1.1  christos 	  regcache->cooked_write (regnum, valbuf);
    695  1.1  christos 	  if (len > 4 && element < 5)
    696  1.1  christos 	    regcache->cooked_write (regnum + 1, valbuf + 4);
    697  1.1  christos 	}
    698  1.1  christos 
    699  1.1  christos       /* Always store the argument in memory.  */
    700  1.1  christos       write_memory (sp + 4 + element * 4, valbuf, len);
    701  1.8  christos       element += len / 4;
    702  1.1  christos     }
    703  1.1  christos 
    704  1.1  christos   gdb_assert (element == num_elements);
    705  1.1  christos 
    706  1.1  christos   if (return_method == return_method_struct)
    707  1.1  christos     {
    708  1.1  christos       gdb_byte buf[4];
    709  1.1  christos 
    710  1.1  christos       store_unsigned_integer (buf, 4, byte_order, struct_addr);
    711  1.1  christos       write_memory (sp, buf, 4);
    712  1.1  christos     }
    713  1.1  christos 
    714  1.1  christos   return sp;
    715  1.1  christos }
    716  1.8  christos 
    717  1.8  christos static CORE_ADDR
    718  1.1  christos sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
    719  1.8  christos 			 struct regcache *regcache, CORE_ADDR bp_addr,
    720  1.8  christos 			 int nargs, struct value **args, CORE_ADDR sp,
    721  1.1  christos 			 function_call_return_method return_method,
    722  1.1  christos 			 CORE_ADDR struct_addr)
    723  1.1  christos {
    724  1.1  christos   CORE_ADDR call_pc = (return_method == return_method_struct
    725  1.1  christos 		       ? (bp_addr - 12) : (bp_addr - 8));
    726  1.8  christos 
    727  1.8  christos   /* Set return address.  */
    728  1.1  christos   regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
    729  1.1  christos 
    730  1.1  christos   /* Set up function arguments.  */
    731  1.1  christos   sp = sparc32_store_arguments (regcache, nargs, args, sp, return_method,
    732  1.1  christos 				struct_addr);
    733  1.1  christos 
    734  1.1  christos   /* Allocate the 16-word window save area.  */
    735  1.1  christos   sp -= 16 * 4;
    736  1.1  christos 
    737  1.1  christos   /* Stack should be doubleword aligned at this point.  */
    738  1.1  christos   gdb_assert (sp % 8 == 0);
    739  1.1  christos 
    740  1.1  christos   /* Finally, update the stack pointer.  */
    741  1.1  christos   regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
    742  1.1  christos 
    743  1.1  christos   return sp;
    744  1.1  christos }
    745  1.1  christos 
    746  1.1  christos 
    748  1.1  christos /* Use the program counter to determine the contents and size of a
    749  1.7  christos    breakpoint instruction.  Return a pointer to a string of bytes that
    750  1.1  christos    encode a breakpoint instruction, store the length of the string in
    751  1.1  christos    *LEN and optionally adjust *PC to point to the correct memory
    752  1.1  christos    location for inserting the breakpoint.  */
    753  1.1  christos constexpr gdb_byte sparc_break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
    754  1.1  christos 
    755  1.1  christos typedef BP_MANIPULATION (sparc_break_insn) sparc_breakpoint;
    756  1.1  christos 
    757  1.1  christos 
    759  1.1  christos /* Allocate and initialize a frame cache.  */
    760  1.1  christos 
    761  1.1  christos static struct sparc_frame_cache *
    762  1.1  christos sparc_alloc_frame_cache (void)
    763  1.1  christos {
    764  1.1  christos   struct sparc_frame_cache *cache;
    765  1.1  christos 
    766  1.1  christos   cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
    767  1.1  christos 
    768  1.1  christos   /* Base address.  */
    769  1.1  christos   cache->base = 0;
    770  1.1  christos   cache->pc = 0;
    771  1.1  christos 
    772  1.1  christos   /* Frameless until proven otherwise.  */
    773  1.1  christos   cache->frameless_p = 1;
    774  1.1  christos   cache->frame_offset = 0;
    775  1.1  christos   cache->saved_regs_mask = 0;
    776  1.1  christos   cache->copied_regs_mask = 0;
    777  1.1  christos   cache->struct_return_p = 0;
    778  1.1  christos 
    779  1.1  christos   return cache;
    780  1.1  christos }
    781  1.1  christos 
    782  1.1  christos /* GCC generates several well-known sequences of instructions at the begining
    783  1.1  christos    of each function prologue when compiling with -fstack-check.  If one of
    784  1.1  christos    such sequences starts at START_PC, then return the address of the
    785  1.1  christos    instruction immediately past this sequence.  Otherwise, return START_PC.  */
    786  1.1  christos 
    787  1.1  christos static CORE_ADDR
    788  1.1  christos sparc_skip_stack_check (const CORE_ADDR start_pc)
    789  1.1  christos {
    790  1.1  christos   CORE_ADDR pc = start_pc;
    791  1.1  christos   unsigned long insn;
    792  1.1  christos   int probing_loop = 0;
    793  1.1  christos 
    794  1.1  christos   /* With GCC, all stack checking sequences begin with the same two
    795  1.1  christos      instructions, plus an optional one in the case of a probing loop:
    796  1.1  christos 
    797  1.1  christos          sethi <some immediate>, %g1
    798  1.1  christos          sub %sp, %g1, %g1
    799  1.1  christos 
    800  1.1  christos      or:
    801  1.1  christos 
    802  1.1  christos          sethi <some immediate>, %g1
    803  1.1  christos          sethi <some immediate>, %g4
    804  1.1  christos          sub %sp, %g1, %g1
    805  1.1  christos 
    806  1.1  christos      or:
    807  1.1  christos 
    808  1.1  christos          sethi <some immediate>, %g1
    809  1.1  christos          sub %sp, %g1, %g1
    810  1.1  christos          sethi <some immediate>, %g4
    811  1.1  christos 
    812  1.1  christos      If the optional instruction is found (setting g4), assume that a
    813  1.1  christos      probing loop will follow.  */
    814  1.1  christos 
    815  1.1  christos   /* sethi <some immediate>, %g1 */
    816  1.1  christos   insn = sparc_fetch_instruction (pc);
    817  1.1  christos   pc = pc + 4;
    818  1.1  christos   if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
    819  1.1  christos     return start_pc;
    820  1.1  christos 
    821  1.1  christos   /* optional: sethi <some immediate>, %g4 */
    822  1.1  christos   insn = sparc_fetch_instruction (pc);
    823  1.1  christos   pc = pc + 4;
    824  1.1  christos   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
    825  1.1  christos     {
    826  1.1  christos       probing_loop = 1;
    827  1.1  christos       insn = sparc_fetch_instruction (pc);
    828  1.1  christos       pc = pc + 4;
    829  1.1  christos     }
    830  1.1  christos 
    831  1.1  christos   /* sub %sp, %g1, %g1 */
    832  1.1  christos   if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
    833  1.1  christos         && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
    834  1.1  christos     return start_pc;
    835  1.1  christos 
    836  1.1  christos   insn = sparc_fetch_instruction (pc);
    837  1.1  christos   pc = pc + 4;
    838  1.1  christos 
    839  1.1  christos   /* optional: sethi <some immediate>, %g4 */
    840  1.1  christos   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
    841  1.1  christos     {
    842  1.1  christos       probing_loop = 1;
    843  1.1  christos       insn = sparc_fetch_instruction (pc);
    844  1.1  christos       pc = pc + 4;
    845  1.1  christos     }
    846  1.1  christos 
    847  1.1  christos   /* First possible sequence:
    848  1.1  christos          [first two instructions above]
    849  1.1  christos          clr [%g1 - some immediate]  */
    850  1.1  christos 
    851  1.1  christos   /* clr [%g1 - some immediate]  */
    852  1.1  christos   if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    853  1.1  christos       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
    854  1.1  christos     {
    855  1.1  christos       /* Valid stack-check sequence, return the new PC.  */
    856  1.1  christos       return pc;
    857  1.1  christos     }
    858  1.1  christos 
    859  1.1  christos   /* Second possible sequence: A small number of probes.
    860  1.1  christos          [first two instructions above]
    861  1.1  christos          clr [%g1]
    862  1.1  christos          add   %g1, -<some immediate>, %g1
    863  1.1  christos          clr [%g1]
    864  1.1  christos          [repeat the two instructions above any (small) number of times]
    865  1.1  christos          clr [%g1 - some immediate]  */
    866  1.1  christos 
    867  1.1  christos   /* clr [%g1] */
    868  1.1  christos   else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
    869  1.1  christos       && X_RS1 (insn) == 1 && X_RD (insn) == 0)
    870  1.1  christos     {
    871  1.1  christos       while (1)
    872  1.1  christos         {
    873  1.1  christos           /* add %g1, -<some immediate>, %g1 */
    874  1.1  christos           insn = sparc_fetch_instruction (pc);
    875  1.1  christos           pc = pc + 4;
    876  1.1  christos           if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
    877  1.1  christos                 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
    878  1.1  christos             break;
    879  1.1  christos 
    880  1.1  christos           /* clr [%g1] */
    881  1.1  christos           insn = sparc_fetch_instruction (pc);
    882  1.1  christos           pc = pc + 4;
    883  1.1  christos           if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
    884  1.1  christos                 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
    885  1.1  christos             return start_pc;
    886  1.1  christos         }
    887  1.1  christos 
    888  1.1  christos       /* clr [%g1 - some immediate] */
    889  1.1  christos       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    890  1.1  christos             && X_RS1 (insn) == 1 && X_RD (insn) == 0))
    891  1.1  christos         return start_pc;
    892  1.1  christos 
    893  1.1  christos       /* We found a valid stack-check sequence, return the new PC.  */
    894  1.1  christos       return pc;
    895  1.1  christos     }
    896  1.1  christos 
    897  1.1  christos   /* Third sequence: A probing loop.
    898  1.1  christos          [first three instructions above]
    899  1.1  christos          sub  %g1, %g4, %g4
    900  1.1  christos          cmp  %g1, %g4
    901  1.1  christos          be  <disp>
    902  1.1  christos          add  %g1, -<some immediate>, %g1
    903  1.1  christos          ba  <disp>
    904  1.1  christos          clr  [%g1]
    905  1.1  christos 
    906  1.1  christos      And an optional last probe for the remainder:
    907  1.1  christos 
    908  1.1  christos          clr [%g4 - some immediate]  */
    909  1.1  christos 
    910  1.1  christos   if (probing_loop)
    911  1.1  christos     {
    912  1.1  christos       /* sub  %g1, %g4, %g4 */
    913  1.1  christos       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
    914  1.1  christos             && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
    915  1.1  christos         return start_pc;
    916  1.1  christos 
    917  1.1  christos       /* cmp  %g1, %g4 */
    918  1.1  christos       insn = sparc_fetch_instruction (pc);
    919  1.1  christos       pc = pc + 4;
    920  1.1  christos       if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
    921  1.1  christos             && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
    922  1.1  christos         return start_pc;
    923  1.1  christos 
    924  1.1  christos       /* be  <disp> */
    925  1.1  christos       insn = sparc_fetch_instruction (pc);
    926  1.1  christos       pc = pc + 4;
    927  1.1  christos       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
    928  1.1  christos         return start_pc;
    929  1.1  christos 
    930  1.1  christos       /* add  %g1, -<some immediate>, %g1 */
    931  1.1  christos       insn = sparc_fetch_instruction (pc);
    932  1.1  christos       pc = pc + 4;
    933  1.1  christos       if (!(X_OP (insn) == 2  && X_OP3(insn) == 0 && X_I(insn)
    934  1.1  christos             && X_RS1 (insn) == 1 && X_RD (insn) == 1))
    935  1.1  christos         return start_pc;
    936  1.1  christos 
    937  1.1  christos       /* ba  <disp> */
    938  1.1  christos       insn = sparc_fetch_instruction (pc);
    939  1.1  christos       pc = pc + 4;
    940  1.1  christos       if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
    941  1.1  christos         return start_pc;
    942  1.1  christos 
    943  1.1  christos       /* clr  [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
    944  1.1  christos       insn = sparc_fetch_instruction (pc);
    945  1.1  christos       pc = pc + 4;
    946  1.1  christos       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
    947  1.1  christos             && X_RD (insn) == 0 && X_RS1 (insn) == 1
    948  1.1  christos 	    && (!X_I(insn) || X_SIMM13 (insn) == 0)))
    949  1.1  christos         return start_pc;
    950  1.1  christos 
    951  1.1  christos       /* We found a valid stack-check sequence, return the new PC.  */
    952  1.1  christos 
    953  1.1  christos       /* optional: clr [%g4 - some immediate]  */
    954  1.1  christos       insn = sparc_fetch_instruction (pc);
    955  1.1  christos       pc = pc + 4;
    956  1.1  christos       if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
    957  1.1  christos             && X_RS1 (insn) == 4 && X_RD (insn) == 0))
    958  1.1  christos         return pc - 4;
    959  1.1  christos       else
    960  1.1  christos 	return pc;
    961  1.1  christos     }
    962  1.1  christos 
    963  1.1  christos   /* No stack check code in our prologue, return the start_pc.  */
    964  1.1  christos   return start_pc;
    965  1.1  christos }
    966  1.1  christos 
    967  1.1  christos /* Record the effect of a SAVE instruction on CACHE.  */
    968  1.1  christos 
    969  1.1  christos void
    970  1.1  christos sparc_record_save_insn (struct sparc_frame_cache *cache)
    971  1.1  christos {
    972  1.1  christos   /* The frame is set up.  */
    973  1.1  christos   cache->frameless_p = 0;
    974  1.1  christos 
    975  1.1  christos   /* The frame pointer contains the CFA.  */
    976  1.1  christos   cache->frame_offset = 0;
    977  1.1  christos 
    978  1.1  christos   /* The `local' and `in' registers are all saved.  */
    979  1.1  christos   cache->saved_regs_mask = 0xffff;
    980  1.1  christos 
    981  1.1  christos   /* The `out' registers are all renamed.  */
    982  1.1  christos   cache->copied_regs_mask = 0xff;
    983  1.1  christos }
    984  1.1  christos 
    985  1.1  christos /* Do a full analysis of the prologue at PC and update CACHE accordingly.
    986  1.1  christos    Bail out early if CURRENT_PC is reached.  Return the address where
    987  1.1  christos    the analysis stopped.
    988  1.1  christos 
    989  1.1  christos    We handle both the traditional register window model and the single
    990  1.1  christos    register window (aka flat) model.  */
    991  1.1  christos 
    992  1.1  christos CORE_ADDR
    993  1.1  christos sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
    994  1.1  christos 			CORE_ADDR current_pc, struct sparc_frame_cache *cache)
    995  1.1  christos {
    996  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    997  1.1  christos   unsigned long insn;
    998  1.1  christos   int offset = 0;
    999  1.1  christos   int dest = -1;
   1000  1.1  christos 
   1001  1.1  christos   pc = sparc_skip_stack_check (pc);
   1002  1.1  christos 
   1003  1.1  christos   if (current_pc <= pc)
   1004  1.1  christos     return current_pc;
   1005  1.1  christos 
   1006  1.1  christos   /* We have to handle to "Procedure Linkage Table" (PLT) special.  On
   1007  1.1  christos      SPARC the linker usually defines a symbol (typically
   1008  1.1  christos      _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
   1009  1.1  christos      This symbol makes us end up here with PC pointing at the start of
   1010  1.1  christos      the PLT and CURRENT_PC probably pointing at a PLT entry.  If we
   1011  1.1  christos      would do our normal prologue analysis, we would probably conclude
   1012  1.1  christos      that we've got a frame when in reality we don't, since the
   1013  1.1  christos      dynamic linker patches up the first PLT with some code that
   1014  1.1  christos      starts with a SAVE instruction.  Patch up PC such that it points
   1015  1.1  christos      at the start of our PLT entry.  */
   1016  1.1  christos   if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
   1017  1.1  christos     pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
   1018  1.1  christos 
   1019  1.1  christos   insn = sparc_fetch_instruction (pc);
   1020  1.1  christos 
   1021  1.1  christos   /* Recognize store insns and record their sources.  */
   1022  1.1  christos   while (X_OP (insn) == 3
   1023  1.1  christos 	 && (X_OP3 (insn) == 0x4     /* stw */
   1024  1.1  christos 	     || X_OP3 (insn) == 0x7  /* std */
   1025  1.1  christos 	     || X_OP3 (insn) == 0xe) /* stx */
   1026  1.1  christos 	 && X_RS1 (insn) == SPARC_SP_REGNUM)
   1027  1.1  christos     {
   1028  1.1  christos       int regnum = X_RD (insn);
   1029  1.1  christos 
   1030  1.1  christos       /* Recognize stores into the corresponding stack slots.  */
   1031  1.1  christos       if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
   1032  1.1  christos 	  && ((X_I (insn)
   1033  1.1  christos 	       && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
   1034  1.1  christos 				      ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
   1035  1.1  christos 				      : (regnum - SPARC_L0_REGNUM) * 4))
   1036  1.1  christos 	      || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
   1037  1.1  christos 	{
   1038  1.1  christos 	  cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
   1039  1.1  christos 	  if (X_OP3 (insn) == 0x7)
   1040  1.1  christos 	    cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
   1041  1.1  christos 	}
   1042  1.1  christos 
   1043  1.1  christos       offset += 4;
   1044  1.1  christos 
   1045  1.1  christos       insn = sparc_fetch_instruction (pc + offset);
   1046  1.1  christos     }
   1047  1.1  christos 
   1048  1.1  christos   /* Recognize a SETHI insn and record its destination.  */
   1049  1.1  christos   if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
   1050  1.1  christos     {
   1051  1.1  christos       dest = X_RD (insn);
   1052  1.1  christos       offset += 4;
   1053  1.1  christos 
   1054  1.1  christos       insn = sparc_fetch_instruction (pc + offset);
   1055  1.1  christos     }
   1056  1.1  christos 
   1057  1.1  christos   /* Allow for an arithmetic operation on DEST or %g1.  */
   1058  1.1  christos   if (X_OP (insn) == 2 && X_I (insn)
   1059  1.1  christos       && (X_RD (insn) == 1 || X_RD (insn) == dest))
   1060  1.1  christos     {
   1061  1.1  christos       offset += 4;
   1062  1.1  christos 
   1063  1.1  christos       insn = sparc_fetch_instruction (pc + offset);
   1064  1.1  christos     }
   1065  1.1  christos 
   1066  1.1  christos   /* Check for the SAVE instruction that sets up the frame.  */
   1067  1.1  christos   if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
   1068  1.1  christos     {
   1069  1.1  christos       sparc_record_save_insn (cache);
   1070  1.1  christos       offset += 4;
   1071  1.1  christos       return pc + offset;
   1072  1.1  christos     }
   1073  1.1  christos 
   1074  1.1  christos   /* Check for an arithmetic operation on %sp.  */
   1075  1.1  christos   if (X_OP (insn) == 2
   1076  1.1  christos       && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
   1077  1.1  christos       && X_RS1 (insn) == SPARC_SP_REGNUM
   1078  1.1  christos       && X_RD (insn) == SPARC_SP_REGNUM)
   1079  1.1  christos     {
   1080  1.1  christos       if (X_I (insn))
   1081  1.1  christos 	{
   1082  1.1  christos 	  cache->frame_offset = X_SIMM13 (insn);
   1083  1.1  christos 	  if (X_OP3 (insn) == 0)
   1084  1.1  christos 	    cache->frame_offset = -cache->frame_offset;
   1085  1.1  christos 	}
   1086  1.1  christos       offset += 4;
   1087  1.1  christos 
   1088  1.1  christos       insn = sparc_fetch_instruction (pc + offset);
   1089  1.1  christos 
   1090  1.1  christos       /* Check for an arithmetic operation that sets up the frame.  */
   1091  1.1  christos       if (X_OP (insn) == 2
   1092  1.1  christos 	  && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
   1093  1.1  christos 	  && X_RS1 (insn) == SPARC_SP_REGNUM
   1094  1.1  christos 	  && X_RD (insn) == SPARC_FP_REGNUM)
   1095  1.1  christos 	{
   1096  1.1  christos 	  cache->frameless_p = 0;
   1097  1.1  christos 	  cache->frame_offset = 0;
   1098  1.1  christos 	  /* We could check that the amount subtracted to %sp above is the
   1099  1.1  christos 	     same as the one added here, but this seems superfluous.  */
   1100  1.1  christos 	  cache->copied_regs_mask |= 0x40;
   1101  1.1  christos 	  offset += 4;
   1102  1.1  christos 
   1103  1.1  christos 	  insn = sparc_fetch_instruction (pc + offset);
   1104  1.1  christos 	}
   1105  1.1  christos 
   1106  1.1  christos       /* Check for a move (or) operation that copies the return register.  */
   1107  1.1  christos       if (X_OP (insn) == 2
   1108  1.1  christos 	  && X_OP3 (insn) == 0x2
   1109  1.1  christos 	  && !X_I (insn)
   1110  1.1  christos 	  && X_RS1 (insn) == SPARC_G0_REGNUM
   1111  1.1  christos 	  && X_RS2 (insn) == SPARC_O7_REGNUM
   1112  1.1  christos 	  && X_RD (insn) == SPARC_I7_REGNUM)
   1113  1.1  christos 	{
   1114  1.1  christos 	   cache->copied_regs_mask |= 0x80;
   1115  1.1  christos 	   offset += 4;
   1116  1.1  christos 	}
   1117  1.1  christos 
   1118  1.1  christos       return pc + offset;
   1119  1.1  christos     }
   1120  1.1  christos 
   1121  1.1  christos   return pc;
   1122  1.1  christos }
   1123  1.1  christos 
   1124  1.1  christos /* Return PC of first real instruction of the function starting at
   1125  1.1  christos    START_PC.  */
   1126  1.1  christos 
   1127  1.1  christos static CORE_ADDR
   1128  1.1  christos sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
   1129  1.1  christos {
   1130  1.1  christos   struct symtab_and_line sal;
   1131  1.1  christos   CORE_ADDR func_start, func_end;
   1132  1.1  christos   struct sparc_frame_cache cache;
   1133  1.1  christos 
   1134  1.1  christos   /* This is the preferred method, find the end of the prologue by
   1135  1.1  christos      using the debugging information.  */
   1136  1.1  christos   if (find_pc_partial_function (start_pc, NULL, &func_start, &func_end))
   1137  1.1  christos     {
   1138  1.1  christos       sal = find_pc_line (func_start, 0);
   1139  1.1  christos 
   1140  1.1  christos       if (sal.end < func_end
   1141  1.1  christos 	  && start_pc <= sal.end)
   1142  1.1  christos 	return sal.end;
   1143  1.1  christos     }
   1144  1.1  christos 
   1145  1.1  christos   start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
   1146  1.1  christos 
   1147  1.1  christos   /* The psABI says that "Although the first 6 words of arguments
   1148  1.1  christos      reside in registers, the standard stack frame reserves space for
   1149  1.1  christos      them.".  It also suggests that a function may use that space to
   1150  1.1  christos      "write incoming arguments 0 to 5" into that space, and that's
   1151  1.1  christos      indeed what GCC seems to be doing.  In that case GCC will
   1152  1.1  christos      generate debug information that points to the stack slots instead
   1153  1.1  christos      of the registers, so we should consider the instructions that
   1154  1.1  christos      write out these incoming arguments onto the stack.  */
   1155  1.1  christos 
   1156  1.1  christos   while (1)
   1157  1.1  christos     {
   1158  1.1  christos       unsigned long insn = sparc_fetch_instruction (start_pc);
   1159  1.1  christos 
   1160  1.1  christos       /* Recognize instructions that store incoming arguments into the
   1161  1.1  christos 	 corresponding stack slots.  */
   1162  1.1  christos       if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
   1163  1.1  christos 	  && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
   1164  1.1  christos 	{
   1165  1.1  christos 	  int regnum = X_RD (insn);
   1166  1.1  christos 
   1167  1.1  christos 	  /* Case of arguments still in %o[0..5].  */
   1168  1.1  christos 	  if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
   1169  1.1  christos 	      && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
   1170  1.1  christos 	      && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
   1171  1.1  christos 	    {
   1172  1.1  christos 	      start_pc += 4;
   1173  1.1  christos 	      continue;
   1174  1.1  christos 	    }
   1175  1.1  christos 
   1176  1.1  christos 	  /* Case of arguments copied into %i[0..5].  */
   1177  1.1  christos 	  if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
   1178  1.1  christos 	      && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
   1179  1.1  christos 	      && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
   1180  1.1  christos 	    {
   1181  1.1  christos 	      start_pc += 4;
   1182  1.1  christos 	      continue;
   1183  1.1  christos 	    }
   1184  1.1  christos 	}
   1185  1.1  christos 
   1186  1.1  christos       break;
   1187  1.1  christos     }
   1188  1.1  christos 
   1189  1.1  christos   return start_pc;
   1190  1.1  christos }
   1191  1.1  christos 
   1192  1.1  christos /* Normal frames.  */
   1193  1.6  christos 
   1194  1.1  christos struct sparc_frame_cache *
   1195  1.1  christos sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
   1196  1.1  christos {
   1197  1.1  christos   struct sparc_frame_cache *cache;
   1198  1.1  christos 
   1199  1.1  christos   if (*this_cache)
   1200  1.1  christos     return (struct sparc_frame_cache *) *this_cache;
   1201  1.1  christos 
   1202  1.1  christos   cache = sparc_alloc_frame_cache ();
   1203  1.1  christos   *this_cache = cache;
   1204  1.1  christos 
   1205  1.1  christos   cache->pc = get_frame_func (this_frame);
   1206  1.1  christos   if (cache->pc != 0)
   1207  1.1  christos     sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
   1208  1.1  christos 			    get_frame_pc (this_frame), cache);
   1209  1.1  christos 
   1210  1.1  christos   if (cache->frameless_p)
   1211  1.1  christos     {
   1212  1.1  christos       /* This function is frameless, so %fp (%i6) holds the frame
   1213  1.1  christos          pointer for our calling frame.  Use %sp (%o6) as this frame's
   1214  1.1  christos          base address.  */
   1215  1.1  christos       cache->base =
   1216  1.1  christos         get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
   1217  1.1  christos     }
   1218  1.1  christos   else
   1219  1.1  christos     {
   1220  1.1  christos       /* For normal frames, %fp (%i6) holds the frame pointer, the
   1221  1.1  christos          base address for the current stack frame.  */
   1222  1.1  christos       cache->base =
   1223  1.1  christos 	get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
   1224  1.1  christos     }
   1225  1.1  christos 
   1226  1.1  christos   cache->base += cache->frame_offset;
   1227  1.1  christos 
   1228  1.1  christos   if (cache->base & 1)
   1229  1.1  christos     cache->base += BIAS;
   1230  1.1  christos 
   1231  1.9  christos   return cache;
   1232  1.1  christos }
   1233  1.1  christos 
   1234  1.1  christos static int
   1235  1.1  christos sparc32_struct_return_from_sym (struct symbol *sym)
   1236  1.1  christos {
   1237  1.1  christos   struct type *type = check_typedef (SYMBOL_TYPE (sym));
   1238  1.1  christos   enum type_code code = type->code ();
   1239  1.1  christos 
   1240  1.1  christos   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
   1241  1.1  christos     {
   1242  1.1  christos       type = check_typedef (TYPE_TARGET_TYPE (type));
   1243  1.1  christos       if (sparc_structure_or_union_p (type)
   1244  1.1  christos 	  || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
   1245  1.1  christos 	return 1;
   1246  1.1  christos     }
   1247  1.1  christos 
   1248  1.1  christos   return 0;
   1249  1.1  christos }
   1250  1.1  christos 
   1251  1.6  christos struct sparc_frame_cache *
   1252  1.1  christos sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
   1253  1.1  christos {
   1254  1.1  christos   struct sparc_frame_cache *cache;
   1255  1.1  christos   struct symbol *sym;
   1256  1.1  christos 
   1257  1.1  christos   if (*this_cache)
   1258  1.1  christos     return (struct sparc_frame_cache *) *this_cache;
   1259  1.1  christos 
   1260  1.1  christos   cache = sparc_frame_cache (this_frame, this_cache);
   1261  1.1  christos 
   1262  1.1  christos   sym = find_pc_function (cache->pc);
   1263  1.1  christos   if (sym)
   1264  1.1  christos     {
   1265  1.1  christos       cache->struct_return_p = sparc32_struct_return_from_sym (sym);
   1266  1.1  christos     }
   1267  1.1  christos   else
   1268  1.1  christos     {
   1269  1.1  christos       /* There is no debugging information for this function to
   1270  1.1  christos          help us determine whether this function returns a struct
   1271  1.1  christos          or not.  So we rely on another heuristic which is to check
   1272  1.1  christos          the instruction at the return address and see if this is
   1273  1.1  christos          an "unimp" instruction.  If it is, then it is a struct-return
   1274  1.1  christos          function.  */
   1275  1.1  christos       CORE_ADDR pc;
   1276  1.1  christos       int regnum =
   1277  1.1  christos 	(cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
   1278  1.1  christos 
   1279  1.1  christos       pc = get_frame_register_unsigned (this_frame, regnum) + 8;
   1280  1.1  christos       if (sparc_is_unimp_insn (pc))
   1281  1.1  christos         cache->struct_return_p = 1;
   1282  1.1  christos     }
   1283  1.1  christos 
   1284  1.1  christos   return cache;
   1285  1.1  christos }
   1286  1.1  christos 
   1287  1.1  christos static void
   1288  1.1  christos sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
   1289  1.1  christos 		       struct frame_id *this_id)
   1290  1.1  christos {
   1291  1.1  christos   struct sparc_frame_cache *cache =
   1292  1.1  christos     sparc32_frame_cache (this_frame, this_cache);
   1293  1.1  christos 
   1294  1.1  christos   /* This marks the outermost frame.  */
   1295  1.1  christos   if (cache->base == 0)
   1296  1.1  christos     return;
   1297  1.1  christos 
   1298  1.1  christos   (*this_id) = frame_id_build (cache->base, cache->pc);
   1299  1.1  christos }
   1300  1.1  christos 
   1301  1.1  christos static struct value *
   1302  1.1  christos sparc32_frame_prev_register (struct frame_info *this_frame,
   1303  1.1  christos 			     void **this_cache, int regnum)
   1304  1.1  christos {
   1305  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   1306  1.1  christos   struct sparc_frame_cache *cache =
   1307  1.1  christos     sparc32_frame_cache (this_frame, this_cache);
   1308  1.1  christos 
   1309  1.1  christos   if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
   1310  1.1  christos     {
   1311  1.1  christos       CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
   1312  1.1  christos 
   1313  1.1  christos       /* If this functions has a Structure, Union or Quad-Precision
   1314  1.1  christos 	 return value, we have to skip the UNIMP instruction that encodes
   1315  1.1  christos 	 the size of the structure.  */
   1316  1.1  christos       if (cache->struct_return_p)
   1317  1.1  christos 	pc += 4;
   1318  1.1  christos 
   1319  1.1  christos       regnum =
   1320  1.1  christos 	(cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
   1321  1.1  christos       pc += get_frame_register_unsigned (this_frame, regnum) + 8;
   1322  1.1  christos       return frame_unwind_got_constant (this_frame, regnum, pc);
   1323  1.1  christos     }
   1324  1.1  christos 
   1325  1.1  christos   /* Handle StackGhost.  */
   1326  1.1  christos   {
   1327  1.1  christos     ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1328  1.1  christos 
   1329  1.1  christos     if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
   1330  1.1  christos       {
   1331  1.1  christos         CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
   1332  1.1  christos         ULONGEST i7;
   1333  1.1  christos 
   1334  1.1  christos         /* Read the value in from memory.  */
   1335  1.1  christos         i7 = get_frame_memory_unsigned (this_frame, addr, 4);
   1336  1.1  christos         return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
   1337  1.1  christos       }
   1338  1.1  christos   }
   1339  1.1  christos 
   1340  1.1  christos   /* The previous frame's `local' and `in' registers may have been saved
   1341  1.1  christos      in the register save area.  */
   1342  1.1  christos   if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
   1343  1.1  christos       && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
   1344  1.1  christos     {
   1345  1.1  christos       CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
   1346  1.1  christos 
   1347  1.1  christos       return frame_unwind_got_memory (this_frame, regnum, addr);
   1348  1.1  christos     }
   1349  1.1  christos 
   1350  1.1  christos   /* The previous frame's `out' registers may be accessible as the current
   1351  1.1  christos      frame's `in' registers.  */
   1352  1.1  christos   if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
   1353  1.1  christos       && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
   1354  1.1  christos     regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
   1355  1.1  christos 
   1356  1.1  christos   return frame_unwind_got_register (this_frame, regnum, regnum);
   1357  1.1  christos }
   1358  1.1  christos 
   1359  1.1  christos static const struct frame_unwind sparc32_frame_unwind =
   1360  1.1  christos {
   1361  1.1  christos   NORMAL_FRAME,
   1362  1.1  christos   default_frame_unwind_stop_reason,
   1363  1.1  christos   sparc32_frame_this_id,
   1364  1.1  christos   sparc32_frame_prev_register,
   1365  1.1  christos   NULL,
   1366  1.1  christos   default_frame_sniffer
   1367  1.1  christos };
   1368  1.1  christos 
   1369  1.1  christos 
   1371  1.1  christos static CORE_ADDR
   1372  1.1  christos sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
   1373  1.1  christos {
   1374  1.1  christos   struct sparc_frame_cache *cache =
   1375  1.1  christos     sparc32_frame_cache (this_frame, this_cache);
   1376  1.1  christos 
   1377  1.1  christos   return cache->base;
   1378  1.1  christos }
   1379  1.1  christos 
   1380  1.1  christos static const struct frame_base sparc32_frame_base =
   1381  1.1  christos {
   1382  1.1  christos   &sparc32_frame_unwind,
   1383  1.1  christos   sparc32_frame_base_address,
   1384  1.1  christos   sparc32_frame_base_address,
   1385  1.1  christos   sparc32_frame_base_address
   1386  1.1  christos };
   1387  1.1  christos 
   1388  1.1  christos static struct frame_id
   1389  1.1  christos sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
   1390  1.1  christos {
   1391  1.1  christos   CORE_ADDR sp;
   1392  1.1  christos 
   1393  1.1  christos   sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
   1394  1.1  christos   if (sp & 1)
   1395  1.1  christos     sp += BIAS;
   1396  1.1  christos   return frame_id_build (sp, get_frame_pc (this_frame));
   1397  1.1  christos }
   1398  1.1  christos 
   1399  1.1  christos 
   1401  1.1  christos /* Extract a function return value of TYPE from REGCACHE, and copy
   1402  1.8  christos    that into VALBUF.  */
   1403  1.1  christos 
   1404  1.8  christos static void
   1405  1.9  christos sparc32_extract_return_value (struct type *type, struct regcache *regcache,
   1406  1.1  christos 			      gdb_byte *valbuf)
   1407  1.1  christos {
   1408  1.8  christos   int len = TYPE_LENGTH (type);
   1409  1.1  christos   gdb_byte buf[32];
   1410  1.8  christos 
   1411  1.1  christos   gdb_assert (!sparc_structure_return_p (type));
   1412  1.1  christos 
   1413  1.8  christos   if (sparc_floating_p (type) || sparc_complex_floating_p (type)
   1414  1.8  christos       || type->code () == TYPE_CODE_ARRAY)
   1415  1.1  christos     {
   1416  1.1  christos       /* Floating return values.  */
   1417  1.1  christos       regcache->cooked_read (SPARC_F0_REGNUM, buf);
   1418  1.8  christos       if (len > 4)
   1419  1.8  christos 	regcache->cooked_read (SPARC_F1_REGNUM, buf + 4);
   1420  1.8  christos       if (len > 8)
   1421  1.8  christos 	{
   1422  1.1  christos 	  regcache->cooked_read (SPARC_F2_REGNUM, buf + 8);
   1423  1.1  christos 	  regcache->cooked_read (SPARC_F3_REGNUM, buf + 12);
   1424  1.1  christos 	}
   1425  1.1  christos       if (len > 16)
   1426  1.1  christos 	{
   1427  1.1  christos 	  regcache->cooked_read (SPARC_F4_REGNUM, buf + 16);
   1428  1.1  christos 	  regcache->cooked_read (SPARC_F5_REGNUM, buf + 20);
   1429  1.1  christos 	  regcache->cooked_read (SPARC_F6_REGNUM, buf + 24);
   1430  1.8  christos 	  regcache->cooked_read (SPARC_F7_REGNUM, buf + 28);
   1431  1.1  christos 	}
   1432  1.1  christos       memcpy (valbuf, buf, len);
   1433  1.8  christos     }
   1434  1.1  christos   else
   1435  1.1  christos     {
   1436  1.1  christos       /* Integral and pointer return values.  */
   1437  1.1  christos       gdb_assert (sparc_integral_or_pointer_p (type));
   1438  1.1  christos 
   1439  1.1  christos       regcache->cooked_read (SPARC_O0_REGNUM, buf);
   1440  1.1  christos       if (len > 4)
   1441  1.1  christos 	{
   1442  1.1  christos 	  regcache->cooked_read (SPARC_O1_REGNUM, buf + 4);
   1443  1.1  christos 	  gdb_assert (len == 8);
   1444  1.1  christos 	  memcpy (valbuf, buf, 8);
   1445  1.1  christos 	}
   1446  1.1  christos       else
   1447  1.1  christos 	{
   1448  1.1  christos 	  /* Just stripping off any unused bytes should preserve the
   1449  1.1  christos 	     signed-ness just fine.  */
   1450  1.1  christos 	  memcpy (valbuf, buf + 4 - len, len);
   1451  1.1  christos 	}
   1452  1.1  christos     }
   1453  1.1  christos }
   1454  1.8  christos 
   1455  1.1  christos /* Store the function return value of type TYPE from VALBUF into
   1456  1.8  christos    REGCACHE.  */
   1457  1.1  christos 
   1458  1.1  christos static void
   1459  1.1  christos sparc32_store_return_value (struct type *type, struct regcache *regcache,
   1460  1.1  christos 			    const gdb_byte *valbuf)
   1461  1.1  christos {
   1462  1.8  christos   int len = TYPE_LENGTH (type);
   1463  1.1  christos   gdb_byte buf[32];
   1464  1.8  christos 
   1465  1.1  christos   gdb_assert (!sparc_structure_return_p (type));
   1466  1.1  christos 
   1467  1.8  christos   if (sparc_floating_p (type) || sparc_complex_floating_p (type))
   1468  1.8  christos     {
   1469  1.1  christos       /* Floating return values.  */
   1470  1.1  christos       memcpy (buf, valbuf, len);
   1471  1.1  christos       regcache->cooked_write (SPARC_F0_REGNUM, buf);
   1472  1.8  christos       if (len > 4)
   1473  1.8  christos 	regcache->cooked_write (SPARC_F1_REGNUM, buf + 4);
   1474  1.8  christos       if (len > 8)
   1475  1.8  christos 	{
   1476  1.1  christos 	  regcache->cooked_write (SPARC_F2_REGNUM, buf + 8);
   1477  1.1  christos 	  regcache->cooked_write (SPARC_F3_REGNUM, buf + 12);
   1478  1.1  christos 	}
   1479  1.1  christos       if (len > 16)
   1480  1.1  christos 	{
   1481  1.1  christos 	  regcache->cooked_write (SPARC_F4_REGNUM, buf + 16);
   1482  1.1  christos 	  regcache->cooked_write (SPARC_F5_REGNUM, buf + 20);
   1483  1.1  christos 	  regcache->cooked_write (SPARC_F6_REGNUM, buf + 24);
   1484  1.1  christos 	  regcache->cooked_write (SPARC_F7_REGNUM, buf + 28);
   1485  1.1  christos 	}
   1486  1.1  christos     }
   1487  1.8  christos   else
   1488  1.1  christos     {
   1489  1.1  christos       /* Integral and pointer return values.  */
   1490  1.1  christos       gdb_assert (sparc_integral_or_pointer_p (type));
   1491  1.1  christos 
   1492  1.1  christos       if (len > 4)
   1493  1.1  christos 	{
   1494  1.8  christos 	  gdb_assert (len == 8);
   1495  1.1  christos 	  memcpy (buf, valbuf, 8);
   1496  1.1  christos 	  regcache->cooked_write (SPARC_O1_REGNUM, buf + 4);
   1497  1.1  christos 	}
   1498  1.1  christos       else
   1499  1.1  christos 	{
   1500  1.1  christos 	  /* ??? Do we need to do any sign-extension here?  */
   1501  1.1  christos 	  memcpy (buf + 4 - len, valbuf, len);
   1502  1.1  christos 	}
   1503  1.1  christos       regcache->cooked_write (SPARC_O0_REGNUM, buf);
   1504  1.1  christos     }
   1505  1.1  christos }
   1506  1.1  christos 
   1507  1.1  christos static enum return_value_convention
   1508  1.1  christos sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
   1509  1.1  christos 		      struct type *type, struct regcache *regcache,
   1510  1.1  christos 		      gdb_byte *readbuf, const gdb_byte *writebuf)
   1511  1.1  christos {
   1512  1.8  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1513  1.1  christos 
   1514  1.1  christos   /* The psABI says that "...every stack frame reserves the word at
   1515  1.1  christos      %fp+64.  If a function returns a structure, union, or
   1516  1.1  christos      quad-precision value, this word should hold the address of the
   1517  1.1  christos      object into which the return value should be copied."  This
   1518  1.1  christos      guarantees that we can always find the return value, not just
   1519  1.1  christos      before the function returns.  */
   1520  1.1  christos 
   1521  1.1  christos   if (sparc_structure_return_p (type))
   1522  1.1  christos     {
   1523  1.1  christos       ULONGEST sp;
   1524  1.1  christos       CORE_ADDR addr;
   1525  1.1  christos 
   1526  1.1  christos       if (readbuf)
   1527  1.1  christos 	{
   1528  1.1  christos 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1529  1.1  christos 	  addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
   1530  1.1  christos 	  read_memory (addr, readbuf, TYPE_LENGTH (type));
   1531  1.1  christos 	}
   1532  1.1  christos       if (writebuf)
   1533  1.1  christos 	{
   1534  1.1  christos 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   1535  1.1  christos 	  addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
   1536  1.1  christos 	  write_memory (addr, writebuf, TYPE_LENGTH (type));
   1537  1.1  christos 	}
   1538  1.1  christos 
   1539  1.1  christos       return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
   1540  1.1  christos     }
   1541  1.1  christos 
   1542  1.1  christos   if (readbuf)
   1543  1.1  christos     sparc32_extract_return_value (type, regcache, readbuf);
   1544  1.1  christos   if (writebuf)
   1545  1.1  christos     sparc32_store_return_value (type, regcache, writebuf);
   1546  1.1  christos 
   1547  1.1  christos   return RETURN_VALUE_REGISTER_CONVENTION;
   1548  1.1  christos }
   1549  1.1  christos 
   1550  1.1  christos static int
   1551  1.1  christos sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
   1552  1.1  christos {
   1553  1.1  christos   return (sparc_structure_or_union_p (type)
   1554  1.1  christos 	  || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
   1555  1.1  christos 	  || sparc_complex_floating_p (type));
   1556  1.1  christos }
   1557  1.1  christos 
   1558  1.1  christos static int
   1559  1.1  christos sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
   1560  1.1  christos {
   1561  1.1  christos   CORE_ADDR pc = get_frame_address_in_block (this_frame);
   1562  1.1  christos   struct symbol *sym = find_pc_function (pc);
   1563  1.1  christos 
   1564  1.1  christos   if (sym)
   1565  1.1  christos     return sparc32_struct_return_from_sym (sym);
   1566  1.1  christos   return 0;
   1567  1.1  christos }
   1568  1.1  christos 
   1569  1.1  christos static void
   1570  1.1  christos sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
   1571  1.1  christos 			       struct dwarf2_frame_state_reg *reg,
   1572  1.1  christos 			       struct frame_info *this_frame)
   1573  1.1  christos {
   1574  1.1  christos   int off;
   1575  1.1  christos 
   1576  1.1  christos   switch (regnum)
   1577  1.1  christos     {
   1578  1.1  christos     case SPARC_G0_REGNUM:
   1579  1.1  christos       /* Since %g0 is always zero, there is no point in saving it, and
   1580  1.1  christos 	 people will be inclined omit it from the CFI.  Make sure we
   1581  1.1  christos 	 don't warn about that.  */
   1582  1.1  christos       reg->how = DWARF2_FRAME_REG_SAME_VALUE;
   1583  1.1  christos       break;
   1584  1.1  christos     case SPARC_SP_REGNUM:
   1585  1.1  christos       reg->how = DWARF2_FRAME_REG_CFA;
   1586  1.1  christos       break;
   1587  1.1  christos     case SPARC32_PC_REGNUM:
   1588  1.1  christos     case SPARC32_NPC_REGNUM:
   1589  1.1  christos       reg->how = DWARF2_FRAME_REG_RA_OFFSET;
   1590  1.1  christos       off = 8;
   1591  1.8  christos       if (sparc32_dwarf2_struct_return_p (this_frame))
   1592  1.8  christos 	off += 4;
   1593  1.8  christos       if (regnum == SPARC32_NPC_REGNUM)
   1594  1.8  christos 	off += 4;
   1595  1.8  christos       reg->loc.offset = off;
   1596  1.8  christos       break;
   1597  1.8  christos     }
   1598  1.8  christos }
   1599  1.8  christos 
   1600  1.8  christos /* Implement the execute_dwarf_cfa_vendor_op method.  */
   1601  1.8  christos 
   1602  1.8  christos static bool
   1603  1.8  christos sparc_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
   1604  1.8  christos 				   struct dwarf2_frame_state *fs)
   1605  1.8  christos {
   1606  1.8  christos   /* Only DW_CFA_GNU_window_save is expected on SPARC.  */
   1607  1.8  christos   if (op != DW_CFA_GNU_window_save)
   1608  1.8  christos     return false;
   1609  1.8  christos 
   1610  1.8  christos   uint64_t reg;
   1611  1.8  christos   int size = register_size (gdbarch, 0);
   1612  1.8  christos 
   1613  1.8  christos   fs->regs.alloc_regs (32);
   1614  1.8  christos   for (reg = 8; reg < 16; reg++)
   1615  1.8  christos     {
   1616  1.8  christos       fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
   1617  1.8  christos       fs->regs.reg[reg].loc.reg = reg + 16;
   1618  1.8  christos     }
   1619  1.1  christos   for (reg = 16; reg < 32; reg++)
   1620  1.1  christos     {
   1621  1.1  christos       fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
   1622  1.1  christos       fs->regs.reg[reg].loc.offset = (reg - 16) * size;
   1623  1.1  christos     }
   1624  1.1  christos 
   1625  1.7  christos   return true;
   1626  1.1  christos }
   1627  1.1  christos 
   1628  1.1  christos 
   1629  1.1  christos /* The SPARC Architecture doesn't have hardware single-step support,
   1631  1.1  christos    and most operating systems don't implement it either, so we provide
   1632  1.1  christos    software single-step mechanism.  */
   1633  1.1  christos 
   1634  1.1  christos static CORE_ADDR
   1635  1.1  christos sparc_analyze_control_transfer (struct regcache *regcache,
   1636  1.1  christos 				CORE_ADDR pc, CORE_ADDR *npc)
   1637  1.1  christos {
   1638  1.1  christos   unsigned long insn = sparc_fetch_instruction (pc);
   1639  1.1  christos   int conditional_p = X_COND (insn) & 0x7;
   1640  1.1  christos   int branch_p = 0, fused_p = 0;
   1641  1.1  christos   long offset = 0;			/* Must be signed for sign-extend.  */
   1642  1.1  christos 
   1643  1.1  christos   if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
   1644  1.1  christos     {
   1645  1.1  christos       if ((insn & 0x10000000) == 0)
   1646  1.1  christos 	{
   1647  1.1  christos 	  /* Branch on Integer Register with Prediction (BPr).  */
   1648  1.1  christos 	  branch_p = 1;
   1649  1.1  christos 	  conditional_p = 1;
   1650  1.1  christos 	}
   1651  1.1  christos       else
   1652  1.1  christos 	{
   1653  1.1  christos 	  /* Compare and Branch  */
   1654  1.1  christos 	  branch_p = 1;
   1655  1.1  christos 	  fused_p = 1;
   1656  1.1  christos 	  offset = 4 * X_DISP10 (insn);
   1657  1.1  christos 	}
   1658  1.1  christos     }
   1659  1.1  christos   else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
   1660  1.1  christos     {
   1661  1.1  christos       /* Branch on Floating-Point Condition Codes (FBfcc).  */
   1662  1.1  christos       branch_p = 1;
   1663  1.1  christos       offset = 4 * X_DISP22 (insn);
   1664  1.1  christos     }
   1665  1.1  christos   else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
   1666  1.1  christos     {
   1667  1.1  christos       /* Branch on Floating-Point Condition Codes with Prediction
   1668  1.1  christos          (FBPfcc).  */
   1669  1.1  christos       branch_p = 1;
   1670  1.1  christos       offset = 4 * X_DISP19 (insn);
   1671  1.1  christos     }
   1672  1.1  christos   else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
   1673  1.1  christos     {
   1674  1.1  christos       /* Branch on Integer Condition Codes (Bicc).  */
   1675  1.1  christos       branch_p = 1;
   1676  1.7  christos       offset = 4 * X_DISP22 (insn);
   1677  1.7  christos     }
   1678  1.1  christos   else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
   1679  1.8  christos     {
   1680  1.7  christos       /* Branch on Integer Condition Codes with Prediction (BPcc).  */
   1681  1.1  christos       branch_p = 1;
   1682  1.1  christos       offset = 4 * X_DISP19 (insn);
   1683  1.1  christos     }
   1684  1.1  christos   else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
   1685  1.1  christos     {
   1686  1.1  christos       struct frame_info *frame = get_current_frame ();
   1687  1.1  christos 
   1688  1.1  christos       /* Trap instruction (TRAP).  */
   1689  1.1  christos       return gdbarch_tdep (regcache->arch ())->step_trap (frame,
   1690  1.9  christos 								     insn);
   1691  1.1  christos     }
   1692  1.1  christos 
   1693  1.1  christos   /* FIXME: Handle DONE and RETRY instructions.  */
   1694  1.1  christos 
   1695  1.1  christos   if (branch_p)
   1696  1.1  christos     {
   1697  1.1  christos       if (fused_p)
   1698  1.1  christos 	{
   1699  1.1  christos 	  /* Fused compare-and-branch instructions are non-delayed,
   1700  1.1  christos 	     and do not have an annulling capability.  So we need to
   1701  1.1  christos 	     always set a breakpoint on both the NPC and the branch
   1702  1.1  christos 	     target address.  */
   1703  1.1  christos 	  gdb_assert (offset != 0);
   1704  1.1  christos 	  return pc + offset;
   1705  1.1  christos 	}
   1706  1.1  christos       else if (conditional_p)
   1707  1.1  christos 	{
   1708  1.1  christos 	  /* For conditional branches, return nPC + 4 iff the annul
   1709  1.1  christos 	     bit is 1.  */
   1710  1.1  christos 	  return (X_A (insn) ? *npc + 4 : 0);
   1711  1.1  christos 	}
   1712  1.1  christos       else
   1713  1.1  christos 	{
   1714  1.1  christos 	  /* For unconditional branches, return the target if its
   1715  1.1  christos 	     specified condition is "always" and return nPC + 4 if the
   1716  1.1  christos 	     condition is "never".  If the annul bit is 1, set *NPC to
   1717  1.1  christos 	     zero.  */
   1718  1.1  christos 	  if (X_COND (insn) == 0x0)
   1719  1.1  christos 	    pc = *npc, offset = 4;
   1720  1.1  christos 	  if (X_A (insn))
   1721  1.1  christos 	    *npc = 0;
   1722  1.1  christos 
   1723  1.1  christos 	  return pc + offset;
   1724  1.1  christos 	}
   1725  1.1  christos     }
   1726  1.8  christos 
   1727  1.7  christos   return 0;
   1728  1.1  christos }
   1729  1.8  christos 
   1730  1.1  christos static CORE_ADDR
   1731  1.1  christos sparc_step_trap (struct frame_info *frame, unsigned long insn)
   1732  1.1  christos {
   1733  1.1  christos   return 0;
   1734  1.8  christos }
   1735  1.1  christos 
   1736  1.7  christos static std::vector<CORE_ADDR>
   1737  1.7  christos sparc_software_single_step (struct regcache *regcache)
   1738  1.1  christos {
   1739  1.1  christos   struct gdbarch *arch = regcache->arch ();
   1740  1.7  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
   1741  1.1  christos   CORE_ADDR npc, nnpc;
   1742  1.8  christos 
   1743  1.1  christos   CORE_ADDR pc, orig_npc;
   1744  1.1  christos   std::vector<CORE_ADDR> next_pcs;
   1745  1.8  christos 
   1746  1.1  christos   pc = regcache_raw_get_unsigned (regcache, tdep->pc_regnum);
   1747  1.1  christos   orig_npc = npc = regcache_raw_get_unsigned (regcache, tdep->npc_regnum);
   1748  1.1  christos 
   1749  1.1  christos   /* Analyze the instruction at PC.  */
   1750  1.1  christos   nnpc = sparc_analyze_control_transfer (regcache, pc, &npc);
   1751  1.1  christos   if (npc != 0)
   1752  1.1  christos     next_pcs.push_back (npc);
   1753  1.7  christos 
   1754  1.1  christos   if (nnpc != 0)
   1755  1.1  christos     next_pcs.push_back (nnpc);
   1756  1.1  christos 
   1757  1.1  christos   /* Assert that we have set at least one breakpoint, and that
   1758  1.1  christos      they're not set at the same spot - unless we're going
   1759  1.8  christos      from here straight to NULL, i.e. a call or jump to 0.  */
   1760  1.1  christos   gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
   1761  1.1  christos   gdb_assert (nnpc != npc || orig_npc == 0);
   1762  1.1  christos 
   1763  1.1  christos   return next_pcs;
   1764  1.1  christos }
   1765  1.1  christos 
   1766  1.3  christos static void
   1767  1.1  christos sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
   1768  1.3  christos {
   1769  1.3  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
   1770  1.3  christos 
   1771  1.3  christos   regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
   1772  1.3  christos   regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
   1773  1.1  christos }
   1774  1.1  christos 
   1775  1.1  christos 
   1777  1.8  christos /* Iterate over core file register note sections.  */
   1778  1.8  christos 
   1779  1.8  christos static void
   1780  1.1  christos sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
   1781  1.1  christos 				    iterate_over_regset_sections_cb *cb,
   1782  1.1  christos 				    void *cb_data,
   1783  1.7  christos 				    const struct regcache *regcache)
   1784  1.7  christos {
   1785  1.7  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   1786  1.7  christos 
   1787  1.7  christos   cb (".reg", tdep->sizeof_gregset, tdep->sizeof_gregset, tdep->gregset, NULL,
   1788  1.7  christos       cb_data);
   1789  1.7  christos   cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, tdep->fpregset,
   1790  1.7  christos       NULL, cb_data);
   1791  1.7  christos }
   1792  1.7  christos 
   1793  1.7  christos 
   1795  1.7  christos static int
   1796  1.7  christos validate_tdesc_registers (const struct target_desc *tdesc,
   1797  1.7  christos                           struct tdesc_arch_data *tdesc_data,
   1798  1.7  christos                           const char *feature_name,
   1799  1.7  christos                           const char *register_names[],
   1800  1.7  christos                           unsigned int registers_num,
   1801  1.7  christos                           unsigned int reg_start)
   1802  1.7  christos {
   1803  1.7  christos   int valid_p = 1;
   1804  1.7  christos   const struct tdesc_feature *feature;
   1805  1.7  christos 
   1806  1.1  christos   feature = tdesc_find_feature (tdesc, feature_name);
   1807  1.1  christos   if (feature == NULL)
   1808  1.1  christos     return 0;
   1809  1.1  christos 
   1810  1.7  christos   for (unsigned int i = 0; i < registers_num; i++)
   1811  1.1  christos     valid_p &= tdesc_numbered_register (feature, tdesc_data,
   1812  1.7  christos                                         reg_start + i,
   1813  1.1  christos                                         register_names[i]);
   1814  1.1  christos 
   1815  1.1  christos   return valid_p;
   1816  1.1  christos }
   1817  1.1  christos 
   1818  1.1  christos static struct gdbarch *
   1819  1.1  christos sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   1820  1.3  christos {
   1821  1.1  christos   struct gdbarch_tdep *tdep;
   1822  1.1  christos   const struct target_desc *tdesc = info.target_desc;
   1823  1.1  christos   struct gdbarch *gdbarch;
   1824  1.1  christos   int valid_p = 1;
   1825  1.1  christos 
   1826  1.7  christos   /* If there is already a candidate, use it.  */
   1827  1.7  christos   arches = gdbarch_list_lookup_by_info (arches, &info);
   1828  1.7  christos   if (arches != NULL)
   1829  1.7  christos     return arches->gdbarch;
   1830  1.1  christos 
   1831  1.1  christos   /* Allocate space for the new architecture.  */
   1832  1.1  christos   tdep = XCNEW (struct gdbarch_tdep);
   1833  1.1  christos   gdbarch = gdbarch_alloc (&info, tdep);
   1834  1.7  christos 
   1835  1.7  christos   tdep->pc_regnum = SPARC32_PC_REGNUM;
   1836  1.7  christos   tdep->npc_regnum = SPARC32_NPC_REGNUM;
   1837  1.1  christos   tdep->step_trap = sparc_step_trap;
   1838  1.1  christos   tdep->fpu_register_names = sparc32_fpu_register_names;
   1839  1.1  christos   tdep->fpu_registers_num = ARRAY_SIZE (sparc32_fpu_register_names);
   1840  1.1  christos   tdep->cp0_register_names = sparc32_cp0_register_names;
   1841  1.7  christos   tdep->cp0_registers_num = ARRAY_SIZE (sparc32_cp0_register_names);
   1842  1.7  christos 
   1843  1.1  christos   set_gdbarch_long_double_bit (gdbarch, 128);
   1844  1.1  christos   set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);
   1845  1.1  christos 
   1846  1.1  christos   set_gdbarch_wchar_bit (gdbarch, 16);
   1847  1.1  christos   set_gdbarch_wchar_signed (gdbarch, 1);
   1848  1.1  christos 
   1849  1.1  christos   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
   1850  1.1  christos   set_gdbarch_register_name (gdbarch, sparc32_register_name);
   1851  1.1  christos   set_gdbarch_register_type (gdbarch, sparc32_register_type);
   1852  1.1  christos   set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
   1853  1.1  christos   set_tdesc_pseudo_register_name (gdbarch, sparc32_pseudo_register_name);
   1854  1.1  christos   set_tdesc_pseudo_register_type (gdbarch, sparc32_pseudo_register_type);
   1855  1.1  christos   set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
   1856  1.1  christos   set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
   1857  1.1  christos 
   1858  1.1  christos   /* Register numbers of various important registers.  */
   1859  1.1  christos   set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
   1860  1.1  christos   set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
   1861  1.1  christos   set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
   1862  1.1  christos 
   1863  1.1  christos   /* Call dummy code.  */
   1864  1.1  christos   set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
   1865  1.1  christos   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
   1866  1.7  christos   set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
   1867  1.7  christos   set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
   1868  1.7  christos 
   1869  1.7  christos   set_gdbarch_return_value (gdbarch, sparc32_return_value);
   1870  1.1  christos   set_gdbarch_stabs_argument_has_addr
   1871  1.1  christos     (gdbarch, sparc32_stabs_argument_has_addr);
   1872  1.1  christos 
   1873  1.1  christos   set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
   1874  1.1  christos 
   1875  1.1  christos   /* Stack grows downward.  */
   1876  1.1  christos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
   1877  1.1  christos 
   1878  1.1  christos   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
   1879  1.1  christos 				       sparc_breakpoint::kind_from_pc);
   1880  1.1  christos   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
   1881  1.1  christos 				       sparc_breakpoint::bp_from_kind);
   1882  1.8  christos 
   1883  1.8  christos   set_gdbarch_frame_args_skip (gdbarch, 8);
   1884  1.8  christos 
   1885  1.1  christos   set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
   1886  1.1  christos   set_gdbarch_write_pc (gdbarch, sparc_write_pc);
   1887  1.1  christos 
   1888  1.1  christos   set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
   1889  1.1  christos 
   1890  1.1  christos   frame_base_set_default (gdbarch, &sparc32_frame_base);
   1891  1.1  christos 
   1892  1.1  christos   /* Hook in the DWARF CFI frame unwinder.  */
   1893  1.7  christos   dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
   1894  1.7  christos   /* Register DWARF vendor CFI handler.  */
   1895  1.7  christos   set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
   1896  1.7  christos 					   sparc_execute_dwarf_cfa_vendor_op);
   1897  1.7  christos   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
   1898  1.7  christos      StackGhost issues have been resolved.  */
   1899  1.7  christos 
   1900  1.7  christos   /* Hook in ABI-specific overrides, if they have been registered.  */
   1901  1.7  christos   gdbarch_init_osabi (info, gdbarch);
   1902  1.7  christos 
   1903  1.7  christos   frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
   1904  1.7  christos 
   1905  1.7  christos   if (tdesc_has_registers (tdesc))
   1906  1.7  christos     {
   1907  1.7  christos       struct tdesc_arch_data *tdesc_data = tdesc_data_alloc ();
   1908  1.7  christos 
   1909  1.7  christos       /* Validate that the descriptor provides the mandatory registers
   1910  1.7  christos          and allocate their numbers. */
   1911  1.7  christos       valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
   1912  1.7  christos                                            "org.gnu.gdb.sparc.cpu",
   1913  1.7  christos                                            sparc_core_register_names,
   1914  1.7  christos                                            ARRAY_SIZE (sparc_core_register_names),
   1915  1.7  christos                                            SPARC_G0_REGNUM);
   1916  1.7  christos       valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
   1917  1.7  christos                                            "org.gnu.gdb.sparc.fpu",
   1918  1.7  christos                                            tdep->fpu_register_names,
   1919  1.7  christos                                            tdep->fpu_registers_num,
   1920  1.7  christos                                            SPARC_F0_REGNUM);
   1921  1.7  christos       valid_p &= validate_tdesc_registers (tdesc, tdesc_data,
   1922  1.8  christos                                            "org.gnu.gdb.sparc.cp0",
   1923  1.7  christos                                            tdep->cp0_register_names,
   1924  1.7  christos                                            tdep->cp0_registers_num,
   1925  1.7  christos                                            SPARC_F0_REGNUM
   1926  1.1  christos                                            + tdep->fpu_registers_num);
   1927  1.1  christos       if (!valid_p)
   1928  1.3  christos         {
   1929  1.3  christos           tdesc_data_cleanup (tdesc_data);
   1930  1.1  christos           return NULL;
   1931  1.1  christos         }
   1932  1.1  christos 
   1933  1.1  christos       /* Target description may have changed. */
   1934  1.1  christos       info.tdesc_data = tdesc_data;
   1935  1.1  christos       tdesc_use_registers (gdbarch, tdesc, tdesc_data);
   1936  1.1  christos     }
   1937  1.1  christos 
   1938  1.1  christos   /* If we have register sets, enable the generic core file support.  */
   1939  1.1  christos   if (tdep->gregset)
   1940  1.1  christos     set_gdbarch_iterate_over_regset_sections
   1941  1.8  christos       (gdbarch, sparc_iterate_over_regset_sections);
   1942  1.1  christos 
   1943  1.1  christos   register_sparc_ravenscar_ops (gdbarch);
   1944  1.1  christos 
   1945  1.1  christos   return gdbarch;
   1946  1.1  christos }
   1947  1.1  christos 
   1948  1.1  christos /* Helper functions for dealing with register windows.  */
   1950  1.1  christos 
   1951  1.1  christos void
   1952  1.1  christos sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
   1953  1.1  christos {
   1954  1.1  christos   struct gdbarch *gdbarch = regcache->arch ();
   1955  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1956  1.1  christos   int offset = 0;
   1957  1.1  christos   gdb_byte buf[8];
   1958  1.1  christos   int i;
   1959  1.1  christos 
   1960  1.1  christos   if (sp & 1)
   1961  1.1  christos     {
   1962  1.1  christos       /* Registers are 64-bit.  */
   1963  1.1  christos       sp += BIAS;
   1964  1.1  christos 
   1965  1.1  christos       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   1966  1.1  christos 	{
   1967  1.1  christos 	  if (regnum == i || regnum == -1)
   1968  1.1  christos 	    {
   1969  1.8  christos 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
   1970  1.1  christos 
   1971  1.1  christos 	      /* Handle StackGhost.  */
   1972  1.1  christos 	      if (i == SPARC_I7_REGNUM)
   1973  1.1  christos 		{
   1974  1.1  christos 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   1975  1.1  christos 		  ULONGEST i7;
   1976  1.1  christos 
   1977  1.1  christos 		  i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
   1978  1.1  christos 		  store_unsigned_integer (buf + offset, 8, byte_order,
   1979  1.1  christos 					  i7 ^ wcookie);
   1980  1.1  christos 		}
   1981  1.8  christos 
   1982  1.1  christos 	      regcache->raw_supply (i, buf);
   1983  1.1  christos 	    }
   1984  1.1  christos 	}
   1985  1.1  christos     }
   1986  1.1  christos   else
   1987  1.1  christos     {
   1988  1.1  christos       /* Registers are 32-bit.  Toss any sign-extension of the stack
   1989  1.1  christos 	 pointer.  */
   1990  1.1  christos       sp &= 0xffffffffUL;
   1991  1.1  christos 
   1992  1.1  christos       /* Clear out the top half of the temporary buffer, and put the
   1993  1.1  christos 	 register value in the bottom half if we're in 64-bit mode.  */
   1994  1.1  christos       if (gdbarch_ptr_bit (regcache->arch ()) == 64)
   1995  1.1  christos 	{
   1996  1.1  christos 	  memset (buf, 0, 4);
   1997  1.1  christos 	  offset = 4;
   1998  1.1  christos 	}
   1999  1.1  christos 
   2000  1.1  christos       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2001  1.1  christos 	{
   2002  1.1  christos 	  if (regnum == i || regnum == -1)
   2003  1.1  christos 	    {
   2004  1.1  christos 	      target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
   2005  1.8  christos 				  buf + offset, 4);
   2006  1.1  christos 
   2007  1.1  christos 	      /* Handle StackGhost.  */
   2008  1.1  christos 	      if (i == SPARC_I7_REGNUM)
   2009  1.1  christos 		{
   2010  1.1  christos 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   2011  1.1  christos 		  ULONGEST i7;
   2012  1.1  christos 
   2013  1.1  christos 		  i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
   2014  1.1  christos 		  store_unsigned_integer (buf + offset, 4, byte_order,
   2015  1.8  christos 					  i7 ^ wcookie);
   2016  1.1  christos 		}
   2017  1.1  christos 
   2018  1.1  christos 	      regcache->raw_supply (i, buf);
   2019  1.1  christos 	    }
   2020  1.1  christos 	}
   2021  1.1  christos     }
   2022  1.1  christos }
   2023  1.1  christos 
   2024  1.1  christos void
   2025  1.1  christos sparc_collect_rwindow (const struct regcache *regcache,
   2026  1.1  christos 		       CORE_ADDR sp, int regnum)
   2027  1.1  christos {
   2028  1.1  christos   struct gdbarch *gdbarch = regcache->arch ();
   2029  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2030  1.8  christos   int offset = 0;
   2031  1.1  christos   gdb_byte buf[8];
   2032  1.1  christos   int i;
   2033  1.1  christos 
   2034  1.1  christos   if (sp & 1)
   2035  1.1  christos     {
   2036  1.1  christos       /* Registers are 64-bit.  */
   2037  1.1  christos       sp += BIAS;
   2038  1.1  christos 
   2039  1.1  christos       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2040  1.1  christos 	{
   2041  1.1  christos 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
   2042  1.1  christos 	    {
   2043  1.1  christos 	      regcache->raw_collect (i, buf);
   2044  1.1  christos 
   2045  1.1  christos 	      /* Handle StackGhost.  */
   2046  1.1  christos 	      if (i == SPARC_I7_REGNUM)
   2047  1.1  christos 		{
   2048  1.1  christos 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   2049  1.1  christos 		  ULONGEST i7;
   2050  1.1  christos 
   2051  1.1  christos 		  i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
   2052  1.1  christos 		  store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
   2053  1.8  christos 		}
   2054  1.1  christos 
   2055  1.1  christos 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
   2056  1.1  christos 	    }
   2057  1.1  christos 	}
   2058  1.1  christos     }
   2059  1.1  christos   else
   2060  1.8  christos     {
   2061  1.1  christos       /* Registers are 32-bit.  Toss any sign-extension of the stack
   2062  1.1  christos 	 pointer.  */
   2063  1.1  christos       sp &= 0xffffffffUL;
   2064  1.1  christos 
   2065  1.1  christos       /* Only use the bottom half if we're in 64-bit mode.  */
   2066  1.1  christos       if (gdbarch_ptr_bit (regcache->arch ()) == 64)
   2067  1.1  christos 	offset = 4;
   2068  1.1  christos 
   2069  1.1  christos       for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2070  1.1  christos 	{
   2071  1.1  christos 	  if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
   2072  1.1  christos 	    {
   2073  1.1  christos 	      regcache->raw_collect (i, buf);
   2074  1.1  christos 
   2075  1.1  christos 	      /* Handle StackGhost.  */
   2076  1.1  christos 	      if (i == SPARC_I7_REGNUM)
   2077  1.1  christos 		{
   2078  1.1  christos 		  ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
   2079  1.1  christos 		  ULONGEST i7;
   2080  1.1  christos 
   2081  1.1  christos 		  i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
   2082  1.1  christos 		  store_unsigned_integer (buf + offset, 4, byte_order,
   2083  1.3  christos 					  i7 ^ wcookie);
   2084  1.1  christos 		}
   2085  1.1  christos 
   2086  1.1  christos 	      target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
   2087  1.6  christos 				   buf + offset, 4);
   2088  1.1  christos 	    }
   2089  1.1  christos 	}
   2090  1.1  christos     }
   2091  1.1  christos }
   2092  1.8  christos 
   2093  1.1  christos /* Helper functions for dealing with register sets.  */
   2094  1.1  christos 
   2095  1.8  christos void
   2096  1.1  christos sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
   2097  1.1  christos 			struct regcache *regcache,
   2098  1.8  christos 			int regnum, const void *gregs)
   2099  1.1  christos {
   2100  1.1  christos   const gdb_byte *regs = (const gdb_byte *) gregs;
   2101  1.8  christos   gdb_byte zero[4] = { 0 };
   2102  1.1  christos   int i;
   2103  1.1  christos 
   2104  1.8  christos   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   2105  1.1  christos     regcache->raw_supply (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
   2106  1.1  christos 
   2107  1.1  christos   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   2108  1.3  christos     regcache->raw_supply (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
   2109  1.1  christos 
   2110  1.1  christos   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   2111  1.1  christos     regcache->raw_supply (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
   2112  1.1  christos 
   2113  1.8  christos   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   2114  1.1  christos     regcache->raw_supply (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
   2115  1.1  christos 
   2116  1.1  christos   if (regnum == SPARC_G0_REGNUM || regnum == -1)
   2117  1.1  christos     regcache->raw_supply (SPARC_G0_REGNUM, &zero);
   2118  1.1  christos 
   2119  1.1  christos   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   2120  1.1  christos     {
   2121  1.1  christos       int offset = gregmap->r_g1_offset;
   2122  1.3  christos 
   2123  1.1  christos       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   2124  1.1  christos 	{
   2125  1.1  christos 	  if (regnum == i || regnum == -1)
   2126  1.1  christos 	    regcache->raw_supply (i, regs + offset);
   2127  1.1  christos 	  offset += 4;
   2128  1.1  christos 	}
   2129  1.1  christos     }
   2130  1.1  christos 
   2131  1.3  christos   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   2132  1.1  christos     {
   2133  1.1  christos       /* Not all of the register set variants include Locals and
   2134  1.1  christos          Inputs.  For those that don't, we read them off the stack.  */
   2135  1.1  christos       if (gregmap->r_l0_offset == -1)
   2136  1.8  christos 	{
   2137  1.1  christos 	  ULONGEST sp;
   2138  1.1  christos 
   2139  1.1  christos 	  regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
   2140  1.1  christos 	  sparc_supply_rwindow (regcache, sp, regnum);
   2141  1.1  christos 	}
   2142  1.1  christos       else
   2143  1.1  christos 	{
   2144  1.3  christos 	  int offset = gregmap->r_l0_offset;
   2145  1.1  christos 
   2146  1.1  christos 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2147  1.1  christos 	    {
   2148  1.6  christos 	      if (regnum == i || regnum == -1)
   2149  1.1  christos 		regcache->raw_supply (i, regs + offset);
   2150  1.1  christos 	      offset += 4;
   2151  1.1  christos 	    }
   2152  1.8  christos 	}
   2153  1.1  christos     }
   2154  1.1  christos }
   2155  1.8  christos 
   2156  1.1  christos void
   2157  1.1  christos sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
   2158  1.8  christos 			 const struct regcache *regcache,
   2159  1.1  christos 			 int regnum, void *gregs)
   2160  1.1  christos {
   2161  1.8  christos   gdb_byte *regs = (gdb_byte *) gregs;
   2162  1.1  christos   int i;
   2163  1.1  christos 
   2164  1.1  christos   if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
   2165  1.3  christos     regcache->raw_collect (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
   2166  1.1  christos 
   2167  1.1  christos   if (regnum == SPARC32_PC_REGNUM || regnum == -1)
   2168  1.1  christos     regcache->raw_collect (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
   2169  1.1  christos 
   2170  1.1  christos   if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
   2171  1.8  christos     regcache->raw_collect (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
   2172  1.1  christos 
   2173  1.1  christos   if (regnum == SPARC32_Y_REGNUM || regnum == -1)
   2174  1.1  christos     regcache->raw_collect (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
   2175  1.1  christos 
   2176  1.1  christos   if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
   2177  1.1  christos     {
   2178  1.1  christos       int offset = gregmap->r_g1_offset;
   2179  1.1  christos 
   2180  1.3  christos       /* %g0 is always zero.  */
   2181  1.1  christos       for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
   2182  1.3  christos 	{
   2183  1.1  christos 	  if (regnum == i || regnum == -1)
   2184  1.1  christos 	    regcache->raw_collect (i, regs + offset);
   2185  1.1  christos 	  offset += 4;
   2186  1.1  christos 	}
   2187  1.8  christos     }
   2188  1.1  christos 
   2189  1.1  christos   if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
   2190  1.1  christos     {
   2191  1.1  christos       /* Not all of the register set variants include Locals and
   2192  1.1  christos          Inputs.  For those that don't, we read them off the stack.  */
   2193  1.1  christos       if (gregmap->r_l0_offset != -1)
   2194  1.1  christos 	{
   2195  1.3  christos 	  int offset = gregmap->r_l0_offset;
   2196  1.1  christos 
   2197  1.1  christos 	  for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
   2198  1.1  christos 	    {
   2199  1.6  christos 	      if (regnum == i || regnum == -1)
   2200  1.1  christos 		regcache->raw_collect (i, regs + offset);
   2201  1.1  christos 	      offset += 4;
   2202  1.1  christos 	    }
   2203  1.1  christos 	}
   2204  1.1  christos     }
   2205  1.8  christos }
   2206  1.8  christos 
   2207  1.1  christos void
   2208  1.1  christos sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
   2209  1.1  christos 			 struct regcache *regcache,
   2210  1.8  christos 			 int regnum, const void *fpregs)
   2211  1.1  christos {
   2212  1.1  christos   const gdb_byte *regs = (const gdb_byte *) fpregs;
   2213  1.1  christos   int i;
   2214  1.3  christos 
   2215  1.1  christos   for (i = 0; i < 32; i++)
   2216  1.1  christos     {
   2217  1.1  christos       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2218  1.6  christos 	regcache->raw_supply (SPARC_F0_REGNUM + i,
   2219  1.1  christos 			      regs + fpregmap->r_f0_offset + (i * 4));
   2220  1.1  christos     }
   2221  1.1  christos 
   2222  1.1  christos   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2223  1.1  christos     regcache->raw_supply (SPARC32_FSR_REGNUM, regs + fpregmap->r_fsr_offset);
   2224  1.8  christos }
   2225  1.8  christos 
   2226  1.1  christos void
   2227  1.1  christos sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
   2228  1.1  christos 			  const struct regcache *regcache,
   2229  1.8  christos 			  int regnum, void *fpregs)
   2230  1.8  christos {
   2231  1.1  christos   gdb_byte *regs = (gdb_byte *) fpregs;
   2232  1.1  christos   int i;
   2233  1.1  christos 
   2234  1.1  christos   for (i = 0; i < 32; i++)
   2235  1.1  christos     {
   2236  1.1  christos       if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
   2237  1.3  christos 	regcache->raw_collect (SPARC_F0_REGNUM + i,
   2238  1.1  christos 			       regs + fpregmap->r_f0_offset + (i * 4));
   2239  1.1  christos     }
   2240  1.1  christos 
   2241  1.1  christos   if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
   2242  1.1  christos     regcache->raw_collect (SPARC32_FSR_REGNUM,
   2243  1.1  christos 			   regs + fpregmap->r_fsr_offset);
   2244  1.1  christos }
   2245  1.1  christos 
   2246  1.1  christos 
   2248  1.1  christos /* SunOS 4.  */
   2249  1.3  christos 
   2250  1.1  christos /* From <machine/reg.h>.  */
   2251  1.1  christos const struct sparc_gregmap sparc32_sunos4_gregmap =
   2252  1.1  christos {
   2253  1.1  christos   0 * 4,			/* %psr */
   2254  1.1  christos   1 * 4,			/* %pc */
   2255  1.3  christos   2 * 4,			/* %npc */
   2256  1.1  christos   3 * 4,			/* %y */
   2257  1.1  christos   -1,				/* %wim */
   2258  1.1  christos   -1,				/* %tbr */
   2259  1.1  christos   4 * 4,			/* %g1 */
   2260  1.1  christos   -1				/* %l0 */
   2261  1.9  christos };
   2262  1.1  christos 
   2263  1.9  christos const struct sparc_fpregmap sparc32_sunos4_fpregmap =
   2264  1.1  christos {
   2265  1.1  christos   0 * 4,			/* %f0 */
   2266  1.1  christos   33 * 4,			/* %fsr */
   2267                };
   2268                
   2269                const struct sparc_fpregmap sparc32_bsd_fpregmap =
   2270                {
   2271                  0 * 4,			/* %f0 */
   2272                  32 * 4,			/* %fsr */
   2273                };
   2274                
   2275                void _initialize_sparc_tdep ();
   2276                void
   2277                _initialize_sparc_tdep ()
   2278                {
   2279                  register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
   2280                }
   2281