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