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