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