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