Home | History | Annotate | Line # | Download | only in gdb
xtensa-tdep.c revision 1.8
      1  1.1  christos /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
      2  1.1  christos 
      3  1.8  christos    Copyright (C) 2003-2019 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 "frame.h"
     22  1.1  christos #include "solib-svr4.h"
     23  1.1  christos #include "symtab.h"
     24  1.1  christos #include "symfile.h"
     25  1.1  christos #include "objfiles.h"
     26  1.1  christos #include "gdbtypes.h"
     27  1.1  christos #include "gdbcore.h"
     28  1.1  christos #include "value.h"
     29  1.1  christos #include "dis-asm.h"
     30  1.1  christos #include "inferior.h"
     31  1.6  christos #include "osabi.h"
     32  1.1  christos #include "regcache.h"
     33  1.1  christos #include "reggroups.h"
     34  1.1  christos #include "regset.h"
     35  1.1  christos 
     36  1.1  christos #include "dummy-frame.h"
     37  1.1  christos #include "dwarf2.h"
     38  1.1  christos #include "dwarf2-frame.h"
     39  1.1  christos #include "dwarf2loc.h"
     40  1.1  christos #include "frame-base.h"
     41  1.1  christos #include "frame-unwind.h"
     42  1.1  christos 
     43  1.1  christos #include "arch-utils.h"
     44  1.1  christos #include "gdbarch.h"
     45  1.1  christos #include "remote.h"
     46  1.1  christos #include "serial.h"
     47  1.1  christos 
     48  1.1  christos #include "command.h"
     49  1.1  christos #include "gdbcmd.h"
     50  1.1  christos 
     51  1.1  christos #include "xtensa-isa.h"
     52  1.1  christos #include "xtensa-tdep.h"
     53  1.1  christos #include "xtensa-config.h"
     54  1.7  christos #include <algorithm>
     55  1.1  christos 
     56  1.1  christos 
     57  1.1  christos static unsigned int xtensa_debug_level = 0;
     58  1.1  christos 
     59  1.1  christos #define DEBUGWARN(args...) \
     60  1.1  christos   if (xtensa_debug_level > 0) \
     61  1.1  christos     fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
     62  1.1  christos 
     63  1.1  christos #define DEBUGINFO(args...) \
     64  1.1  christos   if (xtensa_debug_level > 1) \
     65  1.1  christos     fprintf_unfiltered (gdb_stdlog, "(info ) " args)
     66  1.1  christos 
     67  1.1  christos #define DEBUGTRACE(args...) \
     68  1.1  christos   if (xtensa_debug_level > 2) \
     69  1.1  christos     fprintf_unfiltered (gdb_stdlog, "(trace) " args)
     70  1.1  christos 
     71  1.1  christos #define DEBUGVERB(args...) \
     72  1.1  christos   if (xtensa_debug_level > 3) \
     73  1.1  christos     fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
     74  1.1  christos 
     75  1.1  christos 
     76  1.1  christos /* According to the ABI, the SP must be aligned to 16-byte boundaries.  */
     77  1.1  christos #define SP_ALIGNMENT 16
     78  1.1  christos 
     79  1.1  christos 
     80  1.1  christos /* On Windowed ABI, we use a6 through a11 for passing arguments
     81  1.1  christos    to a function called by GDB because CALL4 is used.  */
     82  1.1  christos #define ARGS_NUM_REGS		6
     83  1.1  christos #define REGISTER_SIZE		4
     84  1.1  christos 
     85  1.1  christos 
     86  1.1  christos /* Extract the call size from the return address or PS register.  */
     87  1.1  christos #define PS_CALLINC_SHIFT	16
     88  1.1  christos #define PS_CALLINC_MASK		0x00030000
     89  1.1  christos #define CALLINC(ps)		(((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
     90  1.1  christos #define WINSIZE(ra)		(4 * (( (ra) >> 30) & 0x3))
     91  1.1  christos 
     92  1.1  christos /* On TX,  hardware can be configured without Exception Option.
     93  1.1  christos    There is no PS register in this case.  Inside XT-GDB,  let us treat
     94  1.1  christos    it as a virtual read-only register always holding the same value.  */
     95  1.1  christos #define TX_PS			0x20
     96  1.1  christos 
     97  1.1  christos /* ABI-independent macros.  */
     98  1.1  christos #define ARG_NOF(gdbarch) \
     99  1.1  christos   (gdbarch_tdep (gdbarch)->call_abi \
    100  1.1  christos    == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
    101  1.1  christos #define ARG_1ST(gdbarch) \
    102  1.1  christos   (gdbarch_tdep (gdbarch)->call_abi  == CallAbiCall0Only \
    103  1.1  christos    ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
    104  1.1  christos    : (gdbarch_tdep (gdbarch)->a0_base + 6))
    105  1.1  christos 
    106  1.1  christos /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
    107  1.1  christos    indicates that the instruction is an ENTRY instruction.  */
    108  1.1  christos 
    109  1.1  christos #define XTENSA_IS_ENTRY(gdbarch, op1) \
    110  1.1  christos   ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
    111  1.1  christos    ? ((op1) == 0x6c) : ((op1) == 0x36))
    112  1.1  christos 
    113  1.1  christos #define XTENSA_ENTRY_LENGTH	3
    114  1.1  christos 
    115  1.1  christos /* windowing_enabled() returns true, if windowing is enabled.
    116  1.1  christos    WOE must be set to 1; EXCM to 0.
    117  1.1  christos    Note: We assume that EXCM is always 0 for XEA1.  */
    118  1.1  christos 
    119  1.1  christos #define PS_WOE			(1<<18)
    120  1.1  christos #define PS_EXC			(1<<4)
    121  1.1  christos 
    122  1.8  christos /* Big enough to hold the size of the largest register in bytes.  */
    123  1.8  christos #define XTENSA_MAX_REGISTER_SIZE	64
    124  1.8  christos 
    125  1.1  christos static int
    126  1.1  christos windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
    127  1.1  christos {
    128  1.1  christos   /* If we know CALL0 ABI is set explicitly,  say it is Call0.  */
    129  1.1  christos   if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
    130  1.1  christos     return 0;
    131  1.1  christos 
    132  1.1  christos   return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos /* Convert a live A-register number to the corresponding AR-register
    136  1.1  christos    number.  */
    137  1.1  christos static int
    138  1.1  christos arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
    139  1.1  christos {
    140  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    141  1.1  christos   int arreg;
    142  1.1  christos 
    143  1.1  christos   arreg = a_regnum - tdep->a0_base;
    144  1.1  christos   arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
    145  1.1  christos   arreg &= tdep->num_aregs - 1;
    146  1.1  christos 
    147  1.1  christos   return arreg + tdep->ar_base;
    148  1.1  christos }
    149  1.1  christos 
    150  1.1  christos /* Convert a live AR-register number to the corresponding A-register order
    151  1.1  christos    number in a range [0..15].  Return -1, if AR_REGNUM is out of WB window.  */
    152  1.1  christos static int
    153  1.1  christos areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
    154  1.1  christos {
    155  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    156  1.1  christos   int areg;
    157  1.1  christos 
    158  1.1  christos   areg = ar_regnum - tdep->ar_base;
    159  1.1  christos   if (areg < 0 || areg >= tdep->num_aregs)
    160  1.1  christos     return -1;
    161  1.1  christos   areg = (areg - wb * 4) & (tdep->num_aregs - 1);
    162  1.1  christos   return (areg > 15) ? -1 : areg;
    163  1.1  christos }
    164  1.1  christos 
    165  1.1  christos /* Read Xtensa register directly from the hardware.  */
    166  1.1  christos static unsigned long
    167  1.1  christos xtensa_read_register (int regnum)
    168  1.1  christos {
    169  1.1  christos   ULONGEST value;
    170  1.1  christos 
    171  1.1  christos   regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
    172  1.1  christos   return (unsigned long) value;
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos /* Write Xtensa register directly to the hardware.  */
    176  1.1  christos static void
    177  1.1  christos xtensa_write_register (int regnum, ULONGEST value)
    178  1.1  christos {
    179  1.1  christos   regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
    180  1.1  christos }
    181  1.1  christos 
    182  1.1  christos /* Return the window size of the previous call to the function from which we
    183  1.1  christos    have just returned.
    184  1.1  christos 
    185  1.1  christos    This function is used to extract the return value after a called function
    186  1.1  christos    has returned to the caller.  On Xtensa, the register that holds the return
    187  1.1  christos    value (from the perspective of the caller) depends on what call
    188  1.1  christos    instruction was used.  For now, we are assuming that the call instruction
    189  1.1  christos    precedes the current address, so we simply analyze the call instruction.
    190  1.1  christos    If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
    191  1.1  christos    method to call the inferior function.  */
    192  1.1  christos 
    193  1.1  christos static int
    194  1.1  christos extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
    195  1.1  christos {
    196  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    197  1.1  christos   int winsize = 4;
    198  1.1  christos   int insn;
    199  1.1  christos   gdb_byte buf[4];
    200  1.1  christos 
    201  1.1  christos   DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
    202  1.1  christos 
    203  1.1  christos   /* Read the previous instruction (should be a call[x]{4|8|12}.  */
    204  1.1  christos   read_memory (pc-3, buf, 3);
    205  1.1  christos   insn = extract_unsigned_integer (buf, 3, byte_order);
    206  1.1  christos 
    207  1.1  christos   /* Decode call instruction:
    208  1.1  christos      Little Endian
    209  1.1  christos        call{0,4,8,12}   OFFSET || {00,01,10,11} || 0101
    210  1.1  christos        callx{0,4,8,12}  OFFSET || 11 || {00,01,10,11} || 0000
    211  1.1  christos      Big Endian
    212  1.1  christos        call{0,4,8,12}   0101 || {00,01,10,11} || OFFSET
    213  1.1  christos        callx{0,4,8,12}  0000 || {00,01,10,11} || 11 || OFFSET.  */
    214  1.1  christos 
    215  1.1  christos   if (byte_order == BFD_ENDIAN_LITTLE)
    216  1.1  christos     {
    217  1.1  christos       if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
    218  1.1  christos 	winsize = (insn & 0x30) >> 2;   /* 0, 4, 8, 12.  */
    219  1.1  christos     }
    220  1.1  christos   else
    221  1.1  christos     {
    222  1.1  christos       if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
    223  1.1  christos 	winsize = (insn >> 16) & 0xc;   /* 0, 4, 8, 12.  */
    224  1.1  christos     }
    225  1.1  christos   return winsize;
    226  1.1  christos }
    227  1.1  christos 
    228  1.1  christos 
    229  1.1  christos /* REGISTER INFORMATION */
    230  1.1  christos 
    231  1.1  christos /* Find register by name.  */
    232  1.1  christos static int
    233  1.7  christos xtensa_find_register_by_name (struct gdbarch *gdbarch, const char *name)
    234  1.1  christos {
    235  1.1  christos   int i;
    236  1.1  christos 
    237  1.8  christos   for (i = 0; i < gdbarch_num_cooked_regs (gdbarch); i++)
    238  1.1  christos 
    239  1.1  christos     if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
    240  1.1  christos       return i;
    241  1.1  christos 
    242  1.1  christos   return -1;
    243  1.1  christos }
    244  1.1  christos 
    245  1.1  christos /* Returns the name of a register.  */
    246  1.1  christos static const char *
    247  1.1  christos xtensa_register_name (struct gdbarch *gdbarch, int regnum)
    248  1.1  christos {
    249  1.1  christos   /* Return the name stored in the register map.  */
    250  1.8  christos   if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
    251  1.1  christos     return gdbarch_tdep (gdbarch)->regmap[regnum].name;
    252  1.1  christos 
    253  1.1  christos   internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
    254  1.1  christos   return 0;
    255  1.1  christos }
    256  1.1  christos 
    257  1.1  christos /* Return the type of a register.  Create a new type, if necessary.  */
    258  1.1  christos 
    259  1.1  christos static struct type *
    260  1.1  christos xtensa_register_type (struct gdbarch *gdbarch, int regnum)
    261  1.1  christos {
    262  1.1  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
    263  1.1  christos 
    264  1.1  christos   /* Return signed integer for ARx and Ax registers.  */
    265  1.1  christos   if ((regnum >= tdep->ar_base
    266  1.1  christos        && regnum < tdep->ar_base + tdep->num_aregs)
    267  1.1  christos       || (regnum >= tdep->a0_base
    268  1.1  christos 	  && regnum < tdep->a0_base + 16))
    269  1.1  christos     return builtin_type (gdbarch)->builtin_int;
    270  1.1  christos 
    271  1.1  christos   if (regnum == gdbarch_pc_regnum (gdbarch)
    272  1.1  christos       || regnum == tdep->a0_base + 1)
    273  1.1  christos     return builtin_type (gdbarch)->builtin_data_ptr;
    274  1.1  christos 
    275  1.1  christos   /* Return the stored type for all other registers.  */
    276  1.8  christos   else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
    277  1.1  christos     {
    278  1.1  christos       xtensa_register_t* reg = &tdep->regmap[regnum];
    279  1.1  christos 
    280  1.1  christos       /* Set ctype for this register (only the first time).  */
    281  1.1  christos 
    282  1.1  christos       if (reg->ctype == 0)
    283  1.1  christos 	{
    284  1.1  christos 	  struct ctype_cache *tp;
    285  1.1  christos 	  int size = reg->byte_size;
    286  1.1  christos 
    287  1.1  christos 	  /* We always use the memory representation,
    288  1.1  christos 	     even if the register width is smaller.  */
    289  1.1  christos 	  switch (size)
    290  1.1  christos 	    {
    291  1.1  christos 	    case 1:
    292  1.1  christos 	      reg->ctype = builtin_type (gdbarch)->builtin_uint8;
    293  1.1  christos 	      break;
    294  1.1  christos 
    295  1.1  christos 	    case 2:
    296  1.1  christos 	      reg->ctype = builtin_type (gdbarch)->builtin_uint16;
    297  1.1  christos 	      break;
    298  1.1  christos 
    299  1.1  christos 	    case 4:
    300  1.1  christos 	      reg->ctype = builtin_type (gdbarch)->builtin_uint32;
    301  1.1  christos 	      break;
    302  1.1  christos 
    303  1.1  christos 	    case 8:
    304  1.1  christos 	      reg->ctype = builtin_type (gdbarch)->builtin_uint64;
    305  1.1  christos 	      break;
    306  1.1  christos 
    307  1.1  christos 	    case 16:
    308  1.1  christos 	      reg->ctype = builtin_type (gdbarch)->builtin_uint128;
    309  1.1  christos 	      break;
    310  1.1  christos 
    311  1.1  christos 	    default:
    312  1.1  christos 	      for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
    313  1.1  christos 		if (tp->size == size)
    314  1.1  christos 		  break;
    315  1.1  christos 
    316  1.1  christos 	      if (tp == NULL)
    317  1.1  christos 		{
    318  1.8  christos 		  std::string name = string_printf ("int%d", size * 8);
    319  1.6  christos 
    320  1.6  christos 		  tp = XNEW (struct ctype_cache);
    321  1.1  christos 		  tp->next = tdep->type_entries;
    322  1.1  christos 		  tdep->type_entries = tp;
    323  1.1  christos 		  tp->size = size;
    324  1.1  christos 		  tp->virtual_type
    325  1.8  christos 		    = arch_integer_type (gdbarch, size * 8, 1, name.c_str ());
    326  1.1  christos 		}
    327  1.1  christos 
    328  1.1  christos 	      reg->ctype = tp->virtual_type;
    329  1.1  christos 	    }
    330  1.1  christos 	}
    331  1.1  christos       return reg->ctype;
    332  1.1  christos     }
    333  1.1  christos 
    334  1.1  christos   internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
    335  1.1  christos   return 0;
    336  1.1  christos }
    337  1.1  christos 
    338  1.1  christos 
    339  1.1  christos /* Return the 'local' register number for stubs, dwarf2, etc.
    340  1.1  christos    The debugging information enumerates registers starting from 0 for A0
    341  1.1  christos    to n for An.  So, we only have to add the base number for A0.  */
    342  1.1  christos 
    343  1.1  christos static int
    344  1.1  christos xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
    345  1.1  christos {
    346  1.1  christos   int i;
    347  1.1  christos 
    348  1.1  christos   if (regnum >= 0 && regnum < 16)
    349  1.1  christos     return gdbarch_tdep (gdbarch)->a0_base + regnum;
    350  1.1  christos 
    351  1.8  christos   for (i = 0; i < gdbarch_num_cooked_regs (gdbarch); i++)
    352  1.1  christos     if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
    353  1.1  christos       return i;
    354  1.1  christos 
    355  1.6  christos   return -1;
    356  1.1  christos }
    357  1.1  christos 
    358  1.1  christos 
    359  1.1  christos /* Write the bits of a masked register to the various registers.
    360  1.1  christos    Only the masked areas of these registers are modified; the other
    361  1.1  christos    fields are untouched.  The size of masked registers is always less
    362  1.1  christos    than or equal to 32 bits.  */
    363  1.1  christos 
    364  1.1  christos static void
    365  1.1  christos xtensa_register_write_masked (struct regcache *regcache,
    366  1.1  christos 			      xtensa_register_t *reg, const gdb_byte *buffer)
    367  1.1  christos {
    368  1.8  christos   unsigned int value[(XTENSA_MAX_REGISTER_SIZE + 3) / 4];
    369  1.1  christos   const xtensa_mask_t *mask = reg->mask;
    370  1.1  christos 
    371  1.1  christos   int shift = 0;		/* Shift for next mask (mod 32).  */
    372  1.1  christos   int start, size;		/* Start bit and size of current mask.  */
    373  1.1  christos 
    374  1.1  christos   unsigned int *ptr = value;
    375  1.1  christos   unsigned int regval, m, mem = 0;
    376  1.1  christos 
    377  1.1  christos   int bytesize = reg->byte_size;
    378  1.1  christos   int bitsize = bytesize * 8;
    379  1.1  christos   int i, r;
    380  1.1  christos 
    381  1.1  christos   DEBUGTRACE ("xtensa_register_write_masked ()\n");
    382  1.1  christos 
    383  1.1  christos   /* Copy the masked register to host byte-order.  */
    384  1.8  christos   if (gdbarch_byte_order (regcache->arch ()) == BFD_ENDIAN_BIG)
    385  1.1  christos     for (i = 0; i < bytesize; i++)
    386  1.1  christos       {
    387  1.1  christos 	mem >>= 8;
    388  1.1  christos 	mem |= (buffer[bytesize - i - 1] << 24);
    389  1.1  christos 	if ((i & 3) == 3)
    390  1.1  christos 	  *ptr++ = mem;
    391  1.1  christos       }
    392  1.1  christos   else
    393  1.1  christos     for (i = 0; i < bytesize; i++)
    394  1.1  christos       {
    395  1.1  christos 	mem >>= 8;
    396  1.1  christos 	mem |= (buffer[i] << 24);
    397  1.1  christos 	if ((i & 3) == 3)
    398  1.1  christos 	  *ptr++ = mem;
    399  1.1  christos       }
    400  1.1  christos 
    401  1.1  christos   /* We might have to shift the final value:
    402  1.1  christos      bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
    403  1.1  christos      bytesize & 3 == x -> shift (4-x) * 8.  */
    404  1.1  christos 
    405  1.1  christos   *ptr = mem >> (((0 - bytesize) & 3) * 8);
    406  1.1  christos   ptr = value;
    407  1.1  christos   mem = *ptr;
    408  1.1  christos 
    409  1.1  christos   /* Write the bits to the masked areas of the other registers.  */
    410  1.1  christos   for (i = 0; i < mask->count; i++)
    411  1.1  christos     {
    412  1.1  christos       start = mask->mask[i].bit_start;
    413  1.1  christos       size = mask->mask[i].bit_size;
    414  1.1  christos       regval = mem >> shift;
    415  1.1  christos 
    416  1.1  christos       if ((shift += size) > bitsize)
    417  1.1  christos 	error (_("size of all masks is larger than the register"));
    418  1.1  christos 
    419  1.1  christos       if (shift >= 32)
    420  1.1  christos 	{
    421  1.1  christos 	  mem = *(++ptr);
    422  1.1  christos 	  shift -= 32;
    423  1.1  christos 	  bitsize -= 32;
    424  1.1  christos 
    425  1.1  christos 	  if (shift > 0)
    426  1.1  christos 	    regval |= mem << (size - shift);
    427  1.1  christos 	}
    428  1.1  christos 
    429  1.1  christos       /* Make sure we have a valid register.  */
    430  1.1  christos       r = mask->mask[i].reg_num;
    431  1.1  christos       if (r >= 0 && size > 0)
    432  1.1  christos 	{
    433  1.1  christos 	  /* Don't overwrite the unmasked areas.  */
    434  1.1  christos 	  ULONGEST old_val;
    435  1.1  christos 	  regcache_cooked_read_unsigned (regcache, r, &old_val);
    436  1.1  christos 	  m = 0xffffffff >> (32 - size) << start;
    437  1.1  christos 	  regval <<= start;
    438  1.1  christos 	  regval = (regval & m) | (old_val & ~m);
    439  1.1  christos 	  regcache_cooked_write_unsigned (regcache, r, regval);
    440  1.1  christos 	}
    441  1.1  christos     }
    442  1.1  christos }
    443  1.1  christos 
    444  1.1  christos 
    445  1.1  christos /* Read a tie state or mapped registers.  Read the masked areas
    446  1.1  christos    of the registers and assemble them into a single value.  */
    447  1.1  christos 
    448  1.1  christos static enum register_status
    449  1.8  christos xtensa_register_read_masked (readable_regcache *regcache,
    450  1.1  christos 			     xtensa_register_t *reg, gdb_byte *buffer)
    451  1.1  christos {
    452  1.8  christos   unsigned int value[(XTENSA_MAX_REGISTER_SIZE + 3) / 4];
    453  1.1  christos   const xtensa_mask_t *mask = reg->mask;
    454  1.1  christos 
    455  1.1  christos   int shift = 0;
    456  1.1  christos   int start, size;
    457  1.1  christos 
    458  1.1  christos   unsigned int *ptr = value;
    459  1.1  christos   unsigned int regval, mem = 0;
    460  1.1  christos 
    461  1.1  christos   int bytesize = reg->byte_size;
    462  1.1  christos   int bitsize = bytesize * 8;
    463  1.1  christos   int i;
    464  1.1  christos 
    465  1.1  christos   DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
    466  1.1  christos 	      reg->name == 0 ? "" : reg->name);
    467  1.1  christos 
    468  1.1  christos   /* Assemble the register from the masked areas of other registers.  */
    469  1.1  christos   for (i = 0; i < mask->count; i++)
    470  1.1  christos     {
    471  1.1  christos       int r = mask->mask[i].reg_num;
    472  1.1  christos       if (r >= 0)
    473  1.1  christos 	{
    474  1.1  christos 	  enum register_status status;
    475  1.1  christos 	  ULONGEST val;
    476  1.1  christos 
    477  1.8  christos 	  status = regcache->cooked_read (r, &val);
    478  1.1  christos 	  if (status != REG_VALID)
    479  1.1  christos 	    return status;
    480  1.1  christos 	  regval = (unsigned int) val;
    481  1.1  christos 	}
    482  1.1  christos       else
    483  1.1  christos 	regval = 0;
    484  1.1  christos 
    485  1.1  christos       start = mask->mask[i].bit_start;
    486  1.1  christos       size = mask->mask[i].bit_size;
    487  1.1  christos 
    488  1.1  christos       regval >>= start;
    489  1.1  christos 
    490  1.1  christos       if (size < 32)
    491  1.1  christos 	regval &= (0xffffffff >> (32 - size));
    492  1.1  christos 
    493  1.1  christos       mem |= regval << shift;
    494  1.1  christos 
    495  1.1  christos       if ((shift += size) > bitsize)
    496  1.1  christos 	error (_("size of all masks is larger than the register"));
    497  1.1  christos 
    498  1.1  christos       if (shift >= 32)
    499  1.1  christos 	{
    500  1.1  christos 	  *ptr++ = mem;
    501  1.1  christos 	  bitsize -= 32;
    502  1.1  christos 	  shift -= 32;
    503  1.1  christos 
    504  1.1  christos 	  if (shift == 0)
    505  1.1  christos 	    mem = 0;
    506  1.1  christos 	  else
    507  1.1  christos 	    mem = regval >> (size - shift);
    508  1.1  christos 	}
    509  1.1  christos     }
    510  1.1  christos 
    511  1.1  christos   if (shift > 0)
    512  1.1  christos     *ptr = mem;
    513  1.1  christos 
    514  1.1  christos   /* Copy value to target byte order.  */
    515  1.1  christos   ptr = value;
    516  1.1  christos   mem = *ptr;
    517  1.1  christos 
    518  1.8  christos   if (gdbarch_byte_order (regcache->arch ()) == BFD_ENDIAN_BIG)
    519  1.1  christos     for (i = 0; i < bytesize; i++)
    520  1.1  christos       {
    521  1.1  christos 	if ((i & 3) == 0)
    522  1.1  christos 	  mem = *ptr++;
    523  1.1  christos 	buffer[bytesize - i - 1] = mem & 0xff;
    524  1.1  christos 	mem >>= 8;
    525  1.1  christos       }
    526  1.1  christos   else
    527  1.1  christos     for (i = 0; i < bytesize; i++)
    528  1.1  christos       {
    529  1.1  christos 	if ((i & 3) == 0)
    530  1.1  christos 	  mem = *ptr++;
    531  1.1  christos 	buffer[i] = mem & 0xff;
    532  1.1  christos 	mem >>= 8;
    533  1.1  christos       }
    534  1.1  christos 
    535  1.1  christos   return REG_VALID;
    536  1.1  christos }
    537  1.1  christos 
    538  1.1  christos 
    539  1.1  christos /* Read pseudo registers.  */
    540  1.1  christos 
    541  1.1  christos static enum register_status
    542  1.1  christos xtensa_pseudo_register_read (struct gdbarch *gdbarch,
    543  1.8  christos 			     readable_regcache *regcache,
    544  1.1  christos 			     int regnum,
    545  1.1  christos 			     gdb_byte *buffer)
    546  1.1  christos {
    547  1.1  christos   DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
    548  1.1  christos 	      regnum, xtensa_register_name (gdbarch, regnum));
    549  1.1  christos 
    550  1.1  christos   /* Read aliases a0..a15, if this is a Windowed ABI.  */
    551  1.1  christos   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
    552  1.1  christos       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
    553  1.1  christos       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
    554  1.1  christos     {
    555  1.8  christos       ULONGEST value;
    556  1.1  christos       enum register_status status;
    557  1.1  christos 
    558  1.8  christos       status = regcache->raw_read (gdbarch_tdep (gdbarch)->wb_regnum,
    559  1.8  christos 				   &value);
    560  1.1  christos       if (status != REG_VALID)
    561  1.1  christos 	return status;
    562  1.8  christos       regnum = arreg_number (gdbarch, regnum, value);
    563  1.1  christos     }
    564  1.1  christos 
    565  1.1  christos   /* We can always read non-pseudo registers.  */
    566  1.1  christos   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
    567  1.8  christos     return regcache->raw_read (regnum, buffer);
    568  1.1  christos 
    569  1.1  christos   /* We have to find out how to deal with priveleged registers.
    570  1.1  christos      Let's treat them as pseudo-registers, but we cannot read/write them.  */
    571  1.1  christos 
    572  1.7  christos   else if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only
    573  1.7  christos 	   || regnum < gdbarch_tdep (gdbarch)->a0_base)
    574  1.1  christos     {
    575  1.1  christos       buffer[0] = (gdb_byte)0;
    576  1.1  christos       buffer[1] = (gdb_byte)0;
    577  1.1  christos       buffer[2] = (gdb_byte)0;
    578  1.1  christos       buffer[3] = (gdb_byte)0;
    579  1.1  christos       return REG_VALID;
    580  1.1  christos     }
    581  1.1  christos   /* Pseudo registers.  */
    582  1.8  christos   else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
    583  1.1  christos     {
    584  1.1  christos       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
    585  1.1  christos       xtensa_register_type_t type = reg->type;
    586  1.1  christos       int flags = gdbarch_tdep (gdbarch)->target_flags;
    587  1.1  christos 
    588  1.1  christos       /* We cannot read Unknown or Unmapped registers.  */
    589  1.1  christos       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
    590  1.1  christos 	{
    591  1.1  christos 	  if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
    592  1.1  christos 	    {
    593  1.1  christos 	      warning (_("cannot read register %s"),
    594  1.1  christos 		       xtensa_register_name (gdbarch, regnum));
    595  1.1  christos 	      return REG_VALID;
    596  1.1  christos 	    }
    597  1.1  christos 	}
    598  1.1  christos 
    599  1.1  christos       /* Some targets cannot read TIE register files.  */
    600  1.1  christos       else if (type == xtRegisterTypeTieRegfile)
    601  1.1  christos         {
    602  1.1  christos 	  /* Use 'fetch' to get register?  */
    603  1.1  christos 	  if (flags & xtTargetFlagsUseFetchStore)
    604  1.1  christos 	    {
    605  1.1  christos 	      warning (_("cannot read register"));
    606  1.1  christos 	      return REG_VALID;
    607  1.1  christos 	    }
    608  1.1  christos 
    609  1.1  christos 	  /* On some targets (esp. simulators), we can always read the reg.  */
    610  1.1  christos 	  else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
    611  1.1  christos 	    {
    612  1.1  christos 	      warning (_("cannot read register"));
    613  1.1  christos 	      return REG_VALID;
    614  1.1  christos 	    }
    615  1.1  christos 	}
    616  1.1  christos 
    617  1.1  christos       /* We can always read mapped registers.  */
    618  1.1  christos       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
    619  1.1  christos 	return xtensa_register_read_masked (regcache, reg, buffer);
    620  1.1  christos 
    621  1.1  christos       /* Assume that we can read the register.  */
    622  1.8  christos       return regcache->raw_read (regnum, buffer);
    623  1.1  christos     }
    624  1.1  christos   else
    625  1.1  christos     internal_error (__FILE__, __LINE__,
    626  1.1  christos 		    _("invalid register number %d"), regnum);
    627  1.1  christos }
    628  1.1  christos 
    629  1.1  christos 
    630  1.1  christos /* Write pseudo registers.  */
    631  1.1  christos 
    632  1.1  christos static void
    633  1.1  christos xtensa_pseudo_register_write (struct gdbarch *gdbarch,
    634  1.1  christos 			      struct regcache *regcache,
    635  1.1  christos 			      int regnum,
    636  1.1  christos 			      const gdb_byte *buffer)
    637  1.1  christos {
    638  1.1  christos   DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
    639  1.1  christos 	      regnum, xtensa_register_name (gdbarch, regnum));
    640  1.1  christos 
    641  1.1  christos   /* Renumber register, if aliase a0..a15 on Windowed ABI.  */
    642  1.1  christos   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
    643  1.1  christos       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
    644  1.1  christos       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
    645  1.1  christos     {
    646  1.8  christos       ULONGEST value;
    647  1.8  christos       regcache_raw_read_unsigned (regcache,
    648  1.8  christos 				  gdbarch_tdep (gdbarch)->wb_regnum, &value);
    649  1.8  christos       regnum = arreg_number (gdbarch, regnum, value);
    650  1.1  christos     }
    651  1.1  christos 
    652  1.1  christos   /* We can always write 'core' registers.
    653  1.1  christos      Note: We might have converted Ax->ARy.  */
    654  1.1  christos   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
    655  1.8  christos     regcache->raw_write (regnum, buffer);
    656  1.1  christos 
    657  1.1  christos   /* We have to find out how to deal with priveleged registers.
    658  1.1  christos      Let's treat them as pseudo-registers, but we cannot read/write them.  */
    659  1.1  christos 
    660  1.1  christos   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
    661  1.1  christos     {
    662  1.1  christos       return;
    663  1.1  christos     }
    664  1.1  christos   /* Pseudo registers.  */
    665  1.8  christos   else if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch))
    666  1.1  christos     {
    667  1.1  christos       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
    668  1.1  christos       xtensa_register_type_t type = reg->type;
    669  1.1  christos       int flags = gdbarch_tdep (gdbarch)->target_flags;
    670  1.1  christos 
    671  1.1  christos       /* On most targets, we cannot write registers
    672  1.1  christos 	 of type "Unknown" or "Unmapped".  */
    673  1.1  christos       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
    674  1.1  christos         {
    675  1.1  christos 	  if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
    676  1.1  christos 	    {
    677  1.1  christos 	      warning (_("cannot write register %s"),
    678  1.1  christos 		       xtensa_register_name (gdbarch, regnum));
    679  1.1  christos 	      return;
    680  1.1  christos 	    }
    681  1.1  christos 	}
    682  1.1  christos 
    683  1.1  christos       /* Some targets cannot read TIE register files.  */
    684  1.1  christos       else if (type == xtRegisterTypeTieRegfile)
    685  1.1  christos         {
    686  1.1  christos 	  /* Use 'store' to get register?  */
    687  1.1  christos 	  if (flags & xtTargetFlagsUseFetchStore)
    688  1.1  christos 	    {
    689  1.1  christos 	      warning (_("cannot write register"));
    690  1.1  christos 	      return;
    691  1.1  christos 	    }
    692  1.1  christos 
    693  1.1  christos 	  /* On some targets (esp. simulators), we can always write
    694  1.1  christos 	     the register.  */
    695  1.1  christos 	  else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
    696  1.1  christos 	    {
    697  1.1  christos 	      warning (_("cannot write register"));
    698  1.1  christos 	      return;
    699  1.1  christos 	    }
    700  1.1  christos 	}
    701  1.1  christos 
    702  1.1  christos       /* We can always write mapped registers.  */
    703  1.1  christos       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
    704  1.1  christos         {
    705  1.1  christos 	  xtensa_register_write_masked (regcache, reg, buffer);
    706  1.1  christos 	  return;
    707  1.1  christos 	}
    708  1.1  christos 
    709  1.1  christos       /* Assume that we can write the register.  */
    710  1.8  christos       regcache->raw_write (regnum, buffer);
    711  1.1  christos     }
    712  1.1  christos   else
    713  1.1  christos     internal_error (__FILE__, __LINE__,
    714  1.1  christos 		    _("invalid register number %d"), regnum);
    715  1.1  christos }
    716  1.1  christos 
    717  1.1  christos static struct reggroup *xtensa_ar_reggroup;
    718  1.1  christos static struct reggroup *xtensa_user_reggroup;
    719  1.1  christos static struct reggroup *xtensa_vectra_reggroup;
    720  1.1  christos static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
    721  1.1  christos 
    722  1.1  christos static void
    723  1.1  christos xtensa_init_reggroups (void)
    724  1.1  christos {
    725  1.1  christos   int i;
    726  1.1  christos 
    727  1.1  christos   xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
    728  1.1  christos   xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
    729  1.1  christos   xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
    730  1.1  christos 
    731  1.1  christos   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
    732  1.8  christos     xtensa_cp[i] = reggroup_new (xstrprintf ("cp%d", i), USER_REGGROUP);
    733  1.1  christos }
    734  1.1  christos 
    735  1.1  christos static void
    736  1.1  christos xtensa_add_reggroups (struct gdbarch *gdbarch)
    737  1.1  christos {
    738  1.1  christos   int i;
    739  1.1  christos 
    740  1.1  christos   /* Predefined groups.  */
    741  1.1  christos   reggroup_add (gdbarch, all_reggroup);
    742  1.1  christos   reggroup_add (gdbarch, save_reggroup);
    743  1.1  christos   reggroup_add (gdbarch, restore_reggroup);
    744  1.1  christos   reggroup_add (gdbarch, system_reggroup);
    745  1.1  christos   reggroup_add (gdbarch, vector_reggroup);
    746  1.1  christos   reggroup_add (gdbarch, general_reggroup);
    747  1.1  christos   reggroup_add (gdbarch, float_reggroup);
    748  1.1  christos 
    749  1.1  christos   /* Xtensa-specific groups.  */
    750  1.1  christos   reggroup_add (gdbarch, xtensa_ar_reggroup);
    751  1.1  christos   reggroup_add (gdbarch, xtensa_user_reggroup);
    752  1.1  christos   reggroup_add (gdbarch, xtensa_vectra_reggroup);
    753  1.1  christos 
    754  1.1  christos   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
    755  1.1  christos     reggroup_add (gdbarch, xtensa_cp[i]);
    756  1.1  christos }
    757  1.1  christos 
    758  1.1  christos static int
    759  1.1  christos xtensa_coprocessor_register_group (struct reggroup *group)
    760  1.1  christos {
    761  1.1  christos   int i;
    762  1.1  christos 
    763  1.1  christos   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
    764  1.1  christos     if (group == xtensa_cp[i])
    765  1.1  christos       return i;
    766  1.1  christos 
    767  1.1  christos   return -1;
    768  1.1  christos }
    769  1.1  christos 
    770  1.1  christos #define SAVE_REST_FLAGS	(XTENSA_REGISTER_FLAGS_READABLE \
    771  1.1  christos 			| XTENSA_REGISTER_FLAGS_WRITABLE \
    772  1.1  christos 			| XTENSA_REGISTER_FLAGS_VOLATILE)
    773  1.1  christos 
    774  1.1  christos #define SAVE_REST_VALID	(XTENSA_REGISTER_FLAGS_READABLE \
    775  1.1  christos 			| XTENSA_REGISTER_FLAGS_WRITABLE)
    776  1.1  christos 
    777  1.1  christos static int
    778  1.1  christos xtensa_register_reggroup_p (struct gdbarch *gdbarch,
    779  1.1  christos 			    int regnum,
    780  1.1  christos     			    struct reggroup *group)
    781  1.1  christos {
    782  1.1  christos   xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
    783  1.1  christos   xtensa_register_type_t type = reg->type;
    784  1.1  christos   xtensa_register_group_t rg = reg->group;
    785  1.1  christos   int cp_number;
    786  1.1  christos 
    787  1.1  christos   if (group == save_reggroup)
    788  1.1  christos     /* Every single register should be included into the list of registers
    789  1.1  christos        to be watched for changes while using -data-list-changed-registers.  */
    790  1.1  christos     return 1;
    791  1.1  christos 
    792  1.1  christos   /* First, skip registers that are not visible to this target
    793  1.1  christos      (unknown and unmapped registers when not using ISS).  */
    794  1.1  christos 
    795  1.1  christos   if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
    796  1.1  christos     return 0;
    797  1.1  christos   if (group == all_reggroup)
    798  1.1  christos     return 1;
    799  1.1  christos   if (group == xtensa_ar_reggroup)
    800  1.1  christos     return rg & xtRegisterGroupAddrReg;
    801  1.1  christos   if (group == xtensa_user_reggroup)
    802  1.1  christos     return rg & xtRegisterGroupUser;
    803  1.1  christos   if (group == float_reggroup)
    804  1.1  christos     return rg & xtRegisterGroupFloat;
    805  1.1  christos   if (group == general_reggroup)
    806  1.1  christos     return rg & xtRegisterGroupGeneral;
    807  1.1  christos   if (group == system_reggroup)
    808  1.1  christos     return rg & xtRegisterGroupState;
    809  1.1  christos   if (group == vector_reggroup || group == xtensa_vectra_reggroup)
    810  1.1  christos     return rg & xtRegisterGroupVectra;
    811  1.1  christos   if (group == restore_reggroup)
    812  1.1  christos     return (regnum < gdbarch_num_regs (gdbarch)
    813  1.1  christos 	    && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
    814  1.1  christos   cp_number = xtensa_coprocessor_register_group (group);
    815  1.1  christos   if (cp_number >= 0)
    816  1.1  christos     return rg & (xtRegisterGroupCP0 << cp_number);
    817  1.1  christos   else
    818  1.1  christos     return 1;
    819  1.1  christos }
    820  1.1  christos 
    821  1.1  christos 
    822  1.1  christos /* Supply register REGNUM from the buffer specified by GREGS and LEN
    823  1.1  christos    in the general-purpose register set REGSET to register cache
    824  1.1  christos    REGCACHE.  If REGNUM is -1 do this for all registers in REGSET.  */
    825  1.1  christos 
    826  1.1  christos static void
    827  1.1  christos xtensa_supply_gregset (const struct regset *regset,
    828  1.1  christos 		       struct regcache *rc,
    829  1.1  christos 		       int regnum,
    830  1.1  christos 		       const void *gregs,
    831  1.1  christos 		       size_t len)
    832  1.1  christos {
    833  1.6  christos   const xtensa_elf_gregset_t *regs = (const xtensa_elf_gregset_t *) gregs;
    834  1.8  christos   struct gdbarch *gdbarch = rc->arch ();
    835  1.1  christos   int i;
    836  1.1  christos 
    837  1.1  christos   DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
    838  1.1  christos 
    839  1.1  christos   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
    840  1.8  christos     rc->raw_supply (gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
    841  1.1  christos   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
    842  1.8  christos     rc->raw_supply (gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
    843  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
    844  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->wb_regnum,
    845  1.8  christos 		    (char *) &regs->windowbase);
    846  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
    847  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->ws_regnum,
    848  1.8  christos 		    (char *) &regs->windowstart);
    849  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
    850  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->lbeg_regnum,
    851  1.8  christos 		    (char *) &regs->lbeg);
    852  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
    853  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->lend_regnum,
    854  1.8  christos 		    (char *) &regs->lend);
    855  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
    856  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->lcount_regnum,
    857  1.8  christos 		    (char *) &regs->lcount);
    858  1.1  christos   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
    859  1.8  christos     rc->raw_supply (gdbarch_tdep (gdbarch)->sar_regnum,
    860  1.8  christos 		    (char *) &regs->sar);
    861  1.1  christos   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
    862  1.1  christos       && regnum < gdbarch_tdep (gdbarch)->ar_base
    863  1.1  christos 		    + gdbarch_tdep (gdbarch)->num_aregs)
    864  1.8  christos     rc->raw_supply
    865  1.8  christos       (regnum, (char *) &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
    866  1.1  christos   else if (regnum == -1)
    867  1.1  christos     {
    868  1.1  christos       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
    869  1.8  christos 	rc->raw_supply (gdbarch_tdep (gdbarch)->ar_base + i,
    870  1.8  christos 			(char *) &regs->ar[i]);
    871  1.1  christos     }
    872  1.1  christos }
    873  1.1  christos 
    874  1.1  christos 
    875  1.1  christos /* Xtensa register set.  */
    876  1.1  christos 
    877  1.1  christos static struct regset
    878  1.1  christos xtensa_gregset =
    879  1.1  christos {
    880  1.1  christos   NULL,
    881  1.1  christos   xtensa_supply_gregset
    882  1.1  christos };
    883  1.1  christos 
    884  1.1  christos 
    885  1.3  christos /* Iterate over supported core file register note sections. */
    886  1.1  christos 
    887  1.3  christos static void
    888  1.3  christos xtensa_iterate_over_regset_sections (struct gdbarch *gdbarch,
    889  1.3  christos 				     iterate_over_regset_sections_cb *cb,
    890  1.3  christos 				     void *cb_data,
    891  1.3  christos 				     const struct regcache *regcache)
    892  1.3  christos {
    893  1.3  christos   DEBUGTRACE ("xtensa_iterate_over_regset_sections\n");
    894  1.1  christos 
    895  1.8  christos   cb (".reg", sizeof (xtensa_elf_gregset_t), sizeof (xtensa_elf_gregset_t),
    896  1.8  christos       &xtensa_gregset, NULL, cb_data);
    897  1.1  christos }
    898  1.1  christos 
    899  1.1  christos 
    900  1.1  christos /* Handling frames.  */
    901  1.1  christos 
    902  1.1  christos /* Number of registers to save in case of Windowed ABI.  */
    903  1.1  christos #define XTENSA_NUM_SAVED_AREGS		12
    904  1.1  christos 
    905  1.1  christos /* Frame cache part for Windowed ABI.  */
    906  1.1  christos typedef struct xtensa_windowed_frame_cache
    907  1.1  christos {
    908  1.1  christos   int wb;		/* WINDOWBASE of the previous frame.  */
    909  1.1  christos   int callsize;		/* Call size of this frame.  */
    910  1.1  christos   int ws;		/* WINDOWSTART of the previous frame.  It keeps track of
    911  1.1  christos 			   life windows only.  If there is no bit set for the
    912  1.1  christos 			   window,  that means it had been already spilled
    913  1.1  christos 			   because of window overflow.  */
    914  1.1  christos 
    915  1.1  christos    /* Addresses of spilled A-registers.
    916  1.1  christos       AREGS[i] == -1, if corresponding AR is alive.  */
    917  1.1  christos   CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
    918  1.1  christos } xtensa_windowed_frame_cache_t;
    919  1.1  christos 
    920  1.1  christos /* Call0 ABI Definitions.  */
    921  1.1  christos 
    922  1.1  christos #define C0_MAXOPDS  3	/* Maximum number of operands for prologue
    923  1.1  christos 			   analysis.  */
    924  1.1  christos #define C0_CLESV   12	/* Callee-saved registers are here and up.  */
    925  1.1  christos #define C0_SP	    1	/* Register used as SP.  */
    926  1.1  christos #define C0_FP	   15	/* Register used as FP.  */
    927  1.1  christos #define C0_RA	    0	/* Register used as return address.  */
    928  1.1  christos #define C0_ARGS	    2	/* Register used as first arg/retval.  */
    929  1.1  christos #define C0_NARGS    6	/* Number of A-regs for args/retvals.  */
    930  1.1  christos 
    931  1.1  christos /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
    932  1.1  christos    A-register where the current content of the reg came from (in terms
    933  1.1  christos    of an original reg and a constant).  Negative values of c0_rt[n].fp_reg
    934  1.1  christos    mean that the orignal content of the register was saved to the stack.
    935  1.1  christos    c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
    936  1.1  christos    know where SP will end up until the entire prologue has been analyzed.  */
    937  1.1  christos 
    938  1.1  christos #define C0_CONST   -1	/* fr_reg value if register contains a constant.  */
    939  1.1  christos #define C0_INEXP   -2	/* fr_reg value if inexpressible as reg + offset.  */
    940  1.1  christos #define C0_NOSTK   -1	/* to_stk value if register has not been stored.  */
    941  1.1  christos 
    942  1.1  christos extern xtensa_isa xtensa_default_isa;
    943  1.1  christos 
    944  1.1  christos typedef struct xtensa_c0reg
    945  1.1  christos {
    946  1.1  christos   int fr_reg;  /* original register from which register content
    947  1.1  christos 		  is derived, or C0_CONST, or C0_INEXP.  */
    948  1.1  christos   int fr_ofs;  /* constant offset from reg, or immediate value.  */
    949  1.1  christos   int to_stk;  /* offset from original SP to register (4-byte aligned),
    950  1.1  christos 		  or C0_NOSTK if register has not been saved.  */
    951  1.1  christos } xtensa_c0reg_t;
    952  1.1  christos 
    953  1.1  christos /* Frame cache part for Call0 ABI.  */
    954  1.1  christos typedef struct xtensa_call0_frame_cache
    955  1.1  christos {
    956  1.1  christos   int c0_frmsz;			   /* Stack frame size.  */
    957  1.1  christos   int c0_hasfp;			   /* Current frame uses frame pointer.  */
    958  1.1  christos   int fp_regnum;		   /* A-register used as FP.  */
    959  1.1  christos   int c0_fp;			   /* Actual value of frame pointer.  */
    960  1.1  christos   int c0_fpalign;		   /* Dinamic adjustment for the stack
    961  1.1  christos 				      pointer. It's an AND mask. Zero,
    962  1.1  christos 				      if alignment was not adjusted.  */
    963  1.1  christos   int c0_old_sp;		   /* In case of dynamic adjustment, it is
    964  1.1  christos 				      a register holding unaligned sp.
    965  1.1  christos 				      C0_INEXP, when undefined.  */
    966  1.1  christos   int c0_sp_ofs;		   /* If "c0_old_sp" was spilled it's a
    967  1.1  christos 				      stack offset. C0_NOSTK otherwise.  */
    968  1.1  christos 
    969  1.1  christos   xtensa_c0reg_t c0_rt[C0_NREGS];  /* Register tracking information.  */
    970  1.1  christos } xtensa_call0_frame_cache_t;
    971  1.1  christos 
    972  1.1  christos typedef struct xtensa_frame_cache
    973  1.1  christos {
    974  1.1  christos   CORE_ADDR base;	/* Stack pointer of this frame.  */
    975  1.1  christos   CORE_ADDR pc;		/* PC of this frame at the function entry point.  */
    976  1.1  christos   CORE_ADDR ra;		/* The raw return address of this frame.  */
    977  1.1  christos   CORE_ADDR ps;		/* The PS register of the previous (older) frame.  */
    978  1.1  christos   CORE_ADDR prev_sp;	/* Stack Pointer of the previous (older) frame.  */
    979  1.1  christos   int call0;		/* It's a call0 framework (else windowed).  */
    980  1.1  christos   union
    981  1.1  christos     {
    982  1.1  christos       xtensa_windowed_frame_cache_t	wd;	/* call0 == false.  */
    983  1.1  christos       xtensa_call0_frame_cache_t       	c0;	/* call0 == true.  */
    984  1.1  christos     };
    985  1.1  christos } xtensa_frame_cache_t;
    986  1.1  christos 
    987  1.1  christos 
    988  1.1  christos static struct xtensa_frame_cache *
    989  1.1  christos xtensa_alloc_frame_cache (int windowed)
    990  1.1  christos {
    991  1.1  christos   xtensa_frame_cache_t *cache;
    992  1.1  christos   int i;
    993  1.1  christos 
    994  1.1  christos   DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
    995  1.1  christos 
    996  1.1  christos   cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
    997  1.1  christos 
    998  1.1  christos   cache->base = 0;
    999  1.1  christos   cache->pc = 0;
   1000  1.1  christos   cache->ra = 0;
   1001  1.1  christos   cache->ps = 0;
   1002  1.1  christos   cache->prev_sp = 0;
   1003  1.1  christos   cache->call0 = !windowed;
   1004  1.1  christos   if (cache->call0)
   1005  1.1  christos     {
   1006  1.1  christos       cache->c0.c0_frmsz  = -1;
   1007  1.1  christos       cache->c0.c0_hasfp  =  0;
   1008  1.1  christos       cache->c0.fp_regnum = -1;
   1009  1.1  christos       cache->c0.c0_fp     = -1;
   1010  1.1  christos       cache->c0.c0_fpalign =  0;
   1011  1.1  christos       cache->c0.c0_old_sp  =  C0_INEXP;
   1012  1.1  christos       cache->c0.c0_sp_ofs  =  C0_NOSTK;
   1013  1.1  christos 
   1014  1.1  christos       for (i = 0; i < C0_NREGS; i++)
   1015  1.1  christos 	{
   1016  1.1  christos 	  cache->c0.c0_rt[i].fr_reg = i;
   1017  1.1  christos 	  cache->c0.c0_rt[i].fr_ofs = 0;
   1018  1.1  christos 	  cache->c0.c0_rt[i].to_stk = C0_NOSTK;
   1019  1.1  christos 	}
   1020  1.1  christos     }
   1021  1.1  christos   else
   1022  1.1  christos     {
   1023  1.1  christos       cache->wd.wb = 0;
   1024  1.1  christos       cache->wd.ws = 0;
   1025  1.1  christos       cache->wd.callsize = -1;
   1026  1.1  christos 
   1027  1.1  christos       for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
   1028  1.1  christos 	cache->wd.aregs[i] = -1;
   1029  1.1  christos     }
   1030  1.1  christos   return cache;
   1031  1.1  christos }
   1032  1.1  christos 
   1033  1.1  christos 
   1034  1.1  christos static CORE_ADDR
   1035  1.1  christos xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
   1036  1.1  christos {
   1037  1.1  christos   return address & ~15;
   1038  1.1  christos }
   1039  1.1  christos 
   1040  1.1  christos 
   1041  1.1  christos static CORE_ADDR
   1042  1.1  christos xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
   1043  1.1  christos {
   1044  1.1  christos   gdb_byte buf[8];
   1045  1.1  christos   CORE_ADDR pc;
   1046  1.1  christos 
   1047  1.1  christos   DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
   1048  1.1  christos 		host_address_to_string (next_frame));
   1049  1.1  christos 
   1050  1.1  christos   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
   1051  1.1  christos   pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
   1052  1.1  christos 
   1053  1.1  christos   DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
   1054  1.1  christos 
   1055  1.1  christos   return pc;
   1056  1.1  christos }
   1057  1.1  christos 
   1058  1.1  christos 
   1059  1.1  christos static struct frame_id
   1060  1.1  christos xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
   1061  1.1  christos {
   1062  1.1  christos   CORE_ADDR pc, fp;
   1063  1.1  christos 
   1064  1.1  christos   /* THIS-FRAME is a dummy frame.  Return a frame ID of that frame.  */
   1065  1.1  christos 
   1066  1.1  christos   pc = get_frame_pc (this_frame);
   1067  1.1  christos   fp = get_frame_register_unsigned
   1068  1.1  christos 	 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
   1069  1.1  christos 
   1070  1.1  christos   /* Make dummy frame ID unique by adding a constant.  */
   1071  1.1  christos   return frame_id_build (fp + SP_ALIGNMENT, pc);
   1072  1.1  christos }
   1073  1.1  christos 
   1074  1.1  christos /* Returns true,  if instruction to execute next is unique to Xtensa Window
   1075  1.1  christos    Interrupt Handlers.  It can only be one of L32E,  S32E,  RFWO,  or RFWU.  */
   1076  1.1  christos 
   1077  1.1  christos static int
   1078  1.1  christos xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
   1079  1.1  christos {
   1080  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1081  1.1  christos   unsigned int insn = read_memory_integer (pc, 4, byte_order);
   1082  1.1  christos   unsigned int code;
   1083  1.1  christos 
   1084  1.1  christos   if (byte_order == BFD_ENDIAN_BIG)
   1085  1.1  christos     {
   1086  1.1  christos       /* Check, if this is L32E or S32E.  */
   1087  1.1  christos       code = insn & 0xf000ff00;
   1088  1.1  christos       if ((code == 0x00009000) || (code == 0x00009400))
   1089  1.1  christos 	return 1;
   1090  1.1  christos       /* Check, if this is RFWU or RFWO.  */
   1091  1.1  christos       code = insn & 0xffffff00;
   1092  1.1  christos       return ((code == 0x00430000) || (code == 0x00530000));
   1093  1.1  christos     }
   1094  1.1  christos   else
   1095  1.1  christos     {
   1096  1.1  christos       /* Check, if this is L32E or S32E.  */
   1097  1.1  christos       code = insn & 0x00ff000f;
   1098  1.1  christos       if ((code == 0x090000) || (code == 0x490000))
   1099  1.1  christos 	return 1;
   1100  1.1  christos       /* Check, if this is RFWU or RFWO.  */
   1101  1.1  christos       code = insn & 0x00ffffff;
   1102  1.1  christos       return ((code == 0x00003400) || (code == 0x00003500));
   1103  1.1  christos     }
   1104  1.1  christos }
   1105  1.1  christos 
   1106  1.1  christos /* Returns the best guess about which register is a frame pointer
   1107  1.1  christos    for the function containing CURRENT_PC.  */
   1108  1.1  christos 
   1109  1.1  christos #define XTENSA_ISA_BSZ		32		/* Instruction buffer size.  */
   1110  1.1  christos #define XTENSA_ISA_BADPC	((CORE_ADDR)0)	/* Bad PC value.  */
   1111  1.1  christos 
   1112  1.1  christos static unsigned int
   1113  1.1  christos xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
   1114  1.1  christos {
   1115  1.1  christos #define RETURN_FP goto done
   1116  1.1  christos 
   1117  1.1  christos   unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
   1118  1.1  christos   CORE_ADDR start_addr;
   1119  1.1  christos   xtensa_isa isa;
   1120  1.1  christos   xtensa_insnbuf ins, slot;
   1121  1.1  christos   gdb_byte ibuf[XTENSA_ISA_BSZ];
   1122  1.1  christos   CORE_ADDR ia, bt, ba;
   1123  1.1  christos   xtensa_format ifmt;
   1124  1.1  christos   int ilen, islots, is;
   1125  1.1  christos   xtensa_opcode opc;
   1126  1.1  christos   const char *opcname;
   1127  1.1  christos 
   1128  1.1  christos   find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
   1129  1.1  christos   if (start_addr == 0)
   1130  1.1  christos     return fp_regnum;
   1131  1.1  christos 
   1132  1.1  christos   isa = xtensa_default_isa;
   1133  1.1  christos   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
   1134  1.1  christos   ins = xtensa_insnbuf_alloc (isa);
   1135  1.1  christos   slot = xtensa_insnbuf_alloc (isa);
   1136  1.1  christos   ba = 0;
   1137  1.1  christos 
   1138  1.1  christos   for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
   1139  1.1  christos     {
   1140  1.1  christos       if (ia + xtensa_isa_maxlength (isa) > bt)
   1141  1.1  christos         {
   1142  1.1  christos 	  ba = ia;
   1143  1.1  christos 	  bt = (ba + XTENSA_ISA_BSZ) < current_pc
   1144  1.1  christos 	    ? ba + XTENSA_ISA_BSZ : current_pc;
   1145  1.1  christos 	  if (target_read_memory (ba, ibuf, bt - ba) != 0)
   1146  1.1  christos 	    RETURN_FP;
   1147  1.1  christos 	}
   1148  1.1  christos 
   1149  1.1  christos       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
   1150  1.1  christos       ifmt = xtensa_format_decode (isa, ins);
   1151  1.1  christos       if (ifmt == XTENSA_UNDEFINED)
   1152  1.1  christos 	RETURN_FP;
   1153  1.1  christos       ilen = xtensa_format_length (isa, ifmt);
   1154  1.1  christos       if (ilen == XTENSA_UNDEFINED)
   1155  1.1  christos 	RETURN_FP;
   1156  1.1  christos       islots = xtensa_format_num_slots (isa, ifmt);
   1157  1.1  christos       if (islots == XTENSA_UNDEFINED)
   1158  1.1  christos 	RETURN_FP;
   1159  1.1  christos 
   1160  1.1  christos       for (is = 0; is < islots; ++is)
   1161  1.1  christos 	{
   1162  1.1  christos 	  if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
   1163  1.1  christos 	    RETURN_FP;
   1164  1.1  christos 
   1165  1.1  christos 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
   1166  1.1  christos 	  if (opc == XTENSA_UNDEFINED)
   1167  1.1  christos 	    RETURN_FP;
   1168  1.1  christos 
   1169  1.1  christos 	  opcname = xtensa_opcode_name (isa, opc);
   1170  1.1  christos 
   1171  1.1  christos 	  if (strcasecmp (opcname, "mov.n") == 0
   1172  1.1  christos 	      || strcasecmp (opcname, "or") == 0)
   1173  1.1  christos 	    {
   1174  1.1  christos 	      unsigned int register_operand;
   1175  1.1  christos 
   1176  1.1  christos 	      /* Possible candidate for setting frame pointer
   1177  1.1  christos 		 from A1.  This is what we are looking for.  */
   1178  1.1  christos 
   1179  1.1  christos 	      if (xtensa_operand_get_field (isa, opc, 1, ifmt,
   1180  1.1  christos 					    is, slot, &register_operand) != 0)
   1181  1.1  christos 		RETURN_FP;
   1182  1.1  christos 	      if (xtensa_operand_decode (isa, opc, 1, &register_operand) != 0)
   1183  1.1  christos 		RETURN_FP;
   1184  1.1  christos 	      if (register_operand == 1)  /* Mov{.n} FP A1.  */
   1185  1.1  christos 		{
   1186  1.1  christos 		  if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
   1187  1.1  christos 						&register_operand) != 0)
   1188  1.1  christos 		    RETURN_FP;
   1189  1.1  christos 		  if (xtensa_operand_decode (isa, opc, 0,
   1190  1.1  christos 					     &register_operand) != 0)
   1191  1.1  christos 		    RETURN_FP;
   1192  1.1  christos 
   1193  1.1  christos 		  fp_regnum
   1194  1.1  christos 		    = gdbarch_tdep (gdbarch)->a0_base + register_operand;
   1195  1.1  christos 		  RETURN_FP;
   1196  1.1  christos 		}
   1197  1.1  christos 	    }
   1198  1.1  christos 
   1199  1.1  christos 	  if (
   1200  1.1  christos 	      /* We have problems decoding the memory.  */
   1201  1.1  christos 	      opcname == NULL
   1202  1.1  christos 	      || strcasecmp (opcname, "ill") == 0
   1203  1.1  christos 	      || strcasecmp (opcname, "ill.n") == 0
   1204  1.1  christos 	      /* Hit planted breakpoint.  */
   1205  1.1  christos 	      || strcasecmp (opcname, "break") == 0
   1206  1.1  christos 	      || strcasecmp (opcname, "break.n") == 0
   1207  1.1  christos 	      /* Flow control instructions finish prologue.  */
   1208  1.1  christos 	      || xtensa_opcode_is_branch (isa, opc) > 0
   1209  1.1  christos 	      || xtensa_opcode_is_jump   (isa, opc) > 0
   1210  1.1  christos 	      || xtensa_opcode_is_loop   (isa, opc) > 0
   1211  1.1  christos 	      || xtensa_opcode_is_call   (isa, opc) > 0
   1212  1.1  christos 	      || strcasecmp (opcname, "simcall") == 0
   1213  1.1  christos 	      || strcasecmp (opcname, "syscall") == 0)
   1214  1.1  christos 	    /* Can not continue analysis.  */
   1215  1.1  christos 	    RETURN_FP;
   1216  1.1  christos 	}
   1217  1.1  christos     }
   1218  1.1  christos done:
   1219  1.1  christos   xtensa_insnbuf_free(isa, slot);
   1220  1.1  christos   xtensa_insnbuf_free(isa, ins);
   1221  1.1  christos   return fp_regnum;
   1222  1.1  christos }
   1223  1.1  christos 
   1224  1.1  christos /* The key values to identify the frame using "cache" are
   1225  1.1  christos 
   1226  1.1  christos 	cache->base    = SP (or best guess about FP) of this frame;
   1227  1.1  christos 	cache->pc      = entry-PC (entry point of the frame function);
   1228  1.1  christos 	cache->prev_sp = SP of the previous frame.  */
   1229  1.1  christos 
   1230  1.1  christos static void
   1231  1.1  christos call0_frame_cache (struct frame_info *this_frame,
   1232  1.1  christos 		   xtensa_frame_cache_t *cache, CORE_ADDR pc);
   1233  1.1  christos 
   1234  1.1  christos static void
   1235  1.1  christos xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
   1236  1.1  christos 				     xtensa_frame_cache_t *cache,
   1237  1.1  christos 				     CORE_ADDR pc);
   1238  1.1  christos 
   1239  1.1  christos static struct xtensa_frame_cache *
   1240  1.1  christos xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
   1241  1.1  christos {
   1242  1.1  christos   xtensa_frame_cache_t *cache;
   1243  1.1  christos   CORE_ADDR ra, wb, ws, pc, sp, ps;
   1244  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   1245  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1246  1.1  christos   unsigned int fp_regnum;
   1247  1.1  christos   int  windowed, ps_regnum;
   1248  1.1  christos 
   1249  1.1  christos   if (*this_cache)
   1250  1.6  christos     return (struct xtensa_frame_cache *) *this_cache;
   1251  1.1  christos 
   1252  1.1  christos   pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
   1253  1.1  christos   ps_regnum = gdbarch_ps_regnum (gdbarch);
   1254  1.1  christos   ps = (ps_regnum >= 0
   1255  1.1  christos 	? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
   1256  1.1  christos 
   1257  1.1  christos   windowed = windowing_enabled (gdbarch, ps);
   1258  1.1  christos 
   1259  1.1  christos   /* Get pristine xtensa-frame.  */
   1260  1.1  christos   cache = xtensa_alloc_frame_cache (windowed);
   1261  1.1  christos   *this_cache = cache;
   1262  1.1  christos 
   1263  1.1  christos   if (windowed)
   1264  1.1  christos     {
   1265  1.6  christos       LONGEST op1;
   1266  1.1  christos 
   1267  1.1  christos       /* Get WINDOWBASE, WINDOWSTART, and PS registers.  */
   1268  1.1  christos       wb = get_frame_register_unsigned (this_frame,
   1269  1.1  christos 					gdbarch_tdep (gdbarch)->wb_regnum);
   1270  1.1  christos       ws = get_frame_register_unsigned (this_frame,
   1271  1.1  christos 					gdbarch_tdep (gdbarch)->ws_regnum);
   1272  1.1  christos 
   1273  1.6  christos       if (safe_read_memory_integer (pc, 1, byte_order, &op1)
   1274  1.6  christos 	  && XTENSA_IS_ENTRY (gdbarch, op1))
   1275  1.1  christos 	{
   1276  1.1  christos 	  int callinc = CALLINC (ps);
   1277  1.1  christos 	  ra = get_frame_register_unsigned
   1278  1.1  christos 	    (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
   1279  1.1  christos 
   1280  1.1  christos 	  /* ENTRY hasn't been executed yet, therefore callsize is still 0.  */
   1281  1.1  christos 	  cache->wd.callsize = 0;
   1282  1.1  christos 	  cache->wd.wb = wb;
   1283  1.1  christos 	  cache->wd.ws = ws;
   1284  1.1  christos 	  cache->prev_sp = get_frame_register_unsigned
   1285  1.1  christos 			     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
   1286  1.1  christos 
   1287  1.1  christos 	  /* This only can be the outermost frame since we are
   1288  1.1  christos 	     just about to execute ENTRY.  SP hasn't been set yet.
   1289  1.1  christos 	     We can assume any frame size, because it does not
   1290  1.1  christos 	     matter, and, let's fake frame base in cache.  */
   1291  1.1  christos 	  cache->base = cache->prev_sp - 16;
   1292  1.1  christos 
   1293  1.1  christos 	  cache->pc = pc;
   1294  1.1  christos 	  cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
   1295  1.1  christos 	  cache->ps = (ps & ~PS_CALLINC_MASK)
   1296  1.1  christos 	    | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
   1297  1.1  christos 
   1298  1.1  christos 	  return cache;
   1299  1.1  christos 	}
   1300  1.1  christos       else
   1301  1.1  christos 	{
   1302  1.1  christos 	  fp_regnum = xtensa_scan_prologue (gdbarch, pc);
   1303  1.1  christos 	  ra = get_frame_register_unsigned (this_frame,
   1304  1.1  christos 					    gdbarch_tdep (gdbarch)->a0_base);
   1305  1.1  christos 	  cache->wd.callsize = WINSIZE (ra);
   1306  1.1  christos 	  cache->wd.wb = (wb - cache->wd.callsize / 4)
   1307  1.1  christos 			  & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
   1308  1.1  christos 	  cache->wd.ws = ws & ~(1 << wb);
   1309  1.1  christos 
   1310  1.1  christos 	  cache->pc = get_frame_func (this_frame);
   1311  1.1  christos 	  cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
   1312  1.1  christos 	  cache->ps = (ps & ~PS_CALLINC_MASK)
   1313  1.1  christos 	    | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
   1314  1.1  christos 	}
   1315  1.1  christos 
   1316  1.1  christos       if (cache->wd.ws == 0)
   1317  1.1  christos 	{
   1318  1.1  christos 	  int i;
   1319  1.1  christos 
   1320  1.1  christos 	  /* Set A0...A3.  */
   1321  1.1  christos 	  sp = get_frame_register_unsigned
   1322  1.1  christos 	    (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
   1323  1.1  christos 
   1324  1.1  christos 	  for (i = 0; i < 4; i++, sp += 4)
   1325  1.1  christos 	    {
   1326  1.1  christos 	      cache->wd.aregs[i] = sp;
   1327  1.1  christos 	    }
   1328  1.1  christos 
   1329  1.1  christos 	  if (cache->wd.callsize > 4)
   1330  1.1  christos 	    {
   1331  1.1  christos 	      /* Set A4...A7/A11.  */
   1332  1.1  christos 	      /* Get the SP of the frame previous to the previous one.
   1333  1.1  christos 	         To achieve this, we have to dereference SP twice.  */
   1334  1.1  christos 	      sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
   1335  1.1  christos 	      sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
   1336  1.1  christos 	      sp -= cache->wd.callsize * 4;
   1337  1.1  christos 
   1338  1.1  christos 	      for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
   1339  1.1  christos 		{
   1340  1.1  christos 		  cache->wd.aregs[i] = sp;
   1341  1.1  christos 		}
   1342  1.1  christos 	    }
   1343  1.1  christos 	}
   1344  1.1  christos 
   1345  1.1  christos       if ((cache->prev_sp == 0) && ( ra != 0 ))
   1346  1.1  christos 	/* If RA is equal to 0 this frame is an outermost frame.  Leave
   1347  1.1  christos 	   cache->prev_sp unchanged marking the boundary of the frame stack.  */
   1348  1.1  christos 	{
   1349  1.1  christos 	  if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
   1350  1.1  christos 	    {
   1351  1.1  christos 	      /* Register window overflow already happened.
   1352  1.1  christos 		 We can read caller's SP from the proper spill loction.  */
   1353  1.1  christos 	      sp = get_frame_register_unsigned
   1354  1.1  christos 		(this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
   1355  1.1  christos 	      cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
   1356  1.1  christos 	    }
   1357  1.1  christos 	  else
   1358  1.1  christos 	    {
   1359  1.1  christos 	      /* Read caller's frame SP directly from the previous window.  */
   1360  1.1  christos 	      int regnum = arreg_number
   1361  1.1  christos 			     (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
   1362  1.1  christos 			      cache->wd.wb);
   1363  1.1  christos 
   1364  1.1  christos 	      cache->prev_sp = xtensa_read_register (regnum);
   1365  1.1  christos 	    }
   1366  1.1  christos 	}
   1367  1.1  christos     }
   1368  1.1  christos   else if (xtensa_window_interrupt_insn (gdbarch, pc))
   1369  1.1  christos     {
   1370  1.1  christos       /* Execution stopped inside Xtensa Window Interrupt Handler.  */
   1371  1.1  christos 
   1372  1.1  christos       xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
   1373  1.1  christos       /* Everything was set already,  including cache->base.  */
   1374  1.1  christos       return cache;
   1375  1.1  christos     }
   1376  1.1  christos   else	/* Call0 framework.  */
   1377  1.1  christos     {
   1378  1.1  christos       call0_frame_cache (this_frame, cache, pc);
   1379  1.1  christos       fp_regnum = cache->c0.fp_regnum;
   1380  1.1  christos     }
   1381  1.1  christos 
   1382  1.1  christos   cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
   1383  1.1  christos 
   1384  1.1  christos   return cache;
   1385  1.1  christos }
   1386  1.1  christos 
   1387  1.1  christos static int xtensa_session_once_reported = 1;
   1388  1.1  christos 
   1389  1.1  christos /* Report a problem with prologue analysis while doing backtracing.
   1390  1.1  christos    But, do it only once to avoid annoyng repeated messages.  */
   1391  1.1  christos 
   1392  1.1  christos static void
   1393  1.1  christos warning_once (void)
   1394  1.1  christos {
   1395  1.1  christos   if (xtensa_session_once_reported == 0)
   1396  1.1  christos     warning (_("\
   1397  1.1  christos \nUnrecognised function prologue. Stack trace cannot be resolved. \
   1398  1.1  christos This message will not be repeated in this session.\n"));
   1399  1.1  christos 
   1400  1.1  christos   xtensa_session_once_reported = 1;
   1401  1.1  christos }
   1402  1.1  christos 
   1403  1.1  christos 
   1404  1.1  christos static void
   1405  1.1  christos xtensa_frame_this_id (struct frame_info *this_frame,
   1406  1.1  christos 		      void **this_cache,
   1407  1.1  christos 		      struct frame_id *this_id)
   1408  1.1  christos {
   1409  1.1  christos   struct xtensa_frame_cache *cache =
   1410  1.1  christos     xtensa_frame_cache (this_frame, this_cache);
   1411  1.1  christos 
   1412  1.1  christos   if (cache->prev_sp == 0)
   1413  1.1  christos     return;
   1414  1.1  christos 
   1415  1.1  christos   (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
   1416  1.1  christos }
   1417  1.1  christos 
   1418  1.1  christos static struct value *
   1419  1.1  christos xtensa_frame_prev_register (struct frame_info *this_frame,
   1420  1.1  christos 			    void **this_cache,
   1421  1.1  christos 			    int regnum)
   1422  1.1  christos {
   1423  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   1424  1.1  christos   struct xtensa_frame_cache *cache;
   1425  1.1  christos   ULONGEST saved_reg = 0;
   1426  1.1  christos   int done = 1;
   1427  1.1  christos 
   1428  1.1  christos   if (*this_cache == NULL)
   1429  1.1  christos     *this_cache = xtensa_frame_cache (this_frame, this_cache);
   1430  1.6  christos   cache = (struct xtensa_frame_cache *) *this_cache;
   1431  1.1  christos 
   1432  1.1  christos   if (regnum ==gdbarch_pc_regnum (gdbarch))
   1433  1.1  christos     saved_reg = cache->ra;
   1434  1.1  christos   else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
   1435  1.1  christos     saved_reg = cache->prev_sp;
   1436  1.1  christos   else if (!cache->call0)
   1437  1.1  christos     {
   1438  1.1  christos       if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
   1439  1.1  christos 	saved_reg = cache->wd.ws;
   1440  1.1  christos       else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
   1441  1.1  christos 	saved_reg = cache->wd.wb;
   1442  1.1  christos       else if (regnum == gdbarch_ps_regnum (gdbarch))
   1443  1.1  christos 	saved_reg = cache->ps;
   1444  1.1  christos       else
   1445  1.1  christos 	done = 0;
   1446  1.1  christos     }
   1447  1.1  christos   else
   1448  1.1  christos     done = 0;
   1449  1.1  christos 
   1450  1.1  christos   if (done)
   1451  1.1  christos     return frame_unwind_got_constant (this_frame, regnum, saved_reg);
   1452  1.1  christos 
   1453  1.1  christos   if (!cache->call0) /* Windowed ABI.  */
   1454  1.1  christos     {
   1455  1.1  christos       /* Convert A-register numbers to AR-register numbers,
   1456  1.1  christos 	 if we deal with A-register.  */
   1457  1.1  christos       if (regnum >= gdbarch_tdep (gdbarch)->a0_base
   1458  1.1  christos           && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
   1459  1.1  christos 	regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
   1460  1.1  christos 
   1461  1.1  christos       /* Check, if we deal with AR-register saved on stack.  */
   1462  1.1  christos       if (regnum >= gdbarch_tdep (gdbarch)->ar_base
   1463  1.1  christos 	  && regnum <= (gdbarch_tdep (gdbarch)->ar_base
   1464  1.1  christos 			 + gdbarch_tdep (gdbarch)->num_aregs))
   1465  1.1  christos 	{
   1466  1.1  christos 	  int areg = areg_number (gdbarch, regnum, cache->wd.wb);
   1467  1.1  christos 
   1468  1.1  christos 	  if (areg >= 0
   1469  1.1  christos 	      && areg < XTENSA_NUM_SAVED_AREGS
   1470  1.1  christos 	      && cache->wd.aregs[areg] != -1)
   1471  1.1  christos 	    return frame_unwind_got_memory (this_frame, regnum,
   1472  1.1  christos 					    cache->wd.aregs[areg]);
   1473  1.1  christos 	}
   1474  1.1  christos     }
   1475  1.1  christos   else /* Call0 ABI.  */
   1476  1.1  christos     {
   1477  1.1  christos       int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
   1478  1.1  christos 		&& regnum <= (gdbarch_tdep (gdbarch)->ar_base
   1479  1.1  christos 			       + C0_NREGS))
   1480  1.1  christos 		  ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
   1481  1.1  christos 
   1482  1.1  christos       if (reg < C0_NREGS)
   1483  1.1  christos 	{
   1484  1.1  christos 	  CORE_ADDR spe;
   1485  1.1  christos 	  int stkofs;
   1486  1.1  christos 
   1487  1.1  christos 	  /* If register was saved in the prologue, retrieve it.  */
   1488  1.1  christos 	  stkofs = cache->c0.c0_rt[reg].to_stk;
   1489  1.1  christos 	  if (stkofs != C0_NOSTK)
   1490  1.1  christos 	    {
   1491  1.1  christos 	      /* Determine SP on entry based on FP.  */
   1492  1.1  christos 	      spe = cache->c0.c0_fp
   1493  1.1  christos 		- cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
   1494  1.1  christos 
   1495  1.1  christos 	      return frame_unwind_got_memory (this_frame, regnum,
   1496  1.1  christos 					      spe + stkofs);
   1497  1.1  christos 	    }
   1498  1.1  christos 	}
   1499  1.1  christos     }
   1500  1.1  christos 
   1501  1.1  christos   /* All other registers have been either saved to
   1502  1.1  christos      the stack or are still alive in the processor.  */
   1503  1.1  christos 
   1504  1.1  christos   return frame_unwind_got_register (this_frame, regnum, regnum);
   1505  1.1  christos }
   1506  1.1  christos 
   1507  1.1  christos 
   1508  1.1  christos static const struct frame_unwind
   1509  1.1  christos xtensa_unwind =
   1510  1.1  christos {
   1511  1.1  christos   NORMAL_FRAME,
   1512  1.1  christos   default_frame_unwind_stop_reason,
   1513  1.1  christos   xtensa_frame_this_id,
   1514  1.1  christos   xtensa_frame_prev_register,
   1515  1.1  christos   NULL,
   1516  1.1  christos   default_frame_sniffer
   1517  1.1  christos };
   1518  1.1  christos 
   1519  1.1  christos static CORE_ADDR
   1520  1.1  christos xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
   1521  1.1  christos {
   1522  1.1  christos   struct xtensa_frame_cache *cache =
   1523  1.1  christos     xtensa_frame_cache (this_frame, this_cache);
   1524  1.1  christos 
   1525  1.1  christos   return cache->base;
   1526  1.1  christos }
   1527  1.1  christos 
   1528  1.1  christos static const struct frame_base
   1529  1.1  christos xtensa_frame_base =
   1530  1.1  christos {
   1531  1.1  christos   &xtensa_unwind,
   1532  1.1  christos   xtensa_frame_base_address,
   1533  1.1  christos   xtensa_frame_base_address,
   1534  1.1  christos   xtensa_frame_base_address
   1535  1.1  christos };
   1536  1.1  christos 
   1537  1.1  christos 
   1538  1.1  christos static void
   1539  1.1  christos xtensa_extract_return_value (struct type *type,
   1540  1.1  christos 			     struct regcache *regcache,
   1541  1.1  christos 			     void *dst)
   1542  1.1  christos {
   1543  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
   1544  1.6  christos   bfd_byte *valbuf = (bfd_byte *) dst;
   1545  1.1  christos   int len = TYPE_LENGTH (type);
   1546  1.1  christos   ULONGEST pc, wb;
   1547  1.1  christos   int callsize, areg;
   1548  1.1  christos   int offset = 0;
   1549  1.1  christos 
   1550  1.1  christos   DEBUGTRACE ("xtensa_extract_return_value (...)\n");
   1551  1.1  christos 
   1552  1.1  christos   gdb_assert(len > 0);
   1553  1.1  christos 
   1554  1.1  christos   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
   1555  1.1  christos     {
   1556  1.1  christos       /* First, we have to find the caller window in the register file.  */
   1557  1.1  christos       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
   1558  1.1  christos       callsize = extract_call_winsize (gdbarch, pc);
   1559  1.1  christos 
   1560  1.1  christos       /* On Xtensa, we can return up to 4 words (or 2 for call12).  */
   1561  1.1  christos       if (len > (callsize > 8 ? 8 : 16))
   1562  1.1  christos 	internal_error (__FILE__, __LINE__,
   1563  1.1  christos 			_("cannot extract return value of %d bytes long"),
   1564  1.1  christos 			len);
   1565  1.1  christos 
   1566  1.1  christos       /* Get the register offset of the return
   1567  1.1  christos 	 register (A2) in the caller window.  */
   1568  1.1  christos       regcache_raw_read_unsigned
   1569  1.1  christos 	(regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
   1570  1.1  christos       areg = arreg_number (gdbarch,
   1571  1.1  christos 			  gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
   1572  1.1  christos     }
   1573  1.1  christos   else
   1574  1.1  christos     {
   1575  1.1  christos       /* No windowing hardware - Call0 ABI.  */
   1576  1.1  christos       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
   1577  1.1  christos     }
   1578  1.1  christos 
   1579  1.1  christos   DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
   1580  1.1  christos 
   1581  1.1  christos   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1582  1.1  christos     offset = 4 - len;
   1583  1.1  christos 
   1584  1.1  christos   for (; len > 0; len -= 4, areg++, valbuf += 4)
   1585  1.1  christos     {
   1586  1.1  christos       if (len < 4)
   1587  1.8  christos 	regcache->raw_read_part (areg, offset, len, valbuf);
   1588  1.1  christos       else
   1589  1.8  christos 	regcache->raw_read (areg, valbuf);
   1590  1.1  christos     }
   1591  1.1  christos }
   1592  1.1  christos 
   1593  1.1  christos 
   1594  1.1  christos static void
   1595  1.1  christos xtensa_store_return_value (struct type *type,
   1596  1.1  christos 			   struct regcache *regcache,
   1597  1.1  christos 			   const void *dst)
   1598  1.1  christos {
   1599  1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
   1600  1.6  christos   const bfd_byte *valbuf = (const bfd_byte *) dst;
   1601  1.1  christos   unsigned int areg;
   1602  1.1  christos   ULONGEST pc, wb;
   1603  1.1  christos   int callsize;
   1604  1.1  christos   int len = TYPE_LENGTH (type);
   1605  1.1  christos   int offset = 0;
   1606  1.1  christos 
   1607  1.1  christos   DEBUGTRACE ("xtensa_store_return_value (...)\n");
   1608  1.1  christos 
   1609  1.1  christos   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
   1610  1.1  christos     {
   1611  1.1  christos       regcache_raw_read_unsigned
   1612  1.1  christos 	(regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
   1613  1.1  christos       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
   1614  1.1  christos       callsize = extract_call_winsize (gdbarch, pc);
   1615  1.1  christos 
   1616  1.1  christos       if (len > (callsize > 8 ? 8 : 16))
   1617  1.1  christos 	internal_error (__FILE__, __LINE__,
   1618  1.1  christos 			_("unimplemented for this length: %d"),
   1619  1.1  christos 			TYPE_LENGTH (type));
   1620  1.1  christos       areg = arreg_number (gdbarch,
   1621  1.1  christos 			   gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
   1622  1.1  christos 
   1623  1.1  christos       DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
   1624  1.1  christos               callsize, (int) wb);
   1625  1.1  christos     }
   1626  1.1  christos   else
   1627  1.1  christos     {
   1628  1.1  christos       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
   1629  1.1  christos     }
   1630  1.1  christos 
   1631  1.1  christos   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1632  1.1  christos     offset = 4 - len;
   1633  1.1  christos 
   1634  1.1  christos   for (; len > 0; len -= 4, areg++, valbuf += 4)
   1635  1.1  christos     {
   1636  1.1  christos       if (len < 4)
   1637  1.8  christos 	regcache->raw_write_part (areg, offset, len, valbuf);
   1638  1.1  christos       else
   1639  1.8  christos 	regcache->raw_write (areg, valbuf);
   1640  1.1  christos     }
   1641  1.1  christos }
   1642  1.1  christos 
   1643  1.1  christos 
   1644  1.1  christos static enum return_value_convention
   1645  1.1  christos xtensa_return_value (struct gdbarch *gdbarch,
   1646  1.1  christos 		     struct value *function,
   1647  1.1  christos 		     struct type *valtype,
   1648  1.1  christos 		     struct regcache *regcache,
   1649  1.1  christos 		     gdb_byte *readbuf,
   1650  1.1  christos 		     const gdb_byte *writebuf)
   1651  1.1  christos {
   1652  1.1  christos   /* Structures up to 16 bytes are returned in registers.  */
   1653  1.1  christos 
   1654  1.1  christos   int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
   1655  1.1  christos 			|| TYPE_CODE (valtype) == TYPE_CODE_UNION
   1656  1.1  christos 			|| TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
   1657  1.1  christos 		       && TYPE_LENGTH (valtype) > 16);
   1658  1.1  christos 
   1659  1.1  christos   if (struct_return)
   1660  1.1  christos     return RETURN_VALUE_STRUCT_CONVENTION;
   1661  1.1  christos 
   1662  1.1  christos   DEBUGTRACE ("xtensa_return_value(...)\n");
   1663  1.1  christos 
   1664  1.1  christos   if (writebuf != NULL)
   1665  1.1  christos     {
   1666  1.1  christos       xtensa_store_return_value (valtype, regcache, writebuf);
   1667  1.1  christos     }
   1668  1.1  christos 
   1669  1.1  christos   if (readbuf != NULL)
   1670  1.1  christos     {
   1671  1.1  christos       gdb_assert (!struct_return);
   1672  1.1  christos       xtensa_extract_return_value (valtype, regcache, readbuf);
   1673  1.1  christos     }
   1674  1.1  christos   return RETURN_VALUE_REGISTER_CONVENTION;
   1675  1.1  christos }
   1676  1.1  christos 
   1677  1.1  christos 
   1678  1.1  christos /* DUMMY FRAME */
   1679  1.1  christos 
   1680  1.1  christos static CORE_ADDR
   1681  1.1  christos xtensa_push_dummy_call (struct gdbarch *gdbarch,
   1682  1.1  christos 			struct value *function,
   1683  1.1  christos 			struct regcache *regcache,
   1684  1.1  christos 			CORE_ADDR bp_addr,
   1685  1.1  christos 			int nargs,
   1686  1.1  christos 			struct value **args,
   1687  1.1  christos 			CORE_ADDR sp,
   1688  1.8  christos 			function_call_return_method return_method,
   1689  1.1  christos 			CORE_ADDR struct_addr)
   1690  1.1  christos {
   1691  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1692  1.1  christos   int size, onstack_size;
   1693  1.1  christos   gdb_byte *buf = (gdb_byte *) alloca (16);
   1694  1.1  christos   CORE_ADDR ra, ps;
   1695  1.1  christos   struct argument_info
   1696  1.1  christos   {
   1697  1.1  christos     const bfd_byte *contents;
   1698  1.1  christos     int length;
   1699  1.1  christos     int onstack;		/* onstack == 0 => in reg */
   1700  1.1  christos     int align;			/* alignment */
   1701  1.1  christos     union
   1702  1.1  christos     {
   1703  1.1  christos       int offset;		/* stack offset if on stack.  */
   1704  1.1  christos       int regno;		/* regno if in register.  */
   1705  1.1  christos     } u;
   1706  1.1  christos   };
   1707  1.1  christos 
   1708  1.1  christos   struct argument_info *arg_info =
   1709  1.1  christos     (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
   1710  1.1  christos 
   1711  1.1  christos   CORE_ADDR osp = sp;
   1712  1.1  christos 
   1713  1.1  christos   DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
   1714  1.1  christos 
   1715  1.1  christos   if (xtensa_debug_level > 3)
   1716  1.1  christos     {
   1717  1.1  christos       DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
   1718  1.8  christos       DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, return_method=%d, "
   1719  1.1  christos 		 "struct_addr=0x%x\n",
   1720  1.8  christos 		 (int) sp, (int) return_method, (int) struct_addr);
   1721  1.1  christos 
   1722  1.8  christos       for (int i = 0; i < nargs; i++)
   1723  1.1  christos         {
   1724  1.1  christos 	  struct value *arg = args[i];
   1725  1.1  christos 	  struct type *arg_type = check_typedef (value_type (arg));
   1726  1.1  christos 	  fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
   1727  1.1  christos 			      host_address_to_string (arg),
   1728  1.1  christos 			      TYPE_LENGTH (arg_type));
   1729  1.1  christos 	  switch (TYPE_CODE (arg_type))
   1730  1.1  christos 	    {
   1731  1.1  christos 	    case TYPE_CODE_INT:
   1732  1.1  christos 	      fprintf_unfiltered (gdb_stdlog, "int");
   1733  1.1  christos 	      break;
   1734  1.1  christos 	    case TYPE_CODE_STRUCT:
   1735  1.1  christos 	      fprintf_unfiltered (gdb_stdlog, "struct");
   1736  1.1  christos 	      break;
   1737  1.1  christos 	    default:
   1738  1.1  christos 	      fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
   1739  1.1  christos 	      break;
   1740  1.1  christos 	    }
   1741  1.1  christos 	  fprintf_unfiltered (gdb_stdlog, " %s\n",
   1742  1.1  christos 			      host_address_to_string (value_contents (arg)));
   1743  1.1  christos 	}
   1744  1.1  christos     }
   1745  1.1  christos 
   1746  1.1  christos   /* First loop: collect information.
   1747  1.1  christos      Cast into type_long.  (This shouldn't happen often for C because
   1748  1.1  christos      GDB already does this earlier.)  It's possible that GDB could
   1749  1.1  christos      do it all the time but it's harmless to leave this code here.  */
   1750  1.1  christos 
   1751  1.1  christos   size = 0;
   1752  1.1  christos   onstack_size = 0;
   1753  1.1  christos 
   1754  1.8  christos   if (return_method == return_method_struct)
   1755  1.1  christos     size = REGISTER_SIZE;
   1756  1.1  christos 
   1757  1.8  christos   for (int i = 0; i < nargs; i++)
   1758  1.1  christos     {
   1759  1.1  christos       struct argument_info *info = &arg_info[i];
   1760  1.1  christos       struct value *arg = args[i];
   1761  1.1  christos       struct type *arg_type = check_typedef (value_type (arg));
   1762  1.1  christos 
   1763  1.1  christos       switch (TYPE_CODE (arg_type))
   1764  1.1  christos 	{
   1765  1.1  christos 	case TYPE_CODE_INT:
   1766  1.1  christos 	case TYPE_CODE_BOOL:
   1767  1.1  christos 	case TYPE_CODE_CHAR:
   1768  1.1  christos 	case TYPE_CODE_RANGE:
   1769  1.1  christos 	case TYPE_CODE_ENUM:
   1770  1.1  christos 
   1771  1.1  christos 	  /* Cast argument to long if necessary as the mask does it too.  */
   1772  1.1  christos 	  if (TYPE_LENGTH (arg_type)
   1773  1.1  christos 	      < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
   1774  1.1  christos 	    {
   1775  1.1  christos 	      arg_type = builtin_type (gdbarch)->builtin_long;
   1776  1.1  christos 	      arg = value_cast (arg_type, arg);
   1777  1.1  christos 	    }
   1778  1.1  christos 	  /* Aligment is equal to the type length for the basic types.  */
   1779  1.1  christos 	  info->align = TYPE_LENGTH (arg_type);
   1780  1.1  christos 	  break;
   1781  1.1  christos 
   1782  1.1  christos 	case TYPE_CODE_FLT:
   1783  1.1  christos 
   1784  1.1  christos 	  /* Align doubles correctly.  */
   1785  1.1  christos 	  if (TYPE_LENGTH (arg_type)
   1786  1.1  christos 	      == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
   1787  1.1  christos 	    info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
   1788  1.1  christos 	  else
   1789  1.1  christos 	    info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
   1790  1.1  christos 	  break;
   1791  1.1  christos 
   1792  1.1  christos 	case TYPE_CODE_STRUCT:
   1793  1.1  christos 	default:
   1794  1.1  christos 	  info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
   1795  1.1  christos 	  break;
   1796  1.1  christos 	}
   1797  1.1  christos       info->length = TYPE_LENGTH (arg_type);
   1798  1.1  christos       info->contents = value_contents (arg);
   1799  1.1  christos 
   1800  1.1  christos       /* Align size and onstack_size.  */
   1801  1.1  christos       size = (size + info->align - 1) & ~(info->align - 1);
   1802  1.1  christos       onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
   1803  1.1  christos 
   1804  1.1  christos       if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
   1805  1.1  christos 	{
   1806  1.1  christos 	  info->onstack = 1;
   1807  1.1  christos 	  info->u.offset = onstack_size;
   1808  1.1  christos 	  onstack_size += info->length;
   1809  1.1  christos 	}
   1810  1.1  christos       else
   1811  1.1  christos 	{
   1812  1.1  christos 	  info->onstack = 0;
   1813  1.1  christos 	  info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
   1814  1.1  christos 	}
   1815  1.1  christos       size += info->length;
   1816  1.1  christos     }
   1817  1.1  christos 
   1818  1.1  christos   /* Adjust the stack pointer and align it.  */
   1819  1.1  christos   sp = align_down (sp - onstack_size, SP_ALIGNMENT);
   1820  1.1  christos 
   1821  1.1  christos   /* Simulate MOVSP, if Windowed ABI.  */
   1822  1.1  christos   if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
   1823  1.1  christos       && (sp != osp))
   1824  1.1  christos     {
   1825  1.1  christos       read_memory (osp - 16, buf, 16);
   1826  1.1  christos       write_memory (sp - 16, buf, 16);
   1827  1.1  christos     }
   1828  1.1  christos 
   1829  1.1  christos   /* Second Loop: Load arguments.  */
   1830  1.1  christos 
   1831  1.8  christos   if (return_method == return_method_struct)
   1832  1.1  christos     {
   1833  1.1  christos       store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
   1834  1.8  christos       regcache->cooked_write (ARG_1ST (gdbarch), buf);
   1835  1.1  christos     }
   1836  1.1  christos 
   1837  1.8  christos   for (int i = 0; i < nargs; i++)
   1838  1.1  christos     {
   1839  1.1  christos       struct argument_info *info = &arg_info[i];
   1840  1.1  christos 
   1841  1.1  christos       if (info->onstack)
   1842  1.1  christos 	{
   1843  1.1  christos 	  int n = info->length;
   1844  1.1  christos 	  CORE_ADDR offset = sp + info->u.offset;
   1845  1.1  christos 
   1846  1.1  christos 	  /* Odd-sized structs are aligned to the lower side of a memory
   1847  1.1  christos 	     word in big-endian mode and require a shift.  This only
   1848  1.1  christos 	     applies for structures smaller than one word.  */
   1849  1.1  christos 
   1850  1.1  christos 	  if (n < REGISTER_SIZE
   1851  1.1  christos 	      && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1852  1.1  christos 	    offset += (REGISTER_SIZE - n);
   1853  1.1  christos 
   1854  1.1  christos 	  write_memory (offset, info->contents, info->length);
   1855  1.1  christos 
   1856  1.1  christos 	}
   1857  1.1  christos       else
   1858  1.1  christos 	{
   1859  1.1  christos 	  int n = info->length;
   1860  1.1  christos 	  const bfd_byte *cp = info->contents;
   1861  1.1  christos 	  int r = info->u.regno;
   1862  1.1  christos 
   1863  1.1  christos 	  /* Odd-sized structs are aligned to the lower side of registers in
   1864  1.1  christos 	     big-endian mode and require a shift.  The odd-sized leftover will
   1865  1.1  christos 	     be at the end.  Note that this is only true for structures smaller
   1866  1.1  christos 	     than REGISTER_SIZE; for larger odd-sized structures the excess
   1867  1.1  christos 	     will be left-aligned in the register on both endiannesses.  */
   1868  1.1  christos 
   1869  1.1  christos 	  if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
   1870  1.1  christos 	    {
   1871  1.1  christos 	      ULONGEST v;
   1872  1.1  christos 	      v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
   1873  1.1  christos 	      v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
   1874  1.1  christos 
   1875  1.1  christos 	      store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
   1876  1.8  christos 	      regcache->cooked_write (r, buf);
   1877  1.1  christos 
   1878  1.1  christos 	      cp += REGISTER_SIZE;
   1879  1.1  christos 	      n -= REGISTER_SIZE;
   1880  1.1  christos 	      r++;
   1881  1.1  christos 	    }
   1882  1.1  christos 	  else
   1883  1.1  christos 	    while (n > 0)
   1884  1.1  christos 	      {
   1885  1.8  christos 		regcache->cooked_write (r, cp);
   1886  1.1  christos 
   1887  1.1  christos 		cp += REGISTER_SIZE;
   1888  1.1  christos 		n -= REGISTER_SIZE;
   1889  1.1  christos 		r++;
   1890  1.1  christos 	      }
   1891  1.1  christos 	}
   1892  1.1  christos     }
   1893  1.1  christos 
   1894  1.1  christos   /* Set the return address of dummy frame to the dummy address.
   1895  1.1  christos      The return address for the current function (in A0) is
   1896  1.1  christos      saved in the dummy frame, so we can savely overwrite A0 here.  */
   1897  1.1  christos 
   1898  1.1  christos   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
   1899  1.1  christos     {
   1900  1.1  christos       ULONGEST val;
   1901  1.1  christos 
   1902  1.1  christos       ra = (bp_addr & 0x3fffffff) | 0x40000000;
   1903  1.1  christos       regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
   1904  1.1  christos       ps = (unsigned long) val & ~0x00030000;
   1905  1.1  christos       regcache_cooked_write_unsigned
   1906  1.1  christos 	(regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
   1907  1.1  christos       regcache_cooked_write_unsigned (regcache,
   1908  1.1  christos 				      gdbarch_ps_regnum (gdbarch),
   1909  1.1  christos 				      ps | 0x00010000);
   1910  1.1  christos 
   1911  1.1  christos       /* All the registers have been saved.  After executing
   1912  1.1  christos 	 dummy call, they all will be restored.  So it's safe
   1913  1.1  christos 	 to modify WINDOWSTART register to make it look like there
   1914  1.1  christos 	 is only one register window corresponding to WINDOWEBASE.  */
   1915  1.1  christos 
   1916  1.8  christos       regcache->raw_read (gdbarch_tdep (gdbarch)->wb_regnum, buf);
   1917  1.1  christos       regcache_cooked_write_unsigned
   1918  1.1  christos 	(regcache, gdbarch_tdep (gdbarch)->ws_regnum,
   1919  1.1  christos 	 1 << extract_unsigned_integer (buf, 4, byte_order));
   1920  1.1  christos     }
   1921  1.1  christos   else
   1922  1.1  christos     {
   1923  1.1  christos       /* Simulate CALL0: write RA into A0 register.  */
   1924  1.1  christos       regcache_cooked_write_unsigned
   1925  1.1  christos 	(regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
   1926  1.1  christos     }
   1927  1.1  christos 
   1928  1.1  christos   /* Set new stack pointer and return it.  */
   1929  1.1  christos   regcache_cooked_write_unsigned (regcache,
   1930  1.1  christos 				  gdbarch_tdep (gdbarch)->a0_base + 1, sp);
   1931  1.1  christos   /* Make dummy frame ID unique by adding a constant.  */
   1932  1.1  christos   return sp + SP_ALIGNMENT;
   1933  1.1  christos }
   1934  1.1  christos 
   1935  1.7  christos /* Implement the breakpoint_kind_from_pc gdbarch method.  */
   1936  1.7  christos 
   1937  1.7  christos static int
   1938  1.7  christos xtensa_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
   1939  1.7  christos {
   1940  1.7  christos   if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
   1941  1.7  christos     return 2;
   1942  1.7  christos   else
   1943  1.7  christos     return 4;
   1944  1.7  christos }
   1945  1.1  christos 
   1946  1.1  christos /* Return a breakpoint for the current location of PC.  We always use
   1947  1.1  christos    the density version if we have density instructions (regardless of the
   1948  1.1  christos    current instruction at PC), and use regular instructions otherwise.  */
   1949  1.1  christos 
   1950  1.1  christos #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
   1951  1.1  christos #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
   1952  1.1  christos #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
   1953  1.1  christos #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
   1954  1.1  christos 
   1955  1.7  christos /* Implement the sw_breakpoint_from_kind gdbarch method.  */
   1956  1.1  christos 
   1957  1.7  christos static const gdb_byte *
   1958  1.7  christos xtensa_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
   1959  1.7  christos {
   1960  1.7  christos   *size = kind;
   1961  1.1  christos 
   1962  1.7  christos   if (kind == 4)
   1963  1.1  christos     {
   1964  1.7  christos       static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
   1965  1.7  christos       static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
   1966  1.7  christos 
   1967  1.1  christos       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1968  1.7  christos 	return big_breakpoint;
   1969  1.1  christos       else
   1970  1.7  christos 	return little_breakpoint;
   1971  1.1  christos     }
   1972  1.1  christos   else
   1973  1.1  christos     {
   1974  1.7  christos       static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
   1975  1.7  christos       static unsigned char density_little_breakpoint[]
   1976  1.7  christos 	= DENSITY_LITTLE_BREAKPOINT;
   1977  1.7  christos 
   1978  1.1  christos       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
   1979  1.7  christos 	return density_big_breakpoint;
   1980  1.1  christos       else
   1981  1.7  christos 	return density_little_breakpoint;
   1982  1.1  christos     }
   1983  1.1  christos }
   1984  1.1  christos 
   1985  1.1  christos /* Call0 ABI support routines.  */
   1986  1.1  christos 
   1987  1.1  christos /* Return true, if PC points to "ret" or "ret.n".  */
   1988  1.1  christos 
   1989  1.1  christos static int
   1990  1.1  christos call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
   1991  1.1  christos {
   1992  1.1  christos #define RETURN_RET goto done
   1993  1.1  christos   xtensa_isa isa;
   1994  1.1  christos   xtensa_insnbuf ins, slot;
   1995  1.1  christos   gdb_byte ibuf[XTENSA_ISA_BSZ];
   1996  1.1  christos   CORE_ADDR ia, bt, ba;
   1997  1.1  christos   xtensa_format ifmt;
   1998  1.1  christos   int ilen, islots, is;
   1999  1.1  christos   xtensa_opcode opc;
   2000  1.1  christos   const char *opcname;
   2001  1.1  christos   int found_ret = 0;
   2002  1.1  christos 
   2003  1.1  christos   isa = xtensa_default_isa;
   2004  1.1  christos   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
   2005  1.1  christos   ins = xtensa_insnbuf_alloc (isa);
   2006  1.1  christos   slot = xtensa_insnbuf_alloc (isa);
   2007  1.1  christos   ba = 0;
   2008  1.1  christos 
   2009  1.1  christos   for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
   2010  1.1  christos     {
   2011  1.1  christos       if (ia + xtensa_isa_maxlength (isa) > bt)
   2012  1.1  christos         {
   2013  1.1  christos 	  ba = ia;
   2014  1.1  christos 	  bt = (ba + XTENSA_ISA_BSZ) < finish_pc
   2015  1.1  christos 	    ? ba + XTENSA_ISA_BSZ : finish_pc;
   2016  1.1  christos 	  if (target_read_memory (ba, ibuf, bt - ba) != 0 )
   2017  1.1  christos 	    RETURN_RET;
   2018  1.1  christos 	}
   2019  1.1  christos 
   2020  1.1  christos       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
   2021  1.1  christos       ifmt = xtensa_format_decode (isa, ins);
   2022  1.1  christos       if (ifmt == XTENSA_UNDEFINED)
   2023  1.1  christos 	RETURN_RET;
   2024  1.1  christos       ilen = xtensa_format_length (isa, ifmt);
   2025  1.1  christos       if (ilen == XTENSA_UNDEFINED)
   2026  1.1  christos 	RETURN_RET;
   2027  1.1  christos       islots = xtensa_format_num_slots (isa, ifmt);
   2028  1.1  christos       if (islots == XTENSA_UNDEFINED)
   2029  1.1  christos 	RETURN_RET;
   2030  1.1  christos 
   2031  1.1  christos       for (is = 0; is < islots; ++is)
   2032  1.1  christos 	{
   2033  1.1  christos 	  if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
   2034  1.1  christos 	    RETURN_RET;
   2035  1.1  christos 
   2036  1.1  christos 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
   2037  1.1  christos 	  if (opc == XTENSA_UNDEFINED)
   2038  1.1  christos 	    RETURN_RET;
   2039  1.1  christos 
   2040  1.1  christos 	  opcname = xtensa_opcode_name (isa, opc);
   2041  1.1  christos 
   2042  1.1  christos 	  if ((strcasecmp (opcname, "ret.n") == 0)
   2043  1.1  christos 	      || (strcasecmp (opcname, "ret") == 0))
   2044  1.1  christos 	    {
   2045  1.1  christos 	      found_ret = 1;
   2046  1.1  christos 	      RETURN_RET;
   2047  1.1  christos 	    }
   2048  1.1  christos 	}
   2049  1.1  christos     }
   2050  1.1  christos  done:
   2051  1.1  christos   xtensa_insnbuf_free(isa, slot);
   2052  1.1  christos   xtensa_insnbuf_free(isa, ins);
   2053  1.1  christos   return found_ret;
   2054  1.1  christos }
   2055  1.1  christos 
   2056  1.1  christos /* Call0 opcode class.  Opcodes are preclassified according to what they
   2057  1.1  christos    mean for Call0 prologue analysis, and their number of significant operands.
   2058  1.1  christos    The purpose of this is to simplify prologue analysis by separating
   2059  1.1  christos    instruction decoding (libisa) from the semantics of prologue analysis.  */
   2060  1.1  christos 
   2061  1.1  christos typedef enum
   2062  1.1  christos {
   2063  1.1  christos   c0opc_illegal,       /* Unknown to libisa (invalid) or 'ill' opcode.  */
   2064  1.1  christos   c0opc_uninteresting, /* Not interesting for Call0 prologue analysis.  */
   2065  1.1  christos   c0opc_flow,	       /* Flow control insn.  */
   2066  1.1  christos   c0opc_entry,	       /* ENTRY indicates non-Call0 prologue.  */
   2067  1.1  christos   c0opc_break,	       /* Debugger software breakpoints.  */
   2068  1.1  christos   c0opc_add,	       /* Adding two registers.  */
   2069  1.1  christos   c0opc_addi,	       /* Adding a register and an immediate.  */
   2070  1.1  christos   c0opc_and,	       /* Bitwise "and"-ing two registers.  */
   2071  1.1  christos   c0opc_sub,	       /* Subtracting a register from a register.  */
   2072  1.1  christos   c0opc_mov,	       /* Moving a register to a register.  */
   2073  1.1  christos   c0opc_movi,	       /* Moving an immediate to a register.  */
   2074  1.1  christos   c0opc_l32r,	       /* Loading a literal.  */
   2075  1.1  christos   c0opc_s32i,	       /* Storing word at fixed offset from a base register.  */
   2076  1.1  christos   c0opc_rwxsr,	       /* RSR, WRS, or XSR instructions.  */
   2077  1.1  christos   c0opc_l32e,          /* L32E instruction.  */
   2078  1.1  christos   c0opc_s32e,          /* S32E instruction.  */
   2079  1.1  christos   c0opc_rfwo,          /* RFWO instruction.  */
   2080  1.1  christos   c0opc_rfwu,          /* RFWU instruction.  */
   2081  1.1  christos   c0opc_NrOf	       /* Number of opcode classifications.  */
   2082  1.1  christos } xtensa_insn_kind;
   2083  1.1  christos 
   2084  1.1  christos /* Return true,  if OPCNAME is RSR,  WRS,  or XSR instruction.  */
   2085  1.1  christos 
   2086  1.1  christos static int
   2087  1.1  christos rwx_special_register (const char *opcname)
   2088  1.1  christos {
   2089  1.1  christos   char ch = *opcname++;
   2090  1.1  christos 
   2091  1.1  christos   if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
   2092  1.1  christos     return 0;
   2093  1.1  christos   if (*opcname++ != 's')
   2094  1.1  christos     return 0;
   2095  1.1  christos   if (*opcname++ != 'r')
   2096  1.1  christos     return 0;
   2097  1.1  christos   if (*opcname++ != '.')
   2098  1.1  christos     return 0;
   2099  1.1  christos 
   2100  1.1  christos   return 1;
   2101  1.1  christos }
   2102  1.1  christos 
   2103  1.1  christos /* Classify an opcode based on what it means for Call0 prologue analysis.  */
   2104  1.1  christos 
   2105  1.1  christos static xtensa_insn_kind
   2106  1.1  christos call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
   2107  1.1  christos {
   2108  1.1  christos   const char *opcname;
   2109  1.1  christos   xtensa_insn_kind opclass = c0opc_uninteresting;
   2110  1.1  christos 
   2111  1.1  christos   DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
   2112  1.1  christos 
   2113  1.1  christos   /* Get opcode name and handle special classifications.  */
   2114  1.1  christos 
   2115  1.1  christos   opcname = xtensa_opcode_name (isa, opc);
   2116  1.1  christos 
   2117  1.1  christos   if (opcname == NULL
   2118  1.1  christos       || strcasecmp (opcname, "ill") == 0
   2119  1.1  christos       || strcasecmp (opcname, "ill.n") == 0)
   2120  1.1  christos     opclass = c0opc_illegal;
   2121  1.1  christos   else if (strcasecmp (opcname, "break") == 0
   2122  1.1  christos 	   || strcasecmp (opcname, "break.n") == 0)
   2123  1.1  christos      opclass = c0opc_break;
   2124  1.1  christos   else if (strcasecmp (opcname, "entry") == 0)
   2125  1.1  christos     opclass = c0opc_entry;
   2126  1.1  christos   else if (strcasecmp (opcname, "rfwo") == 0)
   2127  1.1  christos     opclass = c0opc_rfwo;
   2128  1.1  christos   else if (strcasecmp (opcname, "rfwu") == 0)
   2129  1.1  christos     opclass = c0opc_rfwu;
   2130  1.1  christos   else if (xtensa_opcode_is_branch (isa, opc) > 0
   2131  1.1  christos 	   || xtensa_opcode_is_jump   (isa, opc) > 0
   2132  1.1  christos 	   || xtensa_opcode_is_loop   (isa, opc) > 0
   2133  1.1  christos 	   || xtensa_opcode_is_call   (isa, opc) > 0
   2134  1.1  christos 	   || strcasecmp (opcname, "simcall") == 0
   2135  1.1  christos 	   || strcasecmp (opcname, "syscall") == 0)
   2136  1.1  christos     opclass = c0opc_flow;
   2137  1.1  christos 
   2138  1.1  christos   /* Also, classify specific opcodes that need to be tracked.  */
   2139  1.1  christos   else if (strcasecmp (opcname, "add") == 0
   2140  1.1  christos 	   || strcasecmp (opcname, "add.n") == 0)
   2141  1.1  christos     opclass = c0opc_add;
   2142  1.1  christos   else if (strcasecmp (opcname, "and") == 0)
   2143  1.1  christos     opclass = c0opc_and;
   2144  1.1  christos   else if (strcasecmp (opcname, "addi") == 0
   2145  1.1  christos 	   || strcasecmp (opcname, "addi.n") == 0
   2146  1.1  christos 	   || strcasecmp (opcname, "addmi") == 0)
   2147  1.1  christos     opclass = c0opc_addi;
   2148  1.1  christos   else if (strcasecmp (opcname, "sub") == 0)
   2149  1.1  christos     opclass = c0opc_sub;
   2150  1.1  christos   else if (strcasecmp (opcname, "mov.n") == 0
   2151  1.1  christos 	   || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro.  */
   2152  1.1  christos     opclass = c0opc_mov;
   2153  1.1  christos   else if (strcasecmp (opcname, "movi") == 0
   2154  1.1  christos 	   || strcasecmp (opcname, "movi.n") == 0)
   2155  1.1  christos     opclass = c0opc_movi;
   2156  1.1  christos   else if (strcasecmp (opcname, "l32r") == 0)
   2157  1.1  christos     opclass = c0opc_l32r;
   2158  1.1  christos   else if (strcasecmp (opcname, "s32i") == 0
   2159  1.1  christos 	   || strcasecmp (opcname, "s32i.n") == 0)
   2160  1.1  christos     opclass = c0opc_s32i;
   2161  1.1  christos   else if (strcasecmp (opcname, "l32e") == 0)
   2162  1.1  christos     opclass = c0opc_l32e;
   2163  1.1  christos   else if (strcasecmp (opcname, "s32e") == 0)
   2164  1.1  christos     opclass = c0opc_s32e;
   2165  1.1  christos   else if (rwx_special_register (opcname))
   2166  1.1  christos     opclass = c0opc_rwxsr;
   2167  1.1  christos 
   2168  1.1  christos   return opclass;
   2169  1.1  christos }
   2170  1.1  christos 
   2171  1.1  christos /* Tracks register movement/mutation for a given operation, which may
   2172  1.1  christos    be within a bundle.  Updates the destination register tracking info
   2173  1.1  christos    accordingly.  The pc is needed only for pc-relative load instructions
   2174  1.1  christos    (eg. l32r).  The SP register number is needed to identify stores to
   2175  1.1  christos    the stack frame.  Returns 0, if analysis was succesfull, non-zero
   2176  1.1  christos    otherwise.  */
   2177  1.1  christos 
   2178  1.1  christos static int
   2179  1.1  christos call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
   2180  1.1  christos 		xtensa_insn_kind opclass, int nods, unsigned odv[],
   2181  1.1  christos 		CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
   2182  1.1  christos {
   2183  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2184  1.1  christos   unsigned litbase, litaddr, litval;
   2185  1.1  christos 
   2186  1.1  christos   switch (opclass)
   2187  1.1  christos     {
   2188  1.1  christos     case c0opc_addi:
   2189  1.1  christos       /* 3 operands: dst, src, imm.  */
   2190  1.1  christos       gdb_assert (nods == 3);
   2191  1.1  christos       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
   2192  1.1  christos       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
   2193  1.1  christos       break;
   2194  1.1  christos     case c0opc_add:
   2195  1.1  christos       /* 3 operands: dst, src1, src2.  */
   2196  1.1  christos       gdb_assert (nods == 3);
   2197  1.1  christos       if      (src[odv[1]].fr_reg == C0_CONST)
   2198  1.1  christos         {
   2199  1.1  christos 	  dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
   2200  1.1  christos 	  dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
   2201  1.1  christos 	}
   2202  1.1  christos       else if (src[odv[2]].fr_reg == C0_CONST)
   2203  1.1  christos         {
   2204  1.1  christos 	  dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
   2205  1.1  christos 	  dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
   2206  1.1  christos 	}
   2207  1.1  christos       else dst[odv[0]].fr_reg = C0_INEXP;
   2208  1.1  christos       break;
   2209  1.1  christos     case c0opc_and:
   2210  1.1  christos       /* 3 operands:  dst, src1, src2.  */
   2211  1.1  christos       gdb_assert (nods == 3);
   2212  1.1  christos       if (cache->c0.c0_fpalign == 0)
   2213  1.1  christos 	{
   2214  1.1  christos 	  /* Handle dynamic stack alignment.  */
   2215  1.1  christos 	  if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
   2216  1.1  christos 	    {
   2217  1.1  christos 	      if (src[odv[2]].fr_reg == C0_CONST)
   2218  1.1  christos 		cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
   2219  1.1  christos 	      break;
   2220  1.1  christos 	    }
   2221  1.1  christos 	  else if ((src[odv[0]].fr_reg == spreg)
   2222  1.1  christos 		   && (src[odv[2]].fr_reg == spreg))
   2223  1.1  christos 	    {
   2224  1.1  christos 	      if (src[odv[1]].fr_reg == C0_CONST)
   2225  1.1  christos 		cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
   2226  1.1  christos 	      break;
   2227  1.1  christos 	    }
   2228  1.1  christos 	  /* else fall through.  */
   2229  1.1  christos 	}
   2230  1.1  christos       if      (src[odv[1]].fr_reg == C0_CONST)
   2231  1.1  christos         {
   2232  1.1  christos 	  dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
   2233  1.1  christos 	  dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
   2234  1.1  christos 	}
   2235  1.1  christos       else if (src[odv[2]].fr_reg == C0_CONST)
   2236  1.1  christos         {
   2237  1.1  christos 	  dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
   2238  1.1  christos 	  dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
   2239  1.1  christos 	}
   2240  1.1  christos       else dst[odv[0]].fr_reg = C0_INEXP;
   2241  1.1  christos       break;
   2242  1.1  christos     case c0opc_sub:
   2243  1.1  christos       /* 3 operands: dst, src1, src2.  */
   2244  1.1  christos       gdb_assert (nods == 3);
   2245  1.1  christos       if      (src[odv[2]].fr_reg == C0_CONST)
   2246  1.1  christos         {
   2247  1.1  christos 	  dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
   2248  1.1  christos 	  dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
   2249  1.1  christos 	}
   2250  1.1  christos       else dst[odv[0]].fr_reg = C0_INEXP;
   2251  1.1  christos       break;
   2252  1.1  christos     case c0opc_mov:
   2253  1.1  christos       /* 2 operands: dst, src [, src].  */
   2254  1.1  christos       gdb_assert (nods == 2);
   2255  1.1  christos       /* First, check if it's a special case of saving unaligned SP
   2256  1.1  christos 	 to a spare register in case of dynamic stack adjustment.
   2257  1.1  christos 	 But, only do it one time.  The second time could be initializing
   2258  1.1  christos 	 frame pointer.  We don't want to overwrite the first one.  */
   2259  1.1  christos       if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
   2260  1.1  christos 	cache->c0.c0_old_sp = odv[0];
   2261  1.1  christos 
   2262  1.1  christos       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
   2263  1.1  christos       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
   2264  1.1  christos       break;
   2265  1.1  christos     case c0opc_movi:
   2266  1.1  christos       /* 2 operands: dst, imm.  */
   2267  1.1  christos       gdb_assert (nods == 2);
   2268  1.1  christos       dst[odv[0]].fr_reg = C0_CONST;
   2269  1.1  christos       dst[odv[0]].fr_ofs = odv[1];
   2270  1.1  christos       break;
   2271  1.1  christos     case c0opc_l32r:
   2272  1.1  christos       /* 2 operands: dst, literal offset.  */
   2273  1.1  christos       gdb_assert (nods == 2);
   2274  1.1  christos       /* litbase = xtensa_get_litbase (pc);  can be also used.  */
   2275  1.1  christos       litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
   2276  1.1  christos 	? 0 : xtensa_read_register
   2277  1.1  christos 		(gdbarch_tdep (gdbarch)->litbase_regnum);
   2278  1.1  christos       litaddr = litbase & 1
   2279  1.1  christos 		  ? (litbase & ~1) + (signed)odv[1]
   2280  1.1  christos 		  : (pc + 3  + (signed)odv[1]) & ~3;
   2281  1.1  christos       litval = read_memory_integer (litaddr, 4, byte_order);
   2282  1.1  christos       dst[odv[0]].fr_reg = C0_CONST;
   2283  1.1  christos       dst[odv[0]].fr_ofs = litval;
   2284  1.1  christos       break;
   2285  1.1  christos     case c0opc_s32i:
   2286  1.1  christos       /* 3 operands: value, base, offset.  */
   2287  1.1  christos       gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
   2288  1.1  christos       /* First, check if it's a spill for saved unaligned SP,
   2289  1.1  christos 	 when dynamic stack adjustment was applied to this frame.  */
   2290  1.1  christos       if ((cache->c0.c0_fpalign != 0)		/* Dynamic stack adjustment.  */
   2291  1.1  christos 	  && (odv[1] == spreg)			/* SP usage indicates spill.  */
   2292  1.1  christos 	  && (odv[0] == cache->c0.c0_old_sp))	/* Old SP register spilled.  */
   2293  1.1  christos 	cache->c0.c0_sp_ofs = odv[2];
   2294  1.1  christos 
   2295  1.1  christos       if (src[odv[1]].fr_reg == spreg	     /* Store to stack frame.  */
   2296  1.1  christos 	  && (src[odv[1]].fr_ofs & 3) == 0   /* Alignment preserved.  */
   2297  1.1  christos 	  &&  src[odv[0]].fr_reg >= 0	     /* Value is from a register.  */
   2298  1.1  christos 	  &&  src[odv[0]].fr_ofs == 0	     /* Value hasn't been modified.  */
   2299  1.1  christos 	  &&  src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time.  */
   2300  1.1  christos         {
   2301  1.1  christos 	  /* ISA encoding guarantees alignment.  But, check it anyway.  */
   2302  1.1  christos 	  gdb_assert ((odv[2] & 3) == 0);
   2303  1.1  christos 	  dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
   2304  1.1  christos 	}
   2305  1.1  christos       break;
   2306  1.1  christos       /* If we end up inside Window Overflow / Underflow interrupt handler
   2307  1.1  christos 	 report an error because these handlers should have been handled
   2308  1.1  christos 	 already in a different way.  */
   2309  1.1  christos     case c0opc_l32e:
   2310  1.1  christos     case c0opc_s32e:
   2311  1.1  christos     case c0opc_rfwo:
   2312  1.1  christos     case c0opc_rfwu:
   2313  1.1  christos       return 1;
   2314  1.1  christos     default:
   2315  1.1  christos       return 1;
   2316  1.1  christos     }
   2317  1.1  christos   return 0;
   2318  1.1  christos }
   2319  1.1  christos 
   2320  1.1  christos /* Analyze prologue of the function at start address to determine if it uses
   2321  1.1  christos    the Call0 ABI, and if so track register moves and linear modifications
   2322  1.1  christos    in the prologue up to the PC or just beyond the prologue, whichever is
   2323  1.1  christos    first. An 'entry' instruction indicates non-Call0 ABI and the end of the
   2324  1.1  christos    prologue. The prologue may overlap non-prologue instructions but is
   2325  1.1  christos    guaranteed to end by the first flow-control instruction (jump, branch,
   2326  1.1  christos    call or return).  Since an optimized function may move information around
   2327  1.1  christos    and change the stack frame arbitrarily during the prologue, the information
   2328  1.1  christos    is guaranteed valid only at the point in the function indicated by the PC.
   2329  1.1  christos    May be used to skip the prologue or identify the ABI, w/o tracking.
   2330  1.1  christos 
   2331  1.1  christos    Returns:   Address of first instruction after prologue, or PC (whichever
   2332  1.1  christos 	      is first), or 0, if decoding failed (in libisa).
   2333  1.1  christos    Input args:
   2334  1.1  christos       start   Start address of function/prologue.
   2335  1.1  christos       pc      Program counter to stop at.  Use 0 to continue to end of prologue.
   2336  1.1  christos 	      If 0, avoids infinite run-on in corrupt code memory by bounding
   2337  1.1  christos 	      the scan to the end of the function if that can be determined.
   2338  1.1  christos       nregs   Number of general registers to track.
   2339  1.1  christos    InOut args:
   2340  1.1  christos       cache   Xtensa frame cache.
   2341  1.1  christos 
   2342  1.1  christos       Note that these may produce useful results even if decoding fails
   2343  1.1  christos       because they begin with default assumptions that analysis may change.  */
   2344  1.1  christos 
   2345  1.1  christos static CORE_ADDR
   2346  1.1  christos call0_analyze_prologue (struct gdbarch *gdbarch,
   2347  1.1  christos 			CORE_ADDR start, CORE_ADDR pc,
   2348  1.1  christos 			int nregs, xtensa_frame_cache_t *cache)
   2349  1.1  christos {
   2350  1.1  christos   CORE_ADDR ia;		    /* Current insn address in prologue.  */
   2351  1.1  christos   CORE_ADDR ba = 0;	    /* Current address at base of insn buffer.  */
   2352  1.1  christos   CORE_ADDR bt;		    /* Current address at top+1 of insn buffer.  */
   2353  1.1  christos   gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue.  */
   2354  1.1  christos   xtensa_isa isa;	    /* libisa ISA handle.  */
   2355  1.1  christos   xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot.  */
   2356  1.1  christos   xtensa_format ifmt;	    /* libisa instruction format.  */
   2357  1.1  christos   int ilen, islots, is;	    /* Instruction length, nbr slots, current slot.  */
   2358  1.1  christos   xtensa_opcode opc;	    /* Opcode in current slot.  */
   2359  1.1  christos   xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis.  */
   2360  1.1  christos   int nods;		    /* Opcode number of operands.  */
   2361  1.1  christos   unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa.  */
   2362  1.1  christos   xtensa_c0reg_t *rtmp;	    /* Register tracking info snapshot.  */
   2363  1.1  christos   int j;		    /* General loop counter.  */
   2364  1.1  christos   int fail = 0;		    /* Set non-zero and exit, if decoding fails.  */
   2365  1.1  christos   CORE_ADDR body_pc;	    /* The PC for the first non-prologue insn.  */
   2366  1.1  christos   CORE_ADDR end_pc;	    /* The PC for the lust function insn.  */
   2367  1.1  christos 
   2368  1.1  christos   struct symtab_and_line prologue_sal;
   2369  1.1  christos 
   2370  1.1  christos   DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
   2371  1.1  christos 	      (int)start, (int)pc);
   2372  1.1  christos 
   2373  1.1  christos   /* Try to limit the scan to the end of the function if a non-zero pc
   2374  1.1  christos      arg was not supplied to avoid probing beyond the end of valid memory.
   2375  1.1  christos      If memory is full of garbage that classifies as c0opc_uninteresting.
   2376  1.1  christos      If this fails (eg. if no symbols) pc ends up 0 as it was.
   2377  1.7  christos      Initialize the Call0 frame and register tracking info.
   2378  1.1  christos      Assume it's Call0 until an 'entry' instruction is encountered.
   2379  1.1  christos      Assume we may be in the prologue until we hit a flow control instr.  */
   2380  1.1  christos 
   2381  1.1  christos   rtmp = NULL;
   2382  1.1  christos   body_pc = UINT_MAX;
   2383  1.1  christos   end_pc = 0;
   2384  1.1  christos 
   2385  1.1  christos   /* Find out, if we have an information about the prologue from DWARF.  */
   2386  1.1  christos   prologue_sal = find_pc_line (start, 0);
   2387  1.1  christos   if (prologue_sal.line != 0) /* Found debug info.  */
   2388  1.1  christos     body_pc = prologue_sal.end;
   2389  1.1  christos 
   2390  1.1  christos   /* If we are going to analyze the prologue in general without knowing about
   2391  1.1  christos      the current PC, make the best assumtion for the end of the prologue.  */
   2392  1.1  christos   if (pc == 0)
   2393  1.1  christos     {
   2394  1.1  christos       find_pc_partial_function (start, 0, NULL, &end_pc);
   2395  1.7  christos       body_pc = std::min (end_pc, body_pc);
   2396  1.1  christos     }
   2397  1.1  christos   else
   2398  1.7  christos     body_pc = std::min (pc, body_pc);
   2399  1.1  christos 
   2400  1.1  christos   cache->call0 = 1;
   2401  1.1  christos   rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
   2402  1.1  christos 
   2403  1.1  christos   isa = xtensa_default_isa;
   2404  1.1  christos   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
   2405  1.1  christos   ins = xtensa_insnbuf_alloc (isa);
   2406  1.1  christos   slot = xtensa_insnbuf_alloc (isa);
   2407  1.1  christos 
   2408  1.1  christos   for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
   2409  1.1  christos     {
   2410  1.1  christos       /* (Re)fill instruction buffer from memory if necessary, but do not
   2411  1.1  christos          read memory beyond PC to be sure we stay within text section
   2412  1.1  christos 	 (this protection only works if a non-zero pc is supplied).  */
   2413  1.1  christos 
   2414  1.1  christos       if (ia + xtensa_isa_maxlength (isa) > bt)
   2415  1.1  christos         {
   2416  1.1  christos 	  ba = ia;
   2417  1.1  christos 	  bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
   2418  1.1  christos 	  if (target_read_memory (ba, ibuf, bt - ba) != 0 )
   2419  1.1  christos 	    error (_("Unable to read target memory ..."));
   2420  1.1  christos 	}
   2421  1.1  christos 
   2422  1.1  christos       /* Decode format information.  */
   2423  1.1  christos 
   2424  1.1  christos       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
   2425  1.1  christos       ifmt = xtensa_format_decode (isa, ins);
   2426  1.1  christos       if (ifmt == XTENSA_UNDEFINED)
   2427  1.1  christos 	{
   2428  1.1  christos 	  fail = 1;
   2429  1.1  christos 	  goto done;
   2430  1.1  christos 	}
   2431  1.1  christos       ilen = xtensa_format_length (isa, ifmt);
   2432  1.1  christos       if (ilen == XTENSA_UNDEFINED)
   2433  1.1  christos 	{
   2434  1.1  christos 	  fail = 1;
   2435  1.1  christos 	  goto done;
   2436  1.1  christos 	}
   2437  1.1  christos       islots = xtensa_format_num_slots (isa, ifmt);
   2438  1.1  christos       if (islots == XTENSA_UNDEFINED)
   2439  1.1  christos 	{
   2440  1.1  christos 	  fail = 1;
   2441  1.1  christos 	  goto done;
   2442  1.1  christos 	}
   2443  1.1  christos 
   2444  1.1  christos       /* Analyze a bundle or a single instruction, using a snapshot of
   2445  1.1  christos          the register tracking info as input for the entire bundle so that
   2446  1.1  christos 	 register changes do not take effect within this bundle.  */
   2447  1.1  christos 
   2448  1.1  christos       for (j = 0; j < nregs; ++j)
   2449  1.1  christos 	rtmp[j] = cache->c0.c0_rt[j];
   2450  1.1  christos 
   2451  1.1  christos       for (is = 0; is < islots; ++is)
   2452  1.1  christos         {
   2453  1.1  christos 	  /* Decode a slot and classify the opcode.  */
   2454  1.1  christos 
   2455  1.1  christos 	  fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
   2456  1.1  christos 	  if (fail)
   2457  1.1  christos 	    goto done;
   2458  1.1  christos 
   2459  1.1  christos 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
   2460  1.1  christos 	  DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
   2461  1.1  christos 		     (unsigned)ia, opc);
   2462  1.1  christos 	  if (opc == XTENSA_UNDEFINED)
   2463  1.1  christos 	    opclass = c0opc_illegal;
   2464  1.1  christos 	  else
   2465  1.1  christos 	    opclass = call0_classify_opcode (isa, opc);
   2466  1.1  christos 
   2467  1.1  christos 	  /* Decide whether to track this opcode, ignore it, or bail out.  */
   2468  1.1  christos 
   2469  1.1  christos 	  switch (opclass)
   2470  1.1  christos 	    {
   2471  1.1  christos 	    case c0opc_illegal:
   2472  1.1  christos 	    case c0opc_break:
   2473  1.1  christos 	      fail = 1;
   2474  1.1  christos 	      goto done;
   2475  1.1  christos 
   2476  1.1  christos 	    case c0opc_uninteresting:
   2477  1.1  christos 	      continue;
   2478  1.1  christos 
   2479  1.1  christos 	    case c0opc_flow:  /* Flow control instructions stop analysis.  */
   2480  1.1  christos 	    case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis.  */
   2481  1.1  christos 	      goto done;
   2482  1.1  christos 
   2483  1.1  christos 	    case c0opc_entry:
   2484  1.1  christos 	      cache->call0 = 0;
   2485  1.1  christos 	      ia += ilen;	       	/* Skip over 'entry' insn.  */
   2486  1.1  christos 	      goto done;
   2487  1.1  christos 
   2488  1.1  christos 	    default:
   2489  1.1  christos 	      cache->call0 = 1;
   2490  1.1  christos 	    }
   2491  1.1  christos 
   2492  1.1  christos 	  /* Only expected opcodes should get this far.  */
   2493  1.1  christos 
   2494  1.1  christos 	  /* Extract and decode the operands.  */
   2495  1.1  christos 	  nods = xtensa_opcode_num_operands (isa, opc);
   2496  1.1  christos 	  if (nods == XTENSA_UNDEFINED)
   2497  1.1  christos 	    {
   2498  1.1  christos 	      fail = 1;
   2499  1.1  christos 	      goto done;
   2500  1.1  christos 	    }
   2501  1.1  christos 
   2502  1.1  christos 	  for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
   2503  1.1  christos 	    {
   2504  1.1  christos 	      fail = xtensa_operand_get_field (isa, opc, j, ifmt,
   2505  1.1  christos 					       is, slot, &odv[j]);
   2506  1.1  christos 	      if (fail)
   2507  1.1  christos 		goto done;
   2508  1.1  christos 
   2509  1.1  christos 	      fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
   2510  1.1  christos 	      if (fail)
   2511  1.1  christos 		goto done;
   2512  1.1  christos 	    }
   2513  1.1  christos 
   2514  1.1  christos 	  /* Check operands to verify use of 'mov' assembler macro.  */
   2515  1.1  christos 	  if (opclass == c0opc_mov && nods == 3)
   2516  1.1  christos 	    {
   2517  1.1  christos 	      if (odv[2] == odv[1])
   2518  1.1  christos 		{
   2519  1.1  christos 		  nods = 2;
   2520  1.1  christos 		  if ((odv[0] == 1) && (odv[1] != 1))
   2521  1.1  christos 		    /* OR  A1, An, An  , where n != 1.
   2522  1.1  christos 		       This means we are inside epilogue already.  */
   2523  1.1  christos 		    goto done;
   2524  1.1  christos 		}
   2525  1.1  christos 	      else
   2526  1.1  christos 		{
   2527  1.1  christos 		  opclass = c0opc_uninteresting;
   2528  1.1  christos 		  continue;
   2529  1.1  christos 		}
   2530  1.1  christos 	    }
   2531  1.1  christos 
   2532  1.1  christos 	  /* Track register movement and modification for this operation.  */
   2533  1.1  christos 	  fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
   2534  1.1  christos 				 opclass, nods, odv, ia, 1, cache);
   2535  1.1  christos 	  if (fail)
   2536  1.1  christos 	    goto done;
   2537  1.1  christos 	}
   2538  1.1  christos     }
   2539  1.1  christos done:
   2540  1.1  christos   DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
   2541  1.1  christos 	     (unsigned)ia, fail ? "failed" : "succeeded");
   2542  1.1  christos   xtensa_insnbuf_free(isa, slot);
   2543  1.1  christos   xtensa_insnbuf_free(isa, ins);
   2544  1.1  christos   return fail ? XTENSA_ISA_BADPC : ia;
   2545  1.1  christos }
   2546  1.1  christos 
   2547  1.1  christos /* Initialize frame cache for the current frame in CALL0 ABI.  */
   2548  1.1  christos 
   2549  1.1  christos static void
   2550  1.1  christos call0_frame_cache (struct frame_info *this_frame,
   2551  1.1  christos 		   xtensa_frame_cache_t *cache, CORE_ADDR pc)
   2552  1.1  christos {
   2553  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   2554  1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   2555  1.1  christos   CORE_ADDR start_pc;		/* The beginning of the function.  */
   2556  1.1  christos   CORE_ADDR body_pc=UINT_MAX;	/* PC, where prologue analysis stopped.  */
   2557  1.1  christos   CORE_ADDR sp, fp, ra;
   2558  1.1  christos   int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
   2559  1.1  christos 
   2560  1.1  christos   sp = get_frame_register_unsigned
   2561  1.1  christos     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
   2562  1.1  christos   fp = sp; /* Assume FP == SP until proven otherwise.  */
   2563  1.1  christos 
   2564  1.1  christos   /* Find the beginning of the prologue of the function containing the PC
   2565  1.1  christos      and analyze it up to the PC or the end of the prologue.  */
   2566  1.1  christos 
   2567  1.1  christos   if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
   2568  1.1  christos     {
   2569  1.1  christos       body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
   2570  1.1  christos 
   2571  1.1  christos       if (body_pc == XTENSA_ISA_BADPC)
   2572  1.1  christos 	{
   2573  1.1  christos 	  warning_once ();
   2574  1.1  christos 	  ra = 0;
   2575  1.1  christos 	  goto finish_frame_analysis;
   2576  1.1  christos 	}
   2577  1.1  christos     }
   2578  1.1  christos 
   2579  1.1  christos   /* Get the frame information and FP (if used) at the current PC.
   2580  1.1  christos      If PC is in the prologue, the prologue analysis is more reliable
   2581  1.1  christos      than DWARF info.  We don't not know for sure, if PC is in the prologue,
   2582  1.1  christos      but we do know no calls have yet taken place, so we can almost
   2583  1.1  christos      certainly rely on the prologue analysis.  */
   2584  1.1  christos 
   2585  1.1  christos   if (body_pc <= pc)
   2586  1.1  christos     {
   2587  1.1  christos       /* Prologue analysis was successful up to the PC.
   2588  1.1  christos          It includes the cases when PC == START_PC.  */
   2589  1.1  christos       c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
   2590  1.1  christos       /* c0_hasfp == true means there is a frame pointer because
   2591  1.1  christos 	 we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
   2592  1.1  christos 	 was derived from SP.  Otherwise, it would be C0_FP.  */
   2593  1.1  christos       fp_regnum = c0_hasfp ? C0_FP : C0_SP;
   2594  1.1  christos       c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
   2595  1.1  christos       fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
   2596  1.1  christos     }
   2597  1.1  christos   else  /* No data from the prologue analysis.  */
   2598  1.1  christos     {
   2599  1.1  christos       c0_hasfp = 0;
   2600  1.1  christos       fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
   2601  1.1  christos       c0_frmsz = 0;
   2602  1.1  christos       start_pc = pc;
   2603  1.1  christos    }
   2604  1.1  christos 
   2605  1.1  christos   if (cache->c0.c0_fpalign)
   2606  1.1  christos     {
   2607  1.1  christos       /* This frame has a special prologue with a dynamic stack adjustment
   2608  1.1  christos 	 to force an alignment, which is bigger than standard 16 bytes.  */
   2609  1.1  christos 
   2610  1.1  christos       CORE_ADDR unaligned_sp;
   2611  1.1  christos 
   2612  1.1  christos       if (cache->c0.c0_old_sp == C0_INEXP)
   2613  1.1  christos 	/* This can't be.  Prologue code should be consistent.
   2614  1.1  christos 	   Unaligned stack pointer should be saved in a spare register.  */
   2615  1.1  christos 	{
   2616  1.1  christos 	  warning_once ();
   2617  1.1  christos 	  ra = 0;
   2618  1.1  christos 	  goto finish_frame_analysis;
   2619  1.1  christos 	}
   2620  1.1  christos 
   2621  1.1  christos       if (cache->c0.c0_sp_ofs == C0_NOSTK)
   2622  1.1  christos 	/* Saved unaligned value of SP is kept in a register.  */
   2623  1.1  christos 	unaligned_sp = get_frame_register_unsigned
   2624  1.1  christos 	  (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
   2625  1.1  christos       else
   2626  1.1  christos 	/* Get the value from stack.  */
   2627  1.1  christos 	unaligned_sp = (CORE_ADDR)
   2628  1.1  christos 	  read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
   2629  1.1  christos 
   2630  1.1  christos       prev_sp = unaligned_sp + c0_frmsz;
   2631  1.1  christos     }
   2632  1.1  christos   else
   2633  1.1  christos     prev_sp = fp + c0_frmsz;
   2634  1.1  christos 
   2635  1.1  christos   /* Frame size from debug info or prologue tracking does not account for
   2636  1.1  christos      alloca() and other dynamic allocations.  Adjust frame size by FP - SP.  */
   2637  1.1  christos   if (c0_hasfp)
   2638  1.1  christos     {
   2639  1.1  christos       fp = get_frame_register_unsigned (this_frame, fp_regnum);
   2640  1.1  christos 
   2641  1.1  christos       /* Update the stack frame size.  */
   2642  1.1  christos       c0_frmsz += fp - sp;
   2643  1.1  christos     }
   2644  1.1  christos 
   2645  1.1  christos   /* Get the return address (RA) from the stack if saved,
   2646  1.1  christos      or try to get it from a register.  */
   2647  1.1  christos 
   2648  1.1  christos   to_stk = cache->c0.c0_rt[C0_RA].to_stk;
   2649  1.1  christos   if (to_stk != C0_NOSTK)
   2650  1.1  christos     ra = (CORE_ADDR)
   2651  1.1  christos       read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
   2652  1.1  christos 			   4, byte_order);
   2653  1.1  christos 
   2654  1.1  christos   else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
   2655  1.1  christos 	   && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
   2656  1.1  christos     {
   2657  1.1  christos       /* Special case for terminating backtrace at a function that wants to
   2658  1.1  christos 	 be seen as the outermost one.  Such a function will clear it's RA (A0)
   2659  1.1  christos 	 register to 0 in the prologue instead of saving its original value.  */
   2660  1.1  christos       ra = 0;
   2661  1.1  christos     }
   2662  1.1  christos   else
   2663  1.1  christos     {
   2664  1.1  christos       /* RA was copied to another register or (before any function call) may
   2665  1.1  christos 	 still be in the original RA register.  This is not always reliable:
   2666  1.1  christos 	 even in a leaf function, register tracking stops after prologue, and
   2667  1.1  christos 	 even in prologue, non-prologue instructions (not tracked) may overwrite
   2668  1.1  christos 	 RA or any register it was copied to.  If likely in prologue or before
   2669  1.1  christos 	 any call, use retracking info and hope for the best (compiler should
   2670  1.1  christos 	 have saved RA in stack if not in a leaf function).  If not in prologue,
   2671  1.1  christos 	 too bad.  */
   2672  1.1  christos 
   2673  1.1  christos       int i;
   2674  1.1  christos       for (i = 0;
   2675  1.1  christos 	   (i < C0_NREGS)
   2676  1.1  christos 	   && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
   2677  1.1  christos 	   ++i);
   2678  1.1  christos       if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
   2679  1.1  christos 	i = C0_RA;
   2680  1.1  christos       if (i < C0_NREGS)
   2681  1.1  christos 	{
   2682  1.1  christos 	  ra = get_frame_register_unsigned
   2683  1.1  christos 	    (this_frame,
   2684  1.1  christos 	     gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
   2685  1.1  christos 	}
   2686  1.1  christos       else ra = 0;
   2687  1.1  christos     }
   2688  1.1  christos 
   2689  1.1  christos  finish_frame_analysis:
   2690  1.1  christos   cache->pc = start_pc;
   2691  1.1  christos   cache->ra = ra;
   2692  1.1  christos   /* RA == 0 marks the outermost frame.  Do not go past it.  */
   2693  1.1  christos   cache->prev_sp = (ra != 0) ?  prev_sp : 0;
   2694  1.1  christos   cache->c0.fp_regnum = fp_regnum;
   2695  1.1  christos   cache->c0.c0_frmsz = c0_frmsz;
   2696  1.1  christos   cache->c0.c0_hasfp = c0_hasfp;
   2697  1.1  christos   cache->c0.c0_fp = fp;
   2698  1.1  christos }
   2699  1.1  christos 
   2700  1.1  christos static CORE_ADDR a0_saved;
   2701  1.1  christos static CORE_ADDR a7_saved;
   2702  1.1  christos static CORE_ADDR a11_saved;
   2703  1.1  christos static int a0_was_saved;
   2704  1.1  christos static int a7_was_saved;
   2705  1.1  christos static int a11_was_saved;
   2706  1.1  christos 
   2707  1.1  christos /* Simulate L32E instruction:  AT <-- ref (AS + offset).  */
   2708  1.1  christos static void
   2709  1.1  christos execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
   2710  1.1  christos {
   2711  1.1  christos   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
   2712  1.1  christos   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
   2713  1.1  christos   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
   2714  1.1  christos   unsigned int spilled_value
   2715  1.1  christos     = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
   2716  1.1  christos 
   2717  1.1  christos   if ((at == 0) && !a0_was_saved)
   2718  1.1  christos     {
   2719  1.1  christos       a0_saved = xtensa_read_register (atreg);
   2720  1.1  christos       a0_was_saved = 1;
   2721  1.1  christos     }
   2722  1.1  christos   else if ((at == 7) && !a7_was_saved)
   2723  1.1  christos     {
   2724  1.1  christos       a7_saved = xtensa_read_register (atreg);
   2725  1.1  christos       a7_was_saved = 1;
   2726  1.1  christos     }
   2727  1.1  christos   else if ((at == 11) && !a11_was_saved)
   2728  1.1  christos     {
   2729  1.1  christos       a11_saved = xtensa_read_register (atreg);
   2730  1.1  christos       a11_was_saved = 1;
   2731  1.1  christos     }
   2732  1.1  christos 
   2733  1.1  christos   xtensa_write_register (atreg, spilled_value);
   2734  1.1  christos }
   2735  1.1  christos 
   2736  1.1  christos /* Simulate S32E instruction:  AT --> ref (AS + offset).  */
   2737  1.1  christos static void
   2738  1.1  christos execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
   2739  1.1  christos {
   2740  1.1  christos   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
   2741  1.1  christos   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
   2742  1.1  christos   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
   2743  1.1  christos   ULONGEST spilled_value = xtensa_read_register (atreg);
   2744  1.1  christos 
   2745  1.1  christos   write_memory_unsigned_integer (addr, 4,
   2746  1.1  christos 				 gdbarch_byte_order (gdbarch),
   2747  1.1  christos 				 spilled_value);
   2748  1.1  christos }
   2749  1.1  christos 
   2750  1.1  christos #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN  200
   2751  1.1  christos 
   2752  1.1  christos typedef enum
   2753  1.1  christos {
   2754  1.1  christos   xtWindowOverflow,
   2755  1.1  christos   xtWindowUnderflow,
   2756  1.1  christos   xtNoExceptionHandler
   2757  1.1  christos } xtensa_exception_handler_t;
   2758  1.1  christos 
   2759  1.1  christos /* Execute instruction stream from current PC until hitting RFWU or RFWO.
   2760  1.1  christos    Return type of Xtensa Window Interrupt Handler on success.  */
   2761  1.1  christos static xtensa_exception_handler_t
   2762  1.1  christos execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
   2763  1.1  christos {
   2764  1.1  christos   xtensa_isa isa;
   2765  1.1  christos   xtensa_insnbuf ins, slot;
   2766  1.1  christos   gdb_byte ibuf[XTENSA_ISA_BSZ];
   2767  1.1  christos   CORE_ADDR ia, bt, ba;
   2768  1.1  christos   xtensa_format ifmt;
   2769  1.1  christos   int ilen, islots, is;
   2770  1.1  christos   xtensa_opcode opc;
   2771  1.1  christos   int insn_num = 0;
   2772  1.1  christos   void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
   2773  1.1  christos 
   2774  1.1  christos   uint32_t at, as, offset;
   2775  1.1  christos 
   2776  1.1  christos   /* WindowUnderflow12 = true, when inside _WindowUnderflow12.  */
   2777  1.1  christos   int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;
   2778  1.1  christos 
   2779  1.1  christos   isa = xtensa_default_isa;
   2780  1.1  christos   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
   2781  1.1  christos   ins = xtensa_insnbuf_alloc (isa);
   2782  1.1  christos   slot = xtensa_insnbuf_alloc (isa);
   2783  1.1  christos   ba = 0;
   2784  1.1  christos   ia = current_pc;
   2785  1.1  christos   bt = ia;
   2786  1.1  christos 
   2787  1.1  christos   a0_was_saved = 0;
   2788  1.1  christos   a7_was_saved = 0;
   2789  1.1  christos   a11_was_saved = 0;
   2790  1.1  christos 
   2791  1.1  christos   while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
   2792  1.1  christos     {
   2793  1.1  christos       if (ia + xtensa_isa_maxlength (isa) > bt)
   2794  1.1  christos         {
   2795  1.1  christos 	  ba = ia;
   2796  1.1  christos 	  bt = (ba + XTENSA_ISA_BSZ);
   2797  1.1  christos 	  if (target_read_memory (ba, ibuf, bt - ba) != 0)
   2798  1.1  christos 	    return xtNoExceptionHandler;
   2799  1.1  christos 	}
   2800  1.1  christos       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
   2801  1.1  christos       ifmt = xtensa_format_decode (isa, ins);
   2802  1.1  christos       if (ifmt == XTENSA_UNDEFINED)
   2803  1.1  christos 	return xtNoExceptionHandler;
   2804  1.1  christos       ilen = xtensa_format_length (isa, ifmt);
   2805  1.1  christos       if (ilen == XTENSA_UNDEFINED)
   2806  1.1  christos 	return xtNoExceptionHandler;
   2807  1.1  christos       islots = xtensa_format_num_slots (isa, ifmt);
   2808  1.1  christos       if (islots == XTENSA_UNDEFINED)
   2809  1.1  christos 	return xtNoExceptionHandler;
   2810  1.1  christos       for (is = 0; is < islots; ++is)
   2811  1.1  christos 	{
   2812  1.1  christos 	  if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
   2813  1.1  christos 	    return xtNoExceptionHandler;
   2814  1.1  christos 	  opc = xtensa_opcode_decode (isa, ifmt, is, slot);
   2815  1.1  christos 	  if (opc == XTENSA_UNDEFINED)
   2816  1.1  christos 	    return xtNoExceptionHandler;
   2817  1.1  christos 	  switch (call0_classify_opcode (isa, opc))
   2818  1.1  christos 	    {
   2819  1.1  christos 	    case c0opc_illegal:
   2820  1.1  christos 	    case c0opc_flow:
   2821  1.1  christos 	    case c0opc_entry:
   2822  1.1  christos 	    case c0opc_break:
   2823  1.1  christos 	      /* We expect none of them here.  */
   2824  1.1  christos 	      return xtNoExceptionHandler;
   2825  1.1  christos 	    case c0opc_l32e:
   2826  1.1  christos 	      func = execute_l32e;
   2827  1.1  christos 	      break;
   2828  1.1  christos 	    case c0opc_s32e:
   2829  1.1  christos 	      func = execute_s32e;
   2830  1.1  christos 	      break;
   2831  1.1  christos 	    case c0opc_rfwo: /* RFWO.  */
   2832  1.1  christos 	      /* Here, we return from WindowOverflow handler and,
   2833  1.1  christos 		 if we stopped at the very beginning, which means
   2834  1.1  christos 		 A0 was saved, we have to restore it now.  */
   2835  1.1  christos 	      if (a0_was_saved)
   2836  1.1  christos 		{
   2837  1.1  christos 		  int arreg = arreg_number (gdbarch,
   2838  1.1  christos 					    gdbarch_tdep (gdbarch)->a0_base,
   2839  1.1  christos 					    wb);
   2840  1.1  christos 		  xtensa_write_register (arreg, a0_saved);
   2841  1.1  christos 		}
   2842  1.1  christos 	      return xtWindowOverflow;
   2843  1.1  christos 	    case c0opc_rfwu: /* RFWU.  */
   2844  1.1  christos 	      /* Here, we return from WindowUnderflow handler.
   2845  1.1  christos 		 Let's see if either A7 or A11 has to be restored.  */
   2846  1.1  christos 	      if (WindowUnderflow12)
   2847  1.1  christos 		{
   2848  1.1  christos 		  if (a11_was_saved)
   2849  1.1  christos 		    {
   2850  1.1  christos 		      int arreg = arreg_number (gdbarch,
   2851  1.1  christos 						gdbarch_tdep (gdbarch)->a0_base + 11,
   2852  1.1  christos 						wb);
   2853  1.1  christos 		      xtensa_write_register (arreg, a11_saved);
   2854  1.1  christos 		    }
   2855  1.1  christos 		}
   2856  1.1  christos 	      else if (a7_was_saved)
   2857  1.1  christos 		{
   2858  1.1  christos 		  int arreg = arreg_number (gdbarch,
   2859  1.1  christos 					    gdbarch_tdep (gdbarch)->a0_base + 7,
   2860  1.1  christos 					    wb);
   2861  1.1  christos 		  xtensa_write_register (arreg, a7_saved);
   2862  1.1  christos 		}
   2863  1.1  christos 	      return xtWindowUnderflow;
   2864  1.1  christos  	    default: /* Simply skip this insns.  */
   2865  1.1  christos 	      continue;
   2866  1.1  christos 	    }
   2867  1.1  christos 
   2868  1.1  christos 	  /* Decode arguments for L32E / S32E and simulate their execution.  */
   2869  1.1  christos 	  if ( xtensa_opcode_num_operands (isa, opc) != 3 )
   2870  1.1  christos 	    return xtNoExceptionHandler;
   2871  1.1  christos 	  if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
   2872  1.1  christos 	    return xtNoExceptionHandler;
   2873  1.1  christos 	  if (xtensa_operand_decode (isa, opc, 0, &at))
   2874  1.1  christos 	    return xtNoExceptionHandler;
   2875  1.1  christos 	  if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
   2876  1.1  christos 	    return xtNoExceptionHandler;
   2877  1.1  christos 	  if (xtensa_operand_decode (isa, opc, 1, &as))
   2878  1.1  christos 	    return xtNoExceptionHandler;
   2879  1.1  christos 	  if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
   2880  1.1  christos 	    return xtNoExceptionHandler;
   2881  1.1  christos 	  if (xtensa_operand_decode (isa, opc, 2, &offset))
   2882  1.1  christos 	    return xtNoExceptionHandler;
   2883  1.1  christos 
   2884  1.1  christos 	  (*func) (gdbarch, at, as, offset, wb);
   2885  1.1  christos 	}
   2886  1.1  christos 
   2887  1.1  christos       ia += ilen;
   2888  1.1  christos     }
   2889  1.1  christos   return xtNoExceptionHandler;
   2890  1.1  christos }
   2891  1.1  christos 
   2892  1.1  christos /* Handle Window Overflow / Underflow exception frames.  */
   2893  1.1  christos 
   2894  1.1  christos static void
   2895  1.1  christos xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
   2896  1.1  christos 				     xtensa_frame_cache_t *cache,
   2897  1.1  christos 				     CORE_ADDR pc)
   2898  1.1  christos {
   2899  1.1  christos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
   2900  1.1  christos   CORE_ADDR ps, wb, ws, ra;
   2901  1.1  christos   int epc1_regnum, i, regnum;
   2902  1.1  christos   xtensa_exception_handler_t eh_type;
   2903  1.1  christos 
   2904  1.1  christos   /* Read PS, WB, and WS from the hardware. Note that PS register
   2905  1.1  christos      must be present, if Windowed ABI is supported.  */
   2906  1.1  christos   ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
   2907  1.1  christos   wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
   2908  1.1  christos   ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);
   2909  1.1  christos 
   2910  1.1  christos   /* Execute all the remaining instructions from Window Interrupt Handler
   2911  1.1  christos      by simulating them on the remote protocol level.  On return, set the
   2912  1.1  christos      type of Xtensa Window Interrupt Handler, or report an error.  */
   2913  1.1  christos   eh_type = execute_code (gdbarch, pc, wb);
   2914  1.1  christos   if (eh_type == xtNoExceptionHandler)
   2915  1.1  christos     error (_("\
   2916  1.1  christos Unable to decode Xtensa Window Interrupt Handler's code."));
   2917  1.1  christos 
   2918  1.1  christos   cache->ps = ps ^ PS_EXC;	/* Clear the exception bit in PS.  */
   2919  1.1  christos   cache->call0 = 0;		/* It's Windowed ABI.  */
   2920  1.1  christos 
   2921  1.1  christos   /* All registers for the cached frame will be alive.  */
   2922  1.1  christos   for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
   2923  1.1  christos     cache->wd.aregs[i] = -1;
   2924  1.1  christos 
   2925  1.1  christos   if (eh_type == xtWindowOverflow)
   2926  1.1  christos     cache->wd.ws = ws ^ (1 << wb);
   2927  1.1  christos   else /* eh_type == xtWindowUnderflow.  */
   2928  1.1  christos     cache->wd.ws = ws | (1 << wb);
   2929  1.1  christos 
   2930  1.1  christos   cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB.  */
   2931  1.1  christos   regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
   2932  1.1  christos 			 cache->wd.wb);
   2933  1.1  christos   ra = xtensa_read_register (regnum);
   2934  1.1  christos   cache->wd.callsize = WINSIZE (ra);
   2935  1.1  christos   cache->prev_sp = xtensa_read_register (regnum + 1);
   2936  1.1  christos   /* Set regnum to a frame pointer of the frame being cached.  */
   2937  1.1  christos   regnum = xtensa_scan_prologue (gdbarch, pc);
   2938  1.1  christos   regnum = arreg_number (gdbarch,
   2939  1.1  christos 			 gdbarch_tdep (gdbarch)->a0_base + regnum,
   2940  1.1  christos 			 cache->wd.wb);
   2941  1.1  christos   cache->base = get_frame_register_unsigned (this_frame, regnum);
   2942  1.1  christos 
   2943  1.1  christos   /* Read PC of interrupted function from EPC1 register.  */
   2944  1.1  christos   epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
   2945  1.1  christos   if (epc1_regnum < 0)
   2946  1.1  christos     error(_("Unable to read Xtensa register EPC1"));
   2947  1.1  christos   cache->ra = xtensa_read_register (epc1_regnum);
   2948  1.1  christos   cache->pc = get_frame_func (this_frame);
   2949  1.1  christos }
   2950  1.1  christos 
   2951  1.1  christos 
   2952  1.1  christos /* Skip function prologue.
   2953  1.1  christos 
   2954  1.1  christos    Return the pc of the first instruction after prologue.  GDB calls this to
   2955  1.1  christos    find the address of the first line of the function or (if there is no line
   2956  1.1  christos    number information) to skip the prologue for planting breakpoints on
   2957  1.1  christos    function entries.  Use debug info (if present) or prologue analysis to skip
   2958  1.1  christos    the prologue to achieve reliable debugging behavior.  For windowed ABI,
   2959  1.1  christos    only the 'entry' instruction is skipped.  It is not strictly necessary to
   2960  1.1  christos    skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
   2961  1.1  christos    backtrace at any point in the prologue, however certain potential hazards
   2962  1.1  christos    are avoided and a more "normal" debugging experience is ensured by
   2963  1.1  christos    skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
   2964  1.1  christos    For example, if we don't skip the prologue:
   2965  1.1  christos    - Some args may not yet have been saved to the stack where the debug
   2966  1.1  christos      info expects to find them (true anyway when only 'entry' is skipped);
   2967  1.1  christos    - Software breakpoints ('break' instrs) may not have been unplanted
   2968  1.1  christos      when the prologue analysis is done on initializing the frame cache,
   2969  1.1  christos      and breaks in the prologue will throw off the analysis.
   2970  1.1  christos 
   2971  1.1  christos    If we have debug info ( line-number info, in particular ) we simply skip
   2972  1.1  christos    the code associated with the first function line effectively skipping
   2973  1.1  christos    the prologue code.  It works even in cases like
   2974  1.1  christos 
   2975  1.1  christos    int main()
   2976  1.1  christos    {	int local_var = 1;
   2977  1.1  christos    	....
   2978  1.1  christos    }
   2979  1.1  christos 
   2980  1.1  christos    because, for this source code, both Xtensa compilers will generate two
   2981  1.1  christos    separate entries ( with the same line number ) in dwarf line-number
   2982  1.1  christos    section to make sure there is a boundary between the prologue code and
   2983  1.1  christos    the rest of the function.
   2984  1.1  christos 
   2985  1.1  christos    If there is no debug info, we need to analyze the code.  */
   2986  1.1  christos 
   2987  1.1  christos /* #define DONT_SKIP_PROLOGUE  */
   2988  1.1  christos 
   2989  1.1  christos static CORE_ADDR
   2990  1.1  christos xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
   2991  1.1  christos {
   2992  1.1  christos   struct symtab_and_line prologue_sal;
   2993  1.1  christos   CORE_ADDR body_pc;
   2994  1.1  christos 
   2995  1.1  christos   DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
   2996  1.1  christos 
   2997  1.1  christos #if DONT_SKIP_PROLOGUE
   2998  1.1  christos   return start_pc;
   2999  1.1  christos #endif
   3000  1.1  christos 
   3001  1.1  christos  /* Try to find first body line from debug info.  */
   3002  1.1  christos 
   3003  1.1  christos   prologue_sal = find_pc_line (start_pc, 0);
   3004  1.1  christos   if (prologue_sal.line != 0) /* Found debug info.  */
   3005  1.1  christos     {
   3006  1.1  christos       /* In Call0,  it is possible to have a function with only one instruction
   3007  1.1  christos 	 ('ret') resulting from a one-line optimized function that does nothing.
   3008  1.1  christos 	 In that case,  prologue_sal.end may actually point to the start of the
   3009  1.1  christos 	 next function in the text section,  causing a breakpoint to be set at
   3010  1.1  christos 	 the wrong place.  Check,  if the end address is within a different
   3011  1.1  christos 	 function,  and if so return the start PC.  We know we have symbol
   3012  1.1  christos 	 information.  */
   3013  1.1  christos 
   3014  1.1  christos       CORE_ADDR end_func;
   3015  1.1  christos 
   3016  1.1  christos       if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
   3017  1.1  christos 	  && call0_ret (start_pc, prologue_sal.end))
   3018  1.1  christos 	return start_pc;
   3019  1.1  christos 
   3020  1.1  christos       find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
   3021  1.1  christos       if (end_func != start_pc)
   3022  1.1  christos 	return start_pc;
   3023  1.1  christos 
   3024  1.1  christos       return prologue_sal.end;
   3025  1.1  christos     }
   3026  1.1  christos 
   3027  1.1  christos   /* No debug line info.  Analyze prologue for Call0 or simply skip ENTRY.  */
   3028  1.1  christos   body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
   3029  1.1  christos 				    xtensa_alloc_frame_cache (0));
   3030  1.1  christos   return body_pc != 0 ? body_pc : start_pc;
   3031  1.1  christos }
   3032  1.1  christos 
   3033  1.1  christos /* Verify the current configuration.  */
   3034  1.1  christos static void
   3035  1.1  christos xtensa_verify_config (struct gdbarch *gdbarch)
   3036  1.1  christos {
   3037  1.7  christos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   3038  1.7  christos   string_file log;
   3039  1.1  christos 
   3040  1.1  christos   /* Verify that we got a reasonable number of AREGS.  */
   3041  1.1  christos   if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
   3042  1.7  christos     log.printf (_("\
   3043  1.1  christos \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
   3044  1.7  christos 		tdep->num_aregs);
   3045  1.1  christos 
   3046  1.1  christos   /* Verify that certain registers exist.  */
   3047  1.1  christos 
   3048  1.1  christos   if (tdep->pc_regnum == -1)
   3049  1.7  christos     log.printf (_("\n\tpc_regnum: No PC register"));
   3050  1.1  christos   if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
   3051  1.7  christos     log.printf (_("\n\tps_regnum: No PS register"));
   3052  1.1  christos 
   3053  1.1  christos   if (tdep->isa_use_windowed_registers)
   3054  1.1  christos     {
   3055  1.1  christos       if (tdep->wb_regnum == -1)
   3056  1.7  christos 	log.printf (_("\n\twb_regnum: No WB register"));
   3057  1.1  christos       if (tdep->ws_regnum == -1)
   3058  1.7  christos 	log.printf (_("\n\tws_regnum: No WS register"));
   3059  1.1  christos       if (tdep->ar_base == -1)
   3060  1.7  christos 	log.printf (_("\n\tar_base: No AR registers"));
   3061  1.1  christos     }
   3062  1.1  christos 
   3063  1.1  christos   if (tdep->a0_base == -1)
   3064  1.7  christos     log.printf (_("\n\ta0_base: No Ax registers"));
   3065  1.1  christos 
   3066  1.7  christos   if (!log.empty ())
   3067  1.1  christos     internal_error (__FILE__, __LINE__,
   3068  1.7  christos 		    _("the following are invalid: %s"), log.c_str ());
   3069  1.1  christos }
   3070  1.1  christos 
   3071  1.1  christos 
   3072  1.1  christos /* Derive specific register numbers from the array of registers.  */
   3073  1.1  christos 
   3074  1.1  christos static void
   3075  1.1  christos xtensa_derive_tdep (struct gdbarch_tdep *tdep)
   3076  1.1  christos {
   3077  1.1  christos   xtensa_register_t* rmap;
   3078  1.1  christos   int n, max_size = 4;
   3079  1.1  christos 
   3080  1.1  christos   tdep->num_regs = 0;
   3081  1.1  christos   tdep->num_nopriv_regs = 0;
   3082  1.1  christos 
   3083  1.1  christos /* Special registers 0..255 (core).  */
   3084  1.1  christos #define XTENSA_DBREGN_SREG(n)  (0x0200+(n))
   3085  1.7  christos /* User registers 0..255.  */
   3086  1.7  christos #define XTENSA_DBREGN_UREG(n)  (0x0300+(n))
   3087  1.1  christos 
   3088  1.1  christos   for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
   3089  1.1  christos     {
   3090  1.1  christos       if (rmap->target_number == 0x0020)
   3091  1.1  christos 	tdep->pc_regnum = n;
   3092  1.1  christos       else if (rmap->target_number == 0x0100)
   3093  1.1  christos 	tdep->ar_base = n;
   3094  1.1  christos       else if (rmap->target_number == 0x0000)
   3095  1.1  christos 	tdep->a0_base = n;
   3096  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
   3097  1.1  christos 	tdep->wb_regnum = n;
   3098  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
   3099  1.1  christos 	tdep->ws_regnum = n;
   3100  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
   3101  1.1  christos 	tdep->debugcause_regnum = n;
   3102  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
   3103  1.1  christos 	tdep->exccause_regnum = n;
   3104  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
   3105  1.1  christos 	tdep->excvaddr_regnum = n;
   3106  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
   3107  1.1  christos 	tdep->lbeg_regnum = n;
   3108  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
   3109  1.1  christos 	tdep->lend_regnum = n;
   3110  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
   3111  1.1  christos 	tdep->lcount_regnum = n;
   3112  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
   3113  1.1  christos 	tdep->sar_regnum = n;
   3114  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
   3115  1.1  christos 	tdep->litbase_regnum = n;
   3116  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
   3117  1.1  christos 	tdep->ps_regnum = n;
   3118  1.7  christos       else if (rmap->target_number == XTENSA_DBREGN_UREG(231))
   3119  1.7  christos 	tdep->threadptr_regnum = n;
   3120  1.1  christos #if 0
   3121  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
   3122  1.1  christos 	tdep->interrupt_regnum = n;
   3123  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
   3124  1.1  christos 	tdep->interrupt2_regnum = n;
   3125  1.1  christos       else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
   3126  1.1  christos 	tdep->cpenable_regnum = n;
   3127  1.1  christos #endif
   3128  1.1  christos 
   3129  1.1  christos       if (rmap->byte_size > max_size)
   3130  1.1  christos 	max_size = rmap->byte_size;
   3131  1.1  christos       if (rmap->mask != 0 && tdep->num_regs == 0)
   3132  1.1  christos 	tdep->num_regs = n;
   3133  1.1  christos       if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
   3134  1.8  christos 	  && tdep->num_nopriv_regs == 0)
   3135  1.8  christos 	tdep->num_nopriv_regs = n;
   3136  1.1  christos     }
   3137  1.8  christos   if (tdep->num_regs == 0)
   3138  1.8  christos     tdep->num_regs = tdep->num_nopriv_regs;
   3139  1.1  christos 
   3140  1.1  christos   /* Number of pseudo registers.  */
   3141  1.1  christos   tdep->num_pseudo_regs = n - tdep->num_regs;
   3142  1.1  christos 
   3143  1.1  christos   /* Empirically determined maximum sizes.  */
   3144  1.1  christos   tdep->max_register_raw_size = max_size;
   3145  1.1  christos   tdep->max_register_virtual_size = max_size;
   3146  1.1  christos }
   3147  1.1  christos 
   3148  1.1  christos /* Module "constructor" function.  */
   3149  1.1  christos 
   3150  1.1  christos extern struct gdbarch_tdep xtensa_tdep;
   3151  1.1  christos 
   3152  1.1  christos static struct gdbarch *
   3153  1.1  christos xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   3154  1.1  christos {
   3155  1.1  christos   struct gdbarch_tdep *tdep;
   3156  1.1  christos   struct gdbarch *gdbarch;
   3157  1.1  christos 
   3158  1.1  christos   DEBUGTRACE ("gdbarch_init()\n");
   3159  1.1  christos 
   3160  1.7  christos   if (!xtensa_default_isa)
   3161  1.7  christos     xtensa_default_isa = xtensa_isa_init (0, 0);
   3162  1.7  christos 
   3163  1.1  christos   /* We have to set the byte order before we call gdbarch_alloc.  */
   3164  1.1  christos   info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
   3165  1.1  christos 
   3166  1.1  christos   tdep = &xtensa_tdep;
   3167  1.1  christos   gdbarch = gdbarch_alloc (&info, tdep);
   3168  1.1  christos   xtensa_derive_tdep (tdep);
   3169  1.1  christos 
   3170  1.1  christos   /* Verify our configuration.  */
   3171  1.1  christos   xtensa_verify_config (gdbarch);
   3172  1.1  christos   xtensa_session_once_reported = 0;
   3173  1.1  christos 
   3174  1.7  christos   set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
   3175  1.7  christos   set_gdbarch_wchar_signed (gdbarch, 0);
   3176  1.7  christos 
   3177  1.1  christos   /* Pseudo-Register read/write.  */
   3178  1.1  christos   set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
   3179  1.1  christos   set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
   3180  1.1  christos 
   3181  1.1  christos   /* Set target information.  */
   3182  1.1  christos   set_gdbarch_num_regs (gdbarch, tdep->num_regs);
   3183  1.1  christos   set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
   3184  1.1  christos   set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
   3185  1.1  christos   set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
   3186  1.1  christos   set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
   3187  1.1  christos 
   3188  1.1  christos   /* Renumber registers for known formats (stabs and dwarf2).  */
   3189  1.1  christos   set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
   3190  1.1  christos   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
   3191  1.1  christos 
   3192  1.1  christos   /* We provide our own function to get register information.  */
   3193  1.1  christos   set_gdbarch_register_name (gdbarch, xtensa_register_name);
   3194  1.1  christos   set_gdbarch_register_type (gdbarch, xtensa_register_type);
   3195  1.1  christos 
   3196  1.1  christos   /* To call functions from GDB using dummy frame.  */
   3197  1.1  christos   set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
   3198  1.1  christos 
   3199  1.1  christos   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
   3200  1.1  christos 
   3201  1.1  christos   set_gdbarch_return_value (gdbarch, xtensa_return_value);
   3202  1.1  christos 
   3203  1.1  christos   /* Advance PC across any prologue instructions to reach "real" code.  */
   3204  1.1  christos   set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
   3205  1.1  christos 
   3206  1.1  christos   /* Stack grows downward.  */
   3207  1.1  christos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
   3208  1.1  christos 
   3209  1.1  christos   /* Set breakpoints.  */
   3210  1.7  christos   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
   3211  1.7  christos 				       xtensa_breakpoint_kind_from_pc);
   3212  1.7  christos   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
   3213  1.7  christos 				       xtensa_sw_breakpoint_from_kind);
   3214  1.1  christos 
   3215  1.1  christos   /* After breakpoint instruction or illegal instruction, pc still
   3216  1.1  christos      points at break instruction, so don't decrement.  */
   3217  1.1  christos   set_gdbarch_decr_pc_after_break (gdbarch, 0);
   3218  1.1  christos 
   3219  1.1  christos   /* We don't skip args.  */
   3220  1.1  christos   set_gdbarch_frame_args_skip (gdbarch, 0);
   3221  1.1  christos 
   3222  1.1  christos   set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
   3223  1.1  christos 
   3224  1.1  christos   set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
   3225  1.1  christos 
   3226  1.1  christos   set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
   3227  1.1  christos 
   3228  1.1  christos   /* Frame handling.  */
   3229  1.1  christos   frame_base_set_default (gdbarch, &xtensa_frame_base);
   3230  1.1  christos   frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
   3231  1.1  christos   dwarf2_append_unwinders (gdbarch);
   3232  1.1  christos 
   3233  1.1  christos   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
   3234  1.1  christos 
   3235  1.1  christos   xtensa_add_reggroups (gdbarch);
   3236  1.1  christos   set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
   3237  1.1  christos 
   3238  1.3  christos   set_gdbarch_iterate_over_regset_sections
   3239  1.3  christos     (gdbarch, xtensa_iterate_over_regset_sections);
   3240  1.1  christos 
   3241  1.1  christos   set_solib_svr4_fetch_link_map_offsets
   3242  1.1  christos     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
   3243  1.1  christos 
   3244  1.6  christos   /* Hook in the ABI-specific overrides, if they have been registered.  */
   3245  1.6  christos   gdbarch_init_osabi (info, gdbarch);
   3246  1.6  christos 
   3247  1.1  christos   return gdbarch;
   3248  1.1  christos }
   3249  1.1  christos 
   3250  1.1  christos static void
   3251  1.1  christos xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
   3252  1.1  christos {
   3253  1.1  christos   error (_("xtensa_dump_tdep(): not implemented"));
   3254  1.1  christos }
   3255  1.1  christos 
   3256  1.1  christos void
   3257  1.1  christos _initialize_xtensa_tdep (void)
   3258  1.1  christos {
   3259  1.1  christos   gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
   3260  1.1  christos   xtensa_init_reggroups ();
   3261  1.1  christos 
   3262  1.1  christos   add_setshow_zuinteger_cmd ("xtensa",
   3263  1.1  christos 			     class_maintenance,
   3264  1.1  christos 			     &xtensa_debug_level,
   3265  1.1  christos 			    _("Set Xtensa debugging."),
   3266  1.1  christos 			    _("Show Xtensa debugging."), _("\
   3267  1.1  christos When non-zero, Xtensa-specific debugging is enabled. \
   3268  1.1  christos Can be 1, 2, 3, or 4 indicating the level of debugging."),
   3269  1.1  christos 			     NULL,
   3270  1.1  christos 			     NULL,
   3271  1.1  christos 			     &setdebuglist, &showdebuglist);
   3272  1.1  christos }
   3273