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