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