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