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