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