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