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