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