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