gdb/mips-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.

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

  3.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  4.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.

  5.    This file is part of GDB.

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

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

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

  16. #include "defs.h"
  17. #include "frame.h"
  18. #include "inferior.h"
  19. #include "symtab.h"
  20. #include "value.h"
  21. #include "gdbcmd.h"
  22. #include "language.h"
  23. #include "gdbcore.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "gdbtypes.h"
  27. #include "target.h"
  28. #include "arch-utils.h"
  29. #include "regcache.h"
  30. #include "osabi.h"
  31. #include "mips-tdep.h"
  32. #include "block.h"
  33. #include "reggroups.h"
  34. #include "opcode/mips.h"
  35. #include "elf/mips.h"
  36. #include "elf-bfd.h"
  37. #include "symcat.h"
  38. #include "sim-regno.h"
  39. #include "dis-asm.h"
  40. #include "frame-unwind.h"
  41. #include "frame-base.h"
  42. #include "trad-frame.h"
  43. #include "infcall.h"
  44. #include "floatformat.h"
  45. #include "remote.h"
  46. #include "target-descriptions.h"
  47. #include "dwarf2-frame.h"
  48. #include "user-regs.h"
  49. #include "valprint.h"
  50. #include "ax.h"

  51. static const struct objfile_data *mips_pdr_data;

  52. static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);

  53. static int mips32_instruction_has_delay_slot (struct gdbarch *gdbarch,
  54.                                               ULONGEST inst);
  55. static int micromips_instruction_has_delay_slot (ULONGEST insn, int mustbe32);
  56. static int mips16_instruction_has_delay_slot (unsigned short inst,
  57.                                               int mustbe32);

  58. static int mips32_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
  59.                                              CORE_ADDR addr);
  60. static int micromips_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
  61.                                                 CORE_ADDR addr, int mustbe32);
  62. static int mips16_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
  63.                                              CORE_ADDR addr, int mustbe32);

  64. static void mips_print_float_info (struct gdbarch *, struct ui_file *,
  65.                                    struct frame_info *, const char *);

  66. /* A useful bit in the CP0 status register (MIPS_PS_REGNUM).  */
  67. /* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip.  */
  68. #define ST0_FR (1 << 26)

  69. /* The sizes of floating point registers.  */

  70. enum
  71. {
  72.   MIPS_FPU_SINGLE_REGSIZE = 4,
  73.   MIPS_FPU_DOUBLE_REGSIZE = 8
  74. };

  75. enum
  76. {
  77.   MIPS32_REGSIZE = 4,
  78.   MIPS64_REGSIZE = 8
  79. };

  80. static const char *mips_abi_string;

  81. static const char *const mips_abi_strings[] = {
  82.   "auto",
  83.   "n32",
  84.   "o32",
  85.   "n64",
  86.   "o64",
  87.   "eabi32",
  88.   "eabi64",
  89.   NULL
  90. };

  91. /* For backwards compatibility we default to MIPS16.  This flag is
  92.    overridden as soon as unambiguous ELF file flags tell us the
  93.    compressed ISA encoding used.  */
  94. static const char mips_compression_mips16[] = "mips16";
  95. static const char mips_compression_micromips[] = "micromips";
  96. static const char *const mips_compression_strings[] =
  97. {
  98.   mips_compression_mips16,
  99.   mips_compression_micromips,
  100.   NULL
  101. };

  102. static const char *mips_compression_string = mips_compression_mips16;

  103. /* The standard register names, and all the valid aliases for them.  */
  104. struct register_alias
  105. {
  106.   const char *name;
  107.   int regnum;
  108. };

  109. /* Aliases for o32 and most other ABIs.  */
  110. const struct register_alias mips_o32_aliases[] = {
  111.   { "ta0", 12 },
  112.   { "ta1", 13 },
  113.   { "ta2", 14 },
  114.   { "ta3", 15 }
  115. };

  116. /* Aliases for n32 and n64.  */
  117. const struct register_alias mips_n32_n64_aliases[] = {
  118.   { "ta0", 8 },
  119.   { "ta1", 9 },
  120.   { "ta2", 10 },
  121.   { "ta3", 11 }
  122. };

  123. /* Aliases for ABI-independent registers.  */
  124. const struct register_alias mips_register_aliases[] = {
  125.   /* The architecture manuals specify these ABI-independent names for
  126.      the GPRs.  */
  127. #define R(n) { "r" #n, n }
  128.   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  129.   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
  130.   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
  131.   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
  132. #undef R

  133.   /* k0 and k1 are sometimes called these instead (for "kernel
  134.      temp").  */
  135.   { "kt0", 26 },
  136.   { "kt1", 27 },

  137.   /* This is the traditional GDB name for the CP0 status register.  */
  138.   { "sr", MIPS_PS_REGNUM },

  139.   /* This is the traditional GDB name for the CP0 BadVAddr register.  */
  140.   { "bad", MIPS_EMBED_BADVADDR_REGNUM },

  141.   /* This is the traditional GDB name for the FCSR.  */
  142.   { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
  143. };

  144. const struct register_alias mips_numeric_register_aliases[] = {
  145. #define R(n) { #n, n }
  146.   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
  147.   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
  148.   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
  149.   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
  150. #undef R
  151. };

  152. #ifndef MIPS_DEFAULT_FPU_TYPE
  153. #define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
  154. #endif
  155. static int mips_fpu_type_auto = 1;
  156. static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;

  157. static unsigned int mips_debug = 0;

  158. /* Properties (for struct target_desc) describing the g/G packet
  159.    layout.  */
  160. #define PROPERTY_GP32 "internal: transfers-32bit-registers"
  161. #define PROPERTY_GP64 "internal: transfers-64bit-registers"

  162. struct target_desc *mips_tdesc_gp32;
  163. struct target_desc *mips_tdesc_gp64;

  164. const struct mips_regnum *
  165. mips_regnum (struct gdbarch *gdbarch)
  166. {
  167.   return gdbarch_tdep (gdbarch)->regnum;
  168. }

  169. static int
  170. mips_fpa0_regnum (struct gdbarch *gdbarch)
  171. {
  172.   return mips_regnum (gdbarch)->fp0 + 12;
  173. }

  174. /* Return 1 if REGNUM refers to a floating-point general register, raw
  175.    or cooked.  Otherwise return 0.  */

  176. static int
  177. mips_float_register_p (struct gdbarch *gdbarch, int regnum)
  178. {
  179.   int rawnum = regnum % gdbarch_num_regs (gdbarch);

  180.   return (rawnum >= mips_regnum (gdbarch)->fp0
  181.           && rawnum < mips_regnum (gdbarch)->fp0 + 32);
  182. }

  183. #define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
  184.                      == MIPS_ABI_EABI32 \
  185.                    || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)

  186. #define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
  187.   (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)

  188. #define MIPS_LAST_ARG_REGNUM(gdbarch) \
  189.   (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)

  190. #define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)

  191. /* Return the MIPS ABI associated with GDBARCH.  */
  192. enum mips_abi
  193. mips_abi (struct gdbarch *gdbarch)
  194. {
  195.   return gdbarch_tdep (gdbarch)->mips_abi;
  196. }

  197. int
  198. mips_isa_regsize (struct gdbarch *gdbarch)
  199. {
  200.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  201.   /* If we know how big the registers are, use that size.  */
  202.   if (tdep->register_size_valid_p)
  203.     return tdep->register_size;

  204.   /* Fall back to the previous behavior.  */
  205.   return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
  206.           / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
  207. }

  208. /* Return the currently configured (or set) saved register size.  */

  209. unsigned int
  210. mips_abi_regsize (struct gdbarch *gdbarch)
  211. {
  212.   switch (mips_abi (gdbarch))
  213.     {
  214.     case MIPS_ABI_EABI32:
  215.     case MIPS_ABI_O32:
  216.       return 4;
  217.     case MIPS_ABI_N32:
  218.     case MIPS_ABI_N64:
  219.     case MIPS_ABI_O64:
  220.     case MIPS_ABI_EABI64:
  221.       return 8;
  222.     case MIPS_ABI_UNKNOWN:
  223.     case MIPS_ABI_LAST:
  224.     default:
  225.       internal_error (__FILE__, __LINE__, _("bad switch"));
  226.     }
  227. }

  228. /* MIPS16/microMIPS function addresses are odd (bit 0 is set).  Here
  229.    are some functions to handle addresses associated with compressed
  230.    code including but not limited to testing, setting, or clearing
  231.    bit 0 of such addresses.  */

  232. /* Return one iff compressed code is the MIPS16 instruction set.  */

  233. static int
  234. is_mips16_isa (struct gdbarch *gdbarch)
  235. {
  236.   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MIPS16;
  237. }

  238. /* Return one iff compressed code is the microMIPS instruction set.  */

  239. static int
  240. is_micromips_isa (struct gdbarch *gdbarch)
  241. {
  242.   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MICROMIPS;
  243. }

  244. /* Return one iff ADDR denotes compressed code.  */

  245. static int
  246. is_compact_addr (CORE_ADDR addr)
  247. {
  248.   return ((addr) & 1);
  249. }

  250. /* Return one iff ADDR denotes standard ISA code.  */

  251. static int
  252. is_mips_addr (CORE_ADDR addr)
  253. {
  254.   return !is_compact_addr (addr);
  255. }

  256. /* Return one iff ADDR denotes MIPS16 code.  */

  257. static int
  258. is_mips16_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
  259. {
  260.   return is_compact_addr (addr) && is_mips16_isa (gdbarch);
  261. }

  262. /* Return one iff ADDR denotes microMIPS code.  */

  263. static int
  264. is_micromips_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
  265. {
  266.   return is_compact_addr (addr) && is_micromips_isa (gdbarch);
  267. }

  268. /* Strip the ISA (compression) bit off from ADDR.  */

  269. static CORE_ADDR
  270. unmake_compact_addr (CORE_ADDR addr)
  271. {
  272.   return ((addr) & ~(CORE_ADDR) 1);
  273. }

  274. /* Add the ISA (compression) bit to ADDR.  */

  275. static CORE_ADDR
  276. make_compact_addr (CORE_ADDR addr)
  277. {
  278.   return ((addr) | (CORE_ADDR) 1);
  279. }

  280. /* Extern version of unmake_compact_addr; we use a separate function
  281.    so that unmake_compact_addr can be inlined throughout this file.  */

  282. CORE_ADDR
  283. mips_unmake_compact_addr (CORE_ADDR addr)
  284. {
  285.   return unmake_compact_addr (addr);
  286. }

  287. /* Functions for setting and testing a bit in a minimal symbol that
  288.    marks it as MIPS16 or microMIPS function.  The MSB of the minimal
  289.    symbol's "info" field is used for this purpose.

  290.    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is
  291.    "special", i.e. refers to a MIPS16 or microMIPS function, and sets
  292.    one of the "special" bits in a minimal symbol to mark it accordingly.
  293.    The test checks an ELF-private flag that is valid for true function
  294.    symbols only; for synthetic symbols such as for PLT stubs that have
  295.    no ELF-private part at all the MIPS BFD backend arranges for this
  296.    information to be carried in the asymbol's udata field instead.

  297.    msymbol_is_mips16 and msymbol_is_micromips test the "special" bit
  298.    in a minimal symbol.  */

  299. static void
  300. mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
  301. {
  302.   elf_symbol_type *elfsym = (elf_symbol_type *) sym;
  303.   unsigned char st_other;

  304.   if ((sym->flags & BSF_SYNTHETIC) == 0)
  305.     st_other = elfsym->internal_elf_sym.st_other;
  306.   else if ((sym->flags & BSF_FUNCTION) != 0)
  307.     st_other = sym->udata.i;
  308.   else
  309.     return;

  310.   if (ELF_ST_IS_MICROMIPS (st_other))
  311.     {
  312.       MSYMBOL_TARGET_FLAG_MICROMIPS (msym) = 1;
  313.       SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) | 1);
  314.     }
  315.   else if (ELF_ST_IS_MIPS16 (st_other))
  316.     {
  317.       MSYMBOL_TARGET_FLAG_MIPS16 (msym) = 1;
  318.       SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) | 1);
  319.     }
  320. }

  321. /* Return one iff MSYM refers to standard ISA code.  */

  322. static int
  323. msymbol_is_mips (struct minimal_symbol *msym)
  324. {
  325.   return !(MSYMBOL_TARGET_FLAG_MIPS16 (msym)
  326.            | MSYMBOL_TARGET_FLAG_MICROMIPS (msym));
  327. }

  328. /* Return one iff MSYM refers to MIPS16 code.  */

  329. static int
  330. msymbol_is_mips16 (struct minimal_symbol *msym)
  331. {
  332.   return MSYMBOL_TARGET_FLAG_MIPS16 (msym);
  333. }

  334. /* Return one iff MSYM refers to microMIPS code.  */

  335. static int
  336. msymbol_is_micromips (struct minimal_symbol *msym)
  337. {
  338.   return MSYMBOL_TARGET_FLAG_MICROMIPS (msym);
  339. }

  340. /* Set the ISA bit in the main symbol too, complementing the corresponding
  341.    minimal symbol setting and reflecting the run-time value of the symbol.
  342.    The need for comes from the ISA bit having been cleared as code in
  343.    `_bfd_mips_elf_symbol_processing' separated it into the ELF symbol's
  344.    `st_other' STO_MIPS16 or STO_MICROMIPS annotation, making the values
  345.    of symbols referring to compressed code different in GDB to the values
  346.    used by actual code.  That in turn makes them evaluate incorrectly in
  347.    expressions, producing results different to what the same expressions
  348.    yield when compiled into the program being debugged.  */

  349. static void
  350. mips_make_symbol_special (struct symbol *sym, struct objfile *objfile)
  351. {
  352.   if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  353.     {
  354.       /* We are in symbol reading so it is OK to cast away constness.  */
  355.       struct block *block = (struct block *) SYMBOL_BLOCK_VALUE (sym);
  356.       CORE_ADDR compact_block_start;
  357.       struct bound_minimal_symbol msym;

  358.       compact_block_start = BLOCK_START (block) | 1;
  359.       msym = lookup_minimal_symbol_by_pc (compact_block_start);
  360.       if (msym.minsym && !msymbol_is_mips (msym.minsym))
  361.         {
  362.           BLOCK_START (block) = compact_block_start;
  363.         }
  364.     }
  365. }

  366. /* XFER a value from the big/little/left end of the register.
  367.    Depending on the size of the value it might occupy the entire
  368.    register or just part of it.  Make an allowance for this, aligning
  369.    things accordingly.  */

  370. static void
  371. mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
  372.                     int reg_num, int length,
  373.                     enum bfd_endian endian, gdb_byte *in,
  374.                     const gdb_byte *out, int buf_offset)
  375. {
  376.   int reg_offset = 0;

  377.   gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
  378.   /* Need to transfer the left or right part of the register, based on
  379.      the targets byte order.  */
  380.   switch (endian)
  381.     {
  382.     case BFD_ENDIAN_BIG:
  383.       reg_offset = register_size (gdbarch, reg_num) - length;
  384.       break;
  385.     case BFD_ENDIAN_LITTLE:
  386.       reg_offset = 0;
  387.       break;
  388.     case BFD_ENDIAN_UNKNOWN:        /* Indicates no alignment.  */
  389.       reg_offset = 0;
  390.       break;
  391.     default:
  392.       internal_error (__FILE__, __LINE__, _("bad switch"));
  393.     }
  394.   if (mips_debug)
  395.     fprintf_unfiltered (gdb_stderr,
  396.                         "xfer $%d, reg offset %d, buf offset %d, length %d, ",
  397.                         reg_num, reg_offset, buf_offset, length);
  398.   if (mips_debug && out != NULL)
  399.     {
  400.       int i;
  401.       fprintf_unfiltered (gdb_stdlog, "out ");
  402.       for (i = 0; i < length; i++)
  403.         fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
  404.     }
  405.   if (in != NULL)
  406.     regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
  407.                                in + buf_offset);
  408.   if (out != NULL)
  409.     regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
  410.                                 out + buf_offset);
  411.   if (mips_debug && in != NULL)
  412.     {
  413.       int i;
  414.       fprintf_unfiltered (gdb_stdlog, "in ");
  415.       for (i = 0; i < length; i++)
  416.         fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
  417.     }
  418.   if (mips_debug)
  419.     fprintf_unfiltered (gdb_stdlog, "\n");
  420. }

  421. /* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
  422.    compatiblity mode.  A return value of 1 means that we have
  423.    physical 64-bit registers, but should treat them as 32-bit registers.  */

  424. static int
  425. mips2_fp_compat (struct frame_info *frame)
  426. {
  427.   struct gdbarch *gdbarch = get_frame_arch (frame);
  428.   /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
  429.      meaningful.  */
  430.   if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
  431.     return 0;

  432. #if 0
  433.   /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
  434.      in all the places we deal with FP registers.  PR gdb/413.  */
  435.   /* Otherwise check the FR bit in the status register - it controls
  436.      the FP compatiblity mode.  If it is clear we are in compatibility
  437.      mode.  */
  438.   if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
  439.     return 1;
  440. #endif

  441.   return 0;
  442. }

  443. #define VM_MIN_ADDRESS (CORE_ADDR)0x400000

  444. static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);

  445. static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);

  446. /* The list of available "set mips " and "show mips " commands.  */

  447. static struct cmd_list_element *setmipscmdlist = NULL;
  448. static struct cmd_list_element *showmipscmdlist = NULL;

  449. /* Integer registers 0 thru 31 are handled explicitly by
  450.    mips_register_name().  Processor specific registers 32 and above
  451.    are listed in the following tables.  */

  452. enum
  453. { NUM_MIPS_PROCESSOR_REGS = (90 - 32) };

  454. /* Generic MIPS.  */

  455. static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
  456.   "sr", "lo", "hi", "bad", "cause", "pc",
  457.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  458.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  459.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  460.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  461.   "fsr", "fir",
  462. };

  463. /* Names of IDT R3041 registers.  */

  464. static const char *mips_r3041_reg_names[] = {
  465.   "sr", "lo", "hi", "bad", "cause", "pc",
  466.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  467.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  468.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  469.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  470.   "fsr", "fir", "", /*"fp" */ "",
  471.   "", "", "bus", "ccfg", "", "", "", "",
  472.   "", "", "port", "cmp", "", "", "epc", "prid",
  473. };

  474. /* Names of tx39 registers.  */

  475. static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
  476.   "sr", "lo", "hi", "bad", "cause", "pc",
  477.   "", "", "", "", "", "", "", "",
  478.   "", "", "", "", "", "", "", "",
  479.   "", "", "", "", "", "", "", "",
  480.   "", "", "", "", "", "", "", "",
  481.   "", "", "", "",
  482.   "", "", "", "", "", "", "", "",
  483.   "", "", "config", "cache", "debug", "depc", "epc",
  484. };

  485. /* Names of IRIX registers.  */
  486. static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
  487.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  488.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  489.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  490.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  491.   "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
  492. };

  493. /* Names of registers with Linux kernels.  */
  494. static const char *mips_linux_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
  495.   "sr", "lo", "hi", "bad", "cause", "pc",
  496.   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  497.   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  498.   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  499.   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  500.   "fsr", "fir"
  501. };


  502. /* Return the name of the register corresponding to REGNO.  */
  503. static const char *
  504. mips_register_name (struct gdbarch *gdbarch, int regno)
  505. {
  506.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  507.   /* GPR names for all ABIs other than n32/n64.  */
  508.   static char *mips_gpr_names[] = {
  509.     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  510.     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
  511.     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  512.     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
  513.   };

  514.   /* GPR names for n32 and n64 ABIs.  */
  515.   static char *mips_n32_n64_gpr_names[] = {
  516.     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  517.     "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
  518.     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  519.     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
  520.   };

  521.   enum mips_abi abi = mips_abi (gdbarch);

  522.   /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers,
  523.      but then don't make the raw register names visible.  This (upper)
  524.      range of user visible register numbers are the pseudo-registers.

  525.      This approach was adopted accommodate the following scenario:
  526.      It is possible to debug a 64-bit device using a 32-bit
  527.      programming model.  In such instances, the raw registers are
  528.      configured to be 64-bits wide, while the pseudo registers are
  529.      configured to be 32-bits wide.  The registers that the user
  530.      sees - the pseudo registers - match the users expectations
  531.      given the programming model being used.  */
  532.   int rawnum = regno % gdbarch_num_regs (gdbarch);
  533.   if (regno < gdbarch_num_regs (gdbarch))
  534.     return "";

  535.   /* The MIPS integer registers are always mapped from 0 to 31.  The
  536.      names of the registers (which reflects the conventions regarding
  537.      register use) vary depending on the ABI.  */
  538.   if (0 <= rawnum && rawnum < 32)
  539.     {
  540.       if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
  541.         return mips_n32_n64_gpr_names[rawnum];
  542.       else
  543.         return mips_gpr_names[rawnum];
  544.     }
  545.   else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  546.     return tdesc_register_name (gdbarch, rawnum);
  547.   else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
  548.     {
  549.       gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
  550.       if (tdep->mips_processor_reg_names[rawnum - 32])
  551.         return tdep->mips_processor_reg_names[rawnum - 32];
  552.       return "";
  553.     }
  554.   else
  555.     internal_error (__FILE__, __LINE__,
  556.                     _("mips_register_name: bad register number %d"), rawnum);
  557. }

  558. /* Return the groups that a MIPS register can be categorised into.  */

  559. static int
  560. mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  561.                           struct reggroup *reggroup)
  562. {
  563.   int vector_p;
  564.   int float_p;
  565.   int raw_p;
  566.   int rawnum = regnum % gdbarch_num_regs (gdbarch);
  567.   int pseudo = regnum / gdbarch_num_regs (gdbarch);
  568.   if (reggroup == all_reggroup)
  569.     return pseudo;
  570.   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
  571.   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
  572.   /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
  573.      (gdbarch), as not all architectures are multi-arch.  */
  574.   raw_p = rawnum < gdbarch_num_regs (gdbarch);
  575.   if (gdbarch_register_name (gdbarch, regnum) == NULL
  576.       || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
  577.     return 0;
  578.   if (reggroup == float_reggroup)
  579.     return float_p && pseudo;
  580.   if (reggroup == vector_reggroup)
  581.     return vector_p && pseudo;
  582.   if (reggroup == general_reggroup)
  583.     return (!vector_p && !float_p) && pseudo;
  584.   /* Save the pseudo registers.  Need to make certain that any code
  585.      extracting register values from a saved register cache also uses
  586.      pseudo registers.  */
  587.   if (reggroup == save_reggroup)
  588.     return raw_p && pseudo;
  589.   /* Restore the same pseudo register.  */
  590.   if (reggroup == restore_reggroup)
  591.     return raw_p && pseudo;
  592.   return 0;
  593. }

  594. /* Return the groups that a MIPS register can be categorised into.
  595.    This version is only used if we have a target description which
  596.    describes real registers (and their groups).  */

  597. static int
  598. mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  599.                                 struct reggroup *reggroup)
  600. {
  601.   int rawnum = regnum % gdbarch_num_regs (gdbarch);
  602.   int pseudo = regnum / gdbarch_num_regs (gdbarch);
  603.   int ret;

  604.   /* Only save, restore, and display the pseudo registers.  Need to
  605.      make certain that any code extracting register values from a
  606.      saved register cache also uses pseudo registers.

  607.      Note: saving and restoring the pseudo registers is slightly
  608.      strange; if we have 64 bits, we should save and restore all
  609.      64 bits.  But this is hard and has little benefit.  */
  610.   if (!pseudo)
  611.     return 0;

  612.   ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
  613.   if (ret != -1)
  614.     return ret;

  615.   return mips_register_reggroup_p (gdbarch, regnum, reggroup);
  616. }

  617. /* Map the symbol table registers which live in the range [1 *
  618.    gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
  619.    registers.  Take care of alignment and size problems.  */

  620. static enum register_status
  621. mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  622.                            int cookednum, gdb_byte *buf)
  623. {
  624.   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
  625.   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
  626.               && cookednum < 2 * gdbarch_num_regs (gdbarch));
  627.   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
  628.     return regcache_raw_read (regcache, rawnum, buf);
  629.   else if (register_size (gdbarch, rawnum) >
  630.            register_size (gdbarch, cookednum))
  631.     {
  632.       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
  633.         return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
  634.       else
  635.         {
  636.           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  637.           LONGEST regval;
  638.           enum register_status status;

  639.           status = regcache_raw_read_signed (regcache, rawnum, &regval);
  640.           if (status == REG_VALID)
  641.             store_signed_integer (buf, 4, byte_order, regval);
  642.           return status;
  643.         }
  644.     }
  645.   else
  646.     internal_error (__FILE__, __LINE__, _("bad register size"));
  647. }

  648. static void
  649. mips_pseudo_register_write (struct gdbarch *gdbarch,
  650.                             struct regcache *regcache, int cookednum,
  651.                             const gdb_byte *buf)
  652. {
  653.   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
  654.   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
  655.               && cookednum < 2 * gdbarch_num_regs (gdbarch));
  656.   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
  657.     regcache_raw_write (regcache, rawnum, buf);
  658.   else if (register_size (gdbarch, rawnum) >
  659.            register_size (gdbarch, cookednum))
  660.     {
  661.       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
  662.         regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
  663.       else
  664.         {
  665.           /* Sign extend the shortened version of the register prior
  666.              to placing it in the raw register.  This is required for
  667.              some mips64 parts in order to avoid unpredictable behavior.  */
  668.           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  669.           LONGEST regval = extract_signed_integer (buf, 4, byte_order);
  670.           regcache_raw_write_signed (regcache, rawnum, regval);
  671.         }
  672.     }
  673.   else
  674.     internal_error (__FILE__, __LINE__, _("bad register size"));
  675. }

  676. static int
  677. mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
  678.                                  struct agent_expr *ax, int reg)
  679. {
  680.   int rawnum = reg % gdbarch_num_regs (gdbarch);
  681.   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
  682.               && reg < 2 * gdbarch_num_regs (gdbarch));

  683.   ax_reg_mask (ax, rawnum);

  684.   return 0;
  685. }

  686. static int
  687. mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
  688.                                     struct agent_expr *ax, int reg)
  689. {
  690.   int rawnum = reg % gdbarch_num_regs (gdbarch);
  691.   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
  692.               && reg < 2 * gdbarch_num_regs (gdbarch));
  693.   if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
  694.     {
  695.       ax_reg (ax, rawnum);

  696.       if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
  697.         {
  698.           if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
  699.               || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
  700.             {
  701.               ax_const_l (ax, 32);
  702.               ax_simple (ax, aop_lsh);
  703.             }
  704.           ax_const_l (ax, 32);
  705.           ax_simple (ax, aop_rsh_signed);
  706.         }
  707.     }
  708.   else
  709.     internal_error (__FILE__, __LINE__, _("bad register size"));

  710.   return 0;
  711. }

  712. /* Table to translate 3-bit register field to actual register number.  */
  713. static const signed char mips_reg3_to_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };

  714. /* Heuristic_proc_start may hunt through the text section for a long
  715.    time across a 2400 baud serial line.  Allows the user to limit this
  716.    search.  */

  717. static int heuristic_fence_post = 0;

  718. /* Number of bytes of storage in the actual machine representation for
  719.    register N.  NOTE: This defines the pseudo register type so need to
  720.    rebuild the architecture vector.  */

  721. static int mips64_transfers_32bit_regs_p = 0;

  722. static void
  723. set_mips64_transfers_32bit_regs (char *args, int from_tty,
  724.                                  struct cmd_list_element *c)
  725. {
  726.   struct gdbarch_info info;
  727.   gdbarch_info_init (&info);
  728.   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
  729.      instead of relying on globals.  Doing that would let generic code
  730.      handle the search for this specific architecture.  */
  731.   if (!gdbarch_update_p (info))
  732.     {
  733.       mips64_transfers_32bit_regs_p = 0;
  734.       error (_("32-bit compatibility mode not supported"));
  735.     }
  736. }

  737. /* Convert to/from a register and the corresponding memory value.  */

  738. /* This predicate tests for the case of an 8 byte floating point
  739.    value that is being transferred to or from a pair of floating point
  740.    registers each of which are (or are considered to be) only 4 bytes
  741.    wide.  */
  742. static int
  743. mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
  744.                                     struct type *type)
  745. {
  746.   return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  747.           && register_size (gdbarch, regnum) == 4
  748.           && mips_float_register_p (gdbarch, regnum)
  749.           && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
  750. }

  751. /* This predicate tests for the case of a value of less than 8
  752.    bytes in width that is being transfered to or from an 8 byte
  753.    general purpose register.  */
  754. static int
  755. mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
  756.                                     struct type *type)
  757. {
  758.   int num_regs = gdbarch_num_regs (gdbarch);

  759.   return (register_size (gdbarch, regnum) == 8
  760.           && regnum % num_regs > 0 && regnum % num_regs < 32
  761.           && TYPE_LENGTH (type) < 8);
  762. }

  763. static int
  764. mips_convert_register_p (struct gdbarch *gdbarch,
  765.                          int regnum, struct type *type)
  766. {
  767.   return (mips_convert_register_float_case_p (gdbarch, regnum, type)
  768.           || mips_convert_register_gpreg_case_p (gdbarch, regnum, type));
  769. }

  770. static int
  771. mips_register_to_value (struct frame_info *frame, int regnum,
  772.                         struct type *type, gdb_byte *to,
  773.                         int *optimizedp, int *unavailablep)
  774. {
  775.   struct gdbarch *gdbarch = get_frame_arch (frame);

  776.   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
  777.     {
  778.       get_frame_register (frame, regnum + 0, to + 4);
  779.       get_frame_register (frame, regnum + 1, to + 0);

  780.       if (!get_frame_register_bytes (frame, regnum + 0, 0, 4, to + 4,
  781.                                      optimizedp, unavailablep))
  782.         return 0;

  783.       if (!get_frame_register_bytes (frame, regnum + 1, 0, 4, to + 0,
  784.                                      optimizedp, unavailablep))
  785.         return 0;
  786.       *optimizedp = *unavailablep = 0;
  787.       return 1;
  788.     }
  789.   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
  790.     {
  791.       int len = TYPE_LENGTH (type);
  792.       CORE_ADDR offset;

  793.       offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 - len : 0;
  794.       if (!get_frame_register_bytes (frame, regnum, offset, len, to,
  795.                                      optimizedp, unavailablep))
  796.         return 0;

  797.       *optimizedp = *unavailablep = 0;
  798.       return 1;
  799.     }
  800.   else
  801.     {
  802.       internal_error (__FILE__, __LINE__,
  803.                       _("mips_register_to_value: unrecognized case"));
  804.     }
  805. }

  806. static void
  807. mips_value_to_register (struct frame_info *frame, int regnum,
  808.                         struct type *type, const gdb_byte *from)
  809. {
  810.   struct gdbarch *gdbarch = get_frame_arch (frame);

  811.   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
  812.     {
  813.       put_frame_register (frame, regnum + 0, from + 4);
  814.       put_frame_register (frame, regnum + 1, from + 0);
  815.     }
  816.   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
  817.     {
  818.       gdb_byte fill[8];
  819.       int len = TYPE_LENGTH (type);

  820.       /* Sign extend values, irrespective of type, that are stored to
  821.          a 64-bit general purpose register.  (32-bit unsigned values
  822.          are stored as signed quantities within a 64-bit register.
  823.          When performing an operation, in compiled code, that combines
  824.          a 32-bit unsigned value with a signed 64-bit value, a type
  825.          conversion is first performed that zeroes out the high 32 bits.)  */
  826.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  827.         {
  828.           if (from[0] & 0x80)
  829.             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
  830.           else
  831.             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
  832.           put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
  833.           put_frame_register_bytes (frame, regnum, 8 - len, len, from);
  834.         }
  835.       else
  836.         {
  837.           if (from[len-1] & 0x80)
  838.             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
  839.           else
  840.             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
  841.           put_frame_register_bytes (frame, regnum, 0, len, from);
  842.           put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
  843.         }
  844.     }
  845.   else
  846.     {
  847.       internal_error (__FILE__, __LINE__,
  848.                       _("mips_value_to_register: unrecognized case"));
  849.     }
  850. }

  851. /* Return the GDB type object for the "standard" data type of data in
  852.    register REG.  */

  853. static struct type *
  854. mips_register_type (struct gdbarch *gdbarch, int regnum)
  855. {
  856.   gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
  857.   if (mips_float_register_p (gdbarch, regnum))
  858.     {
  859.       /* The floating-point registers raw, or cooked, always match
  860.          mips_isa_regsize(), and also map 1:1, byte for byte.  */
  861.       if (mips_isa_regsize (gdbarch) == 4)
  862.         return builtin_type (gdbarch)->builtin_float;
  863.       else
  864.         return builtin_type (gdbarch)->builtin_double;
  865.     }
  866.   else if (regnum < gdbarch_num_regs (gdbarch))
  867.     {
  868.       /* The raw or ISA registers.  These are all sized according to
  869.          the ISA regsize.  */
  870.       if (mips_isa_regsize (gdbarch) == 4)
  871.         return builtin_type (gdbarch)->builtin_int32;
  872.       else
  873.         return builtin_type (gdbarch)->builtin_int64;
  874.     }
  875.   else
  876.     {
  877.       int rawnum = regnum - gdbarch_num_regs (gdbarch);

  878.       /* The cooked or ABI registers.  These are sized according to
  879.          the ABI (with a few complications).  */
  880.       if (rawnum == mips_regnum (gdbarch)->fp_control_status
  881.           || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
  882.         return builtin_type (gdbarch)->builtin_int32;
  883.       else if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
  884.                && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
  885.                && rawnum >= MIPS_FIRST_EMBED_REGNUM
  886.                && rawnum <= MIPS_LAST_EMBED_REGNUM)
  887.         /* The pseudo/cooked view of the embedded registers is always
  888.            32-bit.  The raw view is handled below.  */
  889.         return builtin_type (gdbarch)->builtin_int32;
  890.       else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
  891.         /* The target, while possibly using a 64-bit register buffer,
  892.            is only transfering 32-bits of each integer register.
  893.            Reflect this in the cooked/pseudo (ABI) register value.  */
  894.         return builtin_type (gdbarch)->builtin_int32;
  895.       else if (mips_abi_regsize (gdbarch) == 4)
  896.         /* The ABI is restricted to 32-bit registers (the ISA could be
  897.            32- or 64-bit).  */
  898.         return builtin_type (gdbarch)->builtin_int32;
  899.       else
  900.         /* 64-bit ABI.  */
  901.         return builtin_type (gdbarch)->builtin_int64;
  902.     }
  903. }

  904. /* Return the GDB type for the pseudo register REGNUM, which is the
  905.    ABI-level view.  This function is only called if there is a target
  906.    description which includes registers, so we know precisely the
  907.    types of hardware registers.  */

  908. static struct type *
  909. mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  910. {
  911.   const int num_regs = gdbarch_num_regs (gdbarch);
  912.   int rawnum = regnum % num_regs;
  913.   struct type *rawtype;

  914.   gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);

  915.   /* Absent registers are still absent.  */
  916.   rawtype = gdbarch_register_type (gdbarch, rawnum);
  917.   if (TYPE_LENGTH (rawtype) == 0)
  918.     return rawtype;

  919.   if (mips_float_register_p (gdbarch, rawnum))
  920.     /* Present the floating point registers however the hardware did;
  921.        do not try to convert between FPU layouts.  */
  922.     return rawtype;

  923.   /* Use pointer types for registers if we can.  For n32 we can not,
  924.      since we do not have a 64-bit pointer type.  */
  925.   if (mips_abi_regsize (gdbarch)
  926.       == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
  927.     {
  928.       if (rawnum == MIPS_SP_REGNUM
  929.           || rawnum == mips_regnum (gdbarch)->badvaddr)
  930.         return builtin_type (gdbarch)->builtin_data_ptr;
  931.       else if (rawnum == mips_regnum (gdbarch)->pc)
  932.         return builtin_type (gdbarch)->builtin_func_ptr;
  933.     }

  934.   if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
  935.       && ((rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_PS_REGNUM)
  936.           || rawnum == mips_regnum (gdbarch)->lo
  937.           || rawnum == mips_regnum (gdbarch)->hi
  938.           || rawnum == mips_regnum (gdbarch)->badvaddr
  939.           || rawnum == mips_regnum (gdbarch)->cause
  940.           || rawnum == mips_regnum (gdbarch)->pc
  941.           || (mips_regnum (gdbarch)->dspacc != -1
  942.               && rawnum >= mips_regnum (gdbarch)->dspacc
  943.               && rawnum < mips_regnum (gdbarch)->dspacc + 6)))
  944.     return builtin_type (gdbarch)->builtin_int32;

  945.   if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
  946.       && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
  947.       && rawnum >= MIPS_EMBED_FP0_REGNUM + 32
  948.       && rawnum <= MIPS_LAST_EMBED_REGNUM)
  949.     {
  950.       /* The pseudo/cooked view of embedded registers is always
  951.          32-bit, even if the target transfers 64-bit values for them.
  952.          New targets relying on XML descriptions should only transfer
  953.          the necessary 32 bits, but older versions of GDB expected 64,
  954.          so allow the target to provide 64 bits without interfering
  955.          with the displayed type.  */
  956.       return builtin_type (gdbarch)->builtin_int32;
  957.     }

  958.   /* For all other registers, pass through the hardware type.  */
  959.   return rawtype;
  960. }

  961. /* Should the upper word of 64-bit addresses be zeroed?  */
  962. enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;

  963. static int
  964. mips_mask_address_p (struct gdbarch_tdep *tdep)
  965. {
  966.   switch (mask_address_var)
  967.     {
  968.     case AUTO_BOOLEAN_TRUE:
  969.       return 1;
  970.     case AUTO_BOOLEAN_FALSE:
  971.       return 0;
  972.       break;
  973.     case AUTO_BOOLEAN_AUTO:
  974.       return tdep->default_mask_address_p;
  975.     default:
  976.       internal_error (__FILE__, __LINE__,
  977.                       _("mips_mask_address_p: bad switch"));
  978.       return -1;
  979.     }
  980. }

  981. static void
  982. show_mask_address (struct ui_file *file, int from_tty,
  983.                    struct cmd_list_element *c, const char *value)
  984. {
  985.   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());

  986.   deprecated_show_value_hack (file, from_tty, c, value);
  987.   switch (mask_address_var)
  988.     {
  989.     case AUTO_BOOLEAN_TRUE:
  990.       printf_filtered ("The 32 bit mips address mask is enabled\n");
  991.       break;
  992.     case AUTO_BOOLEAN_FALSE:
  993.       printf_filtered ("The 32 bit mips address mask is disabled\n");
  994.       break;
  995.     case AUTO_BOOLEAN_AUTO:
  996.       printf_filtered
  997.         ("The 32 bit address mask is set automatically.  Currently %s\n",
  998.          mips_mask_address_p (tdep) ? "enabled" : "disabled");
  999.       break;
  1000.     default:
  1001.       internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
  1002.       break;
  1003.     }
  1004. }

  1005. /* Tell if the program counter value in MEMADDR is in a standard ISA
  1006.    function.  */

  1007. int
  1008. mips_pc_is_mips (CORE_ADDR memaddr)
  1009. {
  1010.   struct bound_minimal_symbol sym;

  1011.   /* Flags indicating that this is a MIPS16 or microMIPS function is
  1012.      stored by elfread.c in the high bit of the info field.  Use this
  1013.      to decide if the function is standard MIPS.  Otherwise if bit 0
  1014.      of the address is clear, then this is a standard MIPS function.  */
  1015.   sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
  1016.   if (sym.minsym)
  1017.     return msymbol_is_mips (sym.minsym);
  1018.   else
  1019.     return is_mips_addr (memaddr);
  1020. }

  1021. /* Tell if the program counter value in MEMADDR is in a MIPS16 function.  */

  1022. int
  1023. mips_pc_is_mips16 (struct gdbarch *gdbarch, CORE_ADDR memaddr)
  1024. {
  1025.   struct bound_minimal_symbol sym;

  1026.   /* A flag indicating that this is a MIPS16 function is stored by
  1027.      elfread.c in the high bit of the info field.  Use this to decide
  1028.      if the function is MIPS16.  Otherwise if bit 0 of the address is
  1029.      set, then ELF file flags will tell if this is a MIPS16 function.  */
  1030.   sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
  1031.   if (sym.minsym)
  1032.     return msymbol_is_mips16 (sym.minsym);
  1033.   else
  1034.     return is_mips16_addr (gdbarch, memaddr);
  1035. }

  1036. /* Tell if the program counter value in MEMADDR is in a microMIPS function.  */

  1037. int
  1038. mips_pc_is_micromips (struct gdbarch *gdbarch, CORE_ADDR memaddr)
  1039. {
  1040.   struct bound_minimal_symbol sym;

  1041.   /* A flag indicating that this is a microMIPS function is stored by
  1042.      elfread.c in the high bit of the info field.  Use this to decide
  1043.      if the function is microMIPS.  Otherwise if bit 0 of the address
  1044.      is set, then ELF file flags will tell if this is a microMIPS
  1045.      function.  */
  1046.   sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
  1047.   if (sym.minsym)
  1048.     return msymbol_is_micromips (sym.minsym);
  1049.   else
  1050.     return is_micromips_addr (gdbarch, memaddr);
  1051. }

  1052. /* Tell the ISA type of the function the program counter value in MEMADDR
  1053.    is in.  */

  1054. static enum mips_isa
  1055. mips_pc_isa (struct gdbarch *gdbarch, CORE_ADDR memaddr)
  1056. {
  1057.   struct bound_minimal_symbol sym;

  1058.   /* A flag indicating that this is a MIPS16 or a microMIPS function
  1059.      is stored by elfread.c in the high bit of the info field.  Use
  1060.      this to decide if the function is MIPS16 or microMIPS or normal
  1061.      MIPS.  Otherwise if bit 0 of the address is set, then ELF file
  1062.      flags will tell if this is a MIPS16 or a microMIPS function.  */
  1063.   sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
  1064.   if (sym.minsym)
  1065.     {
  1066.       if (msymbol_is_micromips (sym.minsym))
  1067.         return ISA_MICROMIPS;
  1068.       else if (msymbol_is_mips16 (sym.minsym))
  1069.         return ISA_MIPS16;
  1070.       else
  1071.         return ISA_MIPS;
  1072.     }
  1073.   else
  1074.     {
  1075.       if (is_mips_addr (memaddr))
  1076.         return ISA_MIPS;
  1077.       else if (is_micromips_addr (gdbarch, memaddr))
  1078.         return ISA_MICROMIPS;
  1079.       else
  1080.         return ISA_MIPS16;
  1081.     }
  1082. }

  1083. /* Set the ISA bit correctly in the PC, used by DWARF-2 machinery.
  1084.    The need for comes from the ISA bit having been cleared, making
  1085.    addresses in FDE, range records, etc. referring to compressed code
  1086.    different to those in line information, the symbol table and finally
  1087.    the PC register.  That in turn confuses many operations.  */

  1088. static CORE_ADDR
  1089. mips_adjust_dwarf2_addr (CORE_ADDR pc)
  1090. {
  1091.   pc = unmake_compact_addr (pc);
  1092.   return mips_pc_is_mips (pc) ? pc : make_compact_addr (pc);
  1093. }

  1094. /* Recalculate the line record requested so that the resulting PC has
  1095.    the ISA bit set correctly, used by DWARF-2 machinery.  The need for
  1096.    this adjustment comes from some records associated with compressed
  1097.    code having the ISA bit cleared, most notably at function prologue
  1098.    ends.  The ISA bit is in this context retrieved from the minimal
  1099.    symbol covering the address requested, which in turn has been
  1100.    constructed from the binary's symbol table rather than DWARF-2
  1101.    information.  The correct setting of the ISA bit is required for
  1102.    breakpoint addresses to correctly match against the stop PC.

  1103.    As line entries can specify relative address adjustments we need to
  1104.    keep track of the absolute value of the last line address recorded
  1105.    in line information, so that we can calculate the actual address to
  1106.    apply the ISA bit adjustment to.  We use PC for this tracking and
  1107.    keep the original address there.

  1108.    As such relative address adjustments can be odd within compressed
  1109.    code we need to keep track of the last line address with the ISA
  1110.    bit adjustment applied too, as the original address may or may not
  1111.    have had the ISA bit set.  We use ADJ_PC for this tracking and keep
  1112.    the adjusted address there.

  1113.    For relative address adjustments we then use these variables to
  1114.    calculate the address intended by line information, which will be
  1115.    PC-relative, and return an updated adjustment carrying ISA bit
  1116.    information, which will be ADJ_PC-relative.  For absolute address
  1117.    adjustments we just return the same address that we store in ADJ_PC
  1118.    too.

  1119.    As the first line entry can be relative to an implied address value
  1120.    of 0 we need to have the initial address set up that we store in PC
  1121.    and ADJ_PC.  This is arranged with a call from `dwarf_decode_lines_1'
  1122.    that sets PC to 0 and ADJ_PC accordingly, usually 0 as well.  */

  1123. static CORE_ADDR
  1124. mips_adjust_dwarf2_line (CORE_ADDR addr, int rel)
  1125. {
  1126.   static CORE_ADDR adj_pc;
  1127.   static CORE_ADDR pc;
  1128.   CORE_ADDR isa_pc;

  1129.   pc = rel ? pc + addr : addr;
  1130.   isa_pc = mips_adjust_dwarf2_addr (pc);
  1131.   addr = rel ? isa_pc - adj_pc : isa_pc;
  1132.   adj_pc = isa_pc;
  1133.   return addr;
  1134. }

  1135. /* Various MIPS16 thunk (aka stub or trampoline) names.  */

  1136. static const char mips_str_mips16_call_stub[] = "__mips16_call_stub_";
  1137. static const char mips_str_mips16_ret_stub[] = "__mips16_ret_";
  1138. static const char mips_str_call_fp_stub[] = "__call_stub_fp_";
  1139. static const char mips_str_call_stub[] = "__call_stub_";
  1140. static const char mips_str_fn_stub[] = "__fn_stub_";

  1141. /* This is used as a PIC thunk prefix.  */

  1142. static const char mips_str_pic[] = ".pic.";

  1143. /* Return non-zero if the PC is inside a call thunk (aka stub or
  1144.    trampoline) that should be treated as a temporary frame.  */

  1145. static int
  1146. mips_in_frame_stub (CORE_ADDR pc)
  1147. {
  1148.   CORE_ADDR start_addr;
  1149.   const char *name;

  1150.   /* Find the starting address of the function containing the PC.  */
  1151.   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
  1152.     return 0;

  1153.   /* If the PC is in __mips16_call_stub_*, this is a call/return stub.  */
  1154.   if (strncmp (name, mips_str_mips16_call_stub,
  1155.                strlen (mips_str_mips16_call_stub)) == 0)
  1156.     return 1;
  1157.   /* If the PC is in __call_stub_*, this is a call/return or a call stub.  */
  1158.   if (strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
  1159.     return 1;
  1160.   /* If the PC is in __fn_stub_*, this is a call stub.  */
  1161.   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0)
  1162.     return 1;

  1163.   return 0;                        /* Not a stub.  */
  1164. }

  1165. /* MIPS believes that the PC has a sign extended value.  Perhaps the
  1166.    all registers should be sign extended for simplicity?  */

  1167. static CORE_ADDR
  1168. mips_read_pc (struct regcache *regcache)
  1169. {
  1170.   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
  1171.   LONGEST pc;

  1172.   regcache_cooked_read_signed (regcache, regnum, &pc);
  1173.   return pc;
  1174. }

  1175. static CORE_ADDR
  1176. mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1177. {
  1178.   CORE_ADDR pc;

  1179.   pc = frame_unwind_register_signed (next_frame, gdbarch_pc_regnum (gdbarch));
  1180.   /* macro/2012-04-20: This hack skips over MIPS16 call thunks as
  1181.      intermediate frames.  In this case we can get the caller's address
  1182.      from $ra, or if $ra contains an address within a thunk as well, then
  1183.      it must be in the return path of __mips16_call_stub_{s,d}{f,c}_{0..10}
  1184.      and thus the caller's address is in $s2.  */
  1185.   if (frame_relative_level (next_frame) >= 0 && mips_in_frame_stub (pc))
  1186.     {
  1187.       pc = frame_unwind_register_signed
  1188.              (next_frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
  1189.       if (mips_in_frame_stub (pc))
  1190.         pc = frame_unwind_register_signed
  1191.                (next_frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
  1192.     }
  1193.   return pc;
  1194. }

  1195. static CORE_ADDR
  1196. mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1197. {
  1198.   return frame_unwind_register_signed
  1199.            (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
  1200. }

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

  1205. static struct frame_id
  1206. mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1207. {
  1208.   return frame_id_build
  1209.            (get_frame_register_signed (this_frame,
  1210.                                        gdbarch_num_regs (gdbarch)
  1211.                                        + MIPS_SP_REGNUM),
  1212.             get_frame_pc (this_frame));
  1213. }

  1214. /* Implement the "write_pc" gdbarch method.  */

  1215. void
  1216. mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1217. {
  1218.   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));

  1219.   regcache_cooked_write_unsigned (regcache, regnum, pc);
  1220. }

  1221. /* Fetch and return instruction from the specified location.  Handle
  1222.    MIPS16/microMIPS as appropriate.  */

  1223. static ULONGEST
  1224. mips_fetch_instruction (struct gdbarch *gdbarch,
  1225.                         enum mips_isa isa, CORE_ADDR addr, int *statusp)
  1226. {
  1227.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1228.   gdb_byte buf[MIPS_INSN32_SIZE];
  1229.   int instlen;
  1230.   int status;

  1231.   switch (isa)
  1232.     {
  1233.     case ISA_MICROMIPS:
  1234.     case ISA_MIPS16:
  1235.       instlen = MIPS_INSN16_SIZE;
  1236.       addr = unmake_compact_addr (addr);
  1237.       break;
  1238.     case ISA_MIPS:
  1239.       instlen = MIPS_INSN32_SIZE;
  1240.       break;
  1241.     default:
  1242.       internal_error (__FILE__, __LINE__, _("invalid ISA"));
  1243.       break;
  1244.     }
  1245.   status = target_read_memory (addr, buf, instlen);
  1246.   if (statusp != NULL)
  1247.     *statusp = status;
  1248.   if (status)
  1249.     {
  1250.       if (statusp == NULL)
  1251.         memory_error (status, addr);
  1252.       return 0;
  1253.     }
  1254.   return extract_unsigned_integer (buf, instlen, byte_order);
  1255. }

  1256. /* These are the fields of 32 bit mips instructions.  */
  1257. #define mips32_op(x) (x >> 26)
  1258. #define itype_op(x) (x >> 26)
  1259. #define itype_rs(x) ((x >> 21) & 0x1f)
  1260. #define itype_rt(x) ((x >> 16) & 0x1f)
  1261. #define itype_immediate(x) (x & 0xffff)

  1262. #define jtype_op(x) (x >> 26)
  1263. #define jtype_target(x) (x & 0x03ffffff)

  1264. #define rtype_op(x) (x >> 26)
  1265. #define rtype_rs(x) ((x >> 21) & 0x1f)
  1266. #define rtype_rt(x) ((x >> 16) & 0x1f)
  1267. #define rtype_rd(x) ((x >> 11) & 0x1f)
  1268. #define rtype_shamt(x) ((x >> 6) & 0x1f)
  1269. #define rtype_funct(x) (x & 0x3f)

  1270. /* MicroMIPS instruction fields.  */
  1271. #define micromips_op(x) ((x) >> 10)

  1272. /* 16-bit/32-bit-high-part instruction formats, B and S refer to the lowest
  1273.    bit and the size respectively of the field extracted.  */
  1274. #define b0s4_imm(x) ((x) & 0xf)
  1275. #define b0s5_imm(x) ((x) & 0x1f)
  1276. #define b0s5_reg(x) ((x) & 0x1f)
  1277. #define b0s7_imm(x) ((x) & 0x7f)
  1278. #define b0s10_imm(x) ((x) & 0x3ff)
  1279. #define b1s4_imm(x) (((x) >> 1) & 0xf)
  1280. #define b1s9_imm(x) (((x) >> 1) & 0x1ff)
  1281. #define b2s3_cc(x) (((x) >> 2) & 0x7)
  1282. #define b4s2_regl(x) (((x) >> 4) & 0x3)
  1283. #define b5s5_op(x) (((x) >> 5) & 0x1f)
  1284. #define b5s5_reg(x) (((x) >> 5) & 0x1f)
  1285. #define b6s4_op(x) (((x) >> 6) & 0xf)
  1286. #define b7s3_reg(x) (((x) >> 7) & 0x7)

  1287. /* 32-bit instruction formats, B and S refer to the lowest bit and the size
  1288.    respectively of the field extracted.  */
  1289. #define b0s6_op(x) ((x) & 0x3f)
  1290. #define b0s11_op(x) ((x) & 0x7ff)
  1291. #define b0s12_imm(x) ((x) & 0xfff)
  1292. #define b0s16_imm(x) ((x) & 0xffff)
  1293. #define b0s26_imm(x) ((x) & 0x3ffffff)
  1294. #define b6s10_ext(x) (((x) >> 6) & 0x3ff)
  1295. #define b11s5_reg(x) (((x) >> 11) & 0x1f)
  1296. #define b12s4_op(x) (((x) >> 12) & 0xf)

  1297. /* Return the size in bytes of the instruction INSN encoded in the ISA
  1298.    instruction set.  */

  1299. static int
  1300. mips_insn_size (enum mips_isa isa, ULONGEST insn)
  1301. {
  1302.   switch (isa)
  1303.     {
  1304.     case ISA_MICROMIPS:
  1305.       if (micromips_op (insn) == 0x1f)
  1306.         return 3 * MIPS_INSN16_SIZE;
  1307.       else if (((micromips_op (insn) & 0x4) == 0x4)
  1308.                || ((micromips_op (insn) & 0x7) == 0x0))
  1309.         return 2 * MIPS_INSN16_SIZE;
  1310.       else
  1311.         return MIPS_INSN16_SIZE;
  1312.     case ISA_MIPS16:
  1313.       if ((insn & 0xf800) == 0xf000)
  1314.         return 2 * MIPS_INSN16_SIZE;
  1315.       else
  1316.         return MIPS_INSN16_SIZE;
  1317.     case ISA_MIPS:
  1318.         return MIPS_INSN32_SIZE;
  1319.     }
  1320.   internal_error (__FILE__, __LINE__, _("invalid ISA"));
  1321. }

  1322. static LONGEST
  1323. mips32_relative_offset (ULONGEST inst)
  1324. {
  1325.   return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
  1326. }

  1327. /* Determine the address of the next instruction executed after the INST
  1328.    floating condition branch instruction at PCCOUNT specifies the
  1329.    number of the floating condition bits tested by the branch.  */

  1330. static CORE_ADDR
  1331. mips32_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
  1332.                ULONGEST inst, CORE_ADDR pc, int count)
  1333. {
  1334.   int fcsr = mips_regnum (gdbarch)->fp_control_status;
  1335.   int cnum = (itype_rt (inst) >> 2) & (count - 1);
  1336.   int tf = itype_rt (inst) & 1;
  1337.   int mask = (1 << count) - 1;
  1338.   ULONGEST fcs;
  1339.   int cond;

  1340.   if (fcsr == -1)
  1341.     /* No way to handle; it'll most likely trap anyway.  */
  1342.     return pc;

  1343.   fcs = get_frame_register_unsigned (frame, fcsr);
  1344.   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);

  1345.   if (((cond >> cnum) & mask) != mask * !tf)
  1346.     pc += mips32_relative_offset (inst);
  1347.   else
  1348.     pc += 4;

  1349.   return pc;
  1350. }

  1351. /* Return nonzero if the gdbarch is an Octeon series.  */

  1352. static int
  1353. is_octeon (struct gdbarch *gdbarch)
  1354. {
  1355.   const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);

  1356.   return (info->mach == bfd_mach_mips_octeon
  1357.          || info->mach == bfd_mach_mips_octeonp
  1358.          || info->mach == bfd_mach_mips_octeon2);
  1359. }

  1360. /* Return true if the OP represents the Octeon's BBIT instruction.  */

  1361. static int
  1362. is_octeon_bbit_op (int op, struct gdbarch *gdbarch)
  1363. {
  1364.   if (!is_octeon (gdbarch))
  1365.     return 0;
  1366.   /* BBIT0 is encoded as LWC2: 110 010.  */
  1367.   /* BBIT032 is encoded as LDC2: 110 110.  */
  1368.   /* BBIT1 is encoded as SWC2: 111 010.  */
  1369.   /* BBIT132 is encoded as SDC2: 111 110.  */
  1370.   if (op == 50 || op == 54 || op == 58 || op == 62)
  1371.     return 1;
  1372.   return 0;
  1373. }


  1374. /* Determine where to set a single step breakpoint while considering
  1375.    branch prediction.  */

  1376. static CORE_ADDR
  1377. mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
  1378. {
  1379.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1380.   unsigned long inst;
  1381.   int op;
  1382.   inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
  1383.   op = itype_op (inst);
  1384.   if ((inst & 0xe0000000) != 0)                /* Not a special, jump or branch
  1385.                                            instruction.  */
  1386.     {
  1387.       if (op >> 2 == 5)
  1388.         /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
  1389.         {
  1390.           switch (op & 0x03)
  1391.             {
  1392.             case 0:                /* BEQL */
  1393.               goto equal_branch;
  1394.             case 1:                /* BNEL */
  1395.               goto neq_branch;
  1396.             case 2:                /* BLEZL */
  1397.               goto less_branch;
  1398.             case 3:                /* BGTZL */
  1399.               goto greater_branch;
  1400.             default:
  1401.               pc += 4;
  1402.             }
  1403.         }
  1404.       else if (op == 17 && itype_rs (inst) == 8)
  1405.         /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
  1406.         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 1);
  1407.       else if (op == 17 && itype_rs (inst) == 9
  1408.                && (itype_rt (inst) & 2) == 0)
  1409.         /* BC1ANY2F, BC1ANY2T: 010001 01001 xxx0x */
  1410.         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 2);
  1411.       else if (op == 17 && itype_rs (inst) == 10
  1412.                && (itype_rt (inst) & 2) == 0)
  1413.         /* BC1ANY4F, BC1ANY4T: 010001 01010 xxx0x */
  1414.         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 4);
  1415.       else if (op == 29)
  1416.         /* JALX: 011101 */
  1417.         /* The new PC will be alternate mode.  */
  1418.         {
  1419.           unsigned long reg;

  1420.           reg = jtype_target (inst) << 2;
  1421.           /* Add 1 to indicate 16-bit mode -- invert ISA mode.  */
  1422.           pc = ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + reg + 1;
  1423.         }
  1424.       else if (is_octeon_bbit_op (op, gdbarch))
  1425.         {
  1426.           int bit, branch_if;

  1427.           branch_if = op == 58 || op == 62;
  1428.           bit = itype_rt (inst);

  1429.           /* Take into account the *32 instructions.  */
  1430.           if (op == 54 || op == 62)
  1431.             bit += 32;

  1432.           if (((get_frame_register_signed (frame,
  1433.                                            itype_rs (inst)) >> bit) & 1)
  1434.               == branch_if)
  1435.             pc += mips32_relative_offset (inst) + 4;
  1436.           else
  1437.             pc += 8;        /* After the delay slot.  */
  1438.         }

  1439.       else
  1440.         pc += 4;                /* Not a branch, next instruction is easy.  */
  1441.     }
  1442.   else
  1443.     {                                /* This gets way messy.  */

  1444.       /* Further subdivide into SPECIAL, REGIMM and other.  */
  1445.       switch (op & 0x07)        /* Extract bits 28,27,26.  */
  1446.         {
  1447.         case 0:                /* SPECIAL */
  1448.           op = rtype_funct (inst);
  1449.           switch (op)
  1450.             {
  1451.             case 8:                /* JR */
  1452.             case 9:                /* JALR */
  1453.               /* Set PC to that address.  */
  1454.               pc = get_frame_register_signed (frame, rtype_rs (inst));
  1455.               break;
  1456.             case 12:            /* SYSCALL */
  1457.               {
  1458.                 struct gdbarch_tdep *tdep;

  1459.                 tdep = gdbarch_tdep (get_frame_arch (frame));
  1460.                 if (tdep->syscall_next_pc != NULL)
  1461.                   pc = tdep->syscall_next_pc (frame);
  1462.                 else
  1463.                   pc += 4;
  1464.               }
  1465.               break;
  1466.             default:
  1467.               pc += 4;
  1468.             }

  1469.           break;                /* end SPECIAL */
  1470.         case 1:                        /* REGIMM */
  1471.           {
  1472.             op = itype_rt (inst);        /* branch condition */
  1473.             switch (op)
  1474.               {
  1475.               case 0:                /* BLTZ */
  1476.               case 2:                /* BLTZL */
  1477.               case 16:                /* BLTZAL */
  1478.               case 18:                /* BLTZALL */
  1479.               less_branch:
  1480.                 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
  1481.                   pc += mips32_relative_offset (inst) + 4;
  1482.                 else
  1483.                   pc += 8;        /* after the delay slot */
  1484.                 break;
  1485.               case 1:                /* BGEZ */
  1486.               case 3:                /* BGEZL */
  1487.               case 17:                /* BGEZAL */
  1488.               case 19:                /* BGEZALL */
  1489.                 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
  1490.                   pc += mips32_relative_offset (inst) + 4;
  1491.                 else
  1492.                   pc += 8;        /* after the delay slot */
  1493.                 break;
  1494.               case 0x1c:        /* BPOSGE32 */
  1495.               case 0x1e:        /* BPOSGE64 */
  1496.                 pc += 4;
  1497.                 if (itype_rs (inst) == 0)
  1498.                   {
  1499.                     unsigned int pos = (op & 2) ? 64 : 32;
  1500.                     int dspctl = mips_regnum (gdbarch)->dspctl;

  1501.                     if (dspctl == -1)
  1502.                       /* No way to handle; it'll most likely trap anyway.  */
  1503.                       break;

  1504.                     if ((get_frame_register_unsigned (frame,
  1505.                                                       dspctl) & 0x7f) >= pos)
  1506.                       pc += mips32_relative_offset (inst);
  1507.                     else
  1508.                       pc += 4;
  1509.                   }
  1510.                 break;
  1511.                 /* All of the other instructions in the REGIMM category */
  1512.               default:
  1513.                 pc += 4;
  1514.               }
  1515.           }
  1516.           break;                /* end REGIMM */
  1517.         case 2:                /* J */
  1518.         case 3:                /* JAL */
  1519.           {
  1520.             unsigned long reg;
  1521.             reg = jtype_target (inst) << 2;
  1522.             /* Upper four bits get never changed...  */
  1523.             pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
  1524.           }
  1525.           break;
  1526.         case 4:                /* BEQ, BEQL */
  1527.         equal_branch:
  1528.           if (get_frame_register_signed (frame, itype_rs (inst)) ==
  1529.               get_frame_register_signed (frame, itype_rt (inst)))
  1530.             pc += mips32_relative_offset (inst) + 4;
  1531.           else
  1532.             pc += 8;
  1533.           break;
  1534.         case 5:                /* BNE, BNEL */
  1535.         neq_branch:
  1536.           if (get_frame_register_signed (frame, itype_rs (inst)) !=
  1537.               get_frame_register_signed (frame, itype_rt (inst)))
  1538.             pc += mips32_relative_offset (inst) + 4;
  1539.           else
  1540.             pc += 8;
  1541.           break;
  1542.         case 6:                /* BLEZ, BLEZL */
  1543.           if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
  1544.             pc += mips32_relative_offset (inst) + 4;
  1545.           else
  1546.             pc += 8;
  1547.           break;
  1548.         case 7:
  1549.         default:
  1550.         greater_branch:        /* BGTZ, BGTZL */
  1551.           if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
  1552.             pc += mips32_relative_offset (inst) + 4;
  1553.           else
  1554.             pc += 8;
  1555.           break;
  1556.         }                        /* switch */
  1557.     }                                /* else */
  1558.   return pc;
  1559. }                                /* mips32_next_pc */

  1560. /* Extract the 7-bit signed immediate offset from the microMIPS instruction
  1561.    INSN.  */

  1562. static LONGEST
  1563. micromips_relative_offset7 (ULONGEST insn)
  1564. {
  1565.   return ((b0s7_imm (insn) ^ 0x40) - 0x40) << 1;
  1566. }

  1567. /* Extract the 10-bit signed immediate offset from the microMIPS instruction
  1568.    INSN.  */

  1569. static LONGEST
  1570. micromips_relative_offset10 (ULONGEST insn)
  1571. {
  1572.   return ((b0s10_imm (insn) ^ 0x200) - 0x200) << 1;
  1573. }

  1574. /* Extract the 16-bit signed immediate offset from the microMIPS instruction
  1575.    INSN.  */

  1576. static LONGEST
  1577. micromips_relative_offset16 (ULONGEST insn)
  1578. {
  1579.   return ((b0s16_imm (insn) ^ 0x8000) - 0x8000) << 1;
  1580. }

  1581. /* Return the size in bytes of the microMIPS instruction at the address PC.  */

  1582. static int
  1583. micromips_pc_insn_size (struct gdbarch *gdbarch, CORE_ADDR pc)
  1584. {
  1585.   ULONGEST insn;

  1586.   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
  1587.   return mips_insn_size (ISA_MICROMIPS, insn);
  1588. }

  1589. /* Calculate the address of the next microMIPS instruction to execute
  1590.    after the INSN coprocessor 1 conditional branch instruction at the
  1591.    address PCCOUNT denotes the number of coprocessor condition bits
  1592.    examined by the branch.  */

  1593. static CORE_ADDR
  1594. micromips_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
  1595.                   ULONGEST insn, CORE_ADDR pc, int count)
  1596. {
  1597.   int fcsr = mips_regnum (gdbarch)->fp_control_status;
  1598.   int cnum = b2s3_cc (insn >> 16) & (count - 1);
  1599.   int tf = b5s5_op (insn >> 16) & 1;
  1600.   int mask = (1 << count) - 1;
  1601.   ULONGEST fcs;
  1602.   int cond;

  1603.   if (fcsr == -1)
  1604.     /* No way to handle; it'll most likely trap anyway.  */
  1605.     return pc;

  1606.   fcs = get_frame_register_unsigned (frame, fcsr);
  1607.   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);

  1608.   if (((cond >> cnum) & mask) != mask * !tf)
  1609.     pc += micromips_relative_offset16 (insn);
  1610.   else
  1611.     pc += micromips_pc_insn_size (gdbarch, pc);

  1612.   return pc;
  1613. }

  1614. /* Calculate the address of the next microMIPS instruction to execute
  1615.    after the instruction at the address PC.  */

  1616. static CORE_ADDR
  1617. micromips_next_pc (struct frame_info *frame, CORE_ADDR pc)
  1618. {
  1619.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1620.   ULONGEST insn;

  1621.   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
  1622.   pc += MIPS_INSN16_SIZE;
  1623.   switch (mips_insn_size (ISA_MICROMIPS, insn))
  1624.     {
  1625.     /* 48-bit instructions.  */
  1626.     case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
  1627.       /* No branch or jump instructions in this category.  */
  1628.       pc += 2 * MIPS_INSN16_SIZE;
  1629.       break;

  1630.     /* 32-bit instructions.  */
  1631.     case 2 * MIPS_INSN16_SIZE:
  1632.       insn <<= 16;
  1633.       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
  1634.       pc += MIPS_INSN16_SIZE;
  1635.       switch (micromips_op (insn >> 16))
  1636.         {
  1637.         case 0x00: /* POOL32A: bits 000000 */
  1638.           if (b0s6_op (insn) == 0x3c
  1639.                                 /* POOL32Axf: bits 000000 ... 111100 */
  1640.               && (b6s10_ext (insn) & 0x2bf) == 0x3c)
  1641.                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
  1642.                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
  1643.             pc = get_frame_register_signed (frame, b0s5_reg (insn >> 16));
  1644.           break;

  1645.         case 0x10: /* POOL32I: bits 010000 */
  1646.           switch (b5s5_op (insn >> 16))
  1647.             {
  1648.             case 0x00: /* BLTZ: bits 010000 00000 */
  1649.             case 0x01: /* BLTZAL: bits 010000 00001 */
  1650.             case 0x11: /* BLTZALS: bits 010000 10001 */
  1651.               if (get_frame_register_signed (frame,
  1652.                                              b0s5_reg (insn >> 16)) < 0)
  1653.                 pc += micromips_relative_offset16 (insn);
  1654.               else
  1655.                 pc += micromips_pc_insn_size (gdbarch, pc);
  1656.               break;

  1657.             case 0x02: /* BGEZ: bits 010000 00010 */
  1658.             case 0x03: /* BGEZAL: bits 010000 00011 */
  1659.             case 0x13: /* BGEZALS: bits 010000 10011 */
  1660.               if (get_frame_register_signed (frame,
  1661.                                              b0s5_reg (insn >> 16)) >= 0)
  1662.                 pc += micromips_relative_offset16 (insn);
  1663.               else
  1664.                 pc += micromips_pc_insn_size (gdbarch, pc);
  1665.               break;

  1666.             case 0x04: /* BLEZ: bits 010000 00100 */
  1667.               if (get_frame_register_signed (frame,
  1668.                                              b0s5_reg (insn >> 16)) <= 0)
  1669.                 pc += micromips_relative_offset16 (insn);
  1670.               else
  1671.                 pc += micromips_pc_insn_size (gdbarch, pc);
  1672.               break;

  1673.             case 0x05: /* BNEZC: bits 010000 00101 */
  1674.               if (get_frame_register_signed (frame,
  1675.                                              b0s5_reg (insn >> 16)) != 0)
  1676.                 pc += micromips_relative_offset16 (insn);
  1677.               break;

  1678.             case 0x06: /* BGTZ: bits 010000 00110 */
  1679.               if (get_frame_register_signed (frame,
  1680.                                              b0s5_reg (insn >> 16)) > 0)
  1681.                 pc += micromips_relative_offset16 (insn);
  1682.               else
  1683.                 pc += micromips_pc_insn_size (gdbarch, pc);
  1684.               break;

  1685.             case 0x07: /* BEQZC: bits 010000 00111 */
  1686.               if (get_frame_register_signed (frame,
  1687.                                              b0s5_reg (insn >> 16)) == 0)
  1688.                 pc += micromips_relative_offset16 (insn);
  1689.               break;

  1690.             case 0x14: /* BC2F: bits 010000 10100 xxx00 */
  1691.             case 0x15: /* BC2T: bits 010000 10101 xxx00 */
  1692.               if (((insn >> 16) & 0x3) == 0x0)
  1693.                 /* BC2F, BC2T: don't know how to handle these.  */
  1694.                 break;
  1695.               break;

  1696.             case 0x1a: /* BPOSGE64: bits 010000 11010 */
  1697.             case 0x1b: /* BPOSGE32: bits 010000 11011 */
  1698.               {
  1699.                 unsigned int pos = (b5s5_op (insn >> 16) & 1) ? 32 : 64;
  1700.                 int dspctl = mips_regnum (gdbarch)->dspctl;

  1701.                 if (dspctl == -1)
  1702.                   /* No way to handle; it'll most likely trap anyway.  */
  1703.                   break;

  1704.                 if ((get_frame_register_unsigned (frame,
  1705.                                                   dspctl) & 0x7f) >= pos)
  1706.                   pc += micromips_relative_offset16 (insn);
  1707.                 else
  1708.                   pc += micromips_pc_insn_size (gdbarch, pc);
  1709.               }
  1710.               break;

  1711.             case 0x1c: /* BC1F: bits 010000 11100 xxx00 */
  1712.                        /* BC1ANY2F: bits 010000 11100 xxx01 */
  1713.             case 0x1d: /* BC1T: bits 010000 11101 xxx00 */
  1714.                        /* BC1ANY2T: bits 010000 11101 xxx01 */
  1715.               if (((insn >> 16) & 0x2) == 0x0)
  1716.                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc,
  1717.                                        ((insn >> 16) & 0x1) + 1);
  1718.               break;

  1719.             case 0x1e: /* BC1ANY4F: bits 010000 11110 xxx01 */
  1720.             case 0x1f: /* BC1ANY4T: bits 010000 11111 xxx01 */
  1721.               if (((insn >> 16) & 0x3) == 0x1)
  1722.                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc, 4);
  1723.               break;
  1724.             }
  1725.           break;

  1726.         case 0x1d: /* JALS: bits 011101 */
  1727.         case 0x35: /* J: bits 110101 */
  1728.         case 0x3d: /* JAL: bits 111101 */
  1729.             pc = ((pc | 0x7fffffe) ^ 0x7fffffe) | (b0s26_imm (insn) << 1);
  1730.           break;

  1731.         case 0x25: /* BEQ: bits 100101 */
  1732.             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
  1733.                 == get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
  1734.               pc += micromips_relative_offset16 (insn);
  1735.             else
  1736.               pc += micromips_pc_insn_size (gdbarch, pc);
  1737.           break;

  1738.         case 0x2d: /* BNE: bits 101101 */
  1739.             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
  1740.                 != get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
  1741.               pc += micromips_relative_offset16 (insn);
  1742.             else
  1743.               pc += micromips_pc_insn_size (gdbarch, pc);
  1744.           break;

  1745.         case 0x3c: /* JALX: bits 111100 */
  1746.             pc = ((pc | 0xfffffff) ^ 0xfffffff) | (b0s26_imm (insn) << 2);
  1747.           break;
  1748.         }
  1749.       break;

  1750.     /* 16-bit instructions.  */
  1751.     case MIPS_INSN16_SIZE:
  1752.       switch (micromips_op (insn))
  1753.         {
  1754.         case 0x11: /* POOL16C: bits 010001 */
  1755.           if ((b5s5_op (insn) & 0x1c) == 0xc)
  1756.             /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
  1757.             pc = get_frame_register_signed (frame, b0s5_reg (insn));
  1758.           else if (b5s5_op (insn) == 0x18)
  1759.             /* JRADDIUSP: bits 010001 11000 */
  1760.             pc = get_frame_register_signed (frame, MIPS_RA_REGNUM);
  1761.           break;

  1762.         case 0x23: /* BEQZ16: bits 100011 */
  1763.           {
  1764.             int rs = mips_reg3_to_reg[b7s3_reg (insn)];

  1765.             if (get_frame_register_signed (frame, rs) == 0)
  1766.               pc += micromips_relative_offset7 (insn);
  1767.             else
  1768.               pc += micromips_pc_insn_size (gdbarch, pc);
  1769.           }
  1770.           break;

  1771.         case 0x2b: /* BNEZ16: bits 101011 */
  1772.           {
  1773.             int rs = mips_reg3_to_reg[b7s3_reg (insn)];

  1774.             if (get_frame_register_signed (frame, rs) != 0)
  1775.               pc += micromips_relative_offset7 (insn);
  1776.             else
  1777.               pc += micromips_pc_insn_size (gdbarch, pc);
  1778.           }
  1779.           break;

  1780.         case 0x33: /* B16: bits 110011 */
  1781.           pc += micromips_relative_offset10 (insn);
  1782.           break;
  1783.         }
  1784.       break;
  1785.     }

  1786.   return pc;
  1787. }

  1788. /* Decoding the next place to set a breakpoint is irregular for the
  1789.    mips 16 variant, but fortunately, there fewer instructions.  We have
  1790.    to cope ith extensions for 16 bit instructions and a pair of actual
  1791.    32 bit instructions.  We dont want to set a single step instruction
  1792.    on the extend instruction either.  */

  1793. /* Lots of mips16 instruction formats */
  1794. /* Predicting jumps requires itype,ritype,i8type
  1795.    and their extensions      extItype,extritype,extI8type.  */
  1796. enum mips16_inst_fmts
  1797. {
  1798.   itype,                        /* 0  immediate 5,10 */
  1799.   ritype,                        /* 1   5,3,8 */
  1800.   rrtype,                        /* 2   5,3,3,5 */
  1801.   rritype,                        /* 3   5,3,3,5 */
  1802.   rrrtype,                        /* 4   5,3,3,3,2 */
  1803.   rriatype,                        /* 5   5,3,3,1,4 */
  1804.   shifttype,                        /* 6   5,3,3,3,2 */
  1805.   i8type,                        /* 7   5,3,8 */
  1806.   i8movtype,                        /* 8   5,3,3,5 */
  1807.   i8mov32rtype,                        /* 9   5,3,5,3 */
  1808.   i64type,                        /* 10  5,3,8 */
  1809.   ri64type,                        /* 11  5,3,3,5 */
  1810.   jalxtype,                        /* 12  5,1,5,5,16 - a 32 bit instruction */
  1811.   exiItype,                        /* 13  5,6,5,5,1,1,1,1,1,1,5 */
  1812.   extRitype,                        /* 14  5,6,5,5,3,1,1,1,5 */
  1813.   extRRItype,                        /* 15  5,5,5,5,3,3,5 */
  1814.   extRRIAtype,                        /* 16  5,7,4,5,3,3,1,4 */
  1815.   EXTshifttype,                        /* 17  5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
  1816.   extI8type,                        /* 18  5,6,5,5,3,1,1,1,5 */
  1817.   extI64type,                        /* 19  5,6,5,5,3,1,1,1,5 */
  1818.   extRi64type,                        /* 20  5,6,5,5,3,3,5 */
  1819.   extshift64type                /* 21  5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
  1820. };
  1821. /* I am heaping all the fields of the formats into one structure and
  1822.    then, only the fields which are involved in instruction extension.  */
  1823. struct upk_mips16
  1824. {
  1825.   CORE_ADDR offset;
  1826.   unsigned int regx;                /* Function in i8 type.  */
  1827.   unsigned int regy;
  1828. };


  1829. /* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
  1830.    for the bits which make up the immediate extension.  */

  1831. static CORE_ADDR
  1832. extended_offset (unsigned int extension)
  1833. {
  1834.   CORE_ADDR value;

  1835.   value = (extension >> 16) & 0x1f;        /* Extract 15:11.  */
  1836.   value = value << 6;
  1837.   value |= (extension >> 21) & 0x3f;        /* Extract 10:5.  */
  1838.   value = value << 5;
  1839.   value |= extension & 0x1f;                /* Extract 4:0.  */

  1840.   return value;
  1841. }

  1842. /* Only call this function if you know that this is an extendable
  1843.    instruction.  It won't malfunction, but why make excess remote memory
  1844.    references?  If the immediate operands get sign extended or something,
  1845.    do it after the extension is performed.  */
  1846. /* FIXME: Every one of these cases needs to worry about sign extension
  1847.    when the offset is to be used in relative addressing.  */

  1848. static unsigned int
  1849. fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
  1850. {
  1851.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1852.   gdb_byte buf[8];

  1853.   pc = unmake_compact_addr (pc);        /* Clear the low order bit.  */
  1854.   target_read_memory (pc, buf, 2);
  1855.   return extract_unsigned_integer (buf, 2, byte_order);
  1856. }

  1857. static void
  1858. unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
  1859.                unsigned int extension,
  1860.                unsigned int inst,
  1861.                enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
  1862. {
  1863.   CORE_ADDR offset;
  1864.   int regx;
  1865.   int regy;
  1866.   switch (insn_format)
  1867.     {
  1868.     case itype:
  1869.       {
  1870.         CORE_ADDR value;
  1871.         if (extension)
  1872.           {
  1873.             value = extended_offset ((extension << 16) | inst);
  1874.             value = (value ^ 0x8000) - 0x8000;                /* Sign-extend.  */
  1875.           }
  1876.         else
  1877.           {
  1878.             value = inst & 0x7ff;
  1879.             value = (value ^ 0x400) - 0x400;                /* Sign-extend.  */
  1880.           }
  1881.         offset = value;
  1882.         regx = -1;
  1883.         regy = -1;
  1884.       }
  1885.       break;
  1886.     case ritype:
  1887.     case i8type:
  1888.       {                                /* A register identifier and an offset.  */
  1889.         /* Most of the fields are the same as I type but the
  1890.            immediate value is of a different length.  */
  1891.         CORE_ADDR value;
  1892.         if (extension)
  1893.           {
  1894.             value = extended_offset ((extension << 16) | inst);
  1895.             value = (value ^ 0x8000) - 0x8000;                /* Sign-extend.  */
  1896.           }
  1897.         else
  1898.           {
  1899.             value = inst & 0xff;                        /* 8 bits */
  1900.             value = (value ^ 0x80) - 0x80;                /* Sign-extend.  */
  1901.           }
  1902.         offset = value;
  1903.         regx = (inst >> 8) & 0x07;                        /* i8 funct */
  1904.         regy = -1;
  1905.         break;
  1906.       }
  1907.     case jalxtype:
  1908.       {
  1909.         unsigned long value;
  1910.         unsigned int nexthalf;
  1911.         value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
  1912.         value = value << 16;
  1913.         nexthalf = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc + 2, NULL);
  1914.                                                 /* Low bit still set.  */
  1915.         value |= nexthalf;
  1916.         offset = value;
  1917.         regx = -1;
  1918.         regy = -1;
  1919.         break;
  1920.       }
  1921.     default:
  1922.       internal_error (__FILE__, __LINE__, _("bad switch"));
  1923.     }
  1924.   upk->offset = offset;
  1925.   upk->regx = regx;
  1926.   upk->regy = regy;
  1927. }


  1928. /* Calculate the destination of a branch whose 16-bit opcode word is at PC,
  1929.    and having a signed 16-bit OFFSET.  */

  1930. static CORE_ADDR
  1931. add_offset_16 (CORE_ADDR pc, int offset)
  1932. {
  1933.   return pc + (offset << 1) + 2;
  1934. }

  1935. static CORE_ADDR
  1936. extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
  1937.                          unsigned int extension, unsigned int insn)
  1938. {
  1939.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1940.   int op = (insn >> 11);
  1941.   switch (op)
  1942.     {
  1943.     case 2:                        /* Branch */
  1944.       {
  1945.         struct upk_mips16 upk;
  1946.         unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
  1947.         pc = add_offset_16 (pc, upk.offset);
  1948.         break;
  1949.       }
  1950.     case 3:                        /* JAL , JALX - Watch out, these are 32 bit
  1951.                                    instructions.  */
  1952.       {
  1953.         struct upk_mips16 upk;
  1954.         unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
  1955.         pc = ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)) | (upk.offset << 2);
  1956.         if ((insn >> 10) & 0x01)        /* Exchange mode */
  1957.           pc = pc & ~0x01;        /* Clear low bit, indicate 32 bit mode.  */
  1958.         else
  1959.           pc |= 0x01;
  1960.         break;
  1961.       }
  1962.     case 4:                        /* beqz */
  1963.       {
  1964.         struct upk_mips16 upk;
  1965.         int reg;
  1966.         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
  1967.         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
  1968.         if (reg == 0)
  1969.           pc = add_offset_16 (pc, upk.offset);
  1970.         else
  1971.           pc += 2;
  1972.         break;
  1973.       }
  1974.     case 5:                        /* bnez */
  1975.       {
  1976.         struct upk_mips16 upk;
  1977.         int reg;
  1978.         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
  1979.         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
  1980.         if (reg != 0)
  1981.           pc = add_offset_16 (pc, upk.offset);
  1982.         else
  1983.           pc += 2;
  1984.         break;
  1985.       }
  1986.     case 12:                        /* I8 Formats btez btnez */
  1987.       {
  1988.         struct upk_mips16 upk;
  1989.         int reg;
  1990.         unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
  1991.         /* upk.regx contains the opcode */
  1992.         reg = get_frame_register_signed (frame, 24);  /* Test register is 24 */
  1993.         if (((upk.regx == 0) && (reg == 0))        /* BTEZ */
  1994.             || ((upk.regx == 1) && (reg != 0)))        /* BTNEZ */
  1995.           pc = add_offset_16 (pc, upk.offset);
  1996.         else
  1997.           pc += 2;
  1998.         break;
  1999.       }
  2000.     case 29:                        /* RR Formats JR, JALR, JALR-RA */
  2001.       {
  2002.         struct upk_mips16 upk;
  2003.         /* upk.fmt = rrtype; */
  2004.         op = insn & 0x1f;
  2005.         if (op == 0)
  2006.           {
  2007.             int reg;
  2008.             upk.regx = (insn >> 8) & 0x07;
  2009.             upk.regy = (insn >> 5) & 0x07;
  2010.             if ((upk.regy & 1) == 0)
  2011.               reg = mips_reg3_to_reg[upk.regx];
  2012.             else
  2013.               reg = 31;                /* Function return instruction.  */
  2014.             pc = get_frame_register_signed (frame, reg);
  2015.           }
  2016.         else
  2017.           pc += 2;
  2018.         break;
  2019.       }
  2020.     case 30:
  2021.       /* This is an instruction extension.  Fetch the real instruction
  2022.          (which follows the extension) and decode things based on
  2023.          that.  */
  2024.       {
  2025.         pc += 2;
  2026.         pc = extended_mips16_next_pc (frame, pc, insn,
  2027.                                       fetch_mips_16 (gdbarch, pc));
  2028.         break;
  2029.       }
  2030.     default:
  2031.       {
  2032.         pc += 2;
  2033.         break;
  2034.       }
  2035.     }
  2036.   return pc;
  2037. }

  2038. static CORE_ADDR
  2039. mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
  2040. {
  2041.   struct gdbarch *gdbarch = get_frame_arch (frame);
  2042.   unsigned int insn = fetch_mips_16 (gdbarch, pc);
  2043.   return extended_mips16_next_pc (frame, pc, 0, insn);
  2044. }

  2045. /* The mips_next_pc function supports single_step when the remote
  2046.    target monitor or stub is not developed enough to do a single_step.
  2047.    It works by decoding the current instruction and predicting where a
  2048.    branch will go.  This isn't hard because all the data is available.
  2049.    The MIPS32, MIPS16 and microMIPS variants are quite different.  */
  2050. static CORE_ADDR
  2051. mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
  2052. {
  2053.   struct gdbarch *gdbarch = get_frame_arch (frame);

  2054.   if (mips_pc_is_mips16 (gdbarch, pc))
  2055.     return mips16_next_pc (frame, pc);
  2056.   else if (mips_pc_is_micromips (gdbarch, pc))
  2057.     return micromips_next_pc (frame, pc);
  2058.   else
  2059.     return mips32_next_pc (frame, pc);
  2060. }

  2061. /* Return non-zero if the MIPS16 instruction INSN is a compact branch
  2062.    or jump.  */

  2063. static int
  2064. mips16_instruction_is_compact_branch (unsigned short insn)
  2065. {
  2066.   switch (insn & 0xf800)
  2067.     {
  2068.     case 0xe800:
  2069.       return (insn & 0x009f) == 0x80;        /* JALRC/JRC */
  2070.     case 0x6000:
  2071.       return (insn & 0x0600) == 0;        /* BTNEZ/BTEQZ */
  2072.     case 0x2800:                        /* BNEZ */
  2073.     case 0x2000:                        /* BEQZ */
  2074.     case 0x1000:                        /* B */
  2075.       return 1;
  2076.     default:
  2077.       return 0;
  2078.     }
  2079. }

  2080. /* Return non-zero if the microMIPS instruction INSN is a compact branch
  2081.    or jump.  */

  2082. static int
  2083. micromips_instruction_is_compact_branch (unsigned short insn)
  2084. {
  2085.   switch (micromips_op (insn))
  2086.     {
  2087.     case 0x11:                        /* POOL16C: bits 010001 */
  2088.       return (b5s5_op (insn) == 0x18
  2089.                                 /* JRADDIUSP: bits 010001 11000 */
  2090.               || b5s5_op (insn) == 0xd);
  2091.                                 /* JRC: bits 010011 01101 */
  2092.     case 0x10:                        /* POOL32I: bits 010000 */
  2093.       return (b5s5_op (insn) & 0x1d) == 0x5;
  2094.                                 /* BEQZC/BNEZC: bits 010000 001x1 */
  2095.     default:
  2096.       return 0;
  2097.     }
  2098. }

  2099. struct mips_frame_cache
  2100. {
  2101.   CORE_ADDR base;
  2102.   struct trad_frame_saved_reg *saved_regs;
  2103. };

  2104. /* Set a register's saved stack address in temp_saved_regs.  If an
  2105.    address has already been set for this register, do nothing; this
  2106.    way we will only recognize the first save of a given register in a
  2107.    function prologue.

  2108.    For simplicity, save the address in both [0 .. gdbarch_num_regs) and
  2109.    [gdbarch_num_regs .. 2*gdbarch_num_regs).
  2110.    Strictly speaking, only the second range is used as it is only second
  2111.    range (the ABI instead of ISA registers) that comes into play when finding
  2112.    saved registers in a frame.  */

  2113. static void
  2114. set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
  2115.                 int regnum, CORE_ADDR offset)
  2116. {
  2117.   if (this_cache != NULL
  2118.       && this_cache->saved_regs[regnum].addr == -1)
  2119.     {
  2120.       this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
  2121.         = offset;
  2122.       this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
  2123.         = offset;
  2124.     }
  2125. }


  2126. /* Fetch the immediate value from a MIPS16 instruction.
  2127.    If the previous instruction was an EXTEND, use it to extend
  2128.    the upper bits of the immediate value.  This is a helper function
  2129.    for mips16_scan_prologue.  */

  2130. static int
  2131. mips16_get_imm (unsigned short prev_inst,        /* previous instruction */
  2132.                 unsigned short inst,        /* current instruction */
  2133.                 int nbits,        /* number of bits in imm field */
  2134.                 int scale,        /* scale factor to be applied to imm */
  2135.                 int is_signed)        /* is the imm field signed?  */
  2136. {
  2137.   int offset;

  2138.   if ((prev_inst & 0xf800) == 0xf000)        /* prev instruction was EXTEND? */
  2139.     {
  2140.       offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
  2141.       if (offset & 0x8000)        /* check for negative extend */
  2142.         offset = 0 - (0x10000 - (offset & 0xffff));
  2143.       return offset | (inst & 0x1f);
  2144.     }
  2145.   else
  2146.     {
  2147.       int max_imm = 1 << nbits;
  2148.       int mask = max_imm - 1;
  2149.       int sign_bit = max_imm >> 1;

  2150.       offset = inst & mask;
  2151.       if (is_signed && (offset & sign_bit))
  2152.         offset = 0 - (max_imm - offset);
  2153.       return offset * scale;
  2154.     }
  2155. }


  2156. /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
  2157.    the associated FRAME_CACHE if not null.
  2158.    Return the address of the first instruction past the prologue.  */

  2159. static CORE_ADDR
  2160. mips16_scan_prologue (struct gdbarch *gdbarch,
  2161.                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
  2162.                       struct frame_info *this_frame,
  2163.                       struct mips_frame_cache *this_cache)
  2164. {
  2165.   int prev_non_prologue_insn = 0;
  2166.   int this_non_prologue_insn;
  2167.   int non_prologue_insns = 0;
  2168.   CORE_ADDR prev_pc;
  2169.   CORE_ADDR cur_pc;
  2170.   CORE_ADDR frame_addr = 0;        /* Value of $r17, used as frame pointer.  */
  2171.   CORE_ADDR sp;
  2172.   long frame_offset = 0;        /* Size of stack frame.  */
  2173.   long frame_adjust = 0;        /* Offset of FP from SP.  */
  2174.   int frame_reg = MIPS_SP_REGNUM;
  2175.   unsigned short prev_inst = 0;        /* saved copy of previous instruction.  */
  2176.   unsigned inst = 0;                /* current instruction */
  2177.   unsigned entry_inst = 0;        /* the entry instruction */
  2178.   unsigned save_inst = 0;        /* the save instruction */
  2179.   int prev_delay_slot = 0;
  2180.   int in_delay_slot;
  2181.   int reg, offset;

  2182.   int extend_bytes = 0;
  2183.   int prev_extend_bytes = 0;
  2184.   CORE_ADDR end_prologue_addr;

  2185.   /* Can be called when there's no process, and hence when there's no
  2186.      THIS_FRAME.  */
  2187.   if (this_frame != NULL)
  2188.     sp = get_frame_register_signed (this_frame,
  2189.                                     gdbarch_num_regs (gdbarch)
  2190.                                     + MIPS_SP_REGNUM);
  2191.   else
  2192.     sp = 0;

  2193.   if (limit_pc > start_pc + 200)
  2194.     limit_pc = start_pc + 200;
  2195.   prev_pc = start_pc;

  2196.   /* Permit at most one non-prologue non-control-transfer instruction
  2197.      in the middle which may have been reordered by the compiler for
  2198.      optimisation.  */
  2199.   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
  2200.     {
  2201.       this_non_prologue_insn = 0;
  2202.       in_delay_slot = 0;

  2203.       /* Save the previous instruction.  If it's an EXTEND, we'll extract
  2204.          the immediate offset extension from it in mips16_get_imm.  */
  2205.       prev_inst = inst;

  2206.       /* Fetch and decode the instruction.  */
  2207.       inst = (unsigned short) mips_fetch_instruction (gdbarch, ISA_MIPS16,
  2208.                                                       cur_pc, NULL);

  2209.       /* Normally we ignore extend instructions.  However, if it is
  2210.          not followed by a valid prologue instruction, then this
  2211.          instruction is not part of the prologue either.  We must
  2212.          remember in this case to adjust the end_prologue_addr back
  2213.          over the extend.  */
  2214.       if ((inst & 0xf800) == 0xf000)    /* extend */
  2215.         {
  2216.           extend_bytes = MIPS_INSN16_SIZE;
  2217.           continue;
  2218.         }

  2219.       prev_extend_bytes = extend_bytes;
  2220.       extend_bytes = 0;

  2221.       if ((inst & 0xff00) == 0x6300        /* addiu sp */
  2222.           || (inst & 0xff00) == 0xfb00)        /* daddiu sp */
  2223.         {
  2224.           offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
  2225.           if (offset < 0)        /* Negative stack adjustment?  */
  2226.             frame_offset -= offset;
  2227.           else
  2228.             /* Exit loop if a positive stack adjustment is found, which
  2229.                usually means that the stack cleanup code in the function
  2230.                epilogue is reached.  */
  2231.             break;
  2232.         }
  2233.       else if ((inst & 0xf800) == 0xd000)        /* sw reg,n($sp) */
  2234.         {
  2235.           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
  2236.           reg = mips_reg3_to_reg[(inst & 0x700) >> 8];
  2237.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2238.         }
  2239.       else if ((inst & 0xff00) == 0xf900)        /* sd reg,n($sp) */
  2240.         {
  2241.           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
  2242.           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
  2243.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2244.         }
  2245.       else if ((inst & 0xff00) == 0x6200)        /* sw $ra,n($sp) */
  2246.         {
  2247.           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
  2248.           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
  2249.         }
  2250.       else if ((inst & 0xff00) == 0xfa00)        /* sd $ra,n($sp) */
  2251.         {
  2252.           offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
  2253.           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
  2254.         }
  2255.       else if (inst == 0x673d)        /* move $s1, $sp */
  2256.         {
  2257.           frame_addr = sp;
  2258.           frame_reg = 17;
  2259.         }
  2260.       else if ((inst & 0xff00) == 0x0100)        /* addiu $s1,sp,n */
  2261.         {
  2262.           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
  2263.           frame_addr = sp + offset;
  2264.           frame_reg = 17;
  2265.           frame_adjust = offset;
  2266.         }
  2267.       else if ((inst & 0xFF00) == 0xd900)        /* sw reg,offset($s1) */
  2268.         {
  2269.           offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
  2270.           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
  2271.           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
  2272.         }
  2273.       else if ((inst & 0xFF00) == 0x7900)        /* sd reg,offset($s1) */
  2274.         {
  2275.           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
  2276.           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
  2277.           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
  2278.         }
  2279.       else if ((inst & 0xf81f) == 0xe809
  2280.                && (inst & 0x700) != 0x700)        /* entry */
  2281.         entry_inst = inst;        /* Save for later processing.  */
  2282.       else if ((inst & 0xff80) == 0x6480)        /* save */
  2283.         {
  2284.           save_inst = inst;        /* Save for later processing.  */
  2285.           if (prev_extend_bytes)                /* extend */
  2286.             save_inst |= prev_inst << 16;
  2287.         }
  2288.       else if ((inst & 0xff1c) == 0x6704)        /* move reg,$a0-$a3 */
  2289.         {
  2290.           /* This instruction is part of the prologue, but we don't
  2291.              need to do anything special to handle it.  */
  2292.         }
  2293.       else if (mips16_instruction_has_delay_slot (inst, 0))
  2294.                                                 /* JAL/JALR/JALX/JR */
  2295.         {
  2296.           /* The instruction in the delay slot can be a part
  2297.              of the prologue, so move forward once more.  */
  2298.           in_delay_slot = 1;
  2299.           if (mips16_instruction_has_delay_slot (inst, 1))
  2300.                                                 /* JAL/JALX */
  2301.             {
  2302.               prev_extend_bytes = MIPS_INSN16_SIZE;
  2303.               cur_pc += MIPS_INSN16_SIZE;        /* 32-bit instruction */
  2304.             }
  2305.         }
  2306.       else
  2307.         {
  2308.           this_non_prologue_insn = 1;
  2309.         }

  2310.       non_prologue_insns += this_non_prologue_insn;

  2311.       /* A jump or branch, or enough non-prologue insns seen?  If so,
  2312.          then we must have reached the end of the prologue by now.  */
  2313.       if (prev_delay_slot || non_prologue_insns > 1
  2314.           || mips16_instruction_is_compact_branch (inst))
  2315.         break;

  2316.       prev_non_prologue_insn = this_non_prologue_insn;
  2317.       prev_delay_slot = in_delay_slot;
  2318.       prev_pc = cur_pc - prev_extend_bytes;
  2319.     }

  2320.   /* The entry instruction is typically the first instruction in a function,
  2321.      and it stores registers at offsets relative to the value of the old SP
  2322.      (before the prologue).  But the value of the sp parameter to this
  2323.      function is the new SP (after the prologue has been executed).  So we
  2324.      can't calculate those offsets until we've seen the entire prologue,
  2325.      and can calculate what the old SP must have been.  */
  2326.   if (entry_inst != 0)
  2327.     {
  2328.       int areg_count = (entry_inst >> 8) & 7;
  2329.       int sreg_count = (entry_inst >> 6) & 3;

  2330.       /* The entry instruction always subtracts 32 from the SP.  */
  2331.       frame_offset += 32;

  2332.       /* Now we can calculate what the SP must have been at the
  2333.          start of the function prologue.  */
  2334.       sp += frame_offset;

  2335.       /* Check if a0-a3 were saved in the caller's argument save area.  */
  2336.       for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
  2337.         {
  2338.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2339.           offset += mips_abi_regsize (gdbarch);
  2340.         }

  2341.       /* Check if the ra register was pushed on the stack.  */
  2342.       offset = -4;
  2343.       if (entry_inst & 0x20)
  2344.         {
  2345.           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
  2346.           offset -= mips_abi_regsize (gdbarch);
  2347.         }

  2348.       /* Check if the s0 and s1 registers were pushed on the stack.  */
  2349.       for (reg = 16; reg < sreg_count + 16; reg++)
  2350.         {
  2351.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2352.           offset -= mips_abi_regsize (gdbarch);
  2353.         }
  2354.     }

  2355.   /* The SAVE instruction is similar to ENTRY, except that defined by the
  2356.      MIPS16e ASE of the MIPS Architecture.  Unlike with ENTRY though, the
  2357.      size of the frame is specified as an immediate field of instruction
  2358.      and an extended variation exists which lets additional registers and
  2359.      frame space to be specified.  The instruction always treats registers
  2360.      as 32-bit so its usefulness for 64-bit ABIs is questionable.  */
  2361.   if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
  2362.     {
  2363.       static int args_table[16] = {
  2364.         0, 0, 0, 0, 1, 1, 1, 1,
  2365.         2, 2, 2, 0, 3, 3, 4, -1,
  2366.       };
  2367.       static int astatic_table[16] = {
  2368.         0, 1, 2, 3, 0, 1, 2, 3,
  2369.         0, 1, 2, 4, 0, 1, 0, -1,
  2370.       };
  2371.       int aregs = (save_inst >> 16) & 0xf;
  2372.       int xsregs = (save_inst >> 24) & 0x7;
  2373.       int args = args_table[aregs];
  2374.       int astatic = astatic_table[aregs];
  2375.       long frame_size;

  2376.       if (args < 0)
  2377.         {
  2378.           warning (_("Invalid number of argument registers encoded in SAVE."));
  2379.           args = 0;
  2380.         }
  2381.       if (astatic < 0)
  2382.         {
  2383.           warning (_("Invalid number of static registers encoded in SAVE."));
  2384.           astatic = 0;
  2385.         }

  2386.       /* For standard SAVE the frame size of 0 means 128.  */
  2387.       frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
  2388.       if (frame_size == 0 && (save_inst >> 16) == 0)
  2389.         frame_size = 16;
  2390.       frame_size *= 8;
  2391.       frame_offset += frame_size;

  2392.       /* Now we can calculate what the SP must have been at the
  2393.          start of the function prologue.  */
  2394.       sp += frame_offset;

  2395.       /* Check if A0-A3 were saved in the caller's argument save area.  */
  2396.       for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
  2397.         {
  2398.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2399.           offset += mips_abi_regsize (gdbarch);
  2400.         }

  2401.       offset = -4;

  2402.       /* Check if the RA register was pushed on the stack.  */
  2403.       if (save_inst & 0x40)
  2404.         {
  2405.           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
  2406.           offset -= mips_abi_regsize (gdbarch);
  2407.         }

  2408.       /* Check if the S8 register was pushed on the stack.  */
  2409.       if (xsregs > 6)
  2410.         {
  2411.           set_reg_offset (gdbarch, this_cache, 30, sp + offset);
  2412.           offset -= mips_abi_regsize (gdbarch);
  2413.           xsregs--;
  2414.         }
  2415.       /* Check if S2-S7 were pushed on the stack.  */
  2416.       for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
  2417.         {
  2418.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2419.           offset -= mips_abi_regsize (gdbarch);
  2420.         }

  2421.       /* Check if the S1 register was pushed on the stack.  */
  2422.       if (save_inst & 0x10)
  2423.         {
  2424.           set_reg_offset (gdbarch, this_cache, 17, sp + offset);
  2425.           offset -= mips_abi_regsize (gdbarch);
  2426.         }
  2427.       /* Check if the S0 register was pushed on the stack.  */
  2428.       if (save_inst & 0x20)
  2429.         {
  2430.           set_reg_offset (gdbarch, this_cache, 16, sp + offset);
  2431.           offset -= mips_abi_regsize (gdbarch);
  2432.         }

  2433.       /* Check if A0-A3 were pushed on the stack.  */
  2434.       for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
  2435.         {
  2436.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  2437.           offset -= mips_abi_regsize (gdbarch);
  2438.         }
  2439.     }

  2440.   if (this_cache != NULL)
  2441.     {
  2442.       this_cache->base =
  2443.         (get_frame_register_signed (this_frame,
  2444.                                     gdbarch_num_regs (gdbarch) + frame_reg)
  2445.          + frame_offset - frame_adjust);
  2446.       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
  2447.          be able to get rid of the assignment below, evetually.  But it's
  2448.          still needed for now.  */
  2449.       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
  2450.                              + mips_regnum (gdbarch)->pc]
  2451.         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
  2452.     }

  2453.   /* Set end_prologue_addr to the address of the instruction immediately
  2454.      after the last one we scanned.  Unless the last one looked like a
  2455.      non-prologue instruction (and we looked ahead), in which case use
  2456.      its address instead.  */
  2457.   end_prologue_addr = (prev_non_prologue_insn || prev_delay_slot
  2458.                        ? prev_pc : cur_pc - prev_extend_bytes);

  2459.   return end_prologue_addr;
  2460. }

  2461. /* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
  2462.    Procedures that use the 32-bit instruction set are handled by the
  2463.    mips_insn32 unwinder.  */

  2464. static struct mips_frame_cache *
  2465. mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
  2466. {
  2467.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2468.   struct mips_frame_cache *cache;

  2469.   if ((*this_cache) != NULL)
  2470.     return (*this_cache);
  2471.   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
  2472.   (*this_cache) = cache;
  2473.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  2474.   /* Analyze the function prologue.  */
  2475.   {
  2476.     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
  2477.     CORE_ADDR start_addr;

  2478.     find_pc_partial_function (pc, NULL, &start_addr, NULL);
  2479.     if (start_addr == 0)
  2480.       start_addr = heuristic_proc_start (gdbarch, pc);
  2481.     /* We can't analyze the prologue if we couldn't find the begining
  2482.        of the function.  */
  2483.     if (start_addr == 0)
  2484.       return cache;

  2485.     mips16_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
  2486.   }

  2487.   /* gdbarch_sp_regnum contains the value and not the address.  */
  2488.   trad_frame_set_value (cache->saved_regs,
  2489.                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
  2490.                         cache->base);

  2491.   return (*this_cache);
  2492. }

  2493. static void
  2494. mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
  2495.                            struct frame_id *this_id)
  2496. {
  2497.   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
  2498.                                                            this_cache);
  2499.   /* This marks the outermost frame.  */
  2500.   if (info->base == 0)
  2501.     return;
  2502.   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
  2503. }

  2504. static struct value *
  2505. mips_insn16_frame_prev_register (struct frame_info *this_frame,
  2506.                                  void **this_cache, int regnum)
  2507. {
  2508.   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
  2509.                                                            this_cache);
  2510.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  2511. }

  2512. static int
  2513. mips_insn16_frame_sniffer (const struct frame_unwind *self,
  2514.                            struct frame_info *this_frame, void **this_cache)
  2515. {
  2516.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2517.   CORE_ADDR pc = get_frame_pc (this_frame);
  2518.   if (mips_pc_is_mips16 (gdbarch, pc))
  2519.     return 1;
  2520.   return 0;
  2521. }

  2522. static const struct frame_unwind mips_insn16_frame_unwind =
  2523. {
  2524.   NORMAL_FRAME,
  2525.   default_frame_unwind_stop_reason,
  2526.   mips_insn16_frame_this_id,
  2527.   mips_insn16_frame_prev_register,
  2528.   NULL,
  2529.   mips_insn16_frame_sniffer
  2530. };

  2531. static CORE_ADDR
  2532. mips_insn16_frame_base_address (struct frame_info *this_frame,
  2533.                                 void **this_cache)
  2534. {
  2535.   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
  2536.                                                            this_cache);
  2537.   return info->base;
  2538. }

  2539. static const struct frame_base mips_insn16_frame_base =
  2540. {
  2541.   &mips_insn16_frame_unwind,
  2542.   mips_insn16_frame_base_address,
  2543.   mips_insn16_frame_base_address,
  2544.   mips_insn16_frame_base_address
  2545. };

  2546. static const struct frame_base *
  2547. mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
  2548. {
  2549.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2550.   CORE_ADDR pc = get_frame_pc (this_frame);
  2551.   if (mips_pc_is_mips16 (gdbarch, pc))
  2552.     return &mips_insn16_frame_base;
  2553.   else
  2554.     return NULL;
  2555. }

  2556. /* Decode a 9-bit signed immediate argument of ADDIUSP -- -2 is mapped
  2557.    to -258, -1 -- to -257, 0 -- to 256, 1 -- to 257 and other values are
  2558.    interpreted directly, and then multiplied by 4.  */

  2559. static int
  2560. micromips_decode_imm9 (int imm)
  2561. {
  2562.   imm = (imm ^ 0x100) - 0x100;
  2563.   if (imm > -3 && imm < 2)
  2564.     imm ^= 0x100;
  2565.   return imm << 2;
  2566. }

  2567. /* Analyze the function prologue from START_PC to LIMIT_PC.  Return
  2568.    the address of the first instruction past the prologue.  */

  2569. static CORE_ADDR
  2570. micromips_scan_prologue (struct gdbarch *gdbarch,
  2571.                          CORE_ADDR start_pc, CORE_ADDR limit_pc,
  2572.                          struct frame_info *this_frame,
  2573.                          struct mips_frame_cache *this_cache)
  2574. {
  2575.   CORE_ADDR end_prologue_addr;
  2576.   int prev_non_prologue_insn = 0;
  2577.   int frame_reg = MIPS_SP_REGNUM;
  2578.   int this_non_prologue_insn;
  2579.   int non_prologue_insns = 0;
  2580.   long frame_offset = 0;        /* Size of stack frame.  */
  2581.   long frame_adjust = 0;        /* Offset of FP from SP.  */
  2582.   CORE_ADDR frame_addr = 0;        /* Value of $30, used as frame pointer.  */
  2583.   int prev_delay_slot = 0;
  2584.   int in_delay_slot;
  2585.   CORE_ADDR prev_pc;
  2586.   CORE_ADDR cur_pc;
  2587.   ULONGEST insn;                /* current instruction */
  2588.   CORE_ADDR sp;
  2589.   long offset;
  2590.   long sp_adj;
  2591.   long v1_off = 0;                /* The assumption is LUI will replace it.  */
  2592.   int reglist;
  2593.   int breg;
  2594.   int dreg;
  2595.   int sreg;
  2596.   int treg;
  2597.   int loc;
  2598.   int op;
  2599.   int s;
  2600.   int i;

  2601.   /* Can be called when there's no process, and hence when there's no
  2602.      THIS_FRAME.  */
  2603.   if (this_frame != NULL)
  2604.     sp = get_frame_register_signed (this_frame,
  2605.                                     gdbarch_num_regs (gdbarch)
  2606.                                     + MIPS_SP_REGNUM);
  2607.   else
  2608.     sp = 0;

  2609.   if (limit_pc > start_pc + 200)
  2610.     limit_pc = start_pc + 200;
  2611.   prev_pc = start_pc;

  2612.   /* Permit at most one non-prologue non-control-transfer instruction
  2613.      in the middle which may have been reordered by the compiler for
  2614.      optimisation.  */
  2615.   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += loc)
  2616.     {
  2617.       this_non_prologue_insn = 0;
  2618.       in_delay_slot = 0;
  2619.       sp_adj = 0;
  2620.       loc = 0;
  2621.       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, cur_pc, NULL);
  2622.       loc += MIPS_INSN16_SIZE;
  2623.       switch (mips_insn_size (ISA_MICROMIPS, insn))
  2624.         {
  2625.         /* 48-bit instructions.  */
  2626.         case 3 * MIPS_INSN16_SIZE:
  2627.           /* No prologue instructions in this category.  */
  2628.           this_non_prologue_insn = 1;
  2629.           loc += 2 * MIPS_INSN16_SIZE;
  2630.           break;

  2631.         /* 32-bit instructions.  */
  2632.         case 2 * MIPS_INSN16_SIZE:
  2633.           insn <<= 16;
  2634.           insn |= mips_fetch_instruction (gdbarch,
  2635.                                           ISA_MICROMIPS, cur_pc + loc, NULL);
  2636.           loc += MIPS_INSN16_SIZE;
  2637.           switch (micromips_op (insn >> 16))
  2638.             {
  2639.             /* Record $sp/$fp adjustment.  */
  2640.             /* Discard (D)ADDU $gp,$jp used for PIC code.  */
  2641.             case 0x0: /* POOL32A: bits 000000 */
  2642.             case 0x16: /* POOL32S: bits 010110 */
  2643.               op = b0s11_op (insn);
  2644.               sreg = b0s5_reg (insn >> 16);
  2645.               treg = b5s5_reg (insn >> 16);
  2646.               dreg = b11s5_reg (insn);
  2647.               if (op == 0x1d0
  2648.                                 /* SUBU: bits 000000 00111010000 */
  2649.                                 /* DSUBU: bits 010110 00111010000 */
  2650.                   && dreg == MIPS_SP_REGNUM && sreg == MIPS_SP_REGNUM
  2651.                   && treg == 3)
  2652.                                 /* (D)SUBU $sp, $v1 */
  2653.                     sp_adj = v1_off;
  2654.               else if (op != 0x150
  2655.                                 /* ADDU: bits 000000 00101010000 */
  2656.                                 /* DADDU: bits 010110 00101010000 */
  2657.                        || dreg != 28 || sreg != 28 || treg != MIPS_T9_REGNUM)
  2658.                 this_non_prologue_insn = 1;
  2659.               break;

  2660.             case 0x8: /* POOL32B: bits 001000 */
  2661.               op = b12s4_op (insn);
  2662.               breg = b0s5_reg (insn >> 16);
  2663.               reglist = sreg = b5s5_reg (insn >> 16);
  2664.               offset = (b0s12_imm (insn) ^ 0x800) - 0x800;
  2665.               if ((op == 0x9 || op == 0xc)
  2666.                                 /* SWP: bits 001000 1001 */
  2667.                                 /* SDP: bits 001000 1100 */
  2668.                   && breg == MIPS_SP_REGNUM && sreg < MIPS_RA_REGNUM)
  2669.                                 /* S[DW]P reg,offset($sp) */
  2670.                 {
  2671.                   s = 4 << ((b12s4_op (insn) & 0x4) == 0x4);
  2672.                   set_reg_offset (gdbarch, this_cache,
  2673.                                   sreg, sp + offset);
  2674.                   set_reg_offset (gdbarch, this_cache,
  2675.                                   sreg + 1, sp + offset + s);
  2676.                 }
  2677.               else if ((op == 0xd || op == 0xf)
  2678.                                 /* SWM: bits 001000 1101 */
  2679.                                 /* SDM: bits 001000 1111 */
  2680.                        && breg == MIPS_SP_REGNUM
  2681.                                 /* SWM reglist,offset($sp) */
  2682.                        && ((reglist >= 1 && reglist <= 9)
  2683.                            || (reglist >= 16 && reglist <= 25)))
  2684.                 {
  2685.                   int sreglist = min(reglist & 0xf, 8);

  2686.                   s = 4 << ((b12s4_op (insn) & 0x2) == 0x2);
  2687.                   for (i = 0; i < sreglist; i++)
  2688.                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + s * i);
  2689.                   if ((reglist & 0xf) > 8)
  2690.                     set_reg_offset (gdbarch, this_cache, 30, sp + s * i++);
  2691.                   if ((reglist & 0x10) == 0x10)
  2692.                     set_reg_offset (gdbarch, this_cache,
  2693.                                     MIPS_RA_REGNUM, sp + s * i++);
  2694.                 }
  2695.               else
  2696.                 this_non_prologue_insn = 1;
  2697.               break;

  2698.             /* Record $sp/$fp adjustment.  */
  2699.             /* Discard (D)ADDIU $gp used for PIC code.  */
  2700.             case 0xc: /* ADDIU: bits 001100 */
  2701.             case 0x17: /* DADDIU: bits 010111 */
  2702.               sreg = b0s5_reg (insn >> 16);
  2703.               dreg = b5s5_reg (insn >> 16);
  2704.               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
  2705.               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM)
  2706.                                 /* (D)ADDIU $sp, imm */
  2707.                 sp_adj = offset;
  2708.               else if (sreg == MIPS_SP_REGNUM && dreg == 30)
  2709.                                 /* (D)ADDIU $fp, $sp, imm */
  2710.                 {
  2711.                   frame_addr = sp + offset;
  2712.                   frame_adjust = offset;
  2713.                   frame_reg = 30;
  2714.                 }
  2715.               else if (sreg != 28 || dreg != 28)
  2716.                                 /* (D)ADDIU $gp, imm */
  2717.                 this_non_prologue_insn = 1;
  2718.               break;

  2719.             /* LUI $v1 is used for larger $sp adjustments.  */
  2720.             /* Discard LUI $gp used for PIC code.  */
  2721.             case 0x10: /* POOL32I: bits 010000 */
  2722.               if (b5s5_op (insn >> 16) == 0xd
  2723.                                 /* LUI: bits 010000 001101 */
  2724.                   && b0s5_reg (insn >> 16) == 3)
  2725.                                 /* LUI $v1, imm */
  2726.                 v1_off = ((b0s16_imm (insn) << 16) ^ 0x80000000) - 0x80000000;
  2727.               else if (b5s5_op (insn >> 16) != 0xd
  2728.                                 /* LUI: bits 010000 001101 */
  2729.                        || b0s5_reg (insn >> 16) != 28)
  2730.                                 /* LUI $gp, imm */
  2731.                 this_non_prologue_insn = 1;
  2732.               break;

  2733.             /* ORI $v1 is used for larger $sp adjustments.  */
  2734.             case 0x14: /* ORI: bits 010100 */
  2735.               sreg = b0s5_reg (insn >> 16);
  2736.               dreg = b5s5_reg (insn >> 16);
  2737.               if (sreg == 3 && dreg == 3)
  2738.                                 /* ORI $v1, imm */
  2739.                 v1_off |= b0s16_imm (insn);
  2740.               else
  2741.                 this_non_prologue_insn = 1;
  2742.               break;

  2743.             case 0x26: /* SWC1: bits 100110 */
  2744.             case 0x2e: /* SDC1: bits 101110 */
  2745.               breg = b0s5_reg (insn >> 16);
  2746.               if (breg != MIPS_SP_REGNUM)
  2747.                                 /* S[DW]C1 reg,offset($sp) */
  2748.                 this_non_prologue_insn = 1;
  2749.               break;

  2750.             case 0x36: /* SD: bits 110110 */
  2751.             case 0x3e: /* SW: bits 111110 */
  2752.               breg = b0s5_reg (insn >> 16);
  2753.               sreg = b5s5_reg (insn >> 16);
  2754.               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
  2755.               if (breg == MIPS_SP_REGNUM)
  2756.                                 /* S[DW] reg,offset($sp) */
  2757.                 set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
  2758.               else
  2759.                 this_non_prologue_insn = 1;
  2760.               break;

  2761.             default:
  2762.               /* The instruction in the delay slot can be a part
  2763.                  of the prologue, so move forward once more.  */
  2764.               if (micromips_instruction_has_delay_slot (insn, 0))
  2765.                 in_delay_slot = 1;
  2766.               else
  2767.                 this_non_prologue_insn = 1;
  2768.               break;
  2769.             }
  2770.           insn >>= 16;
  2771.           break;

  2772.         /* 16-bit instructions.  */
  2773.         case MIPS_INSN16_SIZE:
  2774.           switch (micromips_op (insn))
  2775.             {
  2776.             case 0x3: /* MOVE: bits 000011 */
  2777.               sreg = b0s5_reg (insn);
  2778.               dreg = b5s5_reg (insn);
  2779.               if (sreg == MIPS_SP_REGNUM && dreg == 30)
  2780.                                 /* MOVE  $fp, $sp */
  2781.                 {
  2782.                   frame_addr = sp;
  2783.                   frame_reg = 30;
  2784.                 }
  2785.               else if ((sreg & 0x1c) != 0x4)
  2786.                                 /* MOVE  reg, $a0-$a3 */
  2787.                 this_non_prologue_insn = 1;
  2788.               break;

  2789.             case 0x11: /* POOL16C: bits 010001 */
  2790.               if (b6s4_op (insn) == 0x5)
  2791.                                 /* SWM: bits 010001 0101 */
  2792.                 {
  2793.                   offset = ((b0s4_imm (insn) << 2) ^ 0x20) - 0x20;
  2794.                   reglist = b4s2_regl (insn);
  2795.                   for (i = 0; i <= reglist; i++)
  2796.                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + 4 * i);
  2797.                   set_reg_offset (gdbarch, this_cache,
  2798.                                   MIPS_RA_REGNUM, sp + 4 * i++);
  2799.                 }
  2800.               else
  2801.                 this_non_prologue_insn = 1;
  2802.               break;

  2803.             case 0x13: /* POOL16D: bits 010011 */
  2804.               if ((insn & 0x1) == 0x1)
  2805.                                 /* ADDIUSP: bits 010011 1 */
  2806.                 sp_adj = micromips_decode_imm9 (b1s9_imm (insn));
  2807.               else if (b5s5_reg (insn) == MIPS_SP_REGNUM)
  2808.                                 /* ADDIUS5: bits 010011 0 */
  2809.                                 /* ADDIUS5 $sp, imm */
  2810.                 sp_adj = (b1s4_imm (insn) ^ 8) - 8;
  2811.               else
  2812.                 this_non_prologue_insn = 1;
  2813.               break;

  2814.             case 0x32: /* SWSP: bits 110010 */
  2815.               offset = b0s5_imm (insn) << 2;
  2816.               sreg = b5s5_reg (insn);
  2817.               set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
  2818.               break;

  2819.             default:
  2820.               /* The instruction in the delay slot can be a part
  2821.                  of the prologue, so move forward once more.  */
  2822.               if (micromips_instruction_has_delay_slot (insn << 16, 0))
  2823.                 in_delay_slot = 1;
  2824.               else
  2825.                 this_non_prologue_insn = 1;
  2826.               break;
  2827.             }
  2828.           break;
  2829.         }
  2830.       if (sp_adj < 0)
  2831.         frame_offset -= sp_adj;

  2832.       non_prologue_insns += this_non_prologue_insn;

  2833.       /* A jump or branch, enough non-prologue insns seen or positive
  2834.          stack adjustment?  If so, then we must have reached the end
  2835.          of the prologue by now.  */
  2836.       if (prev_delay_slot || non_prologue_insns > 1 || sp_adj > 0
  2837.           || micromips_instruction_is_compact_branch (insn))
  2838.         break;

  2839.       prev_non_prologue_insn = this_non_prologue_insn;
  2840.       prev_delay_slot = in_delay_slot;
  2841.       prev_pc = cur_pc;
  2842.     }

  2843.   if (this_cache != NULL)
  2844.     {
  2845.       this_cache->base =
  2846.         (get_frame_register_signed (this_frame,
  2847.                                     gdbarch_num_regs (gdbarch) + frame_reg)
  2848.          + frame_offset - frame_adjust);
  2849.       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
  2850.          be able to get rid of the assignment below, evetually. But it's
  2851.          still needed for now.  */
  2852.       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
  2853.                              + mips_regnum (gdbarch)->pc]
  2854.         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
  2855.     }

  2856.   /* Set end_prologue_addr to the address of the instruction immediately
  2857.      after the last one we scanned.  Unless the last one looked like a
  2858.      non-prologue instruction (and we looked ahead), in which case use
  2859.      its address instead.  */
  2860.   end_prologue_addr
  2861.     = prev_non_prologue_insn || prev_delay_slot ? prev_pc : cur_pc;

  2862.   return end_prologue_addr;
  2863. }

  2864. /* Heuristic unwinder for procedures using microMIPS instructions.
  2865.    Procedures that use the 32-bit instruction set are handled by the
  2866.    mips_insn32 unwinder.  Likewise MIPS16 and the mips_insn16 unwinder. */

  2867. static struct mips_frame_cache *
  2868. mips_micro_frame_cache (struct frame_info *this_frame, void **this_cache)
  2869. {
  2870.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2871.   struct mips_frame_cache *cache;

  2872.   if ((*this_cache) != NULL)
  2873.     return (*this_cache);

  2874.   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
  2875.   (*this_cache) = cache;
  2876.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  2877.   /* Analyze the function prologue.  */
  2878.   {
  2879.     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
  2880.     CORE_ADDR start_addr;

  2881.     find_pc_partial_function (pc, NULL, &start_addr, NULL);
  2882.     if (start_addr == 0)
  2883.       start_addr = heuristic_proc_start (get_frame_arch (this_frame), pc);
  2884.     /* We can't analyze the prologue if we couldn't find the begining
  2885.        of the function.  */
  2886.     if (start_addr == 0)
  2887.       return cache;

  2888.     micromips_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
  2889.   }

  2890.   /* gdbarch_sp_regnum contains the value and not the address.  */
  2891.   trad_frame_set_value (cache->saved_regs,
  2892.                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
  2893.                         cache->base);

  2894.   return (*this_cache);
  2895. }

  2896. static void
  2897. mips_micro_frame_this_id (struct frame_info *this_frame, void **this_cache,
  2898.                           struct frame_id *this_id)
  2899. {
  2900.   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
  2901.                                                           this_cache);
  2902.   /* This marks the outermost frame.  */
  2903.   if (info->base == 0)
  2904.     return;
  2905.   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
  2906. }

  2907. static struct value *
  2908. mips_micro_frame_prev_register (struct frame_info *this_frame,
  2909.                                 void **this_cache, int regnum)
  2910. {
  2911.   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
  2912.                                                           this_cache);
  2913.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  2914. }

  2915. static int
  2916. mips_micro_frame_sniffer (const struct frame_unwind *self,
  2917.                           struct frame_info *this_frame, void **this_cache)
  2918. {
  2919.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2920.   CORE_ADDR pc = get_frame_pc (this_frame);

  2921.   if (mips_pc_is_micromips (gdbarch, pc))
  2922.     return 1;
  2923.   return 0;
  2924. }

  2925. static const struct frame_unwind mips_micro_frame_unwind =
  2926. {
  2927.   NORMAL_FRAME,
  2928.   default_frame_unwind_stop_reason,
  2929.   mips_micro_frame_this_id,
  2930.   mips_micro_frame_prev_register,
  2931.   NULL,
  2932.   mips_micro_frame_sniffer
  2933. };

  2934. static CORE_ADDR
  2935. mips_micro_frame_base_address (struct frame_info *this_frame,
  2936.                                void **this_cache)
  2937. {
  2938.   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
  2939.                                                           this_cache);
  2940.   return info->base;
  2941. }

  2942. static const struct frame_base mips_micro_frame_base =
  2943. {
  2944.   &mips_micro_frame_unwind,
  2945.   mips_micro_frame_base_address,
  2946.   mips_micro_frame_base_address,
  2947.   mips_micro_frame_base_address
  2948. };

  2949. static const struct frame_base *
  2950. mips_micro_frame_base_sniffer (struct frame_info *this_frame)
  2951. {
  2952.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2953.   CORE_ADDR pc = get_frame_pc (this_frame);

  2954.   if (mips_pc_is_micromips (gdbarch, pc))
  2955.     return &mips_micro_frame_base;
  2956.   else
  2957.     return NULL;
  2958. }

  2959. /* Mark all the registers as unset in the saved_regs array
  2960.    of THIS_CACHE.  Do nothing if THIS_CACHE is null.  */

  2961. static void
  2962. reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
  2963. {
  2964.   if (this_cache == NULL || this_cache->saved_regs == NULL)
  2965.     return;

  2966.   {
  2967.     const int num_regs = gdbarch_num_regs (gdbarch);
  2968.     int i;

  2969.     for (i = 0; i < num_regs; i++)
  2970.       {
  2971.         this_cache->saved_regs[i].addr = -1;
  2972.       }
  2973.   }
  2974. }

  2975. /* Analyze the function prologue from START_PC to LIMIT_PC.  Builds
  2976.    the associated FRAME_CACHE if not null.
  2977.    Return the address of the first instruction past the prologue.  */

  2978. static CORE_ADDR
  2979. mips32_scan_prologue (struct gdbarch *gdbarch,
  2980.                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
  2981.                       struct frame_info *this_frame,
  2982.                       struct mips_frame_cache *this_cache)
  2983. {
  2984.   int prev_non_prologue_insn;
  2985.   int this_non_prologue_insn;
  2986.   int non_prologue_insns;
  2987.   CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
  2988.                                frame-pointer.  */
  2989.   int prev_delay_slot;
  2990.   CORE_ADDR prev_pc;
  2991.   CORE_ADDR cur_pc;
  2992.   CORE_ADDR sp;
  2993.   long frame_offset;
  2994.   int  frame_reg = MIPS_SP_REGNUM;

  2995.   CORE_ADDR end_prologue_addr;
  2996.   int seen_sp_adjust = 0;
  2997.   int load_immediate_bytes = 0;
  2998.   int in_delay_slot;
  2999.   int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);

  3000.   /* Can be called when there's no process, and hence when there's no
  3001.      THIS_FRAME.  */
  3002.   if (this_frame != NULL)
  3003.     sp = get_frame_register_signed (this_frame,
  3004.                                     gdbarch_num_regs (gdbarch)
  3005.                                     + MIPS_SP_REGNUM);
  3006.   else
  3007.     sp = 0;

  3008.   if (limit_pc > start_pc + 200)
  3009.     limit_pc = start_pc + 200;

  3010. restart:
  3011.   prev_non_prologue_insn = 0;
  3012.   non_prologue_insns = 0;
  3013.   prev_delay_slot = 0;
  3014.   prev_pc = start_pc;

  3015.   /* Permit at most one non-prologue non-control-transfer instruction
  3016.      in the middle which may have been reordered by the compiler for
  3017.      optimisation.  */
  3018.   frame_offset = 0;
  3019.   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
  3020.     {
  3021.       unsigned long inst, high_word;
  3022.       long offset;
  3023.       int reg;

  3024.       this_non_prologue_insn = 0;
  3025.       in_delay_slot = 0;

  3026.       /* Fetch the instruction.  */
  3027.       inst = (unsigned long) mips_fetch_instruction (gdbarch, ISA_MIPS,
  3028.                                                      cur_pc, NULL);

  3029.       /* Save some code by pre-extracting some useful fields.  */
  3030.       high_word = (inst >> 16) & 0xffff;
  3031.       offset = ((inst & 0xffff) ^ 0x8000) - 0x8000;
  3032.       reg = high_word & 0x1f;

  3033.       if (high_word == 0x27bd                /* addiu $sp,$sp,-i */
  3034.           || high_word == 0x23bd        /* addi $sp,$sp,-i */
  3035.           || high_word == 0x67bd)        /* daddiu $sp,$sp,-i */
  3036.         {
  3037.           if (offset < 0)                /* Negative stack adjustment?  */
  3038.             frame_offset -= offset;
  3039.           else
  3040.             /* Exit loop if a positive stack adjustment is found, which
  3041.                usually means that the stack cleanup code in the function
  3042.                epilogue is reached.  */
  3043.             break;
  3044.           seen_sp_adjust = 1;
  3045.         }
  3046.       else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
  3047.                && !regsize_is_64_bits)
  3048.         {
  3049.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  3050.         }
  3051.       else if (((high_word & 0xFFE0) == 0xffa0)        /* sd reg,offset($sp) */
  3052.                && regsize_is_64_bits)
  3053.         {
  3054.           /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra.  */
  3055.           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
  3056.         }
  3057.       else if (high_word == 0x27be)        /* addiu $30,$sp,size */
  3058.         {
  3059.           /* Old gcc frame, r30 is virtual frame pointer.  */
  3060.           if (offset != frame_offset)
  3061.             frame_addr = sp + offset;
  3062.           else if (this_frame && frame_reg == MIPS_SP_REGNUM)
  3063.             {
  3064.               unsigned alloca_adjust;

  3065.               frame_reg = 30;
  3066.               frame_addr = get_frame_register_signed
  3067.                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
  3068.               frame_offset = 0;

  3069.               alloca_adjust = (unsigned) (frame_addr - (sp + offset));
  3070.               if (alloca_adjust > 0)
  3071.                 {
  3072.                   /* FP > SP + frame_size.  This may be because of
  3073.                      an alloca or somethings similar.  Fix sp to
  3074.                      "pre-alloca" value, and try again.  */
  3075.                   sp += alloca_adjust;
  3076.                   /* Need to reset the status of all registers.  Otherwise,
  3077.                      we will hit a guard that prevents the new address
  3078.                      for each register to be recomputed during the second
  3079.                      pass.  */
  3080.                   reset_saved_regs (gdbarch, this_cache);
  3081.                   goto restart;
  3082.                 }
  3083.             }
  3084.         }
  3085.       /* move $30,$sp.  With different versions of gas this will be either
  3086.          `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
  3087.          Accept any one of these.  */
  3088.       else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
  3089.         {
  3090.           /* New gcc frame, virtual frame pointer is at r30 + frame_size.  */
  3091.           if (this_frame && frame_reg == MIPS_SP_REGNUM)
  3092.             {
  3093.               unsigned alloca_adjust;

  3094.               frame_reg = 30;
  3095.               frame_addr = get_frame_register_signed
  3096.                 (this_frame, gdbarch_num_regs (gdbarch) + 30);

  3097.               alloca_adjust = (unsigned) (frame_addr - sp);
  3098.               if (alloca_adjust > 0)
  3099.                 {
  3100.                   /* FP > SP + frame_size.  This may be because of
  3101.                      an alloca or somethings similar.  Fix sp to
  3102.                      "pre-alloca" value, and try again.  */
  3103.                   sp = frame_addr;
  3104.                   /* Need to reset the status of all registers.  Otherwise,
  3105.                      we will hit a guard that prevents the new address
  3106.                      for each register to be recomputed during the second
  3107.                      pass.  */
  3108.                   reset_saved_regs (gdbarch, this_cache);
  3109.                   goto restart;
  3110.                 }
  3111.             }
  3112.         }
  3113.       else if ((high_word & 0xFFE0) == 0xafc0         /* sw reg,offset($30) */
  3114.                && !regsize_is_64_bits)
  3115.         {
  3116.           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
  3117.         }
  3118.       else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
  3119.                || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
  3120.                || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
  3121.                || high_word == 0x3c1c /* lui $gp,n */
  3122.                || high_word == 0x279c /* addiu $gp,$gp,n */
  3123.                || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
  3124.                || inst == 0x033ce021 /* addu $gp,$t9,$gp */
  3125.               )
  3126.         {
  3127.           /* These instructions are part of the prologue, but we don't
  3128.              need to do anything special to handle them.  */
  3129.         }
  3130.       /* The instructions below load $at or $t0 with an immediate
  3131.          value in preparation for a stack adjustment via
  3132.          subu $sp,$sp,[$at,$t0].  These instructions could also
  3133.          initialize a local variable, so we accept them only before
  3134.          a stack adjustment instruction was seen.  */
  3135.       else if (!seen_sp_adjust
  3136.                && !prev_delay_slot
  3137.                && (high_word == 0x3c01 /* lui $at,n */
  3138.                    || high_word == 0x3c08 /* lui $t0,n */
  3139.                    || high_word == 0x3421 /* ori $at,$at,n */
  3140.                    || high_word == 0x3508 /* ori $t0,$t0,n */
  3141.                    || high_word == 0x3401 /* ori $at,$zero,n */
  3142.                    || high_word == 0x3408 /* ori $t0,$zero,n */
  3143.                   ))
  3144.         {
  3145.           load_immediate_bytes += MIPS_INSN32_SIZE;                /* FIXME!  */
  3146.         }
  3147.       /* Check for branches and jumps.  The instruction in the delay
  3148.          slot can be a part of the prologue, so move forward once more.  */
  3149.       else if (mips32_instruction_has_delay_slot (gdbarch, inst))
  3150.         {
  3151.           in_delay_slot = 1;
  3152.         }
  3153.       /* This instruction is not an instruction typically found
  3154.          in a prologue, so we must have reached the end of the
  3155.          prologue.  */
  3156.       else
  3157.         {
  3158.           this_non_prologue_insn = 1;
  3159.         }

  3160.       non_prologue_insns += this_non_prologue_insn;

  3161.       /* A jump or branch, or enough non-prologue insns seen?  If so,
  3162.          then we must have reached the end of the prologue by now.  */
  3163.       if (prev_delay_slot || non_prologue_insns > 1)
  3164.         break;

  3165.       prev_non_prologue_insn = this_non_prologue_insn;
  3166.       prev_delay_slot = in_delay_slot;
  3167.       prev_pc = cur_pc;
  3168.     }

  3169.   if (this_cache != NULL)
  3170.     {
  3171.       this_cache->base =
  3172.         (get_frame_register_signed (this_frame,
  3173.                                     gdbarch_num_regs (gdbarch) + frame_reg)
  3174.          + frame_offset);
  3175.       /* FIXME: brobecker/2004-09-15: We should be able to get rid of
  3176.          this assignment below, eventually.  But it's still needed
  3177.          for now.  */
  3178.       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
  3179.                              + mips_regnum (gdbarch)->pc]
  3180.         = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
  3181.                                  + MIPS_RA_REGNUM];
  3182.     }

  3183.   /* Set end_prologue_addr to the address of the instruction immediately
  3184.      after the last one we scanned.  Unless the last one looked like a
  3185.      non-prologue instruction (and we looked ahead), in which case use
  3186.      its address instead.  */
  3187.   end_prologue_addr
  3188.     = prev_non_prologue_insn || prev_delay_slot ? prev_pc : cur_pc;

  3189.   /* In a frameless function, we might have incorrectly
  3190.      skipped some load immediate instructions.  Undo the skipping
  3191.      if the load immediate was not followed by a stack adjustment.  */
  3192.   if (load_immediate_bytes && !seen_sp_adjust)
  3193.     end_prologue_addr -= load_immediate_bytes;

  3194.   return end_prologue_addr;
  3195. }

  3196. /* Heuristic unwinder for procedures using 32-bit instructions (covers
  3197.    both 32-bit and 64-bit MIPS ISAs).  Procedures using 16-bit
  3198.    instructions (a.k.a. MIPS16) are handled by the mips_insn16
  3199.    unwinder.  Likewise microMIPS and the mips_micro unwinder. */

  3200. static struct mips_frame_cache *
  3201. mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
  3202. {
  3203.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  3204.   struct mips_frame_cache *cache;

  3205.   if ((*this_cache) != NULL)
  3206.     return (*this_cache);

  3207.   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
  3208.   (*this_cache) = cache;
  3209.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  3210.   /* Analyze the function prologue.  */
  3211.   {
  3212.     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
  3213.     CORE_ADDR start_addr;

  3214.     find_pc_partial_function (pc, NULL, &start_addr, NULL);
  3215.     if (start_addr == 0)
  3216.       start_addr = heuristic_proc_start (gdbarch, pc);
  3217.     /* We can't analyze the prologue if we couldn't find the begining
  3218.        of the function.  */
  3219.     if (start_addr == 0)
  3220.       return cache;

  3221.     mips32_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
  3222.   }

  3223.   /* gdbarch_sp_regnum contains the value and not the address.  */
  3224.   trad_frame_set_value (cache->saved_regs,
  3225.                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
  3226.                         cache->base);

  3227.   return (*this_cache);
  3228. }

  3229. static void
  3230. mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
  3231.                            struct frame_id *this_id)
  3232. {
  3233.   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
  3234.                                                            this_cache);
  3235.   /* This marks the outermost frame.  */
  3236.   if (info->base == 0)
  3237.     return;
  3238.   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
  3239. }

  3240. static struct value *
  3241. mips_insn32_frame_prev_register (struct frame_info *this_frame,
  3242.                                  void **this_cache, int regnum)
  3243. {
  3244.   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
  3245.                                                            this_cache);
  3246.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  3247. }

  3248. static int
  3249. mips_insn32_frame_sniffer (const struct frame_unwind *self,
  3250.                            struct frame_info *this_frame, void **this_cache)
  3251. {
  3252.   CORE_ADDR pc = get_frame_pc (this_frame);
  3253.   if (mips_pc_is_mips (pc))
  3254.     return 1;
  3255.   return 0;
  3256. }

  3257. static const struct frame_unwind mips_insn32_frame_unwind =
  3258. {
  3259.   NORMAL_FRAME,
  3260.   default_frame_unwind_stop_reason,
  3261.   mips_insn32_frame_this_id,
  3262.   mips_insn32_frame_prev_register,
  3263.   NULL,
  3264.   mips_insn32_frame_sniffer
  3265. };

  3266. static CORE_ADDR
  3267. mips_insn32_frame_base_address (struct frame_info *this_frame,
  3268.                                 void **this_cache)
  3269. {
  3270.   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
  3271.                                                            this_cache);
  3272.   return info->base;
  3273. }

  3274. static const struct frame_base mips_insn32_frame_base =
  3275. {
  3276.   &mips_insn32_frame_unwind,
  3277.   mips_insn32_frame_base_address,
  3278.   mips_insn32_frame_base_address,
  3279.   mips_insn32_frame_base_address
  3280. };

  3281. static const struct frame_base *
  3282. mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
  3283. {
  3284.   CORE_ADDR pc = get_frame_pc (this_frame);
  3285.   if (mips_pc_is_mips (pc))
  3286.     return &mips_insn32_frame_base;
  3287.   else
  3288.     return NULL;
  3289. }

  3290. static struct trad_frame_cache *
  3291. mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
  3292. {
  3293.   CORE_ADDR pc;
  3294.   CORE_ADDR start_addr;
  3295.   CORE_ADDR stack_addr;
  3296.   struct trad_frame_cache *this_trad_cache;
  3297.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  3298.   int num_regs = gdbarch_num_regs (gdbarch);

  3299.   if ((*this_cache) != NULL)
  3300.     return (*this_cache);
  3301.   this_trad_cache = trad_frame_cache_zalloc (this_frame);
  3302.   (*this_cache) = this_trad_cache;

  3303.   /* The return address is in the link register.  */
  3304.   trad_frame_set_reg_realreg (this_trad_cache,
  3305.                               gdbarch_pc_regnum (gdbarch),
  3306.                               num_regs + MIPS_RA_REGNUM);

  3307.   /* Frame ID, since it's a frameless / stackless function, no stack
  3308.      space is allocated and SP on entry is the current SP.  */
  3309.   pc = get_frame_pc (this_frame);
  3310.   find_pc_partial_function (pc, NULL, &start_addr, NULL);
  3311.   stack_addr = get_frame_register_signed (this_frame,
  3312.                                           num_regs + MIPS_SP_REGNUM);
  3313.   trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));

  3314.   /* Assume that the frame's base is the same as the
  3315.      stack-pointer.  */
  3316.   trad_frame_set_this_base (this_trad_cache, stack_addr);

  3317.   return this_trad_cache;
  3318. }

  3319. static void
  3320. mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
  3321.                          struct frame_id *this_id)
  3322. {
  3323.   struct trad_frame_cache *this_trad_cache
  3324.     = mips_stub_frame_cache (this_frame, this_cache);
  3325.   trad_frame_get_id (this_trad_cache, this_id);
  3326. }

  3327. static struct value *
  3328. mips_stub_frame_prev_register (struct frame_info *this_frame,
  3329.                                void **this_cache, int regnum)
  3330. {
  3331.   struct trad_frame_cache *this_trad_cache
  3332.     = mips_stub_frame_cache (this_frame, this_cache);
  3333.   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
  3334. }

  3335. static int
  3336. mips_stub_frame_sniffer (const struct frame_unwind *self,
  3337.                          struct frame_info *this_frame, void **this_cache)
  3338. {
  3339.   gdb_byte dummy[4];
  3340.   struct obj_section *s;
  3341.   CORE_ADDR pc = get_frame_address_in_block (this_frame);
  3342.   struct bound_minimal_symbol msym;

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

  3346.   if (in_plt_section (pc) || in_mips_stubs_section (pc))
  3347.     return 1;

  3348.   /* Calling a PIC function from a non-PIC function passes through a
  3349.      stub.  The stub for foo is named ".pic.foo".  */
  3350.   msym = lookup_minimal_symbol_by_pc (pc);
  3351.   if (msym.minsym != NULL
  3352.       && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL
  3353.       && strncmp (MSYMBOL_LINKAGE_NAME (msym.minsym), ".pic.", 5) == 0)
  3354.     return 1;

  3355.   return 0;
  3356. }

  3357. static const struct frame_unwind mips_stub_frame_unwind =
  3358. {
  3359.   NORMAL_FRAME,
  3360.   default_frame_unwind_stop_reason,
  3361.   mips_stub_frame_this_id,
  3362.   mips_stub_frame_prev_register,
  3363.   NULL,
  3364.   mips_stub_frame_sniffer
  3365. };

  3366. static CORE_ADDR
  3367. mips_stub_frame_base_address (struct frame_info *this_frame,
  3368.                               void **this_cache)
  3369. {
  3370.   struct trad_frame_cache *this_trad_cache
  3371.     = mips_stub_frame_cache (this_frame, this_cache);
  3372.   return trad_frame_get_this_base (this_trad_cache);
  3373. }

  3374. static const struct frame_base mips_stub_frame_base =
  3375. {
  3376.   &mips_stub_frame_unwind,
  3377.   mips_stub_frame_base_address,
  3378.   mips_stub_frame_base_address,
  3379.   mips_stub_frame_base_address
  3380. };

  3381. static const struct frame_base *
  3382. mips_stub_frame_base_sniffer (struct frame_info *this_frame)
  3383. {
  3384.   if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
  3385.     return &mips_stub_frame_base;
  3386.   else
  3387.     return NULL;
  3388. }

  3389. /* mips_addr_bits_remove - remove useless address bits  */

  3390. static CORE_ADDR
  3391. mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  3392. {
  3393.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  3394.   if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
  3395.     /* This hack is a work-around for existing boards using PMON, the
  3396.        simulator, and any other 64-bit targets that doesn't have true
  3397.        64-bit addressing.  On these targets, the upper 32 bits of
  3398.        addresses are ignored by the hardware.  Thus, the PC or SP are
  3399.        likely to have been sign extended to all 1s by instruction
  3400.        sequences that load 32-bit addresses.  For example, a typical
  3401.        piece of code that loads an address is this:

  3402.        lui $r2, <upper 16 bits>
  3403.        ori $r2, <lower 16 bits>

  3404.        But the lui sign-extends the value such that the upper 32 bits
  3405.        may be all 1s.  The workaround is simply to mask off these
  3406.        bits.  In the future, gcc may be changed to support true 64-bit
  3407.        addressing, and this masking will have to be disabled.  */
  3408.     return addr &= 0xffffffffUL;
  3409.   else
  3410.     return addr;
  3411. }


  3412. /* Checks for an atomic sequence of instructions beginning with a LL/LLD
  3413.    instruction and ending with a SC/SCD instruction.  If such a sequence
  3414.    is found, attempt to step through it.  A breakpoint is placed at the end of
  3415.    the sequence.  */

  3416. /* Instructions used during single-stepping of atomic sequences, standard
  3417.    ISA version.  */
  3418. #define LL_OPCODE 0x30
  3419. #define LLD_OPCODE 0x34
  3420. #define SC_OPCODE 0x38
  3421. #define SCD_OPCODE 0x3c

  3422. static int
  3423. mips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
  3424.                                  struct address_space *aspace, CORE_ADDR pc)
  3425. {
  3426.   CORE_ADDR breaks[2] = {-1, -1};
  3427.   CORE_ADDR loc = pc;
  3428.   CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination.  */
  3429.   ULONGEST insn;
  3430.   int insn_count;
  3431.   int index;
  3432.   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
  3433.   const int atomic_sequence_length = 16; /* Instruction sequence length.  */

  3434.   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
  3435.   /* Assume all atomic sequences start with a ll/lld instruction.  */
  3436.   if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
  3437.     return 0;

  3438.   /* Assume that no atomic sequence is longer than "atomic_sequence_length"
  3439.      instructions.  */
  3440.   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
  3441.     {
  3442.       int is_branch = 0;
  3443.       loc += MIPS_INSN32_SIZE;
  3444.       insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);

  3445.       /* Assume that there is at most one branch in the atomic
  3446.          sequence.  If a branch is found, put a breakpoint in its
  3447.          destination address.  */
  3448.       switch (itype_op (insn))
  3449.         {
  3450.         case 0: /* SPECIAL */
  3451.           if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
  3452.             return 0; /* fallback to the standard single-step code.  */
  3453.           break;
  3454.         case 1: /* REGIMM */
  3455.           is_branch = ((itype_rt (insn) & 0xc) == 0 /* B{LT,GE}Z* */
  3456.                        || ((itype_rt (insn) & 0x1e) == 0
  3457.                            && itype_rs (insn) == 0)); /* BPOSGE* */
  3458.           break;
  3459.         case 2: /* J */
  3460.         case 3: /* JAL */
  3461.           return 0; /* fallback to the standard single-step code.  */
  3462.         case 4: /* BEQ */
  3463.         case 5: /* BNE */
  3464.         case 6: /* BLEZ */
  3465.         case 7: /* BGTZ */
  3466.         case 20: /* BEQL */
  3467.         case 21: /* BNEL */
  3468.         case 22: /* BLEZL */
  3469.         case 23: /* BGTTL */
  3470.           is_branch = 1;
  3471.           break;
  3472.         case 17: /* COP1 */
  3473.           is_branch = ((itype_rs (insn) == 9 || itype_rs (insn) == 10)
  3474.                        && (itype_rt (insn) & 0x2) == 0);
  3475.           if (is_branch) /* BC1ANY2F, BC1ANY2T, BC1ANY4F, BC1ANY4T */
  3476.             break;
  3477.         /* Fall through.  */
  3478.         case 18: /* COP2 */
  3479.         case 19: /* COP3 */
  3480.           is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
  3481.           break;
  3482.         }
  3483.       if (is_branch)
  3484.         {
  3485.           branch_bp = loc + mips32_relative_offset (insn) + 4;
  3486.           if (last_breakpoint >= 1)
  3487.             return 0; /* More than one branch found, fallback to the
  3488.                          standard single-step code.  */
  3489.           breaks[1] = branch_bp;
  3490.           last_breakpoint++;
  3491.         }

  3492.       if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
  3493.         break;
  3494.     }

  3495.   /* Assume that the atomic sequence ends with a sc/scd instruction.  */
  3496.   if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
  3497.     return 0;

  3498.   loc += MIPS_INSN32_SIZE;

  3499.   /* Insert a breakpoint right after the end of the atomic sequence.  */
  3500.   breaks[0] = loc;

  3501.   /* Check for duplicated breakpoints.  Check also for a breakpoint
  3502.      placed (branch instruction's destination) in the atomic sequence.  */
  3503.   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
  3504.     last_breakpoint = 0;

  3505.   /* Effectively inserts the breakpoints.  */
  3506.   for (index = 0; index <= last_breakpoint; index++)
  3507.     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);

  3508.   return 1;
  3509. }

  3510. static int
  3511. micromips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
  3512.                                      struct address_space *aspace,
  3513.                                      CORE_ADDR pc)
  3514. {
  3515.   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
  3516.   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
  3517.   CORE_ADDR breaks[2] = {-1, -1};
  3518.   CORE_ADDR branch_bp = 0; /* Breakpoint at branch instruction's
  3519.                               destination.  */
  3520.   CORE_ADDR loc = pc;
  3521.   int sc_found = 0;
  3522.   ULONGEST insn;
  3523.   int insn_count;
  3524.   int index;

  3525.   /* Assume all atomic sequences start with a ll/lld instruction.  */
  3526.   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
  3527.   if (micromips_op (insn) != 0x18)        /* POOL32C: bits 011000 */
  3528.     return 0;
  3529.   loc += MIPS_INSN16_SIZE;
  3530.   insn <<= 16;
  3531.   insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
  3532.   if ((b12s4_op (insn) & 0xb) != 0x3)        /* LL, LLD: bits 011000 0x11 */
  3533.     return 0;
  3534.   loc += MIPS_INSN16_SIZE;

  3535.   /* Assume all atomic sequences end with an sc/scd instruction.  Assume
  3536.      that no atomic sequence is longer than "atomic_sequence_length"
  3537.      instructions.  */
  3538.   for (insn_count = 0;
  3539.        !sc_found && insn_count < atomic_sequence_length;
  3540.        ++insn_count)
  3541.     {
  3542.       int is_branch = 0;

  3543.       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
  3544.       loc += MIPS_INSN16_SIZE;

  3545.       /* Assume that there is at most one conditional branch in the
  3546.          atomic sequence.  If a branch is found, put a breakpoint in
  3547.          its destination address.  */
  3548.       switch (mips_insn_size (ISA_MICROMIPS, insn))
  3549.         {
  3550.         /* 48-bit instructions.  */
  3551.         case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
  3552.           loc += 2 * MIPS_INSN16_SIZE;
  3553.           break;

  3554.         /* 32-bit instructions.  */
  3555.         case 2 * MIPS_INSN16_SIZE:
  3556.           switch (micromips_op (insn))
  3557.             {
  3558.             case 0x10: /* POOL32I: bits 010000 */
  3559.               if ((b5s5_op (insn) & 0x18) != 0x0
  3560.                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
  3561.                                 /* BLEZ, BNEZC, BGTZ, BEQZC: 010000 001xx */
  3562.                   && (b5s5_op (insn) & 0x1d) != 0x11
  3563.                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
  3564.                   && ((b5s5_op (insn) & 0x1e) != 0x14
  3565.                       || (insn & 0x3) != 0x0)
  3566.                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
  3567.                   && (b5s5_op (insn) & 0x1e) != 0x1a
  3568.                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
  3569.                   && ((b5s5_op (insn) & 0x1e) != 0x1c
  3570.                       || (insn & 0x3) != 0x0)
  3571.                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
  3572.                   && ((b5s5_op (insn) & 0x1c) != 0x1c
  3573.                       || (insn & 0x3) != 0x1))
  3574.                                 /* BC1ANY*: bits 010000 111xx xxx01 */
  3575.                 break;
  3576.               /* Fall through.  */

  3577.             case 0x25: /* BEQ: bits 100101 */
  3578.             case 0x2d: /* BNE: bits 101101 */
  3579.               insn <<= 16;
  3580.               insn |= mips_fetch_instruction (gdbarch,
  3581.                                               ISA_MICROMIPS, loc, NULL);
  3582.               branch_bp = (loc + MIPS_INSN16_SIZE
  3583.                            + micromips_relative_offset16 (insn));
  3584.               is_branch = 1;
  3585.               break;

  3586.             case 0x00: /* POOL32A: bits 000000 */
  3587.               insn <<= 16;
  3588.               insn |= mips_fetch_instruction (gdbarch,
  3589.                                               ISA_MICROMIPS, loc, NULL);
  3590.               if (b0s6_op (insn) != 0x3c
  3591.                                 /* POOL32Axf: bits 000000 ... 111100 */
  3592.                   || (b6s10_ext (insn) & 0x2bf) != 0x3c)
  3593.                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
  3594.                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
  3595.                 break;
  3596.               /* Fall through.  */

  3597.             case 0x1d: /* JALS: bits 011101 */
  3598.             case 0x35: /* J: bits 110101 */
  3599.             case 0x3d: /* JAL: bits 111101 */
  3600.             case 0x3c: /* JALX: bits 111100 */
  3601.               return 0; /* Fall back to the standard single-step code. */

  3602.             case 0x18: /* POOL32C: bits 011000 */
  3603.               if ((b12s4_op (insn) & 0xb) == 0xb)
  3604.                                 /* SC, SCD: bits 011000 1x11 */
  3605.                 sc_found = 1;
  3606.               break;
  3607.             }
  3608.           loc += MIPS_INSN16_SIZE;
  3609.           break;

  3610.         /* 16-bit instructions.  */
  3611.         case MIPS_INSN16_SIZE:
  3612.           switch (micromips_op (insn))
  3613.             {
  3614.             case 0x23: /* BEQZ16: bits 100011 */
  3615.             case 0x2b: /* BNEZ16: bits 101011 */
  3616.               branch_bp = loc + micromips_relative_offset7 (insn);
  3617.               is_branch = 1;
  3618.               break;

  3619.             case 0x11: /* POOL16C: bits 010001 */
  3620.               if ((b5s5_op (insn) & 0x1c) != 0xc
  3621.                                 /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
  3622.                   && b5s5_op (insn) != 0x18)
  3623.                                 /* JRADDIUSP: bits 010001 11000 */
  3624.                 break;
  3625.               return 0; /* Fall back to the standard single-step code. */

  3626.             case 0x33: /* B16: bits 110011 */
  3627.               return 0; /* Fall back to the standard single-step code. */
  3628.             }
  3629.           break;
  3630.         }
  3631.       if (is_branch)
  3632.         {
  3633.           if (last_breakpoint >= 1)
  3634.             return 0; /* More than one branch found, fallback to the
  3635.                          standard single-step code.  */
  3636.           breaks[1] = branch_bp;
  3637.           last_breakpoint++;
  3638.         }
  3639.     }
  3640.   if (!sc_found)
  3641.     return 0;

  3642.   /* Insert a breakpoint right after the end of the atomic sequence.  */
  3643.   breaks[0] = loc;

  3644.   /* Check for duplicated breakpoints.  Check also for a breakpoint
  3645.      placed (branch instruction's destination) in the atomic sequence */
  3646.   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
  3647.     last_breakpoint = 0;

  3648.   /* Effectively inserts the breakpoints.  */
  3649.   for (index = 0; index <= last_breakpoint; index++)
  3650.     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);

  3651.   return 1;
  3652. }

  3653. static int
  3654. deal_with_atomic_sequence (struct gdbarch *gdbarch,
  3655.                            struct address_space *aspace, CORE_ADDR pc)
  3656. {
  3657.   if (mips_pc_is_mips (pc))
  3658.     return mips_deal_with_atomic_sequence (gdbarch, aspace, pc);
  3659.   else if (mips_pc_is_micromips (gdbarch, pc))
  3660.     return micromips_deal_with_atomic_sequence (gdbarch, aspace, pc);
  3661.   else
  3662.     return 0;
  3663. }

  3664. /* mips_software_single_step() is called just before we want to resume
  3665.    the inferior, if we want to single-step it but there is no hardware
  3666.    or kernel single-step support (MIPS on GNU/Linux for example).  We find
  3667.    the target of the coming instruction and breakpoint it.  */

  3668. int
  3669. mips_software_single_step (struct frame_info *frame)
  3670. {
  3671.   struct gdbarch *gdbarch = get_frame_arch (frame);
  3672.   struct address_space *aspace = get_frame_address_space (frame);
  3673.   CORE_ADDR pc, next_pc;

  3674.   pc = get_frame_pc (frame);
  3675.   if (deal_with_atomic_sequence (gdbarch, aspace, pc))
  3676.     return 1;

  3677.   next_pc = mips_next_pc (frame, pc);

  3678.   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
  3679.   return 1;
  3680. }

  3681. /* Test whether the PC points to the return instruction at the
  3682.    end of a function.  */

  3683. static int
  3684. mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
  3685. {
  3686.   ULONGEST insn;
  3687.   ULONGEST hint;

  3688.   /* This used to check for MIPS16, but this piece of code is never
  3689.      called for MIPS16 functions.  And likewise microMIPS ones.  */
  3690.   gdb_assert (mips_pc_is_mips (pc));

  3691.   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
  3692.   hint = 0x7c0;
  3693.   return (insn & ~hint) == 0x3e00008;                        /* jr(.hb) $ra */
  3694. }


  3695. /* This fencepost looks highly suspicious to me.  Removing it also
  3696.    seems suspicious as it could affect remote debugging across serial
  3697.    lines.  */

  3698. static CORE_ADDR
  3699. heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
  3700. {
  3701.   CORE_ADDR start_pc;
  3702.   CORE_ADDR fence;
  3703.   int instlen;
  3704.   int seen_adjsp = 0;
  3705.   struct inferior *inf;

  3706.   pc = gdbarch_addr_bits_remove (gdbarch, pc);
  3707.   start_pc = pc;
  3708.   fence = start_pc - heuristic_fence_post;
  3709.   if (start_pc == 0)
  3710.     return 0;

  3711.   if (heuristic_fence_post == -1 || fence < VM_MIN_ADDRESS)
  3712.     fence = VM_MIN_ADDRESS;

  3713.   instlen = mips_pc_is_mips (pc) ? MIPS_INSN32_SIZE : MIPS_INSN16_SIZE;

  3714.   inf = current_inferior ();

  3715.   /* Search back for previous return.  */
  3716.   for (start_pc -= instlen;; start_pc -= instlen)
  3717.     if (start_pc < fence)
  3718.       {
  3719.         /* It's not clear to me why we reach this point when
  3720.            stop_soon, but with this test, at least we
  3721.            don't print out warnings for every child forked (eg, on
  3722.            decstation).  22apr93 rich@cygnus.com.  */
  3723.         if (inf->control.stop_soon == NO_STOP_QUIETLY)
  3724.           {
  3725.             static int blurb_printed = 0;

  3726.             warning (_("GDB can't find the start of the function at %s."),
  3727.                      paddress (gdbarch, pc));

  3728.             if (!blurb_printed)
  3729.               {
  3730.                 /* This actually happens frequently in embedded
  3731.                    development, when you first connect to a board
  3732.                    and your stack pointer and pc are nowhere in
  3733.                    particular.  This message needs to give people
  3734.                    in that situation enough information to
  3735.                    determine that it's no big deal.  */
  3736.                 printf_filtered ("\n\
  3737.     GDB is unable to find the start of the function at %s\n\
  3738. and thus can't determine the size of that function's stack frame.\n\
  3739. This means that GDB may be unable to access that stack frame, or\n\
  3740. the frames below it.\n\
  3741.     This problem is most likely caused by an invalid program counter or\n\
  3742. stack pointer.\n\
  3743.     However, if you think GDB should simply search farther back\n\
  3744. from %s for code which looks like the beginning of a\n\
  3745. function, you can increase the range of the search using the `set\n\
  3746. heuristic-fence-post' command.\n",
  3747.                         paddress (gdbarch, pc), paddress (gdbarch, pc));
  3748.                 blurb_printed = 1;
  3749.               }
  3750.           }

  3751.         return 0;
  3752.       }
  3753.     else if (mips_pc_is_mips16 (gdbarch, start_pc))
  3754.       {
  3755.         unsigned short inst;

  3756.         /* On MIPS16, any one of the following is likely to be the
  3757.            start of a function:
  3758.              extend save
  3759.            save
  3760.            entry
  3761.            addiu sp,-n
  3762.            daddiu sp,-n
  3763.            extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'.  */
  3764.         inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, start_pc, NULL);
  3765.         if ((inst & 0xff80) == 0x6480)                /* save */
  3766.           {
  3767.             if (start_pc - instlen >= fence)
  3768.               {
  3769.                 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16,
  3770.                                                start_pc - instlen, NULL);
  3771.                 if ((inst & 0xf800) == 0xf000)        /* extend */
  3772.                   start_pc -= instlen;
  3773.               }
  3774.             break;
  3775.           }
  3776.         else if (((inst & 0xf81f) == 0xe809
  3777.                   && (inst & 0x700) != 0x700)        /* entry */
  3778.                  || (inst & 0xff80) == 0x6380        /* addiu sp,-n */
  3779.                  || (inst & 0xff80) == 0xfb80        /* daddiu sp,-n */
  3780.                  || ((inst & 0xf810) == 0xf010 && seen_adjsp))        /* extend -n */
  3781.           break;
  3782.         else if ((inst & 0xff00) == 0x6300        /* addiu sp */
  3783.                  || (inst & 0xff00) == 0xfb00)        /* daddiu sp */
  3784.           seen_adjsp = 1;
  3785.         else
  3786.           seen_adjsp = 0;
  3787.       }
  3788.     else if (mips_pc_is_micromips (gdbarch, start_pc))
  3789.       {
  3790.         ULONGEST insn;
  3791.         int stop = 0;
  3792.         long offset;
  3793.         int dreg;
  3794.         int sreg;

  3795.         /* On microMIPS, any one of the following is likely to be the
  3796.            start of a function:
  3797.            ADDIUSP -imm
  3798.            (D)ADDIU $sp, -imm
  3799.            LUI $gp, imm  */
  3800.         insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
  3801.         switch (micromips_op (insn))
  3802.           {
  3803.           case 0xc: /* ADDIU: bits 001100 */
  3804.           case 0x17: /* DADDIU: bits 010111 */
  3805.             sreg = b0s5_reg (insn);
  3806.             dreg = b5s5_reg (insn);
  3807.             insn <<= 16;
  3808.             insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS,
  3809.                                             pc + MIPS_INSN16_SIZE, NULL);
  3810.             offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
  3811.             if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
  3812.                                 /* (D)ADDIU $sp, imm */
  3813.                 && offset < 0)
  3814.               stop = 1;
  3815.             break;

  3816.           case 0x10: /* POOL32I: bits 010000 */
  3817.             if (b5s5_op (insn) == 0xd
  3818.                                 /* LUI: bits 010000 001101 */
  3819.                 && b0s5_reg (insn >> 16) == 28)
  3820.                                 /* LUI $gp, imm */
  3821.               stop = 1;
  3822.             break;

  3823.           case 0x13: /* POOL16D: bits 010011 */
  3824.             if ((insn & 0x1) == 0x1)
  3825.                                 /* ADDIUSP: bits 010011 1 */
  3826.               {
  3827.                 offset = micromips_decode_imm9 (b1s9_imm (insn));
  3828.                 if (offset < 0)
  3829.                                 /* ADDIUSP -imm */
  3830.                   stop = 1;
  3831.               }
  3832.             else
  3833.                                 /* ADDIUS5: bits 010011 0 */
  3834.               {
  3835.                 dreg = b5s5_reg (insn);
  3836.                 offset = (b1s4_imm (insn) ^ 8) - 8;
  3837.                 if (dreg == MIPS_SP_REGNUM && offset < 0)
  3838.                                 /* ADDIUS5  $sp, -imm */
  3839.                   stop = 1;
  3840.               }
  3841.             break;
  3842.           }
  3843.         if (stop)
  3844.           break;
  3845.       }
  3846.     else if (mips_about_to_return (gdbarch, start_pc))
  3847.       {
  3848.         /* Skip return and its delay slot.  */
  3849.         start_pc += 2 * MIPS_INSN32_SIZE;
  3850.         break;
  3851.       }

  3852.   return start_pc;
  3853. }

  3854. struct mips_objfile_private
  3855. {
  3856.   bfd_size_type size;
  3857.   char *contents;
  3858. };

  3859. /* According to the current ABI, should the type be passed in a
  3860.    floating-point register (assuming that there is space)?  When there
  3861.    is no FPU, FP are not even considered as possible candidates for
  3862.    FP registers and, consequently this returns false - forces FP
  3863.    arguments into integer registers.  */

  3864. static int
  3865. fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
  3866.                    struct type *arg_type)
  3867. {
  3868.   return ((typecode == TYPE_CODE_FLT
  3869.            || (MIPS_EABI (gdbarch)
  3870.                && (typecode == TYPE_CODE_STRUCT
  3871.                    || typecode == TYPE_CODE_UNION)
  3872.                && TYPE_NFIELDS (arg_type) == 1
  3873.                && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0)))
  3874.                == TYPE_CODE_FLT))
  3875.           && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
  3876. }

  3877. /* On o32, argument passing in GPRs depends on the alignment of the type being
  3878.    passed.  Return 1 if this type must be aligned to a doubleword boundary.  */

  3879. static int
  3880. mips_type_needs_double_align (struct type *type)
  3881. {
  3882.   enum type_code typecode = TYPE_CODE (type);

  3883.   if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
  3884.     return 1;
  3885.   else if (typecode == TYPE_CODE_STRUCT)
  3886.     {
  3887.       if (TYPE_NFIELDS (type) < 1)
  3888.         return 0;
  3889.       return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
  3890.     }
  3891.   else if (typecode == TYPE_CODE_UNION)
  3892.     {
  3893.       int i, n;

  3894.       n = TYPE_NFIELDS (type);
  3895.       for (i = 0; i < n; i++)
  3896.         if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
  3897.           return 1;
  3898.       return 0;
  3899.     }
  3900.   return 0;
  3901. }

  3902. /* Adjust the address downward (direction of stack growth) so that it
  3903.    is correctly aligned for a new stack frame.  */
  3904. static CORE_ADDR
  3905. mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  3906. {
  3907.   return align_down (addr, 16);
  3908. }

  3909. /* Implement the "push_dummy_code" gdbarch method.  */

  3910. static CORE_ADDR
  3911. mips_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  3912.                       CORE_ADDR funaddr, struct value **args,
  3913.                       int nargs, struct type *value_type,
  3914.                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  3915.                       struct regcache *regcache)
  3916. {
  3917.   static gdb_byte nop_insn[] = { 0, 0, 0, 0 };
  3918.   CORE_ADDR nop_addr;
  3919.   CORE_ADDR bp_slot;

  3920.   /* Reserve enough room on the stack for our breakpoint instruction.  */
  3921.   bp_slot = sp - sizeof (nop_insn);

  3922.   /* Return to microMIPS mode if calling microMIPS code to avoid
  3923.      triggering an address error exception on processors that only
  3924.      support microMIPS execution.  */
  3925.   *bp_addr = (mips_pc_is_micromips (gdbarch, funaddr)
  3926.               ? make_compact_addr (bp_slot) : bp_slot);

  3927.   /* The breakpoint layer automatically adjusts the address of
  3928.      breakpoints inserted in a branch delay slot.  With enough
  3929.      bad luck, the 4 bytes located just before our breakpoint
  3930.      instruction could look like a branch instruction, and thus
  3931.      trigger the adjustement, and break the function call entirely.
  3932.      So, we reserve those 4 bytes and write a nop instruction
  3933.      to prevent that from happening.  */
  3934.   nop_addr = bp_slot - sizeof (nop_insn);
  3935.   write_memory (nop_addr, nop_insn, sizeof (nop_insn));
  3936.   sp = mips_frame_align (gdbarch, nop_addr);

  3937.   /* Inferior resumes at the function entry point.  */
  3938.   *real_pc = funaddr;

  3939.   return sp;
  3940. }

  3941. static CORE_ADDR
  3942. mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  3943.                            struct regcache *regcache, CORE_ADDR bp_addr,
  3944.                            int nargs, struct value **args, CORE_ADDR sp,
  3945.                            int struct_return, CORE_ADDR struct_addr)
  3946. {
  3947.   int argreg;
  3948.   int float_argreg;
  3949.   int argnum;
  3950.   int len = 0;
  3951.   int stack_offset = 0;
  3952.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  3953.   CORE_ADDR func_addr = find_function_addr (function, NULL);
  3954.   int regsize = mips_abi_regsize (gdbarch);

  3955.   /* For shared libraries, "t9" needs to point at the function
  3956.      address.  */
  3957.   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);

  3958.   /* Set the return address register to point to the entry point of
  3959.      the program, where a breakpoint lies in wait.  */
  3960.   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);

  3961.   /* First ensure that the stack and structure return address (if any)
  3962.      are properly aligned.  The stack has to be at least 64-bit
  3963.      aligned even on 32-bit machines, because doubles must be 64-bit
  3964.      aligned.  For n32 and n64, stack frames need to be 128-bit
  3965.      aligned, so we round to this widest known alignment.  */

  3966.   sp = align_down (sp, 16);
  3967.   struct_addr = align_down (struct_addr, 16);

  3968.   /* Now make space on the stack for the args.  We allocate more
  3969.      than necessary for EABI, because the first few arguments are
  3970.      passed in registers, but that's OK.  */
  3971.   for (argnum = 0; argnum < nargs; argnum++)
  3972.     len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
  3973.   sp -= align_up (len, 16);

  3974.   if (mips_debug)
  3975.     fprintf_unfiltered (gdb_stdlog,
  3976.                         "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
  3977.                         paddress (gdbarch, sp), (long) align_up (len, 16));

  3978.   /* Initialize the integer and float register pointers.  */
  3979.   argreg = MIPS_A0_REGNUM;
  3980.   float_argreg = mips_fpa0_regnum (gdbarch);

  3981.   /* The struct_return pointer occupies the first parameter-passing reg.  */
  3982.   if (struct_return)
  3983.     {
  3984.       if (mips_debug)
  3985.         fprintf_unfiltered (gdb_stdlog,
  3986.                             "mips_eabi_push_dummy_call: "
  3987.                             "struct_return reg=%d %s\n",
  3988.                             argreg, paddress (gdbarch, struct_addr));
  3989.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  3990.     }

  3991.   /* Now load as many as possible of the first arguments into
  3992.      registers, and push the rest onto the stack.  Loop thru args
  3993.      from first to last.  */
  3994.   for (argnum = 0; argnum < nargs; argnum++)
  3995.     {
  3996.       const gdb_byte *val;
  3997.       gdb_byte valbuf[MAX_REGISTER_SIZE];
  3998.       struct value *arg = args[argnum];
  3999.       struct type *arg_type = check_typedef (value_type (arg));
  4000.       int len = TYPE_LENGTH (arg_type);
  4001.       enum type_code typecode = TYPE_CODE (arg_type);

  4002.       if (mips_debug)
  4003.         fprintf_unfiltered (gdb_stdlog,
  4004.                             "mips_eabi_push_dummy_call: %d len=%d type=%d",
  4005.                             argnum + 1, len, (int) typecode);

  4006.       /* The EABI passes structures that do not fit in a register by
  4007.          reference.  */
  4008.       if (len > regsize
  4009.           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
  4010.         {
  4011.           store_unsigned_integer (valbuf, regsize, byte_order,
  4012.                                   value_address (arg));
  4013.           typecode = TYPE_CODE_PTR;
  4014.           len = regsize;
  4015.           val = valbuf;
  4016.           if (mips_debug)
  4017.             fprintf_unfiltered (gdb_stdlog, " push");
  4018.         }
  4019.       else
  4020.         val = value_contents (arg);

  4021.       /* 32-bit ABIs always start floating point arguments in an
  4022.          even-numbered floating point register.  Round the FP register
  4023.          up before the check to see if there are any FP registers
  4024.          left.  Non MIPS_EABI targets also pass the FP in the integer
  4025.          registers so also round up normal registers.  */
  4026.       if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
  4027.         {
  4028.           if ((float_argreg & 1))
  4029.             float_argreg++;
  4030.         }

  4031.       /* Floating point arguments passed in registers have to be
  4032.          treated specially.  On 32-bit architectures, doubles
  4033.          are passed in register pairs; the even register gets
  4034.          the low word, and the odd register gets the high word.
  4035.          On non-EABI processors, the first two floating point arguments are
  4036.          also copied to general registers, because MIPS16 functions
  4037.          don't use float registers for arguments.  This duplication of
  4038.          arguments in general registers can't hurt non-MIPS16 functions
  4039.          because those registers are normally skipped.  */
  4040.       /* MIPS_EABI squeezes a struct that contains a single floating
  4041.          point value into an FP register instead of pushing it onto the
  4042.          stack.  */
  4043.       if (fp_register_arg_p (gdbarch, typecode, arg_type)
  4044.           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
  4045.         {
  4046.           /* EABI32 will pass doubles in consecutive registers, even on
  4047.              64-bit cores.  At one time, we used to check the size of
  4048.              `float_argreg' to determine whether or not to pass doubles
  4049.              in consecutive registers, but this is not sufficient for
  4050.              making the ABI determination.  */
  4051.           if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
  4052.             {
  4053.               int low_offset = gdbarch_byte_order (gdbarch)
  4054.                                == BFD_ENDIAN_BIG ? 4 : 0;
  4055.               long regval;

  4056.               /* Write the low word of the double to the even register(s).  */
  4057.               regval = extract_signed_integer (val + low_offset,
  4058.                                                4, byte_order);
  4059.               if (mips_debug)
  4060.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4061.                                     float_argreg, phex (regval, 4));
  4062.               regcache_cooked_write_signed (regcache, float_argreg++, regval);

  4063.               /* Write the high word of the double to the odd register(s).  */
  4064.               regval = extract_signed_integer (val + 4 - low_offset,
  4065.                                                4, byte_order);
  4066.               if (mips_debug)
  4067.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4068.                                     float_argreg, phex (regval, 4));
  4069.               regcache_cooked_write_signed (regcache, float_argreg++, regval);
  4070.             }
  4071.           else
  4072.             {
  4073.               /* This is a floating point value that fits entirely
  4074.                  in a single register.  */
  4075.               /* On 32 bit ABI's the float_argreg is further adjusted
  4076.                  above to ensure that it is even register aligned.  */
  4077.               LONGEST regval = extract_signed_integer (val, len, byte_order);
  4078.               if (mips_debug)
  4079.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4080.                                     float_argreg, phex (regval, len));
  4081.               regcache_cooked_write_signed (regcache, float_argreg++, regval);
  4082.             }
  4083.         }
  4084.       else
  4085.         {
  4086.           /* Copy the argument to general registers or the stack in
  4087.              register-sized pieces.  Large arguments are split between
  4088.              registers and stack.  */
  4089.           /* Note: structs whose size is not a multiple of regsize
  4090.              are treated specially: Irix cc passes
  4091.              them in registers where gcc sometimes puts them on the
  4092.              stack.  For maximum compatibility, we will put them in
  4093.              both places.  */
  4094.           int odd_sized_struct = (len > regsize && len % regsize != 0);

  4095.           /* Note: Floating-point values that didn't fit into an FP
  4096.              register are only written to memory.  */
  4097.           while (len > 0)
  4098.             {
  4099.               /* Remember if the argument was written to the stack.  */
  4100.               int stack_used_p = 0;
  4101.               int partial_len = (len < regsize ? len : regsize);

  4102.               if (mips_debug)
  4103.                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
  4104.                                     partial_len);

  4105.               /* Write this portion of the argument to the stack.  */
  4106.               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
  4107.                   || odd_sized_struct
  4108.                   || fp_register_arg_p (gdbarch, typecode, arg_type))
  4109.                 {
  4110.                   /* Should shorter than int integer values be
  4111.                      promoted to int before being stored?  */
  4112.                   int longword_offset = 0;
  4113.                   CORE_ADDR addr;
  4114.                   stack_used_p = 1;
  4115.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  4116.                     {
  4117.                       if (regsize == 8
  4118.                           && (typecode == TYPE_CODE_INT
  4119.                               || typecode == TYPE_CODE_PTR
  4120.                               || typecode == TYPE_CODE_FLT) && len <= 4)
  4121.                         longword_offset = regsize - len;
  4122.                       else if ((typecode == TYPE_CODE_STRUCT
  4123.                                 || typecode == TYPE_CODE_UNION)
  4124.                                && TYPE_LENGTH (arg_type) < regsize)
  4125.                         longword_offset = regsize - len;
  4126.                     }

  4127.                   if (mips_debug)
  4128.                     {
  4129.                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
  4130.                                           paddress (gdbarch, stack_offset));
  4131.                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
  4132.                                           paddress (gdbarch, longword_offset));
  4133.                     }

  4134.                   addr = sp + stack_offset + longword_offset;

  4135.                   if (mips_debug)
  4136.                     {
  4137.                       int i;
  4138.                       fprintf_unfiltered (gdb_stdlog, " @%s ",
  4139.                                           paddress (gdbarch, addr));
  4140.                       for (i = 0; i < partial_len; i++)
  4141.                         {
  4142.                           fprintf_unfiltered (gdb_stdlog, "%02x",
  4143.                                               val[i] & 0xff);
  4144.                         }
  4145.                     }
  4146.                   write_memory (addr, val, partial_len);
  4147.                 }

  4148.               /* Note!!! This is NOT an else clause.  Odd sized
  4149.                  structs may go thru BOTH paths.  Floating point
  4150.                  arguments will not.  */
  4151.               /* Write this portion of the argument to a general
  4152.                  purpose register.  */
  4153.               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
  4154.                   && !fp_register_arg_p (gdbarch, typecode, arg_type))
  4155.                 {
  4156.                   LONGEST regval =
  4157.                     extract_signed_integer (val, partial_len, byte_order);

  4158.                   if (mips_debug)
  4159.                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
  4160.                                       argreg,
  4161.                                       phex (regval, regsize));
  4162.                   regcache_cooked_write_signed (regcache, argreg, regval);
  4163.                   argreg++;
  4164.                 }

  4165.               len -= partial_len;
  4166.               val += partial_len;

  4167.               /* Compute the offset into the stack at which we will
  4168.                  copy the next parameter.

  4169.                  In the new EABI (and the NABI32), the stack_offset
  4170.                  only needs to be adjusted when it has been used.  */

  4171.               if (stack_used_p)
  4172.                 stack_offset += align_up (partial_len, regsize);
  4173.             }
  4174.         }
  4175.       if (mips_debug)
  4176.         fprintf_unfiltered (gdb_stdlog, "\n");
  4177.     }

  4178.   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);

  4179.   /* Return adjusted stack pointer.  */
  4180.   return sp;
  4181. }

  4182. /* Determine the return value convention being used.  */

  4183. static enum return_value_convention
  4184. mips_eabi_return_value (struct gdbarch *gdbarch, struct value *function,
  4185.                         struct type *type, struct regcache *regcache,
  4186.                         gdb_byte *readbuf, const gdb_byte *writebuf)
  4187. {
  4188.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  4189.   int fp_return_type = 0;
  4190.   int offset, regnum, xfer;

  4191.   if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
  4192.     return RETURN_VALUE_STRUCT_CONVENTION;

  4193.   /* Floating point type?  */
  4194.   if (tdep->mips_fpu_type != MIPS_FPU_NONE)
  4195.     {
  4196.       if (TYPE_CODE (type) == TYPE_CODE_FLT)
  4197.         fp_return_type = 1;
  4198.       /* Structs with a single field of float type
  4199.          are returned in a floating point register.  */
  4200.       if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
  4201.            || TYPE_CODE (type) == TYPE_CODE_UNION)
  4202.           && TYPE_NFIELDS (type) == 1)
  4203.         {
  4204.           struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);

  4205.           if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
  4206.             fp_return_type = 1;
  4207.         }
  4208.     }

  4209.   if (fp_return_type)
  4210.     {
  4211.       /* A floating-point value belongs in the least significant part
  4212.          of FP0/FP1.  */
  4213.       if (mips_debug)
  4214.         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
  4215.       regnum = mips_regnum (gdbarch)->fp0;
  4216.     }
  4217.   else
  4218.     {
  4219.       /* An integer value goes in V0/V1.  */
  4220.       if (mips_debug)
  4221.         fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
  4222.       regnum = MIPS_V0_REGNUM;
  4223.     }
  4224.   for (offset = 0;
  4225.        offset < TYPE_LENGTH (type);
  4226.        offset += mips_abi_regsize (gdbarch), regnum++)
  4227.     {
  4228.       xfer = mips_abi_regsize (gdbarch);
  4229.       if (offset + xfer > TYPE_LENGTH (type))
  4230.         xfer = TYPE_LENGTH (type) - offset;
  4231.       mips_xfer_register (gdbarch, regcache,
  4232.                           gdbarch_num_regs (gdbarch) + regnum, xfer,
  4233.                           gdbarch_byte_order (gdbarch), readbuf, writebuf,
  4234.                           offset);
  4235.     }

  4236.   return RETURN_VALUE_REGISTER_CONVENTION;
  4237. }


  4238. /* N32/N64 ABI stuff.  */

  4239. /* Search for a naturally aligned double at OFFSET inside a struct
  4240.    ARG_TYPE.  The N32 / N64 ABIs pass these in floating point
  4241.    registers.  */

  4242. static int
  4243. mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
  4244.                             int offset)
  4245. {
  4246.   int i;

  4247.   if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
  4248.     return 0;

  4249.   if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
  4250.     return 0;

  4251.   if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
  4252.     return 0;

  4253.   for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
  4254.     {
  4255.       int pos;
  4256.       struct type *field_type;

  4257.       /* We're only looking at normal fields.  */
  4258.       if (field_is_static (&TYPE_FIELD (arg_type, i))
  4259.           || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
  4260.         continue;

  4261.       /* If we have gone past the offset, there is no double to pass.  */
  4262.       pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
  4263.       if (pos > offset)
  4264.         return 0;

  4265.       field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));

  4266.       /* If this field is entirely before the requested offset, go
  4267.          on to the next one.  */
  4268.       if (pos + TYPE_LENGTH (field_type) <= offset)
  4269.         continue;

  4270.       /* If this is our special aligned double, we can stop.  */
  4271.       if (TYPE_CODE (field_type) == TYPE_CODE_FLT
  4272.           && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
  4273.         return 1;

  4274.       /* This field starts at or before the requested offset, and
  4275.          overlaps it.  If it is a structure, recurse inwards.  */
  4276.       return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
  4277.     }

  4278.   return 0;
  4279. }

  4280. static CORE_ADDR
  4281. mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  4282.                              struct regcache *regcache, CORE_ADDR bp_addr,
  4283.                              int nargs, struct value **args, CORE_ADDR sp,
  4284.                              int struct_return, CORE_ADDR struct_addr)
  4285. {
  4286.   int argreg;
  4287.   int float_argreg;
  4288.   int argnum;
  4289.   int len = 0;
  4290.   int stack_offset = 0;
  4291.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  4292.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  4293.   /* For shared libraries, "t9" needs to point at the function
  4294.      address.  */
  4295.   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);

  4296.   /* Set the return address register to point to the entry point of
  4297.      the program, where a breakpoint lies in wait.  */
  4298.   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);

  4299.   /* First ensure that the stack and structure return address (if any)
  4300.      are properly aligned.  The stack has to be at least 64-bit
  4301.      aligned even on 32-bit machines, because doubles must be 64-bit
  4302.      aligned.  For n32 and n64, stack frames need to be 128-bit
  4303.      aligned, so we round to this widest known alignment.  */

  4304.   sp = align_down (sp, 16);
  4305.   struct_addr = align_down (struct_addr, 16);

  4306.   /* Now make space on the stack for the args.  */
  4307.   for (argnum = 0; argnum < nargs; argnum++)
  4308.     len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
  4309.   sp -= align_up (len, 16);

  4310.   if (mips_debug)
  4311.     fprintf_unfiltered (gdb_stdlog,
  4312.                         "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
  4313.                         paddress (gdbarch, sp), (long) align_up (len, 16));

  4314.   /* Initialize the integer and float register pointers.  */
  4315.   argreg = MIPS_A0_REGNUM;
  4316.   float_argreg = mips_fpa0_regnum (gdbarch);

  4317.   /* The struct_return pointer occupies the first parameter-passing reg.  */
  4318.   if (struct_return)
  4319.     {
  4320.       if (mips_debug)
  4321.         fprintf_unfiltered (gdb_stdlog,
  4322.                             "mips_n32n64_push_dummy_call: "
  4323.                             "struct_return reg=%d %s\n",
  4324.                             argreg, paddress (gdbarch, struct_addr));
  4325.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  4326.     }

  4327.   /* Now load as many as possible of the first arguments into
  4328.      registers, and push the rest onto the stack.  Loop thru args
  4329.      from first to last.  */
  4330.   for (argnum = 0; argnum < nargs; argnum++)
  4331.     {
  4332.       const gdb_byte *val;
  4333.       struct value *arg = args[argnum];
  4334.       struct type *arg_type = check_typedef (value_type (arg));
  4335.       int len = TYPE_LENGTH (arg_type);
  4336.       enum type_code typecode = TYPE_CODE (arg_type);

  4337.       if (mips_debug)
  4338.         fprintf_unfiltered (gdb_stdlog,
  4339.                             "mips_n32n64_push_dummy_call: %d len=%d type=%d",
  4340.                             argnum + 1, len, (int) typecode);

  4341.       val = value_contents (arg);

  4342.       /* A 128-bit long double value requires an even-odd pair of
  4343.          floating-point registers.  */
  4344.       if (len == 16
  4345.           && fp_register_arg_p (gdbarch, typecode, arg_type)
  4346.           && (float_argreg & 1))
  4347.         {
  4348.           float_argreg++;
  4349.           argreg++;
  4350.         }

  4351.       if (fp_register_arg_p (gdbarch, typecode, arg_type)
  4352.           && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
  4353.         {
  4354.           /* This is a floating point value that fits entirely
  4355.              in a single register or a pair of registers.  */
  4356.           int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
  4357.           LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
  4358.           if (mips_debug)
  4359.             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4360.                                 float_argreg, phex (regval, reglen));
  4361.           regcache_cooked_write_unsigned (regcache, float_argreg, regval);

  4362.           if (mips_debug)
  4363.             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  4364.                                 argreg, phex (regval, reglen));
  4365.           regcache_cooked_write_unsigned (regcache, argreg, regval);
  4366.           float_argreg++;
  4367.           argreg++;
  4368.           if (len == 16)
  4369.             {
  4370.               regval = extract_unsigned_integer (val + reglen,
  4371.                                                  reglen, byte_order);
  4372.               if (mips_debug)
  4373.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4374.                                     float_argreg, phex (regval, reglen));
  4375.               regcache_cooked_write_unsigned (regcache, float_argreg, regval);

  4376.               if (mips_debug)
  4377.                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  4378.                                     argreg, phex (regval, reglen));
  4379.               regcache_cooked_write_unsigned (regcache, argreg, regval);
  4380.               float_argreg++;
  4381.               argreg++;
  4382.             }
  4383.         }
  4384.       else
  4385.         {
  4386.           /* Copy the argument to general registers or the stack in
  4387.              register-sized pieces.  Large arguments are split between
  4388.              registers and stack.  */
  4389.           /* For N32/N64, structs, unions, or other composite types are
  4390.              treated as a sequence of doublewords, and are passed in integer
  4391.              or floating point registers as though they were simple scalar
  4392.              parameters to the extent that they fit, with any excess on the
  4393.              stack packed according to the normal memory layout of the
  4394.              object.
  4395.              The caller does not reserve space for the register arguments;
  4396.              the callee is responsible for reserving it if required.  */
  4397.           /* Note: Floating-point values that didn't fit into an FP
  4398.              register are only written to memory.  */
  4399.           while (len > 0)
  4400.             {
  4401.               /* Remember if the argument was written to the stack.  */
  4402.               int stack_used_p = 0;
  4403.               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);

  4404.               if (mips_debug)
  4405.                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
  4406.                                     partial_len);

  4407.               if (fp_register_arg_p (gdbarch, typecode, arg_type))
  4408.                 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));

  4409.               /* Write this portion of the argument to the stack.  */
  4410.               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
  4411.                 {
  4412.                   /* Should shorter than int integer values be
  4413.                      promoted to int before being stored?  */
  4414.                   int longword_offset = 0;
  4415.                   CORE_ADDR addr;
  4416.                   stack_used_p = 1;
  4417.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  4418.                     {
  4419.                       if ((typecode == TYPE_CODE_INT
  4420.                            || typecode == TYPE_CODE_PTR)
  4421.                           && len <= 4)
  4422.                         longword_offset = MIPS64_REGSIZE - len;
  4423.                     }

  4424.                   if (mips_debug)
  4425.                     {
  4426.                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
  4427.                                           paddress (gdbarch, stack_offset));
  4428.                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
  4429.                                           paddress (gdbarch, longword_offset));
  4430.                     }

  4431.                   addr = sp + stack_offset + longword_offset;

  4432.                   if (mips_debug)
  4433.                     {
  4434.                       int i;
  4435.                       fprintf_unfiltered (gdb_stdlog, " @%s ",
  4436.                                           paddress (gdbarch, addr));
  4437.                       for (i = 0; i < partial_len; i++)
  4438.                         {
  4439.                           fprintf_unfiltered (gdb_stdlog, "%02x",
  4440.                                               val[i] & 0xff);
  4441.                         }
  4442.                     }
  4443.                   write_memory (addr, val, partial_len);
  4444.                 }

  4445.               /* Note!!! This is NOT an else clause.  Odd sized
  4446.                  structs may go thru BOTH paths.  */
  4447.               /* Write this portion of the argument to a general
  4448.                  purpose register.  */
  4449.               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
  4450.                 {
  4451.                   LONGEST regval;

  4452.                   /* Sign extend pointers, 32-bit integers and signed
  4453.                      16-bit and 8-bit integers; everything else is taken
  4454.                      as is.  */

  4455.                   if ((partial_len == 4
  4456.                        && (typecode == TYPE_CODE_PTR
  4457.                            || typecode == TYPE_CODE_INT))
  4458.                       || (partial_len < 4
  4459.                           && typecode == TYPE_CODE_INT
  4460.                           && !TYPE_UNSIGNED (arg_type)))
  4461.                     regval = extract_signed_integer (val, partial_len,
  4462.                                                      byte_order);
  4463.                   else
  4464.                     regval = extract_unsigned_integer (val, partial_len,
  4465.                                                        byte_order);

  4466.                   /* A non-floating-point argument being passed in a
  4467.                      general register.  If a struct or union, and if
  4468.                      the remaining length is smaller than the register
  4469.                      size, we have to adjust the register value on
  4470.                      big endian targets.

  4471.                      It does not seem to be necessary to do the
  4472.                      same for integral types.  */

  4473.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  4474.                       && partial_len < MIPS64_REGSIZE
  4475.                       && (typecode == TYPE_CODE_STRUCT
  4476.                           || typecode == TYPE_CODE_UNION))
  4477.                     regval <<= ((MIPS64_REGSIZE - partial_len)
  4478.                                 * TARGET_CHAR_BIT);

  4479.                   if (mips_debug)
  4480.                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
  4481.                                       argreg,
  4482.                                       phex (regval, MIPS64_REGSIZE));
  4483.                   regcache_cooked_write_unsigned (regcache, argreg, regval);

  4484.                   if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
  4485.                                                   TYPE_LENGTH (arg_type) - len))
  4486.                     {
  4487.                       if (mips_debug)
  4488.                         fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
  4489.                                           float_argreg,
  4490.                                           phex (regval, MIPS64_REGSIZE));
  4491.                       regcache_cooked_write_unsigned (regcache, float_argreg,
  4492.                                                       regval);
  4493.                     }

  4494.                   float_argreg++;
  4495.                   argreg++;
  4496.                 }

  4497.               len -= partial_len;
  4498.               val += partial_len;

  4499.               /* Compute the offset into the stack at which we will
  4500.                  copy the next parameter.

  4501.                  In N32 (N64?), the stack_offset only needs to be
  4502.                  adjusted when it has been used.  */

  4503.               if (stack_used_p)
  4504.                 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
  4505.             }
  4506.         }
  4507.       if (mips_debug)
  4508.         fprintf_unfiltered (gdb_stdlog, "\n");
  4509.     }

  4510.   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);

  4511.   /* Return adjusted stack pointer.  */
  4512.   return sp;
  4513. }

  4514. static enum return_value_convention
  4515. mips_n32n64_return_value (struct gdbarch *gdbarch, struct value *function,
  4516.                           struct type *type, struct regcache *regcache,
  4517.                           gdb_byte *readbuf, const gdb_byte *writebuf)
  4518. {
  4519.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  4520.   /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004

  4521.      Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
  4522.      if needed), as appropriate for the type.  Composite results (struct,
  4523.      union, or array) are returned in $2/$f0 and $3/$f2 according to the
  4524.      following rules:

  4525.      * A struct with only one or two floating point fields is returned in $f0
  4526.      (and $f2 if necessary).  This is a generalization of the Fortran COMPLEX
  4527.      case.

  4528.      * Any other composite results of at most 128 bits are returned in
  4529.      $2 (first 64 bits) and $3 (remainder, if necessary).

  4530.      * Larger composite results are handled by converting the function to a
  4531.      procedure with an implicit first parameter, which is a pointer to an area
  4532.      reserved by the caller to receive the result.  [The o32-bit ABI requires
  4533.      that all composite results be handled by conversion to implicit first
  4534.      parameters.  The MIPS/SGI Fortran implementation has always made a
  4535.      specific exception to return COMPLEX results in the floating point
  4536.      registers.]  */

  4537.   if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
  4538.     return RETURN_VALUE_STRUCT_CONVENTION;
  4539.   else if (TYPE_CODE (type) == TYPE_CODE_FLT
  4540.            && TYPE_LENGTH (type) == 16
  4541.            && tdep->mips_fpu_type != MIPS_FPU_NONE)
  4542.     {
  4543.       /* A 128-bit floating-point value fills both $f0 and $f2.  The
  4544.          two registers are used in the same as memory order, so the
  4545.          eight bytes with the lower memory address are in $f0.  */
  4546.       if (mips_debug)
  4547.         fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
  4548.       mips_xfer_register (gdbarch, regcache,
  4549.                           (gdbarch_num_regs (gdbarch)
  4550.                            + mips_regnum (gdbarch)->fp0),
  4551.                           8, gdbarch_byte_order (gdbarch),
  4552.                           readbuf, writebuf, 0);
  4553.       mips_xfer_register (gdbarch, regcache,
  4554.                           (gdbarch_num_regs (gdbarch)
  4555.                            + mips_regnum (gdbarch)->fp0 + 2),
  4556.                           8, gdbarch_byte_order (gdbarch),
  4557.                           readbuf ? readbuf + 8 : readbuf,
  4558.                           writebuf ? writebuf + 8 : writebuf, 0);
  4559.       return RETURN_VALUE_REGISTER_CONVENTION;
  4560.     }
  4561.   else if (TYPE_CODE (type) == TYPE_CODE_FLT
  4562.            && tdep->mips_fpu_type != MIPS_FPU_NONE)
  4563.     {
  4564.       /* A single or double floating-point value that fits in FP0.  */
  4565.       if (mips_debug)
  4566.         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
  4567.       mips_xfer_register (gdbarch, regcache,
  4568.                           (gdbarch_num_regs (gdbarch)
  4569.                            + mips_regnum (gdbarch)->fp0),
  4570.                           TYPE_LENGTH (type),
  4571.                           gdbarch_byte_order (gdbarch),
  4572.                           readbuf, writebuf, 0);
  4573.       return RETURN_VALUE_REGISTER_CONVENTION;
  4574.     }
  4575.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  4576.            && TYPE_NFIELDS (type) <= 2
  4577.            && TYPE_NFIELDS (type) >= 1
  4578.            && ((TYPE_NFIELDS (type) == 1
  4579.                 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
  4580.                     == TYPE_CODE_FLT))
  4581.                || (TYPE_NFIELDS (type) == 2
  4582.                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
  4583.                        == TYPE_CODE_FLT)
  4584.                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
  4585.                        == TYPE_CODE_FLT))))
  4586.     {
  4587.       /* A struct that contains one or two floats.  Each value is part
  4588.          in the least significant part of their floating point
  4589.          register (or GPR, for soft float).  */
  4590.       int regnum;
  4591.       int field;
  4592.       for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
  4593.                                 ? mips_regnum (gdbarch)->fp0
  4594.                                 : MIPS_V0_REGNUM);
  4595.            field < TYPE_NFIELDS (type); field++, regnum += 2)
  4596.         {
  4597.           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
  4598.                         / TARGET_CHAR_BIT);
  4599.           if (mips_debug)
  4600.             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
  4601.                                 offset);
  4602.           if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
  4603.             {
  4604.               /* A 16-byte long double field goes in two consecutive
  4605.                  registers.  */
  4606.               mips_xfer_register (gdbarch, regcache,
  4607.                                   gdbarch_num_regs (gdbarch) + regnum,
  4608.                                   8,
  4609.                                   gdbarch_byte_order (gdbarch),
  4610.                                   readbuf, writebuf, offset);
  4611.               mips_xfer_register (gdbarch, regcache,
  4612.                                   gdbarch_num_regs (gdbarch) + regnum + 1,
  4613.                                   8,
  4614.                                   gdbarch_byte_order (gdbarch),
  4615.                                   readbuf, writebuf, offset + 8);
  4616.             }
  4617.           else
  4618.             mips_xfer_register (gdbarch, regcache,
  4619.                                 gdbarch_num_regs (gdbarch) + regnum,
  4620.                                 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
  4621.                                 gdbarch_byte_order (gdbarch),
  4622.                                 readbuf, writebuf, offset);
  4623.         }
  4624.       return RETURN_VALUE_REGISTER_CONVENTION;
  4625.     }
  4626.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  4627.            || TYPE_CODE (type) == TYPE_CODE_UNION
  4628.            || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  4629.     {
  4630.       /* A composite type.  Extract the left justified value,
  4631.          regardless of the byte order.  I.e. DO NOT USE
  4632.          mips_xfer_lower.  */
  4633.       int offset;
  4634.       int regnum;
  4635.       for (offset = 0, regnum = MIPS_V0_REGNUM;
  4636.            offset < TYPE_LENGTH (type);
  4637.            offset += register_size (gdbarch, regnum), regnum++)
  4638.         {
  4639.           int xfer = register_size (gdbarch, regnum);
  4640.           if (offset + xfer > TYPE_LENGTH (type))
  4641.             xfer = TYPE_LENGTH (type) - offset;
  4642.           if (mips_debug)
  4643.             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
  4644.                                 offset, xfer, regnum);
  4645.           mips_xfer_register (gdbarch, regcache,
  4646.                               gdbarch_num_regs (gdbarch) + regnum,
  4647.                               xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
  4648.                               offset);
  4649.         }
  4650.       return RETURN_VALUE_REGISTER_CONVENTION;
  4651.     }
  4652.   else
  4653.     {
  4654.       /* A scalar extract each part but least-significant-byte
  4655.          justified.  */
  4656.       int offset;
  4657.       int regnum;
  4658.       for (offset = 0, regnum = MIPS_V0_REGNUM;
  4659.            offset < TYPE_LENGTH (type);
  4660.            offset += register_size (gdbarch, regnum), regnum++)
  4661.         {
  4662.           int xfer = register_size (gdbarch, regnum);
  4663.           if (offset + xfer > TYPE_LENGTH (type))
  4664.             xfer = TYPE_LENGTH (type) - offset;
  4665.           if (mips_debug)
  4666.             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
  4667.                                 offset, xfer, regnum);
  4668.           mips_xfer_register (gdbarch, regcache,
  4669.                               gdbarch_num_regs (gdbarch) + regnum,
  4670.                               xfer, gdbarch_byte_order (gdbarch),
  4671.                               readbuf, writebuf, offset);
  4672.         }
  4673.       return RETURN_VALUE_REGISTER_CONVENTION;
  4674.     }
  4675. }

  4676. /* Which registers to use for passing floating-point values between
  4677.    function calls, one of floating-point, general and both kinds of
  4678.    registers.  O32 and O64 use different register kinds for standard
  4679.    MIPS and MIPS16 code; to make the handling of cases where we may
  4680.    not know what kind of code is being used (e.g. no debug information)
  4681.    easier we sometimes use both kinds.  */

  4682. enum mips_fval_reg
  4683. {
  4684.   mips_fval_fpr,
  4685.   mips_fval_gpr,
  4686.   mips_fval_both
  4687. };

  4688. /* O32 ABI stuff.  */

  4689. static CORE_ADDR
  4690. mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  4691.                           struct regcache *regcache, CORE_ADDR bp_addr,
  4692.                           int nargs, struct value **args, CORE_ADDR sp,
  4693.                           int struct_return, CORE_ADDR struct_addr)
  4694. {
  4695.   int argreg;
  4696.   int float_argreg;
  4697.   int argnum;
  4698.   int len = 0;
  4699.   int stack_offset = 0;
  4700.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  4701.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  4702.   /* For shared libraries, "t9" needs to point at the function
  4703.      address.  */
  4704.   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);

  4705.   /* Set the return address register to point to the entry point of
  4706.      the program, where a breakpoint lies in wait.  */
  4707.   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);

  4708.   /* First ensure that the stack and structure return address (if any)
  4709.      are properly aligned.  The stack has to be at least 64-bit
  4710.      aligned even on 32-bit machines, because doubles must be 64-bit
  4711.      aligned.  For n32 and n64, stack frames need to be 128-bit
  4712.      aligned, so we round to this widest known alignment.  */

  4713.   sp = align_down (sp, 16);
  4714.   struct_addr = align_down (struct_addr, 16);

  4715.   /* Now make space on the stack for the args.  */
  4716.   for (argnum = 0; argnum < nargs; argnum++)
  4717.     {
  4718.       struct type *arg_type = check_typedef (value_type (args[argnum]));

  4719.       /* Align to double-word if necessary.  */
  4720.       if (mips_type_needs_double_align (arg_type))
  4721.         len = align_up (len, MIPS32_REGSIZE * 2);
  4722.       /* Allocate space on the stack.  */
  4723.       len += align_up (TYPE_LENGTH (arg_type), MIPS32_REGSIZE);
  4724.     }
  4725.   sp -= align_up (len, 16);

  4726.   if (mips_debug)
  4727.     fprintf_unfiltered (gdb_stdlog,
  4728.                         "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
  4729.                         paddress (gdbarch, sp), (long) align_up (len, 16));

  4730.   /* Initialize the integer and float register pointers.  */
  4731.   argreg = MIPS_A0_REGNUM;
  4732.   float_argreg = mips_fpa0_regnum (gdbarch);

  4733.   /* The struct_return pointer occupies the first parameter-passing reg.  */
  4734.   if (struct_return)
  4735.     {
  4736.       if (mips_debug)
  4737.         fprintf_unfiltered (gdb_stdlog,
  4738.                             "mips_o32_push_dummy_call: "
  4739.                             "struct_return reg=%d %s\n",
  4740.                             argreg, paddress (gdbarch, struct_addr));
  4741.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  4742.       stack_offset += MIPS32_REGSIZE;
  4743.     }

  4744.   /* Now load as many as possible of the first arguments into
  4745.      registers, and push the rest onto the stack.  Loop thru args
  4746.      from first to last.  */
  4747.   for (argnum = 0; argnum < nargs; argnum++)
  4748.     {
  4749.       const gdb_byte *val;
  4750.       struct value *arg = args[argnum];
  4751.       struct type *arg_type = check_typedef (value_type (arg));
  4752.       int len = TYPE_LENGTH (arg_type);
  4753.       enum type_code typecode = TYPE_CODE (arg_type);

  4754.       if (mips_debug)
  4755.         fprintf_unfiltered (gdb_stdlog,
  4756.                             "mips_o32_push_dummy_call: %d len=%d type=%d",
  4757.                             argnum + 1, len, (int) typecode);

  4758.       val = value_contents (arg);

  4759.       /* 32-bit ABIs always start floating point arguments in an
  4760.          even-numbered floating point register.  Round the FP register
  4761.          up before the check to see if there are any FP registers
  4762.          left.  O32 targets also pass the FP in the integer registers
  4763.          so also round up normal registers.  */
  4764.       if (fp_register_arg_p (gdbarch, typecode, arg_type))
  4765.         {
  4766.           if ((float_argreg & 1))
  4767.             float_argreg++;
  4768.         }

  4769.       /* Floating point arguments passed in registers have to be
  4770.          treated specially.  On 32-bit architectures, doubles are
  4771.          passed in register pairs; the even FP register gets the
  4772.          low word, and the odd FP register gets the high word.
  4773.          On O32, the first two floating point arguments are also
  4774.          copied to general registers, following their memory order,
  4775.          because MIPS16 functions don't use float registers for
  4776.          arguments.  This duplication of arguments in general
  4777.          registers can't hurt non-MIPS16 functions, because those
  4778.          registers are normally skipped.  */

  4779.       if (fp_register_arg_p (gdbarch, typecode, arg_type)
  4780.           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
  4781.         {
  4782.           if (register_size (gdbarch, float_argreg) < 8 && len == 8)
  4783.             {
  4784.               int freg_offset = gdbarch_byte_order (gdbarch)
  4785.                                 == BFD_ENDIAN_BIG ? 1 : 0;
  4786.               unsigned long regval;

  4787.               /* First word.  */
  4788.               regval = extract_unsigned_integer (val, 4, byte_order);
  4789.               if (mips_debug)
  4790.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4791.                                     float_argreg + freg_offset,
  4792.                                     phex (regval, 4));
  4793.               regcache_cooked_write_unsigned (regcache,
  4794.                                               float_argreg++ + freg_offset,
  4795.                                               regval);
  4796.               if (mips_debug)
  4797.                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  4798.                                     argreg, phex (regval, 4));
  4799.               regcache_cooked_write_unsigned (regcache, argreg++, regval);

  4800.               /* Second word.  */
  4801.               regval = extract_unsigned_integer (val + 4, 4, byte_order);
  4802.               if (mips_debug)
  4803.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4804.                                     float_argreg - freg_offset,
  4805.                                     phex (regval, 4));
  4806.               regcache_cooked_write_unsigned (regcache,
  4807.                                               float_argreg++ - freg_offset,
  4808.                                               regval);
  4809.               if (mips_debug)
  4810.                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  4811.                                     argreg, phex (regval, 4));
  4812.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  4813.             }
  4814.           else
  4815.             {
  4816.               /* This is a floating point value that fits entirely
  4817.                  in a single register.  */
  4818.               /* On 32 bit ABI's the float_argreg is further adjusted
  4819.                  above to ensure that it is even register aligned.  */
  4820.               LONGEST regval = extract_unsigned_integer (val, len, byte_order);
  4821.               if (mips_debug)
  4822.                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  4823.                                     float_argreg, phex (regval, len));
  4824.               regcache_cooked_write_unsigned (regcache,
  4825.                                               float_argreg++, regval);
  4826.               /* Although two FP registers are reserved for each
  4827.                  argument, only one corresponding integer register is
  4828.                  reserved.  */
  4829.               if (mips_debug)
  4830.                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  4831.                                     argreg, phex (regval, len));
  4832.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  4833.             }
  4834.           /* Reserve space for the FP register.  */
  4835.           stack_offset += align_up (len, MIPS32_REGSIZE);
  4836.         }
  4837.       else
  4838.         {
  4839.           /* Copy the argument to general registers or the stack in
  4840.              register-sized pieces.  Large arguments are split between
  4841.              registers and stack.  */
  4842.           /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
  4843.              are treated specially: Irix cc passes
  4844.              them in registers where gcc sometimes puts them on the
  4845.              stack.  For maximum compatibility, we will put them in
  4846.              both places.  */
  4847.           int odd_sized_struct = (len > MIPS32_REGSIZE
  4848.                                   && len % MIPS32_REGSIZE != 0);
  4849.           /* Structures should be aligned to eight bytes (even arg registers)
  4850.              on MIPS_ABI_O32, if their first member has double precision.  */
  4851.           if (mips_type_needs_double_align (arg_type))
  4852.             {
  4853.               if ((argreg & 1))
  4854.                 {
  4855.                   argreg++;
  4856.                   stack_offset += MIPS32_REGSIZE;
  4857.                 }
  4858.             }
  4859.           while (len > 0)
  4860.             {
  4861.               /* Remember if the argument was written to the stack.  */
  4862.               int stack_used_p = 0;
  4863.               int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);

  4864.               if (mips_debug)
  4865.                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
  4866.                                     partial_len);

  4867.               /* Write this portion of the argument to the stack.  */
  4868.               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
  4869.                   || odd_sized_struct)
  4870.                 {
  4871.                   /* Should shorter than int integer values be
  4872.                      promoted to int before being stored?  */
  4873.                   int longword_offset = 0;
  4874.                   CORE_ADDR addr;
  4875.                   stack_used_p = 1;

  4876.                   if (mips_debug)
  4877.                     {
  4878.                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
  4879.                                           paddress (gdbarch, stack_offset));
  4880.                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
  4881.                                           paddress (gdbarch, longword_offset));
  4882.                     }

  4883.                   addr = sp + stack_offset + longword_offset;

  4884.                   if (mips_debug)
  4885.                     {
  4886.                       int i;
  4887.                       fprintf_unfiltered (gdb_stdlog, " @%s ",
  4888.                                           paddress (gdbarch, addr));
  4889.                       for (i = 0; i < partial_len; i++)
  4890.                         {
  4891.                           fprintf_unfiltered (gdb_stdlog, "%02x",
  4892.                                               val[i] & 0xff);
  4893.                         }
  4894.                     }
  4895.                   write_memory (addr, val, partial_len);
  4896.                 }

  4897.               /* Note!!! This is NOT an else clause.  Odd sized
  4898.                  structs may go thru BOTH paths.  */
  4899.               /* Write this portion of the argument to a general
  4900.                  purpose register.  */
  4901.               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
  4902.                 {
  4903.                   LONGEST regval = extract_signed_integer (val, partial_len,
  4904.                                                            byte_order);
  4905.                   /* Value may need to be sign extended, because
  4906.                      mips_isa_regsize() != mips_abi_regsize().  */

  4907.                   /* A non-floating-point argument being passed in a
  4908.                      general register.  If a struct or union, and if
  4909.                      the remaining length is smaller than the register
  4910.                      size, we have to adjust the register value on
  4911.                      big endian targets.

  4912.                      It does not seem to be necessary to do the
  4913.                      same for integral types.

  4914.                      Also don't do this adjustment on O64 binaries.

  4915.                      cagney/2001-07-23: gdb/179: Also, GCC, when
  4916.                      outputting LE O32 with sizeof (struct) <
  4917.                      mips_abi_regsize(), generates a left shift
  4918.                      as part of storing the argument in a register
  4919.                      (the left shift isn't generated when
  4920.                      sizeof (struct) >= mips_abi_regsize()).  Since
  4921.                      it is quite possible that this is GCC
  4922.                      contradicting the LE/O32 ABI, GDB has not been
  4923.                      adjusted to accommodate this.  Either someone
  4924.                      needs to demonstrate that the LE/O32 ABI
  4925.                      specifies such a left shift OR this new ABI gets
  4926.                      identified as such and GDB gets tweaked
  4927.                      accordingly.  */

  4928.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  4929.                       && partial_len < MIPS32_REGSIZE
  4930.                       && (typecode == TYPE_CODE_STRUCT
  4931.                           || typecode == TYPE_CODE_UNION))
  4932.                     regval <<= ((MIPS32_REGSIZE - partial_len)
  4933.                                 * TARGET_CHAR_BIT);

  4934.                   if (mips_debug)
  4935.                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
  4936.                                       argreg,
  4937.                                       phex (regval, MIPS32_REGSIZE));
  4938.                   regcache_cooked_write_unsigned (regcache, argreg, regval);
  4939.                   argreg++;

  4940.                   /* Prevent subsequent floating point arguments from
  4941.                      being passed in floating point registers.  */
  4942.                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
  4943.                 }

  4944.               len -= partial_len;
  4945.               val += partial_len;

  4946.               /* Compute the offset into the stack at which we will
  4947.                  copy the next parameter.

  4948.                  In older ABIs, the caller reserved space for
  4949.                  registers that contained arguments.  This was loosely
  4950.                  refered to as their "home".  Consequently, space is
  4951.                  always allocated.  */

  4952.               stack_offset += align_up (partial_len, MIPS32_REGSIZE);
  4953.             }
  4954.         }
  4955.       if (mips_debug)
  4956.         fprintf_unfiltered (gdb_stdlog, "\n");
  4957.     }

  4958.   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);

  4959.   /* Return adjusted stack pointer.  */
  4960.   return sp;
  4961. }

  4962. static enum return_value_convention
  4963. mips_o32_return_value (struct gdbarch *gdbarch, struct value *function,
  4964.                        struct type *type, struct regcache *regcache,
  4965.                        gdb_byte *readbuf, const gdb_byte *writebuf)
  4966. {
  4967.   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
  4968.   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
  4969.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  4970.   enum mips_fval_reg fval_reg;

  4971.   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
  4972.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  4973.       || TYPE_CODE (type) == TYPE_CODE_UNION
  4974.       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  4975.     return RETURN_VALUE_STRUCT_CONVENTION;
  4976.   else if (TYPE_CODE (type) == TYPE_CODE_FLT
  4977.            && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
  4978.     {
  4979.       /* A single-precision floating-point value.  If reading in or copying,
  4980.          then we get it from/put it to FP0 for standard MIPS code or GPR2
  4981.          for MIPS16 code.  If writing out only, then we put it to both FP0
  4982.          and GPR2.  We do not support reading in with no function known, if
  4983.          this safety check ever triggers, then we'll have to try harder.  */
  4984.       gdb_assert (function || !readbuf);
  4985.       if (mips_debug)
  4986.         switch (fval_reg)
  4987.           {
  4988.           case mips_fval_fpr:
  4989.             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
  4990.             break;
  4991.           case mips_fval_gpr:
  4992.             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
  4993.             break;
  4994.           case mips_fval_both:
  4995.             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
  4996.             break;
  4997.           }
  4998.       if (fval_reg != mips_fval_gpr)
  4999.         mips_xfer_register (gdbarch, regcache,
  5000.                             (gdbarch_num_regs (gdbarch)
  5001.                              + mips_regnum (gdbarch)->fp0),
  5002.                             TYPE_LENGTH (type),
  5003.                             gdbarch_byte_order (gdbarch),
  5004.                             readbuf, writebuf, 0);
  5005.       if (fval_reg != mips_fval_fpr)
  5006.         mips_xfer_register (gdbarch, regcache,
  5007.                             gdbarch_num_regs (gdbarch) + 2,
  5008.                             TYPE_LENGTH (type),
  5009.                             gdbarch_byte_order (gdbarch),
  5010.                             readbuf, writebuf, 0);
  5011.       return RETURN_VALUE_REGISTER_CONVENTION;
  5012.     }
  5013.   else if (TYPE_CODE (type) == TYPE_CODE_FLT
  5014.            && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
  5015.     {
  5016.       /* A double-precision floating-point value.  If reading in or copying,
  5017.          then we get it from/put it to FP1 and FP0 for standard MIPS code or
  5018.          GPR2 and GPR3 for MIPS16 code.  If writing out only, then we put it
  5019.          to both FP1/FP0 and GPR2/GPR3.  We do not support reading in with
  5020.          no function known, if this safety check ever triggers, then we'll
  5021.          have to try harder.  */
  5022.       gdb_assert (function || !readbuf);
  5023.       if (mips_debug)
  5024.         switch (fval_reg)
  5025.           {
  5026.           case mips_fval_fpr:
  5027.             fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
  5028.             break;
  5029.           case mips_fval_gpr:
  5030.             fprintf_unfiltered (gdb_stderr, "Return float in $2/$3\n");
  5031.             break;
  5032.           case mips_fval_both:
  5033.             fprintf_unfiltered (gdb_stderr,
  5034.                                 "Return float in $fp1/$fp0 and $2/$3\n");
  5035.             break;
  5036.           }
  5037.       if (fval_reg != mips_fval_gpr)
  5038.         {
  5039.           /* The most significant part goes in FP1, and the least significant
  5040.              in FP0.  */
  5041.           switch (gdbarch_byte_order (gdbarch))
  5042.             {
  5043.             case BFD_ENDIAN_LITTLE:
  5044.               mips_xfer_register (gdbarch, regcache,
  5045.                                   (gdbarch_num_regs (gdbarch)
  5046.                                    + mips_regnum (gdbarch)->fp0 + 0),
  5047.                                   4, gdbarch_byte_order (gdbarch),
  5048.                                   readbuf, writebuf, 0);
  5049.               mips_xfer_register (gdbarch, regcache,
  5050.                                   (gdbarch_num_regs (gdbarch)
  5051.                                    + mips_regnum (gdbarch)->fp0 + 1),
  5052.                                   4, gdbarch_byte_order (gdbarch),
  5053.                                   readbuf, writebuf, 4);
  5054.               break;
  5055.             case BFD_ENDIAN_BIG:
  5056.               mips_xfer_register (gdbarch, regcache,
  5057.                                   (gdbarch_num_regs (gdbarch)
  5058.                                    + mips_regnum (gdbarch)->fp0 + 1),
  5059.                                   4, gdbarch_byte_order (gdbarch),
  5060.                                   readbuf, writebuf, 0);
  5061.               mips_xfer_register (gdbarch, regcache,
  5062.                                   (gdbarch_num_regs (gdbarch)
  5063.                                    + mips_regnum (gdbarch)->fp0 + 0),
  5064.                                   4, gdbarch_byte_order (gdbarch),
  5065.                                   readbuf, writebuf, 4);
  5066.               break;
  5067.             default:
  5068.               internal_error (__FILE__, __LINE__, _("bad switch"));
  5069.             }
  5070.         }
  5071.       if (fval_reg != mips_fval_fpr)
  5072.         {
  5073.           /* The two 32-bit parts are always placed in GPR2 and GPR3
  5074.              following these registers' memory order.  */
  5075.           mips_xfer_register (gdbarch, regcache,
  5076.                               gdbarch_num_regs (gdbarch) + 2,
  5077.                               4, gdbarch_byte_order (gdbarch),
  5078.                               readbuf, writebuf, 0);
  5079.           mips_xfer_register (gdbarch, regcache,
  5080.                               gdbarch_num_regs (gdbarch) + 3,
  5081.                               4, gdbarch_byte_order (gdbarch),
  5082.                               readbuf, writebuf, 4);
  5083.         }
  5084.       return RETURN_VALUE_REGISTER_CONVENTION;
  5085.     }
  5086. #if 0
  5087.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  5088.            && TYPE_NFIELDS (type) <= 2
  5089.            && TYPE_NFIELDS (type) >= 1
  5090.            && ((TYPE_NFIELDS (type) == 1
  5091.                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
  5092.                     == TYPE_CODE_FLT))
  5093.                || (TYPE_NFIELDS (type) == 2
  5094.                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
  5095.                        == TYPE_CODE_FLT)
  5096.                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
  5097.                        == TYPE_CODE_FLT)))
  5098.            && tdep->mips_fpu_type != MIPS_FPU_NONE)
  5099.     {
  5100.       /* A struct that contains one or two floats.  Each value is part
  5101.          in the least significant part of their floating point
  5102.          register..  */
  5103.       gdb_byte reg[MAX_REGISTER_SIZE];
  5104.       int regnum;
  5105.       int field;
  5106.       for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
  5107.            field < TYPE_NFIELDS (type); field++, regnum += 2)
  5108.         {
  5109.           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
  5110.                         / TARGET_CHAR_BIT);
  5111.           if (mips_debug)
  5112.             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
  5113.                                 offset);
  5114.           mips_xfer_register (gdbarch, regcache,
  5115.                               gdbarch_num_regs (gdbarch) + regnum,
  5116.                               TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
  5117.                               gdbarch_byte_order (gdbarch),
  5118.                               readbuf, writebuf, offset);
  5119.         }
  5120.       return RETURN_VALUE_REGISTER_CONVENTION;
  5121.     }
  5122. #endif
  5123. #if 0
  5124.   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  5125.            || TYPE_CODE (type) == TYPE_CODE_UNION)
  5126.     {
  5127.       /* A structure or union.  Extract the left justified value,
  5128.          regardless of the byte order.  I.e. DO NOT USE
  5129.          mips_xfer_lower.  */
  5130.       int offset;
  5131.       int regnum;
  5132.       for (offset = 0, regnum = MIPS_V0_REGNUM;
  5133.            offset < TYPE_LENGTH (type);
  5134.            offset += register_size (gdbarch, regnum), regnum++)
  5135.         {
  5136.           int xfer = register_size (gdbarch, regnum);
  5137.           if (offset + xfer > TYPE_LENGTH (type))
  5138.             xfer = TYPE_LENGTH (type) - offset;
  5139.           if (mips_debug)
  5140.             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
  5141.                                 offset, xfer, regnum);
  5142.           mips_xfer_register (gdbarch, regcache,
  5143.                               gdbarch_num_regs (gdbarch) + regnum, xfer,
  5144.                               BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
  5145.         }
  5146.       return RETURN_VALUE_REGISTER_CONVENTION;
  5147.     }
  5148. #endif
  5149.   else
  5150.     {
  5151.       /* A scalar extract each part but least-significant-byte
  5152.          justified.  o32 thinks registers are 4 byte, regardless of
  5153.          the ISA.  */
  5154.       int offset;
  5155.       int regnum;
  5156.       for (offset = 0, regnum = MIPS_V0_REGNUM;
  5157.            offset < TYPE_LENGTH (type);
  5158.            offset += MIPS32_REGSIZE, regnum++)
  5159.         {
  5160.           int xfer = MIPS32_REGSIZE;
  5161.           if (offset + xfer > TYPE_LENGTH (type))
  5162.             xfer = TYPE_LENGTH (type) - offset;
  5163.           if (mips_debug)
  5164.             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
  5165.                                 offset, xfer, regnum);
  5166.           mips_xfer_register (gdbarch, regcache,
  5167.                               gdbarch_num_regs (gdbarch) + regnum, xfer,
  5168.                               gdbarch_byte_order (gdbarch),
  5169.                               readbuf, writebuf, offset);
  5170.         }
  5171.       return RETURN_VALUE_REGISTER_CONVENTION;
  5172.     }
  5173. }

  5174. /* O64 ABI.  This is a hacked up kind of 64-bit version of the o32
  5175.    ABI.  */

  5176. static CORE_ADDR
  5177. mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  5178.                           struct regcache *regcache, CORE_ADDR bp_addr,
  5179.                           int nargs,
  5180.                           struct value **args, CORE_ADDR sp,
  5181.                           int struct_return, CORE_ADDR struct_addr)
  5182. {
  5183.   int argreg;
  5184.   int float_argreg;
  5185.   int argnum;
  5186.   int len = 0;
  5187.   int stack_offset = 0;
  5188.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  5189.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  5190.   /* For shared libraries, "t9" needs to point at the function
  5191.      address.  */
  5192.   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);

  5193.   /* Set the return address register to point to the entry point of
  5194.      the program, where a breakpoint lies in wait.  */
  5195.   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);

  5196.   /* First ensure that the stack and structure return address (if any)
  5197.      are properly aligned.  The stack has to be at least 64-bit
  5198.      aligned even on 32-bit machines, because doubles must be 64-bit
  5199.      aligned.  For n32 and n64, stack frames need to be 128-bit
  5200.      aligned, so we round to this widest known alignment.  */

  5201.   sp = align_down (sp, 16);
  5202.   struct_addr = align_down (struct_addr, 16);

  5203.   /* Now make space on the stack for the args.  */
  5204.   for (argnum = 0; argnum < nargs; argnum++)
  5205.     {
  5206.       struct type *arg_type = check_typedef (value_type (args[argnum]));

  5207.       /* Allocate space on the stack.  */
  5208.       len += align_up (TYPE_LENGTH (arg_type), MIPS64_REGSIZE);
  5209.     }
  5210.   sp -= align_up (len, 16);

  5211.   if (mips_debug)
  5212.     fprintf_unfiltered (gdb_stdlog,
  5213.                         "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
  5214.                         paddress (gdbarch, sp), (long) align_up (len, 16));

  5215.   /* Initialize the integer and float register pointers.  */
  5216.   argreg = MIPS_A0_REGNUM;
  5217.   float_argreg = mips_fpa0_regnum (gdbarch);

  5218.   /* The struct_return pointer occupies the first parameter-passing reg.  */
  5219.   if (struct_return)
  5220.     {
  5221.       if (mips_debug)
  5222.         fprintf_unfiltered (gdb_stdlog,
  5223.                             "mips_o64_push_dummy_call: "
  5224.                             "struct_return reg=%d %s\n",
  5225.                             argreg, paddress (gdbarch, struct_addr));
  5226.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  5227.       stack_offset += MIPS64_REGSIZE;
  5228.     }

  5229.   /* Now load as many as possible of the first arguments into
  5230.      registers, and push the rest onto the stack.  Loop thru args
  5231.      from first to last.  */
  5232.   for (argnum = 0; argnum < nargs; argnum++)
  5233.     {
  5234.       const gdb_byte *val;
  5235.       struct value *arg = args[argnum];
  5236.       struct type *arg_type = check_typedef (value_type (arg));
  5237.       int len = TYPE_LENGTH (arg_type);
  5238.       enum type_code typecode = TYPE_CODE (arg_type);

  5239.       if (mips_debug)
  5240.         fprintf_unfiltered (gdb_stdlog,
  5241.                             "mips_o64_push_dummy_call: %d len=%d type=%d",
  5242.                             argnum + 1, len, (int) typecode);

  5243.       val = value_contents (arg);

  5244.       /* Floating point arguments passed in registers have to be
  5245.          treated specially.  On 32-bit architectures, doubles are
  5246.          passed in register pairs; the even FP register gets the
  5247.          low word, and the odd FP register gets the high word.
  5248.          On O64, the first two floating point arguments are also
  5249.          copied to general registers, because MIPS16 functions
  5250.          don't use float registers for arguments.  This duplication
  5251.          of arguments in general registers can't hurt non-MIPS16
  5252.          functions because those registers are normally skipped.  */

  5253.       if (fp_register_arg_p (gdbarch, typecode, arg_type)
  5254.           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
  5255.         {
  5256.           LONGEST regval = extract_unsigned_integer (val, len, byte_order);
  5257.           if (mips_debug)
  5258.             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
  5259.                                 float_argreg, phex (regval, len));
  5260.           regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
  5261.           if (mips_debug)
  5262.             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
  5263.                                 argreg, phex (regval, len));
  5264.           regcache_cooked_write_unsigned (regcache, argreg, regval);
  5265.           argreg++;
  5266.           /* Reserve space for the FP register.  */
  5267.           stack_offset += align_up (len, MIPS64_REGSIZE);
  5268.         }
  5269.       else
  5270.         {
  5271.           /* Copy the argument to general registers or the stack in
  5272.              register-sized pieces.  Large arguments are split between
  5273.              registers and stack.  */
  5274.           /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
  5275.              are treated specially: Irix cc passes them in registers
  5276.              where gcc sometimes puts them on the stack.  For maximum
  5277.              compatibility, we will put them in both places.  */
  5278.           int odd_sized_struct = (len > MIPS64_REGSIZE
  5279.                                   && len % MIPS64_REGSIZE != 0);
  5280.           while (len > 0)
  5281.             {
  5282.               /* Remember if the argument was written to the stack.  */
  5283.               int stack_used_p = 0;
  5284.               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);

  5285.               if (mips_debug)
  5286.                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
  5287.                                     partial_len);

  5288.               /* Write this portion of the argument to the stack.  */
  5289.               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
  5290.                   || odd_sized_struct)
  5291.                 {
  5292.                   /* Should shorter than int integer values be
  5293.                      promoted to int before being stored?  */
  5294.                   int longword_offset = 0;
  5295.                   CORE_ADDR addr;
  5296.                   stack_used_p = 1;
  5297.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  5298.                     {
  5299.                       if ((typecode == TYPE_CODE_INT
  5300.                            || typecode == TYPE_CODE_PTR
  5301.                            || typecode == TYPE_CODE_FLT)
  5302.                           && len <= 4)
  5303.                         longword_offset = MIPS64_REGSIZE - len;
  5304.                     }

  5305.                   if (mips_debug)
  5306.                     {
  5307.                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
  5308.                                           paddress (gdbarch, stack_offset));
  5309.                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
  5310.                                           paddress (gdbarch, longword_offset));
  5311.                     }

  5312.                   addr = sp + stack_offset + longword_offset;

  5313.                   if (mips_debug)
  5314.                     {
  5315.                       int i;
  5316.                       fprintf_unfiltered (gdb_stdlog, " @%s ",
  5317.                                           paddress (gdbarch, addr));
  5318.                       for (i = 0; i < partial_len; i++)
  5319.                         {
  5320.                           fprintf_unfiltered (gdb_stdlog, "%02x",
  5321.                                               val[i] & 0xff);
  5322.                         }
  5323.                     }
  5324.                   write_memory (addr, val, partial_len);
  5325.                 }

  5326.               /* Note!!! This is NOT an else clause.  Odd sized
  5327.                  structs may go thru BOTH paths.  */
  5328.               /* Write this portion of the argument to a general
  5329.                  purpose register.  */
  5330.               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
  5331.                 {
  5332.                   LONGEST regval = extract_signed_integer (val, partial_len,
  5333.                                                            byte_order);
  5334.                   /* Value may need to be sign extended, because
  5335.                      mips_isa_regsize() != mips_abi_regsize().  */

  5336.                   /* A non-floating-point argument being passed in a
  5337.                      general register.  If a struct or union, and if
  5338.                      the remaining length is smaller than the register
  5339.                      size, we have to adjust the register value on
  5340.                      big endian targets.

  5341.                      It does not seem to be necessary to do the
  5342.                      same for integral types.  */

  5343.                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  5344.                       && partial_len < MIPS64_REGSIZE
  5345.                       && (typecode == TYPE_CODE_STRUCT
  5346.                           || typecode == TYPE_CODE_UNION))
  5347.                     regval <<= ((MIPS64_REGSIZE - partial_len)
  5348.                                 * TARGET_CHAR_BIT);

  5349.                   if (mips_debug)
  5350.                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
  5351.                                       argreg,
  5352.                                       phex (regval, MIPS64_REGSIZE));
  5353.                   regcache_cooked_write_unsigned (regcache, argreg, regval);
  5354.                   argreg++;

  5355.                   /* Prevent subsequent floating point arguments from
  5356.                      being passed in floating point registers.  */
  5357.                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
  5358.                 }

  5359.               len -= partial_len;
  5360.               val += partial_len;

  5361.               /* Compute the offset into the stack at which we will
  5362.                  copy the next parameter.

  5363.                  In older ABIs, the caller reserved space for
  5364.                  registers that contained arguments.  This was loosely
  5365.                  refered to as their "home".  Consequently, space is
  5366.                  always allocated.  */

  5367.               stack_offset += align_up (partial_len, MIPS64_REGSIZE);
  5368.             }
  5369.         }
  5370.       if (mips_debug)
  5371.         fprintf_unfiltered (gdb_stdlog, "\n");
  5372.     }

  5373.   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);

  5374.   /* Return adjusted stack pointer.  */
  5375.   return sp;
  5376. }

  5377. static enum return_value_convention
  5378. mips_o64_return_value (struct gdbarch *gdbarch, struct value *function,
  5379.                        struct type *type, struct regcache *regcache,
  5380.                        gdb_byte *readbuf, const gdb_byte *writebuf)
  5381. {
  5382.   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
  5383.   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
  5384.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  5385.   enum mips_fval_reg fval_reg;

  5386.   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
  5387.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  5388.       || TYPE_CODE (type) == TYPE_CODE_UNION
  5389.       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  5390.     return RETURN_VALUE_STRUCT_CONVENTION;
  5391.   else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
  5392.     {
  5393.       /* A floating-point value.  If reading in or copying, then we get it
  5394.          from/put it to FP0 for standard MIPS code or GPR2 for MIPS16 code.
  5395.          If writing out only, then we put it to both FP0 and GPR2.  We do
  5396.          not support reading in with no function known, if this safety
  5397.          check ever triggers, then we'll have to try harder.  */
  5398.       gdb_assert (function || !readbuf);
  5399.       if (mips_debug)
  5400.         switch (fval_reg)
  5401.           {
  5402.           case mips_fval_fpr:
  5403.             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
  5404.             break;
  5405.           case mips_fval_gpr:
  5406.             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
  5407.             break;
  5408.           case mips_fval_both:
  5409.             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
  5410.             break;
  5411.           }
  5412.       if (fval_reg != mips_fval_gpr)
  5413.         mips_xfer_register (gdbarch, regcache,
  5414.                             (gdbarch_num_regs (gdbarch)
  5415.                              + mips_regnum (gdbarch)->fp0),
  5416.                             TYPE_LENGTH (type),
  5417.                             gdbarch_byte_order (gdbarch),
  5418.                             readbuf, writebuf, 0);
  5419.       if (fval_reg != mips_fval_fpr)
  5420.         mips_xfer_register (gdbarch, regcache,
  5421.                             gdbarch_num_regs (gdbarch) + 2,
  5422.                             TYPE_LENGTH (type),
  5423.                             gdbarch_byte_order (gdbarch),
  5424.                             readbuf, writebuf, 0);
  5425.       return RETURN_VALUE_REGISTER_CONVENTION;
  5426.     }
  5427.   else
  5428.     {
  5429.       /* A scalar extract each part but least-significant-byte
  5430.          justified.  */
  5431.       int offset;
  5432.       int regnum;
  5433.       for (offset = 0, regnum = MIPS_V0_REGNUM;
  5434.            offset < TYPE_LENGTH (type);
  5435.            offset += MIPS64_REGSIZE, regnum++)
  5436.         {
  5437.           int xfer = MIPS64_REGSIZE;
  5438.           if (offset + xfer > TYPE_LENGTH (type))
  5439.             xfer = TYPE_LENGTH (type) - offset;
  5440.           if (mips_debug)
  5441.             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
  5442.                                 offset, xfer, regnum);
  5443.           mips_xfer_register (gdbarch, regcache,
  5444.                               gdbarch_num_regs (gdbarch) + regnum,
  5445.                               xfer, gdbarch_byte_order (gdbarch),
  5446.                               readbuf, writebuf, offset);
  5447.         }
  5448.       return RETURN_VALUE_REGISTER_CONVENTION;
  5449.     }
  5450. }

  5451. /* Floating point register management.

  5452.    Background: MIPS1 & 2 fp registers are 32 bits wide.  To support
  5453.    64bit operations, these early MIPS cpus treat fp register pairs
  5454.    (f0,f1) as a single register (d0).  Later MIPS cpu's have 64 bit fp
  5455.    registers and offer a compatibility mode that emulates the MIPS2 fp
  5456.    model.  When operating in MIPS2 fp compat mode, later cpu's split
  5457.    double precision floats into two 32-bit chunks and store them in
  5458.    consecutive fp regs.  To display 64-bit floats stored in this
  5459.    fashion, we have to combine 32 bits from f0 and 32 bits from f1.
  5460.    Throw in user-configurable endianness and you have a real mess.

  5461.    The way this works is:
  5462.      - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
  5463.        double-precision value will be split across two logical registers.
  5464.        The lower-numbered logical register will hold the low-order bits,
  5465.        regardless of the processor's endianness.
  5466.      - If we are on a 64-bit processor, and we are looking for a
  5467.        single-precision value, it will be in the low ordered bits
  5468.        of a 64-bit GPR (after mfc1, for example) or a 64-bit register
  5469.        save slot in memory.
  5470.      - If we are in 64-bit mode, everything is straightforward.

  5471.    Note that this code only deals with "live" registers at the top of the
  5472.    stack.  We will attempt to deal with saved registers later, when
  5473.    the raw/cooked register interface is in place.  (We need a general
  5474.    interface that can deal with dynamic saved register sizes -- fp
  5475.    regs could be 32 bits wide in one frame and 64 on the frame above
  5476.    and below).  */

  5477. /* Copy a 32-bit single-precision value from the current frame
  5478.    into rare_buffer.  */

  5479. static void
  5480. mips_read_fp_register_single (struct frame_info *frame, int regno,
  5481.                               gdb_byte *rare_buffer)
  5482. {
  5483.   struct gdbarch *gdbarch = get_frame_arch (frame);
  5484.   int raw_size = register_size (gdbarch, regno);
  5485.   gdb_byte *raw_buffer = alloca (raw_size);

  5486.   if (!deprecated_frame_register_read (frame, regno, raw_buffer))
  5487.     error (_("can't read register %d (%s)"),
  5488.            regno, gdbarch_register_name (gdbarch, regno));
  5489.   if (raw_size == 8)
  5490.     {
  5491.       /* We have a 64-bit value for this register.  Find the low-order
  5492.          32 bits.  */
  5493.       int offset;

  5494.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  5495.         offset = 4;
  5496.       else
  5497.         offset = 0;

  5498.       memcpy (rare_buffer, raw_buffer + offset, 4);
  5499.     }
  5500.   else
  5501.     {
  5502.       memcpy (rare_buffer, raw_buffer, 4);
  5503.     }
  5504. }

  5505. /* Copy a 64-bit double-precision value from the current frame into
  5506.    rare_buffer.  This may include getting half of it from the next
  5507.    register.  */

  5508. static void
  5509. mips_read_fp_register_double (struct frame_info *frame, int regno,
  5510.                               gdb_byte *rare_buffer)
  5511. {
  5512.   struct gdbarch *gdbarch = get_frame_arch (frame);
  5513.   int raw_size = register_size (gdbarch, regno);

  5514.   if (raw_size == 8 && !mips2_fp_compat (frame))
  5515.     {
  5516.       /* We have a 64-bit value for this register, and we should use
  5517.          all 64 bits.  */
  5518.       if (!deprecated_frame_register_read (frame, regno, rare_buffer))
  5519.         error (_("can't read register %d (%s)"),
  5520.                regno, gdbarch_register_name (gdbarch, regno));
  5521.     }
  5522.   else
  5523.     {
  5524.       int rawnum = regno % gdbarch_num_regs (gdbarch);

  5525.       if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
  5526.         internal_error (__FILE__, __LINE__,
  5527.                         _("mips_read_fp_register_double: bad access to "
  5528.                         "odd-numbered FP register"));

  5529.       /* mips_read_fp_register_single will find the correct 32 bits from
  5530.          each register.  */
  5531.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  5532.         {
  5533.           mips_read_fp_register_single (frame, regno, rare_buffer + 4);
  5534.           mips_read_fp_register_single (frame, regno + 1, rare_buffer);
  5535.         }
  5536.       else
  5537.         {
  5538.           mips_read_fp_register_single (frame, regno, rare_buffer);
  5539.           mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
  5540.         }
  5541.     }
  5542. }

  5543. static void
  5544. mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
  5545.                         int regnum)
  5546. {                                /* Do values for FP (float) regs.  */
  5547.   struct gdbarch *gdbarch = get_frame_arch (frame);
  5548.   gdb_byte *raw_buffer;
  5549.   double doub, flt1;        /* Doubles extracted from raw hex data.  */
  5550.   int inv1, inv2;

  5551.   raw_buffer = alloca (2 * register_size (gdbarch,
  5552.                                           mips_regnum (gdbarch)->fp0));

  5553.   fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
  5554.   fprintf_filtered (file, "%*s",
  5555.                     4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
  5556.                     "");

  5557.   if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
  5558.     {
  5559.       struct value_print_options opts;

  5560.       /* 4-byte registers: Print hex and floating.  Also print even
  5561.          numbered registers as doubles.  */
  5562.       mips_read_fp_register_single (frame, regnum, raw_buffer);
  5563.       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
  5564.                             raw_buffer, &inv1);

  5565.       get_formatted_print_options (&opts, 'x');
  5566.       print_scalar_formatted (raw_buffer,
  5567.                               builtin_type (gdbarch)->builtin_uint32,
  5568.                               &opts, 'w', file);

  5569.       fprintf_filtered (file, " flt: ");
  5570.       if (inv1)
  5571.         fprintf_filtered (file, " <invalid float> ");
  5572.       else
  5573.         fprintf_filtered (file, "%-17.9g", flt1);

  5574.       if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
  5575.         {
  5576.           mips_read_fp_register_double (frame, regnum, raw_buffer);
  5577.           doub = unpack_double (builtin_type (gdbarch)->builtin_double,
  5578.                                 raw_buffer, &inv2);

  5579.           fprintf_filtered (file, " dbl: ");
  5580.           if (inv2)
  5581.             fprintf_filtered (file, "<invalid double>");
  5582.           else
  5583.             fprintf_filtered (file, "%-24.17g", doub);
  5584.         }
  5585.     }
  5586.   else
  5587.     {
  5588.       struct value_print_options opts;

  5589.       /* Eight byte registers: print each one as hex, float and double.  */
  5590.       mips_read_fp_register_single (frame, regnum, raw_buffer);
  5591.       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
  5592.                             raw_buffer, &inv1);

  5593.       mips_read_fp_register_double (frame, regnum, raw_buffer);
  5594.       doub = unpack_double (builtin_type (gdbarch)->builtin_double,
  5595.                             raw_buffer, &inv2);

  5596.       get_formatted_print_options (&opts, 'x');
  5597.       print_scalar_formatted (raw_buffer,
  5598.                               builtin_type (gdbarch)->builtin_uint64,
  5599.                               &opts, 'g', file);

  5600.       fprintf_filtered (file, " flt: ");
  5601.       if (inv1)
  5602.         fprintf_filtered (file, "<invalid float>");
  5603.       else
  5604.         fprintf_filtered (file, "%-17.9g", flt1);

  5605.       fprintf_filtered (file, " dbl: ");
  5606.       if (inv2)
  5607.         fprintf_filtered (file, "<invalid double>");
  5608.       else
  5609.         fprintf_filtered (file, "%-24.17g", doub);
  5610.     }
  5611. }

  5612. static void
  5613. mips_print_register (struct ui_file *file, struct frame_info *frame,
  5614.                      int regnum)
  5615. {
  5616.   struct gdbarch *gdbarch = get_frame_arch (frame);
  5617.   struct value_print_options opts;
  5618.   struct value *val;

  5619.   if (mips_float_register_p (gdbarch, regnum))
  5620.     {
  5621.       mips_print_fp_register (file, frame, regnum);
  5622.       return;
  5623.     }

  5624.   val = get_frame_register_value (frame, regnum);

  5625.   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);

  5626.   /* The problem with printing numeric register names (r26, etc.) is that
  5627.      the user can't use them on input.  Probably the best solution is to
  5628.      fix it so that either the numeric or the funky (a2, etc.) names
  5629.      are accepted on input.  */
  5630.   if (regnum < MIPS_NUMREGS)
  5631.     fprintf_filtered (file, "(r%d): ", regnum);
  5632.   else
  5633.     fprintf_filtered (file, ": ");

  5634.   get_formatted_print_options (&opts, 'x');
  5635.   val_print_scalar_formatted (value_type (val),
  5636.                               value_contents_for_printing (val),
  5637.                               value_embedded_offset (val),
  5638.                               val,
  5639.                               &opts, 0, file);
  5640. }

  5641. /* Print IEEE exception condition bits in FLAGS.  */

  5642. static void
  5643. print_fpu_flags (struct ui_file *file, int flags)
  5644. {
  5645.   if (flags & (1 << 0))
  5646.     fputs_filtered (" inexact", file);
  5647.   if (flags & (1 << 1))
  5648.     fputs_filtered (" uflow", file);
  5649.   if (flags & (1 << 2))
  5650.     fputs_filtered (" oflow", file);
  5651.   if (flags & (1 << 3))
  5652.     fputs_filtered (" div0", file);
  5653.   if (flags & (1 << 4))
  5654.     fputs_filtered (" inval", file);
  5655.   if (flags & (1 << 5))
  5656.     fputs_filtered (" unimp", file);
  5657.   fputc_filtered ('\n', file);
  5658. }

  5659. /* Print interesting information about the floating point processor
  5660.    (if present) or emulator.  */

  5661. static void
  5662. mips_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
  5663.                       struct frame_info *frame, const char *args)
  5664. {
  5665.   int fcsr = mips_regnum (gdbarch)->fp_control_status;
  5666.   enum mips_fpu_type type = MIPS_FPU_TYPE (gdbarch);
  5667.   ULONGEST fcs = 0;
  5668.   int i;

  5669.   if (fcsr == -1 || !read_frame_register_unsigned (frame, fcsr, &fcs))
  5670.     type = MIPS_FPU_NONE;

  5671.   fprintf_filtered (file, "fpu type: %s\n",
  5672.                     type == MIPS_FPU_DOUBLE ? "double-precision"
  5673.                     : type == MIPS_FPU_SINGLE ? "single-precision"
  5674.                     : "none / unused");

  5675.   if (type == MIPS_FPU_NONE)
  5676.     return;

  5677.   fprintf_filtered (file, "reg size: %d bits\n",
  5678.                     register_size (gdbarch, mips_regnum (gdbarch)->fp0) * 8);

  5679.   fputs_filtered ("cond    :", file);
  5680.   if (fcs & (1 << 23))
  5681.     fputs_filtered (" 0", file);
  5682.   for (i = 1; i <= 7; i++)
  5683.     if (fcs & (1 << (24 + i)))
  5684.       fprintf_filtered (file, " %d", i);
  5685.   fputc_filtered ('\n', file);

  5686.   fputs_filtered ("cause   :", file);
  5687.   print_fpu_flags (file, (fcs >> 12) & 0x3f);
  5688.   fputs ("mask    :", stdout);
  5689.   print_fpu_flags (file, (fcs >> 7) & 0x1f);
  5690.   fputs ("flags   :", stdout);
  5691.   print_fpu_flags (file, (fcs >> 2) & 0x1f);

  5692.   fputs_filtered ("rounding: ", file);
  5693.   switch (fcs & 3)
  5694.     {
  5695.     case 0: fputs_filtered ("nearest\n", file); break;
  5696.     case 1: fputs_filtered ("zero\n", file); break;
  5697.     case 2: fputs_filtered ("+inf\n", file); break;
  5698.     case 3: fputs_filtered ("-inf\n", file); break;
  5699.     }

  5700.   fputs_filtered ("flush   :", file);
  5701.   if (fcs & (1 << 21))
  5702.     fputs_filtered (" nearest", file);
  5703.   if (fcs & (1 << 22))
  5704.     fputs_filtered (" override", file);
  5705.   if (fcs & (1 << 24))
  5706.     fputs_filtered (" zero", file);
  5707.   if ((fcs & (0xb << 21)) == 0)
  5708.     fputs_filtered (" no", file);
  5709.   fputc_filtered ('\n', file);

  5710.   fprintf_filtered (file, "nan2008 : %s\n", fcs & (1 << 18) ? "yes" : "no");
  5711.   fprintf_filtered (file, "abs2008 : %s\n", fcs & (1 << 19) ? "yes" : "no");
  5712.   fputc_filtered ('\n', file);

  5713.   default_print_float_info (gdbarch, file, frame, args);
  5714. }

  5715. /* Replacement for generic do_registers_info.
  5716.    Print regs in pretty columns.  */

  5717. static int
  5718. print_fp_register_row (struct ui_file *file, struct frame_info *frame,
  5719.                        int regnum)
  5720. {
  5721.   fprintf_filtered (file, " ");
  5722.   mips_print_fp_register (file, frame, regnum);
  5723.   fprintf_filtered (file, "\n");
  5724.   return regnum + 1;
  5725. }


  5726. /* Print a row's worth of GP (int) registers, with name labels above.  */

  5727. static int
  5728. print_gp_register_row (struct ui_file *file, struct frame_info *frame,
  5729.                        int start_regnum)
  5730. {
  5731.   struct gdbarch *gdbarch = get_frame_arch (frame);
  5732.   /* Do values for GP (int) regs.  */
  5733.   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
  5734.   int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8);    /* display cols
  5735.                                                                per row.  */
  5736.   int col, byte;
  5737.   int regnum;

  5738.   /* For GP registers, we print a separate row of names above the vals.  */
  5739.   for (col = 0, regnum = start_regnum;
  5740.        col < ncols && regnum < gdbarch_num_regs (gdbarch)
  5741.                                + gdbarch_num_pseudo_regs (gdbarch);
  5742.        regnum++)
  5743.     {
  5744.       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
  5745.         continue;                /* unused register */
  5746.       if (mips_float_register_p (gdbarch, regnum))
  5747.         break;                        /* End the row: reached FP register.  */
  5748.       /* Large registers are handled separately.  */
  5749.       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
  5750.         {
  5751.           if (col > 0)
  5752.             break;                /* End the row before this register.  */

  5753.           /* Print this register on a row by itself.  */
  5754.           mips_print_register (file, frame, regnum);
  5755.           fprintf_filtered (file, "\n");
  5756.           return regnum + 1;
  5757.         }
  5758.       if (col == 0)
  5759.         fprintf_filtered (file, "     ");
  5760.       fprintf_filtered (file,
  5761.                         mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
  5762.                         gdbarch_register_name (gdbarch, regnum));
  5763.       col++;
  5764.     }

  5765.   if (col == 0)
  5766.     return regnum;

  5767.   /* Print the R0 to R31 names.  */
  5768.   if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
  5769.     fprintf_filtered (file, "\n R%-4d",
  5770.                       start_regnum % gdbarch_num_regs (gdbarch));
  5771.   else
  5772.     fprintf_filtered (file, "\n      ");

  5773.   /* Now print the values in hex, 4 or 8 to the row.  */
  5774.   for (col = 0, regnum = start_regnum;
  5775.        col < ncols && regnum < gdbarch_num_regs (gdbarch)
  5776.                                + gdbarch_num_pseudo_regs (gdbarch);
  5777.        regnum++)
  5778.     {
  5779.       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
  5780.         continue;                /* unused register */
  5781.       if (mips_float_register_p (gdbarch, regnum))
  5782.         break;                        /* End row: reached FP register.  */
  5783.       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
  5784.         break;                        /* End row: large register.  */

  5785.       /* OK: get the data in raw format.  */
  5786.       if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
  5787.         error (_("can't read register %d (%s)"),
  5788.                regnum, gdbarch_register_name (gdbarch, regnum));
  5789.       /* pad small registers */
  5790.       for (byte = 0;
  5791.            byte < (mips_abi_regsize (gdbarch)
  5792.                    - register_size (gdbarch, regnum)); byte++)
  5793.         printf_filtered ("  ");
  5794.       /* Now print the register value in hex, endian order.  */
  5795.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  5796.         for (byte =
  5797.              register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
  5798.              byte < register_size (gdbarch, regnum); byte++)
  5799.           fprintf_filtered (file, "%02x", raw_buffer[byte]);
  5800.       else
  5801.         for (byte = register_size (gdbarch, regnum) - 1;
  5802.              byte >= 0; byte--)
  5803.           fprintf_filtered (file, "%02x", raw_buffer[byte]);
  5804.       fprintf_filtered (file, " ");
  5805.       col++;
  5806.     }
  5807.   if (col > 0)                        /* ie. if we actually printed anything...  */
  5808.     fprintf_filtered (file, "\n");

  5809.   return regnum;
  5810. }

  5811. /* MIPS_DO_REGISTERS_INFO(): called by "info register" command.  */

  5812. static void
  5813. mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  5814.                            struct frame_info *frame, int regnum, int all)
  5815. {
  5816.   if (regnum != -1)                /* Do one specified register.  */
  5817.     {
  5818.       gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
  5819.       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
  5820.         error (_("Not a valid register for the current processor type"));

  5821.       mips_print_register (file, frame, regnum);
  5822.       fprintf_filtered (file, "\n");
  5823.     }
  5824.   else
  5825.     /* Do all (or most) registers.  */
  5826.     {
  5827.       regnum = gdbarch_num_regs (gdbarch);
  5828.       while (regnum < gdbarch_num_regs (gdbarch)
  5829.                       + gdbarch_num_pseudo_regs (gdbarch))
  5830.         {
  5831.           if (mips_float_register_p (gdbarch, regnum))
  5832.             {
  5833.               if (all)                /* True for "INFO ALL-REGISTERS" command.  */
  5834.                 regnum = print_fp_register_row (file, frame, regnum);
  5835.               else
  5836.                 regnum += MIPS_NUMREGS;        /* Skip floating point regs.  */
  5837.             }
  5838.           else
  5839.             regnum = print_gp_register_row (file, frame, regnum);
  5840.         }
  5841.     }
  5842. }

  5843. static int
  5844. mips_single_step_through_delay (struct gdbarch *gdbarch,
  5845.                                 struct frame_info *frame)
  5846. {
  5847.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  5848.   CORE_ADDR pc = get_frame_pc (frame);
  5849.   struct address_space *aspace;
  5850.   enum mips_isa isa;
  5851.   ULONGEST insn;
  5852.   int status;
  5853.   int size;

  5854.   if ((mips_pc_is_mips (pc)
  5855.        && !mips32_insn_at_pc_has_delay_slot (gdbarch, pc))
  5856.       || (mips_pc_is_micromips (gdbarch, pc)
  5857.           && !micromips_insn_at_pc_has_delay_slot (gdbarch, pc, 0))
  5858.       || (mips_pc_is_mips16 (gdbarch, pc)
  5859.           && !mips16_insn_at_pc_has_delay_slot (gdbarch, pc, 0)))
  5860.     return 0;

  5861.   isa = mips_pc_isa (gdbarch, pc);
  5862.   /* _has_delay_slot above will have validated the read.  */
  5863.   insn = mips_fetch_instruction (gdbarch, isa, pc, NULL);
  5864.   size = mips_insn_size (isa, insn);
  5865.   aspace = get_frame_address_space (frame);
  5866.   return breakpoint_here_p (aspace, pc + size) != no_breakpoint_here;
  5867. }

  5868. /* To skip prologues, I use this predicate.  Returns either PC itself
  5869.    if the code at PC does not look like a function prologue; otherwise
  5870.    returns an address that (if we're lucky) follows the prologue.  If
  5871.    LENIENT, then we must skip everything which is involved in setting
  5872.    up the frame (it's OK to skip more, just so long as we don't skip
  5873.    anything which might clobber the registers which are being saved.
  5874.    We must skip more in the case where part of the prologue is in the
  5875.    delay slot of a non-prologue instruction).  */

  5876. static CORE_ADDR
  5877. mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  5878. {
  5879.   CORE_ADDR limit_pc;
  5880.   CORE_ADDR func_addr;

  5881.   /* See if we can determine the end of the prologue via the symbol table.
  5882.      If so, then return either PC, or the PC after the prologue, whichever
  5883.      is greater.  */
  5884.   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  5885.     {
  5886.       CORE_ADDR post_prologue_pc
  5887.         = skip_prologue_using_sal (gdbarch, func_addr);
  5888.       if (post_prologue_pc != 0)
  5889.         return max (pc, post_prologue_pc);
  5890.     }

  5891.   /* Can't determine prologue from the symbol table, need to examine
  5892.      instructions.  */

  5893.   /* Find an upper limit on the function prologue using the debug
  5894.      information.  If the debug information could not be used to provide
  5895.      that bound, then use an arbitrary large number as the upper bound.  */
  5896.   limit_pc = skip_prologue_using_sal (gdbarch, pc);
  5897.   if (limit_pc == 0)
  5898.     limit_pc = pc + 100;          /* Magic.  */

  5899.   if (mips_pc_is_mips16 (gdbarch, pc))
  5900.     return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
  5901.   else if (mips_pc_is_micromips (gdbarch, pc))
  5902.     return micromips_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
  5903.   else
  5904.     return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
  5905. }

  5906. /* Check whether the PC is in a function epilogue (32-bit version).
  5907.    This is a helper function for mips_in_function_epilogue_p.  */
  5908. static int
  5909. mips32_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  5910. {
  5911.   CORE_ADDR func_addr = 0, func_end = 0;

  5912.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  5913.     {
  5914.       /* The MIPS epilogue is max. 12 bytes long.  */
  5915.       CORE_ADDR addr = func_end - 12;

  5916.       if (addr < func_addr + 4)
  5917.         addr = func_addr + 4;
  5918.       if (pc < addr)
  5919.         return 0;

  5920.       for (; pc < func_end; pc += MIPS_INSN32_SIZE)
  5921.         {
  5922.           unsigned long high_word;
  5923.           unsigned long inst;

  5924.           inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
  5925.           high_word = (inst >> 16) & 0xffff;

  5926.           if (high_word != 0x27bd        /* addiu $sp,$sp,offset */
  5927.               && high_word != 0x67bd        /* daddiu $sp,$sp,offset */
  5928.               && inst != 0x03e00008        /* jr $ra */
  5929.               && inst != 0x00000000)        /* nop */
  5930.             return 0;
  5931.         }

  5932.       return 1;
  5933.     }

  5934.   return 0;
  5935. }

  5936. /* Check whether the PC is in a function epilogue (microMIPS version).
  5937.    This is a helper function for mips_in_function_epilogue_p.  */

  5938. static int
  5939. micromips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  5940. {
  5941.   CORE_ADDR func_addr = 0;
  5942.   CORE_ADDR func_end = 0;
  5943.   CORE_ADDR addr;
  5944.   ULONGEST insn;
  5945.   long offset;
  5946.   int dreg;
  5947.   int sreg;
  5948.   int loc;

  5949.   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  5950.     return 0;

  5951.   /* The microMIPS epilogue is max. 12 bytes long.  */
  5952.   addr = func_end - 12;

  5953.   if (addr < func_addr + 2)
  5954.     addr = func_addr + 2;
  5955.   if (pc < addr)
  5956.     return 0;

  5957.   for (; pc < func_end; pc += loc)
  5958.     {
  5959.       loc = 0;
  5960.       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
  5961.       loc += MIPS_INSN16_SIZE;
  5962.       switch (mips_insn_size (ISA_MICROMIPS, insn))
  5963.         {
  5964.         /* 48-bit instructions.  */
  5965.         case 3 * MIPS_INSN16_SIZE:
  5966.           /* No epilogue instructions in this category.  */
  5967.           return 0;

  5968.         /* 32-bit instructions.  */
  5969.         case 2 * MIPS_INSN16_SIZE:
  5970.           insn <<= 16;
  5971.           insn |= mips_fetch_instruction (gdbarch,
  5972.                                           ISA_MICROMIPS, pc + loc, NULL);
  5973.           loc += MIPS_INSN16_SIZE;
  5974.           switch (micromips_op (insn >> 16))
  5975.             {
  5976.             case 0xc: /* ADDIU: bits 001100 */
  5977.             case 0x17: /* DADDIU: bits 010111 */
  5978.               sreg = b0s5_reg (insn >> 16);
  5979.               dreg = b5s5_reg (insn >> 16);
  5980.               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
  5981.               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
  5982.                             /* (D)ADDIU $sp, imm */
  5983.                   && offset >= 0)
  5984.                 break;
  5985.               return 0;

  5986.             default:
  5987.               return 0;
  5988.             }
  5989.           break;

  5990.         /* 16-bit instructions.  */
  5991.         case MIPS_INSN16_SIZE:
  5992.           switch (micromips_op (insn))
  5993.             {
  5994.             case 0x3: /* MOVE: bits 000011 */
  5995.               sreg = b0s5_reg (insn);
  5996.               dreg = b5s5_reg (insn);
  5997.               if (sreg == 0 && dreg == 0)
  5998.                                 /* MOVE $zero, $zero aka NOP */
  5999.                 break;
  6000.               return 0;

  6001.             case 0x11: /* POOL16C: bits 010001 */
  6002.               if (b5s5_op (insn) == 0x18
  6003.                                 /* JRADDIUSP: bits 010011 11000 */
  6004.                   || (b5s5_op (insn) == 0xd
  6005.                                 /* JRC: bits 010011 01101 */
  6006.                       && b0s5_reg (insn) == MIPS_RA_REGNUM))
  6007.                                 /* JRC $ra */
  6008.                 break;
  6009.               return 0;

  6010.             case 0x13: /* POOL16D: bits 010011 */
  6011.               offset = micromips_decode_imm9 (b1s9_imm (insn));
  6012.               if ((insn & 0x1) == 0x1
  6013.                                 /* ADDIUSP: bits 010011 1 */
  6014.                   && offset > 0)
  6015.                 break;
  6016.               return 0;

  6017.             default:
  6018.               return 0;
  6019.             }
  6020.         }
  6021.     }

  6022.   return 1;
  6023. }

  6024. /* Check whether the PC is in a function epilogue (16-bit version).
  6025.    This is a helper function for mips_in_function_epilogue_p.  */
  6026. static int
  6027. mips16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  6028. {
  6029.   CORE_ADDR func_addr = 0, func_end = 0;

  6030.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  6031.     {
  6032.       /* The MIPS epilogue is max. 12 bytes long.  */
  6033.       CORE_ADDR addr = func_end - 12;

  6034.       if (addr < func_addr + 4)
  6035.         addr = func_addr + 4;
  6036.       if (pc < addr)
  6037.         return 0;

  6038.       for (; pc < func_end; pc += MIPS_INSN16_SIZE)
  6039.         {
  6040.           unsigned short inst;

  6041.           inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc, NULL);

  6042.           if ((inst & 0xf800) == 0xf000)        /* extend */
  6043.             continue;

  6044.           if (inst != 0x6300                /* addiu $sp,offset */
  6045.               && inst != 0xfb00                /* daddiu $sp,$sp,offset */
  6046.               && inst != 0xe820                /* jr $ra */
  6047.               && inst != 0xe8a0                /* jrc $ra */
  6048.               && inst != 0x6500)        /* nop */
  6049.             return 0;
  6050.         }

  6051.       return 1;
  6052.     }

  6053.   return 0;
  6054. }

  6055. /* The epilogue is defined here as the area at the end of a function,
  6056.    after an instruction which destroys the function's stack frame.  */
  6057. static int
  6058. mips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  6059. {
  6060.   if (mips_pc_is_mips16 (gdbarch, pc))
  6061.     return mips16_in_function_epilogue_p (gdbarch, pc);
  6062.   else if (mips_pc_is_micromips (gdbarch, pc))
  6063.     return micromips_in_function_epilogue_p (gdbarch, pc);
  6064.   else
  6065.     return mips32_in_function_epilogue_p (gdbarch, pc);
  6066. }

  6067. /* Root of all "set mips "/"show mips " commands.  This will eventually be
  6068.    used for all MIPS-specific commands.  */

  6069. static void
  6070. show_mips_command (char *args, int from_tty)
  6071. {
  6072.   help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
  6073. }

  6074. static void
  6075. set_mips_command (char *args, int from_tty)
  6076. {
  6077.   printf_unfiltered
  6078.     ("\"set mips\" must be followed by an appropriate subcommand.\n");
  6079.   help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
  6080. }

  6081. /* Commands to show/set the MIPS FPU type.  */

  6082. static void
  6083. show_mipsfpu_command (char *args, int from_tty)
  6084. {
  6085.   char *fpu;

  6086.   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
  6087.     {
  6088.       printf_unfiltered
  6089.         ("The MIPS floating-point coprocessor is unknown "
  6090.          "because the current architecture is not MIPS.\n");
  6091.       return;
  6092.     }

  6093.   switch (MIPS_FPU_TYPE (target_gdbarch ()))
  6094.     {
  6095.     case MIPS_FPU_SINGLE:
  6096.       fpu = "single-precision";
  6097.       break;
  6098.     case MIPS_FPU_DOUBLE:
  6099.       fpu = "double-precision";
  6100.       break;
  6101.     case MIPS_FPU_NONE:
  6102.       fpu = "absent (none)";
  6103.       break;
  6104.     default:
  6105.       internal_error (__FILE__, __LINE__, _("bad switch"));
  6106.     }
  6107.   if (mips_fpu_type_auto)
  6108.     printf_unfiltered ("The MIPS floating-point coprocessor "
  6109.                        "is set automatically (currently %s)\n",
  6110.                        fpu);
  6111.   else
  6112.     printf_unfiltered
  6113.       ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
  6114. }


  6115. static void
  6116. set_mipsfpu_command (char *args, int from_tty)
  6117. {
  6118.   printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
  6119.                      "\"single\",\"none\" or \"auto\".\n");
  6120.   show_mipsfpu_command (args, from_tty);
  6121. }

  6122. static void
  6123. set_mipsfpu_single_command (char *args, int from_tty)
  6124. {
  6125.   struct gdbarch_info info;
  6126.   gdbarch_info_init (&info);
  6127.   mips_fpu_type = MIPS_FPU_SINGLE;
  6128.   mips_fpu_type_auto = 0;
  6129.   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
  6130.      instead of relying on globals.  Doing that would let generic code
  6131.      handle the search for this specific architecture.  */
  6132.   if (!gdbarch_update_p (info))
  6133.     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
  6134. }

  6135. static void
  6136. set_mipsfpu_double_command (char *args, int from_tty)
  6137. {
  6138.   struct gdbarch_info info;
  6139.   gdbarch_info_init (&info);
  6140.   mips_fpu_type = MIPS_FPU_DOUBLE;
  6141.   mips_fpu_type_auto = 0;
  6142.   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
  6143.      instead of relying on globals.  Doing that would let generic code
  6144.      handle the search for this specific architecture.  */
  6145.   if (!gdbarch_update_p (info))
  6146.     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
  6147. }

  6148. static void
  6149. set_mipsfpu_none_command (char *args, int from_tty)
  6150. {
  6151.   struct gdbarch_info info;
  6152.   gdbarch_info_init (&info);
  6153.   mips_fpu_type = MIPS_FPU_NONE;
  6154.   mips_fpu_type_auto = 0;
  6155.   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
  6156.      instead of relying on globals.  Doing that would let generic code
  6157.      handle the search for this specific architecture.  */
  6158.   if (!gdbarch_update_p (info))
  6159.     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
  6160. }

  6161. static void
  6162. set_mipsfpu_auto_command (char *args, int from_tty)
  6163. {
  6164.   mips_fpu_type_auto = 1;
  6165. }

  6166. /* Attempt to identify the particular processor model by reading the
  6167.    processor id.  NOTE: cagney/2003-11-15: Firstly it isn't clear that
  6168.    the relevant processor still exists (it dates back to '94) and
  6169.    secondly this is not the way to do this.  The processor type should
  6170.    be set by forcing an architecture change.  */

  6171. void
  6172. deprecated_mips_set_processor_regs_hack (void)
  6173. {
  6174.   struct regcache *regcache = get_current_regcache ();
  6175.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  6176.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  6177.   ULONGEST prid;

  6178.   regcache_cooked_read_unsigned (regcache, MIPS_PRID_REGNUM, &prid);
  6179.   if ((prid & ~0xf) == 0x700)
  6180.     tdep->mips_processor_reg_names = mips_r3041_reg_names;
  6181. }

  6182. /* Just like reinit_frame_cache, but with the right arguments to be
  6183.    callable as an sfunc.  */

  6184. static void
  6185. reinit_frame_cache_sfunc (char *args, int from_tty,
  6186.                           struct cmd_list_element *c)
  6187. {
  6188.   reinit_frame_cache ();
  6189. }

  6190. static int
  6191. gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
  6192. {
  6193.   struct gdbarch *gdbarch = info->application_data;

  6194.   /* FIXME: cagney/2003-06-26: Is this even necessary?  The
  6195.      disassembler needs to be able to locally determine the ISA, and
  6196.      not rely on GDB.  Otherwize the stand-alone 'objdump -d' will not
  6197.      work.  */
  6198.   if (mips_pc_is_mips16 (gdbarch, memaddr))
  6199.     info->mach = bfd_mach_mips16;
  6200.   else if (mips_pc_is_micromips (gdbarch, memaddr))
  6201.     info->mach = bfd_mach_mips_micromips;

  6202.   /* Round down the instruction address to the appropriate boundary.  */
  6203.   memaddr &= (info->mach == bfd_mach_mips16
  6204.               || info->mach == bfd_mach_mips_micromips) ? ~1 : ~3;

  6205.   /* Set the disassembler options.  */
  6206.   if (!info->disassembler_options)
  6207.     /* This string is not recognized explicitly by the disassembler,
  6208.        but it tells the disassembler to not try to guess the ABI from
  6209.        the bfd elf headers, such that, if the user overrides the ABI
  6210.        of a program linked as NewABI, the disassembly will follow the
  6211.        register naming conventions specified by the user.  */
  6212.     info->disassembler_options = "gpr-names=32";

  6213.   /* Call the appropriate disassembler based on the target endian-ness.  */
  6214.   if (info->endian == BFD_ENDIAN_BIG)
  6215.     return print_insn_big_mips (memaddr, info);
  6216.   else
  6217.     return print_insn_little_mips (memaddr, info);
  6218. }

  6219. static int
  6220. gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
  6221. {
  6222.   /* Set up the disassembler info, so that we get the right
  6223.      register names from libopcodes.  */
  6224.   info->disassembler_options = "gpr-names=n32";
  6225.   info->flavour = bfd_target_elf_flavour;

  6226.   return gdb_print_insn_mips (memaddr, info);
  6227. }

  6228. static int
  6229. gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
  6230. {
  6231.   /* Set up the disassembler info, so that we get the right
  6232.      register names from libopcodes.  */
  6233.   info->disassembler_options = "gpr-names=64";
  6234.   info->flavour = bfd_target_elf_flavour;

  6235.   return gdb_print_insn_mips (memaddr, info);
  6236. }

  6237. /* This function implements gdbarch_breakpoint_from_pc.  It uses the
  6238.    program counter value to determine whether a 16- or 32-bit breakpoint
  6239.    should be used.  It returns a pointer to a string of bytes that encode a
  6240.    breakpoint instruction, stores the length of the string to *lenptr, and
  6241.    adjusts pc (if necessary) to point to the actual memory location where
  6242.    the breakpoint should be inserted.  */

  6243. static const gdb_byte *
  6244. mips_breakpoint_from_pc (struct gdbarch *gdbarch,
  6245.                          CORE_ADDR *pcptr, int *lenptr)
  6246. {
  6247.   CORE_ADDR pc = *pcptr;

  6248.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  6249.     {
  6250.       if (mips_pc_is_mips16 (gdbarch, pc))
  6251.         {
  6252.           static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
  6253.           *pcptr = unmake_compact_addr (pc);
  6254.           *lenptr = sizeof (mips16_big_breakpoint);
  6255.           return mips16_big_breakpoint;
  6256.         }
  6257.       else if (mips_pc_is_micromips (gdbarch, pc))
  6258.         {
  6259.           static gdb_byte micromips16_big_breakpoint[] = { 0x46, 0x85 };
  6260.           static gdb_byte micromips32_big_breakpoint[] = { 0, 0x5, 0, 0x7 };
  6261.           ULONGEST insn;
  6262.           int status;
  6263.           int size;

  6264.           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
  6265.           size = status ? 2
  6266.                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
  6267.           *pcptr = unmake_compact_addr (pc);
  6268.           *lenptr = size;
  6269.           return (size == 2) ? micromips16_big_breakpoint
  6270.                              : micromips32_big_breakpoint;
  6271.         }
  6272.       else
  6273.         {
  6274.           /* The IDT board uses an unusual breakpoint value, and
  6275.              sometimes gets confused when it sees the usual MIPS
  6276.              breakpoint instruction.  */
  6277.           static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
  6278.           static gdb_byte pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
  6279.           static gdb_byte idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
  6280.           /* Likewise, IRIX appears to expect a different breakpoint,
  6281.              although this is not apparent until you try to use pthreads.  */
  6282.           static gdb_byte irix_big_breakpoint[] = { 0, 0, 0, 0xd };

  6283.           *lenptr = sizeof (big_breakpoint);

  6284.           if (strcmp (target_shortname, "mips") == 0)
  6285.             return idt_big_breakpoint;
  6286.           else if (strcmp (target_shortname, "ddb") == 0
  6287.                    || strcmp (target_shortname, "pmon") == 0
  6288.                    || strcmp (target_shortname, "lsi") == 0)
  6289.             return pmon_big_breakpoint;
  6290.           else if (gdbarch_osabi (gdbarch) == GDB_OSABI_IRIX)
  6291.             return irix_big_breakpoint;
  6292.           else
  6293.             return big_breakpoint;
  6294.         }
  6295.     }
  6296.   else
  6297.     {
  6298.       if (mips_pc_is_mips16 (gdbarch, pc))
  6299.         {
  6300.           static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
  6301.           *pcptr = unmake_compact_addr (pc);
  6302.           *lenptr = sizeof (mips16_little_breakpoint);
  6303.           return mips16_little_breakpoint;
  6304.         }
  6305.       else if (mips_pc_is_micromips (gdbarch, pc))
  6306.         {
  6307.           static gdb_byte micromips16_little_breakpoint[] = { 0x85, 0x46 };
  6308.           static gdb_byte micromips32_little_breakpoint[] = { 0x5, 0, 0x7, 0 };
  6309.           ULONGEST insn;
  6310.           int status;
  6311.           int size;

  6312.           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
  6313.           size = status ? 2
  6314.                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
  6315.           *pcptr = unmake_compact_addr (pc);
  6316.           *lenptr = size;
  6317.           return (size == 2) ? micromips16_little_breakpoint
  6318.                              : micromips32_little_breakpoint;
  6319.         }
  6320.       else
  6321.         {
  6322.           static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
  6323.           static gdb_byte pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
  6324.           static gdb_byte idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };

  6325.           *lenptr = sizeof (little_breakpoint);

  6326.           if (strcmp (target_shortname, "mips") == 0)
  6327.             return idt_little_breakpoint;
  6328.           else if (strcmp (target_shortname, "ddb") == 0
  6329.                    || strcmp (target_shortname, "pmon") == 0
  6330.                    || strcmp (target_shortname, "lsi") == 0)
  6331.             return pmon_little_breakpoint;
  6332.           else
  6333.             return little_breakpoint;
  6334.         }
  6335.     }
  6336. }

  6337. /* Determine the remote breakpoint kind suitable for the PC.  The following
  6338.    kinds are used:

  6339.    * 2 -- 16-bit MIPS16 mode breakpoint,

  6340.    * 3 -- 16-bit microMIPS mode breakpoint,

  6341.    * 4 -- 32-bit standard MIPS mode breakpoint,

  6342.    * 5 -- 32-bit microMIPS mode breakpoint.  */

  6343. static void
  6344. mips_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  6345.                                 int *kindptr)
  6346. {
  6347.   CORE_ADDR pc = *pcptr;

  6348.   if (mips_pc_is_mips16 (gdbarch, pc))
  6349.     {
  6350.       *pcptr = unmake_compact_addr (pc);
  6351.       *kindptr = 2;
  6352.     }
  6353.   else if (mips_pc_is_micromips (gdbarch, pc))
  6354.     {
  6355.       ULONGEST insn;
  6356.       int status;
  6357.       int size;

  6358.       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
  6359.       size = status ? 2 : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
  6360.       *pcptr = unmake_compact_addr (pc);
  6361.       *kindptr = size | 1;
  6362.     }
  6363.   else
  6364.     *kindptr = 4;
  6365. }

  6366. /* Return non-zero if the standard MIPS instruction INST has a branch
  6367.    delay slot (i.e. it is a jump or branch instruction).  This function
  6368.    is based on mips32_next_pc.  */

  6369. static int
  6370. mips32_instruction_has_delay_slot (struct gdbarch *gdbarch, ULONGEST inst)
  6371. {
  6372.   int op;
  6373.   int rs;
  6374.   int rt;

  6375.   op = itype_op (inst);
  6376.   if ((inst & 0xe0000000) != 0)
  6377.     {
  6378.       rs = itype_rs (inst);
  6379.       rt = itype_rt (inst);
  6380.       return (is_octeon_bbit_op (op, gdbarch)
  6381.               || op >> 2 == 5        /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx  */
  6382.               || op == 29        /* JALX: bits 011101  */
  6383.               || (op == 17
  6384.                   && (rs == 8
  6385.                                 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000  */
  6386.                       || (rs == 9 && (rt & 0x2) == 0)
  6387.                                 /* BC1ANY2F, BC1ANY2T: bits 010001 01001  */
  6388.                       || (rs == 10 && (rt & 0x2) == 0))));
  6389.                                 /* BC1ANY4F, BC1ANY4T: bits 010001 01010  */
  6390.     }
  6391.   else
  6392.     switch (op & 0x07)                /* extract bits 28,27,26  */
  6393.       {
  6394.       case 0:                        /* SPECIAL  */
  6395.         op = rtype_funct (inst);
  6396.         return (op == 8                /* JR  */
  6397.                 || op == 9);        /* JALR  */
  6398.         break;                        /* end SPECIAL  */
  6399.       case 1:                        /* REGIMM  */
  6400.         rs = itype_rs (inst);
  6401.         rt = itype_rt (inst);        /* branch condition  */
  6402.         return ((rt & 0xc) == 0
  6403.                                 /* BLTZ, BLTZL, BGEZ, BGEZL: bits 000xx  */
  6404.                                 /* BLTZAL, BLTZALL, BGEZAL, BGEZALL: 100xx  */
  6405.                 || ((rt & 0x1e) == 0x1c && rs == 0));
  6406.                                 /* BPOSGE32, BPOSGE64: bits 1110x  */
  6407.         break;                        /* end REGIMM  */
  6408.       default:                        /* J, JAL, BEQ, BNE, BLEZ, BGTZ  */
  6409.         return 1;
  6410.         break;
  6411.       }
  6412. }

  6413. /* Return non-zero if a standard MIPS instruction at ADDR has a branch
  6414.    delay slot (i.e. it is a jump or branch instruction).  */

  6415. static int
  6416. mips32_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr)
  6417. {
  6418.   ULONGEST insn;
  6419.   int status;

  6420.   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, addr, &status);
  6421.   if (status)
  6422.     return 0;

  6423.   return mips32_instruction_has_delay_slot (gdbarch, insn);
  6424. }

  6425. /* Return non-zero if the microMIPS instruction INSN, comprising the
  6426.    16-bit major opcode word in the high 16 bits and any second word
  6427.    in the low 16 bits, has a branch delay slot (i.e. it is a non-compact
  6428.    jump or branch instruction).  The instruction must be 32-bit if
  6429.    MUSTBE32 is set or can be any instruction otherwise.  */

  6430. static int
  6431. micromips_instruction_has_delay_slot (ULONGEST insn, int mustbe32)
  6432. {
  6433.   ULONGEST major = insn >> 16;

  6434.   switch (micromips_op (major))
  6435.     {
  6436.     /* 16-bit instructions.  */
  6437.     case 0x33:                        /* B16: bits 110011 */
  6438.     case 0x2b:                        /* BNEZ16: bits 101011 */
  6439.     case 0x23:                        /* BEQZ16: bits 100011 */
  6440.       return !mustbe32;
  6441.     case 0x11:                        /* POOL16C: bits 010001 */
  6442.       return (!mustbe32
  6443.               && ((b5s5_op (major) == 0xc
  6444.                                 /* JR16: bits 010001 01100 */
  6445.                   || (b5s5_op (major) & 0x1e) == 0xe)));
  6446.                                 /* JALR16, JALRS16: bits 010001 0111x */
  6447.     /* 32-bit instructions.  */
  6448.     case 0x3d:                        /* JAL: bits 111101 */
  6449.     case 0x3c:                        /* JALX: bits 111100 */
  6450.     case 0x35:                        /* J: bits 110101 */
  6451.     case 0x2d:                        /* BNE: bits 101101 */
  6452.     case 0x25:                        /* BEQ: bits 100101 */
  6453.     case 0x1d:                        /* JALS: bits 011101 */
  6454.       return 1;
  6455.     case 0x10:                        /* POOL32I: bits 010000 */
  6456.       return ((b5s5_op (major) & 0x1c) == 0x0
  6457.                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
  6458.               || (b5s5_op (major) & 0x1d) == 0x4
  6459.                                 /* BLEZ, BGTZ: bits 010000 001x0 */
  6460.               || (b5s5_op (major) & 0x1d) == 0x11
  6461.                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
  6462.               || ((b5s5_op (major) & 0x1e) == 0x14
  6463.                   && (major & 0x3) == 0x0)
  6464.                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
  6465.               || (b5s5_op (major) & 0x1e) == 0x1a
  6466.                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
  6467.               || ((b5s5_op (major) & 0x1e) == 0x1c
  6468.                   && (major & 0x3) == 0x0)
  6469.                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
  6470.               || ((b5s5_op (major) & 0x1c) == 0x1c
  6471.                   && (major & 0x3) == 0x1));
  6472.                                 /* BC1ANY*: bits 010000 111xx xxx01 */
  6473.     case 0x0:                        /* POOL32A: bits 000000 */
  6474.       return (b0s6_op (insn) == 0x3c
  6475.                                 /* POOL32Axf: bits 000000 ... 111100 */
  6476.               && (b6s10_ext (insn) & 0x2bf) == 0x3c);
  6477.                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
  6478.                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
  6479.     default:
  6480.       return 0;
  6481.     }
  6482. }

  6483. /* Return non-zero if a microMIPS instruction at ADDR has a branch delay
  6484.    slot (i.e. it is a non-compact jump instruction).  The instruction
  6485.    must be 32-bit if MUSTBE32 is set or can be any instruction otherwise.  */

  6486. static int
  6487. micromips_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
  6488.                                      CORE_ADDR addr, int mustbe32)
  6489. {
  6490.   ULONGEST insn;
  6491.   int status;

  6492.   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
  6493.   if (status)
  6494.     return 0;
  6495.   insn <<= 16;
  6496.   if (mips_insn_size (ISA_MICROMIPS, insn) == 2 * MIPS_INSN16_SIZE)
  6497.     {
  6498.       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
  6499.       if (status)
  6500.         return 0;
  6501.     }

  6502.   return micromips_instruction_has_delay_slot (insn, mustbe32);
  6503. }

  6504. /* Return non-zero if the MIPS16 instruction INST, which must be
  6505.    a 32-bit instruction if MUSTBE32 is set or can be any instruction
  6506.    otherwise, has a branch delay slot (i.e. it is a non-compact jump
  6507.    instruction).  This function is based on mips16_next_pc.  */

  6508. static int
  6509. mips16_instruction_has_delay_slot (unsigned short inst, int mustbe32)
  6510. {
  6511.   if ((inst & 0xf89f) == 0xe800)        /* JR/JALR (16-bit instruction)  */
  6512.     return !mustbe32;
  6513.   return (inst & 0xf800) == 0x1800;        /* JAL/JALX (32-bit instruction)  */
  6514. }

  6515. /* Return non-zero if a MIPS16 instruction at ADDR has a branch delay
  6516.    slot (i.e. it is a non-compact jump instruction).  The instruction
  6517.    must be 32-bit if MUSTBE32 is set or can be any instruction otherwise.  */

  6518. static int
  6519. mips16_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
  6520.                                   CORE_ADDR addr, int mustbe32)
  6521. {
  6522.   unsigned short insn;
  6523.   int status;

  6524.   insn = mips_fetch_instruction (gdbarch, ISA_MIPS16, addr, &status);
  6525.   if (status)
  6526.     return 0;

  6527.   return mips16_instruction_has_delay_slot (insn, mustbe32);
  6528. }

  6529. /* Calculate the starting address of the MIPS memory segment BPADDR is in.
  6530.    This assumes KSSEG exists.  */

  6531. static CORE_ADDR
  6532. mips_segment_boundary (CORE_ADDR bpaddr)
  6533. {
  6534.   CORE_ADDR mask = CORE_ADDR_MAX;
  6535.   int segsize;

  6536.   if (sizeof (CORE_ADDR) == 8)
  6537.     /* Get the topmost two bits of bpaddr in a 32-bit safe manner (avoid
  6538.        a compiler warning produced where CORE_ADDR is a 32-bit type even
  6539.        though in that case this is dead code).  */
  6540.     switch (bpaddr >> ((sizeof (CORE_ADDR) << 3) - 2) & 3)
  6541.       {
  6542.       case 3:
  6543.         if (bpaddr == (bfd_signed_vma) (int32_t) bpaddr)
  6544.           segsize = 29;                        /* 32-bit compatibility segment  */
  6545.         else
  6546.           segsize = 62;                        /* xkseg  */
  6547.         break;
  6548.       case 2:                                /* xkphys  */
  6549.         segsize = 59;
  6550.         break;
  6551.       default:                                /* xksseg (1), xkuseg/kuseg (0)  */
  6552.         segsize = 62;
  6553.         break;
  6554.       }
  6555.   else if (bpaddr & 0x80000000)                /* kernel segment  */
  6556.     segsize = 29;
  6557.   else
  6558.     segsize = 31;                        /* user segment  */
  6559.   mask <<= segsize;
  6560.   return bpaddr & mask;
  6561. }

  6562. /* Move the breakpoint at BPADDR out of any branch delay slot by shifting
  6563.    it backwards if necessary.  Return the address of the new location.  */

  6564. static CORE_ADDR
  6565. mips_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  6566. {
  6567.   CORE_ADDR prev_addr;
  6568.   CORE_ADDR boundary;
  6569.   CORE_ADDR func_addr;

  6570.   /* If a breakpoint is set on the instruction in a branch delay slot,
  6571.      GDB gets confused.  When the breakpoint is hit, the PC isn't on
  6572.      the instruction in the branch delay slot, the PC will point to
  6573.      the branch instruction.  Since the PC doesn't match any known
  6574.      breakpoints, GDB reports a trap exception.

  6575.      There are two possible fixes for this problem.

  6576.      1) When the breakpoint gets hit, see if the BD bit is set in the
  6577.      Cause register (which indicates the last exception occurred in a
  6578.      branch delay slot).  If the BD bit is set, fix the PC to point to
  6579.      the instruction in the branch delay slot.

  6580.      2) When the user sets the breakpoint, don't allow him to set the
  6581.      breakpoint on the instruction in the branch delay slot.  Instead
  6582.      move the breakpoint to the branch instruction (which will have
  6583.      the same result).

  6584.      The problem with the first solution is that if the user then
  6585.      single-steps the processor, the branch instruction will get
  6586.      skipped (since GDB thinks the PC is on the instruction in the
  6587.      branch delay slot).

  6588.      So, we'll use the second solution.  To do this we need to know if
  6589.      the instruction we're trying to set the breakpoint on is in the
  6590.      branch delay slot.  */

  6591.   boundary = mips_segment_boundary (bpaddr);

  6592.   /* Make sure we don't scan back before the beginning of the current
  6593.      function, since we may fetch constant data or insns that look like
  6594.      a jump.  Of course we might do that anyway if the compiler has
  6595.      moved constants inline. :-(  */
  6596.   if (find_pc_partial_function (bpaddr, NULL, &func_addr, NULL)
  6597.       && func_addr > boundary && func_addr <= bpaddr)
  6598.     boundary = func_addr;

  6599.   if (mips_pc_is_mips (bpaddr))
  6600.     {
  6601.       if (bpaddr == boundary)
  6602.         return bpaddr;

  6603.       /* If the previous instruction has a branch delay slot, we have
  6604.          to move the breakpoint to the branch instruction. */
  6605.       prev_addr = bpaddr - 4;
  6606.       if (mips32_insn_at_pc_has_delay_slot (gdbarch, prev_addr))
  6607.         bpaddr = prev_addr;
  6608.     }
  6609.   else
  6610.     {
  6611.       int (*insn_at_pc_has_delay_slot) (struct gdbarch *, CORE_ADDR, int);
  6612.       CORE_ADDR addr, jmpaddr;
  6613.       int i;

  6614.       boundary = unmake_compact_addr (boundary);

  6615.       /* The only MIPS16 instructions with delay slots are JAL, JALX,
  6616.          JALR and JR.  An absolute JAL/JALX is always 4 bytes long,
  6617.          so try for that first, then try the 2 byte JALR/JR.
  6618.          The microMIPS ASE has a whole range of jumps and branches
  6619.          with delay slots, some of which take 4 bytes and some take
  6620.          2 bytes, so the idea is the same.
  6621.          FIXME: We have to assume that bpaddr is not the second half
  6622.          of an extended instruction.  */
  6623.       insn_at_pc_has_delay_slot = (mips_pc_is_micromips (gdbarch, bpaddr)
  6624.                                    ? micromips_insn_at_pc_has_delay_slot
  6625.                                    : mips16_insn_at_pc_has_delay_slot);

  6626.       jmpaddr = 0;
  6627.       addr = bpaddr;
  6628.       for (i = 1; i < 4; i++)
  6629.         {
  6630.           if (unmake_compact_addr (addr) == boundary)
  6631.             break;
  6632.           addr -= MIPS_INSN16_SIZE;
  6633.           if (i == 1 && insn_at_pc_has_delay_slot (gdbarch, addr, 0))
  6634.             /* Looks like a JR/JALR at [target-1], but it could be
  6635.                the second word of a previous JAL/JALX, so record it
  6636.                and check back one more.  */
  6637.             jmpaddr = addr;
  6638.           else if (i > 1 && insn_at_pc_has_delay_slot (gdbarch, addr, 1))
  6639.             {
  6640.               if (i == 2)
  6641.                 /* Looks like a JAL/JALX at [target-2], but it could also
  6642.                    be the second word of a previous JAL/JALX, record it,
  6643.                    and check back one more.  */
  6644.                 jmpaddr = addr;
  6645.               else
  6646.                 /* Looks like a JAL/JALX at [target-3], so any previously
  6647.                    recorded JAL/JALX or JR/JALR must be wrong, because:

  6648.                    >-3: JAL
  6649.                     -2: JAL-ext (can't be JAL/JALX)
  6650.                     -1: bdslot (can't be JR/JALR)
  6651.                      0: target insn

  6652.                    Of course it could be another JAL-ext which looks
  6653.                    like a JAL, but in that case we'd have broken out
  6654.                    of this loop at [target-2]:

  6655.                     -4: JAL
  6656.                    >-3: JAL-ext
  6657.                     -2: bdslot (can't be jmp)
  6658.                     -1: JR/JALR
  6659.                      0: target insn  */
  6660.                 jmpaddr = 0;
  6661.             }
  6662.           else
  6663.             {
  6664.               /* Not a jump instruction: if we're at [target-1] this
  6665.                  could be the second word of a JAL/JALX, so continue;
  6666.                  otherwise we're done.  */
  6667.               if (i > 1)
  6668.                 break;
  6669.             }
  6670.         }

  6671.       if (jmpaddr)
  6672.         bpaddr = jmpaddr;
  6673.     }

  6674.   return bpaddr;
  6675. }

  6676. /* Return non-zero if SUFFIX is one of the numeric suffixes used for MIPS16
  6677.    call stubs, one of 1, 2, 5, 6, 9, 10, or, if ZERO is non-zero, also 0.  */

  6678. static int
  6679. mips_is_stub_suffix (const char *suffix, int zero)
  6680. {
  6681.   switch (suffix[0])
  6682.    {
  6683.    case '0':
  6684.      return zero && suffix[1] == '\0';
  6685.    case '1':
  6686.      return suffix[1] == '\0' || (suffix[1] == '0' && suffix[2] == '\0');
  6687.    case '2':
  6688.    case '5':
  6689.    case '6':
  6690.    case '9':
  6691.      return suffix[1] == '\0';
  6692.    default:
  6693.      return 0;
  6694.    }
  6695. }

  6696. /* Return non-zero if MODE is one of the mode infixes used for MIPS16
  6697.    call stubs, one of sf, df, sc, or dc.  */

  6698. static int
  6699. mips_is_stub_mode (const char *mode)
  6700. {
  6701.   return ((mode[0] == 's' || mode[0] == 'd')
  6702.           && (mode[1] == 'f' || mode[1] == 'c'));
  6703. }

  6704. /* Code at PC is a compiler-generated stub.  Such a stub for a function
  6705.    bar might have a name like __fn_stub_bar, and might look like this:

  6706.       mfc1    $4, $f13
  6707.       mfc1    $5, $f12
  6708.       mfc1    $6, $f15
  6709.       mfc1    $7, $f14

  6710.    followed by (or interspersed with):

  6711.       j       bar

  6712.    or:

  6713.       lui     $25, %hi(bar)
  6714.       addiu   $25, $25, %lo(bar)
  6715.       jr      $25

  6716.    ($1 may be used in old code; for robustness we accept any register)
  6717.    or, in PIC code:

  6718.       lui     $28, %hi(_gp_disp)
  6719.       addiu   $28, $28, %lo(_gp_disp)
  6720.       addu    $28, $28, $25
  6721.       lw      $25, %got(bar)
  6722.       addiu   $25, $25, %lo(bar)
  6723.       jr      $25

  6724.    In the case of a __call_stub_bar stub, the sequence to set up
  6725.    arguments might look like this:

  6726.       mtc1    $4, $f13
  6727.       mtc1    $5, $f12
  6728.       mtc1    $6, $f15
  6729.       mtc1    $7, $f14

  6730.    followed by (or interspersed with) one of the jump sequences above.

  6731.    In the case of a __call_stub_fp_bar stub, JAL or JALR is used instead
  6732.    of J or JR, respectively, followed by:

  6733.       mfc1    $2, $f0
  6734.       mfc1    $3, $f1
  6735.       jr      $18

  6736.    We are at the beginning of the stub here, and scan down and extract
  6737.    the target address from the jump immediate instruction or, if a jump
  6738.    register instruction is used, from the register referred.  Return
  6739.    the value of PC calculated or 0 if inconclusive.

  6740.    The limit on the search is arbitrarily set to 20 instructions.  FIXME.  */

  6741. static CORE_ADDR
  6742. mips_get_mips16_fn_stub_pc (struct frame_info *frame, CORE_ADDR pc)
  6743. {
  6744.   struct gdbarch *gdbarch = get_frame_arch (frame);
  6745.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  6746.   int addrreg = MIPS_ZERO_REGNUM;
  6747.   CORE_ADDR start_pc = pc;
  6748.   CORE_ADDR target_pc = 0;
  6749.   CORE_ADDR addr = 0;
  6750.   CORE_ADDR gp = 0;
  6751.   int status = 0;
  6752.   int i;

  6753.   for (i = 0;
  6754.        status == 0 && target_pc == 0 && i < 20;
  6755.        i++, pc += MIPS_INSN32_SIZE)
  6756.     {
  6757.       ULONGEST inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
  6758.       CORE_ADDR imm;
  6759.       int rt;
  6760.       int rs;
  6761.       int rd;

  6762.       switch (itype_op (inst))
  6763.         {
  6764.         case 0:                /* SPECIAL */
  6765.           switch (rtype_funct (inst))
  6766.             {
  6767.             case 8:                /* JR */
  6768.             case 9:                /* JALR */
  6769.               rs = rtype_rs (inst);
  6770.               if (rs == MIPS_GP_REGNUM)
  6771.                 target_pc = gp;                                /* Hmm...  */
  6772.               else if (rs == addrreg)
  6773.                 target_pc = addr;
  6774.               break;

  6775.             case 0x21:                /* ADDU */
  6776.               rt = rtype_rt (inst);
  6777.               rs = rtype_rs (inst);
  6778.               rd = rtype_rd (inst);
  6779.               if (rd == MIPS_GP_REGNUM
  6780.                   && ((rs == MIPS_GP_REGNUM && rt == MIPS_T9_REGNUM)
  6781.                       || (rs == MIPS_T9_REGNUM && rt == MIPS_GP_REGNUM)))
  6782.                 gp += start_pc;
  6783.               break;
  6784.             }
  6785.           break;

  6786.         case 2:                /* J */
  6787.         case 3:                /* JAL */
  6788.           target_pc = jtype_target (inst) << 2;
  6789.           target_pc += ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
  6790.           break;

  6791.         case 9:                /* ADDIU */
  6792.           rt = itype_rt (inst);
  6793.           rs = itype_rs (inst);
  6794.           if (rt == rs)
  6795.             {
  6796.               imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
  6797.               if (rt == MIPS_GP_REGNUM)
  6798.                 gp += imm;
  6799.               else if (rt == addrreg)
  6800.                 addr += imm;
  6801.             }
  6802.           break;

  6803.         case 0xf:        /* LUI */
  6804.           rt = itype_rt (inst);
  6805.           imm = ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 16;
  6806.           if (rt == MIPS_GP_REGNUM)
  6807.             gp = imm;
  6808.           else if (rt != MIPS_ZERO_REGNUM)
  6809.             {
  6810.               addrreg = rt;
  6811.               addr = imm;
  6812.             }
  6813.           break;

  6814.         case 0x23:        /* LW */
  6815.           rt = itype_rt (inst);
  6816.           rs = itype_rs (inst);
  6817.           imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
  6818.           if (gp != 0 && rs == MIPS_GP_REGNUM)
  6819.             {
  6820.               gdb_byte buf[4];

  6821.               memset (buf, 0, sizeof (buf));
  6822.               status = target_read_memory (gp + imm, buf, sizeof (buf));
  6823.               addrreg = rt;
  6824.               addr = extract_signed_integer (buf, sizeof (buf), byte_order);
  6825.             }
  6826.           break;
  6827.         }
  6828.     }

  6829.   return target_pc;
  6830. }

  6831. /* If PC is in a MIPS16 call or return stub, return the address of the
  6832.    target PC, which is either the callee or the caller.  There are several
  6833.    cases which must be handled:

  6834.    * If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
  6835.      and the target PC is in $31 ($ra).
  6836.    * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
  6837.      and the target PC is in $2.
  6838.    * If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
  6839.      i.e. before the JALR instruction, this is effectively a call stub
  6840.      and the target PC is in $2.  Otherwise this is effectively
  6841.      a return stub and the target PC is in $18.
  6842.    * If the PC is at the start of __call_stub_fp_*, i.e. before the
  6843.      JAL or JALR instruction, this is effectively a call stub and the
  6844.      target PC is buried in the instruction stream.  Otherwise this
  6845.      is effectively a return stub and the target PC is in $18.
  6846.    * If the PC is in __call_stub_* or in __fn_stub_*, this is a call
  6847.      stub and the target PC is buried in the instruction stream.

  6848.    See the source code for the stubs in gcc/config/mips/mips16.S, or the
  6849.    stub builder in gcc/config/mips/mips.c (mips16_build_call_stub) for the
  6850.    gory details.  */

  6851. static CORE_ADDR
  6852. mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  6853. {
  6854.   struct gdbarch *gdbarch = get_frame_arch (frame);
  6855.   CORE_ADDR start_addr;
  6856.   const char *name;
  6857.   size_t prefixlen;

  6858.   /* Find the starting address and name of the function containing the PC.  */
  6859.   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
  6860.     return 0;

  6861.   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
  6862.      and the target PC is in $31 ($ra).  */
  6863.   prefixlen = strlen (mips_str_mips16_ret_stub);
  6864.   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
  6865.       && mips_is_stub_mode (name + prefixlen)
  6866.       && name[prefixlen + 2] == '\0')
  6867.     return get_frame_register_signed
  6868.              (frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);

  6869.   /* If the PC is in __mips16_call_stub_*, this is one of the call
  6870.      call/return stubs.  */
  6871.   prefixlen = strlen (mips_str_mips16_call_stub);
  6872.   if (strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0)
  6873.     {
  6874.       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
  6875.          and the target PC is in $2.  */
  6876.       if (mips_is_stub_suffix (name + prefixlen, 0))
  6877.         return get_frame_register_signed
  6878.                  (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);

  6879.       /* If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
  6880.          i.e. before the JALR instruction, this is effectively a call stub
  6881.          and the target PC is in $2.  Otherwise this is effectively
  6882.          a return stub and the target PC is in $18.  */
  6883.       else if (mips_is_stub_mode (name + prefixlen)
  6884.                && name[prefixlen + 2] == '_'
  6885.                && mips_is_stub_suffix (name + prefixlen + 3, 0))
  6886.         {
  6887.           if (pc == start_addr)
  6888.             /* This is the 'call' part of a call stub.  The return
  6889.                address is in $2.  */
  6890.             return get_frame_register_signed
  6891.                      (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
  6892.           else
  6893.             /* This is the 'return' part of a call stub.  The return
  6894.                address is in $18.  */
  6895.             return get_frame_register_signed
  6896.                      (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
  6897.         }
  6898.       else
  6899.         return 0;                /* Not a stub.  */
  6900.     }

  6901.   /* If the PC is in __call_stub_* or __fn_stub*, this is one of the
  6902.      compiler-generated call or call/return stubs.  */
  6903.   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0
  6904.       || strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
  6905.     {
  6906.       if (pc == start_addr)
  6907.         /* This is the 'call' part of a call stub.  Call this helper
  6908.            to scan through this code for interesting instructions
  6909.            and determine the final PC.  */
  6910.         return mips_get_mips16_fn_stub_pc (frame, pc);
  6911.       else
  6912.         /* This is the 'return' part of a call stub.  The return address
  6913.            is in $18.  */
  6914.         return get_frame_register_signed
  6915.                  (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
  6916.     }

  6917.   return 0;                        /* Not a stub.  */
  6918. }

  6919. /* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
  6920.    This implements the IN_SOLIB_RETURN_TRAMPOLINE macro.  */

  6921. static int
  6922. mips_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc, const char *name)
  6923. {
  6924.   CORE_ADDR start_addr;
  6925.   size_t prefixlen;

  6926.   /* Find the starting address of the function containing the PC.  */
  6927.   if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
  6928.     return 0;

  6929.   /* If the PC is in __mips16_call_stub_{s,d}{f,c}_{0..10} but not at
  6930.      the start, i.e. after the JALR instruction, this is effectively
  6931.      a return stub.  */
  6932.   prefixlen = strlen (mips_str_mips16_call_stub);
  6933.   if (pc != start_addr
  6934.       && strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0
  6935.       && mips_is_stub_mode (name + prefixlen)
  6936.       && name[prefixlen + 2] == '_'
  6937.       && mips_is_stub_suffix (name + prefixlen + 3, 1))
  6938.     return 1;

  6939.   /* If the PC is in __call_stub_fp_* but not at the start, i.e. after
  6940.      the JAL or JALR instruction, this is effectively a return stub.  */
  6941.   prefixlen = strlen (mips_str_call_fp_stub);
  6942.   if (pc != start_addr
  6943.       && strncmp (name, mips_str_call_fp_stub, prefixlen) == 0)
  6944.     return 1;

  6945.   /* Consume the .pic. prefix of any PIC stub, this function must return
  6946.      true when the PC is in a PIC stub of a __mips16_ret_{d,s}{f,c} stub
  6947.      or the call stub path will trigger in handle_inferior_event causing
  6948.      it to go astray.  */
  6949.   prefixlen = strlen (mips_str_pic);
  6950.   if (strncmp (name, mips_str_pic, prefixlen) == 0)
  6951.     name += prefixlen;

  6952.   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub.  */
  6953.   prefixlen = strlen (mips_str_mips16_ret_stub);
  6954.   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
  6955.       && mips_is_stub_mode (name + prefixlen)
  6956.       && name[prefixlen + 2] == '\0')
  6957.     return 1;

  6958.   return 0;                        /* Not a stub.  */
  6959. }

  6960. /* If the current PC is the start of a non-PIC-to-PIC stub, return the
  6961.    PC of the stub target.  The stub just loads $t9 and jumps to it,
  6962.    so that $t9 has the correct value at function entry.  */

  6963. static CORE_ADDR
  6964. mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  6965. {
  6966.   struct gdbarch *gdbarch = get_frame_arch (frame);
  6967.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  6968.   struct bound_minimal_symbol msym;
  6969.   int i;
  6970.   gdb_byte stub_code[16];
  6971.   int32_t stub_words[4];

  6972.   /* The stub for foo is named ".pic.foo", and is either two
  6973.      instructions inserted before foo or a three instruction sequence
  6974.      which jumps to foo.  */
  6975.   msym = lookup_minimal_symbol_by_pc (pc);
  6976.   if (msym.minsym == NULL
  6977.       || BMSYMBOL_VALUE_ADDRESS (msym) != pc
  6978.       || MSYMBOL_LINKAGE_NAME (msym.minsym) == NULL
  6979.       || strncmp (MSYMBOL_LINKAGE_NAME (msym.minsym), ".pic.", 5) != 0)
  6980.     return 0;

  6981.   /* A two-instruction header.  */
  6982.   if (MSYMBOL_SIZE (msym.minsym) == 8)
  6983.     return pc + 8;

  6984.   /* A three-instruction (plus delay slot) trampoline.  */
  6985.   if (MSYMBOL_SIZE (msym.minsym) == 16)
  6986.     {
  6987.       if (target_read_memory (pc, stub_code, 16) != 0)
  6988.         return 0;
  6989.       for (i = 0; i < 4; i++)
  6990.         stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
  6991.                                                   4, byte_order);

  6992.       /* A stub contains these instructions:
  6993.          lui        t9, %hi(target)
  6994.          j        target
  6995.           addiu        t9, t9, %lo(target)
  6996.          nop

  6997.          This works even for N64, since stubs are only generated with
  6998.          -msym32.  */
  6999.       if ((stub_words[0] & 0xffff0000U) == 0x3c190000
  7000.           && (stub_words[1] & 0xfc000000U) == 0x08000000
  7001.           && (stub_words[2] & 0xffff0000U) == 0x27390000
  7002.           && stub_words[3] == 0x00000000)
  7003.         return ((((stub_words[0] & 0x0000ffff) << 16)
  7004.                  + (stub_words[2] & 0x0000ffff)) ^ 0x8000) - 0x8000;
  7005.     }

  7006.   /* Not a recognized stub.  */
  7007.   return 0;
  7008. }

  7009. static CORE_ADDR
  7010. mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  7011. {
  7012.   CORE_ADDR requested_pc = pc;
  7013.   CORE_ADDR target_pc;
  7014.   CORE_ADDR new_pc;

  7015.   do
  7016.     {
  7017.       target_pc = pc;

  7018.       new_pc = mips_skip_mips16_trampoline_code (frame, pc);
  7019.       if (new_pc)
  7020.         pc = new_pc;

  7021.       new_pc = find_solib_trampoline_target (frame, pc);
  7022.       if (new_pc)
  7023.         pc = new_pc;

  7024.       new_pc = mips_skip_pic_trampoline_code (frame, pc);
  7025.       if (new_pc)
  7026.         pc = new_pc;
  7027.     }
  7028.   while (pc != target_pc);

  7029.   return pc != requested_pc ? pc : 0;
  7030. }

  7031. /* Convert a dbx stab register number (from `r' declaration) to a GDB
  7032.    [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */

  7033. static int
  7034. mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
  7035. {
  7036.   int regnum;
  7037.   if (num >= 0 && num < 32)
  7038.     regnum = num;
  7039.   else if (num >= 38 && num < 70)
  7040.     regnum = num + mips_regnum (gdbarch)->fp0 - 38;
  7041.   else if (num == 70)
  7042.     regnum = mips_regnum (gdbarch)->hi;
  7043.   else if (num == 71)
  7044.     regnum = mips_regnum (gdbarch)->lo;
  7045.   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 72 && num < 78)
  7046.     regnum = num + mips_regnum (gdbarch)->dspacc - 72;
  7047.   else
  7048.     /* This will hopefully (eventually) provoke a warning.  Should
  7049.        we be calling complaint() here?  */
  7050.     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
  7051.   return gdbarch_num_regs (gdbarch) + regnum;
  7052. }


  7053. /* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
  7054.    gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */

  7055. static int
  7056. mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
  7057. {
  7058.   int regnum;
  7059.   if (num >= 0 && num < 32)
  7060.     regnum = num;
  7061.   else if (num >= 32 && num < 64)
  7062.     regnum = num + mips_regnum (gdbarch)->fp0 - 32;
  7063.   else if (num == 64)
  7064.     regnum = mips_regnum (gdbarch)->hi;
  7065.   else if (num == 65)
  7066.     regnum = mips_regnum (gdbarch)->lo;
  7067.   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 66 && num < 72)
  7068.     regnum = num + mips_regnum (gdbarch)->dspacc - 66;
  7069.   else
  7070.     /* This will hopefully (eventually) provoke a warning.  Should we
  7071.        be calling complaint() here?  */
  7072.     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
  7073.   return gdbarch_num_regs (gdbarch) + regnum;
  7074. }

  7075. static int
  7076. mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  7077. {
  7078.   /* Only makes sense to supply raw registers.  */
  7079.   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
  7080.   /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
  7081.      decide if it is valid.  Should instead define a standard sim/gdb
  7082.      register numbering scheme.  */
  7083.   if (gdbarch_register_name (gdbarch,
  7084.                              gdbarch_num_regs (gdbarch) + regnum) != NULL
  7085.       && gdbarch_register_name (gdbarch,
  7086.                                 gdbarch_num_regs (gdbarch)
  7087.                                 + regnum)[0] != '\0')
  7088.     return regnum;
  7089.   else
  7090.     return LEGACY_SIM_REGNO_IGNORE;
  7091. }


  7092. /* Convert an integer into an address.  Extracting the value signed
  7093.    guarantees a correctly sign extended address.  */

  7094. static CORE_ADDR
  7095. mips_integer_to_address (struct gdbarch *gdbarch,
  7096.                          struct type *type, const gdb_byte *buf)
  7097. {
  7098.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  7099.   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
  7100. }

  7101. /* Dummy virtual frame pointer method.  This is no more or less accurate
  7102.    than most other architectures; we just need to be explicit about it,
  7103.    because the pseudo-register gdbarch_sp_regnum will otherwise lead to
  7104.    an assertion failure.  */

  7105. static void
  7106. mips_virtual_frame_pointer (struct gdbarch *gdbarch,
  7107.                             CORE_ADDR pc, int *reg, LONGEST *offset)
  7108. {
  7109.   *reg = MIPS_SP_REGNUM;
  7110.   *offset = 0;
  7111. }

  7112. static void
  7113. mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
  7114. {
  7115.   enum mips_abi *abip = (enum mips_abi *) obj;
  7116.   const char *name = bfd_get_section_name (abfd, sect);

  7117.   if (*abip != MIPS_ABI_UNKNOWN)
  7118.     return;

  7119.   if (strncmp (name, ".mdebug.", 8) != 0)
  7120.     return;

  7121.   if (strcmp (name, ".mdebug.abi32") == 0)
  7122.     *abip = MIPS_ABI_O32;
  7123.   else if (strcmp (name, ".mdebug.abiN32") == 0)
  7124.     *abip = MIPS_ABI_N32;
  7125.   else if (strcmp (name, ".mdebug.abi64") == 0)
  7126.     *abip = MIPS_ABI_N64;
  7127.   else if (strcmp (name, ".mdebug.abiO64") == 0)
  7128.     *abip = MIPS_ABI_O64;
  7129.   else if (strcmp (name, ".mdebug.eabi32") == 0)
  7130.     *abip = MIPS_ABI_EABI32;
  7131.   else if (strcmp (name, ".mdebug.eabi64") == 0)
  7132.     *abip = MIPS_ABI_EABI64;
  7133.   else
  7134.     warning (_("unsupported ABI %s."), name + 8);
  7135. }

  7136. static void
  7137. mips_find_long_section (bfd *abfd, asection *sect, void *obj)
  7138. {
  7139.   int *lbp = (int *) obj;
  7140.   const char *name = bfd_get_section_name (abfd, sect);

  7141.   if (strncmp (name, ".gcc_compiled_long32", 20) == 0)
  7142.     *lbp = 32;
  7143.   else if (strncmp (name, ".gcc_compiled_long64", 20) == 0)
  7144.     *lbp = 64;
  7145.   else if (strncmp (name, ".gcc_compiled_long", 18) == 0)
  7146.     warning (_("unrecognized .gcc_compiled_longXX"));
  7147. }

  7148. static enum mips_abi
  7149. global_mips_abi (void)
  7150. {
  7151.   int i;

  7152.   for (i = 0; mips_abi_strings[i] != NULL; i++)
  7153.     if (mips_abi_strings[i] == mips_abi_string)
  7154.       return (enum mips_abi) i;

  7155.   internal_error (__FILE__, __LINE__, _("unknown ABI string"));
  7156. }

  7157. /* Return the default compressed instruction set, either of MIPS16
  7158.    or microMIPS, selected when none could have been determined from
  7159.    the ELF header of the binary being executed (or no binary has been
  7160.    selected.  */

  7161. static enum mips_isa
  7162. global_mips_compression (void)
  7163. {
  7164.   int i;

  7165.   for (i = 0; mips_compression_strings[i] != NULL; i++)
  7166.     if (mips_compression_strings[i] == mips_compression_string)
  7167.       return (enum mips_isa) i;

  7168.   internal_error (__FILE__, __LINE__, _("unknown compressed ISA string"));
  7169. }

  7170. static void
  7171. mips_register_g_packet_guesses (struct gdbarch *gdbarch)
  7172. {
  7173.   /* If the size matches the set of 32-bit or 64-bit integer registers,
  7174.      assume that's what we've got.  */
  7175.   register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
  7176.   register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);

  7177.   /* If the size matches the full set of registers GDB traditionally
  7178.      knows about, including floating point, for either 32-bit or
  7179.      64-bit, assume that's what we've got.  */
  7180.   register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
  7181.   register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);

  7182.   /* Otherwise we don't have a useful guess.  */
  7183. }

  7184. static struct value *
  7185. value_of_mips_user_reg (struct frame_info *frame, const void *baton)
  7186. {
  7187.   const int *reg_p = baton;
  7188.   return value_of_register (*reg_p, frame);
  7189. }

  7190. static struct gdbarch *
  7191. mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  7192. {
  7193.   struct gdbarch *gdbarch;
  7194.   struct gdbarch_tdep *tdep;
  7195.   int elf_flags;
  7196.   enum mips_abi mips_abi, found_abi, wanted_abi;
  7197.   int i, num_regs;
  7198.   enum mips_fpu_type fpu_type;
  7199.   struct tdesc_arch_data *tdesc_data = NULL;
  7200.   int elf_fpu_type = Val_GNU_MIPS_ABI_FP_ANY;
  7201.   const char **reg_names;
  7202.   struct mips_regnum mips_regnum, *regnum;
  7203.   enum mips_isa mips_isa;
  7204.   int dspacc;
  7205.   int dspctl;

  7206.   /* Fill in the OS dependent register numbers and names.  */
  7207.   if (info.osabi == GDB_OSABI_IRIX)
  7208.     {
  7209.       mips_regnum.fp0 = 32;
  7210.       mips_regnum.pc = 64;
  7211.       mips_regnum.cause = 65;
  7212.       mips_regnum.badvaddr = 66;
  7213.       mips_regnum.hi = 67;
  7214.       mips_regnum.lo = 68;
  7215.       mips_regnum.fp_control_status = 69;
  7216.       mips_regnum.fp_implementation_revision = 70;
  7217.       mips_regnum.dspacc = dspacc = -1;
  7218.       mips_regnum.dspctl = dspctl = -1;
  7219.       num_regs = 71;
  7220.       reg_names = mips_irix_reg_names;
  7221.     }
  7222.   else if (info.osabi == GDB_OSABI_LINUX)
  7223.     {
  7224.       mips_regnum.fp0 = 38;
  7225.       mips_regnum.pc = 37;
  7226.       mips_regnum.cause = 36;
  7227.       mips_regnum.badvaddr = 35;
  7228.       mips_regnum.hi = 34;
  7229.       mips_regnum.lo = 33;
  7230.       mips_regnum.fp_control_status = 70;
  7231.       mips_regnum.fp_implementation_revision = 71;
  7232.       mips_regnum.dspacc = -1;
  7233.       mips_regnum.dspctl = -1;
  7234.       dspacc = 72;
  7235.       dspctl = 78;
  7236.       num_regs = 79;
  7237.       reg_names = mips_linux_reg_names;
  7238.     }
  7239.   else
  7240.     {
  7241.       mips_regnum.lo = MIPS_EMBED_LO_REGNUM;
  7242.       mips_regnum.hi = MIPS_EMBED_HI_REGNUM;
  7243.       mips_regnum.badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
  7244.       mips_regnum.cause = MIPS_EMBED_CAUSE_REGNUM;
  7245.       mips_regnum.pc = MIPS_EMBED_PC_REGNUM;
  7246.       mips_regnum.fp0 = MIPS_EMBED_FP0_REGNUM;
  7247.       mips_regnum.fp_control_status = 70;
  7248.       mips_regnum.fp_implementation_revision = 71;
  7249.       mips_regnum.dspacc = dspacc = -1;
  7250.       mips_regnum.dspctl = dspctl = -1;
  7251.       num_regs = MIPS_LAST_EMBED_REGNUM + 1;
  7252.       if (info.bfd_arch_info != NULL
  7253.           && info.bfd_arch_info->mach == bfd_mach_mips3900)
  7254.         reg_names = mips_tx39_reg_names;
  7255.       else
  7256.         reg_names = mips_generic_reg_names;
  7257.     }

  7258.   /* Check any target description for validity.  */
  7259.   if (tdesc_has_registers (info.target_desc))
  7260.     {
  7261.       static const char *const mips_gprs[] = {
  7262.         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  7263.         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  7264.         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  7265.         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
  7266.       };
  7267.       static const char *const mips_fprs[] = {
  7268.         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  7269.         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
  7270.         "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  7271.         "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
  7272.       };

  7273.       const struct tdesc_feature *feature;
  7274.       int valid_p;

  7275.       feature = tdesc_find_feature (info.target_desc,
  7276.                                     "org.gnu.gdb.mips.cpu");
  7277.       if (feature == NULL)
  7278.         return NULL;

  7279.       tdesc_data = tdesc_data_alloc ();

  7280.       valid_p = 1;
  7281.       for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
  7282.         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  7283.                                             mips_gprs[i]);


  7284.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7285.                                           mips_regnum.lo, "lo");
  7286.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7287.                                           mips_regnum.hi, "hi");
  7288.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7289.                                           mips_regnum.pc, "pc");

  7290.       if (!valid_p)
  7291.         {
  7292.           tdesc_data_cleanup (tdesc_data);
  7293.           return NULL;
  7294.         }

  7295.       feature = tdesc_find_feature (info.target_desc,
  7296.                                     "org.gnu.gdb.mips.cp0");
  7297.       if (feature == NULL)
  7298.         {
  7299.           tdesc_data_cleanup (tdesc_data);
  7300.           return NULL;
  7301.         }

  7302.       valid_p = 1;
  7303.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7304.                                           mips_regnum.badvaddr, "badvaddr");
  7305.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7306.                                           MIPS_PS_REGNUM, "status");
  7307.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7308.                                           mips_regnum.cause, "cause");

  7309.       if (!valid_p)
  7310.         {
  7311.           tdesc_data_cleanup (tdesc_data);
  7312.           return NULL;
  7313.         }

  7314.       /* FIXME drow/2007-05-17: The FPU should be optional.  The MIPS
  7315.          backend is not prepared for that, though.  */
  7316.       feature = tdesc_find_feature (info.target_desc,
  7317.                                     "org.gnu.gdb.mips.fpu");
  7318.       if (feature == NULL)
  7319.         {
  7320.           tdesc_data_cleanup (tdesc_data);
  7321.           return NULL;
  7322.         }

  7323.       valid_p = 1;
  7324.       for (i = 0; i < 32; i++)
  7325.         valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7326.                                             i + mips_regnum.fp0, mips_fprs[i]);

  7327.       valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7328.                                           mips_regnum.fp_control_status,
  7329.                                           "fcsr");
  7330.       valid_p
  7331.         &= tdesc_numbered_register (feature, tdesc_data,
  7332.                                     mips_regnum.fp_implementation_revision,
  7333.                                     "fir");

  7334.       if (!valid_p)
  7335.         {
  7336.           tdesc_data_cleanup (tdesc_data);
  7337.           return NULL;
  7338.         }

  7339.       if (dspacc >= 0)
  7340.         {
  7341.           feature = tdesc_find_feature (info.target_desc,
  7342.                                         "org.gnu.gdb.mips.dsp");
  7343.           /* The DSP registers are optional; it's OK if they are absent.  */
  7344.           if (feature != NULL)
  7345.             {
  7346.               i = 0;
  7347.               valid_p = 1;
  7348.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7349.                                                   dspacc + i++, "hi1");
  7350.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7351.                                                   dspacc + i++, "lo1");
  7352.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7353.                                                   dspacc + i++, "hi2");
  7354.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7355.                                                   dspacc + i++, "lo2");
  7356.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7357.                                                   dspacc + i++, "hi3");
  7358.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7359.                                                   dspacc + i++, "lo3");

  7360.               valid_p &= tdesc_numbered_register (feature, tdesc_data,
  7361.                                                   dspctl, "dspctl");

  7362.               if (!valid_p)
  7363.                 {
  7364.                   tdesc_data_cleanup (tdesc_data);
  7365.                   return NULL;
  7366.                 }

  7367.               mips_regnum.dspacc = dspacc;
  7368.               mips_regnum.dspctl = dspctl;
  7369.             }
  7370.         }

  7371.       /* It would be nice to detect an attempt to use a 64-bit ABI
  7372.          when only 32-bit registers are provided.  */
  7373.       reg_names = NULL;
  7374.     }

  7375.   /* First of all, extract the elf_flags, if available.  */
  7376.   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  7377.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  7378.   else if (arches != NULL)
  7379.     elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
  7380.   else
  7381.     elf_flags = 0;
  7382.   if (gdbarch_debug)
  7383.     fprintf_unfiltered (gdb_stdlog,
  7384.                         "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);

  7385.   /* Check ELF_FLAGS to see if it specifies the ABI being used.  */
  7386.   switch ((elf_flags & EF_MIPS_ABI))
  7387.     {
  7388.     case E_MIPS_ABI_O32:
  7389.       found_abi = MIPS_ABI_O32;
  7390.       break;
  7391.     case E_MIPS_ABI_O64:
  7392.       found_abi = MIPS_ABI_O64;
  7393.       break;
  7394.     case E_MIPS_ABI_EABI32:
  7395.       found_abi = MIPS_ABI_EABI32;
  7396.       break;
  7397.     case E_MIPS_ABI_EABI64:
  7398.       found_abi = MIPS_ABI_EABI64;
  7399.       break;
  7400.     default:
  7401.       if ((elf_flags & EF_MIPS_ABI2))
  7402.         found_abi = MIPS_ABI_N32;
  7403.       else
  7404.         found_abi = MIPS_ABI_UNKNOWN;
  7405.       break;
  7406.     }

  7407.   /* GCC creates a pseudo-section whose name describes the ABI.  */
  7408.   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
  7409.     bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);

  7410.   /* If we have no useful BFD information, use the ABI from the last
  7411.      MIPS architecture (if there is one).  */
  7412.   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
  7413.     found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;

  7414.   /* Try the architecture for any hint of the correct ABI.  */
  7415.   if (found_abi == MIPS_ABI_UNKNOWN
  7416.       && info.bfd_arch_info != NULL
  7417.       && info.bfd_arch_info->arch == bfd_arch_mips)
  7418.     {
  7419.       switch (info.bfd_arch_info->mach)
  7420.         {
  7421.         case bfd_mach_mips3900:
  7422.           found_abi = MIPS_ABI_EABI32;
  7423.           break;
  7424.         case bfd_mach_mips4100:
  7425.         case bfd_mach_mips5000:
  7426.           found_abi = MIPS_ABI_EABI64;
  7427.           break;
  7428.         case bfd_mach_mips8000:
  7429.         case bfd_mach_mips10000:
  7430.           /* On Irix, ELF64 executables use the N64 ABI.  The
  7431.              pseudo-sections which describe the ABI aren't present
  7432.              on IRIX.  (Even for executables created by gcc.)  */
  7433.           if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
  7434.               && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
  7435.             found_abi = MIPS_ABI_N64;
  7436.           else
  7437.             found_abi = MIPS_ABI_N32;
  7438.           break;
  7439.         }
  7440.     }

  7441.   /* Default 64-bit objects to N64 instead of O32.  */
  7442.   if (found_abi == MIPS_ABI_UNKNOWN
  7443.       && info.abfd != NULL
  7444.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
  7445.       && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
  7446.     found_abi = MIPS_ABI_N64;

  7447.   if (gdbarch_debug)
  7448.     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
  7449.                         found_abi);

  7450.   /* What has the user specified from the command line?  */
  7451.   wanted_abi = global_mips_abi ();
  7452.   if (gdbarch_debug)
  7453.     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
  7454.                         wanted_abi);

  7455.   /* Now that we have found what the ABI for this binary would be,
  7456.      check whether the user is overriding it.  */
  7457.   if (wanted_abi != MIPS_ABI_UNKNOWN)
  7458.     mips_abi = wanted_abi;
  7459.   else if (found_abi != MIPS_ABI_UNKNOWN)
  7460.     mips_abi = found_abi;
  7461.   else
  7462.     mips_abi = MIPS_ABI_O32;
  7463.   if (gdbarch_debug)
  7464.     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
  7465.                         mips_abi);

  7466.   /* Determine the default compressed ISA.  */
  7467.   if ((elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0
  7468.       && (elf_flags & EF_MIPS_ARCH_ASE_M16) == 0)
  7469.     mips_isa = ISA_MICROMIPS;
  7470.   else if ((elf_flags & EF_MIPS_ARCH_ASE_M16) != 0
  7471.            && (elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) == 0)
  7472.     mips_isa = ISA_MIPS16;
  7473.   else
  7474.     mips_isa = global_mips_compression ();
  7475.   mips_compression_string = mips_compression_strings[mips_isa];

  7476.   /* Also used when doing an architecture lookup.  */
  7477.   if (gdbarch_debug)
  7478.     fprintf_unfiltered (gdb_stdlog,
  7479.                         "mips_gdbarch_init: "
  7480.                         "mips64_transfers_32bit_regs_p = %d\n",
  7481.                         mips64_transfers_32bit_regs_p);

  7482.   /* Determine the MIPS FPU type.  */
  7483. #ifdef HAVE_ELF
  7484.   if (info.abfd
  7485.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  7486.     elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
  7487.                                              Tag_GNU_MIPS_ABI_FP);
  7488. #endif /* HAVE_ELF */

  7489.   if (!mips_fpu_type_auto)
  7490.     fpu_type = mips_fpu_type;
  7491.   else if (elf_fpu_type != Val_GNU_MIPS_ABI_FP_ANY)
  7492.     {
  7493.       switch (elf_fpu_type)
  7494.         {
  7495.         case Val_GNU_MIPS_ABI_FP_DOUBLE:
  7496.           fpu_type = MIPS_FPU_DOUBLE;
  7497.           break;
  7498.         case Val_GNU_MIPS_ABI_FP_SINGLE:
  7499.           fpu_type = MIPS_FPU_SINGLE;
  7500.           break;
  7501.         case Val_GNU_MIPS_ABI_FP_SOFT:
  7502.         default:
  7503.           /* Soft float or unknown.  */
  7504.           fpu_type = MIPS_FPU_NONE;
  7505.           break;
  7506.         }
  7507.     }
  7508.   else if (info.bfd_arch_info != NULL
  7509.            && info.bfd_arch_info->arch == bfd_arch_mips)
  7510.     switch (info.bfd_arch_info->mach)
  7511.       {
  7512.       case bfd_mach_mips3900:
  7513.       case bfd_mach_mips4100:
  7514.       case bfd_mach_mips4111:
  7515.       case bfd_mach_mips4120:
  7516.         fpu_type = MIPS_FPU_NONE;
  7517.         break;
  7518.       case bfd_mach_mips4650:
  7519.         fpu_type = MIPS_FPU_SINGLE;
  7520.         break;
  7521.       default:
  7522.         fpu_type = MIPS_FPU_DOUBLE;
  7523.         break;
  7524.       }
  7525.   else if (arches != NULL)
  7526.     fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
  7527.   else
  7528.     fpu_type = MIPS_FPU_DOUBLE;
  7529.   if (gdbarch_debug)
  7530.     fprintf_unfiltered (gdb_stdlog,
  7531.                         "mips_gdbarch_init: fpu_type = %d\n", fpu_type);

  7532.   /* Check for blatant incompatibilities.  */

  7533.   /* If we have only 32-bit registers, then we can't debug a 64-bit
  7534.      ABI.  */
  7535.   if (info.target_desc
  7536.       && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
  7537.       && mips_abi != MIPS_ABI_EABI32
  7538.       && mips_abi != MIPS_ABI_O32)
  7539.     {
  7540.       if (tdesc_data != NULL)
  7541.         tdesc_data_cleanup (tdesc_data);
  7542.       return NULL;
  7543.     }

  7544.   /* Try to find a pre-existing architecture.  */
  7545.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  7546.        arches != NULL;
  7547.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  7548.     {
  7549.       /* MIPS needs to be pedantic about which ABI and the compressed
  7550.          ISA variation the object is using.  */
  7551.       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
  7552.         continue;
  7553.       if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
  7554.         continue;
  7555.       if (gdbarch_tdep (arches->gdbarch)->mips_isa != mips_isa)
  7556.         continue;
  7557.       /* Need to be pedantic about which register virtual size is
  7558.          used.  */
  7559.       if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
  7560.           != mips64_transfers_32bit_regs_p)
  7561.         continue;
  7562.       /* Be pedantic about which FPU is selected.  */
  7563.       if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
  7564.         continue;

  7565.       if (tdesc_data != NULL)
  7566.         tdesc_data_cleanup (tdesc_data);
  7567.       return arches->gdbarch;
  7568.     }

  7569.   /* Need a new architecture.  Fill in a target specific vector.  */
  7570.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  7571.   gdbarch = gdbarch_alloc (&info, tdep);
  7572.   tdep->elf_flags = elf_flags;
  7573.   tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
  7574.   tdep->found_abi = found_abi;
  7575.   tdep->mips_abi = mips_abi;
  7576.   tdep->mips_isa = mips_isa;
  7577.   tdep->mips_fpu_type = fpu_type;
  7578.   tdep->register_size_valid_p = 0;
  7579.   tdep->register_size = 0;

  7580.   if (info.target_desc)
  7581.     {
  7582.       /* Some useful properties can be inferred from the target.  */
  7583.       if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
  7584.         {
  7585.           tdep->register_size_valid_p = 1;
  7586.           tdep->register_size = 4;
  7587.         }
  7588.       else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
  7589.         {
  7590.           tdep->register_size_valid_p = 1;
  7591.           tdep->register_size = 8;
  7592.         }
  7593.     }

  7594.   /* Initially set everything according to the default ABI/ISA.  */
  7595.   set_gdbarch_short_bit (gdbarch, 16);
  7596.   set_gdbarch_int_bit (gdbarch, 32);
  7597.   set_gdbarch_float_bit (gdbarch, 32);
  7598.   set_gdbarch_double_bit (gdbarch, 64);
  7599.   set_gdbarch_long_double_bit (gdbarch, 64);
  7600.   set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
  7601.   set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
  7602.   set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);

  7603.   set_gdbarch_ax_pseudo_register_collect (gdbarch,
  7604.                                           mips_ax_pseudo_register_collect);
  7605.   set_gdbarch_ax_pseudo_register_push_stack
  7606.       (gdbarch, mips_ax_pseudo_register_push_stack);

  7607.   set_gdbarch_elf_make_msymbol_special (gdbarch,
  7608.                                         mips_elf_make_msymbol_special);
  7609.   set_gdbarch_make_symbol_special (gdbarch, mips_make_symbol_special);
  7610.   set_gdbarch_adjust_dwarf2_addr (gdbarch, mips_adjust_dwarf2_addr);
  7611.   set_gdbarch_adjust_dwarf2_line (gdbarch, mips_adjust_dwarf2_line);

  7612.   regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct mips_regnum);
  7613.   *regnum = mips_regnum;
  7614.   set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
  7615.   set_gdbarch_num_regs (gdbarch, num_regs);
  7616.   set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
  7617.   set_gdbarch_register_name (gdbarch, mips_register_name);
  7618.   set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
  7619.   tdep->mips_processor_reg_names = reg_names;
  7620.   tdep->regnum = regnum;

  7621.   switch (mips_abi)
  7622.     {
  7623.     case MIPS_ABI_O32:
  7624.       set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
  7625.       set_gdbarch_return_value (gdbarch, mips_o32_return_value);
  7626.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
  7627.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
  7628.       tdep->default_mask_address_p = 0;
  7629.       set_gdbarch_long_bit (gdbarch, 32);
  7630.       set_gdbarch_ptr_bit (gdbarch, 32);
  7631.       set_gdbarch_long_long_bit (gdbarch, 64);
  7632.       break;
  7633.     case MIPS_ABI_O64:
  7634.       set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
  7635.       set_gdbarch_return_value (gdbarch, mips_o64_return_value);
  7636.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
  7637.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
  7638.       tdep->default_mask_address_p = 0;
  7639.       set_gdbarch_long_bit (gdbarch, 32);
  7640.       set_gdbarch_ptr_bit (gdbarch, 32);
  7641.       set_gdbarch_long_long_bit (gdbarch, 64);
  7642.       break;
  7643.     case MIPS_ABI_EABI32:
  7644.       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
  7645.       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
  7646.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
  7647.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
  7648.       tdep->default_mask_address_p = 0;
  7649.       set_gdbarch_long_bit (gdbarch, 32);
  7650.       set_gdbarch_ptr_bit (gdbarch, 32);
  7651.       set_gdbarch_long_long_bit (gdbarch, 64);
  7652.       break;
  7653.     case MIPS_ABI_EABI64:
  7654.       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
  7655.       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
  7656.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
  7657.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
  7658.       tdep->default_mask_address_p = 0;
  7659.       set_gdbarch_long_bit (gdbarch, 64);
  7660.       set_gdbarch_ptr_bit (gdbarch, 64);
  7661.       set_gdbarch_long_long_bit (gdbarch, 64);
  7662.       break;
  7663.     case MIPS_ABI_N32:
  7664.       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
  7665.       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
  7666.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
  7667.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
  7668.       tdep->default_mask_address_p = 0;
  7669.       set_gdbarch_long_bit (gdbarch, 32);
  7670.       set_gdbarch_ptr_bit (gdbarch, 32);
  7671.       set_gdbarch_long_long_bit (gdbarch, 64);
  7672.       set_gdbarch_long_double_bit (gdbarch, 128);
  7673.       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
  7674.       break;
  7675.     case MIPS_ABI_N64:
  7676.       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
  7677.       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
  7678.       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
  7679.       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
  7680.       tdep->default_mask_address_p = 0;
  7681.       set_gdbarch_long_bit (gdbarch, 64);
  7682.       set_gdbarch_ptr_bit (gdbarch, 64);
  7683.       set_gdbarch_long_long_bit (gdbarch, 64);
  7684.       set_gdbarch_long_double_bit (gdbarch, 128);
  7685.       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
  7686.       break;
  7687.     default:
  7688.       internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
  7689.     }

  7690.   /* GCC creates a pseudo-section whose name specifies the size of
  7691.      longs, since -mlong32 or -mlong64 may be used independent of
  7692.      other options.  How those options affect pointer sizes is ABI and
  7693.      architecture dependent, so use them to override the default sizes
  7694.      set by the ABI.  This table shows the relationship between ABI,
  7695.      -mlongXX, and size of pointers:

  7696.      ABI                -mlongXX        ptr bits
  7697.      ---                --------        --------
  7698.      o32                32                32
  7699.      o32                64                32
  7700.      n32                32                32
  7701.      n32                64                64
  7702.      o64                32                32
  7703.      o64                64                64
  7704.      n64                32                32
  7705.      n64                64                64
  7706.      eabi32                32                32
  7707.      eabi32                64                32
  7708.      eabi64                32                32
  7709.      eabi64                64                64

  7710.     Note that for o32 and eabi32, pointers are always 32 bits
  7711.     regardless of any -mlongXX option.  For all others, pointers and
  7712.     longs are the same, as set by -mlongXX or set by defaults.  */

  7713.   if (info.abfd != NULL)
  7714.     {
  7715.       int long_bit = 0;

  7716.       bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
  7717.       if (long_bit)
  7718.         {
  7719.           set_gdbarch_long_bit (gdbarch, long_bit);
  7720.           switch (mips_abi)
  7721.             {
  7722.             case MIPS_ABI_O32:
  7723.             case MIPS_ABI_EABI32:
  7724.               break;
  7725.             case MIPS_ABI_N32:
  7726.             case MIPS_ABI_O64:
  7727.             case MIPS_ABI_N64:
  7728.             case MIPS_ABI_EABI64:
  7729.               set_gdbarch_ptr_bit (gdbarch, long_bit);
  7730.               break;
  7731.             default:
  7732.               internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
  7733.             }
  7734.         }
  7735.     }

  7736.   /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
  7737.      that could indicate -gp32 BUT gas/config/tc-mips.c contains the
  7738.      comment:

  7739.      ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
  7740.      flag in object files because to do so would make it impossible to
  7741.      link with libraries compiled without "-gp32".  This is
  7742.      unnecessarily restrictive.

  7743.      We could solve this problem by adding "-gp32" multilibs to gcc,
  7744.      but to set this flag before gcc is built with such multilibs will
  7745.      break too many systems.''

  7746.      But even more unhelpfully, the default linker output target for
  7747.      mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
  7748.      for 64-bit programs - you need to change the ABI to change this,
  7749.      and not all gcc targets support that currently.  Therefore using
  7750.      this flag to detect 32-bit mode would do the wrong thing given
  7751.      the current gcc - it would make GDB treat these 64-bit programs
  7752.      as 32-bit programs by default.  */

  7753.   set_gdbarch_read_pc (gdbarch, mips_read_pc);
  7754.   set_gdbarch_write_pc (gdbarch, mips_write_pc);

  7755.   /* Add/remove bits from an address.  The MIPS needs be careful to
  7756.      ensure that all 32 bit addresses are sign extended to 64 bits.  */
  7757.   set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);

  7758.   /* Unwind the frame.  */
  7759.   set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
  7760.   set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
  7761.   set_gdbarch_dummy_id (gdbarch, mips_dummy_id);

  7762.   /* Map debug register numbers onto internal register numbers.  */
  7763.   set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
  7764.   set_gdbarch_ecoff_reg_to_regnum (gdbarch,
  7765.                                    mips_dwarf_dwarf2_ecoff_reg_to_regnum);
  7766.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
  7767.                                     mips_dwarf_dwarf2_ecoff_reg_to_regnum);
  7768.   set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);

  7769.   /* MIPS version of CALL_DUMMY.  */

  7770.   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  7771.   set_gdbarch_push_dummy_code (gdbarch, mips_push_dummy_code);
  7772.   set_gdbarch_frame_align (gdbarch, mips_frame_align);

  7773.   set_gdbarch_print_float_info (gdbarch, mips_print_float_info);

  7774.   set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
  7775.   set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
  7776.   set_gdbarch_value_to_register (gdbarch, mips_value_to_register);

  7777.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  7778.   set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
  7779.   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
  7780.                                          mips_remote_breakpoint_from_pc);
  7781.   set_gdbarch_adjust_breakpoint_address (gdbarch,
  7782.                                          mips_adjust_breakpoint_address);

  7783.   set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);

  7784.   set_gdbarch_in_function_epilogue_p (gdbarch, mips_in_function_epilogue_p);

  7785.   set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
  7786.   set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
  7787.   set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);

  7788.   set_gdbarch_register_type (gdbarch, mips_register_type);

  7789.   set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);

  7790.   if (mips_abi == MIPS_ABI_N32)
  7791.     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
  7792.   else if (mips_abi == MIPS_ABI_N64)
  7793.     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
  7794.   else
  7795.     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);

  7796.   /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
  7797.      HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
  7798.      need to all be folded into the target vector.  Since they are
  7799.      being used as guards for target_stopped_by_watchpoint, why not have
  7800.      target_stopped_by_watchpoint return the type of watchpoint that the code
  7801.      is sitting on?  */
  7802.   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  7803.   set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);

  7804.   /* NOTE drow/2012-04-25: We overload the core solib trampoline code
  7805.      to support MIPS16.  This is a bad thing.  Make sure not to do it
  7806.      if we have an OS ABI that actually supports shared libraries, since
  7807.      shared library support is more important.  If we have an OS someday
  7808.      that supports both shared libraries and MIPS16, we'll have to find
  7809.      a better place for these.
  7810.      macro/2012-04-25: But that applies to return trampolines only and
  7811.      currently no MIPS OS ABI uses shared libraries that have them.  */
  7812.   set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);

  7813.   set_gdbarch_single_step_through_delay (gdbarch,
  7814.                                          mips_single_step_through_delay);

  7815.   /* Virtual tables.  */
  7816.   set_gdbarch_vbit_in_delta (gdbarch, 1);

  7817.   mips_register_g_packet_guesses (gdbarch);

  7818.   /* Hook in OS ABI-specific overrides, if they have been registered.  */
  7819.   info.tdep_info = (void *) tdesc_data;
  7820.   gdbarch_init_osabi (info, gdbarch);

  7821.   /* The hook may have adjusted num_regs, fetch the final value and
  7822.      set pc_regnum and sp_regnum now that it has been fixed.  */
  7823.   num_regs = gdbarch_num_regs (gdbarch);
  7824.   set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
  7825.   set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);

  7826.   /* Unwind the frame.  */
  7827.   dwarf2_append_unwinders (gdbarch);
  7828.   frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
  7829.   frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
  7830.   frame_unwind_append_unwinder (gdbarch, &mips_micro_frame_unwind);
  7831.   frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
  7832.   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
  7833.   frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
  7834.   frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
  7835.   frame_base_append_sniffer (gdbarch, mips_micro_frame_base_sniffer);
  7836.   frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);

  7837.   if (tdesc_data)
  7838.     {
  7839.       set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
  7840.       tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);

  7841.       /* Override the normal target description methods to handle our
  7842.          dual real and pseudo registers.  */
  7843.       set_gdbarch_register_name (gdbarch, mips_register_name);
  7844.       set_gdbarch_register_reggroup_p (gdbarch,
  7845.                                        mips_tdesc_register_reggroup_p);

  7846.       num_regs = gdbarch_num_regs (gdbarch);
  7847.       set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
  7848.       set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
  7849.       set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
  7850.     }

  7851.   /* Add ABI-specific aliases for the registers.  */
  7852.   if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
  7853.     for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
  7854.       user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
  7855.                     value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
  7856.   else
  7857.     for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
  7858.       user_reg_add (gdbarch, mips_o32_aliases[i].name,
  7859.                     value_of_mips_user_reg, &mips_o32_aliases[i].regnum);

  7860.   /* Add some other standard aliases.  */
  7861.   for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
  7862.     user_reg_add (gdbarch, mips_register_aliases[i].name,
  7863.                   value_of_mips_user_reg, &mips_register_aliases[i].regnum);

  7864.   for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
  7865.     user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
  7866.                   value_of_mips_user_reg,
  7867.                   &mips_numeric_register_aliases[i].regnum);

  7868.   return gdbarch;
  7869. }

  7870. static void
  7871. mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
  7872. {
  7873.   struct gdbarch_info info;

  7874.   /* Force the architecture to update, and (if it's a MIPS architecture)
  7875.      mips_gdbarch_init will take care of the rest.  */
  7876.   gdbarch_info_init (&info);
  7877.   gdbarch_update_p (info);
  7878. }

  7879. /* Print out which MIPS ABI is in use.  */

  7880. static void
  7881. show_mips_abi (struct ui_file *file,
  7882.                int from_tty,
  7883.                struct cmd_list_element *ignored_cmd,
  7884.                const char *ignored_value)
  7885. {
  7886.   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
  7887.     fprintf_filtered
  7888.       (file,
  7889.        "The MIPS ABI is unknown because the current architecture "
  7890.        "is not MIPS.\n");
  7891.   else
  7892.     {
  7893.       enum mips_abi global_abi = global_mips_abi ();
  7894.       enum mips_abi actual_abi = mips_abi (target_gdbarch ());
  7895.       const char *actual_abi_str = mips_abi_strings[actual_abi];

  7896.       if (global_abi == MIPS_ABI_UNKNOWN)
  7897.         fprintf_filtered
  7898.           (file,
  7899.            "The MIPS ABI is set automatically (currently \"%s\").\n",
  7900.            actual_abi_str);
  7901.       else if (global_abi == actual_abi)
  7902.         fprintf_filtered
  7903.           (file,
  7904.            "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
  7905.            actual_abi_str);
  7906.       else
  7907.         {
  7908.           /* Probably shouldn't happen...  */
  7909.           fprintf_filtered (file,
  7910.                             "The (auto detected) MIPS ABI \"%s\" is in use "
  7911.                             "even though the user setting was \"%s\".\n",
  7912.              actual_abi_str, mips_abi_strings[global_abi]);
  7913.         }
  7914.     }
  7915. }

  7916. /* Print out which MIPS compressed ISA encoding is used.  */

  7917. static void
  7918. show_mips_compression (struct ui_file *file, int from_tty,
  7919.                        struct cmd_list_element *c, const char *value)
  7920. {
  7921.   fprintf_filtered (file, _("The compressed ISA encoding used is %s.\n"),
  7922.                     value);
  7923. }

  7924. static void
  7925. mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  7926. {
  7927.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  7928.   if (tdep != NULL)
  7929.     {
  7930.       int ef_mips_arch;
  7931.       int ef_mips_32bitmode;
  7932.       /* Determine the ISA.  */
  7933.       switch (tdep->elf_flags & EF_MIPS_ARCH)
  7934.         {
  7935.         case E_MIPS_ARCH_1:
  7936.           ef_mips_arch = 1;
  7937.           break;
  7938.         case E_MIPS_ARCH_2:
  7939.           ef_mips_arch = 2;
  7940.           break;
  7941.         case E_MIPS_ARCH_3:
  7942.           ef_mips_arch = 3;
  7943.           break;
  7944.         case E_MIPS_ARCH_4:
  7945.           ef_mips_arch = 4;
  7946.           break;
  7947.         default:
  7948.           ef_mips_arch = 0;
  7949.           break;
  7950.         }
  7951.       /* Determine the size of a pointer.  */
  7952.       ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
  7953.       fprintf_unfiltered (file,
  7954.                           "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
  7955.                           tdep->elf_flags);
  7956.       fprintf_unfiltered (file,
  7957.                           "mips_dump_tdep: ef_mips_32bitmode = %d\n",
  7958.                           ef_mips_32bitmode);
  7959.       fprintf_unfiltered (file,
  7960.                           "mips_dump_tdep: ef_mips_arch = %d\n",
  7961.                           ef_mips_arch);
  7962.       fprintf_unfiltered (file,
  7963.                           "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
  7964.                           tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
  7965.       fprintf_unfiltered (file,
  7966.                           "mips_dump_tdep: "
  7967.                           "mips_mask_address_p() %d (default %d)\n",
  7968.                           mips_mask_address_p (tdep),
  7969.                           tdep->default_mask_address_p);
  7970.     }
  7971.   fprintf_unfiltered (file,
  7972.                       "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
  7973.                       MIPS_DEFAULT_FPU_TYPE,
  7974.                       (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
  7975.                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
  7976.                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
  7977.                        : "???"));
  7978.   fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
  7979.                       MIPS_EABI (gdbarch));
  7980.   fprintf_unfiltered (file,
  7981.                       "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
  7982.                       MIPS_FPU_TYPE (gdbarch),
  7983.                       (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
  7984.                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
  7985.                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
  7986.                        : "???"));
  7987. }

  7988. extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */

  7989. void
  7990. _initialize_mips_tdep (void)
  7991. {
  7992.   static struct cmd_list_element *mipsfpulist = NULL;
  7993.   struct cmd_list_element *c;

  7994.   mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
  7995.   if (MIPS_ABI_LAST + 1
  7996.       != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
  7997.     internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));

  7998.   gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);

  7999.   mips_pdr_data = register_objfile_data ();

  8000.   /* Create feature sets with the appropriate properties.  The values
  8001.      are not important.  */
  8002.   mips_tdesc_gp32 = allocate_target_description ();
  8003.   set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");

  8004.   mips_tdesc_gp64 = allocate_target_description ();
  8005.   set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");

  8006.   /* Add root prefix command for all "set mips"/"show mips" commands.  */
  8007.   add_prefix_cmd ("mips", no_class, set_mips_command,
  8008.                   _("Various MIPS specific commands."),
  8009.                   &setmipscmdlist, "set mips ", 0, &setlist);

  8010.   add_prefix_cmd ("mips", no_class, show_mips_command,
  8011.                   _("Various MIPS specific commands."),
  8012.                   &showmipscmdlist, "show mips ", 0, &showlist);

  8013.   /* Allow the user to override the ABI.  */
  8014.   add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
  8015.                         &mips_abi_string, _("\
  8016. Set the MIPS ABI used by this program."), _("\
  8017. Show the MIPS ABI used by this program."), _("\
  8018. This option can be set to one of:\n\
  8019.   auto  - the default ABI associated with the current binary\n\
  8020.   o32\n\
  8021.   o64\n\
  8022.   n32\n\
  8023.   n64\n\
  8024.   eabi32\n\
  8025.   eabi64"),
  8026.                         mips_abi_update,
  8027.                         show_mips_abi,
  8028.                         &setmipscmdlist, &showmipscmdlist);

  8029.   /* Allow the user to set the ISA to assume for compressed code if ELF
  8030.      file flags don't tell or there is no program file selected.  This
  8031.      setting is updated whenever unambiguous ELF file flags are interpreted,
  8032.      and carried over to subsequent sessions.  */
  8033.   add_setshow_enum_cmd ("compression", class_obscure, mips_compression_strings,
  8034.                         &mips_compression_string, _("\
  8035. Set the compressed ISA encoding used by MIPS code."), _("\
  8036. Show the compressed ISA encoding used by MIPS code."), _("\
  8037. Select the compressed ISA encoding used in functions that have no symbol\n\
  8038. information available.  The encoding can be set to either of:\n\
  8039.   mips16\n\
  8040.   micromips\n\
  8041. and is updated automatically from ELF file flags if available."),
  8042.                         mips_abi_update,
  8043.                         show_mips_compression,
  8044.                         &setmipscmdlist, &showmipscmdlist);

  8045.   /* Let the user turn off floating point and set the fence post for
  8046.      heuristic_proc_start.  */

  8047.   add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
  8048.                   _("Set use of MIPS floating-point coprocessor."),
  8049.                   &mipsfpulist, "set mipsfpu ", 0, &setlist);
  8050.   add_cmd ("single", class_support, set_mipsfpu_single_command,
  8051.            _("Select single-precision MIPS floating-point coprocessor."),
  8052.            &mipsfpulist);
  8053.   add_cmd ("double", class_support, set_mipsfpu_double_command,
  8054.            _("Select double-precision MIPS floating-point coprocessor."),
  8055.            &mipsfpulist);
  8056.   add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
  8057.   add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
  8058.   add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
  8059.   add_cmd ("none", class_support, set_mipsfpu_none_command,
  8060.            _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
  8061.   add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
  8062.   add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
  8063.   add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
  8064.   add_cmd ("auto", class_support, set_mipsfpu_auto_command,
  8065.            _("Select MIPS floating-point coprocessor automatically."),
  8066.            &mipsfpulist);
  8067.   add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
  8068.            _("Show current use of MIPS floating-point coprocessor target."),
  8069.            &showlist);

  8070.   /* We really would like to have both "0" and "unlimited" work, but
  8071.      command.c doesn't deal with that.  So make it a var_zinteger
  8072.      because the user can always use "999999" or some such for unlimited.  */
  8073.   add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
  8074.                             &heuristic_fence_post, _("\
  8075. Set the distance searched for the start of a function."), _("\
  8076. Show the distance searched for the start of a function."), _("\
  8077. If you are debugging a stripped executable, GDB needs to search through the\n\
  8078. program for the start of a function.  This command sets the distance of the\n\
  8079. search.  The only need to set it is when debugging a stripped executable."),
  8080.                             reinit_frame_cache_sfunc,
  8081.                             NULL, /* FIXME: i18n: The distance searched for
  8082.                                      the start of a function is %s.  */
  8083.                             &setlist, &showlist);

  8084.   /* Allow the user to control whether the upper bits of 64-bit
  8085.      addresses should be zeroed.  */
  8086.   add_setshow_auto_boolean_cmd ("mask-address", no_class,
  8087.                                 &mask_address_var, _("\
  8088. Set zeroing of upper 32 bits of 64-bit addresses."), _("\
  8089. Show zeroing of upper 32 bits of 64-bit addresses."), _("\
  8090. Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
  8091. allow GDB to determine the correct value."),
  8092.                                 NULL, show_mask_address,
  8093.                                 &setmipscmdlist, &showmipscmdlist);

  8094.   /* Allow the user to control the size of 32 bit registers within the
  8095.      raw remote packet.  */
  8096.   add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
  8097.                            &mips64_transfers_32bit_regs_p, _("\
  8098. Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
  8099.                            _("\
  8100. Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
  8101.                            _("\
  8102. Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
  8103. that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
  8104. 64 bits for others.  Use \"off\" to disable compatibility mode"),
  8105.                            set_mips64_transfers_32bit_regs,
  8106.                            NULL, /* FIXME: i18n: Compatibility with 64-bit
  8107.                                     MIPS target that transfers 32-bit
  8108.                                     quantities is %s.  */
  8109.                            &setlist, &showlist);

  8110.   /* Debug this files internals.  */
  8111.   add_setshow_zuinteger_cmd ("mips", class_maintenance,
  8112.                              &mips_debug, _("\
  8113. Set mips debugging."), _("\
  8114. Show mips debugging."), _("\
  8115. When non-zero, mips specific debugging is enabled."),
  8116.                              NULL,
  8117.                              NULL, /* FIXME: i18n: Mips debugging is
  8118.                                       currently %s.  */
  8119.                              &setdebuglist, &showdebuglist);
  8120. }