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