GDB (API)
/home/stan/gdb/src/gdb/xtensa-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
00002 
00003    Copyright (C) 2003-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "solib-svr4.h"
00023 #include "symtab.h"
00024 #include "symfile.h"
00025 #include "objfiles.h"
00026 #include "gdbtypes.h"
00027 #include "gdbcore.h"
00028 #include "value.h"
00029 #include "dis-asm.h"
00030 #include "inferior.h"
00031 #include "floatformat.h"
00032 #include "regcache.h"
00033 #include "reggroups.h"
00034 #include "regset.h"
00035 
00036 #include "dummy-frame.h"
00037 #include "dwarf2.h"
00038 #include "dwarf2-frame.h"
00039 #include "dwarf2loc.h"
00040 #include "frame.h"
00041 #include "frame-base.h"
00042 #include "frame-unwind.h"
00043 
00044 #include "arch-utils.h"
00045 #include "gdbarch.h"
00046 #include "remote.h"
00047 #include "serial.h"
00048 
00049 #include "command.h"
00050 #include "gdbcmd.h"
00051 #include "gdb_assert.h"
00052 
00053 #include "xtensa-isa.h"
00054 #include "xtensa-tdep.h"
00055 #include "xtensa-config.h"
00056 
00057 
00058 static unsigned int xtensa_debug_level = 0;
00059 
00060 #define DEBUGWARN(args...) \
00061   if (xtensa_debug_level > 0) \
00062     fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
00063 
00064 #define DEBUGINFO(args...) \
00065   if (xtensa_debug_level > 1) \
00066     fprintf_unfiltered (gdb_stdlog, "(info ) " args)
00067 
00068 #define DEBUGTRACE(args...) \
00069   if (xtensa_debug_level > 2) \
00070     fprintf_unfiltered (gdb_stdlog, "(trace) " args)
00071 
00072 #define DEBUGVERB(args...) \
00073   if (xtensa_debug_level > 3) \
00074     fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
00075 
00076 
00077 /* According to the ABI, the SP must be aligned to 16-byte boundaries.  */
00078 #define SP_ALIGNMENT 16
00079 
00080 
00081 /* On Windowed ABI, we use a6 through a11 for passing arguments
00082    to a function called by GDB because CALL4 is used.  */
00083 #define ARGS_NUM_REGS           6
00084 #define REGISTER_SIZE           4
00085 
00086 
00087 /* Extract the call size from the return address or PS register.  */
00088 #define PS_CALLINC_SHIFT        16
00089 #define PS_CALLINC_MASK         0x00030000
00090 #define CALLINC(ps)             (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
00091 #define WINSIZE(ra)             (4 * (( (ra) >> 30) & 0x3))
00092 
00093 /* On TX,  hardware can be configured without Exception Option.
00094    There is no PS register in this case.  Inside XT-GDB,  let us treat
00095    it as a virtual read-only register always holding the same value.  */
00096 #define TX_PS                   0x20
00097 
00098 /* ABI-independent macros.  */
00099 #define ARG_NOF(gdbarch) \
00100   (gdbarch_tdep (gdbarch)->call_abi \
00101    == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
00102 #define ARG_1ST(gdbarch) \
00103   (gdbarch_tdep (gdbarch)->call_abi  == CallAbiCall0Only \
00104    ? (gdbarch_tdep (gdbarch)->a0_base + C0_ARGS) \
00105    : (gdbarch_tdep (gdbarch)->a0_base + 6))
00106 
00107 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
00108    indicates that the instruction is an ENTRY instruction.  */
00109 
00110 #define XTENSA_IS_ENTRY(gdbarch, op1) \
00111   ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) \
00112    ? ((op1) == 0x6c) : ((op1) == 0x36))
00113 
00114 #define XTENSA_ENTRY_LENGTH     3
00115 
00116 /* windowing_enabled() returns true, if windowing is enabled.
00117    WOE must be set to 1; EXCM to 0.
00118    Note: We assume that EXCM is always 0 for XEA1.  */
00119 
00120 #define PS_WOE                  (1<<18)
00121 #define PS_EXC                  (1<<4)
00122 
00123 static int
00124 windowing_enabled (struct gdbarch *gdbarch, unsigned int ps)
00125 {
00126   /* If we know CALL0 ABI is set explicitly,  say it is Call0.  */
00127   if (gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
00128     return 0;
00129 
00130   return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
00131 }
00132 
00133 /* Convert a live A-register number to the corresponding AR-register
00134    number.  */
00135 static int
00136 arreg_number (struct gdbarch *gdbarch, int a_regnum, ULONGEST wb)
00137 {
00138   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00139   int arreg;
00140 
00141   arreg = a_regnum - tdep->a0_base;
00142   arreg += (wb & ((tdep->num_aregs - 1) >> 2)) << WB_SHIFT;
00143   arreg &= tdep->num_aregs - 1;
00144 
00145   return arreg + tdep->ar_base;
00146 }
00147 
00148 /* Convert a live AR-register number to the corresponding A-register order
00149    number in a range [0..15].  Return -1, if AR_REGNUM is out of WB window.  */
00150 static int
00151 areg_number (struct gdbarch *gdbarch, int ar_regnum, unsigned int wb)
00152 {
00153   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00154   int areg;
00155 
00156   areg = ar_regnum - tdep->ar_base;
00157   if (areg < 0 || areg >= tdep->num_aregs)
00158     return -1;
00159   areg = (areg - wb * 4) & (tdep->num_aregs - 1);
00160   return (areg > 15) ? -1 : areg;
00161 }
00162 
00163 /* Read Xtensa register directly from the hardware.  */ 
00164 static unsigned long
00165 xtensa_read_register (int regnum)
00166 {
00167   ULONGEST value;
00168 
00169   regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
00170   return (unsigned long) value;
00171 }
00172 
00173 /* Write Xtensa register directly to the hardware.  */ 
00174 static void
00175 xtensa_write_register (int regnum, ULONGEST value)
00176 {
00177   regcache_raw_write_unsigned (get_current_regcache (), regnum, value);
00178 }
00179 
00180 /* Return the window size of the previous call to the function from which we
00181    have just returned.
00182 
00183    This function is used to extract the return value after a called function
00184    has returned to the caller.  On Xtensa, the register that holds the return
00185    value (from the perspective of the caller) depends on what call
00186    instruction was used.  For now, we are assuming that the call instruction
00187    precedes the current address, so we simply analyze the call instruction.
00188    If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
00189    method to call the inferior function.  */
00190 
00191 static int
00192 extract_call_winsize (struct gdbarch *gdbarch, CORE_ADDR pc)
00193 {
00194   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00195   int winsize = 4;
00196   int insn;
00197   gdb_byte buf[4];
00198 
00199   DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
00200 
00201   /* Read the previous instruction (should be a call[x]{4|8|12}.  */
00202   read_memory (pc-3, buf, 3);
00203   insn = extract_unsigned_integer (buf, 3, byte_order);
00204 
00205   /* Decode call instruction:
00206      Little Endian
00207        call{0,4,8,12}   OFFSET || {00,01,10,11} || 0101
00208        callx{0,4,8,12}  OFFSET || 11 || {00,01,10,11} || 0000
00209      Big Endian
00210        call{0,4,8,12}   0101 || {00,01,10,11} || OFFSET
00211        callx{0,4,8,12}  0000 || {00,01,10,11} || 11 || OFFSET.  */
00212 
00213   if (byte_order == BFD_ENDIAN_LITTLE)
00214     {
00215       if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
00216         winsize = (insn & 0x30) >> 2;   /* 0, 4, 8, 12.  */
00217     }
00218   else
00219     {
00220       if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
00221         winsize = (insn >> 16) & 0xc;   /* 0, 4, 8, 12.  */
00222     }
00223   return winsize;
00224 }
00225 
00226 
00227 /* REGISTER INFORMATION */
00228 
00229 /* Find register by name.  */
00230 static int
00231 xtensa_find_register_by_name (struct gdbarch *gdbarch, char *name)
00232 {
00233   int i;
00234 
00235   for (i = 0; i < gdbarch_num_regs (gdbarch)
00236          + gdbarch_num_pseudo_regs (gdbarch);
00237        i++)
00238 
00239     if (strcasecmp (gdbarch_tdep (gdbarch)->regmap[i].name, name) == 0)
00240       return i;
00241 
00242   return -1;
00243 }
00244 
00245 /* Returns the name of a register.  */
00246 static const char *
00247 xtensa_register_name (struct gdbarch *gdbarch, int regnum)
00248 {
00249   /* Return the name stored in the register map.  */
00250   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
00251                               + gdbarch_num_pseudo_regs (gdbarch))
00252     return gdbarch_tdep (gdbarch)->regmap[regnum].name;
00253 
00254   internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
00255   return 0;
00256 }
00257 
00258 /* Return the type of a register.  Create a new type, if necessary.  */
00259 
00260 static struct type *
00261 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
00262 {
00263   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00264 
00265   /* Return signed integer for ARx and Ax registers.  */
00266   if ((regnum >= tdep->ar_base
00267        && regnum < tdep->ar_base + tdep->num_aregs)
00268       || (regnum >= tdep->a0_base
00269           && regnum < tdep->a0_base + 16))
00270     return builtin_type (gdbarch)->builtin_int;
00271 
00272   if (regnum == gdbarch_pc_regnum (gdbarch)
00273       || regnum == tdep->a0_base + 1)
00274     return builtin_type (gdbarch)->builtin_data_ptr;
00275 
00276   /* Return the stored type for all other registers.  */
00277   else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch)
00278                                    + gdbarch_num_pseudo_regs (gdbarch))
00279     {
00280       xtensa_register_t* reg = &tdep->regmap[regnum];
00281 
00282       /* Set ctype for this register (only the first time).  */
00283 
00284       if (reg->ctype == 0)
00285         {
00286           struct ctype_cache *tp;
00287           int size = reg->byte_size;
00288 
00289           /* We always use the memory representation,
00290              even if the register width is smaller.  */
00291           switch (size)
00292             {
00293             case 1:
00294               reg->ctype = builtin_type (gdbarch)->builtin_uint8;
00295               break;
00296 
00297             case 2:
00298               reg->ctype = builtin_type (gdbarch)->builtin_uint16;
00299               break;
00300 
00301             case 4:
00302               reg->ctype = builtin_type (gdbarch)->builtin_uint32;
00303               break;
00304 
00305             case 8:
00306               reg->ctype = builtin_type (gdbarch)->builtin_uint64;
00307               break;
00308 
00309             case 16:
00310               reg->ctype = builtin_type (gdbarch)->builtin_uint128;
00311               break;
00312 
00313             default:
00314               for (tp = tdep->type_entries; tp != NULL; tp = tp->next)
00315                 if (tp->size == size)
00316                   break;
00317 
00318               if (tp == NULL)
00319                 {
00320                   char *name = xstrprintf ("int%d", size * 8);
00321                   tp = xmalloc (sizeof (struct ctype_cache));
00322                   tp->next = tdep->type_entries;
00323                   tdep->type_entries = tp;
00324                   tp->size = size;
00325                   tp->virtual_type
00326                     = arch_integer_type (gdbarch, size * 8, 1, name);
00327                   xfree (name);
00328                 }
00329 
00330               reg->ctype = tp->virtual_type;
00331             }
00332         }
00333       return reg->ctype;
00334     }
00335 
00336   internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
00337   return 0;
00338 }
00339 
00340 
00341 /* Return the 'local' register number for stubs, dwarf2, etc.
00342    The debugging information enumerates registers starting from 0 for A0
00343    to n for An.  So, we only have to add the base number for A0.  */
00344 
00345 static int
00346 xtensa_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
00347 {
00348   int i;
00349 
00350   if (regnum >= 0 && regnum < 16)
00351     return gdbarch_tdep (gdbarch)->a0_base + regnum;
00352 
00353   for (i = 0;
00354        i < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
00355        i++)
00356     if (regnum == gdbarch_tdep (gdbarch)->regmap[i].target_number)
00357       return i;
00358 
00359   internal_error (__FILE__, __LINE__,
00360                   _("invalid dwarf/stabs register number %d"), regnum);
00361   return 0;
00362 }
00363 
00364 
00365 /* Write the bits of a masked register to the various registers.
00366    Only the masked areas of these registers are modified; the other
00367    fields are untouched.  The size of masked registers is always less
00368    than or equal to 32 bits.  */
00369 
00370 static void
00371 xtensa_register_write_masked (struct regcache *regcache,
00372                               xtensa_register_t *reg, const gdb_byte *buffer)
00373 {
00374   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
00375   const xtensa_mask_t *mask = reg->mask;
00376 
00377   int shift = 0;                /* Shift for next mask (mod 32).  */
00378   int start, size;              /* Start bit and size of current mask.  */
00379 
00380   unsigned int *ptr = value;
00381   unsigned int regval, m, mem = 0;
00382 
00383   int bytesize = reg->byte_size;
00384   int bitsize = bytesize * 8;
00385   int i, r;
00386 
00387   DEBUGTRACE ("xtensa_register_write_masked ()\n");
00388 
00389   /* Copy the masked register to host byte-order.  */
00390   if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
00391     for (i = 0; i < bytesize; i++)
00392       {
00393         mem >>= 8;
00394         mem |= (buffer[bytesize - i - 1] << 24);
00395         if ((i & 3) == 3)
00396           *ptr++ = mem;
00397       }
00398   else
00399     for (i = 0; i < bytesize; i++)
00400       {
00401         mem >>= 8;
00402         mem |= (buffer[i] << 24);
00403         if ((i & 3) == 3)
00404           *ptr++ = mem;
00405       }
00406 
00407   /* We might have to shift the final value:
00408      bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
00409      bytesize & 3 == x -> shift (4-x) * 8.  */
00410 
00411   *ptr = mem >> (((0 - bytesize) & 3) * 8);
00412   ptr = value;
00413   mem = *ptr;
00414 
00415   /* Write the bits to the masked areas of the other registers.  */
00416   for (i = 0; i < mask->count; i++)
00417     {
00418       start = mask->mask[i].bit_start;
00419       size = mask->mask[i].bit_size;
00420       regval = mem >> shift;
00421 
00422       if ((shift += size) > bitsize)
00423         error (_("size of all masks is larger than the register"));
00424 
00425       if (shift >= 32)
00426         {
00427           mem = *(++ptr);
00428           shift -= 32;
00429           bitsize -= 32;
00430 
00431           if (shift > 0)
00432             regval |= mem << (size - shift);
00433         }
00434 
00435       /* Make sure we have a valid register.  */
00436       r = mask->mask[i].reg_num;
00437       if (r >= 0 && size > 0)
00438         {
00439           /* Don't overwrite the unmasked areas.  */
00440           ULONGEST old_val;
00441           regcache_cooked_read_unsigned (regcache, r, &old_val);
00442           m = 0xffffffff >> (32 - size) << start;
00443           regval <<= start;
00444           regval = (regval & m) | (old_val & ~m);
00445           regcache_cooked_write_unsigned (regcache, r, regval);
00446         }
00447     }
00448 }
00449 
00450 
00451 /* Read a tie state or mapped registers.  Read the masked areas
00452    of the registers and assemble them into a single value.  */
00453 
00454 static enum register_status
00455 xtensa_register_read_masked (struct regcache *regcache,
00456                              xtensa_register_t *reg, gdb_byte *buffer)
00457 {
00458   unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
00459   const xtensa_mask_t *mask = reg->mask;
00460 
00461   int shift = 0;
00462   int start, size;
00463 
00464   unsigned int *ptr = value;
00465   unsigned int regval, mem = 0;
00466 
00467   int bytesize = reg->byte_size;
00468   int bitsize = bytesize * 8;
00469   int i;
00470 
00471   DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
00472               reg->name == 0 ? "" : reg->name);
00473 
00474   /* Assemble the register from the masked areas of other registers.  */
00475   for (i = 0; i < mask->count; i++)
00476     {
00477       int r = mask->mask[i].reg_num;
00478       if (r >= 0)
00479         {
00480           enum register_status status;
00481           ULONGEST val;
00482 
00483           status = regcache_cooked_read_unsigned (regcache, r, &val);
00484           if (status != REG_VALID)
00485             return status;
00486           regval = (unsigned int) val;
00487         }
00488       else
00489         regval = 0;
00490 
00491       start = mask->mask[i].bit_start;
00492       size = mask->mask[i].bit_size;
00493 
00494       regval >>= start;
00495 
00496       if (size < 32)
00497         regval &= (0xffffffff >> (32 - size));
00498 
00499       mem |= regval << shift;
00500 
00501       if ((shift += size) > bitsize)
00502         error (_("size of all masks is larger than the register"));
00503 
00504       if (shift >= 32)
00505         {
00506           *ptr++ = mem;
00507           bitsize -= 32;
00508           shift -= 32;
00509 
00510           if (shift == 0)
00511             mem = 0;
00512           else
00513             mem = regval >> (size - shift);
00514         }
00515     }
00516 
00517   if (shift > 0)
00518     *ptr = mem;
00519 
00520   /* Copy value to target byte order.  */
00521   ptr = value;
00522   mem = *ptr;
00523 
00524   if (gdbarch_byte_order (get_regcache_arch (regcache)) == BFD_ENDIAN_BIG)
00525     for (i = 0; i < bytesize; i++)
00526       {
00527         if ((i & 3) == 0)
00528           mem = *ptr++;
00529         buffer[bytesize - i - 1] = mem & 0xff;
00530         mem >>= 8;
00531       }
00532   else
00533     for (i = 0; i < bytesize; i++)
00534       {
00535         if ((i & 3) == 0)
00536           mem = *ptr++;
00537         buffer[i] = mem & 0xff;
00538         mem >>= 8;
00539       }
00540 
00541   return REG_VALID;
00542 }
00543 
00544 
00545 /* Read pseudo registers.  */
00546 
00547 static enum register_status
00548 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
00549                              struct regcache *regcache,
00550                              int regnum,
00551                              gdb_byte *buffer)
00552 {
00553   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00554 
00555   DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
00556               regnum, xtensa_register_name (gdbarch, regnum));
00557 
00558   if (regnum == gdbarch_num_regs (gdbarch)
00559                 + gdbarch_num_pseudo_regs (gdbarch) - 1)
00560      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
00561 
00562   /* Read aliases a0..a15, if this is a Windowed ABI.  */
00563   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
00564       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
00565       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
00566     {
00567       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
00568       enum register_status status;
00569 
00570       status = regcache_raw_read (regcache,
00571                                   gdbarch_tdep (gdbarch)->wb_regnum,
00572                                   buf);
00573       if (status != REG_VALID)
00574         return status;
00575       regnum = arreg_number (gdbarch, regnum,
00576                              extract_unsigned_integer (buf, 4, byte_order));
00577     }
00578 
00579   /* We can always read non-pseudo registers.  */
00580   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
00581     return regcache_raw_read (regcache, regnum, buffer);
00582 
00583   /* We have to find out how to deal with priveleged registers.
00584      Let's treat them as pseudo-registers, but we cannot read/write them.  */
00585      
00586   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
00587     {
00588       buffer[0] = (gdb_byte)0;
00589       buffer[1] = (gdb_byte)0;
00590       buffer[2] = (gdb_byte)0;
00591       buffer[3] = (gdb_byte)0;
00592       return REG_VALID;
00593     }
00594   /* Pseudo registers.  */
00595   else if (regnum >= 0
00596             && regnum < gdbarch_num_regs (gdbarch)
00597                         + gdbarch_num_pseudo_regs (gdbarch))
00598     {
00599       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
00600       xtensa_register_type_t type = reg->type;
00601       int flags = gdbarch_tdep (gdbarch)->target_flags;
00602 
00603       /* We cannot read Unknown or Unmapped registers.  */
00604       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
00605         {
00606           if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
00607             {
00608               warning (_("cannot read register %s"),
00609                        xtensa_register_name (gdbarch, regnum));
00610               return REG_VALID;
00611             }
00612         }
00613 
00614       /* Some targets cannot read TIE register files.  */
00615       else if (type == xtRegisterTypeTieRegfile)
00616         {
00617           /* Use 'fetch' to get register?  */
00618           if (flags & xtTargetFlagsUseFetchStore)
00619             {
00620               warning (_("cannot read register"));
00621               return REG_VALID;
00622             }
00623 
00624           /* On some targets (esp. simulators), we can always read the reg.  */
00625           else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
00626             {
00627               warning (_("cannot read register"));
00628               return REG_VALID;
00629             }
00630         }
00631 
00632       /* We can always read mapped registers.  */
00633       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
00634         return xtensa_register_read_masked (regcache, reg, buffer);
00635 
00636       /* Assume that we can read the register.  */
00637       return regcache_raw_read (regcache, regnum, buffer);
00638     }
00639   else
00640     internal_error (__FILE__, __LINE__,
00641                     _("invalid register number %d"), regnum);
00642 }
00643 
00644 
00645 /* Write pseudo registers.  */
00646 
00647 static void
00648 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
00649                               struct regcache *regcache,
00650                               int regnum,
00651                               const gdb_byte *buffer)
00652 {
00653   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00654 
00655   DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
00656               regnum, xtensa_register_name (gdbarch, regnum));
00657 
00658   if (regnum == gdbarch_num_regs (gdbarch)
00659                 + gdbarch_num_pseudo_regs (gdbarch) -1)
00660      regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
00661 
00662   /* Renumber register, if aliase a0..a15 on Windowed ABI.  */
00663   if (gdbarch_tdep (gdbarch)->isa_use_windowed_registers
00664       && (regnum >= gdbarch_tdep (gdbarch)->a0_base)
00665       && (regnum <= gdbarch_tdep (gdbarch)->a0_base + 15))
00666     {
00667       gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
00668 
00669       regcache_raw_read (regcache,
00670                          gdbarch_tdep (gdbarch)->wb_regnum, buf);
00671       regnum = arreg_number (gdbarch, regnum,
00672                              extract_unsigned_integer (buf, 4, byte_order));
00673     }
00674 
00675   /* We can always write 'core' registers.
00676      Note: We might have converted Ax->ARy.  */
00677   if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
00678     regcache_raw_write (regcache, regnum, buffer);
00679 
00680   /* We have to find out how to deal with priveleged registers.
00681      Let's treat them as pseudo-registers, but we cannot read/write them.  */
00682 
00683   else if (regnum < gdbarch_tdep (gdbarch)->a0_base)
00684     {
00685       return;
00686     }
00687   /* Pseudo registers.  */
00688   else if (regnum >= 0
00689            && regnum < gdbarch_num_regs (gdbarch)
00690                        + gdbarch_num_pseudo_regs (gdbarch))
00691     {
00692       xtensa_register_t *reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
00693       xtensa_register_type_t type = reg->type;
00694       int flags = gdbarch_tdep (gdbarch)->target_flags;
00695 
00696       /* On most targets, we cannot write registers
00697          of type "Unknown" or "Unmapped".  */
00698       if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
00699         {
00700           if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
00701             {
00702               warning (_("cannot write register %s"),
00703                        xtensa_register_name (gdbarch, regnum));
00704               return;
00705             }
00706         }
00707 
00708       /* Some targets cannot read TIE register files.  */
00709       else if (type == xtRegisterTypeTieRegfile)
00710         {
00711           /* Use 'store' to get register?  */
00712           if (flags & xtTargetFlagsUseFetchStore)
00713             {
00714               warning (_("cannot write register"));
00715               return;
00716             }
00717 
00718           /* On some targets (esp. simulators), we can always write
00719              the register.  */
00720           else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
00721             {
00722               warning (_("cannot write register"));
00723               return;
00724             }
00725         }
00726 
00727       /* We can always write mapped registers.  */
00728       else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
00729         {
00730           xtensa_register_write_masked (regcache, reg, buffer);
00731           return;
00732         }
00733 
00734       /* Assume that we can write the register.  */
00735       regcache_raw_write (regcache, regnum, buffer);
00736     }
00737   else
00738     internal_error (__FILE__, __LINE__,
00739                     _("invalid register number %d"), regnum);
00740 }
00741 
00742 static struct reggroup *xtensa_ar_reggroup;
00743 static struct reggroup *xtensa_user_reggroup;
00744 static struct reggroup *xtensa_vectra_reggroup;
00745 static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
00746 
00747 static void
00748 xtensa_init_reggroups (void)
00749 {
00750   int i;
00751   char cpname[] = "cp0";
00752 
00753   xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
00754   xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
00755   xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
00756 
00757   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
00758     {
00759       cpname[2] = '0' + i;
00760       xtensa_cp[i] = reggroup_new (cpname, USER_REGGROUP);
00761     }
00762 }
00763 
00764 static void
00765 xtensa_add_reggroups (struct gdbarch *gdbarch)
00766 {
00767   int i;
00768 
00769   /* Predefined groups.  */
00770   reggroup_add (gdbarch, all_reggroup);
00771   reggroup_add (gdbarch, save_reggroup);
00772   reggroup_add (gdbarch, restore_reggroup);
00773   reggroup_add (gdbarch, system_reggroup);
00774   reggroup_add (gdbarch, vector_reggroup);
00775   reggroup_add (gdbarch, general_reggroup);
00776   reggroup_add (gdbarch, float_reggroup);
00777 
00778   /* Xtensa-specific groups.  */
00779   reggroup_add (gdbarch, xtensa_ar_reggroup);
00780   reggroup_add (gdbarch, xtensa_user_reggroup);
00781   reggroup_add (gdbarch, xtensa_vectra_reggroup);
00782 
00783   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
00784     reggroup_add (gdbarch, xtensa_cp[i]);
00785 }
00786 
00787 static int 
00788 xtensa_coprocessor_register_group (struct reggroup *group)
00789 {
00790   int i;
00791 
00792   for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
00793     if (group == xtensa_cp[i])
00794       return i;
00795 
00796   return -1;
00797 }
00798 
00799 #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
00800                         | XTENSA_REGISTER_FLAGS_WRITABLE \
00801                         | XTENSA_REGISTER_FLAGS_VOLATILE)
00802 
00803 #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
00804                         | XTENSA_REGISTER_FLAGS_WRITABLE)
00805 
00806 static int
00807 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
00808                             int regnum,
00809                             struct reggroup *group)
00810 {
00811   xtensa_register_t* reg = &gdbarch_tdep (gdbarch)->regmap[regnum];
00812   xtensa_register_type_t type = reg->type;
00813   xtensa_register_group_t rg = reg->group;
00814   int cp_number;
00815 
00816   if (group == save_reggroup)
00817     /* Every single register should be included into the list of registers
00818        to be watched for changes while using -data-list-changed-registers.  */
00819     return 1;
00820 
00821   /* First, skip registers that are not visible to this target
00822      (unknown and unmapped registers when not using ISS).  */
00823 
00824   if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
00825     return 0;
00826   if (group == all_reggroup)
00827     return 1;
00828   if (group == xtensa_ar_reggroup)
00829     return rg & xtRegisterGroupAddrReg;
00830   if (group == xtensa_user_reggroup)
00831     return rg & xtRegisterGroupUser;
00832   if (group == float_reggroup)
00833     return rg & xtRegisterGroupFloat;
00834   if (group == general_reggroup)
00835     return rg & xtRegisterGroupGeneral;
00836   if (group == system_reggroup)
00837     return rg & xtRegisterGroupState;
00838   if (group == vector_reggroup || group == xtensa_vectra_reggroup)
00839     return rg & xtRegisterGroupVectra;
00840   if (group == restore_reggroup)
00841     return (regnum < gdbarch_num_regs (gdbarch)
00842             && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
00843   cp_number = xtensa_coprocessor_register_group (group);
00844   if (cp_number >= 0)
00845     return rg & (xtRegisterGroupCP0 << cp_number);
00846   else
00847     return 1;
00848 }
00849 
00850 
00851 /* Supply register REGNUM from the buffer specified by GREGS and LEN
00852    in the general-purpose register set REGSET to register cache
00853    REGCACHE.  If REGNUM is -1 do this for all registers in REGSET.  */
00854 
00855 static void
00856 xtensa_supply_gregset (const struct regset *regset,
00857                        struct regcache *rc,
00858                        int regnum,
00859                        const void *gregs,
00860                        size_t len)
00861 {
00862   const xtensa_elf_gregset_t *regs = gregs;
00863   struct gdbarch *gdbarch = get_regcache_arch (rc);
00864   int i;
00865 
00866   DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...)\n", regnum);
00867 
00868   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
00869     regcache_raw_supply (rc, gdbarch_pc_regnum (gdbarch), (char *) &regs->pc);
00870   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
00871     regcache_raw_supply (rc, gdbarch_ps_regnum (gdbarch), (char *) &regs->ps);
00872   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
00873     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->wb_regnum,
00874                          (char *) &regs->windowbase);
00875   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
00876     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ws_regnum,
00877                          (char *) &regs->windowstart);
00878   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
00879     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lbeg_regnum,
00880                          (char *) &regs->lbeg);
00881   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
00882     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lend_regnum,
00883                          (char *) &regs->lend);
00884   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
00885     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->lcount_regnum,
00886                          (char *) &regs->lcount);
00887   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
00888     regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->sar_regnum,
00889                          (char *) &regs->sar);
00890   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
00891       && regnum < gdbarch_tdep (gdbarch)->ar_base
00892                     + gdbarch_tdep (gdbarch)->num_aregs)
00893     regcache_raw_supply (rc, regnum,
00894                          (char *) &regs->ar[regnum - gdbarch_tdep
00895                            (gdbarch)->ar_base]);
00896   else if (regnum == -1)
00897     {
00898       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
00899         regcache_raw_supply (rc, gdbarch_tdep (gdbarch)->ar_base + i,
00900                              (char *) &regs->ar[i]);
00901     }
00902 }
00903 
00904 
00905 /* Xtensa register set.  */
00906 
00907 static struct regset
00908 xtensa_gregset =
00909 {
00910   NULL,
00911   xtensa_supply_gregset
00912 };
00913 
00914 
00915 /* Return the appropriate register set for the core
00916    section identified by SECT_NAME and SECT_SIZE.  */
00917 
00918 static const struct regset *
00919 xtensa_regset_from_core_section (struct gdbarch *core_arch,
00920                                  const char *sect_name,
00921                                  size_t sect_size)
00922 {
00923   DEBUGTRACE ("xtensa_regset_from_core_section "
00924               "(..., sect_name==\"%s\", sect_size==%x)\n",
00925               sect_name, (unsigned int) sect_size);
00926 
00927   if (strcmp (sect_name, ".reg") == 0
00928       && sect_size >= sizeof(xtensa_elf_gregset_t))
00929     return &xtensa_gregset;
00930 
00931   return NULL;
00932 }
00933 
00934 
00935 /* Handling frames.  */
00936 
00937 /* Number of registers to save in case of Windowed ABI.  */
00938 #define XTENSA_NUM_SAVED_AREGS          12
00939 
00940 /* Frame cache part for Windowed ABI.  */
00941 typedef struct xtensa_windowed_frame_cache
00942 {
00943   int wb;               /* WINDOWBASE of the previous frame.  */
00944   int callsize;         /* Call size of this frame.  */
00945   int ws;               /* WINDOWSTART of the previous frame.  It keeps track of
00946                            life windows only.  If there is no bit set for the
00947                            window,  that means it had been already spilled
00948                            because of window overflow.  */
00949 
00950    /* Addresses of spilled A-registers.
00951       AREGS[i] == -1, if corresponding AR is alive.  */
00952   CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
00953 } xtensa_windowed_frame_cache_t;
00954 
00955 /* Call0 ABI Definitions.  */
00956 
00957 #define C0_MAXOPDS  3   /* Maximum number of operands for prologue
00958                            analysis.  */
00959 #define C0_NREGS   16   /* Number of A-registers to track.  */
00960 #define C0_CLESV   12   /* Callee-saved registers are here and up.  */
00961 #define C0_SP       1   /* Register used as SP.  */
00962 #define C0_FP      15   /* Register used as FP.  */
00963 #define C0_RA       0   /* Register used as return address.  */
00964 #define C0_ARGS     2   /* Register used as first arg/retval.  */
00965 #define C0_NARGS    6   /* Number of A-regs for args/retvals.  */
00966 
00967 /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
00968    A-register where the current content of the reg came from (in terms
00969    of an original reg and a constant).  Negative values of c0_rt[n].fp_reg
00970    mean that the orignal content of the register was saved to the stack.
00971    c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't 
00972    know where SP will end up until the entire prologue has been analyzed.  */
00973 
00974 #define C0_CONST   -1   /* fr_reg value if register contains a constant.  */
00975 #define C0_INEXP   -2   /* fr_reg value if inexpressible as reg + offset.  */
00976 #define C0_NOSTK   -1   /* to_stk value if register has not been stored.  */
00977 
00978 extern xtensa_isa xtensa_default_isa;
00979 
00980 typedef struct xtensa_c0reg
00981 {
00982   int fr_reg;  /* original register from which register content
00983                   is derived, or C0_CONST, or C0_INEXP.  */
00984   int fr_ofs;  /* constant offset from reg, or immediate value.  */
00985   int to_stk;  /* offset from original SP to register (4-byte aligned),
00986                   or C0_NOSTK if register has not been saved.  */
00987 } xtensa_c0reg_t;
00988 
00989 /* Frame cache part for Call0 ABI.  */
00990 typedef struct xtensa_call0_frame_cache
00991 {
00992   int c0_frmsz;                    /* Stack frame size.  */
00993   int c0_hasfp;                    /* Current frame uses frame pointer.  */
00994   int fp_regnum;                   /* A-register used as FP.  */
00995   int c0_fp;                       /* Actual value of frame pointer.  */
00996   int c0_fpalign;                  /* Dinamic adjustment for the stack
00997                                       pointer. It's an AND mask. Zero,
00998                                       if alignment was not adjusted.  */
00999   int c0_old_sp;                   /* In case of dynamic adjustment, it is
01000                                       a register holding unaligned sp. 
01001                                       C0_INEXP, when undefined.  */
01002   int c0_sp_ofs;                   /* If "c0_old_sp" was spilled it's a
01003                                       stack offset. C0_NOSTK otherwise.  */
01004                                            
01005   xtensa_c0reg_t c0_rt[C0_NREGS];  /* Register tracking information.  */
01006 } xtensa_call0_frame_cache_t;
01007 
01008 typedef struct xtensa_frame_cache
01009 {
01010   CORE_ADDR base;       /* Stack pointer of this frame.  */
01011   CORE_ADDR pc;         /* PC of this frame at the function entry point.  */
01012   CORE_ADDR ra;         /* The raw return address of this frame.  */
01013   CORE_ADDR ps;         /* The PS register of the previous (older) frame.  */
01014   CORE_ADDR prev_sp;    /* Stack Pointer of the previous (older) frame.  */
01015   int call0;            /* It's a call0 framework (else windowed).  */
01016   union
01017     {
01018       xtensa_windowed_frame_cache_t     wd;     /* call0 == false.  */
01019       xtensa_call0_frame_cache_t        c0;     /* call0 == true.  */
01020     };
01021 } xtensa_frame_cache_t;
01022 
01023 
01024 static struct xtensa_frame_cache *
01025 xtensa_alloc_frame_cache (int windowed)
01026 {
01027   xtensa_frame_cache_t *cache;
01028   int i;
01029 
01030   DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
01031 
01032   cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
01033 
01034   cache->base = 0;
01035   cache->pc = 0;
01036   cache->ra = 0;
01037   cache->ps = 0;
01038   cache->prev_sp = 0;
01039   cache->call0 = !windowed;
01040   if (cache->call0)
01041     {
01042       cache->c0.c0_frmsz  = -1;
01043       cache->c0.c0_hasfp  =  0;
01044       cache->c0.fp_regnum = -1;
01045       cache->c0.c0_fp     = -1;
01046       cache->c0.c0_fpalign =  0;
01047       cache->c0.c0_old_sp  =  C0_INEXP;
01048       cache->c0.c0_sp_ofs  =  C0_NOSTK;
01049 
01050       for (i = 0; i < C0_NREGS; i++)
01051         {
01052           cache->c0.c0_rt[i].fr_reg = i;
01053           cache->c0.c0_rt[i].fr_ofs = 0;
01054           cache->c0.c0_rt[i].to_stk = C0_NOSTK;
01055         }
01056     }
01057   else
01058     {
01059       cache->wd.wb = 0;
01060       cache->wd.ws = 0;
01061       cache->wd.callsize = -1;
01062 
01063       for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
01064         cache->wd.aregs[i] = -1;
01065     }
01066   return cache;
01067 }
01068 
01069 
01070 static CORE_ADDR
01071 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
01072 {
01073   return address & ~15;
01074 }
01075 
01076 
01077 static CORE_ADDR
01078 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01079 {
01080   gdb_byte buf[8];
01081   CORE_ADDR pc;
01082 
01083   DEBUGTRACE ("xtensa_unwind_pc (next_frame = %s)\n", 
01084                 host_address_to_string (next_frame));
01085 
01086   frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
01087   pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
01088 
01089   DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int) pc);
01090 
01091   return pc;
01092 }
01093 
01094 
01095 static struct frame_id
01096 xtensa_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
01097 {
01098   CORE_ADDR pc, fp;
01099 
01100   /* THIS-FRAME is a dummy frame.  Return a frame ID of that frame.  */
01101 
01102   pc = get_frame_pc (this_frame);
01103   fp = get_frame_register_unsigned
01104          (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
01105 
01106   /* Make dummy frame ID unique by adding a constant.  */
01107   return frame_id_build (fp + SP_ALIGNMENT, pc);
01108 }
01109 
01110 /* Returns true,  if instruction to execute next is unique to Xtensa Window
01111    Interrupt Handlers.  It can only be one of L32E,  S32E,  RFWO,  or RFWU.  */
01112 
01113 static int
01114 xtensa_window_interrupt_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
01115 {
01116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01117   unsigned int insn = read_memory_integer (pc, 4, byte_order);
01118   unsigned int code;
01119 
01120   if (byte_order == BFD_ENDIAN_BIG)
01121     {
01122       /* Check, if this is L32E or S32E.  */
01123       code = insn & 0xf000ff00;
01124       if ((code == 0x00009000) || (code == 0x00009400))
01125         return 1;
01126       /* Check, if this is RFWU or RFWO.  */
01127       code = insn & 0xffffff00;
01128       return ((code == 0x00430000) || (code == 0x00530000));
01129     }
01130   else
01131     {
01132       /* Check, if this is L32E or S32E.  */
01133       code = insn & 0x00ff000f;
01134       if ((code == 0x090000) || (code == 0x490000))
01135         return 1;
01136       /* Check, if this is RFWU or RFWO.  */
01137       code = insn & 0x00ffffff;
01138       return ((code == 0x00003400) || (code == 0x00003500));
01139     }
01140 }
01141 
01142 /* Returns the best guess about which register is a frame pointer
01143    for the function containing CURRENT_PC.  */
01144 
01145 #define XTENSA_ISA_BSZ          32              /* Instruction buffer size.  */
01146 #define XTENSA_ISA_BADPC        ((CORE_ADDR)0)  /* Bad PC value.  */
01147 
01148 static unsigned int
01149 xtensa_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR current_pc)
01150 {
01151 #define RETURN_FP goto done
01152 
01153   unsigned int fp_regnum = gdbarch_tdep (gdbarch)->a0_base + 1;
01154   CORE_ADDR start_addr;
01155   xtensa_isa isa;
01156   xtensa_insnbuf ins, slot;
01157   gdb_byte ibuf[XTENSA_ISA_BSZ];
01158   CORE_ADDR ia, bt, ba;
01159   xtensa_format ifmt;
01160   int ilen, islots, is;
01161   xtensa_opcode opc;
01162   const char *opcname;
01163 
01164   find_pc_partial_function (current_pc, NULL, &start_addr, NULL);
01165   if (start_addr == 0)
01166     return fp_regnum;
01167 
01168   if (!xtensa_default_isa)
01169     xtensa_default_isa = xtensa_isa_init (0, 0);
01170   isa = xtensa_default_isa;
01171   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
01172   ins = xtensa_insnbuf_alloc (isa);
01173   slot = xtensa_insnbuf_alloc (isa);
01174   ba = 0;
01175 
01176   for (ia = start_addr, bt = ia; ia < current_pc ; ia += ilen)
01177     {
01178       if (ia + xtensa_isa_maxlength (isa) > bt)
01179         {
01180           ba = ia;
01181           bt = (ba + XTENSA_ISA_BSZ) < current_pc
01182             ? ba + XTENSA_ISA_BSZ : current_pc;
01183           if (target_read_memory (ba, ibuf, bt - ba) != 0)
01184             RETURN_FP;
01185         }
01186 
01187       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
01188       ifmt = xtensa_format_decode (isa, ins);
01189       if (ifmt == XTENSA_UNDEFINED)
01190         RETURN_FP;
01191       ilen = xtensa_format_length (isa, ifmt);
01192       if (ilen == XTENSA_UNDEFINED)
01193         RETURN_FP;
01194       islots = xtensa_format_num_slots (isa, ifmt);
01195       if (islots == XTENSA_UNDEFINED)
01196         RETURN_FP;
01197       
01198       for (is = 0; is < islots; ++is)
01199         {
01200           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
01201             RETURN_FP;
01202           
01203           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
01204           if (opc == XTENSA_UNDEFINED) 
01205             RETURN_FP;
01206           
01207           opcname = xtensa_opcode_name (isa, opc);
01208 
01209           if (strcasecmp (opcname, "mov.n") == 0
01210               || strcasecmp (opcname, "or") == 0)
01211             {
01212               unsigned int register_operand;
01213 
01214               /* Possible candidate for setting frame pointer
01215                  from A1.  This is what we are looking for.  */
01216 
01217               if (xtensa_operand_get_field (isa, opc, 1, ifmt, 
01218                                             is, slot, &register_operand) != 0)
01219                 RETURN_FP;
01220               if (xtensa_operand_decode (isa, opc, 1, &register_operand) != 0)
01221                 RETURN_FP;
01222               if (register_operand == 1)  /* Mov{.n} FP A1.  */
01223                 {
01224                   if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, 
01225                                                 &register_operand) != 0)
01226                     RETURN_FP;
01227                   if (xtensa_operand_decode (isa, opc, 0,
01228                                              &register_operand) != 0)
01229                     RETURN_FP;
01230 
01231                   fp_regnum
01232                     = gdbarch_tdep (gdbarch)->a0_base + register_operand;
01233                   RETURN_FP;
01234                 }
01235             }
01236 
01237           if (
01238               /* We have problems decoding the memory.  */
01239               opcname == NULL 
01240               || strcasecmp (opcname, "ill") == 0
01241               || strcasecmp (opcname, "ill.n") == 0
01242               /* Hit planted breakpoint.  */
01243               || strcasecmp (opcname, "break") == 0
01244               || strcasecmp (opcname, "break.n") == 0
01245               /* Flow control instructions finish prologue.  */
01246               || xtensa_opcode_is_branch (isa, opc) > 0
01247               || xtensa_opcode_is_jump   (isa, opc) > 0
01248               || xtensa_opcode_is_loop   (isa, opc) > 0
01249               || xtensa_opcode_is_call   (isa, opc) > 0
01250               || strcasecmp (opcname, "simcall") == 0
01251               || strcasecmp (opcname, "syscall") == 0)
01252             /* Can not continue analysis.  */
01253             RETURN_FP;
01254         }
01255     }
01256 done:
01257   xtensa_insnbuf_free(isa, slot);
01258   xtensa_insnbuf_free(isa, ins);
01259   return fp_regnum;
01260 }
01261 
01262 /* The key values to identify the frame using "cache" are 
01263 
01264         cache->base    = SP (or best guess about FP) of this frame;
01265         cache->pc      = entry-PC (entry point of the frame function);
01266         cache->prev_sp = SP of the previous frame.  */
01267 
01268 static void
01269 call0_frame_cache (struct frame_info *this_frame,
01270                    xtensa_frame_cache_t *cache, CORE_ADDR pc);
01271 
01272 static void
01273 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
01274                                      xtensa_frame_cache_t *cache,
01275                                      CORE_ADDR pc);
01276 
01277 static struct xtensa_frame_cache *
01278 xtensa_frame_cache (struct frame_info *this_frame, void **this_cache)
01279 {
01280   xtensa_frame_cache_t *cache;
01281   CORE_ADDR ra, wb, ws, pc, sp, ps;
01282   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01283   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01284   unsigned int fp_regnum;
01285   int  windowed, ps_regnum;
01286 
01287   if (*this_cache)
01288     return *this_cache;
01289 
01290   pc = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
01291   ps_regnum = gdbarch_ps_regnum (gdbarch);
01292   ps = (ps_regnum >= 0
01293         ? get_frame_register_unsigned (this_frame, ps_regnum) : TX_PS);
01294 
01295   windowed = windowing_enabled (gdbarch, ps);
01296 
01297   /* Get pristine xtensa-frame.  */
01298   cache = xtensa_alloc_frame_cache (windowed);
01299   *this_cache = cache;
01300 
01301   if (windowed)
01302     {
01303       char op1;
01304 
01305       /* Get WINDOWBASE, WINDOWSTART, and PS registers.  */
01306       wb = get_frame_register_unsigned (this_frame, 
01307                                         gdbarch_tdep (gdbarch)->wb_regnum);
01308       ws = get_frame_register_unsigned (this_frame,
01309                                         gdbarch_tdep (gdbarch)->ws_regnum);
01310 
01311       op1 = read_memory_integer (pc, 1, byte_order);
01312       if (XTENSA_IS_ENTRY (gdbarch, op1))
01313         {
01314           int callinc = CALLINC (ps);
01315           ra = get_frame_register_unsigned
01316             (this_frame, gdbarch_tdep (gdbarch)->a0_base + callinc * 4);
01317           
01318           /* ENTRY hasn't been executed yet, therefore callsize is still 0.  */
01319           cache->wd.callsize = 0;
01320           cache->wd.wb = wb;
01321           cache->wd.ws = ws;
01322           cache->prev_sp = get_frame_register_unsigned
01323                              (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
01324 
01325           /* This only can be the outermost frame since we are
01326              just about to execute ENTRY.  SP hasn't been set yet.
01327              We can assume any frame size, because it does not
01328              matter, and, let's fake frame base in cache.  */
01329           cache->base = cache->prev_sp - 16;
01330 
01331           cache->pc = pc;
01332           cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
01333           cache->ps = (ps & ~PS_CALLINC_MASK)
01334             | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
01335 
01336           return cache;
01337         }
01338       else
01339         {
01340           fp_regnum = xtensa_scan_prologue (gdbarch, pc);
01341           ra = get_frame_register_unsigned (this_frame,
01342                                             gdbarch_tdep (gdbarch)->a0_base);
01343           cache->wd.callsize = WINSIZE (ra);
01344           cache->wd.wb = (wb - cache->wd.callsize / 4)
01345                           & (gdbarch_tdep (gdbarch)->num_aregs / 4 - 1);
01346           cache->wd.ws = ws & ~(1 << wb);
01347 
01348           cache->pc = get_frame_func (this_frame);
01349           cache->ra = (pc & 0xc0000000) | (ra & 0x3fffffff);
01350           cache->ps = (ps & ~PS_CALLINC_MASK)
01351             | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
01352         }
01353 
01354       if (cache->wd.ws == 0)
01355         {
01356           int i;
01357 
01358           /* Set A0...A3.  */
01359           sp = get_frame_register_unsigned
01360             (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1) - 16;
01361           
01362           for (i = 0; i < 4; i++, sp += 4)
01363             {
01364               cache->wd.aregs[i] = sp;
01365             }
01366 
01367           if (cache->wd.callsize > 4)
01368             {
01369               /* Set A4...A7/A11.  */
01370               /* Get the SP of the frame previous to the previous one.
01371                  To achieve this, we have to dereference SP twice.  */
01372               sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
01373               sp = (CORE_ADDR) read_memory_integer (sp - 12, 4, byte_order);
01374               sp -= cache->wd.callsize * 4;
01375 
01376               for ( i = 4; i < cache->wd.callsize; i++, sp += 4)
01377                 {
01378                   cache->wd.aregs[i] = sp;
01379                 }
01380             }
01381         }
01382 
01383       if ((cache->prev_sp == 0) && ( ra != 0 ))
01384         /* If RA is equal to 0 this frame is an outermost frame.  Leave
01385            cache->prev_sp unchanged marking the boundary of the frame stack.  */
01386         {
01387           if ((cache->wd.ws & (1 << cache->wd.wb)) == 0)
01388             {
01389               /* Register window overflow already happened.
01390                  We can read caller's SP from the proper spill loction.  */
01391               sp = get_frame_register_unsigned
01392                 (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
01393               cache->prev_sp = read_memory_integer (sp - 12, 4, byte_order);
01394             }
01395           else
01396             {
01397               /* Read caller's frame SP directly from the previous window.  */
01398               int regnum = arreg_number
01399                              (gdbarch, gdbarch_tdep (gdbarch)->a0_base + 1,
01400                               cache->wd.wb);
01401 
01402               cache->prev_sp = xtensa_read_register (regnum);
01403             }
01404         }
01405     }
01406   else if (xtensa_window_interrupt_insn (gdbarch, pc))
01407     {
01408       /* Execution stopped inside Xtensa Window Interrupt Handler.  */
01409 
01410       xtensa_window_interrupt_frame_cache (this_frame, cache, pc);
01411       /* Everything was set already,  including cache->base.  */
01412       return cache;
01413     }
01414   else  /* Call0 framework.  */
01415     {
01416       call0_frame_cache (this_frame, cache, pc);  
01417       fp_regnum = cache->c0.fp_regnum;
01418     }
01419 
01420   cache->base = get_frame_register_unsigned (this_frame, fp_regnum);
01421 
01422   return cache;
01423 }
01424 
01425 static int xtensa_session_once_reported = 1;
01426 
01427 /* Report a problem with prologue analysis while doing backtracing.
01428    But, do it only once to avoid annoyng repeated messages.  */
01429 
01430 static void
01431 warning_once (void)
01432 {
01433   if (xtensa_session_once_reported == 0)
01434     warning (_("\
01435 \nUnrecognised function prologue. Stack trace cannot be resolved. \
01436 This message will not be repeated in this session.\n"));
01437 
01438   xtensa_session_once_reported = 1;
01439 }
01440 
01441 
01442 static void
01443 xtensa_frame_this_id (struct frame_info *this_frame,
01444                       void **this_cache,
01445                       struct frame_id *this_id)
01446 {
01447   struct xtensa_frame_cache *cache =
01448     xtensa_frame_cache (this_frame, this_cache);
01449 
01450   if (cache->prev_sp == 0)
01451     return;
01452 
01453   (*this_id) = frame_id_build (cache->prev_sp, cache->pc);
01454 }
01455 
01456 static struct value *
01457 xtensa_frame_prev_register (struct frame_info *this_frame,
01458                             void **this_cache,
01459                             int regnum)
01460 {
01461   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01462   struct xtensa_frame_cache *cache;
01463   ULONGEST saved_reg = 0;
01464   int done = 1;
01465 
01466   if (*this_cache == NULL)
01467     *this_cache = xtensa_frame_cache (this_frame, this_cache);
01468   cache = *this_cache;
01469 
01470   if (regnum ==gdbarch_pc_regnum (gdbarch))
01471     saved_reg = cache->ra;
01472   else if (regnum == gdbarch_tdep (gdbarch)->a0_base + 1)
01473     saved_reg = cache->prev_sp;
01474   else if (!cache->call0)
01475     {
01476       if (regnum == gdbarch_tdep (gdbarch)->ws_regnum)
01477         saved_reg = cache->wd.ws;
01478       else if (regnum == gdbarch_tdep (gdbarch)->wb_regnum)
01479         saved_reg = cache->wd.wb;
01480       else if (regnum == gdbarch_ps_regnum (gdbarch))
01481         saved_reg = cache->ps;
01482       else
01483         done = 0;
01484     }
01485   else
01486     done = 0;
01487 
01488   if (done)
01489     return frame_unwind_got_constant (this_frame, regnum, saved_reg);
01490 
01491   if (!cache->call0) /* Windowed ABI.  */
01492     {
01493       /* Convert A-register numbers to AR-register numbers,
01494          if we deal with A-register.  */
01495       if (regnum >= gdbarch_tdep (gdbarch)->a0_base
01496           && regnum <= gdbarch_tdep (gdbarch)->a0_base + 15)
01497         regnum = arreg_number (gdbarch, regnum, cache->wd.wb);
01498 
01499       /* Check, if we deal with AR-register saved on stack.  */
01500       if (regnum >= gdbarch_tdep (gdbarch)->ar_base
01501           && regnum <= (gdbarch_tdep (gdbarch)->ar_base
01502                          + gdbarch_tdep (gdbarch)->num_aregs))
01503         {
01504           int areg = areg_number (gdbarch, regnum, cache->wd.wb);
01505 
01506           if (areg >= 0
01507               && areg < XTENSA_NUM_SAVED_AREGS
01508               && cache->wd.aregs[areg] != -1)
01509             return frame_unwind_got_memory (this_frame, regnum,
01510                                             cache->wd.aregs[areg]);
01511         }
01512     }
01513   else /* Call0 ABI.  */
01514     {
01515       int reg = (regnum >= gdbarch_tdep (gdbarch)->ar_base
01516                 && regnum <= (gdbarch_tdep (gdbarch)->ar_base
01517                                + C0_NREGS))
01518                   ? regnum - gdbarch_tdep (gdbarch)->ar_base : regnum;
01519 
01520       if (reg < C0_NREGS)
01521         {
01522           CORE_ADDR spe;
01523           int stkofs;
01524 
01525           /* If register was saved in the prologue, retrieve it.  */
01526           stkofs = cache->c0.c0_rt[reg].to_stk;
01527           if (stkofs != C0_NOSTK)
01528             {
01529               /* Determine SP on entry based on FP.  */
01530               spe = cache->c0.c0_fp
01531                 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
01532 
01533               return frame_unwind_got_memory (this_frame, regnum,
01534                                               spe + stkofs);
01535             }
01536         }
01537     }
01538 
01539   /* All other registers have been either saved to
01540      the stack or are still alive in the processor.  */
01541 
01542   return frame_unwind_got_register (this_frame, regnum, regnum);
01543 }
01544 
01545 
01546 static const struct frame_unwind
01547 xtensa_unwind =
01548 {
01549   NORMAL_FRAME,
01550   default_frame_unwind_stop_reason,
01551   xtensa_frame_this_id,
01552   xtensa_frame_prev_register,
01553   NULL,
01554   default_frame_sniffer
01555 };
01556 
01557 static CORE_ADDR
01558 xtensa_frame_base_address (struct frame_info *this_frame, void **this_cache)
01559 {
01560   struct xtensa_frame_cache *cache =
01561     xtensa_frame_cache (this_frame, this_cache);
01562 
01563   return cache->base;
01564 }
01565 
01566 static const struct frame_base
01567 xtensa_frame_base =
01568 {
01569   &xtensa_unwind,
01570   xtensa_frame_base_address,
01571   xtensa_frame_base_address,
01572   xtensa_frame_base_address
01573 };
01574 
01575 
01576 static void
01577 xtensa_extract_return_value (struct type *type,
01578                              struct regcache *regcache,
01579                              void *dst)
01580 {
01581   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01582   bfd_byte *valbuf = dst;
01583   int len = TYPE_LENGTH (type);
01584   ULONGEST pc, wb;
01585   int callsize, areg;
01586   int offset = 0;
01587 
01588   DEBUGTRACE ("xtensa_extract_return_value (...)\n");
01589 
01590   gdb_assert(len > 0);
01591 
01592   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
01593     {
01594       /* First, we have to find the caller window in the register file.  */
01595       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
01596       callsize = extract_call_winsize (gdbarch, pc);
01597 
01598       /* On Xtensa, we can return up to 4 words (or 2 for call12).  */
01599       if (len > (callsize > 8 ? 8 : 16))
01600         internal_error (__FILE__, __LINE__,
01601                         _("cannot extract return value of %d bytes long"),
01602                         len);
01603 
01604       /* Get the register offset of the return
01605          register (A2) in the caller window.  */
01606       regcache_raw_read_unsigned
01607         (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
01608       areg = arreg_number (gdbarch,
01609                           gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
01610     }
01611   else
01612     {
01613       /* No windowing hardware - Call0 ABI.  */
01614       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
01615     }
01616 
01617   DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
01618 
01619   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01620     offset = 4 - len;
01621 
01622   for (; len > 0; len -= 4, areg++, valbuf += 4)
01623     {
01624       if (len < 4)
01625         regcache_raw_read_part (regcache, areg, offset, len, valbuf);
01626       else
01627         regcache_raw_read (regcache, areg, valbuf);
01628     }
01629 }
01630 
01631 
01632 static void
01633 xtensa_store_return_value (struct type *type,
01634                            struct regcache *regcache,
01635                            const void *dst)
01636 {
01637   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01638   const bfd_byte *valbuf = dst;
01639   unsigned int areg;
01640   ULONGEST pc, wb;
01641   int callsize;
01642   int len = TYPE_LENGTH (type);
01643   int offset = 0;
01644 
01645   DEBUGTRACE ("xtensa_store_return_value (...)\n");
01646 
01647   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
01648     {
01649       regcache_raw_read_unsigned 
01650         (regcache, gdbarch_tdep (gdbarch)->wb_regnum, &wb);
01651       regcache_raw_read_unsigned (regcache, gdbarch_pc_regnum (gdbarch), &pc);
01652       callsize = extract_call_winsize (gdbarch, pc);
01653 
01654       if (len > (callsize > 8 ? 8 : 16))
01655         internal_error (__FILE__, __LINE__,
01656                         _("unimplemented for this length: %d"),
01657                         TYPE_LENGTH (type));
01658       areg = arreg_number (gdbarch,
01659                            gdbarch_tdep (gdbarch)->a0_base + 2 + callsize, wb);
01660 
01661       DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
01662               callsize, (int) wb);
01663     }
01664   else
01665     {
01666       areg = gdbarch_tdep (gdbarch)->a0_base + C0_ARGS;
01667     }
01668 
01669   if (len < 4 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01670     offset = 4 - len;
01671 
01672   for (; len > 0; len -= 4, areg++, valbuf += 4)
01673     {
01674       if (len < 4)
01675         regcache_raw_write_part (regcache, areg, offset, len, valbuf);
01676       else
01677         regcache_raw_write (regcache, areg, valbuf);
01678     }
01679 }
01680 
01681 
01682 static enum return_value_convention
01683 xtensa_return_value (struct gdbarch *gdbarch,
01684                      struct value *function,
01685                      struct type *valtype,
01686                      struct regcache *regcache,
01687                      gdb_byte *readbuf,
01688                      const gdb_byte *writebuf)
01689 {
01690   /* Structures up to 16 bytes are returned in registers.  */
01691 
01692   int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
01693                         || TYPE_CODE (valtype) == TYPE_CODE_UNION
01694                         || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
01695                        && TYPE_LENGTH (valtype) > 16);
01696 
01697   if (struct_return)
01698     return RETURN_VALUE_STRUCT_CONVENTION;
01699 
01700   DEBUGTRACE ("xtensa_return_value(...)\n");
01701 
01702   if (writebuf != NULL)
01703     {
01704       xtensa_store_return_value (valtype, regcache, writebuf);
01705     }
01706 
01707   if (readbuf != NULL)
01708     {
01709       gdb_assert (!struct_return);
01710       xtensa_extract_return_value (valtype, regcache, readbuf);
01711     }
01712   return RETURN_VALUE_REGISTER_CONVENTION;
01713 }
01714 
01715 
01716 /* DUMMY FRAME */
01717 
01718 static CORE_ADDR
01719 xtensa_push_dummy_call (struct gdbarch *gdbarch,
01720                         struct value *function,
01721                         struct regcache *regcache,
01722                         CORE_ADDR bp_addr,
01723                         int nargs,
01724                         struct value **args,
01725                         CORE_ADDR sp,
01726                         int struct_return,
01727                         CORE_ADDR struct_addr)
01728 {
01729   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01730   int i;
01731   int size, onstack_size;
01732   gdb_byte *buf = (gdb_byte *) alloca (16);
01733   CORE_ADDR ra, ps;
01734   struct argument_info
01735   {
01736     const bfd_byte *contents;
01737     int length;
01738     int onstack;                /* onstack == 0 => in reg */
01739     int align;                  /* alignment */
01740     union
01741     {
01742       int offset;               /* stack offset if on stack.  */
01743       int regno;                /* regno if in register.  */
01744     } u;
01745   };
01746 
01747   struct argument_info *arg_info =
01748     (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
01749 
01750   CORE_ADDR osp = sp;
01751 
01752   DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
01753 
01754   if (xtensa_debug_level > 3)
01755     {
01756       int i;
01757       DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
01758       DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
01759                  "struct_addr=0x%x\n",
01760                  (int) sp, (int) struct_return, (int) struct_addr);
01761 
01762       for (i = 0; i < nargs; i++)
01763         {
01764           struct value *arg = args[i];
01765           struct type *arg_type = check_typedef (value_type (arg));
01766           fprintf_unfiltered (gdb_stdlog, "%2d: %s %3d ", i,
01767                               host_address_to_string (arg),
01768                               TYPE_LENGTH (arg_type));
01769           switch (TYPE_CODE (arg_type))
01770             {
01771             case TYPE_CODE_INT:
01772               fprintf_unfiltered (gdb_stdlog, "int");
01773               break;
01774             case TYPE_CODE_STRUCT:
01775               fprintf_unfiltered (gdb_stdlog, "struct");
01776               break;
01777             default:
01778               fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
01779               break;
01780             }
01781           fprintf_unfiltered (gdb_stdlog, " %s\n",
01782                               host_address_to_string (value_contents (arg)));
01783         }
01784     }
01785 
01786   /* First loop: collect information.
01787      Cast into type_long.  (This shouldn't happen often for C because
01788      GDB already does this earlier.)  It's possible that GDB could
01789      do it all the time but it's harmless to leave this code here.  */
01790 
01791   size = 0;
01792   onstack_size = 0;
01793   i = 0;
01794 
01795   if (struct_return)
01796     size = REGISTER_SIZE;
01797 
01798   for (i = 0; i < nargs; i++)
01799     {
01800       struct argument_info *info = &arg_info[i];
01801       struct value *arg = args[i];
01802       struct type *arg_type = check_typedef (value_type (arg));
01803 
01804       switch (TYPE_CODE (arg_type))
01805         {
01806         case TYPE_CODE_INT:
01807         case TYPE_CODE_BOOL:
01808         case TYPE_CODE_CHAR:
01809         case TYPE_CODE_RANGE:
01810         case TYPE_CODE_ENUM:
01811 
01812           /* Cast argument to long if necessary as the mask does it too.  */
01813           if (TYPE_LENGTH (arg_type)
01814               < TYPE_LENGTH (builtin_type (gdbarch)->builtin_long))
01815             {
01816               arg_type = builtin_type (gdbarch)->builtin_long;
01817               arg = value_cast (arg_type, arg);
01818             }
01819           /* Aligment is equal to the type length for the basic types.  */
01820           info->align = TYPE_LENGTH (arg_type);
01821           break;
01822 
01823         case TYPE_CODE_FLT:
01824 
01825           /* Align doubles correctly.  */
01826           if (TYPE_LENGTH (arg_type)
01827               == TYPE_LENGTH (builtin_type (gdbarch)->builtin_double))
01828             info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_double);
01829           else
01830             info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
01831           break;
01832 
01833         case TYPE_CODE_STRUCT:
01834         default:
01835           info->align = TYPE_LENGTH (builtin_type (gdbarch)->builtin_long);
01836           break;
01837         }
01838       info->length = TYPE_LENGTH (arg_type);
01839       info->contents = value_contents (arg);
01840 
01841       /* Align size and onstack_size.  */
01842       size = (size + info->align - 1) & ~(info->align - 1);
01843       onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
01844 
01845       if (size + info->length > REGISTER_SIZE * ARG_NOF (gdbarch))
01846         {
01847           info->onstack = 1;
01848           info->u.offset = onstack_size;
01849           onstack_size += info->length;
01850         }
01851       else
01852         {
01853           info->onstack = 0;
01854           info->u.regno = ARG_1ST (gdbarch) + size / REGISTER_SIZE;
01855         }
01856       size += info->length;
01857     }
01858 
01859   /* Adjust the stack pointer and align it.  */
01860   sp = align_down (sp - onstack_size, SP_ALIGNMENT);
01861 
01862   /* Simulate MOVSP, if Windowed ABI.  */
01863   if ((gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
01864       && (sp != osp))
01865     {
01866       read_memory (osp - 16, buf, 16);
01867       write_memory (sp - 16, buf, 16);
01868     }
01869 
01870   /* Second Loop: Load arguments.  */
01871 
01872   if (struct_return)
01873     {
01874       store_unsigned_integer (buf, REGISTER_SIZE, byte_order, struct_addr);
01875       regcache_cooked_write (regcache, ARG_1ST (gdbarch), buf);
01876     }
01877 
01878   for (i = 0; i < nargs; i++)
01879     {
01880       struct argument_info *info = &arg_info[i];
01881 
01882       if (info->onstack)
01883         {
01884           int n = info->length;
01885           CORE_ADDR offset = sp + info->u.offset;
01886 
01887           /* Odd-sized structs are aligned to the lower side of a memory
01888              word in big-endian mode and require a shift.  This only
01889              applies for structures smaller than one word.  */
01890 
01891           if (n < REGISTER_SIZE
01892               && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
01893             offset += (REGISTER_SIZE - n);
01894 
01895           write_memory (offset, info->contents, info->length);
01896 
01897         }
01898       else
01899         {
01900           int n = info->length;
01901           const bfd_byte *cp = info->contents;
01902           int r = info->u.regno;
01903 
01904           /* Odd-sized structs are aligned to the lower side of registers in
01905              big-endian mode and require a shift.  The odd-sized leftover will
01906              be at the end.  Note that this is only true for structures smaller
01907              than REGISTER_SIZE; for larger odd-sized structures the excess
01908              will be left-aligned in the register on both endiannesses.  */
01909 
01910           if (n < REGISTER_SIZE && byte_order == BFD_ENDIAN_BIG)
01911             {
01912               ULONGEST v;
01913               v = extract_unsigned_integer (cp, REGISTER_SIZE, byte_order);
01914               v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
01915 
01916               store_unsigned_integer (buf, REGISTER_SIZE, byte_order, v);
01917               regcache_cooked_write (regcache, r, buf);
01918 
01919               cp += REGISTER_SIZE;
01920               n -= REGISTER_SIZE;
01921               r++;
01922             }
01923           else
01924             while (n > 0)
01925               {
01926                 regcache_cooked_write (regcache, r, cp);
01927 
01928                 cp += REGISTER_SIZE;
01929                 n -= REGISTER_SIZE;
01930                 r++;
01931               }
01932         }
01933     }
01934 
01935   /* Set the return address of dummy frame to the dummy address.
01936      The return address for the current function (in A0) is
01937      saved in the dummy frame, so we can savely overwrite A0 here.  */
01938 
01939   if (gdbarch_tdep (gdbarch)->call_abi != CallAbiCall0Only)
01940     {
01941       ULONGEST val;
01942 
01943       ra = (bp_addr & 0x3fffffff) | 0x40000000;
01944       regcache_raw_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch), &val);
01945       ps = (unsigned long) val & ~0x00030000;
01946       regcache_cooked_write_unsigned
01947         (regcache, gdbarch_tdep (gdbarch)->a0_base + 4, ra);
01948       regcache_cooked_write_unsigned (regcache,
01949                                       gdbarch_ps_regnum (gdbarch),
01950                                       ps | 0x00010000);
01951 
01952       /* All the registers have been saved.  After executing
01953          dummy call, they all will be restored.  So it's safe
01954          to modify WINDOWSTART register to make it look like there
01955          is only one register window corresponding to WINDOWEBASE.  */
01956 
01957       regcache_raw_read (regcache, gdbarch_tdep (gdbarch)->wb_regnum, buf);
01958       regcache_cooked_write_unsigned
01959         (regcache, gdbarch_tdep (gdbarch)->ws_regnum,
01960          1 << extract_unsigned_integer (buf, 4, byte_order));
01961     }
01962   else
01963     {
01964       /* Simulate CALL0: write RA into A0 register.  */
01965       regcache_cooked_write_unsigned
01966         (regcache, gdbarch_tdep (gdbarch)->a0_base, bp_addr);
01967     }
01968 
01969   /* Set new stack pointer and return it.  */
01970   regcache_cooked_write_unsigned (regcache,
01971                                   gdbarch_tdep (gdbarch)->a0_base + 1, sp);
01972   /* Make dummy frame ID unique by adding a constant.  */
01973   return sp + SP_ALIGNMENT;
01974 }
01975 
01976 
01977 /* Return a breakpoint for the current location of PC.  We always use
01978    the density version if we have density instructions (regardless of the
01979    current instruction at PC), and use regular instructions otherwise.  */
01980 
01981 #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
01982 #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
01983 #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
01984 #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
01985 
01986 static const unsigned char *
01987 xtensa_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
01988                            int *lenptr)
01989 {
01990   static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
01991   static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
01992   static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
01993   static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
01994 
01995   DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
01996 
01997   if (gdbarch_tdep (gdbarch)->isa_use_density_instructions)
01998     {
01999       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
02000         {
02001           *lenptr = sizeof (density_big_breakpoint);
02002           return density_big_breakpoint;
02003         }
02004       else
02005         {
02006           *lenptr = sizeof (density_little_breakpoint);
02007           return density_little_breakpoint;
02008         }
02009     }
02010   else
02011     {
02012       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
02013         {
02014           *lenptr = sizeof (big_breakpoint);
02015           return big_breakpoint;
02016         }
02017       else
02018         {
02019           *lenptr = sizeof (little_breakpoint);
02020           return little_breakpoint;
02021         }
02022     }
02023 }
02024 
02025 /* Call0 ABI support routines.  */
02026 
02027 /* Return true, if PC points to "ret" or "ret.n".  */ 
02028 
02029 static int
02030 call0_ret (CORE_ADDR start_pc, CORE_ADDR finish_pc)
02031 {
02032 #define RETURN_RET goto done
02033   xtensa_isa isa;
02034   xtensa_insnbuf ins, slot;
02035   gdb_byte ibuf[XTENSA_ISA_BSZ];
02036   CORE_ADDR ia, bt, ba;
02037   xtensa_format ifmt;
02038   int ilen, islots, is;
02039   xtensa_opcode opc;
02040   const char *opcname;
02041   int found_ret = 0;
02042 
02043   isa = xtensa_default_isa;
02044   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
02045   ins = xtensa_insnbuf_alloc (isa);
02046   slot = xtensa_insnbuf_alloc (isa);
02047   ba = 0;
02048 
02049   for (ia = start_pc, bt = ia; ia < finish_pc ; ia += ilen)
02050     {
02051       if (ia + xtensa_isa_maxlength (isa) > bt)
02052         {
02053           ba = ia;
02054           bt = (ba + XTENSA_ISA_BSZ) < finish_pc
02055             ? ba + XTENSA_ISA_BSZ : finish_pc;
02056           if (target_read_memory (ba, ibuf, bt - ba) != 0 )
02057             RETURN_RET;
02058         }
02059 
02060       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
02061       ifmt = xtensa_format_decode (isa, ins);
02062       if (ifmt == XTENSA_UNDEFINED)
02063         RETURN_RET;
02064       ilen = xtensa_format_length (isa, ifmt);
02065       if (ilen == XTENSA_UNDEFINED)
02066         RETURN_RET;
02067       islots = xtensa_format_num_slots (isa, ifmt);
02068       if (islots == XTENSA_UNDEFINED)
02069         RETURN_RET;
02070       
02071       for (is = 0; is < islots; ++is)
02072         {
02073           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
02074             RETURN_RET;
02075           
02076           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
02077           if (opc == XTENSA_UNDEFINED) 
02078             RETURN_RET;
02079           
02080           opcname = xtensa_opcode_name (isa, opc);
02081           
02082           if ((strcasecmp (opcname, "ret.n") == 0)
02083               || (strcasecmp (opcname, "ret") == 0))
02084             {
02085               found_ret = 1;
02086               RETURN_RET;
02087             }
02088         }
02089     }
02090  done:
02091   xtensa_insnbuf_free(isa, slot);
02092   xtensa_insnbuf_free(isa, ins);
02093   return found_ret;
02094 }
02095 
02096 /* Call0 opcode class.  Opcodes are preclassified according to what they
02097    mean for Call0 prologue analysis, and their number of significant operands.
02098    The purpose of this is to simplify prologue analysis by separating 
02099    instruction decoding (libisa) from the semantics of prologue analysis.  */
02100 
02101 typedef enum
02102 {
02103   c0opc_illegal,       /* Unknown to libisa (invalid) or 'ill' opcode.  */
02104   c0opc_uninteresting, /* Not interesting for Call0 prologue analysis.  */
02105   c0opc_flow,          /* Flow control insn.  */
02106   c0opc_entry,         /* ENTRY indicates non-Call0 prologue.  */
02107   c0opc_break,         /* Debugger software breakpoints.  */
02108   c0opc_add,           /* Adding two registers.  */
02109   c0opc_addi,          /* Adding a register and an immediate.  */
02110   c0opc_and,           /* Bitwise "and"-ing two registers.  */
02111   c0opc_sub,           /* Subtracting a register from a register.  */
02112   c0opc_mov,           /* Moving a register to a register.  */
02113   c0opc_movi,          /* Moving an immediate to a register.  */
02114   c0opc_l32r,          /* Loading a literal.  */
02115   c0opc_s32i,          /* Storing word at fixed offset from a base register.  */
02116   c0opc_rwxsr,         /* RSR, WRS, or XSR instructions.  */
02117   c0opc_l32e,          /* L32E instruction.  */
02118   c0opc_s32e,          /* S32E instruction.  */
02119   c0opc_rfwo,          /* RFWO instruction.  */
02120   c0opc_rfwu,          /* RFWU instruction.  */
02121   c0opc_NrOf           /* Number of opcode classifications.  */
02122 } xtensa_insn_kind;
02123 
02124 /* Return true,  if OPCNAME is RSR,  WRS,  or XSR instruction.  */
02125 
02126 static int
02127 rwx_special_register (const char *opcname)
02128 {
02129   char ch = *opcname++;
02130   
02131   if ((ch != 'r') && (ch != 'w') && (ch != 'x'))
02132     return 0;
02133   if (*opcname++ != 's')
02134     return 0;
02135   if (*opcname++ != 'r')
02136     return 0;
02137   if (*opcname++ != '.')
02138     return 0;
02139 
02140   return 1;
02141 }
02142 
02143 /* Classify an opcode based on what it means for Call0 prologue analysis.  */
02144 
02145 static xtensa_insn_kind
02146 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
02147 {
02148   const char *opcname;
02149   xtensa_insn_kind opclass = c0opc_uninteresting;
02150 
02151   DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
02152 
02153   /* Get opcode name and handle special classifications.  */
02154 
02155   opcname = xtensa_opcode_name (isa, opc);
02156 
02157   if (opcname == NULL 
02158       || strcasecmp (opcname, "ill") == 0
02159       || strcasecmp (opcname, "ill.n") == 0)
02160     opclass = c0opc_illegal;
02161   else if (strcasecmp (opcname, "break") == 0
02162            || strcasecmp (opcname, "break.n") == 0)
02163      opclass = c0opc_break;
02164   else if (strcasecmp (opcname, "entry") == 0)
02165     opclass = c0opc_entry;
02166   else if (strcasecmp (opcname, "rfwo") == 0)
02167     opclass = c0opc_rfwo;
02168   else if (strcasecmp (opcname, "rfwu") == 0)
02169     opclass = c0opc_rfwu;
02170   else if (xtensa_opcode_is_branch (isa, opc) > 0
02171            || xtensa_opcode_is_jump   (isa, opc) > 0
02172            || xtensa_opcode_is_loop   (isa, opc) > 0
02173            || xtensa_opcode_is_call   (isa, opc) > 0
02174            || strcasecmp (opcname, "simcall") == 0
02175            || strcasecmp (opcname, "syscall") == 0)
02176     opclass = c0opc_flow;
02177 
02178   /* Also, classify specific opcodes that need to be tracked.  */
02179   else if (strcasecmp (opcname, "add") == 0 
02180            || strcasecmp (opcname, "add.n") == 0)
02181     opclass = c0opc_add;
02182   else if (strcasecmp (opcname, "and") == 0)
02183     opclass = c0opc_and;
02184   else if (strcasecmp (opcname, "addi") == 0 
02185            || strcasecmp (opcname, "addi.n") == 0
02186            || strcasecmp (opcname, "addmi") == 0)
02187     opclass = c0opc_addi;
02188   else if (strcasecmp (opcname, "sub") == 0)
02189     opclass = c0opc_sub;
02190   else if (strcasecmp (opcname, "mov.n") == 0
02191            || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro.  */
02192     opclass = c0opc_mov;
02193   else if (strcasecmp (opcname, "movi") == 0 
02194            || strcasecmp (opcname, "movi.n") == 0)
02195     opclass = c0opc_movi;
02196   else if (strcasecmp (opcname, "l32r") == 0)
02197     opclass = c0opc_l32r;
02198   else if (strcasecmp (opcname, "s32i") == 0 
02199            || strcasecmp (opcname, "s32i.n") == 0)
02200     opclass = c0opc_s32i;
02201   else if (strcasecmp (opcname, "l32e") == 0)
02202     opclass = c0opc_l32e;
02203   else if (strcasecmp (opcname, "s32e") == 0)
02204     opclass = c0opc_s32e;
02205   else if (rwx_special_register (opcname))
02206     opclass = c0opc_rwxsr;
02207 
02208   return opclass;
02209 }
02210 
02211 /* Tracks register movement/mutation for a given operation, which may
02212    be within a bundle.  Updates the destination register tracking info
02213    accordingly.  The pc is needed only for pc-relative load instructions
02214    (eg. l32r).  The SP register number is needed to identify stores to
02215    the stack frame.  Returns 0, if analysis was succesfull, non-zero
02216    otherwise.  */
02217 
02218 static int
02219 call0_track_op (struct gdbarch *gdbarch, xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
02220                 xtensa_insn_kind opclass, int nods, unsigned odv[],
02221                 CORE_ADDR pc, int spreg, xtensa_frame_cache_t *cache)
02222 {
02223   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02224   unsigned litbase, litaddr, litval;
02225 
02226   switch (opclass)
02227     {
02228     case c0opc_addi:
02229       /* 3 operands: dst, src, imm.  */
02230       gdb_assert (nods == 3);
02231       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
02232       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
02233       break;
02234     case c0opc_add:
02235       /* 3 operands: dst, src1, src2.  */
02236       gdb_assert (nods == 3); 
02237       if      (src[odv[1]].fr_reg == C0_CONST)
02238         {
02239           dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
02240           dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
02241         }
02242       else if (src[odv[2]].fr_reg == C0_CONST)
02243         {
02244           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
02245           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
02246         }
02247       else dst[odv[0]].fr_reg = C0_INEXP;
02248       break;
02249     case c0opc_and:
02250       /* 3 operands:  dst, src1, src2.  */
02251       gdb_assert (nods == 3);
02252       if (cache->c0.c0_fpalign == 0)
02253         {
02254           /* Handle dynamic stack alignment.  */
02255           if ((src[odv[0]].fr_reg == spreg) && (src[odv[1]].fr_reg == spreg))
02256             {
02257               if (src[odv[2]].fr_reg == C0_CONST)
02258                 cache->c0.c0_fpalign = src[odv[2]].fr_ofs;
02259               break;
02260             }
02261           else if ((src[odv[0]].fr_reg == spreg)
02262                    && (src[odv[2]].fr_reg == spreg))
02263             {
02264               if (src[odv[1]].fr_reg == C0_CONST)
02265                 cache->c0.c0_fpalign = src[odv[1]].fr_ofs;
02266               break;
02267             }
02268           /* else fall through.  */
02269         }
02270       if      (src[odv[1]].fr_reg == C0_CONST)
02271         {
02272           dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
02273           dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs & src[odv[1]].fr_ofs;
02274         }
02275       else if (src[odv[2]].fr_reg == C0_CONST)
02276         {
02277           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
02278           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs & src[odv[2]].fr_ofs;
02279         }
02280       else dst[odv[0]].fr_reg = C0_INEXP;
02281       break;
02282     case c0opc_sub:
02283       /* 3 operands: dst, src1, src2.  */
02284       gdb_assert (nods == 3);
02285       if      (src[odv[2]].fr_reg == C0_CONST)
02286         {
02287           dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
02288           dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
02289         }
02290       else dst[odv[0]].fr_reg = C0_INEXP;
02291       break;
02292     case c0opc_mov:
02293       /* 2 operands: dst, src [, src].  */
02294       gdb_assert (nods == 2);
02295       /* First, check if it's a special case of saving unaligned SP
02296          to a spare register in case of dynamic stack adjustment.
02297          But, only do it one time.  The second time could be initializing
02298          frame pointer.  We don't want to overwrite the first one.  */
02299       if ((odv[1] == spreg) && (cache->c0.c0_old_sp == C0_INEXP))
02300         cache->c0.c0_old_sp = odv[0];
02301 
02302       dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
02303       dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
02304       break;
02305     case c0opc_movi:
02306       /* 2 operands: dst, imm.  */
02307       gdb_assert (nods == 2);
02308       dst[odv[0]].fr_reg = C0_CONST;
02309       dst[odv[0]].fr_ofs = odv[1];
02310       break;
02311     case c0opc_l32r:
02312       /* 2 operands: dst, literal offset.  */
02313       gdb_assert (nods == 2);
02314       /* litbase = xtensa_get_litbase (pc);  can be also used.  */
02315       litbase = (gdbarch_tdep (gdbarch)->litbase_regnum == -1)
02316         ? 0 : xtensa_read_register
02317                 (gdbarch_tdep (gdbarch)->litbase_regnum);
02318       litaddr = litbase & 1
02319                   ? (litbase & ~1) + (signed)odv[1]
02320                   : (pc + 3  + (signed)odv[1]) & ~3;
02321       litval = read_memory_integer (litaddr, 4, byte_order);
02322       dst[odv[0]].fr_reg = C0_CONST;
02323       dst[odv[0]].fr_ofs = litval;
02324       break;
02325     case c0opc_s32i:
02326       /* 3 operands: value, base, offset.  */
02327       gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
02328       /* First, check if it's a spill for saved unaligned SP,
02329          when dynamic stack adjustment was applied to this frame.  */
02330       if ((cache->c0.c0_fpalign != 0)           /* Dynamic stack adjustment.  */
02331           && (odv[1] == spreg)                  /* SP usage indicates spill.  */
02332           && (odv[0] == cache->c0.c0_old_sp))   /* Old SP register spilled.  */
02333         cache->c0.c0_sp_ofs = odv[2];
02334 
02335       if (src[odv[1]].fr_reg == spreg        /* Store to stack frame.  */
02336           && (src[odv[1]].fr_ofs & 3) == 0   /* Alignment preserved.  */
02337           &&  src[odv[0]].fr_reg >= 0        /* Value is from a register.  */
02338           &&  src[odv[0]].fr_ofs == 0        /* Value hasn't been modified.  */
02339           &&  src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time.  */
02340         {
02341           /* ISA encoding guarantees alignment.  But, check it anyway.  */
02342           gdb_assert ((odv[2] & 3) == 0);
02343           dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
02344         }
02345       break;
02346       /* If we end up inside Window Overflow / Underflow interrupt handler
02347          report an error because these handlers should have been handled
02348          already in a different way.  */
02349     case c0opc_l32e:
02350     case c0opc_s32e:
02351     case c0opc_rfwo:
02352     case c0opc_rfwu:
02353       return 1;
02354     default:
02355       return 1;
02356     }
02357   return 0;
02358 }
02359 
02360 /* Analyze prologue of the function at start address to determine if it uses
02361    the Call0 ABI, and if so track register moves and linear modifications
02362    in the prologue up to the PC or just beyond the prologue, whichever is
02363    first. An 'entry' instruction indicates non-Call0 ABI and the end of the
02364    prologue. The prologue may overlap non-prologue instructions but is
02365    guaranteed to end by the first flow-control instruction (jump, branch,
02366    call or return).  Since an optimized function may move information around
02367    and change the stack frame arbitrarily during the prologue, the information
02368    is guaranteed valid only at the point in the function indicated by the PC.
02369    May be used to skip the prologue or identify the ABI, w/o tracking.
02370 
02371    Returns:   Address of first instruction after prologue, or PC (whichever 
02372               is first), or 0, if decoding failed (in libisa).
02373    Input args:
02374       start   Start address of function/prologue.
02375       pc      Program counter to stop at.  Use 0 to continue to end of prologue.
02376               If 0, avoids infinite run-on in corrupt code memory by bounding
02377               the scan to the end of the function if that can be determined.
02378       nregs   Number of general registers to track.
02379    InOut args:
02380       cache   Xtensa frame cache.
02381 
02382       Note that these may produce useful results even if decoding fails
02383       because they begin with default assumptions that analysis may change.  */
02384 
02385 static CORE_ADDR
02386 call0_analyze_prologue (struct gdbarch *gdbarch,
02387                         CORE_ADDR start, CORE_ADDR pc,
02388                         int nregs, xtensa_frame_cache_t *cache)
02389 {
02390   CORE_ADDR ia;             /* Current insn address in prologue.  */
02391   CORE_ADDR ba = 0;         /* Current address at base of insn buffer.  */
02392   CORE_ADDR bt;             /* Current address at top+1 of insn buffer.  */
02393   gdb_byte ibuf[XTENSA_ISA_BSZ];/* Instruction buffer for decoding prologue.  */
02394   xtensa_isa isa;           /* libisa ISA handle.  */
02395   xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot.  */
02396   xtensa_format ifmt;       /* libisa instruction format.  */
02397   int ilen, islots, is;     /* Instruction length, nbr slots, current slot.  */
02398   xtensa_opcode opc;        /* Opcode in current slot.  */
02399   xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis.  */
02400   int nods;                 /* Opcode number of operands.  */
02401   unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa.  */
02402   xtensa_c0reg_t *rtmp;     /* Register tracking info snapshot.  */
02403   int j;                    /* General loop counter.  */
02404   int fail = 0;             /* Set non-zero and exit, if decoding fails.  */
02405   CORE_ADDR body_pc;        /* The PC for the first non-prologue insn.  */
02406   CORE_ADDR end_pc;         /* The PC for the lust function insn.  */
02407 
02408   struct symtab_and_line prologue_sal;
02409 
02410   DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n", 
02411               (int)start, (int)pc);
02412 
02413   /* Try to limit the scan to the end of the function if a non-zero pc
02414      arg was not supplied to avoid probing beyond the end of valid memory.
02415      If memory is full of garbage that classifies as c0opc_uninteresting.
02416      If this fails (eg. if no symbols) pc ends up 0 as it was.
02417      Intialize the Call0 frame and register tracking info.
02418      Assume it's Call0 until an 'entry' instruction is encountered.
02419      Assume we may be in the prologue until we hit a flow control instr.  */
02420 
02421   rtmp = NULL;
02422   body_pc = UINT_MAX;
02423   end_pc = 0;
02424 
02425   /* Find out, if we have an information about the prologue from DWARF.  */
02426   prologue_sal = find_pc_line (start, 0);
02427   if (prologue_sal.line != 0) /* Found debug info.  */
02428     body_pc = prologue_sal.end;
02429 
02430   /* If we are going to analyze the prologue in general without knowing about
02431      the current PC, make the best assumtion for the end of the prologue.  */
02432   if (pc == 0)
02433     {
02434       find_pc_partial_function (start, 0, NULL, &end_pc);
02435       body_pc = min (end_pc, body_pc);
02436     }
02437   else
02438     body_pc = min (pc, body_pc);
02439 
02440   cache->call0 = 1;
02441   rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
02442 
02443   if (!xtensa_default_isa)
02444     xtensa_default_isa = xtensa_isa_init (0, 0);
02445   isa = xtensa_default_isa;
02446   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
02447   ins = xtensa_insnbuf_alloc (isa);
02448   slot = xtensa_insnbuf_alloc (isa);
02449 
02450   for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
02451     {
02452       /* (Re)fill instruction buffer from memory if necessary, but do not
02453          read memory beyond PC to be sure we stay within text section
02454          (this protection only works if a non-zero pc is supplied).  */
02455 
02456       if (ia + xtensa_isa_maxlength (isa) > bt)
02457         {
02458           ba = ia;
02459           bt = (ba + XTENSA_ISA_BSZ) < body_pc ? ba + XTENSA_ISA_BSZ : body_pc;
02460           if (target_read_memory (ba, ibuf, bt - ba) != 0 )
02461             error (_("Unable to read target memory ..."));
02462         }
02463 
02464       /* Decode format information.  */
02465 
02466       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
02467       ifmt = xtensa_format_decode (isa, ins);
02468       if (ifmt == XTENSA_UNDEFINED)
02469         {
02470           fail = 1;
02471           goto done;
02472         }
02473       ilen = xtensa_format_length (isa, ifmt);
02474       if (ilen == XTENSA_UNDEFINED)
02475         {
02476           fail = 1;
02477           goto done;
02478         }
02479       islots = xtensa_format_num_slots (isa, ifmt);
02480       if (islots == XTENSA_UNDEFINED)
02481         {
02482           fail = 1;
02483           goto done;
02484         }
02485 
02486       /* Analyze a bundle or a single instruction, using a snapshot of 
02487          the register tracking info as input for the entire bundle so that
02488          register changes do not take effect within this bundle.  */
02489 
02490       for (j = 0; j < nregs; ++j)
02491         rtmp[j] = cache->c0.c0_rt[j];
02492 
02493       for (is = 0; is < islots; ++is)
02494         {
02495           /* Decode a slot and classify the opcode.  */
02496 
02497           fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
02498           if (fail)
02499             goto done;
02500 
02501           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
02502           DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n", 
02503                      (unsigned)ia, opc);
02504           if (opc == XTENSA_UNDEFINED) 
02505             opclass = c0opc_illegal;
02506           else
02507             opclass = call0_classify_opcode (isa, opc);
02508 
02509           /* Decide whether to track this opcode, ignore it, or bail out.  */
02510 
02511           switch (opclass)
02512             {
02513             case c0opc_illegal:
02514             case c0opc_break:
02515               fail = 1;
02516               goto done;
02517 
02518             case c0opc_uninteresting:
02519               continue;
02520 
02521             case c0opc_flow:  /* Flow control instructions stop analysis.  */
02522             case c0opc_rwxsr: /* RSR, WSR, XSR instructions stop analysis.  */
02523               goto done;
02524 
02525             case c0opc_entry:
02526               cache->call0 = 0;
02527               ia += ilen;               /* Skip over 'entry' insn.  */
02528               goto done;
02529 
02530             default:
02531               cache->call0 = 1;
02532             }
02533 
02534           /* Only expected opcodes should get this far.  */
02535 
02536           /* Extract and decode the operands.  */
02537           nods = xtensa_opcode_num_operands (isa, opc);
02538           if (nods == XTENSA_UNDEFINED)
02539             {
02540               fail = 1;
02541               goto done;
02542             }
02543 
02544           for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
02545             {
02546               fail = xtensa_operand_get_field (isa, opc, j, ifmt, 
02547                                                is, slot, &odv[j]);
02548               if (fail)
02549                 goto done;
02550 
02551               fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
02552               if (fail)
02553                 goto done;
02554             }
02555 
02556           /* Check operands to verify use of 'mov' assembler macro.  */
02557           if (opclass == c0opc_mov && nods == 3)
02558             {
02559               if (odv[2] == odv[1])
02560                 {
02561                   nods = 2;
02562                   if ((odv[0] == 1) && (odv[1] != 1))
02563                     /* OR  A1, An, An  , where n != 1.
02564                        This means we are inside epilogue already.  */
02565                     goto done;
02566                 }
02567               else
02568                 {
02569                   opclass = c0opc_uninteresting;
02570                   continue;
02571                 }
02572             }
02573 
02574           /* Track register movement and modification for this operation.  */
02575           fail = call0_track_op (gdbarch, cache->c0.c0_rt, rtmp,
02576                                  opclass, nods, odv, ia, 1, cache);
02577           if (fail)
02578             goto done;
02579         }
02580     }
02581 done:
02582   DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
02583              (unsigned)ia, fail ? "failed" : "succeeded");
02584   xtensa_insnbuf_free(isa, slot);
02585   xtensa_insnbuf_free(isa, ins);
02586   return fail ? XTENSA_ISA_BADPC : ia;
02587 }
02588 
02589 /* Initialize frame cache for the current frame in CALL0 ABI.  */
02590 
02591 static void
02592 call0_frame_cache (struct frame_info *this_frame,
02593                    xtensa_frame_cache_t *cache, CORE_ADDR pc)
02594 {
02595   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02596   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02597   CORE_ADDR start_pc;           /* The beginning of the function.  */
02598   CORE_ADDR body_pc=UINT_MAX;   /* PC, where prologue analysis stopped.  */
02599   CORE_ADDR sp, fp, ra;
02600   int fp_regnum = C0_SP, c0_hasfp = 0, c0_frmsz = 0, prev_sp = 0, to_stk;
02601  
02602   sp = get_frame_register_unsigned
02603     (this_frame, gdbarch_tdep (gdbarch)->a0_base + 1);
02604   fp = sp; /* Assume FP == SP until proven otherwise.  */
02605 
02606   /* Find the beginning of the prologue of the function containing the PC
02607      and analyze it up to the PC or the end of the prologue.  */
02608 
02609   if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
02610     {
02611       body_pc = call0_analyze_prologue (gdbarch, start_pc, pc, C0_NREGS, cache);
02612 
02613       if (body_pc == XTENSA_ISA_BADPC)
02614         {
02615           warning_once ();
02616           ra = 0;
02617           goto finish_frame_analysis;
02618         }
02619     }
02620   
02621   /* Get the frame information and FP (if used) at the current PC.
02622      If PC is in the prologue, the prologue analysis is more reliable
02623      than DWARF info.  We don't not know for sure, if PC is in the prologue,
02624      but we do know no calls have yet taken place, so we can almost
02625      certainly rely on the prologue analysis.  */
02626 
02627   if (body_pc <= pc)
02628     {
02629       /* Prologue analysis was successful up to the PC.
02630          It includes the cases when PC == START_PC.  */
02631       c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
02632       /* c0_hasfp == true means there is a frame pointer because
02633          we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
02634          was derived from SP.  Otherwise, it would be C0_FP.  */
02635       fp_regnum = c0_hasfp ? C0_FP : C0_SP;
02636       c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
02637       fp_regnum += gdbarch_tdep (gdbarch)->a0_base;
02638     }
02639   else  /* No data from the prologue analysis.  */
02640     {
02641       c0_hasfp = 0;
02642       fp_regnum = gdbarch_tdep (gdbarch)->a0_base + C0_SP;
02643       c0_frmsz = 0;
02644       start_pc = pc;
02645    }
02646 
02647   if (cache->c0.c0_fpalign)
02648     {
02649       /* This frame has a special prologue with a dynamic stack adjustment
02650          to force an alignment, which is bigger than standard 16 bytes.  */
02651 
02652       CORE_ADDR unaligned_sp;
02653 
02654       if (cache->c0.c0_old_sp == C0_INEXP)
02655         /* This can't be.  Prologue code should be consistent.
02656            Unaligned stack pointer should be saved in a spare register.  */
02657         {
02658           warning_once ();
02659           ra = 0;
02660           goto finish_frame_analysis;
02661         }
02662 
02663       if (cache->c0.c0_sp_ofs == C0_NOSTK)
02664         /* Saved unaligned value of SP is kept in a register.  */
02665         unaligned_sp = get_frame_register_unsigned
02666           (this_frame, gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_old_sp);
02667       else
02668         /* Get the value from stack.  */
02669         unaligned_sp = (CORE_ADDR)
02670           read_memory_integer (fp + cache->c0.c0_sp_ofs, 4, byte_order);
02671 
02672       prev_sp = unaligned_sp + c0_frmsz;
02673     }
02674   else
02675     prev_sp = fp + c0_frmsz;
02676 
02677   /* Frame size from debug info or prologue tracking does not account for 
02678      alloca() and other dynamic allocations.  Adjust frame size by FP - SP.  */
02679   if (c0_hasfp)
02680     {
02681       fp = get_frame_register_unsigned (this_frame, fp_regnum);
02682 
02683       /* Update the stack frame size.  */
02684       c0_frmsz += fp - sp;
02685     }
02686 
02687   /* Get the return address (RA) from the stack if saved,
02688      or try to get it from a register.  */
02689 
02690   to_stk = cache->c0.c0_rt[C0_RA].to_stk;
02691   if (to_stk != C0_NOSTK)
02692     ra = (CORE_ADDR) 
02693       read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk,
02694                            4, byte_order);
02695 
02696   else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
02697            && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
02698     {
02699       /* Special case for terminating backtrace at a function that wants to
02700          be seen as the outermost one.  Such a function will clear it's RA (A0)
02701          register to 0 in the prologue instead of saving its original value.  */
02702       ra = 0;
02703     }
02704   else
02705     {
02706       /* RA was copied to another register or (before any function call) may
02707          still be in the original RA register.  This is not always reliable:
02708          even in a leaf function, register tracking stops after prologue, and
02709          even in prologue, non-prologue instructions (not tracked) may overwrite
02710          RA or any register it was copied to.  If likely in prologue or before
02711          any call, use retracking info and hope for the best (compiler should
02712          have saved RA in stack if not in a leaf function).  If not in prologue,
02713          too bad.  */
02714 
02715       int i;
02716       for (i = 0;
02717            (i < C0_NREGS)
02718            && (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
02719            ++i);
02720       if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
02721         i = C0_RA;
02722       if (i < C0_NREGS)
02723         {
02724           ra = get_frame_register_unsigned
02725             (this_frame,
02726              gdbarch_tdep (gdbarch)->a0_base + cache->c0.c0_rt[i].fr_reg);
02727         }
02728       else ra = 0;
02729     }
02730   
02731  finish_frame_analysis:
02732   cache->pc = start_pc;
02733   cache->ra = ra;
02734   /* RA == 0 marks the outermost frame.  Do not go past it.  */
02735   cache->prev_sp = (ra != 0) ?  prev_sp : 0;
02736   cache->c0.fp_regnum = fp_regnum;
02737   cache->c0.c0_frmsz = c0_frmsz;
02738   cache->c0.c0_hasfp = c0_hasfp;
02739   cache->c0.c0_fp = fp;
02740 }
02741 
02742 static CORE_ADDR a0_saved;
02743 static CORE_ADDR a7_saved;
02744 static CORE_ADDR a11_saved;
02745 static int a0_was_saved;
02746 static int a7_was_saved;
02747 static int a11_was_saved;
02748 
02749 /* Simulate L32E instruction:  AT <-- ref (AS + offset).  */
02750 static void
02751 execute_l32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
02752 {
02753   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
02754   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
02755   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
02756   unsigned int spilled_value
02757     = read_memory_unsigned_integer (addr, 4, gdbarch_byte_order (gdbarch));
02758 
02759   if ((at == 0) && !a0_was_saved)
02760     {
02761       a0_saved = xtensa_read_register (atreg);
02762       a0_was_saved = 1;
02763     }
02764   else if ((at == 7) && !a7_was_saved)
02765     {
02766       a7_saved = xtensa_read_register (atreg);
02767       a7_was_saved = 1;
02768     }
02769   else if ((at == 11) && !a11_was_saved)
02770     {
02771       a11_saved = xtensa_read_register (atreg);
02772       a11_was_saved = 1;
02773     }
02774 
02775   xtensa_write_register (atreg, spilled_value);
02776 }
02777 
02778 /* Simulate S32E instruction:  AT --> ref (AS + offset).  */
02779 static void
02780 execute_s32e (struct gdbarch *gdbarch, int at, int as, int offset, CORE_ADDR wb)
02781 {
02782   int atreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + at, wb);
02783   int asreg = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base + as, wb);
02784   CORE_ADDR addr = xtensa_read_register (asreg) + offset;
02785   ULONGEST spilled_value = xtensa_read_register (atreg);
02786 
02787   write_memory_unsigned_integer (addr, 4,
02788                                  gdbarch_byte_order (gdbarch),
02789                                  spilled_value);
02790 }
02791 
02792 #define XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN  200
02793 
02794 typedef enum
02795 {
02796   xtWindowOverflow,
02797   xtWindowUnderflow,
02798   xtNoExceptionHandler
02799 } xtensa_exception_handler_t;
02800 
02801 /* Execute instruction stream from current PC until hitting RFWU or RFWO.
02802    Return type of Xtensa Window Interrupt Handler on success.  */
02803 static xtensa_exception_handler_t
02804 execute_code (struct gdbarch *gdbarch, CORE_ADDR current_pc, CORE_ADDR wb)
02805 {
02806   xtensa_isa isa;
02807   xtensa_insnbuf ins, slot;
02808   gdb_byte ibuf[XTENSA_ISA_BSZ];
02809   CORE_ADDR ia, bt, ba;
02810   xtensa_format ifmt;
02811   int ilen, islots, is;
02812   xtensa_opcode opc;
02813   int insn_num = 0;
02814   int fail = 0;
02815   void (*func) (struct gdbarch *, int, int, int, CORE_ADDR);
02816 
02817   uint32_t at, as, offset;
02818 
02819   /* WindowUnderflow12 = true, when inside _WindowUnderflow12.  */ 
02820   int WindowUnderflow12 = (current_pc & 0x1ff) >= 0x140; 
02821 
02822   isa = xtensa_default_isa;
02823   gdb_assert (XTENSA_ISA_BSZ >= xtensa_isa_maxlength (isa));
02824   ins = xtensa_insnbuf_alloc (isa);
02825   slot = xtensa_insnbuf_alloc (isa);
02826   ba = 0;
02827   ia = current_pc;
02828   bt = ia;
02829 
02830   a0_was_saved = 0;
02831   a7_was_saved = 0;
02832   a11_was_saved = 0;
02833 
02834   while (insn_num++ < XTENSA_MAX_WINDOW_INTERRUPT_HANDLER_LEN)
02835     {
02836       if (ia + xtensa_isa_maxlength (isa) > bt)
02837         {
02838           ba = ia;
02839           bt = (ba + XTENSA_ISA_BSZ);
02840           if (target_read_memory (ba, ibuf, bt - ba) != 0)
02841             return xtNoExceptionHandler;
02842         }
02843       xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
02844       ifmt = xtensa_format_decode (isa, ins);
02845       if (ifmt == XTENSA_UNDEFINED)
02846         return xtNoExceptionHandler;
02847       ilen = xtensa_format_length (isa, ifmt);
02848       if (ilen == XTENSA_UNDEFINED)
02849         return xtNoExceptionHandler;
02850       islots = xtensa_format_num_slots (isa, ifmt);
02851       if (islots == XTENSA_UNDEFINED)
02852         return xtNoExceptionHandler;
02853       for (is = 0; is < islots; ++is)
02854         {
02855           if (xtensa_format_get_slot (isa, ifmt, is, ins, slot))
02856             return xtNoExceptionHandler;
02857           opc = xtensa_opcode_decode (isa, ifmt, is, slot);
02858           if (opc == XTENSA_UNDEFINED) 
02859             return xtNoExceptionHandler;
02860           switch (call0_classify_opcode (isa, opc))
02861             {
02862             case c0opc_illegal:
02863             case c0opc_flow:
02864             case c0opc_entry:
02865             case c0opc_break:
02866               /* We expect none of them here.  */
02867               return xtNoExceptionHandler;
02868             case c0opc_l32e:
02869               func = execute_l32e;
02870               break;
02871             case c0opc_s32e:
02872               func = execute_s32e;
02873               break;
02874             case c0opc_rfwo: /* RFWO.  */
02875               /* Here, we return from WindowOverflow handler and,
02876                  if we stopped at the very beginning, which means
02877                  A0 was saved, we have to restore it now.  */
02878               if (a0_was_saved)
02879                 {
02880                   int arreg = arreg_number (gdbarch,
02881                                             gdbarch_tdep (gdbarch)->a0_base,
02882                                             wb);
02883                   xtensa_write_register (arreg, a0_saved);
02884                 }
02885               return xtWindowOverflow;
02886             case c0opc_rfwu: /* RFWU.  */
02887               /* Here, we return from WindowUnderflow handler.
02888                  Let's see if either A7 or A11 has to be restored.  */
02889               if (WindowUnderflow12)
02890                 {
02891                   if (a11_was_saved)
02892                     {
02893                       int arreg = arreg_number (gdbarch,
02894                                                 gdbarch_tdep (gdbarch)->a0_base + 11,
02895                                                 wb);
02896                       xtensa_write_register (arreg, a11_saved);
02897                     }
02898                 }
02899               else if (a7_was_saved)
02900                 {
02901                   int arreg = arreg_number (gdbarch,
02902                                             gdbarch_tdep (gdbarch)->a0_base + 7,
02903                                             wb);
02904                   xtensa_write_register (arreg, a7_saved);
02905                 }
02906               return xtWindowUnderflow;
02907             default: /* Simply skip this insns.  */
02908               continue;
02909             }
02910 
02911           /* Decode arguments for L32E / S32E and simulate their execution.  */
02912           if ( xtensa_opcode_num_operands (isa, opc) != 3 )
02913             return xtNoExceptionHandler;
02914           if (xtensa_operand_get_field (isa, opc, 0, ifmt, is, slot, &at))
02915             return xtNoExceptionHandler;
02916           if (xtensa_operand_decode (isa, opc, 0, &at))
02917             return xtNoExceptionHandler;
02918           if (xtensa_operand_get_field (isa, opc, 1, ifmt, is, slot, &as))
02919             return xtNoExceptionHandler;
02920           if (xtensa_operand_decode (isa, opc, 1, &as))
02921             return xtNoExceptionHandler;
02922           if (xtensa_operand_get_field (isa, opc, 2, ifmt, is, slot, &offset))
02923             return xtNoExceptionHandler;
02924           if (xtensa_operand_decode (isa, opc, 2, &offset))
02925             return xtNoExceptionHandler;
02926 
02927           (*func) (gdbarch, at, as, offset, wb);
02928         }
02929 
02930       ia += ilen;
02931     }
02932   return xtNoExceptionHandler;
02933 }
02934 
02935 /* Handle Window Overflow / Underflow exception frames.  */
02936 
02937 static void
02938 xtensa_window_interrupt_frame_cache (struct frame_info *this_frame,
02939                                      xtensa_frame_cache_t *cache,
02940                                      CORE_ADDR pc)
02941 {
02942   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02943   CORE_ADDR ps, wb, ws, ra;
02944   int epc1_regnum, i, regnum;
02945   xtensa_exception_handler_t eh_type;
02946 
02947   /* Read PS, WB, and WS from the hardware. Note that PS register
02948      must be present, if Windowed ABI is supported.  */
02949   ps = xtensa_read_register (gdbarch_ps_regnum (gdbarch));
02950   wb = xtensa_read_register (gdbarch_tdep (gdbarch)->wb_regnum);
02951   ws = xtensa_read_register (gdbarch_tdep (gdbarch)->ws_regnum);
02952 
02953   /* Execute all the remaining instructions from Window Interrupt Handler
02954      by simulating them on the remote protocol level.  On return, set the
02955      type of Xtensa Window Interrupt Handler, or report an error.  */
02956   eh_type = execute_code (gdbarch, pc, wb);
02957   if (eh_type == xtNoExceptionHandler)
02958     error (_("\
02959 Unable to decode Xtensa Window Interrupt Handler's code."));
02960 
02961   cache->ps = ps ^ PS_EXC;      /* Clear the exception bit in PS.  */
02962   cache->call0 = 0;             /* It's Windowed ABI.  */
02963 
02964   /* All registers for the cached frame will be alive.  */
02965   for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
02966     cache->wd.aregs[i] = -1;
02967 
02968   if (eh_type == xtWindowOverflow)
02969     cache->wd.ws = ws ^ (1 << wb);
02970   else /* eh_type == xtWindowUnderflow.  */
02971     cache->wd.ws = ws | (1 << wb);
02972 
02973   cache->wd.wb = (ps & 0xf00) >> 8; /* Set WB to OWB.  */
02974   regnum = arreg_number (gdbarch, gdbarch_tdep (gdbarch)->a0_base,
02975                          cache->wd.wb);
02976   ra = xtensa_read_register (regnum);
02977   cache->wd.callsize = WINSIZE (ra);
02978   cache->prev_sp = xtensa_read_register (regnum + 1);
02979   /* Set regnum to a frame pointer of the frame being cached.  */
02980   regnum = xtensa_scan_prologue (gdbarch, pc);
02981   regnum = arreg_number (gdbarch,
02982                          gdbarch_tdep (gdbarch)->a0_base + regnum,
02983                          cache->wd.wb);
02984   cache->base = get_frame_register_unsigned (this_frame, regnum);
02985 
02986   /* Read PC of interrupted function from EPC1 register.  */
02987   epc1_regnum = xtensa_find_register_by_name (gdbarch,"epc1");
02988   if (epc1_regnum < 0)
02989     error(_("Unable to read Xtensa register EPC1"));
02990   cache->ra = xtensa_read_register (epc1_regnum);
02991   cache->pc = get_frame_func (this_frame);
02992 }
02993 
02994 
02995 /* Skip function prologue.
02996 
02997    Return the pc of the first instruction after prologue.  GDB calls this to
02998    find the address of the first line of the function or (if there is no line
02999    number information) to skip the prologue for planting breakpoints on 
03000    function entries.  Use debug info (if present) or prologue analysis to skip 
03001    the prologue to achieve reliable debugging behavior.  For windowed ABI, 
03002    only the 'entry' instruction is skipped.  It is not strictly necessary to 
03003    skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
03004    backtrace at any point in the prologue, however certain potential hazards 
03005    are avoided and a more "normal" debugging experience is ensured by 
03006    skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
03007    For example, if we don't skip the prologue:
03008    - Some args may not yet have been saved to the stack where the debug
03009      info expects to find them (true anyway when only 'entry' is skipped);
03010    - Software breakpoints ('break' instrs) may not have been unplanted 
03011      when the prologue analysis is done on initializing the frame cache, 
03012      and breaks in the prologue will throw off the analysis.
03013 
03014    If we have debug info ( line-number info, in particular ) we simply skip
03015    the code associated with the first function line effectively skipping
03016    the prologue code.  It works even in cases like
03017 
03018    int main()
03019    {    int local_var = 1;
03020         ....
03021    }
03022 
03023    because, for this source code, both Xtensa compilers will generate two
03024    separate entries ( with the same line number ) in dwarf line-number
03025    section to make sure there is a boundary between the prologue code and
03026    the rest of the function.
03027 
03028    If there is no debug info, we need to analyze the code.  */
03029 
03030 /* #define DONT_SKIP_PROLOGUE  */
03031 
03032 static CORE_ADDR
03033 xtensa_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
03034 {
03035   struct symtab_and_line prologue_sal;
03036   CORE_ADDR body_pc;
03037 
03038   DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
03039 
03040 #if DONT_SKIP_PROLOGUE
03041   return start_pc;
03042 #endif
03043 
03044  /* Try to find first body line from debug info.  */
03045 
03046   prologue_sal = find_pc_line (start_pc, 0);
03047   if (prologue_sal.line != 0) /* Found debug info.  */
03048     {
03049       /* In Call0,  it is possible to have a function with only one instruction
03050          ('ret') resulting from a one-line optimized function that does nothing.
03051          In that case,  prologue_sal.end may actually point to the start of the
03052          next function in the text section,  causing a breakpoint to be set at
03053          the wrong place.  Check,  if the end address is within a different
03054          function,  and if so return the start PC.  We know we have symbol
03055          information.  */
03056 
03057       CORE_ADDR end_func;
03058 
03059       if ((gdbarch_tdep (gdbarch)->call_abi == CallAbiCall0Only)
03060           && call0_ret (start_pc, prologue_sal.end))
03061         return start_pc;
03062 
03063       find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
03064       if (end_func != start_pc)
03065         return start_pc;
03066 
03067       return prologue_sal.end;
03068     }
03069 
03070   /* No debug line info.  Analyze prologue for Call0 or simply skip ENTRY.  */
03071   body_pc = call0_analyze_prologue (gdbarch, start_pc, 0, 0,
03072                                     xtensa_alloc_frame_cache (0));
03073   return body_pc != 0 ? body_pc : start_pc;
03074 }
03075 
03076 /* Verify the current configuration.  */
03077 static void
03078 xtensa_verify_config (struct gdbarch *gdbarch)
03079 {
03080   struct ui_file *log;
03081   struct cleanup *cleanups;
03082   struct gdbarch_tdep *tdep;
03083   long length;
03084   char *buf;
03085 
03086   tdep = gdbarch_tdep (gdbarch);
03087   log = mem_fileopen ();
03088   cleanups = make_cleanup_ui_file_delete (log);
03089 
03090   /* Verify that we got a reasonable number of AREGS.  */
03091   if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
03092     fprintf_unfiltered (log, _("\
03093 \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
03094                         tdep->num_aregs);
03095 
03096   /* Verify that certain registers exist.  */
03097 
03098   if (tdep->pc_regnum == -1)
03099     fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
03100   if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
03101     fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
03102 
03103   if (tdep->isa_use_windowed_registers)
03104     {
03105       if (tdep->wb_regnum == -1)
03106         fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
03107       if (tdep->ws_regnum == -1)
03108         fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
03109       if (tdep->ar_base == -1)
03110         fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
03111     }
03112 
03113   if (tdep->a0_base == -1)
03114     fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
03115 
03116   buf = ui_file_xstrdup (log, &length);
03117   make_cleanup (xfree, buf);
03118   if (length > 0)
03119     internal_error (__FILE__, __LINE__,
03120                     _("the following are invalid: %s"), buf);
03121   do_cleanups (cleanups);
03122 }
03123 
03124 
03125 /* Derive specific register numbers from the array of registers.  */
03126 
03127 static void
03128 xtensa_derive_tdep (struct gdbarch_tdep *tdep)
03129 {
03130   xtensa_register_t* rmap;
03131   int n, max_size = 4;
03132 
03133   tdep->num_regs = 0;
03134   tdep->num_nopriv_regs = 0;
03135 
03136 /* Special registers 0..255 (core).  */
03137 #define XTENSA_DBREGN_SREG(n)  (0x0200+(n))
03138 
03139   for (rmap = tdep->regmap, n = 0; rmap->target_number != -1; n++, rmap++)
03140     {
03141       if (rmap->target_number == 0x0020)
03142         tdep->pc_regnum = n;
03143       else if (rmap->target_number == 0x0100)
03144         tdep->ar_base = n;
03145       else if (rmap->target_number == 0x0000)
03146         tdep->a0_base = n;
03147       else if (rmap->target_number == XTENSA_DBREGN_SREG(72))
03148         tdep->wb_regnum = n;
03149       else if (rmap->target_number == XTENSA_DBREGN_SREG(73))
03150         tdep->ws_regnum = n;
03151       else if (rmap->target_number == XTENSA_DBREGN_SREG(233))
03152         tdep->debugcause_regnum = n;
03153       else if (rmap->target_number == XTENSA_DBREGN_SREG(232))
03154         tdep->exccause_regnum = n;
03155       else if (rmap->target_number == XTENSA_DBREGN_SREG(238))
03156         tdep->excvaddr_regnum = n;
03157       else if (rmap->target_number == XTENSA_DBREGN_SREG(0))
03158         tdep->lbeg_regnum = n;
03159       else if (rmap->target_number == XTENSA_DBREGN_SREG(1))
03160         tdep->lend_regnum = n;
03161       else if (rmap->target_number == XTENSA_DBREGN_SREG(2))
03162         tdep->lcount_regnum = n;
03163       else if (rmap->target_number == XTENSA_DBREGN_SREG(3))
03164         tdep->sar_regnum = n;
03165       else if (rmap->target_number == XTENSA_DBREGN_SREG(5))
03166         tdep->litbase_regnum = n;
03167       else if (rmap->target_number == XTENSA_DBREGN_SREG(230))
03168         tdep->ps_regnum = n;
03169 #if 0
03170       else if (rmap->target_number == XTENSA_DBREGN_SREG(226))
03171         tdep->interrupt_regnum = n;
03172       else if (rmap->target_number == XTENSA_DBREGN_SREG(227))
03173         tdep->interrupt2_regnum = n;
03174       else if (rmap->target_number == XTENSA_DBREGN_SREG(224))
03175         tdep->cpenable_regnum = n;
03176 #endif
03177 
03178       if (rmap->byte_size > max_size)
03179         max_size = rmap->byte_size;
03180       if (rmap->mask != 0 && tdep->num_regs == 0)
03181         tdep->num_regs = n;
03182       /* Find out out how to deal with priveleged registers.
03183 
03184          if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
03185               && tdep->num_nopriv_regs == 0)
03186            tdep->num_nopriv_regs = n;
03187       */
03188       if ((rmap->flags & XTENSA_REGISTER_FLAGS_PRIVILEGED) != 0
03189           && tdep->num_regs == 0)
03190         tdep->num_regs = n;
03191     }
03192 
03193   /* Number of pseudo registers.  */
03194   tdep->num_pseudo_regs = n - tdep->num_regs;
03195 
03196   /* Empirically determined maximum sizes.  */
03197   tdep->max_register_raw_size = max_size;
03198   tdep->max_register_virtual_size = max_size;
03199 }
03200 
03201 /* Module "constructor" function.  */
03202 
03203 extern struct gdbarch_tdep xtensa_tdep;
03204 
03205 static struct gdbarch *
03206 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
03207 {
03208   struct gdbarch_tdep *tdep;
03209   struct gdbarch *gdbarch;
03210   struct xtensa_abi_handler *abi_handler;
03211 
03212   DEBUGTRACE ("gdbarch_init()\n");
03213 
03214   /* We have to set the byte order before we call gdbarch_alloc.  */
03215   info.byte_order = XCHAL_HAVE_BE ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
03216 
03217   tdep = &xtensa_tdep;
03218   gdbarch = gdbarch_alloc (&info, tdep);
03219   xtensa_derive_tdep (tdep);
03220 
03221   /* Verify our configuration.  */
03222   xtensa_verify_config (gdbarch);
03223   xtensa_session_once_reported = 0;
03224 
03225   /* Pseudo-Register read/write.  */
03226   set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
03227   set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
03228 
03229   /* Set target information.  */
03230   set_gdbarch_num_regs (gdbarch, tdep->num_regs);
03231   set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
03232   set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
03233   set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
03234   set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
03235 
03236   /* Renumber registers for known formats (stabs and dwarf2).  */
03237   set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
03238   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
03239 
03240   /* We provide our own function to get register information.  */
03241   set_gdbarch_register_name (gdbarch, xtensa_register_name);
03242   set_gdbarch_register_type (gdbarch, xtensa_register_type);
03243 
03244   /* To call functions from GDB using dummy frame.  */
03245   set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
03246 
03247   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
03248 
03249   set_gdbarch_return_value (gdbarch, xtensa_return_value);
03250 
03251   /* Advance PC across any prologue instructions to reach "real" code.  */
03252   set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
03253 
03254   /* Stack grows downward.  */
03255   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
03256 
03257   /* Set breakpoints.  */
03258   set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
03259 
03260   /* After breakpoint instruction or illegal instruction, pc still
03261      points at break instruction, so don't decrement.  */
03262   set_gdbarch_decr_pc_after_break (gdbarch, 0);
03263 
03264   /* We don't skip args.  */
03265   set_gdbarch_frame_args_skip (gdbarch, 0);
03266 
03267   set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
03268 
03269   set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
03270 
03271   set_gdbarch_dummy_id (gdbarch, xtensa_dummy_id);
03272 
03273   /* Frame handling.  */
03274   frame_base_set_default (gdbarch, &xtensa_frame_base);
03275   frame_unwind_append_unwinder (gdbarch, &xtensa_unwind);
03276   dwarf2_append_unwinders (gdbarch);
03277 
03278   set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
03279 
03280   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
03281 
03282   xtensa_add_reggroups (gdbarch);
03283   set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
03284 
03285   set_gdbarch_regset_from_core_section (gdbarch,
03286                                         xtensa_regset_from_core_section);
03287 
03288   set_solib_svr4_fetch_link_map_offsets
03289     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
03290 
03291   return gdbarch;
03292 }
03293 
03294 static void
03295 xtensa_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
03296 {
03297   error (_("xtensa_dump_tdep(): not implemented"));
03298 }
03299 
03300 /* Provide a prototype to silence -Wmissing-prototypes.  */
03301 extern initialize_file_ftype _initialize_xtensa_tdep;
03302 
03303 void
03304 _initialize_xtensa_tdep (void)
03305 {
03306   struct cmd_list_element *c;
03307 
03308   gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
03309   xtensa_init_reggroups ();
03310 
03311   add_setshow_zuinteger_cmd ("xtensa",
03312                              class_maintenance,
03313                              &xtensa_debug_level,
03314                             _("Set Xtensa debugging."),
03315                             _("Show Xtensa debugging."), _("\
03316 When non-zero, Xtensa-specific debugging is enabled. \
03317 Can be 1, 2, 3, or 4 indicating the level of debugging."),
03318                              NULL,
03319                              NULL,
03320                              &setdebuglist, &showdebuglist);
03321 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines