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