gdb/cris-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target dependent code for CRIS, for GDB, the GNU debugger.

  2.    Copyright (C) 2001-2015 Free Software Foundation, Inc.

  3.    Contributed by Axis Communications AB.
  4.    Written by Hendrik Ruijter, Stefan Andersson, and Orjan Friberg.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. #include "defs.h"
  17. #include "frame.h"
  18. #include "frame-unwind.h"
  19. #include "frame-base.h"
  20. #include "trad-frame.h"
  21. #include "dwarf2-frame.h"
  22. #include "symtab.h"
  23. #include "inferior.h"
  24. #include "gdbtypes.h"
  25. #include "gdbcore.h"
  26. #include "gdbcmd.h"
  27. #include "target.h"
  28. #include "value.h"
  29. #include "opcode/cris.h"
  30. #include "osabi.h"
  31. #include "arch-utils.h"
  32. #include "regcache.h"

  33. #include "objfiles.h"

  34. #include "solib.h"              /* Support for shared libraries.  */
  35. #include "solib-svr4.h"
  36. #include "dis-asm.h"

  37. #include "cris-tdep.h"

  38. enum cris_num_regs
  39. {
  40.   /* There are no floating point registers.  Used in gdbserver low-linux.c.  */
  41.   NUM_FREGS = 0,

  42.   /* There are 16 general registers.  */
  43.   NUM_GENREGS = 16,

  44.   /* There are 16 special registers.  */
  45.   NUM_SPECREGS = 16,

  46.   /* CRISv32 has a pseudo PC register, not noted here.  */

  47.   /* CRISv32 has 16 support registers.  */
  48.   NUM_SUPPREGS = 16
  49. };

  50. /* Register numbers of various important registers.
  51.    CRIS_FP_REGNUM   Contains address of executing stack frame.
  52.    STR_REGNUM  Contains the address of structure return values.
  53.    RET_REGNUM  Contains the return value when shorter than or equal to 32 bits
  54.    ARG1_REGNUM Contains the first parameter to a function.
  55.    ARG2_REGNUM Contains the second parameter to a function.
  56.    ARG3_REGNUM Contains the third parameter to a function.
  57.    ARG4_REGNUM Contains the fourth parameter to a function.  Rest on stack.
  58.    gdbarch_sp_regnum Contains address of top of stack.
  59.    gdbarch_pc_regnum Contains address of next instruction.
  60.    SRP_REGNUM  Subroutine return pointer register.
  61.    BRP_REGNUM  Breakpoint return pointer register.  */

  62. enum cris_regnums
  63. {
  64.   /* Enums with respect to the general registers, valid for all
  65.      CRIS versions.  The frame pointer is always in R8.  */
  66.   CRIS_FP_REGNUM = 8,
  67.   /* ABI related registers.  */
  68.   STR_REGNUM  = 9,
  69.   RET_REGNUM  = 10,
  70.   ARG1_REGNUM = 10,
  71.   ARG2_REGNUM = 11,
  72.   ARG3_REGNUM = 12,
  73.   ARG4_REGNUM = 13,

  74.   /* Registers which happen to be common.  */
  75.   VR_REGNUM   = 17,
  76.   MOF_REGNUM  = 23,
  77.   SRP_REGNUM  = 27,

  78.   /* CRISv10 et al. specific registers.  */
  79.   P0_REGNUM   = 16,
  80.   P4_REGNUM   = 20,
  81.   CCR_REGNUM  = 21,
  82.   P8_REGNUM   = 24,
  83.   IBR_REGNUM  = 25,
  84.   IRP_REGNUM  = 26,
  85.   BAR_REGNUM  = 28,
  86.   DCCR_REGNUM = 29,
  87.   BRP_REGNUM  = 30,
  88.   USP_REGNUM  = 31,

  89.   /* CRISv32 specific registers.  */
  90.   ACR_REGNUM  = 15,
  91.   BZ_REGNUM   = 16,
  92.   PID_REGNUM  = 18,
  93.   SRS_REGNUM  = 19,
  94.   WZ_REGNUM   = 20,
  95.   EXS_REGNUM  = 21,
  96.   EDA_REGNUM  = 22,
  97.   DZ_REGNUM   = 24,
  98.   EBP_REGNUM  = 25,
  99.   ERP_REGNUM  = 26,
  100.   NRP_REGNUM  = 28,
  101.   CCS_REGNUM  = 29,
  102.   CRISV32USP_REGNUM  = 30, /* Shares name but not number with CRISv10.  */
  103.   SPC_REGNUM  = 31,
  104.   CRISV32PC_REGNUM   = 32, /* Shares name but not number with CRISv10.  */

  105.   S0_REGNUM = 33,
  106.   S1_REGNUM = 34,
  107.   S2_REGNUM = 35,
  108.   S3_REGNUM = 36,
  109.   S4_REGNUM = 37,
  110.   S5_REGNUM = 38,
  111.   S6_REGNUM = 39,
  112.   S7_REGNUM = 40,
  113.   S8_REGNUM = 41,
  114.   S9_REGNUM = 42,
  115.   S10_REGNUM = 43,
  116.   S11_REGNUM = 44,
  117.   S12_REGNUM = 45,
  118.   S13_REGNUM = 46,
  119.   S14_REGNUM = 47,
  120.   S15_REGNUM = 48,
  121. };

  122. extern const struct cris_spec_reg cris_spec_regs[];

  123. /* CRIS version, set via the user command 'set cris-version'.  Affects
  124.    register names and sizes.  */
  125. static unsigned int usr_cmd_cris_version;

  126. /* Indicates whether to trust the above variable.  */
  127. static int usr_cmd_cris_version_valid = 0;

  128. static const char cris_mode_normal[] = "normal";
  129. static const char cris_mode_guru[] = "guru";
  130. static const char *const cris_modes[] = {
  131.   cris_mode_normal,
  132.   cris_mode_guru,
  133.   0
  134. };

  135. /* CRIS mode, set via the user command 'set cris-mode'.  Affects
  136.    type of break instruction among other things.  */
  137. static const char *usr_cmd_cris_mode = cris_mode_normal;

  138. /* Whether to make use of Dwarf-2 CFI (default on).  */
  139. static int usr_cmd_cris_dwarf2_cfi = 1;

  140. /* Sigtramp identification code copied from i386-linux-tdep.c.  */

  141. #define SIGTRAMP_INSN0    0x9c5f  /* movu.w 0xXX, $r9 */
  142. #define SIGTRAMP_OFFSET0  0
  143. #define SIGTRAMP_INSN1    0xe93d  /* break 13 */
  144. #define SIGTRAMP_OFFSET1  4

  145. static const unsigned short sigtramp_code[] =
  146. {
  147.   SIGTRAMP_INSN0, 0x0077/* movu.w $0x77, $r9 */
  148.   SIGTRAMP_INSN1           /* break 13 */
  149. };

  150. #define SIGTRAMP_LEN (sizeof sigtramp_code)

  151. /* Note: same length as normal sigtramp code.  */

  152. static const unsigned short rt_sigtramp_code[] =
  153. {
  154.   SIGTRAMP_INSN0, 0x00ad/* movu.w $0xad, $r9 */
  155.   SIGTRAMP_INSN1           /* break 13 */
  156. };

  157. /* If PC is in a sigtramp routine, return the address of the start of
  158.    the routine.  Otherwise, return 0.  */

  159. static CORE_ADDR
  160. cris_sigtramp_start (struct frame_info *this_frame)
  161. {
  162.   CORE_ADDR pc = get_frame_pc (this_frame);
  163.   gdb_byte buf[SIGTRAMP_LEN];

  164.   if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
  165.     return 0;

  166.   if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
  167.     {
  168.       if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
  169.         return 0;

  170.       pc -= SIGTRAMP_OFFSET1;
  171.       if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
  172.         return 0;
  173.     }

  174.   if (memcmp (buf, sigtramp_code, SIGTRAMP_LEN) != 0)
  175.     return 0;

  176.   return pc;
  177. }

  178. /* If PC is in a RT sigtramp routine, return the address of the start of
  179.    the routine.  Otherwise, return 0.  */

  180. static CORE_ADDR
  181. cris_rt_sigtramp_start (struct frame_info *this_frame)
  182. {
  183.   CORE_ADDR pc = get_frame_pc (this_frame);
  184.   gdb_byte buf[SIGTRAMP_LEN];

  185.   if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
  186.     return 0;

  187.   if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
  188.     {
  189.       if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
  190.         return 0;

  191.       pc -= SIGTRAMP_OFFSET1;
  192.       if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
  193.         return 0;
  194.     }

  195.   if (memcmp (buf, rt_sigtramp_code, SIGTRAMP_LEN) != 0)
  196.     return 0;

  197.   return pc;
  198. }

  199. /* Assuming THIS_FRAME is a frame for a GNU/Linux sigtramp routine,
  200.    return the address of the associated sigcontext structure.  */

  201. static CORE_ADDR
  202. cris_sigcontext_addr (struct frame_info *this_frame)
  203. {
  204.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  205.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  206.   CORE_ADDR pc;
  207.   CORE_ADDR sp;
  208.   gdb_byte buf[4];

  209.   get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
  210.   sp = extract_unsigned_integer (buf, 4, byte_order);

  211.   /* Look for normal sigtramp frame first.  */
  212.   pc = cris_sigtramp_start (this_frame);
  213.   if (pc)
  214.     {
  215.       /* struct signal_frame (arch/cris/kernel/signal.c) contains
  216.          struct sigcontext as its first member, meaning the SP points to
  217.          it already.  */
  218.       return sp;
  219.     }

  220.   pc = cris_rt_sigtramp_start (this_frame);
  221.   if (pc)
  222.     {
  223.       /* struct rt_signal_frame (arch/cris/kernel/signal.c) contains
  224.          a struct ucontext, which in turn contains a struct sigcontext.
  225.          Magic digging:
  226.          4 + 4 + 128 to struct ucontext, then
  227.          4 + 4 + 12 to struct sigcontext.  */
  228.       return (sp + 156);
  229.     }

  230.   error (_("Couldn't recognize signal trampoline."));
  231.   return 0;
  232. }

  233. struct cris_unwind_cache
  234. {
  235.   /* The previous frame's inner most stack address.  Used as this
  236.      frame ID's stack_addr.  */
  237.   CORE_ADDR prev_sp;
  238.   /* The frame's base, optionally used by the high-level debug info.  */
  239.   CORE_ADDR base;
  240.   int size;
  241.   /* How far the SP and r8 (FP) have been offset from the start of
  242.      the stack frame (as defined by the previous frame's stack
  243.      pointer).  */
  244.   LONGEST sp_offset;
  245.   LONGEST r8_offset;
  246.   int uses_frame;

  247.   /* From old frame_extra_info struct.  */
  248.   CORE_ADDR return_pc;
  249.   int leaf_function;

  250.   /* Table indicating the location of each and every register.  */
  251.   struct trad_frame_saved_reg *saved_regs;
  252. };

  253. static struct cris_unwind_cache *
  254. cris_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
  255.                                   void **this_cache)
  256. {
  257.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  258.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  259.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  260.   struct cris_unwind_cache *info;
  261.   CORE_ADDR addr;
  262.   gdb_byte buf[4];
  263.   int i;

  264.   if ((*this_cache))
  265.     return (*this_cache);

  266.   info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
  267.   (*this_cache) = info;
  268.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  269.   /* Zero all fields.  */
  270.   info->prev_sp = 0;
  271.   info->base = 0;
  272.   info->size = 0;
  273.   info->sp_offset = 0;
  274.   info->r8_offset = 0;
  275.   info->uses_frame = 0;
  276.   info->return_pc = 0;
  277.   info->leaf_function = 0;

  278.   get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
  279.   info->base = extract_unsigned_integer (buf, 4, byte_order);

  280.   addr = cris_sigcontext_addr (this_frame);

  281.   /* Layout of the sigcontext struct:
  282.      struct sigcontext {
  283.         struct pt_regs regs;
  284.         unsigned long oldmask;
  285.         unsigned long usp;
  286.      }; */

  287.   if (tdep->cris_version == 10)
  288.     {
  289.       /* R0 to R13 are stored in reverse order at offset (2 * 4) in
  290.          struct pt_regs.  */
  291.       for (i = 0; i <= 13; i++)
  292.         info->saved_regs[i].addr = addr + ((15 - i) * 4);

  293.       info->saved_regs[MOF_REGNUM].addr = addr + (16 * 4);
  294.       info->saved_regs[DCCR_REGNUM].addr = addr + (17 * 4);
  295.       info->saved_regs[SRP_REGNUM].addr = addr + (18 * 4);
  296.       /* Note: IRP is off by 2 at this point.  There's no point in correcting
  297.          it though since that will mean that the backtrace will show a PC
  298.          different from what is shown when stopped.  */
  299.       info->saved_regs[IRP_REGNUM].addr = addr + (19 * 4);
  300.       info->saved_regs[gdbarch_pc_regnum (gdbarch)]
  301.         = info->saved_regs[IRP_REGNUM];
  302.       info->saved_regs[gdbarch_sp_regnum (gdbarch)].addr = addr + (24 * 4);
  303.     }
  304.   else
  305.     {
  306.       /* CRISv32.  */
  307.       /* R0 to R13 are stored in order at offset (1 * 4) in
  308.          struct pt_regs.  */
  309.       for (i = 0; i <= 13; i++)
  310.         info->saved_regs[i].addr = addr + ((i + 1) * 4);

  311.       info->saved_regs[ACR_REGNUM].addr = addr + (15 * 4);
  312.       info->saved_regs[SRS_REGNUM].addr = addr + (16 * 4);
  313.       info->saved_regs[MOF_REGNUM].addr = addr + (17 * 4);
  314.       info->saved_regs[SPC_REGNUM].addr = addr + (18 * 4);
  315.       info->saved_regs[CCS_REGNUM].addr = addr + (19 * 4);
  316.       info->saved_regs[SRP_REGNUM].addr = addr + (20 * 4);
  317.       info->saved_regs[ERP_REGNUM].addr = addr + (21 * 4);
  318.       info->saved_regs[EXS_REGNUM].addr = addr + (22 * 4);
  319.       info->saved_regs[EDA_REGNUM].addr = addr + (23 * 4);

  320.       /* FIXME: If ERP is in a delay slot at this point then the PC will
  321.          be wrong at this point.  This problem manifests itself in the
  322.          sigaltstack.exp test case, which occasionally generates FAILs when
  323.          the signal is received while in a delay slot.

  324.          This could be solved by a couple of read_memory_unsigned_integer and a
  325.          trad_frame_set_value.  */
  326.       info->saved_regs[gdbarch_pc_regnum (gdbarch)]
  327.         = info->saved_regs[ERP_REGNUM];

  328.       info->saved_regs[gdbarch_sp_regnum (gdbarch)].addr
  329.         = addr + (25 * 4);
  330.     }

  331.   return info;
  332. }

  333. static void
  334. cris_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
  335.                              struct frame_id *this_id)
  336. {
  337.   struct cris_unwind_cache *cache =
  338.     cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
  339.   (*this_id) = frame_id_build (cache->base, get_frame_pc (this_frame));
  340. }

  341. /* Forward declaration.  */

  342. static struct value *cris_frame_prev_register (struct frame_info *this_frame,
  343.                                                void **this_cache, int regnum);
  344. static struct value *
  345. cris_sigtramp_frame_prev_register (struct frame_info *this_frame,
  346.                                    void **this_cache, int regnum)
  347. {
  348.   /* Make sure we've initialized the cache.  */
  349.   cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
  350.   return cris_frame_prev_register (this_frame, this_cache, regnum);
  351. }

  352. static int
  353. cris_sigtramp_frame_sniffer (const struct frame_unwind *self,
  354.                              struct frame_info *this_frame,
  355.                              void **this_cache)
  356. {
  357.   if (cris_sigtramp_start (this_frame)
  358.       || cris_rt_sigtramp_start (this_frame))
  359.     return 1;

  360.   return 0;
  361. }

  362. static const struct frame_unwind cris_sigtramp_frame_unwind =
  363. {
  364.   SIGTRAMP_FRAME,
  365.   default_frame_unwind_stop_reason,
  366.   cris_sigtramp_frame_this_id,
  367.   cris_sigtramp_frame_prev_register,
  368.   NULL,
  369.   cris_sigtramp_frame_sniffer
  370. };

  371. static int
  372. crisv32_single_step_through_delay (struct gdbarch *gdbarch,
  373.                                    struct frame_info *this_frame)
  374. {
  375.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  376.   ULONGEST erp;
  377.   int ret = 0;

  378.   if (tdep->cris_mode == cris_mode_guru)
  379.     erp = get_frame_register_unsigned (this_frame, NRP_REGNUM);
  380.   else
  381.     erp = get_frame_register_unsigned (this_frame, ERP_REGNUM);

  382.   if (erp & 0x1)
  383.     {
  384.       /* In delay slot - check if there's a breakpoint at the preceding
  385.          instruction.  */
  386.       if (breakpoint_here_p (get_frame_address_space (this_frame), erp & ~0x1))
  387.         ret = 1;
  388.     }
  389.   return ret;
  390. }

  391. /* The instruction environment needed to find single-step breakpoints.  */

  392. typedef
  393. struct instruction_environment
  394. {
  395.   unsigned long reg[NUM_GENREGS];
  396.   unsigned long preg[NUM_SPECREGS];
  397.   unsigned long branch_break_address;
  398.   unsigned long delay_slot_pc;
  399.   unsigned long prefix_value;
  400.   int   branch_found;
  401.   int   prefix_found;
  402.   int   invalid;
  403.   int   slot_needed;
  404.   int   delay_slot_pc_active;
  405.   int   xflag_found;
  406.   int   disable_interrupt;
  407.   int   byte_order;
  408. } inst_env_type;

  409. /* Machine-dependencies in CRIS for opcodes.  */

  410. /* Instruction sizes.  */
  411. enum cris_instruction_sizes
  412. {
  413.   INST_BYTE_SIZE  = 0,
  414.   INST_WORD_SIZE  = 1,
  415.   INST_DWORD_SIZE = 2
  416. };

  417. /* Addressing modes.  */
  418. enum cris_addressing_modes
  419. {
  420.   REGISTER_MODE = 1,
  421.   INDIRECT_MODE = 2,
  422.   AUTOINC_MODE  = 3
  423. };

  424. /* Prefix addressing modes.  */
  425. enum cris_prefix_addressing_modes
  426. {
  427.   PREFIX_INDEX_MODE  = 2,
  428.   PREFIX_ASSIGN_MODE = 3,

  429.   /* Handle immediate byte offset addressing mode prefix format.  */
  430.   PREFIX_OFFSET_MODE = 2
  431. };

  432. /* Masks for opcodes.  */
  433. enum cris_opcode_masks
  434. {
  435.   BRANCH_SIGNED_SHORT_OFFSET_MASK = 0x1,
  436.   SIGNED_EXTEND_BIT_MASK          = 0x2,
  437.   SIGNED_BYTE_MASK                = 0x80,
  438.   SIGNED_BYTE_EXTEND_MASK         = 0xFFFFFF00,
  439.   SIGNED_WORD_MASK                = 0x8000,
  440.   SIGNED_WORD_EXTEND_MASK         = 0xFFFF0000,
  441.   SIGNED_DWORD_MASK               = 0x80000000,
  442.   SIGNED_QUICK_VALUE_MASK         = 0x20,
  443.   SIGNED_QUICK_VALUE_EXTEND_MASK  = 0xFFFFFFC0
  444. };

  445. /* Functions for opcodes.  The general form of the ETRAX 16-bit instruction:
  446.    Bit 15 - 12   Operand2
  447.        11 - 10   Mode
  448.         9 -  6   Opcode
  449.         5 -  4   Size
  450.         3 -  0   Operand1  */

  451. static int
  452. cris_get_operand2 (unsigned short insn)
  453. {
  454.   return ((insn & 0xF000) >> 12);
  455. }

  456. static int
  457. cris_get_mode (unsigned short insn)
  458. {
  459.   return ((insn & 0x0C00) >> 10);
  460. }

  461. static int
  462. cris_get_opcode (unsigned short insn)
  463. {
  464.   return ((insn & 0x03C0) >> 6);
  465. }

  466. static int
  467. cris_get_size (unsigned short insn)
  468. {
  469.   return ((insn & 0x0030) >> 4);
  470. }

  471. static int
  472. cris_get_operand1 (unsigned short insn)
  473. {
  474.   return (insn & 0x000F);
  475. }

  476. /* Additional functions in order to handle opcodes.  */

  477. static int
  478. cris_get_quick_value (unsigned short insn)
  479. {
  480.   return (insn & 0x003F);
  481. }

  482. static int
  483. cris_get_bdap_quick_offset (unsigned short insn)
  484. {
  485.   return (insn & 0x00FF);
  486. }

  487. static int
  488. cris_get_branch_short_offset (unsigned short insn)
  489. {
  490.   return (insn & 0x00FF);
  491. }

  492. static int
  493. cris_get_asr_shift_steps (unsigned long value)
  494. {
  495.   return (value & 0x3F);
  496. }

  497. static int
  498. cris_get_clear_size (unsigned short insn)
  499. {
  500.   return ((insn) & 0xC000);
  501. }

  502. static int
  503. cris_is_signed_extend_bit_on (unsigned short insn)
  504. {
  505.   return (((insn) & 0x20) == 0x20);
  506. }

  507. static int
  508. cris_is_xflag_bit_on (unsigned short insn)
  509. {
  510.   return (((insn) & 0x1000) == 0x1000);
  511. }

  512. static void
  513. cris_set_size_to_dword (unsigned short *insn)
  514. {
  515.   *insn &= 0xFFCF;
  516.   *insn |= 0x20;
  517. }

  518. static signed char
  519. cris_get_signed_offset (unsigned short insn)
  520. {
  521.   return ((signed char) (insn & 0x00FF));
  522. }

  523. /* Calls an op function given the op-type, working on the insn and the
  524.    inst_env.  */
  525. static void cris_gdb_func (struct gdbarch *, enum cris_op_type, unsigned short,
  526.                            inst_env_type *);

  527. static struct gdbarch *cris_gdbarch_init (struct gdbarch_info,
  528.                                           struct gdbarch_list *);

  529. static void cris_dump_tdep (struct gdbarch *, struct ui_file *);

  530. static void set_cris_version (char *ignore_args, int from_tty,
  531.                               struct cmd_list_element *c);

  532. static void set_cris_mode (char *ignore_args, int from_tty,
  533.                            struct cmd_list_element *c);

  534. static void set_cris_dwarf2_cfi (char *ignore_args, int from_tty,
  535.                                  struct cmd_list_element *c);

  536. static CORE_ADDR cris_scan_prologue (CORE_ADDR pc,
  537.                                      struct frame_info *this_frame,
  538.                                      struct cris_unwind_cache *info);

  539. static CORE_ADDR crisv32_scan_prologue (CORE_ADDR pc,
  540.                                         struct frame_info *this_frame,
  541.                                         struct cris_unwind_cache *info);

  542. static CORE_ADDR cris_unwind_pc (struct gdbarch *gdbarch,
  543.                                  struct frame_info *next_frame);

  544. static CORE_ADDR cris_unwind_sp (struct gdbarch *gdbarch,
  545.                                  struct frame_info *next_frame);

  546. /* When arguments must be pushed onto the stack, they go on in reverse
  547.    order.  The below implements a FILO (stack) to do this.
  548.    Copied from d10v-tdep.c.  */

  549. struct stack_item
  550. {
  551.   int len;
  552.   struct stack_item *prev;
  553.   void *data;
  554. };

  555. static struct stack_item *
  556. push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
  557. {
  558.   struct stack_item *si;
  559.   si = xmalloc (sizeof (struct stack_item));
  560.   si->data = xmalloc (len);
  561.   si->len = len;
  562.   si->prev = prev;
  563.   memcpy (si->data, contents, len);
  564.   return si;
  565. }

  566. static struct stack_item *
  567. pop_stack_item (struct stack_item *si)
  568. {
  569.   struct stack_item *dead = si;
  570.   si = si->prev;
  571.   xfree (dead->data);
  572.   xfree (dead);
  573.   return si;
  574. }

  575. /* Put here the code to store, into fi->saved_regs, the addresses of
  576.    the saved registers of frame described by FRAME_INFO.  This
  577.    includes special registers such as pc and fp saved in special ways
  578.    in the stack frame.  sp is even more special: the address we return
  579.    for it IS the sp for the next frame.  */

  580. static struct cris_unwind_cache *
  581. cris_frame_unwind_cache (struct frame_info *this_frame,
  582.                          void **this_prologue_cache)
  583. {
  584.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  585.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  586.   struct cris_unwind_cache *info;

  587.   if ((*this_prologue_cache))
  588.     return (*this_prologue_cache);

  589.   info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
  590.   (*this_prologue_cache) = info;
  591.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  592.   /* Zero all fields.  */
  593.   info->prev_sp = 0;
  594.   info->base = 0;
  595.   info->size = 0;
  596.   info->sp_offset = 0;
  597.   info->r8_offset = 0;
  598.   info->uses_frame = 0;
  599.   info->return_pc = 0;
  600.   info->leaf_function = 0;

  601.   /* Prologue analysis does the rest...  */
  602.   if (tdep->cris_version == 32)
  603.     crisv32_scan_prologue (get_frame_func (this_frame), this_frame, info);
  604.   else
  605.     cris_scan_prologue (get_frame_func (this_frame), this_frame, info);

  606.   return info;
  607. }

  608. /* Given a GDB frame, determine the address of the calling function's
  609.    frame.  This will be used to create a new GDB frame struct.  */

  610. static void
  611. cris_frame_this_id (struct frame_info *this_frame,
  612.                     void **this_prologue_cache,
  613.                     struct frame_id *this_id)
  614. {
  615.   struct cris_unwind_cache *info
  616.     = cris_frame_unwind_cache (this_frame, this_prologue_cache);
  617.   CORE_ADDR base;
  618.   CORE_ADDR func;
  619.   struct frame_id id;

  620.   /* The FUNC is easy.  */
  621.   func = get_frame_func (this_frame);

  622.   /* Hopefully the prologue analysis either correctly determined the
  623.      frame's base (which is the SP from the previous frame), or set
  624.      that base to "NULL".  */
  625.   base = info->prev_sp;
  626.   if (base == 0)
  627.     return;

  628.   id = frame_id_build (base, func);

  629.   (*this_id) = id;
  630. }

  631. static struct value *
  632. cris_frame_prev_register (struct frame_info *this_frame,
  633.                           void **this_prologue_cache, int regnum)
  634. {
  635.   struct cris_unwind_cache *info
  636.     = cris_frame_unwind_cache (this_frame, this_prologue_cache);
  637.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  638. }

  639. /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
  640.    frame.  The frame ID's base needs to match the TOS value saved by
  641.    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */

  642. static struct frame_id
  643. cris_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  644. {
  645.   CORE_ADDR sp;
  646.   sp = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
  647.   return frame_id_build (sp, get_frame_pc (this_frame));
  648. }

  649. static CORE_ADDR
  650. cris_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  651. {
  652.   /* Align to the size of an instruction (so that they can safely be
  653.      pushed onto the stack).  */
  654.   return sp & ~3;
  655. }

  656. static CORE_ADDR
  657. cris_push_dummy_code (struct gdbarch *gdbarch,
  658.                       CORE_ADDR sp, CORE_ADDR funaddr,
  659.                       struct value **args, int nargs,
  660.                       struct type *value_type,
  661.                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  662.                       struct regcache *regcache)
  663. {
  664.   /* Allocate space sufficient for a breakpoint.  */
  665.   sp = (sp - 4) & ~3;
  666.   /* Store the address of that breakpoint */
  667.   *bp_addr = sp;
  668.   /* CRIS always starts the call at the callee's entry point.  */
  669.   *real_pc = funaddr;
  670.   return sp;
  671. }

  672. static CORE_ADDR
  673. cris_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  674.                       struct regcache *regcache, CORE_ADDR bp_addr,
  675.                       int nargs, struct value **args, CORE_ADDR sp,
  676.                       int struct_return, CORE_ADDR struct_addr)
  677. {
  678.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  679.   int stack_offset;
  680.   int argreg;
  681.   int argnum;

  682.   /* The function's arguments and memory allocated by gdb for the arguments to
  683.      point at reside in separate areas on the stack.
  684.      Both frame pointers grow toward higher addresses.  */
  685.   CORE_ADDR fp_arg;
  686.   CORE_ADDR fp_mem;

  687.   struct stack_item *si = NULL;

  688.   /* Push the return address.  */
  689.   regcache_cooked_write_unsigned (regcache, SRP_REGNUM, bp_addr);

  690.   /* Are we returning a value using a structure return or a normal value
  691.      return?  struct_addr is the address of the reserved space for the return
  692.      structure to be written on the stack.  */
  693.   if (struct_return)
  694.     {
  695.       regcache_cooked_write_unsigned (regcache, STR_REGNUM, struct_addr);
  696.     }

  697.   /* Now load as many as possible of the first arguments into registers,
  698.      and push the rest onto the stack.  */
  699.   argreg = ARG1_REGNUM;
  700.   stack_offset = 0;

  701.   for (argnum = 0; argnum < nargs; argnum++)
  702.     {
  703.       int len;
  704.       const gdb_byte *val;
  705.       int reg_demand;
  706.       int i;

  707.       len = TYPE_LENGTH (value_type (args[argnum]));
  708.       val = value_contents (args[argnum]);

  709.       /* How may registers worth of storage do we need for this argument?  */
  710.       reg_demand = (len / 4) + (len % 4 != 0 ? 1 : 0);

  711.       if (len <= (2 * 4) && (argreg + reg_demand - 1 <= ARG4_REGNUM))
  712.         {
  713.           /* Data passed by value.  Fits in available register(s).  */
  714.           for (i = 0; i < reg_demand; i++)
  715.             {
  716.               regcache_cooked_write (regcache, argreg, val);
  717.               argreg++;
  718.               val += 4;
  719.             }
  720.         }
  721.       else if (len <= (2 * 4) && argreg <= ARG4_REGNUM)
  722.         {
  723.           /* Data passed by value. Does not fit in available register(s).
  724.              Use the register(s) first, then the stack.  */
  725.           for (i = 0; i < reg_demand; i++)
  726.             {
  727.               if (argreg <= ARG4_REGNUM)
  728.                 {
  729.                   regcache_cooked_write (regcache, argreg, val);
  730.                   argreg++;
  731.                   val += 4;
  732.                 }
  733.               else
  734.                 {
  735.                   /* Push item for later so that pushed arguments
  736.                      come in the right order.  */
  737.                   si = push_stack_item (si, val, 4);
  738.                   val += 4;
  739.                 }
  740.             }
  741.         }
  742.       else if (len > (2 * 4))
  743.         {
  744.           /* Data passed by reference.  Push copy of data onto stack
  745.              and pass pointer to this copy as argument.  */
  746.           sp = (sp - len) & ~3;
  747.           write_memory (sp, val, len);

  748.           if (argreg <= ARG4_REGNUM)
  749.             {
  750.               regcache_cooked_write_unsigned (regcache, argreg, sp);
  751.               argreg++;
  752.             }
  753.           else
  754.             {
  755.               gdb_byte buf[4];
  756.               store_unsigned_integer (buf, 4, byte_order, sp);
  757.               si = push_stack_item (si, buf, 4);
  758.             }
  759.         }
  760.       else
  761.         {
  762.           /* Data passed by value.  No available registers.  Put it on
  763.              the stack.  */
  764.            si = push_stack_item (si, val, len);
  765.         }
  766.     }

  767.   while (si)
  768.     {
  769.       /* fp_arg must be word-aligned (i.e., don't += len) to match
  770.          the function prologue.  */
  771.       sp = (sp - si->len) & ~3;
  772.       write_memory (sp, si->data, si->len);
  773.       si = pop_stack_item (si);
  774.     }

  775.   /* Finally, update the SP register.  */
  776.   regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);

  777.   return sp;
  778. }

  779. static const struct frame_unwind cris_frame_unwind =
  780. {
  781.   NORMAL_FRAME,
  782.   default_frame_unwind_stop_reason,
  783.   cris_frame_this_id,
  784.   cris_frame_prev_register,
  785.   NULL,
  786.   default_frame_sniffer
  787. };

  788. static CORE_ADDR
  789. cris_frame_base_address (struct frame_info *this_frame, void **this_cache)
  790. {
  791.   struct cris_unwind_cache *info
  792.     = cris_frame_unwind_cache (this_frame, this_cache);
  793.   return info->base;
  794. }

  795. static const struct frame_base cris_frame_base =
  796. {
  797.   &cris_frame_unwind,
  798.   cris_frame_base_address,
  799.   cris_frame_base_address,
  800.   cris_frame_base_address
  801. };

  802. /* Frames information. The definition of the struct frame_info is

  803.    CORE_ADDR frame
  804.    CORE_ADDR pc
  805.    enum frame_type type;
  806.    CORE_ADDR return_pc
  807.    int leaf_function

  808.    If the compilation option -fno-omit-frame-pointer is present the
  809.    variable frame will be set to the content of R8 which is the frame
  810.    pointer register.

  811.    The variable pc contains the address where execution is performed
  812.    in the present frame.  The innermost frame contains the current content
  813.    of the register PC.  All other frames contain the content of the
  814.    register PC in the next frame.

  815.    The variable `type' indicates the frame's type: normal, SIGTRAMP
  816.    (associated with a signal handler), dummy (associated with a dummy
  817.    frame).

  818.    The variable return_pc contains the address where execution should be
  819.    resumed when the present frame has finished, the return address.

  820.    The variable leaf_function is 1 if the return address is in the register
  821.    SRP, and 0 if it is on the stack.

  822.    Prologue instructions C-code.
  823.    The prologue may consist of (-fno-omit-frame-pointer)
  824.    1)                2)
  825.    push   srp
  826.    push   r8         push   r8
  827.    move.d sp,r8      move.d sp,r8
  828.    subq   X,sp       subq   X,sp
  829.    movem  rY,[sp]    movem  rY,[sp]
  830.    move.S rZ,[r8-U]  move.S rZ,[r8-U]

  831.    where 1 is a non-terminal function, and 2 is a leaf-function.

  832.    Note that this assumption is extremely brittle, and will break at the
  833.    slightest change in GCC's prologue.

  834.    If local variables are declared or register contents are saved on stack
  835.    the subq-instruction will be present with X as the number of bytes
  836.    needed for storage.  The reshuffle with respect to r8 may be performed
  837.    with any size S (b, w, d) and any of the general registers Z={0..13}.
  838.    The offset U should be representable by a signed 8-bit value in all cases.
  839.    Thus, the prefix word is assumed to be immediate byte offset mode followed
  840.    by another word containing the instruction.

  841.    Degenerate cases:
  842.    3)
  843.    push   r8
  844.    move.d sp,r8
  845.    move.d r8,sp
  846.    pop    r8

  847.    Prologue instructions C++-code.
  848.    Case 1) and 2) in the C-code may be followed by

  849.    move.d r10,rS    ; this
  850.    move.d r11,rT    ; P1
  851.    move.d r12,rU    ; P2
  852.    move.d r13,rV    ; P3
  853.    move.S [r8+U],rZ ; P4

  854.    if any of the call parameters are stored.  The host expects these
  855.    instructions to be executed in order to get the call parameters right.  */

  856. /* Examine the prologue of a function.  The variable ip is the address of
  857.    the first instruction of the prologue.  The variable limit is the address
  858.    of the first instruction after the prologue.  The variable fi contains the
  859.    information in struct frame_info.  The variable frameless_p controls whether
  860.    the entire prologue is examined (0) or just enough instructions to
  861.    determine that it is a prologue (1).  */

  862. static CORE_ADDR
  863. cris_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
  864.                     struct cris_unwind_cache *info)
  865. {
  866.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  867.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  868.   /* Present instruction.  */
  869.   unsigned short insn;

  870.   /* Next instruction, lookahead.  */
  871.   unsigned short insn_next;
  872.   int regno;

  873.   /* Is there a push fp?  */
  874.   int have_fp;

  875.   /* Number of byte on stack used for local variables and movem.  */
  876.   int val;

  877.   /* Highest register number in a movem.  */
  878.   int regsave;

  879.   /* move.d r<source_register>,rS */
  880.   short source_register;

  881.   /* Scan limit.  */
  882.   int limit;

  883.   /* This frame is with respect to a leaf until a push srp is found.  */
  884.   if (info)
  885.     {
  886.       info->leaf_function = 1;
  887.     }

  888.   /* Assume nothing on stack.  */
  889.   val = 0;
  890.   regsave = -1;

  891.   /* If we were called without a this_frame, that means we were called
  892.      from cris_skip_prologue which already tried to find the end of the
  893.      prologue through the symbol information.  64 instructions past current
  894.      pc is arbitrarily chosen, but at least it means we'll stop eventually.  */
  895.   limit = this_frame ? get_frame_pc (this_frame) : pc + 64;

  896.   /* Find the prologue instructions.  */
  897.   while (pc > 0 && pc < limit)
  898.     {
  899.       insn = read_memory_unsigned_integer (pc, 2, byte_order);
  900.       pc += 2;
  901.       if (insn == 0xE1FC)
  902.         {
  903.           /* push <reg> 32 bit instruction.  */
  904.           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
  905.           pc += 2;
  906.           regno = cris_get_operand2 (insn_next);
  907.           if (info)
  908.             {
  909.               info->sp_offset += 4;
  910.             }
  911.           /* This check, meant to recognize srp, used to be regno ==
  912.              (SRP_REGNUM - NUM_GENREGS), but that covers r11 also.  */
  913.           if (insn_next == 0xBE7E)
  914.             {
  915.               if (info)
  916.                 {
  917.                   info->leaf_function = 0;
  918.                 }
  919.             }
  920.           else if (insn_next == 0x8FEE)
  921.             {
  922.               /* push $r8 */
  923.               if (info)
  924.                 {
  925.                   info->r8_offset = info->sp_offset;
  926.                 }
  927.             }
  928.         }
  929.       else if (insn == 0x866E)
  930.         {
  931.           /* move.d sp,r8 */
  932.           if (info)
  933.             {
  934.               info->uses_frame = 1;
  935.             }
  936.           continue;
  937.         }
  938.       else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
  939.                && cris_get_mode (insn) == 0x0000
  940.                && cris_get_opcode (insn) == 0x000A)
  941.         {
  942.           /* subq <val>,sp */
  943.           if (info)
  944.             {
  945.               info->sp_offset += cris_get_quick_value (insn);
  946.             }
  947.         }
  948.       else if (cris_get_mode (insn) == 0x0002
  949.                && cris_get_opcode (insn) == 0x000F
  950.                && cris_get_size (insn) == 0x0003
  951.                && cris_get_operand1 (insn) == gdbarch_sp_regnum (gdbarch))
  952.         {
  953.           /* movem r<regsave>,[sp] */
  954.           regsave = cris_get_operand2 (insn);
  955.         }
  956.       else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
  957.                && ((insn & 0x0F00) >> 8) == 0x0001
  958.                && (cris_get_signed_offset (insn) < 0))
  959.         {
  960.           /* Immediate byte offset addressing prefix word with sp as base
  961.              register.  Used for CRIS v8 i.e. ETRAX 100 and newer if <val>
  962.              is between 64 and 128.
  963.              movem r<regsave>,[sp=sp-<val>] */
  964.           if (info)
  965.             {
  966.               info->sp_offset += -cris_get_signed_offset (insn);
  967.             }
  968.           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
  969.           pc += 2;
  970.           if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
  971.               && cris_get_opcode (insn_next) == 0x000F
  972.               && cris_get_size (insn_next) == 0x0003
  973.               && cris_get_operand1 (insn_next) == gdbarch_sp_regnum
  974.                                                   (gdbarch))
  975.             {
  976.               regsave = cris_get_operand2 (insn_next);
  977.             }
  978.           else
  979.             {
  980.               /* The prologue ended before the limit was reached.  */
  981.               pc -= 4;
  982.               break;
  983.             }
  984.         }
  985.       else if (cris_get_mode (insn) == 0x0001
  986.                && cris_get_opcode (insn) == 0x0009
  987.                && cris_get_size (insn) == 0x0002)
  988.         {
  989.           /* move.d r<10..13>,r<0..15> */
  990.           source_register = cris_get_operand1 (insn);

  991.           /* FIXME?  In the glibc solibs, the prologue might contain something
  992.              like (this example taken from relocate_doit):
  993.              move.d $pc,$r0
  994.              sub.d 0xfffef426,$r0
  995.              which isn't covered by the source_register check below.  Question
  996.              is whether to add a check for this combo, or make better use of
  997.              the limit variable instead.  */
  998.           if (source_register < ARG1_REGNUM || source_register > ARG4_REGNUM)
  999.             {
  1000.               /* The prologue ended before the limit was reached.  */
  1001.               pc -= 2;
  1002.               break;
  1003.             }
  1004.         }
  1005.       else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
  1006.                /* The size is a fixed-size.  */
  1007.                && ((insn & 0x0F00) >> 8) == 0x0001
  1008.                /* A negative offset.  */
  1009.                && (cris_get_signed_offset (insn) < 0))
  1010.         {
  1011.           /* move.S rZ,[r8-U] (?) */
  1012.           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
  1013.           pc += 2;
  1014.           regno = cris_get_operand2 (insn_next);
  1015.           if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
  1016.               && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
  1017.               && cris_get_opcode (insn_next) == 0x000F)
  1018.             {
  1019.               /* move.S rZ,[r8-U] */
  1020.               continue;
  1021.             }
  1022.           else
  1023.             {
  1024.               /* The prologue ended before the limit was reached.  */
  1025.               pc -= 4;
  1026.               break;
  1027.             }
  1028.         }
  1029.       else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM
  1030.                /* The size is a fixed-size.  */
  1031.                && ((insn & 0x0F00) >> 8) == 0x0001
  1032.                /* A positive offset.  */
  1033.                && (cris_get_signed_offset (insn) > 0))
  1034.         {
  1035.           /* move.S [r8+U],rZ (?) */
  1036.           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
  1037.           pc += 2;
  1038.           regno = cris_get_operand2 (insn_next);
  1039.           if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
  1040.               && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
  1041.               && cris_get_opcode (insn_next) == 0x0009
  1042.               && cris_get_operand1 (insn_next) == regno)
  1043.             {
  1044.               /* move.S [r8+U],rZ */
  1045.               continue;
  1046.             }
  1047.           else
  1048.             {
  1049.               /* The prologue ended before the limit was reached.  */
  1050.               pc -= 4;
  1051.               break;
  1052.             }
  1053.         }
  1054.       else
  1055.         {
  1056.           /* The prologue ended before the limit was reached.  */
  1057.           pc -= 2;
  1058.           break;
  1059.         }
  1060.     }

  1061.   /* We only want to know the end of the prologue when this_frame and info
  1062.      are NULL (called from cris_skip_prologue i.e.).  */
  1063.   if (this_frame == NULL && info == NULL)
  1064.     {
  1065.       return pc;
  1066.     }

  1067.   info->size = info->sp_offset;

  1068.   /* Compute the previous frame's stack pointer (which is also the
  1069.      frame's ID's stack address), and this frame's base pointer.  */
  1070.   if (info->uses_frame)
  1071.     {
  1072.       ULONGEST this_base;
  1073.       /* The SP was moved to the FP.  This indicates that a new frame
  1074.          was created.  Get THIS frame's FP value by unwinding it from
  1075.          the next frame.  */
  1076.       this_base = get_frame_register_unsigned (this_frame, CRIS_FP_REGNUM);
  1077.       info->base = this_base;
  1078.       info->saved_regs[CRIS_FP_REGNUM].addr = info->base;

  1079.       /* The FP points at the last saved register.  Adjust the FP back
  1080.          to before the first saved register giving the SP.  */
  1081.       info->prev_sp = info->base + info->r8_offset;
  1082.     }
  1083.   else
  1084.     {
  1085.       ULONGEST this_base;
  1086.       /* Assume that the FP is this frame's SP but with that pushed
  1087.          stack space added back.  */
  1088.       this_base = get_frame_register_unsigned (this_frame,
  1089.                                                gdbarch_sp_regnum (gdbarch));
  1090.       info->base = this_base;
  1091.       info->prev_sp = info->base + info->size;
  1092.     }

  1093.   /* Calculate the addresses for the saved registers on the stack.  */
  1094.   /* FIXME: The address calculation should really be done on the fly while
  1095.      we're analyzing the prologue (we only hold one regsave value as it is
  1096.      now).  */
  1097.   val = info->sp_offset;

  1098.   for (regno = regsave; regno >= 0; regno--)
  1099.     {
  1100.       info->saved_regs[regno].addr = info->base + info->r8_offset - val;
  1101.       val -= 4;
  1102.     }

  1103.   /* The previous frame's SP needed to be computed.  Save the computed
  1104.      value.  */
  1105.   trad_frame_set_value (info->saved_regs,
  1106.                         gdbarch_sp_regnum (gdbarch), info->prev_sp);

  1107.   if (!info->leaf_function)
  1108.     {
  1109.       /* SRP saved on the stack.  But where?  */
  1110.       if (info->r8_offset == 0)
  1111.         {
  1112.           /* R8 not pushed yet.  */
  1113.           info->saved_regs[SRP_REGNUM].addr = info->base;
  1114.         }
  1115.       else
  1116.         {
  1117.           /* R8 pushed, but SP may or may not be moved to R8 yet.  */
  1118.           info->saved_regs[SRP_REGNUM].addr = info->base + 4;
  1119.         }
  1120.     }

  1121.   /* The PC is found in SRP (the actual register or located on the stack).  */
  1122.   info->saved_regs[gdbarch_pc_regnum (gdbarch)]
  1123.     = info->saved_regs[SRP_REGNUM];

  1124.   return pc;
  1125. }

  1126. static CORE_ADDR
  1127. crisv32_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
  1128.                     struct cris_unwind_cache *info)
  1129. {
  1130.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1131.   ULONGEST this_base;

  1132.   /* Unlike the CRISv10 prologue scanner (cris_scan_prologue), this is not
  1133.      meant to be a full-fledged prologue scanner.  It is only needed for
  1134.      the cases where we end up in code always lacking DWARF-2 CFI, notably:

  1135.        * PLT stubs (library calls)
  1136.        * call dummys
  1137.        * signal trampolines

  1138.      For those cases, it is assumed that there is no actual prologue; that
  1139.      the stack pointer is not adjusted, and (as a consequence) the return
  1140.      address is not pushed onto the stack.  */

  1141.   /* We only want to know the end of the prologue when this_frame and info
  1142.      are NULL (called from cris_skip_prologue i.e.).  */
  1143.   if (this_frame == NULL && info == NULL)
  1144.     {
  1145.       return pc;
  1146.     }

  1147.   /* The SP is assumed to be unaltered.  */
  1148.   this_base = get_frame_register_unsigned (this_frame,
  1149.                                            gdbarch_sp_regnum (gdbarch));
  1150.   info->base = this_base;
  1151.   info->prev_sp = this_base;

  1152.   /* The PC is assumed to be found in SRP.  */
  1153.   info->saved_regs[gdbarch_pc_regnum (gdbarch)]
  1154.     = info->saved_regs[SRP_REGNUM];

  1155.   return pc;
  1156. }

  1157. /* Advance pc beyond any function entry prologue instructions at pc
  1158.    to reach some "real" code.  */

  1159. /* Given a PC value corresponding to the start of a function, return the PC
  1160.    of the first instruction after the function prologue.  */

  1161. static CORE_ADDR
  1162. cris_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1163. {
  1164.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1165.   CORE_ADDR func_addr, func_end;
  1166.   struct symtab_and_line sal;
  1167.   CORE_ADDR pc_after_prologue;

  1168.   /* If we have line debugging information, then the end of the prologue
  1169.      should the first assembly instruction of the first source line.  */
  1170.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  1171.     {
  1172.       sal = find_pc_line (func_addr, 0);
  1173.       if (sal.end > 0 && sal.end < func_end)
  1174.         return sal.end;
  1175.     }

  1176.   if (tdep->cris_version == 32)
  1177.     pc_after_prologue = crisv32_scan_prologue (pc, NULL, NULL);
  1178.   else
  1179.     pc_after_prologue = cris_scan_prologue (pc, NULL, NULL);

  1180.   return pc_after_prologue;
  1181. }

  1182. static CORE_ADDR
  1183. cris_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1184. {
  1185.   ULONGEST pc;
  1186.   pc = frame_unwind_register_unsigned (next_frame,
  1187.                                        gdbarch_pc_regnum (gdbarch));
  1188.   return pc;
  1189. }

  1190. static CORE_ADDR
  1191. cris_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1192. {
  1193.   ULONGEST sp;
  1194.   sp = frame_unwind_register_unsigned (next_frame,
  1195.                                        gdbarch_sp_regnum (gdbarch));
  1196.   return sp;
  1197. }

  1198. /* Use the program counter to determine the contents and size of a breakpoint
  1199.    instruction.  It returns a pointer to a string of bytes that encode a
  1200.    breakpoint instruction, stores the length of the string to *lenptr, and
  1201.    adjusts pcptr (if necessary) to point to the actual memory location where
  1202.    the breakpoint should be inserted.  */

  1203. static const unsigned char *
  1204. cris_breakpoint_from_pc (struct gdbarch *gdbarch,
  1205.                          CORE_ADDR *pcptr, int *lenptr)
  1206. {
  1207.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1208.   static unsigned char break8_insn[] = {0x38, 0xe9};
  1209.   static unsigned char break15_insn[] = {0x3f, 0xe9};
  1210.   *lenptr = 2;

  1211.   if (tdep->cris_mode == cris_mode_guru)
  1212.     return break15_insn;
  1213.   else
  1214.     return break8_insn;
  1215. }

  1216. /* Returns 1 if spec_reg is applicable to the current gdbarch's CRIS version,
  1217.    0 otherwise.  */

  1218. static int
  1219. cris_spec_reg_applicable (struct gdbarch *gdbarch,
  1220.                           struct cris_spec_reg spec_reg)
  1221. {
  1222.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1223.   unsigned int version = tdep->cris_version;

  1224.   switch (spec_reg.applicable_version)
  1225.     {
  1226.     case cris_ver_version_all:
  1227.       return 1;
  1228.     case cris_ver_warning:
  1229.       /* Indeterminate/obsolete.  */
  1230.       return 0;
  1231.     case cris_ver_v0_3:
  1232.       return (version >= 0 && version <= 3);
  1233.     case cris_ver_v3p:
  1234.       return (version >= 3);
  1235.     case cris_ver_v8:
  1236.       return (version == 8 || version == 9);
  1237.     case cris_ver_v8p:
  1238.       return (version >= 8);
  1239.     case cris_ver_v0_10:
  1240.       return (version >= 0 && version <= 10);
  1241.     case cris_ver_v3_10:
  1242.       return (version >= 3 && version <= 10);
  1243.     case cris_ver_v8_10:
  1244.       return (version >= 8 && version <= 10);
  1245.     case cris_ver_v10:
  1246.       return (version == 10);
  1247.     case cris_ver_v10p:
  1248.       return (version >= 10);
  1249.     case cris_ver_v32p:
  1250.       return (version >= 32);
  1251.     default:
  1252.       /* Invalid cris version.  */
  1253.       return 0;
  1254.     }
  1255. }

  1256. /* Returns the register size in unit byte.  Returns 0 for an unimplemented
  1257.    register, -1 for an invalid register.  */

  1258. static int
  1259. cris_register_size (struct gdbarch *gdbarch, int regno)
  1260. {
  1261.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1262.   int i;
  1263.   int spec_regno;

  1264.   if (regno >= 0 && regno < NUM_GENREGS)
  1265.     {
  1266.       /* General registers (R0 - R15) are 32 bits.  */
  1267.       return 4;
  1268.     }
  1269.   else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
  1270.     {
  1271.       /* Special register (R16 - R31).  cris_spec_regs is zero-based.
  1272.          Adjust regno accordingly.  */
  1273.       spec_regno = regno - NUM_GENREGS;

  1274.       for (i = 0; cris_spec_regs[i].name != NULL; i++)
  1275.         {
  1276.           if (cris_spec_regs[i].number == spec_regno
  1277.               && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
  1278.             /* Go with the first applicable register.  */
  1279.             return cris_spec_regs[i].reg_size;
  1280.         }
  1281.       /* Special register not applicable to this CRIS version.  */
  1282.       return 0;
  1283.     }
  1284.   else if (regno >= gdbarch_pc_regnum (gdbarch)
  1285.            && regno < gdbarch_num_regs (gdbarch))
  1286.     {
  1287.       /* This will apply to CRISv32 only where there are additional registers
  1288.          after the special registers (pseudo PC and support registers).  */
  1289.       return 4;
  1290.     }


  1291.   return -1;
  1292. }

  1293. /* Nonzero if regno should not be fetched from the target.  This is the case
  1294.    for unimplemented (size 0) and non-existant registers.  */

  1295. static int
  1296. cris_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
  1297. {
  1298.   return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  1299.           || (cris_register_size (gdbarch, regno) == 0));
  1300. }

  1301. /* Nonzero if regno should not be written to the target, for various
  1302.    reasons.  */

  1303. static int
  1304. cris_cannot_store_register (struct gdbarch *gdbarch, int regno)
  1305. {
  1306.   /* There are three kinds of registers we refuse to write to.
  1307.      1. Those that not implemented.
  1308.      2. Those that are read-only (depends on the processor mode).
  1309.      3. Those registers to which a write has no effect.  */

  1310.   if (regno < 0
  1311.       || regno >= gdbarch_num_regs (gdbarch)
  1312.       || cris_register_size (gdbarch, regno) == 0)
  1313.     /* Not implemented.  */
  1314.     return 1;

  1315.   else if  (regno == VR_REGNUM)
  1316.     /* Read-only.  */
  1317.     return 1;

  1318.   else if  (regno == P0_REGNUM || regno == P4_REGNUM || regno == P8_REGNUM)
  1319.     /* Writing has no effect.  */
  1320.     return 1;

  1321.   /* IBR, BAR, BRP and IRP are read-only in user mode.  Let the debug
  1322.      agent decide whether they are writable.  */

  1323.   return 0;
  1324. }

  1325. /* Nonzero if regno should not be fetched from the target.  This is the case
  1326.    for unimplemented (size 0) and non-existant registers.  */

  1327. static int
  1328. crisv32_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
  1329. {
  1330.   return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  1331.           || (cris_register_size (gdbarch, regno) == 0));
  1332. }

  1333. /* Nonzero if regno should not be written to the target, for various
  1334.    reasons.  */

  1335. static int
  1336. crisv32_cannot_store_register (struct gdbarch *gdbarch, int regno)
  1337. {
  1338.   /* There are three kinds of registers we refuse to write to.
  1339.      1. Those that not implemented.
  1340.      2. Those that are read-only (depends on the processor mode).
  1341.      3. Those registers to which a write has no effect.  */

  1342.   if (regno < 0
  1343.       || regno >= gdbarch_num_regs (gdbarch)
  1344.       || cris_register_size (gdbarch, regno) == 0)
  1345.     /* Not implemented.  */
  1346.     return 1;

  1347.   else if  (regno == VR_REGNUM)
  1348.     /* Read-only.  */
  1349.     return 1;

  1350.   else if  (regno == BZ_REGNUM || regno == WZ_REGNUM || regno == DZ_REGNUM)
  1351.     /* Writing has no effect.  */
  1352.     return 1;

  1353.   /* Many special registers are read-only in user mode.  Let the debug
  1354.      agent decide whether they are writable.  */

  1355.   return 0;
  1356. }

  1357. /* Return the GDB type (defined in gdbtypes.c) for the "standard" data type
  1358.    of data in register regno.  */

  1359. static struct type *
  1360. cris_register_type (struct gdbarch *gdbarch, int regno)
  1361. {
  1362.   if (regno == gdbarch_pc_regnum (gdbarch))
  1363.     return builtin_type (gdbarch)->builtin_func_ptr;
  1364.   else if (regno == gdbarch_sp_regnum (gdbarch)
  1365.            || regno == CRIS_FP_REGNUM)
  1366.     return builtin_type (gdbarch)->builtin_data_ptr;
  1367.   else if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
  1368.            || (regno >= MOF_REGNUM && regno <= USP_REGNUM))
  1369.     /* Note: R8 taken care of previous clause.  */
  1370.     return builtin_type (gdbarch)->builtin_uint32;
  1371.   else if (regno >= P4_REGNUM && regno <= CCR_REGNUM)
  1372.       return builtin_type (gdbarch)->builtin_uint16;
  1373.   else if (regno >= P0_REGNUM && regno <= VR_REGNUM)
  1374.       return builtin_type (gdbarch)->builtin_uint8;
  1375.   else
  1376.       /* Invalid (unimplemented) register.  */
  1377.       return builtin_type (gdbarch)->builtin_int0;
  1378. }

  1379. static struct type *
  1380. crisv32_register_type (struct gdbarch *gdbarch, int regno)
  1381. {
  1382.   if (regno == gdbarch_pc_regnum (gdbarch))
  1383.     return builtin_type (gdbarch)->builtin_func_ptr;
  1384.   else if (regno == gdbarch_sp_regnum (gdbarch)
  1385.            || regno == CRIS_FP_REGNUM)
  1386.     return builtin_type (gdbarch)->builtin_data_ptr;
  1387.   else if ((regno >= 0 && regno <= ACR_REGNUM)
  1388.            || (regno >= EXS_REGNUM && regno <= SPC_REGNUM)
  1389.            || (regno == PID_REGNUM)
  1390.            || (regno >= S0_REGNUM && regno <= S15_REGNUM))
  1391.     /* Note: R8 and SP taken care of by previous clause.  */
  1392.     return builtin_type (gdbarch)->builtin_uint32;
  1393.   else if (regno == WZ_REGNUM)
  1394.       return builtin_type (gdbarch)->builtin_uint16;
  1395.   else if (regno == BZ_REGNUM || regno == VR_REGNUM || regno == SRS_REGNUM)
  1396.       return builtin_type (gdbarch)->builtin_uint8;
  1397.   else
  1398.     {
  1399.       /* Invalid (unimplemented) register.  Should not happen as there are
  1400.          no unimplemented CRISv32 registers.  */
  1401.       warning (_("crisv32_register_type: unknown regno %d"), regno);
  1402.       return builtin_type (gdbarch)->builtin_int0;
  1403.     }
  1404. }

  1405. /* Stores a function return value of type type, where valbuf is the address
  1406.    of the value to be stored.  */

  1407. /* In the CRIS ABI, R10 and R11 are used to store return values.  */

  1408. static void
  1409. cris_store_return_value (struct type *type, struct regcache *regcache,
  1410.                          const gdb_byte *valbuf)
  1411. {
  1412.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1413.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1414.   ULONGEST val;
  1415.   int len = TYPE_LENGTH (type);

  1416.   if (len <= 4)
  1417.     {
  1418.       /* Put the return value in R10.  */
  1419.       val = extract_unsigned_integer (valbuf, len, byte_order);
  1420.       regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
  1421.     }
  1422.   else if (len <= 8)
  1423.     {
  1424.       /* Put the return value in R10 and R11.  */
  1425.       val = extract_unsigned_integer (valbuf, 4, byte_order);
  1426.       regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
  1427.       val = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
  1428.       regcache_cooked_write_unsigned (regcache, ARG2_REGNUM, val);
  1429.     }
  1430.   else
  1431.     error (_("cris_store_return_value: type length too large."));
  1432. }

  1433. /* Return the name of register regno as a string.  Return NULL for an
  1434.    invalid or unimplemented register.  */

  1435. static const char *
  1436. cris_special_register_name (struct gdbarch *gdbarch, int regno)
  1437. {
  1438.   int spec_regno;
  1439.   int i;

  1440.   /* Special register (R16 - R31).  cris_spec_regs is zero-based.
  1441.      Adjust regno accordingly.  */
  1442.   spec_regno = regno - NUM_GENREGS;

  1443.   /* Assume nothing about the layout of the cris_spec_regs struct
  1444.      when searching.  */
  1445.   for (i = 0; cris_spec_regs[i].name != NULL; i++)
  1446.     {
  1447.       if (cris_spec_regs[i].number == spec_regno
  1448.           && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
  1449.         /* Go with the first applicable register.  */
  1450.         return cris_spec_regs[i].name;
  1451.     }
  1452.   /* Special register not applicable to this CRIS version.  */
  1453.   return NULL;
  1454. }

  1455. static const char *
  1456. cris_register_name (struct gdbarch *gdbarch, int regno)
  1457. {
  1458.   static char *cris_genreg_names[] =
  1459.   { "r0""r1""r2""r3", \
  1460.     "r4""r5""r6""r7", \
  1461.     "r8""r9""r10", "r11", \
  1462.     "r12", "r13", "sp""pc" };

  1463.   if (regno >= 0 && regno < NUM_GENREGS)
  1464.     {
  1465.       /* General register.  */
  1466.       return cris_genreg_names[regno];
  1467.     }
  1468.   else if (regno >= NUM_GENREGS && regno < gdbarch_num_regs (gdbarch))
  1469.     {
  1470.       return cris_special_register_name (gdbarch, regno);
  1471.     }
  1472.   else
  1473.     {
  1474.       /* Invalid register.  */
  1475.       return NULL;
  1476.     }
  1477. }

  1478. static const char *
  1479. crisv32_register_name (struct gdbarch *gdbarch, int regno)
  1480. {
  1481.   static char *crisv32_genreg_names[] =
  1482.     { "r0""r1""r2""r3", \
  1483.       "r4""r5""r6""r7", \
  1484.       "r8""r9""r10", "r11", \
  1485.       "r12", "r13", "sp""acr"
  1486.     };

  1487.   static char *crisv32_sreg_names[] =
  1488.     { "s0""s1""s2""s3", \
  1489.       "s4""s5""s6""s7", \
  1490.       "s8""s9""s10", "s11", \
  1491.       "s12", "s13", "s14""s15"
  1492.     };

  1493.   if (regno >= 0 && regno < NUM_GENREGS)
  1494.     {
  1495.       /* General register.  */
  1496.       return crisv32_genreg_names[regno];
  1497.     }
  1498.   else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
  1499.     {
  1500.       return cris_special_register_name (gdbarch, regno);
  1501.     }
  1502.   else if (regno == gdbarch_pc_regnum (gdbarch))
  1503.     {
  1504.       return "pc";
  1505.     }
  1506.   else if (regno >= S0_REGNUM && regno <= S15_REGNUM)
  1507.     {
  1508.       return crisv32_sreg_names[regno - S0_REGNUM];
  1509.     }
  1510.   else
  1511.     {
  1512.       /* Invalid register.  */
  1513.       return NULL;
  1514.     }
  1515. }

  1516. /* Convert DWARF register number REG to the appropriate register
  1517.    number used by GDB.  */

  1518. static int
  1519. cris_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  1520. {
  1521.   /* We need to re-map a couple of registers (SRP is 16 in Dwarf-2 register
  1522.      numbering, MOF is 18).
  1523.      Adapted from gcc/config/cris/cris.h.  */
  1524.   static int cris_dwarf_regmap[] = {
  1525.     0123,
  1526.     4567,
  1527.     8910, 11,
  1528.     12, 13, 14, 15,
  1529.     27, -1, -1, -1,
  1530.     -1, -1, -1, 23,
  1531.     -1, -1, -1, 27,
  1532.     -1, -1, -1, -1
  1533.   };
  1534.   int regnum = -1;

  1535.   if (reg >= 0 && reg < ARRAY_SIZE (cris_dwarf_regmap))
  1536.     regnum = cris_dwarf_regmap[reg];

  1537.   if (regnum == -1)
  1538.     warning (_("Unmapped DWARF Register #%d encountered."), reg);

  1539.   return regnum;
  1540. }

  1541. /* DWARF-2 frame support.  */

  1542. static void
  1543. cris_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1544.                             struct dwarf2_frame_state_reg *reg,
  1545.                             struct frame_info *this_frame)
  1546. {
  1547.   /* The return address column.  */
  1548.   if (regnum == gdbarch_pc_regnum (gdbarch))
  1549.     reg->how = DWARF2_FRAME_REG_RA;

  1550.   /* The call frame address.  */
  1551.   else if (regnum == gdbarch_sp_regnum (gdbarch))
  1552.     reg->how = DWARF2_FRAME_REG_CFA;
  1553. }

  1554. /* Extract from an array regbuf containing the raw register state a function
  1555.    return value of type type, and copy that, in virtual format, into
  1556.    valbuf.  */

  1557. /* In the CRIS ABI, R10 and R11 are used to store return values.  */

  1558. static void
  1559. cris_extract_return_value (struct type *type, struct regcache *regcache,
  1560.                            gdb_byte *valbuf)
  1561. {
  1562.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1563.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1564.   ULONGEST val;
  1565.   int len = TYPE_LENGTH (type);

  1566.   if (len <= 4)
  1567.     {
  1568.       /* Get the return value from R10.  */
  1569.       regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
  1570.       store_unsigned_integer (valbuf, len, byte_order, val);
  1571.     }
  1572.   else if (len <= 8)
  1573.     {
  1574.       /* Get the return value from R10 and R11.  */
  1575.       regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
  1576.       store_unsigned_integer (valbuf, 4, byte_order, val);
  1577.       regcache_cooked_read_unsigned (regcache, ARG2_REGNUM, &val);
  1578.       store_unsigned_integer (valbuf + 4, len - 4, byte_order, val);
  1579.     }
  1580.   else
  1581.     error (_("cris_extract_return_value: type length too large"));
  1582. }

  1583. /* Handle the CRIS return value convention.  */

  1584. static enum return_value_convention
  1585. cris_return_value (struct gdbarch *gdbarch, struct value *function,
  1586.                    struct type *type, struct regcache *regcache,
  1587.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  1588. {
  1589.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  1590.       || TYPE_CODE (type) == TYPE_CODE_UNION
  1591.       || TYPE_LENGTH (type) > 8)
  1592.     /* Structs, unions, and anything larger than 8 bytes (2 registers)
  1593.        goes on the stack.  */
  1594.     return RETURN_VALUE_STRUCT_CONVENTION;

  1595.   if (readbuf)
  1596.     cris_extract_return_value (type, regcache, readbuf);
  1597.   if (writebuf)
  1598.     cris_store_return_value (type, regcache, writebuf);

  1599.   return RETURN_VALUE_REGISTER_CONVENTION;
  1600. }

  1601. /* Calculates a value that measures how good inst_args constraints an
  1602.    instruction.  It stems from cris_constraint, found in cris-dis.c.  */

  1603. static int
  1604. constraint (unsigned int insn, const char *inst_args,
  1605.             inst_env_type *inst_env)
  1606. {
  1607.   int retval = 0;
  1608.   int tmp, i;

  1609.   const gdb_byte *s = (const gdb_byte *) inst_args;

  1610.   for (; *s; s++)
  1611.     switch (*s)
  1612.       {
  1613.       case 'm':
  1614.         if ((insn & 0x30) == 0x30)
  1615.           return -1;
  1616.         break;

  1617.       case 'S':
  1618.         /* A prefix operand.  */
  1619.         if (inst_env->prefix_found)
  1620.           break;
  1621.         else
  1622.           return -1;

  1623.       case 'B':
  1624.         /* A "push" prefix.  (This check was REMOVED by san 970921.)  Check for
  1625.            valid "push" size.  In case of special register, it may be != 4.  */
  1626.         if (inst_env->prefix_found)
  1627.           break;
  1628.         else
  1629.           return -1;

  1630.       case 'D':
  1631.         retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
  1632.         if (!retval)
  1633.           return -1;
  1634.         else
  1635.           retval += 4;
  1636.         break;

  1637.       case 'P':
  1638.         tmp = (insn >> 0xC) & 0xF;

  1639.         for (i = 0; cris_spec_regs[i].name != NULL; i++)
  1640.           {
  1641.             /* Since we match four bits, we will give a value of
  1642.                4 - 1 = 3 in a match.  If there is a corresponding
  1643.                exact match of a special register in another pattern, it
  1644.                will get a value of 4, which will be higher.  This should
  1645.                be correct in that an exact pattern would match better that
  1646.                a general pattern.
  1647.                Note that there is a reason for not returning zero; the
  1648.                pattern for "clear" is partly  matched in the bit-pattern
  1649.                (the two lower bits must be zero), while the bit-pattern
  1650.                for a move from a special register is matched in the
  1651.                register constraint.
  1652.                This also means we will will have a race condition if
  1653.                there is a partly match in three bits in the bit pattern.  */
  1654.             if (tmp == cris_spec_regs[i].number)
  1655.               {
  1656.                 retval += 3;
  1657.                 break;
  1658.               }
  1659.           }

  1660.         if (cris_spec_regs[i].name == NULL)
  1661.           return -1;
  1662.         break;
  1663.       }
  1664.   return retval;
  1665. }

  1666. /* Returns the number of bits set in the variable value.  */

  1667. static int
  1668. number_of_bits (unsigned int value)
  1669. {
  1670.   int number_of_bits = 0;

  1671.   while (value != 0)
  1672.     {
  1673.       number_of_bits += 1;
  1674.       value &= (value - 1);
  1675.     }
  1676.   return number_of_bits;
  1677. }

  1678. /* Finds the address that should contain the single step breakpoint(s).
  1679.    It stems from code in cris-dis.c.  */

  1680. static int
  1681. find_cris_op (unsigned short insn, inst_env_type *inst_env)
  1682. {
  1683.   int i;
  1684.   int max_level_of_match = -1;
  1685.   int max_matched = -1;
  1686.   int level_of_match;

  1687.   for (i = 0; cris_opcodes[i].name != NULL; i++)
  1688.     {
  1689.       if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match)
  1690.           && ((cris_opcodes[i].lose & insn) == 0)
  1691.           /* Only CRISv10 instructions, please.  */
  1692.           && (cris_opcodes[i].applicable_version != cris_ver_v32p))
  1693.         {
  1694.           level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
  1695.           if (level_of_match >= 0)
  1696.             {
  1697.               level_of_match +=
  1698.                 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
  1699.               if (level_of_match > max_level_of_match)
  1700.                 {
  1701.                   max_matched = i;
  1702.                   max_level_of_match = level_of_match;
  1703.                   if (level_of_match == 16)
  1704.                     {
  1705.                       /* All bits matched, cannot find better.  */
  1706.                       break;
  1707.                     }
  1708.                 }
  1709.             }
  1710.         }
  1711.     }
  1712.   return max_matched;
  1713. }

  1714. /* Attempts to find single-step breakpoints.  Returns -1 on failure which is
  1715.    actually an internal error.  */

  1716. static int
  1717. find_step_target (struct frame_info *frame, inst_env_type *inst_env)
  1718. {
  1719.   int i;
  1720.   int offset;
  1721.   unsigned short insn;
  1722.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1723.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1724.   /* Create a local register image and set the initial state.  */
  1725.   for (i = 0; i < NUM_GENREGS; i++)
  1726.     {
  1727.       inst_env->reg[i] =
  1728.         (unsigned long) get_frame_register_unsigned (frame, i);
  1729.     }
  1730.   offset = NUM_GENREGS;
  1731.   for (i = 0; i < NUM_SPECREGS; i++)
  1732.     {
  1733.       inst_env->preg[i] =
  1734.         (unsigned long) get_frame_register_unsigned (frame, offset + i);
  1735.     }
  1736.   inst_env->branch_found = 0;
  1737.   inst_env->slot_needed = 0;
  1738.   inst_env->delay_slot_pc_active = 0;
  1739.   inst_env->prefix_found = 0;
  1740.   inst_env->invalid = 0;
  1741.   inst_env->xflag_found = 0;
  1742.   inst_env->disable_interrupt = 0;
  1743.   inst_env->byte_order = byte_order;

  1744.   /* Look for a step target.  */
  1745.   do
  1746.     {
  1747.       /* Read an instruction from the client.  */
  1748.       insn = read_memory_unsigned_integer
  1749.              (inst_env->reg[gdbarch_pc_regnum (gdbarch)], 2, byte_order);

  1750.       /* If the instruction is not in a delay slot the new content of the
  1751.          PC is [PC] + 2.  If the instruction is in a delay slot it is not
  1752.          that simple.  Since a instruction in a delay slot cannot change
  1753.          the content of the PC, it does not matter what value PC will have.
  1754.          Just make sure it is a valid instruction.  */
  1755.       if (!inst_env->delay_slot_pc_active)
  1756.         {
  1757.           inst_env->reg[gdbarch_pc_regnum (gdbarch)] += 2;
  1758.         }
  1759.       else
  1760.         {
  1761.           inst_env->delay_slot_pc_active = 0;
  1762.           inst_env->reg[gdbarch_pc_regnum (gdbarch)]
  1763.             = inst_env->delay_slot_pc;
  1764.         }
  1765.       /* Analyse the present instruction.  */
  1766.       i = find_cris_op (insn, inst_env);
  1767.       if (i == -1)
  1768.         {
  1769.           inst_env->invalid = 1;
  1770.         }
  1771.       else
  1772.         {
  1773.           cris_gdb_func (gdbarch, cris_opcodes[i].op, insn, inst_env);
  1774.         }
  1775.     } while (!inst_env->invalid
  1776.              && (inst_env->prefix_found || inst_env->xflag_found
  1777.                  || inst_env->slot_needed));
  1778.   return i;
  1779. }

  1780. /* There is no hardware single-step support.  The function find_step_target
  1781.    digs through the opcodes in order to find all possible targets.
  1782.    Either one ordinary target or two targets for branches may be found.  */

  1783. static int
  1784. cris_software_single_step (struct frame_info *frame)
  1785. {
  1786.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1787.   struct address_space *aspace = get_frame_address_space (frame);
  1788.   inst_env_type inst_env;

  1789.   /* Analyse the present instruction environment and insert
  1790.      breakpoints.  */
  1791.   int status = find_step_target (frame, &inst_env);
  1792.   if (status == -1)
  1793.     {
  1794.       /* Could not find a target.  Things are likely to go downhill
  1795.          from here.  */
  1796.       warning (_("CRIS software single step could not find a step target."));
  1797.     }
  1798.   else
  1799.     {
  1800.       /* Insert at most two breakpoints.  One for the next PC content
  1801.          and possibly another one for a branch, jump, etc.  */
  1802.       CORE_ADDR next_pc
  1803.         = (CORE_ADDR) inst_env.reg[gdbarch_pc_regnum (gdbarch)];
  1804.       insert_single_step_breakpoint (gdbarch, aspace, next_pc);
  1805.       if (inst_env.branch_found
  1806.           && (CORE_ADDR) inst_env.branch_break_address != next_pc)
  1807.         {
  1808.           CORE_ADDR branch_target_address
  1809.                 = (CORE_ADDR) inst_env.branch_break_address;
  1810.           insert_single_step_breakpoint (gdbarch,
  1811.                                          aspace, branch_target_address);
  1812.         }
  1813.     }

  1814.   return 1;
  1815. }

  1816. /* Calculates the prefix value for quick offset addressing mode.  */

  1817. static void
  1818. quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
  1819. {
  1820.   /* It's invalid to be in a delay slot.  You can't have a prefix to this
  1821.      instruction (not 100% sure).  */
  1822.   if (inst_env->slot_needed || inst_env->prefix_found)
  1823.     {
  1824.       inst_env->invalid = 1;
  1825.       return;
  1826.     }

  1827.   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
  1828.   inst_env->prefix_value += cris_get_bdap_quick_offset (inst);

  1829.   /* A prefix doesn't change the xflag_found.  But the rest of the flags
  1830.      need updating.  */
  1831.   inst_env->slot_needed = 0;
  1832.   inst_env->prefix_found = 1;
  1833. }

  1834. /* Updates the autoincrement register.  The size of the increment is derived
  1835.    from the size of the operation.  The PC is always kept aligned on even
  1836.    word addresses.  */

  1837. static void
  1838. process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
  1839. {
  1840.   if (size == INST_BYTE_SIZE)
  1841.     {
  1842.       inst_env->reg[cris_get_operand1 (inst)] += 1;

  1843.       /* The PC must be word aligned, so increase the PC with one
  1844.          word even if the size is byte.  */
  1845.       if (cris_get_operand1 (inst) == REG_PC)
  1846.         {
  1847.           inst_env->reg[REG_PC] += 1;
  1848.         }
  1849.     }
  1850.   else if (size == INST_WORD_SIZE)
  1851.     {
  1852.       inst_env->reg[cris_get_operand1 (inst)] += 2;
  1853.     }
  1854.   else if (size == INST_DWORD_SIZE)
  1855.     {
  1856.       inst_env->reg[cris_get_operand1 (inst)] += 4;
  1857.     }
  1858.   else
  1859.     {
  1860.       /* Invalid size.  */
  1861.       inst_env->invalid = 1;
  1862.     }
  1863. }

  1864. /* Just a forward declaration.  */

  1865. static unsigned long get_data_from_address (unsigned short *inst,
  1866.                                             CORE_ADDR address,
  1867.                                             enum bfd_endian byte_order);

  1868. /* Calculates the prefix value for the general case of offset addressing
  1869.    mode.  */

  1870. static void
  1871. bdap_prefix (unsigned short inst, inst_env_type *inst_env)
  1872. {
  1873.   /* It's invalid to be in a delay slot.  */
  1874.   if (inst_env->slot_needed || inst_env->prefix_found)
  1875.     {
  1876.       inst_env->invalid = 1;
  1877.       return;
  1878.     }

  1879.   /* The calculation of prefix_value used to be after process_autoincrement,
  1880.      but that fails for an instruction such as jsr [$r0+12] which is encoded
  1881.      as 5f0d 0c00 30b9 when compiled with -fpic.  Since PC is operand1 it
  1882.      mustn't be incremented until we have read it and what it points at.  */
  1883.   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];

  1884.   /* The offset is an indirection of the contents of the operand1 register.  */
  1885.   inst_env->prefix_value +=
  1886.     get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)],
  1887.                            inst_env->byte_order);

  1888.   if (cris_get_mode (inst) == AUTOINC_MODE)
  1889.     {
  1890.       process_autoincrement (cris_get_size (inst), inst, inst_env);
  1891.     }

  1892.   /* A prefix doesn't change the xflag_found.  But the rest of the flags
  1893.      need updating.  */
  1894.   inst_env->slot_needed = 0;
  1895.   inst_env->prefix_found = 1;
  1896. }

  1897. /* Calculates the prefix value for the index addressing mode.  */

  1898. static void
  1899. biap_prefix (unsigned short inst, inst_env_type *inst_env)
  1900. {
  1901.   /* It's invalid to be in a delay slot.  I can't see that it's possible to
  1902.      have a prefix to this instruction.  So I will treat this as invalid.  */
  1903.   if (inst_env->slot_needed || inst_env->prefix_found)
  1904.     {
  1905.       inst_env->invalid = 1;
  1906.       return;
  1907.     }

  1908.   inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];

  1909.   /* The offset is the operand2 value shifted the size of the instruction
  1910.      to the left.  */
  1911.   inst_env->prefix_value +=
  1912.     inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);

  1913.   /* If the PC is operand1 (base) the address used is the address after
  1914.      the main instruction, i.e. address + 2 (the PC is already compensated
  1915.      for the prefix operation).  */
  1916.   if (cris_get_operand1 (inst) == REG_PC)
  1917.     {
  1918.       inst_env->prefix_value += 2;
  1919.     }

  1920.   /* A prefix doesn't change the xflag_found.  But the rest of the flags
  1921.      need updating.  */
  1922.   inst_env->slot_needed = 0;
  1923.   inst_env->xflag_found = 0;
  1924.   inst_env->prefix_found = 1;
  1925. }

  1926. /* Calculates the prefix value for the double indirect addressing mode.  */

  1927. static void
  1928. dip_prefix (unsigned short inst, inst_env_type *inst_env)
  1929. {

  1930.   CORE_ADDR address;

  1931.   /* It's invalid to be in a delay slot.  */
  1932.   if (inst_env->slot_needed || inst_env->prefix_found)
  1933.     {
  1934.       inst_env->invalid = 1;
  1935.       return;
  1936.     }

  1937.   /* The prefix value is one dereference of the contents of the operand1
  1938.      register.  */
  1939.   address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
  1940.   inst_env->prefix_value
  1941.     = read_memory_unsigned_integer (address, 4, inst_env->byte_order);

  1942.   /* Check if the mode is autoincrement.  */
  1943.   if (cris_get_mode (inst) == AUTOINC_MODE)
  1944.     {
  1945.       inst_env->reg[cris_get_operand1 (inst)] += 4;
  1946.     }

  1947.   /* A prefix doesn't change the xflag_found.  But the rest of the flags
  1948.      need updating.  */
  1949.   inst_env->slot_needed = 0;
  1950.   inst_env->xflag_found = 0;
  1951.   inst_env->prefix_found = 1;
  1952. }

  1953. /* Finds the destination for a branch with 8-bits offset.  */

  1954. static void
  1955. eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
  1956. {

  1957.   short offset;

  1958.   /* If we have a prefix or are in a delay slot it's bad.  */
  1959.   if (inst_env->slot_needed || inst_env->prefix_found)
  1960.     {
  1961.       inst_env->invalid = 1;
  1962.       return;
  1963.     }

  1964.   /* We have a branch, find out where the branch will land.  */
  1965.   offset = cris_get_branch_short_offset (inst);

  1966.   /* Check if the offset is signed.  */
  1967.   if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
  1968.     {
  1969.       offset |= 0xFF00;
  1970.     }

  1971.   /* The offset ends with the sign bit, set it to zero.  The address
  1972.      should always be word aligned.  */
  1973.   offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;

  1974.   inst_env->branch_found = 1;
  1975.   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;

  1976.   inst_env->slot_needed = 1;
  1977.   inst_env->prefix_found = 0;
  1978.   inst_env->xflag_found = 0;
  1979.   inst_env->disable_interrupt = 1;
  1980. }

  1981. /* Finds the destination for a branch with 16-bits offset.  */

  1982. static void
  1983. sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
  1984. {
  1985.   short offset;

  1986.   /* If we have a prefix or is in a delay slot it's bad.  */
  1987.   if (inst_env->slot_needed || inst_env->prefix_found)
  1988.     {
  1989.       inst_env->invalid = 1;
  1990.       return;
  1991.     }

  1992.   /* We have a branch, find out the offset for the branch.  */
  1993.   offset = read_memory_integer (inst_env->reg[REG_PC], 2,
  1994.                                 inst_env->byte_order);

  1995.   /* The instruction is one word longer than normal, so add one word
  1996.      to the PC.  */
  1997.   inst_env->reg[REG_PC] += 2;

  1998.   inst_env->branch_found = 1;
  1999.   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;


  2000.   inst_env->slot_needed = 1;
  2001.   inst_env->prefix_found = 0;
  2002.   inst_env->xflag_found = 0;
  2003.   inst_env->disable_interrupt = 1;
  2004. }

  2005. /* Handles the ABS instruction.  */

  2006. static void
  2007. abs_op (unsigned short inst, inst_env_type *inst_env)
  2008. {

  2009.   long value;

  2010.   /* ABS can't have a prefix, so it's bad if it does.  */
  2011.   if (inst_env->prefix_found)
  2012.     {
  2013.       inst_env->invalid = 1;
  2014.       return;
  2015.     }

  2016.   /* Check if the operation affects the PC.  */
  2017.   if (cris_get_operand2 (inst) == REG_PC)
  2018.     {

  2019.       /* It's invalid to change to the PC if we are in a delay slot.  */
  2020.       if (inst_env->slot_needed)
  2021.         {
  2022.           inst_env->invalid = 1;
  2023.           return;
  2024.         }

  2025.       value = (long) inst_env->reg[REG_PC];

  2026.       /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK.  */
  2027.       if (value != SIGNED_DWORD_MASK)
  2028.         {
  2029.           value = -value;
  2030.           inst_env->reg[REG_PC] = (long) value;
  2031.         }
  2032.     }

  2033.   inst_env->slot_needed = 0;
  2034.   inst_env->prefix_found = 0;
  2035.   inst_env->xflag_found = 0;
  2036.   inst_env->disable_interrupt = 0;
  2037. }

  2038. /* Handles the ADDI instruction.  */

  2039. static void
  2040. addi_op (unsigned short inst, inst_env_type *inst_env)
  2041. {
  2042.   /* It's invalid to have the PC as base register.  And ADDI can't have
  2043.      a prefix.  */
  2044.   if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
  2045.     {
  2046.       inst_env->invalid = 1;
  2047.       return;
  2048.     }

  2049.   inst_env->slot_needed = 0;
  2050.   inst_env->prefix_found = 0;
  2051.   inst_env->xflag_found = 0;
  2052.   inst_env->disable_interrupt = 0;
  2053. }

  2054. /* Handles the ASR instruction.  */

  2055. static void
  2056. asr_op (unsigned short inst, inst_env_type *inst_env)
  2057. {
  2058.   int shift_steps;
  2059.   unsigned long value;
  2060.   unsigned long signed_extend_mask = 0;

  2061.   /* ASR can't have a prefix, so check that it doesn't.  */
  2062.   if (inst_env->prefix_found)
  2063.     {
  2064.       inst_env->invalid = 1;
  2065.       return;
  2066.     }

  2067.   /* Check if the PC is the target register.  */
  2068.   if (cris_get_operand2 (inst) == REG_PC)
  2069.     {
  2070.       /* It's invalid to change the PC in a delay slot.  */
  2071.       if (inst_env->slot_needed)
  2072.         {
  2073.           inst_env->invalid = 1;
  2074.           return;
  2075.         }
  2076.       /* Get the number of bits to shift.  */
  2077.       shift_steps
  2078.         = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
  2079.       value = inst_env->reg[REG_PC];

  2080.       /* Find out how many bits the operation should apply to.  */
  2081.       if (cris_get_size (inst) == INST_BYTE_SIZE)
  2082.         {
  2083.           if (value & SIGNED_BYTE_MASK)
  2084.             {
  2085.               signed_extend_mask = 0xFF;
  2086.               signed_extend_mask = signed_extend_mask >> shift_steps;
  2087.               signed_extend_mask = ~signed_extend_mask;
  2088.             }
  2089.           value = value >> shift_steps;
  2090.           value |= signed_extend_mask;
  2091.           value &= 0xFF;
  2092.           inst_env->reg[REG_PC] &= 0xFFFFFF00;
  2093.           inst_env->reg[REG_PC] |= value;
  2094.         }
  2095.       else if (cris_get_size (inst) == INST_WORD_SIZE)
  2096.         {
  2097.           if (value & SIGNED_WORD_MASK)
  2098.             {
  2099.               signed_extend_mask = 0xFFFF;
  2100.               signed_extend_mask = signed_extend_mask >> shift_steps;
  2101.               signed_extend_mask = ~signed_extend_mask;
  2102.             }
  2103.           value = value >> shift_steps;
  2104.           value |= signed_extend_mask;
  2105.           value &= 0xFFFF;
  2106.           inst_env->reg[REG_PC] &= 0xFFFF0000;
  2107.           inst_env->reg[REG_PC] |= value;
  2108.         }
  2109.       else if (cris_get_size (inst) == INST_DWORD_SIZE)
  2110.         {
  2111.           if (value & SIGNED_DWORD_MASK)
  2112.             {
  2113.               signed_extend_mask = 0xFFFFFFFF;
  2114.               signed_extend_mask = signed_extend_mask >> shift_steps;
  2115.               signed_extend_mask = ~signed_extend_mask;
  2116.             }
  2117.           value = value >> shift_steps;
  2118.           value |= signed_extend_mask;
  2119.           inst_env->reg[REG_PC]  = value;
  2120.         }
  2121.     }
  2122.   inst_env->slot_needed = 0;
  2123.   inst_env->prefix_found = 0;
  2124.   inst_env->xflag_found = 0;
  2125.   inst_env->disable_interrupt = 0;
  2126. }

  2127. /* Handles the ASRQ instruction.  */

  2128. static void
  2129. asrq_op (unsigned short inst, inst_env_type *inst_env)
  2130. {

  2131.   int shift_steps;
  2132.   unsigned long value;
  2133.   unsigned long signed_extend_mask = 0;

  2134.   /* ASRQ can't have a prefix, so check that it doesn't.  */
  2135.   if (inst_env->prefix_found)
  2136.     {
  2137.       inst_env->invalid = 1;
  2138.       return;
  2139.     }

  2140.   /* Check if the PC is the target register.  */
  2141.   if (cris_get_operand2 (inst) == REG_PC)
  2142.     {

  2143.       /* It's invalid to change the PC in a delay slot.  */
  2144.       if (inst_env->slot_needed)
  2145.         {
  2146.           inst_env->invalid = 1;
  2147.           return;
  2148.         }
  2149.       /* The shift size is given as a 5 bit quick value, i.e. we don't
  2150.          want the sign bit of the quick value.  */
  2151.       shift_steps = cris_get_asr_shift_steps (inst);
  2152.       value = inst_env->reg[REG_PC];
  2153.       if (value & SIGNED_DWORD_MASK)
  2154.         {
  2155.           signed_extend_mask = 0xFFFFFFFF;
  2156.           signed_extend_mask = signed_extend_mask >> shift_steps;
  2157.           signed_extend_mask = ~signed_extend_mask;
  2158.         }
  2159.       value = value >> shift_steps;
  2160.       value |= signed_extend_mask;
  2161.       inst_env->reg[REG_PC]  = value;
  2162.     }
  2163.   inst_env->slot_needed = 0;
  2164.   inst_env->prefix_found = 0;
  2165.   inst_env->xflag_found = 0;
  2166.   inst_env->disable_interrupt = 0;
  2167. }

  2168. /* Handles the AX, EI and SETF instruction.  */

  2169. static void
  2170. ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
  2171. {
  2172.   if (inst_env->prefix_found)
  2173.     {
  2174.       inst_env->invalid = 1;
  2175.       return;
  2176.     }
  2177.   /* Check if the instruction is setting the X flag.  */
  2178.   if (cris_is_xflag_bit_on (inst))
  2179.     {
  2180.       inst_env->xflag_found = 1;
  2181.     }
  2182.   else
  2183.     {
  2184.       inst_env->xflag_found = 0;
  2185.     }
  2186.   inst_env->slot_needed = 0;
  2187.   inst_env->prefix_found = 0;
  2188.   inst_env->disable_interrupt = 1;
  2189. }

  2190. /* Checks if the instruction is in assign mode.  If so, it updates the assign
  2191.    register.  Note that check_assign assumes that the caller has checked that
  2192.    there is a prefix to this instruction.  The mode check depends on this.  */

  2193. static void
  2194. check_assign (unsigned short inst, inst_env_type *inst_env)
  2195. {
  2196.   /* Check if it's an assign addressing mode.  */
  2197.   if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
  2198.     {
  2199.       /* Assign the prefix value to operand 1.  */
  2200.       inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
  2201.     }
  2202. }

  2203. /* Handles the 2-operand BOUND instruction.  */

  2204. static void
  2205. two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
  2206. {
  2207.   /* It's invalid to have the PC as the index operand.  */
  2208.   if (cris_get_operand2 (inst) == REG_PC)
  2209.     {
  2210.       inst_env->invalid = 1;
  2211.       return;
  2212.     }
  2213.   /* Check if we have a prefix.  */
  2214.   if (inst_env->prefix_found)
  2215.     {
  2216.       check_assign (inst, inst_env);
  2217.     }
  2218.   /* Check if this is an autoincrement mode.  */
  2219.   else if (cris_get_mode (inst) == AUTOINC_MODE)
  2220.     {
  2221.       /* It's invalid to change the PC in a delay slot.  */
  2222.       if (inst_env->slot_needed)
  2223.         {
  2224.           inst_env->invalid = 1;
  2225.           return;
  2226.         }
  2227.       process_autoincrement (cris_get_size (inst), inst, inst_env);
  2228.     }
  2229.   inst_env->slot_needed = 0;
  2230.   inst_env->prefix_found = 0;
  2231.   inst_env->xflag_found = 0;
  2232.   inst_env->disable_interrupt = 0;
  2233. }

  2234. /* Handles the 3-operand BOUND instruction.  */

  2235. static void
  2236. three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
  2237. {
  2238.   /* It's an error if we haven't got a prefix.  And it's also an error
  2239.      if the PC is the destination register.  */
  2240.   if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
  2241.     {
  2242.       inst_env->invalid = 1;
  2243.       return;
  2244.     }
  2245.   inst_env->slot_needed = 0;
  2246.   inst_env->prefix_found = 0;
  2247.   inst_env->xflag_found = 0;
  2248.   inst_env->disable_interrupt = 0;
  2249. }

  2250. /* Clears the status flags in inst_env.  */

  2251. static void
  2252. btst_nop_op (unsigned short inst, inst_env_type *inst_env)
  2253. {
  2254.   /* It's an error if we have got a prefix.  */
  2255.   if (inst_env->prefix_found)
  2256.     {
  2257.       inst_env->invalid = 1;
  2258.       return;
  2259.     }

  2260.   inst_env->slot_needed = 0;
  2261.   inst_env->prefix_found = 0;
  2262.   inst_env->xflag_found = 0;
  2263.   inst_env->disable_interrupt = 0;
  2264. }

  2265. /* Clears the status flags in inst_env.  */

  2266. static void
  2267. clearf_di_op (unsigned short inst, inst_env_type *inst_env)
  2268. {
  2269.   /* It's an error if we have got a prefix.  */
  2270.   if (inst_env->prefix_found)
  2271.     {
  2272.       inst_env->invalid = 1;
  2273.       return;
  2274.     }

  2275.   inst_env->slot_needed = 0;
  2276.   inst_env->prefix_found = 0;
  2277.   inst_env->xflag_found = 0;
  2278.   inst_env->disable_interrupt = 1;
  2279. }

  2280. /* Handles the CLEAR instruction if it's in register mode.  */

  2281. static void
  2282. reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
  2283. {
  2284.   /* Check if the target is the PC.  */
  2285.   if (cris_get_operand2 (inst) == REG_PC)
  2286.     {
  2287.       /* The instruction will clear the instruction's size bits.  */
  2288.       int clear_size = cris_get_clear_size (inst);
  2289.       if (clear_size == INST_BYTE_SIZE)
  2290.         {
  2291.           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
  2292.         }
  2293.       if (clear_size == INST_WORD_SIZE)
  2294.         {
  2295.           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
  2296.         }
  2297.       if (clear_size == INST_DWORD_SIZE)
  2298.         {
  2299.           inst_env->delay_slot_pc = 0x0;
  2300.         }
  2301.       /* The jump will be delayed with one delay slot.  So we need a delay
  2302.          slot.  */
  2303.       inst_env->slot_needed = 1;
  2304.       inst_env->delay_slot_pc_active = 1;
  2305.     }
  2306.   else
  2307.     {
  2308.       /* The PC will not change => no delay slot.  */
  2309.       inst_env->slot_needed = 0;
  2310.     }
  2311.   inst_env->prefix_found = 0;
  2312.   inst_env->xflag_found = 0;
  2313.   inst_env->disable_interrupt = 0;
  2314. }

  2315. /* Handles the TEST instruction if it's in register mode.  */

  2316. static void
  2317. reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
  2318. {
  2319.   /* It's an error if we have got a prefix.  */
  2320.   if (inst_env->prefix_found)
  2321.     {
  2322.       inst_env->invalid = 1;
  2323.       return;
  2324.     }
  2325.   inst_env->slot_needed = 0;
  2326.   inst_env->prefix_found = 0;
  2327.   inst_env->xflag_found = 0;
  2328.   inst_env->disable_interrupt = 0;

  2329. }

  2330. /* Handles the CLEAR and TEST instruction if the instruction isn't
  2331.    in register mode.  */

  2332. static void
  2333. none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
  2334. {
  2335.   /* Check if we are in a prefix mode.  */
  2336.   if (inst_env->prefix_found)
  2337.     {
  2338.       /* The only way the PC can change is if this instruction is in
  2339.          assign addressing mode.  */
  2340.       check_assign (inst, inst_env);
  2341.     }
  2342.   /* Indirect mode can't change the PC so just check if the mode is
  2343.      autoincrement.  */
  2344.   else if (cris_get_mode (inst) == AUTOINC_MODE)
  2345.     {
  2346.       process_autoincrement (cris_get_size (inst), inst, inst_env);
  2347.     }
  2348.   inst_env->slot_needed = 0;
  2349.   inst_env->prefix_found = 0;
  2350.   inst_env->xflag_found = 0;
  2351.   inst_env->disable_interrupt = 0;
  2352. }

  2353. /* Checks that the PC isn't the destination register or the instructions has
  2354.    a prefix.  */

  2355. static void
  2356. dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
  2357. {
  2358.   /* It's invalid to have the PC as the destination.  The instruction can't
  2359.      have a prefix.  */
  2360.   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
  2361.     {
  2362.       inst_env->invalid = 1;
  2363.       return;
  2364.     }

  2365.   inst_env->slot_needed = 0;
  2366.   inst_env->prefix_found = 0;
  2367.   inst_env->xflag_found = 0;
  2368.   inst_env->disable_interrupt = 0;
  2369. }

  2370. /* Checks that the instruction doesn't have a prefix.  */

  2371. static void
  2372. break_op (unsigned short inst, inst_env_type *inst_env)
  2373. {
  2374.   /* The instruction can't have a prefix.  */
  2375.   if (inst_env->prefix_found)
  2376.     {
  2377.       inst_env->invalid = 1;
  2378.       return;
  2379.     }

  2380.   inst_env->slot_needed = 0;
  2381.   inst_env->prefix_found = 0;
  2382.   inst_env->xflag_found = 0;
  2383.   inst_env->disable_interrupt = 1;
  2384. }

  2385. /* Checks that the PC isn't the destination register and that the instruction
  2386.    doesn't have a prefix.  */

  2387. static void
  2388. scc_op (unsigned short inst, inst_env_type *inst_env)
  2389. {
  2390.   /* It's invalid to have the PC as the destination.  The instruction can't
  2391.      have a prefix.  */
  2392.   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
  2393.     {
  2394.       inst_env->invalid = 1;
  2395.       return;
  2396.     }

  2397.   inst_env->slot_needed = 0;
  2398.   inst_env->prefix_found = 0;
  2399.   inst_env->xflag_found = 0;
  2400.   inst_env->disable_interrupt = 1;
  2401. }

  2402. /* Handles the register mode JUMP instruction.  */

  2403. static void
  2404. reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
  2405. {
  2406.   /* It's invalid to do a JUMP in a delay slot.  The mode is register, so
  2407.      you can't have a prefix.  */
  2408.   if ((inst_env->slot_needed) || (inst_env->prefix_found))
  2409.     {
  2410.       inst_env->invalid = 1;
  2411.       return;
  2412.     }

  2413.   /* Just change the PC.  */
  2414.   inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
  2415.   inst_env->slot_needed = 0;
  2416.   inst_env->prefix_found = 0;
  2417.   inst_env->xflag_found = 0;
  2418.   inst_env->disable_interrupt = 1;
  2419. }

  2420. /* Handles the JUMP instruction for all modes except register.  */

  2421. static void
  2422. none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
  2423. {
  2424.   unsigned long newpc;
  2425.   CORE_ADDR address;

  2426.   /* It's invalid to do a JUMP in a delay slot.  */
  2427.   if (inst_env->slot_needed)
  2428.     {
  2429.       inst_env->invalid = 1;
  2430.     }
  2431.   else
  2432.     {
  2433.       /* Check if we have a prefix.  */
  2434.       if (inst_env->prefix_found)
  2435.         {
  2436.           check_assign (inst, inst_env);

  2437.           /* Get the new value for the PC.  */
  2438.           newpc =
  2439.             read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
  2440.                                           4, inst_env->byte_order);
  2441.         }
  2442.       else
  2443.         {
  2444.           /* Get the new value for the PC.  */
  2445.           address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
  2446.           newpc = read_memory_unsigned_integer (address,
  2447.                                                 4, inst_env->byte_order);

  2448.           /* Check if we should increment a register.  */
  2449.           if (cris_get_mode (inst) == AUTOINC_MODE)
  2450.             {
  2451.               inst_env->reg[cris_get_operand1 (inst)] += 4;
  2452.             }
  2453.         }
  2454.       inst_env->reg[REG_PC] = newpc;
  2455.     }
  2456.   inst_env->slot_needed = 0;
  2457.   inst_env->prefix_found = 0;
  2458.   inst_env->xflag_found = 0;
  2459.   inst_env->disable_interrupt = 1;
  2460. }

  2461. /* Handles moves to special registers (aka P-register) for all modes.  */

  2462. static void
  2463. move_to_preg_op (struct gdbarch *gdbarch, unsigned short inst,
  2464.                  inst_env_type *inst_env)
  2465. {
  2466.   if (inst_env->prefix_found)
  2467.     {
  2468.       /* The instruction has a prefix that means we are only interested if
  2469.          the instruction is in assign mode.  */
  2470.       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
  2471.         {
  2472.           /* The prefix handles the problem if we are in a delay slot.  */
  2473.           if (cris_get_operand1 (inst) == REG_PC)
  2474.             {
  2475.               /* Just take care of the assign.  */
  2476.               check_assign (inst, inst_env);
  2477.             }
  2478.         }
  2479.     }
  2480.   else if (cris_get_mode (inst) == AUTOINC_MODE)
  2481.     {
  2482.       /* The instruction doesn't have a prefix, the only case left that we
  2483.          are interested in is the autoincrement mode.  */
  2484.       if (cris_get_operand1 (inst) == REG_PC)
  2485.         {
  2486.           /* If the PC is to be incremented it's invalid to be in a
  2487.              delay slot.  */
  2488.           if (inst_env->slot_needed)
  2489.             {
  2490.               inst_env->invalid = 1;
  2491.               return;
  2492.             }

  2493.           /* The increment depends on the size of the special register.  */
  2494.           if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
  2495.             {
  2496.               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
  2497.             }
  2498.           else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
  2499.             {
  2500.               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
  2501.             }
  2502.           else
  2503.             {
  2504.               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
  2505.             }
  2506.         }
  2507.     }
  2508.   inst_env->slot_needed = 0;
  2509.   inst_env->prefix_found = 0;
  2510.   inst_env->xflag_found = 0;
  2511.   inst_env->disable_interrupt = 1;
  2512. }

  2513. /* Handles moves from special registers (aka P-register) for all modes
  2514.    except register.  */

  2515. static void
  2516. none_reg_mode_move_from_preg_op (struct gdbarch *gdbarch, unsigned short inst,
  2517.                                  inst_env_type *inst_env)
  2518. {
  2519.   if (inst_env->prefix_found)
  2520.     {
  2521.       /* The instruction has a prefix that means we are only interested if
  2522.          the instruction is in assign mode.  */
  2523.       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
  2524.         {
  2525.           /* The prefix handles the problem if we are in a delay slot.  */
  2526.           if (cris_get_operand1 (inst) == REG_PC)
  2527.             {
  2528.               /* Just take care of the assign.  */
  2529.               check_assign (inst, inst_env);
  2530.             }
  2531.         }
  2532.     }
  2533.   /* The instruction doesn't have a prefix, the only case left that we
  2534.      are interested in is the autoincrement mode.  */
  2535.   else if (cris_get_mode (inst) == AUTOINC_MODE)
  2536.     {
  2537.       if (cris_get_operand1 (inst) == REG_PC)
  2538.         {
  2539.           /* If the PC is to be incremented it's invalid to be in a
  2540.              delay slot.  */
  2541.           if (inst_env->slot_needed)
  2542.             {
  2543.               inst_env->invalid = 1;
  2544.               return;
  2545.             }

  2546.           /* The increment depends on the size of the special register.  */
  2547.           if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
  2548.             {
  2549.               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
  2550.             }
  2551.           else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
  2552.             {
  2553.               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
  2554.             }
  2555.           else
  2556.             {
  2557.               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
  2558.             }
  2559.         }
  2560.     }
  2561.   inst_env->slot_needed = 0;
  2562.   inst_env->prefix_found = 0;
  2563.   inst_env->xflag_found = 0;
  2564.   inst_env->disable_interrupt = 1;
  2565. }

  2566. /* Handles moves from special registers (aka P-register) when the mode
  2567.    is register.  */

  2568. static void
  2569. reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
  2570. {
  2571.   /* Register mode move from special register can't have a prefix.  */
  2572.   if (inst_env->prefix_found)
  2573.     {
  2574.       inst_env->invalid = 1;
  2575.       return;
  2576.     }

  2577.   if (cris_get_operand1 (inst) == REG_PC)
  2578.     {
  2579.       /* It's invalid to change the PC in a delay slot.  */
  2580.       if (inst_env->slot_needed)
  2581.         {
  2582.           inst_env->invalid = 1;
  2583.           return;
  2584.         }
  2585.       /* The destination is the PC, the jump will have a delay slot.  */
  2586.       inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
  2587.       inst_env->slot_needed = 1;
  2588.       inst_env->delay_slot_pc_active = 1;
  2589.     }
  2590.   else
  2591.     {
  2592.       /* If the destination isn't PC, there will be no jump.  */
  2593.       inst_env->slot_needed = 0;
  2594.     }
  2595.   inst_env->prefix_found = 0;
  2596.   inst_env->xflag_found = 0;
  2597.   inst_env->disable_interrupt = 1;
  2598. }

  2599. /* Handles the MOVEM from memory to general register instruction.  */

  2600. static void
  2601. move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
  2602. {
  2603.   if (inst_env->prefix_found)
  2604.     {
  2605.       /* The prefix handles the problem if we are in a delay slot.  Is the
  2606.          MOVEM instruction going to change the PC?  */
  2607.       if (cris_get_operand2 (inst) >= REG_PC)
  2608.         {
  2609.           inst_env->reg[REG_PC] =
  2610.             read_memory_unsigned_integer (inst_env->prefix_value,
  2611.                                           4, inst_env->byte_order);
  2612.         }
  2613.       /* The assign value is the value after the increment.  Normally, the
  2614.          assign value is the value before the increment.  */
  2615.       if ((cris_get_operand1 (inst) == REG_PC)
  2616.           && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
  2617.         {
  2618.           inst_env->reg[REG_PC] = inst_env->prefix_value;
  2619.           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
  2620.         }
  2621.     }
  2622.   else
  2623.     {
  2624.       /* Is the MOVEM instruction going to change the PC?  */
  2625.       if (cris_get_operand2 (inst) == REG_PC)
  2626.         {
  2627.           /* It's invalid to change the PC in a delay slot.  */
  2628.           if (inst_env->slot_needed)
  2629.             {
  2630.               inst_env->invalid = 1;
  2631.               return;
  2632.             }
  2633.           inst_env->reg[REG_PC] =
  2634.             read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)],
  2635.                                           4, inst_env->byte_order);
  2636.         }
  2637.       /* The increment is not depending on the size, instead it's depending
  2638.          on the number of registers loaded from memory.  */
  2639.       if ((cris_get_operand1 (inst) == REG_PC)
  2640.           && (cris_get_mode (inst) == AUTOINC_MODE))
  2641.         {
  2642.           /* It's invalid to change the PC in a delay slot.  */
  2643.           if (inst_env->slot_needed)
  2644.             {
  2645.               inst_env->invalid = 1;
  2646.               return;
  2647.             }
  2648.           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
  2649.         }
  2650.     }
  2651.   inst_env->slot_needed = 0;
  2652.   inst_env->prefix_found = 0;
  2653.   inst_env->xflag_found = 0;
  2654.   inst_env->disable_interrupt = 0;
  2655. }

  2656. /* Handles the MOVEM to memory from general register instruction.  */

  2657. static void
  2658. move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
  2659. {
  2660.   if (inst_env->prefix_found)
  2661.     {
  2662.       /* The assign value is the value after the increment.  Normally, the
  2663.          assign value is the value before the increment.  */
  2664.       if ((cris_get_operand1 (inst) == REG_PC)
  2665.           && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
  2666.         {
  2667.           /* The prefix handles the problem if we are in a delay slot.  */
  2668.           inst_env->reg[REG_PC] = inst_env->prefix_value;
  2669.           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
  2670.         }
  2671.     }
  2672.   else
  2673.     {
  2674.       /* The increment is not depending on the size, instead it's depending
  2675.          on the number of registers loaded to memory.  */
  2676.       if ((cris_get_operand1 (inst) == REG_PC)
  2677.           && (cris_get_mode (inst) == AUTOINC_MODE))
  2678.         {
  2679.           /* It's invalid to change the PC in a delay slot.  */
  2680.           if (inst_env->slot_needed)
  2681.             {
  2682.               inst_env->invalid = 1;
  2683.               return;
  2684.             }
  2685.           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
  2686.         }
  2687.     }
  2688.   inst_env->slot_needed = 0;
  2689.   inst_env->prefix_found = 0;
  2690.   inst_env->xflag_found = 0;
  2691.   inst_env->disable_interrupt = 0;
  2692. }

  2693. /* Handles the intructions that's not yet implemented, by setting
  2694.    inst_env->invalid to true.  */

  2695. static void
  2696. not_implemented_op (unsigned short inst, inst_env_type *inst_env)
  2697. {
  2698.   inst_env->invalid = 1;
  2699. }

  2700. /* Handles the XOR instruction.  */

  2701. static void
  2702. xor_op (unsigned short inst, inst_env_type *inst_env)
  2703. {
  2704.   /* XOR can't have a prefix.  */
  2705.   if (inst_env->prefix_found)
  2706.     {
  2707.       inst_env->invalid = 1;
  2708.       return;
  2709.     }

  2710.   /* Check if the PC is the target.  */
  2711.   if (cris_get_operand2 (inst) == REG_PC)
  2712.     {
  2713.       /* It's invalid to change the PC in a delay slot.  */
  2714.       if (inst_env->slot_needed)
  2715.         {
  2716.           inst_env->invalid = 1;
  2717.           return;
  2718.         }
  2719.       inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
  2720.     }
  2721.   inst_env->slot_needed = 0;
  2722.   inst_env->prefix_found = 0;
  2723.   inst_env->xflag_found = 0;
  2724.   inst_env->disable_interrupt = 0;
  2725. }

  2726. /* Handles the MULS instruction.  */

  2727. static void
  2728. muls_op (unsigned short inst, inst_env_type *inst_env)
  2729. {
  2730.   /* MULS/U can't have a prefix.  */
  2731.   if (inst_env->prefix_found)
  2732.     {
  2733.       inst_env->invalid = 1;
  2734.       return;
  2735.     }

  2736.   /* Consider it invalid if the PC is the target.  */
  2737.   if (cris_get_operand2 (inst) == REG_PC)
  2738.     {
  2739.       inst_env->invalid = 1;
  2740.       return;
  2741.     }
  2742.   inst_env->slot_needed = 0;
  2743.   inst_env->prefix_found = 0;
  2744.   inst_env->xflag_found = 0;
  2745.   inst_env->disable_interrupt = 0;
  2746. }

  2747. /* Handles the MULU instruction.  */

  2748. static void
  2749. mulu_op (unsigned short inst, inst_env_type *inst_env)
  2750. {
  2751.   /* MULS/U can't have a prefix.  */
  2752.   if (inst_env->prefix_found)
  2753.     {
  2754.       inst_env->invalid = 1;
  2755.       return;
  2756.     }

  2757.   /* Consider it invalid if the PC is the target.  */
  2758.   if (cris_get_operand2 (inst) == REG_PC)
  2759.     {
  2760.       inst_env->invalid = 1;
  2761.       return;
  2762.     }
  2763.   inst_env->slot_needed = 0;
  2764.   inst_env->prefix_found = 0;
  2765.   inst_env->xflag_found = 0;
  2766.   inst_env->disable_interrupt = 0;
  2767. }

  2768. /* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE.
  2769.    The MOVE instruction is the move from source to register.  */

  2770. static void
  2771. add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env,
  2772.                                 unsigned long source1, unsigned long source2)
  2773. {
  2774.   unsigned long pc_mask;
  2775.   unsigned long operation_mask;

  2776.   /* Find out how many bits the operation should apply to.  */
  2777.   if (cris_get_size (inst) == INST_BYTE_SIZE)
  2778.     {
  2779.       pc_mask = 0xFFFFFF00;
  2780.       operation_mask = 0xFF;
  2781.     }
  2782.   else if (cris_get_size (inst) == INST_WORD_SIZE)
  2783.     {
  2784.       pc_mask = 0xFFFF0000;
  2785.       operation_mask = 0xFFFF;
  2786.     }
  2787.   else if (cris_get_size (inst) == INST_DWORD_SIZE)
  2788.     {
  2789.       pc_mask = 0x0;
  2790.       operation_mask = 0xFFFFFFFF;
  2791.     }
  2792.   else
  2793.     {
  2794.       /* The size is out of range.  */
  2795.       inst_env->invalid = 1;
  2796.       return;
  2797.     }

  2798.   /* The instruction just works on uw_operation_mask bits.  */
  2799.   source2 &= operation_mask;
  2800.   source1 &= operation_mask;

  2801.   /* Now calculate the result.  The opcode's 3 first bits separates
  2802.      the different actions.  */
  2803.   switch (cris_get_opcode (inst) & 7)
  2804.     {
  2805.     case 0/* add */
  2806.       source1 += source2;
  2807.       break;

  2808.     case 1/* move */
  2809.       source1 = source2;
  2810.       break;

  2811.     case 2/* subtract */
  2812.       source1 -= source2;
  2813.       break;

  2814.     case 3/* compare */
  2815.       break;

  2816.     case 4/* and */
  2817.       source1 &= source2;
  2818.       break;

  2819.     case 5/* or */
  2820.       source1 |= source2;
  2821.       break;

  2822.     default:
  2823.       inst_env->invalid = 1;
  2824.       return;

  2825.       break;
  2826.     }

  2827.   /* Make sure that the result doesn't contain more than the instruction
  2828.      size bits.  */
  2829.   source2 &= operation_mask;

  2830.   /* Calculate the new breakpoint address.  */
  2831.   inst_env->reg[REG_PC] &= pc_mask;
  2832.   inst_env->reg[REG_PC] |= source1;

  2833. }

  2834. /* Extends the value from either byte or word size to a dword.  If the mode
  2835.    is zero extend then the value is extended with zero.  If instead the mode
  2836.    is signed extend the sign bit of the value is taken into consideration.  */

  2837. static unsigned long
  2838. do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
  2839. {
  2840.   /* The size can be either byte or word, check which one it is.
  2841.      Don't check the highest bit, it's indicating if it's a zero
  2842.      or sign extend.  */
  2843.   if (cris_get_size (*inst) & INST_WORD_SIZE)
  2844.     {
  2845.       /* Word size.  */
  2846.       value &= 0xFFFF;

  2847.       /* Check if the instruction is signed extend.  If so, check if value has
  2848.          the sign bit on.  */
  2849.       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
  2850.         {
  2851.           value |= SIGNED_WORD_EXTEND_MASK;
  2852.         }
  2853.     }
  2854.   else
  2855.     {
  2856.       /* Byte size.  */
  2857.       value &= 0xFF;

  2858.       /* Check if the instruction is signed extend.  If so, check if value has
  2859.          the sign bit on.  */
  2860.       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
  2861.         {
  2862.           value |= SIGNED_BYTE_EXTEND_MASK;
  2863.         }
  2864.     }
  2865.   /* The size should now be dword.  */
  2866.   cris_set_size_to_dword (inst);
  2867.   return value;
  2868. }

  2869. /* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
  2870.    instruction.  The MOVE instruction is the move from source to register.  */

  2871. static void
  2872. reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
  2873.                                      inst_env_type *inst_env)
  2874. {
  2875.   unsigned long operand1;
  2876.   unsigned long operand2;

  2877.   /* It's invalid to have a prefix to the instruction.  This is a register
  2878.      mode instruction and can't have a prefix.  */
  2879.   if (inst_env->prefix_found)
  2880.     {
  2881.       inst_env->invalid = 1;
  2882.       return;
  2883.     }
  2884.   /* Check if the instruction has PC as its target.  */
  2885.   if (cris_get_operand2 (inst) == REG_PC)
  2886.     {
  2887.       if (inst_env->slot_needed)
  2888.         {
  2889.           inst_env->invalid = 1;
  2890.           return;
  2891.         }
  2892.       /* The instruction has the PC as its target register.  */
  2893.       operand1 = inst_env->reg[cris_get_operand1 (inst)];
  2894.       operand2 = inst_env->reg[REG_PC];

  2895.       /* Check if it's a extend, signed or zero instruction.  */
  2896.       if (cris_get_opcode (inst) < 4)
  2897.         {
  2898.           operand1 = do_sign_or_zero_extend (operand1, &inst);
  2899.         }
  2900.       /* Calculate the PC value after the instruction, i.e. where the
  2901.          breakpoint should be.  The order of the udw_operands is vital.  */
  2902.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
  2903.     }
  2904.   inst_env->slot_needed = 0;
  2905.   inst_env->prefix_found = 0;
  2906.   inst_env->xflag_found = 0;
  2907.   inst_env->disable_interrupt = 0;
  2908. }

  2909. /* Returns the data contained at address.  The size of the data is derived from
  2910.    the size of the operation.  If the instruction is a zero or signed
  2911.    extend instruction, the size field is changed in instruction.  */

  2912. static unsigned long
  2913. get_data_from_address (unsigned short *inst, CORE_ADDR address,
  2914.                        enum bfd_endian byte_order)
  2915. {
  2916.   int size = cris_get_size (*inst);
  2917.   unsigned long value;

  2918.   /* If it's an extend instruction we don't want the signed extend bit,
  2919.      because it influences the size.  */
  2920.   if (cris_get_opcode (*inst) < 4)
  2921.     {
  2922.       size &= ~SIGNED_EXTEND_BIT_MASK;
  2923.     }
  2924.   /* Is there a need for checking the size?  Size should contain the number of
  2925.      bytes to read.  */
  2926.   size = 1 << size;
  2927.   value = read_memory_unsigned_integer (address, size, byte_order);

  2928.   /* Check if it's an extend, signed or zero instruction.  */
  2929.   if (cris_get_opcode (*inst) < 4)
  2930.     {
  2931.       value = do_sign_or_zero_extend (value, inst);
  2932.     }
  2933.   return value;
  2934. }

  2935. /* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
  2936.    instructions.  The MOVE instruction is the move from source to register.  */

  2937. static void
  2938. handle_prefix_assign_mode_for_aritm_op (unsigned short inst,
  2939.                                         inst_env_type *inst_env)
  2940. {
  2941.   unsigned long operand2;
  2942.   unsigned long operand3;

  2943.   check_assign (inst, inst_env);
  2944.   if (cris_get_operand2 (inst) == REG_PC)
  2945.     {
  2946.       operand2 = inst_env->reg[REG_PC];

  2947.       /* Get the value of the third operand.  */
  2948.       operand3 = get_data_from_address (&inst, inst_env->prefix_value,
  2949.                                         inst_env->byte_order);

  2950.       /* Calculate the PC value after the instruction, i.e. where the
  2951.          breakpoint should be.  The order of the udw_operands is vital.  */
  2952.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
  2953.     }
  2954.   inst_env->slot_needed = 0;
  2955.   inst_env->prefix_found = 0;
  2956.   inst_env->xflag_found = 0;
  2957.   inst_env->disable_interrupt = 0;
  2958. }

  2959. /* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
  2960.    OR instructions.  Note that for this to work as expected, the calling
  2961.    function must have made sure that there is a prefix to this instruction.  */

  2962. static void
  2963. three_operand_add_sub_cmp_and_or_op (unsigned short inst,
  2964.                                      inst_env_type *inst_env)
  2965. {
  2966.   unsigned long operand2;
  2967.   unsigned long operand3;

  2968.   if (cris_get_operand1 (inst) == REG_PC)
  2969.     {
  2970.       /* The PC will be changed by the instruction.  */
  2971.       operand2 = inst_env->reg[cris_get_operand2 (inst)];

  2972.       /* Get the value of the third operand.  */
  2973.       operand3 = get_data_from_address (&inst, inst_env->prefix_value,
  2974.                                         inst_env->byte_order);

  2975.       /* Calculate the PC value after the instruction, i.e. where the
  2976.          breakpoint should be.  */
  2977.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
  2978.     }
  2979.   inst_env->slot_needed = 0;
  2980.   inst_env->prefix_found = 0;
  2981.   inst_env->xflag_found = 0;
  2982.   inst_env->disable_interrupt = 0;
  2983. }

  2984. /* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
  2985.    instructions.  The MOVE instruction is the move from source to register.  */

  2986. static void
  2987. handle_prefix_index_mode_for_aritm_op (unsigned short inst,
  2988.                                        inst_env_type *inst_env)
  2989. {
  2990.   if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
  2991.     {
  2992.       /* If the instruction is MOVE it's invalid.  If the instruction is ADD,
  2993.          SUB, AND or OR something weird is going on (if everything works these
  2994.          instructions should end up in the three operand version).  */
  2995.       inst_env->invalid = 1;
  2996.       return;
  2997.     }
  2998.   else
  2999.     {
  3000.       /* three_operand_add_sub_cmp_and_or does the same as we should do here
  3001.          so use it.  */
  3002.       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
  3003.     }
  3004.   inst_env->slot_needed = 0;
  3005.   inst_env->prefix_found = 0;
  3006.   inst_env->xflag_found = 0;
  3007.   inst_env->disable_interrupt = 0;
  3008. }

  3009. /* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
  3010.    CMP, AND OR and MOVE instruction.  The MOVE instruction is the move from
  3011.    source to register.  */

  3012. static void
  3013. handle_inc_and_index_mode_for_aritm_op (unsigned short inst,
  3014.                                         inst_env_type *inst_env)
  3015. {
  3016.   unsigned long operand1;
  3017.   unsigned long operand2;
  3018.   unsigned long operand3;
  3019.   int size;

  3020.   /* The instruction is either an indirect or autoincrement addressing mode.
  3021.      Check if the destination register is the PC.  */
  3022.   if (cris_get_operand2 (inst) == REG_PC)
  3023.     {
  3024.       /* Must be done here, get_data_from_address may change the size
  3025.          field.  */
  3026.       size = cris_get_size (inst);
  3027.       operand2 = inst_env->reg[REG_PC];

  3028.       /* Get the value of the third operand, i.e. the indirect operand.  */
  3029.       operand1 = inst_env->reg[cris_get_operand1 (inst)];
  3030.       operand3 = get_data_from_address (&inst, operand1, inst_env->byte_order);

  3031.       /* Calculate the PC value after the instruction, i.e. where the
  3032.          breakpoint should be.  The order of the udw_operands is vital.  */
  3033.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
  3034.     }
  3035.   /* If this is an autoincrement addressing mode, check if the increment
  3036.      changes the PC.  */
  3037.   if ((cris_get_operand1 (inst) == REG_PC)
  3038.       && (cris_get_mode (inst) == AUTOINC_MODE))
  3039.     {
  3040.       /* Get the size field.  */
  3041.       size = cris_get_size (inst);

  3042.       /* If it's an extend instruction we don't want the signed extend bit,
  3043.          because it influences the size.  */
  3044.       if (cris_get_opcode (inst) < 4)
  3045.         {
  3046.           size &= ~SIGNED_EXTEND_BIT_MASK;
  3047.         }
  3048.       process_autoincrement (size, inst, inst_env);
  3049.     }
  3050.   inst_env->slot_needed = 0;
  3051.   inst_env->prefix_found = 0;
  3052.   inst_env->xflag_found = 0;
  3053.   inst_env->disable_interrupt = 0;
  3054. }

  3055. /* Handles the two-operand addressing mode, all modes except register, for
  3056.    the ADD, SUB CMP, AND and OR instruction.  */

  3057. static void
  3058. none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
  3059.                                           inst_env_type *inst_env)
  3060. {
  3061.   if (inst_env->prefix_found)
  3062.     {
  3063.       if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
  3064.         {
  3065.           handle_prefix_index_mode_for_aritm_op (inst, inst_env);
  3066.         }
  3067.       else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
  3068.         {
  3069.           handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
  3070.         }
  3071.       else
  3072.         {
  3073.           /* The mode is invalid for a prefixed base instruction.  */
  3074.           inst_env->invalid = 1;
  3075.           return;
  3076.         }
  3077.     }
  3078.   else
  3079.     {
  3080.       handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
  3081.     }
  3082. }

  3083. /* Handles the quick addressing mode for the ADD and SUB instruction.  */

  3084. static void
  3085. quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
  3086. {
  3087.   unsigned long operand1;
  3088.   unsigned long operand2;

  3089.   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
  3090.      instruction and can't have a prefix.  */
  3091.   if (inst_env->prefix_found)
  3092.     {
  3093.       inst_env->invalid = 1;
  3094.       return;
  3095.     }

  3096.   /* Check if the instruction has PC as its target.  */
  3097.   if (cris_get_operand2 (inst) == REG_PC)
  3098.     {
  3099.       if (inst_env->slot_needed)
  3100.         {
  3101.           inst_env->invalid = 1;
  3102.           return;
  3103.         }
  3104.       operand1 = cris_get_quick_value (inst);
  3105.       operand2 = inst_env->reg[REG_PC];

  3106.       /* The size should now be dword.  */
  3107.       cris_set_size_to_dword (&inst);

  3108.       /* Calculate the PC value after the instruction, i.e. where the
  3109.          breakpoint should be.  */
  3110.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
  3111.     }
  3112.   inst_env->slot_needed = 0;
  3113.   inst_env->prefix_found = 0;
  3114.   inst_env->xflag_found = 0;
  3115.   inst_env->disable_interrupt = 0;
  3116. }

  3117. /* Handles the quick addressing mode for the CMP, AND and OR instruction.  */

  3118. static void
  3119. quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
  3120. {
  3121.   unsigned long operand1;
  3122.   unsigned long operand2;

  3123.   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
  3124.      instruction and can't have a prefix.  */
  3125.   if (inst_env->prefix_found)
  3126.     {
  3127.       inst_env->invalid = 1;
  3128.       return;
  3129.     }
  3130.   /* Check if the instruction has PC as its target.  */
  3131.   if (cris_get_operand2 (inst) == REG_PC)
  3132.     {
  3133.       if (inst_env->slot_needed)
  3134.         {
  3135.           inst_env->invalid = 1;
  3136.           return;
  3137.         }
  3138.       /* The instruction has the PC as its target register.  */
  3139.       operand1 = cris_get_quick_value (inst);
  3140.       operand2 = inst_env->reg[REG_PC];

  3141.       /* The quick value is signed, so check if we must do a signed extend.  */
  3142.       if (operand1 & SIGNED_QUICK_VALUE_MASK)
  3143.         {
  3144.           /* sign extend  */
  3145.           operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
  3146.         }
  3147.       /* The size should now be dword.  */
  3148.       cris_set_size_to_dword (&inst);

  3149.       /* Calculate the PC value after the instruction, i.e. where the
  3150.          breakpoint should be.  */
  3151.       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
  3152.     }
  3153.   inst_env->slot_needed = 0;
  3154.   inst_env->prefix_found = 0;
  3155.   inst_env->xflag_found = 0;
  3156.   inst_env->disable_interrupt = 0;
  3157. }

  3158. /* Translate op_type to a function and call it.  */

  3159. static void
  3160. cris_gdb_func (struct gdbarch *gdbarch, enum cris_op_type op_type,
  3161.                unsigned short inst, inst_env_type *inst_env)
  3162. {
  3163.   switch (op_type)
  3164.     {
  3165.     case cris_not_implemented_op:
  3166.       not_implemented_op (inst, inst_env);
  3167.       break;

  3168.     case cris_abs_op:
  3169.       abs_op (inst, inst_env);
  3170.       break;

  3171.     case cris_addi_op:
  3172.       addi_op (inst, inst_env);
  3173.       break;

  3174.     case cris_asr_op:
  3175.       asr_op (inst, inst_env);
  3176.       break;

  3177.     case cris_asrq_op:
  3178.       asrq_op (inst, inst_env);
  3179.       break;

  3180.     case cris_ax_ei_setf_op:
  3181.       ax_ei_setf_op (inst, inst_env);
  3182.       break;

  3183.     case cris_bdap_prefix:
  3184.       bdap_prefix (inst, inst_env);
  3185.       break;

  3186.     case cris_biap_prefix:
  3187.       biap_prefix (inst, inst_env);
  3188.       break;

  3189.     case cris_break_op:
  3190.       break_op (inst, inst_env);
  3191.       break;

  3192.     case cris_btst_nop_op:
  3193.       btst_nop_op (inst, inst_env);
  3194.       break;

  3195.     case cris_clearf_di_op:
  3196.       clearf_di_op (inst, inst_env);
  3197.       break;

  3198.     case cris_dip_prefix:
  3199.       dip_prefix (inst, inst_env);
  3200.       break;

  3201.     case cris_dstep_logshift_mstep_neg_not_op:
  3202.       dstep_logshift_mstep_neg_not_op (inst, inst_env);
  3203.       break;

  3204.     case cris_eight_bit_offset_branch_op:
  3205.       eight_bit_offset_branch_op (inst, inst_env);
  3206.       break;

  3207.     case cris_move_mem_to_reg_movem_op:
  3208.       move_mem_to_reg_movem_op (inst, inst_env);
  3209.       break;

  3210.     case cris_move_reg_to_mem_movem_op:
  3211.       move_reg_to_mem_movem_op (inst, inst_env);
  3212.       break;

  3213.     case cris_move_to_preg_op:
  3214.       move_to_preg_op (gdbarch, inst, inst_env);
  3215.       break;

  3216.     case cris_muls_op:
  3217.       muls_op (inst, inst_env);
  3218.       break;

  3219.     case cris_mulu_op:
  3220.       mulu_op (inst, inst_env);
  3221.       break;

  3222.     case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
  3223.       none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
  3224.       break;

  3225.     case cris_none_reg_mode_clear_test_op:
  3226.       none_reg_mode_clear_test_op (inst, inst_env);
  3227.       break;

  3228.     case cris_none_reg_mode_jump_op:
  3229.       none_reg_mode_jump_op (inst, inst_env);
  3230.       break;

  3231.     case cris_none_reg_mode_move_from_preg_op:
  3232.       none_reg_mode_move_from_preg_op (gdbarch, inst, inst_env);
  3233.       break;

  3234.     case cris_quick_mode_add_sub_op:
  3235.       quick_mode_add_sub_op (inst, inst_env);
  3236.       break;

  3237.     case cris_quick_mode_and_cmp_move_or_op:
  3238.       quick_mode_and_cmp_move_or_op (inst, inst_env);
  3239.       break;

  3240.     case cris_quick_mode_bdap_prefix:
  3241.       quick_mode_bdap_prefix (inst, inst_env);
  3242.       break;

  3243.     case cris_reg_mode_add_sub_cmp_and_or_move_op:
  3244.       reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
  3245.       break;

  3246.     case cris_reg_mode_clear_op:
  3247.       reg_mode_clear_op (inst, inst_env);
  3248.       break;

  3249.     case cris_reg_mode_jump_op:
  3250.       reg_mode_jump_op (inst, inst_env);
  3251.       break;

  3252.     case cris_reg_mode_move_from_preg_op:
  3253.       reg_mode_move_from_preg_op (inst, inst_env);
  3254.       break;

  3255.     case cris_reg_mode_test_op:
  3256.       reg_mode_test_op (inst, inst_env);
  3257.       break;

  3258.     case cris_scc_op:
  3259.       scc_op (inst, inst_env);
  3260.       break;

  3261.     case cris_sixteen_bit_offset_branch_op:
  3262.       sixteen_bit_offset_branch_op (inst, inst_env);
  3263.       break;

  3264.     case cris_three_operand_add_sub_cmp_and_or_op:
  3265.       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
  3266.       break;

  3267.     case cris_three_operand_bound_op:
  3268.       three_operand_bound_op (inst, inst_env);
  3269.       break;

  3270.     case cris_two_operand_bound_op:
  3271.       two_operand_bound_op (inst, inst_env);
  3272.       break;

  3273.     case cris_xor_op:
  3274.       xor_op (inst, inst_env);
  3275.       break;
  3276.     }
  3277. }

  3278. /* This wrapper is to avoid cris_get_assembler being called before
  3279.    exec_bfd has been set.  */

  3280. static int
  3281. cris_delayed_get_disassembler (bfd_vma addr, struct disassemble_info *info)
  3282. {
  3283.   int (*print_insn) (bfd_vma addr, struct disassemble_info *info);
  3284.   /* FIXME: cagney/2003-08-27: It should be possible to select a CRIS
  3285.      disassembler, even when there is no BFD.  Does something like
  3286.      "gdb; target remote; disassmeble *0x123" work?  */
  3287.   gdb_assert (exec_bfd != NULL);
  3288.   print_insn = cris_get_disassembler (exec_bfd);
  3289.   gdb_assert (print_insn != NULL);
  3290.   return print_insn (addr, info);
  3291. }

  3292. /* Originally from <asm/elf.h>.  */
  3293. typedef unsigned char cris_elf_greg_t[4];

  3294. /* Same as user_regs_struct struct in <asm/user.h>.  */
  3295. #define CRISV10_ELF_NGREG 35
  3296. typedef cris_elf_greg_t cris_elf_gregset_t[CRISV10_ELF_NGREG];

  3297. #define CRISV32_ELF_NGREG 32
  3298. typedef cris_elf_greg_t crisv32_elf_gregset_t[CRISV32_ELF_NGREG];

  3299. /* Unpack a cris_elf_gregset_t into GDB's register cache.  */

  3300. static void
  3301. cris_supply_gregset (struct regcache *regcache, cris_elf_gregset_t *gregsetp)
  3302. {
  3303.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  3304.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  3305.   int i;
  3306.   cris_elf_greg_t *regp = *gregsetp;

  3307.   /* The kernel dumps all 32 registers as unsigned longs, but supply_register
  3308.      knows about the actual size of each register so that's no problem.  */
  3309.   for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
  3310.     {
  3311.       regcache_raw_supply (regcache, i, (char *)&regp[i]);
  3312.     }

  3313.   if (tdep->cris_version == 32)
  3314.     {
  3315.       /* Needed to set pseudo-register PC for CRISv32.  */
  3316.       /* FIXME: If ERP is in a delay slot at this point then the PC will
  3317.          be wrong.  Issue a warning to alert the user.  */
  3318.       regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
  3319.                            (char *)&regp[ERP_REGNUM]);

  3320.       if (*(char *)&regp[ERP_REGNUM] & 0x1)
  3321.         fprintf_unfiltered (gdb_stderr, "Warning: PC in delay slot\n");
  3322.     }
  3323. }

  3324. /*  Use a local version of this function to get the correct types for
  3325.     regsets, until multi-arch core support is ready.  */

  3326. static void
  3327. fetch_core_registers (struct regcache *regcache,
  3328.                       char *core_reg_sect, unsigned core_reg_size,
  3329.                       int which, CORE_ADDR reg_addr)
  3330. {
  3331.   cris_elf_gregset_t gregset;

  3332.   switch (which)
  3333.     {
  3334.     case 0:
  3335.       if (core_reg_size != sizeof (cris_elf_gregset_t)
  3336.           && core_reg_size != sizeof (crisv32_elf_gregset_t))
  3337.         {
  3338.           warning (_("wrong size gregset struct in core file"));
  3339.         }
  3340.       else
  3341.         {
  3342.           memcpy (&gregset, core_reg_sect, sizeof (gregset));
  3343.           cris_supply_gregset (regcache, &gregset);
  3344.         }

  3345.     default:
  3346.       /* We've covered all the kinds of registers we know about here,
  3347.          so this must be something we wouldn't know what to do with
  3348.          anyway.  Just ignore it.  */
  3349.       break;
  3350.     }
  3351. }

  3352. static struct core_fns cris_elf_core_fns =
  3353. {
  3354.   bfd_target_elf_flavour,               /* core_flavour */
  3355.   default_check_format,                 /* check_format */
  3356.   default_core_sniffer,                 /* core_sniffer */
  3357.   fetch_core_registers,                 /* core_read_registers */
  3358.   NULL                                  /* next */
  3359. };

  3360. extern initialize_file_ftype _initialize_cris_tdep; /* -Wmissing-prototypes */

  3361. void
  3362. _initialize_cris_tdep (void)
  3363. {
  3364.   struct cmd_list_element *c;

  3365.   gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);

  3366.   /* CRIS-specific user-commands.  */
  3367.   add_setshow_zuinteger_cmd ("cris-version", class_support,
  3368.                              &usr_cmd_cris_version,
  3369.                              _("Set the current CRIS version."),
  3370.                              _("Show the current CRIS version."),
  3371.                              _("\
  3372. Set to 10 for CRISv10 or 32 for CRISv32 if autodetection fails.\n\
  3373. Defaults to 10. "),
  3374.                              set_cris_version,
  3375.                              NULL, /* FIXME: i18n: Current CRIS version
  3376.                                       is %s.  */
  3377.                              &setlist, &showlist);

  3378.   add_setshow_enum_cmd ("cris-mode", class_support,
  3379.                         cris_modes, &usr_cmd_cris_mode,
  3380.                         _("Set the current CRIS mode."),
  3381.                         _("Show the current CRIS mode."),
  3382.                         _("\
  3383. Set to CRIS_MODE_GURU when debugging in guru mode.\n\
  3384. Makes GDB use the NRP register instead of the ERP register in certain cases."),
  3385.                         set_cris_mode,
  3386.                         NULL, /* FIXME: i18n: Current CRIS version is %s.  */
  3387.                         &setlist, &showlist);

  3388.   add_setshow_boolean_cmd ("cris-dwarf2-cfi", class_support,
  3389.                            &usr_cmd_cris_dwarf2_cfi,
  3390.                            _("Set the usage of Dwarf-2 CFI for CRIS."),
  3391.                            _("Show the usage of Dwarf-2 CFI for CRIS."),
  3392.                            _("Set this to \"off\" if using gcc-cris < R59."),
  3393.                            set_cris_dwarf2_cfi,
  3394.                            NULL, /* FIXME: i18n: Usage of Dwarf-2 CFI
  3395.                                     for CRIS is %d.  */
  3396.                            &setlist, &showlist);

  3397.   deprecated_add_core_fns (&cris_elf_core_fns);
  3398. }

  3399. /* Prints out all target specific values.  */

  3400. static void
  3401. cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  3402. {
  3403.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  3404.   if (tdep != NULL)
  3405.     {
  3406.       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_version = %i\n",
  3407.                           tdep->cris_version);
  3408.       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
  3409.                           tdep->cris_mode);
  3410.       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_dwarf2_cfi = %i\n",
  3411.                           tdep->cris_dwarf2_cfi);
  3412.     }
  3413. }

  3414. static void
  3415. set_cris_version (char *ignore_args, int from_tty,
  3416.                   struct cmd_list_element *c)
  3417. {
  3418.   struct gdbarch_info info;

  3419.   usr_cmd_cris_version_valid = 1;

  3420.   /* Update the current architecture, if needed.  */
  3421.   gdbarch_info_init (&info);
  3422.   if (!gdbarch_update_p (info))
  3423.     internal_error (__FILE__, __LINE__,
  3424.                     _("cris_gdbarch_update: failed to update architecture."));
  3425. }

  3426. static void
  3427. set_cris_mode (char *ignore_args, int from_tty,
  3428.                struct cmd_list_element *c)
  3429. {
  3430.   struct gdbarch_info info;

  3431.   /* Update the current architecture, if needed.  */
  3432.   gdbarch_info_init (&info);
  3433.   if (!gdbarch_update_p (info))
  3434.     internal_error (__FILE__, __LINE__,
  3435.                     "cris_gdbarch_update: failed to update architecture.");
  3436. }

  3437. static void
  3438. set_cris_dwarf2_cfi (char *ignore_args, int from_tty,
  3439.                      struct cmd_list_element *c)
  3440. {
  3441.   struct gdbarch_info info;

  3442.   /* Update the current architecture, if needed.  */
  3443.   gdbarch_info_init (&info);
  3444.   if (!gdbarch_update_p (info))
  3445.     internal_error (__FILE__, __LINE__,
  3446.                     _("cris_gdbarch_update: failed to update architecture."));
  3447. }

  3448. static struct gdbarch *
  3449. cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  3450. {
  3451.   struct gdbarch *gdbarch;
  3452.   struct gdbarch_tdep *tdep;
  3453.   unsigned int cris_version;

  3454.   if (usr_cmd_cris_version_valid)
  3455.     {
  3456.       /* Trust the user's CRIS version setting.  */
  3457.       cris_version = usr_cmd_cris_version;
  3458.     }
  3459.   else if (info.abfd && bfd_get_mach (info.abfd) == bfd_mach_cris_v32)
  3460.     {
  3461.       cris_version = 32;
  3462.     }
  3463.   else
  3464.     {
  3465.       /* Assume it's CRIS version 10.  */
  3466.       cris_version = 10;
  3467.     }

  3468.   /* Make the current settings visible to the user.  */
  3469.   usr_cmd_cris_version = cris_version;

  3470.   /* Find a candidate among the list of pre-declared architectures.  */
  3471.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  3472.        arches != NULL;
  3473.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  3474.     {
  3475.       if ((gdbarch_tdep (arches->gdbarch)->cris_version
  3476.            == usr_cmd_cris_version)
  3477.           && (gdbarch_tdep (arches->gdbarch)->cris_mode
  3478.            == usr_cmd_cris_mode)
  3479.           && (gdbarch_tdep (arches->gdbarch)->cris_dwarf2_cfi
  3480.               == usr_cmd_cris_dwarf2_cfi))
  3481.         return arches->gdbarch;
  3482.     }

  3483.   /* No matching architecture was found.  Create a new one.  */
  3484.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  3485.   gdbarch = gdbarch_alloc (&info, tdep);

  3486.   tdep->cris_version = usr_cmd_cris_version;
  3487.   tdep->cris_mode = usr_cmd_cris_mode;
  3488.   tdep->cris_dwarf2_cfi = usr_cmd_cris_dwarf2_cfi;

  3489.   /* INIT shall ensure that the INFO.BYTE_ORDER is non-zero.  */
  3490.   switch (info.byte_order)
  3491.     {
  3492.     case BFD_ENDIAN_LITTLE:
  3493.       /* Ok.  */
  3494.       break;

  3495.     case BFD_ENDIAN_BIG:
  3496.       internal_error (__FILE__, __LINE__,
  3497.                       _("cris_gdbarch_init: big endian byte order in info"));
  3498.       break;

  3499.     default:
  3500.       internal_error (__FILE__, __LINE__,
  3501.                       _("cris_gdbarch_init: unknown byte order in info"));
  3502.     }

  3503.   set_gdbarch_return_value (gdbarch, cris_return_value);

  3504.   set_gdbarch_sp_regnum (gdbarch, 14);

  3505.   /* Length of ordinary registers used in push_word and a few other
  3506.      places.  register_size() is the real way to know how big a
  3507.      register is.  */

  3508.   set_gdbarch_double_bit (gdbarch, 64);
  3509.   /* The default definition of a long double is 2 * gdbarch_double_bit,
  3510.      which means we have to set this explicitly.  */
  3511.   set_gdbarch_long_double_bit (gdbarch, 64);

  3512.   /* The total amount of space needed to store (in an array called registers)
  3513.      GDB's copy of the machine's register state.  Note: We can not use
  3514.      cris_register_size at this point, since it relies on gdbarch
  3515.      being set.  */
  3516.   switch (tdep->cris_version)
  3517.     {
  3518.     case 0:
  3519.     case 1:
  3520.     case 2:
  3521.     case 3:
  3522.     case 8:
  3523.     case 9:
  3524.       /* Old versions; not supported.  */
  3525.       internal_error (__FILE__, __LINE__,
  3526.                       _("cris_gdbarch_init: unsupported CRIS version"));
  3527.       break;

  3528.     case 10:
  3529.     case 11:
  3530.       /* CRIS v10 and v11, a.k.a. ETRAX 100LX.  In addition to ETRAX 100,
  3531.          P7 (32 bits), and P15 (32 bits) have been implemented.  */
  3532.       set_gdbarch_pc_regnum (gdbarch, 15);
  3533.       set_gdbarch_register_type (gdbarch, cris_register_type);
  3534.       /* There are 32 registers (some of which may not be implemented).  */
  3535.       set_gdbarch_num_regs (gdbarch, 32);
  3536.       set_gdbarch_register_name (gdbarch, cris_register_name);
  3537.       set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
  3538.       set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);

  3539.       set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
  3540.       break;

  3541.     case 32:
  3542.       /* CRIS v32.  General registers R0 - R15 (32 bits), special registers
  3543.          P0 - P15 (32 bits) except P0, P1, P3 (8 bits) and P4 (16 bits)
  3544.          and pseudo-register PC (32 bits).  */
  3545.       set_gdbarch_pc_regnum (gdbarch, 32);
  3546.       set_gdbarch_register_type (gdbarch, crisv32_register_type);
  3547.       /* 32 registers + pseudo-register PC + 16 support registers.  */
  3548.       set_gdbarch_num_regs (gdbarch, 32 + 1 + 16);
  3549.       set_gdbarch_register_name (gdbarch, crisv32_register_name);

  3550.       set_gdbarch_cannot_store_register
  3551.         (gdbarch, crisv32_cannot_store_register);
  3552.       set_gdbarch_cannot_fetch_register
  3553.         (gdbarch, crisv32_cannot_fetch_register);

  3554.       set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  3555.       set_gdbarch_single_step_through_delay
  3556.         (gdbarch, crisv32_single_step_through_delay);

  3557.       break;

  3558.     default:
  3559.       internal_error (__FILE__, __LINE__,
  3560.                       _("cris_gdbarch_init: unknown CRIS version"));
  3561.     }

  3562.   /* Dummy frame functions (shared between CRISv10 and CRISv32 since they
  3563.      have the same ABI).  */
  3564.   set_gdbarch_push_dummy_code (gdbarch, cris_push_dummy_code);
  3565.   set_gdbarch_push_dummy_call (gdbarch, cris_push_dummy_call);
  3566.   set_gdbarch_frame_align (gdbarch, cris_frame_align);
  3567.   set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);

  3568.   /* The stack grows downward.  */
  3569.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  3570.   set_gdbarch_breakpoint_from_pc (gdbarch, cris_breakpoint_from_pc);

  3571.   set_gdbarch_unwind_pc (gdbarch, cris_unwind_pc);
  3572.   set_gdbarch_unwind_sp (gdbarch, cris_unwind_sp);
  3573.   set_gdbarch_dummy_id (gdbarch, cris_dummy_id);

  3574.   if (tdep->cris_dwarf2_cfi == 1)
  3575.     {
  3576.       /* Hook in the Dwarf-2 frame sniffer.  */
  3577.       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, cris_dwarf2_reg_to_regnum);
  3578.       dwarf2_frame_set_init_reg (gdbarch, cris_dwarf2_frame_init_reg);
  3579.       dwarf2_append_unwinders (gdbarch);
  3580.     }

  3581.   if (tdep->cris_mode != cris_mode_guru)
  3582.     {
  3583.       frame_unwind_append_unwinder (gdbarch, &cris_sigtramp_frame_unwind);
  3584.     }

  3585.   frame_unwind_append_unwinder (gdbarch, &cris_frame_unwind);
  3586.   frame_base_set_default (gdbarch, &cris_frame_base);

  3587.   /* Hook in ABI-specific overrides, if they have been registered.  */
  3588.   gdbarch_init_osabi (info, gdbarch);

  3589.   /* FIXME: cagney/2003-08-27: It should be possible to select a CRIS
  3590.      disassembler, even when there is no BFD.  Does something like
  3591.      "gdb; target remote; disassmeble *0x123" work?  */
  3592.   set_gdbarch_print_insn (gdbarch, cris_delayed_get_disassembler);

  3593.   return gdbarch;
  3594. }