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