Constify strings in tracepoint.c, lookup_cmd and the completers.
[platform/upstream/binutils.git] / gdb / mips-tdep.c
1 /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
2
3    Copyright (C) 1988-2013 Free Software Foundation, Inc.
4
5    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
6    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "gdb_assert.h"
26 #include "frame.h"
27 #include "inferior.h"
28 #include "symtab.h"
29 #include "value.h"
30 #include "gdbcmd.h"
31 #include "language.h"
32 #include "gdbcore.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "gdbtypes.h"
36 #include "target.h"
37 #include "arch-utils.h"
38 #include "regcache.h"
39 #include "osabi.h"
40 #include "mips-tdep.h"
41 #include "block.h"
42 #include "reggroups.h"
43 #include "opcode/mips.h"
44 #include "elf/mips.h"
45 #include "elf-bfd.h"
46 #include "symcat.h"
47 #include "sim-regno.h"
48 #include "dis-asm.h"
49 #include "frame-unwind.h"
50 #include "frame-base.h"
51 #include "trad-frame.h"
52 #include "infcall.h"
53 #include "floatformat.h"
54 #include "remote.h"
55 #include "target-descriptions.h"
56 #include "dwarf2-frame.h"
57 #include "user-regs.h"
58 #include "valprint.h"
59 #include "ax.h"
60
61 static const struct objfile_data *mips_pdr_data;
62
63 static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
64
65 static int mips32_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR);
66 static int micromips_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR,
67                                                  int);
68 static int mips16_instruction_has_delay_slot (struct gdbarch *, CORE_ADDR,
69                                               int);
70
71 /* A useful bit in the CP0 status register (MIPS_PS_REGNUM).  */
72 /* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip.  */
73 #define ST0_FR (1 << 26)
74
75 /* The sizes of floating point registers.  */
76
77 enum
78 {
79   MIPS_FPU_SINGLE_REGSIZE = 4,
80   MIPS_FPU_DOUBLE_REGSIZE = 8
81 };
82
83 enum
84 {
85   MIPS32_REGSIZE = 4,
86   MIPS64_REGSIZE = 8
87 };
88
89 static const char *mips_abi_string;
90
91 static const char *const mips_abi_strings[] = {
92   "auto",
93   "n32",
94   "o32",
95   "n64",
96   "o64",
97   "eabi32",
98   "eabi64",
99   NULL
100 };
101
102 /* For backwards compatibility we default to MIPS16.  This flag is
103    overridden as soon as unambiguous ELF file flags tell us the
104    compressed ISA encoding used.  */
105 static const char mips_compression_mips16[] = "mips16";
106 static const char mips_compression_micromips[] = "micromips";
107 static const char *const mips_compression_strings[] =
108 {
109   mips_compression_mips16,
110   mips_compression_micromips,
111   NULL
112 };
113
114 static const char *mips_compression_string = mips_compression_mips16;
115
116 /* The standard register names, and all the valid aliases for them.  */
117 struct register_alias
118 {
119   const char *name;
120   int regnum;
121 };
122
123 /* Aliases for o32 and most other ABIs.  */
124 const struct register_alias mips_o32_aliases[] = {
125   { "ta0", 12 },
126   { "ta1", 13 },
127   { "ta2", 14 },
128   { "ta3", 15 }
129 };
130
131 /* Aliases for n32 and n64.  */
132 const struct register_alias mips_n32_n64_aliases[] = {
133   { "ta0", 8 },
134   { "ta1", 9 },
135   { "ta2", 10 },
136   { "ta3", 11 }
137 };
138
139 /* Aliases for ABI-independent registers.  */
140 const struct register_alias mips_register_aliases[] = {
141   /* The architecture manuals specify these ABI-independent names for
142      the GPRs.  */
143 #define R(n) { "r" #n, n }
144   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
145   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
146   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
147   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
148 #undef R
149
150   /* k0 and k1 are sometimes called these instead (for "kernel
151      temp").  */
152   { "kt0", 26 },
153   { "kt1", 27 },
154
155   /* This is the traditional GDB name for the CP0 status register.  */
156   { "sr", MIPS_PS_REGNUM },
157
158   /* This is the traditional GDB name for the CP0 BadVAddr register.  */
159   { "bad", MIPS_EMBED_BADVADDR_REGNUM },
160
161   /* This is the traditional GDB name for the FCSR.  */
162   { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
163 };
164
165 const struct register_alias mips_numeric_register_aliases[] = {
166 #define R(n) { #n, n }
167   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
168   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
169   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
170   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
171 #undef R
172 };
173
174 #ifndef MIPS_DEFAULT_FPU_TYPE
175 #define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
176 #endif
177 static int mips_fpu_type_auto = 1;
178 static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
179
180 static unsigned int mips_debug = 0;
181
182 /* Properties (for struct target_desc) describing the g/G packet
183    layout.  */
184 #define PROPERTY_GP32 "internal: transfers-32bit-registers"
185 #define PROPERTY_GP64 "internal: transfers-64bit-registers"
186
187 struct target_desc *mips_tdesc_gp32;
188 struct target_desc *mips_tdesc_gp64;
189
190 const struct mips_regnum *
191 mips_regnum (struct gdbarch *gdbarch)
192 {
193   return gdbarch_tdep (gdbarch)->regnum;
194 }
195
196 static int
197 mips_fpa0_regnum (struct gdbarch *gdbarch)
198 {
199   return mips_regnum (gdbarch)->fp0 + 12;
200 }
201
202 /* Return 1 if REGNUM refers to a floating-point general register, raw
203    or cooked.  Otherwise return 0.  */
204
205 static int
206 mips_float_register_p (struct gdbarch *gdbarch, int regnum)
207 {
208   int rawnum = regnum % gdbarch_num_regs (gdbarch);
209
210   return (rawnum >= mips_regnum (gdbarch)->fp0
211           && rawnum < mips_regnum (gdbarch)->fp0 + 32);
212 }
213
214 #define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
215                      == MIPS_ABI_EABI32 \
216                    || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)
217
218 #define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
219   (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)
220
221 #define MIPS_LAST_ARG_REGNUM(gdbarch) \
222   (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)
223
224 #define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)
225
226 /* Return the MIPS ABI associated with GDBARCH.  */
227 enum mips_abi
228 mips_abi (struct gdbarch *gdbarch)
229 {
230   return gdbarch_tdep (gdbarch)->mips_abi;
231 }
232
233 int
234 mips_isa_regsize (struct gdbarch *gdbarch)
235 {
236   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
237
238   /* If we know how big the registers are, use that size.  */
239   if (tdep->register_size_valid_p)
240     return tdep->register_size;
241
242   /* Fall back to the previous behavior.  */
243   return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
244           / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
245 }
246
247 /* Return the currently configured (or set) saved register size.  */
248
249 unsigned int
250 mips_abi_regsize (struct gdbarch *gdbarch)
251 {
252   switch (mips_abi (gdbarch))
253     {
254     case MIPS_ABI_EABI32:
255     case MIPS_ABI_O32:
256       return 4;
257     case MIPS_ABI_N32:
258     case MIPS_ABI_N64:
259     case MIPS_ABI_O64:
260     case MIPS_ABI_EABI64:
261       return 8;
262     case MIPS_ABI_UNKNOWN:
263     case MIPS_ABI_LAST:
264     default:
265       internal_error (__FILE__, __LINE__, _("bad switch"));
266     }
267 }
268
269 /* MIPS16/microMIPS function addresses are odd (bit 0 is set).  Here
270    are some functions to handle addresses associated with compressed
271    code including but not limited to testing, setting, or clearing
272    bit 0 of such addresses.  */
273
274 /* Return one iff compressed code is the MIPS16 instruction set.  */
275
276 static int
277 is_mips16_isa (struct gdbarch *gdbarch)
278 {
279   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MIPS16;
280 }
281
282 /* Return one iff compressed code is the microMIPS instruction set.  */
283
284 static int
285 is_micromips_isa (struct gdbarch *gdbarch)
286 {
287   return gdbarch_tdep (gdbarch)->mips_isa == ISA_MICROMIPS;
288 }
289
290 /* Return one iff ADDR denotes compressed code.  */
291
292 static int
293 is_compact_addr (CORE_ADDR addr)
294 {
295   return ((addr) & 1);
296 }
297
298 /* Return one iff ADDR denotes standard ISA code.  */
299
300 static int
301 is_mips_addr (CORE_ADDR addr)
302 {
303   return !is_compact_addr (addr);
304 }
305
306 /* Return one iff ADDR denotes MIPS16 code.  */
307
308 static int
309 is_mips16_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
310 {
311   return is_compact_addr (addr) && is_mips16_isa (gdbarch);
312 }
313
314 /* Return one iff ADDR denotes microMIPS code.  */
315
316 static int
317 is_micromips_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
318 {
319   return is_compact_addr (addr) && is_micromips_isa (gdbarch);
320 }
321
322 /* Strip the ISA (compression) bit off from ADDR.  */
323
324 static CORE_ADDR
325 unmake_compact_addr (CORE_ADDR addr)
326 {
327   return ((addr) & ~(CORE_ADDR) 1);
328 }
329
330 /* Add the ISA (compression) bit to ADDR.  */
331
332 static CORE_ADDR
333 make_compact_addr (CORE_ADDR addr)
334 {
335   return ((addr) | (CORE_ADDR) 1);
336 }
337
338 /* Functions for setting and testing a bit in a minimal symbol that
339    marks it as MIPS16 or microMIPS function.  The MSB of the minimal
340    symbol's "info" field is used for this purpose.
341
342    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is
343    "special", i.e. refers to a MIPS16 or microMIPS function, and sets
344    one of the "special" bits in a minimal symbol to mark it accordingly.
345    The test checks an ELF-private flag that is valid for true function
346    symbols only; in particular synthetic symbols such as for PLT stubs
347    have no ELF-private part at all.
348
349    msymbol_is_mips16 and msymbol_is_micromips test the "special" bit
350    in a minimal symbol.  */
351
352 static void
353 mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
354 {
355   elf_symbol_type *elfsym = (elf_symbol_type *) sym;
356
357   if ((sym->flags & BSF_SYNTHETIC) != 0)
358     return;
359
360   if (ELF_ST_IS_MICROMIPS (elfsym->internal_elf_sym.st_other))
361     MSYMBOL_TARGET_FLAG_2 (msym) = 1;
362   else if (ELF_ST_IS_MIPS16 (elfsym->internal_elf_sym.st_other))
363     MSYMBOL_TARGET_FLAG_1 (msym) = 1;
364 }
365
366 /* Return one iff MSYM refers to standard ISA code.  */
367
368 static int
369 msymbol_is_mips (struct minimal_symbol *msym)
370 {
371   return !(MSYMBOL_TARGET_FLAG_1 (msym) | MSYMBOL_TARGET_FLAG_2 (msym));
372 }
373
374 /* Return one iff MSYM refers to MIPS16 code.  */
375
376 static int
377 msymbol_is_mips16 (struct minimal_symbol *msym)
378 {
379   return MSYMBOL_TARGET_FLAG_1 (msym);
380 }
381
382 /* Return one iff MSYM refers to microMIPS code.  */
383
384 static int
385 msymbol_is_micromips (struct minimal_symbol *msym)
386 {
387   return MSYMBOL_TARGET_FLAG_2 (msym);
388 }
389
390 /* XFER a value from the big/little/left end of the register.
391    Depending on the size of the value it might occupy the entire
392    register or just part of it.  Make an allowance for this, aligning
393    things accordingly.  */
394
395 static void
396 mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
397                     int reg_num, int length,
398                     enum bfd_endian endian, gdb_byte *in,
399                     const gdb_byte *out, int buf_offset)
400 {
401   int reg_offset = 0;
402
403   gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
404   /* Need to transfer the left or right part of the register, based on
405      the targets byte order.  */
406   switch (endian)
407     {
408     case BFD_ENDIAN_BIG:
409       reg_offset = register_size (gdbarch, reg_num) - length;
410       break;
411     case BFD_ENDIAN_LITTLE:
412       reg_offset = 0;
413       break;
414     case BFD_ENDIAN_UNKNOWN:    /* Indicates no alignment.  */
415       reg_offset = 0;
416       break;
417     default:
418       internal_error (__FILE__, __LINE__, _("bad switch"));
419     }
420   if (mips_debug)
421     fprintf_unfiltered (gdb_stderr,
422                         "xfer $%d, reg offset %d, buf offset %d, length %d, ",
423                         reg_num, reg_offset, buf_offset, length);
424   if (mips_debug && out != NULL)
425     {
426       int i;
427       fprintf_unfiltered (gdb_stdlog, "out ");
428       for (i = 0; i < length; i++)
429         fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
430     }
431   if (in != NULL)
432     regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
433                                in + buf_offset);
434   if (out != NULL)
435     regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
436                                 out + buf_offset);
437   if (mips_debug && in != NULL)
438     {
439       int i;
440       fprintf_unfiltered (gdb_stdlog, "in ");
441       for (i = 0; i < length; i++)
442         fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
443     }
444   if (mips_debug)
445     fprintf_unfiltered (gdb_stdlog, "\n");
446 }
447
448 /* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
449    compatiblity mode.  A return value of 1 means that we have
450    physical 64-bit registers, but should treat them as 32-bit registers.  */
451
452 static int
453 mips2_fp_compat (struct frame_info *frame)
454 {
455   struct gdbarch *gdbarch = get_frame_arch (frame);
456   /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
457      meaningful.  */
458   if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
459     return 0;
460
461 #if 0
462   /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
463      in all the places we deal with FP registers.  PR gdb/413.  */
464   /* Otherwise check the FR bit in the status register - it controls
465      the FP compatiblity mode.  If it is clear we are in compatibility
466      mode.  */
467   if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
468     return 1;
469 #endif
470
471   return 0;
472 }
473
474 #define VM_MIN_ADDRESS (CORE_ADDR)0x400000
475
476 static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);
477
478 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
479
480 /* The list of available "set mips " and "show mips " commands.  */
481
482 static struct cmd_list_element *setmipscmdlist = NULL;
483 static struct cmd_list_element *showmipscmdlist = NULL;
484
485 /* Integer registers 0 thru 31 are handled explicitly by
486    mips_register_name().  Processor specific registers 32 and above
487    are listed in the following tables.  */
488
489 enum
490 { NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
491
492 /* Generic MIPS.  */
493
494 static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
495   "sr", "lo", "hi", "bad", "cause", "pc",
496   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
497   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
498   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
499   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
500   "fsr", "fir",
501 };
502
503 /* Names of IDT R3041 registers.  */
504
505 static const char *mips_r3041_reg_names[] = {
506   "sr", "lo", "hi", "bad", "cause", "pc",
507   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
508   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
509   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
510   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
511   "fsr", "fir", "", /*"fp" */ "",
512   "", "", "bus", "ccfg", "", "", "", "",
513   "", "", "port", "cmp", "", "", "epc", "prid",
514 };
515
516 /* Names of tx39 registers.  */
517
518 static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
519   "sr", "lo", "hi", "bad", "cause", "pc",
520   "", "", "", "", "", "", "", "",
521   "", "", "", "", "", "", "", "",
522   "", "", "", "", "", "", "", "",
523   "", "", "", "", "", "", "", "",
524   "", "", "", "",
525   "", "", "", "", "", "", "", "",
526   "", "", "config", "cache", "debug", "depc", "epc",
527 };
528
529 /* Names of IRIX registers.  */
530 static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
531   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
532   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
533   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
534   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
535   "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
536 };
537
538 /* Names of registers with Linux kernels.  */
539 static const char *mips_linux_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
540   "sr", "lo", "hi", "bad", "cause", "pc",
541   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
542   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
543   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
544   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
545   "fsr", "fir"
546 };
547
548
549 /* Return the name of the register corresponding to REGNO.  */
550 static const char *
551 mips_register_name (struct gdbarch *gdbarch, int regno)
552 {
553   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
554   /* GPR names for all ABIs other than n32/n64.  */
555   static char *mips_gpr_names[] = {
556     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
557     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
558     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
559     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
560   };
561
562   /* GPR names for n32 and n64 ABIs.  */
563   static char *mips_n32_n64_gpr_names[] = {
564     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
565     "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
566     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
567     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
568   };
569
570   enum mips_abi abi = mips_abi (gdbarch);
571
572   /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers, 
573      but then don't make the raw register names visible.  This (upper)
574      range of user visible register numbers are the pseudo-registers.
575
576      This approach was adopted accommodate the following scenario:
577      It is possible to debug a 64-bit device using a 32-bit
578      programming model.  In such instances, the raw registers are
579      configured to be 64-bits wide, while the pseudo registers are
580      configured to be 32-bits wide.  The registers that the user
581      sees - the pseudo registers - match the users expectations
582      given the programming model being used.  */
583   int rawnum = regno % gdbarch_num_regs (gdbarch);
584   if (regno < gdbarch_num_regs (gdbarch))
585     return "";
586
587   /* The MIPS integer registers are always mapped from 0 to 31.  The
588      names of the registers (which reflects the conventions regarding
589      register use) vary depending on the ABI.  */
590   if (0 <= rawnum && rawnum < 32)
591     {
592       if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
593         return mips_n32_n64_gpr_names[rawnum];
594       else
595         return mips_gpr_names[rawnum];
596     }
597   else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
598     return tdesc_register_name (gdbarch, rawnum);
599   else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
600     {
601       gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
602       if (tdep->mips_processor_reg_names[rawnum - 32])
603         return tdep->mips_processor_reg_names[rawnum - 32];
604       return "";
605     }
606   else
607     internal_error (__FILE__, __LINE__,
608                     _("mips_register_name: bad register number %d"), rawnum);
609 }
610
611 /* Return the groups that a MIPS register can be categorised into.  */
612
613 static int
614 mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
615                           struct reggroup *reggroup)
616 {
617   int vector_p;
618   int float_p;
619   int raw_p;
620   int rawnum = regnum % gdbarch_num_regs (gdbarch);
621   int pseudo = regnum / gdbarch_num_regs (gdbarch);
622   if (reggroup == all_reggroup)
623     return pseudo;
624   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
625   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
626   /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
627      (gdbarch), as not all architectures are multi-arch.  */
628   raw_p = rawnum < gdbarch_num_regs (gdbarch);
629   if (gdbarch_register_name (gdbarch, regnum) == NULL
630       || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
631     return 0;
632   if (reggroup == float_reggroup)
633     return float_p && pseudo;
634   if (reggroup == vector_reggroup)
635     return vector_p && pseudo;
636   if (reggroup == general_reggroup)
637     return (!vector_p && !float_p) && pseudo;
638   /* Save the pseudo registers.  Need to make certain that any code
639      extracting register values from a saved register cache also uses
640      pseudo registers.  */
641   if (reggroup == save_reggroup)
642     return raw_p && pseudo;
643   /* Restore the same pseudo register.  */
644   if (reggroup == restore_reggroup)
645     return raw_p && pseudo;
646   return 0;
647 }
648
649 /* Return the groups that a MIPS register can be categorised into.
650    This version is only used if we have a target description which
651    describes real registers (and their groups).  */
652
653 static int
654 mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
655                                 struct reggroup *reggroup)
656 {
657   int rawnum = regnum % gdbarch_num_regs (gdbarch);
658   int pseudo = regnum / gdbarch_num_regs (gdbarch);
659   int ret;
660
661   /* Only save, restore, and display the pseudo registers.  Need to
662      make certain that any code extracting register values from a
663      saved register cache also uses pseudo registers.
664
665      Note: saving and restoring the pseudo registers is slightly
666      strange; if we have 64 bits, we should save and restore all
667      64 bits.  But this is hard and has little benefit.  */
668   if (!pseudo)
669     return 0;
670
671   ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
672   if (ret != -1)
673     return ret;
674
675   return mips_register_reggroup_p (gdbarch, regnum, reggroup);
676 }
677
678 /* Map the symbol table registers which live in the range [1 *
679    gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
680    registers.  Take care of alignment and size problems.  */
681
682 static enum register_status
683 mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
684                            int cookednum, gdb_byte *buf)
685 {
686   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
687   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
688               && cookednum < 2 * gdbarch_num_regs (gdbarch));
689   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
690     return regcache_raw_read (regcache, rawnum, buf);
691   else if (register_size (gdbarch, rawnum) >
692            register_size (gdbarch, cookednum))
693     {
694       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
695         return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
696       else
697         {
698           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
699           LONGEST regval;
700           enum register_status status;
701
702           status = regcache_raw_read_signed (regcache, rawnum, &regval);
703           if (status == REG_VALID)
704             store_signed_integer (buf, 4, byte_order, regval);
705           return status;
706         }
707     }
708   else
709     internal_error (__FILE__, __LINE__, _("bad register size"));
710 }
711
712 static void
713 mips_pseudo_register_write (struct gdbarch *gdbarch,
714                             struct regcache *regcache, int cookednum,
715                             const gdb_byte *buf)
716 {
717   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
718   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
719               && cookednum < 2 * gdbarch_num_regs (gdbarch));
720   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
721     regcache_raw_write (regcache, rawnum, buf);
722   else if (register_size (gdbarch, rawnum) >
723            register_size (gdbarch, cookednum))
724     {
725       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
726         regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
727       else
728         {
729           /* Sign extend the shortened version of the register prior
730              to placing it in the raw register.  This is required for
731              some mips64 parts in order to avoid unpredictable behavior.  */
732           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
733           LONGEST regval = extract_signed_integer (buf, 4, byte_order);
734           regcache_raw_write_signed (regcache, rawnum, regval);
735         }
736     }
737   else
738     internal_error (__FILE__, __LINE__, _("bad register size"));
739 }
740
741 static int
742 mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
743                                  struct agent_expr *ax, int reg)
744 {
745   int rawnum = reg % gdbarch_num_regs (gdbarch);
746   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
747               && reg < 2 * gdbarch_num_regs (gdbarch));
748
749   ax_reg_mask (ax, rawnum);
750
751   return 0;
752 }
753
754 static int
755 mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
756                                     struct agent_expr *ax, int reg)
757 {
758   int rawnum = reg % gdbarch_num_regs (gdbarch);
759   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
760               && reg < 2 * gdbarch_num_regs (gdbarch));
761   if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
762     {
763       ax_reg (ax, rawnum);
764
765       if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
766         {
767           if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
768               || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
769             {
770               ax_const_l (ax, 32);
771               ax_simple (ax, aop_lsh);
772             }
773           ax_const_l (ax, 32);
774           ax_simple (ax, aop_rsh_signed);
775         }
776     }
777   else
778     internal_error (__FILE__, __LINE__, _("bad register size"));
779
780   return 0;
781 }
782
783 /* Table to translate 3-bit register field to actual register number.  */
784 static const signed char mips_reg3_to_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
785
786 /* Heuristic_proc_start may hunt through the text section for a long
787    time across a 2400 baud serial line.  Allows the user to limit this
788    search.  */
789
790 static unsigned int heuristic_fence_post = 0;
791
792 /* Number of bytes of storage in the actual machine representation for
793    register N.  NOTE: This defines the pseudo register type so need to
794    rebuild the architecture vector.  */
795
796 static int mips64_transfers_32bit_regs_p = 0;
797
798 static void
799 set_mips64_transfers_32bit_regs (char *args, int from_tty,
800                                  struct cmd_list_element *c)
801 {
802   struct gdbarch_info info;
803   gdbarch_info_init (&info);
804   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
805      instead of relying on globals.  Doing that would let generic code
806      handle the search for this specific architecture.  */
807   if (!gdbarch_update_p (info))
808     {
809       mips64_transfers_32bit_regs_p = 0;
810       error (_("32-bit compatibility mode not supported"));
811     }
812 }
813
814 /* Convert to/from a register and the corresponding memory value.  */
815
816 /* This predicate tests for the case of an 8 byte floating point
817    value that is being transferred to or from a pair of floating point
818    registers each of which are (or are considered to be) only 4 bytes
819    wide.  */
820 static int
821 mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
822                                     struct type *type)
823 {
824   return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
825           && register_size (gdbarch, regnum) == 4
826           && mips_float_register_p (gdbarch, regnum)
827           && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
828 }
829
830 /* This predicate tests for the case of a value of less than 8
831    bytes in width that is being transfered to or from an 8 byte
832    general purpose register.  */
833 static int
834 mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
835                                     struct type *type)
836 {
837   int num_regs = gdbarch_num_regs (gdbarch);
838
839   return (register_size (gdbarch, regnum) == 8
840           && regnum % num_regs > 0 && regnum % num_regs < 32
841           && TYPE_LENGTH (type) < 8);
842 }
843
844 static int
845 mips_convert_register_p (struct gdbarch *gdbarch,
846                          int regnum, struct type *type)
847 {
848   return (mips_convert_register_float_case_p (gdbarch, regnum, type)
849           || mips_convert_register_gpreg_case_p (gdbarch, regnum, type));
850 }
851
852 static int
853 mips_register_to_value (struct frame_info *frame, int regnum,
854                         struct type *type, gdb_byte *to,
855                         int *optimizedp, int *unavailablep)
856 {
857   struct gdbarch *gdbarch = get_frame_arch (frame);
858
859   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
860     {
861       get_frame_register (frame, regnum + 0, to + 4);
862       get_frame_register (frame, regnum + 1, to + 0);
863
864       if (!get_frame_register_bytes (frame, regnum + 0, 0, 4, to + 4,
865                                      optimizedp, unavailablep))
866         return 0;
867
868       if (!get_frame_register_bytes (frame, regnum + 1, 0, 4, to + 0,
869                                      optimizedp, unavailablep))
870         return 0;
871       *optimizedp = *unavailablep = 0;
872       return 1;
873     }
874   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
875     {
876       int len = TYPE_LENGTH (type);
877       CORE_ADDR offset;
878
879       offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 - len : 0;
880       if (!get_frame_register_bytes (frame, regnum, offset, len, to,
881                                      optimizedp, unavailablep))
882         return 0;
883
884       *optimizedp = *unavailablep = 0;
885       return 1;
886     }
887   else
888     {
889       internal_error (__FILE__, __LINE__,
890                       _("mips_register_to_value: unrecognized case"));
891     }
892 }
893
894 static void
895 mips_value_to_register (struct frame_info *frame, int regnum,
896                         struct type *type, const gdb_byte *from)
897 {
898   struct gdbarch *gdbarch = get_frame_arch (frame);
899
900   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
901     {
902       put_frame_register (frame, regnum + 0, from + 4);
903       put_frame_register (frame, regnum + 1, from + 0);
904     }
905   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
906     {
907       gdb_byte fill[8];
908       int len = TYPE_LENGTH (type);
909       
910       /* Sign extend values, irrespective of type, that are stored to 
911          a 64-bit general purpose register.  (32-bit unsigned values
912          are stored as signed quantities within a 64-bit register.
913          When performing an operation, in compiled code, that combines
914          a 32-bit unsigned value with a signed 64-bit value, a type
915          conversion is first performed that zeroes out the high 32 bits.)  */
916       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
917         {
918           if (from[0] & 0x80)
919             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
920           else
921             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
922           put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
923           put_frame_register_bytes (frame, regnum, 8 - len, len, from);
924         }
925       else
926         {
927           if (from[len-1] & 0x80)
928             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
929           else
930             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
931           put_frame_register_bytes (frame, regnum, 0, len, from);
932           put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
933         }
934     }
935   else
936     {
937       internal_error (__FILE__, __LINE__,
938                       _("mips_value_to_register: unrecognized case"));
939     }
940 }
941
942 /* Return the GDB type object for the "standard" data type of data in
943    register REG.  */
944
945 static struct type *
946 mips_register_type (struct gdbarch *gdbarch, int regnum)
947 {
948   gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
949   if (mips_float_register_p (gdbarch, regnum))
950     {
951       /* The floating-point registers raw, or cooked, always match
952          mips_isa_regsize(), and also map 1:1, byte for byte.  */
953       if (mips_isa_regsize (gdbarch) == 4)
954         return builtin_type (gdbarch)->builtin_float;
955       else
956         return builtin_type (gdbarch)->builtin_double;
957     }
958   else if (regnum < gdbarch_num_regs (gdbarch))
959     {
960       /* The raw or ISA registers.  These are all sized according to
961          the ISA regsize.  */
962       if (mips_isa_regsize (gdbarch) == 4)
963         return builtin_type (gdbarch)->builtin_int32;
964       else
965         return builtin_type (gdbarch)->builtin_int64;
966     }
967   else
968     {
969       int rawnum = regnum - gdbarch_num_regs (gdbarch);
970
971       /* The cooked or ABI registers.  These are sized according to
972          the ABI (with a few complications).  */
973       if (rawnum == mips_regnum (gdbarch)->fp_control_status
974           || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
975         return builtin_type (gdbarch)->builtin_int32;
976       else if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
977                && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
978                && rawnum >= MIPS_FIRST_EMBED_REGNUM
979                && rawnum <= MIPS_LAST_EMBED_REGNUM)
980         /* The pseudo/cooked view of the embedded registers is always
981            32-bit.  The raw view is handled below.  */
982         return builtin_type (gdbarch)->builtin_int32;
983       else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
984         /* The target, while possibly using a 64-bit register buffer,
985            is only transfering 32-bits of each integer register.
986            Reflect this in the cooked/pseudo (ABI) register value.  */
987         return builtin_type (gdbarch)->builtin_int32;
988       else if (mips_abi_regsize (gdbarch) == 4)
989         /* The ABI is restricted to 32-bit registers (the ISA could be
990            32- or 64-bit).  */
991         return builtin_type (gdbarch)->builtin_int32;
992       else
993         /* 64-bit ABI.  */
994         return builtin_type (gdbarch)->builtin_int64;
995     }
996 }
997
998 /* Return the GDB type for the pseudo register REGNUM, which is the
999    ABI-level view.  This function is only called if there is a target
1000    description which includes registers, so we know precisely the
1001    types of hardware registers.  */
1002
1003 static struct type *
1004 mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
1005 {
1006   const int num_regs = gdbarch_num_regs (gdbarch);
1007   int rawnum = regnum % num_regs;
1008   struct type *rawtype;
1009
1010   gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);
1011
1012   /* Absent registers are still absent.  */
1013   rawtype = gdbarch_register_type (gdbarch, rawnum);
1014   if (TYPE_LENGTH (rawtype) == 0)
1015     return rawtype;
1016
1017   if (mips_float_register_p (gdbarch, rawnum))
1018     /* Present the floating point registers however the hardware did;
1019        do not try to convert between FPU layouts.  */
1020     return rawtype;
1021
1022   /* Use pointer types for registers if we can.  For n32 we can not,
1023      since we do not have a 64-bit pointer type.  */
1024   if (mips_abi_regsize (gdbarch)
1025       == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
1026     {
1027       if (rawnum == MIPS_SP_REGNUM
1028           || rawnum == mips_regnum (gdbarch)->badvaddr)
1029         return builtin_type (gdbarch)->builtin_data_ptr;
1030       else if (rawnum == mips_regnum (gdbarch)->pc)
1031         return builtin_type (gdbarch)->builtin_func_ptr;
1032     }
1033
1034   if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
1035       && ((rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_PS_REGNUM)
1036           || rawnum == mips_regnum (gdbarch)->lo
1037           || rawnum == mips_regnum (gdbarch)->hi
1038           || rawnum == mips_regnum (gdbarch)->badvaddr
1039           || rawnum == mips_regnum (gdbarch)->cause
1040           || rawnum == mips_regnum (gdbarch)->pc
1041           || (mips_regnum (gdbarch)->dspacc != -1
1042               && rawnum >= mips_regnum (gdbarch)->dspacc
1043               && rawnum < mips_regnum (gdbarch)->dspacc + 6)))
1044     return builtin_type (gdbarch)->builtin_int32;
1045
1046   if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
1047       && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
1048       && rawnum >= MIPS_EMBED_FP0_REGNUM + 32
1049       && rawnum <= MIPS_LAST_EMBED_REGNUM)
1050     {
1051       /* The pseudo/cooked view of embedded registers is always
1052          32-bit, even if the target transfers 64-bit values for them.
1053          New targets relying on XML descriptions should only transfer
1054          the necessary 32 bits, but older versions of GDB expected 64,
1055          so allow the target to provide 64 bits without interfering
1056          with the displayed type.  */
1057       return builtin_type (gdbarch)->builtin_int32;
1058     }
1059
1060   /* For all other registers, pass through the hardware type.  */
1061   return rawtype;
1062 }
1063
1064 /* Should the upper word of 64-bit addresses be zeroed?  */
1065 enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
1066
1067 static int
1068 mips_mask_address_p (struct gdbarch_tdep *tdep)
1069 {
1070   switch (mask_address_var)
1071     {
1072     case AUTO_BOOLEAN_TRUE:
1073       return 1;
1074     case AUTO_BOOLEAN_FALSE:
1075       return 0;
1076       break;
1077     case AUTO_BOOLEAN_AUTO:
1078       return tdep->default_mask_address_p;
1079     default:
1080       internal_error (__FILE__, __LINE__,
1081                       _("mips_mask_address_p: bad switch"));
1082       return -1;
1083     }
1084 }
1085
1086 static void
1087 show_mask_address (struct ui_file *file, int from_tty,
1088                    struct cmd_list_element *c, const char *value)
1089 {
1090   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
1091
1092   deprecated_show_value_hack (file, from_tty, c, value);
1093   switch (mask_address_var)
1094     {
1095     case AUTO_BOOLEAN_TRUE:
1096       printf_filtered ("The 32 bit mips address mask is enabled\n");
1097       break;
1098     case AUTO_BOOLEAN_FALSE:
1099       printf_filtered ("The 32 bit mips address mask is disabled\n");
1100       break;
1101     case AUTO_BOOLEAN_AUTO:
1102       printf_filtered
1103         ("The 32 bit address mask is set automatically.  Currently %s\n",
1104          mips_mask_address_p (tdep) ? "enabled" : "disabled");
1105       break;
1106     default:
1107       internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
1108       break;
1109     }
1110 }
1111
1112 /* Tell if the program counter value in MEMADDR is in a standard ISA
1113    function.  */
1114
1115 int
1116 mips_pc_is_mips (CORE_ADDR memaddr)
1117 {
1118   struct minimal_symbol *sym;
1119
1120   /* Flags indicating that this is a MIPS16 or microMIPS function is
1121      stored by elfread.c in the high bit of the info field.  Use this
1122      to decide if the function is standard MIPS.  Otherwise if bit 0
1123      of the address is clear, then this is a standard MIPS function.  */
1124   sym = lookup_minimal_symbol_by_pc (memaddr);
1125   if (sym)
1126     return msymbol_is_mips (sym);
1127   else
1128     return is_mips_addr (memaddr);
1129 }
1130
1131 /* Tell if the program counter value in MEMADDR is in a MIPS16 function.  */
1132
1133 int
1134 mips_pc_is_mips16 (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1135 {
1136   struct minimal_symbol *sym;
1137
1138   /* A flag indicating that this is a MIPS16 function is stored by
1139      elfread.c in the high bit of the info field.  Use this to decide
1140      if the function is MIPS16.  Otherwise if bit 0 of the address is
1141      set, then ELF file flags will tell if this is a MIPS16 function.  */
1142   sym = lookup_minimal_symbol_by_pc (memaddr);
1143   if (sym)
1144     return msymbol_is_mips16 (sym);
1145   else
1146     return is_mips16_addr (gdbarch, memaddr);
1147 }
1148
1149 /* Tell if the program counter value in MEMADDR is in a microMIPS function.  */
1150
1151 int
1152 mips_pc_is_micromips (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1153 {
1154   struct minimal_symbol *sym;
1155
1156   /* A flag indicating that this is a microMIPS function is stored by
1157      elfread.c in the high bit of the info field.  Use this to decide
1158      if the function is microMIPS.  Otherwise if bit 0 of the address
1159      is set, then ELF file flags will tell if this is a microMIPS
1160      function.  */
1161   sym = lookup_minimal_symbol_by_pc (memaddr);
1162   if (sym)
1163     return msymbol_is_micromips (sym);
1164   else
1165     return is_micromips_addr (gdbarch, memaddr);
1166 }
1167
1168 /* Tell the ISA type of the function the program counter value in MEMADDR
1169    is in.  */
1170
1171 static enum mips_isa
1172 mips_pc_isa (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1173 {
1174   struct minimal_symbol *sym;
1175
1176   /* A flag indicating that this is a MIPS16 or a microMIPS function
1177      is stored by elfread.c in the high bit of the info field.  Use
1178      this to decide if the function is MIPS16 or microMIPS or normal
1179      MIPS.  Otherwise if bit 0 of the address is set, then ELF file
1180      flags will tell if this is a MIPS16 or a microMIPS function.  */
1181   sym = lookup_minimal_symbol_by_pc (memaddr);
1182   if (sym)
1183     {
1184       if (msymbol_is_micromips (sym))
1185         return ISA_MICROMIPS;
1186       else if (msymbol_is_mips16 (sym))
1187         return ISA_MIPS16;
1188       else
1189         return ISA_MIPS;
1190     }
1191   else
1192     {
1193       if (is_mips_addr (memaddr))
1194         return ISA_MIPS;
1195       else if (is_micromips_addr (gdbarch, memaddr))
1196         return ISA_MICROMIPS;
1197       else
1198         return ISA_MIPS16;
1199     }
1200 }
1201
1202 /* Various MIPS16 thunk (aka stub or trampoline) names.  */
1203
1204 static const char mips_str_mips16_call_stub[] = "__mips16_call_stub_";
1205 static const char mips_str_mips16_ret_stub[] = "__mips16_ret_";
1206 static const char mips_str_call_fp_stub[] = "__call_stub_fp_";
1207 static const char mips_str_call_stub[] = "__call_stub_";
1208 static const char mips_str_fn_stub[] = "__fn_stub_";
1209
1210 /* This is used as a PIC thunk prefix.  */
1211
1212 static const char mips_str_pic[] = ".pic.";
1213
1214 /* Return non-zero if the PC is inside a call thunk (aka stub or
1215    trampoline) that should be treated as a temporary frame.  */
1216
1217 static int
1218 mips_in_frame_stub (CORE_ADDR pc)
1219 {
1220   CORE_ADDR start_addr;
1221   const char *name;
1222
1223   /* Find the starting address of the function containing the PC.  */
1224   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
1225     return 0;
1226
1227   /* If the PC is in __mips16_call_stub_*, this is a call/return stub.  */
1228   if (strncmp (name, mips_str_mips16_call_stub,
1229                strlen (mips_str_mips16_call_stub)) == 0)
1230     return 1;
1231   /* If the PC is in __call_stub_*, this is a call/return or a call stub.  */
1232   if (strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
1233     return 1;
1234   /* If the PC is in __fn_stub_*, this is a call stub.  */
1235   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0)
1236     return 1;
1237
1238   return 0;                     /* Not a stub.  */
1239 }
1240
1241 /* MIPS believes that the PC has a sign extended value.  Perhaps the
1242    all registers should be sign extended for simplicity?  */
1243
1244 static CORE_ADDR
1245 mips_read_pc (struct regcache *regcache)
1246 {
1247   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
1248   ULONGEST pc;
1249
1250   regcache_cooked_read_signed (regcache, regnum, &pc);
1251   if (is_compact_addr (pc))
1252     pc = unmake_compact_addr (pc);
1253   return pc;
1254 }
1255
1256 static CORE_ADDR
1257 mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1258 {
1259   CORE_ADDR pc;
1260
1261   pc = frame_unwind_register_signed (next_frame, gdbarch_pc_regnum (gdbarch));
1262   if (is_compact_addr (pc))
1263     pc = unmake_compact_addr (pc);
1264   /* macro/2012-04-20: This hack skips over MIPS16 call thunks as
1265      intermediate frames.  In this case we can get the caller's address
1266      from $ra, or if $ra contains an address within a thunk as well, then
1267      it must be in the return path of __mips16_call_stub_{s,d}{f,c}_{0..10}
1268      and thus the caller's address is in $s2.  */
1269   if (frame_relative_level (next_frame) >= 0 && mips_in_frame_stub (pc))
1270     {
1271       pc = frame_unwind_register_signed
1272              (next_frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
1273       if (is_compact_addr (pc))
1274         pc = unmake_compact_addr (pc);
1275       if (mips_in_frame_stub (pc))
1276         {
1277           pc = frame_unwind_register_signed
1278                  (next_frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
1279           if (is_compact_addr (pc))
1280             pc = unmake_compact_addr (pc);
1281         }
1282     }
1283   return pc;
1284 }
1285
1286 static CORE_ADDR
1287 mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1288 {
1289   return frame_unwind_register_signed
1290            (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
1291 }
1292
1293 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
1294    dummy frame.  The frame ID's base needs to match the TOS value
1295    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1296    breakpoint.  */
1297
1298 static struct frame_id
1299 mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1300 {
1301   return frame_id_build
1302            (get_frame_register_signed (this_frame,
1303                                        gdbarch_num_regs (gdbarch)
1304                                        + MIPS_SP_REGNUM),
1305             get_frame_pc (this_frame));
1306 }
1307
1308 /* Implement the "write_pc" gdbarch method.  */
1309
1310 void
1311 mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
1312 {
1313   int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
1314
1315   if (mips_pc_is_mips (pc))
1316     regcache_cooked_write_unsigned (regcache, regnum, pc);
1317   else
1318     regcache_cooked_write_unsigned (regcache, regnum, make_compact_addr (pc));
1319 }
1320
1321 /* Fetch and return instruction from the specified location.  Handle
1322    MIPS16/microMIPS as appropriate.  */
1323
1324 static ULONGEST
1325 mips_fetch_instruction (struct gdbarch *gdbarch,
1326                         enum mips_isa isa, CORE_ADDR addr, int *statusp)
1327 {
1328   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1329   gdb_byte buf[MIPS_INSN32_SIZE];
1330   int instlen;
1331   int status;
1332
1333   switch (isa)
1334     {
1335     case ISA_MICROMIPS:
1336     case ISA_MIPS16:
1337       instlen = MIPS_INSN16_SIZE;
1338       addr = unmake_compact_addr (addr);
1339       break;
1340     case ISA_MIPS:
1341       instlen = MIPS_INSN32_SIZE;
1342       break;
1343     default:
1344       internal_error (__FILE__, __LINE__, _("invalid ISA"));
1345       break;
1346     }
1347   status = target_read_memory (addr, buf, instlen);
1348   if (statusp != NULL)
1349     *statusp = status;
1350   if (status)
1351     {
1352       if (statusp == NULL)
1353         memory_error (status, addr);
1354       return 0;
1355     }
1356   return extract_unsigned_integer (buf, instlen, byte_order);
1357 }
1358
1359 /* These are the fields of 32 bit mips instructions.  */
1360 #define mips32_op(x) (x >> 26)
1361 #define itype_op(x) (x >> 26)
1362 #define itype_rs(x) ((x >> 21) & 0x1f)
1363 #define itype_rt(x) ((x >> 16) & 0x1f)
1364 #define itype_immediate(x) (x & 0xffff)
1365
1366 #define jtype_op(x) (x >> 26)
1367 #define jtype_target(x) (x & 0x03ffffff)
1368
1369 #define rtype_op(x) (x >> 26)
1370 #define rtype_rs(x) ((x >> 21) & 0x1f)
1371 #define rtype_rt(x) ((x >> 16) & 0x1f)
1372 #define rtype_rd(x) ((x >> 11) & 0x1f)
1373 #define rtype_shamt(x) ((x >> 6) & 0x1f)
1374 #define rtype_funct(x) (x & 0x3f)
1375
1376 /* MicroMIPS instruction fields.  */
1377 #define micromips_op(x) ((x) >> 10)
1378
1379 /* 16-bit/32-bit-high-part instruction formats, B and S refer to the lowest
1380    bit and the size respectively of the field extracted.  */
1381 #define b0s4_imm(x) ((x) & 0xf)
1382 #define b0s5_imm(x) ((x) & 0x1f)
1383 #define b0s5_reg(x) ((x) & 0x1f)
1384 #define b0s7_imm(x) ((x) & 0x7f)
1385 #define b0s10_imm(x) ((x) & 0x3ff)
1386 #define b1s4_imm(x) (((x) >> 1) & 0xf)
1387 #define b1s9_imm(x) (((x) >> 1) & 0x1ff)
1388 #define b2s3_cc(x) (((x) >> 2) & 0x7)
1389 #define b4s2_regl(x) (((x) >> 4) & 0x3)
1390 #define b5s5_op(x) (((x) >> 5) & 0x1f)
1391 #define b5s5_reg(x) (((x) >> 5) & 0x1f)
1392 #define b6s4_op(x) (((x) >> 6) & 0xf)
1393 #define b7s3_reg(x) (((x) >> 7) & 0x7)
1394
1395 /* 32-bit instruction formats, B and S refer to the lowest bit and the size
1396    respectively of the field extracted.  */
1397 #define b0s6_op(x) ((x) & 0x3f)
1398 #define b0s11_op(x) ((x) & 0x7ff)
1399 #define b0s12_imm(x) ((x) & 0xfff)
1400 #define b0s16_imm(x) ((x) & 0xffff)
1401 #define b0s26_imm(x) ((x) & 0x3ffffff)
1402 #define b6s10_ext(x) (((x) >> 6) & 0x3ff)
1403 #define b11s5_reg(x) (((x) >> 11) & 0x1f)
1404 #define b12s4_op(x) (((x) >> 12) & 0xf)
1405
1406 /* Return the size in bytes of the instruction INSN encoded in the ISA
1407    instruction set.  */
1408
1409 static int
1410 mips_insn_size (enum mips_isa isa, ULONGEST insn)
1411 {
1412   switch (isa)
1413     {
1414     case ISA_MICROMIPS:
1415       if (micromips_op (insn) == 0x1f)
1416         return 3 * MIPS_INSN16_SIZE;
1417       else if (((micromips_op (insn) & 0x4) == 0x4)
1418                || ((micromips_op (insn) & 0x7) == 0x0))
1419         return 2 * MIPS_INSN16_SIZE;
1420       else
1421         return MIPS_INSN16_SIZE;
1422     case ISA_MIPS16:
1423       if ((insn & 0xf800) == 0xf000)
1424         return 2 * MIPS_INSN16_SIZE;
1425       else
1426         return MIPS_INSN16_SIZE;
1427     case ISA_MIPS:
1428         return MIPS_INSN32_SIZE;
1429     }
1430   internal_error (__FILE__, __LINE__, _("invalid ISA"));
1431 }
1432
1433 static LONGEST
1434 mips32_relative_offset (ULONGEST inst)
1435 {
1436   return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
1437 }
1438
1439 /* Determine the address of the next instruction executed after the INST
1440    floating condition branch instruction at PC.  COUNT specifies the
1441    number of the floating condition bits tested by the branch.  */
1442
1443 static CORE_ADDR
1444 mips32_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
1445                ULONGEST inst, CORE_ADDR pc, int count)
1446 {
1447   int fcsr = mips_regnum (gdbarch)->fp_control_status;
1448   int cnum = (itype_rt (inst) >> 2) & (count - 1);
1449   int tf = itype_rt (inst) & 1;
1450   int mask = (1 << count) - 1;
1451   ULONGEST fcs;
1452   int cond;
1453
1454   if (fcsr == -1)
1455     /* No way to handle; it'll most likely trap anyway.  */
1456     return pc;
1457
1458   fcs = get_frame_register_unsigned (frame, fcsr);
1459   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
1460
1461   if (((cond >> cnum) & mask) != mask * !tf)
1462     pc += mips32_relative_offset (inst);
1463   else
1464     pc += 4;
1465
1466   return pc;
1467 }
1468
1469 /* Return nonzero if the gdbarch is an Octeon series.  */
1470
1471 static int
1472 is_octeon (struct gdbarch *gdbarch)
1473 {
1474   const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
1475
1476   return (info->mach == bfd_mach_mips_octeon
1477          || info->mach == bfd_mach_mips_octeonp
1478          || info->mach == bfd_mach_mips_octeon2);
1479 }
1480
1481 /* Return true if the OP represents the Octeon's BBIT instruction.  */
1482
1483 static int
1484 is_octeon_bbit_op (int op, struct gdbarch *gdbarch)
1485 {
1486   if (!is_octeon (gdbarch))
1487     return 0;
1488   /* BBIT0 is encoded as LWC2: 110 010.  */
1489   /* BBIT032 is encoded as LDC2: 110 110.  */
1490   /* BBIT1 is encoded as SWC2: 111 010.  */
1491   /* BBIT132 is encoded as SDC2: 111 110.  */
1492   if (op == 50 || op == 54 || op == 58 || op == 62)
1493     return 1;
1494   return 0;
1495 }
1496
1497
1498 /* Determine where to set a single step breakpoint while considering
1499    branch prediction.  */
1500
1501 static CORE_ADDR
1502 mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
1503 {
1504   struct gdbarch *gdbarch = get_frame_arch (frame);
1505   unsigned long inst;
1506   int op;
1507   inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
1508   op = itype_op (inst);
1509   if ((inst & 0xe0000000) != 0)         /* Not a special, jump or branch
1510                                            instruction.  */
1511     {
1512       if (op >> 2 == 5)
1513         /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
1514         {
1515           switch (op & 0x03)
1516             {
1517             case 0:             /* BEQL */
1518               goto equal_branch;
1519             case 1:             /* BNEL */
1520               goto neq_branch;
1521             case 2:             /* BLEZL */
1522               goto less_branch;
1523             case 3:             /* BGTZL */
1524               goto greater_branch;
1525             default:
1526               pc += 4;
1527             }
1528         }
1529       else if (op == 17 && itype_rs (inst) == 8)
1530         /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
1531         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 1);
1532       else if (op == 17 && itype_rs (inst) == 9
1533                && (itype_rt (inst) & 2) == 0)
1534         /* BC1ANY2F, BC1ANY2T: 010001 01001 xxx0x */
1535         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 2);
1536       else if (op == 17 && itype_rs (inst) == 10
1537                && (itype_rt (inst) & 2) == 0)
1538         /* BC1ANY4F, BC1ANY4T: 010001 01010 xxx0x */
1539         pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 4);
1540       else if (op == 29)
1541         /* JALX: 011101 */
1542         /* The new PC will be alternate mode.  */
1543         {
1544           unsigned long reg;
1545
1546           reg = jtype_target (inst) << 2;
1547           /* Add 1 to indicate 16-bit mode -- invert ISA mode.  */
1548           pc = ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + reg + 1;
1549         }
1550       else if (is_octeon_bbit_op (op, gdbarch))
1551         {
1552           int bit, branch_if;
1553
1554           branch_if = op == 58 || op == 62;
1555           bit = itype_rt (inst);
1556
1557           /* Take into account the *32 instructions.  */
1558           if (op == 54 || op == 62)
1559             bit += 32;
1560
1561           if (((get_frame_register_signed (frame,
1562                                            itype_rs (inst)) >> bit) & 1)
1563               == branch_if)
1564             pc += mips32_relative_offset (inst) + 4;
1565           else
1566             pc += 8;        /* After the delay slot.  */
1567         }
1568
1569       else
1570         pc += 4;                /* Not a branch, next instruction is easy.  */
1571     }
1572   else
1573     {                           /* This gets way messy.  */
1574
1575       /* Further subdivide into SPECIAL, REGIMM and other.  */
1576       switch (op & 0x07)        /* Extract bits 28,27,26.  */
1577         {
1578         case 0:         /* SPECIAL */
1579           op = rtype_funct (inst);
1580           switch (op)
1581             {
1582             case 8:             /* JR */
1583             case 9:             /* JALR */
1584               /* Set PC to that address.  */
1585               pc = get_frame_register_signed (frame, rtype_rs (inst));
1586               break;
1587             case 12:            /* SYSCALL */
1588               {
1589                 struct gdbarch_tdep *tdep;
1590
1591                 tdep = gdbarch_tdep (get_frame_arch (frame));
1592                 if (tdep->syscall_next_pc != NULL)
1593                   pc = tdep->syscall_next_pc (frame);
1594                 else
1595                   pc += 4;
1596               }
1597               break;
1598             default:
1599               pc += 4;
1600             }
1601
1602           break;                /* end SPECIAL */
1603         case 1:                 /* REGIMM */
1604           {
1605             op = itype_rt (inst);       /* branch condition */
1606             switch (op)
1607               {
1608               case 0:           /* BLTZ */
1609               case 2:           /* BLTZL */
1610               case 16:          /* BLTZAL */
1611               case 18:          /* BLTZALL */
1612               less_branch:
1613                 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
1614                   pc += mips32_relative_offset (inst) + 4;
1615                 else
1616                   pc += 8;      /* after the delay slot */
1617                 break;
1618               case 1:           /* BGEZ */
1619               case 3:           /* BGEZL */
1620               case 17:          /* BGEZAL */
1621               case 19:          /* BGEZALL */
1622                 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
1623                   pc += mips32_relative_offset (inst) + 4;
1624                 else
1625                   pc += 8;      /* after the delay slot */
1626                 break;
1627               case 0x1c:        /* BPOSGE32 */
1628               case 0x1e:        /* BPOSGE64 */
1629                 pc += 4;
1630                 if (itype_rs (inst) == 0)
1631                   {
1632                     unsigned int pos = (op & 2) ? 64 : 32;
1633                     int dspctl = mips_regnum (gdbarch)->dspctl;
1634
1635                     if (dspctl == -1)
1636                       /* No way to handle; it'll most likely trap anyway.  */
1637                       break;
1638
1639                     if ((get_frame_register_unsigned (frame,
1640                                                       dspctl) & 0x7f) >= pos)
1641                       pc += mips32_relative_offset (inst);
1642                     else
1643                       pc += 4;
1644                   }
1645                 break;
1646                 /* All of the other instructions in the REGIMM category */
1647               default:
1648                 pc += 4;
1649               }
1650           }
1651           break;                /* end REGIMM */
1652         case 2:         /* J */
1653         case 3:         /* JAL */
1654           {
1655             unsigned long reg;
1656             reg = jtype_target (inst) << 2;
1657             /* Upper four bits get never changed...  */
1658             pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
1659           }
1660           break;
1661         case 4:         /* BEQ, BEQL */
1662         equal_branch:
1663           if (get_frame_register_signed (frame, itype_rs (inst)) ==
1664               get_frame_register_signed (frame, itype_rt (inst)))
1665             pc += mips32_relative_offset (inst) + 4;
1666           else
1667             pc += 8;
1668           break;
1669         case 5:         /* BNE, BNEL */
1670         neq_branch:
1671           if (get_frame_register_signed (frame, itype_rs (inst)) !=
1672               get_frame_register_signed (frame, itype_rt (inst)))
1673             pc += mips32_relative_offset (inst) + 4;
1674           else
1675             pc += 8;
1676           break;
1677         case 6:         /* BLEZ, BLEZL */
1678           if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
1679             pc += mips32_relative_offset (inst) + 4;
1680           else
1681             pc += 8;
1682           break;
1683         case 7:
1684         default:
1685         greater_branch: /* BGTZ, BGTZL */
1686           if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
1687             pc += mips32_relative_offset (inst) + 4;
1688           else
1689             pc += 8;
1690           break;
1691         }                       /* switch */
1692     }                           /* else */
1693   return pc;
1694 }                               /* mips32_next_pc */
1695
1696 /* Extract the 7-bit signed immediate offset from the microMIPS instruction
1697    INSN.  */
1698
1699 static LONGEST
1700 micromips_relative_offset7 (ULONGEST insn)
1701 {
1702   return ((b0s7_imm (insn) ^ 0x40) - 0x40) << 1;
1703 }
1704
1705 /* Extract the 10-bit signed immediate offset from the microMIPS instruction
1706    INSN.  */
1707
1708 static LONGEST
1709 micromips_relative_offset10 (ULONGEST insn)
1710 {
1711   return ((b0s10_imm (insn) ^ 0x200) - 0x200) << 1;
1712 }
1713
1714 /* Extract the 16-bit signed immediate offset from the microMIPS instruction
1715    INSN.  */
1716
1717 static LONGEST
1718 micromips_relative_offset16 (ULONGEST insn)
1719 {
1720   return ((b0s16_imm (insn) ^ 0x8000) - 0x8000) << 1;
1721 }
1722
1723 /* Return the size in bytes of the microMIPS instruction at the address PC.  */
1724
1725 static int
1726 micromips_pc_insn_size (struct gdbarch *gdbarch, CORE_ADDR pc)
1727 {
1728   ULONGEST insn;
1729
1730   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1731   return mips_insn_size (ISA_MICROMIPS, insn);
1732 }
1733
1734 /* Calculate the address of the next microMIPS instruction to execute
1735    after the INSN coprocessor 1 conditional branch instruction at the
1736    address PC.  COUNT denotes the number of coprocessor condition bits
1737    examined by the branch.  */
1738
1739 static CORE_ADDR
1740 micromips_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
1741                   ULONGEST insn, CORE_ADDR pc, int count)
1742 {
1743   int fcsr = mips_regnum (gdbarch)->fp_control_status;
1744   int cnum = b2s3_cc (insn >> 16) & (count - 1);
1745   int tf = b5s5_op (insn >> 16) & 1;
1746   int mask = (1 << count) - 1;
1747   ULONGEST fcs;
1748   int cond;
1749
1750   if (fcsr == -1)
1751     /* No way to handle; it'll most likely trap anyway.  */
1752     return pc;
1753
1754   fcs = get_frame_register_unsigned (frame, fcsr);
1755   cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
1756
1757   if (((cond >> cnum) & mask) != mask * !tf)
1758     pc += micromips_relative_offset16 (insn);
1759   else
1760     pc += micromips_pc_insn_size (gdbarch, pc);
1761
1762   return pc;
1763 }
1764
1765 /* Calculate the address of the next microMIPS instruction to execute
1766    after the instruction at the address PC.  */
1767
1768 static CORE_ADDR
1769 micromips_next_pc (struct frame_info *frame, CORE_ADDR pc)
1770 {
1771   struct gdbarch *gdbarch = get_frame_arch (frame);
1772   ULONGEST insn;
1773
1774   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1775   pc += MIPS_INSN16_SIZE;
1776   switch (mips_insn_size (ISA_MICROMIPS, insn))
1777     {
1778     /* 48-bit instructions.  */
1779     case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
1780       /* No branch or jump instructions in this category.  */
1781       pc += 2 * MIPS_INSN16_SIZE;
1782       break;
1783
1784     /* 32-bit instructions.  */
1785     case 2 * MIPS_INSN16_SIZE:
1786       insn <<= 16;
1787       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1788       pc += MIPS_INSN16_SIZE;
1789       switch (micromips_op (insn >> 16))
1790         {
1791         case 0x00: /* POOL32A: bits 000000 */
1792           if (b0s6_op (insn) == 0x3c
1793                                 /* POOL32Axf: bits 000000 ... 111100 */
1794               && (b6s10_ext (insn) & 0x2bf) == 0x3c)
1795                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
1796                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
1797             pc = get_frame_register_signed (frame, b0s5_reg (insn >> 16));
1798           break;
1799
1800         case 0x10: /* POOL32I: bits 010000 */
1801           switch (b5s5_op (insn >> 16))
1802             {
1803             case 0x00: /* BLTZ: bits 010000 00000 */
1804             case 0x01: /* BLTZAL: bits 010000 00001 */
1805             case 0x11: /* BLTZALS: bits 010000 10001 */
1806               if (get_frame_register_signed (frame,
1807                                              b0s5_reg (insn >> 16)) < 0)
1808                 pc += micromips_relative_offset16 (insn);
1809               else
1810                 pc += micromips_pc_insn_size (gdbarch, pc);
1811               break;
1812
1813             case 0x02: /* BGEZ: bits 010000 00010 */
1814             case 0x03: /* BGEZAL: bits 010000 00011 */
1815             case 0x13: /* BGEZALS: bits 010000 10011 */
1816               if (get_frame_register_signed (frame,
1817                                              b0s5_reg (insn >> 16)) >= 0)
1818                 pc += micromips_relative_offset16 (insn);
1819               else
1820                 pc += micromips_pc_insn_size (gdbarch, pc);
1821               break;
1822
1823             case 0x04: /* BLEZ: bits 010000 00100 */
1824               if (get_frame_register_signed (frame,
1825                                              b0s5_reg (insn >> 16)) <= 0)
1826                 pc += micromips_relative_offset16 (insn);
1827               else
1828                 pc += micromips_pc_insn_size (gdbarch, pc);
1829               break;
1830
1831             case 0x05: /* BNEZC: bits 010000 00101 */
1832               if (get_frame_register_signed (frame,
1833                                              b0s5_reg (insn >> 16)) != 0)
1834                 pc += micromips_relative_offset16 (insn);
1835               break;
1836
1837             case 0x06: /* BGTZ: bits 010000 00110 */
1838               if (get_frame_register_signed (frame,
1839                                              b0s5_reg (insn >> 16)) > 0)
1840                 pc += micromips_relative_offset16 (insn);
1841               else
1842                 pc += micromips_pc_insn_size (gdbarch, pc);
1843               break;
1844
1845             case 0x07: /* BEQZC: bits 010000 00111 */
1846               if (get_frame_register_signed (frame,
1847                                              b0s5_reg (insn >> 16)) == 0)
1848                 pc += micromips_relative_offset16 (insn);
1849               break;
1850
1851             case 0x14: /* BC2F: bits 010000 10100 xxx00 */
1852             case 0x15: /* BC2T: bits 010000 10101 xxx00 */
1853               if (((insn >> 16) & 0x3) == 0x0)
1854                 /* BC2F, BC2T: don't know how to handle these.  */
1855                 break;
1856               break;
1857
1858             case 0x1a: /* BPOSGE64: bits 010000 11010 */
1859             case 0x1b: /* BPOSGE32: bits 010000 11011 */
1860               {
1861                 unsigned int pos = (b5s5_op (insn >> 16) & 1) ? 32 : 64;
1862                 int dspctl = mips_regnum (gdbarch)->dspctl;
1863
1864                 if (dspctl == -1)
1865                   /* No way to handle; it'll most likely trap anyway.  */
1866                   break;
1867
1868                 if ((get_frame_register_unsigned (frame,
1869                                                   dspctl) & 0x7f) >= pos)
1870                   pc += micromips_relative_offset16 (insn);
1871                 else
1872                   pc += micromips_pc_insn_size (gdbarch, pc);
1873               }
1874               break;
1875
1876             case 0x1c: /* BC1F: bits 010000 11100 xxx00 */
1877                        /* BC1ANY2F: bits 010000 11100 xxx01 */
1878             case 0x1d: /* BC1T: bits 010000 11101 xxx00 */
1879                        /* BC1ANY2T: bits 010000 11101 xxx01 */
1880               if (((insn >> 16) & 0x2) == 0x0)
1881                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc,
1882                                        ((insn >> 16) & 0x1) + 1);
1883               break;
1884
1885             case 0x1e: /* BC1ANY4F: bits 010000 11110 xxx01 */
1886             case 0x1f: /* BC1ANY4T: bits 010000 11111 xxx01 */
1887               if (((insn >> 16) & 0x3) == 0x1)
1888                 pc = micromips_bc1_pc (gdbarch, frame, insn, pc, 4);
1889               break;
1890             }
1891           break;
1892
1893         case 0x1d: /* JALS: bits 011101 */
1894         case 0x35: /* J: bits 110101 */
1895         case 0x3d: /* JAL: bits 111101 */
1896             pc = ((pc | 0x7fffffe) ^ 0x7fffffe) | (b0s26_imm (insn) << 1);
1897           break;
1898
1899         case 0x25: /* BEQ: bits 100101 */
1900             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
1901                 == get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
1902               pc += micromips_relative_offset16 (insn);
1903             else
1904               pc += micromips_pc_insn_size (gdbarch, pc);
1905           break;
1906
1907         case 0x2d: /* BNE: bits 101101 */
1908             if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
1909                 != get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
1910               pc += micromips_relative_offset16 (insn);
1911             else
1912               pc += micromips_pc_insn_size (gdbarch, pc);
1913           break;
1914
1915         case 0x3c: /* JALX: bits 111100 */
1916             pc = ((pc | 0xfffffff) ^ 0xfffffff) | (b0s26_imm (insn) << 2);
1917           break;
1918         }
1919       break;
1920
1921     /* 16-bit instructions.  */
1922     case MIPS_INSN16_SIZE:
1923       switch (micromips_op (insn))
1924         {
1925         case 0x11: /* POOL16C: bits 010001 */
1926           if ((b5s5_op (insn) & 0x1c) == 0xc)
1927             /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
1928             pc = get_frame_register_signed (frame, b0s5_reg (insn));
1929           else if (b5s5_op (insn) == 0x18)
1930             /* JRADDIUSP: bits 010001 11000 */
1931             pc = get_frame_register_signed (frame, MIPS_RA_REGNUM);
1932           break;
1933
1934         case 0x23: /* BEQZ16: bits 100011 */
1935           {
1936             int rs = mips_reg3_to_reg[b7s3_reg (insn)];
1937
1938             if (get_frame_register_signed (frame, rs) == 0)
1939               pc += micromips_relative_offset7 (insn);
1940             else
1941               pc += micromips_pc_insn_size (gdbarch, pc);
1942           }
1943           break;
1944
1945         case 0x2b: /* BNEZ16: bits 101011 */
1946           {
1947             int rs = mips_reg3_to_reg[b7s3_reg (insn)];
1948
1949             if (get_frame_register_signed (frame, rs) != 0)
1950               pc += micromips_relative_offset7 (insn);
1951             else
1952               pc += micromips_pc_insn_size (gdbarch, pc);
1953           }
1954           break;
1955
1956         case 0x33: /* B16: bits 110011 */
1957           pc += micromips_relative_offset10 (insn);
1958           break;
1959         }
1960       break;
1961     }
1962
1963   return pc;
1964 }
1965
1966 /* Decoding the next place to set a breakpoint is irregular for the
1967    mips 16 variant, but fortunately, there fewer instructions.  We have
1968    to cope ith extensions for 16 bit instructions and a pair of actual
1969    32 bit instructions.  We dont want to set a single step instruction
1970    on the extend instruction either.  */
1971
1972 /* Lots of mips16 instruction formats */
1973 /* Predicting jumps requires itype,ritype,i8type
1974    and their extensions      extItype,extritype,extI8type.  */
1975 enum mips16_inst_fmts
1976 {
1977   itype,                        /* 0  immediate 5,10 */
1978   ritype,                       /* 1   5,3,8 */
1979   rrtype,                       /* 2   5,3,3,5 */
1980   rritype,                      /* 3   5,3,3,5 */
1981   rrrtype,                      /* 4   5,3,3,3,2 */
1982   rriatype,                     /* 5   5,3,3,1,4 */
1983   shifttype,                    /* 6   5,3,3,3,2 */
1984   i8type,                       /* 7   5,3,8 */
1985   i8movtype,                    /* 8   5,3,3,5 */
1986   i8mov32rtype,                 /* 9   5,3,5,3 */
1987   i64type,                      /* 10  5,3,8 */
1988   ri64type,                     /* 11  5,3,3,5 */
1989   jalxtype,                     /* 12  5,1,5,5,16 - a 32 bit instruction */
1990   exiItype,                     /* 13  5,6,5,5,1,1,1,1,1,1,5 */
1991   extRitype,                    /* 14  5,6,5,5,3,1,1,1,5 */
1992   extRRItype,                   /* 15  5,5,5,5,3,3,5 */
1993   extRRIAtype,                  /* 16  5,7,4,5,3,3,1,4 */
1994   EXTshifttype,                 /* 17  5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
1995   extI8type,                    /* 18  5,6,5,5,3,1,1,1,5 */
1996   extI64type,                   /* 19  5,6,5,5,3,1,1,1,5 */
1997   extRi64type,                  /* 20  5,6,5,5,3,3,5 */
1998   extshift64type                /* 21  5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
1999 };
2000 /* I am heaping all the fields of the formats into one structure and
2001    then, only the fields which are involved in instruction extension.  */
2002 struct upk_mips16
2003 {
2004   CORE_ADDR offset;
2005   unsigned int regx;            /* Function in i8 type.  */
2006   unsigned int regy;
2007 };
2008
2009
2010 /* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
2011    for the bits which make up the immediate extension.  */
2012
2013 static CORE_ADDR
2014 extended_offset (unsigned int extension)
2015 {
2016   CORE_ADDR value;
2017
2018   value = (extension >> 16) & 0x1f;     /* Extract 15:11.  */
2019   value = value << 6;
2020   value |= (extension >> 21) & 0x3f;    /* Extract 10:5.  */
2021   value = value << 5;
2022   value |= extension & 0x1f;            /* Extract 4:0.  */
2023
2024   return value;
2025 }
2026
2027 /* Only call this function if you know that this is an extendable
2028    instruction.  It won't malfunction, but why make excess remote memory
2029    references?  If the immediate operands get sign extended or something,
2030    do it after the extension is performed.  */
2031 /* FIXME: Every one of these cases needs to worry about sign extension
2032    when the offset is to be used in relative addressing.  */
2033
2034 static unsigned int
2035 fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
2036 {
2037   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2038   gdb_byte buf[8];
2039   pc &= 0xfffffffe;             /* Clear the low order bit.  */
2040   target_read_memory (pc, buf, 2);
2041   return extract_unsigned_integer (buf, 2, byte_order);
2042 }
2043
2044 static void
2045 unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
2046                unsigned int extension,
2047                unsigned int inst,
2048                enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
2049 {
2050   CORE_ADDR offset;
2051   int regx;
2052   int regy;
2053   switch (insn_format)
2054     {
2055     case itype:
2056       {
2057         CORE_ADDR value;
2058         if (extension)
2059           {
2060             value = extended_offset ((extension << 16) | inst);
2061             value = (value ^ 0x8000) - 0x8000;          /* Sign-extend.  */
2062           }
2063         else
2064           {
2065             value = inst & 0x7ff;
2066             value = (value ^ 0x400) - 0x400;            /* Sign-extend.  */
2067           }
2068         offset = value;
2069         regx = -1;
2070         regy = -1;
2071       }
2072       break;
2073     case ritype:
2074     case i8type:
2075       {                         /* A register identifier and an offset.  */
2076         /* Most of the fields are the same as I type but the
2077            immediate value is of a different length.  */
2078         CORE_ADDR value;
2079         if (extension)
2080           {
2081             value = extended_offset ((extension << 16) | inst);
2082             value = (value ^ 0x8000) - 0x8000;          /* Sign-extend.  */
2083           }
2084         else
2085           {
2086             value = inst & 0xff;                        /* 8 bits */
2087             value = (value ^ 0x80) - 0x80;              /* Sign-extend.  */
2088           }
2089         offset = value;
2090         regx = (inst >> 8) & 0x07;                      /* i8 funct */
2091         regy = -1;
2092         break;
2093       }
2094     case jalxtype:
2095       {
2096         unsigned long value;
2097         unsigned int nexthalf;
2098         value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
2099         value = value << 16;
2100         nexthalf = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc + 2, NULL);
2101                                                 /* Low bit still set.  */
2102         value |= nexthalf;
2103         offset = value;
2104         regx = -1;
2105         regy = -1;
2106         break;
2107       }
2108     default:
2109       internal_error (__FILE__, __LINE__, _("bad switch"));
2110     }
2111   upk->offset = offset;
2112   upk->regx = regx;
2113   upk->regy = regy;
2114 }
2115
2116
2117 static CORE_ADDR
2118 add_offset_16 (CORE_ADDR pc, int offset)
2119 {
2120   return ((offset << 2) | ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)));
2121 }
2122
2123 static CORE_ADDR
2124 extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
2125                          unsigned int extension, unsigned int insn)
2126 {
2127   struct gdbarch *gdbarch = get_frame_arch (frame);
2128   int op = (insn >> 11);
2129   switch (op)
2130     {
2131     case 2:                     /* Branch */
2132       {
2133         struct upk_mips16 upk;
2134         unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
2135         pc += (upk.offset << 1) + 2;
2136         break;
2137       }
2138     case 3:                     /* JAL , JALX - Watch out, these are 32 bit
2139                                    instructions.  */
2140       {
2141         struct upk_mips16 upk;
2142         unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
2143         pc = add_offset_16 (pc, upk.offset);
2144         if ((insn >> 10) & 0x01)        /* Exchange mode */
2145           pc = pc & ~0x01;      /* Clear low bit, indicate 32 bit mode.  */
2146         else
2147           pc |= 0x01;
2148         break;
2149       }
2150     case 4:                     /* beqz */
2151       {
2152         struct upk_mips16 upk;
2153         int reg;
2154         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
2155         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
2156         if (reg == 0)
2157           pc += (upk.offset << 1) + 2;
2158         else
2159           pc += 2;
2160         break;
2161       }
2162     case 5:                     /* bnez */
2163       {
2164         struct upk_mips16 upk;
2165         int reg;
2166         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
2167         reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
2168         if (reg != 0)
2169           pc += (upk.offset << 1) + 2;
2170         else
2171           pc += 2;
2172         break;
2173       }
2174     case 12:                    /* I8 Formats btez btnez */
2175       {
2176         struct upk_mips16 upk;
2177         int reg;
2178         unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
2179         /* upk.regx contains the opcode */
2180         reg = get_frame_register_signed (frame, 24);  /* Test register is 24 */
2181         if (((upk.regx == 0) && (reg == 0))     /* BTEZ */
2182             || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
2183           /* pc = add_offset_16(pc,upk.offset) ; */
2184           pc += (upk.offset << 1) + 2;
2185         else
2186           pc += 2;
2187         break;
2188       }
2189     case 29:                    /* RR Formats JR, JALR, JALR-RA */
2190       {
2191         struct upk_mips16 upk;
2192         /* upk.fmt = rrtype; */
2193         op = insn & 0x1f;
2194         if (op == 0)
2195           {
2196             int reg;
2197             upk.regx = (insn >> 8) & 0x07;
2198             upk.regy = (insn >> 5) & 0x07;
2199             if ((upk.regy & 1) == 0)
2200               reg = mips_reg3_to_reg[upk.regx];
2201             else
2202               reg = 31;         /* Function return instruction.  */
2203             pc = get_frame_register_signed (frame, reg);
2204           }
2205         else
2206           pc += 2;
2207         break;
2208       }
2209     case 30:
2210       /* This is an instruction extension.  Fetch the real instruction
2211          (which follows the extension) and decode things based on
2212          that.  */
2213       {
2214         pc += 2;
2215         pc = extended_mips16_next_pc (frame, pc, insn,
2216                                       fetch_mips_16 (gdbarch, pc));
2217         break;
2218       }
2219     default:
2220       {
2221         pc += 2;
2222         break;
2223       }
2224     }
2225   return pc;
2226 }
2227
2228 static CORE_ADDR
2229 mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
2230 {
2231   struct gdbarch *gdbarch = get_frame_arch (frame);
2232   unsigned int insn = fetch_mips_16 (gdbarch, pc);
2233   return extended_mips16_next_pc (frame, pc, 0, insn);
2234 }
2235
2236 /* The mips_next_pc function supports single_step when the remote
2237    target monitor or stub is not developed enough to do a single_step.
2238    It works by decoding the current instruction and predicting where a
2239    branch will go.  This isnt hard because all the data is available.
2240    The MIPS32, MIPS16 and microMIPS variants are quite different.  */
2241 static CORE_ADDR
2242 mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
2243 {
2244   struct gdbarch *gdbarch = get_frame_arch (frame);
2245
2246   if (mips_pc_is_mips16 (gdbarch, pc))
2247     return mips16_next_pc (frame, pc);
2248   else if (mips_pc_is_micromips (gdbarch, pc))
2249     return micromips_next_pc (frame, pc);
2250   else
2251     return mips32_next_pc (frame, pc);
2252 }
2253
2254 struct mips_frame_cache
2255 {
2256   CORE_ADDR base;
2257   struct trad_frame_saved_reg *saved_regs;
2258 };
2259
2260 /* Set a register's saved stack address in temp_saved_regs.  If an
2261    address has already been set for this register, do nothing; this
2262    way we will only recognize the first save of a given register in a
2263    function prologue.
2264
2265    For simplicity, save the address in both [0 .. gdbarch_num_regs) and
2266    [gdbarch_num_regs .. 2*gdbarch_num_regs).
2267    Strictly speaking, only the second range is used as it is only second
2268    range (the ABI instead of ISA registers) that comes into play when finding
2269    saved registers in a frame.  */
2270
2271 static void
2272 set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
2273                 int regnum, CORE_ADDR offset)
2274 {
2275   if (this_cache != NULL
2276       && this_cache->saved_regs[regnum].addr == -1)
2277     {
2278       this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
2279         = offset;
2280       this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
2281         = offset;
2282     }
2283 }
2284
2285
2286 /* Fetch the immediate value from a MIPS16 instruction.
2287    If the previous instruction was an EXTEND, use it to extend
2288    the upper bits of the immediate value.  This is a helper function
2289    for mips16_scan_prologue.  */
2290
2291 static int
2292 mips16_get_imm (unsigned short prev_inst,       /* previous instruction */
2293                 unsigned short inst,    /* current instruction */
2294                 int nbits,      /* number of bits in imm field */
2295                 int scale,      /* scale factor to be applied to imm */
2296                 int is_signed)  /* is the imm field signed?  */
2297 {
2298   int offset;
2299
2300   if ((prev_inst & 0xf800) == 0xf000)   /* prev instruction was EXTEND? */
2301     {
2302       offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
2303       if (offset & 0x8000)      /* check for negative extend */
2304         offset = 0 - (0x10000 - (offset & 0xffff));
2305       return offset | (inst & 0x1f);
2306     }
2307   else
2308     {
2309       int max_imm = 1 << nbits;
2310       int mask = max_imm - 1;
2311       int sign_bit = max_imm >> 1;
2312
2313       offset = inst & mask;
2314       if (is_signed && (offset & sign_bit))
2315         offset = 0 - (max_imm - offset);
2316       return offset * scale;
2317     }
2318 }
2319
2320
2321 /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
2322    the associated FRAME_CACHE if not null.
2323    Return the address of the first instruction past the prologue.  */
2324
2325 static CORE_ADDR
2326 mips16_scan_prologue (struct gdbarch *gdbarch,
2327                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
2328                       struct frame_info *this_frame,
2329                       struct mips_frame_cache *this_cache)
2330 {
2331   CORE_ADDR cur_pc;
2332   CORE_ADDR frame_addr = 0;     /* Value of $r17, used as frame pointer.  */
2333   CORE_ADDR sp;
2334   long frame_offset = 0;        /* Size of stack frame.  */
2335   long frame_adjust = 0;        /* Offset of FP from SP.  */
2336   int frame_reg = MIPS_SP_REGNUM;
2337   unsigned short prev_inst = 0; /* saved copy of previous instruction.  */
2338   unsigned inst = 0;            /* current instruction */
2339   unsigned entry_inst = 0;      /* the entry instruction */
2340   unsigned save_inst = 0;       /* the save instruction */
2341   int reg, offset;
2342
2343   int extend_bytes = 0;
2344   int prev_extend_bytes;
2345   CORE_ADDR end_prologue_addr = 0;
2346
2347   /* Can be called when there's no process, and hence when there's no
2348      THIS_FRAME.  */
2349   if (this_frame != NULL)
2350     sp = get_frame_register_signed (this_frame,
2351                                     gdbarch_num_regs (gdbarch)
2352                                     + MIPS_SP_REGNUM);
2353   else
2354     sp = 0;
2355
2356   if (limit_pc > start_pc + 200)
2357     limit_pc = start_pc + 200;
2358
2359   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
2360     {
2361       /* Save the previous instruction.  If it's an EXTEND, we'll extract
2362          the immediate offset extension from it in mips16_get_imm.  */
2363       prev_inst = inst;
2364
2365       /* Fetch and decode the instruction.  */
2366       inst = (unsigned short) mips_fetch_instruction (gdbarch, ISA_MIPS16,
2367                                                       cur_pc, NULL);
2368
2369       /* Normally we ignore extend instructions.  However, if it is
2370          not followed by a valid prologue instruction, then this
2371          instruction is not part of the prologue either.  We must
2372          remember in this case to adjust the end_prologue_addr back
2373          over the extend.  */
2374       if ((inst & 0xf800) == 0xf000)    /* extend */
2375         {
2376           extend_bytes = MIPS_INSN16_SIZE;
2377           continue;
2378         }
2379
2380       prev_extend_bytes = extend_bytes;
2381       extend_bytes = 0;
2382
2383       if ((inst & 0xff00) == 0x6300     /* addiu sp */
2384           || (inst & 0xff00) == 0xfb00) /* daddiu sp */
2385         {
2386           offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
2387           if (offset < 0)       /* Negative stack adjustment?  */
2388             frame_offset -= offset;
2389           else
2390             /* Exit loop if a positive stack adjustment is found, which
2391                usually means that the stack cleanup code in the function
2392                epilogue is reached.  */
2393             break;
2394         }
2395       else if ((inst & 0xf800) == 0xd000)       /* sw reg,n($sp) */
2396         {
2397           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2398           reg = mips_reg3_to_reg[(inst & 0x700) >> 8];
2399           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2400         }
2401       else if ((inst & 0xff00) == 0xf900)       /* sd reg,n($sp) */
2402         {
2403           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
2404           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2405           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2406         }
2407       else if ((inst & 0xff00) == 0x6200)       /* sw $ra,n($sp) */
2408         {
2409           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2410           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2411         }
2412       else if ((inst & 0xff00) == 0xfa00)       /* sd $ra,n($sp) */
2413         {
2414           offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
2415           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2416         }
2417       else if (inst == 0x673d)  /* move $s1, $sp */
2418         {
2419           frame_addr = sp;
2420           frame_reg = 17;
2421         }
2422       else if ((inst & 0xff00) == 0x0100)       /* addiu $s1,sp,n */
2423         {
2424           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2425           frame_addr = sp + offset;
2426           frame_reg = 17;
2427           frame_adjust = offset;
2428         }
2429       else if ((inst & 0xFF00) == 0xd900)       /* sw reg,offset($s1) */
2430         {
2431           offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
2432           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2433           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
2434         }
2435       else if ((inst & 0xFF00) == 0x7900)       /* sd reg,offset($s1) */
2436         {
2437           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
2438           reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2439           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
2440         }
2441       else if ((inst & 0xf81f) == 0xe809
2442                && (inst & 0x700) != 0x700)      /* entry */
2443         entry_inst = inst;      /* Save for later processing.  */
2444       else if ((inst & 0xff80) == 0x6480)       /* save */
2445         {
2446           save_inst = inst;     /* Save for later processing.  */
2447           if (prev_extend_bytes)                /* extend */
2448             save_inst |= prev_inst << 16;
2449         }
2450       else if ((inst & 0xf800) == 0x1800)       /* jal(x) */
2451         cur_pc += MIPS_INSN16_SIZE;     /* 32-bit instruction */
2452       else if ((inst & 0xff1c) == 0x6704)       /* move reg,$a0-$a3 */
2453         {
2454           /* This instruction is part of the prologue, but we don't
2455              need to do anything special to handle it.  */
2456         }
2457       else
2458         {
2459           /* This instruction is not an instruction typically found
2460              in a prologue, so we must have reached the end of the
2461              prologue.  */
2462           if (end_prologue_addr == 0)
2463             end_prologue_addr = cur_pc - prev_extend_bytes;
2464         }
2465     }
2466
2467   /* The entry instruction is typically the first instruction in a function,
2468      and it stores registers at offsets relative to the value of the old SP
2469      (before the prologue).  But the value of the sp parameter to this
2470      function is the new SP (after the prologue has been executed).  So we
2471      can't calculate those offsets until we've seen the entire prologue,
2472      and can calculate what the old SP must have been.  */
2473   if (entry_inst != 0)
2474     {
2475       int areg_count = (entry_inst >> 8) & 7;
2476       int sreg_count = (entry_inst >> 6) & 3;
2477
2478       /* The entry instruction always subtracts 32 from the SP.  */
2479       frame_offset += 32;
2480
2481       /* Now we can calculate what the SP must have been at the
2482          start of the function prologue.  */
2483       sp += frame_offset;
2484
2485       /* Check if a0-a3 were saved in the caller's argument save area.  */
2486       for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
2487         {
2488           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2489           offset += mips_abi_regsize (gdbarch);
2490         }
2491
2492       /* Check if the ra register was pushed on the stack.  */
2493       offset = -4;
2494       if (entry_inst & 0x20)
2495         {
2496           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2497           offset -= mips_abi_regsize (gdbarch);
2498         }
2499
2500       /* Check if the s0 and s1 registers were pushed on the stack.  */
2501       for (reg = 16; reg < sreg_count + 16; reg++)
2502         {
2503           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2504           offset -= mips_abi_regsize (gdbarch);
2505         }
2506     }
2507
2508   /* The SAVE instruction is similar to ENTRY, except that defined by the
2509      MIPS16e ASE of the MIPS Architecture.  Unlike with ENTRY though, the
2510      size of the frame is specified as an immediate field of instruction
2511      and an extended variation exists which lets additional registers and
2512      frame space to be specified.  The instruction always treats registers
2513      as 32-bit so its usefulness for 64-bit ABIs is questionable.  */
2514   if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
2515     {
2516       static int args_table[16] = {
2517         0, 0, 0, 0, 1, 1, 1, 1,
2518         2, 2, 2, 0, 3, 3, 4, -1,
2519       };
2520       static int astatic_table[16] = {
2521         0, 1, 2, 3, 0, 1, 2, 3,
2522         0, 1, 2, 4, 0, 1, 0, -1,
2523       };
2524       int aregs = (save_inst >> 16) & 0xf;
2525       int xsregs = (save_inst >> 24) & 0x7;
2526       int args = args_table[aregs];
2527       int astatic = astatic_table[aregs];
2528       long frame_size;
2529
2530       if (args < 0)
2531         {
2532           warning (_("Invalid number of argument registers encoded in SAVE."));
2533           args = 0;
2534         }
2535       if (astatic < 0)
2536         {
2537           warning (_("Invalid number of static registers encoded in SAVE."));
2538           astatic = 0;
2539         }
2540
2541       /* For standard SAVE the frame size of 0 means 128.  */
2542       frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
2543       if (frame_size == 0 && (save_inst >> 16) == 0)
2544         frame_size = 16;
2545       frame_size *= 8;
2546       frame_offset += frame_size;
2547
2548       /* Now we can calculate what the SP must have been at the
2549          start of the function prologue.  */
2550       sp += frame_offset;
2551
2552       /* Check if A0-A3 were saved in the caller's argument save area.  */
2553       for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
2554         {
2555           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2556           offset += mips_abi_regsize (gdbarch);
2557         }
2558
2559       offset = -4;
2560
2561       /* Check if the RA register was pushed on the stack.  */
2562       if (save_inst & 0x40)
2563         {
2564           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2565           offset -= mips_abi_regsize (gdbarch);
2566         }
2567
2568       /* Check if the S8 register was pushed on the stack.  */
2569       if (xsregs > 6)
2570         {
2571           set_reg_offset (gdbarch, this_cache, 30, sp + offset);
2572           offset -= mips_abi_regsize (gdbarch);
2573           xsregs--;
2574         }
2575       /* Check if S2-S7 were pushed on the stack.  */
2576       for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
2577         {
2578           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2579           offset -= mips_abi_regsize (gdbarch);
2580         }
2581
2582       /* Check if the S1 register was pushed on the stack.  */
2583       if (save_inst & 0x10)
2584         {
2585           set_reg_offset (gdbarch, this_cache, 17, sp + offset);
2586           offset -= mips_abi_regsize (gdbarch);
2587         }
2588       /* Check if the S0 register was pushed on the stack.  */
2589       if (save_inst & 0x20)
2590         {
2591           set_reg_offset (gdbarch, this_cache, 16, sp + offset);
2592           offset -= mips_abi_regsize (gdbarch);
2593         }
2594
2595       /* Check if A0-A3 were pushed on the stack.  */
2596       for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
2597         {
2598           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2599           offset -= mips_abi_regsize (gdbarch);
2600         }
2601     }
2602
2603   if (this_cache != NULL)
2604     {
2605       this_cache->base =
2606         (get_frame_register_signed (this_frame,
2607                                     gdbarch_num_regs (gdbarch) + frame_reg)
2608          + frame_offset - frame_adjust);
2609       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
2610          be able to get rid of the assignment below, evetually.  But it's
2611          still needed for now.  */
2612       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2613                              + mips_regnum (gdbarch)->pc]
2614         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
2615     }
2616
2617   /* If we didn't reach the end of the prologue when scanning the function
2618      instructions, then set end_prologue_addr to the address of the
2619      instruction immediately after the last one we scanned.  */
2620   if (end_prologue_addr == 0)
2621     end_prologue_addr = cur_pc;
2622
2623   return end_prologue_addr;
2624 }
2625
2626 /* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
2627    Procedures that use the 32-bit instruction set are handled by the
2628    mips_insn32 unwinder.  */
2629
2630 static struct mips_frame_cache *
2631 mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
2632 {
2633   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2634   struct mips_frame_cache *cache;
2635
2636   if ((*this_cache) != NULL)
2637     return (*this_cache);
2638   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
2639   (*this_cache) = cache;
2640   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2641
2642   /* Analyze the function prologue.  */
2643   {
2644     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
2645     CORE_ADDR start_addr;
2646
2647     find_pc_partial_function (pc, NULL, &start_addr, NULL);
2648     if (start_addr == 0)
2649       start_addr = heuristic_proc_start (gdbarch, pc);
2650     /* We can't analyze the prologue if we couldn't find the begining
2651        of the function.  */
2652     if (start_addr == 0)
2653       return cache;
2654
2655     mips16_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
2656   }
2657   
2658   /* gdbarch_sp_regnum contains the value and not the address.  */
2659   trad_frame_set_value (cache->saved_regs,
2660                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
2661                         cache->base);
2662
2663   return (*this_cache);
2664 }
2665
2666 static void
2667 mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
2668                            struct frame_id *this_id)
2669 {
2670   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2671                                                            this_cache);
2672   /* This marks the outermost frame.  */
2673   if (info->base == 0)
2674     return;
2675   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
2676 }
2677
2678 static struct value *
2679 mips_insn16_frame_prev_register (struct frame_info *this_frame,
2680                                  void **this_cache, int regnum)
2681 {
2682   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2683                                                            this_cache);
2684   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2685 }
2686
2687 static int
2688 mips_insn16_frame_sniffer (const struct frame_unwind *self,
2689                            struct frame_info *this_frame, void **this_cache)
2690 {
2691   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2692   CORE_ADDR pc = get_frame_pc (this_frame);
2693   if (mips_pc_is_mips16 (gdbarch, pc))
2694     return 1;
2695   return 0;
2696 }
2697
2698 static const struct frame_unwind mips_insn16_frame_unwind =
2699 {
2700   NORMAL_FRAME,
2701   default_frame_unwind_stop_reason,
2702   mips_insn16_frame_this_id,
2703   mips_insn16_frame_prev_register,
2704   NULL,
2705   mips_insn16_frame_sniffer
2706 };
2707
2708 static CORE_ADDR
2709 mips_insn16_frame_base_address (struct frame_info *this_frame,
2710                                 void **this_cache)
2711 {
2712   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2713                                                            this_cache);
2714   return info->base;
2715 }
2716
2717 static const struct frame_base mips_insn16_frame_base =
2718 {
2719   &mips_insn16_frame_unwind,
2720   mips_insn16_frame_base_address,
2721   mips_insn16_frame_base_address,
2722   mips_insn16_frame_base_address
2723 };
2724
2725 static const struct frame_base *
2726 mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
2727 {
2728   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2729   CORE_ADDR pc = get_frame_pc (this_frame);
2730   if (mips_pc_is_mips16 (gdbarch, pc))
2731     return &mips_insn16_frame_base;
2732   else
2733     return NULL;
2734 }
2735
2736 /* Decode a 9-bit signed immediate argument of ADDIUSP -- -2 is mapped
2737    to -258, -1 -- to -257, 0 -- to 256, 1 -- to 257 and other values are
2738    interpreted directly, and then multiplied by 4.  */
2739
2740 static int
2741 micromips_decode_imm9 (int imm)
2742 {
2743   imm = (imm ^ 0x100) - 0x100;
2744   if (imm > -3 && imm < 2)
2745     imm ^= 0x100;
2746   return imm << 2;
2747 }
2748
2749 /* Analyze the function prologue from START_PC to LIMIT_PC.  Return
2750    the address of the first instruction past the prologue.  */
2751
2752 static CORE_ADDR
2753 micromips_scan_prologue (struct gdbarch *gdbarch,
2754                          CORE_ADDR start_pc, CORE_ADDR limit_pc,
2755                          struct frame_info *this_frame,
2756                          struct mips_frame_cache *this_cache)
2757 {
2758   CORE_ADDR end_prologue_addr = 0;
2759   int prev_non_prologue_insn = 0;
2760   int frame_reg = MIPS_SP_REGNUM;
2761   int this_non_prologue_insn;
2762   int non_prologue_insns = 0;
2763   long frame_offset = 0;        /* Size of stack frame.  */
2764   long frame_adjust = 0;        /* Offset of FP from SP.  */
2765   CORE_ADDR frame_addr = 0;     /* Value of $30, used as frame pointer.  */
2766   CORE_ADDR prev_pc;
2767   CORE_ADDR cur_pc;
2768   ULONGEST insn;                /* current instruction */
2769   CORE_ADDR sp;
2770   long offset;
2771   long sp_adj;
2772   long v1_off = 0;              /* The assumption is LUI will replace it.  */
2773   int reglist;
2774   int breg;
2775   int dreg;
2776   int sreg;
2777   int treg;
2778   int loc;
2779   int op;
2780   int s;
2781   int i;
2782
2783   /* Can be called when there's no process, and hence when there's no
2784      THIS_FRAME.  */
2785   if (this_frame != NULL)
2786     sp = get_frame_register_signed (this_frame,
2787                                     gdbarch_num_regs (gdbarch)
2788                                     + MIPS_SP_REGNUM);
2789   else
2790     sp = 0;
2791
2792   if (limit_pc > start_pc + 200)
2793     limit_pc = start_pc + 200;
2794   prev_pc = start_pc;
2795
2796   /* Permit at most one non-prologue non-control-transfer instruction
2797      in the middle which may have been reordered by the compiler for
2798      optimisation.  */
2799   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += loc)
2800     {
2801       this_non_prologue_insn = 0;
2802       sp_adj = 0;
2803       loc = 0;
2804       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, cur_pc, NULL);
2805       loc += MIPS_INSN16_SIZE;
2806       switch (mips_insn_size (ISA_MICROMIPS, insn))
2807         {
2808         /* 48-bit instructions.  */
2809         case 3 * MIPS_INSN16_SIZE:
2810           /* No prologue instructions in this category.  */
2811           this_non_prologue_insn = 1;
2812           loc += 2 * MIPS_INSN16_SIZE;
2813           break;
2814
2815         /* 32-bit instructions.  */
2816         case 2 * MIPS_INSN16_SIZE:
2817           insn <<= 16;
2818           insn |= mips_fetch_instruction (gdbarch,
2819                                           ISA_MICROMIPS, cur_pc + loc, NULL);
2820           loc += MIPS_INSN16_SIZE;
2821           switch (micromips_op (insn >> 16))
2822             {
2823             /* Record $sp/$fp adjustment.  */
2824             /* Discard (D)ADDU $gp,$jp used for PIC code.  */
2825             case 0x0: /* POOL32A: bits 000000 */
2826             case 0x16: /* POOL32S: bits 010110 */
2827               op = b0s11_op (insn);
2828               sreg = b0s5_reg (insn >> 16);
2829               treg = b5s5_reg (insn >> 16);
2830               dreg = b11s5_reg (insn);
2831               if (op == 0x1d0
2832                                 /* SUBU: bits 000000 00111010000 */
2833                                 /* DSUBU: bits 010110 00111010000 */
2834                   && dreg == MIPS_SP_REGNUM && sreg == MIPS_SP_REGNUM
2835                   && treg == 3)
2836                                 /* (D)SUBU $sp, $v1 */
2837                     sp_adj = v1_off;
2838               else if (op != 0x150
2839                                 /* ADDU: bits 000000 00101010000 */
2840                                 /* DADDU: bits 010110 00101010000 */
2841                        || dreg != 28 || sreg != 28 || treg != MIPS_T9_REGNUM)
2842                 this_non_prologue_insn = 1;
2843               break;
2844
2845             case 0x8: /* POOL32B: bits 001000 */
2846               op = b12s4_op (insn);
2847               breg = b0s5_reg (insn >> 16);
2848               reglist = sreg = b5s5_reg (insn >> 16);
2849               offset = (b0s12_imm (insn) ^ 0x800) - 0x800;
2850               if ((op == 0x9 || op == 0xc)
2851                                 /* SWP: bits 001000 1001 */
2852                                 /* SDP: bits 001000 1100 */
2853                   && breg == MIPS_SP_REGNUM && sreg < MIPS_RA_REGNUM)
2854                                 /* S[DW]P reg,offset($sp) */
2855                 {
2856                   s = 4 << ((b12s4_op (insn) & 0x4) == 0x4);
2857                   set_reg_offset (gdbarch, this_cache,
2858                                   sreg, sp + offset);
2859                   set_reg_offset (gdbarch, this_cache,
2860                                   sreg + 1, sp + offset + s);
2861                 }
2862               else if ((op == 0xd || op == 0xf)
2863                                 /* SWM: bits 001000 1101 */
2864                                 /* SDM: bits 001000 1111 */
2865                        && breg == MIPS_SP_REGNUM
2866                                 /* SWM reglist,offset($sp) */
2867                        && ((reglist >= 1 && reglist <= 9)
2868                            || (reglist >= 16 && reglist <= 25)))
2869                 {
2870                   int sreglist = min(reglist & 0xf, 8);
2871
2872                   s = 4 << ((b12s4_op (insn) & 0x2) == 0x2);
2873                   for (i = 0; i < sreglist; i++)
2874                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + s * i);
2875                   if ((reglist & 0xf) > 8)
2876                     set_reg_offset (gdbarch, this_cache, 30, sp + s * i++);
2877                   if ((reglist & 0x10) == 0x10)
2878                     set_reg_offset (gdbarch, this_cache,
2879                                     MIPS_RA_REGNUM, sp + s * i++);
2880                 }
2881               else
2882                 this_non_prologue_insn = 1;
2883               break;
2884
2885             /* Record $sp/$fp adjustment.  */
2886             /* Discard (D)ADDIU $gp used for PIC code.  */
2887             case 0xc: /* ADDIU: bits 001100 */
2888             case 0x17: /* DADDIU: bits 010111 */
2889               sreg = b0s5_reg (insn >> 16);
2890               dreg = b5s5_reg (insn >> 16);
2891               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
2892               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM)
2893                                 /* (D)ADDIU $sp, imm */
2894                 sp_adj = offset;
2895               else if (sreg == MIPS_SP_REGNUM && dreg == 30)
2896                                 /* (D)ADDIU $fp, $sp, imm */
2897                 {
2898                   frame_addr = sp + offset;
2899                   frame_adjust = offset;
2900                   frame_reg = 30;
2901                 }
2902               else if (sreg != 28 || dreg != 28)
2903                                 /* (D)ADDIU $gp, imm */
2904                 this_non_prologue_insn = 1;
2905               break;
2906
2907             /* LUI $v1 is used for larger $sp adjustments.  */
2908             /* Discard LUI $gp is used for PIC code.  */
2909             case 0x10: /* POOL32I: bits 010000 */
2910               if (b5s5_op (insn >> 16) == 0xd
2911                                 /* LUI: bits 010000 001101 */
2912                   && b0s5_reg (insn >> 16) == 3)
2913                                 /* LUI $v1, imm */
2914                 v1_off = ((b0s16_imm (insn) << 16) ^ 0x80000000) - 0x80000000;
2915               else if (b5s5_op (insn >> 16) != 0xd
2916                                 /* LUI: bits 010000 001101 */
2917                        || b0s5_reg (insn >> 16) != 28)
2918                                 /* LUI $gp, imm */
2919                 this_non_prologue_insn = 1;
2920               break;
2921
2922             /* ORI $v1 is used for larger $sp adjustments.  */
2923             case 0x14: /* ORI: bits 010100 */
2924               sreg = b0s5_reg (insn >> 16);
2925               dreg = b5s5_reg (insn >> 16);
2926               if (sreg == 3 && dreg == 3)
2927                                 /* ORI $v1, imm */
2928                 v1_off |= b0s16_imm (insn);
2929               else
2930                 this_non_prologue_insn = 1;
2931               break;
2932
2933             case 0x26: /* SWC1: bits 100110 */
2934             case 0x2e: /* SDC1: bits 101110 */
2935               breg = b0s5_reg (insn >> 16);
2936               if (breg != MIPS_SP_REGNUM)
2937                                 /* S[DW]C1 reg,offset($sp) */
2938                 this_non_prologue_insn = 1;
2939               break;
2940
2941             case 0x36: /* SD: bits 110110 */
2942             case 0x3e: /* SW: bits 111110 */
2943               breg = b0s5_reg (insn >> 16);
2944               sreg = b5s5_reg (insn >> 16);
2945               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
2946               if (breg == MIPS_SP_REGNUM)
2947                                 /* S[DW] reg,offset($sp) */
2948                 set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
2949               else
2950                 this_non_prologue_insn = 1;
2951               break;
2952
2953             default:
2954               this_non_prologue_insn = 1;
2955               break;
2956             }
2957           break;
2958
2959         /* 16-bit instructions.  */
2960         case MIPS_INSN16_SIZE:
2961           switch (micromips_op (insn))
2962             {
2963             case 0x3: /* MOVE: bits 000011 */
2964               sreg = b0s5_reg (insn);
2965               dreg = b5s5_reg (insn);
2966               if (sreg == MIPS_SP_REGNUM && dreg == 30)
2967                                 /* MOVE  $fp, $sp */
2968                 {
2969                   frame_addr = sp;
2970                   frame_reg = 30;
2971                 }
2972               else if ((sreg & 0x1c) != 0x4)
2973                                 /* MOVE  reg, $a0-$a3 */
2974                 this_non_prologue_insn = 1;
2975               break;
2976
2977             case 0x11: /* POOL16C: bits 010001 */
2978               if (b6s4_op (insn) == 0x5)
2979                                 /* SWM: bits 010001 0101 */
2980                 {
2981                   offset = ((b0s4_imm (insn) << 2) ^ 0x20) - 0x20;
2982                   reglist = b4s2_regl (insn);
2983                   for (i = 0; i <= reglist; i++)
2984                     set_reg_offset (gdbarch, this_cache, 16 + i, sp + 4 * i);
2985                   set_reg_offset (gdbarch, this_cache,
2986                                   MIPS_RA_REGNUM, sp + 4 * i++);
2987                 }
2988               else
2989                 this_non_prologue_insn = 1;
2990               break;
2991
2992             case 0x13: /* POOL16D: bits 010011 */
2993               if ((insn & 0x1) == 0x1)
2994                                 /* ADDIUSP: bits 010011 1 */
2995                 sp_adj = micromips_decode_imm9 (b1s9_imm (insn));
2996               else if (b5s5_reg (insn) == MIPS_SP_REGNUM)
2997                                 /* ADDIUS5: bits 010011 0 */
2998                                 /* ADDIUS5 $sp, imm */
2999                 sp_adj = (b1s4_imm (insn) ^ 8) - 8;
3000               else
3001                 this_non_prologue_insn = 1;
3002               break;
3003
3004             case 0x32: /* SWSP: bits 110010 */
3005               offset = b0s5_imm (insn) << 2;
3006               sreg = b5s5_reg (insn);
3007               set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
3008               break;
3009
3010             default:
3011               this_non_prologue_insn = 1;
3012               break;
3013             }
3014           break;
3015         }
3016       if (sp_adj < 0)
3017         frame_offset -= sp_adj;
3018
3019       non_prologue_insns += this_non_prologue_insn;
3020       /* Enough non-prologue insns seen or positive stack adjustment? */
3021       if (end_prologue_addr == 0 && (non_prologue_insns > 1 || sp_adj > 0))
3022         {
3023           end_prologue_addr = prev_non_prologue_insn ? prev_pc : cur_pc;
3024           break;
3025         }
3026       prev_non_prologue_insn = this_non_prologue_insn;
3027       prev_pc = cur_pc;
3028     }
3029
3030   if (this_cache != NULL)
3031     {
3032       this_cache->base =
3033         (get_frame_register_signed (this_frame,
3034                                     gdbarch_num_regs (gdbarch) + frame_reg)
3035          + frame_offset - frame_adjust);
3036       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
3037          be able to get rid of the assignment below, evetually. But it's
3038          still needed for now.  */
3039       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3040                              + mips_regnum (gdbarch)->pc]
3041         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
3042     }
3043
3044   /* If we didn't reach the end of the prologue when scanning the function
3045      instructions, then set end_prologue_addr to the address of the
3046      instruction immediately after the last one we scanned.  Unless the
3047      last one looked like a non-prologue instruction (and we looked ahead),
3048      in which case use its address instead.  */
3049   if (end_prologue_addr == 0)
3050     end_prologue_addr = prev_non_prologue_insn ? prev_pc : cur_pc;
3051
3052   return end_prologue_addr;
3053 }
3054
3055 /* Heuristic unwinder for procedures using microMIPS instructions.
3056    Procedures that use the 32-bit instruction set are handled by the
3057    mips_insn32 unwinder.  Likewise MIPS16 and the mips_insn16 unwinder. */
3058
3059 static struct mips_frame_cache *
3060 mips_micro_frame_cache (struct frame_info *this_frame, void **this_cache)
3061 {
3062   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3063   struct mips_frame_cache *cache;
3064
3065   if ((*this_cache) != NULL)
3066     return (*this_cache);
3067
3068   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
3069   (*this_cache) = cache;
3070   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3071
3072   /* Analyze the function prologue.  */
3073   {
3074     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
3075     CORE_ADDR start_addr;
3076
3077     find_pc_partial_function (pc, NULL, &start_addr, NULL);
3078     if (start_addr == 0)
3079       start_addr = heuristic_proc_start (get_frame_arch (this_frame), pc);
3080     /* We can't analyze the prologue if we couldn't find the begining
3081        of the function.  */
3082     if (start_addr == 0)
3083       return cache;
3084
3085     micromips_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
3086   }
3087
3088   /* gdbarch_sp_regnum contains the value and not the address.  */
3089   trad_frame_set_value (cache->saved_regs,
3090                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
3091                         cache->base);
3092
3093   return (*this_cache);
3094 }
3095
3096 static void
3097 mips_micro_frame_this_id (struct frame_info *this_frame, void **this_cache,
3098                           struct frame_id *this_id)
3099 {
3100   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3101                                                           this_cache);
3102   /* This marks the outermost frame.  */
3103   if (info->base == 0)
3104     return;
3105   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
3106 }
3107
3108 static struct value *
3109 mips_micro_frame_prev_register (struct frame_info *this_frame,
3110                                 void **this_cache, int regnum)
3111 {
3112   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3113                                                           this_cache);
3114   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3115 }
3116
3117 static int
3118 mips_micro_frame_sniffer (const struct frame_unwind *self,
3119                           struct frame_info *this_frame, void **this_cache)
3120 {
3121   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3122   CORE_ADDR pc = get_frame_pc (this_frame);
3123
3124   if (mips_pc_is_micromips (gdbarch, pc))
3125     return 1;
3126   return 0;
3127 }
3128
3129 static const struct frame_unwind mips_micro_frame_unwind =
3130 {
3131   NORMAL_FRAME,
3132   default_frame_unwind_stop_reason,
3133   mips_micro_frame_this_id,
3134   mips_micro_frame_prev_register,
3135   NULL,
3136   mips_micro_frame_sniffer
3137 };
3138
3139 static CORE_ADDR
3140 mips_micro_frame_base_address (struct frame_info *this_frame,
3141                                void **this_cache)
3142 {
3143   struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3144                                                           this_cache);
3145   return info->base;
3146 }
3147
3148 static const struct frame_base mips_micro_frame_base =
3149 {
3150   &mips_micro_frame_unwind,
3151   mips_micro_frame_base_address,
3152   mips_micro_frame_base_address,
3153   mips_micro_frame_base_address
3154 };
3155
3156 static const struct frame_base *
3157 mips_micro_frame_base_sniffer (struct frame_info *this_frame)
3158 {
3159   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3160   CORE_ADDR pc = get_frame_pc (this_frame);
3161
3162   if (mips_pc_is_micromips (gdbarch, pc))
3163     return &mips_micro_frame_base;
3164   else
3165     return NULL;
3166 }
3167
3168 /* Mark all the registers as unset in the saved_regs array
3169    of THIS_CACHE.  Do nothing if THIS_CACHE is null.  */
3170
3171 static void
3172 reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
3173 {
3174   if (this_cache == NULL || this_cache->saved_regs == NULL)
3175     return;
3176
3177   {
3178     const int num_regs = gdbarch_num_regs (gdbarch);
3179     int i;
3180
3181     for (i = 0; i < num_regs; i++)
3182       {
3183         this_cache->saved_regs[i].addr = -1;
3184       }
3185   }
3186 }
3187
3188 /* Analyze the function prologue from START_PC to LIMIT_PC.  Builds
3189    the associated FRAME_CACHE if not null.  
3190    Return the address of the first instruction past the prologue.  */
3191
3192 static CORE_ADDR
3193 mips32_scan_prologue (struct gdbarch *gdbarch,
3194                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
3195                       struct frame_info *this_frame,
3196                       struct mips_frame_cache *this_cache)
3197 {
3198   CORE_ADDR cur_pc;
3199   CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
3200                                frame-pointer.  */
3201   CORE_ADDR sp;
3202   long frame_offset;
3203   int  frame_reg = MIPS_SP_REGNUM;
3204
3205   CORE_ADDR end_prologue_addr = 0;
3206   int seen_sp_adjust = 0;
3207   int load_immediate_bytes = 0;
3208   int in_delay_slot = 0;
3209   int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);
3210
3211   /* Can be called when there's no process, and hence when there's no
3212      THIS_FRAME.  */
3213   if (this_frame != NULL)
3214     sp = get_frame_register_signed (this_frame,
3215                                     gdbarch_num_regs (gdbarch)
3216                                     + MIPS_SP_REGNUM);
3217   else
3218     sp = 0;
3219
3220   if (limit_pc > start_pc + 200)
3221     limit_pc = start_pc + 200;
3222
3223 restart:
3224
3225   frame_offset = 0;
3226   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
3227     {
3228       unsigned long inst, high_word, low_word;
3229       int reg;
3230
3231       /* Fetch the instruction.  */
3232       inst = (unsigned long) mips_fetch_instruction (gdbarch, ISA_MIPS,
3233                                                      cur_pc, NULL);
3234
3235       /* Save some code by pre-extracting some useful fields.  */
3236       high_word = (inst >> 16) & 0xffff;
3237       low_word = inst & 0xffff;
3238       reg = high_word & 0x1f;
3239
3240       if (high_word == 0x27bd           /* addiu $sp,$sp,-i */
3241           || high_word == 0x23bd        /* addi $sp,$sp,-i */
3242           || high_word == 0x67bd)       /* daddiu $sp,$sp,-i */
3243         {
3244           if (low_word & 0x8000)        /* Negative stack adjustment?  */
3245             frame_offset += 0x10000 - low_word;
3246           else
3247             /* Exit loop if a positive stack adjustment is found, which
3248                usually means that the stack cleanup code in the function
3249                epilogue is reached.  */
3250             break;
3251           seen_sp_adjust = 1;
3252         }
3253       else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
3254                && !regsize_is_64_bits)
3255         {
3256           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
3257         }
3258       else if (((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
3259                && regsize_is_64_bits)
3260         {
3261           /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra.  */
3262           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
3263         }
3264       else if (high_word == 0x27be)     /* addiu $30,$sp,size */
3265         {
3266           /* Old gcc frame, r30 is virtual frame pointer.  */
3267           if ((long) low_word != frame_offset)
3268             frame_addr = sp + low_word;
3269           else if (this_frame && frame_reg == MIPS_SP_REGNUM)
3270             {
3271               unsigned alloca_adjust;
3272
3273               frame_reg = 30;
3274               frame_addr = get_frame_register_signed
3275                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
3276               frame_offset = 0;
3277
3278               alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
3279               if (alloca_adjust > 0)
3280                 {
3281                   /* FP > SP + frame_size.  This may be because of
3282                      an alloca or somethings similar.  Fix sp to
3283                      "pre-alloca" value, and try again.  */
3284                   sp += alloca_adjust;
3285                   /* Need to reset the status of all registers.  Otherwise,
3286                      we will hit a guard that prevents the new address
3287                      for each register to be recomputed during the second
3288                      pass.  */
3289                   reset_saved_regs (gdbarch, this_cache);
3290                   goto restart;
3291                 }
3292             }
3293         }
3294       /* move $30,$sp.  With different versions of gas this will be either
3295          `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
3296          Accept any one of these.  */
3297       else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
3298         {
3299           /* New gcc frame, virtual frame pointer is at r30 + frame_size.  */
3300           if (this_frame && frame_reg == MIPS_SP_REGNUM)
3301             {
3302               unsigned alloca_adjust;
3303
3304               frame_reg = 30;
3305               frame_addr = get_frame_register_signed
3306                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
3307
3308               alloca_adjust = (unsigned) (frame_addr - sp);
3309               if (alloca_adjust > 0)
3310                 {
3311                   /* FP > SP + frame_size.  This may be because of
3312                      an alloca or somethings similar.  Fix sp to
3313                      "pre-alloca" value, and try again.  */
3314                   sp = frame_addr;
3315                   /* Need to reset the status of all registers.  Otherwise,
3316                      we will hit a guard that prevents the new address
3317                      for each register to be recomputed during the second
3318                      pass.  */
3319                   reset_saved_regs (gdbarch, this_cache);
3320                   goto restart;
3321                 }
3322             }
3323         }
3324       else if ((high_word & 0xFFE0) == 0xafc0   /* sw reg,offset($30) */
3325                && !regsize_is_64_bits)
3326         {
3327           set_reg_offset (gdbarch, this_cache, reg, frame_addr + low_word);
3328         }
3329       else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
3330                || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
3331                || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
3332                || high_word == 0x3c1c /* lui $gp,n */
3333                || high_word == 0x279c /* addiu $gp,$gp,n */
3334                || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
3335                || inst == 0x033ce021 /* addu $gp,$t9,$gp */
3336               )
3337         {
3338           /* These instructions are part of the prologue, but we don't
3339              need to do anything special to handle them.  */
3340         }
3341       /* The instructions below load $at or $t0 with an immediate
3342          value in preparation for a stack adjustment via
3343          subu $sp,$sp,[$at,$t0].  These instructions could also
3344          initialize a local variable, so we accept them only before
3345          a stack adjustment instruction was seen.  */
3346       else if (!seen_sp_adjust
3347                && (high_word == 0x3c01 /* lui $at,n */
3348                    || high_word == 0x3c08 /* lui $t0,n */
3349                    || high_word == 0x3421 /* ori $at,$at,n */
3350                    || high_word == 0x3508 /* ori $t0,$t0,n */
3351                    || high_word == 0x3401 /* ori $at,$zero,n */
3352                    || high_word == 0x3408 /* ori $t0,$zero,n */
3353                   ))
3354         {
3355           if (end_prologue_addr == 0)
3356             load_immediate_bytes += MIPS_INSN32_SIZE;           /* FIXME!  */
3357         }
3358       else
3359         {
3360           /* This instruction is not an instruction typically found
3361              in a prologue, so we must have reached the end of the
3362              prologue.  */
3363           /* FIXME: brobecker/2004-10-10: Can't we just break out of this
3364              loop now?  Why would we need to continue scanning the function
3365              instructions?  */
3366           if (end_prologue_addr == 0)
3367             end_prologue_addr = cur_pc;
3368
3369           /* Check for branches and jumps.  For now, only jump to
3370              register are caught (i.e. returns).  */
3371           if ((itype_op (inst) & 0x07) == 0 && rtype_funct (inst) == 8)
3372             in_delay_slot = 1;
3373         }
3374
3375       /* If the previous instruction was a jump, we must have reached
3376          the end of the prologue by now.  Stop scanning so that we do
3377          not go past the function return.  */
3378       if (in_delay_slot)
3379         break;
3380     }
3381
3382   if (this_cache != NULL)
3383     {
3384       this_cache->base = 
3385         (get_frame_register_signed (this_frame,
3386                                     gdbarch_num_regs (gdbarch) + frame_reg)
3387          + frame_offset);
3388       /* FIXME: brobecker/2004-09-15: We should be able to get rid of
3389          this assignment below, eventually.  But it's still needed
3390          for now.  */
3391       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3392                              + mips_regnum (gdbarch)->pc]
3393         = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3394                                  + MIPS_RA_REGNUM];
3395     }
3396
3397   /* If we didn't reach the end of the prologue when scanning the function
3398      instructions, then set end_prologue_addr to the address of the
3399      instruction immediately after the last one we scanned.  */
3400   /* brobecker/2004-10-10: I don't think this would ever happen, but
3401      we may as well be careful and do our best if we have a null
3402      end_prologue_addr.  */
3403   if (end_prologue_addr == 0)
3404     end_prologue_addr = cur_pc;
3405      
3406   /* In a frameless function, we might have incorrectly
3407      skipped some load immediate instructions.  Undo the skipping
3408      if the load immediate was not followed by a stack adjustment.  */
3409   if (load_immediate_bytes && !seen_sp_adjust)
3410     end_prologue_addr -= load_immediate_bytes;
3411
3412   return end_prologue_addr;
3413 }
3414
3415 /* Heuristic unwinder for procedures using 32-bit instructions (covers
3416    both 32-bit and 64-bit MIPS ISAs).  Procedures using 16-bit
3417    instructions (a.k.a. MIPS16) are handled by the mips_insn16
3418    unwinder.  Likewise microMIPS and the mips_micro unwinder. */
3419
3420 static struct mips_frame_cache *
3421 mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
3422 {
3423   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3424   struct mips_frame_cache *cache;
3425
3426   if ((*this_cache) != NULL)
3427     return (*this_cache);
3428
3429   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
3430   (*this_cache) = cache;
3431   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3432
3433   /* Analyze the function prologue.  */
3434   {
3435     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
3436     CORE_ADDR start_addr;
3437
3438     find_pc_partial_function (pc, NULL, &start_addr, NULL);
3439     if (start_addr == 0)
3440       start_addr = heuristic_proc_start (gdbarch, pc);
3441     /* We can't analyze the prologue if we couldn't find the begining
3442        of the function.  */
3443     if (start_addr == 0)
3444       return cache;
3445
3446     mips32_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
3447   }
3448   
3449   /* gdbarch_sp_regnum contains the value and not the address.  */
3450   trad_frame_set_value (cache->saved_regs,
3451                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
3452                         cache->base);
3453
3454   return (*this_cache);
3455 }
3456
3457 static void
3458 mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
3459                            struct frame_id *this_id)
3460 {
3461   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3462                                                            this_cache);
3463   /* This marks the outermost frame.  */
3464   if (info->base == 0)
3465     return;
3466   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
3467 }
3468
3469 static struct value *
3470 mips_insn32_frame_prev_register (struct frame_info *this_frame,
3471                                  void **this_cache, int regnum)
3472 {
3473   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3474                                                            this_cache);
3475   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3476 }
3477
3478 static int
3479 mips_insn32_frame_sniffer (const struct frame_unwind *self,
3480                            struct frame_info *this_frame, void **this_cache)
3481 {
3482   CORE_ADDR pc = get_frame_pc (this_frame);
3483   if (mips_pc_is_mips (pc))
3484     return 1;
3485   return 0;
3486 }
3487
3488 static const struct frame_unwind mips_insn32_frame_unwind =
3489 {
3490   NORMAL_FRAME,
3491   default_frame_unwind_stop_reason,
3492   mips_insn32_frame_this_id,
3493   mips_insn32_frame_prev_register,
3494   NULL,
3495   mips_insn32_frame_sniffer
3496 };
3497
3498 static CORE_ADDR
3499 mips_insn32_frame_base_address (struct frame_info *this_frame,
3500                                 void **this_cache)
3501 {
3502   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3503                                                            this_cache);
3504   return info->base;
3505 }
3506
3507 static const struct frame_base mips_insn32_frame_base =
3508 {
3509   &mips_insn32_frame_unwind,
3510   mips_insn32_frame_base_address,
3511   mips_insn32_frame_base_address,
3512   mips_insn32_frame_base_address
3513 };
3514
3515 static const struct frame_base *
3516 mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
3517 {
3518   CORE_ADDR pc = get_frame_pc (this_frame);
3519   if (mips_pc_is_mips (pc))
3520     return &mips_insn32_frame_base;
3521   else
3522     return NULL;
3523 }
3524
3525 static struct trad_frame_cache *
3526 mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
3527 {
3528   CORE_ADDR pc;
3529   CORE_ADDR start_addr;
3530   CORE_ADDR stack_addr;
3531   struct trad_frame_cache *this_trad_cache;
3532   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3533   int num_regs = gdbarch_num_regs (gdbarch);
3534
3535   if ((*this_cache) != NULL)
3536     return (*this_cache);
3537   this_trad_cache = trad_frame_cache_zalloc (this_frame);
3538   (*this_cache) = this_trad_cache;
3539
3540   /* The return address is in the link register.  */
3541   trad_frame_set_reg_realreg (this_trad_cache,
3542                               gdbarch_pc_regnum (gdbarch),
3543                               num_regs + MIPS_RA_REGNUM);
3544
3545   /* Frame ID, since it's a frameless / stackless function, no stack
3546      space is allocated and SP on entry is the current SP.  */
3547   pc = get_frame_pc (this_frame);
3548   find_pc_partial_function (pc, NULL, &start_addr, NULL);
3549   stack_addr = get_frame_register_signed (this_frame,
3550                                           num_regs + MIPS_SP_REGNUM);
3551   trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
3552
3553   /* Assume that the frame's base is the same as the
3554      stack-pointer.  */
3555   trad_frame_set_this_base (this_trad_cache, stack_addr);
3556
3557   return this_trad_cache;
3558 }
3559
3560 static void
3561 mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
3562                          struct frame_id *this_id)
3563 {
3564   struct trad_frame_cache *this_trad_cache
3565     = mips_stub_frame_cache (this_frame, this_cache);
3566   trad_frame_get_id (this_trad_cache, this_id);
3567 }
3568
3569 static struct value *
3570 mips_stub_frame_prev_register (struct frame_info *this_frame,
3571                                void **this_cache, int regnum)
3572 {
3573   struct trad_frame_cache *this_trad_cache
3574     = mips_stub_frame_cache (this_frame, this_cache);
3575   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
3576 }
3577
3578 static int
3579 mips_stub_frame_sniffer (const struct frame_unwind *self,
3580                          struct frame_info *this_frame, void **this_cache)
3581 {
3582   gdb_byte dummy[4];
3583   struct obj_section *s;
3584   CORE_ADDR pc = get_frame_address_in_block (this_frame);
3585   struct minimal_symbol *msym;
3586
3587   /* Use the stub unwinder for unreadable code.  */
3588   if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
3589     return 1;
3590
3591   if (in_plt_section (pc, NULL))
3592     return 1;
3593
3594   /* Binutils for MIPS puts lazy resolution stubs into .MIPS.stubs.  */
3595   s = find_pc_section (pc);
3596
3597   if (s != NULL
3598       && strcmp (bfd_get_section_name (s->objfile->obfd, s->the_bfd_section),
3599                  ".MIPS.stubs") == 0)
3600     return 1;
3601
3602   /* Calling a PIC function from a non-PIC function passes through a
3603      stub.  The stub for foo is named ".pic.foo".  */
3604   msym = lookup_minimal_symbol_by_pc (pc);
3605   if (msym != NULL
3606       && SYMBOL_LINKAGE_NAME (msym) != NULL
3607       && strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) == 0)
3608     return 1;
3609
3610   return 0;
3611 }
3612
3613 static const struct frame_unwind mips_stub_frame_unwind =
3614 {
3615   NORMAL_FRAME,
3616   default_frame_unwind_stop_reason,
3617   mips_stub_frame_this_id,
3618   mips_stub_frame_prev_register,
3619   NULL,
3620   mips_stub_frame_sniffer
3621 };
3622
3623 static CORE_ADDR
3624 mips_stub_frame_base_address (struct frame_info *this_frame,
3625                               void **this_cache)
3626 {
3627   struct trad_frame_cache *this_trad_cache
3628     = mips_stub_frame_cache (this_frame, this_cache);
3629   return trad_frame_get_this_base (this_trad_cache);
3630 }
3631
3632 static const struct frame_base mips_stub_frame_base =
3633 {
3634   &mips_stub_frame_unwind,
3635   mips_stub_frame_base_address,
3636   mips_stub_frame_base_address,
3637   mips_stub_frame_base_address
3638 };
3639
3640 static const struct frame_base *
3641 mips_stub_frame_base_sniffer (struct frame_info *this_frame)
3642 {
3643   if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
3644     return &mips_stub_frame_base;
3645   else
3646     return NULL;
3647 }
3648
3649 /* mips_addr_bits_remove - remove useless address bits  */
3650
3651 static CORE_ADDR
3652 mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
3653 {
3654   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3655
3656   if (is_compact_addr (addr))
3657     addr = unmake_compact_addr (addr);
3658
3659   if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
3660     /* This hack is a work-around for existing boards using PMON, the
3661        simulator, and any other 64-bit targets that doesn't have true
3662        64-bit addressing.  On these targets, the upper 32 bits of
3663        addresses are ignored by the hardware.  Thus, the PC or SP are
3664        likely to have been sign extended to all 1s by instruction
3665        sequences that load 32-bit addresses.  For example, a typical
3666        piece of code that loads an address is this:
3667
3668        lui $r2, <upper 16 bits>
3669        ori $r2, <lower 16 bits>
3670
3671        But the lui sign-extends the value such that the upper 32 bits
3672        may be all 1s.  The workaround is simply to mask off these
3673        bits.  In the future, gcc may be changed to support true 64-bit
3674        addressing, and this masking will have to be disabled.  */
3675     return addr &= 0xffffffffUL;
3676   else
3677     return addr;
3678 }
3679
3680
3681 /* Checks for an atomic sequence of instructions beginning with a LL/LLD
3682    instruction and ending with a SC/SCD instruction.  If such a sequence
3683    is found, attempt to step through it.  A breakpoint is placed at the end of 
3684    the sequence.  */
3685
3686 /* Instructions used during single-stepping of atomic sequences, standard
3687    ISA version.  */
3688 #define LL_OPCODE 0x30
3689 #define LLD_OPCODE 0x34
3690 #define SC_OPCODE 0x38
3691 #define SCD_OPCODE 0x3c
3692
3693 static int
3694 mips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
3695                                 struct address_space *aspace, CORE_ADDR pc)
3696 {
3697   CORE_ADDR breaks[2] = {-1, -1};
3698   CORE_ADDR loc = pc;
3699   CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination.  */
3700   ULONGEST insn;
3701   int insn_count;
3702   int index;
3703   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */  
3704   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
3705
3706   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
3707   /* Assume all atomic sequences start with a ll/lld instruction.  */
3708   if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
3709     return 0;
3710
3711   /* Assume that no atomic sequence is longer than "atomic_sequence_length" 
3712      instructions.  */
3713   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
3714     {
3715       int is_branch = 0;
3716       loc += MIPS_INSN32_SIZE;
3717       insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
3718
3719       /* Assume that there is at most one branch in the atomic
3720          sequence.  If a branch is found, put a breakpoint in its
3721          destination address.  */
3722       switch (itype_op (insn))
3723         {
3724         case 0: /* SPECIAL */
3725           if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
3726             return 0; /* fallback to the standard single-step code.  */
3727           break;
3728         case 1: /* REGIMM */
3729           is_branch = ((itype_rt (insn) & 0xc) == 0 /* B{LT,GE}Z* */
3730                        || ((itype_rt (insn) & 0x1e) == 0
3731                            && itype_rs (insn) == 0)); /* BPOSGE* */
3732           break;
3733         case 2: /* J */
3734         case 3: /* JAL */
3735           return 0; /* fallback to the standard single-step code.  */
3736         case 4: /* BEQ */
3737         case 5: /* BNE */
3738         case 6: /* BLEZ */
3739         case 7: /* BGTZ */
3740         case 20: /* BEQL */
3741         case 21: /* BNEL */
3742         case 22: /* BLEZL */
3743         case 23: /* BGTTL */
3744           is_branch = 1;
3745           break;
3746         case 17: /* COP1 */
3747           is_branch = ((itype_rs (insn) == 9 || itype_rs (insn) == 10)
3748                        && (itype_rt (insn) & 0x2) == 0);
3749           if (is_branch) /* BC1ANY2F, BC1ANY2T, BC1ANY4F, BC1ANY4T */
3750             break;
3751         /* Fall through.  */
3752         case 18: /* COP2 */
3753         case 19: /* COP3 */
3754           is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
3755           break;
3756         }
3757       if (is_branch)
3758         {
3759           branch_bp = loc + mips32_relative_offset (insn) + 4;
3760           if (last_breakpoint >= 1)
3761             return 0; /* More than one branch found, fallback to the
3762                          standard single-step code.  */
3763           breaks[1] = branch_bp;
3764           last_breakpoint++;
3765         }
3766
3767       if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
3768         break;
3769     }
3770
3771   /* Assume that the atomic sequence ends with a sc/scd instruction.  */
3772   if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
3773     return 0;
3774
3775   loc += MIPS_INSN32_SIZE;
3776
3777   /* Insert a breakpoint right after the end of the atomic sequence.  */
3778   breaks[0] = loc;
3779
3780   /* Check for duplicated breakpoints.  Check also for a breakpoint
3781      placed (branch instruction's destination) in the atomic sequence.  */
3782   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
3783     last_breakpoint = 0;
3784
3785   /* Effectively inserts the breakpoints.  */
3786   for (index = 0; index <= last_breakpoint; index++)
3787     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
3788
3789   return 1;
3790 }
3791
3792 static int
3793 micromips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
3794                                      struct address_space *aspace,
3795                                      CORE_ADDR pc)
3796 {
3797   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
3798   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
3799   CORE_ADDR breaks[2] = {-1, -1};
3800   CORE_ADDR branch_bp = 0; /* Breakpoint at branch instruction's
3801                               destination.  */
3802   CORE_ADDR loc = pc;
3803   int sc_found = 0;
3804   ULONGEST insn;
3805   int insn_count;
3806   int index;
3807
3808   /* Assume all atomic sequences start with a ll/lld instruction.  */
3809   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3810   if (micromips_op (insn) != 0x18)      /* POOL32C: bits 011000 */
3811     return 0;
3812   loc += MIPS_INSN16_SIZE;
3813   insn <<= 16;
3814   insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3815   if ((b12s4_op (insn) & 0xb) != 0x3)   /* LL, LLD: bits 011000 0x11 */
3816     return 0;
3817   loc += MIPS_INSN16_SIZE;
3818
3819   /* Assume all atomic sequences end with an sc/scd instruction.  Assume
3820      that no atomic sequence is longer than "atomic_sequence_length"
3821      instructions.  */
3822   for (insn_count = 0;
3823        !sc_found && insn_count < atomic_sequence_length;
3824        ++insn_count)
3825     {
3826       int is_branch = 0;
3827
3828       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3829       loc += MIPS_INSN16_SIZE;
3830
3831       /* Assume that there is at most one conditional branch in the
3832          atomic sequence.  If a branch is found, put a breakpoint in
3833          its destination address.  */
3834       switch (mips_insn_size (ISA_MICROMIPS, insn))
3835         {
3836         /* 48-bit instructions.  */
3837         case 3 * MIPS_INSN16_SIZE: /* POOL48A: bits 011111 */
3838           loc += 2 * MIPS_INSN16_SIZE;
3839           break;
3840
3841         /* 32-bit instructions.  */
3842         case 2 * MIPS_INSN16_SIZE:
3843           switch (micromips_op (insn))
3844             {
3845             case 0x10: /* POOL32I: bits 010000 */
3846               if ((b5s5_op (insn) & 0x18) != 0x0
3847                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
3848                                 /* BLEZ, BNEZC, BGTZ, BEQZC: 010000 001xx */
3849                   && (b5s5_op (insn) & 0x1d) != 0x11
3850                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
3851                   && ((b5s5_op (insn) & 0x1e) != 0x14
3852                       || (insn & 0x3) != 0x0)
3853                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
3854                   && (b5s5_op (insn) & 0x1e) != 0x1a
3855                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
3856                   && ((b5s5_op (insn) & 0x1e) != 0x1c
3857                       || (insn & 0x3) != 0x0)
3858                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
3859                   && ((b5s5_op (insn) & 0x1c) != 0x1c
3860                       || (insn & 0x3) != 0x1))
3861                                 /* BC1ANY*: bits 010000 111xx xxx01 */
3862                 break;
3863               /* Fall through.  */
3864
3865             case 0x25: /* BEQ: bits 100101 */
3866             case 0x2d: /* BNE: bits 101101 */
3867               insn <<= 16;
3868               insn |= mips_fetch_instruction (gdbarch,
3869                                               ISA_MICROMIPS, loc, NULL);
3870               branch_bp = (loc + MIPS_INSN16_SIZE
3871                            + micromips_relative_offset16 (insn));
3872               is_branch = 1;
3873               break;
3874
3875             case 0x00: /* POOL32A: bits 000000 */
3876               insn <<= 16;
3877               insn |= mips_fetch_instruction (gdbarch,
3878                                               ISA_MICROMIPS, loc, NULL);
3879               if (b0s6_op (insn) != 0x3c
3880                                 /* POOL32Axf: bits 000000 ... 111100 */
3881                   || (b6s10_ext (insn) & 0x2bf) != 0x3c)
3882                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
3883                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
3884                 break;
3885               /* Fall through.  */
3886
3887             case 0x1d: /* JALS: bits 011101 */
3888             case 0x35: /* J: bits 110101 */
3889             case 0x3d: /* JAL: bits 111101 */
3890             case 0x3c: /* JALX: bits 111100 */
3891               return 0; /* Fall back to the standard single-step code. */
3892
3893             case 0x18: /* POOL32C: bits 011000 */
3894               if ((b12s4_op (insn) & 0xb) == 0xb)
3895                                 /* SC, SCD: bits 011000 1x11 */
3896                 sc_found = 1;
3897               break;
3898             }
3899           loc += MIPS_INSN16_SIZE;
3900           break;
3901
3902         /* 16-bit instructions.  */
3903         case MIPS_INSN16_SIZE:
3904           switch (micromips_op (insn))
3905             {
3906             case 0x23: /* BEQZ16: bits 100011 */
3907             case 0x2b: /* BNEZ16: bits 101011 */
3908               branch_bp = loc + micromips_relative_offset7 (insn);
3909               is_branch = 1;
3910               break;
3911
3912             case 0x11: /* POOL16C: bits 010001 */
3913               if ((b5s5_op (insn) & 0x1c) != 0xc
3914                                 /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
3915                   && b5s5_op (insn) != 0x18)
3916                                 /* JRADDIUSP: bits 010001 11000 */
3917                 break;
3918               return 0; /* Fall back to the standard single-step code. */
3919
3920             case 0x33: /* B16: bits 110011 */
3921               return 0; /* Fall back to the standard single-step code. */
3922             }
3923           break;
3924         }
3925       if (is_branch)
3926         {
3927           if (last_breakpoint >= 1)
3928             return 0; /* More than one branch found, fallback to the
3929                          standard single-step code.  */
3930           breaks[1] = branch_bp;
3931           last_breakpoint++;
3932         }
3933     }
3934   if (!sc_found)
3935     return 0;
3936
3937   /* Insert a breakpoint right after the end of the atomic sequence.  */
3938   breaks[0] = loc;
3939
3940   /* Check for duplicated breakpoints.  Check also for a breakpoint
3941      placed (branch instruction's destination) in the atomic sequence */
3942   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
3943     last_breakpoint = 0;
3944
3945   /* Effectively inserts the breakpoints.  */
3946   for (index = 0; index <= last_breakpoint; index++)
3947       insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
3948
3949   return 1;
3950 }
3951
3952 static int
3953 deal_with_atomic_sequence (struct gdbarch *gdbarch,
3954                            struct address_space *aspace, CORE_ADDR pc)
3955 {
3956   if (mips_pc_is_mips (pc))
3957     return mips_deal_with_atomic_sequence (gdbarch, aspace, pc);
3958   else if (mips_pc_is_micromips (gdbarch, pc))
3959     return micromips_deal_with_atomic_sequence (gdbarch, aspace, pc);
3960   else
3961     return 0;
3962 }
3963
3964 /* mips_software_single_step() is called just before we want to resume
3965    the inferior, if we want to single-step it but there is no hardware
3966    or kernel single-step support (MIPS on GNU/Linux for example).  We find
3967    the target of the coming instruction and breakpoint it.  */
3968
3969 int
3970 mips_software_single_step (struct frame_info *frame)
3971 {
3972   struct gdbarch *gdbarch = get_frame_arch (frame);
3973   struct address_space *aspace = get_frame_address_space (frame);
3974   CORE_ADDR pc, next_pc;
3975
3976   pc = get_frame_pc (frame);
3977   if (deal_with_atomic_sequence (gdbarch, aspace, pc))
3978     return 1;
3979
3980   next_pc = mips_next_pc (frame, pc);
3981
3982   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
3983   return 1;
3984 }
3985
3986 /* Test whether the PC points to the return instruction at the
3987    end of a function.  */
3988
3989 static int
3990 mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
3991 {
3992   ULONGEST insn;
3993   ULONGEST hint;
3994
3995   /* This used to check for MIPS16, but this piece of code is never
3996      called for MIPS16 functions.  And likewise microMIPS ones.  */
3997   gdb_assert (mips_pc_is_mips (pc));
3998
3999   insn = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
4000   hint = 0x7c0;
4001   return (insn & ~hint) == 0x3e00008;                   /* jr(.hb) $ra */
4002 }
4003
4004
4005 /* This fencepost looks highly suspicious to me.  Removing it also
4006    seems suspicious as it could affect remote debugging across serial
4007    lines.  */
4008
4009 static CORE_ADDR
4010 heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
4011 {
4012   CORE_ADDR start_pc;
4013   CORE_ADDR fence;
4014   int instlen;
4015   int seen_adjsp = 0;
4016   struct inferior *inf;
4017
4018   pc = gdbarch_addr_bits_remove (gdbarch, pc);
4019   start_pc = pc;
4020   fence = start_pc - heuristic_fence_post;
4021   if (start_pc == 0)
4022     return 0;
4023
4024   if (heuristic_fence_post == UINT_MAX || fence < VM_MIN_ADDRESS)
4025     fence = VM_MIN_ADDRESS;
4026
4027   instlen = mips_pc_is_mips (pc) ? MIPS_INSN32_SIZE : MIPS_INSN16_SIZE;
4028
4029   inf = current_inferior ();
4030
4031   /* Search back for previous return.  */
4032   for (start_pc -= instlen;; start_pc -= instlen)
4033     if (start_pc < fence)
4034       {
4035         /* It's not clear to me why we reach this point when
4036            stop_soon, but with this test, at least we
4037            don't print out warnings for every child forked (eg, on
4038            decstation).  22apr93 rich@cygnus.com.  */
4039         if (inf->control.stop_soon == NO_STOP_QUIETLY)
4040           {
4041             static int blurb_printed = 0;
4042
4043             warning (_("GDB can't find the start of the function at %s."),
4044                      paddress (gdbarch, pc));
4045
4046             if (!blurb_printed)
4047               {
4048                 /* This actually happens frequently in embedded
4049                    development, when you first connect to a board
4050                    and your stack pointer and pc are nowhere in
4051                    particular.  This message needs to give people
4052                    in that situation enough information to
4053                    determine that it's no big deal.  */
4054                 printf_filtered ("\n\
4055     GDB is unable to find the start of the function at %s\n\
4056 and thus can't determine the size of that function's stack frame.\n\
4057 This means that GDB may be unable to access that stack frame, or\n\
4058 the frames below it.\n\
4059     This problem is most likely caused by an invalid program counter or\n\
4060 stack pointer.\n\
4061     However, if you think GDB should simply search farther back\n\
4062 from %s for code which looks like the beginning of a\n\
4063 function, you can increase the range of the search using the `set\n\
4064 heuristic-fence-post' command.\n",
4065                         paddress (gdbarch, pc), paddress (gdbarch, pc));
4066                 blurb_printed = 1;
4067               }
4068           }
4069
4070         return 0;
4071       }
4072     else if (mips_pc_is_mips16 (gdbarch, start_pc))
4073       {
4074         unsigned short inst;
4075
4076         /* On MIPS16, any one of the following is likely to be the
4077            start of a function:
4078            extend save
4079            save
4080            entry
4081            addiu sp,-n
4082            daddiu sp,-n
4083            extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'.  */
4084         inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, start_pc, NULL);
4085         if ((inst & 0xff80) == 0x6480)          /* save */
4086           {
4087             if (start_pc - instlen >= fence)
4088               {
4089                 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16,
4090                                                start_pc - instlen, NULL);
4091                 if ((inst & 0xf800) == 0xf000)  /* extend */
4092                   start_pc -= instlen;
4093               }
4094             break;
4095           }
4096         else if (((inst & 0xf81f) == 0xe809
4097                   && (inst & 0x700) != 0x700)   /* entry */
4098                  || (inst & 0xff80) == 0x6380   /* addiu sp,-n */
4099                  || (inst & 0xff80) == 0xfb80   /* daddiu sp,-n */
4100                  || ((inst & 0xf810) == 0xf010 && seen_adjsp))  /* extend -n */
4101           break;
4102         else if ((inst & 0xff00) == 0x6300      /* addiu sp */
4103                  || (inst & 0xff00) == 0xfb00)  /* daddiu sp */
4104           seen_adjsp = 1;
4105         else
4106           seen_adjsp = 0;
4107       }
4108     else if (mips_pc_is_micromips (gdbarch, start_pc))
4109       {
4110         ULONGEST insn;
4111         int stop = 0;
4112         long offset;
4113         int dreg;
4114         int sreg;
4115
4116         /* On microMIPS, any one of the following is likely to be the
4117            start of a function:
4118            ADDIUSP -imm
4119            (D)ADDIU $sp, -imm
4120            LUI $gp, imm  */
4121         insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
4122         switch (micromips_op (insn))
4123           {
4124           case 0xc: /* ADDIU: bits 001100 */
4125           case 0x17: /* DADDIU: bits 010111 */
4126             sreg = b0s5_reg (insn);
4127             dreg = b5s5_reg (insn);
4128             insn <<= 16;
4129             insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS,
4130                                             pc + MIPS_INSN16_SIZE, NULL);
4131             offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
4132             if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
4133                                 /* (D)ADDIU $sp, imm */
4134                 && offset < 0)
4135               stop = 1;
4136             break;
4137
4138           case 0x10: /* POOL32I: bits 010000 */
4139             if (b5s5_op (insn) == 0xd
4140                                 /* LUI: bits 010000 001101 */
4141                 && b0s5_reg (insn >> 16) == 28)
4142                                 /* LUI $gp, imm */
4143               stop = 1;
4144             break;
4145
4146           case 0x13: /* POOL16D: bits 010011 */
4147             if ((insn & 0x1) == 0x1)
4148                                 /* ADDIUSP: bits 010011 1 */
4149               {
4150                 offset = micromips_decode_imm9 (b1s9_imm (insn));
4151                 if (offset < 0)
4152                                 /* ADDIUSP -imm */
4153                   stop = 1;
4154               }
4155             else
4156                                 /* ADDIUS5: bits 010011 0 */
4157               {
4158                 dreg = b5s5_reg (insn);
4159                 offset = (b1s4_imm (insn) ^ 8) - 8;
4160                 if (dreg == MIPS_SP_REGNUM && offset < 0)
4161                                 /* ADDIUS5  $sp, -imm */
4162                   stop = 1;
4163               }
4164             break;
4165           }
4166         if (stop)
4167           break;
4168       }
4169     else if (mips_about_to_return (gdbarch, start_pc))
4170       {
4171         /* Skip return and its delay slot.  */
4172         start_pc += 2 * MIPS_INSN32_SIZE;
4173         break;
4174       }
4175
4176   return start_pc;
4177 }
4178
4179 struct mips_objfile_private
4180 {
4181   bfd_size_type size;
4182   char *contents;
4183 };
4184
4185 /* According to the current ABI, should the type be passed in a
4186    floating-point register (assuming that there is space)?  When there
4187    is no FPU, FP are not even considered as possible candidates for
4188    FP registers and, consequently this returns false - forces FP
4189    arguments into integer registers.  */
4190
4191 static int
4192 fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
4193                    struct type *arg_type)
4194 {
4195   return ((typecode == TYPE_CODE_FLT
4196            || (MIPS_EABI (gdbarch)
4197                && (typecode == TYPE_CODE_STRUCT
4198                    || typecode == TYPE_CODE_UNION)
4199                && TYPE_NFIELDS (arg_type) == 1
4200                && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0))) 
4201                == TYPE_CODE_FLT))
4202           && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
4203 }
4204
4205 /* On o32, argument passing in GPRs depends on the alignment of the type being
4206    passed.  Return 1 if this type must be aligned to a doubleword boundary.  */
4207
4208 static int
4209 mips_type_needs_double_align (struct type *type)
4210 {
4211   enum type_code typecode = TYPE_CODE (type);
4212
4213   if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
4214     return 1;
4215   else if (typecode == TYPE_CODE_STRUCT)
4216     {
4217       if (TYPE_NFIELDS (type) < 1)
4218         return 0;
4219       return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
4220     }
4221   else if (typecode == TYPE_CODE_UNION)
4222     {
4223       int i, n;
4224
4225       n = TYPE_NFIELDS (type);
4226       for (i = 0; i < n; i++)
4227         if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
4228           return 1;
4229       return 0;
4230     }
4231   return 0;
4232 }
4233
4234 /* Adjust the address downward (direction of stack growth) so that it
4235    is correctly aligned for a new stack frame.  */
4236 static CORE_ADDR
4237 mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
4238 {
4239   return align_down (addr, 16);
4240 }
4241
4242 /* Implement the "push_dummy_code" gdbarch method.  */
4243
4244 static CORE_ADDR
4245 mips_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
4246                       CORE_ADDR funaddr, struct value **args,
4247                       int nargs, struct type *value_type,
4248                       CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
4249                       struct regcache *regcache)
4250 {
4251   static gdb_byte nop_insn[] = { 0, 0, 0, 0 };
4252   CORE_ADDR nop_addr;
4253   CORE_ADDR bp_slot;
4254
4255   /* Reserve enough room on the stack for our breakpoint instruction.  */
4256   bp_slot = sp - sizeof (nop_insn);
4257
4258   /* Return to microMIPS mode if calling microMIPS code to avoid
4259      triggering an address error exception on processors that only
4260      support microMIPS execution.  */
4261   *bp_addr = (mips_pc_is_micromips (gdbarch, funaddr)
4262               ? make_compact_addr (bp_slot) : bp_slot);
4263
4264   /* The breakpoint layer automatically adjusts the address of
4265      breakpoints inserted in a branch delay slot.  With enough
4266      bad luck, the 4 bytes located just before our breakpoint
4267      instruction could look like a branch instruction, and thus
4268      trigger the adjustement, and break the function call entirely.
4269      So, we reserve those 4 bytes and write a nop instruction
4270      to prevent that from happening.  */
4271   nop_addr = bp_slot - sizeof (nop_insn);
4272   write_memory (nop_addr, nop_insn, sizeof (nop_insn));
4273   sp = mips_frame_align (gdbarch, nop_addr);
4274
4275   /* Inferior resumes at the function entry point.  */
4276   *real_pc = funaddr;
4277
4278   return sp;
4279 }
4280
4281 static CORE_ADDR
4282 mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4283                            struct regcache *regcache, CORE_ADDR bp_addr,
4284                            int nargs, struct value **args, CORE_ADDR sp,
4285                            int struct_return, CORE_ADDR struct_addr)
4286 {
4287   int argreg;
4288   int float_argreg;
4289   int argnum;
4290   int len = 0;
4291   int stack_offset = 0;
4292   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4293   CORE_ADDR func_addr = find_function_addr (function, NULL);
4294   int regsize = mips_abi_regsize (gdbarch);
4295
4296   /* For shared libraries, "t9" needs to point at the function
4297      address.  */
4298   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
4299
4300   /* Set the return address register to point to the entry point of
4301      the program, where a breakpoint lies in wait.  */
4302   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
4303
4304   /* First ensure that the stack and structure return address (if any)
4305      are properly aligned.  The stack has to be at least 64-bit
4306      aligned even on 32-bit machines, because doubles must be 64-bit
4307      aligned.  For n32 and n64, stack frames need to be 128-bit
4308      aligned, so we round to this widest known alignment.  */
4309
4310   sp = align_down (sp, 16);
4311   struct_addr = align_down (struct_addr, 16);
4312
4313   /* Now make space on the stack for the args.  We allocate more
4314      than necessary for EABI, because the first few arguments are
4315      passed in registers, but that's OK.  */
4316   for (argnum = 0; argnum < nargs; argnum++)
4317     len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
4318   sp -= align_up (len, 16);
4319
4320   if (mips_debug)
4321     fprintf_unfiltered (gdb_stdlog,
4322                         "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
4323                         paddress (gdbarch, sp), (long) align_up (len, 16));
4324
4325   /* Initialize the integer and float register pointers.  */
4326   argreg = MIPS_A0_REGNUM;
4327   float_argreg = mips_fpa0_regnum (gdbarch);
4328
4329   /* The struct_return pointer occupies the first parameter-passing reg.  */
4330   if (struct_return)
4331     {
4332       if (mips_debug)
4333         fprintf_unfiltered (gdb_stdlog,
4334                             "mips_eabi_push_dummy_call: "
4335                             "struct_return reg=%d %s\n",
4336                             argreg, paddress (gdbarch, struct_addr));
4337       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
4338     }
4339
4340   /* Now load as many as possible of the first arguments into
4341      registers, and push the rest onto the stack.  Loop thru args
4342      from first to last.  */
4343   for (argnum = 0; argnum < nargs; argnum++)
4344     {
4345       const gdb_byte *val;
4346       gdb_byte valbuf[MAX_REGISTER_SIZE];
4347       struct value *arg = args[argnum];
4348       struct type *arg_type = check_typedef (value_type (arg));
4349       int len = TYPE_LENGTH (arg_type);
4350       enum type_code typecode = TYPE_CODE (arg_type);
4351
4352       if (mips_debug)
4353         fprintf_unfiltered (gdb_stdlog,
4354                             "mips_eabi_push_dummy_call: %d len=%d type=%d",
4355                             argnum + 1, len, (int) typecode);
4356
4357       /* Function pointer arguments to mips16 code need to be made into
4358          mips16 pointers.  */
4359       if (typecode == TYPE_CODE_PTR
4360           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
4361         {
4362           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
4363                                                    len, byte_order);
4364           if (mips_pc_is_mips (addr))
4365             val = value_contents (arg);
4366           else
4367             {
4368               store_signed_integer (valbuf, len, byte_order, 
4369                                     make_compact_addr (addr));
4370               val = valbuf;
4371             }
4372         }
4373       /* The EABI passes structures that do not fit in a register by
4374          reference.  */
4375       else if (len > regsize
4376           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
4377         {
4378           store_unsigned_integer (valbuf, regsize, byte_order,
4379                                   value_address (arg));
4380           typecode = TYPE_CODE_PTR;
4381           len = regsize;
4382           val = valbuf;
4383           if (mips_debug)
4384             fprintf_unfiltered (gdb_stdlog, " push");
4385         }
4386       else
4387         val = value_contents (arg);
4388
4389       /* 32-bit ABIs always start floating point arguments in an
4390          even-numbered floating point register.  Round the FP register
4391          up before the check to see if there are any FP registers
4392          left.  Non MIPS_EABI targets also pass the FP in the integer
4393          registers so also round up normal registers.  */
4394       if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
4395         {
4396           if ((float_argreg & 1))
4397             float_argreg++;
4398         }
4399
4400       /* Floating point arguments passed in registers have to be
4401          treated specially.  On 32-bit architectures, doubles
4402          are passed in register pairs; the even register gets
4403          the low word, and the odd register gets the high word.
4404          On non-EABI processors, the first two floating point arguments are
4405          also copied to general registers, because MIPS16 functions
4406          don't use float registers for arguments.  This duplication of
4407          arguments in general registers can't hurt non-MIPS16 functions
4408          because those registers are normally skipped.  */
4409       /* MIPS_EABI squeezes a struct that contains a single floating
4410          point value into an FP register instead of pushing it onto the
4411          stack.  */
4412       if (fp_register_arg_p (gdbarch, typecode, arg_type)
4413           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
4414         {
4415           /* EABI32 will pass doubles in consecutive registers, even on
4416              64-bit cores.  At one time, we used to check the size of
4417              `float_argreg' to determine whether or not to pass doubles
4418              in consecutive registers, but this is not sufficient for
4419              making the ABI determination.  */
4420           if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
4421             {
4422               int low_offset = gdbarch_byte_order (gdbarch)
4423                                == BFD_ENDIAN_BIG ? 4 : 0;
4424               long regval;
4425
4426               /* Write the low word of the double to the even register(s).  */
4427               regval = extract_signed_integer (val + low_offset,
4428                                                4, byte_order);
4429               if (mips_debug)
4430                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4431                                     float_argreg, phex (regval, 4));
4432               regcache_cooked_write_signed (regcache, float_argreg++, regval);
4433
4434               /* Write the high word of the double to the odd register(s).  */
4435               regval = extract_signed_integer (val + 4 - low_offset,
4436                                                4, byte_order);
4437               if (mips_debug)
4438                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4439                                     float_argreg, phex (regval, 4));
4440               regcache_cooked_write_signed (regcache, float_argreg++, regval);
4441             }
4442           else
4443             {
4444               /* This is a floating point value that fits entirely
4445                  in a single register.  */
4446               /* On 32 bit ABI's the float_argreg is further adjusted
4447                  above to ensure that it is even register aligned.  */
4448               LONGEST regval = extract_signed_integer (val, len, byte_order);
4449               if (mips_debug)
4450                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4451                                     float_argreg, phex (regval, len));
4452               regcache_cooked_write_signed (regcache, float_argreg++, regval);
4453             }
4454         }
4455       else
4456         {
4457           /* Copy the argument to general registers or the stack in
4458              register-sized pieces.  Large arguments are split between
4459              registers and stack.  */
4460           /* Note: structs whose size is not a multiple of regsize
4461              are treated specially: Irix cc passes
4462              them in registers where gcc sometimes puts them on the
4463              stack.  For maximum compatibility, we will put them in
4464              both places.  */
4465           int odd_sized_struct = (len > regsize && len % regsize != 0);
4466
4467           /* Note: Floating-point values that didn't fit into an FP
4468              register are only written to memory.  */
4469           while (len > 0)
4470             {
4471               /* Remember if the argument was written to the stack.  */
4472               int stack_used_p = 0;
4473               int partial_len = (len < regsize ? len : regsize);
4474
4475               if (mips_debug)
4476                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4477                                     partial_len);
4478
4479               /* Write this portion of the argument to the stack.  */
4480               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
4481                   || odd_sized_struct
4482                   || fp_register_arg_p (gdbarch, typecode, arg_type))
4483                 {
4484                   /* Should shorter than int integer values be
4485                      promoted to int before being stored?  */
4486                   int longword_offset = 0;
4487                   CORE_ADDR addr;
4488                   stack_used_p = 1;
4489                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4490                     {
4491                       if (regsize == 8
4492                           && (typecode == TYPE_CODE_INT
4493                               || typecode == TYPE_CODE_PTR
4494                               || typecode == TYPE_CODE_FLT) && len <= 4)
4495                         longword_offset = regsize - len;
4496                       else if ((typecode == TYPE_CODE_STRUCT
4497                                 || typecode == TYPE_CODE_UNION)
4498                                && TYPE_LENGTH (arg_type) < regsize)
4499                         longword_offset = regsize - len;
4500                     }
4501
4502                   if (mips_debug)
4503                     {
4504                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4505                                           paddress (gdbarch, stack_offset));
4506                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4507                                           paddress (gdbarch, longword_offset));
4508                     }
4509
4510                   addr = sp + stack_offset + longword_offset;
4511
4512                   if (mips_debug)
4513                     {
4514                       int i;
4515                       fprintf_unfiltered (gdb_stdlog, " @%s ",
4516                                           paddress (gdbarch, addr));
4517                       for (i = 0; i < partial_len; i++)
4518                         {
4519                           fprintf_unfiltered (gdb_stdlog, "%02x",
4520                                               val[i] & 0xff);
4521                         }
4522                     }
4523                   write_memory (addr, val, partial_len);
4524                 }
4525
4526               /* Note!!! This is NOT an else clause.  Odd sized
4527                  structs may go thru BOTH paths.  Floating point
4528                  arguments will not.  */
4529               /* Write this portion of the argument to a general
4530                  purpose register.  */
4531               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
4532                   && !fp_register_arg_p (gdbarch, typecode, arg_type))
4533                 {
4534                   LONGEST regval =
4535                     extract_signed_integer (val, partial_len, byte_order);
4536
4537                   if (mips_debug)
4538                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4539                                       argreg,
4540                                       phex (regval, regsize));
4541                   regcache_cooked_write_signed (regcache, argreg, regval);
4542                   argreg++;
4543                 }
4544
4545               len -= partial_len;
4546               val += partial_len;
4547
4548               /* Compute the offset into the stack at which we will
4549                  copy the next parameter.
4550
4551                  In the new EABI (and the NABI32), the stack_offset
4552                  only needs to be adjusted when it has been used.  */
4553
4554               if (stack_used_p)
4555                 stack_offset += align_up (partial_len, regsize);
4556             }
4557         }
4558       if (mips_debug)
4559         fprintf_unfiltered (gdb_stdlog, "\n");
4560     }
4561
4562   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
4563
4564   /* Return adjusted stack pointer.  */
4565   return sp;
4566 }
4567
4568 /* Determine the return value convention being used.  */
4569
4570 static enum return_value_convention
4571 mips_eabi_return_value (struct gdbarch *gdbarch, struct value *function,
4572                         struct type *type, struct regcache *regcache,
4573                         gdb_byte *readbuf, const gdb_byte *writebuf)
4574 {
4575   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4576   int fp_return_type = 0;
4577   int offset, regnum, xfer;
4578
4579   if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
4580     return RETURN_VALUE_STRUCT_CONVENTION;
4581
4582   /* Floating point type?  */
4583   if (tdep->mips_fpu_type != MIPS_FPU_NONE)
4584     {
4585       if (TYPE_CODE (type) == TYPE_CODE_FLT)
4586         fp_return_type = 1;
4587       /* Structs with a single field of float type 
4588          are returned in a floating point register.  */
4589       if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
4590            || TYPE_CODE (type) == TYPE_CODE_UNION)
4591           && TYPE_NFIELDS (type) == 1)
4592         {
4593           struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);
4594
4595           if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
4596             fp_return_type = 1;
4597         }
4598     }
4599
4600   if (fp_return_type)      
4601     {
4602       /* A floating-point value belongs in the least significant part
4603          of FP0/FP1.  */
4604       if (mips_debug)
4605         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4606       regnum = mips_regnum (gdbarch)->fp0;
4607     }
4608   else 
4609     {
4610       /* An integer value goes in V0/V1.  */
4611       if (mips_debug)
4612         fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
4613       regnum = MIPS_V0_REGNUM;
4614     }
4615   for (offset = 0;
4616        offset < TYPE_LENGTH (type);
4617        offset += mips_abi_regsize (gdbarch), regnum++)
4618     {
4619       xfer = mips_abi_regsize (gdbarch);
4620       if (offset + xfer > TYPE_LENGTH (type))
4621         xfer = TYPE_LENGTH (type) - offset;
4622       mips_xfer_register (gdbarch, regcache,
4623                           gdbarch_num_regs (gdbarch) + regnum, xfer,
4624                           gdbarch_byte_order (gdbarch), readbuf, writebuf,
4625                           offset);
4626     }
4627
4628   return RETURN_VALUE_REGISTER_CONVENTION;
4629 }
4630
4631
4632 /* N32/N64 ABI stuff.  */
4633
4634 /* Search for a naturally aligned double at OFFSET inside a struct
4635    ARG_TYPE.  The N32 / N64 ABIs pass these in floating point
4636    registers.  */
4637
4638 static int
4639 mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
4640                             int offset)
4641 {
4642   int i;
4643
4644   if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
4645     return 0;
4646
4647   if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
4648     return 0;
4649
4650   if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
4651     return 0;
4652
4653   for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
4654     {
4655       int pos;
4656       struct type *field_type;
4657
4658       /* We're only looking at normal fields.  */
4659       if (field_is_static (&TYPE_FIELD (arg_type, i))
4660           || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
4661         continue;
4662
4663       /* If we have gone past the offset, there is no double to pass.  */
4664       pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
4665       if (pos > offset)
4666         return 0;
4667
4668       field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));
4669
4670       /* If this field is entirely before the requested offset, go
4671          on to the next one.  */
4672       if (pos + TYPE_LENGTH (field_type) <= offset)
4673         continue;
4674
4675       /* If this is our special aligned double, we can stop.  */
4676       if (TYPE_CODE (field_type) == TYPE_CODE_FLT
4677           && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
4678         return 1;
4679
4680       /* This field starts at or before the requested offset, and
4681          overlaps it.  If it is a structure, recurse inwards.  */
4682       return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
4683     }
4684
4685   return 0;
4686 }
4687
4688 static CORE_ADDR
4689 mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4690                              struct regcache *regcache, CORE_ADDR bp_addr,
4691                              int nargs, struct value **args, CORE_ADDR sp,
4692                              int struct_return, CORE_ADDR struct_addr)
4693 {
4694   int argreg;
4695   int float_argreg;
4696   int argnum;
4697   int len = 0;
4698   int stack_offset = 0;
4699   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4700   CORE_ADDR func_addr = find_function_addr (function, NULL);
4701
4702   /* For shared libraries, "t9" needs to point at the function
4703      address.  */
4704   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
4705
4706   /* Set the return address register to point to the entry point of
4707      the program, where a breakpoint lies in wait.  */
4708   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
4709
4710   /* First ensure that the stack and structure return address (if any)
4711      are properly aligned.  The stack has to be at least 64-bit
4712      aligned even on 32-bit machines, because doubles must be 64-bit
4713      aligned.  For n32 and n64, stack frames need to be 128-bit
4714      aligned, so we round to this widest known alignment.  */
4715
4716   sp = align_down (sp, 16);
4717   struct_addr = align_down (struct_addr, 16);
4718
4719   /* Now make space on the stack for the args.  */
4720   for (argnum = 0; argnum < nargs; argnum++)
4721     len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
4722   sp -= align_up (len, 16);
4723
4724   if (mips_debug)
4725     fprintf_unfiltered (gdb_stdlog,
4726                         "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
4727                         paddress (gdbarch, sp), (long) align_up (len, 16));
4728
4729   /* Initialize the integer and float register pointers.  */
4730   argreg = MIPS_A0_REGNUM;
4731   float_argreg = mips_fpa0_regnum (gdbarch);
4732
4733   /* The struct_return pointer occupies the first parameter-passing reg.  */
4734   if (struct_return)
4735     {
4736       if (mips_debug)
4737         fprintf_unfiltered (gdb_stdlog,
4738                             "mips_n32n64_push_dummy_call: "
4739                             "struct_return reg=%d %s\n",
4740                             argreg, paddress (gdbarch, struct_addr));
4741       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
4742     }
4743
4744   /* Now load as many as possible of the first arguments into
4745      registers, and push the rest onto the stack.  Loop thru args
4746      from first to last.  */
4747   for (argnum = 0; argnum < nargs; argnum++)
4748     {
4749       const gdb_byte *val;
4750       struct value *arg = args[argnum];
4751       struct type *arg_type = check_typedef (value_type (arg));
4752       int len = TYPE_LENGTH (arg_type);
4753       enum type_code typecode = TYPE_CODE (arg_type);
4754
4755       if (mips_debug)
4756         fprintf_unfiltered (gdb_stdlog,
4757                             "mips_n32n64_push_dummy_call: %d len=%d type=%d",
4758                             argnum + 1, len, (int) typecode);
4759
4760       val = value_contents (arg);
4761
4762       /* A 128-bit long double value requires an even-odd pair of
4763          floating-point registers.  */
4764       if (len == 16
4765           && fp_register_arg_p (gdbarch, typecode, arg_type)
4766           && (float_argreg & 1))
4767         {
4768           float_argreg++;
4769           argreg++;
4770         }
4771
4772       if (fp_register_arg_p (gdbarch, typecode, arg_type)
4773           && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
4774         {
4775           /* This is a floating point value that fits entirely
4776              in a single register or a pair of registers.  */
4777           int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
4778           LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
4779           if (mips_debug)
4780             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4781                                 float_argreg, phex (regval, reglen));
4782           regcache_cooked_write_unsigned (regcache, float_argreg, regval);
4783
4784           if (mips_debug)
4785             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4786                                 argreg, phex (regval, reglen));
4787           regcache_cooked_write_unsigned (regcache, argreg, regval);
4788           float_argreg++;
4789           argreg++;
4790           if (len == 16)
4791             {
4792               regval = extract_unsigned_integer (val + reglen,
4793                                                  reglen, byte_order);
4794               if (mips_debug)
4795                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4796                                     float_argreg, phex (regval, reglen));
4797               regcache_cooked_write_unsigned (regcache, float_argreg, regval);
4798
4799               if (mips_debug)
4800                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4801                                     argreg, phex (regval, reglen));
4802               regcache_cooked_write_unsigned (regcache, argreg, regval);
4803               float_argreg++;
4804               argreg++;
4805             }
4806         }
4807       else
4808         {
4809           /* Copy the argument to general registers or the stack in
4810              register-sized pieces.  Large arguments are split between
4811              registers and stack.  */
4812           /* For N32/N64, structs, unions, or other composite types are
4813              treated as a sequence of doublewords, and are passed in integer
4814              or floating point registers as though they were simple scalar
4815              parameters to the extent that they fit, with any excess on the
4816              stack packed according to the normal memory layout of the
4817              object.
4818              The caller does not reserve space for the register arguments;
4819              the callee is responsible for reserving it if required.  */
4820           /* Note: Floating-point values that didn't fit into an FP
4821              register are only written to memory.  */
4822           while (len > 0)
4823             {
4824               /* Remember if the argument was written to the stack.  */
4825               int stack_used_p = 0;
4826               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
4827
4828               if (mips_debug)
4829                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4830                                     partial_len);
4831
4832               if (fp_register_arg_p (gdbarch, typecode, arg_type))
4833                 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));
4834
4835               /* Write this portion of the argument to the stack.  */
4836               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
4837                 {
4838                   /* Should shorter than int integer values be
4839                      promoted to int before being stored?  */
4840                   int longword_offset = 0;
4841                   CORE_ADDR addr;
4842                   stack_used_p = 1;
4843                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4844                     {
4845                       if ((typecode == TYPE_CODE_INT
4846                            || typecode == TYPE_CODE_PTR)
4847                           && len <= 4)
4848                         longword_offset = MIPS64_REGSIZE - len;
4849                     }
4850
4851                   if (mips_debug)
4852                     {
4853                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4854                                           paddress (gdbarch, stack_offset));
4855                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4856                                           paddress (gdbarch, longword_offset));
4857                     }
4858
4859                   addr = sp + stack_offset + longword_offset;
4860
4861                   if (mips_debug)
4862                     {
4863                       int i;
4864                       fprintf_unfiltered (gdb_stdlog, " @%s ",
4865                                           paddress (gdbarch, addr));
4866                       for (i = 0; i < partial_len; i++)
4867                         {
4868                           fprintf_unfiltered (gdb_stdlog, "%02x",
4869                                               val[i] & 0xff);
4870                         }
4871                     }
4872                   write_memory (addr, val, partial_len);
4873                 }
4874
4875               /* Note!!! This is NOT an else clause.  Odd sized
4876                  structs may go thru BOTH paths.  */
4877               /* Write this portion of the argument to a general
4878                  purpose register.  */
4879               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
4880                 {
4881                   LONGEST regval;
4882
4883                   /* Sign extend pointers, 32-bit integers and signed
4884                      16-bit and 8-bit integers; everything else is taken
4885                      as is.  */
4886
4887                   if ((partial_len == 4
4888                        && (typecode == TYPE_CODE_PTR
4889                            || typecode == TYPE_CODE_INT))
4890                       || (partial_len < 4
4891                           && typecode == TYPE_CODE_INT
4892                           && !TYPE_UNSIGNED (arg_type)))
4893                     regval = extract_signed_integer (val, partial_len,
4894                                                      byte_order);
4895                   else
4896                     regval = extract_unsigned_integer (val, partial_len,
4897                                                        byte_order);
4898
4899                   /* A non-floating-point argument being passed in a
4900                      general register.  If a struct or union, and if
4901                      the remaining length is smaller than the register
4902                      size, we have to adjust the register value on
4903                      big endian targets.
4904
4905                      It does not seem to be necessary to do the
4906                      same for integral types.  */
4907
4908                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
4909                       && partial_len < MIPS64_REGSIZE
4910                       && (typecode == TYPE_CODE_STRUCT
4911                           || typecode == TYPE_CODE_UNION))
4912                     regval <<= ((MIPS64_REGSIZE - partial_len)
4913                                 * TARGET_CHAR_BIT);
4914
4915                   if (mips_debug)
4916                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4917                                       argreg,
4918                                       phex (regval, MIPS64_REGSIZE));
4919                   regcache_cooked_write_unsigned (regcache, argreg, regval);
4920
4921                   if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
4922                                                   TYPE_LENGTH (arg_type) - len))
4923                     {
4924                       if (mips_debug)
4925                         fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
4926                                           float_argreg,
4927                                           phex (regval, MIPS64_REGSIZE));
4928                       regcache_cooked_write_unsigned (regcache, float_argreg,
4929                                                       regval);
4930                     }
4931
4932                   float_argreg++;
4933                   argreg++;
4934                 }
4935
4936               len -= partial_len;
4937               val += partial_len;
4938
4939               /* Compute the offset into the stack at which we will
4940                  copy the next parameter.
4941
4942                  In N32 (N64?), the stack_offset only needs to be
4943                  adjusted when it has been used.  */
4944
4945               if (stack_used_p)
4946                 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
4947             }
4948         }
4949       if (mips_debug)
4950         fprintf_unfiltered (gdb_stdlog, "\n");
4951     }
4952
4953   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
4954
4955   /* Return adjusted stack pointer.  */
4956   return sp;
4957 }
4958
4959 static enum return_value_convention
4960 mips_n32n64_return_value (struct gdbarch *gdbarch, struct value *function,
4961                           struct type *type, struct regcache *regcache,
4962                           gdb_byte *readbuf, const gdb_byte *writebuf)
4963 {
4964   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4965
4966   /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004
4967
4968      Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
4969      if needed), as appropriate for the type.  Composite results (struct,
4970      union, or array) are returned in $2/$f0 and $3/$f2 according to the
4971      following rules:
4972
4973      * A struct with only one or two floating point fields is returned in $f0
4974      (and $f2 if necessary).  This is a generalization of the Fortran COMPLEX
4975      case.
4976
4977      * Any other composite results of at most 128 bits are returned in
4978      $2 (first 64 bits) and $3 (remainder, if necessary).
4979
4980      * Larger composite results are handled by converting the function to a
4981      procedure with an implicit first parameter, which is a pointer to an area
4982      reserved by the caller to receive the result.  [The o32-bit ABI requires
4983      that all composite results be handled by conversion to implicit first
4984      parameters.  The MIPS/SGI Fortran implementation has always made a
4985      specific exception to return COMPLEX results in the floating point
4986      registers.]  */
4987
4988   if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
4989     return RETURN_VALUE_STRUCT_CONVENTION;
4990   else if (TYPE_CODE (type) == TYPE_CODE_FLT
4991            && TYPE_LENGTH (type) == 16
4992            && tdep->mips_fpu_type != MIPS_FPU_NONE)
4993     {
4994       /* A 128-bit floating-point value fills both $f0 and $f2.  The
4995          two registers are used in the same as memory order, so the
4996          eight bytes with the lower memory address are in $f0.  */
4997       if (mips_debug)
4998         fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
4999       mips_xfer_register (gdbarch, regcache,
5000                           (gdbarch_num_regs (gdbarch)
5001                            + mips_regnum (gdbarch)->fp0),
5002                           8, gdbarch_byte_order (gdbarch),
5003                           readbuf, writebuf, 0);
5004       mips_xfer_register (gdbarch, regcache,
5005                           (gdbarch_num_regs (gdbarch)
5006                            + mips_regnum (gdbarch)->fp0 + 2),
5007                           8, gdbarch_byte_order (gdbarch),
5008                           readbuf ? readbuf + 8 : readbuf,
5009                           writebuf ? writebuf + 8 : writebuf, 0);
5010       return RETURN_VALUE_REGISTER_CONVENTION;
5011     }
5012   else if (TYPE_CODE (type) == TYPE_CODE_FLT
5013            && tdep->mips_fpu_type != MIPS_FPU_NONE)
5014     {
5015       /* A single or double floating-point value that fits in FP0.  */
5016       if (mips_debug)
5017         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
5018       mips_xfer_register (gdbarch, regcache,
5019                           (gdbarch_num_regs (gdbarch)
5020                            + mips_regnum (gdbarch)->fp0),
5021                           TYPE_LENGTH (type),
5022                           gdbarch_byte_order (gdbarch),
5023                           readbuf, writebuf, 0);
5024       return RETURN_VALUE_REGISTER_CONVENTION;
5025     }
5026   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5027            && TYPE_NFIELDS (type) <= 2
5028            && TYPE_NFIELDS (type) >= 1
5029            && ((TYPE_NFIELDS (type) == 1
5030                 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
5031                     == TYPE_CODE_FLT))
5032                || (TYPE_NFIELDS (type) == 2
5033                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
5034                        == TYPE_CODE_FLT)
5035                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
5036                        == TYPE_CODE_FLT))))
5037     {
5038       /* A struct that contains one or two floats.  Each value is part
5039          in the least significant part of their floating point
5040          register (or GPR, for soft float).  */
5041       int regnum;
5042       int field;
5043       for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
5044                                 ? mips_regnum (gdbarch)->fp0
5045                                 : MIPS_V0_REGNUM);
5046            field < TYPE_NFIELDS (type); field++, regnum += 2)
5047         {
5048           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
5049                         / TARGET_CHAR_BIT);
5050           if (mips_debug)
5051             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
5052                                 offset);
5053           if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
5054             {
5055               /* A 16-byte long double field goes in two consecutive
5056                  registers.  */
5057               mips_xfer_register (gdbarch, regcache,
5058                                   gdbarch_num_regs (gdbarch) + regnum,
5059                                   8,
5060                                   gdbarch_byte_order (gdbarch),
5061                                   readbuf, writebuf, offset);
5062               mips_xfer_register (gdbarch, regcache,
5063                                   gdbarch_num_regs (gdbarch) + regnum + 1,
5064                                   8,
5065                                   gdbarch_byte_order (gdbarch),
5066                                   readbuf, writebuf, offset + 8);
5067             }
5068           else
5069             mips_xfer_register (gdbarch, regcache,
5070                                 gdbarch_num_regs (gdbarch) + regnum,
5071                                 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
5072                                 gdbarch_byte_order (gdbarch),
5073                                 readbuf, writebuf, offset);
5074         }
5075       return RETURN_VALUE_REGISTER_CONVENTION;
5076     }
5077   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5078            || TYPE_CODE (type) == TYPE_CODE_UNION
5079            || TYPE_CODE (type) == TYPE_CODE_ARRAY)
5080     {
5081       /* A composite type.  Extract the left justified value,
5082          regardless of the byte order.  I.e. DO NOT USE
5083          mips_xfer_lower.  */
5084       int offset;
5085       int regnum;
5086       for (offset = 0, regnum = MIPS_V0_REGNUM;
5087            offset < TYPE_LENGTH (type);
5088            offset += register_size (gdbarch, regnum), regnum++)
5089         {
5090           int xfer = register_size (gdbarch, regnum);
5091           if (offset + xfer > TYPE_LENGTH (type))
5092             xfer = TYPE_LENGTH (type) - offset;
5093           if (mips_debug)
5094             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
5095                                 offset, xfer, regnum);
5096           mips_xfer_register (gdbarch, regcache,
5097                               gdbarch_num_regs (gdbarch) + regnum,
5098                               xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
5099                               offset);
5100         }
5101       return RETURN_VALUE_REGISTER_CONVENTION;
5102     }
5103   else
5104     {
5105       /* A scalar extract each part but least-significant-byte
5106          justified.  */
5107       int offset;
5108       int regnum;
5109       for (offset = 0, regnum = MIPS_V0_REGNUM;
5110            offset < TYPE_LENGTH (type);
5111            offset += register_size (gdbarch, regnum), regnum++)
5112         {
5113           int xfer = register_size (gdbarch, regnum);
5114           if (offset + xfer > TYPE_LENGTH (type))
5115             xfer = TYPE_LENGTH (type) - offset;
5116           if (mips_debug)
5117             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
5118                                 offset, xfer, regnum);
5119           mips_xfer_register (gdbarch, regcache,
5120                               gdbarch_num_regs (gdbarch) + regnum,
5121                               xfer, gdbarch_byte_order (gdbarch),
5122                               readbuf, writebuf, offset);
5123         }
5124       return RETURN_VALUE_REGISTER_CONVENTION;
5125     }
5126 }
5127
5128 /* Which registers to use for passing floating-point values between
5129    function calls, one of floating-point, general and both kinds of
5130    registers.  O32 and O64 use different register kinds for standard
5131    MIPS and MIPS16 code; to make the handling of cases where we may
5132    not know what kind of code is being used (e.g. no debug information)
5133    easier we sometimes use both kinds.  */
5134
5135 enum mips_fval_reg
5136 {
5137   mips_fval_fpr,
5138   mips_fval_gpr,
5139   mips_fval_both
5140 };
5141
5142 /* O32 ABI stuff.  */
5143
5144 static CORE_ADDR
5145 mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
5146                           struct regcache *regcache, CORE_ADDR bp_addr,
5147                           int nargs, struct value **args, CORE_ADDR sp,
5148                           int struct_return, CORE_ADDR struct_addr)
5149 {
5150   int argreg;
5151   int float_argreg;
5152   int argnum;
5153   int len = 0;
5154   int stack_offset = 0;
5155   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5156   CORE_ADDR func_addr = find_function_addr (function, NULL);
5157
5158   /* For shared libraries, "t9" needs to point at the function
5159      address.  */
5160   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
5161
5162   /* Set the return address register to point to the entry point of
5163      the program, where a breakpoint lies in wait.  */
5164   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
5165
5166   /* First ensure that the stack and structure return address (if any)
5167      are properly aligned.  The stack has to be at least 64-bit
5168      aligned even on 32-bit machines, because doubles must be 64-bit
5169      aligned.  For n32 and n64, stack frames need to be 128-bit
5170      aligned, so we round to this widest known alignment.  */
5171
5172   sp = align_down (sp, 16);
5173   struct_addr = align_down (struct_addr, 16);
5174
5175   /* Now make space on the stack for the args.  */
5176   for (argnum = 0; argnum < nargs; argnum++)
5177     {
5178       struct type *arg_type = check_typedef (value_type (args[argnum]));
5179
5180       /* Align to double-word if necessary.  */
5181       if (mips_type_needs_double_align (arg_type))
5182         len = align_up (len, MIPS32_REGSIZE * 2);
5183       /* Allocate space on the stack.  */
5184       len += align_up (TYPE_LENGTH (arg_type), MIPS32_REGSIZE);
5185     }
5186   sp -= align_up (len, 16);
5187
5188   if (mips_debug)
5189     fprintf_unfiltered (gdb_stdlog,
5190                         "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
5191                         paddress (gdbarch, sp), (long) align_up (len, 16));
5192
5193   /* Initialize the integer and float register pointers.  */
5194   argreg = MIPS_A0_REGNUM;
5195   float_argreg = mips_fpa0_regnum (gdbarch);
5196
5197   /* The struct_return pointer occupies the first parameter-passing reg.  */
5198   if (struct_return)
5199     {
5200       if (mips_debug)
5201         fprintf_unfiltered (gdb_stdlog,
5202                             "mips_o32_push_dummy_call: "
5203                             "struct_return reg=%d %s\n",
5204                             argreg, paddress (gdbarch, struct_addr));
5205       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
5206       stack_offset += MIPS32_REGSIZE;
5207     }
5208
5209   /* Now load as many as possible of the first arguments into
5210      registers, and push the rest onto the stack.  Loop thru args
5211      from first to last.  */
5212   for (argnum = 0; argnum < nargs; argnum++)
5213     {
5214       const gdb_byte *val;
5215       struct value *arg = args[argnum];
5216       struct type *arg_type = check_typedef (value_type (arg));
5217       int len = TYPE_LENGTH (arg_type);
5218       enum type_code typecode = TYPE_CODE (arg_type);
5219
5220       if (mips_debug)
5221         fprintf_unfiltered (gdb_stdlog,
5222                             "mips_o32_push_dummy_call: %d len=%d type=%d",
5223                             argnum + 1, len, (int) typecode);
5224
5225       val = value_contents (arg);
5226
5227       /* 32-bit ABIs always start floating point arguments in an
5228          even-numbered floating point register.  Round the FP register
5229          up before the check to see if there are any FP registers
5230          left.  O32 targets also pass the FP in the integer registers
5231          so also round up normal registers.  */
5232       if (fp_register_arg_p (gdbarch, typecode, arg_type))
5233         {
5234           if ((float_argreg & 1))
5235             float_argreg++;
5236         }
5237
5238       /* Floating point arguments passed in registers have to be
5239          treated specially.  On 32-bit architectures, doubles are
5240          passed in register pairs; the even FP register gets the
5241          low word, and the odd FP register gets the high word.
5242          On O32, the first two floating point arguments are also
5243          copied to general registers, following their memory order,
5244          because MIPS16 functions don't use float registers for
5245          arguments.  This duplication of arguments in general
5246          registers can't hurt non-MIPS16 functions, because those
5247          registers are normally skipped.  */
5248
5249       if (fp_register_arg_p (gdbarch, typecode, arg_type)
5250           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
5251         {
5252           if (register_size (gdbarch, float_argreg) < 8 && len == 8)
5253             {
5254               int freg_offset = gdbarch_byte_order (gdbarch)
5255                                 == BFD_ENDIAN_BIG ? 1 : 0;
5256               unsigned long regval;
5257
5258               /* First word.  */
5259               regval = extract_unsigned_integer (val, 4, byte_order);
5260               if (mips_debug)
5261                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5262                                     float_argreg + freg_offset,
5263                                     phex (regval, 4));
5264               regcache_cooked_write_unsigned (regcache,
5265                                               float_argreg++ + freg_offset,
5266                                               regval);
5267               if (mips_debug)
5268                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5269                                     argreg, phex (regval, 4));
5270               regcache_cooked_write_unsigned (regcache, argreg++, regval);
5271
5272               /* Second word.  */
5273               regval = extract_unsigned_integer (val + 4, 4, byte_order);
5274               if (mips_debug)
5275                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5276                                     float_argreg - freg_offset,
5277                                     phex (regval, 4));
5278               regcache_cooked_write_unsigned (regcache,
5279                                               float_argreg++ - freg_offset,
5280                                               regval);
5281               if (mips_debug)
5282                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5283                                     argreg, phex (regval, 4));
5284               regcache_cooked_write_unsigned (regcache, argreg++, regval);
5285             }
5286           else
5287             {
5288               /* This is a floating point value that fits entirely
5289                  in a single register.  */
5290               /* On 32 bit ABI's the float_argreg is further adjusted
5291                  above to ensure that it is even register aligned.  */
5292               LONGEST regval = extract_unsigned_integer (val, len, byte_order);
5293               if (mips_debug)
5294                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5295                                     float_argreg, phex (regval, len));
5296               regcache_cooked_write_unsigned (regcache,
5297                                               float_argreg++, regval);
5298               /* Although two FP registers are reserved for each
5299                  argument, only one corresponding integer register is
5300                  reserved.  */
5301               if (mips_debug)
5302                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5303                                     argreg, phex (regval, len));
5304               regcache_cooked_write_unsigned (regcache, argreg++, regval);
5305             }
5306           /* Reserve space for the FP register.  */
5307           stack_offset += align_up (len, MIPS32_REGSIZE);
5308         }
5309       else
5310         {
5311           /* Copy the argument to general registers or the stack in
5312              register-sized pieces.  Large arguments are split between
5313              registers and stack.  */
5314           /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
5315              are treated specially: Irix cc passes
5316              them in registers where gcc sometimes puts them on the
5317              stack.  For maximum compatibility, we will put them in
5318              both places.  */
5319           int odd_sized_struct = (len > MIPS32_REGSIZE
5320                                   && len % MIPS32_REGSIZE != 0);
5321           /* Structures should be aligned to eight bytes (even arg registers)
5322              on MIPS_ABI_O32, if their first member has double precision.  */
5323           if (mips_type_needs_double_align (arg_type))
5324             {
5325               if ((argreg & 1))
5326                 {
5327                   argreg++;
5328                   stack_offset += MIPS32_REGSIZE;
5329                 }
5330             }
5331           while (len > 0)
5332             {
5333               /* Remember if the argument was written to the stack.  */
5334               int stack_used_p = 0;
5335               int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);
5336
5337               if (mips_debug)
5338                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
5339                                     partial_len);
5340
5341               /* Write this portion of the argument to the stack.  */
5342               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
5343                   || odd_sized_struct)
5344                 {
5345                   /* Should shorter than int integer values be
5346                      promoted to int before being stored?  */
5347                   int longword_offset = 0;
5348                   CORE_ADDR addr;
5349                   stack_used_p = 1;
5350
5351                   if (mips_debug)
5352                     {
5353                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
5354                                           paddress (gdbarch, stack_offset));
5355                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
5356                                           paddress (gdbarch, longword_offset));
5357                     }
5358
5359                   addr = sp + stack_offset + longword_offset;
5360
5361                   if (mips_debug)
5362                     {
5363                       int i;
5364                       fprintf_unfiltered (gdb_stdlog, " @%s ",
5365                                           paddress (gdbarch, addr));
5366                       for (i = 0; i < partial_len; i++)
5367                         {
5368                           fprintf_unfiltered (gdb_stdlog, "%02x",
5369                                               val[i] & 0xff);
5370                         }
5371                     }
5372                   write_memory (addr, val, partial_len);
5373                 }
5374
5375               /* Note!!! This is NOT an else clause.  Odd sized
5376                  structs may go thru BOTH paths.  */
5377               /* Write this portion of the argument to a general
5378                  purpose register.  */
5379               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
5380                 {
5381                   LONGEST regval = extract_signed_integer (val, partial_len,
5382                                                            byte_order);
5383                   /* Value may need to be sign extended, because
5384                      mips_isa_regsize() != mips_abi_regsize().  */
5385
5386                   /* A non-floating-point argument being passed in a
5387                      general register.  If a struct or union, and if
5388                      the remaining length is smaller than the register
5389                      size, we have to adjust the register value on
5390                      big endian targets.
5391
5392                      It does not seem to be necessary to do the
5393                      same for integral types.
5394
5395                      Also don't do this adjustment on O64 binaries.
5396
5397                      cagney/2001-07-23: gdb/179: Also, GCC, when
5398                      outputting LE O32 with sizeof (struct) <
5399                      mips_abi_regsize(), generates a left shift
5400                      as part of storing the argument in a register
5401                      (the left shift isn't generated when
5402                      sizeof (struct) >= mips_abi_regsize()).  Since
5403                      it is quite possible that this is GCC
5404                      contradicting the LE/O32 ABI, GDB has not been
5405                      adjusted to accommodate this.  Either someone
5406                      needs to demonstrate that the LE/O32 ABI
5407                      specifies such a left shift OR this new ABI gets
5408                      identified as such and GDB gets tweaked
5409                      accordingly.  */
5410
5411                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
5412                       && partial_len < MIPS32_REGSIZE
5413                       && (typecode == TYPE_CODE_STRUCT
5414                           || typecode == TYPE_CODE_UNION))
5415                     regval <<= ((MIPS32_REGSIZE - partial_len)
5416                                 * TARGET_CHAR_BIT);
5417
5418                   if (mips_debug)
5419                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
5420                                       argreg,
5421                                       phex (regval, MIPS32_REGSIZE));
5422                   regcache_cooked_write_unsigned (regcache, argreg, regval);
5423                   argreg++;
5424
5425                   /* Prevent subsequent floating point arguments from
5426                      being passed in floating point registers.  */
5427                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
5428                 }
5429
5430               len -= partial_len;
5431               val += partial_len;
5432
5433               /* Compute the offset into the stack at which we will
5434                  copy the next parameter.
5435
5436                  In older ABIs, the caller reserved space for
5437                  registers that contained arguments.  This was loosely
5438                  refered to as their "home".  Consequently, space is
5439                  always allocated.  */
5440
5441               stack_offset += align_up (partial_len, MIPS32_REGSIZE);
5442             }
5443         }
5444       if (mips_debug)
5445         fprintf_unfiltered (gdb_stdlog, "\n");
5446     }
5447
5448   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
5449
5450   /* Return adjusted stack pointer.  */
5451   return sp;
5452 }
5453
5454 static enum return_value_convention
5455 mips_o32_return_value (struct gdbarch *gdbarch, struct value *function,
5456                        struct type *type, struct regcache *regcache,
5457                        gdb_byte *readbuf, const gdb_byte *writebuf)
5458 {
5459   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
5460   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
5461   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5462   enum mips_fval_reg fval_reg;
5463
5464   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
5465   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5466       || TYPE_CODE (type) == TYPE_CODE_UNION
5467       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
5468     return RETURN_VALUE_STRUCT_CONVENTION;
5469   else if (TYPE_CODE (type) == TYPE_CODE_FLT
5470            && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5471     {
5472       /* A single-precision floating-point value.  If reading in or copying,
5473          then we get it from/put it to FP0 for standard MIPS code or GPR2
5474          for MIPS16 code.  If writing out only, then we put it to both FP0
5475          and GPR2.  We do not support reading in with no function known, if
5476          this safety check ever triggers, then we'll have to try harder.  */
5477       gdb_assert (function || !readbuf);
5478       if (mips_debug)
5479         switch (fval_reg)
5480           {
5481           case mips_fval_fpr:
5482             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
5483             break;
5484           case mips_fval_gpr:
5485             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
5486             break;
5487           case mips_fval_both:
5488             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
5489             break;
5490           }
5491       if (fval_reg != mips_fval_gpr)
5492         mips_xfer_register (gdbarch, regcache,
5493                             (gdbarch_num_regs (gdbarch)
5494                              + mips_regnum (gdbarch)->fp0),
5495                             TYPE_LENGTH (type),
5496                             gdbarch_byte_order (gdbarch),
5497                             readbuf, writebuf, 0);
5498       if (fval_reg != mips_fval_fpr)
5499         mips_xfer_register (gdbarch, regcache,
5500                             gdbarch_num_regs (gdbarch) + 2,
5501                             TYPE_LENGTH (type),
5502                             gdbarch_byte_order (gdbarch),
5503                             readbuf, writebuf, 0);
5504       return RETURN_VALUE_REGISTER_CONVENTION;
5505     }
5506   else if (TYPE_CODE (type) == TYPE_CODE_FLT
5507            && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5508     {
5509       /* A double-precision floating-point value.  If reading in or copying,
5510          then we get it from/put it to FP1 and FP0 for standard MIPS code or
5511          GPR2 and GPR3 for MIPS16 code.  If writing out only, then we put it
5512          to both FP1/FP0 and GPR2/GPR3.  We do not support reading in with
5513          no function known, if this safety check ever triggers, then we'll
5514          have to try harder.  */
5515       gdb_assert (function || !readbuf);
5516       if (mips_debug)
5517         switch (fval_reg)
5518           {
5519           case mips_fval_fpr:
5520             fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
5521             break;
5522           case mips_fval_gpr:
5523             fprintf_unfiltered (gdb_stderr, "Return float in $2/$3\n");
5524             break;
5525           case mips_fval_both:
5526             fprintf_unfiltered (gdb_stderr,
5527                                 "Return float in $fp1/$fp0 and $2/$3\n");
5528             break;
5529           }
5530       if (fval_reg != mips_fval_gpr)
5531         {
5532           /* The most significant part goes in FP1, and the least significant
5533              in FP0.  */
5534           switch (gdbarch_byte_order (gdbarch))
5535             {
5536             case BFD_ENDIAN_LITTLE:
5537               mips_xfer_register (gdbarch, regcache,
5538                                   (gdbarch_num_regs (gdbarch)
5539                                    + mips_regnum (gdbarch)->fp0 + 0),
5540                                   4, gdbarch_byte_order (gdbarch),
5541                                   readbuf, writebuf, 0);
5542               mips_xfer_register (gdbarch, regcache,
5543                                   (gdbarch_num_regs (gdbarch)
5544                                    + mips_regnum (gdbarch)->fp0 + 1),
5545                                   4, gdbarch_byte_order (gdbarch),
5546                                   readbuf, writebuf, 4);
5547               break;
5548             case BFD_ENDIAN_BIG:
5549               mips_xfer_register (gdbarch, regcache,
5550                                   (gdbarch_num_regs (gdbarch)
5551                                    + mips_regnum (gdbarch)->fp0 + 1),
5552                                   4, gdbarch_byte_order (gdbarch),
5553                                   readbuf, writebuf, 0);
5554               mips_xfer_register (gdbarch, regcache,
5555                                   (gdbarch_num_regs (gdbarch)
5556                                    + mips_regnum (gdbarch)->fp0 + 0),
5557                                   4, gdbarch_byte_order (gdbarch),
5558                                   readbuf, writebuf, 4);
5559               break;
5560             default:
5561               internal_error (__FILE__, __LINE__, _("bad switch"));
5562             }
5563         }
5564       if (fval_reg != mips_fval_fpr)
5565         {
5566           /* The two 32-bit parts are always placed in GPR2 and GPR3
5567              following these registers' memory order.  */
5568           mips_xfer_register (gdbarch, regcache,
5569                               gdbarch_num_regs (gdbarch) + 2,
5570                               4, gdbarch_byte_order (gdbarch),
5571                               readbuf, writebuf, 0);
5572           mips_xfer_register (gdbarch, regcache,
5573                               gdbarch_num_regs (gdbarch) + 3,
5574                               4, gdbarch_byte_order (gdbarch),
5575                               readbuf, writebuf, 4);
5576         }
5577       return RETURN_VALUE_REGISTER_CONVENTION;
5578     }
5579 #if 0
5580   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5581            && TYPE_NFIELDS (type) <= 2
5582            && TYPE_NFIELDS (type) >= 1
5583            && ((TYPE_NFIELDS (type) == 1
5584                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
5585                     == TYPE_CODE_FLT))
5586                || (TYPE_NFIELDS (type) == 2
5587                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
5588                        == TYPE_CODE_FLT)
5589                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
5590                        == TYPE_CODE_FLT)))
5591            && tdep->mips_fpu_type != MIPS_FPU_NONE)
5592     {
5593       /* A struct that contains one or two floats.  Each value is part
5594          in the least significant part of their floating point
5595          register..  */
5596       gdb_byte reg[MAX_REGISTER_SIZE];
5597       int regnum;
5598       int field;
5599       for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
5600            field < TYPE_NFIELDS (type); field++, regnum += 2)
5601         {
5602           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
5603                         / TARGET_CHAR_BIT);
5604           if (mips_debug)
5605             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
5606                                 offset);
5607           mips_xfer_register (gdbarch, regcache,
5608                               gdbarch_num_regs (gdbarch) + regnum,
5609                               TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
5610                               gdbarch_byte_order (gdbarch),
5611                               readbuf, writebuf, offset);
5612         }
5613       return RETURN_VALUE_REGISTER_CONVENTION;
5614     }
5615 #endif
5616 #if 0
5617   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5618            || TYPE_CODE (type) == TYPE_CODE_UNION)
5619     {
5620       /* A structure or union.  Extract the left justified value,
5621          regardless of the byte order.  I.e. DO NOT USE
5622          mips_xfer_lower.  */
5623       int offset;
5624       int regnum;
5625       for (offset = 0, regnum = MIPS_V0_REGNUM;
5626            offset < TYPE_LENGTH (type);
5627            offset += register_size (gdbarch, regnum), regnum++)
5628         {
5629           int xfer = register_size (gdbarch, regnum);
5630           if (offset + xfer > TYPE_LENGTH (type))
5631             xfer = TYPE_LENGTH (type) - offset;
5632           if (mips_debug)
5633             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
5634                                 offset, xfer, regnum);
5635           mips_xfer_register (gdbarch, regcache,
5636                               gdbarch_num_regs (gdbarch) + regnum, xfer,
5637                               BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
5638         }
5639       return RETURN_VALUE_REGISTER_CONVENTION;
5640     }
5641 #endif
5642   else
5643     {
5644       /* A scalar extract each part but least-significant-byte
5645          justified.  o32 thinks registers are 4 byte, regardless of
5646          the ISA.  */
5647       int offset;
5648       int regnum;
5649       for (offset = 0, regnum = MIPS_V0_REGNUM;
5650            offset < TYPE_LENGTH (type);
5651            offset += MIPS32_REGSIZE, regnum++)
5652         {
5653           int xfer = MIPS32_REGSIZE;
5654           if (offset + xfer > TYPE_LENGTH (type))
5655             xfer = TYPE_LENGTH (type) - offset;
5656           if (mips_debug)
5657             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
5658                                 offset, xfer, regnum);
5659           mips_xfer_register (gdbarch, regcache,
5660                               gdbarch_num_regs (gdbarch) + regnum, xfer,
5661                               gdbarch_byte_order (gdbarch),
5662                               readbuf, writebuf, offset);
5663         }
5664       return RETURN_VALUE_REGISTER_CONVENTION;
5665     }
5666 }
5667
5668 /* O64 ABI.  This is a hacked up kind of 64-bit version of the o32
5669    ABI.  */
5670
5671 static CORE_ADDR
5672 mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
5673                           struct regcache *regcache, CORE_ADDR bp_addr,
5674                           int nargs,
5675                           struct value **args, CORE_ADDR sp,
5676                           int struct_return, CORE_ADDR struct_addr)
5677 {
5678   int argreg;
5679   int float_argreg;
5680   int argnum;
5681   int len = 0;
5682   int stack_offset = 0;
5683   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5684   CORE_ADDR func_addr = find_function_addr (function, NULL);
5685
5686   /* For shared libraries, "t9" needs to point at the function
5687      address.  */
5688   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
5689
5690   /* Set the return address register to point to the entry point of
5691      the program, where a breakpoint lies in wait.  */
5692   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
5693
5694   /* First ensure that the stack and structure return address (if any)
5695      are properly aligned.  The stack has to be at least 64-bit
5696      aligned even on 32-bit machines, because doubles must be 64-bit
5697      aligned.  For n32 and n64, stack frames need to be 128-bit
5698      aligned, so we round to this widest known alignment.  */
5699
5700   sp = align_down (sp, 16);
5701   struct_addr = align_down (struct_addr, 16);
5702
5703   /* Now make space on the stack for the args.  */
5704   for (argnum = 0; argnum < nargs; argnum++)
5705     {
5706       struct type *arg_type = check_typedef (value_type (args[argnum]));
5707
5708       /* Allocate space on the stack.  */
5709       len += align_up (TYPE_LENGTH (arg_type), MIPS64_REGSIZE);
5710     }
5711   sp -= align_up (len, 16);
5712
5713   if (mips_debug)
5714     fprintf_unfiltered (gdb_stdlog,
5715                         "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
5716                         paddress (gdbarch, sp), (long) align_up (len, 16));
5717
5718   /* Initialize the integer and float register pointers.  */
5719   argreg = MIPS_A0_REGNUM;
5720   float_argreg = mips_fpa0_regnum (gdbarch);
5721
5722   /* The struct_return pointer occupies the first parameter-passing reg.  */
5723   if (struct_return)
5724     {
5725       if (mips_debug)
5726         fprintf_unfiltered (gdb_stdlog,
5727                             "mips_o64_push_dummy_call: "
5728                             "struct_return reg=%d %s\n",
5729                             argreg, paddress (gdbarch, struct_addr));
5730       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
5731       stack_offset += MIPS64_REGSIZE;
5732     }
5733
5734   /* Now load as many as possible of the first arguments into
5735      registers, and push the rest onto the stack.  Loop thru args
5736      from first to last.  */
5737   for (argnum = 0; argnum < nargs; argnum++)
5738     {
5739       const gdb_byte *val;
5740       gdb_byte valbuf[MAX_REGISTER_SIZE];
5741       struct value *arg = args[argnum];
5742       struct type *arg_type = check_typedef (value_type (arg));
5743       int len = TYPE_LENGTH (arg_type);
5744       enum type_code typecode = TYPE_CODE (arg_type);
5745
5746       if (mips_debug)
5747         fprintf_unfiltered (gdb_stdlog,
5748                             "mips_o64_push_dummy_call: %d len=%d type=%d",
5749                             argnum + 1, len, (int) typecode);
5750
5751       val = value_contents (arg);
5752
5753       /* Function pointer arguments to mips16 code need to be made into
5754          mips16 pointers.  */
5755       if (typecode == TYPE_CODE_PTR
5756           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
5757         {
5758           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
5759                                                    len, byte_order);
5760           if (!mips_pc_is_mips (addr))
5761             {
5762               store_signed_integer (valbuf, len, byte_order, 
5763                                     make_compact_addr (addr));
5764               val = valbuf;
5765             }
5766         }
5767
5768       /* Floating point arguments passed in registers have to be
5769          treated specially.  On 32-bit architectures, doubles are
5770          passed in register pairs; the even FP register gets the
5771          low word, and the odd FP register gets the high word.
5772          On O64, the first two floating point arguments are also
5773          copied to general registers, because MIPS16 functions
5774          don't use float registers for arguments.  This duplication
5775          of arguments in general registers can't hurt non-MIPS16
5776          functions because those registers are normally skipped.  */
5777
5778       if (fp_register_arg_p (gdbarch, typecode, arg_type)
5779           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
5780         {
5781           LONGEST regval = extract_unsigned_integer (val, len, byte_order);
5782           if (mips_debug)
5783             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5784                                 float_argreg, phex (regval, len));
5785           regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
5786           if (mips_debug)
5787             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5788                                 argreg, phex (regval, len));
5789           regcache_cooked_write_unsigned (regcache, argreg, regval);
5790           argreg++;
5791           /* Reserve space for the FP register.  */
5792           stack_offset += align_up (len, MIPS64_REGSIZE);
5793         }
5794       else
5795         {
5796           /* Copy the argument to general registers or the stack in
5797              register-sized pieces.  Large arguments are split between
5798              registers and stack.  */
5799           /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
5800              are treated specially: Irix cc passes them in registers
5801              where gcc sometimes puts them on the stack.  For maximum
5802              compatibility, we will put them in both places.  */
5803           int odd_sized_struct = (len > MIPS64_REGSIZE
5804                                   && len % MIPS64_REGSIZE != 0);
5805           while (len > 0)
5806             {
5807               /* Remember if the argument was written to the stack.  */
5808               int stack_used_p = 0;
5809               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
5810
5811               if (mips_debug)
5812                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
5813                                     partial_len);
5814
5815               /* Write this portion of the argument to the stack.  */
5816               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
5817                   || odd_sized_struct)
5818                 {
5819                   /* Should shorter than int integer values be
5820                      promoted to int before being stored?  */
5821                   int longword_offset = 0;
5822                   CORE_ADDR addr;
5823                   stack_used_p = 1;
5824                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
5825                     {
5826                       if ((typecode == TYPE_CODE_INT
5827                            || typecode == TYPE_CODE_PTR
5828                            || typecode == TYPE_CODE_FLT)
5829                           && len <= 4)
5830                         longword_offset = MIPS64_REGSIZE - len;
5831                     }
5832
5833                   if (mips_debug)
5834                     {
5835                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
5836                                           paddress (gdbarch, stack_offset));
5837                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
5838                                           paddress (gdbarch, longword_offset));
5839                     }
5840
5841                   addr = sp + stack_offset + longword_offset;
5842
5843                   if (mips_debug)
5844                     {
5845                       int i;
5846                       fprintf_unfiltered (gdb_stdlog, " @%s ",
5847                                           paddress (gdbarch, addr));
5848                       for (i = 0; i < partial_len; i++)
5849                         {
5850                           fprintf_unfiltered (gdb_stdlog, "%02x",
5851                                               val[i] & 0xff);
5852                         }
5853                     }
5854                   write_memory (addr, val, partial_len);
5855                 }
5856
5857               /* Note!!! This is NOT an else clause.  Odd sized
5858                  structs may go thru BOTH paths.  */
5859               /* Write this portion of the argument to a general
5860                  purpose register.  */
5861               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
5862                 {
5863                   LONGEST regval = extract_signed_integer (val, partial_len,
5864                                                            byte_order);
5865                   /* Value may need to be sign extended, because
5866                      mips_isa_regsize() != mips_abi_regsize().  */
5867
5868                   /* A non-floating-point argument being passed in a
5869                      general register.  If a struct or union, and if
5870                      the remaining length is smaller than the register
5871                      size, we have to adjust the register value on
5872                      big endian targets.
5873
5874                      It does not seem to be necessary to do the
5875                      same for integral types.  */
5876
5877                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
5878                       && partial_len < MIPS64_REGSIZE
5879                       && (typecode == TYPE_CODE_STRUCT
5880                           || typecode == TYPE_CODE_UNION))
5881                     regval <<= ((MIPS64_REGSIZE - partial_len)
5882                                 * TARGET_CHAR_BIT);
5883
5884                   if (mips_debug)
5885                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
5886                                       argreg,
5887                                       phex (regval, MIPS64_REGSIZE));
5888                   regcache_cooked_write_unsigned (regcache, argreg, regval);
5889                   argreg++;
5890
5891                   /* Prevent subsequent floating point arguments from
5892                      being passed in floating point registers.  */
5893                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
5894                 }
5895
5896               len -= partial_len;
5897               val += partial_len;
5898
5899               /* Compute the offset into the stack at which we will
5900                  copy the next parameter.
5901
5902                  In older ABIs, the caller reserved space for
5903                  registers that contained arguments.  This was loosely
5904                  refered to as their "home".  Consequently, space is
5905                  always allocated.  */
5906
5907               stack_offset += align_up (partial_len, MIPS64_REGSIZE);
5908             }
5909         }
5910       if (mips_debug)
5911         fprintf_unfiltered (gdb_stdlog, "\n");
5912     }
5913
5914   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
5915
5916   /* Return adjusted stack pointer.  */
5917   return sp;
5918 }
5919
5920 static enum return_value_convention
5921 mips_o64_return_value (struct gdbarch *gdbarch, struct value *function,
5922                        struct type *type, struct regcache *regcache,
5923                        gdb_byte *readbuf, const gdb_byte *writebuf)
5924 {
5925   CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
5926   int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
5927   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5928   enum mips_fval_reg fval_reg;
5929
5930   fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
5931   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5932       || TYPE_CODE (type) == TYPE_CODE_UNION
5933       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
5934     return RETURN_VALUE_STRUCT_CONVENTION;
5935   else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
5936     {
5937       /* A floating-point value.  If reading in or copying, then we get it
5938          from/put it to FP0 for standard MIPS code or GPR2 for MIPS16 code.
5939          If writing out only, then we put it to both FP0 and GPR2.  We do
5940          not support reading in with no function known, if this safety
5941          check ever triggers, then we'll have to try harder.  */
5942       gdb_assert (function || !readbuf);
5943       if (mips_debug)
5944         switch (fval_reg)
5945           {
5946           case mips_fval_fpr:
5947             fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
5948             break;
5949           case mips_fval_gpr:
5950             fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
5951             break;
5952           case mips_fval_both:
5953             fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
5954             break;
5955           }
5956       if (fval_reg != mips_fval_gpr)
5957         mips_xfer_register (gdbarch, regcache,
5958                             (gdbarch_num_regs (gdbarch)
5959                              + mips_regnum (gdbarch)->fp0),
5960                             TYPE_LENGTH (type),
5961                             gdbarch_byte_order (gdbarch),
5962                             readbuf, writebuf, 0);
5963       if (fval_reg != mips_fval_fpr)
5964         mips_xfer_register (gdbarch, regcache,
5965                             gdbarch_num_regs (gdbarch) + 2,
5966                             TYPE_LENGTH (type),
5967                             gdbarch_byte_order (gdbarch),
5968                             readbuf, writebuf, 0);
5969       return RETURN_VALUE_REGISTER_CONVENTION;
5970     }
5971   else
5972     {
5973       /* A scalar extract each part but least-significant-byte
5974          justified.  */
5975       int offset;
5976       int regnum;
5977       for (offset = 0, regnum = MIPS_V0_REGNUM;
5978            offset < TYPE_LENGTH (type);
5979            offset += MIPS64_REGSIZE, regnum++)
5980         {
5981           int xfer = MIPS64_REGSIZE;
5982           if (offset + xfer > TYPE_LENGTH (type))
5983             xfer = TYPE_LENGTH (type) - offset;
5984           if (mips_debug)
5985             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
5986                                 offset, xfer, regnum);
5987           mips_xfer_register (gdbarch, regcache,
5988                               gdbarch_num_regs (gdbarch) + regnum,
5989                               xfer, gdbarch_byte_order (gdbarch),
5990                               readbuf, writebuf, offset);
5991         }
5992       return RETURN_VALUE_REGISTER_CONVENTION;
5993     }
5994 }
5995
5996 /* Floating point register management.
5997
5998    Background: MIPS1 & 2 fp registers are 32 bits wide.  To support
5999    64bit operations, these early MIPS cpus treat fp register pairs
6000    (f0,f1) as a single register (d0).  Later MIPS cpu's have 64 bit fp
6001    registers and offer a compatibility mode that emulates the MIPS2 fp
6002    model.  When operating in MIPS2 fp compat mode, later cpu's split
6003    double precision floats into two 32-bit chunks and store them in
6004    consecutive fp regs.  To display 64-bit floats stored in this
6005    fashion, we have to combine 32 bits from f0 and 32 bits from f1.
6006    Throw in user-configurable endianness and you have a real mess.
6007
6008    The way this works is:
6009      - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
6010        double-precision value will be split across two logical registers.
6011        The lower-numbered logical register will hold the low-order bits,
6012        regardless of the processor's endianness.
6013      - If we are on a 64-bit processor, and we are looking for a
6014        single-precision value, it will be in the low ordered bits
6015        of a 64-bit GPR (after mfc1, for example) or a 64-bit register
6016        save slot in memory.
6017      - If we are in 64-bit mode, everything is straightforward.
6018
6019    Note that this code only deals with "live" registers at the top of the
6020    stack.  We will attempt to deal with saved registers later, when
6021    the raw/cooked register interface is in place.  (We need a general
6022    interface that can deal with dynamic saved register sizes -- fp
6023    regs could be 32 bits wide in one frame and 64 on the frame above
6024    and below).  */
6025
6026 /* Copy a 32-bit single-precision value from the current frame
6027    into rare_buffer.  */
6028
6029 static void
6030 mips_read_fp_register_single (struct frame_info *frame, int regno,
6031                               gdb_byte *rare_buffer)
6032 {
6033   struct gdbarch *gdbarch = get_frame_arch (frame);
6034   int raw_size = register_size (gdbarch, regno);
6035   gdb_byte *raw_buffer = alloca (raw_size);
6036
6037   if (!deprecated_frame_register_read (frame, regno, raw_buffer))
6038     error (_("can't read register %d (%s)"),
6039            regno, gdbarch_register_name (gdbarch, regno));
6040   if (raw_size == 8)
6041     {
6042       /* We have a 64-bit value for this register.  Find the low-order
6043          32 bits.  */
6044       int offset;
6045
6046       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6047         offset = 4;
6048       else
6049         offset = 0;
6050
6051       memcpy (rare_buffer, raw_buffer + offset, 4);
6052     }
6053   else
6054     {
6055       memcpy (rare_buffer, raw_buffer, 4);
6056     }
6057 }
6058
6059 /* Copy a 64-bit double-precision value from the current frame into
6060    rare_buffer.  This may include getting half of it from the next
6061    register.  */
6062
6063 static void
6064 mips_read_fp_register_double (struct frame_info *frame, int regno,
6065                               gdb_byte *rare_buffer)
6066 {
6067   struct gdbarch *gdbarch = get_frame_arch (frame);
6068   int raw_size = register_size (gdbarch, regno);
6069
6070   if (raw_size == 8 && !mips2_fp_compat (frame))
6071     {
6072       /* We have a 64-bit value for this register, and we should use
6073          all 64 bits.  */
6074       if (!deprecated_frame_register_read (frame, regno, rare_buffer))
6075         error (_("can't read register %d (%s)"),
6076                regno, gdbarch_register_name (gdbarch, regno));
6077     }
6078   else
6079     {
6080       int rawnum = regno % gdbarch_num_regs (gdbarch);
6081
6082       if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
6083         internal_error (__FILE__, __LINE__,
6084                         _("mips_read_fp_register_double: bad access to "
6085                         "odd-numbered FP register"));
6086
6087       /* mips_read_fp_register_single will find the correct 32 bits from
6088          each register.  */
6089       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6090         {
6091           mips_read_fp_register_single (frame, regno, rare_buffer + 4);
6092           mips_read_fp_register_single (frame, regno + 1, rare_buffer);
6093         }
6094       else
6095         {
6096           mips_read_fp_register_single (frame, regno, rare_buffer);
6097           mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
6098         }
6099     }
6100 }
6101
6102 static void
6103 mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
6104                         int regnum)
6105 {                               /* Do values for FP (float) regs.  */
6106   struct gdbarch *gdbarch = get_frame_arch (frame);
6107   gdb_byte *raw_buffer;
6108   double doub, flt1;    /* Doubles extracted from raw hex data.  */
6109   int inv1, inv2;
6110
6111   raw_buffer = alloca (2 * register_size (gdbarch,
6112                                           mips_regnum (gdbarch)->fp0));
6113
6114   fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
6115   fprintf_filtered (file, "%*s",
6116                     4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
6117                     "");
6118
6119   if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
6120     {
6121       struct value_print_options opts;
6122
6123       /* 4-byte registers: Print hex and floating.  Also print even
6124          numbered registers as doubles.  */
6125       mips_read_fp_register_single (frame, regnum, raw_buffer);
6126       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
6127                             raw_buffer, &inv1);
6128
6129       get_formatted_print_options (&opts, 'x');
6130       print_scalar_formatted (raw_buffer,
6131                               builtin_type (gdbarch)->builtin_uint32,
6132                               &opts, 'w', file);
6133
6134       fprintf_filtered (file, " flt: ");
6135       if (inv1)
6136         fprintf_filtered (file, " <invalid float> ");
6137       else
6138         fprintf_filtered (file, "%-17.9g", flt1);
6139
6140       if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
6141         {
6142           mips_read_fp_register_double (frame, regnum, raw_buffer);
6143           doub = unpack_double (builtin_type (gdbarch)->builtin_double,
6144                                 raw_buffer, &inv2);
6145
6146           fprintf_filtered (file, " dbl: ");
6147           if (inv2)
6148             fprintf_filtered (file, "<invalid double>");
6149           else
6150             fprintf_filtered (file, "%-24.17g", doub);
6151         }
6152     }
6153   else
6154     {
6155       struct value_print_options opts;
6156
6157       /* Eight byte registers: print each one as hex, float and double.  */
6158       mips_read_fp_register_single (frame, regnum, raw_buffer);
6159       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
6160                             raw_buffer, &inv1);
6161
6162       mips_read_fp_register_double (frame, regnum, raw_buffer);
6163       doub = unpack_double (builtin_type (gdbarch)->builtin_double,
6164                             raw_buffer, &inv2);
6165
6166       get_formatted_print_options (&opts, 'x');
6167       print_scalar_formatted (raw_buffer,
6168                               builtin_type (gdbarch)->builtin_uint64,
6169                               &opts, 'g', file);
6170
6171       fprintf_filtered (file, " flt: ");
6172       if (inv1)
6173         fprintf_filtered (file, "<invalid float>");
6174       else
6175         fprintf_filtered (file, "%-17.9g", flt1);
6176
6177       fprintf_filtered (file, " dbl: ");
6178       if (inv2)
6179         fprintf_filtered (file, "<invalid double>");
6180       else
6181         fprintf_filtered (file, "%-24.17g", doub);
6182     }
6183 }
6184
6185 static void
6186 mips_print_register (struct ui_file *file, struct frame_info *frame,
6187                      int regnum)
6188 {
6189   struct gdbarch *gdbarch = get_frame_arch (frame);
6190   struct value_print_options opts;
6191   struct value *val;
6192
6193   if (mips_float_register_p (gdbarch, regnum))
6194     {
6195       mips_print_fp_register (file, frame, regnum);
6196       return;
6197     }
6198
6199   val = get_frame_register_value (frame, regnum);
6200   if (value_optimized_out (val))
6201     {
6202       fprintf_filtered (file, "%s: [Invalid]",
6203                         gdbarch_register_name (gdbarch, regnum));
6204       return;
6205     }
6206
6207   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
6208
6209   /* The problem with printing numeric register names (r26, etc.) is that
6210      the user can't use them on input.  Probably the best solution is to
6211      fix it so that either the numeric or the funky (a2, etc.) names
6212      are accepted on input.  */
6213   if (regnum < MIPS_NUMREGS)
6214     fprintf_filtered (file, "(r%d): ", regnum);
6215   else
6216     fprintf_filtered (file, ": ");
6217
6218   get_formatted_print_options (&opts, 'x');
6219   val_print_scalar_formatted (value_type (val),
6220                               value_contents_for_printing (val),
6221                               value_embedded_offset (val),
6222                               val,
6223                               &opts, 0, file);
6224 }
6225
6226 /* Replacement for generic do_registers_info.
6227    Print regs in pretty columns.  */
6228
6229 static int
6230 print_fp_register_row (struct ui_file *file, struct frame_info *frame,
6231                        int regnum)
6232 {
6233   fprintf_filtered (file, " ");
6234   mips_print_fp_register (file, frame, regnum);
6235   fprintf_filtered (file, "\n");
6236   return regnum + 1;
6237 }
6238
6239
6240 /* Print a row's worth of GP (int) registers, with name labels above.  */
6241
6242 static int
6243 print_gp_register_row (struct ui_file *file, struct frame_info *frame,
6244                        int start_regnum)
6245 {
6246   struct gdbarch *gdbarch = get_frame_arch (frame);
6247   /* Do values for GP (int) regs.  */
6248   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
6249   int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8);    /* display cols
6250                                                                per row.  */
6251   int col, byte;
6252   int regnum;
6253
6254   /* For GP registers, we print a separate row of names above the vals.  */
6255   for (col = 0, regnum = start_regnum;
6256        col < ncols && regnum < gdbarch_num_regs (gdbarch)
6257                                + gdbarch_num_pseudo_regs (gdbarch);
6258        regnum++)
6259     {
6260       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
6261         continue;               /* unused register */
6262       if (mips_float_register_p (gdbarch, regnum))
6263         break;                  /* End the row: reached FP register.  */
6264       /* Large registers are handled separately.  */
6265       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
6266         {
6267           if (col > 0)
6268             break;              /* End the row before this register.  */
6269
6270           /* Print this register on a row by itself.  */
6271           mips_print_register (file, frame, regnum);
6272           fprintf_filtered (file, "\n");
6273           return regnum + 1;
6274         }
6275       if (col == 0)
6276         fprintf_filtered (file, "     ");
6277       fprintf_filtered (file,
6278                         mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
6279                         gdbarch_register_name (gdbarch, regnum));
6280       col++;
6281     }
6282
6283   if (col == 0)
6284     return regnum;
6285
6286   /* Print the R0 to R31 names.  */
6287   if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
6288     fprintf_filtered (file, "\n R%-4d",
6289                       start_regnum % gdbarch_num_regs (gdbarch));
6290   else
6291     fprintf_filtered (file, "\n      ");
6292
6293   /* Now print the values in hex, 4 or 8 to the row.  */
6294   for (col = 0, regnum = start_regnum;
6295        col < ncols && regnum < gdbarch_num_regs (gdbarch)
6296                                + gdbarch_num_pseudo_regs (gdbarch);
6297        regnum++)
6298     {
6299       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
6300         continue;               /* unused register */
6301       if (mips_float_register_p (gdbarch, regnum))
6302         break;                  /* End row: reached FP register.  */
6303       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
6304         break;                  /* End row: large register.  */
6305
6306       /* OK: get the data in raw format.  */
6307       if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
6308         error (_("can't read register %d (%s)"),
6309                regnum, gdbarch_register_name (gdbarch, regnum));
6310       /* pad small registers */
6311       for (byte = 0;
6312            byte < (mips_abi_regsize (gdbarch)
6313                    - register_size (gdbarch, regnum)); byte++)
6314         printf_filtered ("  ");
6315       /* Now print the register value in hex, endian order.  */
6316       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6317         for (byte =
6318              register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
6319              byte < register_size (gdbarch, regnum); byte++)
6320           fprintf_filtered (file, "%02x", raw_buffer[byte]);
6321       else
6322         for (byte = register_size (gdbarch, regnum) - 1;
6323              byte >= 0; byte--)
6324           fprintf_filtered (file, "%02x", raw_buffer[byte]);
6325       fprintf_filtered (file, " ");
6326       col++;
6327     }
6328   if (col > 0)                  /* ie. if we actually printed anything...  */
6329     fprintf_filtered (file, "\n");
6330
6331   return regnum;
6332 }
6333
6334 /* MIPS_DO_REGISTERS_INFO(): called by "info register" command.  */
6335
6336 static void
6337 mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
6338                            struct frame_info *frame, int regnum, int all)
6339 {
6340   if (regnum != -1)             /* Do one specified register.  */
6341     {
6342       gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
6343       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
6344         error (_("Not a valid register for the current processor type"));
6345
6346       mips_print_register (file, frame, regnum);
6347       fprintf_filtered (file, "\n");
6348     }
6349   else
6350     /* Do all (or most) registers.  */
6351     {
6352       regnum = gdbarch_num_regs (gdbarch);
6353       while (regnum < gdbarch_num_regs (gdbarch)
6354                       + gdbarch_num_pseudo_regs (gdbarch))
6355         {
6356           if (mips_float_register_p (gdbarch, regnum))
6357             {
6358               if (all)          /* True for "INFO ALL-REGISTERS" command.  */
6359                 regnum = print_fp_register_row (file, frame, regnum);
6360               else
6361                 regnum += MIPS_NUMREGS; /* Skip floating point regs.  */
6362             }
6363           else
6364             regnum = print_gp_register_row (file, frame, regnum);
6365         }
6366     }
6367 }
6368
6369 static int
6370 mips_single_step_through_delay (struct gdbarch *gdbarch,
6371                                 struct frame_info *frame)
6372 {
6373   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6374   CORE_ADDR pc = get_frame_pc (frame);
6375   struct address_space *aspace;
6376   enum mips_isa isa;
6377   ULONGEST insn;
6378   int status;
6379   int size;
6380
6381   if ((mips_pc_is_mips (pc)
6382        && !mips32_instruction_has_delay_slot (gdbarch, pc))
6383       || (mips_pc_is_micromips (gdbarch, pc)
6384           && !micromips_instruction_has_delay_slot (gdbarch, pc, 0))
6385       || (mips_pc_is_mips16 (gdbarch, pc)
6386           && !mips16_instruction_has_delay_slot (gdbarch, pc, 0)))
6387     return 0;
6388
6389   isa = mips_pc_isa (gdbarch, pc);
6390   /* _has_delay_slot above will have validated the read.  */
6391   insn = mips_fetch_instruction (gdbarch, isa, pc, NULL);
6392   size = mips_insn_size (isa, insn);
6393   aspace = get_frame_address_space (frame);
6394   return breakpoint_here_p (aspace, pc + size) != no_breakpoint_here;
6395 }
6396
6397 /* To skip prologues, I use this predicate.  Returns either PC itself
6398    if the code at PC does not look like a function prologue; otherwise
6399    returns an address that (if we're lucky) follows the prologue.  If
6400    LENIENT, then we must skip everything which is involved in setting
6401    up the frame (it's OK to skip more, just so long as we don't skip
6402    anything which might clobber the registers which are being saved.
6403    We must skip more in the case where part of the prologue is in the
6404    delay slot of a non-prologue instruction).  */
6405
6406 static CORE_ADDR
6407 mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
6408 {
6409   CORE_ADDR limit_pc;
6410   CORE_ADDR func_addr;
6411
6412   /* See if we can determine the end of the prologue via the symbol table.
6413      If so, then return either PC, or the PC after the prologue, whichever
6414      is greater.  */
6415   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
6416     {
6417       CORE_ADDR post_prologue_pc
6418         = skip_prologue_using_sal (gdbarch, func_addr);
6419       if (post_prologue_pc != 0)
6420         return max (pc, post_prologue_pc);
6421     }
6422
6423   /* Can't determine prologue from the symbol table, need to examine
6424      instructions.  */
6425
6426   /* Find an upper limit on the function prologue using the debug
6427      information.  If the debug information could not be used to provide
6428      that bound, then use an arbitrary large number as the upper bound.  */
6429   limit_pc = skip_prologue_using_sal (gdbarch, pc);
6430   if (limit_pc == 0)
6431     limit_pc = pc + 100;          /* Magic.  */
6432
6433   if (mips_pc_is_mips16 (gdbarch, pc))
6434     return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6435   else if (mips_pc_is_micromips (gdbarch, pc))
6436     return micromips_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6437   else
6438     return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6439 }
6440
6441 /* Check whether the PC is in a function epilogue (32-bit version).
6442    This is a helper function for mips_in_function_epilogue_p.  */
6443 static int
6444 mips32_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6445 {
6446   CORE_ADDR func_addr = 0, func_end = 0;
6447
6448   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6449     {
6450       /* The MIPS epilogue is max. 12 bytes long.  */
6451       CORE_ADDR addr = func_end - 12;
6452
6453       if (addr < func_addr + 4)
6454         addr = func_addr + 4;
6455       if (pc < addr)
6456         return 0;
6457
6458       for (; pc < func_end; pc += MIPS_INSN32_SIZE)
6459         {
6460           unsigned long high_word;
6461           unsigned long inst;
6462
6463           inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
6464           high_word = (inst >> 16) & 0xffff;
6465
6466           if (high_word != 0x27bd       /* addiu $sp,$sp,offset */
6467               && high_word != 0x67bd    /* daddiu $sp,$sp,offset */
6468               && inst != 0x03e00008     /* jr $ra */
6469               && inst != 0x00000000)    /* nop */
6470             return 0;
6471         }
6472
6473       return 1;
6474     }
6475
6476   return 0;
6477 }
6478
6479 /* Check whether the PC is in a function epilogue (microMIPS version).
6480    This is a helper function for mips_in_function_epilogue_p.  */
6481
6482 static int
6483 micromips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6484 {
6485   CORE_ADDR func_addr = 0;
6486   CORE_ADDR func_end = 0;
6487   CORE_ADDR addr;
6488   ULONGEST insn;
6489   long offset;
6490   int dreg;
6491   int sreg;
6492   int loc;
6493
6494   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6495     return 0;
6496
6497   /* The microMIPS epilogue is max. 12 bytes long.  */
6498   addr = func_end - 12;
6499
6500   if (addr < func_addr + 2)
6501     addr = func_addr + 2;
6502   if (pc < addr)
6503     return 0;
6504
6505   for (; pc < func_end; pc += loc)
6506     {
6507       loc = 0;
6508       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
6509       loc += MIPS_INSN16_SIZE;
6510       switch (mips_insn_size (ISA_MICROMIPS, insn))
6511         {
6512         /* 48-bit instructions.  */
6513         case 3 * MIPS_INSN16_SIZE:
6514           /* No epilogue instructions in this category.  */
6515           return 0;
6516
6517         /* 32-bit instructions.  */
6518         case 2 * MIPS_INSN16_SIZE:
6519           insn <<= 16;
6520           insn |= mips_fetch_instruction (gdbarch,
6521                                           ISA_MICROMIPS, pc + loc, NULL);
6522           loc += MIPS_INSN16_SIZE;
6523           switch (micromips_op (insn >> 16))
6524             {
6525             case 0xc: /* ADDIU: bits 001100 */
6526             case 0x17: /* DADDIU: bits 010111 */
6527               sreg = b0s5_reg (insn >> 16);
6528               dreg = b5s5_reg (insn >> 16);
6529               offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
6530               if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
6531                             /* (D)ADDIU $sp, imm */
6532                   && offset >= 0)
6533                 break;
6534               return 0;
6535
6536             default:
6537               return 0;
6538             }
6539           break;
6540
6541         /* 16-bit instructions.  */
6542         case MIPS_INSN16_SIZE:
6543           switch (micromips_op (insn))
6544             {
6545             case 0x3: /* MOVE: bits 000011 */
6546               sreg = b0s5_reg (insn);
6547               dreg = b5s5_reg (insn);
6548               if (sreg == 0 && dreg == 0)
6549                                 /* MOVE $zero, $zero aka NOP */
6550                 break;
6551               return 0;
6552
6553             case 0x11: /* POOL16C: bits 010001 */
6554               if (b5s5_op (insn) == 0x18
6555                                 /* JRADDIUSP: bits 010011 11000 */
6556                   || (b5s5_op (insn) == 0xd
6557                                 /* JRC: bits 010011 01101 */
6558                       && b0s5_reg (insn) == MIPS_RA_REGNUM))
6559                                 /* JRC $ra */
6560                 break;
6561               return 0;
6562
6563             case 0x13: /* POOL16D: bits 010011 */
6564               offset = micromips_decode_imm9 (b1s9_imm (insn));
6565               if ((insn & 0x1) == 0x1
6566                                 /* ADDIUSP: bits 010011 1 */
6567                   && offset > 0)
6568                 break;
6569               return 0;
6570
6571             default:
6572               return 0;
6573             }
6574         }
6575     }
6576
6577   return 1;
6578 }
6579
6580 /* Check whether the PC is in a function epilogue (16-bit version).
6581    This is a helper function for mips_in_function_epilogue_p.  */
6582 static int
6583 mips16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6584 {
6585   CORE_ADDR func_addr = 0, func_end = 0;
6586
6587   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6588     {
6589       /* The MIPS epilogue is max. 12 bytes long.  */
6590       CORE_ADDR addr = func_end - 12;
6591
6592       if (addr < func_addr + 4)
6593         addr = func_addr + 4;
6594       if (pc < addr)
6595         return 0;
6596
6597       for (; pc < func_end; pc += MIPS_INSN16_SIZE)
6598         {
6599           unsigned short inst;
6600
6601           inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc, NULL);
6602
6603           if ((inst & 0xf800) == 0xf000)        /* extend */
6604             continue;
6605
6606           if (inst != 0x6300            /* addiu $sp,offset */
6607               && inst != 0xfb00         /* daddiu $sp,$sp,offset */
6608               && inst != 0xe820         /* jr $ra */
6609               && inst != 0xe8a0         /* jrc $ra */
6610               && inst != 0x6500)        /* nop */
6611             return 0;
6612         }
6613
6614       return 1;
6615     }
6616
6617   return 0;
6618 }
6619
6620 /* The epilogue is defined here as the area at the end of a function,
6621    after an instruction which destroys the function's stack frame.  */
6622 static int
6623 mips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6624 {
6625   if (mips_pc_is_mips16 (gdbarch, pc))
6626     return mips16_in_function_epilogue_p (gdbarch, pc);
6627   else if (mips_pc_is_micromips (gdbarch, pc))
6628     return micromips_in_function_epilogue_p (gdbarch, pc);
6629   else
6630     return mips32_in_function_epilogue_p (gdbarch, pc);
6631 }
6632
6633 /* Root of all "set mips "/"show mips " commands.  This will eventually be
6634    used for all MIPS-specific commands.  */
6635
6636 static void
6637 show_mips_command (char *args, int from_tty)
6638 {
6639   help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
6640 }
6641
6642 static void
6643 set_mips_command (char *args, int from_tty)
6644 {
6645   printf_unfiltered
6646     ("\"set mips\" must be followed by an appropriate subcommand.\n");
6647   help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
6648 }
6649
6650 /* Commands to show/set the MIPS FPU type.  */
6651
6652 static void
6653 show_mipsfpu_command (char *args, int from_tty)
6654 {
6655   char *fpu;
6656
6657   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
6658     {
6659       printf_unfiltered
6660         ("The MIPS floating-point coprocessor is unknown "
6661          "because the current architecture is not MIPS.\n");
6662       return;
6663     }
6664
6665   switch (MIPS_FPU_TYPE (target_gdbarch ()))
6666     {
6667     case MIPS_FPU_SINGLE:
6668       fpu = "single-precision";
6669       break;
6670     case MIPS_FPU_DOUBLE:
6671       fpu = "double-precision";
6672       break;
6673     case MIPS_FPU_NONE:
6674       fpu = "absent (none)";
6675       break;
6676     default:
6677       internal_error (__FILE__, __LINE__, _("bad switch"));
6678     }
6679   if (mips_fpu_type_auto)
6680     printf_unfiltered ("The MIPS floating-point coprocessor "
6681                        "is set automatically (currently %s)\n",
6682                        fpu);
6683   else
6684     printf_unfiltered
6685       ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
6686 }
6687
6688
6689 static void
6690 set_mipsfpu_command (char *args, int from_tty)
6691 {
6692   printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
6693                      "\"single\",\"none\" or \"auto\".\n");
6694   show_mipsfpu_command (args, from_tty);
6695 }
6696
6697 static void
6698 set_mipsfpu_single_command (char *args, int from_tty)
6699 {
6700   struct gdbarch_info info;
6701   gdbarch_info_init (&info);
6702   mips_fpu_type = MIPS_FPU_SINGLE;
6703   mips_fpu_type_auto = 0;
6704   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6705      instead of relying on globals.  Doing that would let generic code
6706      handle the search for this specific architecture.  */
6707   if (!gdbarch_update_p (info))
6708     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6709 }
6710
6711 static void
6712 set_mipsfpu_double_command (char *args, int from_tty)
6713 {
6714   struct gdbarch_info info;
6715   gdbarch_info_init (&info);
6716   mips_fpu_type = MIPS_FPU_DOUBLE;
6717   mips_fpu_type_auto = 0;
6718   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6719      instead of relying on globals.  Doing that would let generic code
6720      handle the search for this specific architecture.  */
6721   if (!gdbarch_update_p (info))
6722     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6723 }
6724
6725 static void
6726 set_mipsfpu_none_command (char *args, int from_tty)
6727 {
6728   struct gdbarch_info info;
6729   gdbarch_info_init (&info);
6730   mips_fpu_type = MIPS_FPU_NONE;
6731   mips_fpu_type_auto = 0;
6732   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6733      instead of relying on globals.  Doing that would let generic code
6734      handle the search for this specific architecture.  */
6735   if (!gdbarch_update_p (info))
6736     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6737 }
6738
6739 static void
6740 set_mipsfpu_auto_command (char *args, int from_tty)
6741 {
6742   mips_fpu_type_auto = 1;
6743 }
6744
6745 /* Attempt to identify the particular processor model by reading the
6746    processor id.  NOTE: cagney/2003-11-15: Firstly it isn't clear that
6747    the relevant processor still exists (it dates back to '94) and
6748    secondly this is not the way to do this.  The processor type should
6749    be set by forcing an architecture change.  */
6750
6751 void
6752 deprecated_mips_set_processor_regs_hack (void)
6753 {
6754   struct regcache *regcache = get_current_regcache ();
6755   struct gdbarch *gdbarch = get_regcache_arch (regcache);
6756   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6757   ULONGEST prid;
6758
6759   regcache_cooked_read_unsigned (regcache, MIPS_PRID_REGNUM, &prid);
6760   if ((prid & ~0xf) == 0x700)
6761     tdep->mips_processor_reg_names = mips_r3041_reg_names;
6762 }
6763
6764 /* Just like reinit_frame_cache, but with the right arguments to be
6765    callable as an sfunc.  */
6766
6767 static void
6768 reinit_frame_cache_sfunc (char *args, int from_tty,
6769                           struct cmd_list_element *c)
6770 {
6771   reinit_frame_cache ();
6772 }
6773
6774 static int
6775 gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
6776 {
6777   struct gdbarch *gdbarch = info->application_data;
6778
6779   /* FIXME: cagney/2003-06-26: Is this even necessary?  The
6780      disassembler needs to be able to locally determine the ISA, and
6781      not rely on GDB.  Otherwize the stand-alone 'objdump -d' will not
6782      work.  */
6783   if (mips_pc_is_mips16 (gdbarch, memaddr))
6784     info->mach = bfd_mach_mips16;
6785   else if (mips_pc_is_micromips (gdbarch, memaddr))
6786     info->mach = bfd_mach_mips_micromips;
6787
6788   /* Round down the instruction address to the appropriate boundary.  */
6789   memaddr &= (info->mach == bfd_mach_mips16
6790               || info->mach == bfd_mach_mips_micromips) ? ~1 : ~3;
6791
6792   /* Set the disassembler options.  */
6793   if (!info->disassembler_options)
6794     /* This string is not recognized explicitly by the disassembler,
6795        but it tells the disassembler to not try to guess the ABI from
6796        the bfd elf headers, such that, if the user overrides the ABI
6797        of a program linked as NewABI, the disassembly will follow the
6798        register naming conventions specified by the user.  */
6799     info->disassembler_options = "gpr-names=32";
6800
6801   /* Call the appropriate disassembler based on the target endian-ness.  */
6802   if (info->endian == BFD_ENDIAN_BIG)
6803     return print_insn_big_mips (memaddr, info);
6804   else
6805     return print_insn_little_mips (memaddr, info);
6806 }
6807
6808 static int
6809 gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
6810 {
6811   /* Set up the disassembler info, so that we get the right
6812      register names from libopcodes.  */
6813   info->disassembler_options = "gpr-names=n32";
6814   info->flavour = bfd_target_elf_flavour;
6815
6816   return gdb_print_insn_mips (memaddr, info);
6817 }
6818
6819 static int
6820 gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
6821 {
6822   /* Set up the disassembler info, so that we get the right
6823      register names from libopcodes.  */
6824   info->disassembler_options = "gpr-names=64";
6825   info->flavour = bfd_target_elf_flavour;
6826
6827   return gdb_print_insn_mips (memaddr, info);
6828 }
6829
6830 /* This function implements gdbarch_breakpoint_from_pc.  It uses the
6831    program counter value to determine whether a 16- or 32-bit breakpoint
6832    should be used.  It returns a pointer to a string of bytes that encode a
6833    breakpoint instruction, stores the length of the string to *lenptr, and
6834    adjusts pc (if necessary) to point to the actual memory location where
6835    the breakpoint should be inserted.  */
6836
6837 static const gdb_byte *
6838 mips_breakpoint_from_pc (struct gdbarch *gdbarch,
6839                          CORE_ADDR *pcptr, int *lenptr)
6840 {
6841   CORE_ADDR pc = *pcptr;
6842
6843   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6844     {
6845       if (mips_pc_is_mips16 (gdbarch, pc))
6846         {
6847           static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
6848           *pcptr = unmake_compact_addr (pc);
6849           *lenptr = sizeof (mips16_big_breakpoint);
6850           return mips16_big_breakpoint;
6851         }
6852       else if (mips_pc_is_micromips (gdbarch, pc))
6853         {
6854           static gdb_byte micromips16_big_breakpoint[] = { 0x46, 0x85 };
6855           static gdb_byte micromips32_big_breakpoint[] = { 0, 0x5, 0, 0x7 };
6856           ULONGEST insn;
6857           int status;
6858           int size;
6859
6860           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
6861           size = status ? 2
6862                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
6863           *pcptr = unmake_compact_addr (pc);
6864           *lenptr = size;
6865           return (size == 2) ? micromips16_big_breakpoint
6866                              : micromips32_big_breakpoint;
6867         }
6868       else
6869         {
6870           /* The IDT board uses an unusual breakpoint value, and
6871              sometimes gets confused when it sees the usual MIPS
6872              breakpoint instruction.  */
6873           static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
6874           static gdb_byte pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
6875           static gdb_byte idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
6876           /* Likewise, IRIX appears to expect a different breakpoint,
6877              although this is not apparent until you try to use pthreads.  */
6878           static gdb_byte irix_big_breakpoint[] = { 0, 0, 0, 0xd };
6879
6880           *lenptr = sizeof (big_breakpoint);
6881
6882           if (strcmp (target_shortname, "mips") == 0)
6883             return idt_big_breakpoint;
6884           else if (strcmp (target_shortname, "ddb") == 0
6885                    || strcmp (target_shortname, "pmon") == 0
6886                    || strcmp (target_shortname, "lsi") == 0)
6887             return pmon_big_breakpoint;
6888           else if (gdbarch_osabi (gdbarch) == GDB_OSABI_IRIX)
6889             return irix_big_breakpoint;
6890           else
6891             return big_breakpoint;
6892         }
6893     }
6894   else
6895     {
6896       if (mips_pc_is_mips16 (gdbarch, pc))
6897         {
6898           static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
6899           *pcptr = unmake_compact_addr (pc);
6900           *lenptr = sizeof (mips16_little_breakpoint);
6901           return mips16_little_breakpoint;
6902         }
6903       else if (mips_pc_is_micromips (gdbarch, pc))
6904         {
6905           static gdb_byte micromips16_little_breakpoint[] = { 0x85, 0x46 };
6906           static gdb_byte micromips32_little_breakpoint[] = { 0x5, 0, 0x7, 0 };
6907           ULONGEST insn;
6908           int status;
6909           int size;
6910
6911           insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
6912           size = status ? 2
6913                         : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
6914           *pcptr = unmake_compact_addr (pc);
6915           *lenptr = size;
6916           return (size == 2) ? micromips16_little_breakpoint
6917                              : micromips32_little_breakpoint;
6918         }
6919       else
6920         {
6921           static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
6922           static gdb_byte pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
6923           static gdb_byte idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };
6924
6925           *lenptr = sizeof (little_breakpoint);
6926
6927           if (strcmp (target_shortname, "mips") == 0)
6928             return idt_little_breakpoint;
6929           else if (strcmp (target_shortname, "ddb") == 0
6930                    || strcmp (target_shortname, "pmon") == 0
6931                    || strcmp (target_shortname, "lsi") == 0)
6932             return pmon_little_breakpoint;
6933           else
6934             return little_breakpoint;
6935         }
6936     }
6937 }
6938
6939 /* Determine the remote breakpoint kind suitable for the PC.  The following
6940    kinds are used:
6941
6942    * 2 -- 16-bit MIPS16 mode breakpoint,
6943
6944    * 3 -- 16-bit microMIPS mode breakpoint,
6945
6946    * 4 -- 32-bit standard MIPS mode breakpoint,
6947
6948    * 5 -- 32-bit microMIPS mode breakpoint.  */
6949
6950 static void
6951 mips_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
6952                                 int *kindptr)
6953 {
6954   CORE_ADDR pc = *pcptr;
6955
6956   if (mips_pc_is_mips16 (gdbarch, pc))
6957     {
6958       *pcptr = unmake_compact_addr (pc);
6959       *kindptr = 2;
6960     }
6961   else if (mips_pc_is_micromips (gdbarch, pc))
6962     {
6963       ULONGEST insn;
6964       int status;
6965       int size;
6966
6967       insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
6968       size = status ? 2 : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
6969       *pcptr = unmake_compact_addr (pc);
6970       *kindptr = size | 1;
6971     }
6972   else
6973     *kindptr = 4;
6974 }
6975
6976 /* Return non-zero if the ADDR instruction has a branch delay slot
6977    (i.e. it is a jump or branch instruction).  This function is based
6978    on mips32_next_pc.  */
6979
6980 static int
6981 mips32_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr)
6982 {
6983   unsigned long inst;
6984   int status;
6985   int op;
6986   int rs;
6987   int rt;
6988
6989   inst = mips_fetch_instruction (gdbarch, ISA_MIPS, addr, &status);
6990   if (status)
6991     return 0;
6992
6993   op = itype_op (inst);
6994   if ((inst & 0xe0000000) != 0)
6995     {
6996       rs = itype_rs (inst);
6997       rt = itype_rt (inst);
6998       return (is_octeon_bbit_op (op, gdbarch) 
6999               || op >> 2 == 5   /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx  */
7000               || op == 29       /* JALX: bits 011101  */
7001               || (op == 17
7002                   && (rs == 8
7003                                 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000  */
7004                       || (rs == 9 && (rt & 0x2) == 0)
7005                                 /* BC1ANY2F, BC1ANY2T: bits 010001 01001  */
7006                       || (rs == 10 && (rt & 0x2) == 0))));
7007                                 /* BC1ANY4F, BC1ANY4T: bits 010001 01010  */
7008     }
7009   else
7010     switch (op & 0x07)          /* extract bits 28,27,26  */
7011       {
7012       case 0:                   /* SPECIAL  */
7013         op = rtype_funct (inst);
7014         return (op == 8         /* JR  */
7015                 || op == 9);    /* JALR  */
7016         break;                  /* end SPECIAL  */
7017       case 1:                   /* REGIMM  */
7018         rs = itype_rs (inst);
7019         rt = itype_rt (inst);   /* branch condition  */
7020         return ((rt & 0xc) == 0
7021                                 /* BLTZ, BLTZL, BGEZ, BGEZL: bits 000xx  */
7022                                 /* BLTZAL, BLTZALL, BGEZAL, BGEZALL: 100xx  */
7023                 || ((rt & 0x1e) == 0x1c && rs == 0));
7024                                 /* BPOSGE32, BPOSGE64: bits 1110x  */
7025         break;                  /* end REGIMM  */
7026       default:                  /* J, JAL, BEQ, BNE, BLEZ, BGTZ  */
7027         return 1;
7028         break;
7029       }
7030 }
7031
7032 /* Return non-zero if the ADDR instruction, which must be a 32-bit
7033    instruction if MUSTBE32 is set or can be any instruction otherwise,
7034    has a branch delay slot (i.e. it is a non-compact jump instruction).  */
7035
7036 static int
7037 micromips_instruction_has_delay_slot (struct gdbarch *gdbarch,
7038                                       CORE_ADDR addr, int mustbe32)
7039 {
7040   ULONGEST insn;
7041   int status;
7042
7043   insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
7044   if (status)
7045     return 0;
7046
7047   if (!mustbe32)                /* 16-bit instructions.  */
7048     return (micromips_op (insn) == 0x11
7049                                 /* POOL16C: bits 010001 */
7050             && (b5s5_op (insn) == 0xc
7051                                 /* JR16: bits 010001 01100 */
7052                 || (b5s5_op (insn) & 0x1e) == 0xe))
7053                                 /* JALR16, JALRS16: bits 010001 0111x */
7054            || (micromips_op (insn) & 0x37) == 0x23
7055                                 /* BEQZ16, BNEZ16: bits 10x011 */
7056            || micromips_op (insn) == 0x33;
7057                                 /* B16: bits 110011 */
7058
7059                                 /* 32-bit instructions.  */
7060   if (micromips_op (insn) == 0x0)
7061                                 /* POOL32A: bits 000000 */
7062     {
7063       insn <<= 16;
7064       insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
7065       if (status)
7066         return 0;
7067       return b0s6_op (insn) == 0x3c
7068                                 /* POOL32Axf: bits 000000 ... 111100 */
7069              && (b6s10_ext (insn) & 0x2bf) == 0x3c;
7070                                 /* JALR, JALR.HB: 000000 000x111100 111100 */
7071                                 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
7072     }
7073
7074   return (micromips_op (insn) == 0x10
7075                                 /* POOL32I: bits 010000 */
7076           && ((b5s5_op (insn) & 0x1c) == 0x0
7077                                 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
7078               || (b5s5_op (insn) & 0x1d) == 0x4
7079                                 /* BLEZ, BGTZ: bits 010000 001x0 */
7080               || (b5s5_op (insn) & 0x1d) == 0x11
7081                                 /* BLTZALS, BGEZALS: bits 010000 100x1 */
7082               || ((b5s5_op (insn) & 0x1e) == 0x14
7083                   && (insn & 0x3) == 0x0)
7084                                 /* BC2F, BC2T: bits 010000 1010x xxx00 */
7085               || (b5s5_op (insn) & 0x1e) == 0x1a
7086                                 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
7087               || ((b5s5_op (insn) & 0x1e) == 0x1c
7088                   && (insn & 0x3) == 0x0)
7089                                 /* BC1F, BC1T: bits 010000 1110x xxx00 */
7090               || ((b5s5_op (insn) & 0x1c) == 0x1c
7091                   && (insn & 0x3) == 0x1)))
7092                                 /* BC1ANY*: bits 010000 111xx xxx01 */
7093          || (micromips_op (insn) & 0x1f) == 0x1d
7094                                 /* JALS, JAL: bits x11101 */
7095          || (micromips_op (insn) & 0x37) == 0x25
7096                                 /* BEQ, BNE: bits 10x101 */
7097          || micromips_op (insn) == 0x35
7098                                 /* J: bits 110101 */
7099          || micromips_op (insn) == 0x3c;
7100                                 /* JALX: bits 111100 */
7101 }
7102
7103 static int
7104 mips16_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr,
7105                                    int mustbe32)
7106 {
7107   unsigned short inst;
7108   int status;
7109
7110   inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, addr, &status);
7111   if (status)
7112     return 0;
7113
7114   if (!mustbe32)
7115     return (inst & 0xf89f) == 0xe800;   /* JR/JALR (16-bit instruction)  */
7116   return (inst & 0xf800) == 0x1800;     /* JAL/JALX (32-bit instruction)  */
7117 }
7118
7119 /* Calculate the starting address of the MIPS memory segment BPADDR is in.
7120    This assumes KSSEG exists.  */
7121
7122 static CORE_ADDR
7123 mips_segment_boundary (CORE_ADDR bpaddr)
7124 {
7125   CORE_ADDR mask = CORE_ADDR_MAX;
7126   int segsize;
7127
7128   if (sizeof (CORE_ADDR) == 8)
7129     /* Get the topmost two bits of bpaddr in a 32-bit safe manner (avoid
7130        a compiler warning produced where CORE_ADDR is a 32-bit type even
7131        though in that case this is dead code).  */
7132     switch (bpaddr >> ((sizeof (CORE_ADDR) << 3) - 2) & 3)
7133       {
7134       case 3:
7135         if (bpaddr == (bfd_signed_vma) (int32_t) bpaddr)
7136           segsize = 29;                 /* 32-bit compatibility segment  */
7137         else
7138           segsize = 62;                 /* xkseg  */
7139         break;
7140       case 2:                           /* xkphys  */
7141         segsize = 59;
7142         break;
7143       default:                          /* xksseg (1), xkuseg/kuseg (0)  */
7144         segsize = 62;
7145         break;
7146       }
7147   else if (bpaddr & 0x80000000)         /* kernel segment  */
7148     segsize = 29;
7149   else
7150     segsize = 31;                       /* user segment  */
7151   mask <<= segsize;
7152   return bpaddr & mask;
7153 }
7154
7155 /* Move the breakpoint at BPADDR out of any branch delay slot by shifting
7156    it backwards if necessary.  Return the address of the new location.  */
7157
7158 static CORE_ADDR
7159 mips_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
7160 {
7161   CORE_ADDR prev_addr;
7162   CORE_ADDR boundary;
7163   CORE_ADDR func_addr;
7164
7165   /* If a breakpoint is set on the instruction in a branch delay slot,
7166      GDB gets confused.  When the breakpoint is hit, the PC isn't on
7167      the instruction in the branch delay slot, the PC will point to
7168      the branch instruction.  Since the PC doesn't match any known
7169      breakpoints, GDB reports a trap exception.
7170
7171      There are two possible fixes for this problem.
7172
7173      1) When the breakpoint gets hit, see if the BD bit is set in the
7174      Cause register (which indicates the last exception occurred in a
7175      branch delay slot).  If the BD bit is set, fix the PC to point to
7176      the instruction in the branch delay slot.
7177
7178      2) When the user sets the breakpoint, don't allow him to set the
7179      breakpoint on the instruction in the branch delay slot.  Instead
7180      move the breakpoint to the branch instruction (which will have
7181      the same result).
7182
7183      The problem with the first solution is that if the user then
7184      single-steps the processor, the branch instruction will get
7185      skipped (since GDB thinks the PC is on the instruction in the
7186      branch delay slot).
7187
7188      So, we'll use the second solution.  To do this we need to know if
7189      the instruction we're trying to set the breakpoint on is in the
7190      branch delay slot.  */
7191
7192   boundary = mips_segment_boundary (bpaddr);
7193
7194   /* Make sure we don't scan back before the beginning of the current
7195      function, since we may fetch constant data or insns that look like
7196      a jump.  Of course we might do that anyway if the compiler has
7197      moved constants inline. :-(  */
7198   if (find_pc_partial_function (bpaddr, NULL, &func_addr, NULL)
7199       && func_addr > boundary && func_addr <= bpaddr)
7200     boundary = func_addr;
7201
7202   if (mips_pc_is_mips (bpaddr))
7203     {
7204       if (bpaddr == boundary)
7205         return bpaddr;
7206
7207       /* If the previous instruction has a branch delay slot, we have
7208          to move the breakpoint to the branch instruction. */
7209       prev_addr = bpaddr - 4;
7210       if (mips32_instruction_has_delay_slot (gdbarch, prev_addr))
7211         bpaddr = prev_addr;
7212     }
7213   else
7214     {
7215       int (*instruction_has_delay_slot) (struct gdbarch *, CORE_ADDR, int);
7216       CORE_ADDR addr, jmpaddr;
7217       int i;
7218
7219       boundary = unmake_compact_addr (boundary);
7220
7221       /* The only MIPS16 instructions with delay slots are JAL, JALX,
7222          JALR and JR.  An absolute JAL/JALX is always 4 bytes long,
7223          so try for that first, then try the 2 byte JALR/JR.
7224          The microMIPS ASE has a whole range of jumps and branches
7225          with delay slots, some of which take 4 bytes and some take
7226          2 bytes, so the idea is the same.
7227          FIXME: We have to assume that bpaddr is not the second half
7228          of an extended instruction.  */
7229       instruction_has_delay_slot = (mips_pc_is_micromips (gdbarch, bpaddr)
7230                                      ? micromips_instruction_has_delay_slot
7231                                      : mips16_instruction_has_delay_slot);
7232
7233       jmpaddr = 0;
7234       addr = bpaddr;
7235       for (i = 1; i < 4; i++)
7236         {
7237           if (unmake_compact_addr (addr) == boundary)
7238             break;
7239           addr -= MIPS_INSN16_SIZE;
7240           if (i == 1 && instruction_has_delay_slot (gdbarch, addr, 0))
7241             /* Looks like a JR/JALR at [target-1], but it could be
7242                the second word of a previous JAL/JALX, so record it
7243                and check back one more.  */
7244             jmpaddr = addr;
7245           else if (i > 1 && instruction_has_delay_slot (gdbarch, addr, 1))
7246             {
7247               if (i == 2)
7248                 /* Looks like a JAL/JALX at [target-2], but it could also
7249                    be the second word of a previous JAL/JALX, record it,
7250                    and check back one more.  */
7251                 jmpaddr = addr;
7252               else
7253                 /* Looks like a JAL/JALX at [target-3], so any previously
7254                    recorded JAL/JALX or JR/JALR must be wrong, because:
7255
7256                    >-3: JAL
7257                     -2: JAL-ext (can't be JAL/JALX)
7258                     -1: bdslot (can't be JR/JALR)
7259                      0: target insn
7260
7261                    Of course it could be another JAL-ext which looks
7262                    like a JAL, but in that case we'd have broken out
7263                    of this loop at [target-2]:
7264
7265                     -4: JAL
7266                    >-3: JAL-ext
7267                     -2: bdslot (can't be jmp)
7268                     -1: JR/JALR
7269                      0: target insn  */
7270                 jmpaddr = 0;
7271             }
7272           else
7273             {
7274               /* Not a jump instruction: if we're at [target-1] this
7275                  could be the second word of a JAL/JALX, so continue;
7276                  otherwise we're done.  */
7277               if (i > 1)
7278                 break;
7279             }
7280         }
7281
7282       if (jmpaddr)
7283         bpaddr = jmpaddr;
7284     }
7285
7286   return bpaddr;
7287 }
7288
7289 /* Return non-zero if SUFFIX is one of the numeric suffixes used for MIPS16
7290    call stubs, one of 1, 2, 5, 6, 9, 10, or, if ZERO is non-zero, also 0.  */
7291
7292 static int
7293 mips_is_stub_suffix (const char *suffix, int zero)
7294 {
7295   switch (suffix[0])
7296    {
7297    case '0':
7298      return zero && suffix[1] == '\0';
7299    case '1':
7300      return suffix[1] == '\0' || (suffix[1] == '0' && suffix[2] == '\0');
7301    case '2':
7302    case '5':
7303    case '6':
7304    case '9':
7305      return suffix[1] == '\0';
7306    default:
7307      return 0;
7308    }
7309 }
7310
7311 /* Return non-zero if MODE is one of the mode infixes used for MIPS16
7312    call stubs, one of sf, df, sc, or dc.  */
7313
7314 static int
7315 mips_is_stub_mode (const char *mode)
7316 {
7317   return ((mode[0] == 's' || mode[0] == 'd')
7318           && (mode[1] == 'f' || mode[1] == 'c'));
7319 }
7320
7321 /* Code at PC is a compiler-generated stub.  Such a stub for a function
7322    bar might have a name like __fn_stub_bar, and might look like this:
7323
7324       mfc1    $4, $f13
7325       mfc1    $5, $f12
7326       mfc1    $6, $f15
7327       mfc1    $7, $f14
7328
7329    followed by (or interspersed with):
7330
7331       j       bar
7332
7333    or:
7334
7335       lui     $25, %hi(bar)
7336       addiu   $25, $25, %lo(bar)
7337       jr      $25
7338
7339    ($1 may be used in old code; for robustness we accept any register)
7340    or, in PIC code:
7341
7342       lui     $28, %hi(_gp_disp)
7343       addiu   $28, $28, %lo(_gp_disp)
7344       addu    $28, $28, $25
7345       lw      $25, %got(bar)
7346       addiu   $25, $25, %lo(bar)
7347       jr      $25
7348
7349    In the case of a __call_stub_bar stub, the sequence to set up
7350    arguments might look like this:
7351
7352       mtc1    $4, $f13
7353       mtc1    $5, $f12
7354       mtc1    $6, $f15
7355       mtc1    $7, $f14
7356
7357    followed by (or interspersed with) one of the jump sequences above.
7358
7359    In the case of a __call_stub_fp_bar stub, JAL or JALR is used instead
7360    of J or JR, respectively, followed by:
7361
7362       mfc1    $2, $f0
7363       mfc1    $3, $f1
7364       jr      $18
7365
7366    We are at the beginning of the stub here, and scan down and extract
7367    the target address from the jump immediate instruction or, if a jump
7368    register instruction is used, from the register referred.  Return
7369    the value of PC calculated or 0 if inconclusive.
7370
7371    The limit on the search is arbitrarily set to 20 instructions.  FIXME.  */
7372
7373 static CORE_ADDR
7374 mips_get_mips16_fn_stub_pc (struct frame_info *frame, CORE_ADDR pc)
7375 {
7376   struct gdbarch *gdbarch = get_frame_arch (frame);
7377   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7378   int addrreg = MIPS_ZERO_REGNUM;
7379   CORE_ADDR start_pc = pc;
7380   CORE_ADDR target_pc = 0;
7381   CORE_ADDR addr = 0;
7382   CORE_ADDR gp = 0;
7383   int status = 0;
7384   int i;
7385
7386   for (i = 0;
7387        status == 0 && target_pc == 0 && i < 20;
7388        i++, pc += MIPS_INSN32_SIZE)
7389     {
7390       ULONGEST inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
7391       CORE_ADDR imm;
7392       int rt;
7393       int rs;
7394       int rd;
7395
7396       switch (itype_op (inst))
7397         {
7398         case 0:         /* SPECIAL */
7399           switch (rtype_funct (inst))
7400             {
7401             case 8:             /* JR */
7402             case 9:             /* JALR */
7403               rs = rtype_rs (inst);
7404               if (rs == MIPS_GP_REGNUM)
7405                 target_pc = gp;                         /* Hmm...  */
7406               else if (rs == addrreg)
7407                 target_pc = addr;
7408               break;
7409
7410             case 0x21:          /* ADDU */
7411               rt = rtype_rt (inst);
7412               rs = rtype_rs (inst);
7413               rd = rtype_rd (inst);
7414               if (rd == MIPS_GP_REGNUM
7415                   && ((rs == MIPS_GP_REGNUM && rt == MIPS_T9_REGNUM)
7416                       || (rs == MIPS_T9_REGNUM && rt == MIPS_GP_REGNUM)))
7417                 gp += start_pc;
7418               break;
7419             }
7420           break;
7421
7422         case 2:         /* J */
7423         case 3:         /* JAL */
7424           target_pc = jtype_target (inst) << 2;
7425           target_pc += ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
7426           break;
7427
7428         case 9:         /* ADDIU */
7429           rt = itype_rt (inst);
7430           rs = itype_rs (inst);
7431           if (rt == rs)
7432             {
7433               imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
7434               if (rt == MIPS_GP_REGNUM)
7435                 gp += imm;
7436               else if (rt == addrreg)
7437                 addr += imm;
7438             }
7439           break;
7440
7441         case 0xf:       /* LUI */
7442           rt = itype_rt (inst);
7443           imm = ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 16;
7444           if (rt == MIPS_GP_REGNUM)
7445             gp = imm;
7446           else if (rt != MIPS_ZERO_REGNUM)
7447             {
7448               addrreg = rt;
7449               addr = imm;
7450             }
7451           break;
7452
7453         case 0x23:      /* LW */
7454           rt = itype_rt (inst);
7455           rs = itype_rs (inst);
7456           imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
7457           if (gp != 0 && rs == MIPS_GP_REGNUM)
7458             {
7459               gdb_byte buf[4];
7460
7461               memset (buf, 0, sizeof (buf));
7462               status = target_read_memory (gp + imm, buf, sizeof (buf));
7463               addrreg = rt;
7464               addr = extract_signed_integer (buf, sizeof (buf), byte_order);
7465             }
7466           break;
7467         }
7468     }
7469
7470   return target_pc;
7471 }
7472
7473 /* If PC is in a MIPS16 call or return stub, return the address of the
7474    target PC, which is either the callee or the caller.  There are several
7475    cases which must be handled:
7476
7477    * If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
7478      and the target PC is in $31 ($ra).
7479    * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
7480      and the target PC is in $2.
7481    * If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
7482      i.e. before the JALR instruction, this is effectively a call stub
7483      and the target PC is in $2.  Otherwise this is effectively
7484      a return stub and the target PC is in $18.
7485    * If the PC is at the start of __call_stub_fp_*, i.e. before the
7486      JAL or JALR instruction, this is effectively a call stub and the
7487      target PC is buried in the instruction stream.  Otherwise this
7488      is effectively a return stub and the target PC is in $18.
7489    * If the PC is in __call_stub_* or in __fn_stub_*, this is a call
7490      stub and the target PC is buried in the instruction stream.
7491
7492    See the source code for the stubs in gcc/config/mips/mips16.S, or the
7493    stub builder in gcc/config/mips/mips.c (mips16_build_call_stub) for the
7494    gory details.  */
7495
7496 static CORE_ADDR
7497 mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7498 {
7499   struct gdbarch *gdbarch = get_frame_arch (frame);
7500   CORE_ADDR start_addr;
7501   const char *name;
7502   size_t prefixlen;
7503
7504   /* Find the starting address and name of the function containing the PC.  */
7505   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
7506     return 0;
7507
7508   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
7509      and the target PC is in $31 ($ra).  */
7510   prefixlen = strlen (mips_str_mips16_ret_stub);
7511   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
7512       && mips_is_stub_mode (name + prefixlen)
7513       && name[prefixlen + 2] == '\0')
7514     return get_frame_register_signed
7515              (frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
7516
7517   /* If the PC is in __mips16_call_stub_*, this is one of the call
7518      call/return stubs.  */
7519   prefixlen = strlen (mips_str_mips16_call_stub);
7520   if (strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0)
7521     {
7522       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
7523          and the target PC is in $2.  */
7524       if (mips_is_stub_suffix (name + prefixlen, 0))
7525         return get_frame_register_signed
7526                  (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
7527
7528       /* If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
7529          i.e. before the JALR instruction, this is effectively a call stub
7530          and the target PC is in $2.  Otherwise this is effectively
7531          a return stub and the target PC is in $18.  */
7532       else if (mips_is_stub_mode (name + prefixlen)
7533                && name[prefixlen + 2] == '_'
7534                && mips_is_stub_suffix (name + prefixlen + 3, 0))
7535         {
7536           if (pc == start_addr)
7537             /* This is the 'call' part of a call stub.  The return
7538                address is in $2.  */
7539             return get_frame_register_signed
7540                      (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
7541           else
7542             /* This is the 'return' part of a call stub.  The return
7543                address is in $18.  */
7544             return get_frame_register_signed
7545                      (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
7546         }
7547       else
7548         return 0;               /* Not a stub.  */
7549     }
7550
7551   /* If the PC is in __call_stub_* or __fn_stub*, this is one of the
7552      compiler-generated call or call/return stubs.  */
7553   if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0
7554       || strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
7555     {
7556       if (pc == start_addr)
7557         /* This is the 'call' part of a call stub.  Call this helper
7558            to scan through this code for interesting instructions
7559            and determine the final PC.  */
7560         return mips_get_mips16_fn_stub_pc (frame, pc);
7561       else
7562         /* This is the 'return' part of a call stub.  The return address
7563            is in $18.  */
7564         return get_frame_register_signed
7565                  (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
7566     }
7567
7568   return 0;                     /* Not a stub.  */
7569 }
7570
7571 /* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
7572    This implements the IN_SOLIB_RETURN_TRAMPOLINE macro.  */
7573
7574 static int
7575 mips_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc, const char *name)
7576 {
7577   CORE_ADDR start_addr;
7578   size_t prefixlen;
7579
7580   /* Find the starting address of the function containing the PC.  */
7581   if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
7582     return 0;
7583
7584   /* If the PC is in __mips16_call_stub_{s,d}{f,c}_{0..10} but not at
7585      the start, i.e. after the JALR instruction, this is effectively
7586      a return stub.  */
7587   prefixlen = strlen (mips_str_mips16_call_stub);
7588   if (pc != start_addr
7589       && strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0
7590       && mips_is_stub_mode (name + prefixlen)
7591       && name[prefixlen + 2] == '_'
7592       && mips_is_stub_suffix (name + prefixlen + 3, 1))
7593     return 1;
7594
7595   /* If the PC is in __call_stub_fp_* but not at the start, i.e. after
7596      the JAL or JALR instruction, this is effectively a return stub.  */
7597   prefixlen = strlen (mips_str_call_fp_stub);
7598   if (pc != start_addr
7599       && strncmp (name, mips_str_call_fp_stub, prefixlen) == 0)
7600     return 1;
7601
7602   /* Consume the .pic. prefix of any PIC stub, this function must return
7603      true when the PC is in a PIC stub of a __mips16_ret_{d,s}{f,c} stub
7604      or the call stub path will trigger in handle_inferior_event causing
7605      it to go astray.  */
7606   prefixlen = strlen (mips_str_pic);
7607   if (strncmp (name, mips_str_pic, prefixlen) == 0)
7608     name += prefixlen;
7609
7610   /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub.  */
7611   prefixlen = strlen (mips_str_mips16_ret_stub);
7612   if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
7613       && mips_is_stub_mode (name + prefixlen)
7614       && name[prefixlen + 2] == '\0')
7615     return 1;
7616
7617   return 0;                     /* Not a stub.  */
7618 }
7619
7620 /* If the current PC is the start of a non-PIC-to-PIC stub, return the
7621    PC of the stub target.  The stub just loads $t9 and jumps to it,
7622    so that $t9 has the correct value at function entry.  */
7623
7624 static CORE_ADDR
7625 mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7626 {
7627   struct gdbarch *gdbarch = get_frame_arch (frame);
7628   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7629   struct minimal_symbol *msym;
7630   int i;
7631   gdb_byte stub_code[16];
7632   int32_t stub_words[4];
7633
7634   /* The stub for foo is named ".pic.foo", and is either two
7635      instructions inserted before foo or a three instruction sequence
7636      which jumps to foo.  */
7637   msym = lookup_minimal_symbol_by_pc (pc);
7638   if (msym == NULL
7639       || SYMBOL_VALUE_ADDRESS (msym) != pc
7640       || SYMBOL_LINKAGE_NAME (msym) == NULL
7641       || strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) != 0)
7642     return 0;
7643
7644   /* A two-instruction header.  */
7645   if (MSYMBOL_SIZE (msym) == 8)
7646     return pc + 8;
7647
7648   /* A three-instruction (plus delay slot) trampoline.  */
7649   if (MSYMBOL_SIZE (msym) == 16)
7650     {
7651       if (target_read_memory (pc, stub_code, 16) != 0)
7652         return 0;
7653       for (i = 0; i < 4; i++)
7654         stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
7655                                                   4, byte_order);
7656
7657       /* A stub contains these instructions:
7658          lui    t9, %hi(target)
7659          j      target
7660           addiu t9, t9, %lo(target)
7661          nop
7662
7663          This works even for N64, since stubs are only generated with
7664          -msym32.  */
7665       if ((stub_words[0] & 0xffff0000U) == 0x3c190000
7666           && (stub_words[1] & 0xfc000000U) == 0x08000000
7667           && (stub_words[2] & 0xffff0000U) == 0x27390000
7668           && stub_words[3] == 0x00000000)
7669         return ((((stub_words[0] & 0x0000ffff) << 16)
7670                  + (stub_words[2] & 0x0000ffff)) ^ 0x8000) - 0x8000;
7671     }
7672
7673   /* Not a recognized stub.  */
7674   return 0;
7675 }
7676
7677 static CORE_ADDR
7678 mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7679 {
7680   CORE_ADDR requested_pc = pc;
7681   CORE_ADDR target_pc;
7682   CORE_ADDR new_pc;
7683
7684   do
7685     {
7686       target_pc = pc;
7687
7688       new_pc = mips_skip_mips16_trampoline_code (frame, pc);
7689       if (new_pc)
7690         {
7691           pc = new_pc;
7692           if (is_compact_addr (pc))
7693             pc = unmake_compact_addr (pc);
7694         }
7695
7696       new_pc = find_solib_trampoline_target (frame, pc);
7697       if (new_pc)
7698         {
7699           pc = new_pc;
7700           if (is_compact_addr (pc))
7701             pc = unmake_compact_addr (pc);
7702         }
7703
7704       new_pc = mips_skip_pic_trampoline_code (frame, pc);
7705       if (new_pc)
7706         {
7707           pc = new_pc;
7708           if (is_compact_addr (pc))
7709             pc = unmake_compact_addr (pc);
7710         }
7711     }
7712   while (pc != target_pc);
7713
7714   return pc != requested_pc ? pc : 0;
7715 }
7716
7717 /* Convert a dbx stab register number (from `r' declaration) to a GDB
7718    [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
7719
7720 static int
7721 mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
7722 {
7723   int regnum;
7724   if (num >= 0 && num < 32)
7725     regnum = num;
7726   else if (num >= 38 && num < 70)
7727     regnum = num + mips_regnum (gdbarch)->fp0 - 38;
7728   else if (num == 70)
7729     regnum = mips_regnum (gdbarch)->hi;
7730   else if (num == 71)
7731     regnum = mips_regnum (gdbarch)->lo;
7732   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 72 && num < 78)
7733     regnum = num + mips_regnum (gdbarch)->dspacc - 72;
7734   else
7735     /* This will hopefully (eventually) provoke a warning.  Should
7736        we be calling complaint() here?  */
7737     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
7738   return gdbarch_num_regs (gdbarch) + regnum;
7739 }
7740
7741
7742 /* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
7743    gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
7744
7745 static int
7746 mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
7747 {
7748   int regnum;
7749   if (num >= 0 && num < 32)
7750     regnum = num;
7751   else if (num >= 32 && num < 64)
7752     regnum = num + mips_regnum (gdbarch)->fp0 - 32;
7753   else if (num == 64)
7754     regnum = mips_regnum (gdbarch)->hi;
7755   else if (num == 65)
7756     regnum = mips_regnum (gdbarch)->lo;
7757   else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 66 && num < 72)
7758     regnum = num + mips_regnum (gdbarch)->dspacc - 66;
7759   else
7760     /* This will hopefully (eventually) provoke a warning.  Should we
7761        be calling complaint() here?  */
7762     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
7763   return gdbarch_num_regs (gdbarch) + regnum;
7764 }
7765
7766 static int
7767 mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
7768 {
7769   /* Only makes sense to supply raw registers.  */
7770   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
7771   /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
7772      decide if it is valid.  Should instead define a standard sim/gdb
7773      register numbering scheme.  */
7774   if (gdbarch_register_name (gdbarch,
7775                              gdbarch_num_regs (gdbarch) + regnum) != NULL
7776       && gdbarch_register_name (gdbarch,
7777                                 gdbarch_num_regs (gdbarch)
7778                                 + regnum)[0] != '\0')
7779     return regnum;
7780   else
7781     return LEGACY_SIM_REGNO_IGNORE;
7782 }
7783
7784
7785 /* Convert an integer into an address.  Extracting the value signed
7786    guarantees a correctly sign extended address.  */
7787
7788 static CORE_ADDR
7789 mips_integer_to_address (struct gdbarch *gdbarch,
7790                          struct type *type, const gdb_byte *buf)
7791 {
7792   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7793   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
7794 }
7795
7796 /* Dummy virtual frame pointer method.  This is no more or less accurate
7797    than most other architectures; we just need to be explicit about it,
7798    because the pseudo-register gdbarch_sp_regnum will otherwise lead to
7799    an assertion failure.  */
7800
7801 static void
7802 mips_virtual_frame_pointer (struct gdbarch *gdbarch, 
7803                             CORE_ADDR pc, int *reg, LONGEST *offset)
7804 {
7805   *reg = MIPS_SP_REGNUM;
7806   *offset = 0;
7807 }
7808
7809 static void
7810 mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
7811 {
7812   enum mips_abi *abip = (enum mips_abi *) obj;
7813   const char *name = bfd_get_section_name (abfd, sect);
7814
7815   if (*abip != MIPS_ABI_UNKNOWN)
7816     return;
7817
7818   if (strncmp (name, ".mdebug.", 8) != 0)
7819     return;
7820
7821   if (strcmp (name, ".mdebug.abi32") == 0)
7822     *abip = MIPS_ABI_O32;
7823   else if (strcmp (name, ".mdebug.abiN32") == 0)
7824     *abip = MIPS_ABI_N32;
7825   else if (strcmp (name, ".mdebug.abi64") == 0)
7826     *abip = MIPS_ABI_N64;
7827   else if (strcmp (name, ".mdebug.abiO64") == 0)
7828     *abip = MIPS_ABI_O64;
7829   else if (strcmp (name, ".mdebug.eabi32") == 0)
7830     *abip = MIPS_ABI_EABI32;
7831   else if (strcmp (name, ".mdebug.eabi64") == 0)
7832     *abip = MIPS_ABI_EABI64;
7833   else
7834     warning (_("unsupported ABI %s."), name + 8);
7835 }
7836
7837 static void
7838 mips_find_long_section (bfd *abfd, asection *sect, void *obj)
7839 {
7840   int *lbp = (int *) obj;
7841   const char *name = bfd_get_section_name (abfd, sect);
7842
7843   if (strncmp (name, ".gcc_compiled_long32", 20) == 0)
7844     *lbp = 32;
7845   else if (strncmp (name, ".gcc_compiled_long64", 20) == 0)
7846     *lbp = 64;
7847   else if (strncmp (name, ".gcc_compiled_long", 18) == 0)
7848     warning (_("unrecognized .gcc_compiled_longXX"));
7849 }
7850
7851 static enum mips_abi
7852 global_mips_abi (void)
7853 {
7854   int i;
7855
7856   for (i = 0; mips_abi_strings[i] != NULL; i++)
7857     if (mips_abi_strings[i] == mips_abi_string)
7858       return (enum mips_abi) i;
7859
7860   internal_error (__FILE__, __LINE__, _("unknown ABI string"));
7861 }
7862
7863 /* Return the default compressed instruction set, either of MIPS16
7864    or microMIPS, selected when none could have been determined from
7865    the ELF header of the binary being executed (or no binary has been
7866    selected.  */
7867
7868 static enum mips_isa
7869 global_mips_compression (void)
7870 {
7871   int i;
7872
7873   for (i = 0; mips_compression_strings[i] != NULL; i++)
7874     if (mips_compression_strings[i] == mips_compression_string)
7875       return (enum mips_isa) i;
7876
7877   internal_error (__FILE__, __LINE__, _("unknown compressed ISA string"));
7878 }
7879
7880 static void
7881 mips_register_g_packet_guesses (struct gdbarch *gdbarch)
7882 {
7883   /* If the size matches the set of 32-bit or 64-bit integer registers,
7884      assume that's what we've got.  */
7885   register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
7886   register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);
7887
7888   /* If the size matches the full set of registers GDB traditionally
7889      knows about, including floating point, for either 32-bit or
7890      64-bit, assume that's what we've got.  */
7891   register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
7892   register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);
7893
7894   /* Otherwise we don't have a useful guess.  */
7895 }
7896
7897 static struct value *
7898 value_of_mips_user_reg (struct frame_info *frame, const void *baton)
7899 {
7900   const int *reg_p = baton;
7901   return value_of_register (*reg_p, frame);
7902 }
7903
7904 static struct gdbarch *
7905 mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
7906 {
7907   struct gdbarch *gdbarch;
7908   struct gdbarch_tdep *tdep;
7909   int elf_flags;
7910   enum mips_abi mips_abi, found_abi, wanted_abi;
7911   int i, num_regs;
7912   enum mips_fpu_type fpu_type;
7913   struct tdesc_arch_data *tdesc_data = NULL;
7914   int elf_fpu_type = 0;
7915   const char **reg_names;
7916   struct mips_regnum mips_regnum, *regnum;
7917   enum mips_isa mips_isa;
7918   int dspacc;
7919   int dspctl;
7920
7921   /* Fill in the OS dependent register numbers and names.  */
7922   if (info.osabi == GDB_OSABI_IRIX)
7923     {
7924       mips_regnum.fp0 = 32;
7925       mips_regnum.pc = 64;
7926       mips_regnum.cause = 65;
7927       mips_regnum.badvaddr = 66;
7928       mips_regnum.hi = 67;
7929       mips_regnum.lo = 68;
7930       mips_regnum.fp_control_status = 69;
7931       mips_regnum.fp_implementation_revision = 70;
7932       mips_regnum.dspacc = dspacc = -1;
7933       mips_regnum.dspctl = dspctl = -1;
7934       num_regs = 71;
7935       reg_names = mips_irix_reg_names;
7936     }
7937   else if (info.osabi == GDB_OSABI_LINUX)
7938     {
7939       mips_regnum.fp0 = 38;
7940       mips_regnum.pc = 37;
7941       mips_regnum.cause = 36;
7942       mips_regnum.badvaddr = 35;
7943       mips_regnum.hi = 34;
7944       mips_regnum.lo = 33;
7945       mips_regnum.fp_control_status = 70;
7946       mips_regnum.fp_implementation_revision = 71;
7947       mips_regnum.dspacc = -1;
7948       mips_regnum.dspctl = -1;
7949       dspacc = 72;
7950       dspctl = 78;
7951       num_regs = 79;
7952       reg_names = mips_linux_reg_names;
7953     }
7954   else
7955     {
7956       mips_regnum.lo = MIPS_EMBED_LO_REGNUM;
7957       mips_regnum.hi = MIPS_EMBED_HI_REGNUM;
7958       mips_regnum.badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
7959       mips_regnum.cause = MIPS_EMBED_CAUSE_REGNUM;
7960       mips_regnum.pc = MIPS_EMBED_PC_REGNUM;
7961       mips_regnum.fp0 = MIPS_EMBED_FP0_REGNUM;
7962       mips_regnum.fp_control_status = 70;
7963       mips_regnum.fp_implementation_revision = 71;
7964       mips_regnum.dspacc = dspacc = -1;
7965       mips_regnum.dspctl = dspctl = -1;
7966       num_regs = MIPS_LAST_EMBED_REGNUM + 1;
7967       if (info.bfd_arch_info != NULL
7968           && info.bfd_arch_info->mach == bfd_mach_mips3900)
7969         reg_names = mips_tx39_reg_names;
7970       else
7971         reg_names = mips_generic_reg_names;
7972     }
7973
7974   /* Check any target description for validity.  */
7975   if (tdesc_has_registers (info.target_desc))
7976     {
7977       static const char *const mips_gprs[] = {
7978         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
7979         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
7980         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
7981         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
7982       };
7983       static const char *const mips_fprs[] = {
7984         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
7985         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
7986         "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
7987         "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
7988       };
7989
7990       const struct tdesc_feature *feature;
7991       int valid_p;
7992
7993       feature = tdesc_find_feature (info.target_desc,
7994                                     "org.gnu.gdb.mips.cpu");
7995       if (feature == NULL)
7996         return NULL;
7997
7998       tdesc_data = tdesc_data_alloc ();
7999
8000       valid_p = 1;
8001       for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
8002         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
8003                                             mips_gprs[i]);
8004
8005
8006       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8007                                           mips_regnum.lo, "lo");
8008       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8009                                           mips_regnum.hi, "hi");
8010       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8011                                           mips_regnum.pc, "pc");
8012
8013       if (!valid_p)
8014         {
8015           tdesc_data_cleanup (tdesc_data);
8016           return NULL;
8017         }
8018
8019       feature = tdesc_find_feature (info.target_desc,
8020                                     "org.gnu.gdb.mips.cp0");
8021       if (feature == NULL)
8022         {
8023           tdesc_data_cleanup (tdesc_data);
8024           return NULL;
8025         }
8026
8027       valid_p = 1;
8028       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8029                                           mips_regnum.badvaddr, "badvaddr");
8030       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8031                                           MIPS_PS_REGNUM, "status");
8032       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8033                                           mips_regnum.cause, "cause");
8034
8035       if (!valid_p)
8036         {
8037           tdesc_data_cleanup (tdesc_data);
8038           return NULL;
8039         }
8040
8041       /* FIXME drow/2007-05-17: The FPU should be optional.  The MIPS
8042          backend is not prepared for that, though.  */
8043       feature = tdesc_find_feature (info.target_desc,
8044                                     "org.gnu.gdb.mips.fpu");
8045       if (feature == NULL)
8046         {
8047           tdesc_data_cleanup (tdesc_data);
8048           return NULL;
8049         }
8050
8051       valid_p = 1;
8052       for (i = 0; i < 32; i++)
8053         valid_p &= tdesc_numbered_register (feature, tdesc_data,
8054                                             i + mips_regnum.fp0, mips_fprs[i]);
8055
8056       valid_p &= tdesc_numbered_register (feature, tdesc_data,
8057                                           mips_regnum.fp_control_status,
8058                                           "fcsr");
8059       valid_p
8060         &= tdesc_numbered_register (feature, tdesc_data,
8061                                     mips_regnum.fp_implementation_revision,
8062                                     "fir");
8063
8064       if (!valid_p)
8065         {
8066           tdesc_data_cleanup (tdesc_data);
8067           return NULL;
8068         }
8069
8070       if (dspacc >= 0)
8071         {
8072           feature = tdesc_find_feature (info.target_desc,
8073                                         "org.gnu.gdb.mips.dsp");
8074           /* The DSP registers are optional; it's OK if they are absent.  */
8075           if (feature != NULL)
8076             {
8077               i = 0;
8078               valid_p = 1;
8079               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8080                                                   dspacc + i++, "hi1");
8081               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8082                                                   dspacc + i++, "lo1");
8083               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8084                                                   dspacc + i++, "hi2");
8085               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8086                                                   dspacc + i++, "lo2");
8087               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8088                                                   dspacc + i++, "hi3");
8089               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8090                                                   dspacc + i++, "lo3");
8091
8092               valid_p &= tdesc_numbered_register (feature, tdesc_data,
8093                                                   dspctl, "dspctl");
8094
8095               if (!valid_p)
8096                 {
8097                   tdesc_data_cleanup (tdesc_data);
8098                   return NULL;
8099                 }
8100
8101               mips_regnum.dspacc = dspacc;
8102               mips_regnum.dspctl = dspctl;
8103             }
8104         }
8105
8106       /* It would be nice to detect an attempt to use a 64-bit ABI
8107          when only 32-bit registers are provided.  */
8108       reg_names = NULL;
8109     }
8110
8111   /* First of all, extract the elf_flags, if available.  */
8112   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
8113     elf_flags = elf_elfheader (info.abfd)->e_flags;
8114   else if (arches != NULL)
8115     elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
8116   else
8117     elf_flags = 0;
8118   if (gdbarch_debug)
8119     fprintf_unfiltered (gdb_stdlog,
8120                         "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
8121
8122   /* Check ELF_FLAGS to see if it specifies the ABI being used.  */
8123   switch ((elf_flags & EF_MIPS_ABI))
8124     {
8125     case E_MIPS_ABI_O32:
8126       found_abi = MIPS_ABI_O32;
8127       break;
8128     case E_MIPS_ABI_O64:
8129       found_abi = MIPS_ABI_O64;
8130       break;
8131     case E_MIPS_ABI_EABI32:
8132       found_abi = MIPS_ABI_EABI32;
8133       break;
8134     case E_MIPS_ABI_EABI64:
8135       found_abi = MIPS_ABI_EABI64;
8136       break;
8137     default:
8138       if ((elf_flags & EF_MIPS_ABI2))
8139         found_abi = MIPS_ABI_N32;
8140       else
8141         found_abi = MIPS_ABI_UNKNOWN;
8142       break;
8143     }
8144
8145   /* GCC creates a pseudo-section whose name describes the ABI.  */
8146   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
8147     bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
8148
8149   /* If we have no useful BFD information, use the ABI from the last
8150      MIPS architecture (if there is one).  */
8151   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
8152     found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
8153
8154   /* Try the architecture for any hint of the correct ABI.  */
8155   if (found_abi == MIPS_ABI_UNKNOWN
8156       && info.bfd_arch_info != NULL
8157       && info.bfd_arch_info->arch == bfd_arch_mips)
8158     {
8159       switch (info.bfd_arch_info->mach)
8160         {
8161         case bfd_mach_mips3900:
8162           found_abi = MIPS_ABI_EABI32;
8163           break;
8164         case bfd_mach_mips4100:
8165         case bfd_mach_mips5000:
8166           found_abi = MIPS_ABI_EABI64;
8167           break;
8168         case bfd_mach_mips8000:
8169         case bfd_mach_mips10000:
8170           /* On Irix, ELF64 executables use the N64 ABI.  The
8171              pseudo-sections which describe the ABI aren't present
8172              on IRIX.  (Even for executables created by gcc.)  */
8173           if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
8174               && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
8175             found_abi = MIPS_ABI_N64;
8176           else
8177             found_abi = MIPS_ABI_N32;
8178           break;
8179         }
8180     }
8181
8182   /* Default 64-bit objects to N64 instead of O32.  */
8183   if (found_abi == MIPS_ABI_UNKNOWN
8184       && info.abfd != NULL
8185       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
8186       && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
8187     found_abi = MIPS_ABI_N64;
8188
8189   if (gdbarch_debug)
8190     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
8191                         found_abi);
8192
8193   /* What has the user specified from the command line?  */
8194   wanted_abi = global_mips_abi ();
8195   if (gdbarch_debug)
8196     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
8197                         wanted_abi);
8198
8199   /* Now that we have found what the ABI for this binary would be,
8200      check whether the user is overriding it.  */
8201   if (wanted_abi != MIPS_ABI_UNKNOWN)
8202     mips_abi = wanted_abi;
8203   else if (found_abi != MIPS_ABI_UNKNOWN)
8204     mips_abi = found_abi;
8205   else
8206     mips_abi = MIPS_ABI_O32;
8207   if (gdbarch_debug)
8208     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
8209                         mips_abi);
8210
8211   /* Determine the default compressed ISA.  */
8212   if ((elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0
8213       && (elf_flags & EF_MIPS_ARCH_ASE_M16) == 0)
8214     mips_isa = ISA_MICROMIPS;
8215   else if ((elf_flags & EF_MIPS_ARCH_ASE_M16) != 0
8216            && (elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) == 0)
8217     mips_isa = ISA_MIPS16;
8218   else
8219     mips_isa = global_mips_compression ();
8220   mips_compression_string = mips_compression_strings[mips_isa];
8221
8222   /* Also used when doing an architecture lookup.  */
8223   if (gdbarch_debug)
8224     fprintf_unfiltered (gdb_stdlog,
8225                         "mips_gdbarch_init: "
8226                         "mips64_transfers_32bit_regs_p = %d\n",
8227                         mips64_transfers_32bit_regs_p);
8228
8229   /* Determine the MIPS FPU type.  */
8230 #ifdef HAVE_ELF
8231   if (info.abfd
8232       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
8233     elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
8234                                              Tag_GNU_MIPS_ABI_FP);
8235 #endif /* HAVE_ELF */
8236
8237   if (!mips_fpu_type_auto)
8238     fpu_type = mips_fpu_type;
8239   else if (elf_fpu_type != 0)
8240     {
8241       switch (elf_fpu_type)
8242         {
8243         case 1:
8244           fpu_type = MIPS_FPU_DOUBLE;
8245           break;
8246         case 2:
8247           fpu_type = MIPS_FPU_SINGLE;
8248           break;
8249         case 3:
8250         default:
8251           /* Soft float or unknown.  */
8252           fpu_type = MIPS_FPU_NONE;
8253           break;
8254         }
8255     }
8256   else if (info.bfd_arch_info != NULL
8257            && info.bfd_arch_info->arch == bfd_arch_mips)
8258     switch (info.bfd_arch_info->mach)
8259       {
8260       case bfd_mach_mips3900:
8261       case bfd_mach_mips4100:
8262       case bfd_mach_mips4111:
8263       case bfd_mach_mips4120:
8264         fpu_type = MIPS_FPU_NONE;
8265         break;
8266       case bfd_mach_mips4650:
8267         fpu_type = MIPS_FPU_SINGLE;
8268         break;
8269       default:
8270         fpu_type = MIPS_FPU_DOUBLE;
8271         break;
8272       }
8273   else if (arches != NULL)
8274     fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
8275   else
8276     fpu_type = MIPS_FPU_DOUBLE;
8277   if (gdbarch_debug)
8278     fprintf_unfiltered (gdb_stdlog,
8279                         "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
8280
8281   /* Check for blatant incompatibilities.  */
8282
8283   /* If we have only 32-bit registers, then we can't debug a 64-bit
8284      ABI.  */
8285   if (info.target_desc
8286       && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
8287       && mips_abi != MIPS_ABI_EABI32
8288       && mips_abi != MIPS_ABI_O32)
8289     {
8290       if (tdesc_data != NULL)
8291         tdesc_data_cleanup (tdesc_data);
8292       return NULL;
8293     }
8294
8295   /* Try to find a pre-existing architecture.  */
8296   for (arches = gdbarch_list_lookup_by_info (arches, &info);
8297        arches != NULL;
8298        arches = gdbarch_list_lookup_by_info (arches->next, &info))
8299     {
8300       /* MIPS needs to be pedantic about which ABI the object is
8301          using.  */
8302       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
8303         continue;
8304       if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
8305         continue;
8306       /* Need to be pedantic about which register virtual size is
8307          used.  */
8308       if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
8309           != mips64_transfers_32bit_regs_p)
8310         continue;
8311       /* Be pedantic about which FPU is selected.  */
8312       if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
8313         continue;
8314
8315       if (tdesc_data != NULL)
8316         tdesc_data_cleanup (tdesc_data);
8317       return arches->gdbarch;
8318     }
8319
8320   /* Need a new architecture.  Fill in a target specific vector.  */
8321   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
8322   gdbarch = gdbarch_alloc (&info, tdep);
8323   tdep->elf_flags = elf_flags;
8324   tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
8325   tdep->found_abi = found_abi;
8326   tdep->mips_abi = mips_abi;
8327   tdep->mips_isa = mips_isa;
8328   tdep->mips_fpu_type = fpu_type;
8329   tdep->register_size_valid_p = 0;
8330   tdep->register_size = 0;
8331   tdep->gregset = NULL;
8332   tdep->gregset64 = NULL;
8333   tdep->fpregset = NULL;
8334   tdep->fpregset64 = NULL;
8335
8336   if (info.target_desc)
8337     {
8338       /* Some useful properties can be inferred from the target.  */
8339       if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
8340         {
8341           tdep->register_size_valid_p = 1;
8342           tdep->register_size = 4;
8343         }
8344       else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
8345         {
8346           tdep->register_size_valid_p = 1;
8347           tdep->register_size = 8;
8348         }
8349     }
8350
8351   /* Initially set everything according to the default ABI/ISA.  */
8352   set_gdbarch_short_bit (gdbarch, 16);
8353   set_gdbarch_int_bit (gdbarch, 32);
8354   set_gdbarch_float_bit (gdbarch, 32);
8355   set_gdbarch_double_bit (gdbarch, 64);
8356   set_gdbarch_long_double_bit (gdbarch, 64);
8357   set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
8358   set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
8359   set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
8360
8361   set_gdbarch_ax_pseudo_register_collect (gdbarch,
8362                                           mips_ax_pseudo_register_collect);
8363   set_gdbarch_ax_pseudo_register_push_stack
8364       (gdbarch, mips_ax_pseudo_register_push_stack);
8365
8366   set_gdbarch_elf_make_msymbol_special (gdbarch,
8367                                         mips_elf_make_msymbol_special);
8368
8369   regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct mips_regnum);
8370   *regnum = mips_regnum;
8371   set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
8372   set_gdbarch_num_regs (gdbarch, num_regs);
8373   set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
8374   set_gdbarch_register_name (gdbarch, mips_register_name);
8375   set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
8376   tdep->mips_processor_reg_names = reg_names;
8377   tdep->regnum = regnum;
8378
8379   switch (mips_abi)
8380     {
8381     case MIPS_ABI_O32:
8382       set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
8383       set_gdbarch_return_value (gdbarch, mips_o32_return_value);
8384       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
8385       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
8386       tdep->default_mask_address_p = 0;
8387       set_gdbarch_long_bit (gdbarch, 32);
8388       set_gdbarch_ptr_bit (gdbarch, 32);
8389       set_gdbarch_long_long_bit (gdbarch, 64);
8390       break;
8391     case MIPS_ABI_O64:
8392       set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
8393       set_gdbarch_return_value (gdbarch, mips_o64_return_value);
8394       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
8395       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
8396       tdep->default_mask_address_p = 0;
8397       set_gdbarch_long_bit (gdbarch, 32);
8398       set_gdbarch_ptr_bit (gdbarch, 32);
8399       set_gdbarch_long_long_bit (gdbarch, 64);
8400       break;
8401     case MIPS_ABI_EABI32:
8402       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
8403       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
8404       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8405       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8406       tdep->default_mask_address_p = 0;
8407       set_gdbarch_long_bit (gdbarch, 32);
8408       set_gdbarch_ptr_bit (gdbarch, 32);
8409       set_gdbarch_long_long_bit (gdbarch, 64);
8410       break;
8411     case MIPS_ABI_EABI64:
8412       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
8413       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
8414       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8415       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8416       tdep->default_mask_address_p = 0;
8417       set_gdbarch_long_bit (gdbarch, 64);
8418       set_gdbarch_ptr_bit (gdbarch, 64);
8419       set_gdbarch_long_long_bit (gdbarch, 64);
8420       break;
8421     case MIPS_ABI_N32:
8422       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
8423       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
8424       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8425       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8426       tdep->default_mask_address_p = 0;
8427       set_gdbarch_long_bit (gdbarch, 32);
8428       set_gdbarch_ptr_bit (gdbarch, 32);
8429       set_gdbarch_long_long_bit (gdbarch, 64);
8430       set_gdbarch_long_double_bit (gdbarch, 128);
8431       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
8432       break;
8433     case MIPS_ABI_N64:
8434       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
8435       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
8436       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8437       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8438       tdep->default_mask_address_p = 0;
8439       set_gdbarch_long_bit (gdbarch, 64);
8440       set_gdbarch_ptr_bit (gdbarch, 64);
8441       set_gdbarch_long_long_bit (gdbarch, 64);
8442       set_gdbarch_long_double_bit (gdbarch, 128);
8443       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
8444       break;
8445     default:
8446       internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
8447     }
8448
8449   /* GCC creates a pseudo-section whose name specifies the size of
8450      longs, since -mlong32 or -mlong64 may be used independent of
8451      other options.  How those options affect pointer sizes is ABI and
8452      architecture dependent, so use them to override the default sizes
8453      set by the ABI.  This table shows the relationship between ABI,
8454      -mlongXX, and size of pointers:
8455
8456      ABI                -mlongXX        ptr bits
8457      ---                --------        --------
8458      o32                32              32
8459      o32                64              32
8460      n32                32              32
8461      n32                64              64
8462      o64                32              32
8463      o64                64              64
8464      n64                32              32
8465      n64                64              64
8466      eabi32             32              32
8467      eabi32             64              32
8468      eabi64             32              32
8469      eabi64             64              64
8470
8471     Note that for o32 and eabi32, pointers are always 32 bits
8472     regardless of any -mlongXX option.  For all others, pointers and
8473     longs are the same, as set by -mlongXX or set by defaults.  */
8474
8475   if (info.abfd != NULL)
8476     {
8477       int long_bit = 0;
8478
8479       bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
8480       if (long_bit)
8481         {
8482           set_gdbarch_long_bit (gdbarch, long_bit);
8483           switch (mips_abi)
8484             {
8485             case MIPS_ABI_O32:
8486             case MIPS_ABI_EABI32:
8487               break;
8488             case MIPS_ABI_N32:
8489             case MIPS_ABI_O64:
8490             case MIPS_ABI_N64:
8491             case MIPS_ABI_EABI64:
8492               set_gdbarch_ptr_bit (gdbarch, long_bit);
8493               break;
8494             default:
8495               internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
8496             }
8497         }
8498     }
8499
8500   /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
8501      that could indicate -gp32 BUT gas/config/tc-mips.c contains the
8502      comment:
8503
8504      ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
8505      flag in object files because to do so would make it impossible to
8506      link with libraries compiled without "-gp32".  This is
8507      unnecessarily restrictive.
8508
8509      We could solve this problem by adding "-gp32" multilibs to gcc,
8510      but to set this flag before gcc is built with such multilibs will
8511      break too many systems.''
8512
8513      But even more unhelpfully, the default linker output target for
8514      mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
8515      for 64-bit programs - you need to change the ABI to change this,
8516      and not all gcc targets support that currently.  Therefore using
8517      this flag to detect 32-bit mode would do the wrong thing given
8518      the current gcc - it would make GDB treat these 64-bit programs
8519      as 32-bit programs by default.  */
8520
8521   set_gdbarch_read_pc (gdbarch, mips_read_pc);
8522   set_gdbarch_write_pc (gdbarch, mips_write_pc);
8523
8524   /* Add/remove bits from an address.  The MIPS needs be careful to
8525      ensure that all 32 bit addresses are sign extended to 64 bits.  */
8526   set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
8527
8528   /* Unwind the frame.  */
8529   set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
8530   set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
8531   set_gdbarch_dummy_id (gdbarch, mips_dummy_id);
8532
8533   /* Map debug register numbers onto internal register numbers.  */
8534   set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
8535   set_gdbarch_ecoff_reg_to_regnum (gdbarch,
8536                                    mips_dwarf_dwarf2_ecoff_reg_to_regnum);
8537   set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
8538                                     mips_dwarf_dwarf2_ecoff_reg_to_regnum);
8539   set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
8540
8541   /* MIPS version of CALL_DUMMY.  */
8542
8543   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
8544   set_gdbarch_push_dummy_code (gdbarch, mips_push_dummy_code);
8545   set_gdbarch_frame_align (gdbarch, mips_frame_align);
8546
8547   set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
8548   set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
8549   set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
8550
8551   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
8552   set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
8553   set_gdbarch_remote_breakpoint_from_pc (gdbarch,
8554                                          mips_remote_breakpoint_from_pc);
8555   set_gdbarch_adjust_breakpoint_address (gdbarch,
8556                                          mips_adjust_breakpoint_address);
8557
8558   set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
8559
8560   set_gdbarch_in_function_epilogue_p (gdbarch, mips_in_function_epilogue_p);
8561
8562   set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
8563   set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
8564   set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
8565
8566   set_gdbarch_register_type (gdbarch, mips_register_type);
8567
8568   set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
8569
8570   if (mips_abi == MIPS_ABI_N32)
8571     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
8572   else if (mips_abi == MIPS_ABI_N64)
8573     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
8574   else
8575     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
8576
8577   /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
8578      HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
8579      need to all be folded into the target vector.  Since they are
8580      being used as guards for target_stopped_by_watchpoint, why not have
8581      target_stopped_by_watchpoint return the type of watchpoint that the code
8582      is sitting on?  */
8583   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
8584
8585   set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
8586
8587   /* NOTE drow/2012-04-25: We overload the core solib trampoline code
8588      to support MIPS16.  This is a bad thing.  Make sure not to do it
8589      if we have an OS ABI that actually supports shared libraries, since
8590      shared library support is more important.  If we have an OS someday
8591      that supports both shared libraries and MIPS16, we'll have to find
8592      a better place for these.
8593      macro/2012-04-25: But that applies to return trampolines only and
8594      currently no MIPS OS ABI uses shared libraries that have them.  */
8595   set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);
8596
8597   set_gdbarch_single_step_through_delay (gdbarch,
8598                                          mips_single_step_through_delay);
8599
8600   /* Virtual tables.  */
8601   set_gdbarch_vbit_in_delta (gdbarch, 1);
8602
8603   mips_register_g_packet_guesses (gdbarch);
8604
8605   /* Hook in OS ABI-specific overrides, if they have been registered.  */
8606   info.tdep_info = (void *) tdesc_data;
8607   gdbarch_init_osabi (info, gdbarch);
8608
8609   /* The hook may have adjusted num_regs, fetch the final value and
8610      set pc_regnum and sp_regnum now that it has been fixed.  */
8611   num_regs = gdbarch_num_regs (gdbarch);
8612   set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
8613   set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
8614
8615   /* Unwind the frame.  */
8616   dwarf2_append_unwinders (gdbarch);
8617   frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
8618   frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
8619   frame_unwind_append_unwinder (gdbarch, &mips_micro_frame_unwind);
8620   frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
8621   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
8622   frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
8623   frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
8624   frame_base_append_sniffer (gdbarch, mips_micro_frame_base_sniffer);
8625   frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
8626
8627   if (tdesc_data)
8628     {
8629       set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
8630       tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
8631
8632       /* Override the normal target description methods to handle our
8633          dual real and pseudo registers.  */
8634       set_gdbarch_register_name (gdbarch, mips_register_name);
8635       set_gdbarch_register_reggroup_p (gdbarch,
8636                                        mips_tdesc_register_reggroup_p);
8637
8638       num_regs = gdbarch_num_regs (gdbarch);
8639       set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
8640       set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
8641       set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
8642     }
8643
8644   /* Add ABI-specific aliases for the registers.  */
8645   if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
8646     for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
8647       user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
8648                     value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
8649   else
8650     for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
8651       user_reg_add (gdbarch, mips_o32_aliases[i].name,
8652                     value_of_mips_user_reg, &mips_o32_aliases[i].regnum);
8653
8654   /* Add some other standard aliases.  */
8655   for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
8656     user_reg_add (gdbarch, mips_register_aliases[i].name,
8657                   value_of_mips_user_reg, &mips_register_aliases[i].regnum);
8658
8659   for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
8660     user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
8661                   value_of_mips_user_reg, 
8662                   &mips_numeric_register_aliases[i].regnum);
8663
8664   return gdbarch;
8665 }
8666
8667 static void
8668 mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
8669 {
8670   struct gdbarch_info info;
8671
8672   /* Force the architecture to update, and (if it's a MIPS architecture)
8673      mips_gdbarch_init will take care of the rest.  */
8674   gdbarch_info_init (&info);
8675   gdbarch_update_p (info);
8676 }
8677
8678 /* Print out which MIPS ABI is in use.  */
8679
8680 static void
8681 show_mips_abi (struct ui_file *file,
8682                int from_tty,
8683                struct cmd_list_element *ignored_cmd,
8684                const char *ignored_value)
8685 {
8686   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
8687     fprintf_filtered
8688       (file, 
8689        "The MIPS ABI is unknown because the current architecture "
8690        "is not MIPS.\n");
8691   else
8692     {
8693       enum mips_abi global_abi = global_mips_abi ();
8694       enum mips_abi actual_abi = mips_abi (target_gdbarch ());
8695       const char *actual_abi_str = mips_abi_strings[actual_abi];
8696
8697       if (global_abi == MIPS_ABI_UNKNOWN)
8698         fprintf_filtered
8699           (file, 
8700            "The MIPS ABI is set automatically (currently \"%s\").\n",
8701            actual_abi_str);
8702       else if (global_abi == actual_abi)
8703         fprintf_filtered
8704           (file,
8705            "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
8706            actual_abi_str);
8707       else
8708         {
8709           /* Probably shouldn't happen...  */
8710           fprintf_filtered (file,
8711                             "The (auto detected) MIPS ABI \"%s\" is in use "
8712                             "even though the user setting was \"%s\".\n",
8713              actual_abi_str, mips_abi_strings[global_abi]);
8714         }
8715     }
8716 }
8717
8718 /* Print out which MIPS compressed ISA encoding is used.  */
8719
8720 static void
8721 show_mips_compression (struct ui_file *file, int from_tty,
8722                        struct cmd_list_element *c, const char *value)
8723 {
8724   fprintf_filtered (file, _("The compressed ISA encoding used is %s.\n"),
8725                     value);
8726 }
8727
8728 static void
8729 mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
8730 {
8731   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8732   if (tdep != NULL)
8733     {
8734       int ef_mips_arch;
8735       int ef_mips_32bitmode;
8736       /* Determine the ISA.  */
8737       switch (tdep->elf_flags & EF_MIPS_ARCH)
8738         {
8739         case E_MIPS_ARCH_1:
8740           ef_mips_arch = 1;
8741           break;
8742         case E_MIPS_ARCH_2:
8743           ef_mips_arch = 2;
8744           break;
8745         case E_MIPS_ARCH_3:
8746           ef_mips_arch = 3;
8747           break;
8748         case E_MIPS_ARCH_4:
8749           ef_mips_arch = 4;
8750           break;
8751         default:
8752           ef_mips_arch = 0;
8753           break;
8754         }
8755       /* Determine the size of a pointer.  */
8756       ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
8757       fprintf_unfiltered (file,
8758                           "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
8759                           tdep->elf_flags);
8760       fprintf_unfiltered (file,
8761                           "mips_dump_tdep: ef_mips_32bitmode = %d\n",
8762                           ef_mips_32bitmode);
8763       fprintf_unfiltered (file,
8764                           "mips_dump_tdep: ef_mips_arch = %d\n",
8765                           ef_mips_arch);
8766       fprintf_unfiltered (file,
8767                           "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
8768                           tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
8769       fprintf_unfiltered (file,
8770                           "mips_dump_tdep: "
8771                           "mips_mask_address_p() %d (default %d)\n",
8772                           mips_mask_address_p (tdep),
8773                           tdep->default_mask_address_p);
8774     }
8775   fprintf_unfiltered (file,
8776                       "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
8777                       MIPS_DEFAULT_FPU_TYPE,
8778                       (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
8779                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
8780                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
8781                        : "???"));
8782   fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
8783                       MIPS_EABI (gdbarch));
8784   fprintf_unfiltered (file,
8785                       "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
8786                       MIPS_FPU_TYPE (gdbarch),
8787                       (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
8788                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
8789                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
8790                        : "???"));
8791 }
8792
8793 extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
8794
8795 void
8796 _initialize_mips_tdep (void)
8797 {
8798   static struct cmd_list_element *mipsfpulist = NULL;
8799   struct cmd_list_element *c;
8800
8801   mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
8802   if (MIPS_ABI_LAST + 1
8803       != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
8804     internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
8805
8806   gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
8807
8808   mips_pdr_data = register_objfile_data ();
8809
8810   /* Create feature sets with the appropriate properties.  The values
8811      are not important.  */
8812   mips_tdesc_gp32 = allocate_target_description ();
8813   set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");
8814
8815   mips_tdesc_gp64 = allocate_target_description ();
8816   set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");
8817
8818   /* Add root prefix command for all "set mips"/"show mips" commands.  */
8819   add_prefix_cmd ("mips", no_class, set_mips_command,
8820                   _("Various MIPS specific commands."),
8821                   &setmipscmdlist, "set mips ", 0, &setlist);
8822
8823   add_prefix_cmd ("mips", no_class, show_mips_command,
8824                   _("Various MIPS specific commands."),
8825                   &showmipscmdlist, "show mips ", 0, &showlist);
8826
8827   /* Allow the user to override the ABI.  */
8828   add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
8829                         &mips_abi_string, _("\
8830 Set the MIPS ABI used by this program."), _("\
8831 Show the MIPS ABI used by this program."), _("\
8832 This option can be set to one of:\n\
8833   auto  - the default ABI associated with the current binary\n\
8834   o32\n\
8835   o64\n\
8836   n32\n\
8837   n64\n\
8838   eabi32\n\
8839   eabi64"),
8840                         mips_abi_update,
8841                         show_mips_abi,
8842                         &setmipscmdlist, &showmipscmdlist);
8843
8844   /* Allow the user to set the ISA to assume for compressed code if ELF
8845      file flags don't tell or there is no program file selected.  This
8846      setting is updated whenever unambiguous ELF file flags are interpreted,
8847      and carried over to subsequent sessions.  */
8848   add_setshow_enum_cmd ("compression", class_obscure, mips_compression_strings,
8849                         &mips_compression_string, _("\
8850 Set the compressed ISA encoding used by MIPS code."), _("\
8851 Show the compressed ISA encoding used by MIPS code."), _("\
8852 Select the compressed ISA encoding used in functions that have no symbol\n\
8853 information available.  The encoding can be set to either of:\n\
8854   mips16\n\
8855   micromips\n\
8856 and is updated automatically from ELF file flags if available."),
8857                         mips_abi_update,
8858                         show_mips_compression,
8859                         &setmipscmdlist, &showmipscmdlist);
8860
8861   /* Let the user turn off floating point and set the fence post for
8862      heuristic_proc_start.  */
8863
8864   add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
8865                   _("Set use of MIPS floating-point coprocessor."),
8866                   &mipsfpulist, "set mipsfpu ", 0, &setlist);
8867   add_cmd ("single", class_support, set_mipsfpu_single_command,
8868            _("Select single-precision MIPS floating-point coprocessor."),
8869            &mipsfpulist);
8870   add_cmd ("double", class_support, set_mipsfpu_double_command,
8871            _("Select double-precision MIPS floating-point coprocessor."),
8872            &mipsfpulist);
8873   add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
8874   add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
8875   add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
8876   add_cmd ("none", class_support, set_mipsfpu_none_command,
8877            _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
8878   add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
8879   add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
8880   add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
8881   add_cmd ("auto", class_support, set_mipsfpu_auto_command,
8882            _("Select MIPS floating-point coprocessor automatically."),
8883            &mipsfpulist);
8884   add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
8885            _("Show current use of MIPS floating-point coprocessor target."),
8886            &showlist);
8887
8888   /* We really would like to have both "0" and "unlimited" work, but
8889      command.c doesn't deal with that.  So make it a var_zinteger
8890      because the user can always use "999999" or some such for unlimited.  */
8891   add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
8892                             &heuristic_fence_post, _("\
8893 Set the distance searched for the start of a function."), _("\
8894 Show the distance searched for the start of a function."), _("\
8895 If you are debugging a stripped executable, GDB needs to search through the\n\
8896 program for the start of a function.  This command sets the distance of the\n\
8897 search.  The only need to set it is when debugging a stripped executable."),
8898                             reinit_frame_cache_sfunc,
8899                             NULL, /* FIXME: i18n: The distance searched for
8900                                      the start of a function is %s.  */
8901                             &setlist, &showlist);
8902
8903   /* Allow the user to control whether the upper bits of 64-bit
8904      addresses should be zeroed.  */
8905   add_setshow_auto_boolean_cmd ("mask-address", no_class,
8906                                 &mask_address_var, _("\
8907 Set zeroing of upper 32 bits of 64-bit addresses."), _("\
8908 Show zeroing of upper 32 bits of 64-bit addresses."), _("\
8909 Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
8910 allow GDB to determine the correct value."),
8911                                 NULL, show_mask_address,
8912                                 &setmipscmdlist, &showmipscmdlist);
8913
8914   /* Allow the user to control the size of 32 bit registers within the
8915      raw remote packet.  */
8916   add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
8917                            &mips64_transfers_32bit_regs_p, _("\
8918 Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
8919                            _("\
8920 Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
8921                            _("\
8922 Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
8923 that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
8924 64 bits for others.  Use \"off\" to disable compatibility mode"),
8925                            set_mips64_transfers_32bit_regs,
8926                            NULL, /* FIXME: i18n: Compatibility with 64-bit
8927                                     MIPS target that transfers 32-bit
8928                                     quantities is %s.  */
8929                            &setlist, &showlist);
8930
8931   /* Debug this files internals.  */
8932   add_setshow_zuinteger_cmd ("mips", class_maintenance,
8933                              &mips_debug, _("\
8934 Set mips debugging."), _("\
8935 Show mips debugging."), _("\
8936 When non-zero, mips specific debugging is enabled."),
8937                              NULL,
8938                              NULL, /* FIXME: i18n: Mips debugging is
8939                                       currently %s.  */
8940                              &setdebuglist, &showdebuglist);
8941 }