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