gdb/nios2-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-machine dependent code for Nios II, for GDB.
  2.    Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3.    Contributed by Peter Brookes (pbrookes@altera.com)
  4.    and Andrew Draper (adraper@altera.com).
  5.    Contributed by Mentor Graphics, Inc.

  6.    This file is part of GDB.

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

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

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

  17. #include "defs.h"
  18. #include "frame.h"
  19. #include "frame-unwind.h"
  20. #include "frame-base.h"
  21. #include "trad-frame.h"
  22. #include "dwarf2-frame.h"
  23. #include "symtab.h"
  24. #include "inferior.h"
  25. #include "gdbtypes.h"
  26. #include "gdbcore.h"
  27. #include "gdbcmd.h"
  28. #include "osabi.h"
  29. #include "target.h"
  30. #include "dis-asm.h"
  31. #include "regcache.h"
  32. #include "value.h"
  33. #include "symfile.h"
  34. #include "arch-utils.h"
  35. #include "floatformat.h"
  36. #include "infcall.h"
  37. #include "regset.h"
  38. #include "target-descriptions.h"

  39. /* To get entry_point_address.  */
  40. #include "objfiles.h"

  41. /* Nios II ISA specific encodings and macros.  */
  42. #include "opcode/nios2.h"

  43. /* Nios II specific header.  */
  44. #include "nios2-tdep.h"

  45. #include "features/nios2.c"

  46. /* Control debugging information emitted in this file.  */

  47. static int nios2_debug = 0;

  48. /* The following structures are used in the cache for prologue
  49.    analysis; see the reg_value and reg_saved tables in
  50.    struct nios2_unwind_cache, respectively.  */

  51. /* struct reg_value is used to record that a register has the same value
  52.    as reg at the given offset from the start of a function.  */

  53. struct reg_value
  54. {
  55.   int reg;
  56.   unsigned int offset;
  57. };

  58. /* struct reg_saved is used to record that a register value has been saved at
  59.    basereg + addr, for basereg >= 0.  If basereg < 0, that indicates
  60.    that the register is not known to have been saved.  Note that when
  61.    basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
  62.    addr is an absolute address.  */

  63. struct reg_saved
  64. {
  65.   int basereg;
  66.   CORE_ADDR addr;
  67. };

  68. struct nios2_unwind_cache
  69. {
  70.   /* The frame's base, optionally used by the high-level debug info.  */
  71.   CORE_ADDR base;

  72.   /* The previous frame's inner most stack address.  Used as this
  73.      frame ID's stack_addr.  */
  74.   CORE_ADDR cfa;

  75.   /* The address of the first instruction in this function.  */
  76.   CORE_ADDR pc;

  77.   /* Which register holds the return address for the frame.  */
  78.   int return_regnum;

  79.   /* Table indicating what changes have been made to each register.  */
  80.   struct reg_value reg_value[NIOS2_NUM_REGS];

  81.   /* Table indicating where each register has been saved.  */
  82.   struct reg_saved reg_saved[NIOS2_NUM_REGS];
  83. };


  84. /* This array is a mapping from Dwarf-2 register numbering to GDB's.  */

  85. static int nios2_dwarf2gdb_regno_map[] =
  86. {
  87.   0, 1, 2, 3,
  88.   4, 5, 6, 7,
  89.   8, 9, 10, 11,
  90.   12, 13, 14, 15,
  91.   16, 17, 18, 19,
  92.   20, 21, 22, 23,
  93.   24, 25,
  94.   NIOS2_GP_REGNUM,        /* 26 */
  95.   NIOS2_SP_REGNUM,        /* 27 */
  96.   NIOS2_FP_REGNUM,        /* 28 */
  97.   NIOS2_EA_REGNUM,        /* 29 */
  98.   NIOS2_BA_REGNUM,        /* 30 */
  99.   NIOS2_RA_REGNUM,        /* 31 */
  100.   NIOS2_PC_REGNUM,        /* 32 */
  101.   NIOS2_STATUS_REGNUM,    /* 33 */
  102.   NIOS2_ESTATUS_REGNUM,   /* 34 */
  103.   NIOS2_BSTATUS_REGNUM,   /* 35 */
  104.   NIOS2_IENABLE_REGNUM,   /* 36 */
  105.   NIOS2_IPENDING_REGNUM/* 37 */
  106.   NIOS2_CPUID_REGNUM,     /* 38 */
  107.   39, /* CTL6 */          /* 39 */
  108.   NIOS2_EXCEPTION_REGNUM, /* 40 */
  109.   NIOS2_PTEADDR_REGNUM,   /* 41 */
  110.   NIOS2_TLBACC_REGNUM,    /* 42 */
  111.   NIOS2_TLBMISC_REGNUM,   /* 43 */
  112.   NIOS2_ECCINJ_REGNUM,    /* 44 */
  113.   NIOS2_BADADDR_REGNUM,   /* 45 */
  114.   NIOS2_CONFIG_REGNUM,    /* 46 */
  115.   NIOS2_MPUBASE_REGNUM,   /* 47 */
  116.   NIOS2_MPUACC_REGNUM     /* 48 */
  117. };


  118. /* Implement the dwarf2_reg_to_regnum gdbarch method.  */

  119. static int
  120. nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
  121. {
  122.   if (dw_reg < 0 || dw_reg > NIOS2_NUM_REGS)
  123.     {
  124.       warning (_("Dwarf-2 uses unmapped register #%d"), dw_reg);
  125.       return dw_reg;
  126.     }

  127.   return nios2_dwarf2gdb_regno_map[dw_reg];
  128. }

  129. /* Canonical names for the 49 registers.  */

  130. static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
  131. {
  132.   "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
  133.   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  134.   "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  135.   "et", "bt", "gp", "sp", "fp", "ea", "sstatus", "ra",
  136.   "pc",
  137.   "status", "estatus", "bstatus", "ienable",
  138.   "ipending", "cpuid", "ctl6", "exception",
  139.   "pteaddr", "tlbacc", "tlbmisc", "eccinj",
  140.   "badaddr", "config", "mpubase", "mpuacc"
  141. };

  142. /* Implement the register_name gdbarch method.  */

  143. static const char *
  144. nios2_register_name (struct gdbarch *gdbarch, int regno)
  145. {
  146.   /* Use mnemonic aliases for GPRs.  */
  147.   if (regno >= 0 && regno < NIOS2_NUM_REGS)
  148.     return nios2_reg_names[regno];
  149.   else
  150.     return tdesc_register_name (gdbarch, regno);
  151. }

  152. /* Implement the register_type gdbarch method.  */

  153. static struct type *
  154. nios2_register_type (struct gdbarch *gdbarch, int regno)
  155. {
  156.   /* If the XML description has register information, use that to
  157.      determine the register type.  */
  158.   if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  159.     return tdesc_register_type (gdbarch, regno);

  160.   if (regno == NIOS2_PC_REGNUM)
  161.     return builtin_type (gdbarch)->builtin_func_ptr;
  162.   else if (regno == NIOS2_SP_REGNUM)
  163.     return builtin_type (gdbarch)->builtin_data_ptr;
  164.   else
  165.     return builtin_type (gdbarch)->builtin_uint32;
  166. }

  167. /* Given a return value in REGCACHE with a type VALTYPE,
  168.    extract and copy its value into VALBUF.  */

  169. static void
  170. nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
  171.                             struct regcache *regcache, gdb_byte *valbuf)
  172. {
  173.   int len = TYPE_LENGTH (valtype);

  174.   /* Return values of up to 8 bytes are returned in $r2 $r3.  */
  175.   if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
  176.     regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
  177.   else
  178.     {
  179.       gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
  180.                           + register_size (gdbarch, NIOS2_R3_REGNUM)));
  181.       regcache_cooked_read (regcache, NIOS2_R2_REGNUM, valbuf);
  182.       regcache_cooked_read (regcache, NIOS2_R3_REGNUM, valbuf + 4);
  183.     }
  184. }

  185. /* Write into appropriate registers a function return value
  186.    of type TYPE, given in virtual format.  */

  187. static void
  188. nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
  189.                           struct regcache *regcache, const gdb_byte *valbuf)
  190. {
  191.   int len = TYPE_LENGTH (valtype);

  192.   /* Return values of up to 8 bytes are returned in $r2 $r3.  */
  193.   if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
  194.     regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
  195.   else
  196.     {
  197.       gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
  198.                           + register_size (gdbarch, NIOS2_R3_REGNUM)));
  199.       regcache_cooked_write (regcache, NIOS2_R2_REGNUM, valbuf);
  200.       regcache_cooked_write (regcache, NIOS2_R3_REGNUM, valbuf + 4);
  201.     }
  202. }


  203. /* Set up the default values of the registers.  */

  204. static void
  205. nios2_setup_default (struct nios2_unwind_cache *cache)
  206. {
  207.   int i;

  208.   for (i = 0; i < NIOS2_NUM_REGS; i++)
  209.   {
  210.     /* All registers start off holding their previous values.  */
  211.     cache->reg_value[i].reg    = i;
  212.     cache->reg_value[i].offset = 0;

  213.     /* All registers start off not saved.  */
  214.     cache->reg_saved[i].basereg = -1;
  215.     cache->reg_saved[i].addr    = 0;
  216.   }
  217. }

  218. /* Initialize the unwind cache.  */

  219. static void
  220. nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
  221. {
  222.   cache->base = 0;
  223.   cache->cfa = 0;
  224.   cache->pc = pc;
  225.   cache->return_regnum = NIOS2_RA_REGNUM;
  226.   nios2_setup_default (cache);
  227. }

  228. /* Read and identify an instruction at PC.  If INSNP is non-null,
  229.    store the instruction word into that location.  Return the opcode
  230.    pointer or NULL if the memory couldn't be read or disassembled.  */

  231. static const struct nios2_opcode *
  232. nios2_fetch_insn (struct gdbarch *gdbarch, CORE_ADDR pc,
  233.                   unsigned int *insnp)
  234. {
  235.   LONGEST memword;
  236.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  237.   unsigned int insn;

  238.   if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
  239.                                  gdbarch_byte_order (gdbarch), &memword))
  240.     return NULL;

  241.   insn = (unsigned int) memword;
  242.   if (insnp)
  243.     *insnp = insn;
  244.   return nios2_find_opcode_hash (insn, mach);
  245. }


  246. /* Match and disassemble an ADD-type instruction, with 3 register operands.
  247.    Returns true on success, and fills in the operand pointers.  */

  248. static int
  249. nios2_match_add (uint32_t insn, const struct nios2_opcode *op,
  250.                  unsigned long mach, int *ra, int *rb, int *rc)
  251. {
  252.   if (op->match == MATCH_R1_ADD || op->match == MATCH_R1_MOV)
  253.     {
  254.       *ra = GET_IW_R_A (insn);
  255.       *rb = GET_IW_R_B (insn);
  256.       *rc = GET_IW_R_C (insn);
  257.       return 1;
  258.     }
  259.   return 0;
  260. }

  261. /* Match and disassemble a SUB-type instruction, with 3 register operands.
  262.    Returns true on success, and fills in the operand pointers.  */

  263. static int
  264. nios2_match_sub (uint32_t insn, const struct nios2_opcode *op,
  265.                  unsigned long mach, int *ra, int *rb, int *rc)
  266. {
  267.   if (op->match == MATCH_R1_SUB)
  268.     {
  269.       *ra = GET_IW_R_A (insn);
  270.       *rb = GET_IW_R_B (insn);
  271.       *rc = GET_IW_R_C (insn);
  272.       return 1;
  273.     }
  274.   return 0;
  275. }

  276. /* Match and disassemble an ADDI-type instruction, with 2 register operands
  277.    and one immediate operand.
  278.    Returns true on success, and fills in the operand pointers.  */

  279. static int
  280. nios2_match_addi (uint32_t insn, const struct nios2_opcode *op,
  281.                   unsigned long mach, int *ra, int *rb, int *imm)
  282. {
  283.   if (op->match == MATCH_R1_ADDI)
  284.     {
  285.       *ra = GET_IW_I_A (insn);
  286.       *rb = GET_IW_I_B (insn);
  287.       *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  288.       return 1;
  289.     }
  290.   return 0;
  291. }

  292. /* Match and disassemble an ORHI-type instruction, with 2 register operands
  293.    and one unsigned immediate operand.
  294.    Returns true on success, and fills in the operand pointers.  */

  295. static int
  296. nios2_match_orhi (uint32_t insn, const struct nios2_opcode *op,
  297.                   unsigned long mach, int *ra, int *rb, unsigned int *uimm)
  298. {
  299.   if (op->match == MATCH_R1_ORHI)
  300.     {
  301.       *ra = GET_IW_I_A (insn);
  302.       *rb = GET_IW_I_B (insn);
  303.       *uimm = GET_IW_I_IMM16 (insn);
  304.       return 1;
  305.     }
  306.   return 0;
  307. }

  308. /* Match and disassemble a STW-type instruction, with 2 register operands
  309.    and one immediate operand.
  310.    Returns true on success, and fills in the operand pointers.  */

  311. static int
  312. nios2_match_stw (uint32_t insn, const struct nios2_opcode *op,
  313.                  unsigned long mach, int *ra, int *rb, int *imm)
  314. {
  315.   if (op->match == MATCH_R1_STW || op->match == MATCH_R1_STWIO)
  316.     {
  317.       *ra = GET_IW_I_A (insn);
  318.       *rb = GET_IW_I_B (insn);
  319.       *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  320.       return 1;
  321.     }
  322.   return 0;
  323. }

  324. /* Match and disassemble a LDW-type instruction, with 2 register operands
  325.    and one immediate operand.
  326.    Returns true on success, and fills in the operand pointers.  */

  327. static int
  328. nios2_match_ldw (uint32_t insn, const struct nios2_opcode *op,
  329.                  unsigned long mach, int *ra, int *rb, int *imm)
  330. {
  331.   if (op->match == MATCH_R1_LDW || op->match == MATCH_R1_LDWIO)
  332.     {
  333.       *ra = GET_IW_I_A (insn);
  334.       *rb = GET_IW_I_B (insn);
  335.       *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  336.       return 1;
  337.     }
  338.   return 0;
  339. }

  340. /* Match and disassemble a RDCTL instruction, with 2 register operands.
  341.    Returns true on success, and fills in the operand pointers.  */

  342. static int
  343. nios2_match_rdctl (uint32_t insn, const struct nios2_opcode *op,
  344.                    unsigned long mach, int *ra, int *rc)
  345. {
  346.   if (op->match == MATCH_R1_RDCTL)
  347.     {
  348.       *ra = GET_IW_R_IMM5 (insn);
  349.       *rc = GET_IW_R_C (insn);
  350.       return 1;
  351.     }
  352.   return 0;
  353. }


  354. /* Match and disassemble a branch instruction, with (potentially)
  355.    2 register operands and one immediate operand.
  356.    Returns true on success, and fills in the operand pointers.  */

  357. enum branch_condition {
  358.   branch_none,
  359.   branch_eq,
  360.   branch_ne,
  361.   branch_ge,
  362.   branch_geu,
  363.   branch_lt,
  364.   branch_ltu
  365. };

  366. static int
  367. nios2_match_branch (uint32_t insn, const struct nios2_opcode *op,
  368.                     unsigned long mach, int *ra, int *rb, int *imm,
  369.                     enum branch_condition *cond)
  370. {
  371.   switch (op->match)
  372.     {
  373.     case MATCH_R1_BR:
  374.       *cond = branch_none;
  375.       break;
  376.     case MATCH_R1_BEQ:
  377.       *cond = branch_eq;
  378.       break;
  379.     case MATCH_R1_BNE:
  380.       *cond = branch_ne;
  381.       break;
  382.     case MATCH_R1_BGE:
  383.       *cond = branch_ge;
  384.       break;
  385.     case MATCH_R1_BGEU:
  386.       *cond = branch_geu;
  387.       break;
  388.     case MATCH_R1_BLT:
  389.       *cond = branch_lt;
  390.       break;
  391.     case MATCH_R1_BLTU:
  392.       *cond = branch_ltu;
  393.       break;
  394.     default:
  395.       return 0;
  396.     }
  397.   *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  398.   *ra = GET_IW_I_A (insn);
  399.   *rb = GET_IW_I_B (insn);
  400.   return 1;
  401. }

  402. /* Match and disassemble a direct jump instruction, with an
  403.    unsigned operand.  Returns true on success, and fills in the operand
  404.    pointer.  */

  405. static int
  406. nios2_match_jmpi (uint32_t insn, const struct nios2_opcode *op,
  407.                   unsigned long mach, unsigned int *uimm)
  408. {
  409.   if (op->match == MATCH_R1_JMPI)
  410.     {
  411.       *uimm = GET_IW_J_IMM26 (insn) << 2;
  412.       return 1;
  413.     }
  414.   return 0;
  415. }

  416. /* Match and disassemble a direct call instruction, with an
  417.    unsigned operand.  Returns true on success, and fills in the operand
  418.    pointer.  */

  419. static int
  420. nios2_match_calli (uint32_t insn, const struct nios2_opcode *op,
  421.                    unsigned long mach, unsigned int *uimm)
  422. {
  423.   if (op->match == MATCH_R1_CALL)
  424.     {
  425.       *uimm = GET_IW_J_IMM26 (insn) << 2;
  426.       return 1;
  427.     }
  428.   return 0;
  429. }

  430. /* Match and disassemble an indirect jump instruction, with a
  431.    (possibly implicit) register operand.  Returns true on success, and fills
  432.    in the operand pointer.  */

  433. static int
  434. nios2_match_jmpr (uint32_t insn, const struct nios2_opcode *op,
  435.                   unsigned long mach, int *ra)
  436. {
  437.   switch (op->match)
  438.     {
  439.     case MATCH_R1_JMP:
  440.       *ra = GET_IW_I_A (insn);
  441.       return 1;
  442.     case MATCH_R1_RET:
  443.       *ra = NIOS2_RA_REGNUM;
  444.       return 1;
  445.     case MATCH_R1_ERET:
  446.       *ra = NIOS2_EA_REGNUM;
  447.       return 1;
  448.     case MATCH_R1_BRET:
  449.       *ra = NIOS2_BA_REGNUM;
  450.       return 1;
  451.     default:
  452.       return 0;
  453.     }
  454. }

  455. /* Match and disassemble an indirect call instruction, with a register
  456.    operand.  Returns true on success, and fills in the operand pointer.  */

  457. static int
  458. nios2_match_callr (uint32_t insn, const struct nios2_opcode *op,
  459.                    unsigned long mach, int *ra)
  460. {
  461.   if (op->match == MATCH_R1_CALLR)
  462.     {
  463.       *ra = GET_IW_I_A (insn);
  464.       return 1;
  465.     }
  466.   return 0;
  467. }

  468. /* Match and disassemble a break instruction, with an unsigned operand.
  469.    Returns true on success, and fills in the operand pointer.  */

  470. static int
  471. nios2_match_break (uint32_t insn, const struct nios2_opcode *op,
  472.                   unsigned long mach, unsigned int *uimm)
  473. {
  474.   if (op->match == MATCH_R1_BREAK)
  475.     {
  476.       *uimm = GET_IW_R_IMM5 (insn);
  477.       return 1;
  478.     }
  479.   return 0;
  480. }

  481. /* Match and disassemble a trap instruction, with an unsigned operand.
  482.    Returns true on success, and fills in the operand pointer.  */

  483. static int
  484. nios2_match_trap (uint32_t insn, const struct nios2_opcode *op,
  485.                   unsigned long mach, unsigned int *uimm)
  486. {
  487.   if (op->match == MATCH_R1_TRAP)
  488.     {
  489.       *uimm = GET_IW_R_IMM5 (insn);
  490.       return 1;
  491.     }
  492.   return 0;
  493. }

  494. /* Helper function to identify when we're in a function epilogue;
  495.    that is, the part of the function from the point at which the
  496.    stack adjustments are made, to the return or sibcall.
  497.    Note that we may have several stack adjustment instructions, and
  498.    this function needs to test whether the stack teardown has already
  499.    started before current_pc, not whether it has completed.  */

  500. static int
  501. nios2_in_epilogue_p (struct gdbarch *gdbarch,
  502.                      CORE_ADDR current_pc,
  503.                      CORE_ADDR start_pc)
  504. {
  505.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  506.   /* Maximum number of possibly-epilogue instructions to check.
  507.      Note that this number should not be too large, else we can
  508.      potentially end up iterating through unmapped memory.  */
  509.   int ninsns, max_insns = 5;
  510.   unsigned int insn;
  511.   const struct nios2_opcode *op = NULL;
  512.   unsigned int uimm;
  513.   int imm;
  514.   int ra, rb, rc;
  515.   enum branch_condition cond;
  516.   CORE_ADDR pc;

  517.   /* There has to be a previous instruction in the function.  */
  518.   if (current_pc <= start_pc)
  519.     return 0;

  520.   /* Find the previous instruction before current_pc.
  521.      For the moment we will assume that all instructions are the
  522.      same size here.  */
  523.   pc = current_pc - NIOS2_OPCODE_SIZE;

  524.   /* Beginning with the previous instruction we just located, check whether
  525.      we are in a sequence of at least one stack adjustment instruction.
  526.      Possible instructions here include:
  527.          ADDI sp, sp, n
  528.          ADD sp, sp, rn
  529.          LDW sp, n(sp)  */
  530.   for (ninsns = 0; ninsns < max_insns; ninsns++)
  531.     {
  532.       int ok = 0;

  533.       /* Fetch the insn at pc.  */
  534.       op = nios2_fetch_insn (gdbarch, pc, &insn);
  535.       if (op == NULL)
  536.         return 0;
  537.       pc += op->size;

  538.       /* Was it a stack adjustment?  */
  539.       if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
  540.         ok = (rb == NIOS2_SP_REGNUM);
  541.       else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
  542.         ok = (rc == NIOS2_SP_REGNUM);
  543.       else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
  544.         ok = (rb == NIOS2_SP_REGNUM);
  545.       if (!ok)
  546.         break;
  547.     }

  548.   /* No stack adjustments found.  */
  549.   if (ninsns == 0)
  550.     return 0;

  551.   /* We found more stack adjustments than we expect GCC to be generating.
  552.      Since it looks like a stack unwind might be in progress tell GDB to
  553.      treat it as such.  */
  554.   if (ninsns == max_insns)
  555.     return 1;

  556.   /* The next instruction following the stack adjustments must be a
  557.      return, jump, or unconditional branch.  */
  558.   if (nios2_match_jmpr (insn, op, mach, &ra)
  559.       || nios2_match_jmpi (insn, op, mach, &uimm)
  560.       || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
  561.           && cond == branch_none))
  562.     return 1;

  563.   return 0;
  564. }

  565. /* Implement the in_function_epilogue_p gdbarch method.  */

  566. static int
  567. nios2_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  568. {
  569.   CORE_ADDR func_addr;

  570.   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  571.     return nios2_in_epilogue_p (gdbarch, pc, func_addr);

  572.   return 0;
  573. }

  574. /* Do prologue analysis, returning the PC of the first instruction
  575.    after the function prologue.  Assumes CACHE has already been
  576.    initialized.  THIS_FRAME can be null, in which case we are only
  577.    interested in skipping the prologue.  Otherwise CACHE is filled in
  578.    from the frame information.

  579.    The prologue may consist of the following parts:
  580.      1) Profiling instrumentation.  For non-PIC code it looks like:
  581.           mov         r8, ra
  582.           call         mcount
  583.           mov         ra, r8

  584.      2) A stack adjustment and save of R4-R7 for varargs functions.
  585.         This is typically merged with item 3.

  586.      3) A stack adjustment and save of the callee-saved registers;
  587.         typically an explicit SP decrement and individual register
  588.         saves.

  589.         There may also be a stack switch here in an exception handler
  590.         in place of a stack adjustment.  It looks like:
  591.           movhi  rx, %hiadj(newstack)
  592.           addhi  rx, rx, %lo(newstack)
  593.           stw    sp, constant(rx)
  594.           mov    sp, rx

  595.      5) A frame pointer save, which can be either a MOV or ADDI.

  596.      6) A further stack pointer adjustment.  This is normally included
  597.         adjustment in step 4 unless the total adjustment is too large
  598.         to be done in one step.

  599.      7) A stack overflow check, which can take either of these forms:
  600.           bgeu   sp, rx, +8
  601.           break  3
  602.         or
  603.           bltu   sp, rx, .Lstack_overflow
  604.           ...
  605.         .Lstack_overflow:
  606.           break  3
  607.         If present, this is inserted after the stack pointer adjustments
  608.         for steps 3, 4, and 6.

  609.     The prologue instructions may be combined or interleaved with other
  610.     instructions.

  611.     To cope with all this variability we decode all the instructions
  612.     from the start of the prologue until we hit an instruction that
  613.     cannot possibly be a prologue instruction, such as a branch, call,
  614.     return, or epilogue instruction.  The prologue is considered to end
  615.     at the last instruction that can definitely be considered a
  616.     prologue instruction.  */

  617. static CORE_ADDR
  618. nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
  619.                         const CORE_ADDR current_pc,
  620.                         struct nios2_unwind_cache *cache,
  621.                         struct frame_info *this_frame)
  622. {
  623.   /* Maximum number of possibly-prologue instructions to check.
  624.      Note that this number should not be too large, else we can
  625.      potentially end up iterating through unmapped memory.  */
  626.   int ninsns, max_insns = 50;
  627.   int regno;
  628.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  629.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;

  630.   /* Does the frame set up the FP register?  */
  631.   int base_reg = 0;

  632.   struct reg_value *value = cache->reg_value;
  633.   struct reg_value temp_value[NIOS2_NUM_REGS];

  634.   int i;

  635.   /* Save the starting PC so we can correct the pc after running
  636.      through the prolog, using symbol info.  */
  637.   CORE_ADDR pc = start_pc;

  638.   /* Is this an exception handler?  */
  639.   int exception_handler = 0;

  640.   /* What was the original value of SP (or fake original value for
  641.      functions which switch stacks?  */
  642.   CORE_ADDR frame_high;

  643.   /* The last definitely-prologue instruction seen.  */
  644.   CORE_ADDR prologue_end;

  645.   /* Is this the innermost function?  */
  646.   int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);

  647.   if (nios2_debug)
  648.     fprintf_unfiltered (gdb_stdlog,
  649.                         "{ nios2_analyze_prologue start=%s, current=%s ",
  650.                         paddress (gdbarch, start_pc),
  651.                         paddress (gdbarch, current_pc));

  652.   /* Set up the default values of the registers.  */
  653.   nios2_setup_default (cache);

  654.   /* Find the prologue instructions.  */
  655.   prologue_end = start_pc;
  656.   for (ninsns = 0; ninsns < max_insns; ninsns++)
  657.     {
  658.       /* Present instruction.  */
  659.       uint32_t insn;
  660.       const struct nios2_opcode *op;
  661.       int ra, rb, rc, imm;
  662.       unsigned int uimm;
  663.       unsigned int reglist;
  664.       int wb, ret;
  665.       enum branch_condition cond;

  666.       if (pc == current_pc)
  667.       {
  668.         /* When we reach the current PC we must save the current
  669.            register state (for the backtrace) but keep analysing
  670.            because there might be more to find out (eg. is this an
  671.            exception handler).  */
  672.         memcpy (temp_value, value, sizeof (temp_value));
  673.         value = temp_value;
  674.         if (nios2_debug)
  675.           fprintf_unfiltered (gdb_stdlog, "*");
  676.       }

  677.       op = nios2_fetch_insn (gdbarch, pc, &insn);

  678.       /* Unknown opcode?  Stop scanning.  */
  679.       if (op == NULL)
  680.         break;
  681.       pc += op->size;

  682.       if (nios2_debug)
  683.         fprintf_unfiltered (gdb_stdlog, "[%08X]", insn);

  684.       /* The following instructions can appear in the prologue.  */

  685.       if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
  686.         {
  687.           /* ADD   rc, ra, rb  (also used for MOV) */
  688.           if (rc == NIOS2_SP_REGNUM
  689.               && rb == 0
  690.               && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
  691.             {
  692.               /* If the previous value of SP is available somewhere
  693.                  near the new stack pointer value then this is a
  694.                  stack switch.  */

  695.               /* If any registers were saved on the stack before then
  696.                  we can't backtrace into them now.  */
  697.               for (i = 0 ; i < NIOS2_NUM_REGS ; i++)
  698.                 {
  699.                   if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
  700.                     cache->reg_saved[i].basereg = -1;
  701.                   if (value[i].reg == NIOS2_SP_REGNUM)
  702.                     value[i].reg = -1;
  703.                 }

  704.               /* Create a fake "high water mark" 4 bytes above where SP
  705.                  was stored and fake up the registers to be consistent
  706.                  with that.  */
  707.               value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
  708.               value[NIOS2_SP_REGNUM].offset
  709.                 = (value[ra].offset
  710.                    - cache->reg_saved[NIOS2_SP_REGNUM].addr
  711.                    - 4);
  712.               cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
  713.               cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
  714.             }

  715.           else if (rc == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
  716.             /* This is setting SP from FP.  This only happens in the
  717.                function epilogue.  */
  718.             break;

  719.           else if (rc != 0)
  720.             {
  721.               if (value[rb].reg == 0)
  722.                 value[rc].reg = value[ra].reg;
  723.               else if (value[ra].reg == 0)
  724.                 value[rc].reg = value[rb].reg;
  725.               else
  726.                 value[rc].reg = -1;
  727.               value[rc].offset = value[ra].offset + value[rb].offset;
  728.             }

  729.           /* The add/move is only considered a prologue instruction
  730.              if the destination is SP or FP.  */
  731.           if (rc == NIOS2_SP_REGNUM || rc == NIOS2_FP_REGNUM)
  732.             prologue_end = pc;
  733.         }

  734.       else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc))
  735.         {
  736.           /* SUB   rc, ra, rb */
  737.           if (rc == NIOS2_SP_REGNUM && rb == NIOS2_SP_REGNUM
  738.               && value[rc].reg != 0)
  739.             /* If we are decrementing the SP by a non-constant amount,
  740.                this is alloca, not part of the prologue.  */
  741.             break;
  742.           else if (rc != 0)
  743.             {
  744.               if (value[rb].reg == 0)
  745.                 value[rc].reg = value[ra].reg;
  746.               else
  747.                 value[rc].reg = -1;
  748.               value[rc].offset = value[ra].offset - value[rb].offset;
  749.             }
  750.         }

  751.       else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
  752.         {
  753.           /* ADDI    rb, ra, imm */

  754.           /* A positive stack adjustment has to be part of the epilogue.  */
  755.           if (rb == NIOS2_SP_REGNUM
  756.               && (imm > 0 || value[ra].reg != NIOS2_SP_REGNUM))
  757.             break;

  758.           /* Likewise restoring SP from FP.  */
  759.           else if (rb == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
  760.             break;

  761.           if (rb != 0)
  762.             {
  763.               value[rb].reg    = value[ra].reg;
  764.               value[rb].offset = value[ra].offset + imm;
  765.             }

  766.           /* The add is only considered a prologue instruction
  767.              if the destination is SP or FP.  */
  768.           if (rb == NIOS2_SP_REGNUM || rb == NIOS2_FP_REGNUM)
  769.             prologue_end = pc;
  770.         }

  771.       else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm))
  772.         {
  773.           /* ORHI  rb, ra, uimm   (also used for MOVHI) */
  774.           if (rb != 0)
  775.             {
  776.                 value[rb].reg    = (value[ra].reg == 0) ? 0 : -1;
  777.               value[rb].offset = value[ra].offset | (uimm << 16);
  778.             }
  779.         }

  780.       else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm))
  781.         {
  782.           /* STW rb, imm(ra) */

  783.           /* Are we storing the original value of a register to the stack?
  784.              For exception handlers the value of EA-4 (return
  785.              address from interrupts etc) is sometimes stored.  */
  786.           int orig = value[rb].reg;
  787.           if (orig > 0
  788.               && (value[rb].offset == 0
  789.                   || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4))
  790.               && ((value[ra].reg == NIOS2_SP_REGNUM
  791.                    && cache->reg_saved[orig].basereg != NIOS2_SP_REGNUM)
  792.                   || cache->reg_saved[orig].basereg == -1))
  793.             {
  794.               if (pc < current_pc)
  795.                 {
  796.                   /* Save off callee saved registers.  */
  797.                   cache->reg_saved[orig].basereg = value[ra].reg;
  798.                   cache->reg_saved[orig].addr = value[ra].offset + imm;
  799.                 }

  800.               prologue_end = pc;

  801.               if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
  802.                 exception_handler = 1;
  803.             }
  804.           else
  805.             /* Non-stack memory writes cannot appear in the prologue.  */
  806.             break;
  807.         }

  808.       else if (nios2_match_rdctl (insn, op, mach, &ra, &rc))
  809.         {
  810.           /* RDCTL rC, ctlN
  811.              This can appear in exception handlers in combination with
  812.              a subsequent save to the stack frame.  */
  813.           if (rc != 0)
  814.             {
  815.               value[rc].reg    = NIOS2_STATUS_REGNUM + ra;
  816.               value[rc].offset = 0;
  817.             }
  818.         }

  819.       else if (nios2_match_calli (insn, op, mach, &uimm))
  820.         {
  821.           if (value[8].reg == NIOS2_RA_REGNUM
  822.               && value[8].offset == 0
  823.               && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
  824.               && value[NIOS2_SP_REGNUM].offset == 0)
  825.             {
  826.               /* A CALL instruction.  This is treated as a call to mcount
  827.                  if ra has been stored into r8 beforehand and if it's
  828.                  before the stack adjust.
  829.                  Note mcount corrupts r2-r3, r9-r15 & ra.  */
  830.               for (i = 2 ; i <= 3 ; i++)
  831.                 value[i].reg = -1;
  832.               for (i = 9 ; i <= 15 ; i++)
  833.                 value[i].reg = -1;
  834.               value[NIOS2_RA_REGNUM].reg = -1;

  835.               prologue_end = pc;
  836.             }

  837.           /* Other calls are not part of the prologue.  */
  838.           else
  839.             break;
  840.         }

  841.       else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
  842.         {
  843.           /* Branches not involving a stack overflow check aren't part of
  844.              the prologue.  */
  845.           if (ra != NIOS2_SP_REGNUM)
  846.             break;
  847.           else if (cond == branch_geu)
  848.             {
  849.               /* BGEU sp, rx, +8
  850.                  BREAK 3
  851.                  This instruction sequence is used in stack checking;
  852.                  we can ignore it.  */
  853.               unsigned int next_insn;
  854.               const struct nios2_opcode *next_op
  855.                 = nios2_fetch_insn (gdbarch, pc, &next_insn);
  856.               if (next_op != NULL
  857.                   && nios2_match_break (next_insn, op, mach, &uimm))
  858.                 pc += next_op->size;
  859.               else
  860.                 break;
  861.             }
  862.           else if (cond == branch_ltu)
  863.             {
  864.               /* BLTU sp, rx, .Lstackoverflow
  865.                  If the location branched to holds a BREAK 3 instruction
  866.                  then this is also stack overflow detection.  */
  867.               unsigned int next_insn;
  868.               const struct nios2_opcode *next_op
  869.                 = nios2_fetch_insn (gdbarch, pc + imm, &next_insn);
  870.               if (next_op != NULL
  871.                   && nios2_match_break (next_insn, op, mach, &uimm))
  872.                 ;
  873.               else
  874.                 break;
  875.             }
  876.           else
  877.             break;
  878.         }

  879.       /* All other calls or jumps (including returns) terminate
  880.          the prologue.  */
  881.       else if (nios2_match_callr (insn, op, mach, &ra)
  882.                || nios2_match_jmpr (insn, op, mach, &ra)
  883.                || nios2_match_jmpi (insn, op, mach, &uimm))
  884.         break;
  885.     }

  886.   /* If THIS_FRAME is NULL, we are being called from skip_prologue
  887.      and are only interested in the PROLOGUE_END value, so just
  888.      return that now and skip over the cache updates, which depend
  889.      on having frame information.  */
  890.   if (this_frame == NULL)
  891.     return prologue_end;

  892.   /* If we are in the function epilogue and have already popped
  893.      registers off the stack in preparation for returning, then we
  894.      want to go back to the original register values.  */
  895.   if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
  896.     nios2_setup_default (cache);

  897.   /* Exception handlers use a different return address register.  */
  898.   if (exception_handler)
  899.     cache->return_regnum = NIOS2_EA_REGNUM;

  900.   if (nios2_debug)
  901.     fprintf_unfiltered (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);

  902.   if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
  903.     /* If the FP now holds an offset from the CFA then this is a
  904.        normal frame which uses the frame pointer.  */
  905.     base_reg = NIOS2_FP_REGNUM;
  906.   else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
  907.     /* FP doesn't hold an offset from the CFA.  If SP still holds an
  908.        offset from the CFA then we might be in a function which omits
  909.        the frame pointer, or we might be partway through the prologue.
  910.        In both cases we can find the CFA using SP.  */
  911.     base_reg = NIOS2_SP_REGNUM;
  912.   else
  913.     {
  914.       /* Somehow the stack pointer has been corrupted.
  915.          We can't return.  */
  916.       if (nios2_debug)
  917.         fprintf_unfiltered (gdb_stdlog, "<can't reach cfa> }\n");
  918.       return 0;
  919.     }

  920.   if (cache->reg_value[base_reg].offset == 0
  921.       || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
  922.       || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
  923.     {
  924.       /* If the frame didn't adjust the stack, didn't save RA or
  925.          didn't save EA in an exception handler then it must either
  926.          be a leaf function (doesn't call any other functions) or it
  927.          can't return.  If it has called another function then it
  928.          can't be a leaf, so set base == 0 to indicate that we can't
  929.          backtrace past it.  */

  930.       if (!innermost)
  931.         {
  932.           /* If it isn't the innermost function then it can't be a
  933.              leaf, unless it was interrupted.  Check whether RA for
  934.              this frame is the same as PC.  If so then it probably
  935.              wasn't interrupted.  */
  936.           CORE_ADDR ra
  937.             = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);

  938.           if (ra == current_pc)
  939.             {
  940.               if (nios2_debug)
  941.                 fprintf_unfiltered
  942.                   (gdb_stdlog,
  943.                    "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
  944.                    paddress (gdbarch, cache->reg_value[base_reg].offset),
  945.                    cache->reg_saved[NIOS2_RA_REGNUM].basereg,
  946.                    cache->return_regnum,
  947.                    cache->reg_saved[cache->return_regnum].basereg);
  948.               return 0;
  949.             }
  950.         }
  951.     }

  952.   /* Get the value of whichever register we are using for the
  953.      base.  */
  954.   cache->base = get_frame_register_unsigned (this_frame, base_reg);

  955.   /* What was the value of SP at the start of this function (or just
  956.      after the stack switch).  */
  957.   frame_high = cache->base - cache->reg_value[base_reg].offset;

  958.   /* Adjust all the saved registers such that they contain addresses
  959.      instead of offsets.  */
  960.   for (i = 0; i < NIOS2_NUM_REGS; i++)
  961.     if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
  962.       {
  963.         cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
  964.         cache->reg_saved[i].addr += frame_high;
  965.       }

  966.   for (i = 0; i < NIOS2_NUM_REGS; i++)
  967.     if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
  968.       {
  969.         CORE_ADDR gp = get_frame_register_unsigned (this_frame,
  970.                                                     NIOS2_GP_REGNUM);

  971.         for ( ; i < NIOS2_NUM_REGS; i++)
  972.           if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
  973.             {
  974.               cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
  975.               cache->reg_saved[i].addr += gp;
  976.             }
  977.       }

  978.   /* Work out what the value of SP was on the first instruction of
  979.      this function.  If we didn't switch stacks then this can be
  980.      trivially computed from the base address.  */
  981.   if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
  982.     cache->cfa
  983.       = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
  984.                                       4, byte_order);
  985.   else
  986.     cache->cfa = frame_high;

  987.   /* Exception handlers restore ESTATUS into STATUS.  */
  988.   if (exception_handler)
  989.     {
  990.       cache->reg_saved[NIOS2_STATUS_REGNUM]
  991.         = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
  992.       cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
  993.     }

  994.   if (nios2_debug)
  995.     fprintf_unfiltered (gdb_stdlog, "cfa=%s }\n",
  996.                         paddress (gdbarch, cache->cfa));

  997.   return prologue_end;
  998. }

  999. /* Implement the skip_prologue gdbarch hook.  */

  1000. static CORE_ADDR
  1001. nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  1002. {
  1003.   CORE_ADDR func_addr;

  1004.   struct nios2_unwind_cache cache;

  1005.   /* See if we can determine the end of the prologue via the symbol
  1006.      table.  If so, then return either PC, or the PC after the
  1007.      prologue, whichever is greater.  */
  1008.   if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
  1009.     {
  1010.       CORE_ADDR post_prologue_pc
  1011.         = skip_prologue_using_sal (gdbarch, func_addr);

  1012.       if (post_prologue_pc != 0)
  1013.         return max (start_pc, post_prologue_pc);
  1014.     }

  1015.   /* Prologue analysis does the rest....  */
  1016.   nios2_init_cache (&cache, start_pc);
  1017.   return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
  1018. }

  1019. /* Implement the breakpoint_from_pc gdbarch hook.  */

  1020. static const gdb_byte*
  1021. nios2_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
  1022.                           int *bp_size)
  1023. {
  1024.   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
  1025.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;

  1026.   /* R1 break encoding:
  1027.      ((0x1e << 17) | (0x34 << 11) | (0x1f << 6) | (0x3a << 0))
  1028.      0x003da7fa */
  1029.   static const gdb_byte r1_breakpoint_le[] = {0xfa, 0xa7, 0x3d, 0x0};
  1030.   static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3d, 0xa7, 0xfa};
  1031.   *bp_size = NIOS2_OPCODE_SIZE;
  1032.   if (byte_order_for_code == BFD_ENDIAN_BIG)
  1033.     return r1_breakpoint_be;
  1034.   else
  1035.     return r1_breakpoint_le;
  1036. }

  1037. /* Implement the print_insn gdbarch method.  */

  1038. static int
  1039. nios2_print_insn (bfd_vma memaddr, disassemble_info *info)
  1040. {
  1041.   if (info->endian == BFD_ENDIAN_BIG)
  1042.     return print_insn_big_nios2 (memaddr, info);
  1043.   else
  1044.     return print_insn_little_nios2 (memaddr, info);
  1045. }


  1046. /* Implement the frame_align gdbarch method.  */

  1047. static CORE_ADDR
  1048. nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  1049. {
  1050.   return align_down (addr, 4);
  1051. }


  1052. /* Implement the return_value gdbarch method.  */

  1053. static enum return_value_convention
  1054. nios2_return_value (struct gdbarch *gdbarch, struct value *function,
  1055.                     struct type *type, struct regcache *regcache,
  1056.                     gdb_byte *readbuf, const gdb_byte *writebuf)
  1057. {
  1058.   if (TYPE_LENGTH (type) > 8)
  1059.     return RETURN_VALUE_STRUCT_CONVENTION;

  1060.   if (readbuf)
  1061.     nios2_extract_return_value (gdbarch, type, regcache, readbuf);
  1062.   if (writebuf)
  1063.     nios2_store_return_value (gdbarch, type, regcache, writebuf);

  1064.   return RETURN_VALUE_REGISTER_CONVENTION;
  1065. }

  1066. /* Implement the dummy_id gdbarch method.  */

  1067. static struct frame_id
  1068. nios2_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1069. {
  1070.   return frame_id_build
  1071.     (get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM),
  1072.      get_frame_pc (this_frame));
  1073. }

  1074. /* Implement the push_dummy_call gdbarch method.  */

  1075. static CORE_ADDR
  1076. nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1077.                        struct regcache *regcache, CORE_ADDR bp_addr,
  1078.                        int nargs, struct value **args, CORE_ADDR sp,
  1079.                        int struct_return, CORE_ADDR struct_addr)
  1080. {
  1081.   int argreg;
  1082.   int float_argreg;
  1083.   int argnum;
  1084.   int len = 0;
  1085.   int stack_offset = 0;
  1086.   CORE_ADDR func_addr = find_function_addr (function, NULL);
  1087.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1088.   /* Set the return address register to point to the entry point of
  1089.      the program, where a breakpoint lies in wait.  */
  1090.   regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);

  1091.   /* Now make space on the stack for the args.  */
  1092.   for (argnum = 0; argnum < nargs; argnum++)
  1093.     len += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
  1094.   sp -= len;

  1095.   /* Initialize the register pointer.  */
  1096.   argreg = NIOS2_FIRST_ARGREG;

  1097.   /* The struct_return pointer occupies the first parameter-passing
  1098.      register.  */
  1099.   if (struct_return)
  1100.     regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);

  1101.   /* Now load as many as possible of the first arguments into
  1102.      registers, and push the rest onto the stack.  Loop through args
  1103.      from first to last.  */
  1104.   for (argnum = 0; argnum < nargs; argnum++)
  1105.     {
  1106.       const gdb_byte *val;
  1107.       gdb_byte valbuf[MAX_REGISTER_SIZE];
  1108.       struct value *arg = args[argnum];
  1109.       struct type *arg_type = check_typedef (value_type (arg));
  1110.       int len = TYPE_LENGTH (arg_type);
  1111.       enum type_code typecode = TYPE_CODE (arg_type);

  1112.       val = value_contents (arg);

  1113.       /* Copy the argument to general registers or the stack in
  1114.          register-sized pieces.  Large arguments are split between
  1115.          registers and stack.  */
  1116.       while (len > 0)
  1117.         {
  1118.           int partial_len = (len < 4 ? len : 4);

  1119.           if (argreg <= NIOS2_LAST_ARGREG)
  1120.             {
  1121.               /* The argument is being passed in a register.  */
  1122.               CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
  1123.                                                            byte_order);

  1124.               regcache_cooked_write_unsigned (regcache, argreg, regval);
  1125.               argreg++;
  1126.             }
  1127.           else
  1128.             {
  1129.               /* The argument is being passed on the stack.  */
  1130.               CORE_ADDR addr = sp + stack_offset;

  1131.               write_memory (addr, val, partial_len);
  1132.               stack_offset += align_up (partial_len, 4);
  1133.             }

  1134.           len -= partial_len;
  1135.           val += partial_len;
  1136.         }
  1137.     }

  1138.   regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);

  1139.   /* Return adjusted stack pointer.  */
  1140.   return sp;
  1141. }

  1142. /* Implement the unwind_pc gdbarch method.  */

  1143. static CORE_ADDR
  1144. nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1145. {
  1146.   gdb_byte buf[4];

  1147.   frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
  1148.   return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  1149. }

  1150. /* Implement the unwind_sp gdbarch method.  */

  1151. static CORE_ADDR
  1152. nios2_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1153. {
  1154.   return frame_unwind_register_unsigned (this_frame, NIOS2_SP_REGNUM);
  1155. }

  1156. /* Use prologue analysis to fill in the register cache
  1157.    *THIS_PROLOGUE_CACHE for THIS_FRAME.  This function initializes
  1158.    *THIS_PROLOGUE_CACHE first.  */

  1159. static struct nios2_unwind_cache *
  1160. nios2_frame_unwind_cache (struct frame_info *this_frame,
  1161.                           void **this_prologue_cache)
  1162. {
  1163.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1164.   CORE_ADDR current_pc;
  1165.   struct nios2_unwind_cache *cache;
  1166.   int i;

  1167.   if (*this_prologue_cache)
  1168.     return *this_prologue_cache;

  1169.   cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
  1170.   *this_prologue_cache = cache;

  1171.   /* Zero all fields.  */
  1172.   nios2_init_cache (cache, get_frame_func (this_frame));

  1173.   /* Prologue analysis does the rest...  */
  1174.   current_pc = get_frame_pc (this_frame);
  1175.   if (cache->pc != 0)
  1176.     nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);

  1177.   return cache;
  1178. }

  1179. /* Implement the this_id function for the normal unwinder.  */

  1180. static void
  1181. nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1182.                      struct frame_id *this_id)
  1183. {
  1184.   struct nios2_unwind_cache *cache =
  1185.     nios2_frame_unwind_cache (this_frame, this_cache);

  1186.   /* This marks the outermost frame.  */
  1187.   if (cache->base == 0)
  1188.     return;

  1189.   *this_id = frame_id_build (cache->cfa, cache->pc);
  1190. }

  1191. /* Implement the prev_register function for the normal unwinder.  */

  1192. static struct value *
  1193. nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  1194.                            int regnum)
  1195. {
  1196.   struct nios2_unwind_cache *cache =
  1197.     nios2_frame_unwind_cache (this_frame, this_cache);

  1198.   gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);

  1199.   /* The PC of the previous frame is stored in the RA register of
  1200.      the current frame.  Frob regnum so that we pull the value from
  1201.      the correct place.  */
  1202.   if (regnum == NIOS2_PC_REGNUM)
  1203.     regnum = cache->return_regnum;

  1204.   if (regnum == NIOS2_SP_REGNUM && cache->cfa)
  1205.     return frame_unwind_got_constant (this_frame, regnum, cache->cfa);

  1206.   /* If we've worked out where a register is stored then load it from
  1207.      there.  */
  1208.   if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
  1209.     return frame_unwind_got_memory (this_frame, regnum,
  1210.                                     cache->reg_saved[regnum].addr);

  1211.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1212. }

  1213. /* Implement the this_base, this_locals, and this_args hooks
  1214.    for the normal unwinder.  */

  1215. static CORE_ADDR
  1216. nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1217. {
  1218.   struct nios2_unwind_cache *info
  1219.     = nios2_frame_unwind_cache (this_frame, this_cache);

  1220.   return info->base;
  1221. }

  1222. /* Data structures for the normal prologue-analysis-based
  1223.    unwinder.  */

  1224. static const struct frame_unwind nios2_frame_unwind =
  1225. {
  1226.   NORMAL_FRAME,
  1227.   default_frame_unwind_stop_reason,
  1228.   nios2_frame_this_id,
  1229.   nios2_frame_prev_register,
  1230.   NULL,
  1231.   default_frame_sniffer
  1232. };

  1233. static const struct frame_base nios2_frame_base =
  1234. {
  1235.   &nios2_frame_unwind,
  1236.   nios2_frame_base_address,
  1237.   nios2_frame_base_address,
  1238.   nios2_frame_base_address
  1239. };

  1240. /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
  1241.    in the stub unwinder.  */

  1242. static struct trad_frame_cache *
  1243. nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
  1244. {
  1245.   CORE_ADDR pc;
  1246.   CORE_ADDR start_addr;
  1247.   CORE_ADDR stack_addr;
  1248.   struct trad_frame_cache *this_trad_cache;
  1249.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1250.   int num_regs = gdbarch_num_regs (gdbarch);

  1251.   if (*this_cache != NULL)
  1252.     return *this_cache;
  1253.   this_trad_cache = trad_frame_cache_zalloc (this_frame);
  1254.   *this_cache = this_trad_cache;

  1255.   /* The return address is in the link register.  */
  1256.   trad_frame_set_reg_realreg (this_trad_cache,
  1257.                               gdbarch_pc_regnum (gdbarch),
  1258.                               NIOS2_RA_REGNUM);

  1259.   /* Frame ID, since it's a frameless / stackless function, no stack
  1260.      space is allocated and SP on entry is the current SP.  */
  1261.   pc = get_frame_pc (this_frame);
  1262.   find_pc_partial_function (pc, NULL, &start_addr, NULL);
  1263.   stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
  1264.   trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
  1265.   /* Assume that the frame's base is the same as the stack pointer.  */
  1266.   trad_frame_set_this_base (this_trad_cache, stack_addr);

  1267.   return this_trad_cache;
  1268. }

  1269. /* Implement the this_id function for the stub unwinder.  */

  1270. static void
  1271. nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1272.                           struct frame_id *this_id)
  1273. {
  1274.   struct trad_frame_cache *this_trad_cache
  1275.     = nios2_stub_frame_cache (this_frame, this_cache);

  1276.   trad_frame_get_id (this_trad_cache, this_id);
  1277. }

  1278. /* Implement the prev_register function for the stub unwinder.  */

  1279. static struct value *
  1280. nios2_stub_frame_prev_register (struct frame_info *this_frame,
  1281.                                 void **this_cache, int regnum)
  1282. {
  1283.   struct trad_frame_cache *this_trad_cache
  1284.     = nios2_stub_frame_cache (this_frame, this_cache);

  1285.   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
  1286. }

  1287. /* Implement the sniffer function for the stub unwinder.
  1288.    This unwinder is used for cases where the normal
  1289.    prologue-analysis-based unwinder can't work,
  1290.    such as PLT stubs.  */

  1291. static int
  1292. nios2_stub_frame_sniffer (const struct frame_unwind *self,
  1293.                           struct frame_info *this_frame, void **cache)
  1294. {
  1295.   gdb_byte dummy[4];
  1296.   struct obj_section *s;
  1297.   CORE_ADDR pc = get_frame_address_in_block (this_frame);

  1298.   /* Use the stub unwinder for unreadable code.  */
  1299.   if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
  1300.     return 1;

  1301.   if (in_plt_section (pc))
  1302.     return 1;

  1303.   return 0;
  1304. }

  1305. /* Define the data structures for the stub unwinder.  */

  1306. static const struct frame_unwind nios2_stub_frame_unwind =
  1307. {
  1308.   NORMAL_FRAME,
  1309.   default_frame_unwind_stop_reason,
  1310.   nios2_stub_frame_this_id,
  1311.   nios2_stub_frame_prev_register,
  1312.   NULL,
  1313.   nios2_stub_frame_sniffer
  1314. };



  1315. /* Determine where to set a single step breakpoint while considering
  1316.    branch prediction.  */

  1317. static CORE_ADDR
  1318. nios2_get_next_pc (struct frame_info *frame, CORE_ADDR pc)
  1319. {
  1320.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1321.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1322.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1323.   unsigned int insn;
  1324.   const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn);
  1325.   int ra;
  1326.   int rb;
  1327.   int imm;
  1328.   unsigned int uimm;
  1329.   int wb, ret;
  1330.   enum branch_condition cond;

  1331.   /* Do something stupid if we can't disassemble the insn at pc.  */
  1332.   if (op == NULL)
  1333.     return pc + NIOS2_OPCODE_SIZE;

  1334.   if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
  1335.     {
  1336.       int ras = get_frame_register_signed (frame, ra);
  1337.       int rbs = get_frame_register_signed (frame, rb);
  1338.       unsigned int rau = get_frame_register_unsigned (frame, ra);
  1339.       unsigned int rbu = get_frame_register_unsigned (frame, rb);

  1340.       pc += op->size;
  1341.       switch (cond)
  1342.         {
  1343.         case branch_none:
  1344.           pc += imm;
  1345.           break;
  1346.         case branch_eq:
  1347.           if (ras == rbs)
  1348.             pc += imm;
  1349.           break;
  1350.         case branch_ne:
  1351.           if (ras != rbs)
  1352.             pc += imm;
  1353.           break;
  1354.         case branch_ge:
  1355.           if (ras >= rbs)
  1356.             pc += imm;
  1357.           break;
  1358.         case branch_geu:
  1359.           if (rau >= rbu)
  1360.             pc += imm;
  1361.           break;
  1362.         case branch_lt:
  1363.           if (ras < rbs)
  1364.             pc += imm;
  1365.           break;
  1366.         case branch_ltu:
  1367.           if (rau < rbu)
  1368.             pc += imm;
  1369.           break;
  1370.         default:
  1371.           break;
  1372.         }
  1373.     }

  1374.   else if (nios2_match_jmpi (insn, op, mach, &uimm)
  1375.            || nios2_match_calli (insn, op, mach, &uimm))
  1376.     pc = (pc & 0xf0000000) | uimm;

  1377.   else if (nios2_match_jmpr (insn, op, mach, &ra)
  1378.            || nios2_match_callr (insn, op, mach, &ra))
  1379.     pc = get_frame_register_unsigned (frame, ra);

  1380.   else if (nios2_match_trap (insn, op, mach, &uimm))
  1381.     {
  1382.       if (tdep->syscall_next_pc != NULL)
  1383.         return tdep->syscall_next_pc (frame);
  1384.     }

  1385.   else
  1386.     pc += op->size;

  1387.   return pc;
  1388. }

  1389. /* Implement the software_single_step gdbarch method.  */

  1390. static int
  1391. nios2_software_single_step (struct frame_info *frame)
  1392. {
  1393.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1394.   struct address_space *aspace = get_frame_address_space (frame);
  1395.   CORE_ADDR next_pc = nios2_get_next_pc (frame, get_frame_pc (frame));

  1396.   insert_single_step_breakpoint (gdbarch, aspace, next_pc);

  1397.   return 1;
  1398. }

  1399. /* Implement the get_longjump_target gdbarch method.  */

  1400. static int
  1401. nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  1402. {
  1403.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1404.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1405.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1406.   CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
  1407.   gdb_byte buf[4];

  1408.   if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
  1409.     return 0;

  1410.   *pc = extract_unsigned_integer (buf, 4, byte_order);
  1411.   return 1;
  1412. }

  1413. /* Initialize the Nios II gdbarch.  */

  1414. static struct gdbarch *
  1415. nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1416. {
  1417.   struct gdbarch *gdbarch;
  1418.   struct gdbarch_tdep *tdep;
  1419.   int register_bytes, i;
  1420.   struct tdesc_arch_data *tdesc_data = NULL;
  1421.   const struct target_desc *tdesc = info.target_desc;

  1422.   if (!tdesc_has_registers (tdesc))
  1423.     /* Pick a default target description.  */
  1424.     tdesc = tdesc_nios2;

  1425.   /* Check any target description for validity.  */
  1426.   if (tdesc_has_registers (tdesc))
  1427.     {
  1428.       const struct tdesc_feature *feature;
  1429.       int valid_p;

  1430.       feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
  1431.       if (feature == NULL)
  1432.         return NULL;

  1433.       tdesc_data = tdesc_data_alloc ();

  1434.       valid_p = 1;

  1435.       for (i = 0; i < NIOS2_NUM_REGS; i++)
  1436.         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  1437.                                             nios2_reg_names[i]);

  1438.       if (!valid_p)
  1439.         {
  1440.           tdesc_data_cleanup (tdesc_data);
  1441.           return NULL;
  1442.         }
  1443.     }

  1444.   /* Find a candidate among the list of pre-declared architectures.  */
  1445.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1446.   if (arches != NULL)
  1447.     return arches->gdbarch;

  1448.   /* None found, create a new architecture from the information
  1449.      provided.  */
  1450.   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
  1451.   gdbarch = gdbarch_alloc (&info, tdep);

  1452.   /* longjmp support not enabled by default.  */
  1453.   tdep->jb_pc = -1;

  1454.   /* Data type sizes.  */
  1455.   set_gdbarch_ptr_bit (gdbarch, 32);
  1456.   set_gdbarch_addr_bit (gdbarch, 32);
  1457.   set_gdbarch_short_bit (gdbarch, 16);
  1458.   set_gdbarch_int_bit (gdbarch, 32);
  1459.   set_gdbarch_long_bit (gdbarch, 32);
  1460.   set_gdbarch_long_long_bit (gdbarch, 64);
  1461.   set_gdbarch_float_bit (gdbarch, 32);
  1462.   set_gdbarch_double_bit (gdbarch, 64);

  1463.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1464.   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);

  1465.   /* The register set.  */
  1466.   set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
  1467.   set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
  1468.   set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM);        /* Pseudo register PC */

  1469.   set_gdbarch_register_name (gdbarch, nios2_register_name);
  1470.   set_gdbarch_register_type (gdbarch, nios2_register_type);

  1471.   /* Provide register mappings for stabs and dwarf2.  */
  1472.   set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
  1473.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);

  1474.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  1475.   /* Call dummy code.  */
  1476.   set_gdbarch_frame_align (gdbarch, nios2_frame_align);

  1477.   set_gdbarch_return_value (gdbarch, nios2_return_value);

  1478.   set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
  1479.   set_gdbarch_in_function_epilogue_p (gdbarch, nios2_in_function_epilogue_p);
  1480.   set_gdbarch_breakpoint_from_pc (gdbarch, nios2_breakpoint_from_pc);

  1481.   set_gdbarch_dummy_id (gdbarch, nios2_dummy_id);
  1482.   set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
  1483.   set_gdbarch_unwind_sp (gdbarch, nios2_unwind_sp);

  1484.   /* The dwarf2 unwinder will normally produce the best results if
  1485.      the debug information is available, so register it first.  */
  1486.   dwarf2_append_unwinders (gdbarch);
  1487.   frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
  1488.   frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);

  1489.   /* Single stepping.  */
  1490.   set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);

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

  1493.   if (tdep->jb_pc >= 0)
  1494.     set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);

  1495.   frame_base_set_default (gdbarch, &nios2_frame_base);

  1496.   set_gdbarch_print_insn (gdbarch, nios2_print_insn);

  1497.   /* Enable inferior call support.  */
  1498.   set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);

  1499.   if (tdesc_data)
  1500.     tdesc_use_registers (gdbarch, tdesc, tdesc_data);

  1501.   return gdbarch;
  1502. }

  1503. extern initialize_file_ftype _initialize_nios2_tdep; /* -Wmissing-prototypes */

  1504. void
  1505. _initialize_nios2_tdep (void)
  1506. {
  1507.   gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
  1508.   initialize_tdesc_nios2 ();

  1509.   /* Allow debugging this file's internals.  */
  1510.   add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
  1511.                            _("Set Nios II debugging."),
  1512.                            _("Show Nios II debugging."),
  1513.                            _("When on, Nios II specific debugging is enabled."),
  1514.                            NULL,
  1515.                            NULL,
  1516.                            &setdebuglist, &showdebuglist);
  1517. }