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