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