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