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