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