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