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