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