OSDN Git Service

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