]> Git Repo - binutils.git/blob - gdb/cris-tdep.c
* configure: Regenerate with proper autoconf 2.13.
[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   fi->extra_info->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               fi->extra_info->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 (fi->extra_info->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 either frame_saved_regs_zalloc
1152    or frame_obstack_alloc.  */
1153
1154 void
1155 cris_frame_init_saved_regs (struct frame_info *fi)
1156 {
1157   CORE_ADDR ip;
1158   struct symtab_and_line sal;
1159   int best_limit;
1160   char *dummy_regs = deprecated_generic_find_dummy_frame (get_frame_pc (fi), fi->frame);
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 (&fi->saved_regs, dummy_regs, sizeof (fi->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 (fi->next)
1204     {
1205       /* Called from get_prev_frame.  */
1206       deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (fi->next));
1207     }
1208  
1209   frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
1210  
1211   fi->extra_info->return_pc = 0;
1212   fi->extra_info->leaf_function = 0;
1213
1214   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
1215     {    
1216       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
1217          by assuming it's always FP.  */
1218       deprecated_update_frame_base_hack (fi, deprecated_read_register_dummy (get_frame_pc (fi), fi->frame,
1219                                                                              SP_REGNUM));
1220       fi->extra_info->return_pc = 
1221         deprecated_read_register_dummy (get_frame_pc (fi), fi->frame, PC_REGNUM);
1222
1223       /* FIXME: Is this necessarily true?  */
1224       fi->extra_info->leaf_function = 0;
1225     }
1226   else
1227     {
1228       cris_frame_init_saved_regs (fi);
1229
1230       /* Check fromleaf/frameless_function_invocation.  (FIXME)  */
1231
1232       if (get_frame_saved_regs (fi)[SRP_REGNUM] != 0)
1233         {
1234           /* SRP was saved on the stack; non-leaf function.  */
1235           fi->extra_info->return_pc =
1236             read_memory_integer (get_frame_saved_regs (fi)[SRP_REGNUM], 
1237                                  REGISTER_RAW_SIZE (SRP_REGNUM));
1238         }
1239       else
1240         {
1241           /* SRP is still in a register; leaf function.  */
1242           fi->extra_info->return_pc = read_register (SRP_REGNUM);
1243           /* FIXME: Should leaf_function be set to 1 here?  */
1244           fi->extra_info->leaf_function = 1;
1245         }
1246     }
1247 }
1248
1249 /* Return the content of the frame pointer in the present frame.  In other
1250    words, determine the address of the calling function's frame.  */
1251
1252 CORE_ADDR
1253 cris_frame_chain (struct frame_info *fi)
1254 {
1255   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
1256     {
1257       return fi->frame;
1258     }
1259   else if (!inside_entry_file (get_frame_pc (fi)))
1260     {
1261       return read_memory_unsigned_integer (get_frame_base (fi), 4);
1262     }
1263   else
1264     {
1265       return 0;
1266     }
1267 }
1268
1269 /* Return the saved PC (which equals the return address) of this frame.  */
1270
1271 CORE_ADDR
1272 cris_frame_saved_pc (struct frame_info *fi)
1273 {
1274   return fi->extra_info->return_pc;
1275 }
1276
1277 /* Setup the function arguments for calling a function in the inferior.  */
1278
1279 CORE_ADDR 
1280 cris_abi_original_push_arguments (int nargs, struct value **args, 
1281                                   CORE_ADDR sp, int struct_return, 
1282                                   CORE_ADDR struct_addr)
1283 {
1284   int stack_alloc;
1285   int stack_offset;
1286   int argreg;
1287   int argnum;
1288   struct type *type;
1289   int len;
1290   CORE_ADDR regval;
1291   char *val;
1292
1293   /* Data and parameters reside in different areas on the stack. 
1294      Both frame pointers grow toward higher addresses.  */  
1295   CORE_ADDR fp_params;
1296   CORE_ADDR fp_data;
1297   
1298   /* Are we returning a value using a structure return or a normal value 
1299      return?  struct_addr is the address of the reserved space for the return 
1300      structure to be written on the stack.  */
1301   if (struct_return)
1302     {
1303       write_register (STR_REGNUM, struct_addr);
1304     }
1305
1306   /* Make sure there's space on the stack.  Allocate space for data and a 
1307      parameter to refer to that data.  */
1308   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
1309     stack_alloc += (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + REGISTER_SIZE);
1310   sp -= stack_alloc;
1311   /* We may over-allocate a little here, but that won't hurt anything.  */
1312
1313   /* Initialize stack frame pointers.  */
1314   fp_params = sp;
1315   fp_data = sp + (nargs * REGISTER_SIZE);
1316
1317   /* Now load as many as possible of the first arguments into
1318      registers, and push the rest onto the stack.  */
1319   argreg = ARG1_REGNUM; 
1320   stack_offset = 0;
1321
1322   for (argnum = 0; argnum < nargs; argnum++)
1323     {
1324       type = VALUE_TYPE (args[argnum]);
1325       len = TYPE_LENGTH (type);
1326       val = (char *) VALUE_CONTENTS (args[argnum]);
1327     
1328       if (len <= REGISTER_SIZE && argreg <= ARG4_REGNUM)
1329         {
1330           /* Data fits in a register; put it in the first available 
1331              register.  */
1332           write_register (argreg, *(unsigned long *) val);
1333           argreg++;
1334         }
1335       else if (len > REGISTER_SIZE && argreg <= ARG4_REGNUM)
1336         {
1337           /* Data does not fit in register; pass it on the stack and
1338              put its address in the first available register.  */
1339           write_memory (fp_data, val, len);
1340           write_register (argreg, fp_data);
1341           fp_data += len;
1342           argreg++;      
1343         }
1344       else if (len > REGISTER_SIZE)
1345         {
1346           /* Data does not fit in register; put both data and 
1347              parameter on the stack.  */
1348           write_memory (fp_data, val, len);
1349           write_memory (fp_params, (char *) (&fp_data), REGISTER_SIZE);
1350           fp_data += len;
1351           fp_params += REGISTER_SIZE;
1352         }
1353       else
1354         {
1355           /* Data fits in a register, but we are out of registers;
1356              put the parameter on the stack.  */
1357           write_memory (fp_params, val, REGISTER_SIZE);
1358           fp_params += REGISTER_SIZE;
1359         }
1360     }
1361
1362   return sp;
1363 }
1364
1365 CORE_ADDR 
1366 cris_abi_v2_push_arguments (int nargs, struct value **args, CORE_ADDR sp, 
1367                      int struct_return, CORE_ADDR struct_addr)
1368 {
1369   int stack_alloc;
1370   int stack_offset;
1371   int argreg;
1372   int argnum;
1373
1374   CORE_ADDR regval;
1375
1376   /* The function's arguments and memory allocated by gdb for the arguments to
1377      point at reside in separate areas on the stack.
1378      Both frame pointers grow toward higher addresses.  */
1379   CORE_ADDR fp_arg;
1380   CORE_ADDR fp_mem;
1381   
1382   /* Are we returning a value using a structure return or a normal value 
1383      return?  struct_addr is the address of the reserved space for the return 
1384      structure to be written on the stack.  */
1385   if (struct_return)
1386     {
1387       write_register (STR_REGNUM, struct_addr);
1388     }
1389
1390   /* Allocate enough to keep things word-aligned on both parts of the 
1391      stack.  */
1392   stack_alloc = 0;
1393   for (argnum = 0; argnum < nargs; argnum++)
1394     {
1395       int len;
1396       int reg_demand;
1397       
1398       len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1399       reg_demand = (len / REGISTER_SIZE) + (len % REGISTER_SIZE != 0 ? 1 : 0);
1400
1401       /* reg_demand * REGISTER_SIZE is the amount of memory we might need to
1402          allocate for this argument.  2 * REGISTER_SIZE is the amount of stack
1403          space we might need to pass the argument itself (either by value or by
1404          reference).  */
1405       stack_alloc += (reg_demand * REGISTER_SIZE + 2 * REGISTER_SIZE);
1406     }
1407   sp -= stack_alloc;
1408   /* We may over-allocate a little here, but that won't hurt anything.  */
1409
1410   /* Initialize frame pointers.  */
1411   fp_arg = sp;
1412   fp_mem = sp + (nargs * (2 * REGISTER_SIZE));
1413
1414   /* Now load as many as possible of the first arguments into registers,
1415      and push the rest onto the stack.  */
1416   argreg = ARG1_REGNUM; 
1417   stack_offset = 0;
1418
1419   for (argnum = 0; argnum < nargs; argnum++)
1420     {
1421       int len;
1422       char *val;
1423       int reg_demand;
1424       int i;
1425       
1426       len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
1427       val = (char *) VALUE_CONTENTS (args[argnum]);
1428       
1429       /* How may registers worth of storage do we need for this argument?  */
1430       reg_demand = (len / REGISTER_SIZE) + (len % REGISTER_SIZE != 0 ? 1 : 0);
1431         
1432       if (len <= (2 * REGISTER_SIZE)
1433           && (argreg + reg_demand - 1 <= ARG4_REGNUM)) 
1434         {
1435           /* Data passed by value.  Fits in available register(s).  */
1436           for (i = 0; i < reg_demand; i++)
1437             {
1438               write_register (argreg, *(unsigned long *) val);
1439               argreg++;
1440               val += REGISTER_SIZE;
1441             }
1442         }
1443       else if (len <= (2 * REGISTER_SIZE) && argreg <= ARG4_REGNUM)
1444         {
1445           /* Data passed by value. Does not fit in available register(s).  
1446              Use the register(s) first, then the stack.  */
1447           for (i = 0; i < reg_demand; i++)
1448             {
1449               if (argreg <= ARG4_REGNUM)
1450                 {
1451                   write_register (argreg, *(unsigned long *) val);
1452                   argreg++;
1453                   val += REGISTER_SIZE;
1454                 }
1455               else
1456                 {
1457                   /* I guess this memory write could write the remaining data
1458                      all at once instead of in REGISTER_SIZE chunks.  */
1459                   write_memory (fp_arg, val, REGISTER_SIZE);
1460                   fp_arg += REGISTER_SIZE;
1461                   val += REGISTER_SIZE;              
1462                 }
1463             }    
1464         }
1465       else if (len > (2 * REGISTER_SIZE))
1466         {
1467           /* Data passed by reference.  Put it on the stack.  */
1468           write_memory (fp_mem, val, len);
1469           write_memory (fp_arg, (char *) (&fp_mem), REGISTER_SIZE);
1470
1471           /* fp_mem need not be word-aligned since it's just a chunk of
1472              memory being pointed at.  That is, += len would do.  */
1473           fp_mem += reg_demand * REGISTER_SIZE;
1474           fp_arg += REGISTER_SIZE;
1475         }
1476       else
1477         {
1478           /* Data passed by value.  No available registers.  Put it on 
1479              the stack.  */
1480           write_memory (fp_arg, val, len);
1481
1482           /* fp_arg must be word-aligned (i.e., don't += len) to match
1483              the function prologue.  */
1484           fp_arg += reg_demand * REGISTER_SIZE;
1485         }
1486     }
1487
1488   return sp;
1489 }
1490
1491 /* Never put the return address on the stack.  The register SRP is pushed
1492    by the called function unless it is a leaf-function.  Due to the BRP
1493    register the PC will change when continue is sent.  */
1494
1495 CORE_ADDR
1496 cris_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
1497 {
1498   write_register (SRP_REGNUM, CALL_DUMMY_ADDRESS ());
1499   return sp;
1500 }
1501
1502 /* Restore the machine to the state it had before the current frame 
1503    was created.  Discard the innermost frame from the stack and restore 
1504    all saved registers.  */
1505
1506 void 
1507 cris_pop_frame (void)
1508 {
1509   register struct frame_info *fi = get_current_frame ();
1510   register int regno;
1511   register int stack_offset = 0;
1512   
1513   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), fi->frame, fi->frame))
1514     {
1515       /* This happens when we hit a breakpoint set at the entry point,
1516          when returning from a dummy frame.  */
1517       generic_pop_dummy_frame ();
1518     }
1519   else
1520     {
1521       cris_frame_init_saved_regs (fi);
1522
1523       /* For each register, the address of where it was saved on entry to
1524          the frame now lies in fi->saved_regs[regno], or zero if it was not 
1525          saved.  This includes special registers such as PC and FP saved in
1526          special ways in the stack frame.  The SP_REGNUM is even more
1527          special, the address here is the SP for the next frame, not the
1528          address where the SP was saved.  */
1529                                                      
1530       /* Restore general registers R0 - R7.  They were pushed on the stack 
1531          after SP was saved.  */
1532       for (regno = 0; regno < FP_REGNUM; regno++)
1533         {
1534           if (get_frame_saved_regs (fi)[regno])
1535             {
1536               write_register (regno, 
1537                               read_memory_integer (get_frame_saved_regs (fi)[regno], 4));
1538             }
1539         }
1540      
1541       if (get_frame_saved_regs (fi)[FP_REGNUM])
1542         {
1543           /* Pop the frame pointer (R8).  It was pushed before SP 
1544              was saved.  */
1545           write_register (FP_REGNUM, 
1546                           read_memory_integer (get_frame_saved_regs (fi)[FP_REGNUM], 4));
1547           stack_offset += 4;
1548
1549           /* Not a leaf function.  */
1550           if (get_frame_saved_regs (fi)[SRP_REGNUM])
1551             {     
1552               /* SRP was pushed before SP was saved.  */
1553               stack_offset += 4;
1554             }
1555       
1556           /* Restore the SP and adjust for R8 and (possibly) SRP.  */
1557           write_register (SP_REGNUM, get_frame_saved_regs (fi)[FP_REGNUM] + stack_offset);
1558         } 
1559       else
1560         {
1561           /* Currently, we can't get the correct info into fi->saved_regs 
1562              without a frame pointer.  */
1563         }
1564     
1565       /* Restore the PC.  */
1566       write_register (PC_REGNUM, fi->extra_info->return_pc);
1567     }
1568   flush_cached_frames ();
1569 }
1570
1571 /* Calculates a value that measures how good inst_args constraints an 
1572    instruction.  It stems from cris_constraint, found in cris-dis.c.  */
1573
1574 static int
1575 constraint (unsigned int insn, const signed char *inst_args, 
1576             inst_env_type *inst_env)
1577 {
1578   int retval = 0;
1579   int tmp, i;
1580
1581   const char *s = inst_args;
1582
1583   for (; *s; s++)
1584     switch (*s) 
1585       {
1586       case 'm':
1587         if ((insn & 0x30) == 0x30)
1588           return -1;
1589         break;
1590         
1591       case 'S':
1592         /* A prefix operand.  */
1593         if (inst_env->prefix_found)
1594           break;
1595         else
1596           return -1;
1597
1598       case 'B':
1599         /* A "push" prefix.  (This check was REMOVED by san 970921.)  Check for
1600            valid "push" size.  In case of special register, it may be != 4.  */
1601         if (inst_env->prefix_found)
1602           break;
1603         else
1604           return -1;
1605
1606       case 'D':
1607         retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
1608         if (!retval)
1609           return -1;
1610         else 
1611           retval += 4;
1612         break;
1613
1614       case 'P':
1615         tmp = (insn >> 0xC) & 0xF;
1616
1617         for (i = 0; cris_spec_regs[i].name != NULL; i++)
1618           {
1619             /* Since we match four bits, we will give a value of
1620                4 - 1 = 3 in a match.  If there is a corresponding
1621                exact match of a special register in another pattern, it
1622                will get a value of 4, which will be higher.  This should
1623                be correct in that an exact pattern would match better that
1624                a general pattern.
1625                Note that there is a reason for not returning zero; the
1626                pattern for "clear" is partly  matched in the bit-pattern
1627                (the two lower bits must be zero), while the bit-pattern
1628                for a move from a special register is matched in the
1629                register constraint.
1630                This also means we will will have a race condition if
1631                there is a partly match in three bits in the bit pattern.  */
1632             if (tmp == cris_spec_regs[i].number)
1633               {
1634                 retval += 3;
1635                 break;
1636               }
1637           }
1638         
1639         if (cris_spec_regs[i].name == NULL)
1640           return -1;
1641         break;
1642       }
1643   return retval;
1644 }
1645
1646 /* Returns the number of bits set in the variable value.  */
1647
1648 static int
1649 number_of_bits (unsigned int value)
1650 {
1651   int number_of_bits = 0;
1652   
1653   while (value != 0)
1654     {
1655       number_of_bits += 1;
1656       value &= (value - 1);
1657     }
1658   return number_of_bits;
1659 }
1660
1661 /* Finds the address that should contain the single step breakpoint(s). 
1662    It stems from code in cris-dis.c.  */
1663
1664 static int
1665 find_cris_op (unsigned short insn, inst_env_type *inst_env)
1666 {
1667   int i;
1668   int max_level_of_match = -1;
1669   int max_matched = -1;
1670   int level_of_match;
1671
1672   for (i = 0; cris_opcodes[i].name != NULL; i++)
1673     {
1674       if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match) 
1675           && ((cris_opcodes[i].lose & insn) == 0))
1676         {
1677           level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
1678           if (level_of_match >= 0)
1679             {
1680               level_of_match +=
1681                 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
1682               if (level_of_match > max_level_of_match)
1683                 {
1684                   max_matched = i;
1685                   max_level_of_match = level_of_match;
1686                   if (level_of_match == 16)
1687                     {
1688                       /* All bits matched, cannot find better.  */
1689                       break;
1690                     }
1691                 }
1692             }
1693         }
1694     }
1695   return max_matched;
1696 }
1697
1698 /* Attempts to find single-step breakpoints.  Returns -1 on failure which is
1699    actually an internal error.  */
1700
1701 static int
1702 find_step_target (inst_env_type *inst_env)
1703 {
1704   int i;
1705   int offset;
1706   unsigned short insn;
1707
1708   /* Create a local register image and set the initial state.  */
1709   for (i = 0; i < NUM_GENREGS; i++)
1710     {
1711       inst_env->reg[i] = (unsigned long) read_register (i);
1712     }
1713   offset = NUM_GENREGS;
1714   for (i = 0; i < NUM_SPECREGS; i++)
1715     {
1716       inst_env->preg[i] = (unsigned long) read_register (offset + i);
1717     }
1718   inst_env->branch_found = 0;
1719   inst_env->slot_needed = 0;
1720   inst_env->delay_slot_pc_active = 0;
1721   inst_env->prefix_found = 0;
1722   inst_env->invalid = 0;
1723   inst_env->xflag_found = 0;
1724   inst_env->disable_interrupt = 0;
1725
1726   /* Look for a step target.  */
1727   do
1728     {
1729       /* Read an instruction from the client.  */
1730       insn = read_memory_unsigned_integer (inst_env->reg[PC_REGNUM], 2);
1731
1732       /* If the instruction is not in a delay slot the new content of the
1733          PC is [PC] + 2.  If the instruction is in a delay slot it is not
1734          that simple.  Since a instruction in a delay slot cannot change 
1735          the content of the PC, it does not matter what value PC will have. 
1736          Just make sure it is a valid instruction.  */
1737       if (!inst_env->delay_slot_pc_active)
1738         {
1739           inst_env->reg[PC_REGNUM] += 2;
1740         }
1741       else
1742         {
1743           inst_env->delay_slot_pc_active = 0;
1744           inst_env->reg[PC_REGNUM] = inst_env->delay_slot_pc;
1745         }
1746       /* Analyse the present instruction.  */
1747       i = find_cris_op (insn, inst_env);
1748       if (i == -1)
1749         {
1750           inst_env->invalid = 1;
1751         }
1752       else
1753         {
1754           cris_gdb_func (cris_opcodes[i].op, insn, inst_env);
1755         }
1756     } while (!inst_env->invalid 
1757              && (inst_env->prefix_found || inst_env->xflag_found 
1758                  || inst_env->slot_needed));
1759   return i;
1760 }
1761
1762 /* There is no hardware single-step support.  The function find_step_target
1763    digs through the opcodes in order to find all possible targets. 
1764    Either one ordinary target or two targets for branches may be found.  */
1765
1766 void
1767 cris_software_single_step (enum target_signal ignore, int insert_breakpoints)
1768 {
1769   inst_env_type inst_env;
1770   
1771   if (insert_breakpoints)
1772     {
1773       /* Analyse the present instruction environment and insert 
1774          breakpoints.  */
1775       int status = find_step_target (&inst_env);
1776       if (status == -1)
1777         {
1778           /* Could not find a target.  FIXME: Should do something.  */
1779         }
1780       else
1781         {
1782           /* Insert at most two breakpoints.  One for the next PC content
1783              and possibly another one for a branch, jump, etc.  */
1784           next_pc = (CORE_ADDR) inst_env.reg[PC_REGNUM];
1785           target_insert_breakpoint (next_pc, break_mem[0]);
1786           if (inst_env.branch_found 
1787               && (CORE_ADDR) inst_env.branch_break_address != next_pc)
1788             {
1789               branch_target_address = 
1790                 (CORE_ADDR) inst_env.branch_break_address;
1791               target_insert_breakpoint (branch_target_address, break_mem[1]);
1792               branch_break_inserted = 1;
1793             }
1794         }
1795     }
1796   else
1797     {
1798       /* Remove breakpoints.  */
1799       target_remove_breakpoint (next_pc, break_mem[0]);
1800       if (branch_break_inserted)
1801         {
1802           target_remove_breakpoint (branch_target_address, break_mem[1]);
1803           branch_break_inserted = 0;
1804         }
1805     }
1806 }
1807
1808 /* Calculates the prefix value for quick offset addressing mode.  */
1809
1810 void
1811 quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
1812 {
1813   /* It's invalid to be in a delay slot.  You can't have a prefix to this
1814      instruction (not 100% sure).  */
1815   if (inst_env->slot_needed || inst_env->prefix_found)
1816     {
1817       inst_env->invalid = 1;
1818       return; 
1819     }
1820  
1821   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
1822   inst_env->prefix_value += cris_get_bdap_quick_offset (inst);
1823
1824   /* A prefix doesn't change the xflag_found.  But the rest of the flags
1825      need updating.  */
1826   inst_env->slot_needed = 0;
1827   inst_env->prefix_found = 1;
1828 }
1829
1830 /* Updates the autoincrement register.  The size of the increment is derived 
1831    from the size of the operation.  The PC is always kept aligned on even
1832    word addresses.  */
1833
1834 void 
1835 process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
1836 {
1837   if (size == INST_BYTE_SIZE)
1838     {
1839       inst_env->reg[cris_get_operand1 (inst)] += 1;
1840
1841       /* The PC must be word aligned, so increase the PC with one
1842          word even if the size is byte.  */
1843       if (cris_get_operand1 (inst) == REG_PC)
1844         {
1845           inst_env->reg[REG_PC] += 1;
1846         }
1847     }
1848   else if (size == INST_WORD_SIZE)
1849     {
1850       inst_env->reg[cris_get_operand1 (inst)] += 2;
1851     }
1852   else if (size == INST_DWORD_SIZE)
1853     {
1854       inst_env->reg[cris_get_operand1 (inst)] += 4;
1855     }
1856   else
1857     {
1858       /* Invalid size.  */
1859       inst_env->invalid = 1;
1860     }
1861 }
1862
1863 /* Just a forward declaration.  */
1864
1865 unsigned long
1866 get_data_from_address (unsigned short *inst, CORE_ADDR address);
1867
1868 /* Calculates the prefix value for the general case of offset addressing 
1869    mode.  */
1870
1871 void
1872 bdap_prefix (unsigned short inst, inst_env_type *inst_env)
1873 {
1874
1875   long offset;
1876
1877   /* It's invalid to be in a delay slot.  */
1878   if (inst_env->slot_needed || inst_env->prefix_found)
1879     {
1880       inst_env->invalid = 1;
1881       return; 
1882     }
1883
1884   /* The calculation of prefix_value used to be after process_autoincrement,
1885      but that fails for an instruction such as jsr [$r0+12] which is encoded
1886      as 5f0d 0c00 30b9 when compiled with -fpic.  Since PC is operand1 it
1887      mustn't be incremented until we have read it and what it points at.  */
1888   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
1889
1890   /* The offset is an indirection of the contents of the operand1 register.  */
1891   inst_env->prefix_value += 
1892     get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)]);
1893   
1894   if (cris_get_mode (inst) == AUTOINC_MODE)
1895     {
1896       process_autoincrement (cris_get_size (inst), inst, inst_env); 
1897     }
1898    
1899   /* A prefix doesn't change the xflag_found.  But the rest of the flags
1900      need updating.  */
1901   inst_env->slot_needed = 0;
1902   inst_env->prefix_found = 1;
1903 }
1904
1905 /* Calculates the prefix value for the index addressing mode.  */
1906
1907 void
1908 biap_prefix (unsigned short inst, inst_env_type *inst_env)
1909 {
1910   /* It's invalid to be in a delay slot.  I can't see that it's possible to
1911      have a prefix to this instruction.  So I will treat this as invalid.  */
1912   if (inst_env->slot_needed || inst_env->prefix_found)
1913     {
1914       inst_env->invalid = 1;
1915       return;
1916     }
1917   
1918   inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];
1919
1920   /* The offset is the operand2 value shifted the size of the instruction 
1921      to the left.  */
1922   inst_env->prefix_value += 
1923     inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);
1924   
1925   /* If the PC is operand1 (base) the address used is the address after 
1926      the main instruction, i.e. address + 2 (the PC is already compensated
1927      for the prefix operation).  */
1928   if (cris_get_operand1 (inst) == REG_PC)
1929     {
1930       inst_env->prefix_value += 2;
1931     }
1932
1933   /* A prefix doesn't change the xflag_found.  But the rest of the flags
1934      need updating.  */
1935   inst_env->slot_needed = 0;
1936   inst_env->xflag_found = 0;
1937   inst_env->prefix_found = 1;
1938 }
1939
1940 /* Calculates the prefix value for the double indirect addressing mode.  */
1941
1942 void 
1943 dip_prefix (unsigned short inst, inst_env_type *inst_env)
1944 {
1945
1946   CORE_ADDR address;
1947
1948   /* It's invalid to be in a delay slot.  */
1949   if (inst_env->slot_needed || inst_env->prefix_found)
1950     {
1951       inst_env->invalid = 1;
1952       return;
1953     }
1954   
1955   /* The prefix value is one dereference of the contents of the operand1
1956      register.  */
1957   address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
1958   inst_env->prefix_value = read_memory_unsigned_integer (address, 4);
1959     
1960   /* Check if the mode is autoincrement.  */
1961   if (cris_get_mode (inst) == AUTOINC_MODE)
1962     {
1963       inst_env->reg[cris_get_operand1 (inst)] += 4;
1964     }
1965
1966   /* A prefix doesn't change the xflag_found.  But the rest of the flags
1967      need updating.  */
1968   inst_env->slot_needed = 0;
1969   inst_env->xflag_found = 0;
1970   inst_env->prefix_found = 1;
1971 }
1972
1973 /* Finds the destination for a branch with 8-bits offset.  */
1974
1975 void
1976 eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
1977 {
1978
1979   short offset;
1980
1981   /* If we have a prefix or are in a delay slot it's bad.  */
1982   if (inst_env->slot_needed || inst_env->prefix_found)
1983     {
1984       inst_env->invalid = 1;
1985       return;
1986     }
1987   
1988   /* We have a branch, find out where the branch will land.  */
1989   offset = cris_get_branch_short_offset (inst);
1990
1991   /* Check if the offset is signed.  */
1992   if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
1993     {
1994       offset |= 0xFF00;
1995     }
1996   
1997   /* The offset ends with the sign bit, set it to zero.  The address
1998      should always be word aligned.  */
1999   offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;
2000   
2001   inst_env->branch_found = 1;
2002   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2003
2004   inst_env->slot_needed = 1;
2005   inst_env->prefix_found = 0;
2006   inst_env->xflag_found = 0;
2007   inst_env->disable_interrupt = 1;
2008 }
2009
2010 /* Finds the destination for a branch with 16-bits offset.  */
2011
2012 void 
2013 sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
2014 {
2015   short offset;
2016
2017   /* If we have a prefix or is in a delay slot it's bad.  */
2018   if (inst_env->slot_needed || inst_env->prefix_found)
2019     {
2020       inst_env->invalid = 1;
2021       return;
2022     }
2023
2024   /* We have a branch, find out the offset for the branch.  */
2025   offset = read_memory_integer (inst_env->reg[REG_PC], 2);
2026
2027   /* The instruction is one word longer than normal, so add one word
2028      to the PC.  */
2029   inst_env->reg[REG_PC] += 2;
2030
2031   inst_env->branch_found = 1;
2032   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
2033
2034
2035   inst_env->slot_needed = 1;
2036   inst_env->prefix_found = 0;
2037   inst_env->xflag_found = 0;
2038   inst_env->disable_interrupt = 1;
2039 }
2040
2041 /* Handles the ABS instruction.  */
2042
2043 void 
2044 abs_op (unsigned short inst, inst_env_type *inst_env)
2045 {
2046
2047   long value;
2048   
2049   /* ABS can't have a prefix, so it's bad if it does.  */
2050   if (inst_env->prefix_found)
2051     {
2052       inst_env->invalid = 1;
2053       return;
2054     }
2055
2056   /* Check if the operation affects the PC.  */
2057   if (cris_get_operand2 (inst) == REG_PC)
2058     {
2059     
2060       /* It's invalid to change to the PC if we are in a delay slot.  */
2061       if (inst_env->slot_needed)
2062         {
2063           inst_env->invalid = 1;
2064           return;
2065         }
2066
2067       value = (long) inst_env->reg[REG_PC];
2068
2069       /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK.  */
2070       if (value != SIGNED_DWORD_MASK)
2071         {
2072           value = -value;
2073           inst_env->reg[REG_PC] = (long) value;
2074         }
2075     }
2076
2077   inst_env->slot_needed = 0;
2078   inst_env->prefix_found = 0;
2079   inst_env->xflag_found = 0;
2080   inst_env->disable_interrupt = 0;
2081 }
2082
2083 /* Handles the ADDI instruction.  */
2084
2085 void 
2086 addi_op (unsigned short inst, inst_env_type *inst_env)
2087 {
2088   /* It's invalid to have the PC as base register.  And ADDI can't have
2089      a prefix.  */
2090   if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
2091     {
2092       inst_env->invalid = 1;
2093       return;
2094     }
2095
2096   inst_env->slot_needed = 0;
2097   inst_env->prefix_found = 0;
2098   inst_env->xflag_found = 0;
2099   inst_env->disable_interrupt = 0;
2100 }
2101
2102 /* Handles the ASR instruction.  */
2103
2104 void 
2105 asr_op (unsigned short inst, inst_env_type *inst_env)
2106 {
2107   int shift_steps;
2108   unsigned long value;
2109   unsigned long signed_extend_mask = 0;
2110
2111   /* ASR can't have a prefix, so check that it doesn't.  */
2112   if (inst_env->prefix_found)
2113     {
2114       inst_env->invalid = 1;
2115       return;
2116     }
2117
2118   /* Check if the PC is the target register.  */
2119   if (cris_get_operand2 (inst) == REG_PC)
2120     {
2121       /* It's invalid to change the PC in a delay slot.  */
2122       if (inst_env->slot_needed)
2123         {
2124           inst_env->invalid = 1;
2125           return;
2126         }
2127       /* Get the number of bits to shift.  */
2128       shift_steps = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
2129       value = inst_env->reg[REG_PC];
2130
2131       /* Find out how many bits the operation should apply to.  */
2132       if (cris_get_size (inst) == INST_BYTE_SIZE)
2133         {
2134           if (value & SIGNED_BYTE_MASK)
2135             {
2136               signed_extend_mask = 0xFF;
2137               signed_extend_mask = signed_extend_mask >> shift_steps;
2138               signed_extend_mask = ~signed_extend_mask;
2139             }
2140           value = value >> shift_steps;
2141           value |= signed_extend_mask;
2142           value &= 0xFF;
2143           inst_env->reg[REG_PC] &= 0xFFFFFF00;
2144           inst_env->reg[REG_PC] |= value;
2145         }
2146       else if (cris_get_size (inst) == INST_WORD_SIZE)
2147         {
2148           if (value & SIGNED_WORD_MASK)
2149             {
2150               signed_extend_mask = 0xFFFF;
2151               signed_extend_mask = signed_extend_mask >> shift_steps;
2152               signed_extend_mask = ~signed_extend_mask;
2153             }
2154           value = value >> shift_steps;
2155           value |= signed_extend_mask;
2156           value &= 0xFFFF;
2157           inst_env->reg[REG_PC] &= 0xFFFF0000;
2158           inst_env->reg[REG_PC] |= value;
2159         }
2160       else if (cris_get_size (inst) == INST_DWORD_SIZE)
2161         {
2162           if (value & SIGNED_DWORD_MASK)
2163             {
2164               signed_extend_mask = 0xFFFFFFFF;
2165               signed_extend_mask = signed_extend_mask >> shift_steps;
2166               signed_extend_mask = ~signed_extend_mask;
2167             }
2168           value = value >> shift_steps;
2169           value |= signed_extend_mask;
2170           inst_env->reg[REG_PC]  = value;
2171         }
2172     }
2173   inst_env->slot_needed = 0;
2174   inst_env->prefix_found = 0;
2175   inst_env->xflag_found = 0;
2176   inst_env->disable_interrupt = 0;
2177 }
2178
2179 /* Handles the ASRQ instruction.  */
2180
2181 void 
2182 asrq_op (unsigned short inst, inst_env_type *inst_env)
2183 {
2184
2185   int shift_steps;
2186   unsigned long value;
2187   unsigned long signed_extend_mask = 0;
2188   
2189   /* ASRQ can't have a prefix, so check that it doesn't.  */
2190   if (inst_env->prefix_found)
2191     {
2192       inst_env->invalid = 1;
2193       return;
2194     }
2195
2196   /* Check if the PC is the target register.  */
2197   if (cris_get_operand2 (inst) == REG_PC)
2198     {
2199
2200       /* It's invalid to change the PC in a delay slot.  */
2201       if (inst_env->slot_needed)
2202         {
2203           inst_env->invalid = 1;
2204           return;
2205         }
2206       /* The shift size is given as a 5 bit quick value, i.e. we don't
2207          want the the sign bit of the quick value.  */
2208       shift_steps = cris_get_asr_shift_steps (inst);
2209       value = inst_env->reg[REG_PC];
2210       if (value & SIGNED_DWORD_MASK)
2211         {
2212           signed_extend_mask = 0xFFFFFFFF;
2213           signed_extend_mask = signed_extend_mask >> shift_steps;
2214           signed_extend_mask = ~signed_extend_mask;
2215         }
2216       value = value >> shift_steps;
2217       value |= signed_extend_mask;
2218       inst_env->reg[REG_PC]  = value;
2219     }
2220   inst_env->slot_needed = 0;
2221   inst_env->prefix_found = 0;
2222   inst_env->xflag_found = 0;
2223   inst_env->disable_interrupt = 0;
2224 }
2225
2226 /* Handles the AX, EI and SETF instruction.  */
2227
2228 void 
2229 ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
2230 {
2231   if (inst_env->prefix_found)
2232     {
2233       inst_env->invalid = 1;
2234       return;
2235     }
2236   /* Check if the instruction is setting the X flag.  */
2237   if (cris_is_xflag_bit_on (inst))
2238     {
2239       inst_env->xflag_found = 1;
2240     }
2241   else
2242     {
2243       inst_env->xflag_found = 0;
2244     }
2245   inst_env->slot_needed = 0;
2246   inst_env->prefix_found = 0;
2247   inst_env->disable_interrupt = 1;
2248 }
2249
2250 /* Checks if the instruction is in assign mode.  If so, it updates the assign 
2251    register.  Note that check_assign assumes that the caller has checked that
2252    there is a prefix to this instruction.  The mode check depends on this.  */
2253
2254 void 
2255 check_assign (unsigned short inst, inst_env_type *inst_env)
2256 {
2257   /* Check if it's an assign addressing mode.  */
2258   if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2259     {
2260       /* Assign the prefix value to operand 1.  */
2261       inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
2262     }
2263 }
2264
2265 /* Handles the 2-operand BOUND instruction.  */
2266
2267 void 
2268 two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2269 {
2270   /* It's invalid to have the PC as the index operand.  */
2271   if (cris_get_operand2 (inst) == REG_PC)
2272     {
2273       inst_env->invalid = 1;
2274       return;
2275     }
2276   /* Check if we have a prefix.  */
2277   if (inst_env->prefix_found)
2278     {
2279       check_assign (inst, inst_env);
2280     }
2281   /* Check if this is an autoincrement mode.  */
2282   else if (cris_get_mode (inst) == AUTOINC_MODE)
2283     {
2284       /* It's invalid to change the PC in a delay slot.  */
2285       if (inst_env->slot_needed)
2286         {
2287           inst_env->invalid = 1;
2288           return;
2289         }
2290       process_autoincrement (cris_get_size (inst), inst, inst_env);
2291     }
2292   inst_env->slot_needed = 0;
2293   inst_env->prefix_found = 0;
2294   inst_env->xflag_found = 0;
2295   inst_env->disable_interrupt = 0;
2296 }
2297
2298 /* Handles the 3-operand BOUND instruction.  */
2299
2300 void 
2301 three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
2302 {
2303   /* It's an error if we haven't got a prefix.  And it's also an error
2304      if the PC is the destination register.  */
2305   if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
2306     {
2307       inst_env->invalid = 1;
2308       return;
2309     }
2310   inst_env->slot_needed = 0;
2311   inst_env->prefix_found = 0;
2312   inst_env->xflag_found = 0;
2313   inst_env->disable_interrupt = 0;
2314 }
2315
2316 /* Clears the status flags in inst_env.  */
2317
2318 void 
2319 btst_nop_op (unsigned short inst, inst_env_type *inst_env)
2320 {
2321   /* It's an error if we have got a prefix.  */
2322   if (inst_env->prefix_found)
2323     {
2324       inst_env->invalid = 1;
2325       return;
2326     }
2327
2328   inst_env->slot_needed = 0;
2329   inst_env->prefix_found = 0;
2330   inst_env->xflag_found = 0;
2331   inst_env->disable_interrupt = 0;
2332 }
2333
2334 /* Clears the status flags in inst_env.  */
2335
2336 void 
2337 clearf_di_op (unsigned short inst, inst_env_type *inst_env)
2338 {
2339   /* It's an error if we have got a prefix.  */
2340   if (inst_env->prefix_found)
2341     {
2342       inst_env->invalid = 1;
2343       return;
2344     }
2345
2346   inst_env->slot_needed = 0;
2347   inst_env->prefix_found = 0;
2348   inst_env->xflag_found = 0;
2349   inst_env->disable_interrupt = 1;
2350 }
2351
2352 /* Handles the CLEAR instruction if it's in register mode.  */
2353
2354 void 
2355 reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
2356 {
2357   /* Check if the target is the PC.  */
2358   if (cris_get_operand2 (inst) == REG_PC)
2359     {
2360       /* The instruction will clear the instruction's size bits.  */
2361       int clear_size = cris_get_clear_size (inst);
2362       if (clear_size == INST_BYTE_SIZE)
2363         {
2364           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
2365         }
2366       if (clear_size == INST_WORD_SIZE)
2367         {
2368           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
2369         }
2370       if (clear_size == INST_DWORD_SIZE)
2371         {
2372           inst_env->delay_slot_pc = 0x0;
2373         }
2374       /* The jump will be delayed with one delay slot.  So we need a delay 
2375          slot.  */
2376       inst_env->slot_needed = 1;
2377       inst_env->delay_slot_pc_active = 1;
2378     }
2379   else
2380     {
2381       /* The PC will not change => no delay slot.  */
2382       inst_env->slot_needed = 0;
2383     }
2384   inst_env->prefix_found = 0;
2385   inst_env->xflag_found = 0;
2386   inst_env->disable_interrupt = 0;
2387 }
2388
2389 /* Handles the TEST instruction if it's in register mode.  */
2390
2391 void
2392 reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
2393 {
2394   /* It's an error if we have got a prefix.  */
2395   if (inst_env->prefix_found)
2396     {
2397       inst_env->invalid = 1;
2398       return;
2399     }
2400   inst_env->slot_needed = 0;
2401   inst_env->prefix_found = 0;
2402   inst_env->xflag_found = 0;
2403   inst_env->disable_interrupt = 0;
2404
2405 }
2406
2407 /* Handles the CLEAR and TEST instruction if the instruction isn't 
2408    in register mode.  */
2409
2410 void 
2411 none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
2412 {
2413   /* Check if we are in a prefix mode.  */
2414   if (inst_env->prefix_found)
2415     {
2416       /* The only way the PC can change is if this instruction is in
2417          assign addressing mode.  */
2418       check_assign (inst, inst_env);
2419     }
2420   /* Indirect mode can't change the PC so just check if the mode is
2421      autoincrement.  */
2422   else if (cris_get_mode (inst) == AUTOINC_MODE)
2423     {
2424       process_autoincrement (cris_get_size (inst), inst, inst_env);
2425     }
2426   inst_env->slot_needed = 0;
2427   inst_env->prefix_found = 0;
2428   inst_env->xflag_found = 0;
2429   inst_env->disable_interrupt = 0;
2430 }
2431
2432 /* Checks that the PC isn't the destination register or the instructions has
2433    a prefix.  */
2434
2435 void 
2436 dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
2437 {
2438   /* It's invalid to have the PC as the destination.  The instruction can't
2439      have a prefix.  */
2440   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2441     {
2442       inst_env->invalid = 1;
2443       return;
2444     }
2445
2446   inst_env->slot_needed = 0;
2447   inst_env->prefix_found = 0;
2448   inst_env->xflag_found = 0;
2449   inst_env->disable_interrupt = 0;
2450 }
2451
2452 /* Checks that the instruction doesn't have a prefix.  */
2453
2454 void
2455 break_op (unsigned short inst, inst_env_type *inst_env)
2456 {
2457   /* The instruction can't have a prefix.  */
2458   if (inst_env->prefix_found)
2459     {
2460       inst_env->invalid = 1;
2461       return;
2462     }
2463
2464   inst_env->slot_needed = 0;
2465   inst_env->prefix_found = 0;
2466   inst_env->xflag_found = 0;
2467   inst_env->disable_interrupt = 1;
2468 }
2469
2470 /* Checks that the PC isn't the destination register and that the instruction
2471    doesn't have a prefix.  */
2472
2473 void
2474 scc_op (unsigned short inst, inst_env_type *inst_env)
2475 {
2476   /* It's invalid to have the PC as the destination.  The instruction can't
2477      have a prefix.  */
2478   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
2479     {
2480       inst_env->invalid = 1;
2481       return;
2482     }
2483
2484   inst_env->slot_needed = 0;
2485   inst_env->prefix_found = 0;
2486   inst_env->xflag_found = 0;
2487   inst_env->disable_interrupt = 1;
2488 }
2489
2490 /* Handles the register mode JUMP instruction.  */
2491
2492 void 
2493 reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2494 {
2495   /* It's invalid to do a JUMP in a delay slot.  The mode is register, so 
2496      you can't have a prefix.  */
2497   if ((inst_env->slot_needed) || (inst_env->prefix_found))
2498     {
2499       inst_env->invalid = 1;
2500       return;
2501     }
2502   
2503   /* Just change the PC.  */
2504   inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
2505   inst_env->slot_needed = 0;
2506   inst_env->prefix_found = 0;
2507   inst_env->xflag_found = 0;
2508   inst_env->disable_interrupt = 1;
2509 }
2510
2511 /* Handles the JUMP instruction for all modes except register.  */
2512
2513 void none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
2514 {
2515   unsigned long newpc;
2516   CORE_ADDR address;
2517
2518   /* It's invalid to do a JUMP in a delay slot.  */
2519   if (inst_env->slot_needed)
2520     {
2521       inst_env->invalid = 1;
2522     }
2523   else
2524     {
2525       /* Check if we have a prefix.  */
2526       if (inst_env->prefix_found)
2527         {
2528           check_assign (inst, inst_env);
2529
2530           /* Get the new value for the the PC.  */
2531           newpc = 
2532             read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
2533                                           4);
2534         }
2535       else
2536         {
2537           /* Get the new value for the PC.  */
2538           address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
2539           newpc = read_memory_unsigned_integer (address, 4);
2540
2541           /* Check if we should increment a register.  */
2542           if (cris_get_mode (inst) == AUTOINC_MODE)
2543             {
2544               inst_env->reg[cris_get_operand1 (inst)] += 4;
2545             }
2546         }
2547       inst_env->reg[REG_PC] = newpc;
2548     }
2549   inst_env->slot_needed = 0;
2550   inst_env->prefix_found = 0;
2551   inst_env->xflag_found = 0;
2552   inst_env->disable_interrupt = 1;
2553 }
2554
2555 /* Handles moves to special registers (aka P-register) for all modes.  */
2556
2557 void 
2558 move_to_preg_op (unsigned short inst, inst_env_type *inst_env)
2559 {
2560   if (inst_env->prefix_found)
2561     {
2562       /* The instruction has a prefix that means we are only interested if
2563          the instruction is in assign mode.  */
2564       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2565         {
2566           /* The prefix handles the problem if we are in a delay slot.  */
2567           if (cris_get_operand1 (inst) == REG_PC)
2568             {
2569               /* Just take care of the assign.  */
2570               check_assign (inst, inst_env);
2571             }
2572         }
2573     }
2574   else if (cris_get_mode (inst) == AUTOINC_MODE)
2575     {
2576       /* The instruction doesn't have a prefix, the only case left that we
2577          are interested in is the autoincrement mode.  */
2578       if (cris_get_operand1 (inst) == REG_PC)
2579         {
2580           /* If the PC is to be incremented it's invalid to be in a 
2581              delay slot.  */
2582           if (inst_env->slot_needed)
2583             {
2584               inst_env->invalid = 1;
2585               return;
2586             }
2587
2588           /* The increment depends on the size of the special register.  */
2589           if (cris_register_size (cris_get_operand2 (inst)) == 1)
2590             {
2591               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2592             }
2593           else if (cris_register_size (cris_get_operand2 (inst)) == 2)
2594             {
2595               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2596             }
2597           else
2598             {
2599               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2600             }
2601         }
2602     }
2603   inst_env->slot_needed = 0;
2604   inst_env->prefix_found = 0;
2605   inst_env->xflag_found = 0;
2606   inst_env->disable_interrupt = 1;
2607 }
2608
2609 /* Handles moves from special registers (aka P-register) for all modes
2610    except register.  */
2611
2612 void 
2613 none_reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2614 {
2615   if (inst_env->prefix_found)
2616     {
2617       /* The instruction has a prefix that means we are only interested if
2618          the instruction is in assign mode.  */
2619       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
2620         {
2621           /* The prefix handles the problem if we are in a delay slot.  */
2622           if (cris_get_operand1 (inst) == REG_PC)
2623             {
2624               /* Just take care of the assign.  */
2625               check_assign (inst, inst_env);
2626             }
2627         }
2628     }    
2629   /* The instruction doesn't have a prefix, the only case left that we
2630      are interested in is the autoincrement mode.  */
2631   else if (cris_get_mode (inst) == AUTOINC_MODE)
2632     {
2633       if (cris_get_operand1 (inst) == REG_PC)
2634         {
2635           /* If the PC is to be incremented it's invalid to be in a 
2636              delay slot.  */
2637           if (inst_env->slot_needed)
2638             {
2639               inst_env->invalid = 1;
2640               return;
2641             }
2642           
2643           /* The increment depends on the size of the special register.  */
2644           if (cris_register_size (cris_get_operand2 (inst)) == 1)
2645             {
2646               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
2647             }
2648           else if (cris_register_size (cris_get_operand2 (inst)) == 2)
2649             {
2650               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
2651             }
2652           else
2653             {
2654               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
2655             }
2656         }
2657     }
2658   inst_env->slot_needed = 0;
2659   inst_env->prefix_found = 0;
2660   inst_env->xflag_found = 0;
2661   inst_env->disable_interrupt = 1;
2662 }
2663
2664 /* Handles moves from special registers (aka P-register) when the mode
2665    is register.  */
2666
2667 void 
2668 reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
2669 {
2670   /* Register mode move from special register can't have a prefix.  */
2671   if (inst_env->prefix_found)
2672     {
2673       inst_env->invalid = 1;
2674       return;
2675     }
2676
2677   if (cris_get_operand1 (inst) == REG_PC)
2678     {
2679       /* It's invalid to change the PC in a delay slot.  */
2680       if (inst_env->slot_needed)
2681         {
2682           inst_env->invalid = 1;
2683           return;
2684         }
2685       /* The destination is the PC, the jump will have a delay slot.  */
2686       inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
2687       inst_env->slot_needed = 1;
2688       inst_env->delay_slot_pc_active = 1;
2689     }
2690   else
2691     {
2692       /* If the destination isn't PC, there will be no jump.  */
2693       inst_env->slot_needed = 0;
2694     }
2695   inst_env->prefix_found = 0;
2696   inst_env->xflag_found = 0;
2697   inst_env->disable_interrupt = 1;
2698 }
2699
2700 /* Handles the MOVEM from memory to general register instruction.  */
2701
2702 void 
2703 move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
2704 {
2705   if (inst_env->prefix_found)
2706     {
2707       /* The prefix handles the problem if we are in a delay slot.  Is the
2708          MOVEM instruction going to change the PC?  */
2709       if (cris_get_operand2 (inst) >= REG_PC)
2710         {
2711           inst_env->reg[REG_PC] = 
2712             read_memory_unsigned_integer (inst_env->prefix_value, 4);
2713         }
2714       /* The assign value is the value after the increment.  Normally, the   
2715          assign value is the value before the increment.  */
2716       if ((cris_get_operand1 (inst) == REG_PC) 
2717           && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2718         {
2719           inst_env->reg[REG_PC] = inst_env->prefix_value;
2720           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2721         }
2722     }
2723   else
2724     {
2725       /* Is the MOVEM instruction going to change the PC?  */
2726       if (cris_get_operand2 (inst) == REG_PC)
2727         {
2728           /* It's invalid to change the PC in a delay slot.  */
2729           if (inst_env->slot_needed)
2730             {
2731               inst_env->invalid = 1;
2732               return;
2733             }
2734           inst_env->reg[REG_PC] =
2735             read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)], 
2736                                           4);
2737         }
2738       /* The increment is not depending on the size, instead it's depending
2739          on the number of registers loaded from memory.  */
2740       if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
2741         {
2742           /* It's invalid to change the PC in a delay slot.  */
2743           if (inst_env->slot_needed)
2744             {
2745               inst_env->invalid = 1;
2746               return;
2747             }
2748           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1); 
2749         }
2750     }
2751   inst_env->slot_needed = 0;
2752   inst_env->prefix_found = 0;
2753   inst_env->xflag_found = 0;
2754   inst_env->disable_interrupt = 0;
2755 }
2756
2757 /* Handles the MOVEM to memory from general register instruction.  */
2758
2759 void 
2760 move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
2761 {
2762   if (inst_env->prefix_found)
2763     {
2764       /* The assign value is the value after the increment.  Normally, the
2765          assign value is the value before the increment.  */
2766       if ((cris_get_operand1 (inst) == REG_PC) &&
2767           (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
2768         {
2769           /* The prefix handles the problem if we are in a delay slot.  */
2770           inst_env->reg[REG_PC] = inst_env->prefix_value;
2771           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2772         }
2773     }
2774   else
2775     {
2776       /* The increment is not depending on the size, instead it's depending
2777          on the number of registers loaded to memory.  */
2778       if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
2779         {
2780           /* It's invalid to change the PC in a delay slot.  */
2781           if (inst_env->slot_needed)
2782             {
2783               inst_env->invalid = 1;
2784               return;
2785             }
2786           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
2787         }
2788     }
2789   inst_env->slot_needed = 0;
2790   inst_env->prefix_found = 0;
2791   inst_env->xflag_found = 0;
2792   inst_env->disable_interrupt = 0;
2793 }
2794
2795 /* Handles the pop instruction to a general register. 
2796    POP is a assembler macro for MOVE.D [SP+], Rd.  */
2797
2798 void 
2799 reg_pop_op (unsigned short inst, inst_env_type *inst_env)
2800 {
2801   /* POP can't have a prefix.  */
2802   if (inst_env->prefix_found)
2803     {
2804       inst_env->invalid = 1;
2805       return;
2806     }
2807   if (cris_get_operand2 (inst) == REG_PC)
2808     {
2809       /* It's invalid to change the PC in a delay slot.  */
2810       if (inst_env->slot_needed)
2811         {
2812           inst_env->invalid = 1;
2813           return;
2814         }
2815       inst_env->reg[REG_PC] = 
2816         read_memory_unsigned_integer (inst_env->reg[REG_SP], 4);
2817     }
2818   inst_env->slot_needed = 0;
2819   inst_env->prefix_found = 0;
2820   inst_env->xflag_found = 0;
2821   inst_env->disable_interrupt = 0;
2822 }
2823
2824 /* Handles moves from register to memory.  */
2825
2826 void 
2827 move_reg_to_mem_index_inc_op (unsigned short inst, inst_env_type *inst_env)
2828 {
2829   /* Check if we have a prefix.  */
2830   if (inst_env->prefix_found)
2831     {
2832       /* The only thing that can change the PC is an assign.  */
2833       check_assign (inst, inst_env);
2834     }
2835   else if ((cris_get_operand1 (inst) == REG_PC) 
2836            && (cris_get_mode (inst) == AUTOINC_MODE))
2837     {
2838       /* It's invalid to change the PC in a delay slot.  */
2839       if (inst_env->slot_needed)
2840         {
2841           inst_env->invalid = 1;
2842           return;
2843         }
2844       process_autoincrement (cris_get_size (inst), inst, inst_env);
2845     }
2846   inst_env->slot_needed = 0;
2847   inst_env->prefix_found = 0;
2848   inst_env->xflag_found = 0;
2849   inst_env->disable_interrupt = 0;
2850 }
2851
2852 /* Handles the intructions that's not yet implemented, by setting 
2853    inst_env->invalid to true.  */
2854
2855 void 
2856 not_implemented_op (unsigned short inst, inst_env_type *inst_env)
2857 {
2858   inst_env->invalid = 1;
2859 }
2860
2861 /* Handles the XOR instruction.  */
2862
2863 void 
2864 xor_op (unsigned short inst, inst_env_type *inst_env)
2865 {
2866   /* XOR can't have a prefix.  */
2867   if (inst_env->prefix_found)
2868     {
2869       inst_env->invalid = 1;
2870       return;
2871     }
2872
2873   /* Check if the PC is the target.  */
2874   if (cris_get_operand2 (inst) == REG_PC)
2875     {
2876       /* It's invalid to change the PC in a delay slot.  */
2877       if (inst_env->slot_needed)
2878         {
2879           inst_env->invalid = 1;
2880           return;
2881         }
2882       inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
2883     }
2884   inst_env->slot_needed = 0;
2885   inst_env->prefix_found = 0;
2886   inst_env->xflag_found = 0;
2887   inst_env->disable_interrupt = 0;
2888 }
2889
2890 /* Handles the MULS instruction.  */
2891
2892 void 
2893 muls_op (unsigned short inst, inst_env_type *inst_env)
2894 {
2895   /* MULS/U can't have a prefix.  */
2896   if (inst_env->prefix_found)
2897     {
2898       inst_env->invalid = 1;
2899       return;
2900     }
2901
2902   /* Consider it invalid if the PC is the target.  */
2903   if (cris_get_operand2 (inst) == REG_PC)
2904     {
2905       inst_env->invalid = 1;
2906       return;
2907     }
2908   inst_env->slot_needed = 0;
2909   inst_env->prefix_found = 0;
2910   inst_env->xflag_found = 0;
2911   inst_env->disable_interrupt = 0;
2912 }
2913
2914 /* Handles the MULU instruction.  */
2915
2916 void 
2917 mulu_op (unsigned short inst, inst_env_type *inst_env)
2918 {
2919   /* MULS/U can't have a prefix.  */
2920   if (inst_env->prefix_found)
2921     {
2922       inst_env->invalid = 1;
2923       return;
2924     }
2925
2926   /* Consider it invalid if the PC is the target.  */
2927   if (cris_get_operand2 (inst) == REG_PC)
2928     {
2929       inst_env->invalid = 1;
2930       return;
2931     }
2932   inst_env->slot_needed = 0;
2933   inst_env->prefix_found = 0;
2934   inst_env->xflag_found = 0;
2935   inst_env->disable_interrupt = 0;
2936 }
2937
2938 /* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE. 
2939    The MOVE instruction is the move from source to register.  */
2940
2941 void 
2942 add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env, 
2943                                 unsigned long source1, unsigned long source2)
2944 {
2945   unsigned long pc_mask;
2946   unsigned long operation_mask;
2947   
2948   /* Find out how many bits the operation should apply to.  */
2949   if (cris_get_size (inst) == INST_BYTE_SIZE)
2950     {
2951       pc_mask = 0xFFFFFF00; 
2952       operation_mask = 0xFF;
2953     }
2954   else if (cris_get_size (inst) == INST_WORD_SIZE)
2955     {
2956       pc_mask = 0xFFFF0000;
2957       operation_mask = 0xFFFF;
2958     }
2959   else if (cris_get_size (inst) == INST_DWORD_SIZE)
2960     {
2961       pc_mask = 0x0;
2962       operation_mask = 0xFFFFFFFF;
2963     }
2964   else
2965     {
2966       /* The size is out of range.  */
2967       inst_env->invalid = 1;
2968       return;
2969     }
2970
2971   /* The instruction just works on uw_operation_mask bits.  */
2972   source2 &= operation_mask;
2973   source1 &= operation_mask;
2974
2975   /* Now calculate the result.  The opcode's 3 first bits separates
2976      the different actions.  */
2977   switch (cris_get_opcode (inst) & 7)
2978     {
2979     case 0:  /* add */
2980       source1 += source2;
2981       break;
2982
2983     case 1:  /* move */
2984       source1 = source2;
2985       break;
2986
2987     case 2:  /* subtract */
2988       source1 -= source2;
2989       break;
2990
2991     case 3:  /* compare */
2992       break;
2993
2994     case 4:  /* and */
2995       source1 &= source2;
2996       break;
2997
2998     case 5:  /* or */
2999       source1 |= source2;
3000       break;
3001
3002     default:
3003       inst_env->invalid = 1;
3004       return;
3005
3006       break;
3007     }
3008
3009   /* Make sure that the result doesn't contain more than the instruction
3010      size bits.  */
3011   source2 &= operation_mask;
3012
3013   /* Calculate the new breakpoint address.  */
3014   inst_env->reg[REG_PC] &= pc_mask;
3015   inst_env->reg[REG_PC] |= source1;
3016
3017 }
3018
3019 /* Extends the value from either byte or word size to a dword.  If the mode
3020    is zero extend then the value is extended with zero.  If instead the mode
3021    is signed extend the sign bit of the value is taken into consideration.  */
3022
3023 unsigned long 
3024 do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
3025 {
3026   /* The size can be either byte or word, check which one it is. 
3027      Don't check the highest bit, it's indicating if it's a zero
3028      or sign extend.  */
3029   if (cris_get_size (*inst) & INST_WORD_SIZE)
3030     {
3031       /* Word size.  */
3032       value &= 0xFFFF;
3033
3034       /* Check if the instruction is signed extend.  If so, check if value has
3035          the sign bit on.  */
3036       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
3037         {
3038           value |= SIGNED_WORD_EXTEND_MASK;
3039         } 
3040     }
3041   else
3042     {
3043       /* Byte size.  */
3044       value &= 0xFF;
3045
3046       /* Check if the instruction is signed extend.  If so, check if value has
3047          the sign bit on.  */
3048       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
3049         {
3050           value |= SIGNED_BYTE_EXTEND_MASK;
3051         }
3052     }
3053   /* The size should now be dword.  */
3054   cris_set_size_to_dword (inst);
3055   return value;
3056 }
3057
3058 /* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
3059    instruction.  The MOVE instruction is the move from source to register.  */
3060
3061 void 
3062 reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
3063                                      inst_env_type *inst_env)
3064 {
3065   unsigned long operand1;
3066   unsigned long operand2;
3067
3068   /* It's invalid to have a prefix to the instruction.  This is a register 
3069      mode instruction and can't have a prefix.  */
3070   if (inst_env->prefix_found)
3071     {
3072       inst_env->invalid = 1;
3073       return;
3074     }
3075   /* Check if the instruction has PC as its target.  */
3076   if (cris_get_operand2 (inst) == REG_PC)
3077     {
3078       if (inst_env->slot_needed)
3079         {
3080           inst_env->invalid = 1;
3081           return;
3082         }
3083       /* The instruction has the PC as its target register.  */
3084       operand1 = inst_env->reg[cris_get_operand1 (inst)]; 
3085       operand2 = inst_env->reg[REG_PC];
3086
3087       /* Check if it's a extend, signed or zero instruction.  */
3088       if (cris_get_opcode (inst) < 4)
3089         {
3090           operand1 = do_sign_or_zero_extend (operand1, &inst);
3091         }
3092       /* Calculate the PC value after the instruction, i.e. where the
3093          breakpoint should be.  The order of the udw_operands is vital.  */
3094       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1); 
3095     }
3096   inst_env->slot_needed = 0;
3097   inst_env->prefix_found = 0;
3098   inst_env->xflag_found = 0;
3099   inst_env->disable_interrupt = 0;
3100 }
3101
3102 /* Returns the data contained at address.  The size of the data is derived from
3103    the size of the operation.  If the instruction is a zero or signed
3104    extend instruction, the size field is changed in instruction.  */
3105
3106 unsigned long 
3107 get_data_from_address (unsigned short *inst, CORE_ADDR address)
3108 {
3109   int size = cris_get_size (*inst);
3110   unsigned long value;
3111
3112   /* If it's an extend instruction we don't want the signed extend bit,
3113      because it influences the size.  */
3114   if (cris_get_opcode (*inst) < 4)
3115     {
3116       size &= ~SIGNED_EXTEND_BIT_MASK;
3117     }
3118   /* Is there a need for checking the size?  Size should contain the number of
3119      bytes to read.  */
3120   size = 1 << size;
3121   value = read_memory_unsigned_integer (address, size);
3122
3123   /* Check if it's an extend, signed or zero instruction.  */
3124   if (cris_get_opcode (*inst) < 4)
3125     {
3126       value = do_sign_or_zero_extend (value, inst);
3127     }
3128   return value;
3129 }
3130
3131 /* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE 
3132    instructions.  The MOVE instruction is the move from source to register.  */
3133
3134 void 
3135 handle_prefix_assign_mode_for_aritm_op (unsigned short inst, 
3136                                         inst_env_type *inst_env)
3137 {
3138   unsigned long operand2;
3139   unsigned long operand3;
3140
3141   check_assign (inst, inst_env);
3142   if (cris_get_operand2 (inst) == REG_PC)
3143     {
3144       operand2 = inst_env->reg[REG_PC];
3145
3146       /* Get the value of the third operand.  */
3147       operand3 = get_data_from_address (&inst, inst_env->prefix_value);
3148
3149       /* Calculate the PC value after the instruction, i.e. where the
3150          breakpoint should be.  The order of the udw_operands is vital.  */
3151       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3152     }
3153   inst_env->slot_needed = 0;
3154   inst_env->prefix_found = 0;
3155   inst_env->xflag_found = 0;
3156   inst_env->disable_interrupt = 0;
3157 }
3158
3159 /* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
3160    OR instructions.  Note that for this to work as expected, the calling
3161    function must have made sure that there is a prefix to this instruction.  */
3162
3163 void 
3164 three_operand_add_sub_cmp_and_or_op (unsigned short inst, 
3165                                      inst_env_type *inst_env)
3166 {
3167   unsigned long operand2;
3168   unsigned long operand3;
3169
3170   if (cris_get_operand1 (inst) == REG_PC)
3171     {
3172       /* The PC will be changed by the instruction.  */
3173       operand2 = inst_env->reg[cris_get_operand2 (inst)];
3174
3175       /* Get the value of the third operand.  */
3176       operand3 = get_data_from_address (&inst, inst_env->prefix_value);
3177
3178       /* Calculate the PC value after the instruction, i.e. where the
3179          breakpoint should be.  */
3180       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
3181     }
3182   inst_env->slot_needed = 0;
3183   inst_env->prefix_found = 0;
3184   inst_env->xflag_found = 0;
3185   inst_env->disable_interrupt = 0;
3186 }
3187
3188 /* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
3189    instructions.  The MOVE instruction is the move from source to register.  */
3190
3191 void 
3192 handle_prefix_index_mode_for_aritm_op (unsigned short inst, 
3193                                        inst_env_type *inst_env)
3194 {
3195   if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
3196     {
3197       /* If the instruction is MOVE it's invalid.  If the instruction is ADD,
3198          SUB, AND or OR something weird is going on (if everything works these
3199          instructions should end up in the three operand version).  */
3200       inst_env->invalid = 1;
3201       return;
3202     }
3203   else
3204     {
3205       /* three_operand_add_sub_cmp_and_or does the same as we should do here
3206          so use it.  */
3207       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3208     }
3209   inst_env->slot_needed = 0;
3210   inst_env->prefix_found = 0;
3211   inst_env->xflag_found = 0;
3212   inst_env->disable_interrupt = 0;
3213 }
3214
3215 /* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
3216    CMP, AND OR and MOVE instruction.  The MOVE instruction is the move from
3217    source to register.  */
3218
3219 void 
3220 handle_inc_and_index_mode_for_aritm_op (unsigned short inst, 
3221                                         inst_env_type *inst_env)
3222 {
3223   unsigned long operand1;
3224   unsigned long operand2;
3225   unsigned long operand3;
3226   int size;
3227
3228   /* The instruction is either an indirect or autoincrement addressing mode. 
3229      Check if the destination register is the PC.  */
3230   if (cris_get_operand2 (inst) == REG_PC)
3231     {
3232       /* Must be done here, get_data_from_address may change the size 
3233          field.  */
3234       size = cris_get_size (inst);
3235       operand2 = inst_env->reg[REG_PC];
3236
3237       /* Get the value of the third operand, i.e. the indirect operand.  */
3238       operand1 = inst_env->reg[cris_get_operand1 (inst)];
3239       operand3 = get_data_from_address (&inst, operand1);
3240
3241       /* Calculate the PC value after the instruction, i.e. where the
3242          breakpoint should be.  The order of the udw_operands is vital.  */
3243       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3); 
3244     }
3245   /* If this is an autoincrement addressing mode, check if the increment
3246      changes the PC.  */
3247   if ((cris_get_operand1 (inst) == REG_PC) && (cris_get_mode (inst) == AUTOINC_MODE))
3248     {
3249       /* Get the size field.  */
3250       size = cris_get_size (inst);
3251
3252       /* If it's an extend instruction we don't want the signed extend bit,
3253          because it influences the size.  */
3254       if (cris_get_opcode (inst) < 4)
3255         {
3256           size &= ~SIGNED_EXTEND_BIT_MASK;
3257         }
3258       process_autoincrement (size, inst, inst_env);
3259     } 
3260   inst_env->slot_needed = 0;
3261   inst_env->prefix_found = 0;
3262   inst_env->xflag_found = 0;
3263   inst_env->disable_interrupt = 0;
3264 }
3265
3266 /* Handles the two-operand addressing mode, all modes except register, for
3267    the ADD, SUB CMP, AND and OR instruction.  */
3268
3269 void 
3270 none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst, 
3271                                           inst_env_type *inst_env)
3272 {
3273   if (inst_env->prefix_found)
3274     {
3275       if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
3276         {
3277           handle_prefix_index_mode_for_aritm_op (inst, inst_env);
3278         }
3279       else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
3280         {
3281           handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
3282         }
3283       else
3284         {
3285           /* The mode is invalid for a prefixed base instruction.  */
3286           inst_env->invalid = 1;
3287           return;
3288         }
3289     }
3290   else
3291     {
3292       handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
3293     }
3294 }
3295
3296 /* Handles the quick addressing mode for the ADD and SUB instruction.  */
3297
3298 void 
3299 quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
3300 {
3301   unsigned long operand1;
3302   unsigned long operand2;
3303
3304   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
3305      instruction and can't have a prefix.  */
3306   if (inst_env->prefix_found)
3307     {
3308       inst_env->invalid = 1;
3309       return;
3310     }
3311
3312   /* Check if the instruction has PC as its target.  */
3313   if (cris_get_operand2 (inst) == REG_PC)
3314     {
3315       if (inst_env->slot_needed)
3316         {
3317           inst_env->invalid = 1;
3318           return;
3319         }
3320       operand1 = cris_get_quick_value (inst);
3321       operand2 = inst_env->reg[REG_PC];
3322
3323       /* The size should now be dword.  */
3324       cris_set_size_to_dword (&inst);
3325
3326       /* Calculate the PC value after the instruction, i.e. where the
3327          breakpoint should be.  */
3328       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3329     }
3330   inst_env->slot_needed = 0;
3331   inst_env->prefix_found = 0;
3332   inst_env->xflag_found = 0;
3333   inst_env->disable_interrupt = 0;
3334 }
3335
3336 /* Handles the quick addressing mode for the CMP, AND and OR instruction.  */
3337
3338 void 
3339 quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
3340 {
3341   unsigned long operand1;
3342   unsigned long operand2;
3343
3344   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
3345      instruction and can't have a prefix.  */
3346   if (inst_env->prefix_found)
3347     {
3348       inst_env->invalid = 1;
3349       return;
3350     }
3351   /* Check if the instruction has PC as its target.  */
3352   if (cris_get_operand2 (inst) == REG_PC)
3353     {
3354       if (inst_env->slot_needed)
3355         {
3356           inst_env->invalid = 1;
3357           return;
3358         }
3359       /* The instruction has the PC as its target register.  */
3360       operand1 = cris_get_quick_value (inst);
3361       operand2 = inst_env->reg[REG_PC];
3362
3363       /* The quick value is signed, so check if we must do a signed extend.  */
3364       if (operand1 & SIGNED_QUICK_VALUE_MASK)
3365         {
3366           /* sign extend  */
3367           operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
3368         }
3369       /* The size should now be dword.  */
3370       cris_set_size_to_dword (&inst);
3371
3372       /* Calculate the PC value after the instruction, i.e. where the
3373          breakpoint should be.  */
3374       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
3375     }
3376   inst_env->slot_needed = 0;
3377   inst_env->prefix_found = 0;
3378   inst_env->xflag_found = 0;
3379   inst_env->disable_interrupt = 0;
3380 }
3381
3382 /* Translate op_type to a function and call it.  */
3383
3384 static void cris_gdb_func (enum cris_op_type op_type, unsigned short inst, 
3385                            inst_env_type *inst_env)
3386 {
3387   switch (op_type)
3388     {
3389     case cris_not_implemented_op:
3390       not_implemented_op (inst, inst_env);
3391       break;
3392
3393     case cris_abs_op:
3394       abs_op (inst, inst_env);
3395       break;
3396
3397     case cris_addi_op:
3398       addi_op (inst, inst_env);
3399       break;
3400
3401     case cris_asr_op:
3402       asr_op (inst, inst_env);
3403       break;
3404
3405     case cris_asrq_op:
3406       asrq_op (inst, inst_env);
3407       break;
3408
3409     case cris_ax_ei_setf_op:
3410       ax_ei_setf_op (inst, inst_env);
3411       break;
3412
3413     case cris_bdap_prefix:
3414       bdap_prefix (inst, inst_env);
3415       break;
3416
3417     case cris_biap_prefix:
3418       biap_prefix (inst, inst_env);
3419       break;
3420
3421     case cris_break_op:
3422       break_op (inst, inst_env);
3423       break;
3424
3425     case cris_btst_nop_op:
3426       btst_nop_op (inst, inst_env);
3427       break;
3428
3429     case cris_clearf_di_op:
3430       clearf_di_op (inst, inst_env);
3431       break;
3432
3433     case cris_dip_prefix:
3434       dip_prefix (inst, inst_env);
3435       break;
3436
3437     case cris_dstep_logshift_mstep_neg_not_op:
3438       dstep_logshift_mstep_neg_not_op (inst, inst_env);
3439       break;
3440
3441     case cris_eight_bit_offset_branch_op:
3442       eight_bit_offset_branch_op (inst, inst_env);
3443       break;
3444
3445     case cris_move_mem_to_reg_movem_op:
3446       move_mem_to_reg_movem_op (inst, inst_env);
3447       break;
3448
3449     case cris_move_reg_to_mem_movem_op:
3450       move_reg_to_mem_movem_op (inst, inst_env);
3451       break;
3452
3453     case cris_move_to_preg_op:
3454       move_to_preg_op (inst, inst_env);
3455       break;
3456
3457     case cris_muls_op:
3458       muls_op (inst, inst_env);
3459       break;
3460
3461     case cris_mulu_op:
3462       mulu_op (inst, inst_env);
3463       break;
3464
3465     case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
3466       none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3467       break;
3468
3469     case cris_none_reg_mode_clear_test_op:
3470       none_reg_mode_clear_test_op (inst, inst_env);
3471       break;
3472
3473     case cris_none_reg_mode_jump_op:
3474       none_reg_mode_jump_op (inst, inst_env);
3475       break;
3476
3477     case cris_none_reg_mode_move_from_preg_op:
3478       none_reg_mode_move_from_preg_op (inst, inst_env);
3479       break;
3480
3481     case cris_quick_mode_add_sub_op:
3482       quick_mode_add_sub_op (inst, inst_env);
3483       break;
3484
3485     case cris_quick_mode_and_cmp_move_or_op:
3486       quick_mode_and_cmp_move_or_op (inst, inst_env);
3487       break;
3488
3489     case cris_quick_mode_bdap_prefix:
3490       quick_mode_bdap_prefix (inst, inst_env);
3491       break;
3492
3493     case cris_reg_mode_add_sub_cmp_and_or_move_op:
3494       reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
3495       break;
3496
3497     case cris_reg_mode_clear_op:
3498       reg_mode_clear_op (inst, inst_env);
3499       break;
3500
3501     case cris_reg_mode_jump_op:
3502       reg_mode_jump_op (inst, inst_env);
3503       break;
3504
3505     case cris_reg_mode_move_from_preg_op:
3506       reg_mode_move_from_preg_op (inst, inst_env);
3507       break;
3508
3509     case cris_reg_mode_test_op:
3510       reg_mode_test_op (inst, inst_env);
3511       break;
3512
3513     case cris_scc_op:
3514       scc_op (inst, inst_env);
3515       break;
3516
3517     case cris_sixteen_bit_offset_branch_op:
3518       sixteen_bit_offset_branch_op (inst, inst_env);
3519       break;
3520
3521     case cris_three_operand_add_sub_cmp_and_or_op:
3522       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
3523       break;
3524
3525     case cris_three_operand_bound_op:
3526       three_operand_bound_op (inst, inst_env);
3527       break;
3528
3529     case cris_two_operand_bound_op:
3530       two_operand_bound_op (inst, inst_env);
3531       break;
3532
3533     case cris_xor_op:
3534       xor_op (inst, inst_env);
3535       break;
3536     }
3537 }
3538
3539 /* This wrapper is to avoid cris_get_assembler being called before 
3540    exec_bfd has been set.  */
3541
3542 static int
3543 cris_delayed_get_disassembler (bfd_vma addr, disassemble_info *info)
3544 {
3545   tm_print_insn = cris_get_disassembler (exec_bfd);
3546   return TARGET_PRINT_INSN (addr, info);
3547 }
3548
3549 /* Copied from <asm/elf.h>.  */
3550 typedef unsigned long elf_greg_t;
3551
3552 /* Same as user_regs_struct struct in <asm/user.h>.  */
3553 typedef elf_greg_t elf_gregset_t[35];
3554
3555 /* Unpack an elf_gregset_t into GDB's register cache.  */
3556
3557 void 
3558 supply_gregset (elf_gregset_t *gregsetp)
3559 {
3560   int i;
3561   elf_greg_t *regp = *gregsetp;
3562   static char zerobuf[4] = {0};
3563
3564   /* The kernel dumps all 32 registers as unsigned longs, but supply_register
3565      knows about the actual size of each register so that's no problem.  */
3566   for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
3567     {
3568       supply_register (i, (char *)&regp[i]);
3569     }
3570 }
3571
3572 /*  Use a local version of this function to get the correct types for
3573     regsets, until multi-arch core support is ready.  */
3574
3575 static void
3576 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
3577                       int which, CORE_ADDR reg_addr)
3578 {
3579   elf_gregset_t gregset;
3580
3581   switch (which)
3582     {
3583     case 0:
3584       if (core_reg_size != sizeof (gregset))
3585         {
3586           warning ("wrong size gregset struct in core file");
3587         }
3588       else
3589         {
3590           memcpy (&gregset, core_reg_sect, sizeof (gregset));
3591           supply_gregset (&gregset);
3592         }
3593
3594     default:
3595       /* We've covered all the kinds of registers we know about here,
3596          so this must be something we wouldn't know what to do with
3597          anyway.  Just ignore it.  */
3598       break;
3599     }
3600 }
3601
3602 static struct core_fns cris_elf_core_fns =
3603 {
3604   bfd_target_elf_flavour,               /* core_flavour */
3605   default_check_format,                 /* check_format */
3606   default_core_sniffer,                 /* core_sniffer */
3607   fetch_core_registers,                 /* core_read_registers */
3608   NULL                                  /* next */
3609 };
3610
3611 /* Fetch (and possibly build) an appropriate link_map_offsets
3612    structure for native GNU/Linux CRIS targets using the struct
3613    offsets defined in link.h (but without actual reference to that
3614    file).
3615
3616    This makes it possible to access GNU/Linux CRIS shared libraries
3617    from a GDB that was not built on an GNU/Linux CRIS host (for cross
3618    debugging).
3619
3620    See gdb/solib-svr4.h for an explanation of these fields.  */
3621
3622 struct link_map_offsets *
3623 cris_linux_svr4_fetch_link_map_offsets (void)
3624
3625   static struct link_map_offsets lmo;
3626   static struct link_map_offsets *lmp = NULL;
3627
3628   if (lmp == NULL)
3629     { 
3630       lmp = &lmo;
3631
3632       lmo.r_debug_size = 8;     /* The actual size is 20 bytes, but
3633                                    this is all we need.  */
3634       lmo.r_map_offset = 4;
3635       lmo.r_map_size   = 4;
3636
3637       lmo.link_map_size = 20;
3638
3639       lmo.l_addr_offset = 0;
3640       lmo.l_addr_size   = 4;
3641
3642       lmo.l_name_offset = 4;
3643       lmo.l_name_size   = 4;
3644
3645       lmo.l_next_offset = 12;
3646       lmo.l_next_size   = 4;
3647
3648       lmo.l_prev_offset = 16;
3649       lmo.l_prev_size   = 4;
3650     }
3651
3652   return lmp;
3653 }
3654
3655 static void
3656 cris_fpless_backtrace (char *noargs, int from_tty)
3657 {
3658   /* Points at the instruction after the jsr (except when in innermost frame
3659      where it points at the original pc).  */
3660   CORE_ADDR pc = 0;
3661
3662   /* Temporary variable, used for parsing from the start of the function that
3663      the pc is in, up to the pc.  */
3664   CORE_ADDR tmp_pc = 0;
3665   CORE_ADDR sp = 0;
3666
3667   /* Information about current frame.  */
3668   struct symtab_and_line sal;
3669   char* func_name;
3670
3671   /* Present instruction.  */
3672   unsigned short insn;
3673   
3674   /* Next instruction, lookahead.  */
3675   unsigned short insn_next; 
3676
3677   /* This is to store the offset between sp at start of function and until we
3678      reach push srp (if any).  */
3679   int sp_add_later = 0;
3680   int push_srp_found = 0;
3681
3682   int val = 0;
3683
3684   /* Frame counter.  */
3685   int frame = 0;
3686
3687   /* For the innermost frame, we want to look at srp in case it's a leaf
3688      function (since there's no push srp in that case).  */
3689   int innermost_frame = 1;
3690   
3691   deprecated_read_register_gen (PC_REGNUM, (char *) &pc);
3692   deprecated_read_register_gen (SP_REGNUM, (char *) &sp);
3693   
3694   /* We make an explicit return when we can't find an outer frame.  */
3695   while (1)
3696     {
3697       /* Get file name and line number.  */
3698       sal = find_pc_line (pc, 0);
3699
3700       /* Get function name.  */
3701       find_pc_partial_function (pc, &func_name, (CORE_ADDR *) NULL,
3702                                 (CORE_ADDR *) NULL);
3703
3704       /* Print information about current frame.  */
3705       printf_unfiltered ("#%i  0x%08lx in %s", frame++, pc, func_name);
3706       if (sal.symtab)
3707         {    
3708           printf_unfiltered (" at %s:%i", sal.symtab->filename, sal.line);
3709         }
3710       printf_unfiltered ("\n");
3711       
3712       /* Get the start address of this function.  */
3713       tmp_pc = get_pc_function_start (pc);
3714   
3715       /* Mini parser, only meant to find push sp and sub ...,sp from the start
3716          of the function, up to the pc.  */
3717       while (tmp_pc < pc)
3718         {
3719           insn = read_memory_unsigned_integer (tmp_pc, sizeof (short));
3720           tmp_pc += sizeof (short);
3721           if (insn == 0xE1FC)
3722             {
3723               /* push <reg> 32 bit instruction */
3724               insn_next = read_memory_unsigned_integer (tmp_pc, 
3725                                                         sizeof (short));
3726               tmp_pc += sizeof (short);
3727
3728               /* Recognize srp.  */
3729               if (insn_next == 0xBE7E)
3730                 {
3731                   /* For subsequent (not this one though) push or sub which
3732                      affects sp, adjust sp immediately.  */
3733                   push_srp_found = 1;
3734
3735                   /* Note: this will break if we ever encounter a 
3736                      push vr (1 byte) or push ccr (2 bytes).  */
3737                   sp_add_later += 4;
3738                 }
3739               else
3740                 {
3741                   /* Some other register was pushed.  */
3742                   if (push_srp_found)
3743                     {    
3744                       sp += 4;
3745                     }
3746                   else
3747                     {
3748                       sp_add_later += 4;
3749                     }
3750                 }
3751             }
3752           else if (cris_get_operand2 (insn) == SP_REGNUM 
3753                    && cris_get_mode (insn) == 0x0000
3754                    && cris_get_opcode (insn) == 0x000A)
3755             {
3756               /* subq <val>,sp */
3757               val = cris_get_quick_value (insn);
3758
3759               if (push_srp_found)
3760                 {
3761                   sp += val;
3762                 }
3763               else
3764                 {
3765                   sp_add_later += val;
3766                 }
3767               
3768             }
3769           else if (cris_get_operand2 (insn) == SP_REGNUM
3770                    /* Autoincrement addressing mode.  */
3771                    && cris_get_mode (insn) == 0x0003
3772                    /* Opcode.  */
3773                    && ((insn) & 0x03E0) >> 5 == 0x0004)
3774             {
3775               /* subu <val>,sp */
3776               val = get_data_from_address (&insn, tmp_pc);
3777
3778               if (push_srp_found)
3779                 {
3780                   sp += val;
3781                 }
3782               else
3783                 {
3784                   sp_add_later += val;
3785                 }
3786             }
3787           else if (cris_get_operand2 (insn) == SP_REGNUM
3788                    && ((insn & 0x0F00) >> 8) == 0x0001
3789                    && (cris_get_signed_offset (insn) < 0))
3790             {
3791               /* Immediate byte offset addressing prefix word with sp as base 
3792                  register.  Used for CRIS v8 i.e. ETRAX 100 and newer if <val> 
3793                  is between 64 and 128. 
3794                  movem r<regsave>,[sp=sp-<val>] */
3795               val = -cris_get_signed_offset (insn);
3796               insn_next = read_memory_unsigned_integer (tmp_pc, 
3797                                                         sizeof (short));
3798               tmp_pc += sizeof (short);
3799               
3800               if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
3801                   && cris_get_opcode (insn_next) == 0x000F
3802                   && cris_get_size (insn_next) == 0x0003
3803                   && cris_get_operand1 (insn_next) == SP_REGNUM)
3804                 {             
3805                   if (push_srp_found)
3806                     {
3807                       sp += val;
3808                     }
3809                   else
3810                     {
3811                       sp_add_later += val;
3812                     }
3813                 }
3814             }
3815         }
3816       
3817       if (push_srp_found)
3818         {
3819           /* Reset flag.  */
3820           push_srp_found = 0;
3821
3822           /* sp should now point at where srp is stored on the stack.  Update
3823              the pc to the srp.  */
3824           pc = read_memory_unsigned_integer (sp, 4);
3825         }
3826       else if (innermost_frame)
3827         {
3828           /* We couldn't find a push srp in the prologue, so this must be
3829              a leaf function, and thus we use the srp register directly.
3830              This should happen at most once, for the innermost function.  */
3831           deprecated_read_register_gen (SRP_REGNUM, (char *) &pc);
3832         }
3833       else
3834         {
3835           /* Couldn't find an outer frame.  */
3836           return;
3837         }
3838
3839       /* Reset flag.  (In case the innermost frame wasn't a leaf, we don't
3840          want to look at the srp register later either).  */
3841       innermost_frame = 0;
3842
3843       /* Now, add the offset for everything up to, and including push srp,
3844          that was held back during the prologue parsing.  */ 
3845       sp += sp_add_later;
3846       sp_add_later = 0;
3847     }
3848 }
3849
3850 void
3851 _initialize_cris_tdep (void)
3852 {
3853   struct cmd_list_element *c;
3854
3855   gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);
3856   
3857   /* Used in disassembly.  */
3858   tm_print_insn = cris_delayed_get_disassembler;
3859
3860   /* CRIS-specific user-commands.  */
3861   c = add_set_cmd ("cris-version", class_support, var_integer, 
3862                    (char *) &usr_cmd_cris_version, 
3863                    "Set the current CRIS version.", &setlist);
3864   set_cmd_sfunc (c, cris_version_update);
3865   add_show_from_set (c, &showlist);
3866   
3867   c = add_set_enum_cmd ("cris-mode", class_support, cris_mode_enums, 
3868                         &usr_cmd_cris_mode, 
3869                         "Set the current CRIS mode.", &setlist);
3870   set_cmd_sfunc (c, cris_mode_update);
3871   add_show_from_set (c, &showlist);
3872
3873   c = add_set_enum_cmd ("cris-abi", class_support, cris_abi_enums, 
3874                         &usr_cmd_cris_abi, 
3875                         "Set the current CRIS ABI version.", &setlist);
3876   set_cmd_sfunc (c, cris_abi_update);
3877   add_show_from_set (c, &showlist);
3878
3879   c = add_cmd ("cris-fpless-backtrace", class_support, cris_fpless_backtrace, 
3880                "Display call chain using the subroutine return pointer.\n"
3881                "Note that this displays the address after the jump to the "
3882                "subroutine.", &cmdlist);
3883   
3884   add_core_fns (&cris_elf_core_fns);
3885   
3886 }
3887
3888 /* Prints out all target specific values.  */
3889
3890 static void
3891 cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3892 {
3893   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
3894   if (tdep != NULL)
3895     {
3896       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_version = %i\n",
3897                           tdep->cris_version);
3898       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
3899                           tdep->cris_mode);
3900       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_abi = %s\n",
3901                           tdep->cris_abi);
3902
3903     }
3904 }
3905
3906 static void
3907 cris_version_update (char *ignore_args, int from_tty, 
3908                      struct cmd_list_element *c)
3909 {
3910   struct gdbarch_info info;
3911
3912   /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3913      the set command passed as a parameter.  The clone operation will
3914      include (BUG?) any ``set'' command callback, if present.
3915      Commands like ``info set'' call all the ``show'' command
3916      callbacks.  Unfortunatly, for ``show'' commands cloned from
3917      ``set'', this includes callbacks belonging to ``set'' commands.
3918      Making this worse, this only occures if add_show_from_set() is
3919      called after add_cmd_sfunc() (BUG?).  */
3920
3921   /* From here on, trust the user's CRIS version setting.  */
3922   if (cmd_type (c) == set_cmd)
3923     {
3924       usr_cmd_cris_version_valid = 1;
3925   
3926       /* Update the current architecture, if needed.  */
3927       gdbarch_info_init (&info);
3928       if (!gdbarch_update_p (info))
3929         internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3930     }  
3931 }
3932
3933 static void
3934 cris_mode_update (char *ignore_args, int from_tty, 
3935                  struct cmd_list_element *c)
3936 {
3937   struct gdbarch_info info;
3938   
3939   /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3940      the set command passed as a parameter.  The clone operation will
3941      include (BUG?) any ``set'' command callback, if present.
3942      Commands like ``info set'' call all the ``show'' command
3943      callbacks.  Unfortunatly, for ``show'' commands cloned from
3944      ``set'', this includes callbacks belonging to ``set'' commands.
3945      Making this worse, this only occures if add_show_from_set() is
3946      called after add_cmd_sfunc() (BUG?).  */
3947
3948   /* From here on, trust the user's CRIS mode setting.  */
3949   if (cmd_type (c) == set_cmd)
3950     {
3951       usr_cmd_cris_mode_valid = 1;
3952   
3953       /* Update the current architecture, if needed.  */
3954       gdbarch_info_init (&info);
3955       if (!gdbarch_update_p (info))
3956         internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3957     }
3958 }
3959
3960 static void
3961 cris_abi_update (char *ignore_args, int from_tty, 
3962                  struct cmd_list_element *c)
3963 {
3964   struct gdbarch_info info;
3965   
3966   /* NOTE: cagney/2002-03-17: The add_show_from_set() function clones
3967      the set command passed as a parameter.  The clone operation will
3968      include (BUG?) any ``set'' command callback, if present.
3969      Commands like ``info set'' call all the ``show'' command
3970      callbacks.  Unfortunatly, for ``show'' commands cloned from
3971      ``set'', this includes callbacks belonging to ``set'' commands.
3972      Making this worse, this only occures if add_show_from_set() is
3973      called after add_cmd_sfunc() (BUG?).  */
3974
3975   /* From here on, trust the user's CRIS ABI setting.  */
3976   if (cmd_type (c) == set_cmd)
3977     {
3978       usr_cmd_cris_abi_valid = 1;
3979   
3980       /* Update the current architecture, if needed.  */
3981       gdbarch_info_init (&info);
3982       if (!gdbarch_update_p (info))
3983         internal_error (__FILE__, __LINE__, "cris_gdbarch_update: failed to update architecture.");
3984     }
3985 }
3986
3987 /* Copied from pa64solib.c, with a couple of minor changes.  */
3988
3989 static CORE_ADDR
3990 bfd_lookup_symbol (bfd *abfd, const char *symname)
3991 {
3992   unsigned int storage_needed;
3993   asymbol *sym;
3994   asymbol **symbol_table;
3995   unsigned int number_of_symbols;
3996   unsigned int i;
3997   struct cleanup *back_to;
3998   CORE_ADDR symaddr = 0;
3999
4000   storage_needed = bfd_get_symtab_upper_bound (abfd);
4001
4002   if (storage_needed > 0)
4003     {
4004       symbol_table = (asymbol **) xmalloc (storage_needed);
4005       back_to = make_cleanup (free, (PTR) symbol_table);
4006       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
4007
4008       for (i = 0; i < number_of_symbols; i++)
4009         {
4010           sym = *symbol_table++;
4011           if (!strcmp (sym->name, symname))
4012             {
4013               /* Bfd symbols are section relative.  */
4014               symaddr = sym->value + sym->section->vma;
4015               break;
4016             }
4017         }
4018       do_cleanups (back_to);
4019     }
4020   return (symaddr);
4021 }
4022
4023 static struct gdbarch *
4024 cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
4025 {
4026   struct gdbarch *gdbarch;
4027   struct gdbarch_tdep *tdep;
4028   int cris_version;
4029   const char *cris_mode;
4030   const char *cris_abi;
4031   CORE_ADDR cris_abi_sym = 0;
4032   int register_bytes;
4033
4034   if (usr_cmd_cris_version_valid)
4035     {
4036       /* Trust the user's CRIS version setting.  */ 
4037       cris_version = usr_cmd_cris_version;
4038     }
4039   else
4040     {
4041       /* Assume it's CRIS version 10.  */
4042       cris_version = 10;
4043     }
4044
4045   if (usr_cmd_cris_mode_valid)
4046     {
4047       /* Trust the user's CRIS mode setting.  */ 
4048       cris_mode = usr_cmd_cris_mode;
4049     }
4050   else if (cris_version == 10)
4051     {
4052       /* Assume CRIS version 10 is in user mode.  */
4053       cris_mode = CRIS_MODE_USER;
4054     }
4055   else
4056     {
4057       /* Strictly speaking, older CRIS version don't have a supervisor mode,
4058          but we regard its only mode as supervisor mode.  */
4059       cris_mode = CRIS_MODE_SUPERVISOR;
4060     }
4061
4062   if (usr_cmd_cris_abi_valid)
4063     {
4064       /* Trust the user's ABI setting.  */
4065       cris_abi = usr_cmd_cris_abi;
4066     }
4067   else if (info.abfd)
4068     {
4069       if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
4070         {
4071           /* An elf target uses the new ABI.  */
4072           cris_abi = CRIS_ABI_V2;
4073         }
4074       else if (bfd_get_flavour (info.abfd) == bfd_target_aout_flavour)
4075         {
4076           /* An a.out target may use either ABI.  Look for hints in the
4077              symbol table.  */
4078           cris_abi_sym = bfd_lookup_symbol (info.abfd, CRIS_ABI_SYMBOL);
4079           cris_abi = cris_abi_sym ? CRIS_ABI_V2 : CRIS_ABI_ORIGINAL;
4080         }
4081       else
4082         {
4083           /* Unknown bfd flavour.  Assume it's the new ABI.  */
4084           cris_abi = CRIS_ABI_V2;
4085         }
4086     }
4087   else if (arches != NULL)
4088     {
4089       /* No bfd available.  Stick with the ABI from the most recently
4090          selected architecture of this same family (the head of arches
4091          always points to this).  (This is to avoid changing the ABI
4092          when the user updates the architecture with the 'set
4093          cris-version' command.)  */
4094       cris_abi = gdbarch_tdep (arches->gdbarch)->cris_abi;
4095     }
4096   else
4097     {
4098       /* No bfd, and no previously selected architecture available.
4099          Assume it's the new ABI.  */
4100       cris_abi = CRIS_ABI_V2;
4101     }
4102
4103   /* Make the current settings visible to the user.  */
4104   usr_cmd_cris_version = cris_version;
4105   usr_cmd_cris_mode = cris_mode;
4106   usr_cmd_cris_abi = cris_abi;
4107   
4108   /* Find a candidate among the list of pre-declared architectures.  Both
4109      CRIS version and ABI must match.  */
4110   for (arches = gdbarch_list_lookup_by_info (arches, &info); 
4111        arches != NULL;
4112        arches = gdbarch_list_lookup_by_info (arches->next, &info))
4113     {
4114       if ((gdbarch_tdep (arches->gdbarch)->cris_version == cris_version)
4115           && (gdbarch_tdep (arches->gdbarch)->cris_mode == cris_mode)
4116           && (gdbarch_tdep (arches->gdbarch)->cris_abi == cris_abi))
4117         return arches->gdbarch;
4118     }
4119
4120   /* No matching architecture was found.  Create a new one.  */
4121   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
4122   gdbarch = gdbarch_alloc (&info, tdep);
4123
4124   /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
4125      ready to unwind the PC first (see frame.c:get_prev_frame()).  */
4126   set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
4127
4128   tdep->cris_version = cris_version;
4129   tdep->cris_mode = cris_mode;
4130   tdep->cris_abi = cris_abi;
4131
4132   /* INIT shall ensure that the INFO.BYTE_ORDER is non-zero.  */
4133   switch (info.byte_order)
4134     {
4135     case BFD_ENDIAN_LITTLE:
4136       /* Ok.  */
4137       break;
4138
4139     case BFD_ENDIAN_BIG:
4140       internal_error (__FILE__, __LINE__, "cris_gdbarch_init: big endian byte order in info");
4141       break;
4142     
4143     default:
4144       internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown byte order in info");
4145     }
4146
4147   /* Initialize the ABI dependent things.  */
4148   if (tdep->cris_abi == CRIS_ABI_ORIGINAL)
4149     {
4150       set_gdbarch_double_bit (gdbarch, 32);
4151       set_gdbarch_push_arguments (gdbarch, cris_abi_original_push_arguments);
4152       set_gdbarch_deprecated_store_return_value (gdbarch, 
4153                                       cris_abi_original_store_return_value);
4154       set_gdbarch_deprecated_extract_return_value 
4155         (gdbarch, cris_abi_original_extract_return_value);
4156       set_gdbarch_reg_struct_has_addr 
4157         (gdbarch, cris_abi_original_reg_struct_has_addr);
4158     }
4159   else if (tdep->cris_abi == CRIS_ABI_V2)
4160     {
4161       set_gdbarch_double_bit (gdbarch, 64);
4162       set_gdbarch_push_arguments (gdbarch, cris_abi_v2_push_arguments);
4163       set_gdbarch_deprecated_store_return_value (gdbarch, cris_abi_v2_store_return_value);
4164       set_gdbarch_deprecated_extract_return_value
4165         (gdbarch, cris_abi_v2_extract_return_value);
4166       set_gdbarch_reg_struct_has_addr (gdbarch, 
4167                                        cris_abi_v2_reg_struct_has_addr);
4168     }
4169   else
4170     internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown CRIS ABI");
4171
4172   /* The default definition of a long double is 2 * TARGET_DOUBLE_BIT,
4173      which means we have to set this explicitly.  */
4174   set_gdbarch_long_double_bit (gdbarch, 64);
4175     
4176   /* There are 32 registers (some of which may not be implemented).  */
4177   set_gdbarch_num_regs (gdbarch, 32);
4178   set_gdbarch_sp_regnum (gdbarch, 14);
4179   set_gdbarch_fp_regnum (gdbarch, 8);
4180   set_gdbarch_pc_regnum (gdbarch, 15);
4181
4182   set_gdbarch_register_name (gdbarch, cris_register_name);
4183   
4184   /* Length of ordinary registers used in push_word and a few other places. 
4185      REGISTER_RAW_SIZE is the real way to know how big a register is.  */
4186   set_gdbarch_register_size (gdbarch, 4);
4187   
4188   /* NEW */
4189   set_gdbarch_register_bytes_ok (gdbarch, cris_register_bytes_ok);
4190   set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
4191
4192   
4193   set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
4194   set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);
4195
4196
4197   /* The total amount of space needed to store (in an array called registers)
4198      GDB's copy of the machine's register state.  Note: We can not use
4199      cris_register_size at this point, since it relies on current_gdbarch
4200      being set.  */
4201   switch (tdep->cris_version)
4202     {
4203     case 0:
4204     case 1:
4205     case 2:
4206     case 3:
4207       /* Support for these may be added later.  */
4208       internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unsupported CRIS version");
4209       break;
4210       
4211     case 8:
4212     case 9:
4213       /* CRIS v8 and v9, a.k.a. ETRAX 100.  General registers R0 - R15 
4214          (32 bits), special registers P0 - P1 (8 bits), P4 - P5 (16 bits), 
4215          and P8 - P14 (32 bits).  */
4216       register_bytes = (16 * 4) + (2 * 1) + (2 * 2) + (7 * 4);
4217       break;
4218
4219     case 10:
4220     case 11: 
4221       /* CRIS v10 and v11, a.k.a. ETRAX 100LX.  In addition to ETRAX 100, 
4222          P7 (32 bits), and P15 (32 bits) have been implemented.  */
4223       register_bytes = (16 * 4) + (2 * 1) + (2 * 2) + (9 * 4);
4224       break;
4225
4226     default:
4227       internal_error (__FILE__, __LINE__, "cris_gdbarch_init: unknown CRIS version");
4228     }
4229
4230   set_gdbarch_register_bytes (gdbarch, register_bytes);
4231
4232   /* Returns the register offset for the first byte of register regno's space 
4233      in the saved register state.  */
4234   set_gdbarch_register_byte (gdbarch, cris_register_offset);
4235   
4236   /* The length of the registers in the actual machine representation.  */
4237   set_gdbarch_register_raw_size (gdbarch, cris_register_size);
4238   
4239   /* The largest value REGISTER_RAW_SIZE can have.  */
4240   set_gdbarch_max_register_raw_size (gdbarch, 32);
4241   
4242   /* The length of the registers in the program's representation.  */
4243   set_gdbarch_register_virtual_size (gdbarch, cris_register_size);
4244   
4245   /* The largest value REGISTER_VIRTUAL_SIZE can have.  */
4246   set_gdbarch_max_register_virtual_size (gdbarch, 32);
4247
4248   set_gdbarch_register_virtual_type (gdbarch, cris_register_virtual_type);
4249   
4250   /* Use generic dummy frames.  */
4251   
4252   /* Where to execute the call in the memory segments.  */
4253   set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
4254   
4255   /* Start execution at the beginning of dummy.  */
4256   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
4257   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
4258   
4259   /* Set to 1 since call_dummy_breakpoint_offset was defined.  */
4260   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
4261   
4262   /* Read all about dummy frames in blockframe.c.  */
4263   set_gdbarch_call_dummy_length (gdbarch, 0);
4264   set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_at_entry_point);
4265   
4266   /* Defined to 1 to indicate that the target supports inferior function 
4267      calls.  */
4268   set_gdbarch_call_dummy_p (gdbarch, 1);
4269   set_gdbarch_call_dummy_words (gdbarch, 0);
4270   set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
4271   
4272   /* No stack adjustment needed when peforming an inferior function call.  */
4273   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
4274   set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
4275
4276   set_gdbarch_get_saved_register (gdbarch, deprecated_generic_get_saved_register);
4277   
4278   /* No register requires conversion from raw format to virtual format.  */
4279   set_gdbarch_register_convertible (gdbarch, generic_register_convertible_not);
4280
4281   set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
4282   set_gdbarch_push_return_address (gdbarch, cris_push_return_address);
4283   set_gdbarch_pop_frame (gdbarch, cris_pop_frame);
4284
4285   set_gdbarch_store_struct_return (gdbarch, cris_store_struct_return);
4286   set_gdbarch_deprecated_extract_struct_value_address
4287     (gdbarch, cris_extract_struct_value_address);
4288   set_gdbarch_use_struct_convention (gdbarch, cris_use_struct_convention);
4289
4290   set_gdbarch_frame_init_saved_regs (gdbarch, cris_frame_init_saved_regs);
4291   set_gdbarch_init_extra_frame_info (gdbarch, cris_init_extra_frame_info);
4292   set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);
4293   set_gdbarch_prologue_frameless_p (gdbarch, generic_prologue_frameless_p);
4294   
4295   /* The stack grows downward.  */
4296   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
4297
4298   set_gdbarch_breakpoint_from_pc (gdbarch, cris_breakpoint_from_pc);
4299   
4300   /* The PC must not be decremented after a breakpoint.  (The breakpoint
4301      handler takes care of that.)  */
4302   set_gdbarch_decr_pc_after_break (gdbarch, 0);
4303   
4304   /* Offset from address of function to start of its code.  */
4305   set_gdbarch_function_start_offset (gdbarch, 0);  
4306   
4307   /* The number of bytes at the start of arglist that are not really args,
4308      0 in the CRIS ABI.  */
4309   set_gdbarch_frame_args_skip (gdbarch, 0);
4310   set_gdbarch_frameless_function_invocation 
4311     (gdbarch, cris_frameless_function_invocation);
4312   set_gdbarch_frame_chain (gdbarch, cris_frame_chain);
4313
4314   set_gdbarch_frame_saved_pc (gdbarch, cris_frame_saved_pc);
4315   set_gdbarch_saved_pc_after_call (gdbarch, cris_saved_pc_after_call);
4316
4317   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
4318   
4319   /* No extra stack alignment needed.  Set to 1 by default.  */
4320   set_gdbarch_extra_stack_alignment_needed (gdbarch, 0);
4321   
4322   /* Helpful for backtracing and returning in a call dummy.  */
4323   set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
4324
4325   /* Use target_specific function to define link map offsets.  */
4326   set_solib_svr4_fetch_link_map_offsets 
4327     (gdbarch, cris_linux_svr4_fetch_link_map_offsets);
4328   
4329   return gdbarch;
4330 }
This page took 0.263153 seconds and 4 git commands to generate.