GDB (API)
/home/stan/gdb/src/gdb/mips-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
00002 
00003    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
00006    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
00007 
00008    This file is part of GDB.
00009 
00010    This program is free software; you can redistribute it and/or modify
00011    it under the terms of the GNU General Public License as published by
00012    the Free Software Foundation; either version 3 of the License, or
00013    (at your option) any later version.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License
00021    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00022 
00023 #include "defs.h"
00024 #include "gdb_string.h"
00025 #include "gdb_assert.h"
00026 #include "frame.h"
00027 #include "inferior.h"
00028 #include "symtab.h"
00029 #include "value.h"
00030 #include "gdbcmd.h"
00031 #include "language.h"
00032 #include "gdbcore.h"
00033 #include "symfile.h"
00034 #include "objfiles.h"
00035 #include "gdbtypes.h"
00036 #include "target.h"
00037 #include "arch-utils.h"
00038 #include "regcache.h"
00039 #include "osabi.h"
00040 #include "mips-tdep.h"
00041 #include "block.h"
00042 #include "reggroups.h"
00043 #include "opcode/mips.h"
00044 #include "elf/mips.h"
00045 #include "elf-bfd.h"
00046 #include "symcat.h"
00047 #include "sim-regno.h"
00048 #include "dis-asm.h"
00049 #include "frame-unwind.h"
00050 #include "frame-base.h"
00051 #include "trad-frame.h"
00052 #include "infcall.h"
00053 #include "floatformat.h"
00054 #include "remote.h"
00055 #include "target-descriptions.h"
00056 #include "dwarf2-frame.h"
00057 #include "user-regs.h"
00058 #include "valprint.h"
00059 #include "ax.h"
00060 
00061 static const struct objfile_data *mips_pdr_data;
00062 
00063 static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
00064 
00065 static int mips32_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR);
00066 static int micromips_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR,
00067                                                  int);
00068 static int mips16_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR,
00069                                               int);
00070 
00071 /* A useful bit in the CP0 status register (MIPS_PS_REGNUM).  */
00072 /* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip.  */
00073 #define ST0_FR (1 << 26)
00074 
00075 /* The sizes of floating point registers.  */
00076 
00077 enum
00078 {
00079   MIPS_FPU_SINGLE_REGSIZE = 4,
00080   MIPS_FPU_DOUBLE_REGSIZE = 8
00081 };
00082 
00083 enum
00084 {
00085   MIPS32_REGSIZE = 4,
00086   MIPS64_REGSIZE = 8
00087 };
00088 
00089 static const char *mips_abi_string;
00090 
00091 static const char *const mips_abi_strings[] = {
00092   "auto",
00093   "n32",
00094   "o32",
00095   "n64",
00096   "o64",
00097   "eabi32",
00098   "eabi64",
00099   NULL
00100 };
00101 
00102 /* For backwards compatibility we default to MIPS16.  This flag is
00103    overridden as soon as unambiguous ELF file flags tell us the
00104    compressed ISA encoding used.  */
00105 static const char mips_compression_mips16[] = "mips16";
00106 static const char mips_compression_micromips[] = "micromips";
00107 static const char *const mips_compression_strings[] =
00108 {
00109   mips_compression_mips16,
00110   mips_compression_micromips,
00111   NULL
00112 };
00113 
00114 static const char *mips_compression_string = mips_compression_mips16;
00115 
00116 /* The standard register names, and all the valid aliases for them.  */
00117 struct register_alias
00118 {
00119   const char *name;
00120   int regnum;
00121 };
00122 
00123 /* Aliases for o32 and most other ABIs.  */
00124 const struct register_alias mips_o32_aliases[] = {
00125   { "ta0", 12 },
00126   { "ta1", 13 },
00127   { "ta2", 14 },
00128   { "ta3", 15 }
00129 };
00130 
00131 /* Aliases for n32 and n64.  */
00132 const struct register_alias mips_n32_n64_aliases[] = {
00133   { "ta0", 8 },
00134   { "ta1", 9 },
00135   { "ta2", 10 },
00136   { "ta3", 11 }
00137 };
00138 
00139 /* Aliases for ABI-independent registers.  */
00140 const struct register_alias mips_register_aliases[] = {
00141   /* The architecture manuals specify these ABI-independent names for
00142      the GPRs.  */
00143 #define R(n) { "r" #n, n }
00144   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
00145   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
00146   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
00147   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
00148 #undef R
00149 
00150   /* k0 and k1 are sometimes called these instead (for "kernel
00151      temp").  */
00152   { "kt0", 26 },
00153   { "kt1", 27 },
00154 
00155   /* This is the traditional GDB name for the CP0 status register.  */
00156   { "sr", MIPS_PS_REGNUM },
00157 
00158   /* This is the traditional GDB name for the CP0 BadVAddr register.  */
00159   { "bad", MIPS_EMBED_BADVADDR_REGNUM },
00160 
00161   /* This is the traditional GDB name for the FCSR.  */
00162   { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
00163 };
00164 
00165 const struct register_alias mips_numeric_register_aliases[] = {
00166 #define R(n) { #n, n }
00167   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
00168   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
00169   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
00170   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
00171 #undef R
00172 };
00173 
00174 #ifndef MIPS_DEFAULT_FPU_TYPE
00175 #define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
00176 #endif
00177 static int mips_fpu_type_auto = 1;
00178 static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
00179 
00180 static unsigned int mips_debug = 0;
00181 
00182 /* Properties (for struct target_desc) describing the g/G packet
00183    layout.  */
00184 #define PROPERTY_GP32 "internal: transfers-32bit-registers"
00185 #define PROPERTY_GP64 "internal: transfers-64bit-registers"
00186 
00187 struct target_desc *mips_tdesc_gp32;
00188 struct target_desc *mips_tdesc_gp64;
00189 
00190 const struct mips_regnum *
00191 mips_regnum (struct gdbarch *gdbarch)
00192 {
00193   return gdbarch_tdep (gdbarch)->regnum;
00194 }
00195 
00196 static int
00197 mips_fpa0_regnum (struct gdbarch *gdbarch)
00198 {
00199   return mips_regnum (gdbarch)->fp0 + 12;
00200 }
00201 
00202 /* Return 1 if REGNUM refers to a floating-point general register, raw
00203    or cooked.  Otherwise return 0.  */
00204 
00205 static int
00206 mips_float_register_p (struct gdbarch *gdbarch, int regnum)
00207 {
00208   int rawnum = regnum % gdbarch_num_regs (gdbarch);
00209 
00210   return (rawnum >= mips_regnum (gdbarch)->fp0
00211           && rawnum < mips_regnum (gdbarch)->fp0 + 32);
00212 }
00213 
00214 #define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
00215                      == MIPS_ABI_EABI32 \
00216                    || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)
00217 
00218 #define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
00219   (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)
00220 
00221 #define MIPS_LAST_ARG_REGNUM(gdbarch) \
00222   (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)
00223 
00224 #define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)
00225 
00226 /* Return the MIPS ABI associated with GDBARCH.  */
00227 enum mips_abi
00228 mips_abi (struct gdbarch *gdbarch)
00229 {
00230   return gdbarch_tdep (gdbarch)->mips_abi;
00231 }
00232 
00233 int
00234 mips_isa_regsize (struct gdbarch *gdbarch)
00235 {
00236   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00237 
00238   /* If we know how big the registers are, use that size.  */
00239   if (tdep->register_size_valid_p)
00240     return tdep->register_size;
00241 
00242   /* Fall back to the previous behavior.  */
00243   return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
00244           / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
00245 }
00246 
00247 /* Return the currently configured (or set) saved register size.  */
00248 
00249 unsigned int
00250 mips_abi_regsize (struct gdbarch *gdbarch)
00251 {
00252   switch (mips_abi (gdbarch))
00253     {
00254     case MIPS_ABI_EABI32:
00255     case MIPS_ABI_O32:
00256       return 4;
00257     case MIPS_ABI_N32:
00258     case MIPS_ABI_N64:
00259     case MIPS_ABI_O64:
00260     case MIPS_ABI_EABI64:
00261       return 8;
00262     case MIPS_ABI_UNKNOWN:
00263     case MIPS_ABI_LAST:
00264     default:
00265       internal_error (__FILE__, __LINE__, _("bad switch"));
00266     }
00267 }
00268 
00269 /* MIPS16/microMIPS function addresses are odd (bit 0 is set).  Here
00270    are some functions to handle addresses associated with compressed
00271    code including but not limited to testing, setting, or clearing
00272    bit 0 of such addresses.  */
00273 
00274 /* Return one iff compressed code is the MIPS16 instruction set.  */
00275 
00276 static int
00277 is_mips16_isa (struct gdbarch *gdbarch)
00278 {
00279   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MIPS16;
00280 }
00281 
00282 /* Return one iff compressed code is the microMIPS instruction set.  */
00283 
00284 static int
00285 is_micromips_isa (struct gdbarch *gdbarch)
00286 {
00287   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MICROMIPS;
00288 }
00289 
00290 /* Return one iff ADDR denotes compressed code.  */
00291 
00292 static int
00293 is_compact_addr (CORE_ADDR addr)
00294 {
00295   return ((addr) & 1);
00296 }
00297 
00298 /* Return one iff ADDR denotes standard ISA code.  */
00299 
00300 static int
00301 is_mips_addr (CORE_ADDR addr)
00302 {
00303   return !is_compact_addr (addr);
00304 }
00305 
00306 /* Return one iff ADDR denotes MIPS16 code.  */
00307 
00308 static int
00309 is_mips16_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
00310 {
00311   return is_compact_addr (addr) && is_mips16_isa (gdbarch);
00312 }
00313 
00314 /* Return one iff ADDR denotes microMIPS code.  */
00315 
00316 static int
00317 is_micromips_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
00318 {
00319   return is_compact_addr (addr) && is_micromips_isa (gdbarch);
00320 }
00321 
00322 /* Strip the ISA (compression) bit off from ADDR.  */
00323 
00324 static CORE_ADDR
00325 unmake_compact_addr (CORE_ADDR addr)
00326 {
00327   return ((addr) & ~(CORE_ADDR) 1);
00328 }
00329 
00330 /* Add the ISA (compression) bit to ADDR.  */
00331 
00332 static CORE_ADDR
00333 make_compact_addr (CORE_ADDR addr)
00334 {
00335   return ((addr) | (CORE_ADDR) 1);
00336 }
00337 
00338 /* Functions for setting and testing a bit in a minimal symbol that
00339    marks it as MIPS16 or microMIPS function.  The MSB of the minimal
00340    symbol's "info" field is used for this purpose.
00341 
00342    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is
00343    "special", i.e. refers to a MIPS16 or microMIPS function, and sets
00344    one of the "special" bits in a minimal symbol to mark it accordingly.
00345    The test checks an ELF-private flag that is valid for true function
00346    symbols only; for synthetic symbols such as for PLT stubs that have
00347    no ELF-private part at all the MIPS BFD backend arranges for this
00348    information to be carried in the asymbol's udata field instead.
00349 
00350    msymbol_is_mips16 and msymbol_is_micromips test the "special" bit
00351    in a minimal symbol.  */
00352 
00353 static void
00354 mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
00355 {
00356   elf_symbol_type *elfsym = (elf_symbol_type *) sym;
00357   unsigned char st_other;
00358 
00359   if ((sym->flags & BSF_SYNTHETIC) == 0)
00360     st_other = elfsym->internal_elf_sym.st_other;
00361   else if ((sym->flags & BSF_FUNCTION) != 0)
00362     st_other = sym->udata.i;
00363   else
00364     return;
00365 
00366   if (ELF_ST_IS_MICROMIPS (st_other))
00367     MSYMBOL_TARGET_FLAG_2 (msym) = 1;
00368   else if (ELF_ST_IS_MIPS16 (st_other))
00369     MSYMBOL_TARGET_FLAG_1 (msym) = 1;
00370 }
00371 
00372 /* Return one iff MSYM refers to standard ISA code.  */
00373 
00374 static int
00375 msymbol_is_mips (struct minimal_symbol *msym)
00376 {
00377   return !(MSYMBOL_TARGET_FLAG_1 (msym) | MSYMBOL_TARGET_FLAG_2 (msym));
00378 }
00379 
00380 /* Return one iff MSYM refers to MIPS16 code.  */
00381 
00382 static int
00383 msymbol_is_mips16 (struct minimal_symbol *msym)
00384 {
00385   return MSYMBOL_TARGET_FLAG_1 (msym);
00386 }
00387 
00388 /* Return one iff MSYM refers to microMIPS code.  */
00389 
00390 static int
00391 msymbol_is_micromips (struct minimal_symbol *msym)
00392 {
00393   return MSYMBOL_TARGET_FLAG_2 (msym);
00394 }
00395 
00396 /* XFER a value from the big/little/left end of the register.
00397    Depending on the size of the value it might occupy the entire
00398    register or just part of it.  Make an allowance for this, aligning
00399    things accordingly.  */
00400 
00401 static void
00402 mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
00403                     int reg_num, int length,
00404                     enum bfd_endian endian, gdb_byte *in,
00405                     const gdb_byte *out, int buf_offset)
00406 {
00407   int reg_offset = 0;
00408 
00409   gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
00410   /* Need to transfer the left or right part of the register, based on
00411      the targets byte order.  */
00412   switch (endian)
00413     {
00414     case BFD_ENDIAN_BIG:
00415       reg_offset = register_size (gdbarch, reg_num) - length;
00416       break;
00417     case BFD_ENDIAN_LITTLE:
00418       reg_offset = 0;
00419       break;
00420     case BFD_ENDIAN_UNKNOWN:    /* Indicates no alignment.  */
00421       reg_offset = 0;
00422       break;
00423     default:
00424       internal_error (__FILE__, __LINE__, _("bad switch"));
00425     }
00426   if (mips_debug)
00427     fprintf_unfiltered (gdb_stderr,
00428                         "xfer $%d, reg offset %d, buf offset %d, length %d, ",
00429                         reg_num, reg_offset, buf_offset, length);
00430   if (mips_debug && out != NULL)
00431     {
00432       int i;
00433       fprintf_unfiltered (gdb_stdlog, "out ");
00434       for (i = 0; i < length; i++)
00435         fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
00436     }
00437   if (in != NULL)
00438     regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
00439                                in + buf_offset);
00440   if (out != NULL)
00441     regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
00442                                 out + buf_offset);
00443   if (mips_debug && in != NULL)
00444     {
00445       int i;
00446       fprintf_unfiltered (gdb_stdlog, "in ");
00447       for (i = 0; i < length; i++)
00448         fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
00449     }
00450   if (mips_debug)
00451     fprintf_unfiltered (gdb_stdlog, "\n");
00452 }
00453 
00454 /* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
00455    compatiblity mode.  A return value of 1 means that we have
00456    physical 64-bit registers, but should treat them as 32-bit registers.  */
00457 
00458 static int
00459 mips2_fp_compat (struct frame_info *frame)
00460 {
00461   struct gdbarch *gdbarch = get_frame_arch (frame);
00462   /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
00463      meaningful.  */
00464   if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
00465     return 0;
00466 
00467 #if 0
00468   /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
00469      in all the places we deal with FP registers.  PR gdb/413.  */
00470   /* Otherwise check the FR bit in the status register - it controls
00471      the FP compatiblity mode.  If it is clear we are in compatibility
00472      mode.  */
00473   if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
00474     return 1;
00475 #endif
00476 
00477   return 0;
00478 }
00479 
00480 #define VM_MIN_ADDRESS (CORE_ADDR)0x400000
00481 
00482 static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);
00483 
00484 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
00485 
00486 /* The list of available "set mips " and "show mips " commands.  */
00487 
00488 static struct cmd_list_element *setmipscmdlist = NULL;
00489 static struct cmd_list_element *showmipscmdlist = NULL;
00490 
00491 /* Integer registers 0 thru 31 are handled explicitly by
00492    mips_register_name().  Processor specific registers 32 and above
00493    are listed in the following tables.  */
00494 
00495 enum
00496 { NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
00497 
00498 /* Generic MIPS.  */
00499 
00500 static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
00501   "sr", "lo", "hi", "bad", "cause", "pc",
00502   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
00503   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
00504   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
00505   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
00506   "fsr", "fir",
00507 };
00508 
00509 /* Names of IDT R3041 registers.  */
00510 
00511 static const char *mips_r3041_reg_names[] = {
00512   "sr", "lo", "hi", "bad", "cause", "pc",
00513   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
00514   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
00515   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
00516   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
00517   "fsr", "fir", "", /*"fp" */ "",
00518   "", "", "bus", "ccfg", "", "", "", "",
00519   "", "", "port", "cmp", "", "", "epc", "prid",
00520 };
00521 
00522 /* Names of tx39 registers.  */
00523 
00524 static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
00525   "sr", "lo", "hi", "bad", "cause", "pc",
00526   "", "", "", "", "", "", "", "",
00527   "", "", "", "", "", "", "", "",
00528   "", "", "", "", "", "", "", "",
00529   "", "", "", "", "", "", "", "",
00530   "", "", "", "",
00531   "", "", "", "", "", "", "", "",
00532   "", "", "config", "cache", "debug", "depc", "epc",
00533 };
00534 
00535 /* Names of IRIX registers.  */
00536 static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
00537   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
00538   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
00539   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
00540   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
00541   "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
00542 };
00543 
00544 /* Names of registers with Linux kernels.  */
00545 static const char *mips_linux_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
00546   "sr", "lo", "hi", "bad", "cause", "pc",
00547   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
00548   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
00549   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
00550   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
00551   "fsr", "fir"
00552 };
00553 
00554 
00555 /* Return the name of the register corresponding to REGNO.  */
00556 static const char *
00557 mips_register_name (struct gdbarch *gdbarch, int regno)
00558 {
00559   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00560   /* GPR names for all ABIs other than n32/n64.  */
00561   static char *mips_gpr_names[] = {
00562     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
00563     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
00564     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
00565     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
00566   };
00567 
00568   /* GPR names for n32 and n64 ABIs.  */
00569   static char *mips_n32_n64_gpr_names[] = {
00570     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
00571     "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
00572     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
00573     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
00574   };
00575 
00576   enum mips_abi abi = mips_abi (gdbarch);
00577 
00578   /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers, 
00579      but then don't make the raw register names visible.  This (upper)
00580      range of user visible register numbers are the pseudo-registers.
00581 
00582      This approach was adopted accommodate the following scenario:
00583      It is possible to debug a 64-bit device using a 32-bit
00584      programming model.  In such instances, the raw registers are
00585      configured to be 64-bits wide, while the pseudo registers are
00586      configured to be 32-bits wide.  The registers that the user
00587      sees - the pseudo registers - match the users expectations
00588      given the programming model being used.  */
00589   int rawnum = regno % gdbarch_num_regs (gdbarch);
00590   if (regno < gdbarch_num_regs (gdbarch))
00591     return "";
00592 
00593   /* The MIPS integer registers are always mapped from 0 to 31.  The
00594      names of the registers (which reflects the conventions regarding
00595      register use) vary depending on the ABI.  */
00596   if (0 <= rawnum && rawnum < 32)
00597     {
00598       if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
00599         return mips_n32_n64_gpr_names[rawnum];
00600       else
00601         return mips_gpr_names[rawnum];
00602     }
00603   else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
00604     return tdesc_register_name (gdbarch, rawnum);
00605   else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
00606     {
00607       gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
00608       if (tdep->mips_processor_reg_names[rawnum - 32])
00609         return tdep->mips_processor_reg_names[rawnum - 32];
00610       return "";
00611     }
00612   else
00613     internal_error (__FILE__, __LINE__,
00614                     _("mips_register_name: bad register number %d"), rawnum);
00615 }
00616 
00617 /* Return the groups that a MIPS register can be categorised into.  */
00618 
00619 static int
00620 mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
00621                           struct reggroup *reggroup)
00622 {
00623   int vector_p;
00624   int float_p;
00625   int raw_p;
00626   int rawnum = regnum % gdbarch_num_regs (gdbarch);
00627   int pseudo = regnum / gdbarch_num_regs (gdbarch);
00628   if (reggroup == all_reggroup)
00629     return pseudo;
00630   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
00631   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
00632   /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
00633      (gdbarch), as not all architectures are multi-arch.  */
00634   raw_p = rawnum < gdbarch_num_regs (gdbarch);
00635   if (gdbarch_register_name (gdbarch, regnum) == NULL
00636       || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
00637     return 0;
00638   if (reggroup == float_reggroup)
00639     return float_p && pseudo;
00640   if (reggroup == vector_reggroup)
00641     return vector_p && pseudo;
00642   if (reggroup == general_reggroup)
00643     return (!vector_p && !float_p) && pseudo;
00644   /* Save the pseudo registers.  Need to make certain that any code
00645      extracting register values from a saved register cache also uses
00646      pseudo registers.  */
00647   if (reggroup == save_reggroup)
00648     return raw_p && pseudo;
00649   /* Restore the same pseudo register.  */
00650   if (reggroup == restore_reggroup)
00651     return raw_p && pseudo;
00652   return 0;
00653 }
00654 
00655 /* Return the groups that a MIPS register can be categorised into.
00656    This version is only used if we have a target description which
00657    describes real registers (and their groups).  */
00658 
00659 static int
00660 mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
00661                                 struct reggroup *reggroup)
00662 {
00663   int rawnum = regnum % gdbarch_num_regs (gdbarch);
00664   int pseudo = regnum / gdbarch_num_regs (gdbarch);
00665   int ret;
00666 
00667   /* Only save, restore, and display the pseudo registers.  Need to
00668      make certain that any code extracting register values from a
00669      saved register cache also uses pseudo registers.
00670 
00671      Note: saving and restoring the pseudo registers is slightly
00672      strange; if we have 64 bits, we should save and restore all
00673      64 bits.  But this is hard and has little benefit.  */
00674   if (!pseudo)
00675     return 0;
00676 
00677   ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
00678   if (ret != -1)
00679     return ret;
00680 
00681   return mips_register_reggroup_p (gdbarch, regnum, reggroup);
00682 }
00683 
00684 /* Map the symbol table registers which live in the range [1 *
00685    gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
00686    registers.  Take care of alignment and size problems.  */
00687 
00688 static enum register_status
00689 mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
00690                            int cookednum, gdb_byte *buf)
00691 {
00692   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
00693   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
00694               && cookednum < 2 * gdbarch_num_regs (gdbarch));
00695   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
00696     return regcache_raw_read (regcache, rawnum, buf);
00697   else if (register_size (gdbarch, rawnum) >
00698            register_size (gdbarch, cookednum))
00699     {
00700       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
00701         return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
00702       else
00703         {
00704           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00705           LONGEST regval;
00706           enum register_status status;
00707 
00708           status = regcache_raw_read_signed (regcache, rawnum, &regval);
00709           if (status == REG_VALID)
00710             store_signed_integer (buf, 4, byte_order, regval);
00711           return status;
00712         }
00713     }
00714   else
00715     internal_error (__FILE__, __LINE__, _("bad register size"));
00716 }
00717 
00718 static void
00719 mips_pseudo_register_write (struct gdbarch *gdbarch,
00720                             struct regcache *regcache, int cookednum,
00721                             const gdb_byte *buf)
00722 {
00723   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
00724   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
00725               && cookednum < 2 * gdbarch_num_regs (gdbarch));
00726   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
00727     regcache_raw_write (regcache, rawnum, buf);
00728   else if (register_size (gdbarch, rawnum) >
00729            register_size (gdbarch, cookednum))
00730     {
00731       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
00732         regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
00733       else
00734         {
00735           /* Sign extend the shortened version of the register prior
00736              to placing it in the raw register.  This is required for
00737              some mips64 parts in order to avoid unpredictable behavior.  */
00738           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00739           LONGEST regval = extract_signed_integer (buf, 4, byte_order);
00740           regcache_raw_write_signed (regcache, rawnum, regval);
00741         }
00742     }
00743   else
00744     internal_error (__FILE__, __LINE__, _("bad register size"));
00745 }
00746 
00747 static int
00748 mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
00749                                  struct agent_expr *ax, int reg)
00750 {
00751   int rawnum = reg % gdbarch_num_regs (gdbarch);
00752   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
00753               && reg < 2 * gdbarch_num_regs (gdbarch));
00754 
00755   ax_reg_mask (ax, rawnum);
00756 
00757   return 0;
00758 }
00759 
00760 static int
00761 mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
00762                                     struct agent_expr *ax, int reg)
00763 {
00764   int rawnum = reg % gdbarch_num_regs (gdbarch);
00765   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
00766               && reg < 2 * gdbarch_num_regs (gdbarch));
00767   if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
00768     {
00769       ax_reg (ax, rawnum);
00770 
00771       if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
00772         {
00773           if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
00774               || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
00775             {
00776               ax_const_l (ax, 32);
00777               ax_simple (ax, aop_lsh);
00778             }
00779           ax_const_l (ax, 32);
00780           ax_simple (ax, aop_rsh_signed);
00781         }
00782     }
00783   else
00784     internal_error (__FILE__, __LINE__, _("bad register size"));
00785 
00786   return 0;
00787 }
00788 
00789 /* Table to translate 3-bit register field to actual register number.  */
00790 static const signed char mips_reg3_to_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
00791 
00792 /* Heuristic_proc_start may hunt through the text section for a long
00793    time across a 2400 baud serial line.  Allows the user to limit this
00794    search.  */
00795 
00796 static int heuristic_fence_post = 0;
00797 
00798 /* Number of bytes of storage in the actual machine representation for
00799    register N.  NOTE: This defines the pseudo register type so need to
00800    rebuild the architecture vector.  */
00801 
00802 static int mips64_transfers_32bit_regs_p = 0;
00803 
00804 static void
00805 set_mips64_transfers_32bit_regs (char *args, int from_tty,
00806                                  struct cmd_list_element *c)
00807 {
00808   struct gdbarch_info info;
00809   gdbarch_info_init (&info);
00810   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
00811      instead of relying on globals.  Doing that would let generic code
00812      handle the search for this specific architecture.  */
00813   if (!gdbarch_update_p (info))
00814     {
00815       mips64_transfers_32bit_regs_p = 0;
00816       error (_("32-bit compatibility mode not supported"));
00817     }
00818 }
00819 
00820 /* Convert to/from a register and the corresponding memory value.  */
00821 
00822 /* This predicate tests for the case of an 8 byte floating point
00823    value that is being transferred to or from a pair of floating point
00824    registers each of which are (or are considered to be) only 4 bytes
00825    wide.  */
00826 static int
00827 mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
00828                                     struct type *type)
00829 {
00830   return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
00831           && register_size (gdbarch, regnum) == 4
00832           && mips_float_register_p (gdbarch, regnum)
00833           && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
00834 }
00835 
00836 /* This predicate tests for the case of a value of less than 8
00837    bytes in width that is being transfered to or from an 8 byte
00838    general purpose register.  */
00839 static int
00840 mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
00841                                     struct type *type)
00842 {
00843   int num_regs = gdbarch_num_regs (gdbarch);
00844 
00845   return (register_size (gdbarch, regnum) == 8
00846           && regnum % num_regs > 0 && regnum % num_regs < 32
00847           && TYPE_LENGTH (type) < 8);
00848 }
00849 
00850 static int
00851 mips_convert_register_p (struct gdbarch *gdbarch,
00852                          int regnum, struct type *type)
00853 {
00854   return (mips_convert_register_float_case_p (gdbarch, regnum, type)
00855           || mips_convert_register_gpreg_case_p (gdbarch, regnum, type));
00856 }
00857 
00858 static int
00859 mips_register_to_value (struct frame_info *frame, int regnum,
00860                         struct type *type, gdb_byte *to,
00861                         int *optimizedp, int *unavailablep)
00862 {
00863   struct gdbarch *gdbarch = get_frame_arch (frame);
00864 
00865   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
00866     {
00867       get_frame_register (frame, regnum + 0, to + 4);
00868       get_frame_register (frame, regnum + 1, to + 0);
00869 
00870       if (!get_frame_register_bytes (frame, regnum + 0, 0, 4, to + 4,
00871                                      optimizedp, unavailablep))
00872         return 0;
00873 
00874       if (!get_frame_register_bytes (frame, regnum + 1, 0, 4, to + 0,
00875                                      optimizedp, unavailablep))
00876         return 0;
00877       *optimizedp = *unavailablep = 0;
00878       return 1;
00879     }
00880   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
00881     {
00882       int len = TYPE_LENGTH (type);
00883       CORE_ADDR offset;
00884 
00885       offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 - len : 0;
00886       if (!get_frame_register_bytes (frame, regnum, offset, len, to,
00887                                      optimizedp, unavailablep))
00888         return 0;
00889 
00890       *optimizedp = *unavailablep = 0;
00891       return 1;
00892     }
00893   else
00894     {
00895       internal_error (__FILE__, __LINE__,
00896                       _("mips_register_to_value: unrecognized case"));
00897     }
00898 }
00899 
00900 static void
00901 mips_value_to_register (struct frame_info *frame, int regnum,
00902                         struct type *type, const gdb_byte *from)
00903 {
00904   struct gdbarch *gdbarch = get_frame_arch (frame);
00905 
00906   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
00907     {
00908       put_frame_register (frame, regnum + 0, from + 4);
00909       put_frame_register (frame, regnum + 1, from + 0);
00910     }
00911   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
00912     {
00913       gdb_byte fill[8];
00914       int len = TYPE_LENGTH (type);
00915       
00916       /* Sign extend values, irrespective of type, that are stored to 
00917          a 64-bit general purpose register.  (32-bit unsigned values
00918          are stored as signed quantities within a 64-bit register.
00919          When performing an operation, in compiled code, that combines
00920          a 32-bit unsigned value with a signed 64-bit value, a type
00921          conversion is first performed that zeroes out the high 32 bits.)  */
00922       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
00923         {
00924           if (from[0] & 0x80)
00925             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
00926           else
00927             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
00928           put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
00929           put_frame_register_bytes (frame, regnum, 8 - len, len, from);
00930         }
00931       else
00932         {
00933           if (from[len-1] & 0x80)
00934             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
00935           else
00936             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
00937           put_frame_register_bytes (frame, regnum, 0, len, from);
00938           put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
00939         }
00940     }
00941   else
00942     {
00943       internal_error (__FILE__, __LINE__,
00944                       _("mips_value_to_register: unrecognized case"));
00945     }
00946 }
00947 
00948 /* Return the GDB type object for the "standard" data type of data in
00949    register REG.  */
00950 
00951 static struct type *
00952 mips_register_type (struct gdbarch *gdbarch, int regnum)
00953 {
00954   gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
00955   if (mips_float_register_p (gdbarch, regnum))
00956     {
00957       /* The floating-point registers raw, or cooked, always match
00958          mips_isa_regsize(), and also map 1:1, byte for byte.  */
00959       if (mips_isa_regsize (gdbarch) == 4)
00960         return builtin_type (gdbarch)->builtin_float;
00961       else
00962         return builtin_type (gdbarch)->builtin_double;
00963     }
00964   else if (regnum < gdbarch_num_regs (gdbarch))
00965     {
00966       /* The raw or ISA registers.  These are all sized according to
00967          the ISA regsize.  */
00968       if (mips_isa_regsize (gdbarch) == 4)
00969         return builtin_type (gdbarch)->builtin_int32;
00970       else
00971         return builtin_type (gdbarch)->builtin_int64;
00972     }
00973   else
00974     {
00975       int rawnum = regnum - gdbarch_num_regs (gdbarch);
00976 
00977       /* The cooked or ABI registers.  These are sized according to
00978          the ABI (with a few complications).  */
00979       if (rawnum == mips_regnum (gdbarch)->fp_control_status
00980           || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
00981         return builtin_type (gdbarch)->builtin_int32;
00982       else if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
00983                && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
00984                && rawnum >= MIPS_FIRST_EMBED_REGNUM
00985                && rawnum <= MIPS_LAST_EMBED_REGNUM)
00986         /* The pseudo/cooked view of the embedded registers is always
00987            32-bit.  The raw view is handled below.  */
00988         return builtin_type (gdbarch)->builtin_int32;
00989       else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
00990         /* The target, while possibly using a 64-bit register buffer,
00991            is only transfering 32-bits of each integer register.
00992            Reflect this in the cooked/pseudo (ABI) register value.  */
00993         return builtin_type (gdbarch)->builtin_int32;
00994       else if (mips_abi_regsize (gdbarch) == 4)
00995         /* The ABI is restricted to 32-bit registers (the ISA could be
00996            32- or 64-bit).  */
00997         return builtin_type (gdbarch)->builtin_int32;
00998       else
00999         /* 64-bit ABI.  */
01000         return builtin_type (gdbarch)->builtin_int64;
01001     }
01002 }
01003 
01004 /* Return the GDB type for the pseudo register REGNUM, which is the
01005    ABI-level view.  This function is only called if there is a target
01006    description which includes registers, so we know precisely the
01007    types of hardware registers.  */
01008 
01009 static struct type *
01010 mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
01011 {
01012   const int num_regs = gdbarch_num_regs (gdbarch);
01013   int rawnum = regnum % num_regs;
01014   struct type *rawtype;
01015 
01016   gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);
01017 
01018   /* Absent registers are still absent.  */
01019   rawtype = gdbarch_register_type (gdbarch, rawnum);
01020   if (TYPE_LENGTH (rawtype) == 0)
01021     return rawtype;
01022 
01023   if (mips_float_register_p (gdbarch, rawnum))
01024     /* Present the floating point registers however the hardware did;
01025        do not try to convert between FPU layouts.  */
01026     return rawtype;
01027 
01028   /* Use pointer types for registers if we can.  For n32 we can not,
01029      since we do not have a 64-bit pointer type.  */
01030   if (mips_abi_regsize (gdbarch)
01031       == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
01032     {
01033       if (rawnum == MIPS_SP_REGNUM
01034           || rawnum == mips_regnum (gdbarch)->badvaddr)
01035         return builtin_type (gdbarch)->builtin_data_ptr;
01036       else if (rawnum == mips_regnum (gdbarch)->pc)
01037         return builtin_type (gdbarch)->builtin_func_ptr;
01038     }
01039 
01040   if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
01041       && ((rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_PS_REGNUM)
01042           || rawnum == mips_regnum (gdbarch)->lo
01043           || rawnum == mips_regnum (gdbarch)->hi
01044           || rawnum == mips_regnum (gdbarch)->badvaddr
01045           || rawnum == mips_regnum (gdbarch)->cause
01046           || rawnum == mips_regnum (gdbarch)->pc
01047           || (mips_regnum (gdbarch)->dspacc != -1
01048               && rawnum >= mips_regnum (gdbarch)->dspacc
01049               && rawnum < mips_regnum (gdbarch)->dspacc + 6)))
01050     return builtin_type (gdbarch)->builtin_int32;
01051 
01052   if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
01053       && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
01054       && rawnum >= MIPS_EMBED_FP0_REGNUM + 32
01055       && rawnum <= MIPS_LAST_EMBED_REGNUM)
01056     {
01057       /* The pseudo/cooked view of embedded registers is always
01058          32-bit, even if the target transfers 64-bit values for them.
01059          New targets relying on XML descriptions should only transfer
01060          the necessary 32 bits, but older versions of GDB expected 64,
01061          so allow the target to provide 64 bits without interfering
01062          with the displayed type.  */
01063       return builtin_type (gdbarch)->builtin_int32;
01064     }
01065 
01066   /* For all other registers, pass through the hardware type.  */
01067   return rawtype;
01068 }
01069 
01070 /* Should the upper word of 64-bit addresses be zeroed?  */
01071 enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
01072 
01073 static int
01074 mips_mask_address_p (struct gdbarch_tdep *tdep)
01075 {
01076   switch (mask_address_var)
01077     {
01078     case AUTO_BOOLEAN_TRUE:
01079       return 1;
01080     case AUTO_BOOLEAN_FALSE:
01081       return 0;
01082       break;
01083     case AUTO_BOOLEAN_AUTO:
01084       return tdep->default_mask_address_p;
01085     default:
01086       internal_error (__FILE__, __LINE__,
01087                       _("mips_mask_address_p: bad switch"));
01088       return -1;
01089     }
01090 }
01091 
01092 static void
01093 show_mask_address (struct ui_file *file, int from_tty,
01094                    struct cmd_list_element *c, const char *value)
01095 {
01096   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
01097 
01098   deprecated_show_value_hack (file, from_tty, c, value);
01099   switch (mask_address_var)
01100     {
01101     case AUTO_BOOLEAN_TRUE:
01102       printf_filtered ("The 32 bit mips address mask is enabled\n");
01103       break;
01104     case AUTO_BOOLEAN_FALSE:
01105       printf_filtered ("The 32 bit mips address mask is disabled\n");
01106       break;
01107     case AUTO_BOOLEAN_AUTO:
01108       printf_filtered
01109         ("The 32 bit address mask is set automatically.  Currently %s\n",
01110          mips_mask_address_p (tdep) ? "enabled" : "disabled");
01111       break;
01112     default:
01113       internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
01114       break;
01115     }
01116 }
01117 
01118 /* Tell if the program counter value in MEMADDR is in a standard ISA
01119    function.  */
01120 
01121 int
01122 mips_pc_is_mips (CORE_ADDR memaddr)
01123 {
01124   struct bound_minimal_symbol sym;
01125 
01126   /* Flags indicating that this is a MIPS16 or microMIPS function is
01127      stored by elfread.c in the high bit of the info field.  Use this
01128      to decide if the function is standard MIPS.  Otherwise if bit 0
01129      of the address is clear, then this is a standard MIPS function.  */
01130   sym = lookup_minimal_symbol_by_pc (memaddr);
01131   if (sym.minsym)
01132     return msymbol_is_mips (sym.minsym);
01133   else
01134     return is_mips_addr (memaddr);
01135 }
01136 
01137 /* Tell if the program counter value in MEMADDR is in a MIPS16 function.  */
01138 
01139 int
01140 mips_pc_is_mips16 (struct gdbarch *gdbarch, CORE_ADDR memaddr)
01141 {
01142   struct bound_minimal_symbol sym;
01143 
01144   /* A flag indicating that this is a MIPS16 function is stored by
01145      elfread.c in the high bit of the info field.  Use this to decide
01146      if the function is MIPS16.  Otherwise if bit 0 of the address is
01147      set, then ELF file flags will tell if this is a MIPS16 function.  */
01148   sym = lookup_minimal_symbol_by_pc (memaddr);
01149   if (sym.minsym)
01150     return msymbol_is_mips16 (sym.minsym);
01151   else
01152     return is_mips16_addr (gdbarch, memaddr);
01153 }
01154 
01155 /* Tell if the program counter value in MEMADDR is in a microMIPS function.  */
01156 
01157 int
01158 mips_pc_is_micromips (struct gdbarch *gdbarch, CORE_ADDR memaddr)
01159 {
01160   struct bound_minimal_symbol sym;
01161 
01162   /* A flag indicating that this is a microMIPS function is stored by
01163      elfread.c in the high bit of the info field.  Use this to decide
01164      if the function is microMIPS.  Otherwise if bit 0 of the address
01165      is set, then ELF file flags will tell if this is a microMIPS
01166      function.  */
01167   sym = lookup_minimal_symbol_by_pc (memaddr);
01168   if (sym.minsym)
01169     return msymbol_is_micromips (sym.minsym);
01170   else
01171     return is_micromips_addr (gdbarch, memaddr);
01172 }
01173 
01174 /* Tell the ISA type of the function the program counter value in MEMADDR
01175    is in.  */
01176 
01177 static enum mips_isa
01178 mips_pc_isa (struct gdbarch *gdbarch, CORE_ADDR memaddr)
01179 {
01180   struct bound_minimal_symbol sym;
01181 
01182   /* A flag indicating that this is a MIPS16 or a microMIPS function
01183      is stored by elfread.c in the high bit of the info field.  Use
01184      this to decide if the function is MIPS16 or microMIPS or normal
01185      MIPS.  Otherwise if bit 0 of the address is set, then ELF file
01186      flags will tell if this is a MIPS16 or a microMIPS function.  */
01187   sym = lookup_minimal_symbol_by_pc (memaddr);
01188   if (sym.minsym)
01189     {
01190       if (msymbol_is_micromips (sym.minsym))
01191         return ISA_MICROMIPS;
01192       else if (msymbol_is_mips16 (sym.minsym))
01193         return ISA_MIPS16;
01194       else
01195         return ISA_MIPS;
01196     }
01197   else
01198     {
01199       if (is_mips_addr (memaddr))
01200         return ISA_MIPS;
01201       else if (is_micromips_addr (gdbarch, memaddr))
01202         return ISA_MICROMIPS;
01203       else
01204         return ISA_MIPS16;
01205     }
01206 }
01207 
01208 /* Various MIPS16 thunk (aka stub or trampoline) names.  */
01209 
01210 static const char mips_str_mips16_call_stub[] = "__mips16_call_stub_";
01211 static const char mips_str_mips16_ret_stub[] = "__mips16_ret_";
01212 static const char mips_str_call_fp_stub[] = "__call_stub_fp_";
01213 static const char mips_str_call_stub[] = "__call_stub_";
01214 static const char mips_str_fn_stub[] = "__fn_stub_";
01215 
01216 /* This is used as a PIC thunk prefix.  */
01217 
01218 static const char mips_str_pic[] = ".pic.";
01219 
01220 /* Return non-zero if the PC is inside a call thunk (aka stub or
01221    trampoline) that should be treated as a temporary frame.  */
01222 
01223 static int
01224 mips_in_frame_stub (CORE_ADDR pc)
01225 {
01226   CORE_ADDR start_addr;
01227   const char *name;
01228 
01229   /* Find the starting address of the function containing the PC.  */
01230   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
01231     return 0;
01232 
01233   /* If the PC is in __mips16_call_stub_*, this is a call/return stub.  */
01234   if (strncmp (name, mips_str_mips16_call_stub,
01235                strlen (mips_str_mips16_call_stub)) == 0)
01236     return 1;
01237   /* If the PC is in __call_stub_*, this is a call/return or a call stub.  */
01238   if (strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
01239     return 1;
01240   /* If the PC is in __fn_stub_*, this is a call stub.  */
01241   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0)
01242     return 1;
01243 
01244   return 0;                     /* Not a stub.  */
01245 }
01246 
01247 /* MIPS believes that the PC has a sign extended value.  Perhaps the
01248    all registers should be sign extended for simplicity?  */
01249 
01250 static CORE_ADDR
01251 mips_read_pc (struct regcache *regcache)
01252 {
01253   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
01254   LONGEST pc;
01255 
01256   regcache_cooked_read_signed (regcache, regnum, &pc);
01257   if (is_compact_addr (pc))
01258     pc = unmake_compact_addr (pc);
01259   return pc;
01260 }
01261 
01262 static CORE_ADDR
01263 mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
01264 {
01265   CORE_ADDR pc;
01266 
01267   pc = frame_unwind_register_signed (next_frame, gdbarch_pc_regnum (gdbarch));
01268   if (is_compact_addr (pc))
01269     pc = unmake_compact_addr (pc);
01270   /* macro/2012-04-20: This hack skips over MIPS16 call thunks as
01271      intermediate frames.  In this case we can get the caller's address
01272      from $ra, or if $ra contains an address within a thunk as well, then
01273      it must be in the return path of __mips16_call_stub_{s,d}{f,c}_{0..10}
01274      and thus the caller's address is in $s2.  */
01275   if (frame_relative_level (next_frame) >= 0 && mips_in_frame_stub (pc))
01276     {
01277       pc = frame_unwind_register_signed
01278              (next_frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
01279       if (is_compact_addr (pc))
01280         pc = unmake_compact_addr (pc);
01281       if (mips_in_frame_stub (pc))
01282         {
01283           pc = frame_unwind_register_signed
01284                  (next_frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
01285           if (is_compact_addr (pc))
01286             pc = unmake_compact_addr (pc);
01287         }
01288     }
01289   return pc;
01290 }
01291 
01292 static CORE_ADDR
01293 mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
01294 {
01295   return frame_unwind_register_signed
01296            (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
01297 }
01298 
01299 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
01300    dummy frame.  The frame ID's base needs to match the TOS value
01301    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
01302    breakpoint.  */
01303 
01304 static struct frame_id
01305 mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
01306 {
01307   return frame_id_build
01308            (get_frame_register_signed (this_frame,
01309                                        gdbarch_num_regs (gdbarch)
01310                                        + MIPS_SP_REGNUM),
01311             get_frame_pc (this_frame));
01312 }
01313 
01314 /* Implement the "write_pc" gdbarch method.  */
01315 
01316 void
01317 mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
01318 {
01319   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
01320 
01321   if (mips_pc_is_mips (pc))
01322     regcache_cooked_write_unsigned (regcache, regnum, pc);
01323   else
01324     regcache_cooked_write_unsigned (regcache, regnum, make_compact_addr (pc));
01325 }
01326 
01327 /* Fetch and return instruction from the specified location.  Handle
01328    MIPS16/microMIPS as appropriate.  */
01329 
01330 static ULONGEST
01331 mips_fetch_instruction (struct gdbarch *gdbarch,
01332                         enum mips_isa isa, CORE_ADDR addr, int *statusp)
01333 {
01334   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01335   gdb_byte buf[MIPS_INSN32_SIZE];
01336   int instlen;
01337   int status;
01338 
01339   switch (isa)
01340     {
01341     case ISA_MICROMIPS:
01342     case ISA_MIPS16:
01343       instlen = MIPS_INSN16_SIZE;
01344       addr = unmake_compact_addr (addr);
01345       break;
01346     case ISA_MIPS:
01347       instlen = MIPS_INSN32_SIZE;
01348       break;
01349     default:
01350       internal_error (__FILE__, __LINE__, _("invalid ISA"));
01351       break;
01352     }
01353   status = target_read_memory (addr, buf, instlen);
01354   if (statusp != NULL)
01355     *statusp = status;
01356   if (status)
01357     {
01358       if (statusp == NULL)
01359         memory_error (status, addr);
01360       return 0;
01361     }
01362   return extract_unsigned_integer (buf, instlen, byte_order);
01363 }
01364 
01365 /* These are the fields of 32 bit mips instructions.  */
01366 #define mips32_op(x) (x >> 26)
01367 #define itype_op(x) (x >> 26)
01368 #define itype_rs(x) ((x >> 21) & 0x1f)
01369 #define itype_rt(x) ((x >> 16) & 0x1f)
01370 #define itype_immediate(x) (x & 0xffff)
01371 
01372 #define jtype_op(x) (x >> 26)
01373 #define jtype_target(x) (x & 0x03ffffff)
01374 
01375 #define rtype_op(x) (x >> 26)
01376 #define rtype_rs(x) ((x >> 21) & 0x1f)
01377 #define rtype_rt(x) ((x >> 16) & 0x1f)
01378 #define rtype_rd(x) ((x >> 11) & 0x1f)
01379 #define rtype_shamt(x) ((x >> 6) & 0x1f)
01380 #define rtype_funct(x) (x & 0x3f)
01381 
01382 /* MicroMIPS instruction fields.  */
01383 #define micromips_op(x) ((x) >> 10)
01384 
01385 /* 16-bit/32-bit-high-part instruction formats, B and S refer to the lowest
01386    bit and the size respectively of the field extracted.  */
01387 #define b0s4_imm(x) ((x) & 0xf)
01388 #define b0s5_imm(x) ((x) & 0x1f)
01389 #define b0s5_reg(x) ((x) & 0x1f)
01390 #define b0s7_imm(x) ((x) & 0x7f)
01391 #define b0s10_imm(x) ((x) & 0x3ff)
01392 #define b1s4_imm(x) (((x) >> 1) & 0xf)
01393 #define b1s9_imm(x) (((x) >> 1) & 0x1ff)
01394 #define b2s3_cc(x) (((x) >> 2) & 0x7)
01395 #define b4s2_regl(x) (((x) >> 4) & 0x3)
01396 #define b5s5_op(x) (((x) >> 5) & 0x1f)
01397 #define b5s5_reg(x) (((x) >> 5) & 0x1f)
01398 #define b6s4_op(x) (((x) >> 6) & 0xf)
01399 #define b7s3_reg(x) (((x) >> 7) & 0x7)
01400 
01401 /* 32-bit instruction formats, B and S refer to the lowest bit and the size
01402    respectively of the field extracted.  */
01403 #define b0s6_op(x) ((x) & 0x3f)
01404 #define b0s11_op(x) ((x) & 0x7ff)
01405 #define b0s12_imm(x) ((x) & 0xfff)
01406 #define b0s16_imm(x) ((x) & 0xffff)
01407 #define b0s26_imm(x) ((x) & 0x3ffffff)
01408 #define b6s10_ext(x) (((x) >> 6) & 0x3ff)
01409 #define b11s5_reg(x) (((x) >> 11) & 0x1f)
01410 #define b12s4_op(x) (((x) >> 12) & 0xf)
01411 
01412 /* Return the size in bytes of the instruction INSN encoded in the ISA
01413    instruction set.  */
01414 
01415 static int
01416 mips_insn_size (enum mips_isa isa, ULONGEST insn)
01417 {
01418   switch (isa)
01419     {
01420     case ISA_MICROMIPS:
01421       if (micromips_op (insn) == 0x1f)
01422         return 3 * MIPS_INSN16_SIZE;
01423       else if (((micromips_op (insn) & 0x4) == 0x4)
01424                || ((micromips_op (insn) & 0x7) == 0x0))
01425         return 2 * MIPS_INSN16_SIZE;
01426       else
01427         return MIPS_INSN16_SIZE;
01428     case ISA_MIPS16:
01429       if ((insn & 0xf800) == 0xf000)
01430         return 2 * MIPS_INSN16_SIZE;
01431       else
01432         return MIPS_INSN16_SIZE;
01433     case ISA_MIPS:
01434         return MIPS_INSN32_SIZE;
01435     }
01436   internal_error (__FILE__, __LINE__, _("invalid ISA"));
01437 }
01438 
01439 static LONGEST
01440 mips32_relative_offset (ULONGEST inst)
01441 {
01442   return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
01443 }
01444 
01445 /* Determine the address of the next instruction executed after the INST
01446    floating condition branch instruction at PC.  COUNT specifies the
01447    number of the floating condition bits tested by the branch.  */
01448 
01449 static CORE_ADDR
01450 mips32_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
01451                ULONGEST inst, CORE_ADDR pc, int count)
01452 {
01453   int fcsr = mips_regnum (gdbarch)->fp_control_status;
01454   int cnum = (itype_rt (inst) >> 2) & (count - 1);
01455   int tf = itype_rt (inst) & 1;
01456   int mask = (1 << count) - 1;
01457   ULONGEST fcs;
01458   int cond;
01459 
01460   if (fcsr == -1)
01461     /* No way to handle; it'll most likely trap anyway.  */
01462     return pc;
01463 
01464   fcs = get_frame_register_unsigned (frame, fcsr);
01465   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
01466 
01467   if (((cond >> cnum) & mask) != mask * !tf)
01468     pc += mips32_relative_offset (inst);
01469   else
01470     pc += 4;
01471 
01472   return pc;
01473 }
01474 
01475 /* Return nonzero if the gdbarch is an Octeon series.  */
01476 
01477 static int
01478 is_octeon (struct gdbarch *gdbarch)
01479 {
01480   const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
01481 
01482   return (info->mach == bfd_mach_mips_octeon
01483          || info->mach == bfd_mach_mips_octeonp
01484          || info->mach == bfd_mach_mips_octeon2);
01485 }
01486 
01487 /* Return true if the OP represents the Octeon's BBIT instruction.  */
01488 
01489 static int
01490 is_octeon_bbit_op (int op, struct gdbarch *gdbarch)
01491 {
01492   if (!is_octeon (gdbarch))
01493     return 0;
01494   /* BBIT0 is encoded as LWC2: 110 010.  */
01495   /* BBIT032 is encoded as LDC2: 110 110.  */
01496   /* BBIT1 is encoded as SWC2: 111 010.  */
01497   /* BBIT132 is encoded as SDC2: 111 110.  */
01498   if (op == 50 || op == 54 || op == 58 || op == 62)
01499     return 1;
01500   return 0;
01501 }
01502 
01503 
01504 /* Determine where to set a single step breakpoint while considering
01505    branch prediction.  */
01506 
01507 static CORE_ADDR
01508 mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
01509 {
01510   struct gdbarch *gdbarch = get_frame_arch (frame);
01511   unsigned long inst;
01512   int op;
01513   inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
01514   op = itype_op (inst);
01515   if ((inst & 0xe0000000) != 0)         /* Not a special, jump or branch
01516                                            instruction.  */
01517     {
01518       if (op >> 2 == 5)
01519         /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
01520         {
01521           switch (op & 0x03)
01522             {
01523             case 0:             /* BEQL */
01524               goto equal_branch;
01525             case 1:             /* BNEL */
01526               goto neq_branch;
01527             case 2:             /* BLEZL */
01528               goto less_branch;
01529             case 3:             /* BGTZL */
01530               goto greater_branch;
01531             default:
01532               pc += 4;
01533             }
01534         }
01535       else if (op == 17 && itype_rs (inst) == 8)
01536         /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
01537         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 1);
01538       else if (op == 17 && itype_rs (inst) == 9
01539                && (itype_rt (inst) & 2) == 0)
01540         /* BC1ANY2F, BC1ANY2T: 010001 01001 xxx0x */
01541         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 2);
01542       else if (op == 17 && itype_rs (inst) == 10
01543                && (itype_rt (inst) & 2) == 0)
01544         /* BC1ANY4F, BC1ANY4T: 010001 01010 xxx0x */
01545         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 4);
01546       else if (op == 29)
01547         /* JALX: 011101 */
01548         /* The new PC will be alternate mode.  */
01549         {
01550           unsigned long reg;
01551 
01552           reg = jtype_target (inst) << 2;
01553           /* Add 1 to indicate 16-bit mode -- invert ISA mode.  */
01554           pc = ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + reg + 1;
01555         }
01556       else if (is_octeon_bbit_op (op, gdbarch))
01557         {
01558           int bit, branch_if;
01559 
01560           branch_if = op == 58 || op == 62;
01561           bit = itype_rt (inst);
01562 
01563           /* Take into account the *32 instructions.  */
01564           if (op == 54 || op == 62)
01565             bit += 32;
01566 
01567           if (((get_frame_register_signed (frame,
01568                                            itype_rs (inst)) >> bit) & 1)
01569               == branch_if)
01570             pc += mips32_relative_offset (inst) + 4;
01571           else
01572             pc += 8;        /* After the delay slot.  */
01573         }
01574 
01575       else
01576         pc += 4;                /* Not a branch, next instruction is easy.  */
01577     }
01578   else
01579     {                           /* This gets way messy.  */
01580 
01581       /* Further subdivide into SPECIAL, REGIMM and other.  */
01582       switch (op & 0x07)        /* Extract bits 28,27,26.  */
01583         {
01584         case 0:         /* SPECIAL */
01585           op = rtype_funct (inst);
01586           switch (op)
01587             {
01588             case 8:             /* JR */
01589             case 9:             /* JALR */
01590               /* Set PC to that address.  */
01591               pc = get_frame_register_signed (frame, rtype_rs (inst));
01592               break;
01593             case 12:            /* SYSCALL */
01594               {
01595                 struct gdbarch_tdep *tdep;
01596 
01597                 tdep = gdbarch_tdep (get_frame_arch (frame));
01598                 if (tdep->syscall_next_pc != NULL)
01599                   pc = tdep->syscall_next_pc (frame);
01600                 else
01601                   pc += 4;
01602               }
01603               break;
01604             default:
01605               pc += 4;
01606             }
01607 
01608           break;                /* end SPECIAL */
01609         case 1:                 /* REGIMM */
01610           {
01611             op = itype_rt (inst);       /* branch condition */
01612             switch (op)
01613               {
01614               case 0:           /* BLTZ */
01615               case 2:           /* BLTZL */
01616               case 16:          /* BLTZAL */
01617               case 18:          /* BLTZALL */
01618               less_branch:
01619                 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
01620                   pc += mips32_relative_offset (inst) + 4;
01621                 else
01622                   pc += 8;      /* after the delay slot */
01623                 break;
01624               case 1:           /* BGEZ */
01625               case 3:           /* BGEZL */
01626               case 17:          /* BGEZAL */
01627               case 19:          /* BGEZALL */
01628                 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
01629                   pc += mips32_relative_offset (inst) + 4;
01630                 else
01631                   pc += 8;      /* after the delay slot */
01632                 break;
01633               case 0x1c:        /* BPOSGE32 */
01634               case 0x1e:        /* BPOSGE64 */
01635                 pc += 4;
01636                 if (itype_rs (inst) == 0)
01637                   {
01638                     unsigned int pos = (op & 2) ? 64 : 32;
01639                     int dspctl = mips_regnum (gdbarch)->dspctl;
01640 
01641                     if (dspctl == -1)
01642                       /* No way to handle; it'll most likely trap anyway.  */
01643                       break;
01644 
01645                     if ((get_frame_register_unsigned (frame,
01646                                                       dspctl) & 0x7f) >= pos)
01647                       pc += mips32_relative_offset (inst);
01648                     else
01649                       pc += 4;
01650                   }
01651                 break;
01652                 /* All of the other instructions in the REGIMM category */
01653               default:
01654                 pc += 4;
01655               }
01656           }
01657           break;                /* end REGIMM */
01658         case 2:         /* J */
01659         case 3:         /* JAL */
01660           {
01661             unsigned long reg;
01662             reg = jtype_target (inst) << 2;
01663             /* Upper four bits get never changed...  */
01664             pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
01665           }
01666           break;
01667         case 4:         /* BEQ, BEQL */
01668         equal_branch:
01669           if (get_frame_register_signed (frame, itype_rs (inst)) ==
01670               get_frame_register_signed (frame, itype_rt (inst)))
01671             pc += mips32_relative_offset (inst) + 4;
01672           else
01673             pc += 8;
01674           break;
01675         case 5:         /* BNE, BNEL */
01676         neq_branch:
01677           if (get_frame_register_signed (frame, itype_rs (inst)) !=
01678               get_frame_register_signed (frame, itype_rt (inst)))
01679             pc += mips32_relative_offset (inst) + 4;
01680           else
01681             pc += 8;
01682           break;
01683         case 6:         /* BLEZ, BLEZL */
01684           if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
01685             pc += mips32_relative_offset (inst) + 4;
01686           else
01687             pc += 8;
01688           break;
01689         case 7:
01690         default:
01691         greater_branch: /* BGTZ, BGTZL */
01692           if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
01693             pc += mips32_relative_offset (inst) + 4;
01694           else
01695             pc += 8;
01696           break;
01697         }                       /* switch */
01698     }                           /* else */
01699   return pc;
01700 }                               /* mips32_next_pc */
01701 
01702 /* Extract the 7-bit signed immediate offset from the microMIPS instruction
01703    INSN.  */
01704 
01705 static LONGEST
01706 micromips_relative_offset7 (ULONGEST insn)
01707 {
01708   return ((b0s7_imm (insn) ^ 0x40) - 0x40) << 1;
01709 }
01710 
01711 /* Extract the 10-bit signed immediate offset from the microMIPS instruction
01712    INSN.  */
01713 
01714 static LONGEST
01715 micromips_relative_offset10 (ULONGEST insn)
01716 {
01717   return ((b0s10_imm (insn) ^ 0x200) - 0x200) << 1;
01718 }
01719 
01720 /* Extract the 16-bit signed immediate offset from the microMIPS instruction
01721    INSN.  */
01722 
01723 static LONGEST
01724 micromips_relative_offset16 (ULONGEST insn)
01725 {
01726   return ((b0s16_imm (insn) ^ 0x8000) - 0x8000) << 1;
01727 }
01728 
01729 /* Return the size in bytes of the microMIPS instruction at the address PC.  */
01730 
01731 static int
01732 micromips_pc_insn_size (struct gdbarch *gdbarch, CORE_ADDR pc)
01733 {
01734   ULONGEST insn;
01735 
01736   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
01737   return mips_insn_size (ISA_MICROMIPS, insn);
01738 }
01739 
01740 /* Calculate the address of the next microMIPS instruction to execute
01741    after the INSN coprocessor 1 conditional branch instruction at the
01742    address PC.  COUNT denotes the number of coprocessor condition bits
01743    examined by the branch.  */
01744 
01745 static CORE_ADDR
01746 micromips_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
01747                   ULONGEST insn, CORE_ADDR pc, int count)
01748 {
01749   int fcsr = mips_regnum (gdbarch)->fp_control_status;
01750   int cnum = b2s3_cc (insn >> 16) & (count - 1);
01751   int tf = b5s5_op (insn >> 16) & 1;
01752   int mask = (1 << count) - 1;
01753   ULONGEST fcs;
01754   int cond;
01755 
01756   if (fcsr == -1)
01757     /* No way to handle; it'll most likely trap anyway.  */
01758     return pc;
01759 
01760   fcs = get_frame_register_unsigned (frame, fcsr);
01761   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
01762 
01763   if (((cond >> cnum) & mask) != mask * !tf)
01764     pc += micromips_relative_offset16 (insn);
01765   else
01766     pc += micromips_pc_insn_size (gdbarch, pc);
01767 
01768   return pc;
01769 }
01770 
01771 /* Calculate the address of the next microMIPS instruction to execute
01772    after the instruction at the address PC.  */
01773 
01774 static CORE_ADDR
01775 micromips_next_pc (struct frame_info *frame, CORE_ADDR pc)
01776 {
01777   struct gdbarch *gdbarch = get_frame_arch (frame);
01778   ULONGEST insn;
01779 
01780   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
01781   pc += MIPS_INSN16_SIZE;
01782   switch (mips_insn_size (ISA_MICROMIPS, insn))
01783     {
01784     /* 48-bit instructions.  */
01785     case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
01786       /* No branch or jump instructions in this category.  */
01787       pc += 2 * MIPS_INSN16_SIZE;
01788       break;
01789 
01790     /* 32-bit instructions.  */
01791     case 2 * MIPS_INSN16_SIZE:
01792       insn <<= 16;
01793       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
01794       pc += MIPS_INSN16_SIZE;
01795       switch (micromips_op (insn >> 16))
01796         {
01797         case 0x00: /* POOL32A: bits 000000 */
01798           if (b0s6_op (insn) == 0x3c
01799                                 /* POOL32Axf: bits 000000 ... 111100 */
01800               && (b6s10_ext (insn) & 0x2bf) == 0x3c)
01801                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
01802                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
01803             pc = get_frame_register_signed (frame, b0s5_reg (insn >> 16));
01804           break;
01805 
01806         case 0x10: /* POOL32I: bits 010000 */
01807           switch (b5s5_op (insn >> 16))
01808             {
01809             case 0x00: /* BLTZ: bits 010000 00000 */
01810             case 0x01: /* BLTZAL: bits 010000 00001 */
01811             case 0x11: /* BLTZALS: bits 010000 10001 */
01812               if (get_frame_register_signed (frame,
01813                                              b0s5_reg (insn >> 16)) < 0)
01814                 pc += micromips_relative_offset16 (insn);
01815               else
01816                 pc += micromips_pc_insn_size (gdbarch, pc);
01817               break;
01818 
01819             case 0x02: /* BGEZ: bits 010000 00010 */
01820             case 0x03: /* BGEZAL: bits 010000 00011 */
01821             case 0x13: /* BGEZALS: bits 010000 10011 */
01822               if (get_frame_register_signed (frame,
01823                                              b0s5_reg (insn >> 16)) >= 0)
01824                 pc += micromips_relative_offset16 (insn);
01825               else
01826                 pc += micromips_pc_insn_size (gdbarch, pc);
01827               break;
01828 
01829             case 0x04: /* BLEZ: bits 010000 00100 */
01830               if (get_frame_register_signed (frame,
01831                                              b0s5_reg (insn >> 16)) <= 0)
01832                 pc += micromips_relative_offset16 (insn);
01833               else
01834                 pc += micromips_pc_insn_size (gdbarch, pc);
01835               break;
01836 
01837             case 0x05: /* BNEZC: bits 010000 00101 */
01838               if (get_frame_register_signed (frame,
01839                                              b0s5_reg (insn >> 16)) != 0)
01840                 pc += micromips_relative_offset16 (insn);
01841               break;
01842 
01843             case 0x06: /* BGTZ: bits 010000 00110 */
01844               if (get_frame_register_signed (frame,
01845                                              b0s5_reg (insn >> 16)) > 0)
01846                 pc += micromips_relative_offset16 (insn);
01847               else
01848                 pc += micromips_pc_insn_size (gdbarch, pc);
01849               break;
01850 
01851             case 0x07: /* BEQZC: bits 010000 00111 */
01852               if (get_frame_register_signed (frame,
01853                                              b0s5_reg (insn >> 16)) == 0)
01854                 pc += micromips_relative_offset16 (insn);
01855               break;
01856 
01857             case 0x14: /* BC2F: bits 010000 10100 xxx00 */
01858             case 0x15: /* BC2T: bits 010000 10101 xxx00 */
01859               if (((insn >> 16) & 0x3) == 0x0)
01860                 /* BC2F, BC2T: don't know how to handle these.  */
01861                 break;
01862               break;
01863 
01864             case 0x1a: /* BPOSGE64: bits 010000 11010 */
01865             case 0x1b: /* BPOSGE32: bits 010000 11011 */
01866               {
01867                 unsigned int pos = (b5s5_op (insn >> 16) & 1) ? 32 : 64;
01868                 int dspctl = mips_regnum (gdbarch)->dspctl;
01869 
01870                 if (dspctl == -1)
01871                   /* No way to handle; it'll most likely trap anyway.  */
01872                   break;
01873 
01874                 if ((get_frame_register_unsigned (frame,
01875                                                   dspctl) & 0x7f) >= pos)
01876                   pc += micromips_relative_offset16 (insn);
01877                 else
01878                   pc += micromips_pc_insn_size (gdbarch, pc);
01879               }
01880               break;
01881 
01882             case 0x1c: /* BC1F: bits 010000 11100 xxx00 */
01883                        /* BC1ANY2F: bits 010000 11100 xxx01 */
01884             case 0x1d: /* BC1T: bits 010000 11101 xxx00 */
01885                        /* BC1ANY2T: bits 010000 11101 xxx01 */
01886               if (((insn >> 16) & 0x2) == 0x0)
01887                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc,
01888                                        ((insn >> 16) & 0x1) + 1);
01889               break;
01890 
01891             case 0x1e: /* BC1ANY4F: bits 010000 11110 xxx01 */
01892             case 0x1f: /* BC1ANY4T: bits 010000 11111 xxx01 */
01893               if (((insn >> 16) & 0x3) == 0x1)
01894                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc, 4);
01895               break;
01896             }
01897           break;
01898 
01899         case 0x1d: /* JALS: bits 011101 */
01900         case 0x35: /* J: bits 110101 */
01901         case 0x3d: /* JAL: bits 111101 */
01902             pc = ((pc | 0x7fffffe) ^ 0x7fffffe) | (b0s26_imm (insn) << 1);
01903           break;
01904 
01905         case 0x25: /* BEQ: bits 100101 */
01906             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
01907                 == get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
01908               pc += micromips_relative_offset16 (insn);
01909             else
01910               pc += micromips_pc_insn_size (gdbarch, pc);
01911           break;
01912 
01913         case 0x2d: /* BNE: bits 101101 */
01914             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
01915                 != get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
01916               pc += micromips_relative_offset16 (insn);
01917             else
01918               pc += micromips_pc_insn_size (gdbarch, pc);
01919           break;
01920 
01921         case 0x3c: /* JALX: bits 111100 */
01922             pc = ((pc | 0xfffffff) ^ 0xfffffff) | (b0s26_imm (insn) << 2);
01923           break;
01924         }
01925       break;
01926 
01927     /* 16-bit instructions.  */
01928     case MIPS_INSN16_SIZE:
01929       switch (micromips_op (insn))
01930         {
01931         case 0x11: /* POOL16C: bits 010001 */
01932           if ((b5s5_op (insn) & 0x1c) == 0xc)
01933             /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
01934             pc = get_frame_register_signed (frame, b0s5_reg (insn));
01935           else if (b5s5_op (insn) == 0x18)
01936             /* JRADDIUSP: bits 010001 11000 */
01937             pc = get_frame_register_signed (frame, MIPS_RA_REGNUM);
01938           break;
01939 
01940         case 0x23: /* BEQZ16: bits 100011 */
01941           {
01942             int rs = mips_reg3_to_reg[b7s3_reg (insn)];
01943 
01944             if (get_frame_register_signed (frame, rs) == 0)
01945               pc += micromips_relative_offset7 (insn);
01946             else
01947               pc += micromips_pc_insn_size (gdbarch, pc);
01948           }
01949           break;
01950 
01951         case 0x2b: /* BNEZ16: bits 101011 */
01952           {
01953             int rs = mips_reg3_to_reg[b7s3_reg (insn)];
01954 
01955             if (get_frame_register_signed (frame, rs) != 0)
01956               pc += micromips_relative_offset7 (insn);
01957             else
01958               pc += micromips_pc_insn_size (gdbarch, pc);
01959           }
01960           break;
01961 
01962         case 0x33: /* B16: bits 110011 */
01963           pc += micromips_relative_offset10 (insn);
01964           break;
01965         }
01966       break;
01967     }
01968 
01969   return pc;
01970 }
01971 
01972 /* Decoding the next place to set a breakpoint is irregular for the
01973    mips 16 variant, but fortunately, there fewer instructions.  We have
01974    to cope ith extensions for 16 bit instructions and a pair of actual
01975    32 bit instructions.  We dont want to set a single step instruction
01976    on the extend instruction either.  */
01977 
01978 /* Lots of mips16 instruction formats */
01979 /* Predicting jumps requires itype,ritype,i8type
01980    and their extensions      extItype,extritype,extI8type.  */
01981 enum mips16_inst_fmts
01982 {
01983   itype,                        /* 0  immediate 5,10 */
01984   ritype,                       /* 1   5,3,8 */
01985   rrtype,                       /* 2   5,3,3,5 */
01986   rritype,                      /* 3   5,3,3,5 */
01987   rrrtype,                      /* 4   5,3,3,3,2 */
01988   rriatype,                     /* 5   5,3,3,1,4 */
01989   shifttype,                    /* 6   5,3,3,3,2 */
01990   i8type,                       /* 7   5,3,8 */
01991   i8movtype,                    /* 8   5,3,3,5 */
01992   i8mov32rtype,                 /* 9   5,3,5,3 */
01993   i64type,                      /* 10  5,3,8 */
01994   ri64type,                     /* 11  5,3,3,5 */
01995   jalxtype,                     /* 12  5,1,5,5,16 - a 32 bit instruction */
01996   exiItype,                     /* 13  5,6,5,5,1,1,1,1,1,1,5 */
01997   extRitype,                    /* 14  5,6,5,5,3,1,1,1,5 */
01998   extRRItype,                   /* 15  5,5,5,5,3,3,5 */
01999   extRRIAtype,                  /* 16  5,7,4,5,3,3,1,4 */
02000   EXTshifttype,                 /* 17  5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
02001   extI8type,                    /* 18  5,6,5,5,3,1,1,1,5 */
02002   extI64type,                   /* 19  5,6,5,5,3,1,1,1,5 */
02003   extRi64type,                  /* 20  5,6,5,5,3,3,5 */
02004   extshift64type                /* 21  5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
02005 };
02006 /* I am heaping all the fields of the formats into one structure and
02007    then, only the fields which are involved in instruction extension.  */
02008 struct upk_mips16
02009 {
02010   CORE_ADDR offset;
02011   unsigned int regx;            /* Function in i8 type.  */
02012   unsigned int regy;
02013 };
02014 
02015 
02016 /* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
02017    for the bits which make up the immediate extension.  */
02018 
02019 static CORE_ADDR
02020 extended_offset (unsigned int extension)
02021 {
02022   CORE_ADDR value;
02023 
02024   value = (extension >> 16) & 0x1f;     /* Extract 15:11.  */
02025   value = value << 6;
02026   value |= (extension >> 21) & 0x3f;    /* Extract 10:5.  */
02027   value = value << 5;
02028   value |= extension & 0x1f;            /* Extract 4:0.  */
02029 
02030   return value;
02031 }
02032 
02033 /* Only call this function if you know that this is an extendable
02034    instruction.  It won't malfunction, but why make excess remote memory
02035    references?  If the immediate operands get sign extended or something,
02036    do it after the extension is performed.  */
02037 /* FIXME: Every one of these cases needs to worry about sign extension
02038    when the offset is to be used in relative addressing.  */
02039 
02040 static unsigned int
02041 fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
02042 {
02043   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02044   gdb_byte buf[8];
02045 
02046   pc = unmake_compact_addr (pc);        /* Clear the low order bit.  */
02047   target_read_memory (pc, buf, 2);
02048   return extract_unsigned_integer (buf, 2, byte_order);
02049 }
02050 
02051 static void
02052 unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
02053                unsigned int extension,
02054                unsigned int inst,
02055                enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
02056 {
02057   CORE_ADDR offset;
02058   int regx;
02059   int regy;
02060   switch (insn_format)
02061     {
02062     case itype:
02063       {
02064         CORE_ADDR value;
02065         if (extension)
02066           {
02067             value = extended_offset ((extension << 16) | inst);
02068             value = (value ^ 0x8000) - 0x8000;          /* Sign-extend.  */
02069           }
02070         else
02071           {
02072             value = inst & 0x7ff;
02073             value = (value ^ 0x400) - 0x400;            /* Sign-extend.  */
02074           }
02075         offset = value;
02076         regx = -1;
02077         regy = -1;
02078       }
02079       break;
02080     case ritype:
02081     case i8type:
02082       {                         /* A register identifier and an offset.  */
02083         /* Most of the fields are the same as I type but the
02084            immediate value is of a different length.  */
02085         CORE_ADDR value;
02086         if (extension)
02087           {
02088             value = extended_offset ((extension << 16) | inst);
02089             value = (value ^ 0x8000) - 0x8000;          /* Sign-extend.  */
02090           }
02091         else
02092           {
02093             value = inst & 0xff;                        /* 8 bits */
02094             value = (value ^ 0x80) - 0x80;              /* Sign-extend.  */
02095           }
02096         offset = value;
02097         regx = (inst >> 8) & 0x07;                      /* i8 funct */
02098         regy = -1;
02099         break;
02100       }
02101     case jalxtype:
02102       {
02103         unsigned long value;
02104         unsigned int nexthalf;
02105         value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
02106         value = value << 16;
02107         nexthalf = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc + 2, NULL);
02108                                                 /* Low bit still set.  */
02109         value |= nexthalf;
02110         offset = value;
02111         regx = -1;
02112         regy = -1;
02113         break;
02114       }
02115     default:
02116       internal_error (__FILE__, __LINE__, _("bad switch"));
02117     }
02118   upk->offset = offset;
02119   upk->regx = regx;
02120   upk->regy = regy;
02121 }
02122 
02123 
02124 static CORE_ADDR
02125 add_offset_16 (CORE_ADDR pc, int offset)
02126 {
02127   return ((offset << 2) | ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)));
02128 }
02129 
02130 static CORE_ADDR
02131 extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
02132                          unsigned int extension, unsigned int insn)
02133 {
02134   struct gdbarch *gdbarch = get_frame_arch (frame);
02135   int op = (insn >> 11);
02136   switch (op)
02137     {
02138     case 2:                     /* Branch */
02139       {
02140         struct upk_mips16 upk;
02141         unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
02142         pc += (upk.offset << 1) + 2;
02143         break;
02144       }
02145     case 3:                     /* JAL , JALX - Watch out, these are 32 bit
02146                                    instructions.  */
02147       {
02148         struct upk_mips16 upk;
02149         unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
02150         pc = add_offset_16 (pc, upk.offset);
02151         if ((insn >> 10) & 0x01)        /* Exchange mode */
02152           pc = pc & ~0x01;      /* Clear low bit, indicate 32 bit mode.  */
02153         else
02154           pc |= 0x01;
02155         break;
02156       }
02157     case 4:                     /* beqz */
02158       {
02159         struct upk_mips16 upk;
02160         int reg;
02161         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
02162         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
02163         if (reg == 0)
02164           pc += (upk.offset << 1) + 2;
02165         else
02166           pc += 2;
02167         break;
02168       }
02169     case 5:                     /* bnez */
02170       {
02171         struct upk_mips16 upk;
02172         int reg;
02173         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
02174         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
02175         if (reg != 0)
02176           pc += (upk.offset << 1) + 2;
02177         else
02178           pc += 2;
02179         break;
02180       }
02181     case 12:                    /* I8 Formats btez btnez */
02182       {
02183         struct upk_mips16 upk;
02184         int reg;
02185         unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
02186         /* upk.regx contains the opcode */
02187         reg = get_frame_register_signed (frame, 24);  /* Test register is 24 */
02188         if (((upk.regx == 0) && (reg == 0))     /* BTEZ */
02189             || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
02190           /* pc = add_offset_16(pc,upk.offset) ; */
02191           pc += (upk.offset << 1) + 2;
02192         else
02193           pc += 2;
02194         break;
02195       }
02196     case 29:                    /* RR Formats JR, JALR, JALR-RA */
02197       {
02198         struct upk_mips16 upk;
02199         /* upk.fmt = rrtype; */
02200         op = insn & 0x1f;
02201         if (op == 0)
02202           {
02203             int reg;
02204             upk.regx = (insn >> 8) & 0x07;
02205             upk.regy = (insn >> 5) & 0x07;
02206             if ((upk.regy & 1) == 0)
02207               reg = mips_reg3_to_reg[upk.regx];
02208             else
02209               reg = 31;         /* Function return instruction.  */
02210             pc = get_frame_register_signed (frame, reg);
02211           }
02212         else
02213           pc += 2;
02214         break;
02215       }
02216     case 30:
02217       /* This is an instruction extension.  Fetch the real instruction
02218          (which follows the extension) and decode things based on
02219          that.  */
02220       {
02221         pc += 2;
02222         pc = extended_mips16_next_pc (frame, pc, insn,
02223                                       fetch_mips_16 (gdbarch, pc));
02224         break;
02225       }
02226     default:
02227       {
02228         pc += 2;
02229         break;
02230       }
02231     }
02232   return pc;
02233 }
02234 
02235 static CORE_ADDR
02236 mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
02237 {
02238   struct gdbarch *gdbarch = get_frame_arch (frame);
02239   unsigned int insn = fetch_mips_16 (gdbarch, pc);
02240   return extended_mips16_next_pc (frame, pc, 0, insn);
02241 }
02242 
02243 /* The mips_next_pc function supports single_step when the remote
02244    target monitor or stub is not developed enough to do a single_step.
02245    It works by decoding the current instruction and predicting where a
02246    branch will go.  This isn't hard because all the data is available.
02247    The MIPS32, MIPS16 and microMIPS variants are quite different.  */
02248 static CORE_ADDR
02249 mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
02250 {
02251   struct gdbarch *gdbarch = get_frame_arch (frame);
02252 
02253   if (mips_pc_is_mips16 (gdbarch, pc))
02254     return mips16_next_pc (frame, pc);
02255   else if (mips_pc_is_micromips (gdbarch, pc))
02256     return micromips_next_pc (frame, pc);
02257   else
02258     return mips32_next_pc (frame, pc);
02259 }
02260 
02261 struct mips_frame_cache
02262 {
02263   CORE_ADDR base;
02264   struct trad_frame_saved_reg *saved_regs;
02265 };
02266 
02267 /* Set a register's saved stack address in temp_saved_regs.  If an
02268    address has already been set for this register, do nothing; this
02269    way we will only recognize the first save of a given register in a
02270    function prologue.
02271 
02272    For simplicity, save the address in both [0 .. gdbarch_num_regs) and
02273    [gdbarch_num_regs .. 2*gdbarch_num_regs).
02274    Strictly speaking, only the second range is used as it is only second
02275    range (the ABI instead of ISA registers) that comes into play when finding
02276    saved registers in a frame.  */
02277 
02278 static void
02279 set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
02280                 int regnum, CORE_ADDR offset)
02281 {
02282   if (this_cache != NULL
02283       && this_cache->saved_regs[regnum].addr == -1)
02284     {
02285       this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
02286         = offset;
02287       this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
02288         = offset;
02289     }
02290 }
02291 
02292 
02293 /* Fetch the immediate value from a MIPS16 instruction.
02294    If the previous instruction was an EXTEND, use it to extend
02295    the upper bits of the immediate value.  This is a helper function
02296    for mips16_scan_prologue.  */
02297 
02298 static int
02299 mips16_get_imm (unsigned short prev_inst,       /* previous instruction */
02300                 unsigned short inst,    /* current instruction */
02301                 int nbits,      /* number of bits in imm field */
02302                 int scale,      /* scale factor to be applied to imm */
02303                 int is_signed)  /* is the imm field signed?  */
02304 {
02305   int offset;
02306 
02307   if ((prev_inst & 0xf800) == 0xf000)   /* prev instruction was EXTEND? */
02308     {
02309       offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
02310       if (offset & 0x8000)      /* check for negative extend */
02311         offset = 0 - (0x10000 - (offset & 0xffff));
02312       return offset | (inst & 0x1f);
02313     }
02314   else
02315     {
02316       int max_imm = 1 << nbits;
02317       int mask = max_imm - 1;
02318       int sign_bit = max_imm >> 1;
02319 
02320       offset = inst & mask;
02321       if (is_signed && (offset & sign_bit))
02322         offset = 0 - (max_imm - offset);
02323       return offset * scale;
02324     }
02325 }
02326 
02327 
02328 /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
02329    the associated FRAME_CACHE if not null.
02330    Return the address of the first instruction past the prologue.  */
02331 
02332 static CORE_ADDR
02333 mips16_scan_prologue (struct gdbarch *gdbarch,
02334                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
02335                       struct frame_info *this_frame,
02336                       struct mips_frame_cache *this_cache)
02337 {
02338   CORE_ADDR cur_pc;
02339   CORE_ADDR frame_addr = 0;     /* Value of $r17, used as frame pointer.  */
02340   CORE_ADDR sp;
02341   long frame_offset = 0;        /* Size of stack frame.  */
02342   long frame_adjust = 0;        /* Offset of FP from SP.  */
02343   int frame_reg = MIPS_SP_REGNUM;
02344   unsigned short prev_inst = 0; /* saved copy of previous instruction.  */
02345   unsigned inst = 0;            /* current instruction */
02346   unsigned entry_inst = 0;      /* the entry instruction */
02347   unsigned save_inst = 0;       /* the save instruction */
02348   int reg, offset;
02349 
02350   int extend_bytes = 0;
02351   int prev_extend_bytes;
02352   CORE_ADDR end_prologue_addr = 0;
02353 
02354   /* Can be called when there's no process, and hence when there's no
02355      THIS_FRAME.  */
02356   if (this_frame != NULL)
02357     sp = get_frame_register_signed (this_frame,
02358                                     gdbarch_num_regs (gdbarch)
02359                                     + MIPS_SP_REGNUM);
02360   else
02361     sp = 0;
02362 
02363   if (limit_pc > start_pc + 200)
02364     limit_pc = start_pc + 200;
02365 
02366   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
02367     {
02368       /* Save the previous instruction.  If it's an EXTEND, we'll extract
02369          the immediate offset extension from it in mips16_get_imm.  */
02370       prev_inst = inst;
02371 
02372       /* Fetch and decode the instruction.  */
02373       inst = (unsigned short) mips_fetch_instruction (gdbarch, ISA_MIPS16,
02374                                                       cur_pc, NULL);
02375 
02376       /* Normally we ignore extend instructions.  However, if it is
02377          not followed by a valid prologue instruction, then this
02378          instruction is not part of the prologue either.  We must
02379          remember in this case to adjust the end_prologue_addr back
02380          over the extend.  */
02381       if ((inst & 0xf800) == 0xf000)    /* extend */
02382         {
02383           extend_bytes = MIPS_INSN16_SIZE;
02384           continue;
02385         }
02386 
02387       prev_extend_bytes = extend_bytes;
02388       extend_bytes = 0;
02389 
02390       if ((inst & 0xff00) == 0x6300     /* addiu sp */
02391           || (inst & 0xff00) == 0xfb00) /* daddiu sp */
02392         {
02393           offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
02394           if (offset < 0)       /* Negative stack adjustment?  */
02395             frame_offset -= offset;
02396           else
02397             /* Exit loop if a positive stack adjustment is found, which
02398                usually means that the stack cleanup code in the function
02399                epilogue is reached.  */
02400             break;
02401         }
02402       else if ((inst & 0xf800) == 0xd000)       /* sw reg,n($sp) */
02403         {
02404           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
02405           reg = mips_reg3_to_reg[(inst & 0x700) >> 8];
02406           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02407         }
02408       else if ((inst & 0xff00) == 0xf900)       /* sd reg,n($sp) */
02409         {
02410           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
02411           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
02412           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02413         }
02414       else if ((inst & 0xff00) == 0x6200)       /* sw $ra,n($sp) */
02415         {
02416           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
02417           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
02418         }
02419       else if ((inst & 0xff00) == 0xfa00)       /* sd $ra,n($sp) */
02420         {
02421           offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
02422           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
02423         }
02424       else if (inst == 0x673d)  /* move $s1, $sp */
02425         {
02426           frame_addr = sp;
02427           frame_reg = 17;
02428         }
02429       else if ((inst & 0xff00) == 0x0100)       /* addiu $s1,sp,n */
02430         {
02431           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
02432           frame_addr = sp + offset;
02433           frame_reg = 17;
02434           frame_adjust = offset;
02435         }
02436       else if ((inst & 0xFF00) == 0xd900)       /* sw reg,offset($s1) */
02437         {
02438           offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
02439           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
02440           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
02441         }
02442       else if ((inst & 0xFF00) == 0x7900)       /* sd reg,offset($s1) */
02443         {
02444           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
02445           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
02446           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
02447         }
02448       else if ((inst & 0xf81f) == 0xe809
02449                && (inst & 0x700) != 0x700)      /* entry */
02450         entry_inst = inst;      /* Save for later processing.  */
02451       else if ((inst & 0xff80) == 0x6480)       /* save */
02452         {
02453           save_inst = inst;     /* Save for later processing.  */
02454           if (prev_extend_bytes)                /* extend */
02455             save_inst |= prev_inst << 16;
02456         }
02457       else if ((inst & 0xf800) == 0x1800)       /* jal(x) */
02458         cur_pc += MIPS_INSN16_SIZE;     /* 32-bit instruction */
02459       else if ((inst & 0xff1c) == 0x6704)       /* move reg,$a0-$a3 */
02460         {
02461           /* This instruction is part of the prologue, but we don't
02462              need to do anything special to handle it.  */
02463         }
02464       else
02465         {
02466           /* This instruction is not an instruction typically found
02467              in a prologue, so we must have reached the end of the
02468              prologue.  */
02469           if (end_prologue_addr == 0)
02470             end_prologue_addr = cur_pc - prev_extend_bytes;
02471         }
02472     }
02473 
02474   /* The entry instruction is typically the first instruction in a function,
02475      and it stores registers at offsets relative to the value of the old SP
02476      (before the prologue).  But the value of the sp parameter to this
02477      function is the new SP (after the prologue has been executed).  So we
02478      can't calculate those offsets until we've seen the entire prologue,
02479      and can calculate what the old SP must have been.  */
02480   if (entry_inst != 0)
02481     {
02482       int areg_count = (entry_inst >> 8) & 7;
02483       int sreg_count = (entry_inst >> 6) & 3;
02484 
02485       /* The entry instruction always subtracts 32 from the SP.  */
02486       frame_offset += 32;
02487 
02488       /* Now we can calculate what the SP must have been at the
02489          start of the function prologue.  */
02490       sp += frame_offset;
02491 
02492       /* Check if a0-a3 were saved in the caller's argument save area.  */
02493       for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
02494         {
02495           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02496           offset += mips_abi_regsize (gdbarch);
02497         }
02498 
02499       /* Check if the ra register was pushed on the stack.  */
02500       offset = -4;
02501       if (entry_inst & 0x20)
02502         {
02503           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
02504           offset -= mips_abi_regsize (gdbarch);
02505         }
02506 
02507       /* Check if the s0 and s1 registers were pushed on the stack.  */
02508       for (reg = 16; reg < sreg_count + 16; reg++)
02509         {
02510           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02511           offset -= mips_abi_regsize (gdbarch);
02512         }
02513     }
02514 
02515   /* The SAVE instruction is similar to ENTRY, except that defined by the
02516      MIPS16e ASE of the MIPS Architecture.  Unlike with ENTRY though, the
02517      size of the frame is specified as an immediate field of instruction
02518      and an extended variation exists which lets additional registers and
02519      frame space to be specified.  The instruction always treats registers
02520      as 32-bit so its usefulness for 64-bit ABIs is questionable.  */
02521   if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
02522     {
02523       static int args_table[16] = {
02524         0, 0, 0, 0, 1, 1, 1, 1,
02525         2, 2, 2, 0, 3, 3, 4, -1,
02526       };
02527       static int astatic_table[16] = {
02528         0, 1, 2, 3, 0, 1, 2, 3,
02529         0, 1, 2, 4, 0, 1, 0, -1,
02530       };
02531       int aregs = (save_inst >> 16) & 0xf;
02532       int xsregs = (save_inst >> 24) & 0x7;
02533       int args = args_table[aregs];
02534       int astatic = astatic_table[aregs];
02535       long frame_size;
02536 
02537       if (args < 0)
02538         {
02539           warning (_("Invalid number of argument registers encoded in SAVE."));
02540           args = 0;
02541         }
02542       if (astatic < 0)
02543         {
02544           warning (_("Invalid number of static registers encoded in SAVE."));
02545           astatic = 0;
02546         }
02547 
02548       /* For standard SAVE the frame size of 0 means 128.  */
02549       frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
02550       if (frame_size == 0 && (save_inst >> 16) == 0)
02551         frame_size = 16;
02552       frame_size *= 8;
02553       frame_offset += frame_size;
02554 
02555       /* Now we can calculate what the SP must have been at the
02556          start of the function prologue.  */
02557       sp += frame_offset;
02558 
02559       /* Check if A0-A3 were saved in the caller's argument save area.  */
02560       for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
02561         {
02562           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02563           offset += mips_abi_regsize (gdbarch);
02564         }
02565 
02566       offset = -4;
02567 
02568       /* Check if the RA register was pushed on the stack.  */
02569       if (save_inst & 0x40)
02570         {
02571           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
02572           offset -= mips_abi_regsize (gdbarch);
02573         }
02574 
02575       /* Check if the S8 register was pushed on the stack.  */
02576       if (xsregs > 6)
02577         {
02578           set_reg_offset (gdbarch, this_cache, 30, sp + offset);
02579           offset -= mips_abi_regsize (gdbarch);
02580           xsregs--;
02581         }
02582       /* Check if S2-S7 were pushed on the stack.  */
02583       for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
02584         {
02585           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02586           offset -= mips_abi_regsize (gdbarch);
02587         }
02588 
02589       /* Check if the S1 register was pushed on the stack.  */
02590       if (save_inst & 0x10)
02591         {
02592           set_reg_offset (gdbarch, this_cache, 17, sp + offset);
02593           offset -= mips_abi_regsize (gdbarch);
02594         }
02595       /* Check if the S0 register was pushed on the stack.  */
02596       if (save_inst & 0x20)
02597         {
02598           set_reg_offset (gdbarch, this_cache, 16, sp + offset);
02599           offset -= mips_abi_regsize (gdbarch);
02600         }
02601 
02602       /* Check if A0-A3 were pushed on the stack.  */
02603       for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
02604         {
02605           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
02606           offset -= mips_abi_regsize (gdbarch);
02607         }
02608     }
02609 
02610   if (this_cache != NULL)
02611     {
02612       this_cache->base =
02613         (get_frame_register_signed (this_frame,
02614                                     gdbarch_num_regs (gdbarch) + frame_reg)
02615          + frame_offset - frame_adjust);
02616       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
02617          be able to get rid of the assignment below, evetually.  But it's
02618          still needed for now.  */
02619       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
02620                              + mips_regnum (gdbarch)->pc]
02621         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
02622     }
02623 
02624   /* If we didn't reach the end of the prologue when scanning the function
02625      instructions, then set end_prologue_addr to the address of the
02626      instruction immediately after the last one we scanned.  */
02627   if (end_prologue_addr == 0)
02628     end_prologue_addr = cur_pc;
02629 
02630   return end_prologue_addr;
02631 }
02632 
02633 /* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
02634    Procedures that use the 32-bit instruction set are handled by the
02635    mips_insn32 unwinder.  */
02636 
02637 static struct mips_frame_cache *
02638 mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
02639 {
02640   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02641   struct mips_frame_cache *cache;
02642 
02643   if ((*this_cache) != NULL)
02644     return (*this_cache);
02645   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
02646   (*this_cache) = cache;
02647   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
02648 
02649   /* Analyze the function prologue.  */
02650   {
02651     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
02652     CORE_ADDR start_addr;
02653 
02654     find_pc_partial_function (pc, NULL, &start_addr, NULL);
02655     if (start_addr == 0)
02656       start_addr = heuristic_proc_start (gdbarch, pc);
02657     /* We can't analyze the prologue if we couldn't find the begining
02658        of the function.  */
02659     if (start_addr == 0)
02660       return cache;
02661 
02662     mips16_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
02663   }
02664   
02665   /* gdbarch_sp_regnum contains the value and not the address.  */
02666   trad_frame_set_value (cache->saved_regs,
02667                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
02668                         cache->base);
02669 
02670   return (*this_cache);
02671 }
02672 
02673 static void
02674 mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
02675                            struct frame_id *this_id)
02676 {
02677   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
02678                                                            this_cache);
02679   /* This marks the outermost frame.  */
02680   if (info->base == 0)
02681     return;
02682   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
02683 }
02684 
02685 static struct value *
02686 mips_insn16_frame_prev_register (struct frame_info *this_frame,
02687                                  void **this_cache, int regnum)
02688 {
02689   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
02690                                                            this_cache);
02691   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
02692 }
02693 
02694 static int
02695 mips_insn16_frame_sniffer (const struct frame_unwind *self,
02696                            struct frame_info *this_frame, void **this_cache)
02697 {
02698   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02699   CORE_ADDR pc = get_frame_pc (this_frame);
02700   if (mips_pc_is_mips16 (gdbarch, pc))
02701     return 1;
02702   return 0;
02703 }
02704 
02705 static const struct frame_unwind mips_insn16_frame_unwind =
02706 {
02707   NORMAL_FRAME,
02708   default_frame_unwind_stop_reason,
02709   mips_insn16_frame_this_id,
02710   mips_insn16_frame_prev_register,
02711   NULL,
02712   mips_insn16_frame_sniffer
02713 };
02714 
02715 static CORE_ADDR
02716 mips_insn16_frame_base_address (struct frame_info *this_frame,
02717                                 void **this_cache)
02718 {
02719   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
02720                                                            this_cache);
02721   return info->base;
02722 }
02723 
02724 static const struct frame_base mips_insn16_frame_base =
02725 {
02726   &mips_insn16_frame_unwind,
02727   mips_insn16_frame_base_address,
02728   mips_insn16_frame_base_address,
02729   mips_insn16_frame_base_address
02730 };
02731 
02732 static const struct frame_base *
02733 mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
02734 {
02735   struct gdbarch *gdbarch = get_frame_arch (this_frame);
02736   CORE_ADDR pc = get_frame_pc (this_frame);
02737   if (mips_pc_is_mips16 (gdbarch, pc))
02738     return &mips_insn16_frame_base;
02739   else
02740     return NULL;
02741 }
02742 
02743 /* Decode a 9-bit signed immediate argument of ADDIUSP -- -2 is mapped
02744    to -258, -1 -- to -257, 0 -- to 256, 1 -- to 257 and other values are
02745    interpreted directly, and then multiplied by 4.  */
02746 
02747 static int
02748 micromips_decode_imm9 (int imm)
02749 {
02750   imm = (imm ^ 0x100) - 0x100;
02751   if (imm > -3 && imm < 2)
02752     imm ^= 0x100;
02753   return imm << 2;
02754 }
02755 
02756 /* Analyze the function prologue from START_PC to LIMIT_PC.  Return
02757    the address of the first instruction past the prologue.  */
02758 
02759 static CORE_ADDR
02760 micromips_scan_prologue (struct gdbarch *gdbarch,
02761                          CORE_ADDR start_pc, CORE_ADDR limit_pc,
02762                          struct frame_info *this_frame,
02763                          struct mips_frame_cache *this_cache)
02764 {
02765   CORE_ADDR end_prologue_addr = 0;
02766   int prev_non_prologue_insn = 0;
02767   int frame_reg = MIPS_SP_REGNUM;
02768   int this_non_prologue_insn;
02769   int non_prologue_insns = 0;
02770   long frame_offset = 0;        /* Size of stack frame.  */
02771   long frame_adjust = 0;        /* Offset of FP from SP.  */
02772   CORE_ADDR frame_addr = 0;     /* Value of $30, used as frame pointer.  */
02773   CORE_ADDR prev_pc;
02774   CORE_ADDR cur_pc;
02775   ULONGEST insn;                /* current instruction */
02776   CORE_ADDR sp;
02777   long offset;
02778   long sp_adj;
02779   long v1_off = 0;              /* The assumption is LUI will replace it.  */
02780   int reglist;
02781   int breg;
02782   int dreg;
02783   int sreg;
02784   int treg;
02785   int loc;
02786   int op;
02787   int s;
02788   int i;
02789 
02790   /* Can be called when there's no process, and hence when there's no
02791      THIS_FRAME.  */
02792   if (this_frame != NULL)
02793     sp = get_frame_register_signed (this_frame,
02794                                     gdbarch_num_regs (gdbarch)
02795                                     + MIPS_SP_REGNUM);
02796   else
02797     sp = 0;
02798 
02799   if (limit_pc > start_pc + 200)
02800     limit_pc = start_pc + 200;
02801   prev_pc = start_pc;
02802 
02803   /* Permit at most one non-prologue non-control-transfer instruction
02804      in the middle which may have been reordered by the compiler for
02805      optimisation.  */
02806   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += loc)
02807     {
02808       this_non_prologue_insn = 0;
02809       sp_adj = 0;
02810       loc = 0;
02811       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, cur_pc, NULL);
02812       loc += MIPS_INSN16_SIZE;
02813       switch (mips_insn_size (ISA_MICROMIPS, insn))
02814         {
02815         /* 48-bit instructions.  */
02816         case 3 * MIPS_INSN16_SIZE:
02817           /* No prologue instructions in this category.  */
02818           this_non_prologue_insn = 1;
02819           loc += 2 * MIPS_INSN16_SIZE;
02820           break;
02821 
02822         /* 32-bit instructions.  */
02823         case 2 * MIPS_INSN16_SIZE:
02824           insn <<= 16;
02825           insn |= mips_fetch_instruction (gdbarch,
02826                                           ISA_MICROMIPS, cur_pc + loc, NULL);
02827           loc += MIPS_INSN16_SIZE;
02828           switch (micromips_op (insn >> 16))
02829             {
02830             /* Record $sp/$fp adjustment.  */
02831             /* Discard (D)ADDU $gp,$jp used for PIC code.  */
02832             case 0x0: /* POOL32A: bits 000000 */
02833             case 0x16: /* POOL32S: bits 010110 */
02834               op = b0s11_op (insn);
02835               sreg = b0s5_reg (insn >> 16);
02836               treg = b5s5_reg (insn >> 16);
02837               dreg = b11s5_reg (insn);
02838               if (op == 0x1d0
02839                                 /* SUBU: bits 000000 00111010000 */
02840                                 /* DSUBU: bits 010110 00111010000 */
02841                   && dreg == MIPS_SP_REGNUM && sreg == MIPS_SP_REGNUM
02842                   && treg == 3)
02843                                 /* (D)SUBU $sp, $v1 */
02844                     sp_adj = v1_off;
02845               else if (op != 0x150
02846                                 /* ADDU: bits 000000 00101010000 */
02847                                 /* DADDU: bits 010110 00101010000 */
02848                        || dreg != 28 || sreg != 28 || treg != MIPS_T9_REGNUM)
02849                 this_non_prologue_insn = 1;
02850               break;
02851 
02852             case 0x8: /* POOL32B: bits 001000 */
02853               op = b12s4_op (insn);
02854               breg = b0s5_reg (insn >> 16);
02855               reglist = sreg = b5s5_reg (insn >> 16);
02856               offset = (b0s12_imm (insn) ^ 0x800) - 0x800;
02857               if ((op == 0x9 || op == 0xc)
02858                                 /* SWP: bits 001000 1001 */
02859                                 /* SDP: bits 001000 1100 */
02860                   && breg == MIPS_SP_REGNUM && sreg < MIPS_RA_REGNUM)
02861                                 /* S[DW]P reg,offset($sp) */
02862                 {
02863                   s = 4 << ((b12s4_op (insn) & 0x4) == 0x4);
02864                   set_reg_offset (gdbarch, this_cache,
02865                                   sreg, sp + offset);
02866                   set_reg_offset (gdbarch, this_cache,
02867                                   sreg + 1, sp + offset + s);
02868                 }
02869               else if ((op == 0xd || op == 0xf)
02870                                 /* SWM: bits 001000 1101 */
02871                                 /* SDM: bits 001000 1111 */
02872                        && breg == MIPS_SP_REGNUM
02873                                 /* SWM reglist,offset($sp) */
02874                        && ((reglist >= 1 && reglist <= 9)
02875                            || (reglist >= 16 && reglist <= 25)))
02876                 {
02877                   int sreglist = min(reglist & 0xf, 8);
02878 
02879                   s = 4 << ((b12s4_op (insn) & 0x2) == 0x2);
02880                   for (i = 0; i < sreglist; i++)
02881                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + s * i);
02882                   if ((reglist & 0xf) > 8)
02883                     set_reg_offset (gdbarch, this_cache, 30, sp + s * i++);
02884                   if ((reglist & 0x10) == 0x10)
02885                     set_reg_offset (gdbarch, this_cache,
02886                                     MIPS_RA_REGNUM, sp + s * i++);
02887                 }
02888               else
02889                 this_non_prologue_insn = 1;
02890               break;
02891 
02892             /* Record $sp/$fp adjustment.  */
02893             /* Discard (D)ADDIU $gp used for PIC code.  */
02894             case 0xc: /* ADDIU: bits 001100 */
02895             case 0x17: /* DADDIU: bits 010111 */
02896               sreg = b0s5_reg (insn >> 16);
02897               dreg = b5s5_reg (insn >> 16);
02898               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
02899               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM)
02900                                 /* (D)ADDIU $sp, imm */
02901                 sp_adj = offset;
02902               else if (sreg == MIPS_SP_REGNUM && dreg == 30)
02903                                 /* (D)ADDIU $fp, $sp, imm */
02904                 {
02905                   frame_addr = sp + offset;
02906                   frame_adjust = offset;
02907                   frame_reg = 30;
02908                 }
02909               else if (sreg != 28 || dreg != 28)
02910                                 /* (D)ADDIU $gp, imm */
02911                 this_non_prologue_insn = 1;
02912               break;
02913 
02914             /* LUI $v1 is used for larger $sp adjustments.  */
02915             /* Discard LUI $gp used for PIC code.  */
02916             case 0x10: /* POOL32I: bits 010000 */
02917               if (b5s5_op (insn >> 16) == 0xd
02918                                 /* LUI: bits 010000 001101 */
02919                   && b0s5_reg (insn >> 16) == 3)
02920                                 /* LUI $v1, imm */
02921                 v1_off = ((b0s16_imm (insn) << 16) ^ 0x80000000) - 0x80000000;
02922               else if (b5s5_op (insn >> 16) != 0xd
02923                                 /* LUI: bits 010000 001101 */
02924                        || b0s5_reg (insn >> 16) != 28)
02925                                 /* LUI $gp, imm */
02926                 this_non_prologue_insn = 1;
02927               break;
02928 
02929             /* ORI $v1 is used for larger $sp adjustments.  */
02930             case 0x14: /* ORI: bits 010100 */
02931               sreg = b0s5_reg (insn >> 16);
02932               dreg = b5s5_reg (insn >> 16);
02933               if (sreg == 3 && dreg == 3)
02934                                 /* ORI $v1, imm */
02935                 v1_off |= b0s16_imm (insn);
02936               else
02937                 this_non_prologue_insn = 1;
02938               break;
02939 
02940             case 0x26: /* SWC1: bits 100110 */
02941             case 0x2e: /* SDC1: bits 101110 */
02942               breg = b0s5_reg (insn >> 16);
02943               if (breg != MIPS_SP_REGNUM)
02944                                 /* S[DW]C1 reg,offset($sp) */
02945                 this_non_prologue_insn = 1;
02946               break;
02947 
02948             case 0x36: /* SD: bits 110110 */
02949             case 0x3e: /* SW: bits 111110 */
02950               breg = b0s5_reg (insn >> 16);
02951               sreg = b5s5_reg (insn >> 16);
02952               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
02953               if (breg == MIPS_SP_REGNUM)
02954                                 /* S[DW] reg,offset($sp) */
02955                 set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
02956               else
02957                 this_non_prologue_insn = 1;
02958               break;
02959 
02960             default:
02961               this_non_prologue_insn = 1;
02962               break;
02963             }
02964           break;
02965 
02966         /* 16-bit instructions.  */
02967         case MIPS_INSN16_SIZE:
02968           switch (micromips_op (insn))
02969             {
02970             case 0x3: /* MOVE: bits 000011 */
02971               sreg = b0s5_reg (insn);
02972               dreg = b5s5_reg (insn);
02973               if (sreg == MIPS_SP_REGNUM && dreg == 30)
02974                                 /* MOVE  $fp, $sp */
02975                 {
02976                   frame_addr = sp;
02977                   frame_reg = 30;
02978                 }
02979               else if ((sreg & 0x1c) != 0x4)
02980                                 /* MOVE  reg, $a0-$a3 */
02981                 this_non_prologue_insn = 1;
02982               break;
02983 
02984             case 0x11: /* POOL16C: bits 010001 */
02985               if (b6s4_op (insn) == 0x5)
02986                                 /* SWM: bits 010001 0101 */
02987                 {
02988                   offset = ((b0s4_imm (insn) << 2) ^ 0x20) - 0x20;
02989                   reglist = b4s2_regl (insn);
02990                   for (i = 0; i <= reglist; i++)
02991                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + 4 * i);
02992                   set_reg_offset (gdbarch, this_cache,
02993                                   MIPS_RA_REGNUM, sp + 4 * i++);
02994                 }
02995               else
02996                 this_non_prologue_insn = 1;
02997               break;
02998 
02999             case 0x13: /* POOL16D: bits 010011 */
03000               if ((insn & 0x1) == 0x1)
03001                                 /* ADDIUSP: bits 010011 1 */
03002                 sp_adj = micromips_decode_imm9 (b1s9_imm (insn));
03003               else if (b5s5_reg (insn) == MIPS_SP_REGNUM)
03004                                 /* ADDIUS5: bits 010011 0 */
03005                                 /* ADDIUS5 $sp, imm */
03006                 sp_adj = (b1s4_imm (insn) ^ 8) - 8;
03007               else
03008                 this_non_prologue_insn = 1;
03009               break;
03010 
03011             case 0x32: /* SWSP: bits 110010 */
03012               offset = b0s5_imm (insn) << 2;
03013               sreg = b5s5_reg (insn);
03014               set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
03015               break;
03016 
03017             default:
03018               this_non_prologue_insn = 1;
03019               break;
03020             }
03021           break;
03022         }
03023       if (sp_adj < 0)
03024         frame_offset -= sp_adj;
03025 
03026       non_prologue_insns += this_non_prologue_insn;
03027       /* Enough non-prologue insns seen or positive stack adjustment? */
03028       if (end_prologue_addr == 0 && (non_prologue_insns > 1 || sp_adj > 0))
03029         {
03030           end_prologue_addr = prev_non_prologue_insn ? prev_pc : cur_pc;
03031           break;
03032         }
03033       prev_non_prologue_insn = this_non_prologue_insn;
03034       prev_pc = cur_pc;
03035     }
03036 
03037   if (this_cache != NULL)
03038     {
03039       this_cache->base =
03040         (get_frame_register_signed (this_frame,
03041                                     gdbarch_num_regs (gdbarch) + frame_reg)
03042          + frame_offset - frame_adjust);
03043       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
03044          be able to get rid of the assignment below, evetually. But it's
03045          still needed for now.  */
03046       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
03047                              + mips_regnum (gdbarch)->pc]
03048         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
03049     }
03050 
03051   /* If we didn't reach the end of the prologue when scanning the function
03052      instructions, then set end_prologue_addr to the address of the
03053      instruction immediately after the last one we scanned.  Unless the
03054      last one looked like a non-prologue instruction (and we looked ahead),
03055      in which case use its address instead.  */
03056   if (end_prologue_addr == 0)
03057     end_prologue_addr = prev_non_prologue_insn ? prev_pc : cur_pc;
03058 
03059   return end_prologue_addr;
03060 }
03061 
03062 /* Heuristic unwinder for procedures using microMIPS instructions.
03063    Procedures that use the 32-bit instruction set are handled by the
03064    mips_insn32 unwinder.  Likewise MIPS16 and the mips_insn16 unwinder. */
03065 
03066 static struct mips_frame_cache *
03067 mips_micro_frame_cache (struct frame_info *this_frame, void **this_cache)
03068 {
03069   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03070   struct mips_frame_cache *cache;
03071 
03072   if ((*this_cache) != NULL)
03073     return (*this_cache);
03074 
03075   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
03076   (*this_cache) = cache;
03077   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
03078 
03079   /* Analyze the function prologue.  */
03080   {
03081     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
03082     CORE_ADDR start_addr;
03083 
03084     find_pc_partial_function (pc, NULL, &start_addr, NULL);
03085     if (start_addr == 0)
03086       start_addr = heuristic_proc_start (get_frame_arch (this_frame), pc);
03087     /* We can't analyze the prologue if we couldn't find the begining
03088        of the function.  */
03089     if (start_addr == 0)
03090       return cache;
03091 
03092     micromips_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
03093   }
03094 
03095   /* gdbarch_sp_regnum contains the value and not the address.  */
03096   trad_frame_set_value (cache->saved_regs,
03097                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
03098                         cache->base);
03099 
03100   return (*this_cache);
03101 }
03102 
03103 static void
03104 mips_micro_frame_this_id (struct frame_info *this_frame, void **this_cache,
03105                           struct frame_id *this_id)
03106 {
03107   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
03108                                                           this_cache);
03109   /* This marks the outermost frame.  */
03110   if (info->base == 0)
03111     return;
03112   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
03113 }
03114 
03115 static struct value *
03116 mips_micro_frame_prev_register (struct frame_info *this_frame,
03117                                 void **this_cache, int regnum)
03118 {
03119   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
03120                                                           this_cache);
03121   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
03122 }
03123 
03124 static int
03125 mips_micro_frame_sniffer (const struct frame_unwind *self,
03126                           struct frame_info *this_frame, void **this_cache)
03127 {
03128   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03129   CORE_ADDR pc = get_frame_pc (this_frame);
03130 
03131   if (mips_pc_is_micromips (gdbarch, pc))
03132     return 1;
03133   return 0;
03134 }
03135 
03136 static const struct frame_unwind mips_micro_frame_unwind =
03137 {
03138   NORMAL_FRAME,
03139   default_frame_unwind_stop_reason,
03140   mips_micro_frame_this_id,
03141   mips_micro_frame_prev_register,
03142   NULL,
03143   mips_micro_frame_sniffer
03144 };
03145 
03146 static CORE_ADDR
03147 mips_micro_frame_base_address (struct frame_info *this_frame,
03148                                void **this_cache)
03149 {
03150   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
03151                                                           this_cache);
03152   return info->base;
03153 }
03154 
03155 static const struct frame_base mips_micro_frame_base =
03156 {
03157   &mips_micro_frame_unwind,
03158   mips_micro_frame_base_address,
03159   mips_micro_frame_base_address,
03160   mips_micro_frame_base_address
03161 };
03162 
03163 static const struct frame_base *
03164 mips_micro_frame_base_sniffer (struct frame_info *this_frame)
03165 {
03166   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03167   CORE_ADDR pc = get_frame_pc (this_frame);
03168 
03169   if (mips_pc_is_micromips (gdbarch, pc))
03170     return &mips_micro_frame_base;
03171   else
03172     return NULL;
03173 }
03174 
03175 /* Mark all the registers as unset in the saved_regs array
03176    of THIS_CACHE.  Do nothing if THIS_CACHE is null.  */
03177 
03178 static void
03179 reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
03180 {
03181   if (this_cache == NULL || this_cache->saved_regs == NULL)
03182     return;
03183 
03184   {
03185     const int num_regs = gdbarch_num_regs (gdbarch);
03186     int i;
03187 
03188     for (i = 0; i < num_regs; i++)
03189       {
03190         this_cache->saved_regs[i].addr = -1;
03191       }
03192   }
03193 }
03194 
03195 /* Analyze the function prologue from START_PC to LIMIT_PC.  Builds
03196    the associated FRAME_CACHE if not null.  
03197    Return the address of the first instruction past the prologue.  */
03198 
03199 static CORE_ADDR
03200 mips32_scan_prologue (struct gdbarch *gdbarch,
03201                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
03202                       struct frame_info *this_frame,
03203                       struct mips_frame_cache *this_cache)
03204 {
03205   CORE_ADDR cur_pc;
03206   CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
03207                                frame-pointer.  */
03208   CORE_ADDR sp;
03209   long frame_offset;
03210   int  frame_reg = MIPS_SP_REGNUM;
03211 
03212   CORE_ADDR end_prologue_addr = 0;
03213   int seen_sp_adjust = 0;
03214   int load_immediate_bytes = 0;
03215   int in_delay_slot = 0;
03216   int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);
03217 
03218   /* Can be called when there's no process, and hence when there's no
03219      THIS_FRAME.  */
03220   if (this_frame != NULL)
03221     sp = get_frame_register_signed (this_frame,
03222                                     gdbarch_num_regs (gdbarch)
03223                                     + MIPS_SP_REGNUM);
03224   else
03225     sp = 0;
03226 
03227   if (limit_pc > start_pc + 200)
03228     limit_pc = start_pc + 200;
03229 
03230 restart:
03231 
03232   frame_offset = 0;
03233   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
03234     {
03235       unsigned long inst, high_word, low_word;
03236       int reg;
03237 
03238       /* Fetch the instruction.  */
03239       inst = (unsigned long) mips_fetch_instruction (gdbarch, ISA_MIPS,
03240                                                      cur_pc, NULL);
03241 
03242       /* Save some code by pre-extracting some useful fields.  */
03243       high_word = (inst >> 16) & 0xffff;
03244       low_word = inst & 0xffff;
03245       reg = high_word & 0x1f;
03246 
03247       if (high_word == 0x27bd           /* addiu $sp,$sp,-i */
03248           || high_word == 0x23bd        /* addi $sp,$sp,-i */
03249           || high_word == 0x67bd)       /* daddiu $sp,$sp,-i */
03250         {
03251           if (low_word & 0x8000)        /* Negative stack adjustment?  */
03252             frame_offset += 0x10000 - low_word;
03253           else
03254             /* Exit loop if a positive stack adjustment is found, which
03255                usually means that the stack cleanup code in the function
03256                epilogue is reached.  */
03257             break;
03258           seen_sp_adjust = 1;
03259         }
03260       else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
03261                && !regsize_is_64_bits)
03262         {
03263           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
03264         }
03265       else if (((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
03266                && regsize_is_64_bits)
03267         {
03268           /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra.  */
03269           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
03270         }
03271       else if (high_word == 0x27be)     /* addiu $30,$sp,size */
03272         {
03273           /* Old gcc frame, r30 is virtual frame pointer.  */
03274           if ((long) low_word != frame_offset)
03275             frame_addr = sp + low_word;
03276           else if (this_frame && frame_reg == MIPS_SP_REGNUM)
03277             {
03278               unsigned alloca_adjust;
03279 
03280               frame_reg = 30;
03281               frame_addr = get_frame_register_signed
03282                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
03283               frame_offset = 0;
03284 
03285               alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
03286               if (alloca_adjust > 0)
03287                 {
03288                   /* FP > SP + frame_size.  This may be because of
03289                      an alloca or somethings similar.  Fix sp to
03290                      "pre-alloca" value, and try again.  */
03291                   sp += alloca_adjust;
03292                   /* Need to reset the status of all registers.  Otherwise,
03293                      we will hit a guard that prevents the new address
03294                      for each register to be recomputed during the second
03295                      pass.  */
03296                   reset_saved_regs (gdbarch, this_cache);
03297                   goto restart;
03298                 }
03299             }
03300         }
03301       /* move $30,$sp.  With different versions of gas this will be either
03302          `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
03303          Accept any one of these.  */
03304       else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
03305         {
03306           /* New gcc frame, virtual frame pointer is at r30 + frame_size.  */
03307           if (this_frame && frame_reg == MIPS_SP_REGNUM)
03308             {
03309               unsigned alloca_adjust;
03310 
03311               frame_reg = 30;
03312               frame_addr = get_frame_register_signed
03313                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
03314 
03315               alloca_adjust = (unsigned) (frame_addr - sp);
03316               if (alloca_adjust > 0)
03317                 {
03318                   /* FP > SP + frame_size.  This may be because of
03319                      an alloca or somethings similar.  Fix sp to
03320                      "pre-alloca" value, and try again.  */
03321                   sp = frame_addr;
03322                   /* Need to reset the status of all registers.  Otherwise,
03323                      we will hit a guard that prevents the new address
03324                      for each register to be recomputed during the second
03325                      pass.  */
03326                   reset_saved_regs (gdbarch, this_cache);
03327                   goto restart;
03328                 }
03329             }
03330         }
03331       else if ((high_word & 0xFFE0) == 0xafc0   /* sw reg,offset($30) */
03332                && !regsize_is_64_bits)
03333         {
03334           set_reg_offset (gdbarch, this_cache, reg, frame_addr + low_word);
03335         }
03336       else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
03337                || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
03338                || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
03339                || high_word == 0x3c1c /* lui $gp,n */
03340                || high_word == 0x279c /* addiu $gp,$gp,n */
03341                || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
03342                || inst == 0x033ce021 /* addu $gp,$t9,$gp */
03343               )
03344         {
03345           /* These instructions are part of the prologue, but we don't
03346              need to do anything special to handle them.  */
03347         }
03348       /* The instructions below load $at or $t0 with an immediate
03349          value in preparation for a stack adjustment via
03350          subu $sp,$sp,[$at,$t0].  These instructions could also
03351          initialize a local variable, so we accept them only before
03352          a stack adjustment instruction was seen.  */
03353       else if (!seen_sp_adjust
03354                && (high_word == 0x3c01 /* lui $at,n */
03355                    || high_word == 0x3c08 /* lui $t0,n */
03356                    || high_word == 0x3421 /* ori $at,$at,n */
03357                    || high_word == 0x3508 /* ori $t0,$t0,n */
03358                    || high_word == 0x3401 /* ori $at,$zero,n */
03359                    || high_word == 0x3408 /* ori $t0,$zero,n */
03360                   ))
03361         {
03362           if (end_prologue_addr == 0)
03363             load_immediate_bytes += MIPS_INSN32_SIZE;           /* FIXME!  */
03364         }
03365       else
03366         {
03367           /* This instruction is not an instruction typically found
03368              in a prologue, so we must have reached the end of the
03369              prologue.  */
03370           /* FIXME: brobecker/2004-10-10: Can't we just break out of this
03371              loop now?  Why would we need to continue scanning the function
03372              instructions?  */
03373           if (end_prologue_addr == 0)
03374             end_prologue_addr = cur_pc;
03375 
03376           /* Check for branches and jumps.  For now, only jump to
03377              register are caught (i.e. returns).  */
03378           if ((itype_op (inst) & 0x07) == 0 && rtype_funct (inst) == 8)
03379             in_delay_slot = 1;
03380         }
03381 
03382       /* If the previous instruction was a jump, we must have reached
03383          the end of the prologue by now.  Stop scanning so that we do
03384          not go past the function return.  */
03385       if (in_delay_slot)
03386         break;
03387     }
03388 
03389   if (this_cache != NULL)
03390     {
03391       this_cache->base = 
03392         (get_frame_register_signed (this_frame,
03393                                     gdbarch_num_regs (gdbarch) + frame_reg)
03394          + frame_offset);
03395       /* FIXME: brobecker/2004-09-15: We should be able to get rid of
03396          this assignment below, eventually.  But it's still needed
03397          for now.  */
03398       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
03399                              + mips_regnum (gdbarch)->pc]
03400         = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
03401                                  + MIPS_RA_REGNUM];
03402     }
03403 
03404   /* If we didn't reach the end of the prologue when scanning the function
03405      instructions, then set end_prologue_addr to the address of the
03406      instruction immediately after the last one we scanned.  */
03407   /* brobecker/2004-10-10: I don't think this would ever happen, but
03408      we may as well be careful and do our best if we have a null
03409      end_prologue_addr.  */
03410   if (end_prologue_addr == 0)
03411     end_prologue_addr = cur_pc;
03412      
03413   /* In a frameless function, we might have incorrectly
03414      skipped some load immediate instructions.  Undo the skipping
03415      if the load immediate was not followed by a stack adjustment.  */
03416   if (load_immediate_bytes && !seen_sp_adjust)
03417     end_prologue_addr -= load_immediate_bytes;
03418 
03419   return end_prologue_addr;
03420 }
03421 
03422 /* Heuristic unwinder for procedures using 32-bit instructions (covers
03423    both 32-bit and 64-bit MIPS ISAs).  Procedures using 16-bit
03424    instructions (a.k.a. MIPS16) are handled by the mips_insn16
03425    unwinder.  Likewise microMIPS and the mips_micro unwinder. */
03426 
03427 static struct mips_frame_cache *
03428 mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
03429 {
03430   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03431   struct mips_frame_cache *cache;
03432 
03433   if ((*this_cache) != NULL)
03434     return (*this_cache);
03435 
03436   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
03437   (*this_cache) = cache;
03438   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
03439 
03440   /* Analyze the function prologue.  */
03441   {
03442     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
03443     CORE_ADDR start_addr;
03444 
03445     find_pc_partial_function (pc, NULL, &start_addr, NULL);
03446     if (start_addr == 0)
03447       start_addr = heuristic_proc_start (gdbarch, pc);
03448     /* We can't analyze the prologue if we couldn't find the begining
03449        of the function.  */
03450     if (start_addr == 0)
03451       return cache;
03452 
03453     mips32_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
03454   }
03455   
03456   /* gdbarch_sp_regnum contains the value and not the address.  */
03457   trad_frame_set_value (cache->saved_regs,
03458                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
03459                         cache->base);
03460 
03461   return (*this_cache);
03462 }
03463 
03464 static void
03465 mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
03466                            struct frame_id *this_id)
03467 {
03468   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
03469                                                            this_cache);
03470   /* This marks the outermost frame.  */
03471   if (info->base == 0)
03472     return;
03473   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
03474 }
03475 
03476 static struct value *
03477 mips_insn32_frame_prev_register (struct frame_info *this_frame,
03478                                  void **this_cache, int regnum)
03479 {
03480   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
03481                                                            this_cache);
03482   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
03483 }
03484 
03485 static int
03486 mips_insn32_frame_sniffer (const struct frame_unwind *self,
03487                            struct frame_info *this_frame, void **this_cache)
03488 {
03489   CORE_ADDR pc = get_frame_pc (this_frame);
03490   if (mips_pc_is_mips (pc))
03491     return 1;
03492   return 0;
03493 }
03494 
03495 static const struct frame_unwind mips_insn32_frame_unwind =
03496 {
03497   NORMAL_FRAME,
03498   default_frame_unwind_stop_reason,
03499   mips_insn32_frame_this_id,
03500   mips_insn32_frame_prev_register,
03501   NULL,
03502   mips_insn32_frame_sniffer
03503 };
03504 
03505 static CORE_ADDR
03506 mips_insn32_frame_base_address (struct frame_info *this_frame,
03507                                 void **this_cache)
03508 {
03509   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
03510                                                            this_cache);
03511   return info->base;
03512 }
03513 
03514 static const struct frame_base mips_insn32_frame_base =
03515 {
03516   &mips_insn32_frame_unwind,
03517   mips_insn32_frame_base_address,
03518   mips_insn32_frame_base_address,
03519   mips_insn32_frame_base_address
03520 };
03521 
03522 static const struct frame_base *
03523 mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
03524 {
03525   CORE_ADDR pc = get_frame_pc (this_frame);
03526   if (mips_pc_is_mips (pc))
03527     return &mips_insn32_frame_base;
03528   else
03529     return NULL;
03530 }
03531 
03532 static struct trad_frame_cache *
03533 mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
03534 {
03535   CORE_ADDR pc;
03536   CORE_ADDR start_addr;
03537   CORE_ADDR stack_addr;
03538   struct trad_frame_cache *this_trad_cache;
03539   struct gdbarch *gdbarch = get_frame_arch (this_frame);
03540   int num_regs = gdbarch_num_regs (gdbarch);
03541 
03542   if ((*this_cache) != NULL)
03543     return (*this_cache);
03544   this_trad_cache = trad_frame_cache_zalloc (this_frame);
03545   (*this_cache) = this_trad_cache;
03546 
03547   /* The return address is in the link register.  */
03548   trad_frame_set_reg_realreg (this_trad_cache,
03549                               gdbarch_pc_regnum (gdbarch),
03550                               num_regs + MIPS_RA_REGNUM);
03551 
03552   /* Frame ID, since it's a frameless / stackless function, no stack
03553      space is allocated and SP on entry is the current SP.  */
03554   pc = get_frame_pc (this_frame);
03555   find_pc_partial_function (pc, NULL, &start_addr, NULL);
03556   stack_addr = get_frame_register_signed (this_frame,
03557                                           num_regs + MIPS_SP_REGNUM);
03558   trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
03559 
03560   /* Assume that the frame's base is the same as the
03561      stack-pointer.  */
03562   trad_frame_set_this_base (this_trad_cache, stack_addr);
03563 
03564   return this_trad_cache;
03565 }
03566 
03567 static void
03568 mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
03569                          struct frame_id *this_id)
03570 {
03571   struct trad_frame_cache *this_trad_cache
03572     = mips_stub_frame_cache (this_frame, this_cache);
03573   trad_frame_get_id (this_trad_cache, this_id);
03574 }
03575 
03576 static struct value *
03577 mips_stub_frame_prev_register (struct frame_info *this_frame,
03578                                void **this_cache, int regnum)
03579 {
03580   struct trad_frame_cache *this_trad_cache
03581     = mips_stub_frame_cache (this_frame, this_cache);
03582   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
03583 }
03584 
03585 static int
03586 mips_stub_frame_sniffer (const struct frame_unwind *self,
03587                          struct frame_info *this_frame, void **this_cache)
03588 {
03589   gdb_byte dummy[4];
03590   struct obj_section *s;
03591   CORE_ADDR pc = get_frame_address_in_block (this_frame);
03592   struct bound_minimal_symbol msym;
03593 
03594   /* Use the stub unwinder for unreadable code.  */
03595   if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
03596     return 1;
03597 
03598   if (in_plt_section (pc) || in_mips_stubs_section (pc))
03599     return 1;
03600 
03601   /* Calling a PIC function from a non-PIC function passes through a
03602      stub.  The stub for foo is named ".pic.foo".  */
03603   msym = lookup_minimal_symbol_by_pc (pc);
03604   if (msym.minsym != NULL
03605       && SYMBOL_LINKAGE_NAME (msym.minsym) != NULL
03606       && strncmp (SYMBOL_LINKAGE_NAME (msym.minsym), ".pic.", 5) == 0)
03607     return 1;
03608 
03609   return 0;
03610 }
03611 
03612 static const struct frame_unwind mips_stub_frame_unwind =
03613 {
03614   NORMAL_FRAME,
03615   default_frame_unwind_stop_reason,
03616   mips_stub_frame_this_id,
03617   mips_stub_frame_prev_register,
03618   NULL,
03619   mips_stub_frame_sniffer
03620 };
03621 
03622 static CORE_ADDR
03623 mips_stub_frame_base_address (struct frame_info *this_frame,
03624                               void **this_cache)
03625 {
03626   struct trad_frame_cache *this_trad_cache
03627     = mips_stub_frame_cache (this_frame, this_cache);
03628   return trad_frame_get_this_base (this_trad_cache);
03629 }
03630 
03631 static const struct frame_base mips_stub_frame_base =
03632 {
03633   &mips_stub_frame_unwind,
03634   mips_stub_frame_base_address,
03635   mips_stub_frame_base_address,
03636   mips_stub_frame_base_address
03637 };
03638 
03639 static const struct frame_base *
03640 mips_stub_frame_base_sniffer (struct frame_info *this_frame)
03641 {
03642   if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
03643     return &mips_stub_frame_base;
03644   else
03645     return NULL;
03646 }
03647 
03648 /* mips_addr_bits_remove - remove useless address bits  */
03649 
03650 static CORE_ADDR
03651 mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
03652 {
03653   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
03654 
03655   if (is_compact_addr (addr))
03656     addr = unmake_compact_addr (addr);
03657 
03658   if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
03659     /* This hack is a work-around for existing boards using PMON, the
03660        simulator, and any other 64-bit targets that doesn't have true
03661        64-bit addressing.  On these targets, the upper 32 bits of
03662        addresses are ignored by the hardware.  Thus, the PC or SP are
03663        likely to have been sign extended to all 1s by instruction
03664        sequences that load 32-bit addresses.  For example, a typical
03665        piece of code that loads an address is this:
03666 
03667        lui $r2, <upper 16 bits>
03668        ori $r2, <lower 16 bits>
03669 
03670        But the lui sign-extends the value such that the upper 32 bits
03671        may be all 1s.  The workaround is simply to mask off these
03672        bits.  In the future, gcc may be changed to support true 64-bit
03673        addressing, and this masking will have to be disabled.  */
03674     return addr &= 0xffffffffUL;
03675   else
03676     return addr;
03677 }
03678 
03679 
03680 /* Checks for an atomic sequence of instructions beginning with a LL/LLD
03681    instruction and ending with a SC/SCD instruction.  If such a sequence
03682    is found, attempt to step through it.  A breakpoint is placed at the end of 
03683    the sequence.  */
03684 
03685 /* Instructions used during single-stepping of atomic sequences, standard
03686    ISA version.  */
03687 #define LL_OPCODE 0x30
03688 #define LLD_OPCODE 0x34
03689 #define SC_OPCODE 0x38
03690 #define SCD_OPCODE 0x3c
03691 
03692 static int
03693 mips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
03694                                 struct address_space *aspace, CORE_ADDR pc)
03695 {
03696   CORE_ADDR breaks[2] = {-1, -1};
03697   CORE_ADDR loc = pc;
03698   CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination.  */
03699   ULONGEST insn;
03700   int insn_count;
03701   int index;
03702   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */  
03703   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
03704 
03705   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
03706   /* Assume all atomic sequences start with a ll/lld instruction.  */
03707   if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
03708     return 0;
03709 
03710   /* Assume that no atomic sequence is longer than "atomic_sequence_length" 
03711      instructions.  */
03712   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
03713     {
03714       int is_branch = 0;
03715       loc += MIPS_INSN32_SIZE;
03716       insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
03717 
03718       /* Assume that there is at most one branch in the atomic
03719          sequence.  If a branch is found, put a breakpoint in its
03720          destination address.  */
03721       switch (itype_op (insn))
03722         {
03723         case 0: /* SPECIAL */
03724           if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
03725             return 0; /* fallback to the standard single-step code.  */
03726           break;
03727         case 1: /* REGIMM */
03728           is_branch = ((itype_rt (insn) & 0xc) == 0 /* B{LT,GE}Z* */
03729                        || ((itype_rt (insn) & 0x1e) == 0
03730                            && itype_rs (insn) == 0)); /* BPOSGE* */
03731           break;
03732         case 2: /* J */
03733         case 3: /* JAL */
03734           return 0; /* fallback to the standard single-step code.  */
03735         case 4: /* BEQ */
03736         case 5: /* BNE */
03737         case 6: /* BLEZ */
03738         case 7: /* BGTZ */
03739         case 20: /* BEQL */
03740         case 21: /* BNEL */
03741         case 22: /* BLEZL */
03742         case 23: /* BGTTL */
03743           is_branch = 1;
03744           break;
03745         case 17: /* COP1 */
03746           is_branch = ((itype_rs (insn) == 9 || itype_rs (insn) == 10)
03747                        && (itype_rt (insn) & 0x2) == 0);
03748           if (is_branch) /* BC1ANY2F, BC1ANY2T, BC1ANY4F, BC1ANY4T */
03749             break;
03750         /* Fall through.  */
03751         case 18: /* COP2 */
03752         case 19: /* COP3 */
03753           is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
03754           break;
03755         }
03756       if (is_branch)
03757         {
03758           branch_bp = loc + mips32_relative_offset (insn) + 4;
03759           if (last_breakpoint >= 1)
03760             return 0; /* More than one branch found, fallback to the
03761                          standard single-step code.  */
03762           breaks[1] = branch_bp;
03763           last_breakpoint++;
03764         }
03765 
03766       if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
03767         break;
03768     }
03769 
03770   /* Assume that the atomic sequence ends with a sc/scd instruction.  */
03771   if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
03772     return 0;
03773 
03774   loc += MIPS_INSN32_SIZE;
03775 
03776   /* Insert a breakpoint right after the end of the atomic sequence.  */
03777   breaks[0] = loc;
03778 
03779   /* Check for duplicated breakpoints.  Check also for a breakpoint
03780      placed (branch instruction's destination) in the atomic sequence.  */
03781   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
03782     last_breakpoint = 0;
03783 
03784   /* Effectively inserts the breakpoints.  */
03785   for (index = 0; index <= last_breakpoint; index++)
03786     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
03787 
03788   return 1;
03789 }
03790 
03791 static int
03792 micromips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
03793                                      struct address_space *aspace,
03794                                      CORE_ADDR pc)
03795 {
03796   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
03797   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
03798   CORE_ADDR breaks[2] = {-1, -1};
03799   CORE_ADDR branch_bp = 0; /* Breakpoint at branch instruction's
03800                               destination.  */
03801   CORE_ADDR loc = pc;
03802   int sc_found = 0;
03803   ULONGEST insn;
03804   int insn_count;
03805   int index;
03806 
03807   /* Assume all atomic sequences start with a ll/lld instruction.  */
03808   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
03809   if (micromips_op (insn) != 0x18)      /* POOL32C: bits 011000 */
03810     return 0;
03811   loc += MIPS_INSN16_SIZE;
03812   insn <<= 16;
03813   insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
03814   if ((b12s4_op (insn) & 0xb) != 0x3)   /* LL, LLD: bits 011000 0x11 */
03815     return 0;
03816   loc += MIPS_INSN16_SIZE;
03817 
03818   /* Assume all atomic sequences end with an sc/scd instruction.  Assume
03819      that no atomic sequence is longer than "atomic_sequence_length"
03820      instructions.  */
03821   for (insn_count = 0;
03822        !sc_found && insn_count < atomic_sequence_length;
03823        ++insn_count)
03824     {
03825       int is_branch = 0;
03826 
03827       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
03828       loc += MIPS_INSN16_SIZE;
03829 
03830       /* Assume that there is at most one conditional branch in the
03831          atomic sequence.  If a branch is found, put a breakpoint in
03832          its destination address.  */
03833       switch (mips_insn_size (ISA_MICROMIPS, insn))
03834         {
03835         /* 48-bit instructions.  */
03836         case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
03837           loc += 2 * MIPS_INSN16_SIZE;
03838           break;
03839 
03840         /* 32-bit instructions.  */
03841         case 2 * MIPS_INSN16_SIZE:
03842           switch (micromips_op (insn))
03843             {
03844             case 0x10: /* POOL32I: bits 010000 */
03845               if ((b5s5_op (insn) & 0x18) != 0x0
03846                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
03847                                 /* BLEZ, BNEZC, BGTZ, BEQZC: 010000 001xx */
03848                   && (b5s5_op (insn) & 0x1d) != 0x11
03849                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
03850                   && ((b5s5_op (insn) & 0x1e) != 0x14
03851                       || (insn & 0x3) != 0x0)
03852                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
03853                   && (b5s5_op (insn) & 0x1e) != 0x1a
03854                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
03855                   && ((b5s5_op (insn) & 0x1e) != 0x1c
03856                       || (insn & 0x3) != 0x0)
03857                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
03858                   && ((b5s5_op (insn) & 0x1c) != 0x1c
03859                       || (insn & 0x3) != 0x1))
03860                                 /* BC1ANY*: bits 010000 111xx xxx01 */
03861                 break;
03862               /* Fall through.  */
03863 
03864             case 0x25: /* BEQ: bits 100101 */
03865             case 0x2d: /* BNE: bits 101101 */
03866               insn <<= 16;
03867               insn |= mips_fetch_instruction (gdbarch,
03868                                               ISA_MICROMIPS, loc, NULL);
03869               branch_bp = (loc + MIPS_INSN16_SIZE
03870                            + micromips_relative_offset16 (insn));
03871               is_branch = 1;
03872               break;
03873 
03874             case 0x00: /* POOL32A: bits 000000 */
03875               insn <<= 16;
03876               insn |= mips_fetch_instruction (gdbarch,
03877                                               ISA_MICROMIPS, loc, NULL);
03878               if (b0s6_op (insn) != 0x3c
03879                                 /* POOL32Axf: bits 000000 ... 111100 */
03880                   || (b6s10_ext (insn) & 0x2bf) != 0x3c)
03881                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
03882                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
03883                 break;
03884               /* Fall through.  */
03885 
03886             case 0x1d: /* JALS: bits 011101 */
03887             case 0x35: /* J: bits 110101 */
03888             case 0x3d: /* JAL: bits 111101 */
03889             case 0x3c: /* JALX: bits 111100 */
03890               return 0; /* Fall back to the standard single-step code. */
03891 
03892             case 0x18: /* POOL32C: bits 011000 */
03893               if ((b12s4_op (insn) & 0xb) == 0xb)
03894                                 /* SC, SCD: bits 011000 1x11 */
03895                 sc_found = 1;
03896               break;
03897             }
03898           loc += MIPS_INSN16_SIZE;
03899           break;
03900 
03901         /* 16-bit instructions.  */
03902         case MIPS_INSN16_SIZE:
03903           switch (micromips_op (insn))
03904             {
03905             case 0x23: /* BEQZ16: bits 100011 */
03906             case 0x2b: /* BNEZ16: bits 101011 */
03907               branch_bp = loc + micromips_relative_offset7 (insn);
03908               is_branch = 1;
03909               break;
03910 
03911             case 0x11: /* POOL16C: bits 010001 */
03912               if ((b5s5_op (insn) & 0x1c) != 0xc
03913                                 /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
03914                   && b5s5_op (insn) != 0x18)
03915                                 /* JRADDIUSP: bits 010001 11000 */
03916                 break;
03917               return 0; /* Fall back to the standard single-step code. */
03918 
03919             case 0x33: /* B16: bits 110011 */
03920               return 0; /* Fall back to the standard single-step code. */
03921             }
03922           break;
03923         }
03924       if (is_branch)
03925         {
03926           if (last_breakpoint >= 1)
03927             return 0; /* More than one branch found, fallback to the
03928                          standard single-step code.  */
03929           breaks[1] = branch_bp;
03930           last_breakpoint++;
03931         }
03932     }
03933   if (!sc_found)
03934     return 0;
03935 
03936   /* Insert a breakpoint right after the end of the atomic sequence.  */
03937   breaks[0] = loc;
03938 
03939   /* Check for duplicated breakpoints.  Check also for a breakpoint
03940      placed (branch instruction's destination) in the atomic sequence */
03941   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
03942     last_breakpoint = 0;
03943 
03944   /* Effectively inserts the breakpoints.  */
03945   for (index = 0; index <= last_breakpoint; index++)
03946     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
03947 
03948   return 1;
03949 }
03950 
03951 static int
03952 deal_with_atomic_sequence (struct gdbarch *gdbarch,
03953                            struct address_space *aspace, CORE_ADDR pc)
03954 {
03955   if (mips_pc_is_mips (pc))
03956     return mips_deal_with_atomic_sequence (gdbarch, aspace, pc);
03957   else if (mips_pc_is_micromips (gdbarch, pc))
03958     return micromips_deal_with_atomic_sequence (gdbarch, aspace, pc);
03959   else
03960     return 0;
03961 }
03962 
03963 /* mips_software_single_step() is called just before we want to resume
03964    the inferior, if we want to single-step it but there is no hardware
03965    or kernel single-step support (MIPS on GNU/Linux for example).  We find
03966    the target of the coming instruction and breakpoint it.  */
03967 
03968 int
03969 mips_software_single_step (struct frame_info *frame)
03970 {
03971   struct gdbarch *gdbarch = get_frame_arch (frame);
03972   struct address_space *aspace = get_frame_address_space (frame);
03973   CORE_ADDR pc, next_pc;
03974 
03975   pc = get_frame_pc (frame);
03976   if (deal_with_atomic_sequence (gdbarch, aspace, pc))
03977     return 1;
03978 
03979   next_pc = mips_next_pc (frame, pc);
03980 
03981   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
03982   return 1;
03983 }
03984 
03985 /* Test whether the PC points to the return instruction at the
03986    end of a function.  */
03987 
03988 static int
03989 mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
03990 {
03991   ULONGEST insn;
03992   ULONGEST hint;
03993 
03994   /* This used to check for MIPS16, but this piece of code is never
03995      called for MIPS16 functions.  And likewise microMIPS ones.  */
03996   gdb_assert (mips_pc_is_mips (pc));
03997 
03998   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
03999   hint = 0x7c0;
04000   return (insn & ~hint) == 0x3e00008;                   /* jr(.hb) $ra */
04001 }
04002 
04003 
04004 /* This fencepost looks highly suspicious to me.  Removing it also
04005    seems suspicious as it could affect remote debugging across serial
04006    lines.  */
04007 
04008 static CORE_ADDR
04009 heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
04010 {
04011   CORE_ADDR start_pc;
04012   CORE_ADDR fence;
04013   int instlen;
04014   int seen_adjsp = 0;
04015   struct inferior *inf;
04016 
04017   pc = gdbarch_addr_bits_remove (gdbarch, pc);
04018   start_pc = pc;
04019   fence = start_pc - heuristic_fence_post;
04020   if (start_pc == 0)
04021     return 0;
04022 
04023   if (heuristic_fence_post == -1 || fence < VM_MIN_ADDRESS)
04024     fence = VM_MIN_ADDRESS;
04025 
04026   instlen = mips_pc_is_mips (pc) ? MIPS_INSN32_SIZE : MIPS_INSN16_SIZE;
04027 
04028   inf = current_inferior ();
04029 
04030   /* Search back for previous return.  */
04031   for (start_pc -= instlen;; start_pc -= instlen)
04032     if (start_pc < fence)
04033       {
04034         /* It's not clear to me why we reach this point when
04035            stop_soon, but with this test, at least we
04036            don't print out warnings for every child forked (eg, on
04037            decstation).  22apr93 rich@cygnus.com.  */
04038         if (inf->control.stop_soon == NO_STOP_QUIETLY)
04039           {
04040             static int blurb_printed = 0;
04041 
04042             warning (_("GDB can't find the start of the function at %s."),
04043                      paddress (gdbarch, pc));
04044 
04045             if (!blurb_printed)
04046               {
04047                 /* This actually happens frequently in embedded
04048                    development, when you first connect to a board
04049                    and your stack pointer and pc are nowhere in
04050                    particular.  This message needs to give people
04051                    in that situation enough information to
04052                    determine that it's no big deal.  */
04053                 printf_filtered ("\n\
04054     GDB is unable to find the start of the function at %s\n\
04055 and thus can't determine the size of that function's stack frame.\n\
04056 This means that GDB may be unable to access that stack frame, or\n\
04057 the frames below it.\n\
04058     This problem is most likely caused by an invalid program counter or\n\
04059 stack pointer.\n\
04060     However, if you think GDB should simply search farther back\n\
04061 from %s for code which looks like the beginning of a\n\
04062 function, you can increase the range of the search using the `set\n\
04063 heuristic-fence-post' command.\n",
04064                         paddress (gdbarch, pc), paddress (gdbarch, pc));
04065                 blurb_printed = 1;
04066               }
04067           }
04068 
04069         return 0;
04070       }
04071     else if (mips_pc_is_mips16 (gdbarch, start_pc))
04072       {
04073         unsigned short inst;
04074 
04075         /* On MIPS16, any one of the following is likely to be the
04076            start of a function:
04077            extend save
04078            save
04079            entry
04080            addiu sp,-n
04081            daddiu sp,-n
04082            extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'.  */
04083         inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, start_pc, NULL);
04084         if ((inst & 0xff80) == 0x6480)          /* save */
04085           {
04086             if (start_pc - instlen >= fence)
04087               {
04088                 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16,
04089                                                start_pc - instlen, NULL);
04090                 if ((inst & 0xf800) == 0xf000)  /* extend */
04091                   start_pc -= instlen;
04092               }
04093             break;
04094           }
04095         else if (((inst & 0xf81f) == 0xe809
04096                   && (inst & 0x700) != 0x700)   /* entry */
04097                  || (inst & 0xff80) == 0x6380   /* addiu sp,-n */
04098                  || (inst & 0xff80) == 0xfb80   /* daddiu sp,-n */
04099                  || ((inst & 0xf810) == 0xf010 && seen_adjsp))  /* extend -n */
04100           break;
04101         else if ((inst & 0xff00) == 0x6300      /* addiu sp */
04102                  || (inst & 0xff00) == 0xfb00)  /* daddiu sp */
04103           seen_adjsp = 1;
04104         else
04105           seen_adjsp = 0;
04106       }
04107     else if (mips_pc_is_micromips (gdbarch, start_pc))
04108       {
04109         ULONGEST insn;
04110         int stop = 0;
04111         long offset;
04112         int dreg;
04113         int sreg;
04114 
04115         /* On microMIPS, any one of the following is likely to be the
04116            start of a function:
04117            ADDIUSP -imm
04118            (D)ADDIU $sp, -imm
04119            LUI $gp, imm  */
04120         insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
04121         switch (micromips_op (insn))
04122           {
04123           case 0xc: /* ADDIU: bits 001100 */
04124           case 0x17: /* DADDIU: bits 010111 */
04125             sreg = b0s5_reg (insn);
04126             dreg = b5s5_reg (insn);
04127             insn <<= 16;
04128             insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS,
04129                                             pc + MIPS_INSN16_SIZE, NULL);
04130             offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
04131             if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
04132                                 /* (D)ADDIU $sp, imm */
04133                 && offset < 0)
04134               stop = 1;
04135             break;
04136 
04137           case 0x10: /* POOL32I: bits 010000 */
04138             if (b5s5_op (insn) == 0xd
04139                                 /* LUI: bits 010000 001101 */
04140                 && b0s5_reg (insn >> 16) == 28)
04141                                 /* LUI $gp, imm */
04142               stop = 1;
04143             break;
04144 
04145           case 0x13: /* POOL16D: bits 010011 */
04146             if ((insn & 0x1) == 0x1)
04147                                 /* ADDIUSP: bits 010011 1 */
04148               {
04149                 offset = micromips_decode_imm9 (b1s9_imm (insn));
04150                 if (offset < 0)
04151                                 /* ADDIUSP -imm */
04152                   stop = 1;
04153               }
04154             else
04155                                 /* ADDIUS5: bits 010011 0 */
04156               {
04157                 dreg = b5s5_reg (insn);
04158                 offset = (b1s4_imm (insn) ^ 8) - 8;
04159                 if (dreg == MIPS_SP_REGNUM && offset < 0)
04160                                 /* ADDIUS5  $sp, -imm */
04161                   stop = 1;
04162               }
04163             break;
04164           }
04165         if (stop)
04166           break;
04167       }
04168     else if (mips_about_to_return (gdbarch, start_pc))
04169       {
04170         /* Skip return and its delay slot.  */
04171         start_pc += 2 * MIPS_INSN32_SIZE;
04172         break;
04173       }
04174 
04175   return start_pc;
04176 }
04177 
04178 struct mips_objfile_private
04179 {
04180   bfd_size_type size;
04181   char *contents;
04182 };
04183 
04184 /* According to the current ABI, should the type be passed in a
04185    floating-point register (assuming that there is space)?  When there
04186    is no FPU, FP are not even considered as possible candidates for
04187    FP registers and, consequently this returns false - forces FP
04188    arguments into integer registers.  */
04189 
04190 static int
04191 fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
04192                    struct type *arg_type)
04193 {
04194   return ((typecode == TYPE_CODE_FLT
04195            || (MIPS_EABI (gdbarch)
04196                && (typecode == TYPE_CODE_STRUCT
04197                    || typecode == TYPE_CODE_UNION)
04198                && TYPE_NFIELDS (arg_type) == 1
04199                && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0))) 
04200                == TYPE_CODE_FLT))
04201           && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
04202 }
04203 
04204 /* On o32, argument passing in GPRs depends on the alignment of the type being
04205    passed.  Return 1 if this type must be aligned to a doubleword boundary.  */
04206 
04207 static int
04208 mips_type_needs_double_align (struct type *type)
04209 {
04210   enum type_code typecode = TYPE_CODE (type);
04211 
04212   if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
04213     return 1;
04214   else if (typecode == TYPE_CODE_STRUCT)
04215     {
04216       if (TYPE_NFIELDS (type) < 1)
04217         return 0;
04218       return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
04219     }
04220   else if (typecode == TYPE_CODE_UNION)
04221     {
04222       int i, n;
04223 
04224       n = TYPE_NFIELDS (type);
04225       for (i = 0; i < n; i++)
04226         if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
04227           return 1;
04228       return 0;
04229     }
04230   return 0;
04231 }
04232 
04233 /* Adjust the address downward (direction of stack growth) so that it
04234    is correctly aligned for a new stack frame.  */
04235 static CORE_ADDR
04236 mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
04237 {
04238   return align_down (addr, 16);
04239 }
04240 
04241 /* Implement the "push_dummy_code" gdbarch method.  */
04242 
04243 static CORE_ADDR
04244 mips_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
04245                       CORE_ADDR funaddr, struct value **args,
04246                       int nargs, struct type *value_type,
04247                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
04248                       struct regcache *regcache)
04249 {
04250   static gdb_byte nop_insn[] = { 0, 0, 0, 0 };
04251   CORE_ADDR nop_addr;
04252   CORE_ADDR bp_slot;
04253 
04254   /* Reserve enough room on the stack for our breakpoint instruction.  */
04255   bp_slot = sp - sizeof (nop_insn);
04256 
04257   /* Return to microMIPS mode if calling microMIPS code to avoid
04258      triggering an address error exception on processors that only
04259      support microMIPS execution.  */
04260   *bp_addr = (mips_pc_is_micromips (gdbarch, funaddr)
04261               ? make_compact_addr (bp_slot) : bp_slot);
04262 
04263   /* The breakpoint layer automatically adjusts the address of
04264      breakpoints inserted in a branch delay slot.  With enough
04265      bad luck, the 4 bytes located just before our breakpoint
04266      instruction could look like a branch instruction, and thus
04267      trigger the adjustement, and break the function call entirely.
04268      So, we reserve those 4 bytes and write a nop instruction
04269      to prevent that from happening.  */
04270   nop_addr = bp_slot - sizeof (nop_insn);
04271   write_memory (nop_addr, nop_insn, sizeof (nop_insn));
04272   sp = mips_frame_align (gdbarch, nop_addr);
04273 
04274   /* Inferior resumes at the function entry point.  */
04275   *real_pc = funaddr;
04276 
04277   return sp;
04278 }
04279 
04280 static CORE_ADDR
04281 mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
04282                            struct regcache *regcache, CORE_ADDR bp_addr,
04283                            int nargs, struct value **args, CORE_ADDR sp,
04284                            int struct_return, CORE_ADDR struct_addr)
04285 {
04286   int argreg;
04287   int float_argreg;
04288   int argnum;
04289   int len = 0;
04290   int stack_offset = 0;
04291   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04292   CORE_ADDR func_addr = find_function_addr (function, NULL);
04293   int regsize = mips_abi_regsize (gdbarch);
04294 
04295   /* For shared libraries, "t9" needs to point at the function
04296      address.  */
04297   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
04298 
04299   /* Set the return address register to point to the entry point of
04300      the program, where a breakpoint lies in wait.  */
04301   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
04302 
04303   /* First ensure that the stack and structure return address (if any)
04304      are properly aligned.  The stack has to be at least 64-bit
04305      aligned even on 32-bit machines, because doubles must be 64-bit
04306      aligned.  For n32 and n64, stack frames need to be 128-bit
04307      aligned, so we round to this widest known alignment.  */
04308 
04309   sp = align_down (sp, 16);
04310   struct_addr = align_down (struct_addr, 16);
04311 
04312   /* Now make space on the stack for the args.  We allocate more
04313      than necessary for EABI, because the first few arguments are
04314      passed in registers, but that's OK.  */
04315   for (argnum = 0; argnum < nargs; argnum++)
04316     len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
04317   sp -= align_up (len, 16);
04318 
04319   if (mips_debug)
04320     fprintf_unfiltered (gdb_stdlog,
04321                         "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
04322                         paddress (gdbarch, sp), (long) align_up (len, 16));
04323 
04324   /* Initialize the integer and float register pointers.  */
04325   argreg = MIPS_A0_REGNUM;
04326   float_argreg = mips_fpa0_regnum (gdbarch);
04327 
04328   /* The struct_return pointer occupies the first parameter-passing reg.  */
04329   if (struct_return)
04330     {
04331       if (mips_debug)
04332         fprintf_unfiltered (gdb_stdlog,
04333                             "mips_eabi_push_dummy_call: "
04334                             "struct_return reg=%d %s\n",
04335                             argreg, paddress (gdbarch, struct_addr));
04336       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
04337     }
04338 
04339   /* Now load as many as possible of the first arguments into
04340      registers, and push the rest onto the stack.  Loop thru args
04341      from first to last.  */
04342   for (argnum = 0; argnum < nargs; argnum++)
04343     {
04344       const gdb_byte *val;
04345       gdb_byte valbuf[MAX_REGISTER_SIZE];
04346       struct value *arg = args[argnum];
04347       struct type *arg_type = check_typedef (value_type (arg));
04348       int len = TYPE_LENGTH (arg_type);
04349       enum type_code typecode = TYPE_CODE (arg_type);
04350 
04351       if (mips_debug)
04352         fprintf_unfiltered (gdb_stdlog,
04353                             "mips_eabi_push_dummy_call: %d len=%d type=%d",
04354                             argnum + 1, len, (int) typecode);
04355 
04356       /* Function pointer arguments to mips16 code need to be made into
04357          mips16 pointers.  */
04358       if (typecode == TYPE_CODE_PTR
04359           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
04360         {
04361           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
04362                                                    len, byte_order);
04363           if (mips_pc_is_mips (addr))
04364             val = value_contents (arg);
04365           else
04366             {
04367               store_signed_integer (valbuf, len, byte_order, 
04368                                     make_compact_addr (addr));
04369               val = valbuf;
04370             }
04371         }
04372       /* The EABI passes structures that do not fit in a register by
04373          reference.  */
04374       else if (len > regsize
04375           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
04376         {
04377           store_unsigned_integer (valbuf, regsize, byte_order,
04378                                   value_address (arg));
04379           typecode = TYPE_CODE_PTR;
04380           len = regsize;
04381           val = valbuf;
04382           if (mips_debug)
04383             fprintf_unfiltered (gdb_stdlog, " push");
04384         }
04385       else
04386         val = value_contents (arg);
04387 
04388       /* 32-bit ABIs always start floating point arguments in an
04389          even-numbered floating point register.  Round the FP register
04390          up before the check to see if there are any FP registers
04391          left.  Non MIPS_EABI targets also pass the FP in the integer
04392          registers so also round up normal registers.  */
04393       if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
04394         {
04395           if ((float_argreg & 1))
04396             float_argreg++;
04397         }
04398 
04399       /* Floating point arguments passed in registers have to be
04400          treated specially.  On 32-bit architectures, doubles
04401          are passed in register pairs; the even register gets
04402          the low word, and the odd register gets the high word.
04403          On non-EABI processors, the first two floating point arguments are
04404          also copied to general registers, because MIPS16 functions
04405          don't use float registers for arguments.  This duplication of
04406          arguments in general registers can't hurt non-MIPS16 functions
04407          because those registers are normally skipped.  */
04408       /* MIPS_EABI squeezes a struct that contains a single floating
04409          point value into an FP register instead of pushing it onto the
04410          stack.  */
04411       if (fp_register_arg_p (gdbarch, typecode, arg_type)
04412           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
04413         {
04414           /* EABI32 will pass doubles in consecutive registers, even on
04415              64-bit cores.  At one time, we used to check the size of
04416              `float_argreg' to determine whether or not to pass doubles
04417              in consecutive registers, but this is not sufficient for
04418              making the ABI determination.  */
04419           if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
04420             {
04421               int low_offset = gdbarch_byte_order (gdbarch)
04422                                == BFD_ENDIAN_BIG ? 4 : 0;
04423               long regval;
04424 
04425               /* Write the low word of the double to the even register(s).  */
04426               regval = extract_signed_integer (val + low_offset,
04427                                                4, byte_order);
04428               if (mips_debug)
04429                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
04430                                     float_argreg, phex (regval, 4));
04431               regcache_cooked_write_signed (regcache, float_argreg++, regval);
04432 
04433               /* Write the high word of the double to the odd register(s).  */
04434               regval = extract_signed_integer (val + 4 - low_offset,
04435                                                4, byte_order);
04436               if (mips_debug)
04437                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
04438                                     float_argreg, phex (regval, 4));
04439               regcache_cooked_write_signed (regcache, float_argreg++, regval);
04440             }
04441           else
04442             {
04443               /* This is a floating point value that fits entirely
04444                  in a single register.  */
04445               /* On 32 bit ABI's the float_argreg is further adjusted
04446                  above to ensure that it is even register aligned.  */
04447               LONGEST regval = extract_signed_integer (val, len, byte_order);
04448               if (mips_debug)
04449                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
04450                                     float_argreg, phex (regval, len));
04451               regcache_cooked_write_signed (regcache, float_argreg++, regval);
04452             }
04453         }
04454       else
04455         {
04456           /* Copy the argument to general registers or the stack in
04457              register-sized pieces.  Large arguments are split between
04458              registers and stack.  */
04459           /* Note: structs whose size is not a multiple of regsize
04460              are treated specially: Irix cc passes
04461              them in registers where gcc sometimes puts them on the
04462              stack.  For maximum compatibility, we will put them in
04463              both places.  */
04464           int odd_sized_struct = (len > regsize && len % regsize != 0);
04465 
04466           /* Note: Floating-point values that didn't fit into an FP
04467              register are only written to memory.  */
04468           while (len > 0)
04469             {
04470               /* Remember if the argument was written to the stack.  */
04471               int stack_used_p = 0;
04472               int partial_len = (len < regsize ? len : regsize);
04473 
04474               if (mips_debug)
04475                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
04476                                     partial_len);
04477 
04478               /* Write this portion of the argument to the stack.  */
04479               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
04480                   || odd_sized_struct
04481                   || fp_register_arg_p (gdbarch, typecode, arg_type))
04482                 {
04483                   /* Should shorter than int integer values be
04484                      promoted to int before being stored?  */
04485                   int longword_offset = 0;
04486                   CORE_ADDR addr;
04487                   stack_used_p = 1;
04488                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
04489                     {
04490                       if (regsize == 8
04491                           && (typecode == TYPE_CODE_INT
04492                               || typecode == TYPE_CODE_PTR
04493                               || typecode == TYPE_CODE_FLT) && len <= 4)
04494                         longword_offset = regsize - len;
04495                       else if ((typecode == TYPE_CODE_STRUCT
04496                                 || typecode == TYPE_CODE_UNION)
04497                                && TYPE_LENGTH (arg_type) < regsize)
04498                         longword_offset = regsize - len;
04499                     }
04500 
04501                   if (mips_debug)
04502                     {
04503                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
04504                                           paddress (gdbarch, stack_offset));
04505                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
04506                                           paddress (gdbarch, longword_offset));
04507                     }
04508 
04509                   addr = sp + stack_offset + longword_offset;
04510 
04511                   if (mips_debug)
04512                     {
04513                       int i;
04514                       fprintf_unfiltered (gdb_stdlog, " @%s ",
04515                                           paddress (gdbarch, addr));
04516                       for (i = 0; i < partial_len; i++)
04517                         {
04518                           fprintf_unfiltered (gdb_stdlog, "%02x",
04519                                               val[i] & 0xff);
04520                         }
04521                     }
04522                   write_memory (addr, val, partial_len);
04523                 }
04524 
04525               /* Note!!! This is NOT an else clause.  Odd sized
04526                  structs may go thru BOTH paths.  Floating point
04527                  arguments will not.  */
04528               /* Write this portion of the argument to a general
04529                  purpose register.  */
04530               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
04531                   && !fp_register_arg_p (gdbarch, typecode, arg_type))
04532                 {
04533                   LONGEST regval =
04534                     extract_signed_integer (val, partial_len, byte_order);
04535 
04536                   if (mips_debug)
04537                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
04538                                       argreg,
04539                                       phex (regval, regsize));
04540                   regcache_cooked_write_signed (regcache, argreg, regval);
04541                   argreg++;
04542                 }
04543 
04544               len -= partial_len;
04545               val += partial_len;
04546 
04547               /* Compute the offset into the stack at which we will
04548                  copy the next parameter.
04549 
04550                  In the new EABI (and the NABI32), the stack_offset
04551                  only needs to be adjusted when it has been used.  */
04552 
04553               if (stack_used_p)
04554                 stack_offset += align_up (partial_len, regsize);
04555             }
04556         }
04557       if (mips_debug)
04558         fprintf_unfiltered (gdb_stdlog, "\n");
04559     }
04560 
04561   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
04562 
04563   /* Return adjusted stack pointer.  */
04564   return sp;
04565 }
04566 
04567 /* Determine the return value convention being used.  */
04568 
04569 static enum return_value_convention
04570 mips_eabi_return_value (struct gdbarch *gdbarch, struct value *function,
04571                         struct type *type, struct regcache *regcache,
04572                         gdb_byte *readbuf, const gdb_byte *writebuf)
04573 {
04574   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
04575   int fp_return_type = 0;
04576   int offset, regnum, xfer;
04577 
04578   if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
04579     return RETURN_VALUE_STRUCT_CONVENTION;
04580 
04581   /* Floating point type?  */
04582   if (tdep->mips_fpu_type != MIPS_FPU_NONE)
04583     {
04584       if (TYPE_CODE (type) == TYPE_CODE_FLT)
04585         fp_return_type = 1;
04586       /* Structs with a single field of float type 
04587          are returned in a floating point register.  */
04588       if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
04589            || TYPE_CODE (type) == TYPE_CODE_UNION)
04590           && TYPE_NFIELDS (type) == 1)
04591         {
04592           struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);
04593 
04594           if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
04595             fp_return_type = 1;
04596         }
04597     }
04598 
04599   if (fp_return_type)      
04600     {
04601       /* A floating-point value belongs in the least significant part
04602          of FP0/FP1.  */
04603       if (mips_debug)
04604         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
04605       regnum = mips_regnum (gdbarch)->fp0;
04606     }
04607   else 
04608     {
04609       /* An integer value goes in V0/V1.  */
04610       if (mips_debug)
04611         fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
04612       regnum = MIPS_V0_REGNUM;
04613     }
04614   for (offset = 0;
04615        offset < TYPE_LENGTH (type);
04616        offset += mips_abi_regsize (gdbarch), regnum++)
04617     {
04618       xfer = mips_abi_regsize (gdbarch);
04619       if (offset + xfer > TYPE_LENGTH (type))
04620         xfer = TYPE_LENGTH (type) - offset;
04621       mips_xfer_register (gdbarch, regcache,
04622                           gdbarch_num_regs (gdbarch) + regnum, xfer,
04623                           gdbarch_byte_order (gdbarch), readbuf, writebuf,
04624                           offset);
04625     }
04626 
04627   return RETURN_VALUE_REGISTER_CONVENTION;
04628 }
04629 
04630 
04631 /* N32/N64 ABI stuff.  */
04632 
04633 /* Search for a naturally aligned double at OFFSET inside a struct
04634    ARG_TYPE.  The N32 / N64 ABIs pass these in floating point
04635    registers.  */
04636 
04637 static int
04638 mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
04639                             int offset)
04640 {
04641   int i;
04642 
04643   if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
04644     return 0;
04645 
04646   if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
04647     return 0;
04648 
04649   if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
04650     return 0;
04651 
04652   for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
04653     {
04654       int pos;
04655       struct type *field_type;
04656 
04657       /* We're only looking at normal fields.  */
04658       if (field_is_static (&TYPE_FIELD (arg_type, i))
04659           || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
04660         continue;
04661 
04662       /* If we have gone past the offset, there is no double to pass.  */
04663       pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
04664       if (pos > offset)
04665         return 0;
04666 
04667       field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));
04668 
04669       /* If this field is entirely before the requested offset, go
04670          on to the next one.  */
04671       if (pos + TYPE_LENGTH (field_type) <= offset)
04672         continue;
04673 
04674       /* If this is our special aligned double, we can stop.  */
04675       if (TYPE_CODE (field_type) == TYPE_CODE_FLT
04676           && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
04677         return 1;
04678 
04679       /* This field starts at or before the requested offset, and
04680          overlaps it.  If it is a structure, recurse inwards.  */
04681       return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
04682     }
04683 
04684   return 0;
04685 }
04686 
04687 static CORE_ADDR
04688 mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
04689                              struct regcache *regcache, CORE_ADDR bp_addr,
04690                              int nargs, struct value **args, CORE_ADDR sp,
04691                              int struct_return, CORE_ADDR struct_addr)
04692 {
04693   int argreg;
04694   int float_argreg;
04695   int argnum;
04696   int len = 0;
04697   int stack_offset = 0;
04698   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
04699   CORE_ADDR func_addr = find_function_addr (function, NULL);
04700 
04701   /* For shared libraries, "t9" needs to point at the function
04702      address.  */
04703   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
04704 
04705   /* Set the return address register to point to the entry point of
04706      the program, where a breakpoint lies in wait.  */
04707   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
04708 
04709   /* First ensure that the stack and structure return address (if any)
04710      are properly aligned.  The stack has to be at least 64-bit
04711      aligned even on 32-bit machines, because doubles must be 64-bit
04712      aligned.  For n32 and n64, stack frames need to be 128-bit
04713      aligned, so we round to this widest known alignment.  */
04714 
04715   sp = align_down (sp, 16);
04716   struct_addr = align_down (struct_addr, 16);
04717 
04718   /* Now make space on the stack for the args.  */
04719   for (argnum = 0; argnum < nargs; argnum++)
04720     len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
04721   sp -= align_up (len, 16);
04722 
04723   if (mips_debug)
04724     fprintf_unfiltered (gdb_stdlog,
04725                         "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
04726                         paddress (gdbarch, sp), (long) align_up (len, 16));
04727 
04728   /* Initialize the integer and float register pointers.  */
04729   argreg = MIPS_A0_REGNUM;
04730   float_argreg = mips_fpa0_regnum (gdbarch);
04731 
04732   /* The struct_return pointer occupies the first parameter-passing reg.  */
04733   if (struct_return)
04734     {
04735       if (mips_debug)
04736         fprintf_unfiltered (gdb_stdlog,
04737                             "mips_n32n64_push_dummy_call: "
04738                             "struct_return reg=%d %s\n",
04739                             argreg, paddress (gdbarch, struct_addr));
04740       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
04741     }
04742 
04743   /* Now load as many as possible of the first arguments into
04744      registers, and push the rest onto the stack.  Loop thru args
04745      from first to last.  */
04746   for (argnum = 0; argnum < nargs; argnum++)
04747     {
04748       const gdb_byte *val;
04749       struct value *arg = args[argnum];
04750       struct type *arg_type = check_typedef (value_type (arg));
04751       int len = TYPE_LENGTH (arg_type);
04752       enum type_code typecode = TYPE_CODE (arg_type);
04753 
04754       if (mips_debug)
04755         fprintf_unfiltered (gdb_stdlog,
04756                             "mips_n32n64_push_dummy_call: %d len=%d type=%d",
04757                             argnum + 1, len, (int) typecode);
04758 
04759       val = value_contents (arg);
04760 
04761       /* A 128-bit long double value requires an even-odd pair of
04762          floating-point registers.  */
04763       if (len == 16
04764           && fp_register_arg_p (gdbarch, typecode, arg_type)
04765           && (float_argreg & 1))
04766         {
04767           float_argreg++;
04768           argreg++;
04769         }
04770 
04771       if (fp_register_arg_p (gdbarch, typecode, arg_type)
04772           && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
04773         {
04774           /* This is a floating point value that fits entirely
04775              in a single register or a pair of registers.  */
04776           int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
04777           LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
04778           if (mips_debug)
04779             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
04780                                 float_argreg, phex (regval, reglen));
04781           regcache_cooked_write_unsigned (regcache, float_argreg, regval);
04782 
04783           if (mips_debug)
04784             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
04785                                 argreg, phex (regval, reglen));
04786           regcache_cooked_write_unsigned (regcache, argreg, regval);
04787           float_argreg++;
04788           argreg++;
04789           if (len == 16)
04790             {
04791               regval = extract_unsigned_integer (val + reglen,
04792                                                  reglen, byte_order);
04793               if (mips_debug)
04794                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
04795                                     float_argreg, phex (regval, reglen));
04796               regcache_cooked_write_unsigned (regcache, float_argreg, regval);
04797 
04798               if (mips_debug)
04799                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
04800                                     argreg, phex (regval, reglen));
04801               regcache_cooked_write_unsigned (regcache, argreg, regval);
04802               float_argreg++;
04803               argreg++;
04804             }
04805         }
04806       else
04807         {
04808           /* Copy the argument to general registers or the stack in
04809              register-sized pieces.  Large arguments are split between
04810              registers and stack.  */
04811           /* For N32/N64, structs, unions, or other composite types are
04812              treated as a sequence of doublewords, and are passed in integer
04813              or floating point registers as though they were simple scalar
04814              parameters to the extent that they fit, with any excess on the
04815              stack packed according to the normal memory layout of the
04816              object.
04817              The caller does not reserve space for the register arguments;
04818              the callee is responsible for reserving it if required.  */
04819           /* Note: Floating-point values that didn't fit into an FP
04820              register are only written to memory.  */
04821           while (len > 0)
04822             {
04823               /* Remember if the argument was written to the stack.  */
04824               int stack_used_p = 0;
04825               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
04826 
04827               if (mips_debug)
04828                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
04829                                     partial_len);
04830 
04831               if (fp_register_arg_p (gdbarch, typecode, arg_type))
04832                 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));
04833 
04834               /* Write this portion of the argument to the stack.  */
04835               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
04836                 {
04837                   /* Should shorter than int integer values be
04838                      promoted to int before being stored?  */
04839                   int longword_offset = 0;
04840                   CORE_ADDR addr;
04841                   stack_used_p = 1;
04842                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
04843                     {
04844                       if ((typecode == TYPE_CODE_INT
04845                            || typecode == TYPE_CODE_PTR)
04846                           && len <= 4)
04847                         longword_offset = MIPS64_REGSIZE - len;
04848                     }
04849 
04850                   if (mips_debug)
04851                     {
04852                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
04853                                           paddress (gdbarch, stack_offset));
04854                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
04855                                           paddress (gdbarch, longword_offset));
04856                     }
04857 
04858                   addr = sp + stack_offset + longword_offset;
04859 
04860                   if (mips_debug)
04861                     {
04862                       int i;
04863                       fprintf_unfiltered (gdb_stdlog, " @%s ",
04864                                           paddress (gdbarch, addr));
04865                       for (i = 0; i < partial_len; i++)
04866                         {
04867                           fprintf_unfiltered (gdb_stdlog, "%02x",
04868                                               val[i] & 0xff);
04869                         }
04870                     }
04871                   write_memory (addr, val, partial_len);
04872                 }
04873 
04874               /* Note!!! This is NOT an else clause.  Odd sized
04875                  structs may go thru BOTH paths.  */
04876               /* Write this portion of the argument to a general
04877                  purpose register.  */
04878               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
04879                 {
04880                   LONGEST regval;
04881 
04882                   /* Sign extend pointers, 32-bit integers and signed
04883                      16-bit and 8-bit integers; everything else is taken
04884                      as is.  */
04885 
04886                   if ((partial_len == 4
04887                        && (typecode == TYPE_CODE_PTR
04888                            || typecode == TYPE_CODE_INT))
04889                       || (partial_len < 4
04890                           && typecode == TYPE_CODE_INT
04891                           && !TYPE_UNSIGNED (arg_type)))
04892                     regval = extract_signed_integer (val, partial_len,
04893                                                      byte_order);
04894                   else
04895                     regval = extract_unsigned_integer (val, partial_len,
04896                                                        byte_order);
04897 
04898                   /* A non-floating-point argument being passed in a
04899                      general register.  If a struct or union, and if
04900                      the remaining length is smaller than the register
04901                      size, we have to adjust the register value on
04902                      big endian targets.
04903 
04904                      It does not seem to be necessary to do the
04905                      same for integral types.  */
04906 
04907                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
04908                       && partial_len < MIPS64_REGSIZE
04909                       && (typecode == TYPE_CODE_STRUCT
04910                           || typecode == TYPE_CODE_UNION))
04911                     regval <<= ((MIPS64_REGSIZE - partial_len)
04912                                 * TARGET_CHAR_BIT);
04913 
04914                   if (mips_debug)
04915                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
04916                                       argreg,
04917                                       phex (regval, MIPS64_REGSIZE));
04918                   regcache_cooked_write_unsigned (regcache, argreg, regval);
04919 
04920                   if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
04921                                                   TYPE_LENGTH (arg_type) - len))
04922                     {
04923                       if (mips_debug)
04924                         fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
04925                                           float_argreg,
04926                                           phex (regval, MIPS64_REGSIZE));
04927                       regcache_cooked_write_unsigned (regcache, float_argreg,
04928                                                       regval);
04929                     }
04930 
04931                   float_argreg++;
04932                   argreg++;
04933                 }
04934 
04935               len -= partial_len;
04936               val += partial_len;
04937 
04938               /* Compute the offset into the stack at which we will
04939                  copy the next parameter.
04940 
04941                  In N32 (N64?), the stack_offset only needs to be
04942                  adjusted when it has been used.  */
04943 
04944               if (stack_used_p)
04945                 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
04946             }
04947         }
04948       if (mips_debug)
04949         fprintf_unfiltered (gdb_stdlog, "\n");
04950     }
04951 
04952   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
04953 
04954   /* Return adjusted stack pointer.  */
04955   return sp;
04956 }
04957 
04958 static enum return_value_convention
04959 mips_n32n64_return_value (struct gdbarch *gdbarch, struct value *function,
04960                           struct type *type, struct regcache *regcache,
04961                           gdb_byte *readbuf, const gdb_byte *writebuf)
04962 {
04963   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
04964 
04965   /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004
04966 
04967      Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
04968      if needed), as appropriate for the type.  Composite results (struct,
04969      union, or array) are returned in $2/$f0 and $3/$f2 according to the
04970      following rules:
04971 
04972      * A struct with only one or two floating point fields is returned in $f0
04973      (and $f2 if necessary).  This is a generalization of the Fortran COMPLEX
04974      case.
04975 
04976      * Any other composite results of at most 128 bits are returned in
04977      $2 (first 64 bits) and $3 (remainder, if necessary).
04978 
04979      * Larger composite results are handled by converting the function to a
04980      procedure with an implicit first parameter, which is a pointer to an area
04981      reserved by the caller to receive the result.  [The o32-bit ABI requires
04982      that all composite results be handled by conversion to implicit first
04983      parameters.  The MIPS/SGI Fortran implementation has always made a
04984      specific exception to return COMPLEX results in the floating point
04985      registers.]  */
04986 
04987   if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
04988     return RETURN_VALUE_STRUCT_CONVENTION;
04989   else if (TYPE_CODE (type) == TYPE_CODE_FLT
04990            && TYPE_LENGTH (type) == 16
04991            && tdep->mips_fpu_type != MIPS_FPU_NONE)
04992     {
04993       /* A 128-bit floating-point value fills both $f0 and $f2.  The
04994          two registers are used in the same as memory order, so the
04995          eight bytes with the lower memory address are in $f0.  */
04996       if (mips_debug)
04997         fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
04998       mips_xfer_register (gdbarch, regcache,
04999                           (gdbarch_num_regs (gdbarch)
05000                            + mips_regnum (gdbarch)->fp0),
05001                           8, gdbarch_byte_order (gdbarch),
05002                           readbuf, writebuf, 0);
05003       mips_xfer_register (gdbarch, regcache,
05004                           (gdbarch_num_regs (gdbarch)
05005                            + mips_regnum (gdbarch)->fp0 + 2),
05006                           8, gdbarch_byte_order (gdbarch),
05007                           readbuf ? readbuf + 8 : readbuf,
05008                           writebuf ? writebuf + 8 : writebuf, 0);
05009       return RETURN_VALUE_REGISTER_CONVENTION;
05010     }
05011   else if (TYPE_CODE (type) == TYPE_CODE_FLT
05012            && tdep->mips_fpu_type != MIPS_FPU_NONE)
05013     {
05014       /* A single or double floating-point value that fits in FP0.  */
05015       if (mips_debug)
05016         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
05017       mips_xfer_register (gdbarch, regcache,
05018                           (gdbarch_num_regs (gdbarch)
05019                            + mips_regnum (gdbarch)->fp0),
05020                           TYPE_LENGTH (type),
05021                           gdbarch_byte_order (gdbarch),
05022                           readbuf, writebuf, 0);
05023       return RETURN_VALUE_REGISTER_CONVENTION;
05024     }
05025   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05026            && TYPE_NFIELDS (type) <= 2
05027            && TYPE_NFIELDS (type) >= 1
05028            && ((TYPE_NFIELDS (type) == 1
05029                 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
05030                     == TYPE_CODE_FLT))
05031                || (TYPE_NFIELDS (type) == 2
05032                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
05033                        == TYPE_CODE_FLT)
05034                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
05035                        == TYPE_CODE_FLT))))
05036     {
05037       /* A struct that contains one or two floats.  Each value is part
05038          in the least significant part of their floating point
05039          register (or GPR, for soft float).  */
05040       int regnum;
05041       int field;
05042       for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
05043                                 ? mips_regnum (gdbarch)->fp0
05044                                 : MIPS_V0_REGNUM);
05045            field < TYPE_NFIELDS (type); field++, regnum += 2)
05046         {
05047           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
05048                         / TARGET_CHAR_BIT);
05049           if (mips_debug)
05050             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
05051                                 offset);
05052           if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
05053             {
05054               /* A 16-byte long double field goes in two consecutive
05055                  registers.  */
05056               mips_xfer_register (gdbarch, regcache,
05057                                   gdbarch_num_regs (gdbarch) + regnum,
05058                                   8,
05059                                   gdbarch_byte_order (gdbarch),
05060                                   readbuf, writebuf, offset);
05061               mips_xfer_register (gdbarch, regcache,
05062                                   gdbarch_num_regs (gdbarch) + regnum + 1,
05063                                   8,
05064                                   gdbarch_byte_order (gdbarch),
05065                                   readbuf, writebuf, offset + 8);
05066             }
05067           else
05068             mips_xfer_register (gdbarch, regcache,
05069                                 gdbarch_num_regs (gdbarch) + regnum,
05070                                 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
05071                                 gdbarch_byte_order (gdbarch),
05072                                 readbuf, writebuf, offset);
05073         }
05074       return RETURN_VALUE_REGISTER_CONVENTION;
05075     }
05076   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05077            || TYPE_CODE (type) == TYPE_CODE_UNION
05078            || TYPE_CODE (type) == TYPE_CODE_ARRAY)
05079     {
05080       /* A composite type.  Extract the left justified value,
05081          regardless of the byte order.  I.e. DO NOT USE
05082          mips_xfer_lower.  */
05083       int offset;
05084       int regnum;
05085       for (offset = 0, regnum = MIPS_V0_REGNUM;
05086            offset < TYPE_LENGTH (type);
05087            offset += register_size (gdbarch, regnum), regnum++)
05088         {
05089           int xfer = register_size (gdbarch, regnum);
05090           if (offset + xfer > TYPE_LENGTH (type))
05091             xfer = TYPE_LENGTH (type) - offset;
05092           if (mips_debug)
05093             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
05094                                 offset, xfer, regnum);
05095           mips_xfer_register (gdbarch, regcache,
05096                               gdbarch_num_regs (gdbarch) + regnum,
05097                               xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
05098                               offset);
05099         }
05100       return RETURN_VALUE_REGISTER_CONVENTION;
05101     }
05102   else
05103     {
05104       /* A scalar extract each part but least-significant-byte
05105          justified.  */
05106       int offset;
05107       int regnum;
05108       for (offset = 0, regnum = MIPS_V0_REGNUM;
05109            offset < TYPE_LENGTH (type);
05110            offset += register_size (gdbarch, regnum), regnum++)
05111         {
05112           int xfer = register_size (gdbarch, regnum);
05113           if (offset + xfer > TYPE_LENGTH (type))
05114             xfer = TYPE_LENGTH (type) - offset;
05115           if (mips_debug)
05116             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
05117                                 offset, xfer, regnum);
05118           mips_xfer_register (gdbarch, regcache,
05119                               gdbarch_num_regs (gdbarch) + regnum,
05120                               xfer, gdbarch_byte_order (gdbarch),
05121                               readbuf, writebuf, offset);
05122         }
05123       return RETURN_VALUE_REGISTER_CONVENTION;
05124     }
05125 }
05126 
05127 /* Which registers to use for passing floating-point values between
05128    function calls, one of floating-point, general and both kinds of
05129    registers.  O32 and O64 use different register kinds for standard
05130    MIPS and MIPS16 code; to make the handling of cases where we may
05131    not know what kind of code is being used (e.g. no debug information)
05132    easier we sometimes use both kinds.  */
05133 
05134 enum mips_fval_reg
05135 {
05136   mips_fval_fpr,
05137   mips_fval_gpr,
05138   mips_fval_both
05139 };
05140 
05141 /* O32 ABI stuff.  */
05142 
05143 static CORE_ADDR
05144 mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
05145                           struct regcache *regcache, CORE_ADDR bp_addr,
05146                           int nargs, struct value **args, CORE_ADDR sp,
05147                           int struct_return, CORE_ADDR struct_addr)
05148 {
05149   int argreg;
05150   int float_argreg;
05151   int argnum;
05152   int len = 0;
05153   int stack_offset = 0;
05154   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
05155   CORE_ADDR func_addr = find_function_addr (function, NULL);
05156 
05157   /* For shared libraries, "t9" needs to point at the function
05158      address.  */
05159   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
05160 
05161   /* Set the return address register to point to the entry point of
05162      the program, where a breakpoint lies in wait.  */
05163   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
05164 
05165   /* First ensure that the stack and structure return address (if any)
05166      are properly aligned.  The stack has to be at least 64-bit
05167      aligned even on 32-bit machines, because doubles must be 64-bit
05168      aligned.  For n32 and n64, stack frames need to be 128-bit
05169      aligned, so we round to this widest known alignment.  */
05170 
05171   sp = align_down (sp, 16);
05172   struct_addr = align_down (struct_addr, 16);
05173 
05174   /* Now make space on the stack for the args.  */
05175   for (argnum = 0; argnum < nargs; argnum++)
05176     {
05177       struct type *arg_type = check_typedef (value_type (args[argnum]));
05178 
05179       /* Align to double-word if necessary.  */
05180       if (mips_type_needs_double_align (arg_type))
05181         len = align_up (len, MIPS32_REGSIZE * 2);
05182       /* Allocate space on the stack.  */
05183       len += align_up (TYPE_LENGTH (arg_type), MIPS32_REGSIZE);
05184     }
05185   sp -= align_up (len, 16);
05186 
05187   if (mips_debug)
05188     fprintf_unfiltered (gdb_stdlog,
05189                         "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
05190                         paddress (gdbarch, sp), (long) align_up (len, 16));
05191 
05192   /* Initialize the integer and float register pointers.  */
05193   argreg = MIPS_A0_REGNUM;
05194   float_argreg = mips_fpa0_regnum (gdbarch);
05195 
05196   /* The struct_return pointer occupies the first parameter-passing reg.  */
05197   if (struct_return)
05198     {
05199       if (mips_debug)
05200         fprintf_unfiltered (gdb_stdlog,
05201                             "mips_o32_push_dummy_call: "
05202                             "struct_return reg=%d %s\n",
05203                             argreg, paddress (gdbarch, struct_addr));
05204       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
05205       stack_offset += MIPS32_REGSIZE;
05206     }
05207 
05208   /* Now load as many as possible of the first arguments into
05209      registers, and push the rest onto the stack.  Loop thru args
05210      from first to last.  */
05211   for (argnum = 0; argnum < nargs; argnum++)
05212     {
05213       const gdb_byte *val;
05214       struct value *arg = args[argnum];
05215       struct type *arg_type = check_typedef (value_type (arg));
05216       int len = TYPE_LENGTH (arg_type);
05217       enum type_code typecode = TYPE_CODE (arg_type);
05218 
05219       if (mips_debug)
05220         fprintf_unfiltered (gdb_stdlog,
05221                             "mips_o32_push_dummy_call: %d len=%d type=%d",
05222                             argnum + 1, len, (int) typecode);
05223 
05224       val = value_contents (arg);
05225 
05226       /* 32-bit ABIs always start floating point arguments in an
05227          even-numbered floating point register.  Round the FP register
05228          up before the check to see if there are any FP registers
05229          left.  O32 targets also pass the FP in the integer registers
05230          so also round up normal registers.  */
05231       if (fp_register_arg_p (gdbarch, typecode, arg_type))
05232         {
05233           if ((float_argreg & 1))
05234             float_argreg++;
05235         }
05236 
05237       /* Floating point arguments passed in registers have to be
05238          treated specially.  On 32-bit architectures, doubles are
05239          passed in register pairs; the even FP register gets the
05240          low word, and the odd FP register gets the high word.
05241          On O32, the first two floating point arguments are also
05242          copied to general registers, following their memory order,
05243          because MIPS16 functions don't use float registers for
05244          arguments.  This duplication of arguments in general
05245          registers can't hurt non-MIPS16 functions, because those
05246          registers are normally skipped.  */
05247 
05248       if (fp_register_arg_p (gdbarch, typecode, arg_type)
05249           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
05250         {
05251           if (register_size (gdbarch, float_argreg) < 8 && len == 8)
05252             {
05253               int freg_offset = gdbarch_byte_order (gdbarch)
05254                                 == BFD_ENDIAN_BIG ? 1 : 0;
05255               unsigned long regval;
05256 
05257               /* First word.  */
05258               regval = extract_unsigned_integer (val, 4, byte_order);
05259               if (mips_debug)
05260                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
05261                                     float_argreg + freg_offset,
05262                                     phex (regval, 4));
05263               regcache_cooked_write_unsigned (regcache,
05264                                               float_argreg++ + freg_offset,
05265                                               regval);
05266               if (mips_debug)
05267                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
05268                                     argreg, phex (regval, 4));
05269               regcache_cooked_write_unsigned (regcache, argreg++, regval);
05270 
05271               /* Second word.  */
05272               regval = extract_unsigned_integer (val + 4, 4, byte_order);
05273               if (mips_debug)
05274                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
05275                                     float_argreg - freg_offset,
05276                                     phex (regval, 4));
05277               regcache_cooked_write_unsigned (regcache,
05278                                               float_argreg++ - freg_offset,
05279                                               regval);
05280               if (mips_debug)
05281                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
05282                                     argreg, phex (regval, 4));
05283               regcache_cooked_write_unsigned (regcache, argreg++, regval);
05284             }
05285           else
05286             {
05287               /* This is a floating point value that fits entirely
05288                  in a single register.  */
05289               /* On 32 bit ABI's the float_argreg is further adjusted
05290                  above to ensure that it is even register aligned.  */
05291               LONGEST regval = extract_unsigned_integer (val, len, byte_order);
05292               if (mips_debug)
05293                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
05294                                     float_argreg, phex (regval, len));
05295               regcache_cooked_write_unsigned (regcache,
05296                                               float_argreg++, regval);
05297               /* Although two FP registers are reserved for each
05298                  argument, only one corresponding integer register is
05299                  reserved.  */
05300               if (mips_debug)
05301                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
05302                                     argreg, phex (regval, len));
05303               regcache_cooked_write_unsigned (regcache, argreg++, regval);
05304             }
05305           /* Reserve space for the FP register.  */
05306           stack_offset += align_up (len, MIPS32_REGSIZE);
05307         }
05308       else
05309         {
05310           /* Copy the argument to general registers or the stack in
05311              register-sized pieces.  Large arguments are split between
05312              registers and stack.  */
05313           /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
05314              are treated specially: Irix cc passes
05315              them in registers where gcc sometimes puts them on the
05316              stack.  For maximum compatibility, we will put them in
05317              both places.  */
05318           int odd_sized_struct = (len > MIPS32_REGSIZE
05319                                   && len % MIPS32_REGSIZE != 0);
05320           /* Structures should be aligned to eight bytes (even arg registers)
05321              on MIPS_ABI_O32, if their first member has double precision.  */
05322           if (mips_type_needs_double_align (arg_type))
05323             {
05324               if ((argreg & 1))
05325                 {
05326                   argreg++;
05327                   stack_offset += MIPS32_REGSIZE;
05328                 }
05329             }
05330           while (len > 0)
05331             {
05332               /* Remember if the argument was written to the stack.  */
05333               int stack_used_p = 0;
05334               int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);
05335 
05336               if (mips_debug)
05337                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
05338                                     partial_len);
05339 
05340               /* Write this portion of the argument to the stack.  */
05341               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
05342                   || odd_sized_struct)
05343                 {
05344                   /* Should shorter than int integer values be
05345                      promoted to int before being stored?  */
05346                   int longword_offset = 0;
05347                   CORE_ADDR addr;
05348                   stack_used_p = 1;
05349 
05350                   if (mips_debug)
05351                     {
05352                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
05353                                           paddress (gdbarch, stack_offset));
05354                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
05355                                           paddress (gdbarch, longword_offset));
05356                     }
05357 
05358                   addr = sp + stack_offset + longword_offset;
05359 
05360                   if (mips_debug)
05361                     {
05362                       int i;
05363                       fprintf_unfiltered (gdb_stdlog, " @%s ",
05364                                           paddress (gdbarch, addr));
05365                       for (i = 0; i < partial_len; i++)
05366                         {
05367                           fprintf_unfiltered (gdb_stdlog, "%02x",
05368                                               val[i] & 0xff);
05369                         }
05370                     }
05371                   write_memory (addr, val, partial_len);
05372                 }
05373 
05374               /* Note!!! This is NOT an else clause.  Odd sized
05375                  structs may go thru BOTH paths.  */
05376               /* Write this portion of the argument to a general
05377                  purpose register.  */
05378               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
05379                 {
05380                   LONGEST regval = extract_signed_integer (val, partial_len,
05381                                                            byte_order);
05382                   /* Value may need to be sign extended, because
05383                      mips_isa_regsize() != mips_abi_regsize().  */
05384 
05385                   /* A non-floating-point argument being passed in a
05386                      general register.  If a struct or union, and if
05387                      the remaining length is smaller than the register
05388                      size, we have to adjust the register value on
05389                      big endian targets.
05390 
05391                      It does not seem to be necessary to do the
05392                      same for integral types.
05393 
05394                      Also don't do this adjustment on O64 binaries.
05395 
05396                      cagney/2001-07-23: gdb/179: Also, GCC, when
05397                      outputting LE O32 with sizeof (struct) <
05398                      mips_abi_regsize(), generates a left shift
05399                      as part of storing the argument in a register
05400                      (the left shift isn't generated when
05401                      sizeof (struct) >= mips_abi_regsize()).  Since
05402                      it is quite possible that this is GCC
05403                      contradicting the LE/O32 ABI, GDB has not been
05404                      adjusted to accommodate this.  Either someone
05405                      needs to demonstrate that the LE/O32 ABI
05406                      specifies such a left shift OR this new ABI gets
05407                      identified as such and GDB gets tweaked
05408                      accordingly.  */
05409 
05410                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
05411                       && partial_len < MIPS32_REGSIZE
05412                       && (typecode == TYPE_CODE_STRUCT
05413                           || typecode == TYPE_CODE_UNION))
05414                     regval <<= ((MIPS32_REGSIZE - partial_len)
05415                                 * TARGET_CHAR_BIT);
05416 
05417                   if (mips_debug)
05418                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
05419                                       argreg,
05420                                       phex (regval, MIPS32_REGSIZE));
05421                   regcache_cooked_write_unsigned (regcache, argreg, regval);
05422                   argreg++;
05423 
05424                   /* Prevent subsequent floating point arguments from
05425                      being passed in floating point registers.  */
05426                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
05427                 }
05428 
05429               len -= partial_len;
05430               val += partial_len;
05431 
05432               /* Compute the offset into the stack at which we will
05433                  copy the next parameter.
05434 
05435                  In older ABIs, the caller reserved space for
05436                  registers that contained arguments.  This was loosely
05437                  refered to as their "home".  Consequently, space is
05438                  always allocated.  */
05439 
05440               stack_offset += align_up (partial_len, MIPS32_REGSIZE);
05441             }
05442         }
05443       if (mips_debug)
05444         fprintf_unfiltered (gdb_stdlog, "\n");
05445     }
05446 
05447   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
05448 
05449   /* Return adjusted stack pointer.  */
05450   return sp;
05451 }
05452 
05453 static enum return_value_convention
05454 mips_o32_return_value (struct gdbarch *gdbarch, struct value *function,
05455                        struct type *type, struct regcache *regcache,
05456                        gdb_byte *readbuf, const gdb_byte *writebuf)
05457 {
05458   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
05459   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
05460   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
05461   enum mips_fval_reg fval_reg;
05462 
05463   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
05464   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05465       || TYPE_CODE (type) == TYPE_CODE_UNION
05466       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
05467     return RETURN_VALUE_STRUCT_CONVENTION;
05468   else if (TYPE_CODE (type) == TYPE_CODE_FLT
05469            && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
05470     {
05471       /* A single-precision floating-point value.  If reading in or copying,
05472          then we get it from/put it to FP0 for standard MIPS code or GPR2
05473          for MIPS16 code.  If writing out only, then we put it to both FP0
05474          and GPR2.  We do not support reading in with no function known, if
05475          this safety check ever triggers, then we'll have to try harder.  */
05476       gdb_assert (function || !readbuf);
05477       if (mips_debug)
05478         switch (fval_reg)
05479           {
05480           case mips_fval_fpr:
05481             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
05482             break;
05483           case mips_fval_gpr:
05484             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
05485             break;
05486           case mips_fval_both:
05487             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
05488             break;
05489           }
05490       if (fval_reg != mips_fval_gpr)
05491         mips_xfer_register (gdbarch, regcache,
05492                             (gdbarch_num_regs (gdbarch)
05493                              + mips_regnum (gdbarch)->fp0),
05494                             TYPE_LENGTH (type),
05495                             gdbarch_byte_order (gdbarch),
05496                             readbuf, writebuf, 0);
05497       if (fval_reg != mips_fval_fpr)
05498         mips_xfer_register (gdbarch, regcache,
05499                             gdbarch_num_regs (gdbarch) + 2,
05500                             TYPE_LENGTH (type),
05501                             gdbarch_byte_order (gdbarch),
05502                             readbuf, writebuf, 0);
05503       return RETURN_VALUE_REGISTER_CONVENTION;
05504     }
05505   else if (TYPE_CODE (type) == TYPE_CODE_FLT
05506            && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
05507     {
05508       /* A double-precision floating-point value.  If reading in or copying,
05509          then we get it from/put it to FP1 and FP0 for standard MIPS code or
05510          GPR2 and GPR3 for MIPS16 code.  If writing out only, then we put it
05511          to both FP1/FP0 and GPR2/GPR3.  We do not support reading in with
05512          no function known, if this safety check ever triggers, then we'll
05513          have to try harder.  */
05514       gdb_assert (function || !readbuf);
05515       if (mips_debug)
05516         switch (fval_reg)
05517           {
05518           case mips_fval_fpr:
05519             fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
05520             break;
05521           case mips_fval_gpr:
05522             fprintf_unfiltered (gdb_stderr, "Return float in $2/$3\n");
05523             break;
05524           case mips_fval_both:
05525             fprintf_unfiltered (gdb_stderr,
05526                                 "Return float in $fp1/$fp0 and $2/$3\n");
05527             break;
05528           }
05529       if (fval_reg != mips_fval_gpr)
05530         {
05531           /* The most significant part goes in FP1, and the least significant
05532              in FP0.  */
05533           switch (gdbarch_byte_order (gdbarch))
05534             {
05535             case BFD_ENDIAN_LITTLE:
05536               mips_xfer_register (gdbarch, regcache,
05537                                   (gdbarch_num_regs (gdbarch)
05538                                    + mips_regnum (gdbarch)->fp0 + 0),
05539                                   4, gdbarch_byte_order (gdbarch),
05540                                   readbuf, writebuf, 0);
05541               mips_xfer_register (gdbarch, regcache,
05542                                   (gdbarch_num_regs (gdbarch)
05543                                    + mips_regnum (gdbarch)->fp0 + 1),
05544                                   4, gdbarch_byte_order (gdbarch),
05545                                   readbuf, writebuf, 4);
05546               break;
05547             case BFD_ENDIAN_BIG:
05548               mips_xfer_register (gdbarch, regcache,
05549                                   (gdbarch_num_regs (gdbarch)
05550                                    + mips_regnum (gdbarch)->fp0 + 1),
05551                                   4, gdbarch_byte_order (gdbarch),
05552                                   readbuf, writebuf, 0);
05553               mips_xfer_register (gdbarch, regcache,
05554                                   (gdbarch_num_regs (gdbarch)
05555                                    + mips_regnum (gdbarch)->fp0 + 0),
05556                                   4, gdbarch_byte_order (gdbarch),
05557                                   readbuf, writebuf, 4);
05558               break;
05559             default:
05560               internal_error (__FILE__, __LINE__, _("bad switch"));
05561             }
05562         }
05563       if (fval_reg != mips_fval_fpr)
05564         {
05565           /* The two 32-bit parts are always placed in GPR2 and GPR3
05566              following these registers' memory order.  */
05567           mips_xfer_register (gdbarch, regcache,
05568                               gdbarch_num_regs (gdbarch) + 2,
05569                               4, gdbarch_byte_order (gdbarch),
05570                               readbuf, writebuf, 0);
05571           mips_xfer_register (gdbarch, regcache,
05572                               gdbarch_num_regs (gdbarch) + 3,
05573                               4, gdbarch_byte_order (gdbarch),
05574                               readbuf, writebuf, 4);
05575         }
05576       return RETURN_VALUE_REGISTER_CONVENTION;
05577     }
05578 #if 0
05579   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05580            && TYPE_NFIELDS (type) <= 2
05581            && TYPE_NFIELDS (type) >= 1
05582            && ((TYPE_NFIELDS (type) == 1
05583                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
05584                     == TYPE_CODE_FLT))
05585                || (TYPE_NFIELDS (type) == 2
05586                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
05587                        == TYPE_CODE_FLT)
05588                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
05589                        == TYPE_CODE_FLT)))
05590            && tdep->mips_fpu_type != MIPS_FPU_NONE)
05591     {
05592       /* A struct that contains one or two floats.  Each value is part
05593          in the least significant part of their floating point
05594          register..  */
05595       gdb_byte reg[MAX_REGISTER_SIZE];
05596       int regnum;
05597       int field;
05598       for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
05599            field < TYPE_NFIELDS (type); field++, regnum += 2)
05600         {
05601           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
05602                         / TARGET_CHAR_BIT);
05603           if (mips_debug)
05604             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
05605                                 offset);
05606           mips_xfer_register (gdbarch, regcache,
05607                               gdbarch_num_regs (gdbarch) + regnum,
05608                               TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
05609                               gdbarch_byte_order (gdbarch),
05610                               readbuf, writebuf, offset);
05611         }
05612       return RETURN_VALUE_REGISTER_CONVENTION;
05613     }
05614 #endif
05615 #if 0
05616   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05617            || TYPE_CODE (type) == TYPE_CODE_UNION)
05618     {
05619       /* A structure or union.  Extract the left justified value,
05620          regardless of the byte order.  I.e. DO NOT USE
05621          mips_xfer_lower.  */
05622       int offset;
05623       int regnum;
05624       for (offset = 0, regnum = MIPS_V0_REGNUM;
05625            offset < TYPE_LENGTH (type);
05626            offset += register_size (gdbarch, regnum), regnum++)
05627         {
05628           int xfer = register_size (gdbarch, regnum);
05629           if (offset + xfer > TYPE_LENGTH (type))
05630             xfer = TYPE_LENGTH (type) - offset;
05631           if (mips_debug)
05632             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
05633                                 offset, xfer, regnum);
05634           mips_xfer_register (gdbarch, regcache,
05635                               gdbarch_num_regs (gdbarch) + regnum, xfer,
05636                               BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
05637         }
05638       return RETURN_VALUE_REGISTER_CONVENTION;
05639     }
05640 #endif
05641   else
05642     {
05643       /* A scalar extract each part but least-significant-byte
05644          justified.  o32 thinks registers are 4 byte, regardless of
05645          the ISA.  */
05646       int offset;
05647       int regnum;
05648       for (offset = 0, regnum = MIPS_V0_REGNUM;
05649            offset < TYPE_LENGTH (type);
05650            offset += MIPS32_REGSIZE, regnum++)
05651         {
05652           int xfer = MIPS32_REGSIZE;
05653           if (offset + xfer > TYPE_LENGTH (type))
05654             xfer = TYPE_LENGTH (type) - offset;
05655           if (mips_debug)
05656             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
05657                                 offset, xfer, regnum);
05658           mips_xfer_register (gdbarch, regcache,
05659                               gdbarch_num_regs (gdbarch) + regnum, xfer,
05660                               gdbarch_byte_order (gdbarch),
05661                               readbuf, writebuf, offset);
05662         }
05663       return RETURN_VALUE_REGISTER_CONVENTION;
05664     }
05665 }
05666 
05667 /* O64 ABI.  This is a hacked up kind of 64-bit version of the o32
05668    ABI.  */
05669 
05670 static CORE_ADDR
05671 mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
05672                           struct regcache *regcache, CORE_ADDR bp_addr,
05673                           int nargs,
05674                           struct value **args, CORE_ADDR sp,
05675                           int struct_return, CORE_ADDR struct_addr)
05676 {
05677   int argreg;
05678   int float_argreg;
05679   int argnum;
05680   int len = 0;
05681   int stack_offset = 0;
05682   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
05683   CORE_ADDR func_addr = find_function_addr (function, NULL);
05684 
05685   /* For shared libraries, "t9" needs to point at the function
05686      address.  */
05687   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
05688 
05689   /* Set the return address register to point to the entry point of
05690      the program, where a breakpoint lies in wait.  */
05691   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
05692 
05693   /* First ensure that the stack and structure return address (if any)
05694      are properly aligned.  The stack has to be at least 64-bit
05695      aligned even on 32-bit machines, because doubles must be 64-bit
05696      aligned.  For n32 and n64, stack frames need to be 128-bit
05697      aligned, so we round to this widest known alignment.  */
05698 
05699   sp = align_down (sp, 16);
05700   struct_addr = align_down (struct_addr, 16);
05701 
05702   /* Now make space on the stack for the args.  */
05703   for (argnum = 0; argnum < nargs; argnum++)
05704     {
05705       struct type *arg_type = check_typedef (value_type (args[argnum]));
05706 
05707       /* Allocate space on the stack.  */
05708       len += align_up (TYPE_LENGTH (arg_type), MIPS64_REGSIZE);
05709     }
05710   sp -= align_up (len, 16);
05711 
05712   if (mips_debug)
05713     fprintf_unfiltered (gdb_stdlog,
05714                         "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
05715                         paddress (gdbarch, sp), (long) align_up (len, 16));
05716 
05717   /* Initialize the integer and float register pointers.  */
05718   argreg = MIPS_A0_REGNUM;
05719   float_argreg = mips_fpa0_regnum (gdbarch);
05720 
05721   /* The struct_return pointer occupies the first parameter-passing reg.  */
05722   if (struct_return)
05723     {
05724       if (mips_debug)
05725         fprintf_unfiltered (gdb_stdlog,
05726                             "mips_o64_push_dummy_call: "
05727                             "struct_return reg=%d %s\n",
05728                             argreg, paddress (gdbarch, struct_addr));
05729       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
05730       stack_offset += MIPS64_REGSIZE;
05731     }
05732 
05733   /* Now load as many as possible of the first arguments into
05734      registers, and push the rest onto the stack.  Loop thru args
05735      from first to last.  */
05736   for (argnum = 0; argnum < nargs; argnum++)
05737     {
05738       const gdb_byte *val;
05739       gdb_byte valbuf[MAX_REGISTER_SIZE];
05740       struct value *arg = args[argnum];
05741       struct type *arg_type = check_typedef (value_type (arg));
05742       int len = TYPE_LENGTH (arg_type);
05743       enum type_code typecode = TYPE_CODE (arg_type);
05744 
05745       if (mips_debug)
05746         fprintf_unfiltered (gdb_stdlog,
05747                             "mips_o64_push_dummy_call: %d len=%d type=%d",
05748                             argnum + 1, len, (int) typecode);
05749 
05750       val = value_contents (arg);
05751 
05752       /* Function pointer arguments to mips16 code need to be made into
05753          mips16 pointers.  */
05754       if (typecode == TYPE_CODE_PTR
05755           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
05756         {
05757           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
05758                                                    len, byte_order);
05759           if (!mips_pc_is_mips (addr))
05760             {
05761               store_signed_integer (valbuf, len, byte_order, 
05762                                     make_compact_addr (addr));
05763               val = valbuf;
05764             }
05765         }
05766 
05767       /* Floating point arguments passed in registers have to be
05768          treated specially.  On 32-bit architectures, doubles are
05769          passed in register pairs; the even FP register gets the
05770          low word, and the odd FP register gets the high word.
05771          On O64, the first two floating point arguments are also
05772          copied to general registers, because MIPS16 functions
05773          don't use float registers for arguments.  This duplication
05774          of arguments in general registers can't hurt non-MIPS16
05775          functions because those registers are normally skipped.  */
05776 
05777       if (fp_register_arg_p (gdbarch, typecode, arg_type)
05778           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
05779         {
05780           LONGEST regval = extract_unsigned_integer (val, len, byte_order);
05781           if (mips_debug)
05782             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
05783                                 float_argreg, phex (regval, len));
05784           regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
05785           if (mips_debug)
05786             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
05787                                 argreg, phex (regval, len));
05788           regcache_cooked_write_unsigned (regcache, argreg, regval);
05789           argreg++;
05790           /* Reserve space for the FP register.  */
05791           stack_offset += align_up (len, MIPS64_REGSIZE);
05792         }
05793       else
05794         {
05795           /* Copy the argument to general registers or the stack in
05796              register-sized pieces.  Large arguments are split between
05797              registers and stack.  */
05798           /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
05799              are treated specially: Irix cc passes them in registers
05800              where gcc sometimes puts them on the stack.  For maximum
05801              compatibility, we will put them in both places.  */
05802           int odd_sized_struct = (len > MIPS64_REGSIZE
05803                                   && len % MIPS64_REGSIZE != 0);
05804           while (len > 0)
05805             {
05806               /* Remember if the argument was written to the stack.  */
05807               int stack_used_p = 0;
05808               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
05809 
05810               if (mips_debug)
05811                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
05812                                     partial_len);
05813 
05814               /* Write this portion of the argument to the stack.  */
05815               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
05816                   || odd_sized_struct)
05817                 {
05818                   /* Should shorter than int integer values be
05819                      promoted to int before being stored?  */
05820                   int longword_offset = 0;
05821                   CORE_ADDR addr;
05822                   stack_used_p = 1;
05823                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
05824                     {
05825                       if ((typecode == TYPE_CODE_INT
05826                            || typecode == TYPE_CODE_PTR
05827                            || typecode == TYPE_CODE_FLT)
05828                           && len <= 4)
05829                         longword_offset = MIPS64_REGSIZE - len;
05830                     }
05831 
05832                   if (mips_debug)
05833                     {
05834                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
05835                                           paddress (gdbarch, stack_offset));
05836                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
05837                                           paddress (gdbarch, longword_offset));
05838                     }
05839 
05840                   addr = sp + stack_offset + longword_offset;
05841 
05842                   if (mips_debug)
05843                     {
05844                       int i;
05845                       fprintf_unfiltered (gdb_stdlog, " @%s ",
05846                                           paddress (gdbarch, addr));
05847                       for (i = 0; i < partial_len; i++)
05848                         {
05849                           fprintf_unfiltered (gdb_stdlog, "%02x",
05850                                               val[i] & 0xff);
05851                         }
05852                     }
05853                   write_memory (addr, val, partial_len);
05854                 }
05855 
05856               /* Note!!! This is NOT an else clause.  Odd sized
05857                  structs may go thru BOTH paths.  */
05858               /* Write this portion of the argument to a general
05859                  purpose register.  */
05860               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
05861                 {
05862                   LONGEST regval = extract_signed_integer (val, partial_len,
05863                                                            byte_order);
05864                   /* Value may need to be sign extended, because
05865                      mips_isa_regsize() != mips_abi_regsize().  */
05866 
05867                   /* A non-floating-point argument being passed in a
05868                      general register.  If a struct or union, and if
05869                      the remaining length is smaller than the register
05870                      size, we have to adjust the register value on
05871                      big endian targets.
05872 
05873                      It does not seem to be necessary to do the
05874                      same for integral types.  */
05875 
05876                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
05877                       && partial_len < MIPS64_REGSIZE
05878                       && (typecode == TYPE_CODE_STRUCT
05879                           || typecode == TYPE_CODE_UNION))
05880                     regval <<= ((MIPS64_REGSIZE - partial_len)
05881                                 * TARGET_CHAR_BIT);
05882 
05883                   if (mips_debug)
05884                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
05885                                       argreg,
05886                                       phex (regval, MIPS64_REGSIZE));
05887                   regcache_cooked_write_unsigned (regcache, argreg, regval);
05888                   argreg++;
05889 
05890                   /* Prevent subsequent floating point arguments from
05891                      being passed in floating point registers.  */
05892                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
05893                 }
05894 
05895               len -= partial_len;
05896               val += partial_len;
05897 
05898               /* Compute the offset into the stack at which we will
05899                  copy the next parameter.
05900 
05901                  In older ABIs, the caller reserved space for
05902                  registers that contained arguments.  This was loosely
05903                  refered to as their "home".  Consequently, space is
05904                  always allocated.  */
05905 
05906               stack_offset += align_up (partial_len, MIPS64_REGSIZE);
05907             }
05908         }
05909       if (mips_debug)
05910         fprintf_unfiltered (gdb_stdlog, "\n");
05911     }
05912 
05913   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
05914 
05915   /* Return adjusted stack pointer.  */
05916   return sp;
05917 }
05918 
05919 static enum return_value_convention
05920 mips_o64_return_value (struct gdbarch *gdbarch, struct value *function,
05921                        struct type *type, struct regcache *regcache,
05922                        gdb_byte *readbuf, const gdb_byte *writebuf)
05923 {
05924   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
05925   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
05926   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
05927   enum mips_fval_reg fval_reg;
05928 
05929   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
05930   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
05931       || TYPE_CODE (type) == TYPE_CODE_UNION
05932       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
05933     return RETURN_VALUE_STRUCT_CONVENTION;
05934   else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
05935     {
05936       /* A floating-point value.  If reading in or copying, then we get it
05937          from/put it to FP0 for standard MIPS code or GPR2 for MIPS16 code.
05938          If writing out only, then we put it to both FP0 and GPR2.  We do
05939          not support reading in with no function known, if this safety
05940          check ever triggers, then we'll have to try harder.  */
05941       gdb_assert (function || !readbuf);
05942       if (mips_debug)
05943         switch (fval_reg)
05944           {
05945           case mips_fval_fpr:
05946             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
05947             break;
05948           case mips_fval_gpr:
05949             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
05950             break;
05951           case mips_fval_both:
05952             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
05953             break;
05954           }
05955       if (fval_reg != mips_fval_gpr)
05956         mips_xfer_register (gdbarch, regcache,
05957                             (gdbarch_num_regs (gdbarch)
05958                              + mips_regnum (gdbarch)->fp0),
05959                             TYPE_LENGTH (type),
05960                             gdbarch_byte_order (gdbarch),
05961                             readbuf, writebuf, 0);
05962       if (fval_reg != mips_fval_fpr)
05963         mips_xfer_register (gdbarch, regcache,
05964                             gdbarch_num_regs (gdbarch) + 2,
05965                             TYPE_LENGTH (type),
05966                             gdbarch_byte_order (gdbarch),
05967                             readbuf, writebuf, 0);
05968       return RETURN_VALUE_REGISTER_CONVENTION;
05969     }
05970   else
05971     {
05972       /* A scalar extract each part but least-significant-byte
05973          justified.  */
05974       int offset;
05975       int regnum;
05976       for (offset = 0, regnum = MIPS_V0_REGNUM;
05977            offset < TYPE_LENGTH (type);
05978            offset += MIPS64_REGSIZE, regnum++)
05979         {
05980           int xfer = MIPS64_REGSIZE;
05981           if (offset + xfer > TYPE_LENGTH (type))
05982             xfer = TYPE_LENGTH (type) - offset;
05983           if (mips_debug)
05984             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
05985                                 offset, xfer, regnum);
05986           mips_xfer_register (gdbarch, regcache,
05987                               gdbarch_num_regs (gdbarch) + regnum,
05988                               xfer, gdbarch_byte_order (gdbarch),
05989                               readbuf, writebuf, offset);
05990         }
05991       return RETURN_VALUE_REGISTER_CONVENTION;
05992     }
05993 }
05994 
05995 /* Floating point register management.
05996 
05997    Background: MIPS1 & 2 fp registers are 32 bits wide.  To support
05998    64bit operations, these early MIPS cpus treat fp register pairs
05999    (f0,f1) as a single register (d0).  Later MIPS cpu's have 64 bit fp
06000    registers and offer a compatibility mode that emulates the MIPS2 fp
06001    model.  When operating in MIPS2 fp compat mode, later cpu's split
06002    double precision floats into two 32-bit chunks and store them in
06003    consecutive fp regs.  To display 64-bit floats stored in this
06004    fashion, we have to combine 32 bits from f0 and 32 bits from f1.
06005    Throw in user-configurable endianness and you have a real mess.
06006 
06007    The way this works is:
06008      - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
06009        double-precision value will be split across two logical registers.
06010        The lower-numbered logical register will hold the low-order bits,
06011        regardless of the processor's endianness.
06012      - If we are on a 64-bit processor, and we are looking for a
06013        single-precision value, it will be in the low ordered bits
06014        of a 64-bit GPR (after mfc1, for example) or a 64-bit register
06015        save slot in memory.
06016      - If we are in 64-bit mode, everything is straightforward.
06017 
06018    Note that this code only deals with "live" registers at the top of the
06019    stack.  We will attempt to deal with saved registers later, when
06020    the raw/cooked register interface is in place.  (We need a general
06021    interface that can deal with dynamic saved register sizes -- fp
06022    regs could be 32 bits wide in one frame and 64 on the frame above
06023    and below).  */
06024 
06025 /* Copy a 32-bit single-precision value from the current frame
06026    into rare_buffer.  */
06027 
06028 static void
06029 mips_read_fp_register_single (struct frame_info *frame, int regno,
06030                               gdb_byte *rare_buffer)
06031 {
06032   struct gdbarch *gdbarch = get_frame_arch (frame);
06033   int raw_size = register_size (gdbarch, regno);
06034   gdb_byte *raw_buffer = alloca (raw_size);
06035 
06036   if (!deprecated_frame_register_read (frame, regno, raw_buffer))
06037     error (_("can't read register %d (%s)"),
06038            regno, gdbarch_register_name (gdbarch, regno));
06039   if (raw_size == 8)
06040     {
06041       /* We have a 64-bit value for this register.  Find the low-order
06042          32 bits.  */
06043       int offset;
06044 
06045       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
06046         offset = 4;
06047       else
06048         offset = 0;
06049 
06050       memcpy (rare_buffer, raw_buffer + offset, 4);
06051     }
06052   else
06053     {
06054       memcpy (rare_buffer, raw_buffer, 4);
06055     }
06056 }
06057 
06058 /* Copy a 64-bit double-precision value from the current frame into
06059    rare_buffer.  This may include getting half of it from the next
06060    register.  */
06061 
06062 static void
06063 mips_read_fp_register_double (struct frame_info *frame, int regno,
06064                               gdb_byte *rare_buffer)
06065 {
06066   struct gdbarch *gdbarch = get_frame_arch (frame);
06067   int raw_size = register_size (gdbarch, regno);
06068 
06069   if (raw_size == 8 && !mips2_fp_compat (frame))
06070     {
06071       /* We have a 64-bit value for this register, and we should use
06072          all 64 bits.  */
06073       if (!deprecated_frame_register_read (frame, regno, rare_buffer))
06074         error (_("can't read register %d (%s)"),
06075                regno, gdbarch_register_name (gdbarch, regno));
06076     }
06077   else
06078     {
06079       int rawnum = regno % gdbarch_num_regs (gdbarch);
06080 
06081       if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
06082         internal_error (__FILE__, __LINE__,
06083                         _("mips_read_fp_register_double: bad access to "
06084                         "odd-numbered FP register"));
06085 
06086       /* mips_read_fp_register_single will find the correct 32 bits from
06087          each register.  */
06088       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
06089         {
06090           mips_read_fp_register_single (frame, regno, rare_buffer + 4);
06091           mips_read_fp_register_single (frame, regno + 1, rare_buffer);
06092         }
06093       else
06094         {
06095           mips_read_fp_register_single (frame, regno, rare_buffer);
06096           mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
06097         }
06098     }
06099 }
06100 
06101 static void
06102 mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
06103                         int regnum)
06104 {                               /* Do values for FP (float) regs.  */
06105   struct gdbarch *gdbarch = get_frame_arch (frame);
06106   gdb_byte *raw_buffer;
06107   double doub, flt1;    /* Doubles extracted from raw hex data.  */
06108   int inv1, inv2;
06109 
06110   raw_buffer = alloca (2 * register_size (gdbarch,
06111                                           mips_regnum (gdbarch)->fp0));
06112 
06113   fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
06114   fprintf_filtered (file, "%*s",
06115                     4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
06116                     "");
06117 
06118   if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
06119     {
06120       struct value_print_options opts;
06121 
06122       /* 4-byte registers: Print hex and floating.  Also print even
06123          numbered registers as doubles.  */
06124       mips_read_fp_register_single (frame, regnum, raw_buffer);
06125       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
06126                             raw_buffer, &inv1);
06127 
06128       get_formatted_print_options (&opts, 'x');
06129       print_scalar_formatted (raw_buffer,
06130                               builtin_type (gdbarch)->builtin_uint32,
06131                               &opts, 'w', file);
06132 
06133       fprintf_filtered (file, " flt: ");
06134       if (inv1)
06135         fprintf_filtered (file, " <invalid float> ");
06136       else
06137         fprintf_filtered (file, "%-17.9g", flt1);
06138 
06139       if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
06140         {
06141           mips_read_fp_register_double (frame, regnum, raw_buffer);
06142           doub = unpack_double (builtin_type (gdbarch)->builtin_double,
06143                                 raw_buffer, &inv2);
06144 
06145           fprintf_filtered (file, " dbl: ");
06146           if (inv2)
06147             fprintf_filtered (file, "<invalid double>");
06148           else
06149             fprintf_filtered (file, "%-24.17g", doub);
06150         }
06151     }
06152   else
06153     {
06154       struct value_print_options opts;
06155 
06156       /* Eight byte registers: print each one as hex, float and double.  */
06157       mips_read_fp_register_single (frame, regnum, raw_buffer);
06158       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
06159                             raw_buffer, &inv1);
06160 
06161       mips_read_fp_register_double (frame, regnum, raw_buffer);
06162       doub = unpack_double (builtin_type (gdbarch)->builtin_double,
06163                             raw_buffer, &inv2);
06164 
06165       get_formatted_print_options (&opts, 'x');
06166       print_scalar_formatted (raw_buffer,
06167                               builtin_type (gdbarch)->builtin_uint64,
06168                               &opts, 'g', file);
06169 
06170       fprintf_filtered (file, " flt: ");
06171       if (inv1)
06172         fprintf_filtered (file, "<invalid float>");
06173       else
06174         fprintf_filtered (file, "%-17.9g", flt1);
06175 
06176       fprintf_filtered (file, " dbl: ");
06177       if (inv2)
06178         fprintf_filtered (file, "<invalid double>");
06179       else
06180         fprintf_filtered (file, "%-24.17g", doub);
06181     }
06182 }
06183 
06184 static void
06185 mips_print_register (struct ui_file *file, struct frame_info *frame,
06186                      int regnum)
06187 {
06188   struct gdbarch *gdbarch = get_frame_arch (frame);
06189   struct value_print_options opts;
06190   struct value *val;
06191 
06192   if (mips_float_register_p (gdbarch, regnum))
06193     {
06194       mips_print_fp_register (file, frame, regnum);
06195       return;
06196     }
06197 
06198   val = get_frame_register_value (frame, regnum);
06199   if (value_optimized_out (val))
06200     {
06201       fprintf_filtered (file, "%s: [Invalid]",
06202                         gdbarch_register_name (gdbarch, regnum));
06203       return;
06204     }
06205 
06206   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
06207 
06208   /* The problem with printing numeric register names (r26, etc.) is that
06209      the user can't use them on input.  Probably the best solution is to
06210      fix it so that either the numeric or the funky (a2, etc.) names
06211      are accepted on input.  */
06212   if (regnum < MIPS_NUMREGS)
06213     fprintf_filtered (file, "(r%d): ", regnum);
06214   else
06215     fprintf_filtered (file, ": ");
06216 
06217   get_formatted_print_options (&opts, 'x');
06218   val_print_scalar_formatted (value_type (val),
06219                               value_contents_for_printing (val),
06220                               value_embedded_offset (val),
06221                               val,
06222                               &opts, 0, file);
06223 }
06224 
06225 /* Replacement for generic do_registers_info.
06226    Print regs in pretty columns.  */
06227 
06228 static int
06229 print_fp_register_row (struct ui_file *file, struct frame_info *frame,
06230                        int regnum)
06231 {
06232   fprintf_filtered (file, " ");
06233   mips_print_fp_register (file, frame, regnum);
06234   fprintf_filtered (file, "\n");
06235   return regnum + 1;
06236 }
06237 
06238 
06239 /* Print a row's worth of GP (int) registers, with name labels above.  */
06240 
06241 static int
06242 print_gp_register_row (struct ui_file *file, struct frame_info *frame,
06243                        int start_regnum)
06244 {
06245   struct gdbarch *gdbarch = get_frame_arch (frame);
06246   /* Do values for GP (int) regs.  */
06247   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
06248   int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8);    /* display cols
06249                                                                per row.  */
06250   int col, byte;
06251   int regnum;
06252 
06253   /* For GP registers, we print a separate row of names above the vals.  */
06254   for (col = 0, regnum = start_regnum;
06255        col < ncols && regnum < gdbarch_num_regs (gdbarch)
06256                                + gdbarch_num_pseudo_regs (gdbarch);
06257        regnum++)
06258     {
06259       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
06260         continue;               /* unused register */
06261       if (mips_float_register_p (gdbarch, regnum))
06262         break;                  /* End the row: reached FP register.  */
06263       /* Large registers are handled separately.  */
06264       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
06265         {
06266           if (col > 0)
06267             break;              /* End the row before this register.  */
06268 
06269           /* Print this register on a row by itself.  */
06270           mips_print_register (file, frame, regnum);
06271           fprintf_filtered (file, "\n");
06272           return regnum + 1;
06273         }
06274       if (col == 0)
06275         fprintf_filtered (file, "     ");
06276       fprintf_filtered (file,
06277                         mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
06278                         gdbarch_register_name (gdbarch, regnum));
06279       col++;
06280     }
06281 
06282   if (col == 0)
06283     return regnum;
06284 
06285   /* Print the R0 to R31 names.  */
06286   if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
06287     fprintf_filtered (file, "\n R%-4d",
06288                       start_regnum % gdbarch_num_regs (gdbarch));
06289   else
06290     fprintf_filtered (file, "\n      ");
06291 
06292   /* Now print the values in hex, 4 or 8 to the row.  */
06293   for (col = 0, regnum = start_regnum;
06294        col < ncols && regnum < gdbarch_num_regs (gdbarch)
06295                                + gdbarch_num_pseudo_regs (gdbarch);
06296        regnum++)
06297     {
06298       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
06299         continue;               /* unused register */
06300       if (mips_float_register_p (gdbarch, regnum))
06301         break;                  /* End row: reached FP register.  */
06302       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
06303         break;                  /* End row: large register.  */
06304 
06305       /* OK: get the data in raw format.  */
06306       if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
06307         error (_("can't read register %d (%s)"),
06308                regnum, gdbarch_register_name (gdbarch, regnum));
06309       /* pad small registers */
06310       for (byte = 0;
06311            byte < (mips_abi_regsize (gdbarch)
06312                    - register_size (gdbarch, regnum)); byte++)
06313         printf_filtered ("  ");
06314       /* Now print the register value in hex, endian order.  */
06315       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
06316         for (byte =
06317              register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
06318              byte < register_size (gdbarch, regnum); byte++)
06319           fprintf_filtered (file, "%02x", raw_buffer[byte]);
06320       else
06321         for (byte = register_size (gdbarch, regnum) - 1;
06322              byte >= 0; byte--)
06323           fprintf_filtered (file, "%02x", raw_buffer[byte]);
06324       fprintf_filtered (file, " ");
06325       col++;
06326     }
06327   if (col > 0)                  /* ie. if we actually printed anything...  */
06328     fprintf_filtered (file, "\n");
06329 
06330   return regnum;
06331 }
06332 
06333 /* MIPS_DO_REGISTERS_INFO(): called by "info register" command.  */
06334 
06335 static void
06336 mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
06337                            struct frame_info *frame, int regnum, int all)
06338 {
06339   if (regnum != -1)             /* Do one specified register.  */
06340     {
06341       gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
06342       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
06343         error (_("Not a valid register for the current processor type"));
06344 
06345       mips_print_register (file, frame, regnum);
06346       fprintf_filtered (file, "\n");
06347     }
06348   else
06349     /* Do all (or most) registers.  */
06350     {
06351       regnum = gdbarch_num_regs (gdbarch);
06352       while (regnum < gdbarch_num_regs (gdbarch)
06353                       + gdbarch_num_pseudo_regs (gdbarch))
06354         {
06355           if (mips_float_register_p (gdbarch, regnum))
06356             {
06357               if (all)          /* True for "INFO ALL-REGISTERS" command.  */
06358                 regnum = print_fp_register_row (file, frame, regnum);
06359               else
06360                 regnum += MIPS_NUMREGS; /* Skip floating point regs.  */
06361             }
06362           else
06363             regnum = print_gp_register_row (file, frame, regnum);
06364         }
06365     }
06366 }
06367 
06368 static int
06369 mips_single_step_through_delay (struct gdbarch *gdbarch,
06370                                 struct frame_info *frame)
06371 {
06372   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
06373   CORE_ADDR pc = get_frame_pc (frame);
06374   struct address_space *aspace;
06375   enum mips_isa isa;
06376   ULONGEST insn;
06377   int status;
06378   int size;
06379 
06380   if ((mips_pc_is_mips (pc)
06381        && !mips32_instruction_has_delay_slot (gdbarch, pc))
06382       || (mips_pc_is_micromips (gdbarch, pc)
06383           && !micromips_instruction_has_delay_slot (gdbarch, pc, 0))
06384       || (mips_pc_is_mips16 (gdbarch, pc)
06385           && !mips16_instruction_has_delay_slot (gdbarch, pc, 0)))
06386     return 0;
06387 
06388   isa = mips_pc_isa (gdbarch, pc);
06389   /* _has_delay_slot above will have validated the read.  */
06390   insn = mips_fetch_instruction (gdbarch, isa, pc, NULL);
06391   size = mips_insn_size (isa, insn);
06392   aspace = get_frame_address_space (frame);
06393   return breakpoint_here_p (aspace, pc + size) != no_breakpoint_here;
06394 }
06395 
06396 /* To skip prologues, I use this predicate.  Returns either PC itself
06397    if the code at PC does not look like a function prologue; otherwise
06398    returns an address that (if we're lucky) follows the prologue.  If
06399    LENIENT, then we must skip everything which is involved in setting
06400    up the frame (it's OK to skip more, just so long as we don't skip
06401    anything which might clobber the registers which are being saved.
06402    We must skip more in the case where part of the prologue is in the
06403    delay slot of a non-prologue instruction).  */
06404 
06405 static CORE_ADDR
06406 mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
06407 {
06408   CORE_ADDR limit_pc;
06409   CORE_ADDR func_addr;
06410 
06411   /* See if we can determine the end of the prologue via the symbol table.
06412      If so, then return either PC, or the PC after the prologue, whichever
06413      is greater.  */
06414   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
06415     {
06416       CORE_ADDR post_prologue_pc
06417         = skip_prologue_using_sal (gdbarch, func_addr);
06418       if (post_prologue_pc != 0)
06419         return max (pc, post_prologue_pc);
06420     }
06421 
06422   /* Can't determine prologue from the symbol table, need to examine
06423      instructions.  */
06424 
06425   /* Find an upper limit on the function prologue using the debug
06426      information.  If the debug information could not be used to provide
06427      that bound, then use an arbitrary large number as the upper bound.  */
06428   limit_pc = skip_prologue_using_sal (gdbarch, pc);
06429   if (limit_pc == 0)
06430     limit_pc = pc + 100;          /* Magic.  */
06431 
06432   if (mips_pc_is_mips16 (gdbarch, pc))
06433     return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
06434   else if (mips_pc_is_micromips (gdbarch, pc))
06435     return micromips_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
06436   else
06437     return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
06438 }
06439 
06440 /* Check whether the PC is in a function epilogue (32-bit version).
06441    This is a helper function for mips_in_function_epilogue_p.  */
06442 static int
06443 mips32_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
06444 {
06445   CORE_ADDR func_addr = 0, func_end = 0;
06446 
06447   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
06448     {
06449       /* The MIPS epilogue is max. 12 bytes long.  */
06450       CORE_ADDR addr = func_end - 12;
06451 
06452       if (addr < func_addr + 4)
06453         addr = func_addr + 4;
06454       if (pc < addr)
06455         return 0;
06456 
06457       for (; pc < func_end; pc += MIPS_INSN32_SIZE)
06458         {
06459           unsigned long high_word;
06460           unsigned long inst;
06461 
06462           inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
06463           high_word = (inst >> 16) & 0xffff;
06464 
06465           if (high_word != 0x27bd       /* addiu $sp,$sp,offset */
06466               && high_word != 0x67bd    /* daddiu $sp,$sp,offset */
06467               && inst != 0x03e00008     /* jr $ra */
06468               && inst != 0x00000000)    /* nop */
06469             return 0;
06470         }
06471 
06472       return 1;
06473     }
06474 
06475   return 0;
06476 }
06477 
06478 /* Check whether the PC is in a function epilogue (microMIPS version).
06479    This is a helper function for mips_in_function_epilogue_p.  */
06480 
06481 static int
06482 micromips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
06483 {
06484   CORE_ADDR func_addr = 0;
06485   CORE_ADDR func_end = 0;
06486   CORE_ADDR addr;
06487   ULONGEST insn;
06488   long offset;
06489   int dreg;
06490   int sreg;
06491   int loc;
06492 
06493   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
06494     return 0;
06495 
06496   /* The microMIPS epilogue is max. 12 bytes long.  */
06497   addr = func_end - 12;
06498 
06499   if (addr < func_addr + 2)
06500     addr = func_addr + 2;
06501   if (pc < addr)
06502     return 0;
06503 
06504   for (; pc < func_end; pc += loc)
06505     {
06506       loc = 0;
06507       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
06508       loc += MIPS_INSN16_SIZE;
06509       switch (mips_insn_size (ISA_MICROMIPS, insn))
06510         {
06511         /* 48-bit instructions.  */
06512         case 3 * MIPS_INSN16_SIZE:
06513           /* No epilogue instructions in this category.  */
06514           return 0;
06515 
06516         /* 32-bit instructions.  */
06517         case 2 * MIPS_INSN16_SIZE:
06518           insn <<= 16;
06519           insn |= mips_fetch_instruction (gdbarch,
06520                                           ISA_MICROMIPS, pc + loc, NULL);
06521           loc += MIPS_INSN16_SIZE;
06522           switch (micromips_op (insn >> 16))
06523             {
06524             case 0xc: /* ADDIU: bits 001100 */
06525             case 0x17: /* DADDIU: bits 010111 */
06526               sreg = b0s5_reg (insn >> 16);
06527               dreg = b5s5_reg (insn >> 16);
06528               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
06529               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
06530                             /* (D)ADDIU $sp, imm */
06531                   && offset >= 0)
06532                 break;
06533               return 0;
06534 
06535             default:
06536               return 0;
06537             }
06538           break;
06539 
06540         /* 16-bit instructions.  */
06541         case MIPS_INSN16_SIZE:
06542           switch (micromips_op (insn))
06543             {
06544             case 0x3: /* MOVE: bits 000011 */
06545               sreg = b0s5_reg (insn);
06546               dreg = b5s5_reg (insn);
06547               if (sreg == 0 && dreg == 0)
06548                                 /* MOVE $zero, $zero aka NOP */
06549                 break;
06550               return 0;
06551 
06552             case 0x11: /* POOL16C: bits 010001 */
06553               if (b5s5_op (insn) == 0x18
06554                                 /* JRADDIUSP: bits 010011 11000 */
06555                   || (b5s5_op (insn) == 0xd
06556                                 /* JRC: bits 010011 01101 */
06557                       && b0s5_reg (insn) == MIPS_RA_REGNUM))
06558                                 /* JRC $ra */
06559                 break;
06560               return 0;
06561 
06562             case 0x13: /* POOL16D: bits 010011 */
06563               offset = micromips_decode_imm9 (b1s9_imm (insn));
06564               if ((insn & 0x1) == 0x1
06565                                 /* ADDIUSP: bits 010011 1 */
06566                   && offset > 0)
06567                 break;
06568               return 0;
06569 
06570             default:
06571               return 0;
06572             }
06573         }
06574     }
06575 
06576   return 1;
06577 }
06578 
06579 /* Check whether the PC is in a function epilogue (16-bit version).
06580    This is a helper function for mips_in_function_epilogue_p.  */
06581 static int
06582 mips16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
06583 {
06584   CORE_ADDR func_addr = 0, func_end = 0;
06585 
06586   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
06587     {
06588       /* The MIPS epilogue is max. 12 bytes long.  */
06589       CORE_ADDR addr = func_end - 12;
06590 
06591       if (addr < func_addr + 4)
06592         addr = func_addr + 4;
06593       if (pc < addr)
06594         return 0;
06595 
06596       for (; pc < func_end; pc += MIPS_INSN16_SIZE)
06597         {
06598           unsigned short inst;
06599 
06600           inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc, NULL);
06601 
06602           if ((inst & 0xf800) == 0xf000)        /* extend */
06603             continue;
06604 
06605           if (inst != 0x6300            /* addiu $sp,offset */
06606               && inst != 0xfb00         /* daddiu $sp,$sp,offset */
06607               && inst != 0xe820         /* jr $ra */
06608               && inst != 0xe8a0         /* jrc $ra */
06609               && inst != 0x6500)        /* nop */
06610             return 0;
06611         }
06612 
06613       return 1;
06614     }
06615 
06616   return 0;
06617 }
06618 
06619 /* The epilogue is defined here as the area at the end of a function,
06620    after an instruction which destroys the function's stack frame.  */
06621 static int
06622 mips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
06623 {
06624   if (mips_pc_is_mips16 (gdbarch, pc))
06625     return mips16_in_function_epilogue_p (gdbarch, pc);
06626   else if (mips_pc_is_micromips (gdbarch, pc))
06627     return micromips_in_function_epilogue_p (gdbarch, pc);
06628   else
06629     return mips32_in_function_epilogue_p (gdbarch, pc);
06630 }
06631 
06632 /* Root of all "set mips "/"show mips " commands.  This will eventually be
06633    used for all MIPS-specific commands.  */
06634 
06635 static void
06636 show_mips_command (char *args, int from_tty)
06637 {
06638   help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
06639 }
06640 
06641 static void
06642 set_mips_command (char *args, int from_tty)
06643 {
06644   printf_unfiltered
06645     ("\"set mips\" must be followed by an appropriate subcommand.\n");
06646   help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
06647 }
06648 
06649 /* Commands to show/set the MIPS FPU type.  */
06650 
06651 static void
06652 show_mipsfpu_command (char *args, int from_tty)
06653 {
06654   char *fpu;
06655 
06656   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
06657     {
06658       printf_unfiltered
06659         ("The MIPS floating-point coprocessor is unknown "
06660          "because the current architecture is not MIPS.\n");
06661       return;
06662     }
06663 
06664   switch (MIPS_FPU_TYPE (target_gdbarch ()))
06665     {
06666     case MIPS_FPU_SINGLE:
06667       fpu = "single-precision";
06668       break;
06669     case MIPS_FPU_DOUBLE:
06670       fpu = "double-precision";
06671       break;
06672     case MIPS_FPU_NONE:
06673       fpu = "absent (none)";
06674       break;
06675     default:
06676       internal_error (__FILE__, __LINE__, _("bad switch"));
06677     }
06678   if (mips_fpu_type_auto)
06679     printf_unfiltered ("The MIPS floating-point coprocessor "
06680                        "is set automatically (currently %s)\n",
06681                        fpu);
06682   else
06683     printf_unfiltered
06684       ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
06685 }
06686 
06687 
06688 static void
06689 set_mipsfpu_command (char *args, int from_tty)
06690 {
06691   printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
06692                      "\"single\",\"none\" or \"auto\".\n");
06693   show_mipsfpu_command (args, from_tty);
06694 }
06695 
06696 static void
06697 set_mipsfpu_single_command (char *args, int from_tty)
06698 {
06699   struct gdbarch_info info;
06700   gdbarch_info_init (&info);
06701   mips_fpu_type = MIPS_FPU_SINGLE;
06702   mips_fpu_type_auto = 0;
06703   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
06704      instead of relying on globals.  Doing that would let generic code
06705      handle the search for this specific architecture.  */
06706   if (!gdbarch_update_p (info))
06707     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
06708 }
06709 
06710 static void
06711 set_mipsfpu_double_command (char *args, int from_tty)
06712 {
06713   struct gdbarch_info info;
06714   gdbarch_info_init (&info);
06715   mips_fpu_type = MIPS_FPU_DOUBLE;
06716   mips_fpu_type_auto = 0;
06717   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
06718      instead of relying on globals.  Doing that would let generic code
06719      handle the search for this specific architecture.  */
06720   if (!gdbarch_update_p (info))
06721     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
06722 }
06723 
06724 static void
06725 set_mipsfpu_none_command (char *args, int from_tty)
06726 {
06727   struct gdbarch_info info;
06728   gdbarch_info_init (&info);
06729   mips_fpu_type = MIPS_FPU_NONE;
06730   mips_fpu_type_auto = 0;
06731   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
06732      instead of relying on globals.  Doing that would let generic code
06733      handle the search for this specific architecture.  */
06734   if (!gdbarch_update_p (info))
06735     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
06736 }
06737 
06738 static void
06739 set_mipsfpu_auto_command (char *args, int from_tty)
06740 {
06741   mips_fpu_type_auto = 1;
06742 }
06743 
06744 /* Attempt to identify the particular processor model by reading the
06745    processor id.  NOTE: cagney/2003-11-15: Firstly it isn't clear that
06746    the relevant processor still exists (it dates back to '94) and
06747    secondly this is not the way to do this.  The processor type should
06748    be set by forcing an architecture change.  */
06749 
06750 void
06751 deprecated_mips_set_processor_regs_hack (void)
06752 {
06753   struct regcache *regcache = get_current_regcache ();
06754   struct gdbarch *gdbarch = get_regcache_arch (regcache);
06755   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
06756   ULONGEST prid;
06757 
06758   regcache_cooked_read_unsigned (regcache, MIPS_PRID_REGNUM, &prid);
06759   if ((prid & ~0xf) == 0x700)
06760     tdep->mips_processor_reg_names = mips_r3041_reg_names;
06761 }
06762 
06763 /* Just like reinit_frame_cache, but with the right arguments to be
06764    callable as an sfunc.  */
06765 
06766 static void
06767 reinit_frame_cache_sfunc (char *args, int from_tty,
06768                           struct cmd_list_element *c)
06769 {
06770   reinit_frame_cache ();
06771 }
06772 
06773 static int
06774 gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
06775 {
06776   struct gdbarch *gdbarch = info->application_data;
06777 
06778   /* FIXME: cagney/2003-06-26: Is this even necessary?  The
06779      disassembler needs to be able to locally determine the ISA, and
06780      not rely on GDB.  Otherwize the stand-alone 'objdump -d' will not
06781      work.  */
06782   if (mips_pc_is_mips16 (gdbarch, memaddr))
06783     info->mach = bfd_mach_mips16;
06784   else if (mips_pc_is_micromips (gdbarch, memaddr))
06785     info->mach = bfd_mach_mips_micromips;
06786 
06787   /* Round down the instruction address to the appropriate boundary.  */
06788   memaddr &= (info->mach == bfd_mach_mips16
06789               || info->mach == bfd_mach_mips_micromips) ? ~1 : ~3;
06790 
06791   /* Set the disassembler options.  */
06792   if (!info->disassembler_options)
06793     /* This string is not recognized explicitly by the disassembler,
06794        but it tells the disassembler to not try to guess the ABI from
06795        the bfd elf headers, such that, if the user overrides the ABI
06796        of a program linked as NewABI, the disassembly will follow the
06797        register naming conventions specified by the user.  */
06798     info->disassembler_options = "gpr-names=32";
06799 
06800   /* Call the appropriate disassembler based on the target endian-ness.  */
06801   if (info->endian == BFD_ENDIAN_BIG)
06802     return print_insn_big_mips (memaddr, info);
06803   else
06804     return print_insn_little_mips (memaddr, info);
06805 }
06806 
06807 static int
06808 gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
06809 {
06810   /* Set up the disassembler info, so that we get the right
06811      register names from libopcodes.  */
06812   info->disassembler_options = "gpr-names=n32";
06813   info->flavour = bfd_target_elf_flavour;
06814 
06815   return gdb_print_insn_mips (memaddr, info);
06816 }
06817 
06818 static int
06819 gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
06820 {
06821   /* Set up the disassembler info, so that we get the right
06822      register names from libopcodes.  */
06823   info->disassembler_options = "gpr-names=64";
06824   info->flavour = bfd_target_elf_flavour;
06825 
06826   return gdb_print_insn_mips (memaddr, info);
06827 }
06828 
06829 /* This function implements gdbarch_breakpoint_from_pc.  It uses the
06830    program counter value to determine whether a 16- or 32-bit breakpoint
06831    should be used.  It returns a pointer to a string of bytes that encode a
06832    breakpoint instruction, stores the length of the string to *lenptr, and
06833    adjusts pc (if necessary) to point to the actual memory location where
06834    the breakpoint should be inserted.  */
06835 
06836 static const gdb_byte *
06837 mips_breakpoint_from_pc (struct gdbarch *gdbarch,
06838                          CORE_ADDR *pcptr, int *lenptr)
06839 {
06840   CORE_ADDR pc = *pcptr;
06841 
06842   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
06843     {
06844       if (mips_pc_is_mips16 (gdbarch, pc))
06845         {
06846           static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
06847           *pcptr = unmake_compact_addr (pc);
06848           *lenptr = sizeof (mips16_big_breakpoint);
06849           return mips16_big_breakpoint;
06850         }
06851       else if (mips_pc_is_micromips (gdbarch, pc))
06852         {
06853           static gdb_byte micromips16_big_breakpoint[] = { 0x46, 0x85 };
06854           static gdb_byte micromips32_big_breakpoint[] = { 0, 0x5, 0, 0x7 };
06855           ULONGEST insn;
06856           int status;
06857           int size;
06858 
06859           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
06860           size = status ? 2
06861                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
06862           *pcptr = unmake_compact_addr (pc);
06863           *lenptr = size;
06864           return (size == 2) ? micromips16_big_breakpoint
06865                              : micromips32_big_breakpoint;
06866         }
06867       else
06868         {
06869           /* The IDT board uses an unusual breakpoint value, and
06870              sometimes gets confused when it sees the usual MIPS
06871              breakpoint instruction.  */
06872           static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
06873           static gdb_byte pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
06874           static gdb_byte idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
06875           /* Likewise, IRIX appears to expect a different breakpoint,
06876              although this is not apparent until you try to use pthreads.  */
06877           static gdb_byte irix_big_breakpoint[] = { 0, 0, 0, 0xd };
06878 
06879           *lenptr = sizeof (big_breakpoint);
06880 
06881           if (strcmp (target_shortname, "mips") == 0)
06882             return idt_big_breakpoint;
06883           else if (strcmp (target_shortname, "ddb") == 0
06884                    || strcmp (target_shortname, "pmon") == 0
06885                    || strcmp (target_shortname, "lsi") == 0)
06886             return pmon_big_breakpoint;
06887           else if (gdbarch_osabi (gdbarch) == GDB_OSABI_IRIX)
06888             return irix_big_breakpoint;
06889           else
06890             return big_breakpoint;
06891         }
06892     }
06893   else
06894     {
06895       if (mips_pc_is_mips16 (gdbarch, pc))
06896         {
06897           static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
06898           *pcptr = unmake_compact_addr (pc);
06899           *lenptr = sizeof (mips16_little_breakpoint);
06900           return mips16_little_breakpoint;
06901         }
06902       else if (mips_pc_is_micromips (gdbarch, pc))
06903         {
06904           static gdb_byte micromips16_little_breakpoint[] = { 0x85, 0x46 };
06905           static gdb_byte micromips32_little_breakpoint[] = { 0x5, 0, 0x7, 0 };
06906           ULONGEST insn;
06907           int status;
06908           int size;
06909 
06910           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
06911           size = status ? 2
06912                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
06913           *pcptr = unmake_compact_addr (pc);
06914           *lenptr = size;
06915           return (size == 2) ? micromips16_little_breakpoint
06916                              : micromips32_little_breakpoint;
06917         }
06918       else
06919         {
06920           static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
06921           static gdb_byte pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
06922           static gdb_byte idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };
06923 
06924           *lenptr = sizeof (little_breakpoint);
06925 
06926           if (strcmp (target_shortname, "mips") == 0)
06927             return idt_little_breakpoint;
06928           else if (strcmp (target_shortname, "ddb") == 0
06929                    || strcmp (target_shortname, "pmon") == 0
06930                    || strcmp (target_shortname, "lsi") == 0)
06931             return pmon_little_breakpoint;
06932           else
06933             return little_breakpoint;
06934         }
06935     }
06936 }
06937 
06938 /* Determine the remote breakpoint kind suitable for the PC.  The following
06939    kinds are used:
06940 
06941    * 2 -- 16-bit MIPS16 mode breakpoint,
06942 
06943    * 3 -- 16-bit microMIPS mode breakpoint,
06944 
06945    * 4 -- 32-bit standard MIPS mode breakpoint,
06946 
06947    * 5 -- 32-bit microMIPS mode breakpoint.  */
06948 
06949 static void
06950 mips_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
06951                                 int *kindptr)
06952 {
06953   CORE_ADDR pc = *pcptr;
06954 
06955   if (mips_pc_is_mips16 (gdbarch, pc))
06956     {
06957       *pcptr = unmake_compact_addr (pc);
06958       *kindptr = 2;
06959     }
06960   else if (mips_pc_is_micromips (gdbarch, pc))
06961     {
06962       ULONGEST insn;
06963       int status;
06964       int size;
06965 
06966       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
06967       size = status ? 2 : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
06968       *pcptr = unmake_compact_addr (pc);
06969       *kindptr = size | 1;
06970     }
06971   else
06972     *kindptr = 4;
06973 }
06974 
06975 /* Return non-zero if the ADDR instruction has a branch delay slot
06976    (i.e. it is a jump or branch instruction).  This function is based
06977    on mips32_next_pc.  */
06978 
06979 static int
06980 mips32_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr)
06981 {
06982   unsigned long inst;
06983   int status;
06984   int op;
06985   int rs;
06986   int rt;
06987 
06988   inst = mips_fetch_instruction (gdbarch, ISA_MIPS, addr, &status);
06989   if (status)
06990     return 0;
06991 
06992   op = itype_op (inst);
06993   if ((inst & 0xe0000000) != 0)
06994     {
06995       rs = itype_rs (inst);
06996       rt = itype_rt (inst);
06997       return (is_octeon_bbit_op (op, gdbarch) 
06998               || op >> 2 == 5   /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx  */
06999               || op == 29       /* JALX: bits 011101  */
07000               || (op == 17
07001                   && (rs == 8
07002                                 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000  */
07003                       || (rs == 9 && (rt & 0x2) == 0)
07004                                 /* BC1ANY2F, BC1ANY2T: bits 010001 01001  */
07005                       || (rs == 10 && (rt & 0x2) == 0))));
07006                                 /* BC1ANY4F, BC1ANY4T: bits 010001 01010  */
07007     }
07008   else
07009     switch (op & 0x07)          /* extract bits 28,27,26  */
07010       {
07011       case 0:                   /* SPECIAL  */
07012         op = rtype_funct (inst);
07013         return (op == 8         /* JR  */
07014                 || op == 9);    /* JALR  */
07015         break;                  /* end SPECIAL  */
07016       case 1:                   /* REGIMM  */
07017         rs = itype_rs (inst);
07018         rt = itype_rt (inst);   /* branch condition  */
07019         return ((rt & 0xc) == 0
07020                                 /* BLTZ, BLTZL, BGEZ, BGEZL: bits 000xx  */
07021                                 /* BLTZAL, BLTZALL, BGEZAL, BGEZALL: 100xx  */
07022                 || ((rt & 0x1e) == 0x1c && rs == 0));
07023                                 /* BPOSGE32, BPOSGE64: bits 1110x  */
07024         break;                  /* end REGIMM  */
07025       default:                  /* J, JAL, BEQ, BNE, BLEZ, BGTZ  */
07026         return 1;
07027         break;
07028       }
07029 }
07030 
07031 /* Return non-zero if the ADDR instruction, which must be a 32-bit
07032    instruction if MUSTBE32 is set or can be any instruction otherwise,
07033    has a branch delay slot (i.e. it is a non-compact jump instruction).  */
07034 
07035 static int
07036 micromips_instruction_has_delay_slot (struct gdbarch *gdbarch,
07037                                       CORE_ADDR addr, int mustbe32)
07038 {
07039   ULONGEST insn;
07040   int status;
07041 
07042   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
07043   if (status)
07044     return 0;
07045 
07046   if (!mustbe32)                /* 16-bit instructions.  */
07047     return (micromips_op (insn) == 0x11
07048                                 /* POOL16C: bits 010001 */
07049             && (b5s5_op (insn) == 0xc
07050                                 /* JR16: bits 010001 01100 */
07051                 || (b5s5_op (insn) & 0x1e) == 0xe))
07052                                 /* JALR16, JALRS16: bits 010001 0111x */
07053            || (micromips_op (insn) & 0x37) == 0x23
07054                                 /* BEQZ16, BNEZ16: bits 10x011 */
07055            || micromips_op (insn) == 0x33;
07056                                 /* B16: bits 110011 */
07057 
07058                                 /* 32-bit instructions.  */
07059   if (micromips_op (insn) == 0x0)
07060                                 /* POOL32A: bits 000000 */
07061     {
07062       insn <<= 16;
07063       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
07064       if (status)
07065         return 0;
07066       return b0s6_op (insn) == 0x3c
07067                                 /* POOL32Axf: bits 000000 ... 111100 */
07068              && (b6s10_ext (insn) & 0x2bf) == 0x3c;
07069                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
07070                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
07071     }
07072 
07073   return (micromips_op (insn) == 0x10
07074                                 /* POOL32I: bits 010000 */
07075           && ((b5s5_op (insn) & 0x1c) == 0x0
07076                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
07077               || (b5s5_op (insn) & 0x1d) == 0x4
07078                                 /* BLEZ, BGTZ: bits 010000 001x0 */
07079               || (b5s5_op (insn) & 0x1d) == 0x11
07080                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
07081               || ((b5s5_op (insn) & 0x1e) == 0x14
07082                   && (insn & 0x3) == 0x0)
07083                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
07084               || (b5s5_op (insn) & 0x1e) == 0x1a
07085                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
07086               || ((b5s5_op (insn) & 0x1e) == 0x1c
07087                   && (insn & 0x3) == 0x0)
07088                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
07089               || ((b5s5_op (insn) & 0x1c) == 0x1c
07090                   && (insn & 0x3) == 0x1)))
07091                                 /* BC1ANY*: bits 010000 111xx xxx01 */
07092          || (micromips_op (insn) & 0x1f) == 0x1d
07093                                 /* JALS, JAL: bits x11101 */
07094          || (micromips_op (insn) & 0x37) == 0x25
07095                                 /* BEQ, BNE: bits 10x101 */
07096          || micromips_op (insn) == 0x35
07097                                 /* J: bits 110101 */
07098          || micromips_op (insn) == 0x3c;
07099                                 /* JALX: bits 111100 */
07100 }
07101 
07102 static int
07103 mips16_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr,
07104                                    int mustbe32)
07105 {
07106   unsigned short inst;
07107   int status;
07108 
07109   inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, addr, &status);
07110   if (status)
07111     return 0;
07112 
07113   if (!mustbe32)
07114     return (inst & 0xf89f) == 0xe800;   /* JR/JALR (16-bit instruction)  */
07115   return (inst & 0xf800) == 0x1800;     /* JAL/JALX (32-bit instruction)  */
07116 }
07117 
07118 /* Calculate the starting address of the MIPS memory segment BPADDR is in.
07119    This assumes KSSEG exists.  */
07120 
07121 static CORE_ADDR
07122 mips_segment_boundary (CORE_ADDR bpaddr)
07123 {
07124   CORE_ADDR mask = CORE_ADDR_MAX;
07125   int segsize;
07126 
07127   if (sizeof (CORE_ADDR) == 8)
07128     /* Get the topmost two bits of bpaddr in a 32-bit safe manner (avoid
07129        a compiler warning produced where CORE_ADDR is a 32-bit type even
07130        though in that case this is dead code).  */
07131     switch (bpaddr >> ((sizeof (CORE_ADDR) << 3) - 2) & 3)
07132       {
07133       case 3:
07134         if (bpaddr == (bfd_signed_vma) (int32_t) bpaddr)
07135           segsize = 29;                 /* 32-bit compatibility segment  */
07136         else
07137           segsize = 62;                 /* xkseg  */
07138         break;
07139       case 2:                           /* xkphys  */
07140         segsize = 59;
07141         break;
07142       default:                          /* xksseg (1), xkuseg/kuseg (0)  */
07143         segsize = 62;
07144         break;
07145       }
07146   else if (bpaddr & 0x80000000)         /* kernel segment  */
07147     segsize = 29;
07148   else
07149     segsize = 31;                       /* user segment  */
07150   mask <<= segsize;
07151   return bpaddr & mask;
07152 }
07153 
07154 /* Move the breakpoint at BPADDR out of any branch delay slot by shifting
07155    it backwards if necessary.  Return the address of the new location.  */
07156 
07157 static CORE_ADDR
07158 mips_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
07159 {
07160   CORE_ADDR prev_addr;
07161   CORE_ADDR boundary;
07162   CORE_ADDR func_addr;
07163 
07164   /* If a breakpoint is set on the instruction in a branch delay slot,
07165      GDB gets confused.  When the breakpoint is hit, the PC isn't on
07166      the instruction in the branch delay slot, the PC will point to
07167      the branch instruction.  Since the PC doesn't match any known
07168      breakpoints, GDB reports a trap exception.
07169 
07170      There are two possible fixes for this problem.
07171 
07172      1) When the breakpoint gets hit, see if the BD bit is set in the
07173      Cause register (which indicates the last exception occurred in a
07174      branch delay slot).  If the BD bit is set, fix the PC to point to
07175      the instruction in the branch delay slot.
07176 
07177      2) When the user sets the breakpoint, don't allow him to set the
07178      breakpoint on the instruction in the branch delay slot.  Instead
07179      move the breakpoint to the branch instruction (which will have
07180      the same result).
07181 
07182      The problem with the first solution is that if the user then
07183      single-steps the processor, the branch instruction will get
07184      skipped (since GDB thinks the PC is on the instruction in the
07185      branch delay slot).
07186 
07187      So, we'll use the second solution.  To do this we need to know if
07188      the instruction we're trying to set the breakpoint on is in the
07189      branch delay slot.  */
07190 
07191   boundary = mips_segment_boundary (bpaddr);
07192 
07193   /* Make sure we don't scan back before the beginning of the current
07194      function, since we may fetch constant data or insns that look like
07195      a jump.  Of course we might do that anyway if the compiler has
07196      moved constants inline. :-(  */
07197   if (find_pc_partial_function (bpaddr, NULL, &func_addr, NULL)
07198       && func_addr > boundary && func_addr <= bpaddr)
07199     boundary = func_addr;
07200 
07201   if (mips_pc_is_mips (bpaddr))
07202     {
07203       if (bpaddr == boundary)
07204         return bpaddr;
07205 
07206       /* If the previous instruction has a branch delay slot, we have
07207          to move the breakpoint to the branch instruction. */
07208       prev_addr = bpaddr - 4;
07209       if (mips32_instruction_has_delay_slot (gdbarch, prev_addr))
07210         bpaddr = prev_addr;
07211     }
07212   else
07213     {
07214       int (*instruction_has_delay_slot) (struct gdbarch *, CORE_ADDR, int);
07215       CORE_ADDR addr, jmpaddr;
07216       int i;
07217 
07218       boundary = unmake_compact_addr (boundary);
07219 
07220       /* The only MIPS16 instructions with delay slots are JAL, JALX,
07221          JALR and JR.  An absolute JAL/JALX is always 4 bytes long,
07222          so try for that first, then try the 2 byte JALR/JR.
07223          The microMIPS ASE has a whole range of jumps and branches
07224          with delay slots, some of which take 4 bytes and some take
07225          2 bytes, so the idea is the same.
07226          FIXME: We have to assume that bpaddr is not the second half
07227          of an extended instruction.  */
07228       instruction_has_delay_slot = (mips_pc_is_micromips (gdbarch, bpaddr)
07229                                      ? micromips_instruction_has_delay_slot
07230                                      : mips16_instruction_has_delay_slot);
07231 
07232       jmpaddr = 0;
07233       addr = bpaddr;
07234       for (i = 1; i < 4; i++)
07235         {
07236           if (unmake_compact_addr (addr) == boundary)
07237             break;
07238           addr -= MIPS_INSN16_SIZE;
07239           if (i == 1 && instruction_has_delay_slot (gdbarch, addr, 0))
07240             /* Looks like a JR/JALR at [target-1], but it could be
07241                the second word of a previous JAL/JALX, so record it
07242                and check back one more.  */
07243             jmpaddr = addr;
07244           else if (i > 1 && instruction_has_delay_slot (gdbarch, addr, 1))
07245             {
07246               if (i == 2)
07247                 /* Looks like a JAL/JALX at [target-2], but it could also
07248                    be the second word of a previous JAL/JALX, record it,
07249                    and check back one more.  */
07250                 jmpaddr = addr;
07251               else
07252                 /* Looks like a JAL/JALX at [target-3], so any previously
07253                    recorded JAL/JALX or JR/JALR must be wrong, because:
07254 
07255                    >-3: JAL
07256                     -2: JAL-ext (can't be JAL/JALX)
07257                     -1: bdslot (can't be JR/JALR)
07258                      0: target insn
07259 
07260                    Of course it could be another JAL-ext which looks
07261                    like a JAL, but in that case we'd have broken out
07262                    of this loop at [target-2]:
07263 
07264                     -4: JAL
07265                    >-3: JAL-ext
07266                     -2: bdslot (can't be jmp)
07267                     -1: JR/JALR
07268                      0: target insn  */
07269                 jmpaddr = 0;
07270             }
07271           else
07272             {
07273               /* Not a jump instruction: if we're at [target-1] this
07274                  could be the second word of a JAL/JALX, so continue;
07275                  otherwise we're done.  */
07276               if (i > 1)
07277                 break;
07278             }
07279         }
07280 
07281       if (jmpaddr)
07282         bpaddr = jmpaddr;
07283     }
07284 
07285   return bpaddr;
07286 }
07287 
07288 /* Return non-zero if SUFFIX is one of the numeric suffixes used for MIPS16
07289    call stubs, one of 1, 2, 5, 6, 9, 10, or, if ZERO is non-zero, also 0.  */
07290 
07291 static int
07292 mips_is_stub_suffix (const char *suffix, int zero)
07293 {
07294   switch (suffix[0])
07295    {
07296    case '0':
07297      return zero && suffix[1] == '\0';
07298    case '1':
07299      return suffix[1] == '\0' || (suffix[1] == '0' && suffix[2] == '\0');
07300    case '2':
07301    case '5':
07302    case '6':
07303    case '9':
07304      return suffix[1] == '\0';
07305    default:
07306      return 0;
07307    }
07308 }
07309 
07310 /* Return non-zero if MODE is one of the mode infixes used for MIPS16
07311    call stubs, one of sf, df, sc, or dc.  */
07312 
07313 static int
07314 mips_is_stub_mode (const char *mode)
07315 {
07316   return ((mode[0] == 's' || mode[0] == 'd')
07317           && (mode[1] == 'f' || mode[1] == 'c'));
07318 }
07319 
07320 /* Code at PC is a compiler-generated stub.  Such a stub for a function
07321    bar might have a name like __fn_stub_bar, and might look like this:
07322 
07323       mfc1    $4, $f13
07324       mfc1    $5, $f12
07325       mfc1    $6, $f15
07326       mfc1    $7, $f14
07327 
07328    followed by (or interspersed with):
07329 
07330       j       bar
07331 
07332    or:
07333 
07334       lui     $25, %hi(bar)
07335       addiu   $25, $25, %lo(bar)
07336       jr      $25
07337 
07338    ($1 may be used in old code; for robustness we accept any register)
07339    or, in PIC code:
07340 
07341       lui     $28, %hi(_gp_disp)
07342       addiu   $28, $28, %lo(_gp_disp)
07343       addu    $28, $28, $25
07344       lw      $25, %got(bar)
07345       addiu   $25, $25, %lo(bar)
07346       jr      $25
07347 
07348    In the case of a __call_stub_bar stub, the sequence to set up
07349    arguments might look like this:
07350 
07351       mtc1    $4, $f13
07352       mtc1    $5, $f12
07353       mtc1    $6, $f15
07354       mtc1    $7, $f14
07355 
07356    followed by (or interspersed with) one of the jump sequences above.
07357 
07358    In the case of a __call_stub_fp_bar stub, JAL or JALR is used instead
07359    of J or JR, respectively, followed by:
07360 
07361       mfc1    $2, $f0
07362       mfc1    $3, $f1
07363       jr      $18
07364 
07365    We are at the beginning of the stub here, and scan down and extract
07366    the target address from the jump immediate instruction or, if a jump
07367    register instruction is used, from the register referred.  Return
07368    the value of PC calculated or 0 if inconclusive.
07369 
07370    The limit on the search is arbitrarily set to 20 instructions.  FIXME.  */
07371 
07372 static CORE_ADDR
07373 mips_get_mips16_fn_stub_pc (struct frame_info *frame, CORE_ADDR pc)
07374 {
07375   struct gdbarch *gdbarch = get_frame_arch (frame);
07376   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
07377   int addrreg = MIPS_ZERO_REGNUM;
07378   CORE_ADDR start_pc = pc;
07379   CORE_ADDR target_pc = 0;
07380   CORE_ADDR addr = 0;
07381   CORE_ADDR gp = 0;
07382   int status = 0;
07383   int i;
07384 
07385   for (i = 0;
07386        status == 0 && target_pc == 0 && i < 20;
07387        i++, pc += MIPS_INSN32_SIZE)
07388     {
07389       ULONGEST inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
07390       CORE_ADDR imm;
07391       int rt;
07392       int rs;
07393       int rd;
07394 
07395       switch (itype_op (inst))
07396         {
07397         case 0:         /* SPECIAL */
07398           switch (rtype_funct (inst))
07399             {
07400             case 8:             /* JR */
07401             case 9:             /* JALR */
07402               rs = rtype_rs (inst);
07403               if (rs == MIPS_GP_REGNUM)
07404                 target_pc = gp;                         /* Hmm...  */
07405               else if (rs == addrreg)
07406                 target_pc = addr;
07407               break;
07408 
07409             case 0x21:          /* ADDU */
07410               rt = rtype_rt (inst);
07411               rs = rtype_rs (inst);
07412               rd = rtype_rd (inst);
07413               if (rd == MIPS_GP_REGNUM
07414                   && ((rs == MIPS_GP_REGNUM && rt == MIPS_T9_REGNUM)
07415                       || (rs == MIPS_T9_REGNUM && rt == MIPS_GP_REGNUM)))
07416                 gp += start_pc;
07417               break;
07418             }
07419           break;
07420 
07421         case 2:         /* J */
07422         case 3:         /* JAL */
07423           target_pc = jtype_target (inst) << 2;
07424           target_pc += ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
07425           break;
07426 
07427         case 9:         /* ADDIU */
07428           rt = itype_rt (inst);
07429           rs = itype_rs (inst);
07430           if (rt == rs)
07431             {
07432               imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
07433               if (rt == MIPS_GP_REGNUM)
07434                 gp += imm;
07435               else if (rt == addrreg)
07436                 addr += imm;
07437             }
07438           break;
07439 
07440         case 0xf:       /* LUI */
07441           rt = itype_rt (inst);
07442           imm = ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 16;
07443           if (rt == MIPS_GP_REGNUM)
07444             gp = imm;
07445           else if (rt != MIPS_ZERO_REGNUM)
07446             {
07447               addrreg = rt;
07448               addr = imm;
07449             }
07450           break;
07451 
07452         case 0x23:      /* LW */
07453           rt = itype_rt (inst);
07454           rs = itype_rs (inst);
07455           imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
07456           if (gp != 0 && rs == MIPS_GP_REGNUM)
07457             {
07458               gdb_byte buf[4];
07459 
07460               memset (buf, 0, sizeof (buf));
07461               status = target_read_memory (gp + imm, buf, sizeof (buf));
07462               addrreg = rt;
07463               addr = extract_signed_integer (buf, sizeof (buf), byte_order);
07464             }
07465           break;
07466         }
07467     }
07468 
07469   return target_pc;
07470 }
07471 
07472 /* If PC is in a MIPS16 call or return stub, return the address of the
07473    target PC, which is either the callee or the caller.  There are several
07474    cases which must be handled:
07475 
07476    * If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
07477      and the target PC is in $31 ($ra).
07478    * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
07479      and the target PC is in $2.
07480    * If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
07481      i.e. before the JALR instruction, this is effectively a call stub
07482      and the target PC is in $2.  Otherwise this is effectively
07483      a return stub and the target PC is in $18.
07484    * If the PC is at the start of __call_stub_fp_*, i.e. before the
07485      JAL or JALR instruction, this is effectively a call stub and the
07486      target PC is buried in the instruction stream.  Otherwise this
07487      is effectively a return stub and the target PC is in $18.
07488    * If the PC is in __call_stub_* or in __fn_stub_*, this is a call
07489      stub and the target PC is buried in the instruction stream.
07490 
07491    See the source code for the stubs in gcc/config/mips/mips16.S, or the
07492    stub builder in gcc/config/mips/mips.c (mips16_build_call_stub) for the
07493    gory details.  */
07494 
07495 static CORE_ADDR
07496 mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
07497 {
07498   struct gdbarch *gdbarch = get_frame_arch (frame);
07499   CORE_ADDR start_addr;
07500   const char *name;
07501   size_t prefixlen;
07502 
07503   /* Find the starting address and name of the function containing the PC.  */
07504   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
07505     return 0;
07506 
07507   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
07508      and the target PC is in $31 ($ra).  */
07509   prefixlen = strlen (mips_str_mips16_ret_stub);
07510   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
07511       && mips_is_stub_mode (name + prefixlen)
07512       && name[prefixlen + 2] == '\0')
07513     return get_frame_register_signed
07514              (frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
07515 
07516   /* If the PC is in __mips16_call_stub_*, this is one of the call
07517      call/return stubs.  */
07518   prefixlen = strlen (mips_str_mips16_call_stub);
07519   if (strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0)
07520     {
07521       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
07522          and the target PC is in $2.  */
07523       if (mips_is_stub_suffix (name + prefixlen, 0))
07524         return get_frame_register_signed
07525                  (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
07526 
07527       /* If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
07528          i.e. before the JALR instruction, this is effectively a call stub
07529          and the target PC is in $2.  Otherwise this is effectively
07530          a return stub and the target PC is in $18.  */
07531       else if (mips_is_stub_mode (name + prefixlen)
07532                && name[prefixlen + 2] == '_'
07533                && mips_is_stub_suffix (name + prefixlen + 3, 0))
07534         {
07535           if (pc == start_addr)
07536             /* This is the 'call' part of a call stub.  The return
07537                address is in $2.  */
07538             return get_frame_register_signed
07539                      (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
07540           else
07541             /* This is the 'return' part of a call stub.  The return
07542                address is in $18.  */
07543             return get_frame_register_signed
07544                      (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
07545         }
07546       else
07547         return 0;               /* Not a stub.  */
07548     }
07549 
07550   /* If the PC is in __call_stub_* or __fn_stub*, this is one of the
07551      compiler-generated call or call/return stubs.  */
07552   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0
07553       || strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
07554     {
07555       if (pc == start_addr)
07556         /* This is the 'call' part of a call stub.  Call this helper
07557            to scan through this code for interesting instructions
07558            and determine the final PC.  */
07559         return mips_get_mips16_fn_stub_pc (frame, pc);
07560       else
07561         /* This is the 'return' part of a call stub.  The return address
07562            is in $18.  */
07563         return get_frame_register_signed
07564                  (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
07565     }
07566 
07567   return 0;                     /* Not a stub.  */
07568 }
07569 
07570 /* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
07571    This implements the IN_SOLIB_RETURN_TRAMPOLINE macro.  */
07572 
07573 static int
07574 mips_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc, const char *name)
07575 {
07576   CORE_ADDR start_addr;
07577   size_t prefixlen;
07578 
07579   /* Find the starting address of the function containing the PC.  */
07580   if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
07581     return 0;
07582 
07583   /* If the PC is in __mips16_call_stub_{s,d}{f,c}_{0..10} but not at
07584      the start, i.e. after the JALR instruction, this is effectively
07585      a return stub.  */
07586   prefixlen = strlen (mips_str_mips16_call_stub);
07587   if (pc != start_addr
07588       && strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0
07589       && mips_is_stub_mode (name + prefixlen)
07590       && name[prefixlen + 2] == '_'
07591       && mips_is_stub_suffix (name + prefixlen + 3, 1))
07592     return 1;
07593 
07594   /* If the PC is in __call_stub_fp_* but not at the start, i.e. after
07595      the JAL or JALR instruction, this is effectively a return stub.  */
07596   prefixlen = strlen (mips_str_call_fp_stub);
07597   if (pc != start_addr
07598       && strncmp (name, mips_str_call_fp_stub, prefixlen) == 0)
07599     return 1;
07600 
07601   /* Consume the .pic. prefix of any PIC stub, this function must return
07602      true when the PC is in a PIC stub of a __mips16_ret_{d,s}{f,c} stub
07603      or the call stub path will trigger in handle_inferior_event causing
07604      it to go astray.  */
07605   prefixlen = strlen (mips_str_pic);
07606   if (strncmp (name, mips_str_pic, prefixlen) == 0)
07607     name += prefixlen;
07608 
07609   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub.  */
07610   prefixlen = strlen (mips_str_mips16_ret_stub);
07611   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
07612       && mips_is_stub_mode (name + prefixlen)
07613       && name[prefixlen + 2] == '\0')
07614     return 1;
07615 
07616   return 0;                     /* Not a stub.  */
07617 }
07618 
07619 /* If the current PC is the start of a non-PIC-to-PIC stub, return the
07620    PC of the stub target.  The stub just loads $t9 and jumps to it,
07621    so that $t9 has the correct value at function entry.  */
07622 
07623 static CORE_ADDR
07624 mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
07625 {
07626   struct gdbarch *gdbarch = get_frame_arch (frame);
07627   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
07628   struct bound_minimal_symbol msym;
07629   int i;
07630   gdb_byte stub_code[16];
07631   int32_t stub_words[4];
07632 
07633   /* The stub for foo is named ".pic.foo", and is either two
07634      instructions inserted before foo or a three instruction sequence
07635      which jumps to foo.  */
07636   msym = lookup_minimal_symbol_by_pc (pc);
07637   if (msym.minsym == NULL
07638       || SYMBOL_VALUE_ADDRESS (msym.minsym) != pc
07639       || SYMBOL_LINKAGE_NAME (msym.minsym) == NULL
07640       || strncmp (SYMBOL_LINKAGE_NAME (msym.minsym), ".pic.", 5) != 0)
07641     return 0;
07642 
07643   /* A two-instruction header.  */
07644   if (MSYMBOL_SIZE (msym.minsym) == 8)
07645     return pc + 8;
07646 
07647   /* A three-instruction (plus delay slot) trampoline.  */
07648   if (MSYMBOL_SIZE (msym.minsym) == 16)
07649     {
07650       if (target_read_memory (pc, stub_code, 16) != 0)
07651         return 0;
07652       for (i = 0; i < 4; i++)
07653         stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
07654                                                   4, byte_order);
07655 
07656       /* A stub contains these instructions:
07657          lui    t9, %hi(target)
07658          j      target
07659           addiu t9, t9, %lo(target)
07660          nop
07661 
07662          This works even for N64, since stubs are only generated with
07663          -msym32.  */
07664       if ((stub_words[0] & 0xffff0000U) == 0x3c190000
07665           && (stub_words[1] & 0xfc000000U) == 0x08000000
07666           && (stub_words[2] & 0xffff0000U) == 0x27390000
07667           && stub_words[3] == 0x00000000)
07668         return ((((stub_words[0] & 0x0000ffff) << 16)
07669                  + (stub_words[2] & 0x0000ffff)) ^ 0x8000) - 0x8000;
07670     }
07671 
07672   /* Not a recognized stub.  */
07673   return 0;
07674 }
07675 
07676 static CORE_ADDR
07677 mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
07678 {
07679   CORE_ADDR requested_pc = pc;
07680   CORE_ADDR target_pc;
07681   CORE_ADDR new_pc;
07682 
07683   do
07684     {
07685       target_pc = pc;
07686 
07687       new_pc = mips_skip_mips16_trampoline_code (frame, pc);
07688       if (new_pc)
07689         {
07690           pc = new_pc;
07691           if (is_compact_addr (pc))
07692             pc = unmake_compact_addr (pc);
07693         }
07694 
07695       new_pc = find_solib_trampoline_target (frame, pc);
07696       if (new_pc)
07697         {
07698           pc = new_pc;
07699           if (is_compact_addr (pc))
07700             pc = unmake_compact_addr (pc);
07701         }
07702 
07703       new_pc = mips_skip_pic_trampoline_code (frame, pc);
07704       if (new_pc)
07705         {
07706           pc = new_pc;
07707           if (is_compact_addr (pc))
07708             pc = unmake_compact_addr (pc);
07709         }
07710     }
07711   while (pc != target_pc);
07712 
07713   return pc != requested_pc ? pc : 0;
07714 }
07715 
07716 /* Convert a dbx stab register number (from `r' declaration) to a GDB
07717    [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
07718 
07719 static int
07720 mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
07721 {
07722   int regnum;
07723   if (num >= 0 && num < 32)
07724     regnum = num;
07725   else if (num >= 38 && num < 70)
07726     regnum = num + mips_regnum (gdbarch)->fp0 - 38;
07727   else if (num == 70)
07728     regnum = mips_regnum (gdbarch)->hi;
07729   else if (num == 71)
07730     regnum = mips_regnum (gdbarch)->lo;
07731   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 72 && num < 78)
07732     regnum = num + mips_regnum (gdbarch)->dspacc - 72;
07733   else
07734     /* This will hopefully (eventually) provoke a warning.  Should
07735        we be calling complaint() here?  */
07736     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
07737   return gdbarch_num_regs (gdbarch) + regnum;
07738 }
07739 
07740 
07741 /* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
07742    gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
07743 
07744 static int
07745 mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
07746 {
07747   int regnum;
07748   if (num >= 0 && num < 32)
07749     regnum = num;
07750   else if (num >= 32 && num < 64)
07751     regnum = num + mips_regnum (gdbarch)->fp0 - 32;
07752   else if (num == 64)
07753     regnum = mips_regnum (gdbarch)->hi;
07754   else if (num == 65)
07755     regnum = mips_regnum (gdbarch)->lo;
07756   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 66 && num < 72)
07757     regnum = num + mips_regnum (gdbarch)->dspacc - 66;
07758   else
07759     /* This will hopefully (eventually) provoke a warning.  Should we
07760        be calling complaint() here?  */
07761     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
07762   return gdbarch_num_regs (gdbarch) + regnum;
07763 }
07764 
07765 static int
07766 mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
07767 {
07768   /* Only makes sense to supply raw registers.  */
07769   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
07770   /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
07771      decide if it is valid.  Should instead define a standard sim/gdb
07772      register numbering scheme.  */
07773   if (gdbarch_register_name (gdbarch,
07774                              gdbarch_num_regs (gdbarch) + regnum) != NULL
07775       && gdbarch_register_name (gdbarch,
07776                                 gdbarch_num_regs (gdbarch)
07777                                 + regnum)[0] != '\0')
07778     return regnum;
07779   else
07780     return LEGACY_SIM_REGNO_IGNORE;
07781 }
07782 
07783 
07784 /* Convert an integer into an address.  Extracting the value signed
07785    guarantees a correctly sign extended address.  */
07786 
07787 static CORE_ADDR
07788 mips_integer_to_address (struct gdbarch *gdbarch,
07789                          struct type *type, const gdb_byte *buf)
07790 {
07791   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
07792   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
07793 }
07794 
07795 /* Dummy virtual frame pointer method.  This is no more or less accurate
07796    than most other architectures; we just need to be explicit about it,
07797    because the pseudo-register gdbarch_sp_regnum will otherwise lead to
07798    an assertion failure.  */
07799 
07800 static void
07801 mips_virtual_frame_pointer (struct gdbarch *gdbarch, 
07802                             CORE_ADDR pc, int *reg, LONGEST *offset)
07803 {
07804   *reg = MIPS_SP_REGNUM;
07805   *offset = 0;
07806 }
07807 
07808 static void
07809 mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
07810 {
07811   enum mips_abi *abip = (enum mips_abi *) obj;
07812   const char *name = bfd_get_section_name (abfd, sect);
07813 
07814   if (*abip != MIPS_ABI_UNKNOWN)
07815     return;
07816 
07817   if (strncmp (name, ".mdebug.", 8) != 0)
07818     return;
07819 
07820   if (strcmp (name, ".mdebug.abi32") == 0)
07821     *abip = MIPS_ABI_O32;
07822   else if (strcmp (name, ".mdebug.abiN32") == 0)
07823     *abip = MIPS_ABI_N32;
07824   else if (strcmp (name, ".mdebug.abi64") == 0)
07825     *abip = MIPS_ABI_N64;
07826   else if (strcmp (name, ".mdebug.abiO64") == 0)
07827     *abip = MIPS_ABI_O64;
07828   else if (strcmp (name, ".mdebug.eabi32") == 0)
07829     *abip = MIPS_ABI_EABI32;
07830   else if (strcmp (name, ".mdebug.eabi64") == 0)
07831     *abip = MIPS_ABI_EABI64;
07832   else
07833     warning (_("unsupported ABI %s."), name + 8);
07834 }
07835 
07836 static void
07837 mips_find_long_section (bfd *abfd, asection *sect, void *obj)
07838 {
07839   int *lbp = (int *) obj;
07840   const char *name = bfd_get_section_name (abfd, sect);
07841 
07842   if (strncmp (name, ".gcc_compiled_long32", 20) == 0)
07843     *lbp = 32;
07844   else if (strncmp (name, ".gcc_compiled_long64", 20) == 0)
07845     *lbp = 64;
07846   else if (strncmp (name, ".gcc_compiled_long", 18) == 0)
07847     warning (_("unrecognized .gcc_compiled_longXX"));
07848 }
07849 
07850 static enum mips_abi
07851 global_mips_abi (void)
07852 {
07853   int i;
07854 
07855   for (i = 0; mips_abi_strings[i] != NULL; i++)
07856     if (mips_abi_strings[i] == mips_abi_string)
07857       return (enum mips_abi) i;
07858 
07859   internal_error (__FILE__, __LINE__, _("unknown ABI string"));
07860 }
07861 
07862 /* Return the default compressed instruction set, either of MIPS16
07863    or microMIPS, selected when none could have been determined from
07864    the ELF header of the binary being executed (or no binary has been
07865    selected.  */
07866 
07867 static enum mips_isa
07868 global_mips_compression (void)
07869 {
07870   int i;
07871 
07872   for (i = 0; mips_compression_strings[i] != NULL; i++)
07873     if (mips_compression_strings[i] == mips_compression_string)
07874       return (enum mips_isa) i;
07875 
07876   internal_error (__FILE__, __LINE__, _("unknown compressed ISA string"));
07877 }
07878 
07879 static void
07880 mips_register_g_packet_guesses (struct gdbarch *gdbarch)
07881 {
07882   /* If the size matches the set of 32-bit or 64-bit integer registers,
07883      assume that's what we've got.  */
07884   register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
07885   register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);
07886 
07887   /* If the size matches the full set of registers GDB traditionally
07888      knows about, including floating point, for either 32-bit or
07889      64-bit, assume that's what we've got.  */
07890   register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
07891   register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);
07892 
07893   /* Otherwise we don't have a useful guess.  */
07894 }
07895 
07896 static struct value *
07897 value_of_mips_user_reg (struct frame_info *frame, const void *baton)
07898 {
07899   const int *reg_p = baton;
07900   return value_of_register (*reg_p, frame);
07901 }
07902 
07903 static struct gdbarch *
07904 mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
07905 {
07906   struct gdbarch *gdbarch;
07907   struct gdbarch_tdep *tdep;
07908   int elf_flags;
07909   enum mips_abi mips_abi, found_abi, wanted_abi;
07910   int i, num_regs;
07911   enum mips_fpu_type fpu_type;
07912   struct tdesc_arch_data *tdesc_data = NULL;
07913   int elf_fpu_type = Val_GNU_MIPS_ABI_FP_ANY;
07914   const char **reg_names;
07915   struct mips_regnum mips_regnum, *regnum;
07916   enum mips_isa mips_isa;
07917   int dspacc;
07918   int dspctl;
07919 
07920   /* Fill in the OS dependent register numbers and names.  */
07921   if (info.osabi == GDB_OSABI_IRIX)
07922     {
07923       mips_regnum.fp0 = 32;
07924       mips_regnum.pc = 64;
07925       mips_regnum.cause = 65;
07926       mips_regnum.badvaddr = 66;
07927       mips_regnum.hi = 67;
07928       mips_regnum.lo = 68;
07929       mips_regnum.fp_control_status = 69;
07930       mips_regnum.fp_implementation_revision = 70;
07931       mips_regnum.dspacc = dspacc = -1;
07932       mips_regnum.dspctl = dspctl = -1;
07933       num_regs = 71;
07934       reg_names = mips_irix_reg_names;
07935     }
07936   else if (info.osabi == GDB_OSABI_LINUX)
07937     {
07938       mips_regnum.fp0 = 38;
07939       mips_regnum.pc = 37;
07940       mips_regnum.cause = 36;
07941       mips_regnum.badvaddr = 35;
07942       mips_regnum.hi = 34;
07943       mips_regnum.lo = 33;
07944       mips_regnum.fp_control_status = 70;
07945       mips_regnum.fp_implementation_revision = 71;
07946       mips_regnum.dspacc = -1;
07947       mips_regnum.dspctl = -1;
07948       dspacc = 72;
07949       dspctl = 78;
07950       num_regs = 79;
07951       reg_names = mips_linux_reg_names;
07952     }
07953   else
07954     {
07955       mips_regnum.lo = MIPS_EMBED_LO_REGNUM;
07956       mips_regnum.hi = MIPS_EMBED_HI_REGNUM;
07957       mips_regnum.badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
07958       mips_regnum.cause = MIPS_EMBED_CAUSE_REGNUM;
07959       mips_regnum.pc = MIPS_EMBED_PC_REGNUM;
07960       mips_regnum.fp0 = MIPS_EMBED_FP0_REGNUM;
07961       mips_regnum.fp_control_status = 70;
07962       mips_regnum.fp_implementation_revision = 71;
07963       mips_regnum.dspacc = dspacc = -1;
07964       mips_regnum.dspctl = dspctl = -1;
07965       num_regs = MIPS_LAST_EMBED_REGNUM + 1;
07966       if (info.bfd_arch_info != NULL
07967           && info.bfd_arch_info->mach == bfd_mach_mips3900)
07968         reg_names = mips_tx39_reg_names;
07969       else
07970         reg_names = mips_generic_reg_names;
07971     }
07972 
07973   /* Check any target description for validity.  */
07974   if (tdesc_has_registers (info.target_desc))
07975     {
07976       static const char *const mips_gprs[] = {
07977         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
07978         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
07979         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
07980         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
07981       };
07982       static const char *const mips_fprs[] = {
07983         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
07984         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
07985         "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
07986         "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
07987       };
07988 
07989       const struct tdesc_feature *feature;
07990       int valid_p;
07991 
07992       feature = tdesc_find_feature (info.target_desc,
07993                                     "org.gnu.gdb.mips.cpu");
07994       if (feature == NULL)
07995         return NULL;
07996 
07997       tdesc_data = tdesc_data_alloc ();
07998 
07999       valid_p = 1;
08000       for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
08001         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
08002                                             mips_gprs[i]);
08003 
08004 
08005       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08006                                           mips_regnum.lo, "lo");
08007       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08008                                           mips_regnum.hi, "hi");
08009       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08010                                           mips_regnum.pc, "pc");
08011 
08012       if (!valid_p)
08013         {
08014           tdesc_data_cleanup (tdesc_data);
08015           return NULL;
08016         }
08017 
08018       feature = tdesc_find_feature (info.target_desc,
08019                                     "org.gnu.gdb.mips.cp0");
08020       if (feature == NULL)
08021         {
08022           tdesc_data_cleanup (tdesc_data);
08023           return NULL;
08024         }
08025 
08026       valid_p = 1;
08027       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08028                                           mips_regnum.badvaddr, "badvaddr");
08029       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08030                                           MIPS_PS_REGNUM, "status");
08031       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08032                                           mips_regnum.cause, "cause");
08033 
08034       if (!valid_p)
08035         {
08036           tdesc_data_cleanup (tdesc_data);
08037           return NULL;
08038         }
08039 
08040       /* FIXME drow/2007-05-17: The FPU should be optional.  The MIPS
08041          backend is not prepared for that, though.  */
08042       feature = tdesc_find_feature (info.target_desc,
08043                                     "org.gnu.gdb.mips.fpu");
08044       if (feature == NULL)
08045         {
08046           tdesc_data_cleanup (tdesc_data);
08047           return NULL;
08048         }
08049 
08050       valid_p = 1;
08051       for (i = 0; i < 32; i++)
08052         valid_p &= tdesc_numbered_register (feature, tdesc_data,
08053                                             i + mips_regnum.fp0, mips_fprs[i]);
08054 
08055       valid_p &= tdesc_numbered_register (feature, tdesc_data,
08056                                           mips_regnum.fp_control_status,
08057                                           "fcsr");
08058       valid_p
08059         &= tdesc_numbered_register (feature, tdesc_data,
08060                                     mips_regnum.fp_implementation_revision,
08061                                     "fir");
08062 
08063       if (!valid_p)
08064         {
08065           tdesc_data_cleanup (tdesc_data);
08066           return NULL;
08067         }
08068 
08069       if (dspacc >= 0)
08070         {
08071           feature = tdesc_find_feature (info.target_desc,
08072                                         "org.gnu.gdb.mips.dsp");
08073           /* The DSP registers are optional; it's OK if they are absent.  */
08074           if (feature != NULL)
08075             {
08076               i = 0;
08077               valid_p = 1;
08078               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08079                                                   dspacc + i++, "hi1");
08080               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08081                                                   dspacc + i++, "lo1");
08082               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08083                                                   dspacc + i++, "hi2");
08084               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08085                                                   dspacc + i++, "lo2");
08086               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08087                                                   dspacc + i++, "hi3");
08088               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08089                                                   dspacc + i++, "lo3");
08090 
08091               valid_p &= tdesc_numbered_register (feature, tdesc_data,
08092                                                   dspctl, "dspctl");
08093 
08094               if (!valid_p)
08095                 {
08096                   tdesc_data_cleanup (tdesc_data);
08097                   return NULL;
08098                 }
08099 
08100               mips_regnum.dspacc = dspacc;
08101               mips_regnum.dspctl = dspctl;
08102             }
08103         }
08104 
08105       /* It would be nice to detect an attempt to use a 64-bit ABI
08106          when only 32-bit registers are provided.  */
08107       reg_names = NULL;
08108     }
08109 
08110   /* First of all, extract the elf_flags, if available.  */
08111   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
08112     elf_flags = elf_elfheader (info.abfd)->e_flags;
08113   else if (arches != NULL)
08114     elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
08115   else
08116     elf_flags = 0;
08117   if (gdbarch_debug)
08118     fprintf_unfiltered (gdb_stdlog,
08119                         "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
08120 
08121   /* Check ELF_FLAGS to see if it specifies the ABI being used.  */
08122   switch ((elf_flags & EF_MIPS_ABI))
08123     {
08124     case E_MIPS_ABI_O32:
08125       found_abi = MIPS_ABI_O32;
08126       break;
08127     case E_MIPS_ABI_O64:
08128       found_abi = MIPS_ABI_O64;
08129       break;
08130     case E_MIPS_ABI_EABI32:
08131       found_abi = MIPS_ABI_EABI32;
08132       break;
08133     case E_MIPS_ABI_EABI64:
08134       found_abi = MIPS_ABI_EABI64;
08135       break;
08136     default:
08137       if ((elf_flags & EF_MIPS_ABI2))
08138         found_abi = MIPS_ABI_N32;
08139       else
08140         found_abi = MIPS_ABI_UNKNOWN;
08141       break;
08142     }
08143 
08144   /* GCC creates a pseudo-section whose name describes the ABI.  */
08145   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
08146     bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
08147 
08148   /* If we have no useful BFD information, use the ABI from the last
08149      MIPS architecture (if there is one).  */
08150   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
08151     found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
08152 
08153   /* Try the architecture for any hint of the correct ABI.  */
08154   if (found_abi == MIPS_ABI_UNKNOWN
08155       && info.bfd_arch_info != NULL
08156       && info.bfd_arch_info->arch == bfd_arch_mips)
08157     {
08158       switch (info.bfd_arch_info->mach)
08159         {
08160         case bfd_mach_mips3900:
08161           found_abi = MIPS_ABI_EABI32;
08162           break;
08163         case bfd_mach_mips4100:
08164         case bfd_mach_mips5000:
08165           found_abi = MIPS_ABI_EABI64;
08166           break;
08167         case bfd_mach_mips8000:
08168         case bfd_mach_mips10000:
08169           /* On Irix, ELF64 executables use the N64 ABI.  The
08170              pseudo-sections which describe the ABI aren't present
08171              on IRIX.  (Even for executables created by gcc.)  */
08172           if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
08173               && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
08174             found_abi = MIPS_ABI_N64;
08175           else
08176             found_abi = MIPS_ABI_N32;
08177           break;
08178         }
08179     }
08180 
08181   /* Default 64-bit objects to N64 instead of O32.  */
08182   if (found_abi == MIPS_ABI_UNKNOWN
08183       && info.abfd != NULL
08184       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
08185       && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
08186     found_abi = MIPS_ABI_N64;
08187 
08188   if (gdbarch_debug)
08189     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
08190                         found_abi);
08191 
08192   /* What has the user specified from the command line?  */
08193   wanted_abi = global_mips_abi ();
08194   if (gdbarch_debug)
08195     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
08196                         wanted_abi);
08197 
08198   /* Now that we have found what the ABI for this binary would be,
08199      check whether the user is overriding it.  */
08200   if (wanted_abi != MIPS_ABI_UNKNOWN)
08201     mips_abi = wanted_abi;
08202   else if (found_abi != MIPS_ABI_UNKNOWN)
08203     mips_abi = found_abi;
08204   else
08205     mips_abi = MIPS_ABI_O32;
08206   if (gdbarch_debug)
08207     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
08208                         mips_abi);
08209 
08210   /* Determine the default compressed ISA.  */
08211   if ((elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0
08212       && (elf_flags & EF_MIPS_ARCH_ASE_M16) == 0)
08213     mips_isa = ISA_MICROMIPS;
08214   else if ((elf_flags & EF_MIPS_ARCH_ASE_M16) != 0
08215            && (elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) == 0)
08216     mips_isa = ISA_MIPS16;
08217   else
08218     mips_isa = global_mips_compression ();
08219   mips_compression_string = mips_compression_strings[mips_isa];
08220 
08221   /* Also used when doing an architecture lookup.  */
08222   if (gdbarch_debug)
08223     fprintf_unfiltered (gdb_stdlog,
08224                         "mips_gdbarch_init: "
08225                         "mips64_transfers_32bit_regs_p = %d\n",
08226                         mips64_transfers_32bit_regs_p);
08227 
08228   /* Determine the MIPS FPU type.  */
08229 #ifdef HAVE_ELF
08230   if (info.abfd
08231       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
08232     elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
08233                                              Tag_GNU_MIPS_ABI_FP);
08234 #endif /* HAVE_ELF */
08235 
08236   if (!mips_fpu_type_auto)
08237     fpu_type = mips_fpu_type;
08238   else if (elf_fpu_type != Val_GNU_MIPS_ABI_FP_ANY)
08239     {
08240       switch (elf_fpu_type)
08241         {
08242         case Val_GNU_MIPS_ABI_FP_DOUBLE:
08243           fpu_type = MIPS_FPU_DOUBLE;
08244           break;
08245         case Val_GNU_MIPS_ABI_FP_SINGLE:
08246           fpu_type = MIPS_FPU_SINGLE;
08247           break;
08248         case Val_GNU_MIPS_ABI_FP_SOFT:
08249         default:
08250           /* Soft float or unknown.  */
08251           fpu_type = MIPS_FPU_NONE;
08252           break;
08253         }
08254     }
08255   else if (info.bfd_arch_info != NULL
08256            && info.bfd_arch_info->arch == bfd_arch_mips)
08257     switch (info.bfd_arch_info->mach)
08258       {
08259       case bfd_mach_mips3900:
08260       case bfd_mach_mips4100:
08261       case bfd_mach_mips4111:
08262       case bfd_mach_mips4120:
08263         fpu_type = MIPS_FPU_NONE;
08264         break;
08265       case bfd_mach_mips4650:
08266         fpu_type = MIPS_FPU_SINGLE;
08267         break;
08268       default:
08269         fpu_type = MIPS_FPU_DOUBLE;
08270         break;
08271       }
08272   else if (arches != NULL)
08273     fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
08274   else
08275     fpu_type = MIPS_FPU_DOUBLE;
08276   if (gdbarch_debug)
08277     fprintf_unfiltered (gdb_stdlog,
08278                         "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
08279 
08280   /* Check for blatant incompatibilities.  */
08281 
08282   /* If we have only 32-bit registers, then we can't debug a 64-bit
08283      ABI.  */
08284   if (info.target_desc
08285       && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
08286       && mips_abi != MIPS_ABI_EABI32
08287       && mips_abi != MIPS_ABI_O32)
08288     {
08289       if (tdesc_data != NULL)
08290         tdesc_data_cleanup (tdesc_data);
08291       return NULL;
08292     }
08293 
08294   /* Try to find a pre-existing architecture.  */
08295   for (arches = gdbarch_list_lookup_by_info (arches, &info);
08296        arches != NULL;
08297        arches = gdbarch_list_lookup_by_info (arches->next, &info))
08298     {
08299       /* MIPS needs to be pedantic about which ABI the object is
08300          using.  */
08301       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
08302         continue;
08303       if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
08304         continue;
08305       /* Need to be pedantic about which register virtual size is
08306          used.  */
08307       if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
08308           != mips64_transfers_32bit_regs_p)
08309         continue;
08310       /* Be pedantic about which FPU is selected.  */
08311       if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
08312         continue;
08313 
08314       if (tdesc_data != NULL)
08315         tdesc_data_cleanup (tdesc_data);
08316       return arches->gdbarch;
08317     }
08318 
08319   /* Need a new architecture.  Fill in a target specific vector.  */
08320   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
08321   gdbarch = gdbarch_alloc (&info, tdep);
08322   tdep->elf_flags = elf_flags;
08323   tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
08324   tdep->found_abi = found_abi;
08325   tdep->mips_abi = mips_abi;
08326   tdep->mips_isa = mips_isa;
08327   tdep->mips_fpu_type = fpu_type;
08328   tdep->register_size_valid_p = 0;
08329   tdep->register_size = 0;
08330   tdep->gregset = NULL;
08331   tdep->gregset64 = NULL;
08332   tdep->fpregset = NULL;
08333   tdep->fpregset64 = NULL;
08334 
08335   if (info.target_desc)
08336     {
08337       /* Some useful properties can be inferred from the target.  */
08338       if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
08339         {
08340           tdep->register_size_valid_p = 1;
08341           tdep->register_size = 4;
08342         }
08343       else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
08344         {
08345           tdep->register_size_valid_p = 1;
08346           tdep->register_size = 8;
08347         }
08348     }
08349 
08350   /* Initially set everything according to the default ABI/ISA.  */
08351   set_gdbarch_short_bit (gdbarch, 16);
08352   set_gdbarch_int_bit (gdbarch, 32);
08353   set_gdbarch_float_bit (gdbarch, 32);
08354   set_gdbarch_double_bit (gdbarch, 64);
08355   set_gdbarch_long_double_bit (gdbarch, 64);
08356   set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
08357   set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
08358   set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
08359 
08360   set_gdbarch_ax_pseudo_register_collect (gdbarch,
08361                                           mips_ax_pseudo_register_collect);
08362   set_gdbarch_ax_pseudo_register_push_stack
08363       (gdbarch, mips_ax_pseudo_register_push_stack);
08364 
08365   set_gdbarch_elf_make_msymbol_special (gdbarch,
08366                                         mips_elf_make_msymbol_special);
08367 
08368   regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct mips_regnum);
08369   *regnum = mips_regnum;
08370   set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
08371   set_gdbarch_num_regs (gdbarch, num_regs);
08372   set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
08373   set_gdbarch_register_name (gdbarch, mips_register_name);
08374   set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
08375   tdep->mips_processor_reg_names = reg_names;
08376   tdep->regnum = regnum;
08377 
08378   switch (mips_abi)
08379     {
08380     case MIPS_ABI_O32:
08381       set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
08382       set_gdbarch_return_value (gdbarch, mips_o32_return_value);
08383       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
08384       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
08385       tdep->default_mask_address_p = 0;
08386       set_gdbarch_long_bit (gdbarch, 32);
08387       set_gdbarch_ptr_bit (gdbarch, 32);
08388       set_gdbarch_long_long_bit (gdbarch, 64);
08389       break;
08390     case MIPS_ABI_O64:
08391       set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
08392       set_gdbarch_return_value (gdbarch, mips_o64_return_value);
08393       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
08394       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
08395       tdep->default_mask_address_p = 0;
08396       set_gdbarch_long_bit (gdbarch, 32);
08397       set_gdbarch_ptr_bit (gdbarch, 32);
08398       set_gdbarch_long_long_bit (gdbarch, 64);
08399       break;
08400     case MIPS_ABI_EABI32:
08401       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
08402       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
08403       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
08404       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
08405       tdep->default_mask_address_p = 0;
08406       set_gdbarch_long_bit (gdbarch, 32);
08407       set_gdbarch_ptr_bit (gdbarch, 32);
08408       set_gdbarch_long_long_bit (gdbarch, 64);
08409       break;
08410     case MIPS_ABI_EABI64:
08411       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
08412       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
08413       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
08414       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
08415       tdep->default_mask_address_p = 0;
08416       set_gdbarch_long_bit (gdbarch, 64);
08417       set_gdbarch_ptr_bit (gdbarch, 64);
08418       set_gdbarch_long_long_bit (gdbarch, 64);
08419       break;
08420     case MIPS_ABI_N32:
08421       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
08422       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
08423       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
08424       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
08425       tdep->default_mask_address_p = 0;
08426       set_gdbarch_long_bit (gdbarch, 32);
08427       set_gdbarch_ptr_bit (gdbarch, 32);
08428       set_gdbarch_long_long_bit (gdbarch, 64);
08429       set_gdbarch_long_double_bit (gdbarch, 128);
08430       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
08431       break;
08432     case MIPS_ABI_N64:
08433       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
08434       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
08435       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
08436       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
08437       tdep->default_mask_address_p = 0;
08438       set_gdbarch_long_bit (gdbarch, 64);
08439       set_gdbarch_ptr_bit (gdbarch, 64);
08440       set_gdbarch_long_long_bit (gdbarch, 64);
08441       set_gdbarch_long_double_bit (gdbarch, 128);
08442       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
08443       break;
08444     default:
08445       internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
08446     }
08447 
08448   /* GCC creates a pseudo-section whose name specifies the size of
08449      longs, since -mlong32 or -mlong64 may be used independent of
08450      other options.  How those options affect pointer sizes is ABI and
08451      architecture dependent, so use them to override the default sizes
08452      set by the ABI.  This table shows the relationship between ABI,
08453      -mlongXX, and size of pointers:
08454 
08455      ABI                -mlongXX        ptr bits
08456      ---                --------        --------
08457      o32                32              32
08458      o32                64              32
08459      n32                32              32
08460      n32                64              64
08461      o64                32              32
08462      o64                64              64
08463      n64                32              32
08464      n64                64              64
08465      eabi32             32              32
08466      eabi32             64              32
08467      eabi64             32              32
08468      eabi64             64              64
08469 
08470     Note that for o32 and eabi32, pointers are always 32 bits
08471     regardless of any -mlongXX option.  For all others, pointers and
08472     longs are the same, as set by -mlongXX or set by defaults.  */
08473 
08474   if (info.abfd != NULL)
08475     {
08476       int long_bit = 0;
08477 
08478       bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
08479       if (long_bit)
08480         {
08481           set_gdbarch_long_bit (gdbarch, long_bit);
08482           switch (mips_abi)
08483             {
08484             case MIPS_ABI_O32:
08485             case MIPS_ABI_EABI32:
08486               break;
08487             case MIPS_ABI_N32:
08488             case MIPS_ABI_O64:
08489             case MIPS_ABI_N64:
08490             case MIPS_ABI_EABI64:
08491               set_gdbarch_ptr_bit (gdbarch, long_bit);
08492               break;
08493             default:
08494               internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
08495             }
08496         }
08497     }
08498 
08499   /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
08500      that could indicate -gp32 BUT gas/config/tc-mips.c contains the
08501      comment:
08502 
08503      ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
08504      flag in object files because to do so would make it impossible to
08505      link with libraries compiled without "-gp32".  This is
08506      unnecessarily restrictive.
08507 
08508      We could solve this problem by adding "-gp32" multilibs to gcc,
08509      but to set this flag before gcc is built with such multilibs will
08510      break too many systems.''
08511 
08512      But even more unhelpfully, the default linker output target for
08513      mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
08514      for 64-bit programs - you need to change the ABI to change this,
08515      and not all gcc targets support that currently.  Therefore using
08516      this flag to detect 32-bit mode would do the wrong thing given
08517      the current gcc - it would make GDB treat these 64-bit programs
08518      as 32-bit programs by default.  */
08519 
08520   set_gdbarch_read_pc (gdbarch, mips_read_pc);
08521   set_gdbarch_write_pc (gdbarch, mips_write_pc);
08522 
08523   /* Add/remove bits from an address.  The MIPS needs be careful to
08524      ensure that all 32 bit addresses are sign extended to 64 bits.  */
08525   set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
08526 
08527   /* Unwind the frame.  */
08528   set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
08529   set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
08530   set_gdbarch_dummy_id (gdbarch, mips_dummy_id);
08531 
08532   /* Map debug register numbers onto internal register numbers.  */
08533   set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
08534   set_gdbarch_ecoff_reg_to_regnum (gdbarch,
08535                                    mips_dwarf_dwarf2_ecoff_reg_to_regnum);
08536   set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
08537                                     mips_dwarf_dwarf2_ecoff_reg_to_regnum);
08538   set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
08539 
08540   /* MIPS version of CALL_DUMMY.  */
08541 
08542   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
08543   set_gdbarch_push_dummy_code (gdbarch, mips_push_dummy_code);
08544   set_gdbarch_frame_align (gdbarch, mips_frame_align);
08545 
08546   set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
08547   set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
08548   set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
08549 
08550   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
08551   set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
08552   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
08553                                          mips_remote_breakpoint_from_pc);
08554   set_gdbarch_adjust_breakpoint_address (gdbarch,
08555                                          mips_adjust_breakpoint_address);
08556 
08557   set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
08558 
08559   set_gdbarch_in_function_epilogue_p (gdbarch, mips_in_function_epilogue_p);
08560 
08561   set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
08562   set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
08563   set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
08564 
08565   set_gdbarch_register_type (gdbarch, mips_register_type);
08566 
08567   set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
08568 
08569   if (mips_abi == MIPS_ABI_N32)
08570     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
08571   else if (mips_abi == MIPS_ABI_N64)
08572     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
08573   else
08574     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
08575 
08576   /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
08577      HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
08578      need to all be folded into the target vector.  Since they are
08579      being used as guards for target_stopped_by_watchpoint, why not have
08580      target_stopped_by_watchpoint return the type of watchpoint that the code
08581      is sitting on?  */
08582   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
08583 
08584   set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
08585 
08586   /* NOTE drow/2012-04-25: We overload the core solib trampoline code
08587      to support MIPS16.  This is a bad thing.  Make sure not to do it
08588      if we have an OS ABI that actually supports shared libraries, since
08589      shared library support is more important.  If we have an OS someday
08590      that supports both shared libraries and MIPS16, we'll have to find
08591      a better place for these.
08592      macro/2012-04-25: But that applies to return trampolines only and
08593      currently no MIPS OS ABI uses shared libraries that have them.  */
08594   set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);
08595 
08596   set_gdbarch_single_step_through_delay (gdbarch,
08597                                          mips_single_step_through_delay);
08598 
08599   /* Virtual tables.  */
08600   set_gdbarch_vbit_in_delta (gdbarch, 1);
08601 
08602   mips_register_g_packet_guesses (gdbarch);
08603 
08604   /* Hook in OS ABI-specific overrides, if they have been registered.  */
08605   info.tdep_info = (void *) tdesc_data;
08606   gdbarch_init_osabi (info, gdbarch);
08607 
08608   /* The hook may have adjusted num_regs, fetch the final value and
08609      set pc_regnum and sp_regnum now that it has been fixed.  */
08610   num_regs = gdbarch_num_regs (gdbarch);
08611   set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
08612   set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
08613 
08614   /* Unwind the frame.  */
08615   dwarf2_append_unwinders (gdbarch);
08616   frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
08617   frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
08618   frame_unwind_append_unwinder (gdbarch, &mips_micro_frame_unwind);
08619   frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
08620   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
08621   frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
08622   frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
08623   frame_base_append_sniffer (gdbarch, mips_micro_frame_base_sniffer);
08624   frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
08625 
08626   if (tdesc_data)
08627     {
08628       set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
08629       tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
08630 
08631       /* Override the normal target description methods to handle our
08632          dual real and pseudo registers.  */
08633       set_gdbarch_register_name (gdbarch, mips_register_name);
08634       set_gdbarch_register_reggroup_p (gdbarch,
08635                                        mips_tdesc_register_reggroup_p);
08636 
08637       num_regs = gdbarch_num_regs (gdbarch);
08638       set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
08639       set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
08640       set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
08641     }
08642 
08643   /* Add ABI-specific aliases for the registers.  */
08644   if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
08645     for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
08646       user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
08647                     value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
08648   else
08649     for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
08650       user_reg_add (gdbarch, mips_o32_aliases[i].name,
08651                     value_of_mips_user_reg, &mips_o32_aliases[i].regnum);
08652 
08653   /* Add some other standard aliases.  */
08654   for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
08655     user_reg_add (gdbarch, mips_register_aliases[i].name,
08656                   value_of_mips_user_reg, &mips_register_aliases[i].regnum);
08657 
08658   for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
08659     user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
08660                   value_of_mips_user_reg, 
08661                   &mips_numeric_register_aliases[i].regnum);
08662 
08663   return gdbarch;
08664 }
08665 
08666 static void
08667 mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
08668 {
08669   struct gdbarch_info info;
08670 
08671   /* Force the architecture to update, and (if it's a MIPS architecture)
08672      mips_gdbarch_init will take care of the rest.  */
08673   gdbarch_info_init (&info);
08674   gdbarch_update_p (info);
08675 }
08676 
08677 /* Print out which MIPS ABI is in use.  */
08678 
08679 static void
08680 show_mips_abi (struct ui_file *file,
08681                int from_tty,
08682                struct cmd_list_element *ignored_cmd,
08683                const char *ignored_value)
08684 {
08685   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
08686     fprintf_filtered
08687       (file, 
08688        "The MIPS ABI is unknown because the current architecture "
08689        "is not MIPS.\n");
08690   else
08691     {
08692       enum mips_abi global_abi = global_mips_abi ();
08693       enum mips_abi actual_abi = mips_abi (target_gdbarch ());
08694       const char *actual_abi_str = mips_abi_strings[actual_abi];
08695 
08696       if (global_abi == MIPS_ABI_UNKNOWN)
08697         fprintf_filtered
08698           (file, 
08699            "The MIPS ABI is set automatically (currently \"%s\").\n",
08700            actual_abi_str);
08701       else if (global_abi == actual_abi)
08702         fprintf_filtered
08703           (file,
08704            "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
08705            actual_abi_str);
08706       else
08707         {
08708           /* Probably shouldn't happen...  */
08709           fprintf_filtered (file,
08710                             "The (auto detected) MIPS ABI \"%s\" is in use "
08711                             "even though the user setting was \"%s\".\n",
08712              actual_abi_str, mips_abi_strings[global_abi]);
08713         }
08714     }
08715 }
08716 
08717 /* Print out which MIPS compressed ISA encoding is used.  */
08718 
08719 static void
08720 show_mips_compression (struct ui_file *file, int from_tty,
08721                        struct cmd_list_element *c, const char *value)
08722 {
08723   fprintf_filtered (file, _("The compressed ISA encoding used is %s.\n"),
08724                     value);
08725 }
08726 
08727 static void
08728 mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
08729 {
08730   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
08731   if (tdep != NULL)
08732     {
08733       int ef_mips_arch;
08734       int ef_mips_32bitmode;
08735       /* Determine the ISA.  */
08736       switch (tdep->elf_flags & EF_MIPS_ARCH)
08737         {
08738         case E_MIPS_ARCH_1:
08739           ef_mips_arch = 1;
08740           break;
08741         case E_MIPS_ARCH_2:
08742           ef_mips_arch = 2;
08743           break;
08744         case E_MIPS_ARCH_3:
08745           ef_mips_arch = 3;
08746           break;
08747         case E_MIPS_ARCH_4:
08748           ef_mips_arch = 4;
08749           break;
08750         default:
08751           ef_mips_arch = 0;
08752           break;
08753         }
08754       /* Determine the size of a pointer.  */
08755       ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
08756       fprintf_unfiltered (file,
08757                           "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
08758                           tdep->elf_flags);
08759       fprintf_unfiltered (file,
08760                           "mips_dump_tdep: ef_mips_32bitmode = %d\n",
08761                           ef_mips_32bitmode);
08762       fprintf_unfiltered (file,
08763                           "mips_dump_tdep: ef_mips_arch = %d\n",
08764                           ef_mips_arch);
08765       fprintf_unfiltered (file,
08766                           "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
08767                           tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
08768       fprintf_unfiltered (file,
08769                           "mips_dump_tdep: "
08770                           "mips_mask_address_p() %d (default %d)\n",
08771                           mips_mask_address_p (tdep),
08772                           tdep->default_mask_address_p);
08773     }
08774   fprintf_unfiltered (file,
08775                       "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
08776                       MIPS_DEFAULT_FPU_TYPE,
08777                       (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
08778                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
08779                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
08780                        : "???"));
08781   fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
08782                       MIPS_EABI (gdbarch));
08783   fprintf_unfiltered (file,
08784                       "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
08785                       MIPS_FPU_TYPE (gdbarch),
08786                       (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
08787                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
08788                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
08789                        : "???"));
08790 }
08791 
08792 extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
08793 
08794 void
08795 _initialize_mips_tdep (void)
08796 {
08797   static struct cmd_list_element *mipsfpulist = NULL;
08798   struct cmd_list_element *c;
08799 
08800   mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
08801   if (MIPS_ABI_LAST + 1
08802       != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
08803     internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
08804 
08805   gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
08806 
08807   mips_pdr_data = register_objfile_data ();
08808 
08809   /* Create feature sets with the appropriate properties.  The values
08810      are not important.  */
08811   mips_tdesc_gp32 = allocate_target_description ();
08812   set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");
08813 
08814   mips_tdesc_gp64 = allocate_target_description ();
08815   set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");
08816 
08817   /* Add root prefix command for all "set mips"/"show mips" commands.  */
08818   add_prefix_cmd ("mips", no_class, set_mips_command,
08819                   _("Various MIPS specific commands."),
08820                   &setmipscmdlist, "set mips ", 0, &setlist);
08821 
08822   add_prefix_cmd ("mips", no_class, show_mips_command,
08823                   _("Various MIPS specific commands."),
08824                   &showmipscmdlist, "show mips ", 0, &showlist);
08825 
08826   /* Allow the user to override the ABI.  */
08827   add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
08828                         &mips_abi_string, _("\
08829 Set the MIPS ABI used by this program."), _("\
08830 Show the MIPS ABI used by this program."), _("\
08831 This option can be set to one of:\n\
08832   auto  - the default ABI associated with the current binary\n\
08833   o32\n\
08834   o64\n\
08835   n32\n\
08836   n64\n\
08837   eabi32\n\
08838   eabi64"),
08839                         mips_abi_update,
08840                         show_mips_abi,
08841                         &setmipscmdlist, &showmipscmdlist);
08842 
08843   /* Allow the user to set the ISA to assume for compressed code if ELF
08844      file flags don't tell or there is no program file selected.  This
08845      setting is updated whenever unambiguous ELF file flags are interpreted,
08846      and carried over to subsequent sessions.  */
08847   add_setshow_enum_cmd ("compression", class_obscure, mips_compression_strings,
08848                         &mips_compression_string, _("\
08849 Set the compressed ISA encoding used by MIPS code."), _("\
08850 Show the compressed ISA encoding used by MIPS code."), _("\
08851 Select the compressed ISA encoding used in functions that have no symbol\n\
08852 information available.  The encoding can be set to either of:\n\
08853   mips16\n\
08854   micromips\n\
08855 and is updated automatically from ELF file flags if available."),
08856                         mips_abi_update,
08857                         show_mips_compression,
08858                         &setmipscmdlist, &showmipscmdlist);
08859 
08860   /* Let the user turn off floating point and set the fence post for
08861      heuristic_proc_start.  */
08862 
08863   add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
08864                   _("Set use of MIPS floating-point coprocessor."),
08865                   &mipsfpulist, "set mipsfpu ", 0, &setlist);
08866   add_cmd ("single", class_support, set_mipsfpu_single_command,
08867            _("Select single-precision MIPS floating-point coprocessor."),
08868            &mipsfpulist);
08869   add_cmd ("double", class_support, set_mipsfpu_double_command,
08870            _("Select double-precision MIPS floating-point coprocessor."),
08871            &mipsfpulist);
08872   add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
08873   add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
08874   add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
08875   add_cmd ("none", class_support, set_mipsfpu_none_command,
08876            _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
08877   add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
08878   add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
08879   add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
08880   add_cmd ("auto", class_support, set_mipsfpu_auto_command,
08881            _("Select MIPS floating-point coprocessor automatically."),
08882            &mipsfpulist);
08883   add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
08884            _("Show current use of MIPS floating-point coprocessor target."),
08885            &showlist);
08886 
08887   /* We really would like to have both "0" and "unlimited" work, but
08888      command.c doesn't deal with that.  So make it a var_zinteger
08889      because the user can always use "999999" or some such for unlimited.  */
08890   add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
08891                             &heuristic_fence_post, _("\
08892 Set the distance searched for the start of a function."), _("\
08893 Show the distance searched for the start of a function."), _("\
08894 If you are debugging a stripped executable, GDB needs to search through the\n\
08895 program for the start of a function.  This command sets the distance of the\n\
08896 search.  The only need to set it is when debugging a stripped executable."),
08897                             reinit_frame_cache_sfunc,
08898                             NULL, /* FIXME: i18n: The distance searched for
08899                                      the start of a function is %s.  */
08900                             &setlist, &showlist);
08901 
08902   /* Allow the user to control whether the upper bits of 64-bit
08903      addresses should be zeroed.  */
08904   add_setshow_auto_boolean_cmd ("mask-address", no_class,
08905                                 &mask_address_var, _("\
08906 Set zeroing of upper 32 bits of 64-bit addresses."), _("\
08907 Show zeroing of upper 32 bits of 64-bit addresses."), _("\
08908 Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
08909 allow GDB to determine the correct value."),
08910                                 NULL, show_mask_address,
08911                                 &setmipscmdlist, &showmipscmdlist);
08912 
08913   /* Allow the user to control the size of 32 bit registers within the
08914      raw remote packet.  */
08915   add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
08916                            &mips64_transfers_32bit_regs_p, _("\
08917 Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
08918                            _("\
08919 Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
08920                            _("\
08921 Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
08922 that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
08923 64 bits for others.  Use \"off\" to disable compatibility mode"),
08924                            set_mips64_transfers_32bit_regs,
08925                            NULL, /* FIXME: i18n: Compatibility with 64-bit
08926                                     MIPS target that transfers 32-bit
08927                                     quantities is %s.  */
08928                            &setlist, &showlist);
08929 
08930   /* Debug this files internals.  */
08931   add_setshow_zuinteger_cmd ("mips", class_maintenance,
08932                              &mips_debug, _("\
08933 Set mips debugging."), _("\
08934 Show mips debugging."), _("\
08935 When non-zero, mips specific debugging is enabled."),
08936                              NULL,
08937                              NULL, /* FIXME: i18n: Mips debugging is
08938                                       currently %s.  */
08939                              &setdebuglist, &showdebuglist);
08940 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines