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