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