gdb/xtensa-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "solib-svr4.h"
  17. #include "symtab.h"
  18. #include "symfile.h"
  19. #include "objfiles.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcore.h"
  22. #include "value.h"
  23. #include "dis-asm.h"
  24. #include "inferior.h"
  25. #include "floatformat.h"
  26. #include "regcache.h"
  27. #include "reggroups.h"
  28. #include "regset.h"

  29. #include "dummy-frame.h"
  30. #include "dwarf2.h"
  31. #include "dwarf2-frame.h"
  32. #include "dwarf2loc.h"
  33. #include "frame-base.h"
  34. #include "frame-unwind.h"

  35. #include "arch-utils.h"
  36. #include "gdbarch.h"
  37. #include "remote.h"
  38. #include "serial.h"

  39. #include "command.h"
  40. #include "gdbcmd.h"

  41. #include "xtensa-isa.h"
  42. #include "xtensa-tdep.h"
  43. #include "xtensa-config.h"


  44. static unsigned int xtensa_debug_level = 0;

  45. #define DEBUGWARN(args...) \
  46.   if (xtensa_debug_level > 0) \
  47.     fprintf_unfiltered (gdb_stdlog, "(warn ) " args)

  48. #define DEBUGINFO(args...) \
  49.   if (xtensa_debug_level > 1) \
  50.     fprintf_unfiltered (gdb_stdlog, "(info ) " args)

  51. #define DEBUGTRACE(args...) \
  52.   if (xtensa_debug_level > 2) \
  53.     fprintf_unfiltered (gdb_stdlog, "(trace) " args)

  54. #define DEBUGVERB(args...) \
  55.   if (xtensa_debug_level > 3) \
  56.     fprintf_unfiltered (gdb_stdlog, "(verb ) " args)


  57. /* According to the ABI, the SP must be aligned to 16-byte boundaries.  */
  58. #define SP_ALIGNMENT 16


  59. /* On Windowed ABI, we use a6 through a11 for passing arguments
  60.    to a function called by GDB because CALL4 is used.  */
  61. #define ARGS_NUM_REGS                6
  62. #define REGISTER_SIZE                4


  63. /* Extract the call size from the return address or PS register.  */
  64. #define PS_CALLINC_SHIFT        16
  65. #define PS_CALLINC_MASK                0x00030000
  66. #define CALLINC(ps)                (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
  67. #define WINSIZE(ra)                (4 * (( (ra) >> 30) & 0x3))

  68. /* On TX,  hardware can be configured without Exception Option.
  69.    There is no PS register in this case.  Inside XT-GDB,  let us treat
  70.    it as a virtual read-only register always holding the same value.  */
  71. #define TX_PS                        0x20

  72. /* ABI-independent macros.  */
  73. #define ARG_NOF(gdbarch) \
  74.   (gdbarch_tdep (gdbarch)->call_abi \
  75.    == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
  76. #define ARG_1ST(gdbarch) \
  77.   (gdbarch_tdep (gdbarch)->call_abi  == CallAbiCall0Only \
  78.    ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
  79.    : (gdbarch_tdep (gdbarch)->a0_base + 6))

  80. /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
  81.    indicates that the instruction is an ENTRY instruction.  */

  82. #define XTENSA_IS_ENTRY(gdbarch, op1) \
  83.   ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
  84.    ? ((op1) == 0x6c) : ((op1) == 0x36))

  85. #define XTENSA_ENTRY_LENGTH        3

  86. /* windowing_enabled() returns true, if windowing is enabled.
  87.    WOE must be set to 1; EXCM to 0.
  88.    Note: We assume that EXCM is always 0 for XEA1.  */

  89. #define PS_WOE                        (1<<18)
  90. #define PS_EXC                        (1<<4)

  91. static int
  92. windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
  93. {
  94.   /* If we know CALL0 ABI is set explicitly,  say it is Call0.  */
  95.   if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
  96.     return 0;

  97.   return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
  98. }

  99. /* Convert a live A-register number to the corresponding AR-register
  100.    number.  */
  101. static int
  102. arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
  103. {
  104.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  105.   int arreg;

  106.   arreg = a_regnum - tdep->a0_base;
  107.   arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
  108.   arreg &= tdep->num_aregs - 1;

  109.   return arreg + tdep->ar_base;
  110. }

  111. /* Convert a live AR-register number to the corresponding A-register order
  112.    number in a range [0..15].  Return -1, if AR_REGNUM is out of WB window.  */
  113. static int
  114. areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
  115. {
  116.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  117.   int areg;

  118.   areg = ar_regnum - tdep->ar_base;
  119.   if (areg < 0 || areg >= tdep->num_aregs)
  120.     return -1;
  121.   areg = (areg - wb * 4) & (tdep->num_aregs - 1);
  122.   return (areg > 15) ? -1 : areg;
  123. }

  124. /* Read Xtensa register directly from the hardware.  */
  125. static unsigned long
  126. xtensa_read_register (int regnum)
  127. {
  128.   ULONGEST value;

  129.   regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
  130.   return (unsigned long) value;
  131. }

  132. /* Write Xtensa register directly to the hardware.  */
  133. static void
  134. xtensa_write_register (int regnum, ULONGEST value)
  135. {
  136.   regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
  137. }

  138. /* Return the window size of the previous call to the function from which we
  139.    have just returned.

  140.    This function is used to extract the return value after a called function
  141.    has returned to the caller.  On Xtensa, the register that holds the return
  142.    value (from the perspective of the caller) depends on what call
  143.    instruction was used.  For now, we are assuming that the call instruction
  144.    precedes the current address, so we simply analyze the call instruction.
  145.    If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
  146.    method to call the inferior function.  */

  147. static int
  148. extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
  149. {
  150.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  151.   int winsize = 4;
  152.   int insn;
  153.   gdb_byte buf[4];

  154.   DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);

  155.   /* Read the previous instruction (should be a call[x]{4|8|12}.  */
  156.   read_memory (pc-3, buf, 3);
  157.   insn = extract_unsigned_integer (buf, 3, byte_order);

  158.   /* Decode call instruction:
  159.      Little Endian
  160.        call{0,4,8,12}   OFFSET || {00,01,10,11} || 0101
  161.        callx{0,4,8,12}  OFFSET || 11 || {00,01,10,11} || 0000
  162.      Big Endian
  163.        call{0,4,8,12}   0101 || {00,01,10,11} || OFFSET
  164.        callx{0,4,8,12}  0000 || {00,01,10,11} || 11 || OFFSET.  */

  165.   if (byte_order == BFD_ENDIAN_LITTLE)
  166.     {
  167.       if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
  168.         winsize = (insn & 0x30) >> 2;   /* 0, 4, 8, 12.  */
  169.     }
  170.   else
  171.     {
  172.       if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
  173.         winsize = (insn >> 16) & 0xc;   /* 0, 4, 8, 12.  */
  174.     }
  175.   return winsize;
  176. }


  177. /* REGISTER INFORMATION */

  178. /* Find register by name.  */
  179. static int
  180. xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
  181. {
  182.   int i;

  183.   for (i = 0; i < gdbarch_num_regs (gdbarch)
  184.          + gdbarch_num_pseudo_regs (gdbarch);
  185.        i++)

  186.     if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
  187.       return i;

  188.   return -1;
  189. }

  190. /* Returns the name of a register.  */
  191. static const char *
  192. xtensa_register_name (struct gdbarch *gdbarch, int regnum)
  193. {
  194.   /* Return the name stored in the register map.  */
  195.   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
  196.                               + gdbarch_num_pseudo_regs (gdbarch))
  197.     return gdbarch_tdep (gdbarch)->regmap[regnum].name;

  198.   internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
  199.   return 0;
  200. }

  201. /* Return the type of a register.  Create a new type, if necessary.  */

  202. static struct type *
  203. xtensa_register_type (struct gdbarch *gdbarch, int regnum)
  204. {
  205.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  206.   /* Return signed integer for ARx and Ax registers.  */
  207.   if ((regnum >= tdep->ar_base
  208.        && regnum < tdep->ar_base + tdep->num_aregs)
  209.       || (regnum >= tdep->a0_base
  210.           && regnum < tdep->a0_base + 16))
  211.     return builtin_type (gdbarch)->builtin_int;

  212.   if (regnum == gdbarch_pc_regnum (gdbarch)
  213.       || regnum == tdep->a0_base + 1)
  214.     return builtin_type (gdbarch)->builtin_data_ptr;

  215.   /* Return the stored type for all other registers.  */
  216.   else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
  217.                                    + gdbarch_num_pseudo_regs (gdbarch))
  218.     {
  219.       xtensa_register_t* reg = &tdep->regmap[regnum];

  220.       /* Set ctype for this register (only the first time).  */

  221.       if (reg->ctype == 0)
  222.         {
  223.           struct ctype_cache *tp;
  224.           int size = reg->byte_size;

  225.           /* We always use the memory representation,
  226.              even if the register width is smaller.  */
  227.           switch (size)
  228.             {
  229.             case 1:
  230.               reg->ctype = builtin_type (gdbarch)->builtin_uint8;
  231.               break;

  232.             case 2:
  233.               reg->ctype = builtin_type (gdbarch)->builtin_uint16;
  234.               break;

  235.             case 4:
  236.               reg->ctype = builtin_type (gdbarch)->builtin_uint32;
  237.               break;

  238.             case 8:
  239.               reg->ctype = builtin_type (gdbarch)->builtin_uint64;
  240.               break;

  241.             case 16:
  242.               reg->ctype = builtin_type (gdbarch)->builtin_uint128;
  243.               break;

  244.             default:
  245.               for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
  246.                 if (tp->size == size)
  247.                   break;

  248.               if (tp == NULL)
  249.                 {
  250.                   char *name = xstrprintf ("int%d", size * 8);
  251.                   tp = xmalloc (sizeof (struct ctype_cache));
  252.                   tp->next = tdep->type_entries;
  253.                   tdep->type_entries = tp;
  254.                   tp->size = size;
  255.                   tp->virtual_type
  256.                     = arch_integer_type (gdbarch, size * 8, 1, name);
  257.                   xfree (name);
  258.                 }

  259.               reg->ctype = tp->virtual_type;
  260.             }
  261.         }
  262.       return reg->ctype;
  263.     }

  264.   internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
  265.   return 0;
  266. }


  267. /* Return the 'local' register number for stubs, dwarf2, etc.
  268.    The debugging information enumerates registers starting from 0 for A0
  269.    to n for An.  So, we only have to add the base number for A0.  */

  270. static int
  271. xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
  272. {
  273.   int i;

  274.   if (regnum >= 0 && regnum < 16)
  275.     return gdbarch_tdep (gdbarch)->a0_base + regnum;

  276.   for (i = 0;
  277.        i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
  278.        i++)
  279.     if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
  280.       return i;

  281.   internal_error (__FILE__, __LINE__,
  282.                   _("invalid dwarf/stabs register number %d"), regnum);
  283.   return 0;
  284. }


  285. /* Write the bits of a masked register to the various registers.
  286.    Only the masked areas of these registers are modified; the other
  287.    fields are untouched.  The size of masked registers is always less
  288.    than or equal to 32 bits.  */

  289. static void
  290. xtensa_register_write_masked (struct regcache *regcache,
  291.                               xtensa_register_t *reg, const gdb_byte *buffer)
  292. {
  293.   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
  294.   const xtensa_mask_t *mask = reg->mask;

  295.   int shift = 0;                /* Shift for next mask (mod 32).  */
  296.   int start, size;                /* Start bit and size of current mask.  */

  297.   unsigned int *ptr = value;
  298.   unsigned int regval, m, mem = 0;

  299.   int bytesize = reg->byte_size;
  300.   int bitsize = bytesize * 8;
  301.   int i, r;

  302.   DEBUGTRACE ("xtensa_register_write_masked ()\n");

  303.   /* Copy the masked register to host byte-order.  */
  304.   if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
  305.     for (i = 0; i < bytesize; i++)
  306.       {
  307.         mem >>= 8;
  308.         mem |= (buffer[bytesize - i - 1] << 24);
  309.         if ((i & 3) == 3)
  310.           *ptr++ = mem;
  311.       }
  312.   else
  313.     for (i = 0; i < bytesize; i++)
  314.       {
  315.         mem >>= 8;
  316.         mem |= (buffer[i] << 24);
  317.         if ((i & 3) == 3)
  318.           *ptr++ = mem;
  319.       }

  320.   /* We might have to shift the final value:
  321.      bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
  322.      bytesize & 3 == x -> shift (4-x) * 8.  */

  323.   *ptr = mem >> (((0 - bytesize) & 3) * 8);
  324.   ptr = value;
  325.   mem = *ptr;

  326.   /* Write the bits to the masked areas of the other registers.  */
  327.   for (i = 0; i < mask->count; i++)
  328.     {
  329.       start = mask->mask[i].bit_start;
  330.       size = mask->mask[i].bit_size;
  331.       regval = mem >> shift;

  332.       if ((shift += size) > bitsize)
  333.         error (_("size of all masks is larger than the register"));

  334.       if (shift >= 32)
  335.         {
  336.           mem = *(++ptr);
  337.           shift -= 32;
  338.           bitsize -= 32;

  339.           if (shift > 0)
  340.             regval |= mem << (size - shift);
  341.         }

  342.       /* Make sure we have a valid register.  */
  343.       r = mask->mask[i].reg_num;
  344.       if (r >= 0 && size > 0)
  345.         {
  346.           /* Don't overwrite the unmasked areas.  */
  347.           ULONGEST old_val;
  348.           regcache_cooked_read_unsigned (regcache, r, &old_val);
  349.           m = 0xffffffff >> (32 - size) << start;
  350.           regval <<= start;
  351.           regval = (regval & m) | (old_val & ~m);
  352.           regcache_cooked_write_unsigned (regcache, r, regval);
  353.         }
  354.     }
  355. }


  356. /* Read a tie state or mapped registers.  Read the masked areas
  357.    of the registers and assemble them into a single value.  */

  358. static enum register_status
  359. xtensa_register_read_masked (struct regcache *regcache,
  360.                              xtensa_register_t *reg, gdb_byte *buffer)
  361. {
  362.   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
  363.   const xtensa_mask_t *mask = reg->mask;

  364.   int shift = 0;
  365.   int start, size;

  366.   unsigned int *ptr = value;
  367.   unsigned int regval, mem = 0;

  368.   int bytesize = reg->byte_size;
  369.   int bitsize = bytesize * 8;
  370.   int i;

  371.   DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
  372.               reg->name == 0 ? "" : reg->name);

  373.   /* Assemble the register from the masked areas of other registers.  */
  374.   for (i = 0; i < mask->count; i++)
  375.     {
  376.       int r = mask->mask[i].reg_num;
  377.       if (r >= 0)
  378.         {
  379.           enum register_status status;
  380.           ULONGEST val;

  381.           status = regcache_cooked_read_unsigned (regcache, r, &val);
  382.           if (status != REG_VALID)
  383.             return status;
  384.           regval = (unsigned int) val;
  385.         }
  386.       else
  387.         regval = 0;

  388.       start = mask->mask[i].bit_start;
  389.       size = mask->mask[i].bit_size;

  390.       regval >>= start;

  391.       if (size < 32)
  392.         regval &= (0xffffffff >> (32 - size));

  393.       mem |= regval << shift;

  394.       if ((shift += size) > bitsize)
  395.         error (_("size of all masks is larger than the register"));

  396.       if (shift >= 32)
  397.         {
  398.           *ptr++ = mem;
  399.           bitsize -= 32;
  400.           shift -= 32;

  401.           if (shift == 0)
  402.             mem = 0;
  403.           else
  404.             mem = regval >> (size - shift);
  405.         }
  406.     }

  407.   if (shift > 0)
  408.     *ptr = mem;

  409.   /* Copy value to target byte order.  */
  410.   ptr = value;
  411.   mem = *ptr;

  412.   if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
  413.     for (i = 0; i < bytesize; i++)
  414.       {
  415.         if ((i & 3) == 0)
  416.           mem = *ptr++;
  417.         buffer[bytesize - i - 1] = mem & 0xff;
  418.         mem >>= 8;
  419.       }
  420.   else
  421.     for (i = 0; i < bytesize; i++)
  422.       {
  423.         if ((i & 3) == 0)
  424.           mem = *ptr++;
  425.         buffer[i] = mem & 0xff;
  426.         mem >>= 8;
  427.       }

  428.   return REG_VALID;
  429. }


  430. /* Read pseudo registers.  */

  431. static enum register_status
  432. xtensa_pseudo_register_read (struct gdbarch *gdbarch,
  433.                              struct regcache *regcache,
  434.                              int regnum,
  435.                              gdb_byte *buffer)
  436. {
  437.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  438.   DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
  439.               regnum, xtensa_register_name (gdbarch, regnum));

  440.   if (regnum == gdbarch_num_regs (gdbarch)
  441.                 + gdbarch_num_pseudo_regs (gdbarch) - 1)
  442.      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;

  443.   /* Read aliases a0..a15, if this is a Windowed ABI.  */
  444.   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
  445.       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
  446.       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
  447.     {
  448.       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
  449.       enum register_status status;

  450.       status = regcache_raw_read (regcache,
  451.                                   gdbarch_tdep (gdbarch)->wb_regnum,
  452.                                   buf);
  453.       if (status != REG_VALID)
  454.         return status;
  455.       regnum = arreg_number (gdbarch, regnum,
  456.                              extract_unsigned_integer (buf, 4, byte_order));
  457.     }

  458.   /* We can always read non-pseudo registers.  */
  459.   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  460.     return regcache_raw_read (regcache, regnum, buffer);

  461.   /* We have to find out how to deal with priveleged registers.
  462.      Let's treat them as pseudo-registers, but we cannot read/write them.  */

  463.   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
  464.     {
  465.       buffer[0] = (gdb_byte)0;
  466.       buffer[1] = (gdb_byte)0;
  467.       buffer[2] = (gdb_byte)0;
  468.       buffer[3] = (gdb_byte)0;
  469.       return REG_VALID;
  470.     }
  471.   /* Pseudo registers.  */
  472.   else if (regnum >= 0
  473.             && regnum < gdbarch_num_regs (gdbarch)
  474.                         + gdbarch_num_pseudo_regs (gdbarch))
  475.     {
  476.       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
  477.       xtensa_register_type_t type = reg->type;
  478.       int flags = gdbarch_tdep (gdbarch)->target_flags;

  479.       /* We cannot read Unknown or Unmapped registers.  */
  480.       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  481.         {
  482.           if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  483.             {
  484.               warning (_("cannot read register %s"),
  485.                        xtensa_register_name (gdbarch, regnum));
  486.               return REG_VALID;
  487.             }
  488.         }

  489.       /* Some targets cannot read TIE register files.  */
  490.       else if (type == xtRegisterTypeTieRegfile)
  491.         {
  492.           /* Use 'fetch' to get register?  */
  493.           if (flags & xtTargetFlagsUseFetchStore)
  494.             {
  495.               warning (_("cannot read register"));
  496.               return REG_VALID;
  497.             }

  498.           /* On some targets (esp. simulators), we can always read the reg.  */
  499.           else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  500.             {
  501.               warning (_("cannot read register"));
  502.               return REG_VALID;
  503.             }
  504.         }

  505.       /* We can always read mapped registers.  */
  506.       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
  507.         return xtensa_register_read_masked (regcache, reg, buffer);

  508.       /* Assume that we can read the register.  */
  509.       return regcache_raw_read (regcache, regnum, buffer);
  510.     }
  511.   else
  512.     internal_error (__FILE__, __LINE__,
  513.                     _("invalid register number %d"), regnum);
  514. }


  515. /* Write pseudo registers.  */

  516. static void
  517. xtensa_pseudo_register_write (struct gdbarch *gdbarch,
  518.                               struct regcache *regcache,
  519.                               int regnum,
  520.                               const gdb_byte *buffer)
  521. {
  522.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  523.   DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
  524.               regnum, xtensa_register_name (gdbarch, regnum));

  525.   if (regnum == gdbarch_num_regs (gdbarch)
  526.                 + gdbarch_num_pseudo_regs (gdbarch) -1)
  527.      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;

  528.   /* Renumber register, if aliase a0..a15 on Windowed ABI.  */
  529.   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
  530.       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
  531.       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
  532.     {
  533.       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);

  534.       regcache_raw_read (regcache,
  535.                          gdbarch_tdep (gdbarch)->wb_regnum, buf);
  536.       regnum = arreg_number (gdbarch, regnum,
  537.                              extract_unsigned_integer (buf, 4, byte_order));
  538.     }

  539.   /* We can always write 'core' registers.
  540.      Note: We might have converted Ax->ARy.  */
  541.   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  542.     regcache_raw_write (regcache, regnum, buffer);

  543.   /* We have to find out how to deal with priveleged registers.
  544.      Let's treat them as pseudo-registers, but we cannot read/write them.  */

  545.   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
  546.     {
  547.       return;
  548.     }
  549.   /* Pseudo registers.  */
  550.   else if (regnum >= 0
  551.            && regnum < gdbarch_num_regs (gdbarch)
  552.                        + gdbarch_num_pseudo_regs (gdbarch))
  553.     {
  554.       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
  555.       xtensa_register_type_t type = reg->type;
  556.       int flags = gdbarch_tdep (gdbarch)->target_flags;

  557.       /* On most targets, we cannot write registers
  558.          of type "Unknown" or "Unmapped".  */
  559.       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  560.         {
  561.           if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  562.             {
  563.               warning (_("cannot write register %s"),
  564.                        xtensa_register_name (gdbarch, regnum));
  565.               return;
  566.             }
  567.         }

  568.       /* Some targets cannot read TIE register files.  */
  569.       else if (type == xtRegisterTypeTieRegfile)
  570.         {
  571.           /* Use 'store' to get register?  */
  572.           if (flags & xtTargetFlagsUseFetchStore)
  573.             {
  574.               warning (_("cannot write register"));
  575.               return;
  576.             }

  577.           /* On some targets (esp. simulators), we can always write
  578.              the register.  */
  579.           else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
  580.             {
  581.               warning (_("cannot write register"));
  582.               return;
  583.             }
  584.         }

  585.       /* We can always write mapped registers.  */
  586.       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
  587.         {
  588.           xtensa_register_write_masked (regcache, reg, buffer);
  589.           return;
  590.         }

  591.       /* Assume that we can write the register.  */
  592.       regcache_raw_write (regcache, regnum, buffer);
  593.     }
  594.   else
  595.     internal_error (__FILE__, __LINE__,
  596.                     _("invalid register number %d"), regnum);
  597. }

  598. static struct reggroup *xtensa_ar_reggroup;
  599. static struct reggroup *xtensa_user_reggroup;
  600. static struct reggroup *xtensa_vectra_reggroup;
  601. static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];

  602. static void
  603. xtensa_init_reggroups (void)
  604. {
  605.   int i;
  606.   char cpname[] = "cp0";

  607.   xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
  608.   xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
  609.   xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);

  610.   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  611.     {
  612.       cpname[2] = '0' + i;
  613.       xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
  614.     }
  615. }

  616. static void
  617. xtensa_add_reggroups (struct gdbarch *gdbarch)
  618. {
  619.   int i;

  620.   /* Predefined groups.  */
  621.   reggroup_add (gdbarch, all_reggroup);
  622.   reggroup_add (gdbarch, save_reggroup);
  623.   reggroup_add (gdbarch, restore_reggroup);
  624.   reggroup_add (gdbarch, system_reggroup);
  625.   reggroup_add (gdbarch, vector_reggroup);
  626.   reggroup_add (gdbarch, general_reggroup);
  627.   reggroup_add (gdbarch, float_reggroup);

  628.   /* Xtensa-specific groups.  */
  629.   reggroup_add (gdbarch, xtensa_ar_reggroup);
  630.   reggroup_add (gdbarch, xtensa_user_reggroup);
  631.   reggroup_add (gdbarch, xtensa_vectra_reggroup);

  632.   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  633.     reggroup_add (gdbarch, xtensa_cp[i]);
  634. }

  635. static int
  636. xtensa_coprocessor_register_group (struct reggroup *group)
  637. {
  638.   int i;

  639.   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
  640.     if (group == xtensa_cp[i])
  641.       return i;

  642.   return -1;
  643. }

  644. #define SAVE_REST_FLAGS        (XTENSA_REGISTER_FLAGS_READABLE \
  645.                         | XTENSA_REGISTER_FLAGS_WRITABLE \
  646.                         | XTENSA_REGISTER_FLAGS_VOLATILE)

  647. #define SAVE_REST_VALID        (XTENSA_REGISTER_FLAGS_READABLE \
  648.                         | XTENSA_REGISTER_FLAGS_WRITABLE)

  649. static int
  650. xtensa_register_reggroup_p (struct gdbarch *gdbarch,
  651.                             int regnum,
  652.                                 struct reggroup *group)
  653. {
  654.   xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
  655.   xtensa_register_type_t type = reg->type;
  656.   xtensa_register_group_t rg = reg->group;
  657.   int cp_number;

  658.   if (group == save_reggroup)
  659.     /* Every single register should be included into the list of registers
  660.        to be watched for changes while using -data-list-changed-registers.  */
  661.     return 1;

  662.   /* First, skip registers that are not visible to this target
  663.      (unknown and unmapped registers when not using ISS).  */

  664.   if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
  665.     return 0;
  666.   if (group == all_reggroup)
  667.     return 1;
  668.   if (group == xtensa_ar_reggroup)
  669.     return rg & xtRegisterGroupAddrReg;
  670.   if (group == xtensa_user_reggroup)
  671.     return rg & xtRegisterGroupUser;
  672.   if (group == float_reggroup)
  673.     return rg & xtRegisterGroupFloat;
  674.   if (group == general_reggroup)
  675.     return rg & xtRegisterGroupGeneral;
  676.   if (group == system_reggroup)
  677.     return rg & xtRegisterGroupState;
  678.   if (group == vector_reggroup || group == xtensa_vectra_reggroup)
  679.     return rg & xtRegisterGroupVectra;
  680.   if (group == restore_reggroup)
  681.     return (regnum < gdbarch_num_regs (gdbarch)
  682.             && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
  683.   cp_number = xtensa_coprocessor_register_group (group);
  684.   if (cp_number >= 0)
  685.     return rg & (xtRegisterGroupCP0 << cp_number);
  686.   else
  687.     return 1;
  688. }


  689. /* Supply register REGNUM from the buffer specified by GREGS and LEN
  690.    in the general-purpose register set REGSET to register cache
  691.    REGCACHE.  If REGNUM is -1 do this for all registers in REGSET.  */

  692. static void
  693. xtensa_supply_gregset (const struct regset *regset,
  694.                        struct regcache *rc,
  695.                        int regnum,
  696.                        const void *gregs,
  697.                        size_t len)
  698. {
  699.   const xtensa_elf_gregset_t *regs = gregs;
  700.   struct gdbarch *gdbarch = get_regcache_arch (rc);
  701.   int i;

  702.   DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);

  703.   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
  704.     regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
  705.   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
  706.     regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
  707.   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
  708.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
  709.                          (char *) &regs->windowbase);
  710.   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
  711.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
  712.                          (char *) &regs->windowstart);
  713.   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
  714.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
  715.                          (char *) &regs->lbeg);
  716.   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
  717.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
  718.                          (char *) &regs->lend);
  719.   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
  720.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
  721.                          (char *) &regs->lcount);
  722.   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
  723.     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
  724.                          (char *) &regs->sar);
  725.   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
  726.       && regnum < gdbarch_tdep (gdbarch)->ar_base
  727.                     + gdbarch_tdep (gdbarch)->num_aregs)
  728.     regcache_raw_supply (rc, regnum,
  729.                          (char *) &regs->ar[regnum - gdbarch_tdep
  730.                            (gdbarch)->ar_base]);
  731.   else if (regnum == -1)
  732.     {
  733.       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
  734.         regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
  735.                              (char *) &regs->ar[i]);
  736.     }
  737. }


  738. /* Xtensa register set.  */

  739. static struct regset
  740. xtensa_gregset =
  741. {
  742.   NULL,
  743.   xtensa_supply_gregset
  744. };


  745. /* Iterate over supported core file register note sections. */

  746. static void
  747. xtensa_iterate_over_regset_sections (struct gdbarch *gdbarch,
  748.                                      iterate_over_regset_sections_cb *cb,
  749.                                      void *cb_data,
  750.                                      const struct regcache *regcache)
  751. {
  752.   DEBUGTRACE ("xtensa_iterate_over_regset_sections\n");

  753.   cb (".reg", sizeof (xtensa_elf_gregset_t), &xtensa_gregset,
  754.       NULL, cb_data);
  755. }


  756. /* Handling frames.  */

  757. /* Number of registers to save in case of Windowed ABI.  */
  758. #define XTENSA_NUM_SAVED_AREGS                12

  759. /* Frame cache part for Windowed ABI.  */
  760. typedef struct xtensa_windowed_frame_cache
  761. {
  762.   int wb;                /* WINDOWBASE of the previous frame.  */
  763.   int callsize;                /* Call size of this frame.  */
  764.   int ws;                /* WINDOWSTART of the previous frame.  It keeps track of
  765.                            life windows only.  If there is no bit set for the
  766.                            window,  that means it had been already spilled
  767.                            because of window overflow.  */

  768.    /* Addresses of spilled A-registers.
  769.       AREGS[i] == -1, if corresponding AR is alive.  */
  770.   CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
  771. } xtensa_windowed_frame_cache_t;

  772. /* Call0 ABI Definitions.  */

  773. #define C0_MAXOPDS  3        /* Maximum number of operands for prologue
  774.                            analysis.  */
  775. #define C0_NREGS   16        /* Number of A-registers to track.  */
  776. #define C0_CLESV   12        /* Callee-saved registers are here and up.  */
  777. #define C0_SP            1        /* Register used as SP.  */
  778. #define C0_FP           15        /* Register used as FP.  */
  779. #define C0_RA            0        /* Register used as return address.  */
  780. #define C0_ARGS            2        /* Register used as first arg/retval.  */
  781. #define C0_NARGS    6        /* Number of A-regs for args/retvals.  */

  782. /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
  783.    A-register where the current content of the reg came from (in terms
  784.    of an original reg and a constant).  Negative values of c0_rt[n].fp_reg
  785.    mean that the orignal content of the register was saved to the stack.
  786.    c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
  787.    know where SP will end up until the entire prologue has been analyzed.  */

  788. #define C0_CONST   -1        /* fr_reg value if register contains a constant.  */
  789. #define C0_INEXP   -2        /* fr_reg value if inexpressible as reg + offset.  */
  790. #define C0_NOSTK   -1        /* to_stk value if register has not been stored.  */

  791. extern xtensa_isa xtensa_default_isa;

  792. typedef struct xtensa_c0reg
  793. {
  794.   int fr_reg;  /* original register from which register content
  795.                   is derived, or C0_CONST, or C0_INEXP.  */
  796.   int fr_ofs;  /* constant offset from reg, or immediate value.  */
  797.   int to_stk;  /* offset from original SP to register (4-byte aligned),
  798.                   or C0_NOSTK if register has not been saved.  */
  799. } xtensa_c0reg_t;

  800. /* Frame cache part for Call0 ABI.  */
  801. typedef struct xtensa_call0_frame_cache
  802. {
  803.   int c0_frmsz;                           /* Stack frame size.  */
  804.   int c0_hasfp;                           /* Current frame uses frame pointer.  */
  805.   int fp_regnum;                   /* A-register used as FP.  */
  806.   int c0_fp;                           /* Actual value of frame pointer.  */
  807.   int c0_fpalign;                   /* Dinamic adjustment for the stack
  808.                                       pointer. It's an AND mask. Zero,
  809.                                       if alignment was not adjusted.  */
  810.   int c0_old_sp;                   /* In case of dynamic adjustment, it is
  811.                                       a register holding unaligned sp.
  812.                                       C0_INEXP, when undefined.  */
  813.   int c0_sp_ofs;                   /* If "c0_old_sp" was spilled it's a
  814.                                       stack offset. C0_NOSTK otherwise.  */

  815.   xtensa_c0reg_t c0_rt[C0_NREGS];  /* Register tracking information.  */
  816. } xtensa_call0_frame_cache_t;

  817. typedef struct xtensa_frame_cache
  818. {
  819.   CORE_ADDR base;        /* Stack pointer of this frame.  */
  820.   CORE_ADDR pc;                /* PC of this frame at the function entry point.  */
  821.   CORE_ADDR ra;                /* The raw return address of this frame.  */
  822.   CORE_ADDR ps;                /* The PS register of the previous (older) frame.  */
  823.   CORE_ADDR prev_sp;        /* Stack Pointer of the previous (older) frame.  */
  824.   int call0;                /* It's a call0 framework (else windowed).  */
  825.   union
  826.     {
  827.       xtensa_windowed_frame_cache_t        wd;        /* call0 == false.  */
  828.       xtensa_call0_frame_cache_t               c0;        /* call0 == true.  */
  829.     };
  830. } xtensa_frame_cache_t;


  831. static struct xtensa_frame_cache *
  832. xtensa_alloc_frame_cache (int windowed)
  833. {
  834.   xtensa_frame_cache_t *cache;
  835.   int i;

  836.   DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");

  837.   cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);

  838.   cache->base = 0;
  839.   cache->pc = 0;
  840.   cache->ra = 0;
  841.   cache->ps = 0;
  842.   cache->prev_sp = 0;
  843.   cache->call0 = !windowed;
  844.   if (cache->call0)
  845.     {
  846.       cache->c0.c0_frmsz  = -1;
  847.       cache->c0.c0_hasfp  =  0;
  848.       cache->c0.fp_regnum = -1;
  849.       cache->c0.c0_fp     = -1;
  850.       cache->c0.c0_fpalign =  0;
  851.       cache->c0.c0_old_sp  =  C0_INEXP;
  852.       cache->c0.c0_sp_ofs  =  C0_NOSTK;

  853.       for (i = 0; i < C0_NREGS; i++)
  854.         {
  855.           cache->c0.c0_rt[i].fr_reg = i;
  856.           cache->c0.c0_rt[i].fr_ofs = 0;
  857.           cache->c0.c0_rt[i].to_stk = C0_NOSTK;
  858.         }
  859.     }
  860.   else
  861.     {
  862.       cache->wd.wb = 0;
  863.       cache->wd.ws = 0;
  864.       cache->wd.callsize = -1;

  865.       for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
  866.         cache->wd.aregs[i] = -1;
  867.     }
  868.   return cache;
  869. }


  870. static CORE_ADDR
  871. xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  872. {
  873.   return address & ~15;
  874. }


  875. static CORE_ADDR
  876. xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  877. {
  878.   gdb_byte buf[8];
  879.   CORE_ADDR pc;

  880.   DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n",
  881.                 host_address_to_string (next_frame));

  882.   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
  883.   pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);

  884.   DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);

  885.   return pc;
  886. }


  887. static struct frame_id
  888. xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  889. {
  890.   CORE_ADDR pc, fp;

  891.   /* THIS-FRAME is a dummy frame.  Return a frame ID of that frame.  */

  892.   pc = get_frame_pc (this_frame);
  893.   fp = get_frame_register_unsigned
  894.          (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);

  895.   /* Make dummy frame ID unique by adding a constant.  */
  896.   return frame_id_build (fp + SP_ALIGNMENT, pc);
  897. }

  898. /* Returns true,  if instruction to execute next is unique to Xtensa Window
  899.    Interrupt Handlers.  It can only be one of L32E,  S32E,  RFWO,  or RFWU.  */

  900. static int
  901. xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
  902. {
  903.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  904.   unsigned int insn = read_memory_integer (pc, 4, byte_order);
  905.   unsigned int code;

  906.   if (byte_order == BFD_ENDIAN_BIG)
  907.     {
  908.       /* Check, if this is L32E or S32E.  */
  909.       code = insn & 0xf000ff00;
  910.       if ((code == 0x00009000) || (code == 0x00009400))
  911.         return 1;
  912.       /* Check, if this is RFWU or RFWO.  */
  913.       code = insn & 0xffffff00;
  914.       return ((code == 0x00430000) || (code == 0x00530000));
  915.     }
  916.   else
  917.     {
  918.       /* Check, if this is L32E or S32E.  */
  919.       code = insn & 0x00ff000f;
  920.       if ((code == 0x090000) || (code == 0x490000))
  921.         return 1;
  922.       /* Check, if this is RFWU or RFWO.  */
  923.       code = insn & 0x00ffffff;
  924.       return ((code == 0x00003400) || (code == 0x00003500));
  925.     }
  926. }

  927. /* Returns the best guess about which register is a frame pointer
  928.    for the function containing CURRENT_PC.  */

  929. #define XTENSA_ISA_BSZ                32                /* Instruction buffer size.  */
  930. #define XTENSA_ISA_BADPC        ((CORE_ADDR)0)        /* Bad PC value.  */

  931. static unsigned int
  932. xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
  933. {
  934. #define RETURN_FP goto done

  935.   unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
  936.   CORE_ADDR start_addr;
  937.   xtensa_isa isa;
  938.   xtensa_insnbuf ins, slot;
  939.   gdb_byte ibuf[XTENSA_ISA_BSZ];
  940.   CORE_ADDR ia, bt, ba;
  941.   xtensa_format ifmt;
  942.   int ilen, islots, is;
  943.   xtensa_opcode opc;
  944.   const char *opcname;

  945.   find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
  946.   if (start_addr == 0)
  947.     return fp_regnum;

  948.   if (!xtensa_default_isa)
  949.     xtensa_default_isa = xtensa_isa_init (0, 0);
  950.   isa = xtensa_default_isa;
  951.   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  952.   ins = xtensa_insnbuf_alloc (isa);
  953.   slot = xtensa_insnbuf_alloc (isa);
  954.   ba = 0;

  955.   for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
  956.     {
  957.       if (ia + xtensa_isa_maxlength (isa) > bt)
  958.         {
  959.           ba = ia;
  960.           bt = (ba + XTENSA_ISA_BSZ) < current_pc
  961.             ? ba + XTENSA_ISA_BSZ : current_pc;
  962.           if (target_read_memory (ba, ibuf, bt - ba) != 0)
  963.             RETURN_FP;
  964.         }

  965.       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  966.       ifmt = xtensa_format_decode (isa, ins);
  967.       if (ifmt == XTENSA_UNDEFINED)
  968.         RETURN_FP;
  969.       ilen = xtensa_format_length (isa, ifmt);
  970.       if (ilen == XTENSA_UNDEFINED)
  971.         RETURN_FP;
  972.       islots = xtensa_format_num_slots (isa, ifmt);
  973.       if (islots == XTENSA_UNDEFINED)
  974.         RETURN_FP;

  975.       for (is = 0; is < islots; ++is)
  976.         {
  977.           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  978.             RETURN_FP;

  979.           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  980.           if (opc == XTENSA_UNDEFINED)
  981.             RETURN_FP;

  982.           opcname = xtensa_opcode_name (isa, opc);

  983.           if (strcasecmp (opcname, "mov.n") == 0
  984.               || strcasecmp (opcname, "or") == 0)
  985.             {
  986.               unsigned int register_operand;

  987.               /* Possible candidate for setting frame pointer
  988.                  from A1.  This is what we are looking for.  */

  989.               if (xtensa_operand_get_field (isa, opc, 1, ifmt,
  990.                                             is, slot, &register_operand) != 0)
  991.                 RETURN_FP;
  992.               if (xtensa_operand_decode (isa, opc, 1, &register_operand) != 0)
  993.                 RETURN_FP;
  994.               if (register_operand == 1/* Mov{.n} FP A1.  */
  995.                 {
  996.                   if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot,
  997.                                                 &register_operand) != 0)
  998.                     RETURN_FP;
  999.                   if (xtensa_operand_decode (isa, opc, 0,
  1000.                                              &register_operand) != 0)
  1001.                     RETURN_FP;

  1002.                   fp_regnum
  1003.                     = gdbarch_tdep (gdbarch)->a0_base + register_operand;
  1004.                   RETURN_FP;
  1005.                 }
  1006.             }

  1007.           if (
  1008.               /* We have problems decoding the memory.  */
  1009.               opcname == NULL
  1010.               || strcasecmp (opcname, "ill") == 0
  1011.               || strcasecmp (opcname, "ill.n") == 0
  1012.               /* Hit planted breakpoint.  */
  1013.               || strcasecmp (opcname, "break") == 0
  1014.               || strcasecmp (opcname, "break.n") == 0
  1015.               /* Flow control instructions finish prologue.  */
  1016.               || xtensa_opcode_is_branch (isa, opc) > 0
  1017.               || xtensa_opcode_is_jump   (isa, opc) > 0
  1018.               || xtensa_opcode_is_loop   (isa, opc) > 0
  1019.               || xtensa_opcode_is_call   (isa, opc) > 0
  1020.               || strcasecmp (opcname, "simcall") == 0
  1021.               || strcasecmp (opcname, "syscall") == 0)
  1022.             /* Can not continue analysis.  */
  1023.             RETURN_FP;
  1024.         }
  1025.     }
  1026. done:
  1027.   xtensa_insnbuf_free(isa, slot);
  1028.   xtensa_insnbuf_free(isa, ins);
  1029.   return fp_regnum;
  1030. }

  1031. /* The key values to identify the frame using "cache" are

  1032.         cache->base    = SP (or best guess about FP) of this frame;
  1033.         cache->pc      = entry-PC (entry point of the frame function);
  1034.         cache->prev_sp = SP of the previous frame.  */

  1035. static void
  1036. call0_frame_cache (struct frame_info *this_frame,
  1037.                    xtensa_frame_cache_t *cache, CORE_ADDR pc);

  1038. static void
  1039. xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
  1040.                                      xtensa_frame_cache_t *cache,
  1041.                                      CORE_ADDR pc);

  1042. static struct xtensa_frame_cache *
  1043. xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
  1044. {
  1045.   xtensa_frame_cache_t *cache;
  1046.   CORE_ADDR ra, wb, ws, pc, sp, ps;
  1047.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1048.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1049.   unsigned int fp_regnum;
  1050.   int  windowed, ps_regnum;

  1051.   if (*this_cache)
  1052.     return *this_cache;

  1053.   pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
  1054.   ps_regnum = gdbarch_ps_regnum (gdbarch);
  1055.   ps = (ps_regnum >= 0
  1056.         ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);

  1057.   windowed = windowing_enabled (gdbarch, ps);

  1058.   /* Get pristine xtensa-frame.  */
  1059.   cache = xtensa_alloc_frame_cache (windowed);
  1060.   *this_cache = cache;

  1061.   if (windowed)
  1062.     {
  1063.       char op1;

  1064.       /* Get WINDOWBASE, WINDOWSTART, and PS registers.  */
  1065.       wb = get_frame_register_unsigned (this_frame,
  1066.                                         gdbarch_tdep (gdbarch)->wb_regnum);
  1067.       ws = get_frame_register_unsigned (this_frame,
  1068.                                         gdbarch_tdep (gdbarch)->ws_regnum);

  1069.       op1 = read_memory_integer (pc, 1, byte_order);
  1070.       if (XTENSA_IS_ENTRY (gdbarch, op1))
  1071.         {
  1072.           int callinc = CALLINC (ps);
  1073.           ra = get_frame_register_unsigned
  1074.             (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);

  1075.           /* ENTRY hasn't been executed yet, therefore callsize is still 0.  */
  1076.           cache->wd.callsize = 0;
  1077.           cache->wd.wb = wb;
  1078.           cache->wd.ws = ws;
  1079.           cache->prev_sp = get_frame_register_unsigned
  1080.                              (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);

  1081.           /* This only can be the outermost frame since we are
  1082.              just about to execute ENTRY.  SP hasn't been set yet.
  1083.              We can assume any frame size, because it does not
  1084.              matter, and, let's fake frame base in cache.  */
  1085.           cache->base = cache->prev_sp - 16;

  1086.           cache->pc = pc;
  1087.           cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
  1088.           cache->ps = (ps & ~PS_CALLINC_MASK)
  1089.             | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);

  1090.           return cache;
  1091.         }
  1092.       else
  1093.         {
  1094.           fp_regnum = xtensa_scan_prologue (gdbarch, pc);
  1095.           ra = get_frame_register_unsigned (this_frame,
  1096.                                             gdbarch_tdep (gdbarch)->a0_base);
  1097.           cache->wd.callsize = WINSIZE (ra);
  1098.           cache->wd.wb = (wb - cache->wd.callsize / 4)
  1099.                           & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
  1100.           cache->wd.ws = ws & ~(1 << wb);

  1101.           cache->pc = get_frame_func (this_frame);
  1102.           cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
  1103.           cache->ps = (ps & ~PS_CALLINC_MASK)
  1104.             | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
  1105.         }

  1106.       if (cache->wd.ws == 0)
  1107.         {
  1108.           int i;

  1109.           /* Set A0...A3.  */
  1110.           sp = get_frame_register_unsigned
  1111.             (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;

  1112.           for (i = 0; i < 4; i++, sp += 4)
  1113.             {
  1114.               cache->wd.aregs[i] = sp;
  1115.             }

  1116.           if (cache->wd.callsize > 4)
  1117.             {
  1118.               /* Set A4...A7/A11.  */
  1119.               /* Get the SP of the frame previous to the previous one.
  1120.                  To achieve this, we have to dereference SP twice.  */
  1121.               sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
  1122.               sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
  1123.               sp -= cache->wd.callsize * 4;

  1124.               for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
  1125.                 {
  1126.                   cache->wd.aregs[i] = sp;
  1127.                 }
  1128.             }
  1129.         }

  1130.       if ((cache->prev_sp == 0) && ( ra != 0 ))
  1131.         /* If RA is equal to 0 this frame is an outermost frame.  Leave
  1132.            cache->prev_sp unchanged marking the boundary of the frame stack.  */
  1133.         {
  1134.           if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
  1135.             {
  1136.               /* Register window overflow already happened.
  1137.                  We can read caller's SP from the proper spill loction.  */
  1138.               sp = get_frame_register_unsigned
  1139.                 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
  1140.               cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
  1141.             }
  1142.           else
  1143.             {
  1144.               /* Read caller's frame SP directly from the previous window.  */
  1145.               int regnum = arreg_number
  1146.                              (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
  1147.                               cache->wd.wb);

  1148.               cache->prev_sp = xtensa_read_register (regnum);
  1149.             }
  1150.         }
  1151.     }
  1152.   else if (xtensa_window_interrupt_insn (gdbarch, pc))
  1153.     {
  1154.       /* Execution stopped inside Xtensa Window Interrupt Handler.  */

  1155.       xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
  1156.       /* Everything was set already,  including cache->base.  */
  1157.       return cache;
  1158.     }
  1159.   else        /* Call0 framework.  */
  1160.     {
  1161.       call0_frame_cache (this_frame, cache, pc);
  1162.       fp_regnum = cache->c0.fp_regnum;
  1163.     }

  1164.   cache->base = get_frame_register_unsigned (this_frame, fp_regnum);

  1165.   return cache;
  1166. }

  1167. static int xtensa_session_once_reported = 1;

  1168. /* Report a problem with prologue analysis while doing backtracing.
  1169.    But, do it only once to avoid annoyng repeated messages.  */

  1170. static void
  1171. warning_once (void)
  1172. {
  1173.   if (xtensa_session_once_reported == 0)
  1174.     warning (_("\
  1175. \nUnrecognised function prologue. Stack trace cannot be resolved. \
  1176. This message will not be repeated in this session.\n"));

  1177.   xtensa_session_once_reported = 1;
  1178. }


  1179. static void
  1180. xtensa_frame_this_id (struct frame_info *this_frame,
  1181.                       void **this_cache,
  1182.                       struct frame_id *this_id)
  1183. {
  1184.   struct xtensa_frame_cache *cache =
  1185.     xtensa_frame_cache (this_frame, this_cache);

  1186.   if (cache->prev_sp == 0)
  1187.     return;

  1188.   (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
  1189. }

  1190. static struct value *
  1191. xtensa_frame_prev_register (struct frame_info *this_frame,
  1192.                             void **this_cache,
  1193.                             int regnum)
  1194. {
  1195.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1196.   struct xtensa_frame_cache *cache;
  1197.   ULONGEST saved_reg = 0;
  1198.   int done = 1;

  1199.   if (*this_cache == NULL)
  1200.     *this_cache = xtensa_frame_cache (this_frame, this_cache);
  1201.   cache = *this_cache;

  1202.   if (regnum ==gdbarch_pc_regnum (gdbarch))
  1203.     saved_reg = cache->ra;
  1204.   else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
  1205.     saved_reg = cache->prev_sp;
  1206.   else if (!cache->call0)
  1207.     {
  1208.       if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
  1209.         saved_reg = cache->wd.ws;
  1210.       else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
  1211.         saved_reg = cache->wd.wb;
  1212.       else if (regnum == gdbarch_ps_regnum (gdbarch))
  1213.         saved_reg = cache->ps;
  1214.       else
  1215.         done = 0;
  1216.     }
  1217.   else
  1218.     done = 0;

  1219.   if (done)
  1220.     return frame_unwind_got_constant (this_frame, regnum, saved_reg);

  1221.   if (!cache->call0) /* Windowed ABI.  */
  1222.     {
  1223.       /* Convert A-register numbers to AR-register numbers,
  1224.          if we deal with A-register.  */
  1225.       if (regnum >= gdbarch_tdep (gdbarch)->a0_base
  1226.           && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
  1227.         regnum = arreg_number (gdbarch, regnum, cache->wd.wb);

  1228.       /* Check, if we deal with AR-register saved on stack.  */
  1229.       if (regnum >= gdbarch_tdep (gdbarch)->ar_base
  1230.           && regnum <= (gdbarch_tdep (gdbarch)->ar_base
  1231.                          + gdbarch_tdep (gdbarch)->num_aregs))
  1232.         {
  1233.           int areg = areg_number (gdbarch, regnum, cache->wd.wb);

  1234.           if (areg >= 0
  1235.               && areg < XTENSA_NUM_SAVED_AREGS
  1236.               && cache->wd.aregs[areg] != -1)
  1237.             return frame_unwind_got_memory (this_frame, regnum,
  1238.                                             cache->wd.aregs[areg]);
  1239.         }
  1240.     }
  1241.   else /* Call0 ABI.  */
  1242.     {
  1243.       int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
  1244.                 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
  1245.                                + C0_NREGS))
  1246.                   ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;

  1247.       if (reg < C0_NREGS)
  1248.         {
  1249.           CORE_ADDR spe;
  1250.           int stkofs;

  1251.           /* If register was saved in the prologue, retrieve it.  */
  1252.           stkofs = cache->c0.c0_rt[reg].to_stk;
  1253.           if (stkofs != C0_NOSTK)
  1254.             {
  1255.               /* Determine SP on entry based on FP.  */
  1256.               spe = cache->c0.c0_fp
  1257.                 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;

  1258.               return frame_unwind_got_memory (this_frame, regnum,
  1259.                                               spe + stkofs);
  1260.             }
  1261.         }
  1262.     }

  1263.   /* All other registers have been either saved to
  1264.      the stack or are still alive in the processor.  */

  1265.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1266. }


  1267. static const struct frame_unwind
  1268. xtensa_unwind =
  1269. {
  1270.   NORMAL_FRAME,
  1271.   default_frame_unwind_stop_reason,
  1272.   xtensa_frame_this_id,
  1273.   xtensa_frame_prev_register,
  1274.   NULL,
  1275.   default_frame_sniffer
  1276. };

  1277. static CORE_ADDR
  1278. xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1279. {
  1280.   struct xtensa_frame_cache *cache =
  1281.     xtensa_frame_cache (this_frame, this_cache);

  1282.   return cache->base;
  1283. }

  1284. static const struct frame_base
  1285. xtensa_frame_base =
  1286. {
  1287.   &xtensa_unwind,
  1288.   xtensa_frame_base_address,
  1289.   xtensa_frame_base_address,
  1290.   xtensa_frame_base_address
  1291. };


  1292. static void
  1293. xtensa_extract_return_value (struct type *type,
  1294.                              struct regcache *regcache,
  1295.                              void *dst)
  1296. {
  1297.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1298.   bfd_byte *valbuf = dst;
  1299.   int len = TYPE_LENGTH (type);
  1300.   ULONGEST pc, wb;
  1301.   int callsize, areg;
  1302.   int offset = 0;

  1303.   DEBUGTRACE ("xtensa_extract_return_value (...)\n");

  1304.   gdb_assert(len > 0);

  1305.   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
  1306.     {
  1307.       /* First, we have to find the caller window in the register file.  */
  1308.       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
  1309.       callsize = extract_call_winsize (gdbarch, pc);

  1310.       /* On Xtensa, we can return up to 4 words (or 2 for call12).  */
  1311.       if (len > (callsize > 8 ? 8 : 16))
  1312.         internal_error (__FILE__, __LINE__,
  1313.                         _("cannot extract return value of %d bytes long"),
  1314.                         len);

  1315.       /* Get the register offset of the return
  1316.          register (A2) in the caller window.  */
  1317.       regcache_raw_read_unsigned
  1318.         (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
  1319.       areg = arreg_number (gdbarch,
  1320.                           gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
  1321.     }
  1322.   else
  1323.     {
  1324.       /* No windowing hardware - Call0 ABI.  */
  1325.       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
  1326.     }

  1327.   DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);

  1328.   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1329.     offset = 4 - len;

  1330.   for (; len > 0; len -= 4, areg++, valbuf += 4)
  1331.     {
  1332.       if (len < 4)
  1333.         regcache_raw_read_part (regcache, areg, offset, len, valbuf);
  1334.       else
  1335.         regcache_raw_read (regcache, areg, valbuf);
  1336.     }
  1337. }


  1338. static void
  1339. xtensa_store_return_value (struct type *type,
  1340.                            struct regcache *regcache,
  1341.                            const void *dst)
  1342. {
  1343.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1344.   const bfd_byte *valbuf = dst;
  1345.   unsigned int areg;
  1346.   ULONGEST pc, wb;
  1347.   int callsize;
  1348.   int len = TYPE_LENGTH (type);
  1349.   int offset = 0;

  1350.   DEBUGTRACE ("xtensa_store_return_value (...)\n");

  1351.   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
  1352.     {
  1353.       regcache_raw_read_unsigned
  1354.         (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
  1355.       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
  1356.       callsize = extract_call_winsize (gdbarch, pc);

  1357.       if (len > (callsize > 8 ? 8 : 16))
  1358.         internal_error (__FILE__, __LINE__,
  1359.                         _("unimplemented for this length: %d"),
  1360.                         TYPE_LENGTH (type));
  1361.       areg = arreg_number (gdbarch,
  1362.                            gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);

  1363.       DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
  1364.               callsize, (int) wb);
  1365.     }
  1366.   else
  1367.     {
  1368.       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
  1369.     }

  1370.   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1371.     offset = 4 - len;

  1372.   for (; len > 0; len -= 4, areg++, valbuf += 4)
  1373.     {
  1374.       if (len < 4)
  1375.         regcache_raw_write_part (regcache, areg, offset, len, valbuf);
  1376.       else
  1377.         regcache_raw_write (regcache, areg, valbuf);
  1378.     }
  1379. }


  1380. static enum return_value_convention
  1381. xtensa_return_value (struct gdbarch *gdbarch,
  1382.                      struct value *function,
  1383.                      struct type *valtype,
  1384.                      struct regcache *regcache,
  1385.                      gdb_byte *readbuf,
  1386.                      const gdb_byte *writebuf)
  1387. {
  1388.   /* Structures up to 16 bytes are returned in registers.  */

  1389.   int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  1390.                         || TYPE_CODE (valtype) == TYPE_CODE_UNION
  1391.                         || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
  1392.                        && TYPE_LENGTH (valtype) > 16);

  1393.   if (struct_return)
  1394.     return RETURN_VALUE_STRUCT_CONVENTION;

  1395.   DEBUGTRACE ("xtensa_return_value(...)\n");

  1396.   if (writebuf != NULL)
  1397.     {
  1398.       xtensa_store_return_value (valtype, regcache, writebuf);
  1399.     }

  1400.   if (readbuf != NULL)
  1401.     {
  1402.       gdb_assert (!struct_return);
  1403.       xtensa_extract_return_value (valtype, regcache, readbuf);
  1404.     }
  1405.   return RETURN_VALUE_REGISTER_CONVENTION;
  1406. }


  1407. /* DUMMY FRAME */

  1408. static CORE_ADDR
  1409. xtensa_push_dummy_call (struct gdbarch *gdbarch,
  1410.                         struct value *function,
  1411.                         struct regcache *regcache,
  1412.                         CORE_ADDR bp_addr,
  1413.                         int nargs,
  1414.                         struct value **args,
  1415.                         CORE_ADDR sp,
  1416.                         int struct_return,
  1417.                         CORE_ADDR struct_addr)
  1418. {
  1419.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1420.   int i;
  1421.   int size, onstack_size;
  1422.   gdb_byte *buf = (gdb_byte *) alloca (16);
  1423.   CORE_ADDR ra, ps;
  1424.   struct argument_info
  1425.   {
  1426.     const bfd_byte *contents;
  1427.     int length;
  1428.     int onstack;                /* onstack == 0 => in reg */
  1429.     int align;                        /* alignment */
  1430.     union
  1431.     {
  1432.       int offset;                /* stack offset if on stack.  */
  1433.       int regno;                /* regno if in register.  */
  1434.     } u;
  1435.   };

  1436.   struct argument_info *arg_info =
  1437.     (struct argument_info *) alloca (nargs * sizeof (struct argument_info));

  1438.   CORE_ADDR osp = sp;

  1439.   DEBUGTRACE ("xtensa_push_dummy_call (...)\n");

  1440.   if (xtensa_debug_level > 3)
  1441.     {
  1442.       int i;
  1443.       DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
  1444.       DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
  1445.                  "struct_addr=0x%x\n",
  1446.                  (int) sp, (int) struct_return, (int) struct_addr);

  1447.       for (i = 0; i < nargs; i++)
  1448.         {
  1449.           struct value *arg = args[i];
  1450.           struct type *arg_type = check_typedef (value_type (arg));
  1451.           fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
  1452.                               host_address_to_string (arg),
  1453.                               TYPE_LENGTH (arg_type));
  1454.           switch (TYPE_CODE (arg_type))
  1455.             {
  1456.             case TYPE_CODE_INT:
  1457.               fprintf_unfiltered (gdb_stdlog, "int");
  1458.               break;
  1459.             case TYPE_CODE_STRUCT:
  1460.               fprintf_unfiltered (gdb_stdlog, "struct");
  1461.               break;
  1462.             default:
  1463.               fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
  1464.               break;
  1465.             }
  1466.           fprintf_unfiltered (gdb_stdlog, " %s\n",
  1467.                               host_address_to_string (value_contents (arg)));
  1468.         }
  1469.     }

  1470.   /* First loop: collect information.
  1471.      Cast into type_long.  (This shouldn't happen often for C because
  1472.      GDB already does this earlier.)  It's possible that GDB could
  1473.      do it all the time but it's harmless to leave this code here.  */

  1474.   size = 0;
  1475.   onstack_size = 0;
  1476.   i = 0;

  1477.   if (struct_return)
  1478.     size = REGISTER_SIZE;

  1479.   for (i = 0; i < nargs; i++)
  1480.     {
  1481.       struct argument_info *info = &arg_info[i];
  1482.       struct value *arg = args[i];
  1483.       struct type *arg_type = check_typedef (value_type (arg));

  1484.       switch (TYPE_CODE (arg_type))
  1485.         {
  1486.         case TYPE_CODE_INT:
  1487.         case TYPE_CODE_BOOL:
  1488.         case TYPE_CODE_CHAR:
  1489.         case TYPE_CODE_RANGE:
  1490.         case TYPE_CODE_ENUM:

  1491.           /* Cast argument to long if necessary as the mask does it too.  */
  1492.           if (TYPE_LENGTH (arg_type)
  1493.               < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
  1494.             {
  1495.               arg_type = builtin_type (gdbarch)->builtin_long;
  1496.               arg = value_cast (arg_type, arg);
  1497.             }
  1498.           /* Aligment is equal to the type length for the basic types.  */
  1499.           info->align = TYPE_LENGTH (arg_type);
  1500.           break;

  1501.         case TYPE_CODE_FLT:

  1502.           /* Align doubles correctly.  */
  1503.           if (TYPE_LENGTH (arg_type)
  1504.               == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
  1505.             info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
  1506.           else
  1507.             info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
  1508.           break;

  1509.         case TYPE_CODE_STRUCT:
  1510.         default:
  1511.           info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
  1512.           break;
  1513.         }
  1514.       info->length = TYPE_LENGTH (arg_type);
  1515.       info->contents = value_contents (arg);

  1516.       /* Align size and onstack_size.  */
  1517.       size = (size + info->align - 1) & ~(info->align - 1);
  1518.       onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);

  1519.       if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
  1520.         {
  1521.           info->onstack = 1;
  1522.           info->u.offset = onstack_size;
  1523.           onstack_size += info->length;
  1524.         }
  1525.       else
  1526.         {
  1527.           info->onstack = 0;
  1528.           info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
  1529.         }
  1530.       size += info->length;
  1531.     }

  1532.   /* Adjust the stack pointer and align it.  */
  1533.   sp = align_down (sp - onstack_size, SP_ALIGNMENT);

  1534.   /* Simulate MOVSP, if Windowed ABI.  */
  1535.   if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
  1536.       && (sp != osp))
  1537.     {
  1538.       read_memory (osp - 16, buf, 16);
  1539.       write_memory (sp - 16, buf, 16);
  1540.     }

  1541.   /* Second Loop: Load arguments.  */

  1542.   if (struct_return)
  1543.     {
  1544.       store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
  1545.       regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
  1546.     }

  1547.   for (i = 0; i < nargs; i++)
  1548.     {
  1549.       struct argument_info *info = &arg_info[i];

  1550.       if (info->onstack)
  1551.         {
  1552.           int n = info->length;
  1553.           CORE_ADDR offset = sp + info->u.offset;

  1554.           /* Odd-sized structs are aligned to the lower side of a memory
  1555.              word in big-endian mode and require a shift.  This only
  1556.              applies for structures smaller than one word.  */

  1557.           if (n < REGISTER_SIZE
  1558.               && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1559.             offset += (REGISTER_SIZE - n);

  1560.           write_memory (offset, info->contents, info->length);

  1561.         }
  1562.       else
  1563.         {
  1564.           int n = info->length;
  1565.           const bfd_byte *cp = info->contents;
  1566.           int r = info->u.regno;

  1567.           /* Odd-sized structs are aligned to the lower side of registers in
  1568.              big-endian mode and require a shift.  The odd-sized leftover will
  1569.              be at the end.  Note that this is only true for structures smaller
  1570.              than REGISTER_SIZE; for larger odd-sized structures the excess
  1571.              will be left-aligned in the register on both endiannesses.  */

  1572.           if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
  1573.             {
  1574.               ULONGEST v;
  1575.               v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
  1576.               v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);

  1577.               store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
  1578.               regcache_cooked_write (regcache, r, buf);

  1579.               cp += REGISTER_SIZE;
  1580.               n -= REGISTER_SIZE;
  1581.               r++;
  1582.             }
  1583.           else
  1584.             while (n > 0)
  1585.               {
  1586.                 regcache_cooked_write (regcache, r, cp);

  1587.                 cp += REGISTER_SIZE;
  1588.                 n -= REGISTER_SIZE;
  1589.                 r++;
  1590.               }
  1591.         }
  1592.     }

  1593.   /* Set the return address of dummy frame to the dummy address.
  1594.      The return address for the current function (in A0) is
  1595.      saved in the dummy frame, so we can savely overwrite A0 here.  */

  1596.   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
  1597.     {
  1598.       ULONGEST val;

  1599.       ra = (bp_addr & 0x3fffffff) | 0x40000000;
  1600.       regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
  1601.       ps = (unsigned long) val & ~0x00030000;
  1602.       regcache_cooked_write_unsigned
  1603.         (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
  1604.       regcache_cooked_write_unsigned (regcache,
  1605.                                       gdbarch_ps_regnum (gdbarch),
  1606.                                       ps | 0x00010000);

  1607.       /* All the registers have been saved.  After executing
  1608.          dummy call, they all will be restored.  So it's safe
  1609.          to modify WINDOWSTART register to make it look like there
  1610.          is only one register window corresponding to WINDOWEBASE.  */

  1611.       regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
  1612.       regcache_cooked_write_unsigned
  1613.         (regcache, gdbarch_tdep (gdbarch)->ws_regnum,
  1614.          1 << extract_unsigned_integer (buf, 4, byte_order));
  1615.     }
  1616.   else
  1617.     {
  1618.       /* Simulate CALL0: write RA into A0 register.  */
  1619.       regcache_cooked_write_unsigned
  1620.         (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
  1621.     }

  1622.   /* Set new stack pointer and return it.  */
  1623.   regcache_cooked_write_unsigned (regcache,
  1624.                                   gdbarch_tdep (gdbarch)->a0_base + 1, sp);
  1625.   /* Make dummy frame ID unique by adding a constant.  */
  1626.   return sp + SP_ALIGNMENT;
  1627. }


  1628. /* Return a breakpoint for the current location of PC.  We always use
  1629.    the density version if we have density instructions (regardless of the
  1630.    current instruction at PC), and use regular instructions otherwise.  */

  1631. #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
  1632. #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
  1633. #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
  1634. #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }

  1635. static const unsigned char *
  1636. xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  1637.                            int *lenptr)
  1638. {
  1639.   static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
  1640.   static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
  1641.   static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
  1642.   static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;

  1643.   DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);

  1644.   if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
  1645.     {
  1646.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1647.         {
  1648.           *lenptr = sizeof (density_big_breakpoint);
  1649.           return density_big_breakpoint;
  1650.         }
  1651.       else
  1652.         {
  1653.           *lenptr = sizeof (density_little_breakpoint);
  1654.           return density_little_breakpoint;
  1655.         }
  1656.     }
  1657.   else
  1658.     {
  1659.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1660.         {
  1661.           *lenptr = sizeof (big_breakpoint);
  1662.           return big_breakpoint;
  1663.         }
  1664.       else
  1665.         {
  1666.           *lenptr = sizeof (little_breakpoint);
  1667.           return little_breakpoint;
  1668.         }
  1669.     }
  1670. }

  1671. /* Call0 ABI support routines.  */

  1672. /* Return true, if PC points to "ret" or "ret.n".  */

  1673. static int
  1674. call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
  1675. {
  1676. #define RETURN_RET goto done
  1677.   xtensa_isa isa;
  1678.   xtensa_insnbuf ins, slot;
  1679.   gdb_byte ibuf[XTENSA_ISA_BSZ];
  1680.   CORE_ADDR ia, bt, ba;
  1681.   xtensa_format ifmt;
  1682.   int ilen, islots, is;
  1683.   xtensa_opcode opc;
  1684.   const char *opcname;
  1685.   int found_ret = 0;

  1686.   isa = xtensa_default_isa;
  1687.   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  1688.   ins = xtensa_insnbuf_alloc (isa);
  1689.   slot = xtensa_insnbuf_alloc (isa);
  1690.   ba = 0;

  1691.   for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
  1692.     {
  1693.       if (ia + xtensa_isa_maxlength (isa) > bt)
  1694.         {
  1695.           ba = ia;
  1696.           bt = (ba + XTENSA_ISA_BSZ) < finish_pc
  1697.             ? ba + XTENSA_ISA_BSZ : finish_pc;
  1698.           if (target_read_memory (ba, ibuf, bt - ba) != 0 )
  1699.             RETURN_RET;
  1700.         }

  1701.       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  1702.       ifmt = xtensa_format_decode (isa, ins);
  1703.       if (ifmt == XTENSA_UNDEFINED)
  1704.         RETURN_RET;
  1705.       ilen = xtensa_format_length (isa, ifmt);
  1706.       if (ilen == XTENSA_UNDEFINED)
  1707.         RETURN_RET;
  1708.       islots = xtensa_format_num_slots (isa, ifmt);
  1709.       if (islots == XTENSA_UNDEFINED)
  1710.         RETURN_RET;

  1711.       for (is = 0; is < islots; ++is)
  1712.         {
  1713.           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  1714.             RETURN_RET;

  1715.           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  1716.           if (opc == XTENSA_UNDEFINED)
  1717.             RETURN_RET;

  1718.           opcname = xtensa_opcode_name (isa, opc);

  1719.           if ((strcasecmp (opcname, "ret.n") == 0)
  1720.               || (strcasecmp (opcname, "ret") == 0))
  1721.             {
  1722.               found_ret = 1;
  1723.               RETURN_RET;
  1724.             }
  1725.         }
  1726.     }
  1727. done:
  1728.   xtensa_insnbuf_free(isa, slot);
  1729.   xtensa_insnbuf_free(isa, ins);
  1730.   return found_ret;
  1731. }

  1732. /* Call0 opcode class.  Opcodes are preclassified according to what they
  1733.    mean for Call0 prologue analysis, and their number of significant operands.
  1734.    The purpose of this is to simplify prologue analysis by separating
  1735.    instruction decoding (libisa) from the semantics of prologue analysis.  */

  1736. typedef enum
  1737. {
  1738.   c0opc_illegal,       /* Unknown to libisa (invalid) or 'ill' opcode.  */
  1739.   c0opc_uninteresting, /* Not interesting for Call0 prologue analysis.  */
  1740.   c0opc_flow,               /* Flow control insn.  */
  1741.   c0opc_entry,               /* ENTRY indicates non-Call0 prologue.  */
  1742.   c0opc_break,               /* Debugger software breakpoints.  */
  1743.   c0opc_add,               /* Adding two registers.  */
  1744.   c0opc_addi,               /* Adding a register and an immediate.  */
  1745.   c0opc_and,               /* Bitwise "and"-ing two registers.  */
  1746.   c0opc_sub,               /* Subtracting a register from a register.  */
  1747.   c0opc_mov,               /* Moving a register to a register.  */
  1748.   c0opc_movi,               /* Moving an immediate to a register.  */
  1749.   c0opc_l32r,               /* Loading a literal.  */
  1750.   c0opc_s32i,               /* Storing word at fixed offset from a base register.  */
  1751.   c0opc_rwxsr,               /* RSR, WRS, or XSR instructions.  */
  1752.   c0opc_l32e,          /* L32E instruction.  */
  1753.   c0opc_s32e,          /* S32E instruction.  */
  1754.   c0opc_rfwo,          /* RFWO instruction.  */
  1755.   c0opc_rfwu,          /* RFWU instruction.  */
  1756.   c0opc_NrOf               /* Number of opcode classifications.  */
  1757. } xtensa_insn_kind;

  1758. /* Return true,  if OPCNAME is RSR,  WRS,  or XSR instruction.  */

  1759. static int
  1760. rwx_special_register (const char *opcname)
  1761. {
  1762.   char ch = *opcname++;

  1763.   if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
  1764.     return 0;
  1765.   if (*opcname++ != 's')
  1766.     return 0;
  1767.   if (*opcname++ != 'r')
  1768.     return 0;
  1769.   if (*opcname++ != '.')
  1770.     return 0;

  1771.   return 1;
  1772. }

  1773. /* Classify an opcode based on what it means for Call0 prologue analysis.  */

  1774. static xtensa_insn_kind
  1775. call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
  1776. {
  1777.   const char *opcname;
  1778.   xtensa_insn_kind opclass = c0opc_uninteresting;

  1779.   DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);

  1780.   /* Get opcode name and handle special classifications.  */

  1781.   opcname = xtensa_opcode_name (isa, opc);

  1782.   if (opcname == NULL
  1783.       || strcasecmp (opcname, "ill") == 0
  1784.       || strcasecmp (opcname, "ill.n") == 0)
  1785.     opclass = c0opc_illegal;
  1786.   else if (strcasecmp (opcname, "break") == 0
  1787.            || strcasecmp (opcname, "break.n") == 0)
  1788.      opclass = c0opc_break;
  1789.   else if (strcasecmp (opcname, "entry") == 0)
  1790.     opclass = c0opc_entry;
  1791.   else if (strcasecmp (opcname, "rfwo") == 0)
  1792.     opclass = c0opc_rfwo;
  1793.   else if (strcasecmp (opcname, "rfwu") == 0)
  1794.     opclass = c0opc_rfwu;
  1795.   else if (xtensa_opcode_is_branch (isa, opc) > 0
  1796.            || xtensa_opcode_is_jump   (isa, opc) > 0
  1797.            || xtensa_opcode_is_loop   (isa, opc) > 0
  1798.            || xtensa_opcode_is_call   (isa, opc) > 0
  1799.            || strcasecmp (opcname, "simcall") == 0
  1800.            || strcasecmp (opcname, "syscall") == 0)
  1801.     opclass = c0opc_flow;

  1802.   /* Also, classify specific opcodes that need to be tracked.  */
  1803.   else if (strcasecmp (opcname, "add") == 0
  1804.            || strcasecmp (opcname, "add.n") == 0)
  1805.     opclass = c0opc_add;
  1806.   else if (strcasecmp (opcname, "and") == 0)
  1807.     opclass = c0opc_and;
  1808.   else if (strcasecmp (opcname, "addi") == 0
  1809.            || strcasecmp (opcname, "addi.n") == 0
  1810.            || strcasecmp (opcname, "addmi") == 0)
  1811.     opclass = c0opc_addi;
  1812.   else if (strcasecmp (opcname, "sub") == 0)
  1813.     opclass = c0opc_sub;
  1814.   else if (strcasecmp (opcname, "mov.n") == 0
  1815.            || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro.  */
  1816.     opclass = c0opc_mov;
  1817.   else if (strcasecmp (opcname, "movi") == 0
  1818.            || strcasecmp (opcname, "movi.n") == 0)
  1819.     opclass = c0opc_movi;
  1820.   else if (strcasecmp (opcname, "l32r") == 0)
  1821.     opclass = c0opc_l32r;
  1822.   else if (strcasecmp (opcname, "s32i") == 0
  1823.            || strcasecmp (opcname, "s32i.n") == 0)
  1824.     opclass = c0opc_s32i;
  1825.   else if (strcasecmp (opcname, "l32e") == 0)
  1826.     opclass = c0opc_l32e;
  1827.   else if (strcasecmp (opcname, "s32e") == 0)
  1828.     opclass = c0opc_s32e;
  1829.   else if (rwx_special_register (opcname))
  1830.     opclass = c0opc_rwxsr;

  1831.   return opclass;
  1832. }

  1833. /* Tracks register movement/mutation for a given operation, which may
  1834.    be within a bundle.  Updates the destination register tracking info
  1835.    accordingly.  The pc is needed only for pc-relative load instructions
  1836.    (eg. l32r).  The SP register number is needed to identify stores to
  1837.    the stack frame.  Returns 0, if analysis was succesfull, non-zero
  1838.    otherwise.  */

  1839. static int
  1840. call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
  1841.                 xtensa_insn_kind opclass, int nods, unsigned odv[],
  1842.                 CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
  1843. {
  1844.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1845.   unsigned litbase, litaddr, litval;

  1846.   switch (opclass)
  1847.     {
  1848.     case c0opc_addi:
  1849.       /* 3 operands: dst, src, imm.  */
  1850.       gdb_assert (nods == 3);
  1851.       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1852.       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
  1853.       break;
  1854.     case c0opc_add:
  1855.       /* 3 operands: dst, src1, src2.  */
  1856.       gdb_assert (nods == 3);
  1857.       if      (src[odv[1]].fr_reg == C0_CONST)
  1858.         {
  1859.           dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
  1860.           dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
  1861.         }
  1862.       else if (src[odv[2]].fr_reg == C0_CONST)
  1863.         {
  1864.           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1865.           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
  1866.         }
  1867.       else dst[odv[0]].fr_reg = C0_INEXP;
  1868.       break;
  1869.     case c0opc_and:
  1870.       /* 3 operands:  dst, src1, src2.  */
  1871.       gdb_assert (nods == 3);
  1872.       if (cache->c0.c0_fpalign == 0)
  1873.         {
  1874.           /* Handle dynamic stack alignment.  */
  1875.           if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
  1876.             {
  1877.               if (src[odv[2]].fr_reg == C0_CONST)
  1878.                 cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
  1879.               break;
  1880.             }
  1881.           else if ((src[odv[0]].fr_reg == spreg)
  1882.                    && (src[odv[2]].fr_reg == spreg))
  1883.             {
  1884.               if (src[odv[1]].fr_reg == C0_CONST)
  1885.                 cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
  1886.               break;
  1887.             }
  1888.           /* else fall through.  */
  1889.         }
  1890.       if      (src[odv[1]].fr_reg == C0_CONST)
  1891.         {
  1892.           dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
  1893.           dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
  1894.         }
  1895.       else if (src[odv[2]].fr_reg == C0_CONST)
  1896.         {
  1897.           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1898.           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
  1899.         }
  1900.       else dst[odv[0]].fr_reg = C0_INEXP;
  1901.       break;
  1902.     case c0opc_sub:
  1903.       /* 3 operands: dst, src1, src2.  */
  1904.       gdb_assert (nods == 3);
  1905.       if      (src[odv[2]].fr_reg == C0_CONST)
  1906.         {
  1907.           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1908.           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
  1909.         }
  1910.       else dst[odv[0]].fr_reg = C0_INEXP;
  1911.       break;
  1912.     case c0opc_mov:
  1913.       /* 2 operands: dst, src [, src].  */
  1914.       gdb_assert (nods == 2);
  1915.       /* First, check if it's a special case of saving unaligned SP
  1916.          to a spare register in case of dynamic stack adjustment.
  1917.          But, only do it one time.  The second time could be initializing
  1918.          frame pointer.  We don't want to overwrite the first one.  */
  1919.       if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
  1920.         cache->c0.c0_old_sp = odv[0];

  1921.       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
  1922.       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
  1923.       break;
  1924.     case c0opc_movi:
  1925.       /* 2 operands: dst, imm.  */
  1926.       gdb_assert (nods == 2);
  1927.       dst[odv[0]].fr_reg = C0_CONST;
  1928.       dst[odv[0]].fr_ofs = odv[1];
  1929.       break;
  1930.     case c0opc_l32r:
  1931.       /* 2 operands: dst, literal offset.  */
  1932.       gdb_assert (nods == 2);
  1933.       /* litbase = xtensa_get_litbase (pc);  can be also used.  */
  1934.       litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
  1935.         ? 0 : xtensa_read_register
  1936.                 (gdbarch_tdep (gdbarch)->litbase_regnum);
  1937.       litaddr = litbase & 1
  1938.                   ? (litbase & ~1) + (signed)odv[1]
  1939.                   : (pc + 3  + (signed)odv[1]) & ~3;
  1940.       litval = read_memory_integer (litaddr, 4, byte_order);
  1941.       dst[odv[0]].fr_reg = C0_CONST;
  1942.       dst[odv[0]].fr_ofs = litval;
  1943.       break;
  1944.     case c0opc_s32i:
  1945.       /* 3 operands: value, base, offset.  */
  1946.       gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
  1947.       /* First, check if it's a spill for saved unaligned SP,
  1948.          when dynamic stack adjustment was applied to this frame.  */
  1949.       if ((cache->c0.c0_fpalign != 0)                /* Dynamic stack adjustment.  */
  1950.           && (odv[1] == spreg)                        /* SP usage indicates spill.  */
  1951.           && (odv[0] == cache->c0.c0_old_sp))        /* Old SP register spilled.  */
  1952.         cache->c0.c0_sp_ofs = odv[2];

  1953.       if (src[odv[1]].fr_reg == spreg             /* Store to stack frame.  */
  1954.           && (src[odv[1]].fr_ofs & 3) == 0   /* Alignment preserved.  */
  1955.           &&  src[odv[0]].fr_reg >= 0             /* Value is from a register.  */
  1956.           &&  src[odv[0]].fr_ofs == 0             /* Value hasn't been modified.  */
  1957.           &&  src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time.  */
  1958.         {
  1959.           /* ISA encoding guarantees alignment.  But, check it anyway.  */
  1960.           gdb_assert ((odv[2] & 3) == 0);
  1961.           dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
  1962.         }
  1963.       break;
  1964.       /* If we end up inside Window Overflow / Underflow interrupt handler
  1965.          report an error because these handlers should have been handled
  1966.          already in a different way.  */
  1967.     case c0opc_l32e:
  1968.     case c0opc_s32e:
  1969.     case c0opc_rfwo:
  1970.     case c0opc_rfwu:
  1971.       return 1;
  1972.     default:
  1973.       return 1;
  1974.     }
  1975.   return 0;
  1976. }

  1977. /* Analyze prologue of the function at start address to determine if it uses
  1978.    the Call0 ABI, and if so track register moves and linear modifications
  1979.    in the prologue up to the PC or just beyond the prologue, whichever is
  1980.    first. An 'entry' instruction indicates non-Call0 ABI and the end of the
  1981.    prologue. The prologue may overlap non-prologue instructions but is
  1982.    guaranteed to end by the first flow-control instruction (jump, branch,
  1983.    call or return).  Since an optimized function may move information around
  1984.    and change the stack frame arbitrarily during the prologue, the information
  1985.    is guaranteed valid only at the point in the function indicated by the PC.
  1986.    May be used to skip the prologue or identify the ABI, w/o tracking.

  1987.    Returns:   Address of first instruction after prologue, or PC (whichever
  1988.               is first), or 0, if decoding failed (in libisa).
  1989.    Input args:
  1990.       start   Start address of function/prologue.
  1991.       pc      Program counter to stop at.  Use 0 to continue to end of prologue.
  1992.               If 0, avoids infinite run-on in corrupt code memory by bounding
  1993.               the scan to the end of the function if that can be determined.
  1994.       nregs   Number of general registers to track.
  1995.    InOut args:
  1996.       cache   Xtensa frame cache.

  1997.       Note that these may produce useful results even if decoding fails
  1998.       because they begin with default assumptions that analysis may change.  */

  1999. static CORE_ADDR
  2000. call0_analyze_prologue (struct gdbarch *gdbarch,
  2001.                         CORE_ADDR start, CORE_ADDR pc,
  2002.                         int nregs, xtensa_frame_cache_t *cache)
  2003. {
  2004.   CORE_ADDR ia;                    /* Current insn address in prologue.  */
  2005.   CORE_ADDR ba = 0;            /* Current address at base of insn buffer.  */
  2006.   CORE_ADDR bt;                    /* Current address at top+1 of insn buffer.  */
  2007.   gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue.  */
  2008.   xtensa_isa isa;            /* libisa ISA handle.  */
  2009.   xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot.  */
  2010.   xtensa_format ifmt;            /* libisa instruction format.  */
  2011.   int ilen, islots, is;            /* Instruction length, nbr slots, current slot.  */
  2012.   xtensa_opcode opc;            /* Opcode in current slot.  */
  2013.   xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis.  */
  2014.   int nods;                    /* Opcode number of operands.  */
  2015.   unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa.  */
  2016.   xtensa_c0reg_t *rtmp;            /* Register tracking info snapshot.  */
  2017.   int j;                    /* General loop counter.  */
  2018.   int fail = 0;                    /* Set non-zero and exit, if decoding fails.  */
  2019.   CORE_ADDR body_pc;            /* The PC for the first non-prologue insn.  */
  2020.   CORE_ADDR end_pc;            /* The PC for the lust function insn.  */

  2021.   struct symtab_and_line prologue_sal;

  2022.   DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
  2023.               (int)start, (int)pc);

  2024.   /* Try to limit the scan to the end of the function if a non-zero pc
  2025.      arg was not supplied to avoid probing beyond the end of valid memory.
  2026.      If memory is full of garbage that classifies as c0opc_uninteresting.
  2027.      If this fails (eg. if no symbols) pc ends up 0 as it was.
  2028.      Intialize the Call0 frame and register tracking info.
  2029.      Assume it's Call0 until an 'entry' instruction is encountered.
  2030.      Assume we may be in the prologue until we hit a flow control instr.  */

  2031.   rtmp = NULL;
  2032.   body_pc = UINT_MAX;
  2033.   end_pc = 0;

  2034.   /* Find out, if we have an information about the prologue from DWARF.  */
  2035.   prologue_sal = find_pc_line (start, 0);
  2036.   if (prologue_sal.line != 0) /* Found debug info.  */
  2037.     body_pc = prologue_sal.end;

  2038.   /* If we are going to analyze the prologue in general without knowing about
  2039.      the current PC, make the best assumtion for the end of the prologue.  */
  2040.   if (pc == 0)
  2041.     {
  2042.       find_pc_partial_function (start, 0, NULL, &end_pc);
  2043.       body_pc = min (end_pc, body_pc);
  2044.     }
  2045.   else
  2046.     body_pc = min (pc, body_pc);

  2047.   cache->call0 = 1;
  2048.   rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));

  2049.   if (!xtensa_default_isa)
  2050.     xtensa_default_isa = xtensa_isa_init (0, 0);
  2051.   isa = xtensa_default_isa;
  2052.   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  2053.   ins = xtensa_insnbuf_alloc (isa);
  2054.   slot = xtensa_insnbuf_alloc (isa);

  2055.   for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
  2056.     {
  2057.       /* (Re)fill instruction buffer from memory if necessary, but do not
  2058.          read memory beyond PC to be sure we stay within text section
  2059.          (this protection only works if a non-zero pc is supplied).  */

  2060.       if (ia + xtensa_isa_maxlength (isa) > bt)
  2061.         {
  2062.           ba = ia;
  2063.           bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
  2064.           if (target_read_memory (ba, ibuf, bt - ba) != 0 )
  2065.             error (_("Unable to read target memory ..."));
  2066.         }

  2067.       /* Decode format information.  */

  2068.       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  2069.       ifmt = xtensa_format_decode (isa, ins);
  2070.       if (ifmt == XTENSA_UNDEFINED)
  2071.         {
  2072.           fail = 1;
  2073.           goto done;
  2074.         }
  2075.       ilen = xtensa_format_length (isa, ifmt);
  2076.       if (ilen == XTENSA_UNDEFINED)
  2077.         {
  2078.           fail = 1;
  2079.           goto done;
  2080.         }
  2081.       islots = xtensa_format_num_slots (isa, ifmt);
  2082.       if (islots == XTENSA_UNDEFINED)
  2083.         {
  2084.           fail = 1;
  2085.           goto done;
  2086.         }

  2087.       /* Analyze a bundle or a single instruction, using a snapshot of
  2088.          the register tracking info as input for the entire bundle so that
  2089.          register changes do not take effect within this bundle.  */

  2090.       for (j = 0; j < nregs; ++j)
  2091.         rtmp[j] = cache->c0.c0_rt[j];

  2092.       for (is = 0; is < islots; ++is)
  2093.         {
  2094.           /* Decode a slot and classify the opcode.  */

  2095.           fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
  2096.           if (fail)
  2097.             goto done;

  2098.           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  2099.           DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
  2100.                      (unsigned)ia, opc);
  2101.           if (opc == XTENSA_UNDEFINED)
  2102.             opclass = c0opc_illegal;
  2103.           else
  2104.             opclass = call0_classify_opcode (isa, opc);

  2105.           /* Decide whether to track this opcode, ignore it, or bail out.  */

  2106.           switch (opclass)
  2107.             {
  2108.             case c0opc_illegal:
  2109.             case c0opc_break:
  2110.               fail = 1;
  2111.               goto done;

  2112.             case c0opc_uninteresting:
  2113.               continue;

  2114.             case c0opc_flow:  /* Flow control instructions stop analysis.  */
  2115.             case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis.  */
  2116.               goto done;

  2117.             case c0opc_entry:
  2118.               cache->call0 = 0;
  2119.               ia += ilen;                       /* Skip over 'entry' insn.  */
  2120.               goto done;

  2121.             default:
  2122.               cache->call0 = 1;
  2123.             }

  2124.           /* Only expected opcodes should get this far.  */

  2125.           /* Extract and decode the operands.  */
  2126.           nods = xtensa_opcode_num_operands (isa, opc);
  2127.           if (nods == XTENSA_UNDEFINED)
  2128.             {
  2129.               fail = 1;
  2130.               goto done;
  2131.             }

  2132.           for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
  2133.             {
  2134.               fail = xtensa_operand_get_field (isa, opc, j, ifmt,
  2135.                                                is, slot, &odv[j]);
  2136.               if (fail)
  2137.                 goto done;

  2138.               fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
  2139.               if (fail)
  2140.                 goto done;
  2141.             }

  2142.           /* Check operands to verify use of 'mov' assembler macro.  */
  2143.           if (opclass == c0opc_mov && nods == 3)
  2144.             {
  2145.               if (odv[2] == odv[1])
  2146.                 {
  2147.                   nods = 2;
  2148.                   if ((odv[0] == 1) && (odv[1] != 1))
  2149.                     /* OR  A1, An, An  , where n != 1.
  2150.                        This means we are inside epilogue already.  */
  2151.                     goto done;
  2152.                 }
  2153.               else
  2154.                 {
  2155.                   opclass = c0opc_uninteresting;
  2156.                   continue;
  2157.                 }
  2158.             }

  2159.           /* Track register movement and modification for this operation.  */
  2160.           fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
  2161.                                  opclass, nods, odv, ia, 1, cache);
  2162.           if (fail)
  2163.             goto done;
  2164.         }
  2165.     }
  2166. done:
  2167.   DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
  2168.              (unsigned)ia, fail ? "failed" : "succeeded");
  2169.   xtensa_insnbuf_free(isa, slot);
  2170.   xtensa_insnbuf_free(isa, ins);
  2171.   return fail ? XTENSA_ISA_BADPC : ia;
  2172. }

  2173. /* Initialize frame cache for the current frame in CALL0 ABI.  */

  2174. static void
  2175. call0_frame_cache (struct frame_info *this_frame,
  2176.                    xtensa_frame_cache_t *cache, CORE_ADDR pc)
  2177. {
  2178.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2179.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2180.   CORE_ADDR start_pc;                /* The beginning of the function.  */
  2181.   CORE_ADDR body_pc=UINT_MAX;        /* PC, where prologue analysis stopped.  */
  2182.   CORE_ADDR sp, fp, ra;
  2183.   int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;

  2184.   sp = get_frame_register_unsigned
  2185.     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
  2186.   fp = sp; /* Assume FP == SP until proven otherwise.  */

  2187.   /* Find the beginning of the prologue of the function containing the PC
  2188.      and analyze it up to the PC or the end of the prologue.  */

  2189.   if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
  2190.     {
  2191.       body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);

  2192.       if (body_pc == XTENSA_ISA_BADPC)
  2193.         {
  2194.           warning_once ();
  2195.           ra = 0;
  2196.           goto finish_frame_analysis;
  2197.         }
  2198.     }

  2199.   /* Get the frame information and FP (if used) at the current PC.
  2200.      If PC is in the prologue, the prologue analysis is more reliable
  2201.      than DWARF info.  We don't not know for sure, if PC is in the prologue,
  2202.      but we do know no calls have yet taken place, so we can almost
  2203.      certainly rely on the prologue analysis.  */

  2204.   if (body_pc <= pc)
  2205.     {
  2206.       /* Prologue analysis was successful up to the PC.
  2207.          It includes the cases when PC == START_PC.  */
  2208.       c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
  2209.       /* c0_hasfp == true means there is a frame pointer because
  2210.          we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
  2211.          was derived from SP.  Otherwise, it would be C0_FP.  */
  2212.       fp_regnum = c0_hasfp ? C0_FP : C0_SP;
  2213.       c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
  2214.       fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
  2215.     }
  2216.   else  /* No data from the prologue analysis.  */
  2217.     {
  2218.       c0_hasfp = 0;
  2219.       fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
  2220.       c0_frmsz = 0;
  2221.       start_pc = pc;
  2222.    }

  2223.   if (cache->c0.c0_fpalign)
  2224.     {
  2225.       /* This frame has a special prologue with a dynamic stack adjustment
  2226.          to force an alignment, which is bigger than standard 16 bytes.  */

  2227.       CORE_ADDR unaligned_sp;

  2228.       if (cache->c0.c0_old_sp == C0_INEXP)
  2229.         /* This can't be.  Prologue code should be consistent.
  2230.            Unaligned stack pointer should be saved in a spare register.  */
  2231.         {
  2232.           warning_once ();
  2233.           ra = 0;
  2234.           goto finish_frame_analysis;
  2235.         }

  2236.       if (cache->c0.c0_sp_ofs == C0_NOSTK)
  2237.         /* Saved unaligned value of SP is kept in a register.  */
  2238.         unaligned_sp = get_frame_register_unsigned
  2239.           (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
  2240.       else
  2241.         /* Get the value from stack.  */
  2242.         unaligned_sp = (CORE_ADDR)
  2243.           read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);

  2244.       prev_sp = unaligned_sp + c0_frmsz;
  2245.     }
  2246.   else
  2247.     prev_sp = fp + c0_frmsz;

  2248.   /* Frame size from debug info or prologue tracking does not account for
  2249.      alloca() and other dynamic allocations.  Adjust frame size by FP - SP.  */
  2250.   if (c0_hasfp)
  2251.     {
  2252.       fp = get_frame_register_unsigned (this_frame, fp_regnum);

  2253.       /* Update the stack frame size.  */
  2254.       c0_frmsz += fp - sp;
  2255.     }

  2256.   /* Get the return address (RA) from the stack if saved,
  2257.      or try to get it from a register.  */

  2258.   to_stk = cache->c0.c0_rt[C0_RA].to_stk;
  2259.   if (to_stk != C0_NOSTK)
  2260.     ra = (CORE_ADDR)
  2261.       read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
  2262.                            4, byte_order);

  2263.   else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
  2264.            && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
  2265.     {
  2266.       /* Special case for terminating backtrace at a function that wants to
  2267.          be seen as the outermost one.  Such a function will clear it's RA (A0)
  2268.          register to 0 in the prologue instead of saving its original value.  */
  2269.       ra = 0;
  2270.     }
  2271.   else
  2272.     {
  2273.       /* RA was copied to another register or (before any function call) may
  2274.          still be in the original RA register.  This is not always reliable:
  2275.          even in a leaf function, register tracking stops after prologue, and
  2276.          even in prologue, non-prologue instructions (not tracked) may overwrite
  2277.          RA or any register it was copied to.  If likely in prologue or before
  2278.          any call, use retracking info and hope for the best (compiler should
  2279.          have saved RA in stack if not in a leaf function).  If not in prologue,
  2280.          too bad.  */

  2281.       int i;
  2282.       for (i = 0;
  2283.            (i < C0_NREGS)
  2284.            && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
  2285.            ++i);
  2286.       if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
  2287.         i = C0_RA;
  2288.       if (i < C0_NREGS)
  2289.         {
  2290.           ra = get_frame_register_unsigned
  2291.             (this_frame,
  2292.              gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
  2293.         }
  2294.       else ra = 0;
  2295.     }

  2296. finish_frame_analysis:
  2297.   cache->pc = start_pc;
  2298.   cache->ra = ra;
  2299.   /* RA == 0 marks the outermost frame.  Do not go past it.  */
  2300.   cache->prev_sp = (ra != 0) ?  prev_sp : 0;
  2301.   cache->c0.fp_regnum = fp_regnum;
  2302.   cache->c0.c0_frmsz = c0_frmsz;
  2303.   cache->c0.c0_hasfp = c0_hasfp;
  2304.   cache->c0.c0_fp = fp;
  2305. }

  2306. static CORE_ADDR a0_saved;
  2307. static CORE_ADDR a7_saved;
  2308. static CORE_ADDR a11_saved;
  2309. static int a0_was_saved;
  2310. static int a7_was_saved;
  2311. static int a11_was_saved;

  2312. /* Simulate L32E instruction:  AT <-- ref (AS + offset).  */
  2313. static void
  2314. execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
  2315. {
  2316.   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
  2317.   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
  2318.   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
  2319.   unsigned int spilled_value
  2320.     = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));

  2321.   if ((at == 0) && !a0_was_saved)
  2322.     {
  2323.       a0_saved = xtensa_read_register (atreg);
  2324.       a0_was_saved = 1;
  2325.     }
  2326.   else if ((at == 7) && !a7_was_saved)
  2327.     {
  2328.       a7_saved = xtensa_read_register (atreg);
  2329.       a7_was_saved = 1;
  2330.     }
  2331.   else if ((at == 11) && !a11_was_saved)
  2332.     {
  2333.       a11_saved = xtensa_read_register (atreg);
  2334.       a11_was_saved = 1;
  2335.     }

  2336.   xtensa_write_register (atreg, spilled_value);
  2337. }

  2338. /* Simulate S32E instruction:  AT --> ref (AS + offset).  */
  2339. static void
  2340. execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
  2341. {
  2342.   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
  2343.   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
  2344.   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
  2345.   ULONGEST spilled_value = xtensa_read_register (atreg);

  2346.   write_memory_unsigned_integer (addr, 4,
  2347.                                  gdbarch_byte_order (gdbarch),
  2348.                                  spilled_value);
  2349. }

  2350. #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN  200

  2351. typedef enum
  2352. {
  2353.   xtWindowOverflow,
  2354.   xtWindowUnderflow,
  2355.   xtNoExceptionHandler
  2356. } xtensa_exception_handler_t;

  2357. /* Execute instruction stream from current PC until hitting RFWU or RFWO.
  2358.    Return type of Xtensa Window Interrupt Handler on success.  */
  2359. static xtensa_exception_handler_t
  2360. execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
  2361. {
  2362.   xtensa_isa isa;
  2363.   xtensa_insnbuf ins, slot;
  2364.   gdb_byte ibuf[XTENSA_ISA_BSZ];
  2365.   CORE_ADDR ia, bt, ba;
  2366.   xtensa_format ifmt;
  2367.   int ilen, islots, is;
  2368.   xtensa_opcode opc;
  2369.   int insn_num = 0;
  2370.   int fail = 0;
  2371.   void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);

  2372.   uint32_t at, as, offset;

  2373.   /* WindowUnderflow12 = true, when inside _WindowUnderflow12.  */
  2374.   int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140;

  2375.   isa = xtensa_default_isa;
  2376.   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
  2377.   ins = xtensa_insnbuf_alloc (isa);
  2378.   slot = xtensa_insnbuf_alloc (isa);
  2379.   ba = 0;
  2380.   ia = current_pc;
  2381.   bt = ia;

  2382.   a0_was_saved = 0;
  2383.   a7_was_saved = 0;
  2384.   a11_was_saved = 0;

  2385.   while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
  2386.     {
  2387.       if (ia + xtensa_isa_maxlength (isa) > bt)
  2388.         {
  2389.           ba = ia;
  2390.           bt = (ba + XTENSA_ISA_BSZ);
  2391.           if (target_read_memory (ba, ibuf, bt - ba) != 0)
  2392.             return xtNoExceptionHandler;
  2393.         }
  2394.       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
  2395.       ifmt = xtensa_format_decode (isa, ins);
  2396.       if (ifmt == XTENSA_UNDEFINED)
  2397.         return xtNoExceptionHandler;
  2398.       ilen = xtensa_format_length (isa, ifmt);
  2399.       if (ilen == XTENSA_UNDEFINED)
  2400.         return xtNoExceptionHandler;
  2401.       islots = xtensa_format_num_slots (isa, ifmt);
  2402.       if (islots == XTENSA_UNDEFINED)
  2403.         return xtNoExceptionHandler;
  2404.       for (is = 0; is < islots; ++is)
  2405.         {
  2406.           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
  2407.             return xtNoExceptionHandler;
  2408.           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
  2409.           if (opc == XTENSA_UNDEFINED)
  2410.             return xtNoExceptionHandler;
  2411.           switch (call0_classify_opcode (isa, opc))
  2412.             {
  2413.             case c0opc_illegal:
  2414.             case c0opc_flow:
  2415.             case c0opc_entry:
  2416.             case c0opc_break:
  2417.               /* We expect none of them here.  */
  2418.               return xtNoExceptionHandler;
  2419.             case c0opc_l32e:
  2420.               func = execute_l32e;
  2421.               break;
  2422.             case c0opc_s32e:
  2423.               func = execute_s32e;
  2424.               break;
  2425.             case c0opc_rfwo: /* RFWO.  */
  2426.               /* Here, we return from WindowOverflow handler and,
  2427.                  if we stopped at the very beginning, which means
  2428.                  A0 was saved, we have to restore it now.  */
  2429.               if (a0_was_saved)
  2430.                 {
  2431.                   int arreg = arreg_number (gdbarch,
  2432.                                             gdbarch_tdep (gdbarch)->a0_base,
  2433.                                             wb);
  2434.                   xtensa_write_register (arreg, a0_saved);
  2435.                 }
  2436.               return xtWindowOverflow;
  2437.             case c0opc_rfwu: /* RFWU.  */
  2438.               /* Here, we return from WindowUnderflow handler.
  2439.                  Let's see if either A7 or A11 has to be restored.  */
  2440.               if (WindowUnderflow12)
  2441.                 {
  2442.                   if (a11_was_saved)
  2443.                     {
  2444.                       int arreg = arreg_number (gdbarch,
  2445.                                                 gdbarch_tdep (gdbarch)->a0_base + 11,
  2446.                                                 wb);
  2447.                       xtensa_write_register (arreg, a11_saved);
  2448.                     }
  2449.                 }
  2450.               else if (a7_was_saved)
  2451.                 {
  2452.                   int arreg = arreg_number (gdbarch,
  2453.                                             gdbarch_tdep (gdbarch)->a0_base + 7,
  2454.                                             wb);
  2455.                   xtensa_write_register (arreg, a7_saved);
  2456.                 }
  2457.               return xtWindowUnderflow;
  2458.              default: /* Simply skip this insns.  */
  2459.               continue;
  2460.             }

  2461.           /* Decode arguments for L32E / S32E and simulate their execution.  */
  2462.           if ( xtensa_opcode_num_operands (isa, opc) != 3 )
  2463.             return xtNoExceptionHandler;
  2464.           if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
  2465.             return xtNoExceptionHandler;
  2466.           if (xtensa_operand_decode (isa, opc, 0, &at))
  2467.             return xtNoExceptionHandler;
  2468.           if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
  2469.             return xtNoExceptionHandler;
  2470.           if (xtensa_operand_decode (isa, opc, 1, &as))
  2471.             return xtNoExceptionHandler;
  2472.           if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
  2473.             return xtNoExceptionHandler;
  2474.           if (xtensa_operand_decode (isa, opc, 2, &offset))
  2475.             return xtNoExceptionHandler;

  2476.           (*func) (gdbarch, at, as, offset, wb);
  2477.         }

  2478.       ia += ilen;
  2479.     }
  2480.   return xtNoExceptionHandler;
  2481. }

  2482. /* Handle Window Overflow / Underflow exception frames.  */

  2483. static void
  2484. xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
  2485.                                      xtensa_frame_cache_t *cache,
  2486.                                      CORE_ADDR pc)
  2487. {
  2488.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2489.   CORE_ADDR ps, wb, ws, ra;
  2490.   int epc1_regnum, i, regnum;
  2491.   xtensa_exception_handler_t eh_type;

  2492.   /* Read PS, WB, and WS from the hardware. Note that PS register
  2493.      must be present, if Windowed ABI is supported.  */
  2494.   ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
  2495.   wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
  2496.   ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);

  2497.   /* Execute all the remaining instructions from Window Interrupt Handler
  2498.      by simulating them on the remote protocol level.  On return, set the
  2499.      type of Xtensa Window Interrupt Handler, or report an error.  */
  2500.   eh_type = execute_code (gdbarch, pc, wb);
  2501.   if (eh_type == xtNoExceptionHandler)
  2502.     error (_("\
  2503. Unable to decode Xtensa Window Interrupt Handler's code."));

  2504.   cache->ps = ps ^ PS_EXC;        /* Clear the exception bit in PS.  */
  2505.   cache->call0 = 0;                /* It's Windowed ABI.  */

  2506.   /* All registers for the cached frame will be alive.  */
  2507.   for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
  2508.     cache->wd.aregs[i] = -1;

  2509.   if (eh_type == xtWindowOverflow)
  2510.     cache->wd.ws = ws ^ (1 << wb);
  2511.   else /* eh_type == xtWindowUnderflow.  */
  2512.     cache->wd.ws = ws | (1 << wb);

  2513.   cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB.  */
  2514.   regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
  2515.                          cache->wd.wb);
  2516.   ra = xtensa_read_register (regnum);
  2517.   cache->wd.callsize = WINSIZE (ra);
  2518.   cache->prev_sp = xtensa_read_register (regnum + 1);
  2519.   /* Set regnum to a frame pointer of the frame being cached.  */
  2520.   regnum = xtensa_scan_prologue (gdbarch, pc);
  2521.   regnum = arreg_number (gdbarch,
  2522.                          gdbarch_tdep (gdbarch)->a0_base + regnum,
  2523.                          cache->wd.wb);
  2524.   cache->base = get_frame_register_unsigned (this_frame, regnum);

  2525.   /* Read PC of interrupted function from EPC1 register.  */
  2526.   epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
  2527.   if (epc1_regnum < 0)
  2528.     error(_("Unable to read Xtensa register EPC1"));
  2529.   cache->ra = xtensa_read_register (epc1_regnum);
  2530.   cache->pc = get_frame_func (this_frame);
  2531. }


  2532. /* Skip function prologue.

  2533.    Return the pc of the first instruction after prologue.  GDB calls this to
  2534.    find the address of the first line of the function or (if there is no line
  2535.    number information) to skip the prologue for planting breakpoints on
  2536.    function entries.  Use debug info (if present) or prologue analysis to skip
  2537.    the prologue to achieve reliable debugging behavior.  For windowed ABI,
  2538.    only the 'entry' instruction is skipped.  It is not strictly necessary to
  2539.    skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
  2540.    backtrace at any point in the prologue, however certain potential hazards
  2541.    are avoided and a more "normal" debugging experience is ensured by
  2542.    skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
  2543.    For example, if we don't skip the prologue:
  2544.    - Some args may not yet have been saved to the stack where the debug
  2545.      info expects to find them (true anyway when only 'entry' is skipped);
  2546.    - Software breakpoints ('break' instrs) may not have been unplanted
  2547.      when the prologue analysis is done on initializing the frame cache,
  2548.      and breaks in the prologue will throw off the analysis.

  2549.    If we have debug info ( line-number info, in particular ) we simply skip
  2550.    the code associated with the first function line effectively skipping
  2551.    the prologue code.  It works even in cases like

  2552.    int main()
  2553.    {        int local_var = 1;
  2554.            ....
  2555.    }

  2556.    because, for this source code, both Xtensa compilers will generate two
  2557.    separate entries ( with the same line number ) in dwarf line-number
  2558.    section to make sure there is a boundary between the prologue code and
  2559.    the rest of the function.

  2560.    If there is no debug info, we need to analyze the code.  */

  2561. /* #define DONT_SKIP_PROLOGUE  */

  2562. static CORE_ADDR
  2563. xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  2564. {
  2565.   struct symtab_and_line prologue_sal;
  2566.   CORE_ADDR body_pc;

  2567.   DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);

  2568. #if DONT_SKIP_PROLOGUE
  2569.   return start_pc;
  2570. #endif

  2571. /* Try to find first body line from debug info.  */

  2572.   prologue_sal = find_pc_line (start_pc, 0);
  2573.   if (prologue_sal.line != 0) /* Found debug info.  */
  2574.     {
  2575.       /* In Call0,  it is possible to have a function with only one instruction
  2576.          ('ret') resulting from a one-line optimized function that does nothing.
  2577.          In that case,  prologue_sal.end may actually point to the start of the
  2578.          next function in the text section,  causing a breakpoint to be set at
  2579.          the wrong place.  Check,  if the end address is within a different
  2580.          function,  and if so return the start PC.  We know we have symbol
  2581.          information.  */

  2582.       CORE_ADDR end_func;

  2583.       if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
  2584.           && call0_ret (start_pc, prologue_sal.end))
  2585.         return start_pc;

  2586.       find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
  2587.       if (end_func != start_pc)
  2588.         return start_pc;

  2589.       return prologue_sal.end;
  2590.     }

  2591.   /* No debug line info.  Analyze prologue for Call0 or simply skip ENTRY.  */
  2592.   body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
  2593.                                     xtensa_alloc_frame_cache (0));
  2594.   return body_pc != 0 ? body_pc : start_pc;
  2595. }

  2596. /* Verify the current configuration.  */
  2597. static void
  2598. xtensa_verify_config (struct gdbarch *gdbarch)
  2599. {
  2600.   struct ui_file *log;
  2601.   struct cleanup *cleanups;
  2602.   struct gdbarch_tdep *tdep;
  2603.   long length;
  2604.   char *buf;

  2605.   tdep = gdbarch_tdep (gdbarch);
  2606.   log = mem_fileopen ();
  2607.   cleanups = make_cleanup_ui_file_delete (log);

  2608.   /* Verify that we got a reasonable number of AREGS.  */
  2609.   if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
  2610.     fprintf_unfiltered (log, _("\
  2611. \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
  2612.                         tdep->num_aregs);

  2613.   /* Verify that certain registers exist.  */

  2614.   if (tdep->pc_regnum == -1)
  2615.     fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
  2616.   if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
  2617.     fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));

  2618.   if (tdep->isa_use_windowed_registers)
  2619.     {
  2620.       if (tdep->wb_regnum == -1)
  2621.         fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
  2622.       if (tdep->ws_regnum == -1)
  2623.         fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
  2624.       if (tdep->ar_base == -1)
  2625.         fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
  2626.     }

  2627.   if (tdep->a0_base == -1)
  2628.     fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));

  2629.   buf = ui_file_xstrdup (log, &length);
  2630.   make_cleanup (xfree, buf);
  2631.   if (length > 0)
  2632.     internal_error (__FILE__, __LINE__,
  2633.                     _("the following are invalid: %s"), buf);
  2634.   do_cleanups (cleanups);
  2635. }


  2636. /* Derive specific register numbers from the array of registers.  */

  2637. static void
  2638. xtensa_derive_tdep (struct gdbarch_tdep *tdep)
  2639. {
  2640.   xtensa_register_t* rmap;
  2641.   int n, max_size = 4;

  2642.   tdep->num_regs = 0;
  2643.   tdep->num_nopriv_regs = 0;

  2644. /* Special registers 0..255 (core).  */
  2645. #define XTENSA_DBREGN_SREG(n)  (0x0200+(n))

  2646.   for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
  2647.     {
  2648.       if (rmap->target_number == 0x0020)
  2649.         tdep->pc_regnum = n;
  2650.       else if (rmap->target_number == 0x0100)
  2651.         tdep->ar_base = n;
  2652.       else if (rmap->target_number == 0x0000)
  2653.         tdep->a0_base = n;
  2654.       else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
  2655.         tdep->wb_regnum = n;
  2656.       else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
  2657.         tdep->ws_regnum = n;
  2658.       else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
  2659.         tdep->debugcause_regnum = n;
  2660.       else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
  2661.         tdep->exccause_regnum = n;
  2662.       else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
  2663.         tdep->excvaddr_regnum = n;
  2664.       else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
  2665.         tdep->lbeg_regnum = n;
  2666.       else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
  2667.         tdep->lend_regnum = n;
  2668.       else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
  2669.         tdep->lcount_regnum = n;
  2670.       else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
  2671.         tdep->sar_regnum = n;
  2672.       else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
  2673.         tdep->litbase_regnum = n;
  2674.       else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
  2675.         tdep->ps_regnum = n;
  2676. #if 0
  2677.       else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
  2678.         tdep->interrupt_regnum = n;
  2679.       else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
  2680.         tdep->interrupt2_regnum = n;
  2681.       else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
  2682.         tdep->cpenable_regnum = n;
  2683. #endif

  2684.       if (rmap->byte_size > max_size)
  2685.         max_size = rmap->byte_size;
  2686.       if (rmap->mask != 0 && tdep->num_regs == 0)
  2687.         tdep->num_regs = n;
  2688.       /* Find out out how to deal with priveleged registers.

  2689.          if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
  2690.               && tdep->num_nopriv_regs == 0)
  2691.            tdep->num_nopriv_regs = n;
  2692.       */
  2693.       if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
  2694.           && tdep->num_regs == 0)
  2695.         tdep->num_regs = n;
  2696.     }

  2697.   /* Number of pseudo registers.  */
  2698.   tdep->num_pseudo_regs = n - tdep->num_regs;

  2699.   /* Empirically determined maximum sizes.  */
  2700.   tdep->max_register_raw_size = max_size;
  2701.   tdep->max_register_virtual_size = max_size;
  2702. }

  2703. /* Module "constructor" function.  */

  2704. extern struct gdbarch_tdep xtensa_tdep;

  2705. static struct gdbarch *
  2706. xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2707. {
  2708.   struct gdbarch_tdep *tdep;
  2709.   struct gdbarch *gdbarch;
  2710.   struct xtensa_abi_handler *abi_handler;

  2711.   DEBUGTRACE ("gdbarch_init()\n");

  2712.   /* We have to set the byte order before we call gdbarch_alloc.  */
  2713.   info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;

  2714.   tdep = &xtensa_tdep;
  2715.   gdbarch = gdbarch_alloc (&info, tdep);
  2716.   xtensa_derive_tdep (tdep);

  2717.   /* Verify our configuration.  */
  2718.   xtensa_verify_config (gdbarch);
  2719.   xtensa_session_once_reported = 0;

  2720.   /* Pseudo-Register read/write.  */
  2721.   set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
  2722.   set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);

  2723.   /* Set target information.  */
  2724.   set_gdbarch_num_regs (gdbarch, tdep->num_regs);
  2725.   set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
  2726.   set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
  2727.   set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
  2728.   set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);

  2729.   /* Renumber registers for known formats (stabs and dwarf2).  */
  2730.   set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
  2731.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);

  2732.   /* We provide our own function to get register information.  */
  2733.   set_gdbarch_register_name (gdbarch, xtensa_register_name);
  2734.   set_gdbarch_register_type (gdbarch, xtensa_register_type);

  2735.   /* To call functions from GDB using dummy frame.  */
  2736.   set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);

  2737.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  2738.   set_gdbarch_return_value (gdbarch, xtensa_return_value);

  2739.   /* Advance PC across any prologue instructions to reach "real" code.  */
  2740.   set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);

  2741.   /* Stack grows downward.  */
  2742.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  2743.   /* Set breakpoints.  */
  2744.   set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);

  2745.   /* After breakpoint instruction or illegal instruction, pc still
  2746.      points at break instruction, so don't decrement.  */
  2747.   set_gdbarch_decr_pc_after_break (gdbarch, 0);

  2748.   /* We don't skip args.  */
  2749.   set_gdbarch_frame_args_skip (gdbarch, 0);

  2750.   set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);

  2751.   set_gdbarch_frame_align (gdbarch, xtensa_frame_align);

  2752.   set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);

  2753.   /* Frame handling.  */
  2754.   frame_base_set_default (gdbarch, &xtensa_frame_base);
  2755.   frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
  2756.   dwarf2_append_unwinders (gdbarch);

  2757.   set_gdbarch_print_insn (gdbarch, print_insn_xtensa);

  2758.   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  2759.   xtensa_add_reggroups (gdbarch);
  2760.   set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);

  2761.   set_gdbarch_iterate_over_regset_sections
  2762.     (gdbarch, xtensa_iterate_over_regset_sections);

  2763.   set_solib_svr4_fetch_link_map_offsets
  2764.     (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  2765.   return gdbarch;
  2766. }

  2767. static void
  2768. xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  2769. {
  2770.   error (_("xtensa_dump_tdep(): not implemented"));
  2771. }

  2772. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2773. extern initialize_file_ftype _initialize_xtensa_tdep;

  2774. void
  2775. _initialize_xtensa_tdep (void)
  2776. {
  2777.   struct cmd_list_element *c;

  2778.   gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
  2779.   xtensa_init_reggroups ();

  2780.   add_setshow_zuinteger_cmd ("xtensa",
  2781.                              class_maintenance,
  2782.                              &xtensa_debug_level,
  2783.                             _("Set Xtensa debugging."),
  2784.                             _("Show Xtensa debugging."), _("\
  2785. When non-zero, Xtensa-specific debugging is enabled. \
  2786. Can be 1, 2, 3, or 4 indicating the level of debugging."),
  2787.                              NULL,
  2788.                              NULL,
  2789.                              &setdebuglist, &showdebuglist);
  2790. }