GDB (API)
/home/stan/gdb/src/gdb/nios2-tdep.c
Go to the documentation of this file.
00001 /* Target-machine dependent code for Nios II, for GDB.
00002    Copyright (C) 2012-2013 Free Software Foundation, Inc.
00003    Contributed by Peter Brookes (pbrookes@altera.com)
00004    and Andrew Draper (adraper@altera.com).
00005    Contributed by Mentor Graphics, Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "frame.h"
00024 #include "frame-unwind.h"
00025 #include "frame-base.h"
00026 #include "trad-frame.h"
00027 #include "dwarf2-frame.h"
00028 #include "symtab.h"
00029 #include "inferior.h"
00030 #include "gdbtypes.h"
00031 #include "gdbcore.h"
00032 #include "gdbcmd.h"
00033 #include "osabi.h"
00034 #include "target.h"
00035 #include "dis-asm.h"
00036 #include "regcache.h"
00037 #include "value.h"
00038 #include "symfile.h"
00039 #include "arch-utils.h"
00040 #include "floatformat.h"
00041 #include "gdb_assert.h"
00042 #include "infcall.h"
00043 #include "regset.h"
00044 #include "target-descriptions.h"
00045 
00046 /* To get entry_point_address.  */
00047 #include "objfiles.h"
00048 
00049 /* Nios II ISA specific encodings and macros.  */
00050 #include "opcode/nios2.h"
00051 
00052 /* Nios II specific header.  */
00053 #include "nios2-tdep.h"
00054 
00055 #include "features/nios2.c"
00056 
00057 /* Control debugging information emitted in this file.  */
00058 
00059 static int nios2_debug = 0;
00060 
00061 /* The following structures are used in the cache for prologue
00062    analysis; see the reg_value and reg_saved tables in
00063    struct nios2_unwind_cache, respectively.  */
00064 
00065 /* struct reg_value is used to record that a register has the same value
00066    as reg at the given offset from the start of a function.  */
00067 
00068 struct reg_value
00069 {
00070   int reg;
00071   unsigned int offset;
00072 };
00073 
00074 /* struct reg_saved is used to record that a register value has been saved at
00075    basereg + addr, for basereg >= 0.  If basereg < 0, that indicates
00076    that the register is not known to have been saved.  Note that when
00077    basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
00078    addr is an absolute address.  */
00079 
00080 struct reg_saved
00081 {
00082   int basereg;
00083   CORE_ADDR addr;
00084 };
00085 
00086 struct nios2_unwind_cache
00087 {
00088   /* The frame's base, optionally used by the high-level debug info.  */
00089   CORE_ADDR base;
00090 
00091   /* The previous frame's inner most stack address.  Used as this
00092      frame ID's stack_addr.  */
00093   CORE_ADDR cfa;
00094 
00095   /* The address of the first instruction in this function.  */
00096   CORE_ADDR pc;
00097 
00098   /* Which register holds the return address for the frame.  */
00099   int return_regnum;
00100 
00101   /* Table indicating what changes have been made to each register.  */
00102   struct reg_value reg_value[NIOS2_NUM_REGS];
00103 
00104   /* Table indicating where each register has been saved.  */
00105   struct reg_saved reg_saved[NIOS2_NUM_REGS];
00106 };
00107 
00108 
00109 /* This array is a mapping from Dwarf-2 register numbering to GDB's.  */
00110 
00111 static int nios2_dwarf2gdb_regno_map[] =
00112 {
00113   0, 1, 2, 3,
00114   4, 5, 6, 7,
00115   8, 9, 10, 11,
00116   12, 13, 14, 15,
00117   16, 17, 18, 19,
00118   20, 21, 22, 23,
00119   24, 25,
00120   NIOS2_GP_REGNUM,        /* 26 */
00121   NIOS2_SP_REGNUM,        /* 27 */
00122   NIOS2_FP_REGNUM,        /* 28 */
00123   NIOS2_EA_REGNUM,        /* 29 */
00124   NIOS2_BA_REGNUM,        /* 30 */
00125   NIOS2_RA_REGNUM,        /* 31 */
00126   NIOS2_PC_REGNUM,        /* 32 */
00127   NIOS2_STATUS_REGNUM,    /* 33 */
00128   NIOS2_ESTATUS_REGNUM,   /* 34 */
00129   NIOS2_BSTATUS_REGNUM,   /* 35 */
00130   NIOS2_IENABLE_REGNUM,   /* 36 */
00131   NIOS2_IPENDING_REGNUM,  /* 37 */
00132   NIOS2_CPUID_REGNUM,     /* 38 */
00133   39, /* CTL6 */          /* 39 */
00134   NIOS2_EXCEPTION_REGNUM, /* 40 */
00135   NIOS2_PTEADDR_REGNUM,   /* 41 */
00136   NIOS2_TLBACC_REGNUM,    /* 42 */
00137   NIOS2_TLBMISC_REGNUM,   /* 43 */
00138   NIOS2_ECCINJ_REGNUM,    /* 44 */
00139   NIOS2_BADADDR_REGNUM,   /* 45 */
00140   NIOS2_CONFIG_REGNUM,    /* 46 */
00141   NIOS2_MPUBASE_REGNUM,   /* 47 */
00142   NIOS2_MPUACC_REGNUM     /* 48 */
00143 };
00144 
00145 
00146 /* Implement the dwarf2_reg_to_regnum gdbarch method.  */
00147 
00148 static int
00149 nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
00150 {
00151   if (dw_reg < 0 || dw_reg > NIOS2_NUM_REGS)
00152     {
00153       warning (_("Dwarf-2 uses unmapped register #%d"), dw_reg);
00154       return dw_reg;
00155     }
00156 
00157   return nios2_dwarf2gdb_regno_map[dw_reg];
00158 }
00159 
00160 /* Canonical names for the 49 registers.  */
00161 
00162 static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
00163 {
00164   "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
00165   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
00166   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
00167   "et", "bt", "gp", "sp", "fp", "ea", "ba", "ra",
00168   "pc",
00169   "status", "estatus", "bstatus", "ienable",
00170   "ipending", "cpuid", "ctl6", "exception",
00171   "pteaddr", "tlbacc", "tlbmisc", "eccinj",
00172   "badaddr", "config", "mpubase", "mpuacc"
00173 };
00174 
00175 /* Implement the register_name gdbarch method.  */
00176 
00177 static const char *
00178 nios2_register_name (struct gdbarch *gdbarch, int regno)
00179 {
00180   /* Use mnemonic aliases for GPRs.  */
00181   if (regno >= 0 && regno < NIOS2_NUM_REGS)
00182     return nios2_reg_names[regno];
00183   else
00184     return tdesc_register_name (gdbarch, regno);
00185 }
00186 
00187 /* Implement the register_type gdbarch method.  */
00188 
00189 static struct type *
00190 nios2_register_type (struct gdbarch *gdbarch, int regno)
00191 {
00192   /* If the XML description has register information, use that to
00193      determine the register type.  */
00194   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
00195     return tdesc_register_type (gdbarch, regno);
00196 
00197   if (regno == NIOS2_PC_REGNUM)
00198     return builtin_type (gdbarch)->builtin_func_ptr;
00199   else if (regno == NIOS2_SP_REGNUM)
00200     return builtin_type (gdbarch)->builtin_data_ptr;
00201   else
00202     return builtin_type (gdbarch)->builtin_uint32;
00203 }
00204 
00205 /* Given a return value in REGCACHE with a type VALTYPE,
00206    extract and copy its value into VALBUF.  */
00207 
00208 static void
00209 nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
00210                             struct regcache *regcache, gdb_byte *valbuf)
00211 {
00212   int len = TYPE_LENGTH (valtype);
00213 
00214   /* Return values of up to 8 bytes are returned in $r2 $r3.  */
00215   if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
00216     regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
00217   else
00218     {
00219       gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
00220                           + register_size (gdbarch, NIOS2_R3_REGNUM)));
00221       regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
00222       regcache_cooked_read (regcache, NIOS2_R3_REGNUM, valbuf + 4);
00223     }
00224 }
00225 
00226 /* Write into appropriate registers a function return value
00227    of type TYPE, given in virtual format.  */
00228 
00229 static void
00230 nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
00231                           struct regcache *regcache, const gdb_byte *valbuf)
00232 {
00233   int len = TYPE_LENGTH (valtype);
00234 
00235   /* Return values of up to 8 bytes are returned in $r2 $r3.  */
00236   if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
00237     regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
00238   else
00239     {
00240       gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
00241                           + register_size (gdbarch, NIOS2_R3_REGNUM)));
00242       regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
00243       regcache_cooked_write (regcache, NIOS2_R3_REGNUM, valbuf + 4);
00244     }
00245 }
00246 
00247 
00248 /* Set up the default values of the registers.  */
00249 
00250 static void
00251 nios2_setup_default (struct nios2_unwind_cache *cache)
00252 {
00253   int i;
00254 
00255   for (i = 0; i < NIOS2_NUM_REGS; i++)
00256   {
00257     /* All registers start off holding their previous values.  */
00258     cache->reg_value[i].reg    = i;
00259     cache->reg_value[i].offset = 0;
00260 
00261     /* All registers start off not saved.  */
00262     cache->reg_saved[i].basereg = -1;
00263     cache->reg_saved[i].addr    = 0;
00264   }
00265 }
00266 
00267 /* Initialize the unwind cache.  */
00268 
00269 static void
00270 nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
00271 {
00272   cache->base = 0;
00273   cache->cfa = 0;
00274   cache->pc = pc;
00275   cache->return_regnum = NIOS2_RA_REGNUM;
00276   nios2_setup_default (cache);
00277 }
00278 
00279 /* Helper function to identify when we're in a function epilogue;
00280    that is, the part of the function from the point at which the
00281    stack adjustment is made, to the return or sibcall.  On Nios II,
00282    we want to check that the CURRENT_PC is a return-type instruction
00283    and that the previous instruction is a stack adjustment.
00284    START_PC is the beginning of the function in question.  */
00285 
00286 static int
00287 nios2_in_epilogue_p (struct gdbarch *gdbarch,
00288                      CORE_ADDR current_pc,
00289                      CORE_ADDR start_pc)
00290 {
00291   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00292 
00293   /* There has to be a previous instruction in the function.  */
00294   if (current_pc > start_pc)
00295     {
00296 
00297       /* Check whether the previous instruction was a stack
00298          adjustment.  */
00299       unsigned int insn
00300         = read_memory_unsigned_integer (current_pc - NIOS2_OPCODE_SIZE,
00301                                         NIOS2_OPCODE_SIZE, byte_order);
00302 
00303       if ((insn & 0xffc0003c) == 0xdec00004     /* ADDI sp, sp, */
00304           || (insn & 0xffc1ffff) == 0xdec1883a  /* ADD  sp, sp, */
00305           || (insn & 0xffc0003f) == 0xdec00017) /* LDW  sp, constant(sp) */
00306         {
00307           /* Then check if it's followed by a return or a tail
00308              call.  */
00309           insn = read_memory_unsigned_integer (current_pc, NIOS2_OPCODE_SIZE,
00310                                                byte_order);
00311 
00312           if (insn == 0xf800283a                        /* RET */
00313               || insn == 0xe800083a                     /* ERET */
00314               || (insn & 0x07ffffff) == 0x0000683a      /* JMP */
00315               || (insn & 0xffc0003f) == 6)              /* BR */
00316             return 1;
00317         }
00318     }
00319   return 0;
00320 }
00321 
00322 /* Implement the in_function_epilogue_p gdbarch method.  */
00323 
00324 static int
00325 nios2_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
00326 {
00327   CORE_ADDR func_addr;
00328 
00329   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
00330     return nios2_in_epilogue_p (gdbarch, pc, func_addr);
00331 
00332   return 0;
00333 }
00334 
00335 /* Define some instruction patterns supporting wildcard bits via a
00336    mask.  */
00337 
00338 typedef struct
00339 {
00340   unsigned int insn;
00341   unsigned int mask;
00342 } wild_insn;
00343 
00344 static const wild_insn profiler_insn[] =
00345 {
00346   { 0x0010e03a, 0x00000000 }, /* nextpc r8 */
00347   { 0xf813883a, 0x00000000 }, /* mov    r9,ra */
00348   { 0x02800034, 0x003fffc0 }, /* movhi  r10,257 */
00349   { 0x52800004, 0x003fffc0 }, /* addi   r10,r10,-31992 */
00350   { 0x00000000, 0xffffffc0 }, /* call   <mcount> */
00351   { 0x483f883a, 0x00000000 }  /* mov    ra,r9 */
00352 };
00353 
00354 static const wild_insn irqentry_insn[] =
00355 {
00356   { 0x0031307a, 0x00000000 }, /* rdctl  et,estatus */
00357   { 0xc600004c, 0x00000000 }, /* andi   et,et,1 */
00358   { 0xc0000026, 0x003fffc0 }, /* beq    et,zero, <software_exception> */
00359   { 0x0031313a, 0x00000000 }, /* rdctl  et,ipending */
00360   { 0xc0000026, 0x003fffc0 }  /* beq    et,zero, <software_exception> */
00361 };
00362 
00363 
00364 /* Attempt to match SEQUENCE, which is COUNT insns long, at START_PC.  */
00365 
00366 static int
00367 nios2_match_sequence (struct gdbarch *gdbarch, CORE_ADDR start_pc,
00368                       const wild_insn *sequence, int count)
00369 {
00370   CORE_ADDR pc = start_pc;
00371   int i;
00372   unsigned int insn;
00373   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00374 
00375   for (i = 0 ; i < count ; i++)
00376     {
00377       insn = read_memory_unsigned_integer (pc, NIOS2_OPCODE_SIZE, byte_order);
00378       if ((insn & ~sequence[i].mask) != sequence[i].insn)
00379         return 0;
00380 
00381       pc += NIOS2_OPCODE_SIZE;
00382     }
00383 
00384   return 1;
00385 }
00386 
00387 /* Do prologue analysis, returning the PC of the first instruction
00388    after the function prologue.  Assumes CACHE has already been
00389    initialized.  THIS_FRAME can be null, in which case we are only
00390    interested in skipping the prologue.  Otherwise CACHE is filled in
00391    from the frame information.
00392 
00393    The prologue will consist of the following parts:
00394      1) Optional profiling instrumentation.  The old version uses six
00395         instructions.  We step over this if there is an exact match.
00396           nextpc r8
00397           mov    r9, ra
00398           movhi  r10, %hiadj(.LP2)
00399           addi   r10, r10, %lo(.LP2)
00400           call   mcount
00401           mov    ra, r9
00402         The new version uses two or three instructions (the last of
00403         these might get merged in with the STW which saves RA to the
00404         stack).  We interpret these.
00405           mov    r8, ra
00406           call   mcount
00407           mov    ra, r8
00408 
00409      2) Optional interrupt entry decision.  Again, we step over
00410         this if there is an exact match.
00411           rdctl  et,estatus
00412           andi   et,et,1
00413           beq    et,zero, <software_exception>
00414           rdctl  et,ipending
00415           beq    et,zero, <software_exception>
00416 
00417      3) A stack adjustment or stack which, which will be one of:
00418           addi   sp, sp, -constant
00419         or:
00420           movi   r8, constant
00421           sub    sp, sp, r8
00422         or
00423           movhi  r8, constant
00424           addi   r8, r8, constant
00425           sub    sp, sp, r8
00426         or
00427           movhi  rx, %hiadj(newstack)
00428           addhi  rx, rx, %lo(newstack)
00429           stw    sp, constant(rx)
00430           mov    sp, rx
00431 
00432      4) An optional stack check, which can take either of these forms:
00433           bgeu   sp, rx, +8
00434           break  3
00435         or
00436           bltu   sp, rx, .Lstack_overflow
00437           ...
00438         .Lstack_overflow:
00439           break  3
00440 
00441      5) Saving any registers which need to be saved.  These will
00442         normally just be stored onto the stack:
00443           stw    rx, constant(sp)
00444         but in the large frame case will use r8 as an offset back
00445         to the cfa:
00446           add    r8, r8, sp
00447           stw    rx, -constant(r8)
00448 
00449         Saving control registers looks slightly different:
00450           rdctl  rx, ctlN
00451           stw    rx, constant(sp)
00452 
00453      6) An optional FP setup, either if the user has requested a
00454         frame pointer or if the function calls alloca.
00455         This is always:
00456           mov    fp, sp
00457 
00458     The prologue instructions may be interleaved, and the register
00459     saves and FP setup can occur in either order.
00460 
00461     To cope with all this variability we decode all the instructions
00462     from the start of the prologue until we hit a branch, call or
00463     return.  For each of the instructions mentioned in 3, 4 and 5 we
00464     handle the limited cases of stores to the stack and operations
00465     on constant values.  */
00466 
00467 static CORE_ADDR
00468 nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
00469                         const CORE_ADDR current_pc,
00470                         struct nios2_unwind_cache *cache,
00471                         struct frame_info *this_frame)
00472 {
00473   /* Maximum lines of prologue to check.
00474      Note that this number should not be too large, else we can
00475      potentially end up iterating through unmapped memory.  */
00476   CORE_ADDR limit_pc = start_pc + 200;
00477   int regno;
00478   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00479 
00480   /* Does the frame set up the FP register?  */
00481   int base_reg = 0;
00482 
00483   struct reg_value *value = cache->reg_value;
00484   struct reg_value temp_value[NIOS2_NUM_REGS];
00485 
00486   int i;
00487 
00488   /* Save the starting PC so we can correct the pc after running
00489      through the prolog, using symbol info.  */
00490   CORE_ADDR pc = start_pc;
00491 
00492   /* Is this an exception handler?  */
00493   int exception_handler = 0;
00494 
00495   /* What was the original value of SP (or fake original value for
00496      functions which switch stacks?  */
00497   CORE_ADDR frame_high;
00498 
00499   /* Is this the end of the prologue?  */
00500   int within_prologue = 1;
00501 
00502   CORE_ADDR prologue_end;
00503 
00504   /* Is this the innermost function?  */
00505   int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);
00506 
00507   if (nios2_debug)
00508     fprintf_unfiltered (gdb_stdlog,
00509                         "{ nios2_analyze_prologue start=%s, current=%s ",
00510                         paddress (gdbarch, start_pc),
00511                         paddress (gdbarch, current_pc));
00512 
00513   /* Set up the default values of the registers.  */
00514   nios2_setup_default (cache);
00515 
00516   /* If the first few instructions are the profile entry, then skip
00517      over them.  Newer versions of the compiler use more efficient
00518      profiling code.  */
00519   if (nios2_match_sequence (gdbarch, pc, profiler_insn,
00520                             ARRAY_SIZE (profiler_insn)))
00521     pc += ARRAY_SIZE (profiler_insn) * NIOS2_OPCODE_SIZE;
00522 
00523   /* If the first few instructions are an interrupt entry, then skip
00524      over them too.  */
00525   if (nios2_match_sequence (gdbarch, pc, irqentry_insn,
00526                             ARRAY_SIZE (irqentry_insn)))
00527     {
00528       pc += ARRAY_SIZE (irqentry_insn) * NIOS2_OPCODE_SIZE;
00529       exception_handler = 1;
00530     }
00531 
00532   prologue_end = start_pc;
00533 
00534   /* Find the prologue instructions.  */
00535   while (pc < limit_pc && within_prologue)
00536     {
00537       /* Present instruction.  */
00538       uint32_t insn;
00539 
00540       int prologue_insn = 0;
00541 
00542       if (pc == current_pc)
00543       {
00544         /* When we reach the current PC we must save the current
00545            register state (for the backtrace) but keep analysing
00546            because there might be more to find out (eg. is this an
00547            exception handler).  */
00548         memcpy (temp_value, value, sizeof (temp_value));
00549         value = temp_value;
00550         if (nios2_debug)
00551           fprintf_unfiltered (gdb_stdlog, "*");
00552       }
00553 
00554       insn = read_memory_unsigned_integer (pc, NIOS2_OPCODE_SIZE, byte_order);
00555       pc += NIOS2_OPCODE_SIZE;
00556 
00557       if (nios2_debug)
00558         fprintf_unfiltered (gdb_stdlog, "[%08X]", insn);
00559 
00560       /* The following instructions can appear in the prologue.  */
00561 
00562       if ((insn & 0x0001ffff) == 0x0001883a)
00563         {
00564           /* ADD   rc, ra, rb  (also used for MOV) */
00565 
00566           int ra = GET_IW_A (insn);
00567           int rb = GET_IW_B (insn);
00568           int rc = GET_IW_C (insn);
00569 
00570           if (rc == NIOS2_SP_REGNUM
00571               && rb == 0
00572               && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
00573             {
00574               /* If the previous value of SP is available somewhere
00575                  near the new stack pointer value then this is a
00576                  stack switch.  */
00577 
00578               /* If any registers were saved on the stack before then
00579                  we can't backtrace into them now.  */
00580               for (i = 0 ; i < NIOS2_NUM_REGS ; i++)
00581                 {
00582                   if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
00583                     cache->reg_saved[i].basereg = -1;
00584                   if (value[i].reg == NIOS2_SP_REGNUM)
00585                     value[i].reg = -1;
00586                 }
00587 
00588               /* Create a fake "high water mark" 4 bytes above where SP
00589                  was stored and fake up the registers to be consistent
00590                  with that.  */
00591               value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
00592               value[NIOS2_SP_REGNUM].offset
00593                 = (value[ra].offset
00594                    - cache->reg_saved[NIOS2_SP_REGNUM].addr
00595                    - 4);
00596               cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
00597               cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
00598             }
00599 
00600           else if (rc != 0)
00601             {
00602               if (value[rb].reg == 0)
00603                 value[rc].reg = value[ra].reg;
00604               else if (value[ra].reg == 0)
00605                 value[rc].reg = value[rb].reg;
00606               else
00607                 value[rc].reg = -1;
00608               value[rc].offset = value[ra].offset + value[rb].offset;
00609             }
00610           prologue_insn = 1;
00611         }
00612 
00613       else if ((insn & 0x0001ffff) == 0x0001983a)
00614         {
00615           /* SUB   rc, ra, rb */
00616 
00617           int ra = GET_IW_A (insn);
00618           int rb = GET_IW_B (insn);
00619           int rc = GET_IW_C (insn);
00620 
00621           if (rc != 0)
00622             {
00623               if (value[rb].reg == 0)
00624                 value[rc].reg = value[ra].reg;
00625               else
00626                 value[rc].reg = -1;
00627               value[rc].offset = value[ra].offset - value[rb].offset;
00628             }
00629         }
00630 
00631       else if ((insn & 0x0000003f) == 0x00000004)
00632         {
00633           /* ADDI  rb, ra, immed   (also used for MOVI) */
00634           short immed = GET_IW_IMM16 (insn);
00635           int ra = GET_IW_A (insn);
00636           int rb = GET_IW_B (insn);
00637 
00638           /* The first stack adjustment is part of the prologue.
00639              Any subsequent stack adjustments are either down to
00640              alloca or the epilogue so stop analysing when we hit
00641              them.  */
00642           if (rb == NIOS2_SP_REGNUM
00643               && (value[rb].offset != 0 || value[ra].reg != NIOS2_SP_REGNUM))
00644             break;
00645 
00646           if (rb != 0)
00647             {
00648               value[rb].reg    = value[ra].reg;
00649               value[rb].offset = value[ra].offset + immed;
00650             }
00651 
00652           prologue_insn = 1;
00653         }
00654 
00655       else if ((insn & 0x0000003f) == 0x00000034)
00656         {
00657           /* ORHI  rb, ra, immed   (also used for MOVHI) */
00658           unsigned int immed = GET_IW_IMM16 (insn);
00659           int ra = GET_IW_A (insn);
00660           int rb = GET_IW_B (insn);
00661 
00662           if (rb != 0)
00663             {
00664               value[rb].reg    = (value[ra].reg == 0) ? 0 : -1;
00665               value[rb].offset = value[ra].offset | (immed << 16);
00666             }
00667         }
00668 
00669       else if ((insn & IW_OP_MASK) == OP_STW
00670                || (insn & IW_OP_MASK) == OP_STWIO)
00671         {
00672           /* STW rb, immediate(ra) */
00673 
00674           short immed16 = GET_IW_IMM16 (insn);
00675           int ra = GET_IW_A (insn);
00676           int rb = GET_IW_B (insn);
00677 
00678           /* Are we storing the original value of a register?
00679              For exception handlers the value of EA-4 (return
00680              address from interrupts etc) is sometimes stored.  */
00681           int orig = value[rb].reg;
00682           if (orig > 0
00683               && (value[rb].offset == 0
00684                   || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4)))
00685             {
00686               /* We are most interested in stores to the stack, but
00687                  also take note of stores to other places as they
00688                  might be useful later.  */
00689               if ((value[ra].reg == NIOS2_SP_REGNUM
00690                    && cache->reg_saved[orig].basereg != NIOS2_SP_REGNUM)
00691                   || cache->reg_saved[orig].basereg == -1)
00692                 {
00693                   if (pc < current_pc)
00694                     {
00695                       /* Save off callee saved registers.  */
00696                       cache->reg_saved[orig].basereg = value[ra].reg;
00697                       cache->reg_saved[orig].addr
00698                         = value[ra].offset + GET_IW_IMM16 (insn);
00699                     }
00700 
00701                   prologue_insn = 1;
00702 
00703                   if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
00704                     exception_handler = 1;
00705                 }
00706             }
00707           else
00708             /* Non-stack memory writes are not part of the
00709                prologue.  */
00710             within_prologue = 0;
00711         }
00712 
00713       else if ((insn & 0xffc1f83f) == 0x0001303a)
00714         {
00715           /* RDCTL rC, ctlN */
00716           int rc = GET_IW_C (insn);
00717           int n = GET_IW_CONTROL_REGNUM (insn);
00718 
00719           if (rc != 0)
00720             {
00721               value[rc].reg    = NIOS2_STATUS_REGNUM + n;
00722               value[rc].offset = 0;
00723             }
00724 
00725           prologue_insn = 1;
00726         }
00727 
00728       else if ((insn & 0x0000003f) == 0
00729                && value[8].reg == NIOS2_RA_REGNUM
00730                && value[8].offset == 0
00731                && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
00732                && value[NIOS2_SP_REGNUM].offset == 0)
00733         {
00734           /* A CALL instruction.  This is treated as a call to mcount
00735              if ra has been stored into r8 beforehand and if it's
00736              before the stack adjust.
00737              Note mcount corrupts r2-r3, r9-r15 & ra.  */
00738           for (i = 2 ; i <= 3 ; i++)
00739             value[i].reg = -1;
00740           for (i = 9 ; i <= 15 ; i++)
00741             value[i].reg = -1;
00742           value[NIOS2_RA_REGNUM].reg = -1;
00743 
00744           prologue_insn = 1;
00745         }
00746 
00747       else if ((insn & 0xf83fffff) == 0xd800012e)
00748         {
00749            /* BGEU sp, rx, +8
00750               BREAK 3
00751               This instruction sequence is used in stack checking;
00752               we can ignore it.  */
00753           unsigned int next_insn
00754             = read_memory_unsigned_integer (pc, NIOS2_OPCODE_SIZE, byte_order);
00755 
00756           if (next_insn != 0x003da0fa)
00757             within_prologue = 0;
00758           else
00759             pc += NIOS2_OPCODE_SIZE;
00760         }
00761 
00762       else if ((insn & 0xf800003f) == 0xd8000036)
00763         {
00764            /* BLTU sp, rx, .Lstackoverflow
00765               If the location branched to holds a BREAK 3 instruction
00766               then this is also stack overflow detection.  We can
00767               ignore it.  */
00768           CORE_ADDR target_pc = pc + ((insn & 0x3fffc0) >> 6);
00769           unsigned int target_insn
00770             = read_memory_unsigned_integer (target_pc, NIOS2_OPCODE_SIZE,
00771                                             byte_order);
00772 
00773           if (target_insn != 0x003da0fa)
00774             within_prologue = 0;
00775         }
00776 
00777       /* Any other instructions are allowed to be moved up into the
00778          prologue.  If we reach a branch, call or return then the
00779          prologue is considered over.  We also consider a second stack
00780          adjustment as terminating the prologue (see above).  */
00781       else
00782         {
00783           switch (GET_IW_OP (insn))
00784             {
00785             case OP_BEQ:
00786             case OP_BGE:
00787             case OP_BGEU:
00788             case OP_BLT:
00789             case OP_BLTU:
00790             case OP_BNE:
00791             case OP_BR:
00792             case OP_CALL:
00793               within_prologue = 0;
00794               break;
00795             case OP_OPX:
00796               if (GET_IW_OPX (insn) == OPX_RET
00797                   || GET_IW_OPX (insn) == OPX_ERET
00798                   || GET_IW_OPX (insn) == OPX_BRET
00799                   || GET_IW_OPX (insn) == OPX_CALLR
00800                   || GET_IW_OPX (insn) == OPX_JMP)
00801                 within_prologue = 0;
00802               break;
00803             default:
00804               break;
00805             }
00806         }
00807 
00808       if (prologue_insn)
00809         prologue_end = pc;
00810     }
00811 
00812   /* If THIS_FRAME is NULL, we are being called from skip_prologue
00813      and are only interested in the PROLOGUE_END value, so just
00814      return that now and skip over the cache updates, which depend
00815      on having frame information.  */
00816   if (this_frame == NULL)
00817     return prologue_end;
00818 
00819   /* If we are in the function epilogue and have already popped
00820      registers off the stack in preparation for returning, then we
00821      want to go back to the original register values.  */
00822   if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
00823     nios2_setup_default (cache);
00824 
00825   /* Exception handlers use a different return address register.  */
00826   if (exception_handler)
00827     cache->return_regnum = NIOS2_EA_REGNUM;
00828 
00829   if (nios2_debug)
00830     fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);
00831 
00832   if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
00833     /* If the FP now holds an offset from the CFA then this is a
00834        normal frame which uses the frame pointer.  */
00835     base_reg = NIOS2_FP_REGNUM;
00836   else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
00837     /* FP doesn't hold an offset from the CFA.  If SP still holds an
00838        offset from the CFA then we might be in a function which omits
00839        the frame pointer, or we might be partway through the prologue.
00840        In both cases we can find the CFA using SP.  */
00841     base_reg = NIOS2_SP_REGNUM;
00842   else
00843     {
00844       /* Somehow the stack pointer has been corrupted.
00845          We can't return.  */
00846       if (nios2_debug)
00847         fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n");
00848       return 0;
00849     }
00850 
00851   if (cache->reg_value[base_reg].offset == 0
00852       || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
00853       || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
00854     {
00855       /* If the frame didn't adjust the stack, didn't save RA or
00856          didn't save EA in an exception handler then it must either
00857          be a leaf function (doesn't call any other functions) or it
00858          can't return.  If it has called another function then it
00859          can't be a leaf, so set base == 0 to indicate that we can't
00860          backtrace past it.  */
00861 
00862       if (!innermost)
00863         {
00864           /* If it isn't the innermost function then it can't be a
00865              leaf, unless it was interrupted.  Check whether RA for
00866              this frame is the same as PC.  If so then it probably
00867              wasn't interrupted.  */
00868           CORE_ADDR ra
00869             = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);
00870 
00871           if (ra == current_pc)
00872             {
00873               if (nios2_debug)
00874                 fprintf_unfiltered
00875                   (gdb_stdlog,
00876                    "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
00877                    paddress (gdbarch, cache->reg_value[base_reg].offset),
00878                    cache->reg_saved[NIOS2_RA_REGNUM].basereg,
00879                    cache->return_regnum,
00880                    cache->reg_saved[cache->return_regnum].basereg);
00881               return 0;
00882             }
00883         }
00884     }
00885 
00886   /* Get the value of whichever register we are using for the
00887      base.  */
00888   cache->base = get_frame_register_unsigned (this_frame, base_reg);
00889 
00890   /* What was the value of SP at the start of this function (or just
00891      after the stack switch).  */
00892   frame_high = cache->base - cache->reg_value[base_reg].offset;
00893 
00894   /* Adjust all the saved registers such that they contain addresses
00895      instead of offsets.  */
00896   for (i = 0; i < NIOS2_NUM_REGS; i++)
00897     if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
00898       {
00899         cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
00900         cache->reg_saved[i].addr += frame_high;
00901       }
00902 
00903   for (i = 0; i < NIOS2_NUM_REGS; i++)
00904     if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
00905       {
00906         CORE_ADDR gp = get_frame_register_unsigned (this_frame,
00907                                                     NIOS2_GP_REGNUM);
00908 
00909         for ( ; i < NIOS2_NUM_REGS; i++)
00910           if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
00911             {
00912               cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
00913               cache->reg_saved[i].addr += gp;
00914             }
00915       }
00916 
00917   /* Work out what the value of SP was on the first instruction of
00918      this function.  If we didn't switch stacks then this can be
00919      trivially computed from the base address.  */
00920   if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
00921     cache->cfa
00922       = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
00923                                       4, byte_order);
00924   else
00925     cache->cfa = frame_high;
00926 
00927   /* Exception handlers restore ESTATUS into STATUS.  */
00928   if (exception_handler)
00929     {
00930       cache->reg_saved[NIOS2_STATUS_REGNUM]
00931         = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
00932       cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
00933     }
00934 
00935   if (nios2_debug)
00936     fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n",
00937                         paddress (gdbarch, cache->cfa));
00938 
00939   return prologue_end;
00940 }
00941 
00942 /* Implement the skip_prologue gdbarch hook.  */
00943 
00944 static CORE_ADDR
00945 nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
00946 {
00947   CORE_ADDR limit_pc;
00948   CORE_ADDR func_addr;
00949 
00950   struct nios2_unwind_cache cache;
00951 
00952   /* See if we can determine the end of the prologue via the symbol
00953      table.  If so, then return either PC, or the PC after the
00954      prologue, whichever is greater.  */
00955   if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
00956     {
00957       CORE_ADDR post_prologue_pc
00958         = skip_prologue_using_sal (gdbarch, func_addr);
00959 
00960       if (post_prologue_pc != 0)
00961         return max (start_pc, post_prologue_pc);
00962     }
00963 
00964   /* Prologue analysis does the rest....  */
00965   nios2_init_cache (&cache, start_pc);
00966   return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
00967 }
00968 
00969 /* Implement the breakpoint_from_pc gdbarch hook.  */
00970 
00971 static const gdb_byte*
00972 nios2_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
00973                           int *bp_size)
00974 {
00975   /* break encoding: 31->27  26->22  21->17  16->11 10->6 5->0 */
00976   /*                 00000   00000   0x1d    0x2d   11111 0x3a */
00977   /*                 00000   00000   11101   101101 11111 111010 */
00978   /* In bytes:       00000000 00111011 01101111 11111010 */
00979   /*                 0x0       0x3b    0x6f     0xfa */
00980   static const gdb_byte breakpoint_le[] = {0xfa, 0x6f, 0x3b, 0x0};
00981   static const gdb_byte breakpoint_be[] = {0x0, 0x3b, 0x6f, 0xfa};
00982 
00983   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
00984 
00985   *bp_size = 4;
00986   if (gdbarch_byte_order_for_code (gdbarch) == BFD_ENDIAN_BIG)
00987     return breakpoint_be;
00988   else
00989     return breakpoint_le;
00990 }
00991 
00992 /* Implement the print_insn gdbarch method.  */
00993 
00994 static int
00995 nios2_print_insn (bfd_vma memaddr, disassemble_info *info)
00996 {
00997   if (info->endian == BFD_ENDIAN_BIG)
00998     return print_insn_big_nios2 (memaddr, info);
00999   else
01000     return print_insn_little_nios2 (memaddr, info);
01001 }
01002 
01003 
01004 /* Implement the frame_align gdbarch method.  */
01005 
01006 static CORE_ADDR
01007 nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
01008 {
01009   return align_down (addr, 4);
01010 }
01011 
01012 
01013 /* Implement the return_value gdbarch method.  */
01014 
01015 static enum return_value_convention
01016 nios2_return_value (struct gdbarch *gdbarch, struct value *function,
01017                     struct type *type, struct regcache *regcache,
01018                     gdb_byte *readbuf, const gdb_byte *writebuf)
01019 {
01020   if (TYPE_LENGTH (type) > 8)
01021     return RETURN_VALUE_STRUCT_CONVENTION;
01022 
01023   if (readbuf)
01024     nios2_extract_return_value (gdbarch, type, regcache, readbuf);
01025   if (writebuf)
01026     nios2_store_return_value (gdbarch, type, regcache, writebuf);
01027 
01028   return RETURN_VALUE_REGISTER_CONVENTION;
01029 }
01030 
01031 /* Implement the dummy_id gdbarch method.  */
01032 
01033 static struct frame_id
01034 nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
01035 {
01036   return frame_id_build
01037     (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM),
01038      get_frame_pc (this_frame));
01039 }
01040 
01041 /* Implement the push_dummy_call gdbarch method.  */
01042 
01043 static CORE_ADDR
01044 nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
01045                        struct regcache *regcache, CORE_ADDR bp_addr,
01046                        int nargs, struct value **args, CORE_ADDR sp,
01047                        int struct_return, CORE_ADDR struct_addr)
01048 {
01049   int argreg;
01050   int float_argreg;
01051   int argnum;
01052   int len = 0;
01053   int stack_offset = 0;
01054   CORE_ADDR func_addr = find_function_addr (function, NULL);
01055   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01056 
01057   /* Set the return address register to point to the entry point of
01058      the program, where a breakpoint lies in wait.  */
01059   regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);
01060 
01061   /* Now make space on the stack for the args.  */
01062   for (argnum = 0; argnum < nargs; argnum++)
01063     len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
01064   sp -= len;
01065 
01066   /* Initialize the register pointer.  */
01067   argreg = NIOS2_FIRST_ARGREG;
01068 
01069   /* The struct_return pointer occupies the first parameter-passing
01070      register.  */
01071   if (struct_return)
01072     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
01073 
01074   /* Now load as many as possible of the first arguments into
01075      registers, and push the rest onto the stack.  Loop through args
01076      from first to last.  */
01077   for (argnum = 0; argnum < nargs; argnum++)
01078     {
01079       const gdb_byte *val;
01080       gdb_byte valbuf[MAX_REGISTER_SIZE];
01081       struct value *arg = args[argnum];
01082       struct type *arg_type = check_typedef (value_type (arg));
01083       int len = TYPE_LENGTH (arg_type);
01084       enum type_code typecode = TYPE_CODE (arg_type);
01085 
01086       val = value_contents (arg);
01087 
01088       /* Copy the argument to general registers or the stack in
01089          register-sized pieces.  Large arguments are split between
01090          registers and stack.  */
01091       while (len > 0)
01092         {
01093           int partial_len = (len < 4 ? len : 4);
01094 
01095           if (argreg <= NIOS2_LAST_ARGREG)
01096             {
01097               /* The argument is being passed in a register.  */
01098               CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
01099                                                            byte_order);
01100 
01101               regcache_cooked_write_unsigned (regcache, argreg, regval);
01102               argreg++;
01103             }
01104           else
01105             {
01106               /* The argument is being passed on the stack.  */
01107               CORE_ADDR addr = sp + stack_offset;
01108 
01109               write_memory (addr, val, partial_len);
01110               stack_offset += align_up (partial_len, 4);
01111             }
01112 
01113           len -= partial_len;
01114           val += partial_len;
01115         }
01116     }
01117 
01118   regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);
01119 
01120   /* Return adjusted stack pointer.  */
01121   return sp;
01122 }
01123 
01124 /* Implement the unwind_pc gdbarch method.  */
01125 
01126 static CORE_ADDR
01127 nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01128 {
01129   gdb_byte buf[4];
01130 
01131   frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
01132   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
01133 }
01134 
01135 /* Implement the unwind_sp gdbarch method.  */
01136 
01137 static CORE_ADDR
01138 nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
01139 {
01140   return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM);
01141 }
01142 
01143 /* Use prologue analysis to fill in the register cache
01144    *THIS_PROLOGUE_CACHE for THIS_FRAME.  This function initializes
01145    *THIS_PROLOGUE_CACHE first.  */
01146 
01147 static struct nios2_unwind_cache *
01148 nios2_frame_unwind_cache (struct frame_info *this_frame,
01149                           void **this_prologue_cache)
01150 {
01151   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01152   CORE_ADDR current_pc;
01153   struct nios2_unwind_cache *cache;
01154   int i;
01155 
01156   if (*this_prologue_cache)
01157     return *this_prologue_cache;
01158 
01159   cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
01160   *this_prologue_cache = cache;
01161 
01162   /* Zero all fields.  */
01163   nios2_init_cache (cache, get_frame_func (this_frame));
01164 
01165   /* Prologue analysis does the rest...  */
01166   current_pc = get_frame_pc (this_frame);
01167   if (cache->pc != 0)
01168     nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);
01169 
01170   return cache;
01171 }
01172 
01173 /* Implement the this_id function for the normal unwinder.  */
01174 
01175 static void
01176 nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
01177                      struct frame_id *this_id)
01178 {
01179   struct nios2_unwind_cache *cache =
01180     nios2_frame_unwind_cache (this_frame, this_cache);
01181 
01182   /* This marks the outermost frame.  */
01183   if (cache->base == 0)
01184     return;
01185 
01186   *this_id = frame_id_build (cache->cfa, cache->pc);
01187 }
01188 
01189 /* Implement the prev_register function for the normal unwinder.  */
01190 
01191 static struct value *
01192 nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
01193                            int regnum)
01194 {
01195   struct nios2_unwind_cache *cache =
01196     nios2_frame_unwind_cache (this_frame, this_cache);
01197 
01198   gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);
01199 
01200   /* The PC of the previous frame is stored in the RA register of
01201      the current frame.  Frob regnum so that we pull the value from
01202      the correct place.  */
01203   if (regnum == NIOS2_PC_REGNUM)
01204     regnum = cache->return_regnum;
01205 
01206   if (regnum == NIOS2_SP_REGNUM && cache->cfa)
01207     return frame_unwind_got_constant (this_frame, regnum, cache->cfa);
01208 
01209   /* If we've worked out where a register is stored then load it from
01210      there.  */
01211   if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
01212     return frame_unwind_got_memory (this_frame, regnum,
01213                                     cache->reg_saved[regnum].addr);
01214 
01215   return frame_unwind_got_register (this_frame, regnum, regnum);
01216 }
01217 
01218 /* Implement the this_base, this_locals, and this_args hooks
01219    for the normal unwinder.  */
01220 
01221 static CORE_ADDR
01222 nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
01223 {
01224   struct nios2_unwind_cache *info
01225     = nios2_frame_unwind_cache (this_frame, this_cache);
01226 
01227   return info->base;
01228 }
01229 
01230 /* Data structures for the normal prologue-analysis-based
01231    unwinder.  */
01232 
01233 static const struct frame_unwind nios2_frame_unwind =
01234 {
01235   NORMAL_FRAME,
01236   default_frame_unwind_stop_reason,
01237   nios2_frame_this_id,
01238   nios2_frame_prev_register,
01239   NULL,
01240   default_frame_sniffer
01241 };
01242 
01243 static const struct frame_base nios2_frame_base =
01244 {
01245   &nios2_frame_unwind,
01246   nios2_frame_base_address,
01247   nios2_frame_base_address,
01248   nios2_frame_base_address
01249 };
01250 
01251 /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
01252    in the stub unwinder.  */
01253 
01254 static struct trad_frame_cache *
01255 nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
01256 {
01257   CORE_ADDR pc;
01258   CORE_ADDR start_addr;
01259   CORE_ADDR stack_addr;
01260   struct trad_frame_cache *this_trad_cache;
01261   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01262   int num_regs = gdbarch_num_regs (gdbarch);
01263 
01264   if (*this_cache != NULL)
01265     return *this_cache;
01266   this_trad_cache = trad_frame_cache_zalloc (this_frame);
01267   *this_cache = this_trad_cache;
01268 
01269   /* The return address is in the link register.  */
01270   trad_frame_set_reg_realreg (this_trad_cache,
01271                               gdbarch_pc_regnum (gdbarch),
01272                               NIOS2_RA_REGNUM);
01273 
01274   /* Frame ID, since it's a frameless / stackless function, no stack
01275      space is allocated and SP on entry is the current SP.  */
01276   pc = get_frame_pc (this_frame);
01277   find_pc_partial_function (pc, NULL, &start_addr, NULL);
01278   stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
01279   trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
01280   /* Assume that the frame's base is the same as the stack pointer.  */
01281   trad_frame_set_this_base (this_trad_cache, stack_addr);
01282 
01283   return this_trad_cache;
01284 }
01285 
01286 /* Implement the this_id function for the stub unwinder.  */
01287 
01288 static void
01289 nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
01290                           struct frame_id *this_id)
01291 {
01292   struct trad_frame_cache *this_trad_cache
01293     = nios2_stub_frame_cache (this_frame, this_cache);
01294 
01295   trad_frame_get_id (this_trad_cache, this_id);
01296 }
01297 
01298 /* Implement the prev_register function for the stub unwinder.  */
01299 
01300 static struct value *
01301 nios2_stub_frame_prev_register (struct frame_info *this_frame,
01302                                 void **this_cache, int regnum)
01303 {
01304   struct trad_frame_cache *this_trad_cache
01305     = nios2_stub_frame_cache (this_frame, this_cache);
01306 
01307   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
01308 }
01309 
01310 /* Implement the sniffer function for the stub unwinder.
01311    This unwinder is used for cases where the normal
01312    prologue-analysis-based unwinder can't work,
01313    such as PLT stubs.  */
01314 
01315 static int
01316 nios2_stub_frame_sniffer (const struct frame_unwind *self,
01317                           struct frame_info *this_frame, void **cache)
01318 {
01319   gdb_byte dummy[4];
01320   struct obj_section *s;
01321   CORE_ADDR pc = get_frame_address_in_block (this_frame);
01322 
01323   /* Use the stub unwinder for unreadable code.  */
01324   if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
01325     return 1;
01326 
01327   if (in_plt_section (pc))
01328     return 1;
01329 
01330   return 0;
01331 }
01332 
01333 /* Implement the this_base, this_locals, and this_args hooks
01334    for the stub unwinder.  */
01335 
01336 static CORE_ADDR
01337 nios2_stub_frame_base_address (struct frame_info *this_frame, void **this_cache)
01338 {
01339   struct trad_frame_cache *this_trad_cache
01340     = nios2_stub_frame_cache (this_frame, this_cache);
01341 
01342   return trad_frame_get_this_base (this_trad_cache);
01343 }
01344 
01345 /* Define the data structures for the stub unwinder.  */
01346 
01347 static const struct frame_unwind nios2_stub_frame_unwind =
01348 {
01349   NORMAL_FRAME,
01350   default_frame_unwind_stop_reason,
01351   nios2_stub_frame_this_id,
01352   nios2_stub_frame_prev_register,
01353   NULL,
01354   nios2_stub_frame_sniffer
01355 };
01356 
01357 static const struct frame_base nios2_stub_frame_base =
01358 {
01359   &nios2_stub_frame_unwind,
01360   nios2_stub_frame_base_address,
01361   nios2_stub_frame_base_address,
01362   nios2_stub_frame_base_address
01363 };
01364 
01365 /* Helper function to read an instruction at PC.  */
01366 
01367 static unsigned long
01368 nios2_fetch_instruction (struct gdbarch *gdbarch, CORE_ADDR pc)
01369 {
01370   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01371 
01372   return read_memory_unsigned_integer (pc, NIOS2_OPCODE_SIZE, byte_order);
01373 }
01374 
01375 /* Determine where to set a single step breakpoint while considering
01376    branch prediction.  */
01377 
01378 static CORE_ADDR
01379 nios2_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
01380 {
01381   struct gdbarch *gdbarch = get_frame_arch (frame);
01382   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01383   unsigned long inst;
01384   int op;
01385   int imm16;
01386   int ra;
01387   int rb;
01388   int ras;
01389   int rbs;
01390   unsigned int rau;
01391   unsigned int rbu;
01392 
01393   inst = nios2_fetch_instruction (gdbarch, pc);
01394   pc += NIOS2_OPCODE_SIZE;
01395 
01396   imm16 = (short) GET_IW_IMM16 (inst);
01397   ra = GET_IW_A (inst);
01398   rb = GET_IW_B (inst);
01399   ras = get_frame_register_signed (frame, ra);
01400   rbs = get_frame_register_signed (frame, rb);
01401   rau = get_frame_register_unsigned (frame, ra);
01402   rbu = get_frame_register_unsigned (frame, rb);
01403 
01404   switch (GET_IW_OP (inst))
01405     {
01406     case OP_BEQ:
01407       if (ras == rbs)
01408         pc += imm16;
01409       break;
01410 
01411     case OP_BGE:
01412       if (ras >= rbs)
01413         pc += imm16;
01414       break;
01415 
01416     case OP_BGEU:
01417       if (rau >= rbu)
01418         pc += imm16;
01419       break;
01420 
01421     case OP_BLT:
01422       if (ras < rbs)
01423         pc += imm16;
01424       break;
01425 
01426     case OP_BLTU:
01427       if (rau < rbu)
01428         pc += imm16;
01429       break;
01430 
01431     case OP_BNE:
01432       if (ras != rbs)
01433         pc += imm16;
01434       break;
01435 
01436     case OP_BR:
01437       pc += imm16;
01438       break;
01439 
01440     case OP_JMPI:
01441     case OP_CALL:
01442       pc = (pc & 0xf0000000) | (GET_IW_IMM26 (inst) << 2);
01443       break;
01444 
01445     case OP_OPX:
01446       switch (GET_IW_OPX (inst))
01447         {
01448         case OPX_JMP:
01449         case OPX_CALLR:
01450         case OPX_RET:
01451           pc = ras;
01452           break;
01453 
01454         case OPX_TRAP:
01455           if (tdep->syscall_next_pc != NULL)
01456             return tdep->syscall_next_pc (frame);
01457 
01458         default:
01459           break;
01460         }
01461       break;
01462     default:
01463       break;
01464     }
01465   return pc;
01466 }
01467 
01468 /* Implement the software_single_step gdbarch method.  */
01469 
01470 static int
01471 nios2_software_single_step (struct frame_info *frame)
01472 {
01473   struct gdbarch *gdbarch = get_frame_arch (frame);
01474   struct address_space *aspace = get_frame_address_space (frame);
01475   CORE_ADDR next_pc = nios2_get_next_pc (frame, get_frame_pc (frame));
01476 
01477   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
01478 
01479   return 1;
01480 }
01481 
01482 /* Implement the get_longjump_target gdbarch method.  */
01483 
01484 static int
01485 nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
01486 {
01487   struct gdbarch *gdbarch = get_frame_arch (frame);
01488   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01489   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01490   CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
01491   gdb_byte buf[4];
01492 
01493   if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
01494     return 0;
01495 
01496   *pc = extract_unsigned_integer (buf, 4, byte_order);
01497   return 1;
01498 }
01499 
01500 /* Initialize the Nios II gdbarch.  */
01501 
01502 static struct gdbarch *
01503 nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
01504 {
01505   struct gdbarch *gdbarch;
01506   struct gdbarch_tdep *tdep;
01507   int register_bytes, i;
01508   struct tdesc_arch_data *tdesc_data = NULL;
01509   const struct target_desc *tdesc = info.target_desc;
01510 
01511   if (!tdesc_has_registers (tdesc))
01512     /* Pick a default target description.  */
01513     tdesc = tdesc_nios2;
01514 
01515   /* Check any target description for validity.  */
01516   if (tdesc_has_registers (tdesc))
01517     {
01518       const struct tdesc_feature *feature;
01519       int valid_p;
01520 
01521       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
01522       if (feature == NULL)
01523         return NULL;
01524 
01525       tdesc_data = tdesc_data_alloc ();
01526 
01527       valid_p = 1;
01528       
01529       for (i = 0; i < NIOS2_NUM_REGS; i++)
01530         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
01531                                             nios2_reg_names[i]);
01532 
01533       if (!valid_p)
01534         {
01535           tdesc_data_cleanup (tdesc_data);
01536           return NULL;
01537         }
01538     }
01539 
01540   /* Find a candidate among the list of pre-declared architectures.  */
01541   arches = gdbarch_list_lookup_by_info (arches, &info);
01542   if (arches != NULL)
01543     return arches->gdbarch;
01544 
01545   /* None found, create a new architecture from the information
01546      provided.  */
01547   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
01548   gdbarch = gdbarch_alloc (&info, tdep);
01549 
01550   /* longjmp support not enabled by default.  */
01551   tdep->jb_pc = -1;
01552 
01553   /* Data type sizes.  */
01554   set_gdbarch_ptr_bit (gdbarch, 32);
01555   set_gdbarch_addr_bit (gdbarch, 32);
01556   set_gdbarch_short_bit (gdbarch, 16);
01557   set_gdbarch_int_bit (gdbarch, 32);
01558   set_gdbarch_long_bit (gdbarch, 32);
01559   set_gdbarch_long_long_bit (gdbarch, 64);
01560   set_gdbarch_float_bit (gdbarch, 32);
01561   set_gdbarch_double_bit (gdbarch, 64);
01562 
01563   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
01564   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
01565 
01566   /* The register set.  */
01567   set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
01568   set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
01569   set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM);     /* Pseudo register PC */
01570 
01571   set_gdbarch_register_name (gdbarch, nios2_register_name);
01572   set_gdbarch_register_type (gdbarch, nios2_register_type);
01573 
01574   /* Provide register mappings for stabs and dwarf2.  */
01575   set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
01576   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
01577 
01578   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
01579 
01580   /* Call dummy code.  */
01581   set_gdbarch_frame_align (gdbarch, nios2_frame_align);
01582 
01583   set_gdbarch_return_value (gdbarch, nios2_return_value);
01584 
01585   set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
01586   set_gdbarch_in_function_epilogue_p (gdbarch, nios2_in_function_epilogue_p);
01587   set_gdbarch_breakpoint_from_pc (gdbarch, nios2_breakpoint_from_pc);
01588 
01589   set_gdbarch_dummy_id (gdbarch, nios2_dummy_id);
01590   set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
01591   set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp);
01592 
01593   /* The dwarf2 unwinder will normally produce the best results if
01594      the debug information is available, so register it first.  */
01595   dwarf2_append_unwinders (gdbarch);
01596   frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
01597   frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);
01598 
01599   /* Single stepping.  */
01600   set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);
01601 
01602   /* Hook in ABI-specific overrides, if they have been registered.  */
01603   gdbarch_init_osabi (info, gdbarch);
01604 
01605   if (tdep->jb_pc >= 0)
01606     set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);
01607 
01608   frame_base_set_default (gdbarch, &nios2_frame_base);
01609 
01610   set_gdbarch_print_insn (gdbarch, nios2_print_insn);
01611 
01612   /* Enable inferior call support.  */
01613   set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);
01614 
01615   if (tdesc_data)
01616     tdesc_use_registers (gdbarch, tdesc, tdesc_data);
01617 
01618   return gdbarch;
01619 }
01620 
01621 extern initialize_file_ftype _initialize_nios2_tdep; /* -Wmissing-prototypes */
01622 
01623 void
01624 _initialize_nios2_tdep (void)
01625 {
01626   gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
01627   initialize_tdesc_nios2 ();
01628 
01629   /* Allow debugging this file's internals.  */
01630   add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
01631                            _("Set Nios II debugging."),
01632                            _("Show Nios II debugging."),
01633                            _("When on, Nios II specific debugging is enabled."),
01634                            NULL,
01635                            NULL,
01636                            &setdebuglist, &showdebuglist);
01637 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines