1 /* Target-dependent code for the Xtensa port of GDB, the GNU debugger.
3 Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "floatformat.h"
32 #include "reggroups.h"
35 #include "dummy-frame.h"
36 #include "elf/dwarf2.h"
37 #include "dwarf2-frame.h"
38 #include "dwarf2loc.h"
40 #include "frame-base.h"
41 #include "frame-unwind.h"
43 #include "arch-utils.h"
50 #include "gdb_assert.h"
52 #include "xtensa-isa.h"
53 #include "xtensa-tdep.h"
56 static int xtensa_debug_level = 0;
58 #define DEBUGWARN(args...) \
59 if (xtensa_debug_level > 0) \
60 fprintf_unfiltered (gdb_stdlog, "(warn ) " args)
62 #define DEBUGINFO(args...) \
63 if (xtensa_debug_level > 1) \
64 fprintf_unfiltered (gdb_stdlog, "(info ) " args)
66 #define DEBUGTRACE(args...) \
67 if (xtensa_debug_level > 2) \
68 fprintf_unfiltered (gdb_stdlog, "(trace) " args)
70 #define DEBUGVERB(args...) \
71 if (xtensa_debug_level > 3) \
72 fprintf_unfiltered (gdb_stdlog, "(verb ) " args)
75 /* According to the ABI, the SP must be aligned to 16-byte boundaries. */
76 #define SP_ALIGNMENT 16
79 /* On Windowed ABI, we use a6 through a11 for passing arguments
80 to a function called by GDB because CALL4 is used. */
81 #define ARGS_FIRST_REG A6_REGNUM
82 #define ARGS_NUM_REGS 6
83 #define REGISTER_SIZE 4
86 /* Extract the call size from the return address or PS register. */
87 #define PS_CALLINC_SHIFT 16
88 #define PS_CALLINC_MASK 0x00030000
89 #define CALLINC(ps) (((ps) & PS_CALLINC_MASK) >> PS_CALLINC_SHIFT)
90 #define WINSIZE(ra) (4 * (( (ra) >> 30) & 0x3))
93 /* Convert a live Ax register number to the corresponding Areg number. */
94 #define AREG_NUMBER(r, wb) \
95 ((((r) - A0_REGNUM + (((wb) & WB_MASK) << WB_SHIFT)) & AREGS_MASK) + AR_BASE)
97 /* ABI-independent macros. */
98 #define ARG_NOF (CALL_ABI == CallAbiCall0Only ? C0_NARGS : (ARGS_NUM_REGS))
99 #define ARG_1ST (CALL_ABI == CallAbiCall0Only \
100 ? (A0_REGNUM) + C0_ARGS : (ARGS_FIRST_REG))
102 extern struct gdbarch_tdep *xtensa_config_tdep (struct gdbarch_info *);
103 extern int xtensa_config_byte_order (struct gdbarch_info *);
106 /* XTENSA_IS_ENTRY tests whether the first byte of an instruction
107 indicates that the instruction is an ENTRY instruction. */
109 #define XTENSA_IS_ENTRY(op1) \
110 ((gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG) \
111 ? ((op1) == 0x6c) : ((op1) == 0x36))
113 #define XTENSA_ENTRY_LENGTH 3
115 /* windowing_enabled() returns true, if windowing is enabled.
116 WOE must be set to 1; EXCM to 0.
117 Note: We assume that EXCM is always 0 for XEA1. */
119 #define PS_WOE (1<<18)
120 #define PS_EXC (1<<4)
123 windowing_enabled (CORE_ADDR ps)
125 return ((ps & PS_EXC) == 0 && (ps & PS_WOE) != 0);
128 /* Return the window size of the previous call to the function from which we
131 This function is used to extract the return value after a called function
132 has returned to the caller. On Xtensa, the register that holds the return
133 value (from the perspective of the caller) depends on what call
134 instruction was used. For now, we are assuming that the call instruction
135 precedes the current address, so we simply analyze the call instruction.
136 If we are in a dummy frame, we simply return 4 as we used a 'pseudo-call4'
137 method to call the inferior function. */
140 extract_call_winsize (CORE_ADDR pc)
146 DEBUGTRACE ("extract_call_winsize (pc = 0x%08x)\n", (int) pc);
148 /* Read the previous instruction (should be a call[x]{4|8|12}. */
149 read_memory (pc-3, buf, 3);
150 insn = extract_unsigned_integer (buf, 3);
152 /* Decode call instruction:
154 call{0,4,8,12} OFFSET || {00,01,10,11} || 0101
155 callx{0,4,8,12} OFFSET || 11 || {00,01,10,11} || 0000
157 call{0,4,8,12} 0101 || {00,01,10,11} || OFFSET
158 callx{0,4,8,12} 0000 || {00,01,10,11} || 11 || OFFSET. */
160 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_LITTLE)
162 if (((insn & 0xf) == 0x5) || ((insn & 0xcf) == 0xc0))
163 winsize = (insn & 0x30) >> 2; /* 0, 4, 8, 12. */
167 if (((insn >> 20) == 0x5) || (((insn >> 16) & 0xf3) == 0x03))
168 winsize = (insn >> 16) & 0xc; /* 0, 4, 8, 12. */
174 /* REGISTER INFORMATION */
176 /* Returns the name of a register. */
178 xtensa_register_name (int regnum)
180 /* Return the name stored in the register map. */
181 if (regnum >= 0 && regnum < gdbarch_num_regs (current_gdbarch)
182 + gdbarch_num_pseudo_regs (current_gdbarch))
183 return REGMAP[regnum].name;
185 internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum);
190 xtensa_read_register (int regnum)
194 regcache_raw_read_unsigned (get_current_regcache (), regnum, &value);
195 return (unsigned long) value;
198 /* Return the type of a register. Create a new type, if necessary. */
200 static struct ctype_cache
202 struct ctype_cache *next;
204 struct type *virtual_type;
205 } *type_entries = NULL;
208 xtensa_register_type (struct gdbarch *gdbarch, int regnum)
210 /* Return signed integer for ARx and Ax registers. */
211 if ((regnum >= AR_BASE && regnum < AR_BASE + NUM_AREGS)
212 || (regnum >= A0_BASE && regnum < A0_BASE + 16))
213 return builtin_type_int;
215 if (regnum == gdbarch_pc_regnum (current_gdbarch) || regnum == A1_REGNUM)
216 return lookup_pointer_type (builtin_type_void);
218 /* Return the stored type for all other registers. */
219 else if (regnum >= 0 && regnum < gdbarch_num_regs (current_gdbarch)
220 + gdbarch_num_pseudo_regs (current_gdbarch))
222 xtensa_register_t* reg = ®MAP[regnum];
224 /* Set ctype for this register (only the first time). */
228 struct ctype_cache *tp;
229 int size = reg->byte_size;
231 /* We always use the memory representation,
232 even if the register width is smaller. */
236 reg->ctype = builtin_type_uint8;
240 reg->ctype = builtin_type_uint16;
244 reg->ctype = builtin_type_uint32;
248 reg->ctype = builtin_type_uint64;
252 reg->ctype = builtin_type_uint128;
256 for (tp = type_entries; tp != NULL; tp = tp->next)
257 if (tp->size == size)
262 char *name = xmalloc (16);
263 tp = xmalloc (sizeof (struct ctype_cache));
264 tp->next = type_entries;
268 sprintf (name, "int%d", size * 8);
269 tp->virtual_type = init_type (TYPE_CODE_INT, size,
270 TYPE_FLAG_UNSIGNED, name,
274 reg->ctype = tp->virtual_type;
280 internal_error (__FILE__, __LINE__, _("invalid register number %d"), regnum);
285 /* Return the 'local' register number for stubs, dwarf2, etc.
286 The debugging information enumerates registers starting from 0 for A0
287 to n for An. So, we only have to add the base number for A0. */
290 xtensa_reg_to_regnum (int regnum)
294 if (regnum >= 0 && regnum < 16)
295 return A0_BASE + regnum;
298 i < gdbarch_num_regs (current_gdbarch)
299 + gdbarch_num_pseudo_regs (current_gdbarch);
301 if (regnum == REGMAP[i].target_number)
304 internal_error (__FILE__, __LINE__,
305 _("invalid dwarf/stabs register number %d"), regnum);
310 /* Write the bits of a masked register to the various registers.
311 Only the masked areas of these registers are modified; the other
312 fields are untouched. The size of masked registers is always less
313 than or equal to 32 bits. */
316 xtensa_register_write_masked (struct regcache *regcache,
317 xtensa_register_t *reg, const gdb_byte *buffer)
319 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
320 const xtensa_mask_t *mask = reg->mask;
322 int shift = 0; /* Shift for next mask (mod 32). */
323 int start, size; /* Start bit and size of current mask. */
325 unsigned int *ptr = value;
326 unsigned int regval, m, mem = 0;
328 int bytesize = reg->byte_size;
329 int bitsize = bytesize * 8;
332 DEBUGTRACE ("xtensa_register_write_masked ()\n");
334 /* Copy the masked register to host byte-order. */
335 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
336 for (i = 0; i < bytesize; i++)
339 mem |= (buffer[bytesize - i - 1] << 24);
344 for (i = 0; i < bytesize; i++)
347 mem |= (buffer[i] << 24);
352 /* We might have to shift the final value:
353 bytesize & 3 == 0 -> nothing to do, we use the full 32 bits,
354 bytesize & 3 == x -> shift (4-x) * 8. */
356 *ptr = mem >> (((0 - bytesize) & 3) * 8);
360 /* Write the bits to the masked areas of the other registers. */
361 for (i = 0; i < mask->count; i++)
363 start = mask->mask[i].bit_start;
364 size = mask->mask[i].bit_size;
365 regval = mem >> shift;
367 if ((shift += size) > bitsize)
368 error (_("size of all masks is larger than the register"));
377 regval |= mem << (size - shift);
380 /* Make sure we have a valid register. */
381 r = mask->mask[i].reg_num;
382 if (r >= 0 && size > 0)
384 /* Don't overwrite the unmasked areas. */
386 regcache_cooked_read_unsigned (regcache, r, &old_val);
387 m = 0xffffffff >> (32 - size) << start;
389 regval = (regval & m) | (old_val & ~m);
390 regcache_cooked_write_unsigned (regcache, r, regval);
396 /* Read a tie state or mapped registers. Read the masked areas
397 of the registers and assemble them into a single value. */
400 xtensa_register_read_masked (struct regcache *regcache,
401 xtensa_register_t *reg, gdb_byte *buffer)
403 unsigned int value[(MAX_REGISTER_SIZE + 3) / 4];
404 const xtensa_mask_t *mask = reg->mask;
409 unsigned int *ptr = value;
410 unsigned int regval, mem = 0;
412 int bytesize = reg->byte_size;
413 int bitsize = bytesize * 8;
416 DEBUGTRACE ("xtensa_register_read_masked (reg \"%s\", ...)\n",
417 reg->name == 0 ? "" : reg->name);
419 /* Assemble the register from the masked areas of other registers. */
420 for (i = 0; i < mask->count; i++)
422 int r = mask->mask[i].reg_num;
426 regcache_cooked_read_unsigned (regcache, r, &val);
427 regval = (unsigned int) val;
432 start = mask->mask[i].bit_start;
433 size = mask->mask[i].bit_size;
438 regval &= (0xffffffff >> (32 - size));
440 mem |= regval << shift;
442 if ((shift += size) > bitsize)
443 error (_("size of all masks is larger than the register"));
454 mem = regval >> (size - shift);
461 /* Copy value to target byte order. */
465 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
466 for (i = 0; i < bytesize; i++)
470 buffer[bytesize - i - 1] = mem & 0xff;
474 for (i = 0; i < bytesize; i++)
478 buffer[i] = mem & 0xff;
484 /* Read pseudo registers. */
487 xtensa_pseudo_register_read (struct gdbarch *gdbarch,
488 struct regcache *regcache,
492 DEBUGTRACE ("xtensa_pseudo_register_read (... regnum = %d (%s) ...)\n",
493 regnum, xtensa_register_name (regnum));
495 if (regnum == FP_ALIAS)
498 /* Read aliases a0..a15, if this is a Windowed ABI. */
499 if (ISA_USE_WINDOWED_REGISTERS
500 && (regnum >= A0_REGNUM) && (regnum <= A15_REGNUM))
502 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
504 regcache_raw_read (regcache, WB_REGNUM, buf);
505 regnum = AREG_NUMBER (regnum, extract_unsigned_integer (buf, 4));
508 /* We can always read non-pseudo registers. */
509 if (regnum >= 0 && regnum < gdbarch_num_regs (current_gdbarch))
510 regcache_raw_read (regcache, regnum, buffer);
512 /* Pseudo registers. */
514 && regnum < gdbarch_num_regs (current_gdbarch)
515 + gdbarch_num_pseudo_regs (current_gdbarch))
517 xtensa_register_t *reg = ®MAP[regnum];
518 xtensa_register_type_t type = reg->type;
519 int flags = XTENSA_TARGET_FLAGS;
521 /* We cannot read Unknown or Unmapped registers. */
522 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
524 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
526 warning (_("cannot read register %s"),
527 xtensa_register_name (regnum));
532 /* Some targets cannot read TIE register files. */
533 else if (type == xtRegisterTypeTieRegfile)
535 /* Use 'fetch' to get register? */
536 if (flags & xtTargetFlagsUseFetchStore)
538 warning (_("cannot read register"));
542 /* On some targets (esp. simulators), we can always read the reg. */
543 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
545 warning (_("cannot read register"));
550 /* We can always read mapped registers. */
551 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
553 xtensa_register_read_masked (regcache, reg, buffer);
557 /* Assume that we can read the register. */
558 regcache_raw_read (regcache, regnum, buffer);
561 internal_error (__FILE__, __LINE__,
562 _("invalid register number %d"), regnum);
566 /* Write pseudo registers. */
569 xtensa_pseudo_register_write (struct gdbarch *gdbarch,
570 struct regcache *regcache,
572 const gdb_byte *buffer)
574 DEBUGTRACE ("xtensa_pseudo_register_write (... regnum = %d (%s) ...)\n",
575 regnum, xtensa_register_name (regnum));
577 if (regnum == FP_ALIAS)
580 /* Renumber register, if aliase a0..a15 on Windowed ABI. */
581 if (ISA_USE_WINDOWED_REGISTERS
582 && (regnum >= A0_REGNUM) && (regnum <= A15_REGNUM))
584 gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
587 regcache_raw_read (regcache, WB_REGNUM, buf);
588 regnum = AREG_NUMBER (regnum, extract_unsigned_integer (buf, 4));
591 /* We can always write 'core' registers.
592 Note: We might have converted Ax->ARy. */
593 if (regnum >= 0 && regnum < gdbarch_num_regs (current_gdbarch))
594 regcache_raw_write (regcache, regnum, buffer);
596 /* Pseudo registers. */
598 && regnum < gdbarch_num_regs (current_gdbarch)
599 + gdbarch_num_pseudo_regs (current_gdbarch))
601 xtensa_register_t *reg = ®MAP[regnum];
602 xtensa_register_type_t type = reg->type;
603 int flags = XTENSA_TARGET_FLAGS;
605 /* On most targets, we cannot write registers
606 of type "Unknown" or "Unmapped". */
607 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
609 if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
611 warning (_("cannot write register %s"),
612 xtensa_register_name (regnum));
617 /* Some targets cannot read TIE register files. */
618 else if (type == xtRegisterTypeTieRegfile)
620 /* Use 'store' to get register? */
621 if (flags & xtTargetFlagsUseFetchStore)
623 warning (_("cannot write register"));
627 /* On some targets (esp. simulators), we can always write
629 else if ((flags & xtTargetFlagsNonVisibleRegs) == 0)
631 warning (_("cannot write register"));
636 /* We can always write mapped registers. */
637 else if (type == xtRegisterTypeMapped || type == xtRegisterTypeTieState)
639 xtensa_register_write_masked (regcache, reg, buffer);
643 /* Assume that we can write the register. */
644 regcache_raw_write (regcache, regnum, buffer);
647 internal_error (__FILE__, __LINE__,
648 _("invalid register number %d"), regnum);
651 static struct reggroup *xtensa_ar_reggroup;
652 static struct reggroup *xtensa_user_reggroup;
653 static struct reggroup *xtensa_vectra_reggroup;
654 static struct reggroup *xtensa_cp[XTENSA_MAX_COPROCESSOR];
657 xtensa_init_reggroups (void)
659 xtensa_ar_reggroup = reggroup_new ("ar", USER_REGGROUP);
660 xtensa_user_reggroup = reggroup_new ("user", USER_REGGROUP);
661 xtensa_vectra_reggroup = reggroup_new ("vectra", USER_REGGROUP);
663 xtensa_cp[0] = reggroup_new ("cp0", USER_REGGROUP);
664 xtensa_cp[1] = reggroup_new ("cp1", USER_REGGROUP);
665 xtensa_cp[2] = reggroup_new ("cp2", USER_REGGROUP);
666 xtensa_cp[3] = reggroup_new ("cp3", USER_REGGROUP);
667 xtensa_cp[4] = reggroup_new ("cp4", USER_REGGROUP);
668 xtensa_cp[5] = reggroup_new ("cp5", USER_REGGROUP);
669 xtensa_cp[6] = reggroup_new ("cp6", USER_REGGROUP);
670 xtensa_cp[7] = reggroup_new ("cp7", USER_REGGROUP);
674 xtensa_add_reggroups (struct gdbarch *gdbarch)
678 /* Predefined groups. */
679 reggroup_add (gdbarch, all_reggroup);
680 reggroup_add (gdbarch, save_reggroup);
681 reggroup_add (gdbarch, restore_reggroup);
682 reggroup_add (gdbarch, system_reggroup);
683 reggroup_add (gdbarch, vector_reggroup);
684 reggroup_add (gdbarch, general_reggroup);
685 reggroup_add (gdbarch, float_reggroup);
687 /* Xtensa-specific groups. */
688 reggroup_add (gdbarch, xtensa_ar_reggroup);
689 reggroup_add (gdbarch, xtensa_user_reggroup);
690 reggroup_add (gdbarch, xtensa_vectra_reggroup);
692 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
693 reggroup_add (gdbarch, xtensa_cp[i]);
697 xtensa_coprocessor_register_group (struct reggroup *group)
701 for (i = 0; i < XTENSA_MAX_COPROCESSOR; i++)
702 if (group == xtensa_cp[i])
708 #define SAVE_REST_FLAGS (XTENSA_REGISTER_FLAGS_READABLE \
709 | XTENSA_REGISTER_FLAGS_WRITABLE \
710 | XTENSA_REGISTER_FLAGS_VOLATILE)
712 #define SAVE_REST_VALID (XTENSA_REGISTER_FLAGS_READABLE \
713 | XTENSA_REGISTER_FLAGS_WRITABLE)
716 xtensa_register_reggroup_p (struct gdbarch *gdbarch,
718 struct reggroup *group)
720 xtensa_register_t* reg = ®MAP[regnum];
721 xtensa_register_type_t type = reg->type;
722 xtensa_register_group_t rg = reg->group;
725 /* First, skip registers that are not visible to this target
726 (unknown and unmapped registers when not using ISS). */
728 if (type == xtRegisterTypeUnmapped || type == xtRegisterTypeUnknown)
730 if (group == all_reggroup)
732 if (group == xtensa_ar_reggroup)
733 return rg & xtRegisterGroupAddrReg;
734 if (group == xtensa_user_reggroup)
735 return rg & xtRegisterGroupUser;
736 if (group == float_reggroup)
737 return rg & xtRegisterGroupFloat;
738 if (group == general_reggroup)
739 return rg & xtRegisterGroupGeneral;
740 if (group == float_reggroup)
741 return rg & xtRegisterGroupFloat;
742 if (group == system_reggroup)
743 return rg & xtRegisterGroupState;
744 if (group == vector_reggroup || group == xtensa_vectra_reggroup)
745 return rg & xtRegisterGroupVectra;
746 if (group == save_reggroup || group == restore_reggroup)
747 return (regnum < gdbarch_num_regs (current_gdbarch)
748 && (reg->flags & SAVE_REST_FLAGS) == SAVE_REST_VALID);
749 if ((cp_number = xtensa_coprocessor_register_group (group)) >= 0)
750 return rg & (xtRegisterGroupCP0 << cp_number);
756 /* Supply register REGNUM from the buffer specified by GREGS and LEN
757 in the general-purpose register set REGSET to register cache
758 REGCACHE. If REGNUM is -1 do this for all registers in REGSET. */
761 xtensa_supply_gregset (const struct regset *regset,
767 const xtensa_elf_gregset_t *regs = gregs;
770 DEBUGTRACE ("xtensa_supply_gregset (..., regnum==%d, ...) \n", regnum);
772 if (regnum == gdbarch_pc_regnum (current_gdbarch) || regnum == -1)
773 regcache_raw_supply (rc,
774 gdbarch_pc_regnum (current_gdbarch),
776 if (regnum == gdbarch_ps_regnum (current_gdbarch) || regnum == -1)
777 regcache_raw_supply (rc, gdbarch_ps_regnum (current_gdbarch),
779 if (regnum == WB_REGNUM || regnum == -1)
780 regcache_raw_supply (rc, WB_REGNUM, (char *) ®s->windowbase);
781 if (regnum == WS_REGNUM || regnum == -1)
782 regcache_raw_supply (rc, WS_REGNUM, (char *) ®s->windowstart);
783 if (regnum == LBEG_REGNUM || regnum == -1)
784 regcache_raw_supply (rc, LBEG_REGNUM, (char *) ®s->lbeg);
785 if (regnum == LEND_REGNUM || regnum == -1)
786 regcache_raw_supply (rc, LEND_REGNUM, (char *) ®s->lend);
787 if (regnum == LCOUNT_REGNUM || regnum == -1)
788 regcache_raw_supply (rc, LCOUNT_REGNUM, (char *) ®s->lcount);
789 if (regnum == SAR_REGNUM || regnum == -1)
790 regcache_raw_supply (rc, SAR_REGNUM, (char *) ®s->sar);
791 if (regnum == EXCCAUSE_REGNUM || regnum == -1)
792 regcache_raw_supply (rc, EXCCAUSE_REGNUM, (char *) ®s->exccause);
793 if (regnum == EXCVADDR_REGNUM || regnum == -1)
794 regcache_raw_supply (rc, EXCVADDR_REGNUM, (char *) ®s->excvaddr);
795 if (regnum >= AR_BASE && regnum < AR_BASE + NUM_AREGS)
796 regcache_raw_supply (rc, regnum, (char *) ®s->ar[regnum - AR_BASE]);
797 else if (regnum == -1)
799 for (i = 0; i < NUM_AREGS; ++i)
800 regcache_raw_supply (rc, AR_BASE + i, (char *) ®s->ar[i]);
805 /* Xtensa register set. */
811 xtensa_supply_gregset
815 /* Return the appropriate register set for the core
816 section identified by SECT_NAME and SECT_SIZE. */
818 static const struct regset *
819 xtensa_regset_from_core_section (struct gdbarch *core_arch,
820 const char *sect_name,
823 DEBUGTRACE ("xtensa_regset_from_core_section "
824 "(..., sect_name==\"%s\", sect_size==%x) \n",
825 sect_name, sect_size);
827 if (strcmp (sect_name, ".reg") == 0
828 && sect_size >= sizeof(xtensa_elf_gregset_t))
829 return &xtensa_gregset;
835 /* Handling frames. */
837 /* Number of registers to save in case of Windowed ABI. */
838 #define XTENSA_NUM_SAVED_AREGS 12
840 /* Frame cache part for Windowed ABI. */
841 typedef struct xtensa_windowed_frame_cache
843 int wb; /* Base for this frame; -1 if not in regfile. */
844 int callsize; /* Call size to next frame. */
846 CORE_ADDR aregs[XTENSA_NUM_SAVED_AREGS];
847 } xtensa_windowed_frame_cache_t;
849 /* Call0 ABI Definitions. */
851 #define C0_MAXOPDS 3 /* Maximum number of operands for prologue analysis. */
852 #define C0_NREGS 16 /* Number of A-registers to track. */
853 #define C0_CLESV 12 /* Callee-saved registers are here and up. */
854 #define C0_SP 1 /* Register used as SP. */
855 #define C0_FP 15 /* Register used as FP. */
856 #define C0_RA 0 /* Register used as return address. */
857 #define C0_ARGS 2 /* Register used as first arg/retval. */
858 #define C0_NARGS 6 /* Number of A-regs for args/retvals. */
860 /* Each element of xtensa_call0_frame_cache.c0_rt[] describes for each
861 A-register where the current content of the reg came from (in terms
862 of an original reg and a constant). Negative values of c0_rt[n].fp_reg
863 mean that the orignal content of the register was saved to the stack.
864 c0_rt[n].fr.ofs is NOT the offset from the frame base because we don't
865 know where SP will end up until the entire prologue has been analyzed. */
867 #define C0_CONST -1 /* fr_reg value if register contains a constant. */
868 #define C0_INEXP -2 /* fr_reg value if inexpressible as reg + offset. */
869 #define C0_NOSTK -1 /* to_stk value if register has not been stored. */
871 extern xtensa_isa xtensa_default_isa;
873 typedef struct xtensa_c0reg
875 int fr_reg; /* original register from which register content
876 is derived, or C0_CONST, or C0_INEXP. */
877 int fr_ofs; /* constant offset from reg, or immediate value. */
878 int to_stk; /* offset from original SP to register (4-byte aligned),
879 or C0_NOSTK if register has not been saved. */
883 /* Frame cache part for Call0 ABI. */
884 typedef struct xtensa_call0_frame_cache
886 int c0_frmsz; /* Stack frame size. */
887 int c0_hasfp; /* Current frame uses frame pointer. */
888 int fp_regnum; /* A-register used as FP. */
889 int c0_fp; /* Actual value of frame pointer. */
890 xtensa_c0reg_t c0_rt[C0_NREGS]; /* Register tracking information. */
891 } xtensa_call0_frame_cache_t;
893 typedef struct xtensa_frame_cache
895 CORE_ADDR base; /* Stack pointer of the next frame. */
896 CORE_ADDR pc; /* PC at the entry point to the function. */
897 CORE_ADDR ra; /* The raw return address. */
898 CORE_ADDR ps; /* The PS register of the frame. */
899 CORE_ADDR prev_sp; /* Stack Pointer of the frame. */
900 int call0; /* It's a call0 framework (else windowed). */
903 xtensa_windowed_frame_cache_t wd; /* call0 == false. */
904 xtensa_call0_frame_cache_t c0; /* call0 == true. */
906 } xtensa_frame_cache_t;
909 static struct xtensa_frame_cache *
910 xtensa_alloc_frame_cache (int windowed)
912 xtensa_frame_cache_t *cache;
915 DEBUGTRACE ("xtensa_alloc_frame_cache ()\n");
917 cache = FRAME_OBSTACK_ZALLOC (xtensa_frame_cache_t);
924 cache->call0 = !windowed;
927 cache->c0.c0_frmsz = -1;
928 cache->c0.c0_hasfp = 0;
929 cache->c0.fp_regnum = -1;
930 cache->c0.c0_fp = -1;
932 for (i = 0; i < C0_NREGS; i++)
934 cache->c0.c0_rt[i].fr_reg = i;
935 cache->c0.c0_rt[i].fr_ofs = 0;
936 cache->c0.c0_rt[i].to_stk = C0_NOSTK;
942 cache->wd.callsize = -1;
944 for (i = 0; i < XTENSA_NUM_SAVED_AREGS; i++)
945 cache->wd.aregs[i] = -1;
952 xtensa_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
954 return address & ~15;
959 xtensa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
963 DEBUGTRACE ("xtensa_unwind_pc (next_frame = %p)\n", next_frame);
965 frame_unwind_register (next_frame, gdbarch_pc_regnum (current_gdbarch), buf);
967 DEBUGINFO ("[xtensa_unwind_pc] pc = 0x%08x\n", (unsigned int)
968 extract_typed_address (buf, builtin_type_void_func_ptr));
970 return extract_typed_address (buf, builtin_type_void_func_ptr);
974 static struct frame_id
975 xtensa_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
979 /* next_frame->prev is a dummy frame. Return a frame ID of that frame. */
981 DEBUGTRACE ("xtensa_unwind_dummy_id ()\n");
983 pc = frame_pc_unwind (next_frame);
984 fp = frame_unwind_register_unsigned (next_frame, A1_REGNUM);
986 /* Make dummy frame ID unique by adding a constant. */
987 return frame_id_build (fp + SP_ALIGNMENT, pc);
990 /* The key values to identify the frame using "cache" are
992 cache->base = SP of this frame;
993 cache->pc = entry-PC (entry point of the frame function);
994 cache->prev_sp = SP of the previous frame.
998 call0_frame_cache (struct frame_info *next_frame,
999 xtensa_frame_cache_t *cache,
1002 static struct xtensa_frame_cache *
1003 xtensa_frame_cache (struct frame_info *next_frame, void **this_cache)
1005 xtensa_frame_cache_t *cache;
1006 CORE_ADDR ra, wb, ws, pc, sp, ps;
1007 unsigned int ps_regnum = gdbarch_ps_regnum (current_gdbarch);
1011 DEBUGTRACE ("xtensa_frame_cache (next_frame %p, *this_cache %p)\n",
1012 next_frame, this_cache ? *this_cache : (void*)0xdeadbeef);
1017 windowed = windowing_enabled (xtensa_read_register (ps_regnum));
1019 /* Get pristine xtensa-frame. */
1020 cache = xtensa_alloc_frame_cache (windowed);
1021 *this_cache = cache;
1023 pc = frame_unwind_register_unsigned (next_frame,
1024 gdbarch_pc_regnum (current_gdbarch));
1028 /* Get WINDOWBASE, WINDOWSTART, and PS registers. */
1029 wb = frame_unwind_register_unsigned (next_frame, WB_REGNUM);
1030 ws = frame_unwind_register_unsigned (next_frame, WS_REGNUM);
1031 ps = frame_unwind_register_unsigned (next_frame, ps_regnum);
1033 op1 = read_memory_integer (pc, 1);
1034 if (XTENSA_IS_ENTRY (op1))
1036 int callinc = CALLINC (ps);
1037 ra = frame_unwind_register_unsigned (next_frame,
1038 A0_REGNUM + callinc * 4);
1040 DEBUGINFO("[xtensa_frame_cache] 'entry' at 0x%08x\n (callinc = %d)",
1043 /* ENTRY hasn't been executed yet, therefore callsize is still 0. */
1044 cache->wd.callsize = 0;
1047 cache->prev_sp = frame_unwind_register_unsigned (next_frame,
1052 ra = frame_unwind_register_unsigned (next_frame, A0_REGNUM);
1053 cache->wd.callsize = WINSIZE (ra);
1054 cache->wd.wb = (wb - cache->wd.callsize / 4) & (NUM_AREGS / 4 - 1);
1055 cache->wd.ws = ws & ~(1 << wb);
1058 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
1059 cache->ra = (cache->pc & 0xc0000000) | (ra & 0x3fffffff);
1060 cache->ps = (ps & ~PS_CALLINC_MASK)
1061 | ((WINSIZE(ra)/4) << PS_CALLINC_SHIFT);
1063 if (cache->wd.ws == 0)
1068 sp = frame_unwind_register_unsigned (next_frame, A1_REGNUM) - 16;
1070 for (i = 0; i < 4; i++, sp += 4)
1072 cache->wd.aregs[i] = sp;
1075 if (cache->wd.callsize > 4)
1077 /* Set A4...A7/A11. */
1078 /* Read an SP of the previous frame. */
1079 sp = (CORE_ADDR) read_memory_integer (sp - 12, 4);
1080 sp -= cache->wd.callsize * 4;
1082 for ( /* i=4 */ ; i < cache->wd.callsize; i++, sp += 4)
1084 cache->wd.aregs[i] = sp;
1089 if ((cache->prev_sp == 0) && ( ra != 0 ))
1090 /* If RA is equal to 0 this frame is an outermost frame. Leave
1091 cache->prev_sp unchanged marking the boundary of the frame stack. */
1093 if (cache->wd.ws == 0)
1095 /* Register window overflow already happened.
1096 We can read caller's SP from the proper spill loction. */
1098 read_memory_integer (cache->wd.aregs[1],
1099 register_size (current_gdbarch,
1104 /* Read caller's frame SP directly from the previous window. */
1105 int regnum = AREG_NUMBER (A1_REGNUM, cache->wd.wb);
1107 cache->prev_sp = xtensa_read_register (regnum);
1111 else /* Call0 framework. */
1113 call0_frame_cache (next_frame, cache, pc);
1116 cache->base = frame_unwind_register_unsigned (next_frame,A1_REGNUM);
1122 xtensa_frame_this_id (struct frame_info *next_frame,
1124 struct frame_id *this_id)
1126 struct xtensa_frame_cache *cache =
1127 xtensa_frame_cache (next_frame, this_cache);
1130 DEBUGTRACE ("xtensa_frame_this_id (next 0x%08x, *this 0x%08x)\n",
1131 (unsigned int) next_frame, (unsigned int) *this_cache);
1133 if (cache->prev_sp == 0)
1136 id = frame_id_build (cache->prev_sp, cache->pc);
1137 if (frame_id_eq (id, get_frame_id(next_frame)))
1140 Frame stack is corrupted. That could happen because of \
1141 setting register(s) from GDB or stopping execution \
1142 inside exception handler. Frame backtracing has stopped. \
1143 It can make some GDB commands work inappropriately.\n"));
1151 call0_frame_get_reg_at_entry (struct frame_info *next_frame,
1152 struct xtensa_frame_cache *cache,
1155 enum lval_type *lval,
1160 int reg = (regnum >= AR_BASE && regnum <= (AR_BASE + C0_NREGS))
1161 ? regnum - AR_BASE : regnum;
1163 /* Determine stack pointer on entry to this function, based on FP. */
1164 spe = cache->c0.c0_fp - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1166 /* If register was saved to the stack frame in the prologue, retrieve it. */
1167 stkofs = cache->c0.c0_rt[reg].to_stk;
1168 if (stkofs != C0_NOSTK)
1170 *lval = lval_memory;
1171 *addrp = spe + stkofs;
1174 read_memory (*addrp, valuep, register_size (current_gdbarch, regnum));
1179 /* If not callee-saved or if known to have been overwritten, give up. */
1181 || cache->c0.c0_rt[reg].fr_reg != reg
1182 || cache->c0.c0_rt[reg].fr_ofs != 0)
1185 if (get_frame_type (next_frame) != NORMAL_FRAME)
1186 /* TODO: Do we need a special case for DUMMY_FRAME here? */
1189 return call0_frame_get_reg_at_entry (get_next_frame(next_frame),
1190 cache, regnum, addrp, lval, valuep);
1194 xtensa_frame_prev_register (struct frame_info *next_frame,
1198 enum lval_type *lvalp,
1203 struct xtensa_frame_cache *cache =
1204 xtensa_frame_cache (next_frame, this_cache);
1205 CORE_ADDR saved_reg = 0;
1208 DEBUGTRACE ("xtensa_frame_prev_register (next 0x%08x, "
1209 "*this 0x%08x, regnum %d (%s), ...)\n",
1210 (unsigned int) next_frame,
1211 *this_cache ? (unsigned int) *this_cache : 0, regnum,
1212 xtensa_register_name (regnum));
1214 if (regnum ==gdbarch_pc_regnum (current_gdbarch))
1215 saved_reg = cache->ra;
1216 else if (regnum == A1_REGNUM)
1217 saved_reg = cache->prev_sp;
1218 else if (!cache->call0)
1220 if (regnum == WS_REGNUM)
1222 if (cache->wd.ws != 0)
1223 saved_reg = cache->wd.ws;
1225 saved_reg = 1 << cache->wd.wb;
1227 else if (regnum == WB_REGNUM)
1228 saved_reg = cache->wd.wb;
1229 else if (regnum == gdbarch_ps_regnum (current_gdbarch))
1230 saved_reg = cache->ps;
1244 store_unsigned_integer (valuep, 4, saved_reg);
1249 if (!cache->call0) /* Windowed ABI. */
1251 /* Convert A-register numbers to AR-register numbers. */
1252 if (regnum >= A0_REGNUM && regnum <= A15_REGNUM)
1253 regnum = AREG_NUMBER (regnum, cache->wd.wb);
1255 /* Check if AR-register has been saved to stack. */
1256 if (regnum >= AR_BASE && regnum <= (AR_BASE + NUM_AREGS))
1258 int areg = regnum - AR_BASE - (cache->wd.wb * 4);
1261 && areg < XTENSA_NUM_SAVED_AREGS
1262 && cache->wd.aregs[areg] != -1)
1265 *lvalp = lval_memory;
1266 *addrp = cache->wd.aregs[areg];
1270 read_memory (*addrp, valuep,
1271 register_size (current_gdbarch, regnum));
1273 DEBUGINFO ("[xtensa_frame_prev_register] register on stack\n");
1278 else /* Call0 ABI. */
1280 int reg = (regnum >= AR_BASE && regnum <= (AR_BASE + C0_NREGS))
1281 ? regnum - AR_BASE : regnum;
1288 /* If register was saved in the prologue, retrieve it. */
1289 stkofs = cache->c0.c0_rt[reg].to_stk;
1290 if (stkofs != C0_NOSTK)
1292 /* Determine SP on entry based on FP. */
1293 spe = cache->c0.c0_fp
1294 - cache->c0.c0_rt[cache->c0.fp_regnum].fr_ofs;
1296 *lvalp = lval_memory;
1297 *addrp = spe + stkofs;
1301 read_memory (*addrp, valuep,
1302 register_size (current_gdbarch, regnum));
1304 DEBUGINFO ("[xtensa_frame_prev_register] register on stack\n");
1310 /* All other registers have been either saved to
1311 the stack or are still alive in the processor. */
1314 *lvalp = lval_register;
1318 frame_unwind_register (next_frame, (*realnump), valuep);
1322 static const struct frame_unwind
1323 xtensa_frame_unwind =
1326 xtensa_frame_this_id,
1327 xtensa_frame_prev_register
1330 static const struct frame_unwind *
1331 xtensa_frame_sniffer (struct frame_info *next_frame)
1333 return &xtensa_frame_unwind;
1337 xtensa_frame_base_address (struct frame_info *next_frame, void **this_cache)
1339 struct xtensa_frame_cache *cache =
1340 xtensa_frame_cache (next_frame, this_cache);
1345 static const struct frame_base
1348 &xtensa_frame_unwind,
1349 xtensa_frame_base_address,
1350 xtensa_frame_base_address,
1351 xtensa_frame_base_address
1356 xtensa_extract_return_value (struct type *type,
1357 struct regcache *regcache,
1360 bfd_byte *valbuf = dst;
1361 int len = TYPE_LENGTH (type);
1366 DEBUGTRACE ("xtensa_extract_return_value (...)\n");
1368 gdb_assert(len > 0);
1370 if (CALL_ABI != CallAbiCall0Only)
1372 /* First, we have to find the caller window in the register file. */
1373 regcache_raw_read_unsigned (regcache,
1374 gdbarch_pc_regnum (current_gdbarch), &pc);
1375 callsize = extract_call_winsize (pc);
1377 /* On Xtensa, we can return up to 4 words (or 2 for call12). */
1378 if (len > (callsize > 8 ? 8 : 16))
1379 internal_error (__FILE__, __LINE__,
1380 _("cannot extract return value of %d bytes long"), len);
1382 /* Get the register offset of the return
1383 register (A2) in the caller window. */
1384 regcache_raw_read_unsigned (regcache, WB_REGNUM, &wb);
1385 areg = AREG_NUMBER(A2_REGNUM + callsize, wb);
1389 /* No windowing hardware - Call0 ABI. */
1390 areg = A0_REGNUM + C0_ARGS;
1393 DEBUGINFO ("[xtensa_extract_return_value] areg %d len %d\n", areg, len);
1395 if (len < 4 && gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1398 for (; len > 0; len -= 4, areg++, valbuf += 4)
1401 regcache_raw_read_part (regcache, areg, offset, len, valbuf);
1403 regcache_raw_read (regcache, areg, valbuf);
1409 xtensa_store_return_value (struct type *type,
1410 struct regcache *regcache,
1413 const bfd_byte *valbuf = dst;
1417 int len = TYPE_LENGTH (type);
1420 DEBUGTRACE ("xtensa_store_return_value (...)\n");
1422 if (CALL_ABI != CallAbiCall0Only)
1424 regcache_raw_read_unsigned (regcache, WB_REGNUM, &wb);
1425 regcache_raw_read_unsigned (regcache,
1426 gdbarch_pc_regnum (current_gdbarch), &pc);
1427 callsize = extract_call_winsize (pc);
1429 if (len > (callsize > 8 ? 8 : 16))
1430 internal_error (__FILE__, __LINE__,
1431 _("unimplemented for this length: %d"),
1432 TYPE_LENGTH (type));
1433 areg = AREG_NUMBER (A2_REGNUM + callsize, wb);
1435 DEBUGTRACE ("[xtensa_store_return_value] callsize %d wb %d\n",
1436 callsize, (int) wb);
1440 areg = A0_REGNUM + C0_ARGS;
1443 if (len < 4 && gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1446 for (; len > 0; len -= 4, areg++, valbuf += 4)
1449 regcache_raw_write_part (regcache, areg, offset, len, valbuf);
1451 regcache_raw_write (regcache, areg, valbuf);
1456 static enum return_value_convention
1457 xtensa_return_value (struct gdbarch *gdbarch,
1458 struct type *valtype,
1459 struct regcache *regcache,
1461 const gdb_byte *writebuf)
1463 /* Structures up to 16 bytes are returned in registers. */
1465 int struct_return = ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1466 || TYPE_CODE (valtype) == TYPE_CODE_UNION
1467 || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1468 && TYPE_LENGTH (valtype) > 16);
1471 return RETURN_VALUE_STRUCT_CONVENTION;
1473 DEBUGTRACE ("xtensa_return_value(...)\n");
1475 if (writebuf != NULL)
1477 xtensa_store_return_value (valtype, regcache, writebuf);
1480 if (readbuf != NULL)
1482 gdb_assert (!struct_return);
1483 xtensa_extract_return_value (valtype, regcache, readbuf);
1485 return RETURN_VALUE_REGISTER_CONVENTION;
1492 xtensa_push_dummy_call (struct gdbarch *gdbarch,
1493 struct value *function,
1494 struct regcache *regcache,
1497 struct value **args,
1500 CORE_ADDR struct_addr)
1503 int size, onstack_size;
1504 gdb_byte *buf = (gdb_byte *) alloca (16);
1506 struct argument_info
1508 const bfd_byte *contents;
1510 int onstack; /* onstack == 0 => in reg */
1511 int align; /* alignment */
1514 int offset; /* stack offset if on stack */
1515 int regno; /* regno if in register */
1519 struct argument_info *arg_info =
1520 (struct argument_info *) alloca (nargs * sizeof (struct argument_info));
1524 DEBUGTRACE ("xtensa_push_dummy_call (...)\n");
1526 if (xtensa_debug_level > 3)
1529 DEBUGINFO ("[xtensa_push_dummy_call] nargs = %d\n", nargs);
1530 DEBUGINFO ("[xtensa_push_dummy_call] sp=0x%x, struct_return=%d, "
1531 "struct_addr=0x%x\n",
1532 (int) sp, (int) struct_return, (int) struct_addr);
1534 for (i = 0; i < nargs; i++)
1536 struct value *arg = args[i];
1537 struct type *arg_type = check_typedef (value_type (arg));
1538 fprintf_unfiltered (gdb_stdlog, "%2d: 0x%08x %3d ",
1539 i, (int) arg, TYPE_LENGTH (arg_type));
1540 switch (TYPE_CODE (arg_type))
1543 fprintf_unfiltered (gdb_stdlog, "int");
1545 case TYPE_CODE_STRUCT:
1546 fprintf_unfiltered (gdb_stdlog, "struct");
1549 fprintf_unfiltered (gdb_stdlog, "%3d", TYPE_CODE (arg_type));
1552 fprintf_unfiltered (gdb_stdlog, " 0x%08x\n",
1553 (unsigned int) value_contents (arg));
1557 /* First loop: collect information.
1558 Cast into type_long. (This shouldn't happen often for C because
1559 GDB already does this earlier.) It's possible that GDB could
1560 do it all the time but it's harmless to leave this code here. */
1567 size = REGISTER_SIZE;
1569 for (i = 0; i < nargs; i++)
1571 struct argument_info *info = &arg_info[i];
1572 struct value *arg = args[i];
1573 struct type *arg_type = check_typedef (value_type (arg));
1575 switch (TYPE_CODE (arg_type))
1578 case TYPE_CODE_BOOL:
1579 case TYPE_CODE_CHAR:
1580 case TYPE_CODE_RANGE:
1581 case TYPE_CODE_ENUM:
1583 /* Cast argument to long if necessary as the mask does it too. */
1584 if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1586 arg_type = builtin_type_long;
1587 arg = value_cast (arg_type, arg);
1589 /* Aligment is equal to the type length for the basic types. */
1590 info->align = TYPE_LENGTH (arg_type);
1595 /* Align doubles correctly. */
1596 if (TYPE_LENGTH (arg_type) == TYPE_LENGTH (builtin_type_double))
1597 info->align = TYPE_LENGTH (builtin_type_double);
1599 info->align = TYPE_LENGTH (builtin_type_long);
1602 case TYPE_CODE_STRUCT:
1604 info->align = TYPE_LENGTH (builtin_type_long);
1607 info->length = TYPE_LENGTH (arg_type);
1608 info->contents = value_contents (arg);
1610 /* Align size and onstack_size. */
1611 size = (size + info->align - 1) & ~(info->align - 1);
1612 onstack_size = (onstack_size + info->align - 1) & ~(info->align - 1);
1614 if (size + info->length > REGISTER_SIZE * ARG_NOF)
1617 info->u.offset = onstack_size;
1618 onstack_size += info->length;
1623 info->u.regno = ARG_1ST + size / REGISTER_SIZE;
1625 size += info->length;
1628 /* Adjust the stack pointer and align it. */
1629 sp = align_down (sp - onstack_size, SP_ALIGNMENT);
1631 /* Simulate MOVSP, if Windowed ABI. */
1632 if ((CALL_ABI != CallAbiCall0Only) && (sp != osp))
1634 read_memory (osp - 16, buf, 16);
1635 write_memory (sp - 16, buf, 16);
1638 /* Second Loop: Load arguments. */
1642 store_unsigned_integer (buf, REGISTER_SIZE, struct_addr);
1643 regcache_cooked_write (regcache, ARG_1ST, buf);
1646 for (i = 0; i < nargs; i++)
1648 struct argument_info *info = &arg_info[i];
1652 int n = info->length;
1653 CORE_ADDR offset = sp + info->u.offset;
1655 /* Odd-sized structs are aligned to the lower side of a memory
1656 word in big-endian mode and require a shift. This only
1657 applies for structures smaller than one word. */
1659 if (n < REGISTER_SIZE
1660 && gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1661 offset += (REGISTER_SIZE - n);
1663 write_memory (offset, info->contents, info->length);
1668 int n = info->length;
1669 const bfd_byte *cp = info->contents;
1670 int r = info->u.regno;
1672 /* Odd-sized structs are aligned to the lower side of registers in
1673 big-endian mode and require a shift. The odd-sized leftover will
1674 be at the end. Note that this is only true for structures smaller
1675 than REGISTER_SIZE; for larger odd-sized structures the excess
1676 will be left-aligned in the register on both endiannesses. */
1678 if (n < REGISTER_SIZE
1679 && gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1681 ULONGEST v = extract_unsigned_integer (cp, REGISTER_SIZE);
1682 v = v >> ((REGISTER_SIZE - n) * TARGET_CHAR_BIT);
1684 store_unsigned_integer (buf, REGISTER_SIZE, v);
1685 regcache_cooked_write (regcache, r, buf);
1687 cp += REGISTER_SIZE;
1694 regcache_cooked_write (regcache, r, cp);
1696 cp += REGISTER_SIZE;
1703 /* Set the return address of dummy frame to the dummy address.
1704 The return address for the current function (in A0) is
1705 saved in the dummy frame, so we can savely overwrite A0 here. */
1707 if (CALL_ABI != CallAbiCall0Only)
1709 ra = (bp_addr & 0x3fffffff) | 0x40000000;
1710 regcache_raw_read (regcache, gdbarch_ps_regnum (current_gdbarch), buf);
1711 ps = extract_unsigned_integer (buf, 4) & ~0x00030000;
1712 regcache_cooked_write_unsigned (regcache, A4_REGNUM, ra);
1713 regcache_cooked_write_unsigned (regcache,
1714 gdbarch_ps_regnum (current_gdbarch),
1719 /* Simulate CALL0: write RA into A0 register. */
1720 regcache_cooked_write_unsigned (regcache, A0_REGNUM, bp_addr);
1723 /* Set new stack pointer and return it. */
1724 regcache_cooked_write_unsigned (regcache, A1_REGNUM, sp);
1725 /* Make dummy frame ID unique by adding a constant. */
1726 return sp + SP_ALIGNMENT;
1730 /* Return a breakpoint for the current location of PC. We always use
1731 the density version if we have density instructions (regardless of the
1732 current instruction at PC), and use regular instructions otherwise. */
1734 #define BIG_BREAKPOINT { 0x00, 0x04, 0x00 }
1735 #define LITTLE_BREAKPOINT { 0x00, 0x40, 0x00 }
1736 #define DENSITY_BIG_BREAKPOINT { 0xd2, 0x0f }
1737 #define DENSITY_LITTLE_BREAKPOINT { 0x2d, 0xf0 }
1739 static const unsigned char *
1740 xtensa_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1742 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
1743 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
1744 static unsigned char density_big_breakpoint[] = DENSITY_BIG_BREAKPOINT;
1745 static unsigned char density_little_breakpoint[] = DENSITY_LITTLE_BREAKPOINT;
1747 DEBUGTRACE ("xtensa_breakpoint_from_pc (pc = 0x%08x)\n", (int) *pcptr);
1749 if (ISA_USE_DENSITY_INSTRUCTIONS)
1751 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1753 *lenptr = sizeof (density_big_breakpoint);
1754 return density_big_breakpoint;
1758 *lenptr = sizeof (density_little_breakpoint);
1759 return density_little_breakpoint;
1764 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
1766 *lenptr = sizeof (big_breakpoint);
1767 return big_breakpoint;
1771 *lenptr = sizeof (little_breakpoint);
1772 return little_breakpoint;
1777 /* Call0 ABI support routines. */
1779 /* Call0 opcode class. Opcodes are preclassified according to what they
1780 mean for Call0 prologue analysis, and their number of significant operands.
1781 The purpose of this is to simplify prologue analysis by separating
1782 instruction decoding (libisa) from the semantics of prologue analysis. */
1785 c0opc_illegal, /* Unknown to libisa (invalid) or 'ill' opcode. */
1786 c0opc_uninteresting, /* Not interesting for Call0 prologue analysis. */
1787 c0opc_flow, /* Flow control insn. */
1788 c0opc_entry, /* ENTRY indicates non-Call0 prologue. */
1789 c0opc_break, /* Debugger software breakpoints. */
1790 c0opc_add, /* Adding two registers. */
1791 c0opc_addi, /* Adding a register and an immediate. */
1792 c0opc_sub, /* Subtracting a register from a register. */
1793 c0opc_mov, /* Moving a register to a register. */
1794 c0opc_movi, /* Moving an immediate to a register. */
1795 c0opc_l32r, /* Loading a literal. */
1796 c0opc_s32i, /* Storing word at fixed offset from a base register. */
1797 c0opc_NrOf /* Number of opcode classifications. */
1801 /* Classify an opcode based on what it means for Call0 prologue analysis. */
1803 static xtensa_insn_kind
1804 call0_classify_opcode (xtensa_isa isa, xtensa_opcode opc)
1806 const char *opcname;
1807 xtensa_insn_kind opclass = c0opc_uninteresting;
1809 DEBUGTRACE ("call0_classify_opcode (..., opc = %d)\n", opc);
1811 /* Get opcode name and handle special classifications. */
1813 opcname = xtensa_opcode_name (isa, opc);
1816 || strcasecmp (opcname, "ill") == 0
1817 || strcasecmp (opcname, "ill.n") == 0)
1818 opclass = c0opc_illegal;
1819 else if (strcasecmp (opcname, "break") == 0
1820 || strcasecmp (opcname, "break.n") == 0)
1821 opclass = c0opc_break;
1822 else if (strcasecmp (opcname, "entry") == 0)
1823 opclass = c0opc_entry;
1824 else if (xtensa_opcode_is_branch (isa, opc) > 0
1825 || xtensa_opcode_is_jump (isa, opc) > 0
1826 || xtensa_opcode_is_loop (isa, opc) > 0
1827 || xtensa_opcode_is_call (isa, opc) > 0
1828 || strcasecmp (opcname, "simcall") == 0
1829 || strcasecmp (opcname, "syscall") == 0)
1830 opclass = c0opc_flow;
1832 /* Also, classify specific opcodes that need to be tracked. */
1833 else if (strcasecmp (opcname, "add") == 0
1834 || strcasecmp (opcname, "add.n") == 0)
1835 opclass = c0opc_add;
1836 else if (strcasecmp (opcname, "addi") == 0
1837 || strcasecmp (opcname, "addi.n") == 0
1838 || strcasecmp (opcname, "addmi") == 0)
1839 opclass = c0opc_addi;
1840 else if (strcasecmp (opcname, "sub") == 0)
1841 opclass = c0opc_sub;
1842 else if (strcasecmp (opcname, "mov.n") == 0
1843 || strcasecmp (opcname, "or") == 0) /* Could be 'mov' asm macro. */
1844 opclass = c0opc_mov;
1845 else if (strcasecmp (opcname, "movi") == 0
1846 || strcasecmp (opcname, "movi.n") == 0)
1847 opclass = c0opc_movi;
1848 else if (strcasecmp (opcname, "l32r") == 0)
1849 opclass = c0opc_l32r;
1850 else if (strcasecmp (opcname, "s32i") == 0
1851 || strcasecmp (opcname, "s32i.n") == 0)
1852 opclass = c0opc_s32i;
1857 /* Tracks register movement/mutation for a given operation, which may
1858 be within a bundle. Updates the destination register tracking info
1859 accordingly. The pc is needed only for pc-relative load instructions
1860 (eg. l32r). The SP register number is needed to identify stores to
1864 call0_track_op (xtensa_c0reg_t dst[], xtensa_c0reg_t src[],
1865 xtensa_insn_kind opclass, int nods, unsigned odv[],
1866 CORE_ADDR pc, int spreg)
1868 unsigned litbase, litaddr, litval;
1873 /* 3 operands: dst, src, imm. */
1874 gdb_assert (nods == 3);
1875 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1876 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + odv[2];
1879 /* 3 operands: dst, src1, src2. */
1880 gdb_assert (nods == 3);
1881 if (src[odv[1]].fr_reg == C0_CONST)
1883 dst[odv[0]].fr_reg = src[odv[2]].fr_reg;
1884 dst[odv[0]].fr_ofs = src[odv[2]].fr_ofs + src[odv[1]].fr_ofs;
1886 else if (src[odv[2]].fr_reg == C0_CONST)
1888 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1889 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs + src[odv[2]].fr_ofs;
1891 else dst[odv[0]].fr_reg = C0_INEXP;
1894 /* 3 operands: dst, src1, src2. */
1895 gdb_assert (nods == 3);
1896 if (src[odv[2]].fr_reg == C0_CONST)
1898 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1899 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs - src[odv[2]].fr_ofs;
1901 else dst[odv[0]].fr_reg = C0_INEXP;
1904 /* 2 operands: dst, src [, src]. */
1905 gdb_assert (nods == 2);
1906 dst[odv[0]].fr_reg = src[odv[1]].fr_reg;
1907 dst[odv[0]].fr_ofs = src[odv[1]].fr_ofs;
1910 /* 2 operands: dst, imm. */
1911 gdb_assert (nods == 2);
1912 dst[odv[0]].fr_reg = C0_CONST;
1913 dst[odv[0]].fr_ofs = odv[1];
1916 /* 2 operands: dst, literal offset. */
1917 gdb_assert (nods == 2);
1918 /* litbase = xtensa_get_litbase (pc); can be also used. */
1919 litbase = (LITBASE_REGNUM == -1)
1920 ? 0 : xtensa_read_register (LITBASE_REGNUM);
1921 litaddr = litbase & 1
1922 ? (litbase & ~1) + (signed)odv[1]
1923 : (pc + 3 + (signed)odv[1]) & ~3;
1924 litval = read_memory_integer(litaddr, 4);
1925 dst[odv[0]].fr_reg = C0_CONST;
1926 dst[odv[0]].fr_ofs = litval;
1929 /* 3 operands: value, base, offset. */
1930 gdb_assert (nods == 3 && spreg >= 0 && spreg < C0_NREGS);
1931 if (src[odv[1]].fr_reg == spreg /* Store to stack frame. */
1932 && (src[odv[1]].fr_ofs & 3) == 0 /* Alignment preserved. */
1933 && src[odv[0]].fr_reg >= 0 /* Value is from a register. */
1934 && src[odv[0]].fr_ofs == 0 /* Value hasn't been modified. */
1935 && src[src[odv[0]].fr_reg].to_stk == C0_NOSTK) /* First time. */
1937 /* ISA encoding guarantees alignment. But, check it anyway. */
1938 gdb_assert ((odv[2] & 3) == 0);
1939 dst[src[odv[0]].fr_reg].to_stk = src[odv[1]].fr_ofs + odv[2];
1947 /* Analyze prologue of the function at start address to determine if it uses
1948 the Call0 ABI, and if so track register moves and linear modifications
1949 in the prologue up to the PC or just beyond the prologue, whichever is first.
1950 An 'entry' instruction indicates non-Call0 ABI and the end of the prologue.
1951 The prologue may overlap non-prologue instructions but is guaranteed to end
1952 by the first flow-control instruction (jump, branch, call or return).
1953 Since an optimized function may move information around and change the
1954 stack frame arbitrarily during the prologue, the information is guaranteed
1955 valid only at the point in the function indicated by the PC.
1956 May be used to skip the prologue or identify the ABI, w/o tracking.
1958 Returns: Address of first instruction after prologue, or PC (whichever
1959 is first), or 0, if decoding failed (in libisa).
1961 start Start address of function/prologue.
1962 pc Program counter to stop at. Use 0 to continue to end of prologue.
1963 If 0, avoids infinite run-on in corrupt code memory by bounding
1964 the scan to the end of the function if that can be determined.
1965 nregs Number of general registers to track (size of rt[] array).
1967 rt[] Array[nregs] of xtensa_c0reg structures for register tracking info.
1968 If NULL, registers are not tracked.
1970 call0 If != NULL, *call0 is set non-zero if Call0 ABI used, else 0
1971 (more accurately, non-zero until 'entry' insn is encountered).
1973 Note that these may produce useful results even if decoding fails
1974 because they begin with default assumptions that analysis may change. */
1977 call0_analyze_prologue (CORE_ADDR start, CORE_ADDR pc,
1978 int nregs, xtensa_c0reg_t rt[], int *call0)
1980 CORE_ADDR ia; /* Current insn address in prologue. */
1981 CORE_ADDR ba = 0; /* Current address at base of insn buffer. */
1982 CORE_ADDR bt; /* Current address at top+1 of insn buffer. */
1983 #define BSZ 32 /* Instruction buffer size. */
1984 char ibuf[BSZ]; /* Instruction buffer for decoding prologue. */
1985 xtensa_isa isa; /* libisa ISA handle. */
1986 xtensa_insnbuf ins, slot; /* libisa handle to decoded insn, slot. */
1987 xtensa_format ifmt; /* libisa instruction format. */
1988 int ilen, islots, is; /* Instruction length, nbr slots, current slot. */
1989 xtensa_opcode opc; /* Opcode in current slot. */
1990 xtensa_insn_kind opclass; /* Opcode class for Call0 prologue analysis. */
1991 int nods; /* Opcode number of operands. */
1992 unsigned odv[C0_MAXOPDS]; /* Operand values in order provided by libisa. */
1993 xtensa_c0reg_t *rtmp; /* Register tracking info snapshot. */
1994 int j; /* General loop counter. */
1995 int fail = 0; /* Set non-zero and exit, if decoding fails. */
1996 CORE_ADDR body_pc; /* The PC for the first non-prologue insn. */
1997 CORE_ADDR end_pc; /* The PC for the lust function insn. */
1999 struct symtab_and_line prologue_sal;
2001 DEBUGTRACE ("call0_analyze_prologue (start = 0x%08x, pc = 0x%08x, ...)\n",
2002 (int)start, (int)pc);
2004 /* Try to limit the scan to the end of the function if a non-zero pc
2005 arg was not supplied to avoid probing beyond the end of valid memory.
2006 If memory is full of garbage that classifies as c0opc_uninteresting.
2007 If this fails (eg. if no symbols) pc ends up 0 as it was.
2008 Intialize the Call0 frame and register tracking info.
2009 Assume it's Call0 until an 'entry' instruction is encountered.
2010 Assume we may be in the prologue until we hit a flow control instr. */
2016 /* Find out, if we have an information about the prologue from DWARF. */
2017 prologue_sal = find_pc_line (start, 0);
2018 if (prologue_sal.line != 0) /* Found debug info. */
2019 body_pc = prologue_sal.end;
2021 /* If we are going to analyze the prologue in general without knowing about
2022 the current PC, make the best assumtion for the end of the prologue. */
2025 find_pc_partial_function (start, 0, NULL, &end_pc);
2026 body_pc = min (end_pc, body_pc);
2029 body_pc = min (pc, body_pc);
2036 rtmp = (xtensa_c0reg_t*) alloca(nregs * sizeof(xtensa_c0reg_t));
2037 /* rt is already initialized in xtensa_alloc_frame_cache(). */
2041 isa = xtensa_default_isa;
2042 gdb_assert (BSZ >= xtensa_isa_maxlength (isa));
2043 ins = xtensa_insnbuf_alloc (isa);
2044 slot = xtensa_insnbuf_alloc (isa);
2046 for (ia = start, bt = ia; ia < body_pc ; ia += ilen)
2048 /* (Re)fill instruction buffer from memory if necessary, but do not
2049 read memory beyond PC to be sure we stay within text section
2050 (this protection only works if a non-zero pc is supplied). */
2052 if (ia + xtensa_isa_maxlength (isa) > bt)
2055 bt = (ba + BSZ) < body_pc ? ba + BSZ : body_pc;
2056 read_memory (ba, ibuf, bt - ba);
2059 /* Decode format information. */
2061 xtensa_insnbuf_from_chars (isa, ins, &ibuf[ia-ba], 0);
2062 ifmt = xtensa_format_decode (isa, ins);
2063 if (ifmt == XTENSA_UNDEFINED)
2068 ilen = xtensa_format_length (isa, ifmt);
2069 if (ilen == XTENSA_UNDEFINED)
2074 islots = xtensa_format_num_slots (isa, ifmt);
2075 if (islots == XTENSA_UNDEFINED)
2081 /* Analyze a bundle or a single instruction, using a snapshot of
2082 the register tracking info as input for the entire bundle so that
2083 register changes do not take effect within this bundle. */
2085 for (j = 0; j < nregs; ++j)
2088 for (is = 0; is < islots; ++is)
2090 /* Decode a slot and classify the opcode. */
2092 fail = xtensa_format_get_slot (isa, ifmt, is, ins, slot);
2096 opc = xtensa_opcode_decode (isa, ifmt, is, slot);
2097 DEBUGVERB ("[call0_analyze_prologue] instr addr = 0x%08x, opc = %d\n",
2099 if (opc == XTENSA_UNDEFINED)
2100 opclass = c0opc_illegal;
2102 opclass = call0_classify_opcode (isa, opc);
2104 /* Decide whether to track this opcode, ignore it, or bail out. */
2113 case c0opc_uninteresting:
2122 ia += ilen; /* Skip over 'entry' insn. */
2130 /* Only expected opcodes should get this far. */
2134 /* Extract and decode the operands. */
2135 nods = xtensa_opcode_num_operands (isa, opc);
2136 if (nods == XTENSA_UNDEFINED)
2142 for (j = 0; j < nods && j < C0_MAXOPDS; ++j)
2144 fail = xtensa_operand_get_field (isa, opc, j, ifmt,
2149 fail = xtensa_operand_decode (isa, opc, j, &odv[j]);
2154 /* Check operands to verify use of 'mov' assembler macro. */
2155 if (opclass == c0opc_mov && nods == 3)
2157 if (odv[2] == odv[1])
2161 opclass = c0opc_uninteresting;
2166 /* Track register movement and modification for this operation. */
2167 call0_track_op (rt, rtmp, opclass, nods, odv, ia, 1);
2171 DEBUGVERB ("[call0_analyze_prologue] stopped at instr addr 0x%08x, %s\n",
2172 (unsigned)ia, fail ? "failed" : "succeeded");
2173 xtensa_insnbuf_free(isa, slot);
2174 xtensa_insnbuf_free(isa, ins);
2175 return fail ? 0 : ia;
2178 /* Initialize frame cache for the current frame. The "next_frame" is the next
2179 one relative to current frame. "cache" is the pointer to the data structure
2180 we have to initialize. "pc" is curretnt PC. */
2183 call0_frame_cache (struct frame_info *next_frame,
2184 xtensa_frame_cache_t *cache, CORE_ADDR pc)
2186 CORE_ADDR start_pc; /* The beginning of the function. */
2187 CORE_ADDR body_pc=UINT_MAX; /* PC, where prologue analysis stopped. */
2188 CORE_ADDR sp, fp, ra;
2189 int fp_regnum, c0_hasfp, c0_frmsz, prev_sp, to_stk;
2191 /* Find the beginning of the prologue of the function containing the PC
2192 and analyze it up to the PC or the end of the prologue. */
2194 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
2196 body_pc = call0_analyze_prologue (start_pc, pc, C0_NREGS,
2197 &cache->c0.c0_rt[0],
2201 sp = frame_unwind_register_unsigned (next_frame, A1_REGNUM);
2202 fp = sp; /* Assume FP == SP until proven otherwise. */
2204 /* Get the frame information and FP (if used) at the current PC.
2205 If PC is in the prologue, the prologue analysis is more reliable
2206 than DWARF info. We don't not know for sure if PC is in the prologue,
2207 but we know no calls have yet taken place, so we can almost
2208 certainly rely on the prologue analysis. */
2212 /* Prologue analysis was successful up to the PC.
2213 It includes the cases when PC == START_PC. */
2214 c0_hasfp = cache->c0.c0_rt[C0_FP].fr_reg == C0_SP;
2215 /* c0_hasfp == true means there is a frame pointer because
2216 we analyzed the prologue and found that cache->c0.c0_rt[C0_FP]
2217 was derived from SP. Otherwise, it would be C0_FP. */
2218 fp_regnum = c0_hasfp ? C0_FP : C0_SP;
2219 c0_frmsz = - cache->c0.c0_rt[fp_regnum].fr_ofs;
2220 fp_regnum += A0_BASE;
2222 else /* No data from the prologue analysis. */
2225 fp_regnum = A0_BASE + C0_SP;
2230 prev_sp = fp + c0_frmsz;
2232 /* Frame size from debug info or prologue tracking does not account for
2233 alloca() and other dynamic allocations. Adjust frame size by FP - SP. */
2236 fp = frame_unwind_register_unsigned (next_frame, fp_regnum);
2238 /* Recalculate previous SP. */
2239 prev_sp = fp + c0_frmsz;
2240 /* Update the stack frame size. */
2241 c0_frmsz += fp - sp;
2244 /* Get the return address (RA) from the stack if saved,
2245 or try to get it from a register. */
2247 to_stk = cache->c0.c0_rt[C0_RA].to_stk;
2248 if (to_stk != C0_NOSTK)
2250 read_memory_integer (sp + c0_frmsz + cache->c0.c0_rt[C0_RA].to_stk, 4);
2252 else if (cache->c0.c0_rt[C0_RA].fr_reg == C0_CONST
2253 && cache->c0.c0_rt[C0_RA].fr_ofs == 0)
2255 /* Special case for terminating backtrace at a function that wants to
2256 be seen as the outermost. Such a function will clear it's RA (A0)
2257 register to 0 in the prologue instead of saving its original value. */
2262 /* RA was copied to another register or (before any function call) may
2263 still be in the original RA register. This is not always reliable:
2264 even in a leaf function, register tracking stops after prologue, and
2265 even in prologue, non-prologue instructions (not tracked) may overwrite
2266 RA or any register it was copied to. If likely in prologue or before
2267 any call, use retracking info and hope for the best (compiler should
2268 have saved RA in stack if not in a leaf function). If not in prologue,
2274 (i == C0_RA || cache->c0.c0_rt[i].fr_reg != C0_RA);
2276 if (i >= C0_NREGS && cache->c0.c0_rt[C0_RA].fr_reg == C0_RA)
2278 if (i < C0_NREGS) /* Read from the next_frame. */
2280 ra = frame_unwind_register_unsigned (next_frame,
2281 A0_REGNUM + cache->c0.c0_rt[i].fr_reg);
2286 cache->pc = start_pc;
2288 /* RA == 0 marks the outermost frame. Do not go past it. */
2289 cache->prev_sp = (ra != 0) ? prev_sp : 0;
2290 cache->c0.fp_regnum = fp_regnum;
2291 cache->c0.c0_frmsz = c0_frmsz;
2292 cache->c0.c0_hasfp = c0_hasfp;
2293 cache->c0.c0_fp = fp;
2297 /* Skip function prologue.
2299 Return the pc of the first instruction after prologue. GDB calls this to
2300 find the address of the first line of the function or (if there is no line
2301 number information) to skip the prologue for planting breakpoints on
2302 function entries. Use debug info (if present) or prologue analysis to skip
2303 the prologue to achieve reliable debugging behavior. For windowed ABI,
2304 only the 'entry' instruction is skipped. It is not strictly necessary to
2305 skip the prologue (Call0) or 'entry' (Windowed) because xt-gdb knows how to
2306 backtrace at any point in the prologue, however certain potential hazards
2307 are avoided and a more "normal" debugging experience is ensured by
2308 skipping the prologue (can be disabled by defining DONT_SKIP_PROLOG).
2309 For example, if we don't skip the prologue:
2310 - Some args may not yet have been saved to the stack where the debug
2311 info expects to find them (true anyway when only 'entry' is skipped);
2312 - Software breakpoints ('break' instrs) may not have been unplanted
2313 when the prologue analysis is done on initializing the frame cache,
2314 and breaks in the prologue will throw off the analysis.
2316 If we have debug info ( line-number info, in particular ) we simply skip
2317 the code associated with the first function line effectively skipping
2318 the prologue code. It works even in cases like
2321 { int local_var = 1;
2325 because, for this source code, both Xtensa compilers will generate two
2326 separate entries ( with the same line number ) in dwarf line-number
2327 section to make sure there is a boundary between the prologue code and
2328 the rest of the function.
2330 If there is no debug info, we need to analyze the code. */
2332 /* #define DONT_SKIP_PROLOGUE */
2335 xtensa_skip_prologue (CORE_ADDR start_pc)
2337 struct symtab_and_line prologue_sal;
2340 DEBUGTRACE ("xtensa_skip_prologue (start_pc = 0x%08x)\n", (int) start_pc);
2342 #if DONT_SKIP_PROLOGUE
2346 /* Try to find first body line from debug info. */
2348 prologue_sal = find_pc_line (start_pc, 0);
2349 if (prologue_sal.line != 0) /* Found debug info. */
2351 /* In Call0, it is possible to have a function with only one instruction
2352 ('ret') resulting from a 1-line optimized function that does nothing.
2353 In that case, prologue_sal.end may actually point to the start of the
2354 next function in the text section, causing a breakpoint to be set at
2355 the wrong place. Check if the end address is in a different function,
2356 and if so return the start PC. We know we have symbol info. */
2360 find_pc_partial_function (prologue_sal.end, NULL, &end_func, NULL);
2361 if (end_func != start_pc)
2364 return prologue_sal.end;
2367 /* No debug line info. Analyze prologue for Call0 or simply skip ENTRY. */
2368 body_pc = call0_analyze_prologue(start_pc, 0, 0, NULL, NULL);
2369 return body_pc != 0 ? body_pc : start_pc;
2372 /* Verify the current configuration. */
2374 xtensa_verify_config (struct gdbarch *gdbarch)
2376 struct ui_file *log;
2377 struct cleanup *cleanups;
2378 struct gdbarch_tdep *tdep;
2382 tdep = gdbarch_tdep (gdbarch);
2383 log = mem_fileopen ();
2384 cleanups = make_cleanup_ui_file_delete (log);
2386 /* Verify that we got a reasonable number of AREGS. */
2387 if ((tdep->num_aregs & -tdep->num_aregs) != tdep->num_aregs)
2388 fprintf_unfiltered (log, _("\
2389 \n\tnum_aregs: Number of AR registers (%d) is not a power of two!"),
2392 /* Verify that certain registers exist. */
2394 if (tdep->pc_regnum == -1)
2395 fprintf_unfiltered (log, _("\n\tpc_regnum: No PC register"));
2396 if (tdep->isa_use_exceptions && tdep->ps_regnum == -1)
2397 fprintf_unfiltered (log, _("\n\tps_regnum: No PS register"));
2399 if (tdep->isa_use_windowed_registers)
2401 if (tdep->wb_regnum == -1)
2402 fprintf_unfiltered (log, _("\n\twb_regnum: No WB register"));
2403 if (tdep->ws_regnum == -1)
2404 fprintf_unfiltered (log, _("\n\tws_regnum: No WS register"));
2405 if (tdep->ar_base == -1)
2406 fprintf_unfiltered (log, _("\n\tar_base: No AR registers"));
2409 if (tdep->a0_base == -1)
2410 fprintf_unfiltered (log, _("\n\ta0_base: No Ax registers"));
2412 buf = ui_file_xstrdup (log, &dummy);
2413 make_cleanup (xfree, buf);
2414 if (strlen (buf) > 0)
2415 internal_error (__FILE__, __LINE__,
2416 _("the following are invalid: %s"), buf);
2417 do_cleanups (cleanups);
2420 /* Module "constructor" function. */
2422 static struct gdbarch *
2423 xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2425 struct gdbarch_tdep *tdep;
2426 struct gdbarch *gdbarch;
2427 struct xtensa_abi_handler *abi_handler;
2429 DEBUGTRACE ("gdbarch_init()\n");
2431 /* We have to set the byte order before we call gdbarch_alloc. */
2432 info.byte_order = xtensa_config_byte_order (&info);
2434 tdep = xtensa_config_tdep (&info);
2435 gdbarch = gdbarch_alloc (&info, tdep);
2437 /* Verify our configuration. */
2438 xtensa_verify_config (gdbarch);
2440 /* Pseudo-Register read/write. */
2441 set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
2442 set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
2444 /* Set target information. */
2445 set_gdbarch_num_regs (gdbarch, tdep->num_regs);
2446 set_gdbarch_num_pseudo_regs (gdbarch, tdep->num_pseudo_regs);
2447 set_gdbarch_sp_regnum (gdbarch, tdep->a0_base + 1);
2448 set_gdbarch_pc_regnum (gdbarch, tdep->pc_regnum);
2449 set_gdbarch_ps_regnum (gdbarch, tdep->ps_regnum);
2451 /* Renumber registers for known formats (stab, dwarf, and dwarf2). */
2452 set_gdbarch_stab_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2453 set_gdbarch_dwarf_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2454 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, xtensa_reg_to_regnum);
2456 /* We provide our own function to get register information. */
2457 set_gdbarch_register_name (gdbarch, xtensa_register_name);
2458 set_gdbarch_register_type (gdbarch, xtensa_register_type);
2460 /* To call functions from GDB using dummy frame */
2461 set_gdbarch_push_dummy_call (gdbarch, xtensa_push_dummy_call);
2463 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2465 set_gdbarch_return_value (gdbarch, xtensa_return_value);
2467 /* Advance PC across any prologue instructions to reach "real" code. */
2468 set_gdbarch_skip_prologue (gdbarch, xtensa_skip_prologue);
2470 /* Stack grows downward. */
2471 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2473 /* Set breakpoints. */
2474 set_gdbarch_breakpoint_from_pc (gdbarch, xtensa_breakpoint_from_pc);
2476 /* After breakpoint instruction or illegal instruction, pc still
2477 points at break instruction, so don't decrement. */
2478 set_gdbarch_decr_pc_after_break (gdbarch, 0);
2480 /* We don't skip args. */
2481 set_gdbarch_frame_args_skip (gdbarch, 0);
2483 set_gdbarch_unwind_pc (gdbarch, xtensa_unwind_pc);
2485 set_gdbarch_frame_align (gdbarch, xtensa_frame_align);
2487 set_gdbarch_unwind_dummy_id (gdbarch, xtensa_unwind_dummy_id);
2489 /* Frame handling. */
2490 frame_base_set_default (gdbarch, &xtensa_frame_base);
2491 frame_unwind_append_sniffer (gdbarch, xtensa_frame_sniffer);
2493 set_gdbarch_print_insn (gdbarch, print_insn_xtensa);
2495 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2497 xtensa_add_reggroups (gdbarch);
2498 set_gdbarch_register_reggroup_p (gdbarch, xtensa_register_reggroup_p);
2500 set_gdbarch_regset_from_core_section (gdbarch,
2501 xtensa_regset_from_core_section);
2507 xtensa_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
2509 error (_("xtensa_dump_tdep(): not implemented"));
2513 _initialize_xtensa_tdep (void)
2515 struct cmd_list_element *c;
2517 gdbarch_register (bfd_arch_xtensa, xtensa_gdbarch_init, xtensa_dump_tdep);
2518 xtensa_init_reggroups ();
2520 add_setshow_zinteger_cmd ("xtensa",
2522 &xtensa_debug_level, _("\
2523 Set Xtensa debugging."), _("\
2524 Show Xtensa debugging."), _("\
2525 When non-zero, Xtensa-specific debugging is enabled. \
2526 Can be 1, 2, 3, or 4 indicating the level of debugging."),
2529 &setdebuglist, &showdebuglist);