GDB (API)
/home/stan/gdb/src/gdb/cris-tdep.c
Go to the documentation of this file.
00001 /* Target dependent code for CRIS, for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2001-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Axis Communications AB.
00006    Written by Hendrik Ruijter, Stefan Andersson, and Orjan Friberg.
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 #include "frame.h"
00025 #include "frame-unwind.h"
00026 #include "frame-base.h"
00027 #include "trad-frame.h"
00028 #include "dwarf2-frame.h"
00029 #include "symtab.h"
00030 #include "inferior.h"
00031 #include "gdbtypes.h"
00032 #include "gdbcore.h"
00033 #include "gdbcmd.h"
00034 #include "target.h"
00035 #include "value.h"
00036 #include "opcode/cris.h"
00037 #include "osabi.h"
00038 #include "arch-utils.h"
00039 #include "regcache.h"
00040 #include "gdb_assert.h"
00041 
00042 #include "objfiles.h"
00043 
00044 #include "solib.h"              /* Support for shared libraries.  */
00045 #include "solib-svr4.h"
00046 #include "gdb_string.h"
00047 #include "dis-asm.h"
00048 
00049 #include "cris-tdep.h"
00050 
00051 enum cris_num_regs
00052 {
00053   /* There are no floating point registers.  Used in gdbserver low-linux.c.  */
00054   NUM_FREGS = 0,
00055   
00056   /* There are 16 general registers.  */
00057   NUM_GENREGS = 16,
00058   
00059   /* There are 16 special registers.  */
00060   NUM_SPECREGS = 16,
00061 
00062   /* CRISv32 has a pseudo PC register, not noted here.  */
00063   
00064   /* CRISv32 has 16 support registers.  */
00065   NUM_SUPPREGS = 16
00066 };
00067 
00068 /* Register numbers of various important registers.
00069    CRIS_FP_REGNUM   Contains address of executing stack frame.
00070    STR_REGNUM  Contains the address of structure return values.
00071    RET_REGNUM  Contains the return value when shorter than or equal to 32 bits
00072    ARG1_REGNUM Contains the first parameter to a function.
00073    ARG2_REGNUM Contains the second parameter to a function.
00074    ARG3_REGNUM Contains the third parameter to a function.
00075    ARG4_REGNUM Contains the fourth parameter to a function.  Rest on stack.
00076    gdbarch_sp_regnum Contains address of top of stack.
00077    gdbarch_pc_regnum Contains address of next instruction.
00078    SRP_REGNUM  Subroutine return pointer register.
00079    BRP_REGNUM  Breakpoint return pointer register.  */
00080 
00081 enum cris_regnums
00082 {
00083   /* Enums with respect to the general registers, valid for all 
00084      CRIS versions.  The frame pointer is always in R8.  */
00085   CRIS_FP_REGNUM = 8,
00086   /* ABI related registers.  */
00087   STR_REGNUM  = 9,
00088   RET_REGNUM  = 10,
00089   ARG1_REGNUM = 10,
00090   ARG2_REGNUM = 11,
00091   ARG3_REGNUM = 12,
00092   ARG4_REGNUM = 13,
00093   
00094   /* Registers which happen to be common.  */
00095   VR_REGNUM   = 17,
00096   MOF_REGNUM  = 23,
00097   SRP_REGNUM  = 27,
00098 
00099   /* CRISv10 et al. specific registers.  */
00100   P0_REGNUM   = 16,
00101   P4_REGNUM   = 20,
00102   CCR_REGNUM  = 21,
00103   P8_REGNUM   = 24,
00104   IBR_REGNUM  = 25,
00105   IRP_REGNUM  = 26,
00106   BAR_REGNUM  = 28,
00107   DCCR_REGNUM = 29,
00108   BRP_REGNUM  = 30,
00109   USP_REGNUM  = 31,
00110 
00111   /* CRISv32 specific registers.  */
00112   ACR_REGNUM  = 15,
00113   BZ_REGNUM   = 16,
00114   PID_REGNUM  = 18,
00115   SRS_REGNUM  = 19,
00116   WZ_REGNUM   = 20,
00117   EXS_REGNUM  = 21,
00118   EDA_REGNUM  = 22,
00119   DZ_REGNUM   = 24,
00120   EBP_REGNUM  = 25,
00121   ERP_REGNUM  = 26,
00122   NRP_REGNUM  = 28,
00123   CCS_REGNUM  = 29,
00124   CRISV32USP_REGNUM  = 30, /* Shares name but not number with CRISv10.  */
00125   SPC_REGNUM  = 31,
00126   CRISV32PC_REGNUM   = 32, /* Shares name but not number with CRISv10.  */
00127 
00128   S0_REGNUM = 33,
00129   S1_REGNUM = 34,
00130   S2_REGNUM = 35,
00131   S3_REGNUM = 36,
00132   S4_REGNUM = 37,
00133   S5_REGNUM = 38,
00134   S6_REGNUM = 39,
00135   S7_REGNUM = 40,
00136   S8_REGNUM = 41,
00137   S9_REGNUM = 42,
00138   S10_REGNUM = 43,
00139   S11_REGNUM = 44,
00140   S12_REGNUM = 45,
00141   S13_REGNUM = 46,
00142   S14_REGNUM = 47,
00143   S15_REGNUM = 48,
00144 };
00145 
00146 extern const struct cris_spec_reg cris_spec_regs[];
00147 
00148 /* CRIS version, set via the user command 'set cris-version'.  Affects
00149    register names and sizes.  */
00150 static unsigned int usr_cmd_cris_version;
00151 
00152 /* Indicates whether to trust the above variable.  */
00153 static int usr_cmd_cris_version_valid = 0;
00154 
00155 static const char cris_mode_normal[] = "normal";
00156 static const char cris_mode_guru[] = "guru";
00157 static const char *const cris_modes[] = {
00158   cris_mode_normal,
00159   cris_mode_guru,
00160   0
00161 };
00162 
00163 /* CRIS mode, set via the user command 'set cris-mode'.  Affects
00164    type of break instruction among other things.  */
00165 static const char *usr_cmd_cris_mode = cris_mode_normal;
00166 
00167 /* Whether to make use of Dwarf-2 CFI (default on).  */
00168 static int usr_cmd_cris_dwarf2_cfi = 1;
00169 
00170 /* Sigtramp identification code copied from i386-linux-tdep.c.  */
00171 
00172 #define SIGTRAMP_INSN0    0x9c5f  /* movu.w 0xXX, $r9 */
00173 #define SIGTRAMP_OFFSET0  0
00174 #define SIGTRAMP_INSN1    0xe93d  /* break 13 */
00175 #define SIGTRAMP_OFFSET1  4
00176 
00177 static const unsigned short sigtramp_code[] =
00178 {
00179   SIGTRAMP_INSN0, 0x0077,  /* movu.w $0x77, $r9 */
00180   SIGTRAMP_INSN1           /* break 13 */
00181 };
00182 
00183 #define SIGTRAMP_LEN (sizeof sigtramp_code)
00184 
00185 /* Note: same length as normal sigtramp code.  */
00186 
00187 static const unsigned short rt_sigtramp_code[] =
00188 {
00189   SIGTRAMP_INSN0, 0x00ad,  /* movu.w $0xad, $r9 */
00190   SIGTRAMP_INSN1           /* break 13 */
00191 };
00192 
00193 /* If PC is in a sigtramp routine, return the address of the start of
00194    the routine.  Otherwise, return 0.  */
00195 
00196 static CORE_ADDR
00197 cris_sigtramp_start (struct frame_info *this_frame)
00198 {
00199   CORE_ADDR pc = get_frame_pc (this_frame);
00200   gdb_byte buf[SIGTRAMP_LEN];
00201 
00202   if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
00203     return 0;
00204 
00205   if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
00206     {
00207       if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
00208         return 0;
00209 
00210       pc -= SIGTRAMP_OFFSET1;
00211       if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
00212         return 0;
00213     }
00214 
00215   if (memcmp (buf, sigtramp_code, SIGTRAMP_LEN) != 0)
00216     return 0;
00217 
00218   return pc;
00219 }
00220 
00221 /* If PC is in a RT sigtramp routine, return the address of the start of
00222    the routine.  Otherwise, return 0.  */
00223 
00224 static CORE_ADDR
00225 cris_rt_sigtramp_start (struct frame_info *this_frame)
00226 {
00227   CORE_ADDR pc = get_frame_pc (this_frame);
00228   gdb_byte buf[SIGTRAMP_LEN];
00229 
00230   if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
00231     return 0;
00232 
00233   if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN0)
00234     {
00235       if (((buf[1] << 8) + buf[0]) != SIGTRAMP_INSN1)
00236         return 0;
00237 
00238       pc -= SIGTRAMP_OFFSET1;
00239       if (!safe_frame_unwind_memory (this_frame, pc, buf, SIGTRAMP_LEN))
00240         return 0;
00241     }
00242 
00243   if (memcmp (buf, rt_sigtramp_code, SIGTRAMP_LEN) != 0)
00244     return 0;
00245 
00246   return pc;
00247 }
00248 
00249 /* Assuming THIS_FRAME is a frame for a GNU/Linux sigtramp routine,
00250    return the address of the associated sigcontext structure.  */
00251 
00252 static CORE_ADDR
00253 cris_sigcontext_addr (struct frame_info *this_frame)
00254 {
00255   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00256   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00257   CORE_ADDR pc;
00258   CORE_ADDR sp;
00259   gdb_byte buf[4];
00260 
00261   get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
00262   sp = extract_unsigned_integer (buf, 4, byte_order);
00263 
00264   /* Look for normal sigtramp frame first.  */
00265   pc = cris_sigtramp_start (this_frame);
00266   if (pc)
00267     {
00268       /* struct signal_frame (arch/cris/kernel/signal.c) contains
00269          struct sigcontext as its first member, meaning the SP points to
00270          it already.  */
00271       return sp;
00272     }
00273 
00274   pc = cris_rt_sigtramp_start (this_frame);
00275   if (pc)
00276     {
00277       /* struct rt_signal_frame (arch/cris/kernel/signal.c) contains
00278          a struct ucontext, which in turn contains a struct sigcontext.
00279          Magic digging:
00280          4 + 4 + 128 to struct ucontext, then
00281          4 + 4 + 12 to struct sigcontext.  */
00282       return (sp + 156);
00283     }
00284 
00285   error (_("Couldn't recognize signal trampoline."));
00286   return 0;
00287 }
00288 
00289 struct cris_unwind_cache
00290 {
00291   /* The previous frame's inner most stack address.  Used as this
00292      frame ID's stack_addr.  */
00293   CORE_ADDR prev_sp;
00294   /* The frame's base, optionally used by the high-level debug info.  */
00295   CORE_ADDR base;
00296   int size;
00297   /* How far the SP and r8 (FP) have been offset from the start of
00298      the stack frame (as defined by the previous frame's stack
00299      pointer).  */
00300   LONGEST sp_offset;
00301   LONGEST r8_offset;
00302   int uses_frame;
00303 
00304   /* From old frame_extra_info struct.  */
00305   CORE_ADDR return_pc;
00306   int leaf_function;
00307 
00308   /* Table indicating the location of each and every register.  */
00309   struct trad_frame_saved_reg *saved_regs;
00310 };
00311 
00312 static struct cris_unwind_cache *
00313 cris_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
00314                                   void **this_cache)
00315 {
00316   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00317   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00318   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00319   struct cris_unwind_cache *info;
00320   CORE_ADDR addr;
00321   gdb_byte buf[4];
00322   int i;
00323 
00324   if ((*this_cache))
00325     return (*this_cache);
00326 
00327   info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
00328   (*this_cache) = info;
00329   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00330 
00331   /* Zero all fields.  */
00332   info->prev_sp = 0;
00333   info->base = 0;
00334   info->size = 0;
00335   info->sp_offset = 0;
00336   info->r8_offset = 0;
00337   info->uses_frame = 0;
00338   info->return_pc = 0;
00339   info->leaf_function = 0;
00340 
00341   get_frame_register (this_frame, gdbarch_sp_regnum (gdbarch), buf);
00342   info->base = extract_unsigned_integer (buf, 4, byte_order);
00343 
00344   addr = cris_sigcontext_addr (this_frame);
00345   
00346   /* Layout of the sigcontext struct:
00347      struct sigcontext {
00348         struct pt_regs regs;
00349         unsigned long oldmask;
00350         unsigned long usp;
00351      }; */
00352   
00353   if (tdep->cris_version == 10)
00354     {
00355       /* R0 to R13 are stored in reverse order at offset (2 * 4) in 
00356          struct pt_regs.  */
00357       for (i = 0; i <= 13; i++)
00358         info->saved_regs[i].addr = addr + ((15 - i) * 4);
00359 
00360       info->saved_regs[MOF_REGNUM].addr = addr + (16 * 4);
00361       info->saved_regs[DCCR_REGNUM].addr = addr + (17 * 4);
00362       info->saved_regs[SRP_REGNUM].addr = addr + (18 * 4);
00363       /* Note: IRP is off by 2 at this point.  There's no point in correcting
00364          it though since that will mean that the backtrace will show a PC 
00365          different from what is shown when stopped.  */
00366       info->saved_regs[IRP_REGNUM].addr = addr + (19 * 4);
00367       info->saved_regs[gdbarch_pc_regnum (gdbarch)]
00368         = info->saved_regs[IRP_REGNUM];
00369       info->saved_regs[gdbarch_sp_regnum (gdbarch)].addr = addr + (24 * 4);
00370     }
00371   else
00372     {
00373       /* CRISv32.  */
00374       /* R0 to R13 are stored in order at offset (1 * 4) in 
00375          struct pt_regs.  */
00376       for (i = 0; i <= 13; i++)
00377         info->saved_regs[i].addr = addr + ((i + 1) * 4);
00378 
00379       info->saved_regs[ACR_REGNUM].addr = addr + (15 * 4);
00380       info->saved_regs[SRS_REGNUM].addr = addr + (16 * 4);
00381       info->saved_regs[MOF_REGNUM].addr = addr + (17 * 4);
00382       info->saved_regs[SPC_REGNUM].addr = addr + (18 * 4);
00383       info->saved_regs[CCS_REGNUM].addr = addr + (19 * 4);
00384       info->saved_regs[SRP_REGNUM].addr = addr + (20 * 4);
00385       info->saved_regs[ERP_REGNUM].addr = addr + (21 * 4);
00386       info->saved_regs[EXS_REGNUM].addr = addr + (22 * 4);
00387       info->saved_regs[EDA_REGNUM].addr = addr + (23 * 4);
00388 
00389       /* FIXME: If ERP is in a delay slot at this point then the PC will
00390          be wrong at this point.  This problem manifests itself in the
00391          sigaltstack.exp test case, which occasionally generates FAILs when
00392          the signal is received while in a delay slot.
00393          
00394          This could be solved by a couple of read_memory_unsigned_integer and a
00395          trad_frame_set_value.  */
00396       info->saved_regs[gdbarch_pc_regnum (gdbarch)]
00397         = info->saved_regs[ERP_REGNUM];
00398 
00399       info->saved_regs[gdbarch_sp_regnum (gdbarch)].addr
00400         = addr + (25 * 4);
00401     }
00402   
00403   return info;
00404 }
00405 
00406 static void
00407 cris_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
00408                              struct frame_id *this_id)
00409 {
00410   struct cris_unwind_cache *cache =
00411     cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
00412   (*this_id) = frame_id_build (cache->base, get_frame_pc (this_frame));
00413 }
00414 
00415 /* Forward declaration.  */
00416 
00417 static struct value *cris_frame_prev_register (struct frame_info *this_frame,
00418                                                void **this_cache, int regnum);
00419 static struct value *
00420 cris_sigtramp_frame_prev_register (struct frame_info *this_frame,
00421                                    void **this_cache, int regnum)
00422 {
00423   /* Make sure we've initialized the cache.  */
00424   cris_sigtramp_frame_unwind_cache (this_frame, this_cache);
00425   return cris_frame_prev_register (this_frame, this_cache, regnum);
00426 }
00427 
00428 static int
00429 cris_sigtramp_frame_sniffer (const struct frame_unwind *self,
00430                              struct frame_info *this_frame,
00431                              void **this_cache)
00432 {
00433   if (cris_sigtramp_start (this_frame) 
00434       || cris_rt_sigtramp_start (this_frame))
00435     return 1;
00436 
00437   return 0;
00438 }
00439 
00440 static const struct frame_unwind cris_sigtramp_frame_unwind =
00441 {
00442   SIGTRAMP_FRAME,
00443   default_frame_unwind_stop_reason,
00444   cris_sigtramp_frame_this_id,
00445   cris_sigtramp_frame_prev_register,
00446   NULL,
00447   cris_sigtramp_frame_sniffer
00448 };
00449 
00450 static int
00451 crisv32_single_step_through_delay (struct gdbarch *gdbarch,
00452                                    struct frame_info *this_frame)
00453 {
00454   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00455   ULONGEST erp;
00456   int ret = 0;
00457 
00458   if (tdep->cris_mode == cris_mode_guru)
00459     erp = get_frame_register_unsigned (this_frame, NRP_REGNUM);
00460   else
00461     erp = get_frame_register_unsigned (this_frame, ERP_REGNUM);
00462 
00463   if (erp & 0x1)
00464     {
00465       /* In delay slot - check if there's a breakpoint at the preceding
00466          instruction.  */
00467       if (breakpoint_here_p (get_frame_address_space (this_frame), erp & ~0x1))
00468         ret = 1;
00469     }
00470   return ret;
00471 }
00472 
00473 /* The instruction environment needed to find single-step breakpoints.  */
00474 
00475 typedef 
00476 struct instruction_environment
00477 {
00478   unsigned long reg[NUM_GENREGS];
00479   unsigned long preg[NUM_SPECREGS];
00480   unsigned long branch_break_address;
00481   unsigned long delay_slot_pc;
00482   unsigned long prefix_value;
00483   int   branch_found;
00484   int   prefix_found;
00485   int   invalid;
00486   int   slot_needed;
00487   int   delay_slot_pc_active;
00488   int   xflag_found;
00489   int   disable_interrupt;
00490   int   byte_order;
00491 } inst_env_type;
00492 
00493 /* Machine-dependencies in CRIS for opcodes.  */
00494 
00495 /* Instruction sizes.  */
00496 enum cris_instruction_sizes
00497 {
00498   INST_BYTE_SIZE  = 0,
00499   INST_WORD_SIZE  = 1,
00500   INST_DWORD_SIZE = 2
00501 };
00502 
00503 /* Addressing modes.  */
00504 enum cris_addressing_modes
00505 {
00506   REGISTER_MODE = 1,
00507   INDIRECT_MODE = 2,
00508   AUTOINC_MODE  = 3
00509 };
00510 
00511 /* Prefix addressing modes.  */
00512 enum cris_prefix_addressing_modes
00513 {
00514   PREFIX_INDEX_MODE  = 2,
00515   PREFIX_ASSIGN_MODE = 3,
00516 
00517   /* Handle immediate byte offset addressing mode prefix format.  */
00518   PREFIX_OFFSET_MODE = 2
00519 };
00520 
00521 /* Masks for opcodes.  */
00522 enum cris_opcode_masks
00523 {
00524   BRANCH_SIGNED_SHORT_OFFSET_MASK = 0x1,
00525   SIGNED_EXTEND_BIT_MASK          = 0x2,
00526   SIGNED_BYTE_MASK                = 0x80,
00527   SIGNED_BYTE_EXTEND_MASK         = 0xFFFFFF00,
00528   SIGNED_WORD_MASK                = 0x8000,
00529   SIGNED_WORD_EXTEND_MASK         = 0xFFFF0000,
00530   SIGNED_DWORD_MASK               = 0x80000000,
00531   SIGNED_QUICK_VALUE_MASK         = 0x20,
00532   SIGNED_QUICK_VALUE_EXTEND_MASK  = 0xFFFFFFC0
00533 };
00534 
00535 /* Functions for opcodes.  The general form of the ETRAX 16-bit instruction:
00536    Bit 15 - 12   Operand2
00537        11 - 10   Mode
00538         9 -  6   Opcode
00539         5 -  4   Size
00540         3 -  0   Operand1  */
00541 
00542 static int 
00543 cris_get_operand2 (unsigned short insn)
00544 {
00545   return ((insn & 0xF000) >> 12);
00546 }
00547 
00548 static int
00549 cris_get_mode (unsigned short insn)
00550 {
00551   return ((insn & 0x0C00) >> 10);
00552 }
00553 
00554 static int
00555 cris_get_opcode (unsigned short insn)
00556 {
00557   return ((insn & 0x03C0) >> 6);
00558 }
00559 
00560 static int
00561 cris_get_size (unsigned short insn)
00562 {
00563   return ((insn & 0x0030) >> 4);
00564 }
00565 
00566 static int
00567 cris_get_operand1 (unsigned short insn)
00568 {
00569   return (insn & 0x000F);
00570 }
00571 
00572 /* Additional functions in order to handle opcodes.  */
00573 
00574 static int
00575 cris_get_quick_value (unsigned short insn)
00576 {
00577   return (insn & 0x003F);
00578 }
00579 
00580 static int
00581 cris_get_bdap_quick_offset (unsigned short insn)
00582 {
00583   return (insn & 0x00FF);
00584 }
00585 
00586 static int
00587 cris_get_branch_short_offset (unsigned short insn)
00588 {
00589   return (insn & 0x00FF);
00590 }
00591 
00592 static int
00593 cris_get_asr_shift_steps (unsigned long value)
00594 {
00595   return (value & 0x3F);
00596 }
00597 
00598 static int
00599 cris_get_clear_size (unsigned short insn)
00600 {
00601   return ((insn) & 0xC000);
00602 }
00603 
00604 static int
00605 cris_is_signed_extend_bit_on (unsigned short insn)
00606 {
00607   return (((insn) & 0x20) == 0x20);
00608 }
00609 
00610 static int
00611 cris_is_xflag_bit_on (unsigned short insn)
00612 {
00613   return (((insn) & 0x1000) == 0x1000);
00614 }
00615 
00616 static void
00617 cris_set_size_to_dword (unsigned short *insn)
00618 {
00619   *insn &= 0xFFCF; 
00620   *insn |= 0x20; 
00621 }
00622 
00623 static signed char
00624 cris_get_signed_offset (unsigned short insn)
00625 {
00626   return ((signed char) (insn & 0x00FF));
00627 }
00628 
00629 /* Calls an op function given the op-type, working on the insn and the
00630    inst_env.  */
00631 static void cris_gdb_func (struct gdbarch *, enum cris_op_type, unsigned short,
00632                            inst_env_type *);
00633 
00634 static struct gdbarch *cris_gdbarch_init (struct gdbarch_info,
00635                                           struct gdbarch_list *);
00636 
00637 static void cris_dump_tdep (struct gdbarch *, struct ui_file *);
00638 
00639 static void set_cris_version (char *ignore_args, int from_tty, 
00640                               struct cmd_list_element *c);
00641 
00642 static void set_cris_mode (char *ignore_args, int from_tty, 
00643                            struct cmd_list_element *c);
00644 
00645 static void set_cris_dwarf2_cfi (char *ignore_args, int from_tty, 
00646                                  struct cmd_list_element *c);
00647 
00648 static CORE_ADDR cris_scan_prologue (CORE_ADDR pc, 
00649                                      struct frame_info *this_frame,
00650                                      struct cris_unwind_cache *info);
00651 
00652 static CORE_ADDR crisv32_scan_prologue (CORE_ADDR pc, 
00653                                         struct frame_info *this_frame,
00654                                         struct cris_unwind_cache *info);
00655 
00656 static CORE_ADDR cris_unwind_pc (struct gdbarch *gdbarch, 
00657                                  struct frame_info *next_frame);
00658 
00659 static CORE_ADDR cris_unwind_sp (struct gdbarch *gdbarch, 
00660                                  struct frame_info *next_frame);
00661 
00662 /* When arguments must be pushed onto the stack, they go on in reverse
00663    order.  The below implements a FILO (stack) to do this.
00664    Copied from d10v-tdep.c.  */
00665 
00666 struct stack_item
00667 {
00668   int len;
00669   struct stack_item *prev;
00670   void *data;
00671 };
00672 
00673 static struct stack_item *
00674 push_stack_item (struct stack_item *prev, const gdb_byte *contents, int len)
00675 {
00676   struct stack_item *si;
00677   si = xmalloc (sizeof (struct stack_item));
00678   si->data = xmalloc (len);
00679   si->len = len;
00680   si->prev = prev;
00681   memcpy (si->data, contents, len);
00682   return si;
00683 }
00684 
00685 static struct stack_item *
00686 pop_stack_item (struct stack_item *si)
00687 {
00688   struct stack_item *dead = si;
00689   si = si->prev;
00690   xfree (dead->data);
00691   xfree (dead);
00692   return si;
00693 }
00694 
00695 /* Put here the code to store, into fi->saved_regs, the addresses of
00696    the saved registers of frame described by FRAME_INFO.  This
00697    includes special registers such as pc and fp saved in special ways
00698    in the stack frame.  sp is even more special: the address we return
00699    for it IS the sp for the next frame.  */
00700 
00701 static struct cris_unwind_cache *
00702 cris_frame_unwind_cache (struct frame_info *this_frame,
00703                          void **this_prologue_cache)
00704 {
00705   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00706   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00707   struct cris_unwind_cache *info;
00708 
00709   if ((*this_prologue_cache))
00710     return (*this_prologue_cache);
00711 
00712   info = FRAME_OBSTACK_ZALLOC (struct cris_unwind_cache);
00713   (*this_prologue_cache) = info;
00714   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00715 
00716   /* Zero all fields.  */
00717   info->prev_sp = 0;
00718   info->base = 0;
00719   info->size = 0;
00720   info->sp_offset = 0;
00721   info->r8_offset = 0;
00722   info->uses_frame = 0;
00723   info->return_pc = 0;
00724   info->leaf_function = 0;
00725 
00726   /* Prologue analysis does the rest...  */
00727   if (tdep->cris_version == 32)
00728     crisv32_scan_prologue (get_frame_func (this_frame), this_frame, info);
00729   else
00730     cris_scan_prologue (get_frame_func (this_frame), this_frame, info);
00731 
00732   return info;
00733 }
00734 
00735 /* Given a GDB frame, determine the address of the calling function's
00736    frame.  This will be used to create a new GDB frame struct.  */
00737 
00738 static void
00739 cris_frame_this_id (struct frame_info *this_frame,
00740                     void **this_prologue_cache,
00741                     struct frame_id *this_id)
00742 {
00743   struct cris_unwind_cache *info
00744     = cris_frame_unwind_cache (this_frame, this_prologue_cache);
00745   CORE_ADDR base;
00746   CORE_ADDR func;
00747   struct frame_id id;
00748 
00749   /* The FUNC is easy.  */
00750   func = get_frame_func (this_frame);
00751 
00752   /* Hopefully the prologue analysis either correctly determined the
00753      frame's base (which is the SP from the previous frame), or set
00754      that base to "NULL".  */
00755   base = info->prev_sp;
00756   if (base == 0)
00757     return;
00758 
00759   id = frame_id_build (base, func);
00760 
00761   (*this_id) = id;
00762 }
00763 
00764 static struct value *
00765 cris_frame_prev_register (struct frame_info *this_frame,
00766                           void **this_prologue_cache, int regnum)
00767 {
00768   struct cris_unwind_cache *info
00769     = cris_frame_unwind_cache (this_frame, this_prologue_cache);
00770   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
00771 }
00772 
00773 /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
00774    frame.  The frame ID's base needs to match the TOS value saved by
00775    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */
00776 
00777 static struct frame_id
00778 cris_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
00779 {
00780   CORE_ADDR sp;
00781   sp = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
00782   return frame_id_build (sp, get_frame_pc (this_frame));
00783 }
00784 
00785 static CORE_ADDR
00786 cris_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
00787 {
00788   /* Align to the size of an instruction (so that they can safely be
00789      pushed onto the stack).  */
00790   return sp & ~3;
00791 }
00792 
00793 static CORE_ADDR
00794 cris_push_dummy_code (struct gdbarch *gdbarch,
00795                       CORE_ADDR sp, CORE_ADDR funaddr,
00796                       struct value **args, int nargs,
00797                       struct type *value_type,
00798                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
00799                       struct regcache *regcache)
00800 {
00801   /* Allocate space sufficient for a breakpoint.  */
00802   sp = (sp - 4) & ~3;
00803   /* Store the address of that breakpoint */
00804   *bp_addr = sp;
00805   /* CRIS always starts the call at the callee's entry point.  */
00806   *real_pc = funaddr;
00807   return sp;
00808 }
00809 
00810 static CORE_ADDR
00811 cris_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
00812                       struct regcache *regcache, CORE_ADDR bp_addr,
00813                       int nargs, struct value **args, CORE_ADDR sp,
00814                       int struct_return, CORE_ADDR struct_addr)
00815 {
00816   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00817   int stack_offset;
00818   int argreg;
00819   int argnum;
00820 
00821   /* The function's arguments and memory allocated by gdb for the arguments to
00822      point at reside in separate areas on the stack.
00823      Both frame pointers grow toward higher addresses.  */
00824   CORE_ADDR fp_arg;
00825   CORE_ADDR fp_mem;
00826 
00827   struct stack_item *si = NULL;
00828 
00829   /* Push the return address.  */
00830   regcache_cooked_write_unsigned (regcache, SRP_REGNUM, bp_addr);
00831 
00832   /* Are we returning a value using a structure return or a normal value
00833      return?  struct_addr is the address of the reserved space for the return
00834      structure to be written on the stack.  */
00835   if (struct_return)
00836     {
00837       regcache_cooked_write_unsigned (regcache, STR_REGNUM, struct_addr);
00838     }
00839 
00840   /* Now load as many as possible of the first arguments into registers,
00841      and push the rest onto the stack.  */
00842   argreg = ARG1_REGNUM;
00843   stack_offset = 0;
00844 
00845   for (argnum = 0; argnum < nargs; argnum++)
00846     {
00847       int len;
00848       const gdb_byte *val;
00849       int reg_demand;
00850       int i;
00851       
00852       len = TYPE_LENGTH (value_type (args[argnum]));
00853       val = value_contents (args[argnum]);
00854       
00855       /* How may registers worth of storage do we need for this argument?  */
00856       reg_demand = (len / 4) + (len % 4 != 0 ? 1 : 0);
00857         
00858       if (len <= (2 * 4) && (argreg + reg_demand - 1 <= ARG4_REGNUM))
00859         {
00860           /* Data passed by value.  Fits in available register(s).  */
00861           for (i = 0; i < reg_demand; i++)
00862             {
00863               regcache_cooked_write (regcache, argreg, val);
00864               argreg++;
00865               val += 4;
00866             }
00867         }
00868       else if (len <= (2 * 4) && argreg <= ARG4_REGNUM)
00869         {
00870           /* Data passed by value. Does not fit in available register(s).
00871              Use the register(s) first, then the stack.  */
00872           for (i = 0; i < reg_demand; i++)
00873             {
00874               if (argreg <= ARG4_REGNUM)
00875                 {
00876                   regcache_cooked_write (regcache, argreg, val);
00877                   argreg++;
00878                   val += 4;
00879                 }
00880               else
00881                 {
00882                   /* Push item for later so that pushed arguments
00883                      come in the right order.  */
00884                   si = push_stack_item (si, val, 4);
00885                   val += 4;
00886                 }
00887             }
00888         }
00889       else if (len > (2 * 4))
00890         {
00891           /* Data passed by reference.  Push copy of data onto stack
00892              and pass pointer to this copy as argument.  */
00893           sp = (sp - len) & ~3;
00894           write_memory (sp, val, len);
00895 
00896           if (argreg <= ARG4_REGNUM)
00897             {
00898               regcache_cooked_write_unsigned (regcache, argreg, sp);
00899               argreg++;
00900             }
00901           else
00902             {
00903               gdb_byte buf[4];
00904               store_unsigned_integer (buf, 4, byte_order, sp);
00905               si = push_stack_item (si, buf, 4);
00906             }
00907         }
00908       else
00909         {
00910           /* Data passed by value.  No available registers.  Put it on
00911              the stack.  */
00912            si = push_stack_item (si, val, len);
00913         }
00914     }
00915 
00916   while (si)
00917     {
00918       /* fp_arg must be word-aligned (i.e., don't += len) to match
00919          the function prologue.  */
00920       sp = (sp - si->len) & ~3;
00921       write_memory (sp, si->data, si->len);
00922       si = pop_stack_item (si);
00923     }
00924 
00925   /* Finally, update the SP register.  */
00926   regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
00927 
00928   return sp;
00929 }
00930 
00931 static const struct frame_unwind cris_frame_unwind = 
00932 {
00933   NORMAL_FRAME,
00934   default_frame_unwind_stop_reason,
00935   cris_frame_this_id,
00936   cris_frame_prev_register,
00937   NULL,
00938   default_frame_sniffer
00939 };
00940 
00941 static CORE_ADDR
00942 cris_frame_base_address (struct frame_info *this_frame, void **this_cache)
00943 {
00944   struct cris_unwind_cache *info
00945     = cris_frame_unwind_cache (this_frame, this_cache);
00946   return info->base;
00947 }
00948 
00949 static const struct frame_base cris_frame_base = 
00950 {
00951   &cris_frame_unwind,
00952   cris_frame_base_address,
00953   cris_frame_base_address,
00954   cris_frame_base_address
00955 };
00956 
00957 /* Frames information. The definition of the struct frame_info is
00958 
00959    CORE_ADDR frame
00960    CORE_ADDR pc
00961    enum frame_type type;
00962    CORE_ADDR return_pc
00963    int leaf_function
00964 
00965    If the compilation option -fno-omit-frame-pointer is present the
00966    variable frame will be set to the content of R8 which is the frame
00967    pointer register.
00968 
00969    The variable pc contains the address where execution is performed
00970    in the present frame.  The innermost frame contains the current content
00971    of the register PC.  All other frames contain the content of the
00972    register PC in the next frame.
00973 
00974    The variable `type' indicates the frame's type: normal, SIGTRAMP
00975    (associated with a signal handler), dummy (associated with a dummy
00976    frame).
00977 
00978    The variable return_pc contains the address where execution should be
00979    resumed when the present frame has finished, the return address.
00980 
00981    The variable leaf_function is 1 if the return address is in the register
00982    SRP, and 0 if it is on the stack.
00983 
00984    Prologue instructions C-code.
00985    The prologue may consist of (-fno-omit-frame-pointer)
00986    1)                2)
00987    push   srp
00988    push   r8         push   r8
00989    move.d sp,r8      move.d sp,r8
00990    subq   X,sp       subq   X,sp
00991    movem  rY,[sp]    movem  rY,[sp]
00992    move.S rZ,[r8-U]  move.S rZ,[r8-U]
00993 
00994    where 1 is a non-terminal function, and 2 is a leaf-function.
00995 
00996    Note that this assumption is extremely brittle, and will break at the
00997    slightest change in GCC's prologue.
00998 
00999    If local variables are declared or register contents are saved on stack
01000    the subq-instruction will be present with X as the number of bytes
01001    needed for storage.  The reshuffle with respect to r8 may be performed
01002    with any size S (b, w, d) and any of the general registers Z={0..13}. 
01003    The offset U should be representable by a signed 8-bit value in all cases.
01004    Thus, the prefix word is assumed to be immediate byte offset mode followed
01005    by another word containing the instruction.
01006 
01007    Degenerate cases:
01008    3)
01009    push   r8
01010    move.d sp,r8
01011    move.d r8,sp
01012    pop    r8   
01013 
01014    Prologue instructions C++-code.
01015    Case 1) and 2) in the C-code may be followed by
01016 
01017    move.d r10,rS    ; this
01018    move.d r11,rT    ; P1
01019    move.d r12,rU    ; P2
01020    move.d r13,rV    ; P3
01021    move.S [r8+U],rZ ; P4
01022 
01023    if any of the call parameters are stored.  The host expects these 
01024    instructions to be executed in order to get the call parameters right.  */
01025 
01026 /* Examine the prologue of a function.  The variable ip is the address of 
01027    the first instruction of the prologue.  The variable limit is the address 
01028    of the first instruction after the prologue.  The variable fi contains the 
01029    information in struct frame_info.  The variable frameless_p controls whether
01030    the entire prologue is examined (0) or just enough instructions to 
01031    determine that it is a prologue (1).  */
01032 
01033 static CORE_ADDR 
01034 cris_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
01035                     struct cris_unwind_cache *info)
01036 {
01037   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01038   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01039 
01040   /* Present instruction.  */
01041   unsigned short insn;
01042 
01043   /* Next instruction, lookahead.  */
01044   unsigned short insn_next; 
01045   int regno;
01046 
01047   /* Is there a push fp?  */
01048   int have_fp; 
01049 
01050   /* Number of byte on stack used for local variables and movem.  */
01051   int val; 
01052 
01053   /* Highest register number in a movem.  */
01054   int regsave;
01055 
01056   /* move.d r<source_register>,rS */
01057   short source_register; 
01058 
01059   /* Scan limit.  */
01060   int limit;
01061 
01062   /* This frame is with respect to a leaf until a push srp is found.  */
01063   if (info)
01064     {
01065       info->leaf_function = 1;
01066     }
01067 
01068   /* Assume nothing on stack.  */
01069   val = 0;
01070   regsave = -1;
01071 
01072   /* If we were called without a this_frame, that means we were called
01073      from cris_skip_prologue which already tried to find the end of the
01074      prologue through the symbol information.  64 instructions past current
01075      pc is arbitrarily chosen, but at least it means we'll stop eventually.  */
01076   limit = this_frame ? get_frame_pc (this_frame) : pc + 64;
01077 
01078   /* Find the prologue instructions.  */
01079   while (pc > 0 && pc < limit)
01080     {
01081       insn = read_memory_unsigned_integer (pc, 2, byte_order);
01082       pc += 2;
01083       if (insn == 0xE1FC)
01084         {
01085           /* push <reg> 32 bit instruction.  */
01086           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
01087           pc += 2;
01088           regno = cris_get_operand2 (insn_next);
01089           if (info)
01090             {
01091               info->sp_offset += 4;
01092             }
01093           /* This check, meant to recognize srp, used to be regno == 
01094              (SRP_REGNUM - NUM_GENREGS), but that covers r11 also.  */
01095           if (insn_next == 0xBE7E)
01096             {
01097               if (info)
01098                 {
01099                   info->leaf_function = 0;
01100                 }
01101             }
01102           else if (insn_next == 0x8FEE)
01103             {
01104               /* push $r8 */
01105               if (info)
01106                 {
01107                   info->r8_offset = info->sp_offset;
01108                 }
01109             }
01110         }
01111       else if (insn == 0x866E)
01112         {
01113           /* move.d sp,r8 */
01114           if (info)
01115             {
01116               info->uses_frame = 1;
01117             }
01118           continue;
01119         }
01120       else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
01121                && cris_get_mode (insn) == 0x0000
01122                && cris_get_opcode (insn) == 0x000A)
01123         {
01124           /* subq <val>,sp */
01125           if (info)
01126             {
01127               info->sp_offset += cris_get_quick_value (insn);
01128             }
01129         }
01130       else if (cris_get_mode (insn) == 0x0002 
01131                && cris_get_opcode (insn) == 0x000F
01132                && cris_get_size (insn) == 0x0003
01133                && cris_get_operand1 (insn) == gdbarch_sp_regnum (gdbarch))
01134         {
01135           /* movem r<regsave>,[sp] */
01136           regsave = cris_get_operand2 (insn);
01137         }
01138       else if (cris_get_operand2 (insn) == gdbarch_sp_regnum (gdbarch)
01139                && ((insn & 0x0F00) >> 8) == 0x0001
01140                && (cris_get_signed_offset (insn) < 0))
01141         {
01142           /* Immediate byte offset addressing prefix word with sp as base 
01143              register.  Used for CRIS v8 i.e. ETRAX 100 and newer if <val> 
01144              is between 64 and 128. 
01145              movem r<regsave>,[sp=sp-<val>] */
01146           if (info)
01147             {
01148               info->sp_offset += -cris_get_signed_offset (insn);
01149             }
01150           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
01151           pc += 2;
01152           if (cris_get_mode (insn_next) == PREFIX_ASSIGN_MODE
01153               && cris_get_opcode (insn_next) == 0x000F
01154               && cris_get_size (insn_next) == 0x0003
01155               && cris_get_operand1 (insn_next) == gdbarch_sp_regnum
01156                                                   (gdbarch))
01157             {
01158               regsave = cris_get_operand2 (insn_next);
01159             }
01160           else
01161             {
01162               /* The prologue ended before the limit was reached.  */
01163               pc -= 4;
01164               break;
01165             }
01166         }
01167       else if (cris_get_mode (insn) == 0x0001
01168                && cris_get_opcode (insn) == 0x0009
01169                && cris_get_size (insn) == 0x0002)
01170         {
01171           /* move.d r<10..13>,r<0..15> */
01172           source_register = cris_get_operand1 (insn);
01173 
01174           /* FIXME?  In the glibc solibs, the prologue might contain something
01175              like (this example taken from relocate_doit):
01176              move.d $pc,$r0
01177              sub.d 0xfffef426,$r0
01178              which isn't covered by the source_register check below.  Question
01179              is whether to add a check for this combo, or make better use of
01180              the limit variable instead.  */
01181           if (source_register < ARG1_REGNUM || source_register > ARG4_REGNUM)
01182             {
01183               /* The prologue ended before the limit was reached.  */
01184               pc -= 2;
01185               break;
01186             }
01187         }
01188       else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM 
01189                /* The size is a fixed-size.  */
01190                && ((insn & 0x0F00) >> 8) == 0x0001 
01191                /* A negative offset.  */
01192                && (cris_get_signed_offset (insn) < 0))  
01193         {
01194           /* move.S rZ,[r8-U] (?) */
01195           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
01196           pc += 2;
01197           regno = cris_get_operand2 (insn_next);
01198           if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
01199               && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
01200               && cris_get_opcode (insn_next) == 0x000F)
01201             {
01202               /* move.S rZ,[r8-U] */
01203               continue;
01204             }
01205           else
01206             {
01207               /* The prologue ended before the limit was reached.  */
01208               pc -= 4;
01209               break;
01210             }
01211         }
01212       else if (cris_get_operand2 (insn) == CRIS_FP_REGNUM 
01213                /* The size is a fixed-size.  */
01214                && ((insn & 0x0F00) >> 8) == 0x0001 
01215                /* A positive offset.  */
01216                && (cris_get_signed_offset (insn) > 0))  
01217         {
01218           /* move.S [r8+U],rZ (?) */
01219           insn_next = read_memory_unsigned_integer (pc, 2, byte_order);
01220           pc += 2;
01221           regno = cris_get_operand2 (insn_next);
01222           if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
01223               && cris_get_mode (insn_next) == PREFIX_OFFSET_MODE
01224               && cris_get_opcode (insn_next) == 0x0009
01225               && cris_get_operand1 (insn_next) == regno)
01226             {
01227               /* move.S [r8+U],rZ */
01228               continue;
01229             }
01230           else
01231             {
01232               /* The prologue ended before the limit was reached.  */
01233               pc -= 4;
01234               break;
01235             }
01236         }
01237       else
01238         {
01239           /* The prologue ended before the limit was reached.  */
01240           pc -= 2;
01241           break;
01242         }
01243     }
01244 
01245   /* We only want to know the end of the prologue when this_frame and info
01246      are NULL (called from cris_skip_prologue i.e.).  */
01247   if (this_frame == NULL && info == NULL)
01248     {
01249       return pc;
01250     }
01251 
01252   info->size = info->sp_offset;
01253 
01254   /* Compute the previous frame's stack pointer (which is also the
01255      frame's ID's stack address), and this frame's base pointer.  */
01256   if (info->uses_frame)
01257     {
01258       ULONGEST this_base;
01259       /* The SP was moved to the FP.  This indicates that a new frame
01260          was created.  Get THIS frame's FP value by unwinding it from
01261          the next frame.  */
01262       this_base = get_frame_register_unsigned (this_frame, CRIS_FP_REGNUM);
01263       info->base = this_base;
01264       info->saved_regs[CRIS_FP_REGNUM].addr = info->base;
01265   
01266       /* The FP points at the last saved register.  Adjust the FP back
01267          to before the first saved register giving the SP.  */
01268       info->prev_sp = info->base + info->r8_offset;
01269     }
01270   else
01271     {
01272       ULONGEST this_base;      
01273       /* Assume that the FP is this frame's SP but with that pushed
01274          stack space added back.  */
01275       this_base = get_frame_register_unsigned (this_frame,
01276                                                gdbarch_sp_regnum (gdbarch));
01277       info->base = this_base;
01278       info->prev_sp = info->base + info->size;
01279     }
01280       
01281   /* Calculate the addresses for the saved registers on the stack.  */
01282   /* FIXME: The address calculation should really be done on the fly while
01283      we're analyzing the prologue (we only hold one regsave value as it is 
01284      now).  */
01285   val = info->sp_offset;
01286 
01287   for (regno = regsave; regno >= 0; regno--)
01288     {
01289       info->saved_regs[regno].addr = info->base + info->r8_offset - val;
01290       val -= 4;
01291     }
01292 
01293   /* The previous frame's SP needed to be computed.  Save the computed
01294      value.  */
01295   trad_frame_set_value (info->saved_regs,
01296                         gdbarch_sp_regnum (gdbarch), info->prev_sp);
01297 
01298   if (!info->leaf_function)
01299     {
01300       /* SRP saved on the stack.  But where?  */
01301       if (info->r8_offset == 0)
01302         {
01303           /* R8 not pushed yet.  */
01304           info->saved_regs[SRP_REGNUM].addr = info->base;
01305         }
01306       else
01307         {
01308           /* R8 pushed, but SP may or may not be moved to R8 yet.  */
01309           info->saved_regs[SRP_REGNUM].addr = info->base + 4;
01310         }
01311     }
01312 
01313   /* The PC is found in SRP (the actual register or located on the stack).  */
01314   info->saved_regs[gdbarch_pc_regnum (gdbarch)]
01315     = info->saved_regs[SRP_REGNUM];
01316 
01317   return pc;
01318 }
01319 
01320 static CORE_ADDR 
01321 crisv32_scan_prologue (CORE_ADDR pc, struct frame_info *this_frame,
01322                     struct cris_unwind_cache *info)
01323 {
01324   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01325   ULONGEST this_base;
01326 
01327   /* Unlike the CRISv10 prologue scanner (cris_scan_prologue), this is not
01328      meant to be a full-fledged prologue scanner.  It is only needed for 
01329      the cases where we end up in code always lacking DWARF-2 CFI, notably:
01330 
01331        * PLT stubs (library calls)
01332        * call dummys
01333        * signal trampolines
01334 
01335      For those cases, it is assumed that there is no actual prologue; that 
01336      the stack pointer is not adjusted, and (as a consequence) the return
01337      address is not pushed onto the stack.  */
01338 
01339   /* We only want to know the end of the prologue when this_frame and info
01340      are NULL (called from cris_skip_prologue i.e.).  */
01341   if (this_frame == NULL && info == NULL)
01342     {
01343       return pc;
01344     }
01345 
01346   /* The SP is assumed to be unaltered.  */
01347   this_base = get_frame_register_unsigned (this_frame,
01348                                            gdbarch_sp_regnum (gdbarch));
01349   info->base = this_base;
01350   info->prev_sp = this_base;
01351       
01352   /* The PC is assumed to be found in SRP.  */
01353   info->saved_regs[gdbarch_pc_regnum (gdbarch)]
01354     = info->saved_regs[SRP_REGNUM];
01355 
01356   return pc;
01357 }
01358 
01359 /* Advance pc beyond any function entry prologue instructions at pc
01360    to reach some "real" code.  */
01361 
01362 /* Given a PC value corresponding to the start of a function, return the PC
01363    of the first instruction after the function prologue.  */
01364 
01365 static CORE_ADDR
01366 cris_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
01367 {
01368   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01369   CORE_ADDR func_addr, func_end;
01370   struct symtab_and_line sal;
01371   CORE_ADDR pc_after_prologue;
01372   
01373   /* If we have line debugging information, then the end of the prologue
01374      should the first assembly instruction of the first source line.  */
01375   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
01376     {
01377       sal = find_pc_line (func_addr, 0);
01378       if (sal.end > 0 && sal.end < func_end)
01379         return sal.end;
01380     }
01381 
01382   if (tdep->cris_version == 32)
01383     pc_after_prologue = crisv32_scan_prologue (pc, NULL, NULL);
01384   else
01385     pc_after_prologue = cris_scan_prologue (pc, NULL, NULL);
01386 
01387   return pc_after_prologue;
01388 }
01389 
01390 static CORE_ADDR
01391 cris_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01392 {
01393   ULONGEST pc;
01394   pc = frame_unwind_register_unsigned (next_frame,
01395                                        gdbarch_pc_regnum (gdbarch));
01396   return pc;
01397 }
01398 
01399 static CORE_ADDR
01400 cris_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
01401 {
01402   ULONGEST sp;
01403   sp = frame_unwind_register_unsigned (next_frame,
01404                                        gdbarch_sp_regnum (gdbarch));
01405   return sp;
01406 }
01407 
01408 /* Use the program counter to determine the contents and size of a breakpoint
01409    instruction.  It returns a pointer to a string of bytes that encode a
01410    breakpoint instruction, stores the length of the string to *lenptr, and
01411    adjusts pcptr (if necessary) to point to the actual memory location where
01412    the breakpoint should be inserted.  */
01413 
01414 static const unsigned char *
01415 cris_breakpoint_from_pc (struct gdbarch *gdbarch,
01416                          CORE_ADDR *pcptr, int *lenptr)
01417 {
01418   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01419   static unsigned char break8_insn[] = {0x38, 0xe9};
01420   static unsigned char break15_insn[] = {0x3f, 0xe9};
01421   *lenptr = 2;
01422 
01423   if (tdep->cris_mode == cris_mode_guru)
01424     return break15_insn;
01425   else
01426     return break8_insn;
01427 }
01428 
01429 /* Returns 1 if spec_reg is applicable to the current gdbarch's CRIS version,
01430    0 otherwise.  */
01431 
01432 static int
01433 cris_spec_reg_applicable (struct gdbarch *gdbarch,
01434                           struct cris_spec_reg spec_reg)
01435 {
01436   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01437   unsigned int version = tdep->cris_version;
01438   
01439   switch (spec_reg.applicable_version)
01440     {
01441     case cris_ver_version_all:
01442       return 1;
01443     case cris_ver_warning:
01444       /* Indeterminate/obsolete.  */
01445       return 0;
01446     case cris_ver_v0_3:
01447       return (version >= 0 && version <= 3);
01448     case cris_ver_v3p:
01449       return (version >= 3);
01450     case cris_ver_v8:
01451       return (version == 8 || version == 9);
01452     case cris_ver_v8p:
01453       return (version >= 8);
01454     case cris_ver_v0_10:
01455       return (version >= 0 && version <= 10);
01456     case cris_ver_v3_10:
01457       return (version >= 3 && version <= 10);
01458     case cris_ver_v8_10:
01459       return (version >= 8 && version <= 10);
01460     case cris_ver_v10:
01461       return (version == 10);
01462     case cris_ver_v10p:
01463       return (version >= 10);
01464     case cris_ver_v32p:
01465       return (version >= 32);
01466     default:
01467       /* Invalid cris version.  */
01468       return 0;
01469     }
01470 }
01471 
01472 /* Returns the register size in unit byte.  Returns 0 for an unimplemented
01473    register, -1 for an invalid register.  */
01474 
01475 static int
01476 cris_register_size (struct gdbarch *gdbarch, int regno)
01477 {
01478   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01479   int i;
01480   int spec_regno;
01481   
01482   if (regno >= 0 && regno < NUM_GENREGS)
01483     {
01484       /* General registers (R0 - R15) are 32 bits.  */
01485       return 4;
01486     }
01487   else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
01488     {
01489       /* Special register (R16 - R31).  cris_spec_regs is zero-based. 
01490          Adjust regno accordingly.  */
01491       spec_regno = regno - NUM_GENREGS;
01492       
01493       for (i = 0; cris_spec_regs[i].name != NULL; i++)
01494         {
01495           if (cris_spec_regs[i].number == spec_regno 
01496               && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
01497             /* Go with the first applicable register.  */
01498             return cris_spec_regs[i].reg_size;
01499         }
01500       /* Special register not applicable to this CRIS version.  */
01501       return 0;
01502     }
01503   else if (regno >= gdbarch_pc_regnum (gdbarch)
01504            && regno < gdbarch_num_regs (gdbarch))
01505     {
01506       /* This will apply to CRISv32 only where there are additional registers
01507          after the special registers (pseudo PC and support registers).  */
01508       return 4;
01509     }
01510 
01511   
01512   return -1;
01513 }
01514 
01515 /* Nonzero if regno should not be fetched from the target.  This is the case
01516    for unimplemented (size 0) and non-existant registers.  */
01517 
01518 static int
01519 cris_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
01520 {
01521   return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
01522           || (cris_register_size (gdbarch, regno) == 0));
01523 }
01524 
01525 /* Nonzero if regno should not be written to the target, for various 
01526    reasons.  */
01527 
01528 static int
01529 cris_cannot_store_register (struct gdbarch *gdbarch, int regno)
01530 {
01531   /* There are three kinds of registers we refuse to write to.
01532      1. Those that not implemented.
01533      2. Those that are read-only (depends on the processor mode).
01534      3. Those registers to which a write has no effect.  */
01535 
01536   if (regno < 0
01537       || regno >= gdbarch_num_regs (gdbarch)
01538       || cris_register_size (gdbarch, regno) == 0)
01539     /* Not implemented.  */
01540     return 1;
01541 
01542   else if  (regno == VR_REGNUM)
01543     /* Read-only.  */
01544     return 1;
01545 
01546   else if  (regno == P0_REGNUM || regno == P4_REGNUM || regno == P8_REGNUM)
01547     /* Writing has no effect.  */
01548     return 1;
01549 
01550   /* IBR, BAR, BRP and IRP are read-only in user mode.  Let the debug
01551      agent decide whether they are writable.  */
01552   
01553   return 0;
01554 }
01555 
01556 /* Nonzero if regno should not be fetched from the target.  This is the case
01557    for unimplemented (size 0) and non-existant registers.  */
01558 
01559 static int
01560 crisv32_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
01561 {
01562   return ((regno < 0 || regno >= gdbarch_num_regs (gdbarch))
01563           || (cris_register_size (gdbarch, regno) == 0));
01564 }
01565 
01566 /* Nonzero if regno should not be written to the target, for various 
01567    reasons.  */
01568 
01569 static int
01570 crisv32_cannot_store_register (struct gdbarch *gdbarch, int regno)
01571 {
01572   /* There are three kinds of registers we refuse to write to.
01573      1. Those that not implemented.
01574      2. Those that are read-only (depends on the processor mode).
01575      3. Those registers to which a write has no effect.  */
01576 
01577   if (regno < 0
01578       || regno >= gdbarch_num_regs (gdbarch)
01579       || cris_register_size (gdbarch, regno) == 0)
01580     /* Not implemented.  */
01581     return 1;
01582 
01583   else if  (regno == VR_REGNUM)
01584     /* Read-only.  */
01585     return 1;
01586 
01587   else if  (regno == BZ_REGNUM || regno == WZ_REGNUM || regno == DZ_REGNUM)
01588     /* Writing has no effect.  */
01589     return 1;
01590 
01591   /* Many special registers are read-only in user mode.  Let the debug
01592      agent decide whether they are writable.  */
01593   
01594   return 0;
01595 }
01596 
01597 /* Return the GDB type (defined in gdbtypes.c) for the "standard" data type
01598    of data in register regno.  */
01599 
01600 static struct type *
01601 cris_register_type (struct gdbarch *gdbarch, int regno)
01602 {
01603   if (regno == gdbarch_pc_regnum (gdbarch))
01604     return builtin_type (gdbarch)->builtin_func_ptr;
01605   else if (regno == gdbarch_sp_regnum (gdbarch)
01606            || regno == CRIS_FP_REGNUM)
01607     return builtin_type (gdbarch)->builtin_data_ptr;
01608   else if ((regno >= 0 && regno < gdbarch_sp_regnum (gdbarch))
01609            || (regno >= MOF_REGNUM && regno <= USP_REGNUM))
01610     /* Note: R8 taken care of previous clause.  */
01611     return builtin_type (gdbarch)->builtin_uint32;
01612   else if (regno >= P4_REGNUM && regno <= CCR_REGNUM)
01613       return builtin_type (gdbarch)->builtin_uint16;
01614   else if (regno >= P0_REGNUM && regno <= VR_REGNUM)
01615       return builtin_type (gdbarch)->builtin_uint8;
01616   else
01617       /* Invalid (unimplemented) register.  */
01618       return builtin_type (gdbarch)->builtin_int0;
01619 }
01620 
01621 static struct type *
01622 crisv32_register_type (struct gdbarch *gdbarch, int regno)
01623 {
01624   if (regno == gdbarch_pc_regnum (gdbarch))
01625     return builtin_type (gdbarch)->builtin_func_ptr;
01626   else if (regno == gdbarch_sp_regnum (gdbarch)
01627            || regno == CRIS_FP_REGNUM)
01628     return builtin_type (gdbarch)->builtin_data_ptr;
01629   else if ((regno >= 0 && regno <= ACR_REGNUM)
01630            || (regno >= EXS_REGNUM && regno <= SPC_REGNUM)
01631            || (regno == PID_REGNUM)
01632            || (regno >= S0_REGNUM && regno <= S15_REGNUM))
01633     /* Note: R8 and SP taken care of by previous clause.  */
01634     return builtin_type (gdbarch)->builtin_uint32;
01635   else if (regno == WZ_REGNUM)
01636       return builtin_type (gdbarch)->builtin_uint16;
01637   else if (regno == BZ_REGNUM || regno == VR_REGNUM || regno == SRS_REGNUM)
01638       return builtin_type (gdbarch)->builtin_uint8;
01639   else
01640     {
01641       /* Invalid (unimplemented) register.  Should not happen as there are
01642          no unimplemented CRISv32 registers.  */
01643       warning (_("crisv32_register_type: unknown regno %d"), regno);
01644       return builtin_type (gdbarch)->builtin_int0;
01645     }
01646 }
01647 
01648 /* Stores a function return value of type type, where valbuf is the address 
01649    of the value to be stored.  */
01650 
01651 /* In the CRIS ABI, R10 and R11 are used to store return values.  */
01652 
01653 static void
01654 cris_store_return_value (struct type *type, struct regcache *regcache,
01655                          const gdb_byte *valbuf)
01656 {
01657   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01658   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01659   ULONGEST val;
01660   int len = TYPE_LENGTH (type);
01661   
01662   if (len <= 4)
01663     {
01664       /* Put the return value in R10.  */
01665       val = extract_unsigned_integer (valbuf, len, byte_order);
01666       regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
01667     }
01668   else if (len <= 8)
01669     {
01670       /* Put the return value in R10 and R11.  */
01671       val = extract_unsigned_integer (valbuf, 4, byte_order);
01672       regcache_cooked_write_unsigned (regcache, ARG1_REGNUM, val);
01673       val = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
01674       regcache_cooked_write_unsigned (regcache, ARG2_REGNUM, val);
01675     }
01676   else
01677     error (_("cris_store_return_value: type length too large."));
01678 }
01679 
01680 /* Return the name of register regno as a string.  Return NULL for an
01681    invalid or unimplemented register.  */
01682 
01683 static const char *
01684 cris_special_register_name (struct gdbarch *gdbarch, int regno)
01685 {
01686   int spec_regno;
01687   int i;
01688 
01689   /* Special register (R16 - R31).  cris_spec_regs is zero-based. 
01690      Adjust regno accordingly.  */
01691   spec_regno = regno - NUM_GENREGS;
01692   
01693   /* Assume nothing about the layout of the cris_spec_regs struct
01694      when searching.  */
01695   for (i = 0; cris_spec_regs[i].name != NULL; i++)
01696     {
01697       if (cris_spec_regs[i].number == spec_regno 
01698           && cris_spec_reg_applicable (gdbarch, cris_spec_regs[i]))
01699         /* Go with the first applicable register.  */
01700         return cris_spec_regs[i].name;
01701     }
01702   /* Special register not applicable to this CRIS version.  */
01703   return NULL;
01704 }
01705 
01706 static const char *
01707 cris_register_name (struct gdbarch *gdbarch, int regno)
01708 {
01709   static char *cris_genreg_names[] =
01710   { "r0",  "r1",  "r2",  "r3", \
01711     "r4",  "r5",  "r6",  "r7", \
01712     "r8",  "r9",  "r10", "r11", \
01713     "r12", "r13", "sp",  "pc" };
01714 
01715   if (regno >= 0 && regno < NUM_GENREGS)
01716     {
01717       /* General register.  */
01718       return cris_genreg_names[regno];
01719     }
01720   else if (regno >= NUM_GENREGS && regno < gdbarch_num_regs (gdbarch))
01721     {
01722       return cris_special_register_name (gdbarch, regno);
01723     }
01724   else
01725     {
01726       /* Invalid register.  */
01727       return NULL;
01728     }
01729 }
01730 
01731 static const char *
01732 crisv32_register_name (struct gdbarch *gdbarch, int regno)
01733 {
01734   static char *crisv32_genreg_names[] =
01735     { "r0",  "r1",  "r2",  "r3", \
01736       "r4",  "r5",  "r6",  "r7", \
01737       "r8",  "r9",  "r10", "r11", \
01738       "r12", "r13", "sp",  "acr"
01739     };
01740 
01741   static char *crisv32_sreg_names[] =
01742     { "s0",  "s1",  "s2",  "s3", \
01743       "s4",  "s5",  "s6",  "s7", \
01744       "s8",  "s9",  "s10", "s11", \
01745       "s12", "s13", "s14",  "s15"
01746     };
01747 
01748   if (regno >= 0 && regno < NUM_GENREGS)
01749     {
01750       /* General register.  */
01751       return crisv32_genreg_names[regno];
01752     }
01753   else if (regno >= NUM_GENREGS && regno < (NUM_GENREGS + NUM_SPECREGS))
01754     {
01755       return cris_special_register_name (gdbarch, regno);
01756     }
01757   else if (regno == gdbarch_pc_regnum (gdbarch))
01758     {
01759       return "pc";
01760     }
01761   else if (regno >= S0_REGNUM && regno <= S15_REGNUM)
01762     {
01763       return crisv32_sreg_names[regno - S0_REGNUM];
01764     }
01765   else
01766     {
01767       /* Invalid register.  */
01768       return NULL;
01769     }
01770 }
01771 
01772 /* Convert DWARF register number REG to the appropriate register
01773    number used by GDB.  */
01774 
01775 static int
01776 cris_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
01777 {
01778   /* We need to re-map a couple of registers (SRP is 16 in Dwarf-2 register
01779      numbering, MOF is 18).
01780      Adapted from gcc/config/cris/cris.h.  */
01781   static int cris_dwarf_regmap[] = {
01782     0,  1,  2,  3,
01783     4,  5,  6,  7,
01784     8,  9,  10, 11,
01785     12, 13, 14, 15,
01786     27, -1, -1, -1,
01787     -1, -1, -1, 23,
01788     -1, -1, -1, 27,
01789     -1, -1, -1, -1
01790   };
01791   int regnum = -1;
01792 
01793   if (reg >= 0 && reg < ARRAY_SIZE (cris_dwarf_regmap))
01794     regnum = cris_dwarf_regmap[reg];
01795 
01796   if (regnum == -1)
01797     warning (_("Unmapped DWARF Register #%d encountered."), reg);
01798 
01799   return regnum;
01800 }
01801 
01802 /* DWARF-2 frame support.  */
01803 
01804 static void
01805 cris_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
01806                             struct dwarf2_frame_state_reg *reg,
01807                             struct frame_info *this_frame)
01808 {
01809   /* The return address column.  */
01810   if (regnum == gdbarch_pc_regnum (gdbarch))
01811     reg->how = DWARF2_FRAME_REG_RA;
01812 
01813   /* The call frame address.  */
01814   else if (regnum == gdbarch_sp_regnum (gdbarch))
01815     reg->how = DWARF2_FRAME_REG_CFA;
01816 }
01817 
01818 /* Extract from an array regbuf containing the raw register state a function
01819    return value of type type, and copy that, in virtual format, into 
01820    valbuf.  */
01821 
01822 /* In the CRIS ABI, R10 and R11 are used to store return values.  */
01823 
01824 static void
01825 cris_extract_return_value (struct type *type, struct regcache *regcache,
01826                            gdb_byte *valbuf)
01827 {
01828   struct gdbarch *gdbarch = get_regcache_arch (regcache);
01829   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01830   ULONGEST val;
01831   int len = TYPE_LENGTH (type);
01832   
01833   if (len <= 4)
01834     {
01835       /* Get the return value from R10.  */
01836       regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
01837       store_unsigned_integer (valbuf, len, byte_order, val);
01838     }
01839   else if (len <= 8)
01840     {
01841       /* Get the return value from R10 and R11.  */
01842       regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &val);
01843       store_unsigned_integer (valbuf, 4, byte_order, val);
01844       regcache_cooked_read_unsigned (regcache, ARG2_REGNUM, &val);
01845       store_unsigned_integer (valbuf + 4, len - 4, byte_order, val);
01846     }
01847   else
01848     error (_("cris_extract_return_value: type length too large"));
01849 }
01850 
01851 /* Handle the CRIS return value convention.  */
01852 
01853 static enum return_value_convention
01854 cris_return_value (struct gdbarch *gdbarch, struct value *function,
01855                    struct type *type, struct regcache *regcache,
01856                    gdb_byte *readbuf, const gdb_byte *writebuf)
01857 {
01858   if (TYPE_CODE (type) == TYPE_CODE_STRUCT 
01859       || TYPE_CODE (type) == TYPE_CODE_UNION
01860       || TYPE_LENGTH (type) > 8)
01861     /* Structs, unions, and anything larger than 8 bytes (2 registers)
01862        goes on the stack.  */
01863     return RETURN_VALUE_STRUCT_CONVENTION;
01864 
01865   if (readbuf)
01866     cris_extract_return_value (type, regcache, readbuf);
01867   if (writebuf)
01868     cris_store_return_value (type, regcache, writebuf);
01869 
01870   return RETURN_VALUE_REGISTER_CONVENTION;
01871 }
01872 
01873 /* Calculates a value that measures how good inst_args constraints an 
01874    instruction.  It stems from cris_constraint, found in cris-dis.c.  */
01875 
01876 static int
01877 constraint (unsigned int insn, const char *inst_args,
01878             inst_env_type *inst_env)
01879 {
01880   int retval = 0;
01881   int tmp, i;
01882 
01883   const gdb_byte *s = (const gdb_byte *) inst_args;
01884 
01885   for (; *s; s++)
01886     switch (*s) 
01887       {
01888       case 'm':
01889         if ((insn & 0x30) == 0x30)
01890           return -1;
01891         break;
01892         
01893       case 'S':
01894         /* A prefix operand.  */
01895         if (inst_env->prefix_found)
01896           break;
01897         else
01898           return -1;
01899 
01900       case 'B':
01901         /* A "push" prefix.  (This check was REMOVED by san 970921.)  Check for
01902            valid "push" size.  In case of special register, it may be != 4.  */
01903         if (inst_env->prefix_found)
01904           break;
01905         else
01906           return -1;
01907 
01908       case 'D':
01909         retval = (((insn >> 0xC) & 0xF) == (insn & 0xF));
01910         if (!retval)
01911           return -1;
01912         else 
01913           retval += 4;
01914         break;
01915 
01916       case 'P':
01917         tmp = (insn >> 0xC) & 0xF;
01918 
01919         for (i = 0; cris_spec_regs[i].name != NULL; i++)
01920           {
01921             /* Since we match four bits, we will give a value of
01922                4 - 1 = 3 in a match.  If there is a corresponding
01923                exact match of a special register in another pattern, it
01924                will get a value of 4, which will be higher.  This should
01925                be correct in that an exact pattern would match better that
01926                a general pattern.
01927                Note that there is a reason for not returning zero; the
01928                pattern for "clear" is partly  matched in the bit-pattern
01929                (the two lower bits must be zero), while the bit-pattern
01930                for a move from a special register is matched in the
01931                register constraint.
01932                This also means we will will have a race condition if
01933                there is a partly match in three bits in the bit pattern.  */
01934             if (tmp == cris_spec_regs[i].number)
01935               {
01936                 retval += 3;
01937                 break;
01938               }
01939           }
01940         
01941         if (cris_spec_regs[i].name == NULL)
01942           return -1;
01943         break;
01944       }
01945   return retval;
01946 }
01947 
01948 /* Returns the number of bits set in the variable value.  */
01949 
01950 static int
01951 number_of_bits (unsigned int value)
01952 {
01953   int number_of_bits = 0;
01954   
01955   while (value != 0)
01956     {
01957       number_of_bits += 1;
01958       value &= (value - 1);
01959     }
01960   return number_of_bits;
01961 }
01962 
01963 /* Finds the address that should contain the single step breakpoint(s). 
01964    It stems from code in cris-dis.c.  */
01965 
01966 static int
01967 find_cris_op (unsigned short insn, inst_env_type *inst_env)
01968 {
01969   int i;
01970   int max_level_of_match = -1;
01971   int max_matched = -1;
01972   int level_of_match;
01973 
01974   for (i = 0; cris_opcodes[i].name != NULL; i++)
01975     {
01976       if (((cris_opcodes[i].match & insn) == cris_opcodes[i].match) 
01977           && ((cris_opcodes[i].lose & insn) == 0)
01978           /* Only CRISv10 instructions, please.  */
01979           && (cris_opcodes[i].applicable_version != cris_ver_v32p))
01980         {
01981           level_of_match = constraint (insn, cris_opcodes[i].args, inst_env);
01982           if (level_of_match >= 0)
01983             {
01984               level_of_match +=
01985                 number_of_bits (cris_opcodes[i].match | cris_opcodes[i].lose);
01986               if (level_of_match > max_level_of_match)
01987                 {
01988                   max_matched = i;
01989                   max_level_of_match = level_of_match;
01990                   if (level_of_match == 16)
01991                     {
01992                       /* All bits matched, cannot find better.  */
01993                       break;
01994                     }
01995                 }
01996             }
01997         }
01998     }
01999   return max_matched;
02000 }
02001 
02002 /* Attempts to find single-step breakpoints.  Returns -1 on failure which is
02003    actually an internal error.  */
02004 
02005 static int
02006 find_step_target (struct frame_info *frame, inst_env_type *inst_env)
02007 {
02008   int i;
02009   int offset;
02010   unsigned short insn;
02011   struct gdbarch *gdbarch = get_frame_arch (frame);
02012   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02013 
02014   /* Create a local register image and set the initial state.  */
02015   for (i = 0; i < NUM_GENREGS; i++)
02016     {
02017       inst_env->reg[i] = 
02018         (unsigned long) get_frame_register_unsigned (frame, i);
02019     }
02020   offset = NUM_GENREGS;
02021   for (i = 0; i < NUM_SPECREGS; i++)
02022     {
02023       inst_env->preg[i] = 
02024         (unsigned long) get_frame_register_unsigned (frame, offset + i);
02025     }
02026   inst_env->branch_found = 0;
02027   inst_env->slot_needed = 0;
02028   inst_env->delay_slot_pc_active = 0;
02029   inst_env->prefix_found = 0;
02030   inst_env->invalid = 0;
02031   inst_env->xflag_found = 0;
02032   inst_env->disable_interrupt = 0;
02033   inst_env->byte_order = byte_order;
02034 
02035   /* Look for a step target.  */
02036   do
02037     {
02038       /* Read an instruction from the client.  */
02039       insn = read_memory_unsigned_integer
02040              (inst_env->reg[gdbarch_pc_regnum (gdbarch)], 2, byte_order);
02041 
02042       /* If the instruction is not in a delay slot the new content of the
02043          PC is [PC] + 2.  If the instruction is in a delay slot it is not
02044          that simple.  Since a instruction in a delay slot cannot change 
02045          the content of the PC, it does not matter what value PC will have. 
02046          Just make sure it is a valid instruction.  */
02047       if (!inst_env->delay_slot_pc_active)
02048         {
02049           inst_env->reg[gdbarch_pc_regnum (gdbarch)] += 2;
02050         }
02051       else
02052         {
02053           inst_env->delay_slot_pc_active = 0;
02054           inst_env->reg[gdbarch_pc_regnum (gdbarch)]
02055             = inst_env->delay_slot_pc;
02056         }
02057       /* Analyse the present instruction.  */
02058       i = find_cris_op (insn, inst_env);
02059       if (i == -1)
02060         {
02061           inst_env->invalid = 1;
02062         }
02063       else
02064         {
02065           cris_gdb_func (gdbarch, cris_opcodes[i].op, insn, inst_env);
02066         }
02067     } while (!inst_env->invalid 
02068              && (inst_env->prefix_found || inst_env->xflag_found 
02069                  || inst_env->slot_needed));
02070   return i;
02071 }
02072 
02073 /* There is no hardware single-step support.  The function find_step_target
02074    digs through the opcodes in order to find all possible targets.
02075    Either one ordinary target or two targets for branches may be found.  */
02076 
02077 static int
02078 cris_software_single_step (struct frame_info *frame)
02079 {
02080   struct gdbarch *gdbarch = get_frame_arch (frame);
02081   struct address_space *aspace = get_frame_address_space (frame);
02082   inst_env_type inst_env;
02083 
02084   /* Analyse the present instruction environment and insert 
02085      breakpoints.  */
02086   int status = find_step_target (frame, &inst_env);
02087   if (status == -1)
02088     {
02089       /* Could not find a target.  Things are likely to go downhill 
02090          from here.  */
02091       warning (_("CRIS software single step could not find a step target."));
02092     }
02093   else
02094     {
02095       /* Insert at most two breakpoints.  One for the next PC content
02096          and possibly another one for a branch, jump, etc.  */
02097       CORE_ADDR next_pc
02098         = (CORE_ADDR) inst_env.reg[gdbarch_pc_regnum (gdbarch)];
02099       insert_single_step_breakpoint (gdbarch, aspace, next_pc);
02100       if (inst_env.branch_found 
02101           && (CORE_ADDR) inst_env.branch_break_address != next_pc)
02102         {
02103           CORE_ADDR branch_target_address
02104                 = (CORE_ADDR) inst_env.branch_break_address;
02105           insert_single_step_breakpoint (gdbarch,
02106                                          aspace, branch_target_address);
02107         }
02108     }
02109 
02110   return 1;
02111 }
02112 
02113 /* Calculates the prefix value for quick offset addressing mode.  */
02114 
02115 static void
02116 quick_mode_bdap_prefix (unsigned short inst, inst_env_type *inst_env)
02117 {
02118   /* It's invalid to be in a delay slot.  You can't have a prefix to this
02119      instruction (not 100% sure).  */
02120   if (inst_env->slot_needed || inst_env->prefix_found)
02121     {
02122       inst_env->invalid = 1;
02123       return; 
02124     }
02125  
02126   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
02127   inst_env->prefix_value += cris_get_bdap_quick_offset (inst);
02128 
02129   /* A prefix doesn't change the xflag_found.  But the rest of the flags
02130      need updating.  */
02131   inst_env->slot_needed = 0;
02132   inst_env->prefix_found = 1;
02133 }
02134 
02135 /* Updates the autoincrement register.  The size of the increment is derived 
02136    from the size of the operation.  The PC is always kept aligned on even
02137    word addresses.  */
02138 
02139 static void 
02140 process_autoincrement (int size, unsigned short inst, inst_env_type *inst_env)
02141 {
02142   if (size == INST_BYTE_SIZE)
02143     {
02144       inst_env->reg[cris_get_operand1 (inst)] += 1;
02145 
02146       /* The PC must be word aligned, so increase the PC with one
02147          word even if the size is byte.  */
02148       if (cris_get_operand1 (inst) == REG_PC)
02149         {
02150           inst_env->reg[REG_PC] += 1;
02151         }
02152     }
02153   else if (size == INST_WORD_SIZE)
02154     {
02155       inst_env->reg[cris_get_operand1 (inst)] += 2;
02156     }
02157   else if (size == INST_DWORD_SIZE)
02158     {
02159       inst_env->reg[cris_get_operand1 (inst)] += 4;
02160     }
02161   else
02162     {
02163       /* Invalid size.  */
02164       inst_env->invalid = 1;
02165     }
02166 }
02167 
02168 /* Just a forward declaration.  */
02169 
02170 static unsigned long get_data_from_address (unsigned short *inst,
02171                                             CORE_ADDR address,
02172                                             enum bfd_endian byte_order);
02173 
02174 /* Calculates the prefix value for the general case of offset addressing 
02175    mode.  */
02176 
02177 static void
02178 bdap_prefix (unsigned short inst, inst_env_type *inst_env)
02179 {
02180   /* It's invalid to be in a delay slot.  */
02181   if (inst_env->slot_needed || inst_env->prefix_found)
02182     {
02183       inst_env->invalid = 1;
02184       return; 
02185     }
02186 
02187   /* The calculation of prefix_value used to be after process_autoincrement,
02188      but that fails for an instruction such as jsr [$r0+12] which is encoded
02189      as 5f0d 0c00 30b9 when compiled with -fpic.  Since PC is operand1 it
02190      mustn't be incremented until we have read it and what it points at.  */
02191   inst_env->prefix_value = inst_env->reg[cris_get_operand2 (inst)];
02192 
02193   /* The offset is an indirection of the contents of the operand1 register.  */
02194   inst_env->prefix_value += 
02195     get_data_from_address (&inst, inst_env->reg[cris_get_operand1 (inst)],
02196                            inst_env->byte_order);
02197   
02198   if (cris_get_mode (inst) == AUTOINC_MODE)
02199     {
02200       process_autoincrement (cris_get_size (inst), inst, inst_env); 
02201     }
02202    
02203   /* A prefix doesn't change the xflag_found.  But the rest of the flags
02204      need updating.  */
02205   inst_env->slot_needed = 0;
02206   inst_env->prefix_found = 1;
02207 }
02208 
02209 /* Calculates the prefix value for the index addressing mode.  */
02210 
02211 static void
02212 biap_prefix (unsigned short inst, inst_env_type *inst_env)
02213 {
02214   /* It's invalid to be in a delay slot.  I can't see that it's possible to
02215      have a prefix to this instruction.  So I will treat this as invalid.  */
02216   if (inst_env->slot_needed || inst_env->prefix_found)
02217     {
02218       inst_env->invalid = 1;
02219       return;
02220     }
02221   
02222   inst_env->prefix_value = inst_env->reg[cris_get_operand1 (inst)];
02223 
02224   /* The offset is the operand2 value shifted the size of the instruction 
02225      to the left.  */
02226   inst_env->prefix_value += 
02227     inst_env->reg[cris_get_operand2 (inst)] << cris_get_size (inst);
02228   
02229   /* If the PC is operand1 (base) the address used is the address after 
02230      the main instruction, i.e. address + 2 (the PC is already compensated
02231      for the prefix operation).  */
02232   if (cris_get_operand1 (inst) == REG_PC)
02233     {
02234       inst_env->prefix_value += 2;
02235     }
02236 
02237   /* A prefix doesn't change the xflag_found.  But the rest of the flags
02238      need updating.  */
02239   inst_env->slot_needed = 0;
02240   inst_env->xflag_found = 0;
02241   inst_env->prefix_found = 1;
02242 }
02243 
02244 /* Calculates the prefix value for the double indirect addressing mode.  */
02245 
02246 static void 
02247 dip_prefix (unsigned short inst, inst_env_type *inst_env)
02248 {
02249 
02250   CORE_ADDR address;
02251 
02252   /* It's invalid to be in a delay slot.  */
02253   if (inst_env->slot_needed || inst_env->prefix_found)
02254     {
02255       inst_env->invalid = 1;
02256       return;
02257     }
02258   
02259   /* The prefix value is one dereference of the contents of the operand1
02260      register.  */
02261   address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
02262   inst_env->prefix_value
02263     = read_memory_unsigned_integer (address, 4, inst_env->byte_order);
02264     
02265   /* Check if the mode is autoincrement.  */
02266   if (cris_get_mode (inst) == AUTOINC_MODE)
02267     {
02268       inst_env->reg[cris_get_operand1 (inst)] += 4;
02269     }
02270 
02271   /* A prefix doesn't change the xflag_found.  But the rest of the flags
02272      need updating.  */
02273   inst_env->slot_needed = 0;
02274   inst_env->xflag_found = 0;
02275   inst_env->prefix_found = 1;
02276 }
02277 
02278 /* Finds the destination for a branch with 8-bits offset.  */
02279 
02280 static void
02281 eight_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
02282 {
02283 
02284   short offset;
02285 
02286   /* If we have a prefix or are in a delay slot it's bad.  */
02287   if (inst_env->slot_needed || inst_env->prefix_found)
02288     {
02289       inst_env->invalid = 1;
02290       return;
02291     }
02292   
02293   /* We have a branch, find out where the branch will land.  */
02294   offset = cris_get_branch_short_offset (inst);
02295 
02296   /* Check if the offset is signed.  */
02297   if (offset & BRANCH_SIGNED_SHORT_OFFSET_MASK)
02298     {
02299       offset |= 0xFF00;
02300     }
02301   
02302   /* The offset ends with the sign bit, set it to zero.  The address
02303      should always be word aligned.  */
02304   offset &= ~BRANCH_SIGNED_SHORT_OFFSET_MASK;
02305   
02306   inst_env->branch_found = 1;
02307   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
02308 
02309   inst_env->slot_needed = 1;
02310   inst_env->prefix_found = 0;
02311   inst_env->xflag_found = 0;
02312   inst_env->disable_interrupt = 1;
02313 }
02314 
02315 /* Finds the destination for a branch with 16-bits offset.  */
02316 
02317 static void 
02318 sixteen_bit_offset_branch_op (unsigned short inst, inst_env_type *inst_env)
02319 {
02320   short offset;
02321 
02322   /* If we have a prefix or is in a delay slot it's bad.  */
02323   if (inst_env->slot_needed || inst_env->prefix_found)
02324     {
02325       inst_env->invalid = 1;
02326       return;
02327     }
02328 
02329   /* We have a branch, find out the offset for the branch.  */
02330   offset = read_memory_integer (inst_env->reg[REG_PC], 2,
02331                                 inst_env->byte_order);
02332 
02333   /* The instruction is one word longer than normal, so add one word
02334      to the PC.  */
02335   inst_env->reg[REG_PC] += 2;
02336 
02337   inst_env->branch_found = 1;
02338   inst_env->branch_break_address = inst_env->reg[REG_PC] + offset;
02339 
02340 
02341   inst_env->slot_needed = 1;
02342   inst_env->prefix_found = 0;
02343   inst_env->xflag_found = 0;
02344   inst_env->disable_interrupt = 1;
02345 }
02346 
02347 /* Handles the ABS instruction.  */
02348 
02349 static void 
02350 abs_op (unsigned short inst, inst_env_type *inst_env)
02351 {
02352 
02353   long value;
02354   
02355   /* ABS can't have a prefix, so it's bad if it does.  */
02356   if (inst_env->prefix_found)
02357     {
02358       inst_env->invalid = 1;
02359       return;
02360     }
02361 
02362   /* Check if the operation affects the PC.  */
02363   if (cris_get_operand2 (inst) == REG_PC)
02364     {
02365     
02366       /* It's invalid to change to the PC if we are in a delay slot.  */
02367       if (inst_env->slot_needed)
02368         {
02369           inst_env->invalid = 1;
02370           return;
02371         }
02372 
02373       value = (long) inst_env->reg[REG_PC];
02374 
02375       /* The value of abs (SIGNED_DWORD_MASK) is SIGNED_DWORD_MASK.  */
02376       if (value != SIGNED_DWORD_MASK)
02377         {
02378           value = -value;
02379           inst_env->reg[REG_PC] = (long) value;
02380         }
02381     }
02382 
02383   inst_env->slot_needed = 0;
02384   inst_env->prefix_found = 0;
02385   inst_env->xflag_found = 0;
02386   inst_env->disable_interrupt = 0;
02387 }
02388 
02389 /* Handles the ADDI instruction.  */
02390 
02391 static void 
02392 addi_op (unsigned short inst, inst_env_type *inst_env)
02393 {
02394   /* It's invalid to have the PC as base register.  And ADDI can't have
02395      a prefix.  */
02396   if (inst_env->prefix_found || (cris_get_operand1 (inst) == REG_PC))
02397     {
02398       inst_env->invalid = 1;
02399       return;
02400     }
02401 
02402   inst_env->slot_needed = 0;
02403   inst_env->prefix_found = 0;
02404   inst_env->xflag_found = 0;
02405   inst_env->disable_interrupt = 0;
02406 }
02407 
02408 /* Handles the ASR instruction.  */
02409 
02410 static void 
02411 asr_op (unsigned short inst, inst_env_type *inst_env)
02412 {
02413   int shift_steps;
02414   unsigned long value;
02415   unsigned long signed_extend_mask = 0;
02416 
02417   /* ASR can't have a prefix, so check that it doesn't.  */
02418   if (inst_env->prefix_found)
02419     {
02420       inst_env->invalid = 1;
02421       return;
02422     }
02423 
02424   /* Check if the PC is the target register.  */
02425   if (cris_get_operand2 (inst) == REG_PC)
02426     {
02427       /* It's invalid to change the PC in a delay slot.  */
02428       if (inst_env->slot_needed)
02429         {
02430           inst_env->invalid = 1;
02431           return;
02432         }
02433       /* Get the number of bits to shift.  */
02434       shift_steps
02435         = cris_get_asr_shift_steps (inst_env->reg[cris_get_operand1 (inst)]);
02436       value = inst_env->reg[REG_PC];
02437 
02438       /* Find out how many bits the operation should apply to.  */
02439       if (cris_get_size (inst) == INST_BYTE_SIZE)
02440         {
02441           if (value & SIGNED_BYTE_MASK)
02442             {
02443               signed_extend_mask = 0xFF;
02444               signed_extend_mask = signed_extend_mask >> shift_steps;
02445               signed_extend_mask = ~signed_extend_mask;
02446             }
02447           value = value >> shift_steps;
02448           value |= signed_extend_mask;
02449           value &= 0xFF;
02450           inst_env->reg[REG_PC] &= 0xFFFFFF00;
02451           inst_env->reg[REG_PC] |= value;
02452         }
02453       else if (cris_get_size (inst) == INST_WORD_SIZE)
02454         {
02455           if (value & SIGNED_WORD_MASK)
02456             {
02457               signed_extend_mask = 0xFFFF;
02458               signed_extend_mask = signed_extend_mask >> shift_steps;
02459               signed_extend_mask = ~signed_extend_mask;
02460             }
02461           value = value >> shift_steps;
02462           value |= signed_extend_mask;
02463           value &= 0xFFFF;
02464           inst_env->reg[REG_PC] &= 0xFFFF0000;
02465           inst_env->reg[REG_PC] |= value;
02466         }
02467       else if (cris_get_size (inst) == INST_DWORD_SIZE)
02468         {
02469           if (value & SIGNED_DWORD_MASK)
02470             {
02471               signed_extend_mask = 0xFFFFFFFF;
02472               signed_extend_mask = signed_extend_mask >> shift_steps;
02473               signed_extend_mask = ~signed_extend_mask;
02474             }
02475           value = value >> shift_steps;
02476           value |= signed_extend_mask;
02477           inst_env->reg[REG_PC]  = value;
02478         }
02479     }
02480   inst_env->slot_needed = 0;
02481   inst_env->prefix_found = 0;
02482   inst_env->xflag_found = 0;
02483   inst_env->disable_interrupt = 0;
02484 }
02485 
02486 /* Handles the ASRQ instruction.  */
02487 
02488 static void 
02489 asrq_op (unsigned short inst, inst_env_type *inst_env)
02490 {
02491 
02492   int shift_steps;
02493   unsigned long value;
02494   unsigned long signed_extend_mask = 0;
02495   
02496   /* ASRQ can't have a prefix, so check that it doesn't.  */
02497   if (inst_env->prefix_found)
02498     {
02499       inst_env->invalid = 1;
02500       return;
02501     }
02502 
02503   /* Check if the PC is the target register.  */
02504   if (cris_get_operand2 (inst) == REG_PC)
02505     {
02506 
02507       /* It's invalid to change the PC in a delay slot.  */
02508       if (inst_env->slot_needed)
02509         {
02510           inst_env->invalid = 1;
02511           return;
02512         }
02513       /* The shift size is given as a 5 bit quick value, i.e. we don't
02514          want the sign bit of the quick value.  */
02515       shift_steps = cris_get_asr_shift_steps (inst);
02516       value = inst_env->reg[REG_PC];
02517       if (value & SIGNED_DWORD_MASK)
02518         {
02519           signed_extend_mask = 0xFFFFFFFF;
02520           signed_extend_mask = signed_extend_mask >> shift_steps;
02521           signed_extend_mask = ~signed_extend_mask;
02522         }
02523       value = value >> shift_steps;
02524       value |= signed_extend_mask;
02525       inst_env->reg[REG_PC]  = value;
02526     }
02527   inst_env->slot_needed = 0;
02528   inst_env->prefix_found = 0;
02529   inst_env->xflag_found = 0;
02530   inst_env->disable_interrupt = 0;
02531 }
02532 
02533 /* Handles the AX, EI and SETF instruction.  */
02534 
02535 static void 
02536 ax_ei_setf_op (unsigned short inst, inst_env_type *inst_env)
02537 {
02538   if (inst_env->prefix_found)
02539     {
02540       inst_env->invalid = 1;
02541       return;
02542     }
02543   /* Check if the instruction is setting the X flag.  */
02544   if (cris_is_xflag_bit_on (inst))
02545     {
02546       inst_env->xflag_found = 1;
02547     }
02548   else
02549     {
02550       inst_env->xflag_found = 0;
02551     }
02552   inst_env->slot_needed = 0;
02553   inst_env->prefix_found = 0;
02554   inst_env->disable_interrupt = 1;
02555 }
02556 
02557 /* Checks if the instruction is in assign mode.  If so, it updates the assign 
02558    register.  Note that check_assign assumes that the caller has checked that
02559    there is a prefix to this instruction.  The mode check depends on this.  */
02560 
02561 static void 
02562 check_assign (unsigned short inst, inst_env_type *inst_env)
02563 {
02564   /* Check if it's an assign addressing mode.  */
02565   if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
02566     {
02567       /* Assign the prefix value to operand 1.  */
02568       inst_env->reg[cris_get_operand1 (inst)] = inst_env->prefix_value;
02569     }
02570 }
02571 
02572 /* Handles the 2-operand BOUND instruction.  */
02573 
02574 static void 
02575 two_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
02576 {
02577   /* It's invalid to have the PC as the index operand.  */
02578   if (cris_get_operand2 (inst) == REG_PC)
02579     {
02580       inst_env->invalid = 1;
02581       return;
02582     }
02583   /* Check if we have a prefix.  */
02584   if (inst_env->prefix_found)
02585     {
02586       check_assign (inst, inst_env);
02587     }
02588   /* Check if this is an autoincrement mode.  */
02589   else if (cris_get_mode (inst) == AUTOINC_MODE)
02590     {
02591       /* It's invalid to change the PC in a delay slot.  */
02592       if (inst_env->slot_needed)
02593         {
02594           inst_env->invalid = 1;
02595           return;
02596         }
02597       process_autoincrement (cris_get_size (inst), inst, inst_env);
02598     }
02599   inst_env->slot_needed = 0;
02600   inst_env->prefix_found = 0;
02601   inst_env->xflag_found = 0;
02602   inst_env->disable_interrupt = 0;
02603 }
02604 
02605 /* Handles the 3-operand BOUND instruction.  */
02606 
02607 static void 
02608 three_operand_bound_op (unsigned short inst, inst_env_type *inst_env)
02609 {
02610   /* It's an error if we haven't got a prefix.  And it's also an error
02611      if the PC is the destination register.  */
02612   if ((!inst_env->prefix_found) || (cris_get_operand1 (inst) == REG_PC))
02613     {
02614       inst_env->invalid = 1;
02615       return;
02616     }
02617   inst_env->slot_needed = 0;
02618   inst_env->prefix_found = 0;
02619   inst_env->xflag_found = 0;
02620   inst_env->disable_interrupt = 0;
02621 }
02622 
02623 /* Clears the status flags in inst_env.  */
02624 
02625 static void 
02626 btst_nop_op (unsigned short inst, inst_env_type *inst_env)
02627 {
02628   /* It's an error if we have got a prefix.  */
02629   if (inst_env->prefix_found)
02630     {
02631       inst_env->invalid = 1;
02632       return;
02633     }
02634 
02635   inst_env->slot_needed = 0;
02636   inst_env->prefix_found = 0;
02637   inst_env->xflag_found = 0;
02638   inst_env->disable_interrupt = 0;
02639 }
02640 
02641 /* Clears the status flags in inst_env.  */
02642 
02643 static void 
02644 clearf_di_op (unsigned short inst, inst_env_type *inst_env)
02645 {
02646   /* It's an error if we have got a prefix.  */
02647   if (inst_env->prefix_found)
02648     {
02649       inst_env->invalid = 1;
02650       return;
02651     }
02652 
02653   inst_env->slot_needed = 0;
02654   inst_env->prefix_found = 0;
02655   inst_env->xflag_found = 0;
02656   inst_env->disable_interrupt = 1;
02657 }
02658 
02659 /* Handles the CLEAR instruction if it's in register mode.  */
02660 
02661 static void 
02662 reg_mode_clear_op (unsigned short inst, inst_env_type *inst_env)
02663 {
02664   /* Check if the target is the PC.  */
02665   if (cris_get_operand2 (inst) == REG_PC)
02666     {
02667       /* The instruction will clear the instruction's size bits.  */
02668       int clear_size = cris_get_clear_size (inst);
02669       if (clear_size == INST_BYTE_SIZE)
02670         {
02671           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFFFF00;
02672         }
02673       if (clear_size == INST_WORD_SIZE)
02674         {
02675           inst_env->delay_slot_pc = inst_env->reg[REG_PC] & 0xFFFF0000;
02676         }
02677       if (clear_size == INST_DWORD_SIZE)
02678         {
02679           inst_env->delay_slot_pc = 0x0;
02680         }
02681       /* The jump will be delayed with one delay slot.  So we need a delay 
02682          slot.  */
02683       inst_env->slot_needed = 1;
02684       inst_env->delay_slot_pc_active = 1;
02685     }
02686   else
02687     {
02688       /* The PC will not change => no delay slot.  */
02689       inst_env->slot_needed = 0;
02690     }
02691   inst_env->prefix_found = 0;
02692   inst_env->xflag_found = 0;
02693   inst_env->disable_interrupt = 0;
02694 }
02695 
02696 /* Handles the TEST instruction if it's in register mode.  */
02697 
02698 static void
02699 reg_mode_test_op (unsigned short inst, inst_env_type *inst_env)
02700 {
02701   /* It's an error if we have got a prefix.  */
02702   if (inst_env->prefix_found)
02703     {
02704       inst_env->invalid = 1;
02705       return;
02706     }
02707   inst_env->slot_needed = 0;
02708   inst_env->prefix_found = 0;
02709   inst_env->xflag_found = 0;
02710   inst_env->disable_interrupt = 0;
02711 
02712 }
02713 
02714 /* Handles the CLEAR and TEST instruction if the instruction isn't 
02715    in register mode.  */
02716 
02717 static void 
02718 none_reg_mode_clear_test_op (unsigned short inst, inst_env_type *inst_env)
02719 {
02720   /* Check if we are in a prefix mode.  */
02721   if (inst_env->prefix_found)
02722     {
02723       /* The only way the PC can change is if this instruction is in
02724          assign addressing mode.  */
02725       check_assign (inst, inst_env);
02726     }
02727   /* Indirect mode can't change the PC so just check if the mode is
02728      autoincrement.  */
02729   else if (cris_get_mode (inst) == AUTOINC_MODE)
02730     {
02731       process_autoincrement (cris_get_size (inst), inst, inst_env);
02732     }
02733   inst_env->slot_needed = 0;
02734   inst_env->prefix_found = 0;
02735   inst_env->xflag_found = 0;
02736   inst_env->disable_interrupt = 0;
02737 }
02738 
02739 /* Checks that the PC isn't the destination register or the instructions has
02740    a prefix.  */
02741 
02742 static void 
02743 dstep_logshift_mstep_neg_not_op (unsigned short inst, inst_env_type *inst_env)
02744 {
02745   /* It's invalid to have the PC as the destination.  The instruction can't
02746      have a prefix.  */
02747   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
02748     {
02749       inst_env->invalid = 1;
02750       return;
02751     }
02752 
02753   inst_env->slot_needed = 0;
02754   inst_env->prefix_found = 0;
02755   inst_env->xflag_found = 0;
02756   inst_env->disable_interrupt = 0;
02757 }
02758 
02759 /* Checks that the instruction doesn't have a prefix.  */
02760 
02761 static void
02762 break_op (unsigned short inst, inst_env_type *inst_env)
02763 {
02764   /* The instruction can't have a prefix.  */
02765   if (inst_env->prefix_found)
02766     {
02767       inst_env->invalid = 1;
02768       return;
02769     }
02770 
02771   inst_env->slot_needed = 0;
02772   inst_env->prefix_found = 0;
02773   inst_env->xflag_found = 0;
02774   inst_env->disable_interrupt = 1;
02775 }
02776 
02777 /* Checks that the PC isn't the destination register and that the instruction
02778    doesn't have a prefix.  */
02779 
02780 static void
02781 scc_op (unsigned short inst, inst_env_type *inst_env)
02782 {
02783   /* It's invalid to have the PC as the destination.  The instruction can't
02784      have a prefix.  */
02785   if ((cris_get_operand2 (inst) == REG_PC) || inst_env->prefix_found)
02786     {
02787       inst_env->invalid = 1;
02788       return;
02789     }
02790 
02791   inst_env->slot_needed = 0;
02792   inst_env->prefix_found = 0;
02793   inst_env->xflag_found = 0;
02794   inst_env->disable_interrupt = 1;
02795 }
02796 
02797 /* Handles the register mode JUMP instruction.  */
02798 
02799 static void 
02800 reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
02801 {
02802   /* It's invalid to do a JUMP in a delay slot.  The mode is register, so 
02803      you can't have a prefix.  */
02804   if ((inst_env->slot_needed) || (inst_env->prefix_found))
02805     {
02806       inst_env->invalid = 1;
02807       return;
02808     }
02809   
02810   /* Just change the PC.  */
02811   inst_env->reg[REG_PC] = inst_env->reg[cris_get_operand1 (inst)];
02812   inst_env->slot_needed = 0;
02813   inst_env->prefix_found = 0;
02814   inst_env->xflag_found = 0;
02815   inst_env->disable_interrupt = 1;
02816 }
02817 
02818 /* Handles the JUMP instruction for all modes except register.  */
02819 
02820 static void
02821 none_reg_mode_jump_op (unsigned short inst, inst_env_type *inst_env)
02822 {
02823   unsigned long newpc;
02824   CORE_ADDR address;
02825 
02826   /* It's invalid to do a JUMP in a delay slot.  */
02827   if (inst_env->slot_needed)
02828     {
02829       inst_env->invalid = 1;
02830     }
02831   else
02832     {
02833       /* Check if we have a prefix.  */
02834       if (inst_env->prefix_found)
02835         {
02836           check_assign (inst, inst_env);
02837 
02838           /* Get the new value for the PC.  */
02839           newpc = 
02840             read_memory_unsigned_integer ((CORE_ADDR) inst_env->prefix_value,
02841                                           4, inst_env->byte_order);
02842         }
02843       else
02844         {
02845           /* Get the new value for the PC.  */
02846           address = (CORE_ADDR) inst_env->reg[cris_get_operand1 (inst)];
02847           newpc = read_memory_unsigned_integer (address,
02848                                                 4, inst_env->byte_order);
02849 
02850           /* Check if we should increment a register.  */
02851           if (cris_get_mode (inst) == AUTOINC_MODE)
02852             {
02853               inst_env->reg[cris_get_operand1 (inst)] += 4;
02854             }
02855         }
02856       inst_env->reg[REG_PC] = newpc;
02857     }
02858   inst_env->slot_needed = 0;
02859   inst_env->prefix_found = 0;
02860   inst_env->xflag_found = 0;
02861   inst_env->disable_interrupt = 1;
02862 }
02863 
02864 /* Handles moves to special registers (aka P-register) for all modes.  */
02865 
02866 static void 
02867 move_to_preg_op (struct gdbarch *gdbarch, unsigned short inst,
02868                  inst_env_type *inst_env)
02869 {
02870   if (inst_env->prefix_found)
02871     {
02872       /* The instruction has a prefix that means we are only interested if
02873          the instruction is in assign mode.  */
02874       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
02875         {
02876           /* The prefix handles the problem if we are in a delay slot.  */
02877           if (cris_get_operand1 (inst) == REG_PC)
02878             {
02879               /* Just take care of the assign.  */
02880               check_assign (inst, inst_env);
02881             }
02882         }
02883     }
02884   else if (cris_get_mode (inst) == AUTOINC_MODE)
02885     {
02886       /* The instruction doesn't have a prefix, the only case left that we
02887          are interested in is the autoincrement mode.  */
02888       if (cris_get_operand1 (inst) == REG_PC)
02889         {
02890           /* If the PC is to be incremented it's invalid to be in a 
02891              delay slot.  */
02892           if (inst_env->slot_needed)
02893             {
02894               inst_env->invalid = 1;
02895               return;
02896             }
02897 
02898           /* The increment depends on the size of the special register.  */
02899           if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
02900             {
02901               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
02902             }
02903           else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
02904             {
02905               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
02906             }
02907           else
02908             {
02909               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
02910             }
02911         }
02912     }
02913   inst_env->slot_needed = 0;
02914   inst_env->prefix_found = 0;
02915   inst_env->xflag_found = 0;
02916   inst_env->disable_interrupt = 1;
02917 }
02918 
02919 /* Handles moves from special registers (aka P-register) for all modes
02920    except register.  */
02921 
02922 static void 
02923 none_reg_mode_move_from_preg_op (struct gdbarch *gdbarch, unsigned short inst,
02924                                  inst_env_type *inst_env)
02925 {
02926   if (inst_env->prefix_found)
02927     {
02928       /* The instruction has a prefix that means we are only interested if
02929          the instruction is in assign mode.  */
02930       if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
02931         {
02932           /* The prefix handles the problem if we are in a delay slot.  */
02933           if (cris_get_operand1 (inst) == REG_PC)
02934             {
02935               /* Just take care of the assign.  */
02936               check_assign (inst, inst_env);
02937             }
02938         }
02939     }    
02940   /* The instruction doesn't have a prefix, the only case left that we
02941      are interested in is the autoincrement mode.  */
02942   else if (cris_get_mode (inst) == AUTOINC_MODE)
02943     {
02944       if (cris_get_operand1 (inst) == REG_PC)
02945         {
02946           /* If the PC is to be incremented it's invalid to be in a 
02947              delay slot.  */
02948           if (inst_env->slot_needed)
02949             {
02950               inst_env->invalid = 1;
02951               return;
02952             }
02953           
02954           /* The increment depends on the size of the special register.  */
02955           if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 1)
02956             {
02957               process_autoincrement (INST_BYTE_SIZE, inst, inst_env);
02958             }
02959           else if (cris_register_size (gdbarch, cris_get_operand2 (inst)) == 2)
02960             {
02961               process_autoincrement (INST_WORD_SIZE, inst, inst_env);
02962             }
02963           else
02964             {
02965               process_autoincrement (INST_DWORD_SIZE, inst, inst_env);
02966             }
02967         }
02968     }
02969   inst_env->slot_needed = 0;
02970   inst_env->prefix_found = 0;
02971   inst_env->xflag_found = 0;
02972   inst_env->disable_interrupt = 1;
02973 }
02974 
02975 /* Handles moves from special registers (aka P-register) when the mode
02976    is register.  */
02977 
02978 static void 
02979 reg_mode_move_from_preg_op (unsigned short inst, inst_env_type *inst_env)
02980 {
02981   /* Register mode move from special register can't have a prefix.  */
02982   if (inst_env->prefix_found)
02983     {
02984       inst_env->invalid = 1;
02985       return;
02986     }
02987 
02988   if (cris_get_operand1 (inst) == REG_PC)
02989     {
02990       /* It's invalid to change the PC in a delay slot.  */
02991       if (inst_env->slot_needed)
02992         {
02993           inst_env->invalid = 1;
02994           return;
02995         }
02996       /* The destination is the PC, the jump will have a delay slot.  */
02997       inst_env->delay_slot_pc = inst_env->preg[cris_get_operand2 (inst)];
02998       inst_env->slot_needed = 1;
02999       inst_env->delay_slot_pc_active = 1;
03000     }
03001   else
03002     {
03003       /* If the destination isn't PC, there will be no jump.  */
03004       inst_env->slot_needed = 0;
03005     }
03006   inst_env->prefix_found = 0;
03007   inst_env->xflag_found = 0;
03008   inst_env->disable_interrupt = 1;
03009 }
03010 
03011 /* Handles the MOVEM from memory to general register instruction.  */
03012 
03013 static void 
03014 move_mem_to_reg_movem_op (unsigned short inst, inst_env_type *inst_env)
03015 {
03016   if (inst_env->prefix_found)
03017     {
03018       /* The prefix handles the problem if we are in a delay slot.  Is the
03019          MOVEM instruction going to change the PC?  */
03020       if (cris_get_operand2 (inst) >= REG_PC)
03021         {
03022           inst_env->reg[REG_PC] = 
03023             read_memory_unsigned_integer (inst_env->prefix_value,
03024                                           4, inst_env->byte_order);
03025         }
03026       /* The assign value is the value after the increment.  Normally, the   
03027          assign value is the value before the increment.  */
03028       if ((cris_get_operand1 (inst) == REG_PC) 
03029           && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
03030         {
03031           inst_env->reg[REG_PC] = inst_env->prefix_value;
03032           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
03033         }
03034     }
03035   else
03036     {
03037       /* Is the MOVEM instruction going to change the PC?  */
03038       if (cris_get_operand2 (inst) == REG_PC)
03039         {
03040           /* It's invalid to change the PC in a delay slot.  */
03041           if (inst_env->slot_needed)
03042             {
03043               inst_env->invalid = 1;
03044               return;
03045             }
03046           inst_env->reg[REG_PC] =
03047             read_memory_unsigned_integer (inst_env->reg[cris_get_operand1 (inst)], 
03048                                           4, inst_env->byte_order);
03049         }
03050       /* The increment is not depending on the size, instead it's depending
03051          on the number of registers loaded from memory.  */
03052       if ((cris_get_operand1 (inst) == REG_PC)
03053           && (cris_get_mode (inst) == AUTOINC_MODE))
03054         {
03055           /* It's invalid to change the PC in a delay slot.  */
03056           if (inst_env->slot_needed)
03057             {
03058               inst_env->invalid = 1;
03059               return;
03060             }
03061           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1); 
03062         }
03063     }
03064   inst_env->slot_needed = 0;
03065   inst_env->prefix_found = 0;
03066   inst_env->xflag_found = 0;
03067   inst_env->disable_interrupt = 0;
03068 }
03069 
03070 /* Handles the MOVEM to memory from general register instruction.  */
03071 
03072 static void 
03073 move_reg_to_mem_movem_op (unsigned short inst, inst_env_type *inst_env)
03074 {
03075   if (inst_env->prefix_found)
03076     {
03077       /* The assign value is the value after the increment.  Normally, the
03078          assign value is the value before the increment.  */
03079       if ((cris_get_operand1 (inst) == REG_PC)
03080           && (cris_get_mode (inst) == PREFIX_ASSIGN_MODE))
03081         {
03082           /* The prefix handles the problem if we are in a delay slot.  */
03083           inst_env->reg[REG_PC] = inst_env->prefix_value;
03084           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
03085         }
03086     }
03087   else
03088     {
03089       /* The increment is not depending on the size, instead it's depending
03090          on the number of registers loaded to memory.  */
03091       if ((cris_get_operand1 (inst) == REG_PC)
03092           && (cris_get_mode (inst) == AUTOINC_MODE))
03093         {
03094           /* It's invalid to change the PC in a delay slot.  */
03095           if (inst_env->slot_needed)
03096             {
03097               inst_env->invalid = 1;
03098               return;
03099             }
03100           inst_env->reg[REG_PC] += 4 * (cris_get_operand2 (inst) + 1);
03101         }
03102     }
03103   inst_env->slot_needed = 0;
03104   inst_env->prefix_found = 0;
03105   inst_env->xflag_found = 0;
03106   inst_env->disable_interrupt = 0;
03107 }
03108 
03109 /* Handles the intructions that's not yet implemented, by setting 
03110    inst_env->invalid to true.  */
03111 
03112 static void 
03113 not_implemented_op (unsigned short inst, inst_env_type *inst_env)
03114 {
03115   inst_env->invalid = 1;
03116 }
03117 
03118 /* Handles the XOR instruction.  */
03119 
03120 static void 
03121 xor_op (unsigned short inst, inst_env_type *inst_env)
03122 {
03123   /* XOR can't have a prefix.  */
03124   if (inst_env->prefix_found)
03125     {
03126       inst_env->invalid = 1;
03127       return;
03128     }
03129 
03130   /* Check if the PC is the target.  */
03131   if (cris_get_operand2 (inst) == REG_PC)
03132     {
03133       /* It's invalid to change the PC in a delay slot.  */
03134       if (inst_env->slot_needed)
03135         {
03136           inst_env->invalid = 1;
03137           return;
03138         }
03139       inst_env->reg[REG_PC] ^= inst_env->reg[cris_get_operand1 (inst)];
03140     }
03141   inst_env->slot_needed = 0;
03142   inst_env->prefix_found = 0;
03143   inst_env->xflag_found = 0;
03144   inst_env->disable_interrupt = 0;
03145 }
03146 
03147 /* Handles the MULS instruction.  */
03148 
03149 static void 
03150 muls_op (unsigned short inst, inst_env_type *inst_env)
03151 {
03152   /* MULS/U can't have a prefix.  */
03153   if (inst_env->prefix_found)
03154     {
03155       inst_env->invalid = 1;
03156       return;
03157     }
03158 
03159   /* Consider it invalid if the PC is the target.  */
03160   if (cris_get_operand2 (inst) == REG_PC)
03161     {
03162       inst_env->invalid = 1;
03163       return;
03164     }
03165   inst_env->slot_needed = 0;
03166   inst_env->prefix_found = 0;
03167   inst_env->xflag_found = 0;
03168   inst_env->disable_interrupt = 0;
03169 }
03170 
03171 /* Handles the MULU instruction.  */
03172 
03173 static void 
03174 mulu_op (unsigned short inst, inst_env_type *inst_env)
03175 {
03176   /* MULS/U can't have a prefix.  */
03177   if (inst_env->prefix_found)
03178     {
03179       inst_env->invalid = 1;
03180       return;
03181     }
03182 
03183   /* Consider it invalid if the PC is the target.  */
03184   if (cris_get_operand2 (inst) == REG_PC)
03185     {
03186       inst_env->invalid = 1;
03187       return;
03188     }
03189   inst_env->slot_needed = 0;
03190   inst_env->prefix_found = 0;
03191   inst_env->xflag_found = 0;
03192   inst_env->disable_interrupt = 0;
03193 }
03194 
03195 /* Calculate the result of the instruction for ADD, SUB, CMP AND, OR and MOVE.
03196    The MOVE instruction is the move from source to register.  */
03197 
03198 static void 
03199 add_sub_cmp_and_or_move_action (unsigned short inst, inst_env_type *inst_env, 
03200                                 unsigned long source1, unsigned long source2)
03201 {
03202   unsigned long pc_mask;
03203   unsigned long operation_mask;
03204   
03205   /* Find out how many bits the operation should apply to.  */
03206   if (cris_get_size (inst) == INST_BYTE_SIZE)
03207     {
03208       pc_mask = 0xFFFFFF00; 
03209       operation_mask = 0xFF;
03210     }
03211   else if (cris_get_size (inst) == INST_WORD_SIZE)
03212     {
03213       pc_mask = 0xFFFF0000;
03214       operation_mask = 0xFFFF;
03215     }
03216   else if (cris_get_size (inst) == INST_DWORD_SIZE)
03217     {
03218       pc_mask = 0x0;
03219       operation_mask = 0xFFFFFFFF;
03220     }
03221   else
03222     {
03223       /* The size is out of range.  */
03224       inst_env->invalid = 1;
03225       return;
03226     }
03227 
03228   /* The instruction just works on uw_operation_mask bits.  */
03229   source2 &= operation_mask;
03230   source1 &= operation_mask;
03231 
03232   /* Now calculate the result.  The opcode's 3 first bits separates
03233      the different actions.  */
03234   switch (cris_get_opcode (inst) & 7)
03235     {
03236     case 0:  /* add */
03237       source1 += source2;
03238       break;
03239 
03240     case 1:  /* move */
03241       source1 = source2;
03242       break;
03243 
03244     case 2:  /* subtract */
03245       source1 -= source2;
03246       break;
03247 
03248     case 3:  /* compare */
03249       break;
03250 
03251     case 4:  /* and */
03252       source1 &= source2;
03253       break;
03254 
03255     case 5:  /* or */
03256       source1 |= source2;
03257       break;
03258 
03259     default:
03260       inst_env->invalid = 1;
03261       return;
03262 
03263       break;
03264     }
03265 
03266   /* Make sure that the result doesn't contain more than the instruction
03267      size bits.  */
03268   source2 &= operation_mask;
03269 
03270   /* Calculate the new breakpoint address.  */
03271   inst_env->reg[REG_PC] &= pc_mask;
03272   inst_env->reg[REG_PC] |= source1;
03273 
03274 }
03275 
03276 /* Extends the value from either byte or word size to a dword.  If the mode
03277    is zero extend then the value is extended with zero.  If instead the mode
03278    is signed extend the sign bit of the value is taken into consideration.  */
03279 
03280 static unsigned long 
03281 do_sign_or_zero_extend (unsigned long value, unsigned short *inst)
03282 {
03283   /* The size can be either byte or word, check which one it is. 
03284      Don't check the highest bit, it's indicating if it's a zero
03285      or sign extend.  */
03286   if (cris_get_size (*inst) & INST_WORD_SIZE)
03287     {
03288       /* Word size.  */
03289       value &= 0xFFFF;
03290 
03291       /* Check if the instruction is signed extend.  If so, check if value has
03292          the sign bit on.  */
03293       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_WORD_MASK))
03294         {
03295           value |= SIGNED_WORD_EXTEND_MASK;
03296         } 
03297     }
03298   else
03299     {
03300       /* Byte size.  */
03301       value &= 0xFF;
03302 
03303       /* Check if the instruction is signed extend.  If so, check if value has
03304          the sign bit on.  */
03305       if (cris_is_signed_extend_bit_on (*inst) && (value & SIGNED_BYTE_MASK))
03306         {
03307           value |= SIGNED_BYTE_EXTEND_MASK;
03308         }
03309     }
03310   /* The size should now be dword.  */
03311   cris_set_size_to_dword (inst);
03312   return value;
03313 }
03314 
03315 /* Handles the register mode for the ADD, SUB, CMP, AND, OR and MOVE
03316    instruction.  The MOVE instruction is the move from source to register.  */
03317 
03318 static void 
03319 reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst,
03320                                      inst_env_type *inst_env)
03321 {
03322   unsigned long operand1;
03323   unsigned long operand2;
03324 
03325   /* It's invalid to have a prefix to the instruction.  This is a register 
03326      mode instruction and can't have a prefix.  */
03327   if (inst_env->prefix_found)
03328     {
03329       inst_env->invalid = 1;
03330       return;
03331     }
03332   /* Check if the instruction has PC as its target.  */
03333   if (cris_get_operand2 (inst) == REG_PC)
03334     {
03335       if (inst_env->slot_needed)
03336         {
03337           inst_env->invalid = 1;
03338           return;
03339         }
03340       /* The instruction has the PC as its target register.  */
03341       operand1 = inst_env->reg[cris_get_operand1 (inst)]; 
03342       operand2 = inst_env->reg[REG_PC];
03343 
03344       /* Check if it's a extend, signed or zero instruction.  */
03345       if (cris_get_opcode (inst) < 4)
03346         {
03347           operand1 = do_sign_or_zero_extend (operand1, &inst);
03348         }
03349       /* Calculate the PC value after the instruction, i.e. where the
03350          breakpoint should be.  The order of the udw_operands is vital.  */
03351       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1); 
03352     }
03353   inst_env->slot_needed = 0;
03354   inst_env->prefix_found = 0;
03355   inst_env->xflag_found = 0;
03356   inst_env->disable_interrupt = 0;
03357 }
03358 
03359 /* Returns the data contained at address.  The size of the data is derived from
03360    the size of the operation.  If the instruction is a zero or signed
03361    extend instruction, the size field is changed in instruction.  */
03362 
03363 static unsigned long 
03364 get_data_from_address (unsigned short *inst, CORE_ADDR address,
03365                        enum bfd_endian byte_order)
03366 {
03367   int size = cris_get_size (*inst);
03368   unsigned long value;
03369 
03370   /* If it's an extend instruction we don't want the signed extend bit,
03371      because it influences the size.  */
03372   if (cris_get_opcode (*inst) < 4)
03373     {
03374       size &= ~SIGNED_EXTEND_BIT_MASK;
03375     }
03376   /* Is there a need for checking the size?  Size should contain the number of
03377      bytes to read.  */
03378   size = 1 << size;
03379   value = read_memory_unsigned_integer (address, size, byte_order);
03380 
03381   /* Check if it's an extend, signed or zero instruction.  */
03382   if (cris_get_opcode (*inst) < 4)
03383     {
03384       value = do_sign_or_zero_extend (value, inst);
03385     }
03386   return value;
03387 }
03388 
03389 /* Handles the assign addresing mode for the ADD, SUB, CMP, AND, OR and MOVE 
03390    instructions.  The MOVE instruction is the move from source to register.  */
03391 
03392 static void 
03393 handle_prefix_assign_mode_for_aritm_op (unsigned short inst, 
03394                                         inst_env_type *inst_env)
03395 {
03396   unsigned long operand2;
03397   unsigned long operand3;
03398 
03399   check_assign (inst, inst_env);
03400   if (cris_get_operand2 (inst) == REG_PC)
03401     {
03402       operand2 = inst_env->reg[REG_PC];
03403 
03404       /* Get the value of the third operand.  */
03405       operand3 = get_data_from_address (&inst, inst_env->prefix_value,
03406                                         inst_env->byte_order);
03407 
03408       /* Calculate the PC value after the instruction, i.e. where the
03409          breakpoint should be.  The order of the udw_operands is vital.  */
03410       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
03411     }
03412   inst_env->slot_needed = 0;
03413   inst_env->prefix_found = 0;
03414   inst_env->xflag_found = 0;
03415   inst_env->disable_interrupt = 0;
03416 }
03417 
03418 /* Handles the three-operand addressing mode for the ADD, SUB, CMP, AND and
03419    OR instructions.  Note that for this to work as expected, the calling
03420    function must have made sure that there is a prefix to this instruction.  */
03421 
03422 static void 
03423 three_operand_add_sub_cmp_and_or_op (unsigned short inst, 
03424                                      inst_env_type *inst_env)
03425 {
03426   unsigned long operand2;
03427   unsigned long operand3;
03428 
03429   if (cris_get_operand1 (inst) == REG_PC)
03430     {
03431       /* The PC will be changed by the instruction.  */
03432       operand2 = inst_env->reg[cris_get_operand2 (inst)];
03433 
03434       /* Get the value of the third operand.  */
03435       operand3 = get_data_from_address (&inst, inst_env->prefix_value,
03436                                         inst_env->byte_order);
03437 
03438       /* Calculate the PC value after the instruction, i.e. where the
03439          breakpoint should be.  */
03440       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3);
03441     }
03442   inst_env->slot_needed = 0;
03443   inst_env->prefix_found = 0;
03444   inst_env->xflag_found = 0;
03445   inst_env->disable_interrupt = 0;
03446 }
03447 
03448 /* Handles the index addresing mode for the ADD, SUB, CMP, AND, OR and MOVE
03449    instructions.  The MOVE instruction is the move from source to register.  */
03450 
03451 static void 
03452 handle_prefix_index_mode_for_aritm_op (unsigned short inst, 
03453                                        inst_env_type *inst_env)
03454 {
03455   if (cris_get_operand1 (inst) != cris_get_operand2 (inst))
03456     {
03457       /* If the instruction is MOVE it's invalid.  If the instruction is ADD,
03458          SUB, AND or OR something weird is going on (if everything works these
03459          instructions should end up in the three operand version).  */
03460       inst_env->invalid = 1;
03461       return;
03462     }
03463   else
03464     {
03465       /* three_operand_add_sub_cmp_and_or does the same as we should do here
03466          so use it.  */
03467       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
03468     }
03469   inst_env->slot_needed = 0;
03470   inst_env->prefix_found = 0;
03471   inst_env->xflag_found = 0;
03472   inst_env->disable_interrupt = 0;
03473 }
03474 
03475 /* Handles the autoincrement and indirect addresing mode for the ADD, SUB,
03476    CMP, AND OR and MOVE instruction.  The MOVE instruction is the move from
03477    source to register.  */
03478 
03479 static void 
03480 handle_inc_and_index_mode_for_aritm_op (unsigned short inst, 
03481                                         inst_env_type *inst_env)
03482 {
03483   unsigned long operand1;
03484   unsigned long operand2;
03485   unsigned long operand3;
03486   int size;
03487 
03488   /* The instruction is either an indirect or autoincrement addressing mode.
03489      Check if the destination register is the PC.  */
03490   if (cris_get_operand2 (inst) == REG_PC)
03491     {
03492       /* Must be done here, get_data_from_address may change the size 
03493          field.  */
03494       size = cris_get_size (inst);
03495       operand2 = inst_env->reg[REG_PC];
03496 
03497       /* Get the value of the third operand, i.e. the indirect operand.  */
03498       operand1 = inst_env->reg[cris_get_operand1 (inst)];
03499       operand3 = get_data_from_address (&inst, operand1, inst_env->byte_order);
03500 
03501       /* Calculate the PC value after the instruction, i.e. where the
03502          breakpoint should be.  The order of the udw_operands is vital.  */
03503       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand3); 
03504     }
03505   /* If this is an autoincrement addressing mode, check if the increment
03506      changes the PC.  */
03507   if ((cris_get_operand1 (inst) == REG_PC)
03508       && (cris_get_mode (inst) == AUTOINC_MODE))
03509     {
03510       /* Get the size field.  */
03511       size = cris_get_size (inst);
03512 
03513       /* If it's an extend instruction we don't want the signed extend bit,
03514          because it influences the size.  */
03515       if (cris_get_opcode (inst) < 4)
03516         {
03517           size &= ~SIGNED_EXTEND_BIT_MASK;
03518         }
03519       process_autoincrement (size, inst, inst_env);
03520     } 
03521   inst_env->slot_needed = 0;
03522   inst_env->prefix_found = 0;
03523   inst_env->xflag_found = 0;
03524   inst_env->disable_interrupt = 0;
03525 }
03526 
03527 /* Handles the two-operand addressing mode, all modes except register, for
03528    the ADD, SUB CMP, AND and OR instruction.  */
03529 
03530 static void 
03531 none_reg_mode_add_sub_cmp_and_or_move_op (unsigned short inst, 
03532                                           inst_env_type *inst_env)
03533 {
03534   if (inst_env->prefix_found)
03535     {
03536       if (cris_get_mode (inst) == PREFIX_INDEX_MODE)
03537         {
03538           handle_prefix_index_mode_for_aritm_op (inst, inst_env);
03539         }
03540       else if (cris_get_mode (inst) == PREFIX_ASSIGN_MODE)
03541         {
03542           handle_prefix_assign_mode_for_aritm_op (inst, inst_env);
03543         }
03544       else
03545         {
03546           /* The mode is invalid for a prefixed base instruction.  */
03547           inst_env->invalid = 1;
03548           return;
03549         }
03550     }
03551   else
03552     {
03553       handle_inc_and_index_mode_for_aritm_op (inst, inst_env);
03554     }
03555 }
03556 
03557 /* Handles the quick addressing mode for the ADD and SUB instruction.  */
03558 
03559 static void 
03560 quick_mode_add_sub_op (unsigned short inst, inst_env_type *inst_env)
03561 {
03562   unsigned long operand1;
03563   unsigned long operand2;
03564 
03565   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
03566      instruction and can't have a prefix.  */
03567   if (inst_env->prefix_found)
03568     {
03569       inst_env->invalid = 1;
03570       return;
03571     }
03572 
03573   /* Check if the instruction has PC as its target.  */
03574   if (cris_get_operand2 (inst) == REG_PC)
03575     {
03576       if (inst_env->slot_needed)
03577         {
03578           inst_env->invalid = 1;
03579           return;
03580         }
03581       operand1 = cris_get_quick_value (inst);
03582       operand2 = inst_env->reg[REG_PC];
03583 
03584       /* The size should now be dword.  */
03585       cris_set_size_to_dword (&inst);
03586 
03587       /* Calculate the PC value after the instruction, i.e. where the
03588          breakpoint should be.  */
03589       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
03590     }
03591   inst_env->slot_needed = 0;
03592   inst_env->prefix_found = 0;
03593   inst_env->xflag_found = 0;
03594   inst_env->disable_interrupt = 0;
03595 }
03596 
03597 /* Handles the quick addressing mode for the CMP, AND and OR instruction.  */
03598 
03599 static void 
03600 quick_mode_and_cmp_move_or_op (unsigned short inst, inst_env_type *inst_env)
03601 {
03602   unsigned long operand1;
03603   unsigned long operand2;
03604 
03605   /* It's a bad idea to be in a prefix instruction now.  This is a quick mode
03606      instruction and can't have a prefix.  */
03607   if (inst_env->prefix_found)
03608     {
03609       inst_env->invalid = 1;
03610       return;
03611     }
03612   /* Check if the instruction has PC as its target.  */
03613   if (cris_get_operand2 (inst) == REG_PC)
03614     {
03615       if (inst_env->slot_needed)
03616         {
03617           inst_env->invalid = 1;
03618           return;
03619         }
03620       /* The instruction has the PC as its target register.  */
03621       operand1 = cris_get_quick_value (inst);
03622       operand2 = inst_env->reg[REG_PC];
03623 
03624       /* The quick value is signed, so check if we must do a signed extend.  */
03625       if (operand1 & SIGNED_QUICK_VALUE_MASK)
03626         {
03627           /* sign extend  */
03628           operand1 |= SIGNED_QUICK_VALUE_EXTEND_MASK;
03629         }
03630       /* The size should now be dword.  */
03631       cris_set_size_to_dword (&inst);
03632 
03633       /* Calculate the PC value after the instruction, i.e. where the
03634          breakpoint should be.  */
03635       add_sub_cmp_and_or_move_action (inst, inst_env, operand2, operand1);
03636     }
03637   inst_env->slot_needed = 0;
03638   inst_env->prefix_found = 0;
03639   inst_env->xflag_found = 0;
03640   inst_env->disable_interrupt = 0;
03641 }
03642 
03643 /* Translate op_type to a function and call it.  */
03644 
03645 static void
03646 cris_gdb_func (struct gdbarch *gdbarch, enum cris_op_type op_type,
03647                unsigned short inst, inst_env_type *inst_env)
03648 {
03649   switch (op_type)
03650     {
03651     case cris_not_implemented_op:
03652       not_implemented_op (inst, inst_env);
03653       break;
03654 
03655     case cris_abs_op:
03656       abs_op (inst, inst_env);
03657       break;
03658 
03659     case cris_addi_op:
03660       addi_op (inst, inst_env);
03661       break;
03662 
03663     case cris_asr_op:
03664       asr_op (inst, inst_env);
03665       break;
03666 
03667     case cris_asrq_op:
03668       asrq_op (inst, inst_env);
03669       break;
03670 
03671     case cris_ax_ei_setf_op:
03672       ax_ei_setf_op (inst, inst_env);
03673       break;
03674 
03675     case cris_bdap_prefix:
03676       bdap_prefix (inst, inst_env);
03677       break;
03678 
03679     case cris_biap_prefix:
03680       biap_prefix (inst, inst_env);
03681       break;
03682 
03683     case cris_break_op:
03684       break_op (inst, inst_env);
03685       break;
03686 
03687     case cris_btst_nop_op:
03688       btst_nop_op (inst, inst_env);
03689       break;
03690 
03691     case cris_clearf_di_op:
03692       clearf_di_op (inst, inst_env);
03693       break;
03694 
03695     case cris_dip_prefix:
03696       dip_prefix (inst, inst_env);
03697       break;
03698 
03699     case cris_dstep_logshift_mstep_neg_not_op:
03700       dstep_logshift_mstep_neg_not_op (inst, inst_env);
03701       break;
03702 
03703     case cris_eight_bit_offset_branch_op:
03704       eight_bit_offset_branch_op (inst, inst_env);
03705       break;
03706 
03707     case cris_move_mem_to_reg_movem_op:
03708       move_mem_to_reg_movem_op (inst, inst_env);
03709       break;
03710 
03711     case cris_move_reg_to_mem_movem_op:
03712       move_reg_to_mem_movem_op (inst, inst_env);
03713       break;
03714 
03715     case cris_move_to_preg_op:
03716       move_to_preg_op (gdbarch, inst, inst_env);
03717       break;
03718 
03719     case cris_muls_op:
03720       muls_op (inst, inst_env);
03721       break;
03722 
03723     case cris_mulu_op:
03724       mulu_op (inst, inst_env);
03725       break;
03726 
03727     case cris_none_reg_mode_add_sub_cmp_and_or_move_op:
03728       none_reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
03729       break;
03730 
03731     case cris_none_reg_mode_clear_test_op:
03732       none_reg_mode_clear_test_op (inst, inst_env);
03733       break;
03734 
03735     case cris_none_reg_mode_jump_op:
03736       none_reg_mode_jump_op (inst, inst_env);
03737       break;
03738 
03739     case cris_none_reg_mode_move_from_preg_op:
03740       none_reg_mode_move_from_preg_op (gdbarch, inst, inst_env);
03741       break;
03742 
03743     case cris_quick_mode_add_sub_op:
03744       quick_mode_add_sub_op (inst, inst_env);
03745       break;
03746 
03747     case cris_quick_mode_and_cmp_move_or_op:
03748       quick_mode_and_cmp_move_or_op (inst, inst_env);
03749       break;
03750 
03751     case cris_quick_mode_bdap_prefix:
03752       quick_mode_bdap_prefix (inst, inst_env);
03753       break;
03754 
03755     case cris_reg_mode_add_sub_cmp_and_or_move_op:
03756       reg_mode_add_sub_cmp_and_or_move_op (inst, inst_env);
03757       break;
03758 
03759     case cris_reg_mode_clear_op:
03760       reg_mode_clear_op (inst, inst_env);
03761       break;
03762 
03763     case cris_reg_mode_jump_op:
03764       reg_mode_jump_op (inst, inst_env);
03765       break;
03766 
03767     case cris_reg_mode_move_from_preg_op:
03768       reg_mode_move_from_preg_op (inst, inst_env);
03769       break;
03770 
03771     case cris_reg_mode_test_op:
03772       reg_mode_test_op (inst, inst_env);
03773       break;
03774 
03775     case cris_scc_op:
03776       scc_op (inst, inst_env);
03777       break;
03778 
03779     case cris_sixteen_bit_offset_branch_op:
03780       sixteen_bit_offset_branch_op (inst, inst_env);
03781       break;
03782 
03783     case cris_three_operand_add_sub_cmp_and_or_op:
03784       three_operand_add_sub_cmp_and_or_op (inst, inst_env);
03785       break;
03786 
03787     case cris_three_operand_bound_op:
03788       three_operand_bound_op (inst, inst_env);
03789       break;
03790 
03791     case cris_two_operand_bound_op:
03792       two_operand_bound_op (inst, inst_env);
03793       break;
03794 
03795     case cris_xor_op:
03796       xor_op (inst, inst_env);
03797       break;
03798     }
03799 }
03800 
03801 /* This wrapper is to avoid cris_get_assembler being called before 
03802    exec_bfd has been set.  */
03803 
03804 static int
03805 cris_delayed_get_disassembler (bfd_vma addr, struct disassemble_info *info)
03806 {
03807   int (*print_insn) (bfd_vma addr, struct disassemble_info *info);
03808   /* FIXME: cagney/2003-08-27: It should be possible to select a CRIS
03809      disassembler, even when there is no BFD.  Does something like
03810      "gdb; target remote; disassmeble *0x123" work?  */
03811   gdb_assert (exec_bfd != NULL);
03812   print_insn = cris_get_disassembler (exec_bfd);
03813   gdb_assert (print_insn != NULL);
03814   return print_insn (addr, info);
03815 }
03816 
03817 /* Originally from <asm/elf.h>.  */
03818 typedef unsigned char cris_elf_greg_t[4];
03819 
03820 /* Same as user_regs_struct struct in <asm/user.h>.  */
03821 #define CRISV10_ELF_NGREG 35
03822 typedef cris_elf_greg_t cris_elf_gregset_t[CRISV10_ELF_NGREG];
03823 
03824 #define CRISV32_ELF_NGREG 32
03825 typedef cris_elf_greg_t crisv32_elf_gregset_t[CRISV32_ELF_NGREG];
03826 
03827 /* Unpack a cris_elf_gregset_t into GDB's register cache.  */
03828 
03829 static void 
03830 cris_supply_gregset (struct regcache *regcache, cris_elf_gregset_t *gregsetp)
03831 {
03832   struct gdbarch *gdbarch = get_regcache_arch (regcache);
03833   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03834   int i;
03835   cris_elf_greg_t *regp = *gregsetp;
03836   static char zerobuf[4] = {0};
03837 
03838   /* The kernel dumps all 32 registers as unsigned longs, but supply_register
03839      knows about the actual size of each register so that's no problem.  */
03840   for (i = 0; i < NUM_GENREGS + NUM_SPECREGS; i++)
03841     {
03842       regcache_raw_supply (regcache, i, (char *)&regp[i]);
03843     }
03844 
03845   if (tdep->cris_version == 32)
03846     {
03847       /* Needed to set pseudo-register PC for CRISv32.  */
03848       /* FIXME: If ERP is in a delay slot at this point then the PC will
03849          be wrong.  Issue a warning to alert the user.  */
03850       regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch),
03851                            (char *)&regp[ERP_REGNUM]);
03852 
03853       if (*(char *)&regp[ERP_REGNUM] & 0x1)
03854         fprintf_unfiltered (gdb_stderr, "Warning: PC in delay slot\n");
03855     }
03856 }
03857 
03858 /*  Use a local version of this function to get the correct types for
03859     regsets, until multi-arch core support is ready.  */
03860 
03861 static void
03862 fetch_core_registers (struct regcache *regcache,
03863                       char *core_reg_sect, unsigned core_reg_size,
03864                       int which, CORE_ADDR reg_addr)
03865 {
03866   cris_elf_gregset_t gregset;
03867 
03868   switch (which)
03869     {
03870     case 0:
03871       if (core_reg_size != sizeof (cris_elf_gregset_t)
03872           && core_reg_size != sizeof (crisv32_elf_gregset_t))
03873         {
03874           warning (_("wrong size gregset struct in core file"));
03875         }
03876       else
03877         {
03878           memcpy (&gregset, core_reg_sect, sizeof (gregset));
03879           cris_supply_gregset (regcache, &gregset);
03880         }
03881 
03882     default:
03883       /* We've covered all the kinds of registers we know about here,
03884          so this must be something we wouldn't know what to do with
03885          anyway.  Just ignore it.  */
03886       break;
03887     }
03888 }
03889 
03890 static struct core_fns cris_elf_core_fns =
03891 {
03892   bfd_target_elf_flavour,               /* core_flavour */
03893   default_check_format,                 /* check_format */
03894   default_core_sniffer,                 /* core_sniffer */
03895   fetch_core_registers,                 /* core_read_registers */
03896   NULL                                  /* next */
03897 };
03898 
03899 extern initialize_file_ftype _initialize_cris_tdep; /* -Wmissing-prototypes */
03900 
03901 void
03902 _initialize_cris_tdep (void)
03903 {
03904   static struct cmd_list_element *cris_set_cmdlist;
03905   static struct cmd_list_element *cris_show_cmdlist;
03906 
03907   struct cmd_list_element *c;
03908 
03909   gdbarch_register (bfd_arch_cris, cris_gdbarch_init, cris_dump_tdep);
03910   
03911   /* CRIS-specific user-commands.  */
03912   add_setshow_zuinteger_cmd ("cris-version", class_support,
03913                              &usr_cmd_cris_version,
03914                              _("Set the current CRIS version."),
03915                              _("Show the current CRIS version."),
03916                              _("\
03917 Set to 10 for CRISv10 or 32 for CRISv32 if autodetection fails.\n\
03918 Defaults to 10. "),
03919                              set_cris_version,
03920                              NULL, /* FIXME: i18n: Current CRIS version
03921                                       is %s.  */
03922                              &setlist, &showlist);
03923 
03924   add_setshow_enum_cmd ("cris-mode", class_support, 
03925                         cris_modes, &usr_cmd_cris_mode, 
03926                         _("Set the current CRIS mode."),
03927                         _("Show the current CRIS mode."),
03928                         _("\
03929 Set to CRIS_MODE_GURU when debugging in guru mode.\n\
03930 Makes GDB use the NRP register instead of the ERP register in certain cases."),
03931                         set_cris_mode,
03932                         NULL, /* FIXME: i18n: Current CRIS version is %s.  */
03933                         &setlist, &showlist);
03934   
03935   add_setshow_boolean_cmd ("cris-dwarf2-cfi", class_support,
03936                            &usr_cmd_cris_dwarf2_cfi,
03937                            _("Set the usage of Dwarf-2 CFI for CRIS."),
03938                            _("Show the usage of Dwarf-2 CFI for CRIS."),
03939                            _("Set this to \"off\" if using gcc-cris < R59."),
03940                            set_cris_dwarf2_cfi,
03941                            NULL, /* FIXME: i18n: Usage of Dwarf-2 CFI
03942                                     for CRIS is %d.  */
03943                            &setlist, &showlist);
03944 
03945   deprecated_add_core_fns (&cris_elf_core_fns);
03946 }
03947 
03948 /* Prints out all target specific values.  */
03949 
03950 static void
03951 cris_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
03952 {
03953   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03954   if (tdep != NULL)
03955     {
03956       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_version = %i\n",
03957                           tdep->cris_version);
03958       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_mode = %s\n",
03959                           tdep->cris_mode);
03960       fprintf_unfiltered (file, "cris_dump_tdep: tdep->cris_dwarf2_cfi = %i\n",
03961                           tdep->cris_dwarf2_cfi);
03962     }
03963 }
03964 
03965 static void
03966 set_cris_version (char *ignore_args, int from_tty, 
03967                   struct cmd_list_element *c)
03968 {
03969   struct gdbarch_info info;
03970 
03971   usr_cmd_cris_version_valid = 1;
03972   
03973   /* Update the current architecture, if needed.  */
03974   gdbarch_info_init (&info);
03975   if (!gdbarch_update_p (info))
03976     internal_error (__FILE__, __LINE__, 
03977                     _("cris_gdbarch_update: failed to update architecture."));
03978 }
03979 
03980 static void
03981 set_cris_mode (char *ignore_args, int from_tty, 
03982                struct cmd_list_element *c)
03983 {
03984   struct gdbarch_info info;
03985 
03986   /* Update the current architecture, if needed.  */
03987   gdbarch_info_init (&info);
03988   if (!gdbarch_update_p (info))
03989     internal_error (__FILE__, __LINE__, 
03990                     "cris_gdbarch_update: failed to update architecture.");
03991 }
03992 
03993 static void
03994 set_cris_dwarf2_cfi (char *ignore_args, int from_tty, 
03995                      struct cmd_list_element *c)
03996 {
03997   struct gdbarch_info info;
03998 
03999   /* Update the current architecture, if needed.  */
04000   gdbarch_info_init (&info);
04001   if (!gdbarch_update_p (info))
04002     internal_error (__FILE__, __LINE__, 
04003                     _("cris_gdbarch_update: failed to update architecture."));
04004 }
04005 
04006 static struct gdbarch *
04007 cris_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
04008 {
04009   struct gdbarch *gdbarch;
04010   struct gdbarch_tdep *tdep;
04011   unsigned int cris_version;
04012 
04013   if (usr_cmd_cris_version_valid)
04014     {
04015       /* Trust the user's CRIS version setting.  */ 
04016       cris_version = usr_cmd_cris_version;
04017     }
04018   else if (info.abfd && bfd_get_mach (info.abfd) == bfd_mach_cris_v32)
04019     {
04020       cris_version = 32;
04021     }
04022   else
04023     {
04024       /* Assume it's CRIS version 10.  */
04025       cris_version = 10;
04026     }
04027 
04028   /* Make the current settings visible to the user.  */
04029   usr_cmd_cris_version = cris_version;
04030   
04031   /* Find a candidate among the list of pre-declared architectures.  */
04032   for (arches = gdbarch_list_lookup_by_info (arches, &info); 
04033        arches != NULL;
04034        arches = gdbarch_list_lookup_by_info (arches->next, &info))
04035     {
04036       if ((gdbarch_tdep (arches->gdbarch)->cris_version 
04037            == usr_cmd_cris_version)
04038           && (gdbarch_tdep (arches->gdbarch)->cris_mode 
04039            == usr_cmd_cris_mode)
04040           && (gdbarch_tdep (arches->gdbarch)->cris_dwarf2_cfi 
04041               == usr_cmd_cris_dwarf2_cfi))
04042         return arches->gdbarch;
04043     }
04044 
04045   /* No matching architecture was found.  Create a new one.  */
04046   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
04047   gdbarch = gdbarch_alloc (&info, tdep);
04048 
04049   tdep->cris_version = usr_cmd_cris_version;
04050   tdep->cris_mode = usr_cmd_cris_mode;
04051   tdep->cris_dwarf2_cfi = usr_cmd_cris_dwarf2_cfi;
04052 
04053   /* INIT shall ensure that the INFO.BYTE_ORDER is non-zero.  */
04054   switch (info.byte_order)
04055     {
04056     case BFD_ENDIAN_LITTLE:
04057       /* Ok.  */
04058       break;
04059 
04060     case BFD_ENDIAN_BIG:
04061       internal_error (__FILE__, __LINE__,
04062                       _("cris_gdbarch_init: big endian byte order in info"));
04063       break;
04064     
04065     default:
04066       internal_error (__FILE__, __LINE__,
04067                       _("cris_gdbarch_init: unknown byte order in info"));
04068     }
04069 
04070   set_gdbarch_return_value (gdbarch, cris_return_value);
04071 
04072   set_gdbarch_sp_regnum (gdbarch, 14);
04073   
04074   /* Length of ordinary registers used in push_word and a few other
04075      places.  register_size() is the real way to know how big a
04076      register is.  */
04077 
04078   set_gdbarch_double_bit (gdbarch, 64);
04079   /* The default definition of a long double is 2 * gdbarch_double_bit,
04080      which means we have to set this explicitly.  */
04081   set_gdbarch_long_double_bit (gdbarch, 64);
04082 
04083   /* The total amount of space needed to store (in an array called registers)
04084      GDB's copy of the machine's register state.  Note: We can not use
04085      cris_register_size at this point, since it relies on gdbarch
04086      being set.  */
04087   switch (tdep->cris_version)
04088     {
04089     case 0:
04090     case 1:
04091     case 2:
04092     case 3:
04093     case 8:
04094     case 9:
04095       /* Old versions; not supported.  */
04096       internal_error (__FILE__, __LINE__, 
04097                       _("cris_gdbarch_init: unsupported CRIS version"));
04098       break;
04099 
04100     case 10:
04101     case 11: 
04102       /* CRIS v10 and v11, a.k.a. ETRAX 100LX.  In addition to ETRAX 100, 
04103          P7 (32 bits), and P15 (32 bits) have been implemented.  */
04104       set_gdbarch_pc_regnum (gdbarch, 15);
04105       set_gdbarch_register_type (gdbarch, cris_register_type);
04106       /* There are 32 registers (some of which may not be implemented).  */
04107       set_gdbarch_num_regs (gdbarch, 32);
04108       set_gdbarch_register_name (gdbarch, cris_register_name);
04109       set_gdbarch_cannot_store_register (gdbarch, cris_cannot_store_register);
04110       set_gdbarch_cannot_fetch_register (gdbarch, cris_cannot_fetch_register);
04111 
04112       set_gdbarch_software_single_step (gdbarch, cris_software_single_step);
04113       break;
04114 
04115     case 32:
04116       /* CRIS v32.  General registers R0 - R15 (32 bits), special registers 
04117          P0 - P15 (32 bits) except P0, P1, P3 (8 bits) and P4 (16 bits)
04118          and pseudo-register PC (32 bits).  */
04119       set_gdbarch_pc_regnum (gdbarch, 32);
04120       set_gdbarch_register_type (gdbarch, crisv32_register_type);
04121       /* 32 registers + pseudo-register PC + 16 support registers.  */
04122       set_gdbarch_num_regs (gdbarch, 32 + 1 + 16);
04123       set_gdbarch_register_name (gdbarch, crisv32_register_name);
04124 
04125       set_gdbarch_cannot_store_register 
04126         (gdbarch, crisv32_cannot_store_register);
04127       set_gdbarch_cannot_fetch_register
04128         (gdbarch, crisv32_cannot_fetch_register);
04129 
04130       set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
04131 
04132       set_gdbarch_single_step_through_delay 
04133         (gdbarch, crisv32_single_step_through_delay);
04134 
04135       break;
04136 
04137     default:
04138       internal_error (__FILE__, __LINE__, 
04139                       _("cris_gdbarch_init: unknown CRIS version"));
04140     }
04141 
04142   /* Dummy frame functions (shared between CRISv10 and CRISv32 since they
04143      have the same ABI).  */
04144   set_gdbarch_push_dummy_code (gdbarch, cris_push_dummy_code);
04145   set_gdbarch_push_dummy_call (gdbarch, cris_push_dummy_call);
04146   set_gdbarch_frame_align (gdbarch, cris_frame_align);
04147   set_gdbarch_skip_prologue (gdbarch, cris_skip_prologue);
04148   
04149   /* The stack grows downward.  */
04150   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
04151 
04152   set_gdbarch_breakpoint_from_pc (gdbarch, cris_breakpoint_from_pc);
04153   
04154   set_gdbarch_unwind_pc (gdbarch, cris_unwind_pc);
04155   set_gdbarch_unwind_sp (gdbarch, cris_unwind_sp);
04156   set_gdbarch_dummy_id (gdbarch, cris_dummy_id);
04157 
04158   if (tdep->cris_dwarf2_cfi == 1)
04159     {
04160       /* Hook in the Dwarf-2 frame sniffer.  */
04161       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, cris_dwarf2_reg_to_regnum);
04162       dwarf2_frame_set_init_reg (gdbarch, cris_dwarf2_frame_init_reg);
04163       dwarf2_append_unwinders (gdbarch);
04164     }
04165 
04166   if (tdep->cris_mode != cris_mode_guru)
04167     {
04168       frame_unwind_append_unwinder (gdbarch, &cris_sigtramp_frame_unwind);
04169     }
04170 
04171   frame_unwind_append_unwinder (gdbarch, &cris_frame_unwind);
04172   frame_base_set_default (gdbarch, &cris_frame_base);
04173 
04174   /* Hook in ABI-specific overrides, if they have been registered.  */
04175   gdbarch_init_osabi (info, gdbarch);
04176 
04177   /* FIXME: cagney/2003-08-27: It should be possible to select a CRIS
04178      disassembler, even when there is no BFD.  Does something like
04179      "gdb; target remote; disassmeble *0x123" work?  */
04180   set_gdbarch_print_insn (gdbarch, cris_delayed_get_disassembler);
04181 
04182   return gdbarch;
04183 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines