Automatic date update in version.in
[binutils-gdb.git] / gdb / cris-tdep.c
blob549bc066480fb787bf410eac5ac184ce67229171
1 /* Target dependent code for CRIS, for GDB, the GNU debugger.
3 Copyright (C) 2001-2022 Free Software Foundation, Inc.
5 Contributed by Axis Communications AB.
6 Written by Hendrik Ruijter, Stefan Andersson, and Orjan Friberg.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "defs.h"
24 #include "frame.h"
25 #include "frame-unwind.h"
26 #include "frame-base.h"
27 #include "trad-frame.h"
28 #include "dwarf2/frame.h"
29 #include "symtab.h"
30 #include "inferior.h"
31 #include "gdbtypes.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "target.h"
35 #include "value.h"
36 #include "opcode/cris.h"
37 #include "osabi.h"
38 #include "arch-utils.h"
39 #include "regcache.h"
40 #include "regset.h"
42 #include "objfiles.h"
44 #include "solib.h" /* Support for shared libraries. */
45 #include "solib-svr4.h"
46 #include "dis-asm.h"
48 #include "cris-tdep.h"
50 enum cris_num_regs
52 /* There are no floating point registers. Used in gdbserver low-linux.c. */
53 NUM_FREGS = 0,
55 /* There are 16 general registers. */
56 NUM_GENREGS = 16,
58 /* There are 16 special registers. */
59 NUM_SPECREGS = 16,
61 /* CRISv32 has a pseudo PC register, not noted here. */
63 /* CRISv32 has 16 support registers. */
64 NUM_SUPPREGS = 16
67 /* Register numbers of various important registers.
68 CRIS_FP_REGNUM Contains address of executing stack frame.
69 STR_REGNUM Contains the address of structure return values.
70 RET_REGNUM Contains the return value when shorter than or equal to 32 bits
71 ARG1_REGNUM Contains the first parameter to a function.
72 ARG2_REGNUM Contains the second parameter to a function.
73 ARG3_REGNUM Contains the third parameter to a function.
74 ARG4_REGNUM Contains the fourth parameter to a function. Rest on stack.
75 gdbarch_sp_regnum Contains address of top of stack.
76 gdbarch_pc_regnum Contains address of next instruction.
77 SRP_REGNUM Subroutine return pointer register.
78 BRP_REGNUM Breakpoint return pointer register. */
80 enum cris_regnums
82 /* Enums with respect to the general registers, valid for all
83 CRIS versions. The frame pointer is always in R8. */
84 CRIS_FP_REGNUM = 8,
85 /* ABI related registers. */
86 STR_REGNUM = 9,
87 RET_REGNUM = 10,
88 ARG1_REGNUM = 10,
89 ARG2_REGNUM = 11,
90 ARG3_REGNUM = 12,
91 ARG4_REGNUM = 13,
93 /* Registers which happen to be common. */
94 VR_REGNUM = 17,
95 MOF_REGNUM = 23,
96 SRP_REGNUM = 27,
98 /* CRISv10 et al. specific registers. */
99 P0_REGNUM = 16,
100 P4_REGNUM = 20,
101 CCR_REGNUM = 21,
102 P8_REGNUM = 24,
103 IBR_REGNUM = 25,
104 IRP_REGNUM = 26,
105 BAR_REGNUM = 28,
106 DCCR_REGNUM = 29,
107 BRP_REGNUM = 30,
108 USP_REGNUM = 31,
110 /* CRISv32 specific registers. */
111 ACR_REGNUM = 15,
112 BZ_REGNUM = 16,
113 PID_REGNUM = 18,
114 SRS_REGNUM = 19,
115 WZ_REGNUM = 20,
116 EXS_REGNUM = 21,
117 EDA_REGNUM = 22,
118 DZ_REGNUM = 24,
119 EBP_REGNUM = 25,
120 ERP_REGNUM = 26,
121 NRP_REGNUM = 28,
122 CCS_REGNUM = 29,
123 CRISV32USP_REGNUM = 30, /* Shares name but not number with CRISv10. */
124 SPC_REGNUM = 31,
125 CRISV32PC_REGNUM = 32, /* Shares name but not number with CRISv10. */
127 S0_REGNUM = 33,
128 S1_REGNUM = 34,
129 S2_REGNUM = 35,
130 S3_REGNUM = 36,
131 S4_REGNUM = 37,
132 S5_REGNUM = 38,
133 S6_REGNUM = 39,
134 S7_REGNUM = 40,
135 S8_REGNUM = 41,
136 S9_REGNUM = 42,
137 S10_REGNUM = 43,
138 S11_REGNUM = 44,
139 S12_REGNUM = 45,
140 S13_REGNUM = 46,
141 S14_REGNUM = 47,
142 S15_REGNUM = 48,
145 extern const struct cris_spec_reg cris_spec_regs[];
147 /* CRIS version, set via the user command 'set cris-version'. Affects
148 register names and sizes. */
149 static unsigned int usr_cmd_cris_version;
151 /* Indicates whether to trust the above variable. */
152 static bool usr_cmd_cris_version_valid = false;
154 static const char cris_mode_normal[] = "normal";
155 static const char cris_mode_guru[] = "guru";
156 static const char *const cris_modes[] = {
157 cris_mode_normal,
158 cris_mode_guru,
162 /* CRIS mode, set via the user command 'set cris-mode'. Affects
163 type of break instruction among other things. */
164 static const char *usr_cmd_cris_mode = cris_mode_normal;
166 /* Whether to make use of Dwarf-2 CFI (default on). */
167 static bool usr_cmd_cris_dwarf2_cfi = true;
169 /* Sigtramp identification code copied from i386-linux-tdep.c. */
171 #define SIGTRAMP_INSN0 0x9c5f /* movu.w 0xXX, $r9 */
172 #define SIGTRAMP_OFFSET0 0
173 #define SIGTRAMP_INSN1 0xe93d /* break 13 */
174 #define SIGTRAMP_OFFSET1 4
176 static const unsigned short sigtramp_code[] =
178 SIGTRAMP_INSN0, 0x0077, /* movu.w $0x77, $r9 */
179 SIGTRAMP_INSN1 /* break 13 */
182 #define SIGTRAMP_LEN (sizeof sigtramp_code)
184 /* Note: same length as normal sigtramp code. */
186 static const unsigned short rt_sigtramp_code[] =
188 SIGTRAMP_INSN0, 0x00ad, /* movu.w $0xad, $r9 */
189 SIGTRAMP_INSN1 /* break 13 */
192 /* If PC is in a sigtramp routine, return the address of the start of
193 the routine. Otherwise, return 0. */
195 static CORE_ADDR
196 cris_sigtramp_start (struct frame_info *this_frame)
198 CORE_ADDR pc = get_frame_pc (this_frame);
199 gdb_byte buf[SIGTRAMP_LEN];
201 if (!safe_frame_unwind_memory (this_frame, pc, buf))
202 return 0;
204 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
206 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
207 return 0;
209 pc -= SIGTRAMP_OFFSET1;
210 if (!safe_frame_unwind_memory (this_frame, pc, buf))
211 return 0;
214 if (memcmp (buf, sigtramp_code, SIGTRAMP_LEN) != 0)
215 return 0;
217 return pc;
220 /* If PC is in a RT sigtramp routine, return the address of the start of
221 the routine. Otherwise, return 0. */
223 static CORE_ADDR
224 cris_rt_sigtramp_start (struct frame_info *this_frame)
226 CORE_ADDR pc = get_frame_pc (this_frame);
227 gdb_byte buf[SIGTRAMP_LEN];
229 if (!safe_frame_unwind_memory (this_frame, pc, buf))
230 return 0;
232 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
234 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
235 return 0;
237 pc -= SIGTRAMP_OFFSET1;
238 if (!safe_frame_unwind_memory (this_frame, pc, buf))
239 return 0;
242 if (memcmp (buf, rt_sigtramp_code, SIGTRAMP_LEN) != 0)
243 return 0;
245 return pc;
248 /* Assuming THIS_FRAME is a frame for a GNU/Linux sigtramp routine,
249 return the address of the associated sigcontext structure. */
251 static CORE_ADDR
252 cris_sigcontext_addr (struct frame_info *this_frame)
254 struct gdbarch *gdbarch = get_frame_arch (this_frame);
255 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
256 CORE_ADDR pc;
257 CORE_ADDR sp;
258 gdb_byte buf[4];
260 get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
261 sp = extract_unsigned_integer (buf, 4, byte_order);
263 /* Look for normal sigtramp frame first. */
264 pc = cris_sigtramp_start (this_frame);
265 if (pc)
267 /* struct signal_frame (arch/cris/kernel/signal.c) contains
268 struct sigcontext as its first member, meaning the SP points to
269 it already. */
270 return sp;
273 pc = cris_rt_sigtramp_start (this_frame);
274 if (pc)
276 /* struct rt_signal_frame (arch/cris/kernel/signal.c) contains
277 a struct ucontext, which in turn contains a struct sigcontext.
278 Magic digging:
279 4 + 4 + 128 to struct ucontext, then
280 4 + 4 + 12 to struct sigcontext. */
281 return (sp + 156);
284 error (_("Couldn't recognize signal trampoline."));
285 return 0;
288 struct cris_unwind_cache
290 /* The previous frame's inner most stack address. Used as this
291 frame ID's stack_addr. */
292 CORE_ADDR prev_sp;
293 /* The frame's base, optionally used by the high-level debug info. */
294 CORE_ADDR base;
295 int size;
296 /* How far the SP and r8 (FP) have been offset from the start of
297 the stack frame (as defined by the previous frame's stack
298 pointer). */
299 LONGEST sp_offset;
300 LONGEST r8_offset;
301 int uses_frame;
303 /* From old frame_extra_info struct. */
304 CORE_ADDR return_pc;
305 int leaf_function;
307 /* Table indicating the location of each and every register. */
308 trad_frame_saved_reg *saved_regs;
311 static struct cris_unwind_cache *
312 cris_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
313 void **this_cache)
315 struct gdbarch *gdbarch = get_frame_arch (this_frame);
316 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
317 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
318 struct cris_unwind_cache *info;
319 CORE_ADDR addr;
320 gdb_byte buf[4];
321 int i;
323 if ((*this_cache))
324 return (struct cris_unwind_cache *) (*this_cache);
326 info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
327 (*this_cache) = info;
328 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
330 /* Zero all fields. */
331 info->prev_sp = 0;
332 info->base = 0;
333 info->size = 0;
334 info->sp_offset = 0;
335 info->r8_offset = 0;
336 info->uses_frame = 0;
337 info->return_pc = 0;
338 info->leaf_function = 0;
340 get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
341 info->base = extract_unsigned_integer (buf, 4, byte_order);
343 addr = cris_sigcontext_addr (this_frame);
345 /* Layout of the sigcontext struct:
346 struct sigcontext {
347 struct pt_regs regs;
348 unsigned long oldmask;
349 unsigned long usp;
350 }; */
352 if (tdep->cris_version == 10)
354 /* R0 to R13 are stored in reverse order at offset (2 * 4) in
355 struct pt_regs. */
356 for (i = 0; i <= 13; i++)
357 info->saved_regs[i].set_addr (addr + ((15 - i) * 4));
359 info->saved_regs[MOF_REGNUM].set_addr (addr + (16 * 4));
360 info->saved_regs[DCCR_REGNUM].set_addr (addr + (17 * 4));
361 info->saved_regs[SRP_REGNUM].set_addr (addr + (18 * 4));
362 /* Note: IRP is off by 2 at this point. There's no point in correcting
363 it though since that will mean that the backtrace will show a PC
364 different from what is shown when stopped. */
365 info->saved_regs[IRP_REGNUM].set_addr (addr + (19 * 4));
366 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
367 = info->saved_regs[IRP_REGNUM];
368 info->saved_regs[gdbarch_sp_regnum (gdbarch)].set_addr (addr + (24 * 4));
370 else
372 /* CRISv32. */
373 /* R0 to R13 are stored in order at offset (1 * 4) in
374 struct pt_regs. */
375 for (i = 0; i <= 13; i++)
376 info->saved_regs[i].set_addr (addr + ((i + 1) * 4));
378 info->saved_regs[ACR_REGNUM].set_addr (addr + (15 * 4));
379 info->saved_regs[SRS_REGNUM].set_addr (addr + (16 * 4));
380 info->saved_regs[MOF_REGNUM].set_addr (addr + (17 * 4));
381 info->saved_regs[SPC_REGNUM].set_addr (addr + (18 * 4));
382 info->saved_regs[CCS_REGNUM].set_addr (addr + (19 * 4));
383 info->saved_regs[SRP_REGNUM].set_addr (addr + (20 * 4));
384 info->saved_regs[ERP_REGNUM].set_addr (addr + (21 * 4));
385 info->saved_regs[EXS_REGNUM].set_addr (addr + (22 * 4));
386 info->saved_regs[EDA_REGNUM].set_addr (addr + (23 * 4));
388 /* FIXME: If ERP is in a delay slot at this point then the PC will
389 be wrong at this point. This problem manifests itself in the
390 sigaltstack.exp test case, which occasionally generates FAILs when
391 the signal is received while in a delay slot.
393 This could be solved by a couple of read_memory_unsigned_integer and a
394 trad_frame_set_value. */
395 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
396 = info->saved_regs[ERP_REGNUM];
398 info->saved_regs[gdbarch_sp_regnum (gdbarch)].set_addr (addr + (25 * 4));
401 return info;
404 static void
405 cris_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
406 struct frame_id *this_id)
408 struct cris_unwind_cache *cache =
409 cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
410 (*this_id) = frame_id_build (cache->base, get_frame_pc (this_frame));
413 /* Forward declaration. */
415 static struct value *cris_frame_prev_register (struct frame_info *this_frame,
416 void **this_cache, int regnum);
417 static struct value *
418 cris_sigtramp_frame_prev_register (struct frame_info *this_frame,
419 void **this_cache, int regnum)
421 /* Make sure we've initialized the cache. */
422 cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
423 return cris_frame_prev_register (this_frame, this_cache, regnum);
426 static int
427 cris_sigtramp_frame_sniffer (const struct frame_unwind *self,
428 struct frame_info *this_frame,
429 void **this_cache)
431 if (cris_sigtramp_start (this_frame)
432 || cris_rt_sigtramp_start (this_frame))
433 return 1;
435 return 0;
438 static const struct frame_unwind cris_sigtramp_frame_unwind =
440 "cris sigtramp",
441 SIGTRAMP_FRAME,
442 default_frame_unwind_stop_reason,
443 cris_sigtramp_frame_this_id,
444 cris_sigtramp_frame_prev_register,
445 NULL,
446 cris_sigtramp_frame_sniffer
449 static int
450 crisv32_single_step_through_delay (struct gdbarch *gdbarch,
451 struct frame_info *this_frame)
453 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
454 ULONGEST erp;
455 int ret = 0;
457 if (tdep->cris_mode == cris_mode_guru)
458 erp = get_frame_register_unsigned (this_frame, NRP_REGNUM);
459 else
460 erp = get_frame_register_unsigned (this_frame, ERP_REGNUM);
462 if (erp & 0x1)
464 /* In delay slot - check if there's a breakpoint at the preceding
465 instruction. */
466 if (breakpoint_here_p (get_frame_address_space (this_frame), erp & ~0x1))
467 ret = 1;
469 return ret;
472 /* The instruction environment needed to find single-step breakpoints. */
474 typedef
475 struct instruction_environment
477 unsigned long reg[NUM_GENREGS];
478 unsigned long preg[NUM_SPECREGS];
479 unsigned long branch_break_address;
480 unsigned long delay_slot_pc;
481 unsigned long prefix_value;
482 int branch_found;
483 int prefix_found;
484 int invalid;
485 int slot_needed;
486 int delay_slot_pc_active;
487 int xflag_found;
488 int disable_interrupt;
489 enum bfd_endian byte_order;
490 } inst_env_type;
492 /* Machine-dependencies in CRIS for opcodes. */
494 /* Instruction sizes. */
495 enum cris_instruction_sizes
497 INST_BYTE_SIZE = 0,
498 INST_WORD_SIZE = 1,
499 INST_DWORD_SIZE = 2
502 /* Addressing modes. */
503 enum cris_addressing_modes
505 REGISTER_MODE = 1,
506 INDIRECT_MODE = 2,
507 AUTOINC_MODE = 3
510 /* Prefix addressing modes. */
511 enum cris_prefix_addressing_modes
513 PREFIX_INDEX_MODE = 2,
514 PREFIX_ASSIGN_MODE = 3,
516 /* Handle immediate byte offset addressing mode prefix format. */
517 PREFIX_OFFSET_MODE = 2
520 /* Masks for opcodes. */
521 enum cris_opcode_masks
523 BRANCH_SIGNED_SHORT_OFFSET_MASK = 0x1,
524 SIGNED_EXTEND_BIT_MASK = 0x2,
525 SIGNED_BYTE_MASK = 0x80,
526 SIGNED_BYTE_EXTEND_MASK = 0xFFFFFF00,
527 SIGNED_WORD_MASK = 0x8000,
528 SIGNED_WORD_EXTEND_MASK = 0xFFFF0000,
529 SIGNED_DWORD_MASK = 0x80000000,
530 SIGNED_QUICK_VALUE_MASK = 0x20,
531 SIGNED_QUICK_VALUE_EXTEND_MASK = 0xFFFFFFC0
534 /* Functions for opcodes. The general form of the ETRAX 16-bit instruction:
535 Bit 15 - 12 Operand2
536 11 - 10 Mode
537 9 - 6 Opcode
538 5 - 4 Size
539 3 - 0 Operand1 */
541 static int
542 cris_get_operand2 (unsigned short insn)
544 return ((insn & 0xF000) >> 12);
547 static int
548 cris_get_mode (unsigned short insn)
550 return ((insn & 0x0C00) >> 10);
553 static int
554 cris_get_opcode (unsigned short insn)
556 return ((insn & 0x03C0) >> 6);
559 static int
560 cris_get_size (unsigned short insn)
562 return ((insn & 0x0030) >> 4);
565 static int
566 cris_get_operand1 (unsigned short insn)
568 return (insn & 0x000F);
571 /* Additional functions in order to handle opcodes. */
573 static int
574 cris_get_quick_value (unsigned short insn)
576 return (insn & 0x003F);
579 static int
580 cris_get_bdap_quick_offset (unsigned short insn)
582 return (insn & 0x00FF);
585 static int
586 cris_get_branch_short_offset (unsigned short insn)
588 return (insn & 0x00FF);
591 static int
592 cris_get_asr_shift_steps (unsigned long value)
594 return (value & 0x3F);
597 static int
598 cris_get_clear_size (unsigned short insn)
600 return ((insn) & 0xC000);
603 static int
604 cris_is_signed_extend_bit_on (unsigned short insn)
606 return (((insn) & 0x20) == 0x20);
609 static int
610 cris_is_xflag_bit_on (unsigned short insn)
612 return (((insn) & 0x1000) == 0x1000);
615 static void
616 cris_set_size_to_dword (unsigned short *insn)
618 *insn &= 0xFFCF;
619 *insn |= 0x20;
622 static signed char
623 cris_get_signed_offset (unsigned short insn)
625 return ((signed char) (insn & 0x00FF));
628 /* Calls an op function given the op-type, working on the insn and the
629 inst_env. */
630 static void cris_gdb_func (struct gdbarch *, enum cris_op_type, unsigned short,
631 inst_env_type *);
633 static struct gdbarch *cris_gdbarch_init (struct gdbarch_info,
634 struct gdbarch_list *);
636 static void cris_dump_tdep (struct gdbarch *, struct ui_file *);
638 static void set_cris_version (const char *ignore_args, int from_tty,
639 struct cmd_list_element *c);
641 static void set_cris_mode (const char *ignore_args, int from_tty,
642 struct cmd_list_element *c);
644 static void set_cris_dwarf2_cfi (const char *ignore_args, int from_tty,
645 struct cmd_list_element *c);
647 static CORE_ADDR cris_scan_prologue (CORE_ADDR pc,
648 struct frame_info *this_frame,
649 struct cris_unwind_cache *info);
651 static CORE_ADDR crisv32_scan_prologue (CORE_ADDR pc,
652 struct frame_info *this_frame,
653 struct cris_unwind_cache *info);
655 /* When arguments must be pushed onto the stack, they go on in reverse
656 order. The below implements a FILO (stack) to do this.
657 Copied from d10v-tdep.c. */
659 struct cris_stack_item
661 int len;
662 struct cris_stack_item *prev;
663 gdb_byte *data;
666 static struct cris_stack_item *
667 push_stack_item (struct cris_stack_item *prev, const gdb_byte *contents,
668 int len)
670 struct cris_stack_item *si = XNEW (struct cris_stack_item);
671 si->data = (gdb_byte *) xmalloc (len);
672 si->len = len;
673 si->prev = prev;
674 memcpy (si->data, contents, len);
675 return si;
678 static struct cris_stack_item *
679 pop_stack_item (struct cris_stack_item *si)
681 struct cris_stack_item *dead = si;
682 si = si->prev;
683 xfree (dead->data);
684 xfree (dead);
685 return si;
688 /* Put here the code to store, into fi->saved_regs, the addresses of
689 the saved registers of frame described by FRAME_INFO. This
690 includes special registers such as pc and fp saved in special ways
691 in the stack frame. sp is even more special: the address we return
692 for it IS the sp for the next frame. */
694 static struct cris_unwind_cache *
695 cris_frame_unwind_cache (struct frame_info *this_frame,
696 void **this_prologue_cache)
698 struct gdbarch *gdbarch = get_frame_arch (this_frame);
699 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
700 struct cris_unwind_cache *info;
702 if ((*this_prologue_cache))
703 return (struct cris_unwind_cache *) (*this_prologue_cache);
705 info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
706 (*this_prologue_cache) = info;
707 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
709 /* Zero all fields. */
710 info->prev_sp = 0;
711 info->base = 0;
712 info->size = 0;
713 info->sp_offset = 0;
714 info->r8_offset = 0;
715 info->uses_frame = 0;
716 info->return_pc = 0;
717 info->leaf_function = 0;
719 /* Prologue analysis does the rest... */
720 if (tdep->cris_version == 32)
721 crisv32_scan_prologue (get_frame_func (this_frame), this_frame, info);
722 else
723 cris_scan_prologue (get_frame_func (this_frame), this_frame, info);
725 return info;
728 /* Given a GDB frame, determine the address of the calling function's
729 frame. This will be used to create a new GDB frame struct. */
731 static void
732 cris_frame_this_id (struct frame_info *this_frame,
733 void **this_prologue_cache,
734 struct frame_id *this_id)
736 struct cris_unwind_cache *info
737 = cris_frame_unwind_cache (this_frame, this_prologue_cache);
738 CORE_ADDR base;
739 CORE_ADDR func;
740 struct frame_id id;
742 /* The FUNC is easy. */
743 func = get_frame_func (this_frame);
745 /* Hopefully the prologue analysis either correctly determined the
746 frame's base (which is the SP from the previous frame), or set
747 that base to "NULL". */
748 base = info->prev_sp;
749 if (base == 0)
750 return;
752 id = frame_id_build (base, func);
754 (*this_id) = id;
757 static struct value *
758 cris_frame_prev_register (struct frame_info *this_frame,
759 void **this_prologue_cache, int regnum)
761 struct cris_unwind_cache *info
762 = cris_frame_unwind_cache (this_frame, this_prologue_cache);
763 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
766 static CORE_ADDR
767 cris_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
769 /* Align to the size of an instruction (so that they can safely be
770 pushed onto the stack). */
771 return sp & ~3;
774 static CORE_ADDR
775 cris_push_dummy_code (struct gdbarch *gdbarch,
776 CORE_ADDR sp, CORE_ADDR funaddr,
777 struct value **args, int nargs,
778 struct type *value_type,
779 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
780 struct regcache *regcache)
782 /* Allocate space sufficient for a breakpoint. */
783 sp = (sp - 4) & ~3;
784 /* Store the address of that breakpoint */
785 *bp_addr = sp;
786 /* CRIS always starts the call at the callee's entry point. */
787 *real_pc = funaddr;
788 return sp;
791 static CORE_ADDR
792 cris_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
793 struct regcache *regcache, CORE_ADDR bp_addr,
794 int nargs, struct value **args, CORE_ADDR sp,
795 function_call_return_method return_method,
796 CORE_ADDR struct_addr)
798 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
799 int argreg;
800 int argnum;
802 struct cris_stack_item *si = NULL;
804 /* Push the return address. */
805 regcache_cooked_write_unsigned (regcache, SRP_REGNUM, bp_addr);
807 /* Are we returning a value using a structure return or a normal value
808 return? struct_addr is the address of the reserved space for the return
809 structure to be written on the stack. */
810 if (return_method == return_method_struct)
811 regcache_cooked_write_unsigned (regcache, STR_REGNUM, struct_addr);
813 /* Now load as many as possible of the first arguments into registers,
814 and push the rest onto the stack. */
815 argreg = ARG1_REGNUM;
817 for (argnum = 0; argnum < nargs; argnum++)
819 int len;
820 const gdb_byte *val;
821 int reg_demand;
822 int i;
824 len = value_type (args[argnum])->length ();
825 val = value_contents (args[argnum]).data ();
827 /* How may registers worth of storage do we need for this argument? */
828 reg_demand = (len / 4) + (len % 4 != 0 ? 1 : 0);
830 if (len <= (2 * 4) && (argreg + reg_demand - 1 <= ARG4_REGNUM))
832 /* Data passed by value. Fits in available register(s). */
833 for (i = 0; i < reg_demand; i++)
835 regcache->cooked_write (argreg, val);
836 argreg++;
837 val += 4;
840 else if (len <= (2 * 4) && argreg <= ARG4_REGNUM)
842 /* Data passed by value. Does not fit in available register(s).
843 Use the register(s) first, then the stack. */
844 for (i = 0; i < reg_demand; i++)
846 if (argreg <= ARG4_REGNUM)
848 regcache->cooked_write (argreg, val);
849 argreg++;
850 val += 4;
852 else
854 /* Push item for later so that pushed arguments
855 come in the right order. */
856 si = push_stack_item (si, val, 4);
857 val += 4;
861 else if (len > (2 * 4))
863 /* Data passed by reference. Push copy of data onto stack
864 and pass pointer to this copy as argument. */
865 sp = (sp - len) & ~3;
866 write_memory (sp, val, len);
868 if (argreg <= ARG4_REGNUM)
870 regcache_cooked_write_unsigned (regcache, argreg, sp);
871 argreg++;
873 else
875 gdb_byte buf[4];
876 store_unsigned_integer (buf, 4, byte_order, sp);
877 si = push_stack_item (si, buf, 4);
880 else
882 /* Data passed by value. No available registers. Put it on
883 the stack. */
884 si = push_stack_item (si, val, len);
888 while (si)
890 /* fp_arg must be word-aligned (i.e., don't += len) to match
891 the function prologue. */
892 sp = (sp - si->len) & ~3;
893 write_memory (sp, si->data, si->len);
894 si = pop_stack_item (si);
897 /* Finally, update the SP register. */
898 regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
900 return sp;
903 static const struct frame_unwind cris_frame_unwind =
905 "cris prologue",
906 NORMAL_FRAME,
907 default_frame_unwind_stop_reason,
908 cris_frame_this_id,
909 cris_frame_prev_register,
910 NULL,
911 default_frame_sniffer
914 static CORE_ADDR
915 cris_frame_base_address (struct frame_info *this_frame, void **this_cache)
917 struct cris_unwind_cache *info
918 = cris_frame_unwind_cache (this_frame, this_cache);
919 return info->base;
922 static const struct frame_base cris_frame_base =
924 &cris_frame_unwind,
925 cris_frame_base_address,
926 cris_frame_base_address,
927 cris_frame_base_address
930 /* Frames information. The definition of the struct frame_info is
932 CORE_ADDR frame
933 CORE_ADDR pc
934 enum frame_type type;
935 CORE_ADDR return_pc
936 int leaf_function
938 If the compilation option -fno-omit-frame-pointer is present the
939 variable frame will be set to the content of R8 which is the frame
940 pointer register.
942 The variable pc contains the address where execution is performed
943 in the present frame. The innermost frame contains the current content
944 of the register PC. All other frames contain the content of the
945 register PC in the next frame.
947 The variable `type' indicates the frame's type: normal, SIGTRAMP
948 (associated with a signal handler), dummy (associated with a dummy
949 frame).
951 The variable return_pc contains the address where execution should be
952 resumed when the present frame has finished, the return address.
954 The variable leaf_function is 1 if the return address is in the register
955 SRP, and 0 if it is on the stack.
957 Prologue instructions C-code.
958 The prologue may consist of (-fno-omit-frame-pointer)
959 1) 2)
960 push srp
961 push r8 push r8
962 move.d sp,r8 move.d sp,r8
963 subq X,sp subq X,sp
964 movem rY,[sp] movem rY,[sp]
965 move.S rZ,[r8-U] move.S rZ,[r8-U]
967 where 1 is a non-terminal function, and 2 is a leaf-function.
969 Note that this assumption is extremely brittle, and will break at the
970 slightest change in GCC's prologue.
972 If local variables are declared or register contents are saved on stack
973 the subq-instruction will be present with X as the number of bytes
974 needed for storage. The reshuffle with respect to r8 may be performed
975 with any size S (b, w, d) and any of the general registers Z={0..13}.
976 The offset U should be representable by a signed 8-bit value in all cases.
977 Thus, the prefix word is assumed to be immediate byte offset mode followed
978 by another word containing the instruction.
980 Degenerate cases:
982 push r8
983 move.d sp,r8
984 move.d r8,sp
985 pop r8
987 Prologue instructions C++-code.
988 Case 1) and 2) in the C-code may be followed by
990 move.d r10,rS ; this
991 move.d r11,rT ; P1
992 move.d r12,rU ; P2
993 move.d r13,rV ; P3
994 move.S [r8+U],rZ ; P4
996 if any of the call parameters are stored. The host expects these
997 instructions to be executed in order to get the call parameters right. */
999 /* Examine the prologue of a function. The variable ip is the address of
1000 the first instruction of the prologue. The variable limit is the address
1001 of the first instruction after the prologue. The variable fi contains the
1002 information in struct frame_info. The variable frameless_p controls whether
1003 the entire prologue is examined (0) or just enough instructions to
1004 determine that it is a prologue (1). */
1006 static CORE_ADDR
1007 cris_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
1008 struct cris_unwind_cache *info)
1010 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1011 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1013 /* Present instruction. */
1014 unsigned short insn;
1016 /* Next instruction, lookahead. */
1017 unsigned short insn_next;
1018 int regno;
1020 /* Number of byte on stack used for local variables and movem. */
1021 int val;
1023 /* Highest register number in a movem. */
1024 int regsave;
1026 /* move.d r<source_register>,rS */
1027 short source_register;
1029 /* Scan limit. */
1030 int limit;
1032 /* This frame is with respect to a leaf until a push srp is found. */
1033 if (info)
1035 info->leaf_function = 1;
1038 /* Assume nothing on stack. */
1039 val = 0;
1040 regsave = -1;
1042 /* If we were called without a this_frame, that means we were called
1043 from cris_skip_prologue which already tried to find the end of the
1044 prologue through the symbol information. 64 instructions past current
1045 pc is arbitrarily chosen, but at least it means we'll stop eventually. */
1046 limit = this_frame ? get_frame_pc (this_frame) : pc + 64;
1048 /* Find the prologue instructions. */
1049 while (pc > 0 && pc < limit)
1051 insn = read_memory_unsigned_integer (pc, 2, byte_order);
1052 pc += 2;
1053 if (insn == 0xE1FC)
1055 /* push <reg> 32 bit instruction. */
1056 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1057 pc += 2;
1058 regno = cris_get_operand2 (insn_next);
1059 if (info)
1061 info->sp_offset += 4;
1063 /* This check, meant to recognize srp, used to be regno ==
1064 (SRP_REGNUM - NUM_GENREGS), but that covers r11 also. */
1065 if (insn_next == 0xBE7E)
1067 if (info)
1069 info->leaf_function = 0;
1072 else if (insn_next == 0x8FEE)
1074 /* push $r8 */
1075 if (info)
1077 info->r8_offset = info->sp_offset;
1081 else if (insn == 0x866E)
1083 /* move.d sp,r8 */
1084 if (info)
1086 info->uses_frame = 1;
1088 continue;
1090 else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
1091 && cris_get_mode (insn) == 0x0000
1092 && cris_get_opcode (insn) == 0x000A)
1094 /* subq <val>,sp */
1095 if (info)
1097 info->sp_offset += cris_get_quick_value (insn);
1100 else if (cris_get_mode (insn) == 0x0002
1101 && cris_get_opcode (insn) == 0x000F
1102 && cris_get_size (insn) == 0x0003
1103 && cris_get_operand1 (insn) == gdbarch_sp_regnum (gdbarch))
1105 /* movem r<regsave>,[sp] */
1106 regsave = cris_get_operand2 (insn);
1108 else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
1109 && ((insn & 0x0F00) >> 8) == 0x0001
1110 && (cris_get_signed_offset (insn) < 0))
1112 /* Immediate byte offset addressing prefix word with sp as base
1113 register. Used for CRIS v8 i.e. ETRAX 100 and newer if <val>
1114 is between 64 and 128.
1115 movem r<regsave>,[sp=sp-<val>] */
1116 if (info)
1118 info->sp_offset += -cris_get_signed_offset (insn);
1120 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1121 pc += 2;
1122 if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
1123 && cris_get_opcode (insn_next) == 0x000F
1124 && cris_get_size (insn_next) == 0x0003
1125 && cris_get_operand1 (insn_next) == gdbarch_sp_regnum
1126 (gdbarch))
1128 regsave = cris_get_operand2 (insn_next);
1130 else
1132 /* The prologue ended before the limit was reached. */
1133 pc -= 4;
1134 break;
1137 else if (cris_get_mode (insn) == 0x0001
1138 && cris_get_opcode (insn) == 0x0009
1139 && cris_get_size (insn) == 0x0002)
1141 /* move.d r<10..13>,r<0..15> */
1142 source_register = cris_get_operand1 (insn);
1144 /* FIXME? In the glibc solibs, the prologue might contain something
1145 like (this example taken from relocate_doit):
1146 move.d $pc,$r0
1147 sub.d 0xfffef426,$r0
1148 which isn't covered by the source_register check below. Question
1149 is whether to add a check for this combo, or make better use of
1150 the limit variable instead. */
1151 if (source_register < ARG1_REGNUM || source_register > ARG4_REGNUM)
1153 /* The prologue ended before the limit was reached. */
1154 pc -= 2;
1155 break;
1158 else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
1159 /* The size is a fixed-size. */
1160 && ((insn & 0x0F00) >> 8) == 0x0001
1161 /* A negative offset. */
1162 && (cris_get_signed_offset (insn) < 0))
1164 /* move.S rZ,[r8-U] (?) */
1165 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1166 pc += 2;
1167 regno = cris_get_operand2 (insn_next);
1168 if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1169 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
1170 && cris_get_opcode (insn_next) == 0x000F)
1172 /* move.S rZ,[r8-U] */
1173 continue;
1175 else
1177 /* The prologue ended before the limit was reached. */
1178 pc -= 4;
1179 break;
1182 else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
1183 /* The size is a fixed-size. */
1184 && ((insn & 0x0F00) >> 8) == 0x0001
1185 /* A positive offset. */
1186 && (cris_get_signed_offset (insn) > 0))
1188 /* move.S [r8+U],rZ (?) */
1189 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1190 pc += 2;
1191 regno = cris_get_operand2 (insn_next);
1192 if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1193 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
1194 && cris_get_opcode (insn_next) == 0x0009
1195 && cris_get_operand1 (insn_next) == regno)
1197 /* move.S [r8+U],rZ */
1198 continue;
1200 else
1202 /* The prologue ended before the limit was reached. */
1203 pc -= 4;
1204 break;
1207 else
1209 /* The prologue ended before the limit was reached. */
1210 pc -= 2;
1211 break;
1215 /* We only want to know the end of the prologue when this_frame and info
1216 are NULL (called from cris_skip_prologue i.e.). */
1217 if (this_frame == NULL && info == NULL)
1219 return pc;
1222 info->size = info->sp_offset;
1224 /* Compute the previous frame's stack pointer (which is also the
1225 frame's ID's stack address), and this frame's base pointer. */
1226 if (info->uses_frame)
1228 ULONGEST this_base;
1229 /* The SP was moved to the FP. This indicates that a new frame
1230 was created. Get THIS frame's FP value by unwinding it from
1231 the next frame. */
1232 this_base = get_frame_register_unsigned (this_frame, CRIS_FP_REGNUM);
1233 info->base = this_base;
1234 info->saved_regs[CRIS_FP_REGNUM].set_addr (info->base);
1236 /* The FP points at the last saved register. Adjust the FP back
1237 to before the first saved register giving the SP. */
1238 info->prev_sp = info->base + info->r8_offset;
1240 else
1242 ULONGEST this_base;
1243 /* Assume that the FP is this frame's SP but with that pushed
1244 stack space added back. */
1245 this_base = get_frame_register_unsigned (this_frame,
1246 gdbarch_sp_regnum (gdbarch));
1247 info->base = this_base;
1248 info->prev_sp = info->base + info->size;
1251 /* Calculate the addresses for the saved registers on the stack. */
1252 /* FIXME: The address calculation should really be done on the fly while
1253 we're analyzing the prologue (we only hold one regsave value as it is
1254 now). */
1255 val = info->sp_offset;
1257 for (regno = regsave; regno >= 0; regno--)
1259 info->saved_regs[regno].set_addr (info->base + info->r8_offset - val);
1260 val -= 4;
1263 /* The previous frame's SP needed to be computed. Save the computed
1264 value. */
1265 info->saved_regs[gdbarch_sp_regnum (gdbarch)].set_value (info->prev_sp);
1267 if (!info->leaf_function)
1269 /* SRP saved on the stack. But where? */
1270 if (info->r8_offset == 0)
1272 /* R8 not pushed yet. */
1273 info->saved_regs[SRP_REGNUM].set_addr (info->base);
1275 else
1277 /* R8 pushed, but SP may or may not be moved to R8 yet. */
1278 info->saved_regs[SRP_REGNUM].set_addr (info->base + 4);
1282 /* The PC is found in SRP (the actual register or located on the stack). */
1283 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
1284 = info->saved_regs[SRP_REGNUM];
1286 return pc;
1289 static CORE_ADDR
1290 crisv32_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
1291 struct cris_unwind_cache *info)
1293 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1294 ULONGEST this_base;
1296 /* Unlike the CRISv10 prologue scanner (cris_scan_prologue), this is not
1297 meant to be a full-fledged prologue scanner. It is only needed for
1298 the cases where we end up in code always lacking DWARF-2 CFI, notably:
1300 * PLT stubs (library calls)
1301 * call dummys
1302 * signal trampolines
1304 For those cases, it is assumed that there is no actual prologue; that
1305 the stack pointer is not adjusted, and (as a consequence) the return
1306 address is not pushed onto the stack. */
1308 /* We only want to know the end of the prologue when this_frame and info
1309 are NULL (called from cris_skip_prologue i.e.). */
1310 if (this_frame == NULL && info == NULL)
1312 return pc;
1315 /* The SP is assumed to be unaltered. */
1316 this_base = get_frame_register_unsigned (this_frame,
1317 gdbarch_sp_regnum (gdbarch));
1318 info->base = this_base;
1319 info->prev_sp = this_base;
1321 /* The PC is assumed to be found in SRP. */
1322 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
1323 = info->saved_regs[SRP_REGNUM];
1325 return pc;
1328 /* Advance pc beyond any function entry prologue instructions at pc
1329 to reach some "real" code. */
1331 /* Given a PC value corresponding to the start of a function, return the PC
1332 of the first instruction after the function prologue. */
1334 static CORE_ADDR
1335 cris_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1337 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
1338 CORE_ADDR func_addr, func_end;
1339 struct symtab_and_line sal;
1340 CORE_ADDR pc_after_prologue;
1342 /* If we have line debugging information, then the end of the prologue
1343 should the first assembly instruction of the first source line. */
1344 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1346 sal = find_pc_line (func_addr, 0);
1347 if (sal.end > 0 && sal.end < func_end)
1348 return sal.end;
1351 if (tdep->cris_version == 32)
1352 pc_after_prologue = crisv32_scan_prologue (pc, NULL, NULL);
1353 else
1354 pc_after_prologue = cris_scan_prologue (pc, NULL, NULL);
1356 return pc_after_prologue;
1359 /* Implement the breakpoint_kind_from_pc gdbarch method. */
1361 static int
1362 cris_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1364 return 2;
1367 /* Implement the sw_breakpoint_from_kind gdbarch method. */
1369 static const gdb_byte *
1370 cris_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1372 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
1373 static unsigned char break8_insn[] = {0x38, 0xe9};
1374 static unsigned char break15_insn[] = {0x3f, 0xe9};
1376 *size = kind;
1378 if (tdep->cris_mode == cris_mode_guru)
1379 return break15_insn;
1380 else
1381 return break8_insn;
1384 /* Returns 1 if spec_reg is applicable to the current gdbarch's CRIS version,
1385 0 otherwise. */
1387 static int
1388 cris_spec_reg_applicable (struct gdbarch *gdbarch,
1389 struct cris_spec_reg spec_reg)
1391 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
1392 unsigned int version = tdep->cris_version;
1394 switch (spec_reg.applicable_version)
1396 case cris_ver_version_all:
1397 return 1;
1398 case cris_ver_warning:
1399 /* Indeterminate/obsolete. */
1400 return 0;
1401 case cris_ver_v0_3:
1402 return in_inclusive_range (version, 0U, 3U);
1403 case cris_ver_v3p:
1404 return (version >= 3);
1405 case cris_ver_v8:
1406 return in_inclusive_range (version, 8U, 9U);
1407 case cris_ver_v8p:
1408 return (version >= 8);
1409 case cris_ver_v0_10:
1410 return in_inclusive_range (version, 0U, 10U);
1411 case cris_ver_v3_10:
1412 return in_inclusive_range (version, 3U, 10U);
1413 case cris_ver_v8_10:
1414 return in_inclusive_range (version, 8U, 10U);
1415 case cris_ver_v10:
1416 return (version == 10);
1417 case cris_ver_v10p:
1418 return (version >= 10);
1419 case cris_ver_v32p:
1420 return (version >= 32);
1421 default:
1422 /* Invalid cris version. */
1423 return 0;
1427 /* Returns the register size in unit byte. Returns 0 for an unimplemented
1428 register, -1 for an invalid register. */
1430 static int
1431 cris_register_size (struct gdbarch *gdbarch, int regno)
1433 int i;
1434 int spec_regno;
1436 if (regno >= 0 && regno < NUM_GENREGS)
1438 /* General registers (R0 - R15) are 32 bits. */
1439 return 4;
1441 else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
1443 /* Special register (R16 - R31). cris_spec_regs is zero-based.
1444 Adjust regno accordingly. */
1445 spec_regno = regno - NUM_GENREGS;
1447 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1449 if (cris_spec_regs[i].number == spec_regno
1450 && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
1451 /* Go with the first applicable register. */
1452 return cris_spec_regs[i].reg_size;
1454 /* Special register not applicable to this CRIS version. */
1455 return 0;
1457 else if (regno >= gdbarch_pc_regnum (gdbarch)
1458 && regno < gdbarch_num_regs (gdbarch))
1460 /* This will apply to CRISv32 only where there are additional registers
1461 after the special registers (pseudo PC and support registers). */
1462 return 4;
1466 return -1;
1469 /* Nonzero if regno should not be fetched from the target. This is the case
1470 for unimplemented (size 0) and non-existant registers. */
1472 static int
1473 cris_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
1475 return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
1476 || (cris_register_size (gdbarch, regno) == 0));
1479 /* Nonzero if regno should not be written to the target, for various
1480 reasons. */
1482 static int
1483 cris_cannot_store_register (struct gdbarch *gdbarch, int regno)
1485 /* There are three kinds of registers we refuse to write to.
1486 1. Those that not implemented.
1487 2. Those that are read-only (depends on the processor mode).
1488 3. Those registers to which a write has no effect. */
1490 if (regno < 0
1491 || regno >= gdbarch_num_regs (gdbarch)
1492 || cris_register_size (gdbarch, regno) == 0)
1493 /* Not implemented. */
1494 return 1;
1496 else if (regno == VR_REGNUM)
1497 /* Read-only. */
1498 return 1;
1500 else if (regno == P0_REGNUM || regno == P4_REGNUM || regno == P8_REGNUM)
1501 /* Writing has no effect. */
1502 return 1;
1504 /* IBR, BAR, BRP and IRP are read-only in user mode. Let the debug
1505 agent decide whether they are writable. */
1507 return 0;
1510 /* Nonzero if regno should not be fetched from the target. This is the case
1511 for unimplemented (size 0) and non-existant registers. */
1513 static int
1514 crisv32_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
1516 return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
1517 || (cris_register_size (gdbarch, regno) == 0));
1520 /* Nonzero if regno should not be written to the target, for various
1521 reasons. */
1523 static int
1524 crisv32_cannot_store_register (struct gdbarch *gdbarch, int regno)
1526 /* There are three kinds of registers we refuse to write to.
1527 1. Those that not implemented.
1528 2. Those that are read-only (depends on the processor mode).
1529 3. Those registers to which a write has no effect. */
1531 if (regno < 0
1532 || regno >= gdbarch_num_regs (gdbarch)
1533 || cris_register_size (gdbarch, regno) == 0)
1534 /* Not implemented. */
1535 return 1;
1537 else if (regno == VR_REGNUM)
1538 /* Read-only. */
1539 return 1;
1541 else if (regno == BZ_REGNUM || regno == WZ_REGNUM || regno == DZ_REGNUM)
1542 /* Writing has no effect. */
1543 return 1;
1545 /* Many special registers are read-only in user mode. Let the debug
1546 agent decide whether they are writable. */
1548 return 0;
1551 /* Return the GDB type (defined in gdbtypes.c) for the "standard" data type
1552 of data in register regno. */
1554 static struct type *
1555 cris_register_type (struct gdbarch *gdbarch, int regno)
1557 if (regno == gdbarch_pc_regnum (gdbarch))
1558 return builtin_type (gdbarch)->builtin_func_ptr;
1559 else if (regno == gdbarch_sp_regnum (gdbarch)
1560 || regno == CRIS_FP_REGNUM)
1561 return builtin_type (gdbarch)->builtin_data_ptr;
1562 else if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1563 || (regno >= MOF_REGNUM && regno <= USP_REGNUM))
1564 /* Note: R8 taken care of previous clause. */
1565 return builtin_type (gdbarch)->builtin_uint32;
1566 else if (regno >= P4_REGNUM && regno <= CCR_REGNUM)
1567 return builtin_type (gdbarch)->builtin_uint16;
1568 else if (regno >= P0_REGNUM && regno <= VR_REGNUM)
1569 return builtin_type (gdbarch)->builtin_uint8;
1570 else
1571 /* Invalid (unimplemented) register. */
1572 return builtin_type (gdbarch)->builtin_int0;
1575 static struct type *
1576 crisv32_register_type (struct gdbarch *gdbarch, int regno)
1578 if (regno == gdbarch_pc_regnum (gdbarch))
1579 return builtin_type (gdbarch)->builtin_func_ptr;
1580 else if (regno == gdbarch_sp_regnum (gdbarch)
1581 || regno == CRIS_FP_REGNUM)
1582 return builtin_type (gdbarch)->builtin_data_ptr;
1583 else if ((regno >= 0 && regno <= ACR_REGNUM)
1584 || (regno >= EXS_REGNUM && regno <= SPC_REGNUM)
1585 || (regno == PID_REGNUM)
1586 || (regno >= S0_REGNUM && regno <= S15_REGNUM))
1587 /* Note: R8 and SP taken care of by previous clause. */
1588 return builtin_type (gdbarch)->builtin_uint32;
1589 else if (regno == WZ_REGNUM)
1590 return builtin_type (gdbarch)->builtin_uint16;
1591 else if (regno == BZ_REGNUM || regno == VR_REGNUM || regno == SRS_REGNUM)
1592 return builtin_type (gdbarch)->builtin_uint8;
1593 else
1595 /* Invalid (unimplemented) register. Should not happen as there are
1596 no unimplemented CRISv32 registers. */
1597 warning (_("crisv32_register_type: unknown regno %d"), regno);
1598 return builtin_type (gdbarch)->builtin_int0;
1602 /* Stores a function return value of type type, where valbuf is the address
1603 of the value to be stored. */
1605 /* In the CRIS ABI, R10 and R11 are used to store return values. */
1607 static void
1608 cris_store_return_value (struct type *type, struct regcache *regcache,
1609 const gdb_byte *valbuf)
1611 struct gdbarch *gdbarch = regcache->arch ();
1612 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1613 ULONGEST val;
1614 int len = type->length ();
1616 if (len <= 4)
1618 /* Put the return value in R10. */
1619 val = extract_unsigned_integer (valbuf, len, byte_order);
1620 regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
1622 else if (len <= 8)
1624 /* Put the return value in R10 and R11. */
1625 val = extract_unsigned_integer (valbuf, 4, byte_order);
1626 regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
1627 val = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
1628 regcache_cooked_write_unsigned (regcache, ARG2_REGNUM, val);
1630 else
1631 error (_("cris_store_return_value: type length too large."));
1634 /* Return the name of register regno as a string. Return NULL for an
1635 invalid or unimplemented register. */
1637 static const char *
1638 cris_special_register_name (struct gdbarch *gdbarch, int regno)
1640 int spec_regno;
1641 int i;
1643 /* Special register (R16 - R31). cris_spec_regs is zero-based.
1644 Adjust regno accordingly. */
1645 spec_regno = regno - NUM_GENREGS;
1647 /* Assume nothing about the layout of the cris_spec_regs struct
1648 when searching. */
1649 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1651 if (cris_spec_regs[i].number == spec_regno
1652 && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
1653 /* Go with the first applicable register. */
1654 return cris_spec_regs[i].name;
1656 /* Special register not applicable to this CRIS version. */
1657 return NULL;
1660 static const char *
1661 cris_register_name (struct gdbarch *gdbarch, int regno)
1663 static const char *cris_genreg_names[] =
1664 { "r0", "r1", "r2", "r3", \
1665 "r4", "r5", "r6", "r7", \
1666 "r8", "r9", "r10", "r11", \
1667 "r12", "r13", "sp", "pc" };
1669 if (regno >= 0 && regno < NUM_GENREGS)
1671 /* General register. */
1672 return cris_genreg_names[regno];
1674 else if (regno >= NUM_GENREGS && regno < gdbarch_num_regs (gdbarch))
1676 return cris_special_register_name (gdbarch, regno);
1678 else
1680 /* Invalid register. */
1681 return NULL;
1685 static const char *
1686 crisv32_register_name (struct gdbarch *gdbarch, int regno)
1688 static const char *crisv32_genreg_names[] =
1689 { "r0", "r1", "r2", "r3", \
1690 "r4", "r5", "r6", "r7", \
1691 "r8", "r9", "r10", "r11", \
1692 "r12", "r13", "sp", "acr"
1695 static const char *crisv32_sreg_names[] =
1696 { "s0", "s1", "s2", "s3", \
1697 "s4", "s5", "s6", "s7", \
1698 "s8", "s9", "s10", "s11", \
1699 "s12", "s13", "s14", "s15"
1702 if (regno >= 0 && regno < NUM_GENREGS)
1704 /* General register. */
1705 return crisv32_genreg_names[regno];
1707 else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
1709 return cris_special_register_name (gdbarch, regno);
1711 else if (regno == gdbarch_pc_regnum (gdbarch))
1713 return "pc";
1715 else if (regno >= S0_REGNUM && regno <= S15_REGNUM)
1717 return crisv32_sreg_names[regno - S0_REGNUM];
1719 else
1721 /* Invalid register. */
1722 return NULL;
1726 /* Convert DWARF register number REG to the appropriate register
1727 number used by GDB. */
1729 static int
1730 cris_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1732 /* We need to re-map a couple of registers (SRP is 16 in Dwarf-2 register
1733 numbering, MOF is 18).
1734 Adapted from gcc/config/cris/cris.h. */
1735 static int cris_dwarf_regmap[] = {
1736 0, 1, 2, 3,
1737 4, 5, 6, 7,
1738 8, 9, 10, 11,
1739 12, 13, 14, 15,
1740 27, -1, -1, -1,
1741 -1, -1, -1, 23,
1742 -1, -1, -1, 27,
1743 -1, -1, -1, -1
1745 int regnum = -1;
1747 if (reg >= 0 && reg < ARRAY_SIZE (cris_dwarf_regmap))
1748 regnum = cris_dwarf_regmap[reg];
1750 return regnum;
1753 /* DWARF-2 frame support. */
1755 static void
1756 cris_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1757 struct dwarf2_frame_state_reg *reg,
1758 struct frame_info *this_frame)
1760 /* The return address column. */
1761 if (regnum == gdbarch_pc_regnum (gdbarch))
1762 reg->how = DWARF2_FRAME_REG_RA;
1764 /* The call frame address. */
1765 else if (regnum == gdbarch_sp_regnum (gdbarch))
1766 reg->how = DWARF2_FRAME_REG_CFA;
1769 /* Extract from an array regbuf containing the raw register state a function
1770 return value of type type, and copy that, in virtual format, into
1771 valbuf. */
1773 /* In the CRIS ABI, R10 and R11 are used to store return values. */
1775 static void
1776 cris_extract_return_value (struct type *type, struct regcache *regcache,
1777 gdb_byte *valbuf)
1779 struct gdbarch *gdbarch = regcache->arch ();
1780 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1781 ULONGEST val;
1782 int len = type->length ();
1784 if (len <= 4)
1786 /* Get the return value from R10. */
1787 regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
1788 store_unsigned_integer (valbuf, len, byte_order, val);
1790 else if (len <= 8)
1792 /* Get the return value from R10 and R11. */
1793 regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
1794 store_unsigned_integer (valbuf, 4, byte_order, val);
1795 regcache_cooked_read_unsigned (regcache, ARG2_REGNUM, &val);
1796 store_unsigned_integer (valbuf + 4, len - 4, byte_order, val);
1798 else
1799 error (_("cris_extract_return_value: type length too large"));
1802 /* Handle the CRIS return value convention. */
1804 static enum return_value_convention
1805 cris_return_value (struct gdbarch *gdbarch, struct value *function,
1806 struct type *type, struct regcache *regcache,
1807 gdb_byte *readbuf, const gdb_byte *writebuf)
1809 if (type->code () == TYPE_CODE_STRUCT
1810 || type->code () == TYPE_CODE_UNION
1811 || type->length () > 8)
1812 /* Structs, unions, and anything larger than 8 bytes (2 registers)
1813 goes on the stack. */
1814 return RETURN_VALUE_STRUCT_CONVENTION;
1816 if (readbuf)
1817 cris_extract_return_value (type, regcache, readbuf);
1818 if (writebuf)
1819 cris_store_return_value (type, regcache, writebuf);
1821 return RETURN_VALUE_REGISTER_CONVENTION;
1824 /* Calculates a value that measures how good inst_args constraints an
1825 instruction. It stems from cris_constraint, found in cris-dis.c. */
1827 static int
1828 constraint (unsigned int insn, const char *inst_args,
1829 inst_env_type *inst_env)
1831 int retval = 0;
1832 int tmp, i;
1834 const gdb_byte *s = (const gdb_byte *) inst_args;
1836 for (; *s; s++)
1837 switch (*s)
1839 case 'm':
1840 if ((insn & 0x30) == 0x30)
1841 return -1;
1842 break;
1844 case 'S':
1845 /* A prefix operand. */
1846 if (inst_env->prefix_found)
1847 break;
1848 else
1849 return -1;
1851 case 'B':
1852 /* A "push" prefix. (This check was REMOVED by san 970921.) Check for
1853 valid "push" size. In case of special register, it may be != 4. */
1854 if (inst_env->prefix_found)
1855 break;
1856 else
1857 return -1;
1859 case 'D':
1860 retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
1861 if (!retval)
1862 return -1;
1863 else
1864 retval += 4;
1865 break;
1867 case 'P':
1868 tmp = (insn >> 0xC) & 0xF;
1870 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1872 /* Since we match four bits, we will give a value of
1873 4 - 1 = 3 in a match. If there is a corresponding
1874 exact match of a special register in another pattern, it
1875 will get a value of 4, which will be higher. This should
1876 be correct in that an exact pattern would match better that
1877 a general pattern.
1878 Note that there is a reason for not returning zero; the
1879 pattern for "clear" is partly matched in the bit-pattern
1880 (the two lower bits must be zero), while the bit-pattern
1881 for a move from a special register is matched in the
1882 register constraint.
1883 This also means we will will have a race condition if
1884 there is a partly match in three bits in the bit pattern. */
1885 if (tmp == cris_spec_regs[i].number)
1887 retval += 3;
1888 break;
1892 if (cris_spec_regs[i].name == NULL)
1893 return -1;
1894 break;
1896 return retval;
1899 /* Returns the number of bits set in the variable value. */
1901 static int
1902 number_of_bits (unsigned int value)
1904 int number_of_bits = 0;
1906 while (value != 0)
1908 number_of_bits += 1;
1909 value &= (value - 1);
1911 return number_of_bits;
1914 /* Finds the address that should contain the single step breakpoint(s).
1915 It stems from code in cris-dis.c. */
1917 static int
1918 find_cris_op (unsigned short insn, inst_env_type *inst_env)
1920 int i;
1921 int max_level_of_match = -1;
1922 int max_matched = -1;
1923 int level_of_match;
1925 for (i = 0; cris_opcodes[i].name != NULL; i++)
1927 if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match)
1928 && ((cris_opcodes[i].lose & insn) == 0)
1929 /* Only CRISv10 instructions, please. */
1930 && (cris_opcodes[i].applicable_version != cris_ver_v32p))
1932 level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
1933 if (level_of_match >= 0)
1935 level_of_match +=
1936 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
1937 if (level_of_match > max_level_of_match)
1939 max_matched = i;
1940 max_level_of_match = level_of_match;
1941 if (level_of_match == 16)
1943 /* All bits matched, cannot find better. */
1944 break;
1950 return max_matched;
1953 /* Attempts to find single-step breakpoints. Returns -1 on failure which is
1954 actually an internal error. */
1956 static int
1957 find_step_target (struct regcache *regcache, inst_env_type *inst_env)
1959 int i;
1960 int offset;
1961 unsigned short insn;
1962 struct gdbarch *gdbarch = regcache->arch ();
1963 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1965 /* Create a local register image and set the initial state. */
1966 for (i = 0; i < NUM_GENREGS; i++)
1968 inst_env->reg[i] =
1969 (unsigned long) regcache_raw_get_unsigned (regcache, i);
1971 offset = NUM_GENREGS;
1972 for (i = 0; i < NUM_SPECREGS; i++)
1974 inst_env->preg[i] =
1975 (unsigned long) regcache_raw_get_unsigned (regcache, offset + i);
1977 inst_env->branch_found = 0;
1978 inst_env->slot_needed = 0;
1979 inst_env->delay_slot_pc_active = 0;
1980 inst_env->prefix_found = 0;
1981 inst_env->invalid = 0;
1982 inst_env->xflag_found = 0;
1983 inst_env->disable_interrupt = 0;
1984 inst_env->byte_order = byte_order;
1986 /* Look for a step target. */
1989 /* Read an instruction from the client. */
1990 insn = read_memory_unsigned_integer
1991 (inst_env->reg[gdbarch_pc_regnum (gdbarch)], 2, byte_order);
1993 /* If the instruction is not in a delay slot the new content of the
1994 PC is [PC] + 2. If the instruction is in a delay slot it is not
1995 that simple. Since a instruction in a delay slot cannot change
1996 the content of the PC, it does not matter what value PC will have.
1997 Just make sure it is a valid instruction. */
1998 if (!inst_env->delay_slot_pc_active)
2000 inst_env->reg[gdbarch_pc_regnum (gdbarch)] += 2;
2002 else
2004 inst_env->delay_slot_pc_active = 0;
2005 inst_env->reg[gdbarch_pc_regnum (gdbarch)]
2006 = inst_env->delay_slot_pc;
2008 /* Analyse the present instruction. */
2009 i = find_cris_op (insn, inst_env);
2010 if (i == -1)
2012 inst_env->invalid = 1;
2014 else
2016 cris_gdb_func (gdbarch, cris_opcodes[i].op, insn, inst_env);
2018 } while (!inst_env->invalid
2019 && (inst_env->prefix_found || inst_env->xflag_found
2020 || inst_env->slot_needed));
2021 return i;
2024 /* There is no hardware single-step support. The function find_step_target
2025 digs through the opcodes in order to find all possible targets.
2026 Either one ordinary target or two targets for branches may be found. */
2028 static std::vector<CORE_ADDR>
2029 cris_software_single_step (struct regcache *regcache)
2031 struct gdbarch *gdbarch = regcache->arch ();
2032 inst_env_type inst_env;
2033 std::vector<CORE_ADDR> next_pcs;
2035 /* Analyse the present instruction environment and insert
2036 breakpoints. */
2037 int status = find_step_target (regcache, &inst_env);
2038 if (status == -1)
2040 /* Could not find a target. Things are likely to go downhill
2041 from here. */
2042 warning (_("CRIS software single step could not find a step target."));
2044 else
2046 /* Insert at most two breakpoints. One for the next PC content
2047 and possibly another one for a branch, jump, etc. */
2048 CORE_ADDR next_pc
2049 = (CORE_ADDR) inst_env.reg[gdbarch_pc_regnum (gdbarch)];
2051 next_pcs.push_back (next_pc);
2052 if (inst_env.branch_found
2053 && (CORE_ADDR) inst_env.branch_break_address != next_pc)
2055 CORE_ADDR branch_target_address
2056 = (CORE_ADDR) inst_env.branch_break_address;
2058 next_pcs.push_back (branch_target_address);
2062 return next_pcs;
2065 /* Calculates the prefix value for quick offset addressing mode. */
2067 static void
2068 quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
2070 /* It's invalid to be in a delay slot. You can't have a prefix to this
2071 instruction (not 100% sure). */
2072 if (inst_env->slot_needed || inst_env->prefix_found)
2074 inst_env->invalid = 1;
2075 return;
2078 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
2079 inst_env->prefix_value += cris_get_bdap_quick_offset (inst);
2081 /* A prefix doesn't change the xflag_found. But the rest of the flags
2082 need updating. */
2083 inst_env->slot_needed = 0;
2084 inst_env->prefix_found = 1;
2087 /* Updates the autoincrement register. The size of the increment is derived
2088 from the size of the operation. The PC is always kept aligned on even
2089 word addresses. */
2091 static void
2092 process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
2094 if (size == INST_BYTE_SIZE)
2096 inst_env->reg[cris_get_operand1 (inst)] += 1;
2098 /* The PC must be word aligned, so increase the PC with one
2099 word even if the size is byte. */
2100 if (cris_get_operand1 (inst) == REG_PC)
2102 inst_env->reg[REG_PC] += 1;
2105 else if (size == INST_WORD_SIZE)
2107 inst_env->reg[cris_get_operand1 (inst)] += 2;
2109 else if (size == INST_DWORD_SIZE)
2111 inst_env->reg[cris_get_operand1 (inst)] += 4;
2113 else
2115 /* Invalid size. */
2116 inst_env->invalid = 1;
2120 /* Just a forward declaration. */
2122 static unsigned long get_data_from_address (unsigned short *inst,
2123 CORE_ADDR address,
2124 enum bfd_endian byte_order);
2126 /* Calculates the prefix value for the general case of offset addressing
2127 mode. */
2129 static void
2130 bdap_prefix (unsigned short inst, inst_env_type *inst_env)
2132 /* It's invalid to be in a delay slot. */
2133 if (inst_env->slot_needed || inst_env->prefix_found)
2135 inst_env->invalid = 1;
2136 return;
2139 /* The calculation of prefix_value used to be after process_autoincrement,
2140 but that fails for an instruction such as jsr [$r0+12] which is encoded
2141 as 5f0d 0c00 30b9 when compiled with -fpic. Since PC is operand1 it
2142 mustn't be incremented until we have read it and what it points at. */
2143 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
2145 /* The offset is an indirection of the contents of the operand1 register. */
2146 inst_env->prefix_value +=
2147 get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)],
2148 inst_env->byte_order);
2150 if (cris_get_mode (inst) == AUTOINC_MODE)
2152 process_autoincrement (cris_get_size (inst), inst, inst_env);
2155 /* A prefix doesn't change the xflag_found. But the rest of the flags
2156 need updating. */
2157 inst_env->slot_needed = 0;
2158 inst_env->prefix_found = 1;
2161 /* Calculates the prefix value for the index addressing mode. */
2163 static void
2164 biap_prefix (unsigned short inst, inst_env_type *inst_env)
2166 /* It's invalid to be in a delay slot. I can't see that it's possible to
2167 have a prefix to this instruction. So I will treat this as invalid. */
2168 if (inst_env->slot_needed || inst_env->prefix_found)
2170 inst_env->invalid = 1;
2171 return;
2174 inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];
2176 /* The offset is the operand2 value shifted the size of the instruction
2177 to the left. */
2178 inst_env->prefix_value +=
2179 inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);
2181 /* If the PC is operand1 (base) the address used is the address after
2182 the main instruction, i.e. address + 2 (the PC is already compensated
2183 for the prefix operation). */
2184 if (cris_get_operand1 (inst) == REG_PC)
2186 inst_env->prefix_value += 2;
2189 /* A prefix doesn't change the xflag_found. But the rest of the flags
2190 need updating. */
2191 inst_env->slot_needed = 0;
2192 inst_env->xflag_found = 0;
2193 inst_env->prefix_found = 1;
2196 /* Calculates the prefix value for the double indirect addressing mode. */
2198 static void
2199 dip_prefix (unsigned short inst, inst_env_type *inst_env)
2202 CORE_ADDR address;
2204 /* It's invalid to be in a delay slot. */
2205 if (inst_env->slot_needed || inst_env->prefix_found)
2207 inst_env->invalid = 1;
2208 return;
2211 /* The prefix value is one dereference of the contents of the operand1
2212 register. */
2213 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2214 inst_env->prefix_value
2215 = read_memory_unsigned_integer (address, 4, inst_env->byte_order);
2217 /* Check if the mode is autoincrement. */
2218 if (cris_get_mode (inst) == AUTOINC_MODE)
2220 inst_env->reg[cris_get_operand1 (inst)] += 4;
2223 /* A prefix doesn't change the xflag_found. But the rest of the flags
2224 need updating. */
2225 inst_env->slot_needed = 0;
2226 inst_env->xflag_found = 0;
2227 inst_env->prefix_found = 1;
2230 /* Finds the destination for a branch with 8-bits offset. */
2232 static void
2233 eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2236 short offset;
2238 /* If we have a prefix or are in a delay slot it's bad. */
2239 if (inst_env->slot_needed || inst_env->prefix_found)
2241 inst_env->invalid = 1;
2242 return;
2245 /* We have a branch, find out where the branch will land. */
2246 offset = cris_get_branch_short_offset (inst);
2248 /* Check if the offset is signed. */
2249 if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
2251 offset |= 0xFF00;
2254 /* The offset ends with the sign bit, set it to zero. The address
2255 should always be word aligned. */
2256 offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;
2258 inst_env->branch_found = 1;
2259 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2261 inst_env->slot_needed = 1;
2262 inst_env->prefix_found = 0;
2263 inst_env->xflag_found = 0;
2264 inst_env->disable_interrupt = 1;
2267 /* Finds the destination for a branch with 16-bits offset. */
2269 static void
2270 sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2272 short offset;
2274 /* If we have a prefix or is in a delay slot it's bad. */
2275 if (inst_env->slot_needed || inst_env->prefix_found)
2277 inst_env->invalid = 1;
2278 return;
2281 /* We have a branch, find out the offset for the branch. */
2282 offset = read_memory_integer (inst_env->reg[REG_PC], 2,
2283 inst_env->byte_order);
2285 /* The instruction is one word longer than normal, so add one word
2286 to the PC. */
2287 inst_env->reg[REG_PC] += 2;
2289 inst_env->branch_found = 1;
2290 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2293 inst_env->slot_needed = 1;
2294 inst_env->prefix_found = 0;
2295 inst_env->xflag_found = 0;
2296 inst_env->disable_interrupt = 1;
2299 /* Handles the ABS instruction. */
2301 static void
2302 abs_op (unsigned short inst, inst_env_type *inst_env)
2305 long value;
2307 /* ABS can't have a prefix, so it's bad if it does. */
2308 if (inst_env->prefix_found)
2310 inst_env->invalid = 1;
2311 return;
2314 /* Check if the operation affects the PC. */
2315 if (cris_get_operand2 (inst) == REG_PC)
2318 /* It's invalid to change to the PC if we are in a delay slot. */
2319 if (inst_env->slot_needed)
2321 inst_env->invalid = 1;
2322 return;
2325 value = (long) inst_env->reg[REG_PC];
2327 /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK. */
2328 if (value != SIGNED_DWORD_MASK)
2330 value = -value;
2331 inst_env->reg[REG_PC] = (long) value;
2335 inst_env->slot_needed = 0;
2336 inst_env->prefix_found = 0;
2337 inst_env->xflag_found = 0;
2338 inst_env->disable_interrupt = 0;
2341 /* Handles the ADDI instruction. */
2343 static void
2344 addi_op (unsigned short inst, inst_env_type *inst_env)
2346 /* It's invalid to have the PC as base register. And ADDI can't have
2347 a prefix. */
2348 if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
2350 inst_env->invalid = 1;
2351 return;
2354 inst_env->slot_needed = 0;
2355 inst_env->prefix_found = 0;
2356 inst_env->xflag_found = 0;
2357 inst_env->disable_interrupt = 0;
2360 /* Handles the ASR instruction. */
2362 static void
2363 asr_op (unsigned short inst, inst_env_type *inst_env)
2365 int shift_steps;
2366 unsigned long value;
2367 unsigned long signed_extend_mask = 0;
2369 /* ASR can't have a prefix, so check that it doesn't. */
2370 if (inst_env->prefix_found)
2372 inst_env->invalid = 1;
2373 return;
2376 /* Check if the PC is the target register. */
2377 if (cris_get_operand2 (inst) == REG_PC)
2379 /* It's invalid to change the PC in a delay slot. */
2380 if (inst_env->slot_needed)
2382 inst_env->invalid = 1;
2383 return;
2385 /* Get the number of bits to shift. */
2386 shift_steps
2387 = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
2388 value = inst_env->reg[REG_PC];
2390 /* Find out how many bits the operation should apply to. */
2391 if (cris_get_size (inst) == INST_BYTE_SIZE)
2393 if (value & SIGNED_BYTE_MASK)
2395 signed_extend_mask = 0xFF;
2396 signed_extend_mask = signed_extend_mask >> shift_steps;
2397 signed_extend_mask = ~signed_extend_mask;
2399 value = value >> shift_steps;
2400 value |= signed_extend_mask;
2401 value &= 0xFF;
2402 inst_env->reg[REG_PC] &= 0xFFFFFF00;
2403 inst_env->reg[REG_PC] |= value;
2405 else if (cris_get_size (inst) == INST_WORD_SIZE)
2407 if (value & SIGNED_WORD_MASK)
2409 signed_extend_mask = 0xFFFF;
2410 signed_extend_mask = signed_extend_mask >> shift_steps;
2411 signed_extend_mask = ~signed_extend_mask;
2413 value = value >> shift_steps;
2414 value |= signed_extend_mask;
2415 value &= 0xFFFF;
2416 inst_env->reg[REG_PC] &= 0xFFFF0000;
2417 inst_env->reg[REG_PC] |= value;
2419 else if (cris_get_size (inst) == INST_DWORD_SIZE)
2421 if (value & SIGNED_DWORD_MASK)
2423 signed_extend_mask = 0xFFFFFFFF;
2424 signed_extend_mask = signed_extend_mask >> shift_steps;
2425 signed_extend_mask = ~signed_extend_mask;
2427 value = value >> shift_steps;
2428 value |= signed_extend_mask;
2429 inst_env->reg[REG_PC] = value;
2432 inst_env->slot_needed = 0;
2433 inst_env->prefix_found = 0;
2434 inst_env->xflag_found = 0;
2435 inst_env->disable_interrupt = 0;
2438 /* Handles the ASRQ instruction. */
2440 static void
2441 asrq_op (unsigned short inst, inst_env_type *inst_env)
2444 int shift_steps;
2445 unsigned long value;
2446 unsigned long signed_extend_mask = 0;
2448 /* ASRQ can't have a prefix, so check that it doesn't. */
2449 if (inst_env->prefix_found)
2451 inst_env->invalid = 1;
2452 return;
2455 /* Check if the PC is the target register. */
2456 if (cris_get_operand2 (inst) == REG_PC)
2459 /* It's invalid to change the PC in a delay slot. */
2460 if (inst_env->slot_needed)
2462 inst_env->invalid = 1;
2463 return;
2465 /* The shift size is given as a 5 bit quick value, i.e. we don't
2466 want the sign bit of the quick value. */
2467 shift_steps = cris_get_asr_shift_steps (inst);
2468 value = inst_env->reg[REG_PC];
2469 if (value & SIGNED_DWORD_MASK)
2471 signed_extend_mask = 0xFFFFFFFF;
2472 signed_extend_mask = signed_extend_mask >> shift_steps;
2473 signed_extend_mask = ~signed_extend_mask;
2475 value = value >> shift_steps;
2476 value |= signed_extend_mask;
2477 inst_env->reg[REG_PC] = value;
2479 inst_env->slot_needed = 0;
2480 inst_env->prefix_found = 0;
2481 inst_env->xflag_found = 0;
2482 inst_env->disable_interrupt = 0;
2485 /* Handles the AX, EI and SETF instruction. */
2487 static void
2488 ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
2490 if (inst_env->prefix_found)
2492 inst_env->invalid = 1;
2493 return;
2495 /* Check if the instruction is setting the X flag. */
2496 if (cris_is_xflag_bit_on (inst))
2498 inst_env->xflag_found = 1;
2500 else
2502 inst_env->xflag_found = 0;
2504 inst_env->slot_needed = 0;
2505 inst_env->prefix_found = 0;
2506 inst_env->disable_interrupt = 1;
2509 /* Checks if the instruction is in assign mode. If so, it updates the assign
2510 register. Note that check_assign assumes that the caller has checked that
2511 there is a prefix to this instruction. The mode check depends on this. */
2513 static void
2514 check_assign (unsigned short inst, inst_env_type *inst_env)
2516 /* Check if it's an assign addressing mode. */
2517 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2519 /* Assign the prefix value to operand 1. */
2520 inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
2524 /* Handles the 2-operand BOUND instruction. */
2526 static void
2527 two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2529 /* It's invalid to have the PC as the index operand. */
2530 if (cris_get_operand2 (inst) == REG_PC)
2532 inst_env->invalid = 1;
2533 return;
2535 /* Check if we have a prefix. */
2536 if (inst_env->prefix_found)
2538 check_assign (inst, inst_env);
2540 /* Check if this is an autoincrement mode. */
2541 else if (cris_get_mode (inst) == AUTOINC_MODE)
2543 /* It's invalid to change the PC in a delay slot. */
2544 if (inst_env->slot_needed)
2546 inst_env->invalid = 1;
2547 return;
2549 process_autoincrement (cris_get_size (inst), inst, inst_env);
2551 inst_env->slot_needed = 0;
2552 inst_env->prefix_found = 0;
2553 inst_env->xflag_found = 0;
2554 inst_env->disable_interrupt = 0;
2557 /* Handles the 3-operand BOUND instruction. */
2559 static void
2560 three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2562 /* It's an error if we haven't got a prefix. And it's also an error
2563 if the PC is the destination register. */
2564 if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
2566 inst_env->invalid = 1;
2567 return;
2569 inst_env->slot_needed = 0;
2570 inst_env->prefix_found = 0;
2571 inst_env->xflag_found = 0;
2572 inst_env->disable_interrupt = 0;
2575 /* Clears the status flags in inst_env. */
2577 static void
2578 btst_nop_op (unsigned short inst, inst_env_type *inst_env)
2580 /* It's an error if we have got a prefix. */
2581 if (inst_env->prefix_found)
2583 inst_env->invalid = 1;
2584 return;
2587 inst_env->slot_needed = 0;
2588 inst_env->prefix_found = 0;
2589 inst_env->xflag_found = 0;
2590 inst_env->disable_interrupt = 0;
2593 /* Clears the status flags in inst_env. */
2595 static void
2596 clearf_di_op (unsigned short inst, inst_env_type *inst_env)
2598 /* It's an error if we have got a prefix. */
2599 if (inst_env->prefix_found)
2601 inst_env->invalid = 1;
2602 return;
2605 inst_env->slot_needed = 0;
2606 inst_env->prefix_found = 0;
2607 inst_env->xflag_found = 0;
2608 inst_env->disable_interrupt = 1;
2611 /* Handles the CLEAR instruction if it's in register mode. */
2613 static void
2614 reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
2616 /* Check if the target is the PC. */
2617 if (cris_get_operand2 (inst) == REG_PC)
2619 /* The instruction will clear the instruction's size bits. */
2620 int clear_size = cris_get_clear_size (inst);
2621 if (clear_size == INST_BYTE_SIZE)
2623 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
2625 if (clear_size == INST_WORD_SIZE)
2627 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
2629 if (clear_size == INST_DWORD_SIZE)
2631 inst_env->delay_slot_pc = 0x0;
2633 /* The jump will be delayed with one delay slot. So we need a delay
2634 slot. */
2635 inst_env->slot_needed = 1;
2636 inst_env->delay_slot_pc_active = 1;
2638 else
2640 /* The PC will not change => no delay slot. */
2641 inst_env->slot_needed = 0;
2643 inst_env->prefix_found = 0;
2644 inst_env->xflag_found = 0;
2645 inst_env->disable_interrupt = 0;
2648 /* Handles the TEST instruction if it's in register mode. */
2650 static void
2651 reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
2653 /* It's an error if we have got a prefix. */
2654 if (inst_env->prefix_found)
2656 inst_env->invalid = 1;
2657 return;
2659 inst_env->slot_needed = 0;
2660 inst_env->prefix_found = 0;
2661 inst_env->xflag_found = 0;
2662 inst_env->disable_interrupt = 0;
2666 /* Handles the CLEAR and TEST instruction if the instruction isn't
2667 in register mode. */
2669 static void
2670 none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
2672 /* Check if we are in a prefix mode. */
2673 if (inst_env->prefix_found)
2675 /* The only way the PC can change is if this instruction is in
2676 assign addressing mode. */
2677 check_assign (inst, inst_env);
2679 /* Indirect mode can't change the PC so just check if the mode is
2680 autoincrement. */
2681 else if (cris_get_mode (inst) == AUTOINC_MODE)
2683 process_autoincrement (cris_get_size (inst), inst, inst_env);
2685 inst_env->slot_needed = 0;
2686 inst_env->prefix_found = 0;
2687 inst_env->xflag_found = 0;
2688 inst_env->disable_interrupt = 0;
2691 /* Checks that the PC isn't the destination register or the instructions has
2692 a prefix. */
2694 static void
2695 dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
2697 /* It's invalid to have the PC as the destination. The instruction can't
2698 have a prefix. */
2699 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2701 inst_env->invalid = 1;
2702 return;
2705 inst_env->slot_needed = 0;
2706 inst_env->prefix_found = 0;
2707 inst_env->xflag_found = 0;
2708 inst_env->disable_interrupt = 0;
2711 /* Checks that the instruction doesn't have a prefix. */
2713 static void
2714 break_op (unsigned short inst, inst_env_type *inst_env)
2716 /* The instruction can't have a prefix. */
2717 if (inst_env->prefix_found)
2719 inst_env->invalid = 1;
2720 return;
2723 inst_env->slot_needed = 0;
2724 inst_env->prefix_found = 0;
2725 inst_env->xflag_found = 0;
2726 inst_env->disable_interrupt = 1;
2729 /* Checks that the PC isn't the destination register and that the instruction
2730 doesn't have a prefix. */
2732 static void
2733 scc_op (unsigned short inst, inst_env_type *inst_env)
2735 /* It's invalid to have the PC as the destination. The instruction can't
2736 have a prefix. */
2737 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2739 inst_env->invalid = 1;
2740 return;
2743 inst_env->slot_needed = 0;
2744 inst_env->prefix_found = 0;
2745 inst_env->xflag_found = 0;
2746 inst_env->disable_interrupt = 1;
2749 /* Handles the register mode JUMP instruction. */
2751 static void
2752 reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2754 /* It's invalid to do a JUMP in a delay slot. The mode is register, so
2755 you can't have a prefix. */
2756 if ((inst_env->slot_needed) || (inst_env->prefix_found))
2758 inst_env->invalid = 1;
2759 return;
2762 /* Just change the PC. */
2763 inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
2764 inst_env->slot_needed = 0;
2765 inst_env->prefix_found = 0;
2766 inst_env->xflag_found = 0;
2767 inst_env->disable_interrupt = 1;
2770 /* Handles the JUMP instruction for all modes except register. */
2772 static void
2773 none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2775 unsigned long newpc;
2776 CORE_ADDR address;
2778 /* It's invalid to do a JUMP in a delay slot. */
2779 if (inst_env->slot_needed)
2781 inst_env->invalid = 1;
2783 else
2785 /* Check if we have a prefix. */
2786 if (inst_env->prefix_found)
2788 check_assign (inst, inst_env);
2790 /* Get the new value for the PC. */
2791 newpc =
2792 read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
2793 4, inst_env->byte_order);
2795 else
2797 /* Get the new value for the PC. */
2798 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2799 newpc = read_memory_unsigned_integer (address,
2800 4, inst_env->byte_order);
2802 /* Check if we should increment a register. */
2803 if (cris_get_mode (inst) == AUTOINC_MODE)
2805 inst_env->reg[cris_get_operand1 (inst)] += 4;
2808 inst_env->reg[REG_PC] = newpc;
2810 inst_env->slot_needed = 0;
2811 inst_env->prefix_found = 0;
2812 inst_env->xflag_found = 0;
2813 inst_env->disable_interrupt = 1;
2816 /* Handles moves to special registers (aka P-register) for all modes. */
2818 static void
2819 move_to_preg_op (struct gdbarch *gdbarch, unsigned short inst,
2820 inst_env_type *inst_env)
2822 if (inst_env->prefix_found)
2824 /* The instruction has a prefix that means we are only interested if
2825 the instruction is in assign mode. */
2826 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2828 /* The prefix handles the problem if we are in a delay slot. */
2829 if (cris_get_operand1 (inst) == REG_PC)
2831 /* Just take care of the assign. */
2832 check_assign (inst, inst_env);
2836 else if (cris_get_mode (inst) == AUTOINC_MODE)
2838 /* The instruction doesn't have a prefix, the only case left that we
2839 are interested in is the autoincrement mode. */
2840 if (cris_get_operand1 (inst) == REG_PC)
2842 /* If the PC is to be incremented it's invalid to be in a
2843 delay slot. */
2844 if (inst_env->slot_needed)
2846 inst_env->invalid = 1;
2847 return;
2850 /* The increment depends on the size of the special register. */
2851 if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
2853 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2855 else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
2857 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2859 else
2861 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2865 inst_env->slot_needed = 0;
2866 inst_env->prefix_found = 0;
2867 inst_env->xflag_found = 0;
2868 inst_env->disable_interrupt = 1;
2871 /* Handles moves from special registers (aka P-register) for all modes
2872 except register. */
2874 static void
2875 none_reg_mode_move_from_preg_op (struct gdbarch *gdbarch, unsigned short inst,
2876 inst_env_type *inst_env)
2878 if (inst_env->prefix_found)
2880 /* The instruction has a prefix that means we are only interested if
2881 the instruction is in assign mode. */
2882 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2884 /* The prefix handles the problem if we are in a delay slot. */
2885 if (cris_get_operand1 (inst) == REG_PC)
2887 /* Just take care of the assign. */
2888 check_assign (inst, inst_env);
2892 /* The instruction doesn't have a prefix, the only case left that we
2893 are interested in is the autoincrement mode. */
2894 else if (cris_get_mode (inst) == AUTOINC_MODE)
2896 if (cris_get_operand1 (inst) == REG_PC)
2898 /* If the PC is to be incremented it's invalid to be in a
2899 delay slot. */
2900 if (inst_env->slot_needed)
2902 inst_env->invalid = 1;
2903 return;
2906 /* The increment depends on the size of the special register. */
2907 if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
2909 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2911 else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
2913 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2915 else
2917 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2921 inst_env->slot_needed = 0;
2922 inst_env->prefix_found = 0;
2923 inst_env->xflag_found = 0;
2924 inst_env->disable_interrupt = 1;
2927 /* Handles moves from special registers (aka P-register) when the mode
2928 is register. */
2930 static void
2931 reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2933 /* Register mode move from special register can't have a prefix. */
2934 if (inst_env->prefix_found)
2936 inst_env->invalid = 1;
2937 return;
2940 if (cris_get_operand1 (inst) == REG_PC)
2942 /* It's invalid to change the PC in a delay slot. */
2943 if (inst_env->slot_needed)
2945 inst_env->invalid = 1;
2946 return;
2948 /* The destination is the PC, the jump will have a delay slot. */
2949 inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
2950 inst_env->slot_needed = 1;
2951 inst_env->delay_slot_pc_active = 1;
2953 else
2955 /* If the destination isn't PC, there will be no jump. */
2956 inst_env->slot_needed = 0;
2958 inst_env->prefix_found = 0;
2959 inst_env->xflag_found = 0;
2960 inst_env->disable_interrupt = 1;
2963 /* Handles the MOVEM from memory to general register instruction. */
2965 static void
2966 move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
2968 if (inst_env->prefix_found)
2970 /* The prefix handles the problem if we are in a delay slot. Is the
2971 MOVEM instruction going to change the PC? */
2972 if (cris_get_operand2 (inst) >= REG_PC)
2974 inst_env->reg[REG_PC] =
2975 read_memory_unsigned_integer (inst_env->prefix_value,
2976 4, inst_env->byte_order);
2978 /* The assign value is the value after the increment. Normally, the
2979 assign value is the value before the increment. */
2980 if ((cris_get_operand1 (inst) == REG_PC)
2981 && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2983 inst_env->reg[REG_PC] = inst_env->prefix_value;
2984 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2987 else
2989 /* Is the MOVEM instruction going to change the PC? */
2990 if (cris_get_operand2 (inst) == REG_PC)
2992 /* It's invalid to change the PC in a delay slot. */
2993 if (inst_env->slot_needed)
2995 inst_env->invalid = 1;
2996 return;
2998 inst_env->reg[REG_PC] =
2999 read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)],
3000 4, inst_env->byte_order);
3002 /* The increment is not depending on the size, instead it's depending
3003 on the number of registers loaded from memory. */
3004 if ((cris_get_operand1 (inst) == REG_PC)
3005 && (cris_get_mode (inst) == AUTOINC_MODE))
3007 /* It's invalid to change the PC in a delay slot. */
3008 if (inst_env->slot_needed)
3010 inst_env->invalid = 1;
3011 return;
3013 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3016 inst_env->slot_needed = 0;
3017 inst_env->prefix_found = 0;
3018 inst_env->xflag_found = 0;
3019 inst_env->disable_interrupt = 0;
3022 /* Handles the MOVEM to memory from general register instruction. */
3024 static void
3025 move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
3027 if (inst_env->prefix_found)
3029 /* The assign value is the value after the increment. Normally, the
3030 assign value is the value before the increment. */
3031 if ((cris_get_operand1 (inst) == REG_PC)
3032 && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
3034 /* The prefix handles the problem if we are in a delay slot. */
3035 inst_env->reg[REG_PC] = inst_env->prefix_value;
3036 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3039 else
3041 /* The increment is not depending on the size, instead it's depending
3042 on the number of registers loaded to memory. */
3043 if ((cris_get_operand1 (inst) == REG_PC)
3044 && (cris_get_mode (inst) == AUTOINC_MODE))
3046 /* It's invalid to change the PC in a delay slot. */
3047 if (inst_env->slot_needed)
3049 inst_env->invalid = 1;
3050 return;
3052 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3055 inst_env->slot_needed = 0;
3056 inst_env->prefix_found = 0;
3057 inst_env->xflag_found = 0;
3058 inst_env->disable_interrupt = 0;
3061 /* Handles the instructions that's not yet implemented, by setting
3062 inst_env->invalid to true. */
3064 static void
3065 not_implemented_op (unsigned short inst, inst_env_type *inst_env)
3067 inst_env->invalid = 1;
3070 /* Handles the XOR instruction. */
3072 static void
3073 xor_op (unsigned short inst, inst_env_type *inst_env)
3075 /* XOR can't have a prefix. */
3076 if (inst_env->prefix_found)
3078 inst_env->invalid = 1;
3079 return;
3082 /* Check if the PC is the target. */
3083 if (cris_get_operand2 (inst) == REG_PC)
3085 /* It's invalid to change the PC in a delay slot. */
3086 if (inst_env->slot_needed)
3088 inst_env->invalid = 1;
3089 return;
3091 inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
3093 inst_env->slot_needed = 0;
3094 inst_env->prefix_found = 0;
3095 inst_env->xflag_found = 0;
3096 inst_env->disable_interrupt = 0;
3099 /* Handles the MULS instruction. */
3101 static void
3102 muls_op (unsigned short inst, inst_env_type *inst_env)
3104 /* MULS/U can't have a prefix. */
3105 if (inst_env->prefix_found)
3107 inst_env->invalid = 1;
3108 return;
3111 /* Consider it invalid if the PC is the target. */
3112 if (cris_get_operand2 (inst) == REG_PC)
3114 inst_env->invalid = 1;
3115 return;
3117 inst_env->slot_needed = 0;
3118 inst_env->prefix_found = 0;
3119 inst_env->xflag_found = 0;
3120 inst_env->disable_interrupt = 0;
3123 /* Handles the MULU instruction. */
3125 static void
3126 mulu_op (unsigned short inst, inst_env_type *inst_env)
3128 /* MULS/U can't have a prefix. */
3129 if (inst_env->prefix_found)
3131 inst_env->invalid = 1;
3132 return;
3135 /* Consider it invalid if the PC is the target. */
3136 if (cris_get_operand2 (inst) == REG_PC)
3138 inst_env->invalid = 1;
3139 return;
3141 inst_env->slot_needed = 0;
3142 inst_env->prefix_found = 0;
3143 inst_env->xflag_found = 0;
3144 inst_env->disable_interrupt = 0;
3147 /* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE.
3148 The MOVE instruction is the move from source to register. */
3150 static void
3151 add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env,
3152 unsigned long source1, unsigned long source2)
3154 unsigned long pc_mask;
3155 unsigned long operation_mask;
3157 /* Find out how many bits the operation should apply to. */
3158 if (cris_get_size (inst) == INST_BYTE_SIZE)
3160 pc_mask = 0xFFFFFF00;
3161 operation_mask = 0xFF;
3163 else if (cris_get_size (inst) == INST_WORD_SIZE)
3165 pc_mask = 0xFFFF0000;
3166 operation_mask = 0xFFFF;
3168 else if (cris_get_size (inst) == INST_DWORD_SIZE)
3170 pc_mask = 0x0;
3171 operation_mask = 0xFFFFFFFF;
3173 else
3175 /* The size is out of range. */
3176 inst_env->invalid = 1;
3177 return;
3180 /* The instruction just works on uw_operation_mask bits. */
3181 source2 &= operation_mask;
3182 source1 &= operation_mask;
3184 /* Now calculate the result. The opcode's 3 first bits separates
3185 the different actions. */
3186 switch (cris_get_opcode (inst) & 7)
3188 case 0: /* add */
3189 source1 += source2;
3190 break;
3192 case 1: /* move */
3193 source1 = source2;
3194 break;
3196 case 2: /* subtract */
3197 source1 -= source2;
3198 break;
3200 case 3: /* compare */
3201 break;
3203 case 4: /* and */
3204 source1 &= source2;
3205 break;
3207 case 5: /* or */
3208 source1 |= source2;
3209 break;
3211 default:
3212 inst_env->invalid = 1;
3213 return;
3215 break;
3218 /* Make sure that the result doesn't contain more than the instruction
3219 size bits. */
3220 source2 &= operation_mask;
3222 /* Calculate the new breakpoint address. */
3223 inst_env->reg[REG_PC] &= pc_mask;
3224 inst_env->reg[REG_PC] |= source1;
3228 /* Extends the value from either byte or word size to a dword. If the mode
3229 is zero extend then the value is extended with zero. If instead the mode
3230 is signed extend the sign bit of the value is taken into consideration. */
3232 static unsigned long
3233 do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
3235 /* The size can be either byte or word, check which one it is.
3236 Don't check the highest bit, it's indicating if it's a zero
3237 or sign extend. */
3238 if (cris_get_size (*inst) & INST_WORD_SIZE)
3240 /* Word size. */
3241 value &= 0xFFFF;
3243 /* Check if the instruction is signed extend. If so, check if value has
3244 the sign bit on. */
3245 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
3247 value |= SIGNED_WORD_EXTEND_MASK;
3250 else
3252 /* Byte size. */
3253 value &= 0xFF;
3255 /* Check if the instruction is signed extend. If so, check if value has
3256 the sign bit on. */
3257 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
3259 value |= SIGNED_BYTE_EXTEND_MASK;
3262 /* The size should now be dword. */
3263 cris_set_size_to_dword (inst);
3264 return value;
3267 /* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
3268 instruction. The MOVE instruction is the move from source to register. */
3270 static void
3271 reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3272 inst_env_type *inst_env)
3274 unsigned long operand1;
3275 unsigned long operand2;
3277 /* It's invalid to have a prefix to the instruction. This is a register
3278 mode instruction and can't have a prefix. */
3279 if (inst_env->prefix_found)
3281 inst_env->invalid = 1;
3282 return;
3284 /* Check if the instruction has PC as its target. */
3285 if (cris_get_operand2 (inst) == REG_PC)
3287 if (inst_env->slot_needed)
3289 inst_env->invalid = 1;
3290 return;
3292 /* The instruction has the PC as its target register. */
3293 operand1 = inst_env->reg[cris_get_operand1 (inst)];
3294 operand2 = inst_env->reg[REG_PC];
3296 /* Check if it's a extend, signed or zero instruction. */
3297 if (cris_get_opcode (inst) < 4)
3299 operand1 = do_sign_or_zero_extend (operand1, &inst);
3301 /* Calculate the PC value after the instruction, i.e. where the
3302 breakpoint should be. The order of the udw_operands is vital. */
3303 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3305 inst_env->slot_needed = 0;
3306 inst_env->prefix_found = 0;
3307 inst_env->xflag_found = 0;
3308 inst_env->disable_interrupt = 0;
3311 /* Returns the data contained at address. The size of the data is derived from
3312 the size of the operation. If the instruction is a zero or signed
3313 extend instruction, the size field is changed in instruction. */
3315 static unsigned long
3316 get_data_from_address (unsigned short *inst, CORE_ADDR address,
3317 enum bfd_endian byte_order)
3319 int size = cris_get_size (*inst);
3320 unsigned long value;
3322 /* If it's an extend instruction we don't want the signed extend bit,
3323 because it influences the size. */
3324 if (cris_get_opcode (*inst) < 4)
3326 size &= ~SIGNED_EXTEND_BIT_MASK;
3328 /* Is there a need for checking the size? Size should contain the number of
3329 bytes to read. */
3330 size = 1 << size;
3331 value = read_memory_unsigned_integer (address, size, byte_order);
3333 /* Check if it's an extend, signed or zero instruction. */
3334 if (cris_get_opcode (*inst) < 4)
3336 value = do_sign_or_zero_extend (value, inst);
3338 return value;
3341 /* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3342 instructions. The MOVE instruction is the move from source to register. */
3344 static void
3345 handle_prefix_assign_mode_for_aritm_op (unsigned short inst,
3346 inst_env_type *inst_env)
3348 unsigned long operand2;
3349 unsigned long operand3;
3351 check_assign (inst, inst_env);
3352 if (cris_get_operand2 (inst) == REG_PC)
3354 operand2 = inst_env->reg[REG_PC];
3356 /* Get the value of the third operand. */
3357 operand3 = get_data_from_address (&inst, inst_env->prefix_value,
3358 inst_env->byte_order);
3360 /* Calculate the PC value after the instruction, i.e. where the
3361 breakpoint should be. The order of the udw_operands is vital. */
3362 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3364 inst_env->slot_needed = 0;
3365 inst_env->prefix_found = 0;
3366 inst_env->xflag_found = 0;
3367 inst_env->disable_interrupt = 0;
3370 /* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
3371 OR instructions. Note that for this to work as expected, the calling
3372 function must have made sure that there is a prefix to this instruction. */
3374 static void
3375 three_operand_add_sub_cmp_and_or_op (unsigned short inst,
3376 inst_env_type *inst_env)
3378 unsigned long operand2;
3379 unsigned long operand3;
3381 if (cris_get_operand1 (inst) == REG_PC)
3383 /* The PC will be changed by the instruction. */
3384 operand2 = inst_env->reg[cris_get_operand2 (inst)];
3386 /* Get the value of the third operand. */
3387 operand3 = get_data_from_address (&inst, inst_env->prefix_value,
3388 inst_env->byte_order);
3390 /* Calculate the PC value after the instruction, i.e. where the
3391 breakpoint should be. */
3392 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3394 inst_env->slot_needed = 0;
3395 inst_env->prefix_found = 0;
3396 inst_env->xflag_found = 0;
3397 inst_env->disable_interrupt = 0;
3400 /* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3401 instructions. The MOVE instruction is the move from source to register. */
3403 static void
3404 handle_prefix_index_mode_for_aritm_op (unsigned short inst,
3405 inst_env_type *inst_env)
3407 if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
3409 /* If the instruction is MOVE it's invalid. If the instruction is ADD,
3410 SUB, AND or OR something weird is going on (if everything works these
3411 instructions should end up in the three operand version). */
3412 inst_env->invalid = 1;
3413 return;
3415 else
3417 /* three_operand_add_sub_cmp_and_or does the same as we should do here
3418 so use it. */
3419 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3421 inst_env->slot_needed = 0;
3422 inst_env->prefix_found = 0;
3423 inst_env->xflag_found = 0;
3424 inst_env->disable_interrupt = 0;
3427 /* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
3428 CMP, AND OR and MOVE instruction. The MOVE instruction is the move from
3429 source to register. */
3431 static void
3432 handle_inc_and_index_mode_for_aritm_op (unsigned short inst,
3433 inst_env_type *inst_env)
3435 unsigned long operand1;
3436 unsigned long operand2;
3437 unsigned long operand3;
3438 int size;
3440 /* The instruction is either an indirect or autoincrement addressing mode.
3441 Check if the destination register is the PC. */
3442 if (cris_get_operand2 (inst) == REG_PC)
3444 /* Must be done here, get_data_from_address may change the size
3445 field. */
3446 size = cris_get_size (inst);
3447 operand2 = inst_env->reg[REG_PC];
3449 /* Get the value of the third operand, i.e. the indirect operand. */
3450 operand1 = inst_env->reg[cris_get_operand1 (inst)];
3451 operand3 = get_data_from_address (&inst, operand1, inst_env->byte_order);
3453 /* Calculate the PC value after the instruction, i.e. where the
3454 breakpoint should be. The order of the udw_operands is vital. */
3455 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3457 /* If this is an autoincrement addressing mode, check if the increment
3458 changes the PC. */
3459 if ((cris_get_operand1 (inst) == REG_PC)
3460 && (cris_get_mode (inst) == AUTOINC_MODE))
3462 /* Get the size field. */
3463 size = cris_get_size (inst);
3465 /* If it's an extend instruction we don't want the signed extend bit,
3466 because it influences the size. */
3467 if (cris_get_opcode (inst) < 4)
3469 size &= ~SIGNED_EXTEND_BIT_MASK;
3471 process_autoincrement (size, inst, inst_env);
3473 inst_env->slot_needed = 0;
3474 inst_env->prefix_found = 0;
3475 inst_env->xflag_found = 0;
3476 inst_env->disable_interrupt = 0;
3479 /* Handles the two-operand addressing mode, all modes except register, for
3480 the ADD, SUB CMP, AND and OR instruction. */
3482 static void
3483 none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3484 inst_env_type *inst_env)
3486 if (inst_env->prefix_found)
3488 if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
3490 handle_prefix_index_mode_for_aritm_op (inst, inst_env);
3492 else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
3494 handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
3496 else
3498 /* The mode is invalid for a prefixed base instruction. */
3499 inst_env->invalid = 1;
3500 return;
3503 else
3505 handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
3509 /* Handles the quick addressing mode for the ADD and SUB instruction. */
3511 static void
3512 quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
3514 unsigned long operand1;
3515 unsigned long operand2;
3517 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3518 instruction and can't have a prefix. */
3519 if (inst_env->prefix_found)
3521 inst_env->invalid = 1;
3522 return;
3525 /* Check if the instruction has PC as its target. */
3526 if (cris_get_operand2 (inst) == REG_PC)
3528 if (inst_env->slot_needed)
3530 inst_env->invalid = 1;
3531 return;
3533 operand1 = cris_get_quick_value (inst);
3534 operand2 = inst_env->reg[REG_PC];
3536 /* The size should now be dword. */
3537 cris_set_size_to_dword (&inst);
3539 /* Calculate the PC value after the instruction, i.e. where the
3540 breakpoint should be. */
3541 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3543 inst_env->slot_needed = 0;
3544 inst_env->prefix_found = 0;
3545 inst_env->xflag_found = 0;
3546 inst_env->disable_interrupt = 0;
3549 /* Handles the quick addressing mode for the CMP, AND and OR instruction. */
3551 static void
3552 quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
3554 unsigned long operand1;
3555 unsigned long operand2;
3557 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3558 instruction and can't have a prefix. */
3559 if (inst_env->prefix_found)
3561 inst_env->invalid = 1;
3562 return;
3564 /* Check if the instruction has PC as its target. */
3565 if (cris_get_operand2 (inst) == REG_PC)
3567 if (inst_env->slot_needed)
3569 inst_env->invalid = 1;
3570 return;
3572 /* The instruction has the PC as its target register. */
3573 operand1 = cris_get_quick_value (inst);
3574 operand2 = inst_env->reg[REG_PC];
3576 /* The quick value is signed, so check if we must do a signed extend. */
3577 if (operand1 & SIGNED_QUICK_VALUE_MASK)
3579 /* sign extend */
3580 operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
3582 /* The size should now be dword. */
3583 cris_set_size_to_dword (&inst);
3585 /* Calculate the PC value after the instruction, i.e. where the
3586 breakpoint should be. */
3587 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3589 inst_env->slot_needed = 0;
3590 inst_env->prefix_found = 0;
3591 inst_env->xflag_found = 0;
3592 inst_env->disable_interrupt = 0;
3595 /* Translate op_type to a function and call it. */
3597 static void
3598 cris_gdb_func (struct gdbarch *gdbarch, enum cris_op_type op_type,
3599 unsigned short inst, inst_env_type *inst_env)
3601 switch (op_type)
3603 case cris_not_implemented_op:
3604 not_implemented_op (inst, inst_env);
3605 break;
3607 case cris_abs_op:
3608 abs_op (inst, inst_env);
3609 break;
3611 case cris_addi_op:
3612 addi_op (inst, inst_env);
3613 break;
3615 case cris_asr_op:
3616 asr_op (inst, inst_env);
3617 break;
3619 case cris_asrq_op:
3620 asrq_op (inst, inst_env);
3621 break;
3623 case cris_ax_ei_setf_op:
3624 ax_ei_setf_op (inst, inst_env);
3625 break;
3627 case cris_bdap_prefix:
3628 bdap_prefix (inst, inst_env);
3629 break;
3631 case cris_biap_prefix:
3632 biap_prefix (inst, inst_env);
3633 break;
3635 case cris_break_op:
3636 break_op (inst, inst_env);
3637 break;
3639 case cris_btst_nop_op:
3640 btst_nop_op (inst, inst_env);
3641 break;
3643 case cris_clearf_di_op:
3644 clearf_di_op (inst, inst_env);
3645 break;
3647 case cris_dip_prefix:
3648 dip_prefix (inst, inst_env);
3649 break;
3651 case cris_dstep_logshift_mstep_neg_not_op:
3652 dstep_logshift_mstep_neg_not_op (inst, inst_env);
3653 break;
3655 case cris_eight_bit_offset_branch_op:
3656 eight_bit_offset_branch_op (inst, inst_env);
3657 break;
3659 case cris_move_mem_to_reg_movem_op:
3660 move_mem_to_reg_movem_op (inst, inst_env);
3661 break;
3663 case cris_move_reg_to_mem_movem_op:
3664 move_reg_to_mem_movem_op (inst, inst_env);
3665 break;
3667 case cris_move_to_preg_op:
3668 move_to_preg_op (gdbarch, inst, inst_env);
3669 break;
3671 case cris_muls_op:
3672 muls_op (inst, inst_env);
3673 break;
3675 case cris_mulu_op:
3676 mulu_op (inst, inst_env);
3677 break;
3679 case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
3680 none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3681 break;
3683 case cris_none_reg_mode_clear_test_op:
3684 none_reg_mode_clear_test_op (inst, inst_env);
3685 break;
3687 case cris_none_reg_mode_jump_op:
3688 none_reg_mode_jump_op (inst, inst_env);
3689 break;
3691 case cris_none_reg_mode_move_from_preg_op:
3692 none_reg_mode_move_from_preg_op (gdbarch, inst, inst_env);
3693 break;
3695 case cris_quick_mode_add_sub_op:
3696 quick_mode_add_sub_op (inst, inst_env);
3697 break;
3699 case cris_quick_mode_and_cmp_move_or_op:
3700 quick_mode_and_cmp_move_or_op (inst, inst_env);
3701 break;
3703 case cris_quick_mode_bdap_prefix:
3704 quick_mode_bdap_prefix (inst, inst_env);
3705 break;
3707 case cris_reg_mode_add_sub_cmp_and_or_move_op:
3708 reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3709 break;
3711 case cris_reg_mode_clear_op:
3712 reg_mode_clear_op (inst, inst_env);
3713 break;
3715 case cris_reg_mode_jump_op:
3716 reg_mode_jump_op (inst, inst_env);
3717 break;
3719 case cris_reg_mode_move_from_preg_op:
3720 reg_mode_move_from_preg_op (inst, inst_env);
3721 break;
3723 case cris_reg_mode_test_op:
3724 reg_mode_test_op (inst, inst_env);
3725 break;
3727 case cris_scc_op:
3728 scc_op (inst, inst_env);
3729 break;
3731 case cris_sixteen_bit_offset_branch_op:
3732 sixteen_bit_offset_branch_op (inst, inst_env);
3733 break;
3735 case cris_three_operand_add_sub_cmp_and_or_op:
3736 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3737 break;
3739 case cris_three_operand_bound_op:
3740 three_operand_bound_op (inst, inst_env);
3741 break;
3743 case cris_two_operand_bound_op:
3744 two_operand_bound_op (inst, inst_env);
3745 break;
3747 case cris_xor_op:
3748 xor_op (inst, inst_env);
3749 break;
3753 /* Originally from <asm/elf.h>. */
3754 typedef unsigned char cris_elf_greg_t[4];
3756 /* Same as user_regs_struct struct in <asm/user.h>. */
3757 #define CRISV10_ELF_NGREG 35
3758 typedef cris_elf_greg_t cris_elf_gregset_t[CRISV10_ELF_NGREG];
3760 #define CRISV32_ELF_NGREG 32
3761 typedef cris_elf_greg_t crisv32_elf_gregset_t[CRISV32_ELF_NGREG];
3763 /* Unpack a cris_elf_gregset_t into GDB's register cache. */
3765 static void
3766 cris_supply_gregset (const struct regset *regset, struct regcache *regcache,
3767 int regnum, const void *gregs, size_t len)
3769 struct gdbarch *gdbarch = regcache->arch ();
3770 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
3771 int i;
3772 const cris_elf_greg_t *regp = static_cast<const cris_elf_greg_t *>(gregs);
3774 if (len != sizeof (cris_elf_gregset_t)
3775 && len != sizeof (crisv32_elf_gregset_t))
3776 warning (_("wrong size gregset struct in core file"));
3777 gdb_assert (len >= sizeof (crisv32_elf_gregset_t));
3779 /* The kernel dumps all 32 registers as unsigned longs, but supply_register
3780 knows about the actual size of each register so that's no problem. */
3781 for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
3783 if (regnum == -1 || regnum == i)
3784 regcache->raw_supply (i, (char *)&regp[i]);
3787 if (tdep->cris_version == 32 && (regnum == -1 || regnum == ERP_REGNUM))
3789 /* Needed to set pseudo-register PC for CRISv32. */
3790 /* FIXME: If ERP is in a delay slot at this point then the PC will
3791 be wrong. Issue a warning to alert the user. */
3792 regcache->raw_supply (gdbarch_pc_regnum (gdbarch),
3793 (char *)&regp[ERP_REGNUM]);
3795 if (*(char *)&regp[ERP_REGNUM] & 0x1)
3796 gdb_printf (gdb_stderr, "Warning: PC in delay slot\n");
3800 static const struct regset cris_regset = {
3801 nullptr,
3802 cris_supply_gregset,
3803 /* We don't need a collect function because we only use this for core files
3804 (via iterate_over_regset_sections). */
3805 nullptr,
3806 REGSET_VARIABLE_SIZE
3809 static void cris_iterate_over_regset_sections (struct gdbarch *gdbarch,
3810 iterate_over_regset_sections_cb *cb,
3811 void *cb_data,
3812 const struct regcache *regcache)
3814 cb (".reg", sizeof (crisv32_elf_gregset_t), sizeof (crisv32_elf_gregset_t),
3815 &cris_regset, NULL, cb_data);
3818 void _initialize_cris_tdep ();
3819 void
3820 _initialize_cris_tdep ()
3822 gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);
3824 /* CRIS-specific user-commands. */
3825 add_setshow_zuinteger_cmd ("cris-version", class_support,
3826 &usr_cmd_cris_version,
3827 _("Set the current CRIS version."),
3828 _("Show the current CRIS version."),
3829 _("\
3830 Set to 10 for CRISv10 or 32 for CRISv32 if autodetection fails.\n\
3831 Defaults to 10. "),
3832 set_cris_version,
3833 NULL, /* FIXME: i18n: Current CRIS version
3834 is %s. */
3835 &setlist, &showlist);
3837 add_setshow_enum_cmd ("cris-mode", class_support,
3838 cris_modes, &usr_cmd_cris_mode,
3839 _("Set the current CRIS mode."),
3840 _("Show the current CRIS mode."),
3841 _("\
3842 Set to CRIS_MODE_GURU when debugging in guru mode.\n\
3843 Makes GDB use the NRP register instead of the ERP register in certain cases."),
3844 set_cris_mode,
3845 NULL, /* FIXME: i18n: Current CRIS version is %s. */
3846 &setlist, &showlist);
3848 add_setshow_boolean_cmd ("cris-dwarf2-cfi", class_support,
3849 &usr_cmd_cris_dwarf2_cfi,
3850 _("Set the usage of Dwarf-2 CFI for CRIS."),
3851 _("Show the usage of Dwarf-2 CFI for CRIS."),
3852 _("Set this to \"off\" if using gcc-cris < R59."),
3853 set_cris_dwarf2_cfi,
3854 NULL, /* FIXME: i18n: Usage of Dwarf-2 CFI
3855 for CRIS is %d. */
3856 &setlist, &showlist);
3859 /* Prints out all target specific values. */
3861 static void
3862 cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3864 cris_gdbarch_tdep *tdep = gdbarch_tdep<cris_gdbarch_tdep> (gdbarch);
3865 if (tdep != NULL)
3867 gdb_printf (file, "cris_dump_tdep: tdep->cris_version = %i\n",
3868 tdep->cris_version);
3869 gdb_printf (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
3870 tdep->cris_mode);
3871 gdb_printf (file, "cris_dump_tdep: tdep->cris_dwarf2_cfi = %i\n",
3872 tdep->cris_dwarf2_cfi);
3876 static void
3877 set_cris_version (const char *ignore_args, int from_tty,
3878 struct cmd_list_element *c)
3880 struct gdbarch_info info;
3882 usr_cmd_cris_version_valid = 1;
3884 /* Update the current architecture, if needed. */
3885 if (!gdbarch_update_p (info))
3886 internal_error (__FILE__, __LINE__,
3887 _("cris_gdbarch_update: failed to update architecture."));
3890 static void
3891 set_cris_mode (const char *ignore_args, int from_tty,
3892 struct cmd_list_element *c)
3894 struct gdbarch_info info;
3896 /* Update the current architecture, if needed. */
3897 if (!gdbarch_update_p (info))
3898 internal_error (__FILE__, __LINE__,
3899 "cris_gdbarch_update: failed to update architecture.");
3902 static void
3903 set_cris_dwarf2_cfi (const char *ignore_args, int from_tty,
3904 struct cmd_list_element *c)
3906 struct gdbarch_info info;
3908 /* Update the current architecture, if needed. */
3909 if (!gdbarch_update_p (info))
3910 internal_error (__FILE__, __LINE__,
3911 _("cris_gdbarch_update: failed to update architecture."));
3914 static struct gdbarch *
3915 cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3917 struct gdbarch *gdbarch;
3918 unsigned int cris_version;
3920 if (usr_cmd_cris_version_valid)
3922 /* Trust the user's CRIS version setting. */
3923 cris_version = usr_cmd_cris_version;
3925 else if (info.abfd && bfd_get_mach (info.abfd) == bfd_mach_cris_v32)
3927 cris_version = 32;
3929 else
3931 /* Assume it's CRIS version 10. */
3932 cris_version = 10;
3935 /* Make the current settings visible to the user. */
3936 usr_cmd_cris_version = cris_version;
3938 /* Find a candidate among the list of pre-declared architectures. */
3939 for (arches = gdbarch_list_lookup_by_info (arches, &info);
3940 arches != NULL;
3941 arches = gdbarch_list_lookup_by_info (arches->next, &info))
3943 cris_gdbarch_tdep *tdep
3944 = gdbarch_tdep<cris_gdbarch_tdep> (arches->gdbarch);
3946 if (tdep->cris_version == usr_cmd_cris_version
3947 && tdep->cris_mode == usr_cmd_cris_mode
3948 && tdep->cris_dwarf2_cfi == usr_cmd_cris_dwarf2_cfi)
3949 return arches->gdbarch;
3952 /* No matching architecture was found. Create a new one. */
3953 cris_gdbarch_tdep *tdep = new cris_gdbarch_tdep;
3954 info.byte_order = BFD_ENDIAN_LITTLE;
3955 gdbarch = gdbarch_alloc (&info, tdep);
3957 tdep->cris_version = usr_cmd_cris_version;
3958 tdep->cris_mode = usr_cmd_cris_mode;
3959 tdep->cris_dwarf2_cfi = usr_cmd_cris_dwarf2_cfi;
3961 set_gdbarch_return_value (gdbarch, cris_return_value);
3962 set_gdbarch_sp_regnum (gdbarch, 14);
3964 /* Length of ordinary registers used in push_word and a few other
3965 places. register_size() is the real way to know how big a
3966 register is. */
3968 set_gdbarch_double_bit (gdbarch, 64);
3969 /* The default definition of a long double is 2 * gdbarch_double_bit,
3970 which means we have to set this explicitly. */
3971 set_gdbarch_long_double_bit (gdbarch, 64);
3973 /* The total amount of space needed to store (in an array called registers)
3974 GDB's copy of the machine's register state. Note: We can not use
3975 cris_register_size at this point, since it relies on gdbarch
3976 being set. */
3977 switch (tdep->cris_version)
3979 case 0:
3980 case 1:
3981 case 2:
3982 case 3:
3983 case 8:
3984 case 9:
3985 /* Old versions; not supported. */
3986 return 0;
3988 case 10:
3989 case 11:
3990 /* CRIS v10 and v11, a.k.a. ETRAX 100LX. In addition to ETRAX 100,
3991 P7 (32 bits), and P15 (32 bits) have been implemented. */
3992 set_gdbarch_pc_regnum (gdbarch, 15);
3993 set_gdbarch_register_type (gdbarch, cris_register_type);
3994 /* There are 32 registers (some of which may not be implemented). */
3995 set_gdbarch_num_regs (gdbarch, 32);
3996 set_gdbarch_register_name (gdbarch, cris_register_name);
3997 set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
3998 set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);
4000 set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
4001 break;
4003 case 32:
4004 /* CRIS v32. General registers R0 - R15 (32 bits), special registers
4005 P0 - P15 (32 bits) except P0, P1, P3 (8 bits) and P4 (16 bits)
4006 and pseudo-register PC (32 bits). */
4007 set_gdbarch_pc_regnum (gdbarch, 32);
4008 set_gdbarch_register_type (gdbarch, crisv32_register_type);
4009 /* 32 registers + pseudo-register PC + 16 support registers. */
4010 set_gdbarch_num_regs (gdbarch, 32 + 1 + 16);
4011 set_gdbarch_register_name (gdbarch, crisv32_register_name);
4013 set_gdbarch_cannot_store_register
4014 (gdbarch, crisv32_cannot_store_register);
4015 set_gdbarch_cannot_fetch_register
4016 (gdbarch, crisv32_cannot_fetch_register);
4018 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
4020 set_gdbarch_single_step_through_delay
4021 (gdbarch, crisv32_single_step_through_delay);
4023 break;
4025 default:
4026 /* Unknown version. */
4027 return 0;
4030 /* Dummy frame functions (shared between CRISv10 and CRISv32 since they
4031 have the same ABI). */
4032 set_gdbarch_push_dummy_code (gdbarch, cris_push_dummy_code);
4033 set_gdbarch_push_dummy_call (gdbarch, cris_push_dummy_call);
4034 set_gdbarch_frame_align (gdbarch, cris_frame_align);
4035 set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);
4037 /* The stack grows downward. */
4038 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4040 set_gdbarch_breakpoint_kind_from_pc (gdbarch, cris_breakpoint_kind_from_pc);
4041 set_gdbarch_sw_breakpoint_from_kind (gdbarch, cris_sw_breakpoint_from_kind);
4042 set_gdbarch_iterate_over_regset_sections (gdbarch, cris_iterate_over_regset_sections);
4044 if (tdep->cris_dwarf2_cfi == 1)
4046 /* Hook in the Dwarf-2 frame sniffer. */
4047 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, cris_dwarf2_reg_to_regnum);
4048 dwarf2_frame_set_init_reg (gdbarch, cris_dwarf2_frame_init_reg);
4049 dwarf2_append_unwinders (gdbarch);
4052 if (tdep->cris_mode != cris_mode_guru)
4054 frame_unwind_append_unwinder (gdbarch, &cris_sigtramp_frame_unwind);
4057 frame_unwind_append_unwinder (gdbarch, &cris_frame_unwind);
4058 frame_base_set_default (gdbarch, &cris_frame_base);
4060 /* Hook in ABI-specific overrides, if they have been registered. */
4061 gdbarch_init_osabi (info, gdbarch);
4063 return gdbarch;