]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cris-tdep.c
e1c141865cc60b5f97d179b10889b70251485d1d
[thirdparty/binutils-gdb.git] / gdb / cris-tdep.c
1 /* Target dependent code for CRIS, for GDB, the GNU debugger.
2
3 Copyright (C) 2001-2021 Free Software Foundation, Inc.
4
5 Contributed by Axis Communications AB.
6 Written by Hendrik Ruijter, Stefan Andersson, and Orjan Friberg.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
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"
41
42 #include "objfiles.h"
43
44 #include "solib.h" /* Support for shared libraries. */
45 #include "solib-svr4.h"
46 #include "dis-asm.h"
47
48 #include "cris-tdep.h"
49
50 enum cris_num_regs
51 {
52 /* There are no floating point registers. Used in gdbserver low-linux.c. */
53 NUM_FREGS = 0,
54
55 /* There are 16 general registers. */
56 NUM_GENREGS = 16,
57
58 /* There are 16 special registers. */
59 NUM_SPECREGS = 16,
60
61 /* CRISv32 has a pseudo PC register, not noted here. */
62
63 /* CRISv32 has 16 support registers. */
64 NUM_SUPPREGS = 16
65 };
66
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. */
79
80 enum cris_regnums
81 {
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,
92
93 /* Registers which happen to be common. */
94 VR_REGNUM = 17,
95 MOF_REGNUM = 23,
96 SRP_REGNUM = 27,
97
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,
109
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. */
126
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,
143 };
144
145 extern const struct cris_spec_reg cris_spec_regs[];
146
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;
150
151 /* Indicates whether to trust the above variable. */
152 static bool usr_cmd_cris_version_valid = false;
153
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,
159 0
160 };
161
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;
165
166 /* Whether to make use of Dwarf-2 CFI (default on). */
167 static bool usr_cmd_cris_dwarf2_cfi = true;
168
169 /* Sigtramp identification code copied from i386-linux-tdep.c. */
170
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
175
176 static const unsigned short sigtramp_code[] =
177 {
178 SIGTRAMP_INSN0, 0x0077, /* movu.w $0x77, $r9 */
179 SIGTRAMP_INSN1 /* break 13 */
180 };
181
182 #define SIGTRAMP_LEN (sizeof sigtramp_code)
183
184 /* Note: same length as normal sigtramp code. */
185
186 static const unsigned short rt_sigtramp_code[] =
187 {
188 SIGTRAMP_INSN0, 0x00ad, /* movu.w $0xad, $r9 */
189 SIGTRAMP_INSN1 /* break 13 */
190 };
191
192 /* If PC is in a sigtramp routine, return the address of the start of
193 the routine. Otherwise, return 0. */
194
195 static CORE_ADDR
196 cris_sigtramp_start (struct frame_info *this_frame)
197 {
198 CORE_ADDR pc = get_frame_pc (this_frame);
199 gdb_byte buf[SIGTRAMP_LEN];
200
201 if (!safe_frame_unwind_memory (this_frame, pc, buf))
202 return 0;
203
204 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
205 {
206 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
207 return 0;
208
209 pc -= SIGTRAMP_OFFSET1;
210 if (!safe_frame_unwind_memory (this_frame, pc, buf))
211 return 0;
212 }
213
214 if (memcmp (buf, sigtramp_code, SIGTRAMP_LEN) != 0)
215 return 0;
216
217 return pc;
218 }
219
220 /* If PC is in a RT sigtramp routine, return the address of the start of
221 the routine. Otherwise, return 0. */
222
223 static CORE_ADDR
224 cris_rt_sigtramp_start (struct frame_info *this_frame)
225 {
226 CORE_ADDR pc = get_frame_pc (this_frame);
227 gdb_byte buf[SIGTRAMP_LEN];
228
229 if (!safe_frame_unwind_memory (this_frame, pc, buf))
230 return 0;
231
232 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
233 {
234 if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
235 return 0;
236
237 pc -= SIGTRAMP_OFFSET1;
238 if (!safe_frame_unwind_memory (this_frame, pc, buf))
239 return 0;
240 }
241
242 if (memcmp (buf, rt_sigtramp_code, SIGTRAMP_LEN) != 0)
243 return 0;
244
245 return pc;
246 }
247
248 /* Assuming THIS_FRAME is a frame for a GNU/Linux sigtramp routine,
249 return the address of the associated sigcontext structure. */
250
251 static CORE_ADDR
252 cris_sigcontext_addr (struct frame_info *this_frame)
253 {
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];
259
260 get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
261 sp = extract_unsigned_integer (buf, 4, byte_order);
262
263 /* Look for normal sigtramp frame first. */
264 pc = cris_sigtramp_start (this_frame);
265 if (pc)
266 {
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;
271 }
272
273 pc = cris_rt_sigtramp_start (this_frame);
274 if (pc)
275 {
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);
282 }
283
284 error (_("Couldn't recognize signal trampoline."));
285 return 0;
286 }
287
288 struct cris_unwind_cache
289 {
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;
302
303 /* From old frame_extra_info struct. */
304 CORE_ADDR return_pc;
305 int leaf_function;
306
307 /* Table indicating the location of each and every register. */
308 trad_frame_saved_reg *saved_regs;
309 };
310
311 static struct cris_unwind_cache *
312 cris_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
313 void **this_cache)
314 {
315 struct gdbarch *gdbarch = get_frame_arch (this_frame);
316 struct gdbarch_tdep *tdep = 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;
322
323 if ((*this_cache))
324 return (struct cris_unwind_cache *) (*this_cache);
325
326 info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
327 (*this_cache) = info;
328 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
329
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;
339
340 get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
341 info->base = extract_unsigned_integer (buf, 4, byte_order);
342
343 addr = cris_sigcontext_addr (this_frame);
344
345 /* Layout of the sigcontext struct:
346 struct sigcontext {
347 struct pt_regs regs;
348 unsigned long oldmask;
349 unsigned long usp;
350 }; */
351
352 if (tdep->cris_version == 10)
353 {
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));
358
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));
369 }
370 else
371 {
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));
377
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));
387
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.
392
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];
397
398 info->saved_regs[gdbarch_sp_regnum (gdbarch)].set_addr (addr + (25 * 4));
399 }
400
401 return info;
402 }
403
404 static void
405 cris_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
406 struct frame_id *this_id)
407 {
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));
411 }
412
413 /* Forward declaration. */
414
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)
420 {
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);
424 }
425
426 static int
427 cris_sigtramp_frame_sniffer (const struct frame_unwind *self,
428 struct frame_info *this_frame,
429 void **this_cache)
430 {
431 if (cris_sigtramp_start (this_frame)
432 || cris_rt_sigtramp_start (this_frame))
433 return 1;
434
435 return 0;
436 }
437
438 static const struct frame_unwind cris_sigtramp_frame_unwind =
439 {
440 SIGTRAMP_FRAME,
441 default_frame_unwind_stop_reason,
442 cris_sigtramp_frame_this_id,
443 cris_sigtramp_frame_prev_register,
444 NULL,
445 cris_sigtramp_frame_sniffer
446 };
447
448 static int
449 crisv32_single_step_through_delay (struct gdbarch *gdbarch,
450 struct frame_info *this_frame)
451 {
452 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
453 ULONGEST erp;
454 int ret = 0;
455
456 if (tdep->cris_mode == cris_mode_guru)
457 erp = get_frame_register_unsigned (this_frame, NRP_REGNUM);
458 else
459 erp = get_frame_register_unsigned (this_frame, ERP_REGNUM);
460
461 if (erp & 0x1)
462 {
463 /* In delay slot - check if there's a breakpoint at the preceding
464 instruction. */
465 if (breakpoint_here_p (get_frame_address_space (this_frame), erp & ~0x1))
466 ret = 1;
467 }
468 return ret;
469 }
470
471 /* The instruction environment needed to find single-step breakpoints. */
472
473 typedef
474 struct instruction_environment
475 {
476 unsigned long reg[NUM_GENREGS];
477 unsigned long preg[NUM_SPECREGS];
478 unsigned long branch_break_address;
479 unsigned long delay_slot_pc;
480 unsigned long prefix_value;
481 int branch_found;
482 int prefix_found;
483 int invalid;
484 int slot_needed;
485 int delay_slot_pc_active;
486 int xflag_found;
487 int disable_interrupt;
488 enum bfd_endian byte_order;
489 } inst_env_type;
490
491 /* Machine-dependencies in CRIS for opcodes. */
492
493 /* Instruction sizes. */
494 enum cris_instruction_sizes
495 {
496 INST_BYTE_SIZE = 0,
497 INST_WORD_SIZE = 1,
498 INST_DWORD_SIZE = 2
499 };
500
501 /* Addressing modes. */
502 enum cris_addressing_modes
503 {
504 REGISTER_MODE = 1,
505 INDIRECT_MODE = 2,
506 AUTOINC_MODE = 3
507 };
508
509 /* Prefix addressing modes. */
510 enum cris_prefix_addressing_modes
511 {
512 PREFIX_INDEX_MODE = 2,
513 PREFIX_ASSIGN_MODE = 3,
514
515 /* Handle immediate byte offset addressing mode prefix format. */
516 PREFIX_OFFSET_MODE = 2
517 };
518
519 /* Masks for opcodes. */
520 enum cris_opcode_masks
521 {
522 BRANCH_SIGNED_SHORT_OFFSET_MASK = 0x1,
523 SIGNED_EXTEND_BIT_MASK = 0x2,
524 SIGNED_BYTE_MASK = 0x80,
525 SIGNED_BYTE_EXTEND_MASK = 0xFFFFFF00,
526 SIGNED_WORD_MASK = 0x8000,
527 SIGNED_WORD_EXTEND_MASK = 0xFFFF0000,
528 SIGNED_DWORD_MASK = 0x80000000,
529 SIGNED_QUICK_VALUE_MASK = 0x20,
530 SIGNED_QUICK_VALUE_EXTEND_MASK = 0xFFFFFFC0
531 };
532
533 /* Functions for opcodes. The general form of the ETRAX 16-bit instruction:
534 Bit 15 - 12 Operand2
535 11 - 10 Mode
536 9 - 6 Opcode
537 5 - 4 Size
538 3 - 0 Operand1 */
539
540 static int
541 cris_get_operand2 (unsigned short insn)
542 {
543 return ((insn & 0xF000) >> 12);
544 }
545
546 static int
547 cris_get_mode (unsigned short insn)
548 {
549 return ((insn & 0x0C00) >> 10);
550 }
551
552 static int
553 cris_get_opcode (unsigned short insn)
554 {
555 return ((insn & 0x03C0) >> 6);
556 }
557
558 static int
559 cris_get_size (unsigned short insn)
560 {
561 return ((insn & 0x0030) >> 4);
562 }
563
564 static int
565 cris_get_operand1 (unsigned short insn)
566 {
567 return (insn & 0x000F);
568 }
569
570 /* Additional functions in order to handle opcodes. */
571
572 static int
573 cris_get_quick_value (unsigned short insn)
574 {
575 return (insn & 0x003F);
576 }
577
578 static int
579 cris_get_bdap_quick_offset (unsigned short insn)
580 {
581 return (insn & 0x00FF);
582 }
583
584 static int
585 cris_get_branch_short_offset (unsigned short insn)
586 {
587 return (insn & 0x00FF);
588 }
589
590 static int
591 cris_get_asr_shift_steps (unsigned long value)
592 {
593 return (value & 0x3F);
594 }
595
596 static int
597 cris_get_clear_size (unsigned short insn)
598 {
599 return ((insn) & 0xC000);
600 }
601
602 static int
603 cris_is_signed_extend_bit_on (unsigned short insn)
604 {
605 return (((insn) & 0x20) == 0x20);
606 }
607
608 static int
609 cris_is_xflag_bit_on (unsigned short insn)
610 {
611 return (((insn) & 0x1000) == 0x1000);
612 }
613
614 static void
615 cris_set_size_to_dword (unsigned short *insn)
616 {
617 *insn &= 0xFFCF;
618 *insn |= 0x20;
619 }
620
621 static signed char
622 cris_get_signed_offset (unsigned short insn)
623 {
624 return ((signed char) (insn & 0x00FF));
625 }
626
627 /* Calls an op function given the op-type, working on the insn and the
628 inst_env. */
629 static void cris_gdb_func (struct gdbarch *, enum cris_op_type, unsigned short,
630 inst_env_type *);
631
632 static struct gdbarch *cris_gdbarch_init (struct gdbarch_info,
633 struct gdbarch_list *);
634
635 static void cris_dump_tdep (struct gdbarch *, struct ui_file *);
636
637 static void set_cris_version (const char *ignore_args, int from_tty,
638 struct cmd_list_element *c);
639
640 static void set_cris_mode (const char *ignore_args, int from_tty,
641 struct cmd_list_element *c);
642
643 static void set_cris_dwarf2_cfi (const char *ignore_args, int from_tty,
644 struct cmd_list_element *c);
645
646 static CORE_ADDR cris_scan_prologue (CORE_ADDR pc,
647 struct frame_info *this_frame,
648 struct cris_unwind_cache *info);
649
650 static CORE_ADDR crisv32_scan_prologue (CORE_ADDR pc,
651 struct frame_info *this_frame,
652 struct cris_unwind_cache *info);
653
654 /* When arguments must be pushed onto the stack, they go on in reverse
655 order. The below implements a FILO (stack) to do this.
656 Copied from d10v-tdep.c. */
657
658 struct stack_item
659 {
660 int len;
661 struct stack_item *prev;
662 gdb_byte *data;
663 };
664
665 static struct stack_item *
666 push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
667 {
668 struct stack_item *si = XNEW (struct stack_item);
669 si->data = (gdb_byte *) xmalloc (len);
670 si->len = len;
671 si->prev = prev;
672 memcpy (si->data, contents, len);
673 return si;
674 }
675
676 static struct stack_item *
677 pop_stack_item (struct stack_item *si)
678 {
679 struct stack_item *dead = si;
680 si = si->prev;
681 xfree (dead->data);
682 xfree (dead);
683 return si;
684 }
685
686 /* Put here the code to store, into fi->saved_regs, the addresses of
687 the saved registers of frame described by FRAME_INFO. This
688 includes special registers such as pc and fp saved in special ways
689 in the stack frame. sp is even more special: the address we return
690 for it IS the sp for the next frame. */
691
692 static struct cris_unwind_cache *
693 cris_frame_unwind_cache (struct frame_info *this_frame,
694 void **this_prologue_cache)
695 {
696 struct gdbarch *gdbarch = get_frame_arch (this_frame);
697 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
698 struct cris_unwind_cache *info;
699
700 if ((*this_prologue_cache))
701 return (struct cris_unwind_cache *) (*this_prologue_cache);
702
703 info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
704 (*this_prologue_cache) = info;
705 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
706
707 /* Zero all fields. */
708 info->prev_sp = 0;
709 info->base = 0;
710 info->size = 0;
711 info->sp_offset = 0;
712 info->r8_offset = 0;
713 info->uses_frame = 0;
714 info->return_pc = 0;
715 info->leaf_function = 0;
716
717 /* Prologue analysis does the rest... */
718 if (tdep->cris_version == 32)
719 crisv32_scan_prologue (get_frame_func (this_frame), this_frame, info);
720 else
721 cris_scan_prologue (get_frame_func (this_frame), this_frame, info);
722
723 return info;
724 }
725
726 /* Given a GDB frame, determine the address of the calling function's
727 frame. This will be used to create a new GDB frame struct. */
728
729 static void
730 cris_frame_this_id (struct frame_info *this_frame,
731 void **this_prologue_cache,
732 struct frame_id *this_id)
733 {
734 struct cris_unwind_cache *info
735 = cris_frame_unwind_cache (this_frame, this_prologue_cache);
736 CORE_ADDR base;
737 CORE_ADDR func;
738 struct frame_id id;
739
740 /* The FUNC is easy. */
741 func = get_frame_func (this_frame);
742
743 /* Hopefully the prologue analysis either correctly determined the
744 frame's base (which is the SP from the previous frame), or set
745 that base to "NULL". */
746 base = info->prev_sp;
747 if (base == 0)
748 return;
749
750 id = frame_id_build (base, func);
751
752 (*this_id) = id;
753 }
754
755 static struct value *
756 cris_frame_prev_register (struct frame_info *this_frame,
757 void **this_prologue_cache, int regnum)
758 {
759 struct cris_unwind_cache *info
760 = cris_frame_unwind_cache (this_frame, this_prologue_cache);
761 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
762 }
763
764 static CORE_ADDR
765 cris_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
766 {
767 /* Align to the size of an instruction (so that they can safely be
768 pushed onto the stack). */
769 return sp & ~3;
770 }
771
772 static CORE_ADDR
773 cris_push_dummy_code (struct gdbarch *gdbarch,
774 CORE_ADDR sp, CORE_ADDR funaddr,
775 struct value **args, int nargs,
776 struct type *value_type,
777 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
778 struct regcache *regcache)
779 {
780 /* Allocate space sufficient for a breakpoint. */
781 sp = (sp - 4) & ~3;
782 /* Store the address of that breakpoint */
783 *bp_addr = sp;
784 /* CRIS always starts the call at the callee's entry point. */
785 *real_pc = funaddr;
786 return sp;
787 }
788
789 static CORE_ADDR
790 cris_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
791 struct regcache *regcache, CORE_ADDR bp_addr,
792 int nargs, struct value **args, CORE_ADDR sp,
793 function_call_return_method return_method,
794 CORE_ADDR struct_addr)
795 {
796 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
797 int argreg;
798 int argnum;
799
800 struct stack_item *si = NULL;
801
802 /* Push the return address. */
803 regcache_cooked_write_unsigned (regcache, SRP_REGNUM, bp_addr);
804
805 /* Are we returning a value using a structure return or a normal value
806 return? struct_addr is the address of the reserved space for the return
807 structure to be written on the stack. */
808 if (return_method == return_method_struct)
809 regcache_cooked_write_unsigned (regcache, STR_REGNUM, struct_addr);
810
811 /* Now load as many as possible of the first arguments into registers,
812 and push the rest onto the stack. */
813 argreg = ARG1_REGNUM;
814
815 for (argnum = 0; argnum < nargs; argnum++)
816 {
817 int len;
818 const gdb_byte *val;
819 int reg_demand;
820 int i;
821
822 len = TYPE_LENGTH (value_type (args[argnum]));
823 val = value_contents (args[argnum]);
824
825 /* How may registers worth of storage do we need for this argument? */
826 reg_demand = (len / 4) + (len % 4 != 0 ? 1 : 0);
827
828 if (len <= (2 * 4) && (argreg + reg_demand - 1 <= ARG4_REGNUM))
829 {
830 /* Data passed by value. Fits in available register(s). */
831 for (i = 0; i < reg_demand; i++)
832 {
833 regcache->cooked_write (argreg, val);
834 argreg++;
835 val += 4;
836 }
837 }
838 else if (len <= (2 * 4) && argreg <= ARG4_REGNUM)
839 {
840 /* Data passed by value. Does not fit in available register(s).
841 Use the register(s) first, then the stack. */
842 for (i = 0; i < reg_demand; i++)
843 {
844 if (argreg <= ARG4_REGNUM)
845 {
846 regcache->cooked_write (argreg, val);
847 argreg++;
848 val += 4;
849 }
850 else
851 {
852 /* Push item for later so that pushed arguments
853 come in the right order. */
854 si = push_stack_item (si, val, 4);
855 val += 4;
856 }
857 }
858 }
859 else if (len > (2 * 4))
860 {
861 /* Data passed by reference. Push copy of data onto stack
862 and pass pointer to this copy as argument. */
863 sp = (sp - len) & ~3;
864 write_memory (sp, val, len);
865
866 if (argreg <= ARG4_REGNUM)
867 {
868 regcache_cooked_write_unsigned (regcache, argreg, sp);
869 argreg++;
870 }
871 else
872 {
873 gdb_byte buf[4];
874 store_unsigned_integer (buf, 4, byte_order, sp);
875 si = push_stack_item (si, buf, 4);
876 }
877 }
878 else
879 {
880 /* Data passed by value. No available registers. Put it on
881 the stack. */
882 si = push_stack_item (si, val, len);
883 }
884 }
885
886 while (si)
887 {
888 /* fp_arg must be word-aligned (i.e., don't += len) to match
889 the function prologue. */
890 sp = (sp - si->len) & ~3;
891 write_memory (sp, si->data, si->len);
892 si = pop_stack_item (si);
893 }
894
895 /* Finally, update the SP register. */
896 regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
897
898 return sp;
899 }
900
901 static const struct frame_unwind cris_frame_unwind =
902 {
903 NORMAL_FRAME,
904 default_frame_unwind_stop_reason,
905 cris_frame_this_id,
906 cris_frame_prev_register,
907 NULL,
908 default_frame_sniffer
909 };
910
911 static CORE_ADDR
912 cris_frame_base_address (struct frame_info *this_frame, void **this_cache)
913 {
914 struct cris_unwind_cache *info
915 = cris_frame_unwind_cache (this_frame, this_cache);
916 return info->base;
917 }
918
919 static const struct frame_base cris_frame_base =
920 {
921 &cris_frame_unwind,
922 cris_frame_base_address,
923 cris_frame_base_address,
924 cris_frame_base_address
925 };
926
927 /* Frames information. The definition of the struct frame_info is
928
929 CORE_ADDR frame
930 CORE_ADDR pc
931 enum frame_type type;
932 CORE_ADDR return_pc
933 int leaf_function
934
935 If the compilation option -fno-omit-frame-pointer is present the
936 variable frame will be set to the content of R8 which is the frame
937 pointer register.
938
939 The variable pc contains the address where execution is performed
940 in the present frame. The innermost frame contains the current content
941 of the register PC. All other frames contain the content of the
942 register PC in the next frame.
943
944 The variable `type' indicates the frame's type: normal, SIGTRAMP
945 (associated with a signal handler), dummy (associated with a dummy
946 frame).
947
948 The variable return_pc contains the address where execution should be
949 resumed when the present frame has finished, the return address.
950
951 The variable leaf_function is 1 if the return address is in the register
952 SRP, and 0 if it is on the stack.
953
954 Prologue instructions C-code.
955 The prologue may consist of (-fno-omit-frame-pointer)
956 1) 2)
957 push srp
958 push r8 push r8
959 move.d sp,r8 move.d sp,r8
960 subq X,sp subq X,sp
961 movem rY,[sp] movem rY,[sp]
962 move.S rZ,[r8-U] move.S rZ,[r8-U]
963
964 where 1 is a non-terminal function, and 2 is a leaf-function.
965
966 Note that this assumption is extremely brittle, and will break at the
967 slightest change in GCC's prologue.
968
969 If local variables are declared or register contents are saved on stack
970 the subq-instruction will be present with X as the number of bytes
971 needed for storage. The reshuffle with respect to r8 may be performed
972 with any size S (b, w, d) and any of the general registers Z={0..13}.
973 The offset U should be representable by a signed 8-bit value in all cases.
974 Thus, the prefix word is assumed to be immediate byte offset mode followed
975 by another word containing the instruction.
976
977 Degenerate cases:
978 3)
979 push r8
980 move.d sp,r8
981 move.d r8,sp
982 pop r8
983
984 Prologue instructions C++-code.
985 Case 1) and 2) in the C-code may be followed by
986
987 move.d r10,rS ; this
988 move.d r11,rT ; P1
989 move.d r12,rU ; P2
990 move.d r13,rV ; P3
991 move.S [r8+U],rZ ; P4
992
993 if any of the call parameters are stored. The host expects these
994 instructions to be executed in order to get the call parameters right. */
995
996 /* Examine the prologue of a function. The variable ip is the address of
997 the first instruction of the prologue. The variable limit is the address
998 of the first instruction after the prologue. The variable fi contains the
999 information in struct frame_info. The variable frameless_p controls whether
1000 the entire prologue is examined (0) or just enough instructions to
1001 determine that it is a prologue (1). */
1002
1003 static CORE_ADDR
1004 cris_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
1005 struct cris_unwind_cache *info)
1006 {
1007 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1008 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1009
1010 /* Present instruction. */
1011 unsigned short insn;
1012
1013 /* Next instruction, lookahead. */
1014 unsigned short insn_next;
1015 int regno;
1016
1017 /* Number of byte on stack used for local variables and movem. */
1018 int val;
1019
1020 /* Highest register number in a movem. */
1021 int regsave;
1022
1023 /* move.d r<source_register>,rS */
1024 short source_register;
1025
1026 /* Scan limit. */
1027 int limit;
1028
1029 /* This frame is with respect to a leaf until a push srp is found. */
1030 if (info)
1031 {
1032 info->leaf_function = 1;
1033 }
1034
1035 /* Assume nothing on stack. */
1036 val = 0;
1037 regsave = -1;
1038
1039 /* If we were called without a this_frame, that means we were called
1040 from cris_skip_prologue which already tried to find the end of the
1041 prologue through the symbol information. 64 instructions past current
1042 pc is arbitrarily chosen, but at least it means we'll stop eventually. */
1043 limit = this_frame ? get_frame_pc (this_frame) : pc + 64;
1044
1045 /* Find the prologue instructions. */
1046 while (pc > 0 && pc < limit)
1047 {
1048 insn = read_memory_unsigned_integer (pc, 2, byte_order);
1049 pc += 2;
1050 if (insn == 0xE1FC)
1051 {
1052 /* push <reg> 32 bit instruction. */
1053 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1054 pc += 2;
1055 regno = cris_get_operand2 (insn_next);
1056 if (info)
1057 {
1058 info->sp_offset += 4;
1059 }
1060 /* This check, meant to recognize srp, used to be regno ==
1061 (SRP_REGNUM - NUM_GENREGS), but that covers r11 also. */
1062 if (insn_next == 0xBE7E)
1063 {
1064 if (info)
1065 {
1066 info->leaf_function = 0;
1067 }
1068 }
1069 else if (insn_next == 0x8FEE)
1070 {
1071 /* push $r8 */
1072 if (info)
1073 {
1074 info->r8_offset = info->sp_offset;
1075 }
1076 }
1077 }
1078 else if (insn == 0x866E)
1079 {
1080 /* move.d sp,r8 */
1081 if (info)
1082 {
1083 info->uses_frame = 1;
1084 }
1085 continue;
1086 }
1087 else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
1088 && cris_get_mode (insn) == 0x0000
1089 && cris_get_opcode (insn) == 0x000A)
1090 {
1091 /* subq <val>,sp */
1092 if (info)
1093 {
1094 info->sp_offset += cris_get_quick_value (insn);
1095 }
1096 }
1097 else if (cris_get_mode (insn) == 0x0002
1098 && cris_get_opcode (insn) == 0x000F
1099 && cris_get_size (insn) == 0x0003
1100 && cris_get_operand1 (insn) == gdbarch_sp_regnum (gdbarch))
1101 {
1102 /* movem r<regsave>,[sp] */
1103 regsave = cris_get_operand2 (insn);
1104 }
1105 else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
1106 && ((insn & 0x0F00) >> 8) == 0x0001
1107 && (cris_get_signed_offset (insn) < 0))
1108 {
1109 /* Immediate byte offset addressing prefix word with sp as base
1110 register. Used for CRIS v8 i.e. ETRAX 100 and newer if <val>
1111 is between 64 and 128.
1112 movem r<regsave>,[sp=sp-<val>] */
1113 if (info)
1114 {
1115 info->sp_offset += -cris_get_signed_offset (insn);
1116 }
1117 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1118 pc += 2;
1119 if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
1120 && cris_get_opcode (insn_next) == 0x000F
1121 && cris_get_size (insn_next) == 0x0003
1122 && cris_get_operand1 (insn_next) == gdbarch_sp_regnum
1123 (gdbarch))
1124 {
1125 regsave = cris_get_operand2 (insn_next);
1126 }
1127 else
1128 {
1129 /* The prologue ended before the limit was reached. */
1130 pc -= 4;
1131 break;
1132 }
1133 }
1134 else if (cris_get_mode (insn) == 0x0001
1135 && cris_get_opcode (insn) == 0x0009
1136 && cris_get_size (insn) == 0x0002)
1137 {
1138 /* move.d r<10..13>,r<0..15> */
1139 source_register = cris_get_operand1 (insn);
1140
1141 /* FIXME? In the glibc solibs, the prologue might contain something
1142 like (this example taken from relocate_doit):
1143 move.d $pc,$r0
1144 sub.d 0xfffef426,$r0
1145 which isn't covered by the source_register check below. Question
1146 is whether to add a check for this combo, or make better use of
1147 the limit variable instead. */
1148 if (source_register < ARG1_REGNUM || source_register > ARG4_REGNUM)
1149 {
1150 /* The prologue ended before the limit was reached. */
1151 pc -= 2;
1152 break;
1153 }
1154 }
1155 else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
1156 /* The size is a fixed-size. */
1157 && ((insn & 0x0F00) >> 8) == 0x0001
1158 /* A negative offset. */
1159 && (cris_get_signed_offset (insn) < 0))
1160 {
1161 /* move.S rZ,[r8-U] (?) */
1162 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1163 pc += 2;
1164 regno = cris_get_operand2 (insn_next);
1165 if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1166 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
1167 && cris_get_opcode (insn_next) == 0x000F)
1168 {
1169 /* move.S rZ,[r8-U] */
1170 continue;
1171 }
1172 else
1173 {
1174 /* The prologue ended before the limit was reached. */
1175 pc -= 4;
1176 break;
1177 }
1178 }
1179 else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
1180 /* The size is a fixed-size. */
1181 && ((insn & 0x0F00) >> 8) == 0x0001
1182 /* A positive offset. */
1183 && (cris_get_signed_offset (insn) > 0))
1184 {
1185 /* move.S [r8+U],rZ (?) */
1186 insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
1187 pc += 2;
1188 regno = cris_get_operand2 (insn_next);
1189 if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1190 && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
1191 && cris_get_opcode (insn_next) == 0x0009
1192 && cris_get_operand1 (insn_next) == regno)
1193 {
1194 /* move.S [r8+U],rZ */
1195 continue;
1196 }
1197 else
1198 {
1199 /* The prologue ended before the limit was reached. */
1200 pc -= 4;
1201 break;
1202 }
1203 }
1204 else
1205 {
1206 /* The prologue ended before the limit was reached. */
1207 pc -= 2;
1208 break;
1209 }
1210 }
1211
1212 /* We only want to know the end of the prologue when this_frame and info
1213 are NULL (called from cris_skip_prologue i.e.). */
1214 if (this_frame == NULL && info == NULL)
1215 {
1216 return pc;
1217 }
1218
1219 info->size = info->sp_offset;
1220
1221 /* Compute the previous frame's stack pointer (which is also the
1222 frame's ID's stack address), and this frame's base pointer. */
1223 if (info->uses_frame)
1224 {
1225 ULONGEST this_base;
1226 /* The SP was moved to the FP. This indicates that a new frame
1227 was created. Get THIS frame's FP value by unwinding it from
1228 the next frame. */
1229 this_base = get_frame_register_unsigned (this_frame, CRIS_FP_REGNUM);
1230 info->base = this_base;
1231 info->saved_regs[CRIS_FP_REGNUM].set_addr (info->base);
1232
1233 /* The FP points at the last saved register. Adjust the FP back
1234 to before the first saved register giving the SP. */
1235 info->prev_sp = info->base + info->r8_offset;
1236 }
1237 else
1238 {
1239 ULONGEST this_base;
1240 /* Assume that the FP is this frame's SP but with that pushed
1241 stack space added back. */
1242 this_base = get_frame_register_unsigned (this_frame,
1243 gdbarch_sp_regnum (gdbarch));
1244 info->base = this_base;
1245 info->prev_sp = info->base + info->size;
1246 }
1247
1248 /* Calculate the addresses for the saved registers on the stack. */
1249 /* FIXME: The address calculation should really be done on the fly while
1250 we're analyzing the prologue (we only hold one regsave value as it is
1251 now). */
1252 val = info->sp_offset;
1253
1254 for (regno = regsave; regno >= 0; regno--)
1255 {
1256 info->saved_regs[regno].set_addr (info->base + info->r8_offset - val);
1257 val -= 4;
1258 }
1259
1260 /* The previous frame's SP needed to be computed. Save the computed
1261 value. */
1262 info->saved_regs[gdbarch_sp_regnum (gdbarch)].set_value (info->prev_sp);
1263
1264 if (!info->leaf_function)
1265 {
1266 /* SRP saved on the stack. But where? */
1267 if (info->r8_offset == 0)
1268 {
1269 /* R8 not pushed yet. */
1270 info->saved_regs[SRP_REGNUM].set_addr (info->base);
1271 }
1272 else
1273 {
1274 /* R8 pushed, but SP may or may not be moved to R8 yet. */
1275 info->saved_regs[SRP_REGNUM].set_addr (info->base + 4);
1276 }
1277 }
1278
1279 /* The PC is found in SRP (the actual register or located on the stack). */
1280 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
1281 = info->saved_regs[SRP_REGNUM];
1282
1283 return pc;
1284 }
1285
1286 static CORE_ADDR
1287 crisv32_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
1288 struct cris_unwind_cache *info)
1289 {
1290 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1291 ULONGEST this_base;
1292
1293 /* Unlike the CRISv10 prologue scanner (cris_scan_prologue), this is not
1294 meant to be a full-fledged prologue scanner. It is only needed for
1295 the cases where we end up in code always lacking DWARF-2 CFI, notably:
1296
1297 * PLT stubs (library calls)
1298 * call dummys
1299 * signal trampolines
1300
1301 For those cases, it is assumed that there is no actual prologue; that
1302 the stack pointer is not adjusted, and (as a consequence) the return
1303 address is not pushed onto the stack. */
1304
1305 /* We only want to know the end of the prologue when this_frame and info
1306 are NULL (called from cris_skip_prologue i.e.). */
1307 if (this_frame == NULL && info == NULL)
1308 {
1309 return pc;
1310 }
1311
1312 /* The SP is assumed to be unaltered. */
1313 this_base = get_frame_register_unsigned (this_frame,
1314 gdbarch_sp_regnum (gdbarch));
1315 info->base = this_base;
1316 info->prev_sp = this_base;
1317
1318 /* The PC is assumed to be found in SRP. */
1319 info->saved_regs[gdbarch_pc_regnum (gdbarch)]
1320 = info->saved_regs[SRP_REGNUM];
1321
1322 return pc;
1323 }
1324
1325 /* Advance pc beyond any function entry prologue instructions at pc
1326 to reach some "real" code. */
1327
1328 /* Given a PC value corresponding to the start of a function, return the PC
1329 of the first instruction after the function prologue. */
1330
1331 static CORE_ADDR
1332 cris_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1333 {
1334 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1335 CORE_ADDR func_addr, func_end;
1336 struct symtab_and_line sal;
1337 CORE_ADDR pc_after_prologue;
1338
1339 /* If we have line debugging information, then the end of the prologue
1340 should the first assembly instruction of the first source line. */
1341 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1342 {
1343 sal = find_pc_line (func_addr, 0);
1344 if (sal.end > 0 && sal.end < func_end)
1345 return sal.end;
1346 }
1347
1348 if (tdep->cris_version == 32)
1349 pc_after_prologue = crisv32_scan_prologue (pc, NULL, NULL);
1350 else
1351 pc_after_prologue = cris_scan_prologue (pc, NULL, NULL);
1352
1353 return pc_after_prologue;
1354 }
1355
1356 /* Implement the breakpoint_kind_from_pc gdbarch method. */
1357
1358 static int
1359 cris_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1360 {
1361 return 2;
1362 }
1363
1364 /* Implement the sw_breakpoint_from_kind gdbarch method. */
1365
1366 static const gdb_byte *
1367 cris_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1368 {
1369 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1370 static unsigned char break8_insn[] = {0x38, 0xe9};
1371 static unsigned char break15_insn[] = {0x3f, 0xe9};
1372
1373 *size = kind;
1374
1375 if (tdep->cris_mode == cris_mode_guru)
1376 return break15_insn;
1377 else
1378 return break8_insn;
1379 }
1380
1381 /* Returns 1 if spec_reg is applicable to the current gdbarch's CRIS version,
1382 0 otherwise. */
1383
1384 static int
1385 cris_spec_reg_applicable (struct gdbarch *gdbarch,
1386 struct cris_spec_reg spec_reg)
1387 {
1388 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1389 unsigned int version = tdep->cris_version;
1390
1391 switch (spec_reg.applicable_version)
1392 {
1393 case cris_ver_version_all:
1394 return 1;
1395 case cris_ver_warning:
1396 /* Indeterminate/obsolete. */
1397 return 0;
1398 case cris_ver_v0_3:
1399 return in_inclusive_range (version, 0U, 3U);
1400 case cris_ver_v3p:
1401 return (version >= 3);
1402 case cris_ver_v8:
1403 return in_inclusive_range (version, 8U, 9U);
1404 case cris_ver_v8p:
1405 return (version >= 8);
1406 case cris_ver_v0_10:
1407 return in_inclusive_range (version, 0U, 10U);
1408 case cris_ver_v3_10:
1409 return in_inclusive_range (version, 3U, 10U);
1410 case cris_ver_v8_10:
1411 return in_inclusive_range (version, 8U, 10U);
1412 case cris_ver_v10:
1413 return (version == 10);
1414 case cris_ver_v10p:
1415 return (version >= 10);
1416 case cris_ver_v32p:
1417 return (version >= 32);
1418 default:
1419 /* Invalid cris version. */
1420 return 0;
1421 }
1422 }
1423
1424 /* Returns the register size in unit byte. Returns 0 for an unimplemented
1425 register, -1 for an invalid register. */
1426
1427 static int
1428 cris_register_size (struct gdbarch *gdbarch, int regno)
1429 {
1430 int i;
1431 int spec_regno;
1432
1433 if (regno >= 0 && regno < NUM_GENREGS)
1434 {
1435 /* General registers (R0 - R15) are 32 bits. */
1436 return 4;
1437 }
1438 else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
1439 {
1440 /* Special register (R16 - R31). cris_spec_regs is zero-based.
1441 Adjust regno accordingly. */
1442 spec_regno = regno - NUM_GENREGS;
1443
1444 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1445 {
1446 if (cris_spec_regs[i].number == spec_regno
1447 && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
1448 /* Go with the first applicable register. */
1449 return cris_spec_regs[i].reg_size;
1450 }
1451 /* Special register not applicable to this CRIS version. */
1452 return 0;
1453 }
1454 else if (regno >= gdbarch_pc_regnum (gdbarch)
1455 && regno < gdbarch_num_regs (gdbarch))
1456 {
1457 /* This will apply to CRISv32 only where there are additional registers
1458 after the special registers (pseudo PC and support registers). */
1459 return 4;
1460 }
1461
1462
1463 return -1;
1464 }
1465
1466 /* Nonzero if regno should not be fetched from the target. This is the case
1467 for unimplemented (size 0) and non-existant registers. */
1468
1469 static int
1470 cris_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
1471 {
1472 return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
1473 || (cris_register_size (gdbarch, regno) == 0));
1474 }
1475
1476 /* Nonzero if regno should not be written to the target, for various
1477 reasons. */
1478
1479 static int
1480 cris_cannot_store_register (struct gdbarch *gdbarch, int regno)
1481 {
1482 /* There are three kinds of registers we refuse to write to.
1483 1. Those that not implemented.
1484 2. Those that are read-only (depends on the processor mode).
1485 3. Those registers to which a write has no effect. */
1486
1487 if (regno < 0
1488 || regno >= gdbarch_num_regs (gdbarch)
1489 || cris_register_size (gdbarch, regno) == 0)
1490 /* Not implemented. */
1491 return 1;
1492
1493 else if (regno == VR_REGNUM)
1494 /* Read-only. */
1495 return 1;
1496
1497 else if (regno == P0_REGNUM || regno == P4_REGNUM || regno == P8_REGNUM)
1498 /* Writing has no effect. */
1499 return 1;
1500
1501 /* IBR, BAR, BRP and IRP are read-only in user mode. Let the debug
1502 agent decide whether they are writable. */
1503
1504 return 0;
1505 }
1506
1507 /* Nonzero if regno should not be fetched from the target. This is the case
1508 for unimplemented (size 0) and non-existant registers. */
1509
1510 static int
1511 crisv32_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
1512 {
1513 return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
1514 || (cris_register_size (gdbarch, regno) == 0));
1515 }
1516
1517 /* Nonzero if regno should not be written to the target, for various
1518 reasons. */
1519
1520 static int
1521 crisv32_cannot_store_register (struct gdbarch *gdbarch, int regno)
1522 {
1523 /* There are three kinds of registers we refuse to write to.
1524 1. Those that not implemented.
1525 2. Those that are read-only (depends on the processor mode).
1526 3. Those registers to which a write has no effect. */
1527
1528 if (regno < 0
1529 || regno >= gdbarch_num_regs (gdbarch)
1530 || cris_register_size (gdbarch, regno) == 0)
1531 /* Not implemented. */
1532 return 1;
1533
1534 else if (regno == VR_REGNUM)
1535 /* Read-only. */
1536 return 1;
1537
1538 else if (regno == BZ_REGNUM || regno == WZ_REGNUM || regno == DZ_REGNUM)
1539 /* Writing has no effect. */
1540 return 1;
1541
1542 /* Many special registers are read-only in user mode. Let the debug
1543 agent decide whether they are writable. */
1544
1545 return 0;
1546 }
1547
1548 /* Return the GDB type (defined in gdbtypes.c) for the "standard" data type
1549 of data in register regno. */
1550
1551 static struct type *
1552 cris_register_type (struct gdbarch *gdbarch, int regno)
1553 {
1554 if (regno == gdbarch_pc_regnum (gdbarch))
1555 return builtin_type (gdbarch)->builtin_func_ptr;
1556 else if (regno == gdbarch_sp_regnum (gdbarch)
1557 || regno == CRIS_FP_REGNUM)
1558 return builtin_type (gdbarch)->builtin_data_ptr;
1559 else if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
1560 || (regno >= MOF_REGNUM && regno <= USP_REGNUM))
1561 /* Note: R8 taken care of previous clause. */
1562 return builtin_type (gdbarch)->builtin_uint32;
1563 else if (regno >= P4_REGNUM && regno <= CCR_REGNUM)
1564 return builtin_type (gdbarch)->builtin_uint16;
1565 else if (regno >= P0_REGNUM && regno <= VR_REGNUM)
1566 return builtin_type (gdbarch)->builtin_uint8;
1567 else
1568 /* Invalid (unimplemented) register. */
1569 return builtin_type (gdbarch)->builtin_int0;
1570 }
1571
1572 static struct type *
1573 crisv32_register_type (struct gdbarch *gdbarch, int regno)
1574 {
1575 if (regno == gdbarch_pc_regnum (gdbarch))
1576 return builtin_type (gdbarch)->builtin_func_ptr;
1577 else if (regno == gdbarch_sp_regnum (gdbarch)
1578 || regno == CRIS_FP_REGNUM)
1579 return builtin_type (gdbarch)->builtin_data_ptr;
1580 else if ((regno >= 0 && regno <= ACR_REGNUM)
1581 || (regno >= EXS_REGNUM && regno <= SPC_REGNUM)
1582 || (regno == PID_REGNUM)
1583 || (regno >= S0_REGNUM && regno <= S15_REGNUM))
1584 /* Note: R8 and SP taken care of by previous clause. */
1585 return builtin_type (gdbarch)->builtin_uint32;
1586 else if (regno == WZ_REGNUM)
1587 return builtin_type (gdbarch)->builtin_uint16;
1588 else if (regno == BZ_REGNUM || regno == VR_REGNUM || regno == SRS_REGNUM)
1589 return builtin_type (gdbarch)->builtin_uint8;
1590 else
1591 {
1592 /* Invalid (unimplemented) register. Should not happen as there are
1593 no unimplemented CRISv32 registers. */
1594 warning (_("crisv32_register_type: unknown regno %d"), regno);
1595 return builtin_type (gdbarch)->builtin_int0;
1596 }
1597 }
1598
1599 /* Stores a function return value of type type, where valbuf is the address
1600 of the value to be stored. */
1601
1602 /* In the CRIS ABI, R10 and R11 are used to store return values. */
1603
1604 static void
1605 cris_store_return_value (struct type *type, struct regcache *regcache,
1606 const gdb_byte *valbuf)
1607 {
1608 struct gdbarch *gdbarch = regcache->arch ();
1609 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1610 ULONGEST val;
1611 int len = TYPE_LENGTH (type);
1612
1613 if (len <= 4)
1614 {
1615 /* Put the return value in R10. */
1616 val = extract_unsigned_integer (valbuf, len, byte_order);
1617 regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
1618 }
1619 else if (len <= 8)
1620 {
1621 /* Put the return value in R10 and R11. */
1622 val = extract_unsigned_integer (valbuf, 4, byte_order);
1623 regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
1624 val = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
1625 regcache_cooked_write_unsigned (regcache, ARG2_REGNUM, val);
1626 }
1627 else
1628 error (_("cris_store_return_value: type length too large."));
1629 }
1630
1631 /* Return the name of register regno as a string. Return NULL for an
1632 invalid or unimplemented register. */
1633
1634 static const char *
1635 cris_special_register_name (struct gdbarch *gdbarch, int regno)
1636 {
1637 int spec_regno;
1638 int i;
1639
1640 /* Special register (R16 - R31). cris_spec_regs is zero-based.
1641 Adjust regno accordingly. */
1642 spec_regno = regno - NUM_GENREGS;
1643
1644 /* Assume nothing about the layout of the cris_spec_regs struct
1645 when searching. */
1646 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1647 {
1648 if (cris_spec_regs[i].number == spec_regno
1649 && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
1650 /* Go with the first applicable register. */
1651 return cris_spec_regs[i].name;
1652 }
1653 /* Special register not applicable to this CRIS version. */
1654 return NULL;
1655 }
1656
1657 static const char *
1658 cris_register_name (struct gdbarch *gdbarch, int regno)
1659 {
1660 static const char *cris_genreg_names[] =
1661 { "r0", "r1", "r2", "r3", \
1662 "r4", "r5", "r6", "r7", \
1663 "r8", "r9", "r10", "r11", \
1664 "r12", "r13", "sp", "pc" };
1665
1666 if (regno >= 0 && regno < NUM_GENREGS)
1667 {
1668 /* General register. */
1669 return cris_genreg_names[regno];
1670 }
1671 else if (regno >= NUM_GENREGS && regno < gdbarch_num_regs (gdbarch))
1672 {
1673 return cris_special_register_name (gdbarch, regno);
1674 }
1675 else
1676 {
1677 /* Invalid register. */
1678 return NULL;
1679 }
1680 }
1681
1682 static const char *
1683 crisv32_register_name (struct gdbarch *gdbarch, int regno)
1684 {
1685 static const char *crisv32_genreg_names[] =
1686 { "r0", "r1", "r2", "r3", \
1687 "r4", "r5", "r6", "r7", \
1688 "r8", "r9", "r10", "r11", \
1689 "r12", "r13", "sp", "acr"
1690 };
1691
1692 static const char *crisv32_sreg_names[] =
1693 { "s0", "s1", "s2", "s3", \
1694 "s4", "s5", "s6", "s7", \
1695 "s8", "s9", "s10", "s11", \
1696 "s12", "s13", "s14", "s15"
1697 };
1698
1699 if (regno >= 0 && regno < NUM_GENREGS)
1700 {
1701 /* General register. */
1702 return crisv32_genreg_names[regno];
1703 }
1704 else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
1705 {
1706 return cris_special_register_name (gdbarch, regno);
1707 }
1708 else if (regno == gdbarch_pc_regnum (gdbarch))
1709 {
1710 return "pc";
1711 }
1712 else if (regno >= S0_REGNUM && regno <= S15_REGNUM)
1713 {
1714 return crisv32_sreg_names[regno - S0_REGNUM];
1715 }
1716 else
1717 {
1718 /* Invalid register. */
1719 return NULL;
1720 }
1721 }
1722
1723 /* Convert DWARF register number REG to the appropriate register
1724 number used by GDB. */
1725
1726 static int
1727 cris_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1728 {
1729 /* We need to re-map a couple of registers (SRP is 16 in Dwarf-2 register
1730 numbering, MOF is 18).
1731 Adapted from gcc/config/cris/cris.h. */
1732 static int cris_dwarf_regmap[] = {
1733 0, 1, 2, 3,
1734 4, 5, 6, 7,
1735 8, 9, 10, 11,
1736 12, 13, 14, 15,
1737 27, -1, -1, -1,
1738 -1, -1, -1, 23,
1739 -1, -1, -1, 27,
1740 -1, -1, -1, -1
1741 };
1742 int regnum = -1;
1743
1744 if (reg >= 0 && reg < ARRAY_SIZE (cris_dwarf_regmap))
1745 regnum = cris_dwarf_regmap[reg];
1746
1747 return regnum;
1748 }
1749
1750 /* DWARF-2 frame support. */
1751
1752 static void
1753 cris_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1754 struct dwarf2_frame_state_reg *reg,
1755 struct frame_info *this_frame)
1756 {
1757 /* The return address column. */
1758 if (regnum == gdbarch_pc_regnum (gdbarch))
1759 reg->how = DWARF2_FRAME_REG_RA;
1760
1761 /* The call frame address. */
1762 else if (regnum == gdbarch_sp_regnum (gdbarch))
1763 reg->how = DWARF2_FRAME_REG_CFA;
1764 }
1765
1766 /* Extract from an array regbuf containing the raw register state a function
1767 return value of type type, and copy that, in virtual format, into
1768 valbuf. */
1769
1770 /* In the CRIS ABI, R10 and R11 are used to store return values. */
1771
1772 static void
1773 cris_extract_return_value (struct type *type, struct regcache *regcache,
1774 gdb_byte *valbuf)
1775 {
1776 struct gdbarch *gdbarch = regcache->arch ();
1777 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1778 ULONGEST val;
1779 int len = TYPE_LENGTH (type);
1780
1781 if (len <= 4)
1782 {
1783 /* Get the return value from R10. */
1784 regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
1785 store_unsigned_integer (valbuf, len, byte_order, val);
1786 }
1787 else if (len <= 8)
1788 {
1789 /* Get the return value from R10 and R11. */
1790 regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
1791 store_unsigned_integer (valbuf, 4, byte_order, val);
1792 regcache_cooked_read_unsigned (regcache, ARG2_REGNUM, &val);
1793 store_unsigned_integer (valbuf + 4, len - 4, byte_order, val);
1794 }
1795 else
1796 error (_("cris_extract_return_value: type length too large"));
1797 }
1798
1799 /* Handle the CRIS return value convention. */
1800
1801 static enum return_value_convention
1802 cris_return_value (struct gdbarch *gdbarch, struct value *function,
1803 struct type *type, struct regcache *regcache,
1804 gdb_byte *readbuf, const gdb_byte *writebuf)
1805 {
1806 if (type->code () == TYPE_CODE_STRUCT
1807 || type->code () == TYPE_CODE_UNION
1808 || TYPE_LENGTH (type) > 8)
1809 /* Structs, unions, and anything larger than 8 bytes (2 registers)
1810 goes on the stack. */
1811 return RETURN_VALUE_STRUCT_CONVENTION;
1812
1813 if (readbuf)
1814 cris_extract_return_value (type, regcache, readbuf);
1815 if (writebuf)
1816 cris_store_return_value (type, regcache, writebuf);
1817
1818 return RETURN_VALUE_REGISTER_CONVENTION;
1819 }
1820
1821 /* Calculates a value that measures how good inst_args constraints an
1822 instruction. It stems from cris_constraint, found in cris-dis.c. */
1823
1824 static int
1825 constraint (unsigned int insn, const char *inst_args,
1826 inst_env_type *inst_env)
1827 {
1828 int retval = 0;
1829 int tmp, i;
1830
1831 const gdb_byte *s = (const gdb_byte *) inst_args;
1832
1833 for (; *s; s++)
1834 switch (*s)
1835 {
1836 case 'm':
1837 if ((insn & 0x30) == 0x30)
1838 return -1;
1839 break;
1840
1841 case 'S':
1842 /* A prefix operand. */
1843 if (inst_env->prefix_found)
1844 break;
1845 else
1846 return -1;
1847
1848 case 'B':
1849 /* A "push" prefix. (This check was REMOVED by san 970921.) Check for
1850 valid "push" size. In case of special register, it may be != 4. */
1851 if (inst_env->prefix_found)
1852 break;
1853 else
1854 return -1;
1855
1856 case 'D':
1857 retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
1858 if (!retval)
1859 return -1;
1860 else
1861 retval += 4;
1862 break;
1863
1864 case 'P':
1865 tmp = (insn >> 0xC) & 0xF;
1866
1867 for (i = 0; cris_spec_regs[i].name != NULL; i++)
1868 {
1869 /* Since we match four bits, we will give a value of
1870 4 - 1 = 3 in a match. If there is a corresponding
1871 exact match of a special register in another pattern, it
1872 will get a value of 4, which will be higher. This should
1873 be correct in that an exact pattern would match better that
1874 a general pattern.
1875 Note that there is a reason for not returning zero; the
1876 pattern for "clear" is partly matched in the bit-pattern
1877 (the two lower bits must be zero), while the bit-pattern
1878 for a move from a special register is matched in the
1879 register constraint.
1880 This also means we will will have a race condition if
1881 there is a partly match in three bits in the bit pattern. */
1882 if (tmp == cris_spec_regs[i].number)
1883 {
1884 retval += 3;
1885 break;
1886 }
1887 }
1888
1889 if (cris_spec_regs[i].name == NULL)
1890 return -1;
1891 break;
1892 }
1893 return retval;
1894 }
1895
1896 /* Returns the number of bits set in the variable value. */
1897
1898 static int
1899 number_of_bits (unsigned int value)
1900 {
1901 int number_of_bits = 0;
1902
1903 while (value != 0)
1904 {
1905 number_of_bits += 1;
1906 value &= (value - 1);
1907 }
1908 return number_of_bits;
1909 }
1910
1911 /* Finds the address that should contain the single step breakpoint(s).
1912 It stems from code in cris-dis.c. */
1913
1914 static int
1915 find_cris_op (unsigned short insn, inst_env_type *inst_env)
1916 {
1917 int i;
1918 int max_level_of_match = -1;
1919 int max_matched = -1;
1920 int level_of_match;
1921
1922 for (i = 0; cris_opcodes[i].name != NULL; i++)
1923 {
1924 if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match)
1925 && ((cris_opcodes[i].lose & insn) == 0)
1926 /* Only CRISv10 instructions, please. */
1927 && (cris_opcodes[i].applicable_version != cris_ver_v32p))
1928 {
1929 level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
1930 if (level_of_match >= 0)
1931 {
1932 level_of_match +=
1933 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
1934 if (level_of_match > max_level_of_match)
1935 {
1936 max_matched = i;
1937 max_level_of_match = level_of_match;
1938 if (level_of_match == 16)
1939 {
1940 /* All bits matched, cannot find better. */
1941 break;
1942 }
1943 }
1944 }
1945 }
1946 }
1947 return max_matched;
1948 }
1949
1950 /* Attempts to find single-step breakpoints. Returns -1 on failure which is
1951 actually an internal error. */
1952
1953 static int
1954 find_step_target (struct regcache *regcache, inst_env_type *inst_env)
1955 {
1956 int i;
1957 int offset;
1958 unsigned short insn;
1959 struct gdbarch *gdbarch = regcache->arch ();
1960 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1961
1962 /* Create a local register image and set the initial state. */
1963 for (i = 0; i < NUM_GENREGS; i++)
1964 {
1965 inst_env->reg[i] =
1966 (unsigned long) regcache_raw_get_unsigned (regcache, i);
1967 }
1968 offset = NUM_GENREGS;
1969 for (i = 0; i < NUM_SPECREGS; i++)
1970 {
1971 inst_env->preg[i] =
1972 (unsigned long) regcache_raw_get_unsigned (regcache, offset + i);
1973 }
1974 inst_env->branch_found = 0;
1975 inst_env->slot_needed = 0;
1976 inst_env->delay_slot_pc_active = 0;
1977 inst_env->prefix_found = 0;
1978 inst_env->invalid = 0;
1979 inst_env->xflag_found = 0;
1980 inst_env->disable_interrupt = 0;
1981 inst_env->byte_order = byte_order;
1982
1983 /* Look for a step target. */
1984 do
1985 {
1986 /* Read an instruction from the client. */
1987 insn = read_memory_unsigned_integer
1988 (inst_env->reg[gdbarch_pc_regnum (gdbarch)], 2, byte_order);
1989
1990 /* If the instruction is not in a delay slot the new content of the
1991 PC is [PC] + 2. If the instruction is in a delay slot it is not
1992 that simple. Since a instruction in a delay slot cannot change
1993 the content of the PC, it does not matter what value PC will have.
1994 Just make sure it is a valid instruction. */
1995 if (!inst_env->delay_slot_pc_active)
1996 {
1997 inst_env->reg[gdbarch_pc_regnum (gdbarch)] += 2;
1998 }
1999 else
2000 {
2001 inst_env->delay_slot_pc_active = 0;
2002 inst_env->reg[gdbarch_pc_regnum (gdbarch)]
2003 = inst_env->delay_slot_pc;
2004 }
2005 /* Analyse the present instruction. */
2006 i = find_cris_op (insn, inst_env);
2007 if (i == -1)
2008 {
2009 inst_env->invalid = 1;
2010 }
2011 else
2012 {
2013 cris_gdb_func (gdbarch, cris_opcodes[i].op, insn, inst_env);
2014 }
2015 } while (!inst_env->invalid
2016 && (inst_env->prefix_found || inst_env->xflag_found
2017 || inst_env->slot_needed));
2018 return i;
2019 }
2020
2021 /* There is no hardware single-step support. The function find_step_target
2022 digs through the opcodes in order to find all possible targets.
2023 Either one ordinary target or two targets for branches may be found. */
2024
2025 static std::vector<CORE_ADDR>
2026 cris_software_single_step (struct regcache *regcache)
2027 {
2028 struct gdbarch *gdbarch = regcache->arch ();
2029 inst_env_type inst_env;
2030 std::vector<CORE_ADDR> next_pcs;
2031
2032 /* Analyse the present instruction environment and insert
2033 breakpoints. */
2034 int status = find_step_target (regcache, &inst_env);
2035 if (status == -1)
2036 {
2037 /* Could not find a target. Things are likely to go downhill
2038 from here. */
2039 warning (_("CRIS software single step could not find a step target."));
2040 }
2041 else
2042 {
2043 /* Insert at most two breakpoints. One for the next PC content
2044 and possibly another one for a branch, jump, etc. */
2045 CORE_ADDR next_pc
2046 = (CORE_ADDR) inst_env.reg[gdbarch_pc_regnum (gdbarch)];
2047
2048 next_pcs.push_back (next_pc);
2049 if (inst_env.branch_found
2050 && (CORE_ADDR) inst_env.branch_break_address != next_pc)
2051 {
2052 CORE_ADDR branch_target_address
2053 = (CORE_ADDR) inst_env.branch_break_address;
2054
2055 next_pcs.push_back (branch_target_address);
2056 }
2057 }
2058
2059 return next_pcs;
2060 }
2061
2062 /* Calculates the prefix value for quick offset addressing mode. */
2063
2064 static void
2065 quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
2066 {
2067 /* It's invalid to be in a delay slot. You can't have a prefix to this
2068 instruction (not 100% sure). */
2069 if (inst_env->slot_needed || inst_env->prefix_found)
2070 {
2071 inst_env->invalid = 1;
2072 return;
2073 }
2074
2075 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
2076 inst_env->prefix_value += cris_get_bdap_quick_offset (inst);
2077
2078 /* A prefix doesn't change the xflag_found. But the rest of the flags
2079 need updating. */
2080 inst_env->slot_needed = 0;
2081 inst_env->prefix_found = 1;
2082 }
2083
2084 /* Updates the autoincrement register. The size of the increment is derived
2085 from the size of the operation. The PC is always kept aligned on even
2086 word addresses. */
2087
2088 static void
2089 process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
2090 {
2091 if (size == INST_BYTE_SIZE)
2092 {
2093 inst_env->reg[cris_get_operand1 (inst)] += 1;
2094
2095 /* The PC must be word aligned, so increase the PC with one
2096 word even if the size is byte. */
2097 if (cris_get_operand1 (inst) == REG_PC)
2098 {
2099 inst_env->reg[REG_PC] += 1;
2100 }
2101 }
2102 else if (size == INST_WORD_SIZE)
2103 {
2104 inst_env->reg[cris_get_operand1 (inst)] += 2;
2105 }
2106 else if (size == INST_DWORD_SIZE)
2107 {
2108 inst_env->reg[cris_get_operand1 (inst)] += 4;
2109 }
2110 else
2111 {
2112 /* Invalid size. */
2113 inst_env->invalid = 1;
2114 }
2115 }
2116
2117 /* Just a forward declaration. */
2118
2119 static unsigned long get_data_from_address (unsigned short *inst,
2120 CORE_ADDR address,
2121 enum bfd_endian byte_order);
2122
2123 /* Calculates the prefix value for the general case of offset addressing
2124 mode. */
2125
2126 static void
2127 bdap_prefix (unsigned short inst, inst_env_type *inst_env)
2128 {
2129 /* It's invalid to be in a delay slot. */
2130 if (inst_env->slot_needed || inst_env->prefix_found)
2131 {
2132 inst_env->invalid = 1;
2133 return;
2134 }
2135
2136 /* The calculation of prefix_value used to be after process_autoincrement,
2137 but that fails for an instruction such as jsr [$r0+12] which is encoded
2138 as 5f0d 0c00 30b9 when compiled with -fpic. Since PC is operand1 it
2139 mustn't be incremented until we have read it and what it points at. */
2140 inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
2141
2142 /* The offset is an indirection of the contents of the operand1 register. */
2143 inst_env->prefix_value +=
2144 get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)],
2145 inst_env->byte_order);
2146
2147 if (cris_get_mode (inst) == AUTOINC_MODE)
2148 {
2149 process_autoincrement (cris_get_size (inst), inst, inst_env);
2150 }
2151
2152 /* A prefix doesn't change the xflag_found. But the rest of the flags
2153 need updating. */
2154 inst_env->slot_needed = 0;
2155 inst_env->prefix_found = 1;
2156 }
2157
2158 /* Calculates the prefix value for the index addressing mode. */
2159
2160 static void
2161 biap_prefix (unsigned short inst, inst_env_type *inst_env)
2162 {
2163 /* It's invalid to be in a delay slot. I can't see that it's possible to
2164 have a prefix to this instruction. So I will treat this as invalid. */
2165 if (inst_env->slot_needed || inst_env->prefix_found)
2166 {
2167 inst_env->invalid = 1;
2168 return;
2169 }
2170
2171 inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];
2172
2173 /* The offset is the operand2 value shifted the size of the instruction
2174 to the left. */
2175 inst_env->prefix_value +=
2176 inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);
2177
2178 /* If the PC is operand1 (base) the address used is the address after
2179 the main instruction, i.e. address + 2 (the PC is already compensated
2180 for the prefix operation). */
2181 if (cris_get_operand1 (inst) == REG_PC)
2182 {
2183 inst_env->prefix_value += 2;
2184 }
2185
2186 /* A prefix doesn't change the xflag_found. But the rest of the flags
2187 need updating. */
2188 inst_env->slot_needed = 0;
2189 inst_env->xflag_found = 0;
2190 inst_env->prefix_found = 1;
2191 }
2192
2193 /* Calculates the prefix value for the double indirect addressing mode. */
2194
2195 static void
2196 dip_prefix (unsigned short inst, inst_env_type *inst_env)
2197 {
2198
2199 CORE_ADDR address;
2200
2201 /* It's invalid to be in a delay slot. */
2202 if (inst_env->slot_needed || inst_env->prefix_found)
2203 {
2204 inst_env->invalid = 1;
2205 return;
2206 }
2207
2208 /* The prefix value is one dereference of the contents of the operand1
2209 register. */
2210 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2211 inst_env->prefix_value
2212 = read_memory_unsigned_integer (address, 4, inst_env->byte_order);
2213
2214 /* Check if the mode is autoincrement. */
2215 if (cris_get_mode (inst) == AUTOINC_MODE)
2216 {
2217 inst_env->reg[cris_get_operand1 (inst)] += 4;
2218 }
2219
2220 /* A prefix doesn't change the xflag_found. But the rest of the flags
2221 need updating. */
2222 inst_env->slot_needed = 0;
2223 inst_env->xflag_found = 0;
2224 inst_env->prefix_found = 1;
2225 }
2226
2227 /* Finds the destination for a branch with 8-bits offset. */
2228
2229 static void
2230 eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2231 {
2232
2233 short offset;
2234
2235 /* If we have a prefix or are in a delay slot it's bad. */
2236 if (inst_env->slot_needed || inst_env->prefix_found)
2237 {
2238 inst_env->invalid = 1;
2239 return;
2240 }
2241
2242 /* We have a branch, find out where the branch will land. */
2243 offset = cris_get_branch_short_offset (inst);
2244
2245 /* Check if the offset is signed. */
2246 if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
2247 {
2248 offset |= 0xFF00;
2249 }
2250
2251 /* The offset ends with the sign bit, set it to zero. The address
2252 should always be word aligned. */
2253 offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;
2254
2255 inst_env->branch_found = 1;
2256 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2257
2258 inst_env->slot_needed = 1;
2259 inst_env->prefix_found = 0;
2260 inst_env->xflag_found = 0;
2261 inst_env->disable_interrupt = 1;
2262 }
2263
2264 /* Finds the destination for a branch with 16-bits offset. */
2265
2266 static void
2267 sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2268 {
2269 short offset;
2270
2271 /* If we have a prefix or is in a delay slot it's bad. */
2272 if (inst_env->slot_needed || inst_env->prefix_found)
2273 {
2274 inst_env->invalid = 1;
2275 return;
2276 }
2277
2278 /* We have a branch, find out the offset for the branch. */
2279 offset = read_memory_integer (inst_env->reg[REG_PC], 2,
2280 inst_env->byte_order);
2281
2282 /* The instruction is one word longer than normal, so add one word
2283 to the PC. */
2284 inst_env->reg[REG_PC] += 2;
2285
2286 inst_env->branch_found = 1;
2287 inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2288
2289
2290 inst_env->slot_needed = 1;
2291 inst_env->prefix_found = 0;
2292 inst_env->xflag_found = 0;
2293 inst_env->disable_interrupt = 1;
2294 }
2295
2296 /* Handles the ABS instruction. */
2297
2298 static void
2299 abs_op (unsigned short inst, inst_env_type *inst_env)
2300 {
2301
2302 long value;
2303
2304 /* ABS can't have a prefix, so it's bad if it does. */
2305 if (inst_env->prefix_found)
2306 {
2307 inst_env->invalid = 1;
2308 return;
2309 }
2310
2311 /* Check if the operation affects the PC. */
2312 if (cris_get_operand2 (inst) == REG_PC)
2313 {
2314
2315 /* It's invalid to change to the PC if we are in a delay slot. */
2316 if (inst_env->slot_needed)
2317 {
2318 inst_env->invalid = 1;
2319 return;
2320 }
2321
2322 value = (long) inst_env->reg[REG_PC];
2323
2324 /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK. */
2325 if (value != SIGNED_DWORD_MASK)
2326 {
2327 value = -value;
2328 inst_env->reg[REG_PC] = (long) value;
2329 }
2330 }
2331
2332 inst_env->slot_needed = 0;
2333 inst_env->prefix_found = 0;
2334 inst_env->xflag_found = 0;
2335 inst_env->disable_interrupt = 0;
2336 }
2337
2338 /* Handles the ADDI instruction. */
2339
2340 static void
2341 addi_op (unsigned short inst, inst_env_type *inst_env)
2342 {
2343 /* It's invalid to have the PC as base register. And ADDI can't have
2344 a prefix. */
2345 if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
2346 {
2347 inst_env->invalid = 1;
2348 return;
2349 }
2350
2351 inst_env->slot_needed = 0;
2352 inst_env->prefix_found = 0;
2353 inst_env->xflag_found = 0;
2354 inst_env->disable_interrupt = 0;
2355 }
2356
2357 /* Handles the ASR instruction. */
2358
2359 static void
2360 asr_op (unsigned short inst, inst_env_type *inst_env)
2361 {
2362 int shift_steps;
2363 unsigned long value;
2364 unsigned long signed_extend_mask = 0;
2365
2366 /* ASR can't have a prefix, so check that it doesn't. */
2367 if (inst_env->prefix_found)
2368 {
2369 inst_env->invalid = 1;
2370 return;
2371 }
2372
2373 /* Check if the PC is the target register. */
2374 if (cris_get_operand2 (inst) == REG_PC)
2375 {
2376 /* It's invalid to change the PC in a delay slot. */
2377 if (inst_env->slot_needed)
2378 {
2379 inst_env->invalid = 1;
2380 return;
2381 }
2382 /* Get the number of bits to shift. */
2383 shift_steps
2384 = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
2385 value = inst_env->reg[REG_PC];
2386
2387 /* Find out how many bits the operation should apply to. */
2388 if (cris_get_size (inst) == INST_BYTE_SIZE)
2389 {
2390 if (value & SIGNED_BYTE_MASK)
2391 {
2392 signed_extend_mask = 0xFF;
2393 signed_extend_mask = signed_extend_mask >> shift_steps;
2394 signed_extend_mask = ~signed_extend_mask;
2395 }
2396 value = value >> shift_steps;
2397 value |= signed_extend_mask;
2398 value &= 0xFF;
2399 inst_env->reg[REG_PC] &= 0xFFFFFF00;
2400 inst_env->reg[REG_PC] |= value;
2401 }
2402 else if (cris_get_size (inst) == INST_WORD_SIZE)
2403 {
2404 if (value & SIGNED_WORD_MASK)
2405 {
2406 signed_extend_mask = 0xFFFF;
2407 signed_extend_mask = signed_extend_mask >> shift_steps;
2408 signed_extend_mask = ~signed_extend_mask;
2409 }
2410 value = value >> shift_steps;
2411 value |= signed_extend_mask;
2412 value &= 0xFFFF;
2413 inst_env->reg[REG_PC] &= 0xFFFF0000;
2414 inst_env->reg[REG_PC] |= value;
2415 }
2416 else if (cris_get_size (inst) == INST_DWORD_SIZE)
2417 {
2418 if (value & SIGNED_DWORD_MASK)
2419 {
2420 signed_extend_mask = 0xFFFFFFFF;
2421 signed_extend_mask = signed_extend_mask >> shift_steps;
2422 signed_extend_mask = ~signed_extend_mask;
2423 }
2424 value = value >> shift_steps;
2425 value |= signed_extend_mask;
2426 inst_env->reg[REG_PC] = value;
2427 }
2428 }
2429 inst_env->slot_needed = 0;
2430 inst_env->prefix_found = 0;
2431 inst_env->xflag_found = 0;
2432 inst_env->disable_interrupt = 0;
2433 }
2434
2435 /* Handles the ASRQ instruction. */
2436
2437 static void
2438 asrq_op (unsigned short inst, inst_env_type *inst_env)
2439 {
2440
2441 int shift_steps;
2442 unsigned long value;
2443 unsigned long signed_extend_mask = 0;
2444
2445 /* ASRQ can't have a prefix, so check that it doesn't. */
2446 if (inst_env->prefix_found)
2447 {
2448 inst_env->invalid = 1;
2449 return;
2450 }
2451
2452 /* Check if the PC is the target register. */
2453 if (cris_get_operand2 (inst) == REG_PC)
2454 {
2455
2456 /* It's invalid to change the PC in a delay slot. */
2457 if (inst_env->slot_needed)
2458 {
2459 inst_env->invalid = 1;
2460 return;
2461 }
2462 /* The shift size is given as a 5 bit quick value, i.e. we don't
2463 want the sign bit of the quick value. */
2464 shift_steps = cris_get_asr_shift_steps (inst);
2465 value = inst_env->reg[REG_PC];
2466 if (value & SIGNED_DWORD_MASK)
2467 {
2468 signed_extend_mask = 0xFFFFFFFF;
2469 signed_extend_mask = signed_extend_mask >> shift_steps;
2470 signed_extend_mask = ~signed_extend_mask;
2471 }
2472 value = value >> shift_steps;
2473 value |= signed_extend_mask;
2474 inst_env->reg[REG_PC] = value;
2475 }
2476 inst_env->slot_needed = 0;
2477 inst_env->prefix_found = 0;
2478 inst_env->xflag_found = 0;
2479 inst_env->disable_interrupt = 0;
2480 }
2481
2482 /* Handles the AX, EI and SETF instruction. */
2483
2484 static void
2485 ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
2486 {
2487 if (inst_env->prefix_found)
2488 {
2489 inst_env->invalid = 1;
2490 return;
2491 }
2492 /* Check if the instruction is setting the X flag. */
2493 if (cris_is_xflag_bit_on (inst))
2494 {
2495 inst_env->xflag_found = 1;
2496 }
2497 else
2498 {
2499 inst_env->xflag_found = 0;
2500 }
2501 inst_env->slot_needed = 0;
2502 inst_env->prefix_found = 0;
2503 inst_env->disable_interrupt = 1;
2504 }
2505
2506 /* Checks if the instruction is in assign mode. If so, it updates the assign
2507 register. Note that check_assign assumes that the caller has checked that
2508 there is a prefix to this instruction. The mode check depends on this. */
2509
2510 static void
2511 check_assign (unsigned short inst, inst_env_type *inst_env)
2512 {
2513 /* Check if it's an assign addressing mode. */
2514 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2515 {
2516 /* Assign the prefix value to operand 1. */
2517 inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
2518 }
2519 }
2520
2521 /* Handles the 2-operand BOUND instruction. */
2522
2523 static void
2524 two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2525 {
2526 /* It's invalid to have the PC as the index operand. */
2527 if (cris_get_operand2 (inst) == REG_PC)
2528 {
2529 inst_env->invalid = 1;
2530 return;
2531 }
2532 /* Check if we have a prefix. */
2533 if (inst_env->prefix_found)
2534 {
2535 check_assign (inst, inst_env);
2536 }
2537 /* Check if this is an autoincrement mode. */
2538 else if (cris_get_mode (inst) == AUTOINC_MODE)
2539 {
2540 /* It's invalid to change the PC in a delay slot. */
2541 if (inst_env->slot_needed)
2542 {
2543 inst_env->invalid = 1;
2544 return;
2545 }
2546 process_autoincrement (cris_get_size (inst), inst, inst_env);
2547 }
2548 inst_env->slot_needed = 0;
2549 inst_env->prefix_found = 0;
2550 inst_env->xflag_found = 0;
2551 inst_env->disable_interrupt = 0;
2552 }
2553
2554 /* Handles the 3-operand BOUND instruction. */
2555
2556 static void
2557 three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2558 {
2559 /* It's an error if we haven't got a prefix. And it's also an error
2560 if the PC is the destination register. */
2561 if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
2562 {
2563 inst_env->invalid = 1;
2564 return;
2565 }
2566 inst_env->slot_needed = 0;
2567 inst_env->prefix_found = 0;
2568 inst_env->xflag_found = 0;
2569 inst_env->disable_interrupt = 0;
2570 }
2571
2572 /* Clears the status flags in inst_env. */
2573
2574 static void
2575 btst_nop_op (unsigned short inst, inst_env_type *inst_env)
2576 {
2577 /* It's an error if we have got a prefix. */
2578 if (inst_env->prefix_found)
2579 {
2580 inst_env->invalid = 1;
2581 return;
2582 }
2583
2584 inst_env->slot_needed = 0;
2585 inst_env->prefix_found = 0;
2586 inst_env->xflag_found = 0;
2587 inst_env->disable_interrupt = 0;
2588 }
2589
2590 /* Clears the status flags in inst_env. */
2591
2592 static void
2593 clearf_di_op (unsigned short inst, inst_env_type *inst_env)
2594 {
2595 /* It's an error if we have got a prefix. */
2596 if (inst_env->prefix_found)
2597 {
2598 inst_env->invalid = 1;
2599 return;
2600 }
2601
2602 inst_env->slot_needed = 0;
2603 inst_env->prefix_found = 0;
2604 inst_env->xflag_found = 0;
2605 inst_env->disable_interrupt = 1;
2606 }
2607
2608 /* Handles the CLEAR instruction if it's in register mode. */
2609
2610 static void
2611 reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
2612 {
2613 /* Check if the target is the PC. */
2614 if (cris_get_operand2 (inst) == REG_PC)
2615 {
2616 /* The instruction will clear the instruction's size bits. */
2617 int clear_size = cris_get_clear_size (inst);
2618 if (clear_size == INST_BYTE_SIZE)
2619 {
2620 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
2621 }
2622 if (clear_size == INST_WORD_SIZE)
2623 {
2624 inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
2625 }
2626 if (clear_size == INST_DWORD_SIZE)
2627 {
2628 inst_env->delay_slot_pc = 0x0;
2629 }
2630 /* The jump will be delayed with one delay slot. So we need a delay
2631 slot. */
2632 inst_env->slot_needed = 1;
2633 inst_env->delay_slot_pc_active = 1;
2634 }
2635 else
2636 {
2637 /* The PC will not change => no delay slot. */
2638 inst_env->slot_needed = 0;
2639 }
2640 inst_env->prefix_found = 0;
2641 inst_env->xflag_found = 0;
2642 inst_env->disable_interrupt = 0;
2643 }
2644
2645 /* Handles the TEST instruction if it's in register mode. */
2646
2647 static void
2648 reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
2649 {
2650 /* It's an error if we have got a prefix. */
2651 if (inst_env->prefix_found)
2652 {
2653 inst_env->invalid = 1;
2654 return;
2655 }
2656 inst_env->slot_needed = 0;
2657 inst_env->prefix_found = 0;
2658 inst_env->xflag_found = 0;
2659 inst_env->disable_interrupt = 0;
2660
2661 }
2662
2663 /* Handles the CLEAR and TEST instruction if the instruction isn't
2664 in register mode. */
2665
2666 static void
2667 none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
2668 {
2669 /* Check if we are in a prefix mode. */
2670 if (inst_env->prefix_found)
2671 {
2672 /* The only way the PC can change is if this instruction is in
2673 assign addressing mode. */
2674 check_assign (inst, inst_env);
2675 }
2676 /* Indirect mode can't change the PC so just check if the mode is
2677 autoincrement. */
2678 else if (cris_get_mode (inst) == AUTOINC_MODE)
2679 {
2680 process_autoincrement (cris_get_size (inst), inst, inst_env);
2681 }
2682 inst_env->slot_needed = 0;
2683 inst_env->prefix_found = 0;
2684 inst_env->xflag_found = 0;
2685 inst_env->disable_interrupt = 0;
2686 }
2687
2688 /* Checks that the PC isn't the destination register or the instructions has
2689 a prefix. */
2690
2691 static void
2692 dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
2693 {
2694 /* It's invalid to have the PC as the destination. The instruction can't
2695 have a prefix. */
2696 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2697 {
2698 inst_env->invalid = 1;
2699 return;
2700 }
2701
2702 inst_env->slot_needed = 0;
2703 inst_env->prefix_found = 0;
2704 inst_env->xflag_found = 0;
2705 inst_env->disable_interrupt = 0;
2706 }
2707
2708 /* Checks that the instruction doesn't have a prefix. */
2709
2710 static void
2711 break_op (unsigned short inst, inst_env_type *inst_env)
2712 {
2713 /* The instruction can't have a prefix. */
2714 if (inst_env->prefix_found)
2715 {
2716 inst_env->invalid = 1;
2717 return;
2718 }
2719
2720 inst_env->slot_needed = 0;
2721 inst_env->prefix_found = 0;
2722 inst_env->xflag_found = 0;
2723 inst_env->disable_interrupt = 1;
2724 }
2725
2726 /* Checks that the PC isn't the destination register and that the instruction
2727 doesn't have a prefix. */
2728
2729 static void
2730 scc_op (unsigned short inst, inst_env_type *inst_env)
2731 {
2732 /* It's invalid to have the PC as the destination. The instruction can't
2733 have a prefix. */
2734 if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2735 {
2736 inst_env->invalid = 1;
2737 return;
2738 }
2739
2740 inst_env->slot_needed = 0;
2741 inst_env->prefix_found = 0;
2742 inst_env->xflag_found = 0;
2743 inst_env->disable_interrupt = 1;
2744 }
2745
2746 /* Handles the register mode JUMP instruction. */
2747
2748 static void
2749 reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2750 {
2751 /* It's invalid to do a JUMP in a delay slot. The mode is register, so
2752 you can't have a prefix. */
2753 if ((inst_env->slot_needed) || (inst_env->prefix_found))
2754 {
2755 inst_env->invalid = 1;
2756 return;
2757 }
2758
2759 /* Just change the PC. */
2760 inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
2761 inst_env->slot_needed = 0;
2762 inst_env->prefix_found = 0;
2763 inst_env->xflag_found = 0;
2764 inst_env->disable_interrupt = 1;
2765 }
2766
2767 /* Handles the JUMP instruction for all modes except register. */
2768
2769 static void
2770 none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2771 {
2772 unsigned long newpc;
2773 CORE_ADDR address;
2774
2775 /* It's invalid to do a JUMP in a delay slot. */
2776 if (inst_env->slot_needed)
2777 {
2778 inst_env->invalid = 1;
2779 }
2780 else
2781 {
2782 /* Check if we have a prefix. */
2783 if (inst_env->prefix_found)
2784 {
2785 check_assign (inst, inst_env);
2786
2787 /* Get the new value for the PC. */
2788 newpc =
2789 read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
2790 4, inst_env->byte_order);
2791 }
2792 else
2793 {
2794 /* Get the new value for the PC. */
2795 address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2796 newpc = read_memory_unsigned_integer (address,
2797 4, inst_env->byte_order);
2798
2799 /* Check if we should increment a register. */
2800 if (cris_get_mode (inst) == AUTOINC_MODE)
2801 {
2802 inst_env->reg[cris_get_operand1 (inst)] += 4;
2803 }
2804 }
2805 inst_env->reg[REG_PC] = newpc;
2806 }
2807 inst_env->slot_needed = 0;
2808 inst_env->prefix_found = 0;
2809 inst_env->xflag_found = 0;
2810 inst_env->disable_interrupt = 1;
2811 }
2812
2813 /* Handles moves to special registers (aka P-register) for all modes. */
2814
2815 static void
2816 move_to_preg_op (struct gdbarch *gdbarch, unsigned short inst,
2817 inst_env_type *inst_env)
2818 {
2819 if (inst_env->prefix_found)
2820 {
2821 /* The instruction has a prefix that means we are only interested if
2822 the instruction is in assign mode. */
2823 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2824 {
2825 /* The prefix handles the problem if we are in a delay slot. */
2826 if (cris_get_operand1 (inst) == REG_PC)
2827 {
2828 /* Just take care of the assign. */
2829 check_assign (inst, inst_env);
2830 }
2831 }
2832 }
2833 else if (cris_get_mode (inst) == AUTOINC_MODE)
2834 {
2835 /* The instruction doesn't have a prefix, the only case left that we
2836 are interested in is the autoincrement mode. */
2837 if (cris_get_operand1 (inst) == REG_PC)
2838 {
2839 /* If the PC is to be incremented it's invalid to be in a
2840 delay slot. */
2841 if (inst_env->slot_needed)
2842 {
2843 inst_env->invalid = 1;
2844 return;
2845 }
2846
2847 /* The increment depends on the size of the special register. */
2848 if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
2849 {
2850 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2851 }
2852 else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
2853 {
2854 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2855 }
2856 else
2857 {
2858 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2859 }
2860 }
2861 }
2862 inst_env->slot_needed = 0;
2863 inst_env->prefix_found = 0;
2864 inst_env->xflag_found = 0;
2865 inst_env->disable_interrupt = 1;
2866 }
2867
2868 /* Handles moves from special registers (aka P-register) for all modes
2869 except register. */
2870
2871 static void
2872 none_reg_mode_move_from_preg_op (struct gdbarch *gdbarch, unsigned short inst,
2873 inst_env_type *inst_env)
2874 {
2875 if (inst_env->prefix_found)
2876 {
2877 /* The instruction has a prefix that means we are only interested if
2878 the instruction is in assign mode. */
2879 if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2880 {
2881 /* The prefix handles the problem if we are in a delay slot. */
2882 if (cris_get_operand1 (inst) == REG_PC)
2883 {
2884 /* Just take care of the assign. */
2885 check_assign (inst, inst_env);
2886 }
2887 }
2888 }
2889 /* The instruction doesn't have a prefix, the only case left that we
2890 are interested in is the autoincrement mode. */
2891 else if (cris_get_mode (inst) == AUTOINC_MODE)
2892 {
2893 if (cris_get_operand1 (inst) == REG_PC)
2894 {
2895 /* If the PC is to be incremented it's invalid to be in a
2896 delay slot. */
2897 if (inst_env->slot_needed)
2898 {
2899 inst_env->invalid = 1;
2900 return;
2901 }
2902
2903 /* The increment depends on the size of the special register. */
2904 if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
2905 {
2906 process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2907 }
2908 else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
2909 {
2910 process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2911 }
2912 else
2913 {
2914 process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2915 }
2916 }
2917 }
2918 inst_env->slot_needed = 0;
2919 inst_env->prefix_found = 0;
2920 inst_env->xflag_found = 0;
2921 inst_env->disable_interrupt = 1;
2922 }
2923
2924 /* Handles moves from special registers (aka P-register) when the mode
2925 is register. */
2926
2927 static void
2928 reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2929 {
2930 /* Register mode move from special register can't have a prefix. */
2931 if (inst_env->prefix_found)
2932 {
2933 inst_env->invalid = 1;
2934 return;
2935 }
2936
2937 if (cris_get_operand1 (inst) == REG_PC)
2938 {
2939 /* It's invalid to change the PC in a delay slot. */
2940 if (inst_env->slot_needed)
2941 {
2942 inst_env->invalid = 1;
2943 return;
2944 }
2945 /* The destination is the PC, the jump will have a delay slot. */
2946 inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
2947 inst_env->slot_needed = 1;
2948 inst_env->delay_slot_pc_active = 1;
2949 }
2950 else
2951 {
2952 /* If the destination isn't PC, there will be no jump. */
2953 inst_env->slot_needed = 0;
2954 }
2955 inst_env->prefix_found = 0;
2956 inst_env->xflag_found = 0;
2957 inst_env->disable_interrupt = 1;
2958 }
2959
2960 /* Handles the MOVEM from memory to general register instruction. */
2961
2962 static void
2963 move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
2964 {
2965 if (inst_env->prefix_found)
2966 {
2967 /* The prefix handles the problem if we are in a delay slot. Is the
2968 MOVEM instruction going to change the PC? */
2969 if (cris_get_operand2 (inst) >= REG_PC)
2970 {
2971 inst_env->reg[REG_PC] =
2972 read_memory_unsigned_integer (inst_env->prefix_value,
2973 4, inst_env->byte_order);
2974 }
2975 /* The assign value is the value after the increment. Normally, the
2976 assign value is the value before the increment. */
2977 if ((cris_get_operand1 (inst) == REG_PC)
2978 && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2979 {
2980 inst_env->reg[REG_PC] = inst_env->prefix_value;
2981 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2982 }
2983 }
2984 else
2985 {
2986 /* Is the MOVEM instruction going to change the PC? */
2987 if (cris_get_operand2 (inst) == REG_PC)
2988 {
2989 /* It's invalid to change the PC in a delay slot. */
2990 if (inst_env->slot_needed)
2991 {
2992 inst_env->invalid = 1;
2993 return;
2994 }
2995 inst_env->reg[REG_PC] =
2996 read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)],
2997 4, inst_env->byte_order);
2998 }
2999 /* The increment is not depending on the size, instead it's depending
3000 on the number of registers loaded from memory. */
3001 if ((cris_get_operand1 (inst) == REG_PC)
3002 && (cris_get_mode (inst) == AUTOINC_MODE))
3003 {
3004 /* It's invalid to change the PC in a delay slot. */
3005 if (inst_env->slot_needed)
3006 {
3007 inst_env->invalid = 1;
3008 return;
3009 }
3010 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3011 }
3012 }
3013 inst_env->slot_needed = 0;
3014 inst_env->prefix_found = 0;
3015 inst_env->xflag_found = 0;
3016 inst_env->disable_interrupt = 0;
3017 }
3018
3019 /* Handles the MOVEM to memory from general register instruction. */
3020
3021 static void
3022 move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
3023 {
3024 if (inst_env->prefix_found)
3025 {
3026 /* The assign value is the value after the increment. Normally, the
3027 assign value is the value before the increment. */
3028 if ((cris_get_operand1 (inst) == REG_PC)
3029 && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
3030 {
3031 /* The prefix handles the problem if we are in a delay slot. */
3032 inst_env->reg[REG_PC] = inst_env->prefix_value;
3033 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3034 }
3035 }
3036 else
3037 {
3038 /* The increment is not depending on the size, instead it's depending
3039 on the number of registers loaded to memory. */
3040 if ((cris_get_operand1 (inst) == REG_PC)
3041 && (cris_get_mode (inst) == AUTOINC_MODE))
3042 {
3043 /* It's invalid to change the PC in a delay slot. */
3044 if (inst_env->slot_needed)
3045 {
3046 inst_env->invalid = 1;
3047 return;
3048 }
3049 inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
3050 }
3051 }
3052 inst_env->slot_needed = 0;
3053 inst_env->prefix_found = 0;
3054 inst_env->xflag_found = 0;
3055 inst_env->disable_interrupt = 0;
3056 }
3057
3058 /* Handles the instructions that's not yet implemented, by setting
3059 inst_env->invalid to true. */
3060
3061 static void
3062 not_implemented_op (unsigned short inst, inst_env_type *inst_env)
3063 {
3064 inst_env->invalid = 1;
3065 }
3066
3067 /* Handles the XOR instruction. */
3068
3069 static void
3070 xor_op (unsigned short inst, inst_env_type *inst_env)
3071 {
3072 /* XOR can't have a prefix. */
3073 if (inst_env->prefix_found)
3074 {
3075 inst_env->invalid = 1;
3076 return;
3077 }
3078
3079 /* Check if the PC is the target. */
3080 if (cris_get_operand2 (inst) == REG_PC)
3081 {
3082 /* It's invalid to change the PC in a delay slot. */
3083 if (inst_env->slot_needed)
3084 {
3085 inst_env->invalid = 1;
3086 return;
3087 }
3088 inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
3089 }
3090 inst_env->slot_needed = 0;
3091 inst_env->prefix_found = 0;
3092 inst_env->xflag_found = 0;
3093 inst_env->disable_interrupt = 0;
3094 }
3095
3096 /* Handles the MULS instruction. */
3097
3098 static void
3099 muls_op (unsigned short inst, inst_env_type *inst_env)
3100 {
3101 /* MULS/U can't have a prefix. */
3102 if (inst_env->prefix_found)
3103 {
3104 inst_env->invalid = 1;
3105 return;
3106 }
3107
3108 /* Consider it invalid if the PC is the target. */
3109 if (cris_get_operand2 (inst) == REG_PC)
3110 {
3111 inst_env->invalid = 1;
3112 return;
3113 }
3114 inst_env->slot_needed = 0;
3115 inst_env->prefix_found = 0;
3116 inst_env->xflag_found = 0;
3117 inst_env->disable_interrupt = 0;
3118 }
3119
3120 /* Handles the MULU instruction. */
3121
3122 static void
3123 mulu_op (unsigned short inst, inst_env_type *inst_env)
3124 {
3125 /* MULS/U can't have a prefix. */
3126 if (inst_env->prefix_found)
3127 {
3128 inst_env->invalid = 1;
3129 return;
3130 }
3131
3132 /* Consider it invalid if the PC is the target. */
3133 if (cris_get_operand2 (inst) == REG_PC)
3134 {
3135 inst_env->invalid = 1;
3136 return;
3137 }
3138 inst_env->slot_needed = 0;
3139 inst_env->prefix_found = 0;
3140 inst_env->xflag_found = 0;
3141 inst_env->disable_interrupt = 0;
3142 }
3143
3144 /* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE.
3145 The MOVE instruction is the move from source to register. */
3146
3147 static void
3148 add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env,
3149 unsigned long source1, unsigned long source2)
3150 {
3151 unsigned long pc_mask;
3152 unsigned long operation_mask;
3153
3154 /* Find out how many bits the operation should apply to. */
3155 if (cris_get_size (inst) == INST_BYTE_SIZE)
3156 {
3157 pc_mask = 0xFFFFFF00;
3158 operation_mask = 0xFF;
3159 }
3160 else if (cris_get_size (inst) == INST_WORD_SIZE)
3161 {
3162 pc_mask = 0xFFFF0000;
3163 operation_mask = 0xFFFF;
3164 }
3165 else if (cris_get_size (inst) == INST_DWORD_SIZE)
3166 {
3167 pc_mask = 0x0;
3168 operation_mask = 0xFFFFFFFF;
3169 }
3170 else
3171 {
3172 /* The size is out of range. */
3173 inst_env->invalid = 1;
3174 return;
3175 }
3176
3177 /* The instruction just works on uw_operation_mask bits. */
3178 source2 &= operation_mask;
3179 source1 &= operation_mask;
3180
3181 /* Now calculate the result. The opcode's 3 first bits separates
3182 the different actions. */
3183 switch (cris_get_opcode (inst) & 7)
3184 {
3185 case 0: /* add */
3186 source1 += source2;
3187 break;
3188
3189 case 1: /* move */
3190 source1 = source2;
3191 break;
3192
3193 case 2: /* subtract */
3194 source1 -= source2;
3195 break;
3196
3197 case 3: /* compare */
3198 break;
3199
3200 case 4: /* and */
3201 source1 &= source2;
3202 break;
3203
3204 case 5: /* or */
3205 source1 |= source2;
3206 break;
3207
3208 default:
3209 inst_env->invalid = 1;
3210 return;
3211
3212 break;
3213 }
3214
3215 /* Make sure that the result doesn't contain more than the instruction
3216 size bits. */
3217 source2 &= operation_mask;
3218
3219 /* Calculate the new breakpoint address. */
3220 inst_env->reg[REG_PC] &= pc_mask;
3221 inst_env->reg[REG_PC] |= source1;
3222
3223 }
3224
3225 /* Extends the value from either byte or word size to a dword. If the mode
3226 is zero extend then the value is extended with zero. If instead the mode
3227 is signed extend the sign bit of the value is taken into consideration. */
3228
3229 static unsigned long
3230 do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
3231 {
3232 /* The size can be either byte or word, check which one it is.
3233 Don't check the highest bit, it's indicating if it's a zero
3234 or sign extend. */
3235 if (cris_get_size (*inst) & INST_WORD_SIZE)
3236 {
3237 /* Word size. */
3238 value &= 0xFFFF;
3239
3240 /* Check if the instruction is signed extend. If so, check if value has
3241 the sign bit on. */
3242 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
3243 {
3244 value |= SIGNED_WORD_EXTEND_MASK;
3245 }
3246 }
3247 else
3248 {
3249 /* Byte size. */
3250 value &= 0xFF;
3251
3252 /* Check if the instruction is signed extend. If so, check if value has
3253 the sign bit on. */
3254 if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
3255 {
3256 value |= SIGNED_BYTE_EXTEND_MASK;
3257 }
3258 }
3259 /* The size should now be dword. */
3260 cris_set_size_to_dword (inst);
3261 return value;
3262 }
3263
3264 /* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
3265 instruction. The MOVE instruction is the move from source to register. */
3266
3267 static void
3268 reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3269 inst_env_type *inst_env)
3270 {
3271 unsigned long operand1;
3272 unsigned long operand2;
3273
3274 /* It's invalid to have a prefix to the instruction. This is a register
3275 mode instruction and can't have a prefix. */
3276 if (inst_env->prefix_found)
3277 {
3278 inst_env->invalid = 1;
3279 return;
3280 }
3281 /* Check if the instruction has PC as its target. */
3282 if (cris_get_operand2 (inst) == REG_PC)
3283 {
3284 if (inst_env->slot_needed)
3285 {
3286 inst_env->invalid = 1;
3287 return;
3288 }
3289 /* The instruction has the PC as its target register. */
3290 operand1 = inst_env->reg[cris_get_operand1 (inst)];
3291 operand2 = inst_env->reg[REG_PC];
3292
3293 /* Check if it's a extend, signed or zero instruction. */
3294 if (cris_get_opcode (inst) < 4)
3295 {
3296 operand1 = do_sign_or_zero_extend (operand1, &inst);
3297 }
3298 /* Calculate the PC value after the instruction, i.e. where the
3299 breakpoint should be. The order of the udw_operands is vital. */
3300 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3301 }
3302 inst_env->slot_needed = 0;
3303 inst_env->prefix_found = 0;
3304 inst_env->xflag_found = 0;
3305 inst_env->disable_interrupt = 0;
3306 }
3307
3308 /* Returns the data contained at address. The size of the data is derived from
3309 the size of the operation. If the instruction is a zero or signed
3310 extend instruction, the size field is changed in instruction. */
3311
3312 static unsigned long
3313 get_data_from_address (unsigned short *inst, CORE_ADDR address,
3314 enum bfd_endian byte_order)
3315 {
3316 int size = cris_get_size (*inst);
3317 unsigned long value;
3318
3319 /* If it's an extend instruction we don't want the signed extend bit,
3320 because it influences the size. */
3321 if (cris_get_opcode (*inst) < 4)
3322 {
3323 size &= ~SIGNED_EXTEND_BIT_MASK;
3324 }
3325 /* Is there a need for checking the size? Size should contain the number of
3326 bytes to read. */
3327 size = 1 << size;
3328 value = read_memory_unsigned_integer (address, size, byte_order);
3329
3330 /* Check if it's an extend, signed or zero instruction. */
3331 if (cris_get_opcode (*inst) < 4)
3332 {
3333 value = do_sign_or_zero_extend (value, inst);
3334 }
3335 return value;
3336 }
3337
3338 /* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3339 instructions. The MOVE instruction is the move from source to register. */
3340
3341 static void
3342 handle_prefix_assign_mode_for_aritm_op (unsigned short inst,
3343 inst_env_type *inst_env)
3344 {
3345 unsigned long operand2;
3346 unsigned long operand3;
3347
3348 check_assign (inst, inst_env);
3349 if (cris_get_operand2 (inst) == REG_PC)
3350 {
3351 operand2 = inst_env->reg[REG_PC];
3352
3353 /* Get the value of the third operand. */
3354 operand3 = get_data_from_address (&inst, inst_env->prefix_value,
3355 inst_env->byte_order);
3356
3357 /* Calculate the PC value after the instruction, i.e. where the
3358 breakpoint should be. The order of the udw_operands is vital. */
3359 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3360 }
3361 inst_env->slot_needed = 0;
3362 inst_env->prefix_found = 0;
3363 inst_env->xflag_found = 0;
3364 inst_env->disable_interrupt = 0;
3365 }
3366
3367 /* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
3368 OR instructions. Note that for this to work as expected, the calling
3369 function must have made sure that there is a prefix to this instruction. */
3370
3371 static void
3372 three_operand_add_sub_cmp_and_or_op (unsigned short inst,
3373 inst_env_type *inst_env)
3374 {
3375 unsigned long operand2;
3376 unsigned long operand3;
3377
3378 if (cris_get_operand1 (inst) == REG_PC)
3379 {
3380 /* The PC will be changed by the instruction. */
3381 operand2 = inst_env->reg[cris_get_operand2 (inst)];
3382
3383 /* Get the value of the third operand. */
3384 operand3 = get_data_from_address (&inst, inst_env->prefix_value,
3385 inst_env->byte_order);
3386
3387 /* Calculate the PC value after the instruction, i.e. where the
3388 breakpoint should be. */
3389 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3390 }
3391 inst_env->slot_needed = 0;
3392 inst_env->prefix_found = 0;
3393 inst_env->xflag_found = 0;
3394 inst_env->disable_interrupt = 0;
3395 }
3396
3397 /* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3398 instructions. The MOVE instruction is the move from source to register. */
3399
3400 static void
3401 handle_prefix_index_mode_for_aritm_op (unsigned short inst,
3402 inst_env_type *inst_env)
3403 {
3404 if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
3405 {
3406 /* If the instruction is MOVE it's invalid. If the instruction is ADD,
3407 SUB, AND or OR something weird is going on (if everything works these
3408 instructions should end up in the three operand version). */
3409 inst_env->invalid = 1;
3410 return;
3411 }
3412 else
3413 {
3414 /* three_operand_add_sub_cmp_and_or does the same as we should do here
3415 so use it. */
3416 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3417 }
3418 inst_env->slot_needed = 0;
3419 inst_env->prefix_found = 0;
3420 inst_env->xflag_found = 0;
3421 inst_env->disable_interrupt = 0;
3422 }
3423
3424 /* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
3425 CMP, AND OR and MOVE instruction. The MOVE instruction is the move from
3426 source to register. */
3427
3428 static void
3429 handle_inc_and_index_mode_for_aritm_op (unsigned short inst,
3430 inst_env_type *inst_env)
3431 {
3432 unsigned long operand1;
3433 unsigned long operand2;
3434 unsigned long operand3;
3435 int size;
3436
3437 /* The instruction is either an indirect or autoincrement addressing mode.
3438 Check if the destination register is the PC. */
3439 if (cris_get_operand2 (inst) == REG_PC)
3440 {
3441 /* Must be done here, get_data_from_address may change the size
3442 field. */
3443 size = cris_get_size (inst);
3444 operand2 = inst_env->reg[REG_PC];
3445
3446 /* Get the value of the third operand, i.e. the indirect operand. */
3447 operand1 = inst_env->reg[cris_get_operand1 (inst)];
3448 operand3 = get_data_from_address (&inst, operand1, inst_env->byte_order);
3449
3450 /* Calculate the PC value after the instruction, i.e. where the
3451 breakpoint should be. The order of the udw_operands is vital. */
3452 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3453 }
3454 /* If this is an autoincrement addressing mode, check if the increment
3455 changes the PC. */
3456 if ((cris_get_operand1 (inst) == REG_PC)
3457 && (cris_get_mode (inst) == AUTOINC_MODE))
3458 {
3459 /* Get the size field. */
3460 size = cris_get_size (inst);
3461
3462 /* If it's an extend instruction we don't want the signed extend bit,
3463 because it influences the size. */
3464 if (cris_get_opcode (inst) < 4)
3465 {
3466 size &= ~SIGNED_EXTEND_BIT_MASK;
3467 }
3468 process_autoincrement (size, inst, inst_env);
3469 }
3470 inst_env->slot_needed = 0;
3471 inst_env->prefix_found = 0;
3472 inst_env->xflag_found = 0;
3473 inst_env->disable_interrupt = 0;
3474 }
3475
3476 /* Handles the two-operand addressing mode, all modes except register, for
3477 the ADD, SUB CMP, AND and OR instruction. */
3478
3479 static void
3480 none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3481 inst_env_type *inst_env)
3482 {
3483 if (inst_env->prefix_found)
3484 {
3485 if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
3486 {
3487 handle_prefix_index_mode_for_aritm_op (inst, inst_env);
3488 }
3489 else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
3490 {
3491 handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
3492 }
3493 else
3494 {
3495 /* The mode is invalid for a prefixed base instruction. */
3496 inst_env->invalid = 1;
3497 return;
3498 }
3499 }
3500 else
3501 {
3502 handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
3503 }
3504 }
3505
3506 /* Handles the quick addressing mode for the ADD and SUB instruction. */
3507
3508 static void
3509 quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
3510 {
3511 unsigned long operand1;
3512 unsigned long operand2;
3513
3514 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3515 instruction and can't have a prefix. */
3516 if (inst_env->prefix_found)
3517 {
3518 inst_env->invalid = 1;
3519 return;
3520 }
3521
3522 /* Check if the instruction has PC as its target. */
3523 if (cris_get_operand2 (inst) == REG_PC)
3524 {
3525 if (inst_env->slot_needed)
3526 {
3527 inst_env->invalid = 1;
3528 return;
3529 }
3530 operand1 = cris_get_quick_value (inst);
3531 operand2 = inst_env->reg[REG_PC];
3532
3533 /* The size should now be dword. */
3534 cris_set_size_to_dword (&inst);
3535
3536 /* Calculate the PC value after the instruction, i.e. where the
3537 breakpoint should be. */
3538 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3539 }
3540 inst_env->slot_needed = 0;
3541 inst_env->prefix_found = 0;
3542 inst_env->xflag_found = 0;
3543 inst_env->disable_interrupt = 0;
3544 }
3545
3546 /* Handles the quick addressing mode for the CMP, AND and OR instruction. */
3547
3548 static void
3549 quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
3550 {
3551 unsigned long operand1;
3552 unsigned long operand2;
3553
3554 /* It's a bad idea to be in a prefix instruction now. This is a quick mode
3555 instruction and can't have a prefix. */
3556 if (inst_env->prefix_found)
3557 {
3558 inst_env->invalid = 1;
3559 return;
3560 }
3561 /* Check if the instruction has PC as its target. */
3562 if (cris_get_operand2 (inst) == REG_PC)
3563 {
3564 if (inst_env->slot_needed)
3565 {
3566 inst_env->invalid = 1;
3567 return;
3568 }
3569 /* The instruction has the PC as its target register. */
3570 operand1 = cris_get_quick_value (inst);
3571 operand2 = inst_env->reg[REG_PC];
3572
3573 /* The quick value is signed, so check if we must do a signed extend. */
3574 if (operand1 & SIGNED_QUICK_VALUE_MASK)
3575 {
3576 /* sign extend */
3577 operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
3578 }
3579 /* The size should now be dword. */
3580 cris_set_size_to_dword (&inst);
3581
3582 /* Calculate the PC value after the instruction, i.e. where the
3583 breakpoint should be. */
3584 add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3585 }
3586 inst_env->slot_needed = 0;
3587 inst_env->prefix_found = 0;
3588 inst_env->xflag_found = 0;
3589 inst_env->disable_interrupt = 0;
3590 }
3591
3592 /* Translate op_type to a function and call it. */
3593
3594 static void
3595 cris_gdb_func (struct gdbarch *gdbarch, enum cris_op_type op_type,
3596 unsigned short inst, inst_env_type *inst_env)
3597 {
3598 switch (op_type)
3599 {
3600 case cris_not_implemented_op:
3601 not_implemented_op (inst, inst_env);
3602 break;
3603
3604 case cris_abs_op:
3605 abs_op (inst, inst_env);
3606 break;
3607
3608 case cris_addi_op:
3609 addi_op (inst, inst_env);
3610 break;
3611
3612 case cris_asr_op:
3613 asr_op (inst, inst_env);
3614 break;
3615
3616 case cris_asrq_op:
3617 asrq_op (inst, inst_env);
3618 break;
3619
3620 case cris_ax_ei_setf_op:
3621 ax_ei_setf_op (inst, inst_env);
3622 break;
3623
3624 case cris_bdap_prefix:
3625 bdap_prefix (inst, inst_env);
3626 break;
3627
3628 case cris_biap_prefix:
3629 biap_prefix (inst, inst_env);
3630 break;
3631
3632 case cris_break_op:
3633 break_op (inst, inst_env);
3634 break;
3635
3636 case cris_btst_nop_op:
3637 btst_nop_op (inst, inst_env);
3638 break;
3639
3640 case cris_clearf_di_op:
3641 clearf_di_op (inst, inst_env);
3642 break;
3643
3644 case cris_dip_prefix:
3645 dip_prefix (inst, inst_env);
3646 break;
3647
3648 case cris_dstep_logshift_mstep_neg_not_op:
3649 dstep_logshift_mstep_neg_not_op (inst, inst_env);
3650 break;
3651
3652 case cris_eight_bit_offset_branch_op:
3653 eight_bit_offset_branch_op (inst, inst_env);
3654 break;
3655
3656 case cris_move_mem_to_reg_movem_op:
3657 move_mem_to_reg_movem_op (inst, inst_env);
3658 break;
3659
3660 case cris_move_reg_to_mem_movem_op:
3661 move_reg_to_mem_movem_op (inst, inst_env);
3662 break;
3663
3664 case cris_move_to_preg_op:
3665 move_to_preg_op (gdbarch, inst, inst_env);
3666 break;
3667
3668 case cris_muls_op:
3669 muls_op (inst, inst_env);
3670 break;
3671
3672 case cris_mulu_op:
3673 mulu_op (inst, inst_env);
3674 break;
3675
3676 case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
3677 none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3678 break;
3679
3680 case cris_none_reg_mode_clear_test_op:
3681 none_reg_mode_clear_test_op (inst, inst_env);
3682 break;
3683
3684 case cris_none_reg_mode_jump_op:
3685 none_reg_mode_jump_op (inst, inst_env);
3686 break;
3687
3688 case cris_none_reg_mode_move_from_preg_op:
3689 none_reg_mode_move_from_preg_op (gdbarch, inst, inst_env);
3690 break;
3691
3692 case cris_quick_mode_add_sub_op:
3693 quick_mode_add_sub_op (inst, inst_env);
3694 break;
3695
3696 case cris_quick_mode_and_cmp_move_or_op:
3697 quick_mode_and_cmp_move_or_op (inst, inst_env);
3698 break;
3699
3700 case cris_quick_mode_bdap_prefix:
3701 quick_mode_bdap_prefix (inst, inst_env);
3702 break;
3703
3704 case cris_reg_mode_add_sub_cmp_and_or_move_op:
3705 reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3706 break;
3707
3708 case cris_reg_mode_clear_op:
3709 reg_mode_clear_op (inst, inst_env);
3710 break;
3711
3712 case cris_reg_mode_jump_op:
3713 reg_mode_jump_op (inst, inst_env);
3714 break;
3715
3716 case cris_reg_mode_move_from_preg_op:
3717 reg_mode_move_from_preg_op (inst, inst_env);
3718 break;
3719
3720 case cris_reg_mode_test_op:
3721 reg_mode_test_op (inst, inst_env);
3722 break;
3723
3724 case cris_scc_op:
3725 scc_op (inst, inst_env);
3726 break;
3727
3728 case cris_sixteen_bit_offset_branch_op:
3729 sixteen_bit_offset_branch_op (inst, inst_env);
3730 break;
3731
3732 case cris_three_operand_add_sub_cmp_and_or_op:
3733 three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3734 break;
3735
3736 case cris_three_operand_bound_op:
3737 three_operand_bound_op (inst, inst_env);
3738 break;
3739
3740 case cris_two_operand_bound_op:
3741 two_operand_bound_op (inst, inst_env);
3742 break;
3743
3744 case cris_xor_op:
3745 xor_op (inst, inst_env);
3746 break;
3747 }
3748 }
3749
3750 /* Originally from <asm/elf.h>. */
3751 typedef unsigned char cris_elf_greg_t[4];
3752
3753 /* Same as user_regs_struct struct in <asm/user.h>. */
3754 #define CRISV10_ELF_NGREG 35
3755 typedef cris_elf_greg_t cris_elf_gregset_t[CRISV10_ELF_NGREG];
3756
3757 #define CRISV32_ELF_NGREG 32
3758 typedef cris_elf_greg_t crisv32_elf_gregset_t[CRISV32_ELF_NGREG];
3759
3760 /* Unpack a cris_elf_gregset_t into GDB's register cache. */
3761
3762 static void
3763 cris_supply_gregset (const struct regset *regset, struct regcache *regcache,
3764 int regnum, const void *gregs, size_t len)
3765 {
3766 struct gdbarch *gdbarch = regcache->arch ();
3767 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3768 int i;
3769 const cris_elf_greg_t *regp = static_cast<const cris_elf_greg_t *>(gregs);
3770
3771 if (len != sizeof (cris_elf_gregset_t)
3772 && len != sizeof (crisv32_elf_gregset_t))
3773 warning (_("wrong size gregset struct in core file"));
3774 gdb_assert (len >= sizeof (crisv32_elf_gregset_t));
3775
3776 /* The kernel dumps all 32 registers as unsigned longs, but supply_register
3777 knows about the actual size of each register so that's no problem. */
3778 for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
3779 {
3780 if (regnum == -1 || regnum == i)
3781 regcache->raw_supply (i, (char *)&regp[i]);
3782 }
3783
3784 if (tdep->cris_version == 32 && (regnum == -1 || regnum == ERP_REGNUM))
3785 {
3786 /* Needed to set pseudo-register PC for CRISv32. */
3787 /* FIXME: If ERP is in a delay slot at this point then the PC will
3788 be wrong. Issue a warning to alert the user. */
3789 regcache->raw_supply (gdbarch_pc_regnum (gdbarch),
3790 (char *)&regp[ERP_REGNUM]);
3791
3792 if (*(char *)&regp[ERP_REGNUM] & 0x1)
3793 fprintf_unfiltered (gdb_stderr, "Warning: PC in delay slot\n");
3794 }
3795 }
3796
3797 static const struct regset cris_regset = {
3798 nullptr,
3799 cris_supply_gregset,
3800 /* We don't need a collect function because we only use this for core files
3801 (via iterate_over_regset_sections). */
3802 nullptr,
3803 REGSET_VARIABLE_SIZE
3804 };
3805
3806 static void cris_iterate_over_regset_sections (struct gdbarch *gdbarch,
3807 iterate_over_regset_sections_cb *cb,
3808 void *cb_data,
3809 const struct regcache *regcache)
3810 {
3811 cb (".reg", sizeof (crisv32_elf_gregset_t), sizeof (crisv32_elf_gregset_t),
3812 &cris_regset, NULL, cb_data);
3813 }
3814
3815 void _initialize_cris_tdep ();
3816 void
3817 _initialize_cris_tdep ()
3818 {
3819 gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);
3820
3821 /* CRIS-specific user-commands. */
3822 add_setshow_zuinteger_cmd ("cris-version", class_support,
3823 &usr_cmd_cris_version,
3824 _("Set the current CRIS version."),
3825 _("Show the current CRIS version."),
3826 _("\
3827 Set to 10 for CRISv10 or 32 for CRISv32 if autodetection fails.\n\
3828 Defaults to 10. "),
3829 set_cris_version,
3830 NULL, /* FIXME: i18n: Current CRIS version
3831 is %s. */
3832 &setlist, &showlist);
3833
3834 add_setshow_enum_cmd ("cris-mode", class_support,
3835 cris_modes, &usr_cmd_cris_mode,
3836 _("Set the current CRIS mode."),
3837 _("Show the current CRIS mode."),
3838 _("\
3839 Set to CRIS_MODE_GURU when debugging in guru mode.\n\
3840 Makes GDB use the NRP register instead of the ERP register in certain cases."),
3841 set_cris_mode,
3842 NULL, /* FIXME: i18n: Current CRIS version is %s. */
3843 &setlist, &showlist);
3844
3845 add_setshow_boolean_cmd ("cris-dwarf2-cfi", class_support,
3846 &usr_cmd_cris_dwarf2_cfi,
3847 _("Set the usage of Dwarf-2 CFI for CRIS."),
3848 _("Show the usage of Dwarf-2 CFI for CRIS."),
3849 _("Set this to \"off\" if using gcc-cris < R59."),
3850 set_cris_dwarf2_cfi,
3851 NULL, /* FIXME: i18n: Usage of Dwarf-2 CFI
3852 for CRIS is %d. */
3853 &setlist, &showlist);
3854 }
3855
3856 /* Prints out all target specific values. */
3857
3858 static void
3859 cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3860 {
3861 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3862 if (tdep != NULL)
3863 {
3864 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_version = %i\n",
3865 tdep->cris_version);
3866 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
3867 tdep->cris_mode);
3868 fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_dwarf2_cfi = %i\n",
3869 tdep->cris_dwarf2_cfi);
3870 }
3871 }
3872
3873 static void
3874 set_cris_version (const char *ignore_args, int from_tty,
3875 struct cmd_list_element *c)
3876 {
3877 struct gdbarch_info info;
3878
3879 usr_cmd_cris_version_valid = 1;
3880
3881 /* Update the current architecture, if needed. */
3882 gdbarch_info_init (&info);
3883 if (!gdbarch_update_p (info))
3884 internal_error (__FILE__, __LINE__,
3885 _("cris_gdbarch_update: failed to update architecture."));
3886 }
3887
3888 static void
3889 set_cris_mode (const char *ignore_args, int from_tty,
3890 struct cmd_list_element *c)
3891 {
3892 struct gdbarch_info info;
3893
3894 /* Update the current architecture, if needed. */
3895 gdbarch_info_init (&info);
3896 if (!gdbarch_update_p (info))
3897 internal_error (__FILE__, __LINE__,
3898 "cris_gdbarch_update: failed to update architecture.");
3899 }
3900
3901 static void
3902 set_cris_dwarf2_cfi (const char *ignore_args, int from_tty,
3903 struct cmd_list_element *c)
3904 {
3905 struct gdbarch_info info;
3906
3907 /* Update the current architecture, if needed. */
3908 gdbarch_info_init (&info);
3909 if (!gdbarch_update_p (info))
3910 internal_error (__FILE__, __LINE__,
3911 _("cris_gdbarch_update: failed to update architecture."));
3912 }
3913
3914 static struct gdbarch *
3915 cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3916 {
3917 struct gdbarch *gdbarch;
3918 struct gdbarch_tdep *tdep;
3919 unsigned int cris_version;
3920
3921 if (usr_cmd_cris_version_valid)
3922 {
3923 /* Trust the user's CRIS version setting. */
3924 cris_version = usr_cmd_cris_version;
3925 }
3926 else if (info.abfd && bfd_get_mach (info.abfd) == bfd_mach_cris_v32)
3927 {
3928 cris_version = 32;
3929 }
3930 else
3931 {
3932 /* Assume it's CRIS version 10. */
3933 cris_version = 10;
3934 }
3935
3936 /* Make the current settings visible to the user. */
3937 usr_cmd_cris_version = cris_version;
3938
3939 /* Find a candidate among the list of pre-declared architectures. */
3940 for (arches = gdbarch_list_lookup_by_info (arches, &info);
3941 arches != NULL;
3942 arches = gdbarch_list_lookup_by_info (arches->next, &info))
3943 {
3944 if ((gdbarch_tdep (arches->gdbarch)->cris_version
3945 == usr_cmd_cris_version)
3946 && (gdbarch_tdep (arches->gdbarch)->cris_mode
3947 == usr_cmd_cris_mode)
3948 && (gdbarch_tdep (arches->gdbarch)->cris_dwarf2_cfi
3949 == usr_cmd_cris_dwarf2_cfi))
3950 return arches->gdbarch;
3951 }
3952
3953 /* No matching architecture was found. Create a new one. */
3954 tdep = XCNEW (struct gdbarch_tdep);
3955 info.byte_order = BFD_ENDIAN_LITTLE;
3956 gdbarch = gdbarch_alloc (&info, tdep);
3957
3958 tdep->cris_version = usr_cmd_cris_version;
3959 tdep->cris_mode = usr_cmd_cris_mode;
3960 tdep->cris_dwarf2_cfi = usr_cmd_cris_dwarf2_cfi;
3961
3962 set_gdbarch_return_value (gdbarch, cris_return_value);
3963 set_gdbarch_sp_regnum (gdbarch, 14);
3964
3965 /* Length of ordinary registers used in push_word and a few other
3966 places. register_size() is the real way to know how big a
3967 register is. */
3968
3969 set_gdbarch_double_bit (gdbarch, 64);
3970 /* The default definition of a long double is 2 * gdbarch_double_bit,
3971 which means we have to set this explicitly. */
3972 set_gdbarch_long_double_bit (gdbarch, 64);
3973
3974 /* The total amount of space needed to store (in an array called registers)
3975 GDB's copy of the machine's register state. Note: We can not use
3976 cris_register_size at this point, since it relies on gdbarch
3977 being set. */
3978 switch (tdep->cris_version)
3979 {
3980 case 0:
3981 case 1:
3982 case 2:
3983 case 3:
3984 case 8:
3985 case 9:
3986 /* Old versions; not supported. */
3987 return 0;
3988
3989 case 10:
3990 case 11:
3991 /* CRIS v10 and v11, a.k.a. ETRAX 100LX. In addition to ETRAX 100,
3992 P7 (32 bits), and P15 (32 bits) have been implemented. */
3993 set_gdbarch_pc_regnum (gdbarch, 15);
3994 set_gdbarch_register_type (gdbarch, cris_register_type);
3995 /* There are 32 registers (some of which may not be implemented). */
3996 set_gdbarch_num_regs (gdbarch, 32);
3997 set_gdbarch_register_name (gdbarch, cris_register_name);
3998 set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
3999 set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);
4000
4001 set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
4002 break;
4003
4004 case 32:
4005 /* CRIS v32. General registers R0 - R15 (32 bits), special registers
4006 P0 - P15 (32 bits) except P0, P1, P3 (8 bits) and P4 (16 bits)
4007 and pseudo-register PC (32 bits). */
4008 set_gdbarch_pc_regnum (gdbarch, 32);
4009 set_gdbarch_register_type (gdbarch, crisv32_register_type);
4010 /* 32 registers + pseudo-register PC + 16 support registers. */
4011 set_gdbarch_num_regs (gdbarch, 32 + 1 + 16);
4012 set_gdbarch_register_name (gdbarch, crisv32_register_name);
4013
4014 set_gdbarch_cannot_store_register
4015 (gdbarch, crisv32_cannot_store_register);
4016 set_gdbarch_cannot_fetch_register
4017 (gdbarch, crisv32_cannot_fetch_register);
4018
4019 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
4020
4021 set_gdbarch_single_step_through_delay
4022 (gdbarch, crisv32_single_step_through_delay);
4023
4024 break;
4025
4026 default:
4027 /* Unknown version. */
4028 return 0;
4029 }
4030
4031 /* Dummy frame functions (shared between CRISv10 and CRISv32 since they
4032 have the same ABI). */
4033 set_gdbarch_push_dummy_code (gdbarch, cris_push_dummy_code);
4034 set_gdbarch_push_dummy_call (gdbarch, cris_push_dummy_call);
4035 set_gdbarch_frame_align (gdbarch, cris_frame_align);
4036 set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);
4037
4038 /* The stack grows downward. */
4039 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4040
4041 set_gdbarch_breakpoint_kind_from_pc (gdbarch, cris_breakpoint_kind_from_pc);
4042 set_gdbarch_sw_breakpoint_from_kind (gdbarch, cris_sw_breakpoint_from_kind);
4043 set_gdbarch_iterate_over_regset_sections (gdbarch, cris_iterate_over_regset_sections);
4044
4045 if (tdep->cris_dwarf2_cfi == 1)
4046 {
4047 /* Hook in the Dwarf-2 frame sniffer. */
4048 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, cris_dwarf2_reg_to_regnum);
4049 dwarf2_frame_set_init_reg (gdbarch, cris_dwarf2_frame_init_reg);
4050 dwarf2_append_unwinders (gdbarch);
4051 }
4052
4053 if (tdep->cris_mode != cris_mode_guru)
4054 {
4055 frame_unwind_append_unwinder (gdbarch, &cris_sigtramp_frame_unwind);
4056 }
4057
4058 frame_unwind_append_unwinder (gdbarch, &cris_frame_unwind);
4059 frame_base_set_default (gdbarch, &cris_frame_base);
4060
4061 /* Hook in ABI-specific overrides, if they have been registered. */
4062 gdbarch_init_osabi (info, gdbarch);
4063
4064 return gdbarch;
4065 }