OSDN Git Service

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