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, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5    2010, 2011 Free Software Foundation, Inc.
6
7    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
8    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
9
10    This file is part of GDB.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
24
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #include "frame.h"
29 #include "inferior.h"
30 #include "symtab.h"
31 #include "value.h"
32 #include "gdbcmd.h"
33 #include "language.h"
34 #include "gdbcore.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37 #include "gdbtypes.h"
38 #include "target.h"
39 #include "arch-utils.h"
40 #include "regcache.h"
41 #include "osabi.h"
42 #include "mips-tdep.h"
43 #include "block.h"
44 #include "reggroups.h"
45 #include "opcode/mips.h"
46 #include "elf/mips.h"
47 #include "elf-bfd.h"
48 #include "symcat.h"
49 #include "sim-regno.h"
50 #include "dis-asm.h"
51 #include "frame-unwind.h"
52 #include "frame-base.h"
53 #include "trad-frame.h"
54 #include "infcall.h"
55 #include "floatformat.h"
56 #include "remote.h"
57 #include "target-descriptions.h"
58 #include "dwarf2-frame.h"
59 #include "user-regs.h"
60 #include "valprint.h"
61 #include "ax.h"
62
63 static const struct objfile_data *mips_pdr_data;
64
65 static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
66
67 /* A useful bit in the CP0 status register (MIPS_PS_REGNUM).  */
68 /* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip.  */
69 #define ST0_FR (1 << 26)
70
71 /* The sizes of floating point registers.  */
72
73 enum
74 {
75   MIPS_FPU_SINGLE_REGSIZE = 4,
76   MIPS_FPU_DOUBLE_REGSIZE = 8
77 };
78
79 enum
80 {
81   MIPS32_REGSIZE = 4,
82   MIPS64_REGSIZE = 8
83 };
84
85 static const char *mips_abi_string;
86
87 static const char *mips_abi_strings[] = {
88   "auto",
89   "n32",
90   "o32",
91   "n64",
92   "o64",
93   "eabi32",
94   "eabi64",
95   NULL
96 };
97
98 /* The standard register names, and all the valid aliases for them.  */
99 struct register_alias
100 {
101   const char *name;
102   int regnum;
103 };
104
105 /* Aliases for o32 and most other ABIs.  */
106 const struct register_alias mips_o32_aliases[] = {
107   { "ta0", 12 },
108   { "ta1", 13 },
109   { "ta2", 14 },
110   { "ta3", 15 }
111 };
112
113 /* Aliases for n32 and n64.  */
114 const struct register_alias mips_n32_n64_aliases[] = {
115   { "ta0", 8 },
116   { "ta1", 9 },
117   { "ta2", 10 },
118   { "ta3", 11 }
119 };
120
121 /* Aliases for ABI-independent registers.  */
122 const struct register_alias mips_register_aliases[] = {
123   /* The architecture manuals specify these ABI-independent names for
124      the GPRs.  */
125 #define R(n) { "r" #n, n }
126   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
127   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
128   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
129   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
130 #undef R
131
132   /* k0 and k1 are sometimes called these instead (for "kernel
133      temp").  */
134   { "kt0", 26 },
135   { "kt1", 27 },
136
137   /* This is the traditional GDB name for the CP0 status register.  */
138   { "sr", MIPS_PS_REGNUM },
139
140   /* This is the traditional GDB name for the CP0 BadVAddr register.  */
141   { "bad", MIPS_EMBED_BADVADDR_REGNUM },
142
143   /* This is the traditional GDB name for the FCSR.  */
144   { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
145 };
146
147 const struct register_alias mips_numeric_register_aliases[] = {
148 #define R(n) { #n, n }
149   R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
150   R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
151   R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
152   R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
153 #undef R
154 };
155
156 #ifndef MIPS_DEFAULT_FPU_TYPE
157 #define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
158 #endif
159 static int mips_fpu_type_auto = 1;
160 static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
161
162 static int mips_debug = 0;
163
164 /* Properties (for struct target_desc) describing the g/G packet
165    layout.  */
166 #define PROPERTY_GP32 "internal: transfers-32bit-registers"
167 #define PROPERTY_GP64 "internal: transfers-64bit-registers"
168
169 struct target_desc *mips_tdesc_gp32;
170 struct target_desc *mips_tdesc_gp64;
171
172 const struct mips_regnum *
173 mips_regnum (struct gdbarch *gdbarch)
174 {
175   return gdbarch_tdep (gdbarch)->regnum;
176 }
177
178 static int
179 mips_fpa0_regnum (struct gdbarch *gdbarch)
180 {
181   return mips_regnum (gdbarch)->fp0 + 12;
182 }
183
184 #define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
185                      == MIPS_ABI_EABI32 \
186                    || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)
187
188 #define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
189   (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)
190
191 #define MIPS_LAST_ARG_REGNUM(gdbarch) \
192   (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)
193
194 #define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)
195
196 /* MIPS16 function addresses are odd (bit 0 is set).  Here are some
197    functions to test, set, or clear bit 0 of addresses.  */
198
199 static CORE_ADDR
200 is_mips16_addr (CORE_ADDR addr)
201 {
202   return ((addr) & 1);
203 }
204
205 static CORE_ADDR
206 unmake_mips16_addr (CORE_ADDR addr)
207 {
208   return ((addr) & ~(CORE_ADDR) 1);
209 }
210
211 static CORE_ADDR
212 make_mips16_addr (CORE_ADDR addr)
213 {
214   return ((addr) | (CORE_ADDR) 1);
215 }
216
217 /* Return the MIPS ABI associated with GDBARCH.  */
218 enum mips_abi
219 mips_abi (struct gdbarch *gdbarch)
220 {
221   return gdbarch_tdep (gdbarch)->mips_abi;
222 }
223
224 int
225 mips_isa_regsize (struct gdbarch *gdbarch)
226 {
227   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
228
229   /* If we know how big the registers are, use that size.  */
230   if (tdep->register_size_valid_p)
231     return tdep->register_size;
232
233   /* Fall back to the previous behavior.  */
234   return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
235           / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
236 }
237
238 /* Return the currently configured (or set) saved register size.  */
239
240 unsigned int
241 mips_abi_regsize (struct gdbarch *gdbarch)
242 {
243   switch (mips_abi (gdbarch))
244     {
245     case MIPS_ABI_EABI32:
246     case MIPS_ABI_O32:
247       return 4;
248     case MIPS_ABI_N32:
249     case MIPS_ABI_N64:
250     case MIPS_ABI_O64:
251     case MIPS_ABI_EABI64:
252       return 8;
253     case MIPS_ABI_UNKNOWN:
254     case MIPS_ABI_LAST:
255     default:
256       internal_error (__FILE__, __LINE__, _("bad switch"));
257     }
258 }
259
260 /* Functions for setting and testing a bit in a minimal symbol that
261    marks it as 16-bit function.  The MSB of the minimal symbol's
262    "info" field is used for this purpose.
263
264    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is "special",
265    i.e. refers to a 16-bit function, and sets a "special" bit in a
266    minimal symbol to mark it as a 16-bit function
267
268    MSYMBOL_IS_SPECIAL   tests the "special" bit in a minimal symbol  */
269
270 static void
271 mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
272 {
273   if (((elf_symbol_type *) (sym))->internal_elf_sym.st_other == STO_MIPS16)
274     {
275       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
276     }
277 }
278
279 static int
280 msymbol_is_special (struct minimal_symbol *msym)
281 {
282   return MSYMBOL_TARGET_FLAG_1 (msym);
283 }
284
285 /* XFER a value from the big/little/left end of the register.
286    Depending on the size of the value it might occupy the entire
287    register or just part of it.  Make an allowance for this, aligning
288    things accordingly.  */
289
290 static void
291 mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
292                     int reg_num, int length,
293                     enum bfd_endian endian, gdb_byte *in,
294                     const gdb_byte *out, int buf_offset)
295 {
296   int reg_offset = 0;
297
298   gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
299   /* Need to transfer the left or right part of the register, based on
300      the targets byte order.  */
301   switch (endian)
302     {
303     case BFD_ENDIAN_BIG:
304       reg_offset = register_size (gdbarch, reg_num) - length;
305       break;
306     case BFD_ENDIAN_LITTLE:
307       reg_offset = 0;
308       break;
309     case BFD_ENDIAN_UNKNOWN:    /* Indicates no alignment.  */
310       reg_offset = 0;
311       break;
312     default:
313       internal_error (__FILE__, __LINE__, _("bad switch"));
314     }
315   if (mips_debug)
316     fprintf_unfiltered (gdb_stderr,
317                         "xfer $%d, reg offset %d, buf offset %d, length %d, ",
318                         reg_num, reg_offset, buf_offset, length);
319   if (mips_debug && out != NULL)
320     {
321       int i;
322       fprintf_unfiltered (gdb_stdlog, "out ");
323       for (i = 0; i < length; i++)
324         fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
325     }
326   if (in != NULL)
327     regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
328                                in + buf_offset);
329   if (out != NULL)
330     regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
331                                 out + buf_offset);
332   if (mips_debug && in != NULL)
333     {
334       int i;
335       fprintf_unfiltered (gdb_stdlog, "in ");
336       for (i = 0; i < length; i++)
337         fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
338     }
339   if (mips_debug)
340     fprintf_unfiltered (gdb_stdlog, "\n");
341 }
342
343 /* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
344    compatiblity mode.  A return value of 1 means that we have
345    physical 64-bit registers, but should treat them as 32-bit registers.  */
346
347 static int
348 mips2_fp_compat (struct frame_info *frame)
349 {
350   struct gdbarch *gdbarch = get_frame_arch (frame);
351   /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
352      meaningful.  */
353   if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
354     return 0;
355
356 #if 0
357   /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
358      in all the places we deal with FP registers.  PR gdb/413.  */
359   /* Otherwise check the FR bit in the status register - it controls
360      the FP compatiblity mode.  If it is clear we are in compatibility
361      mode.  */
362   if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
363     return 1;
364 #endif
365
366   return 0;
367 }
368
369 #define VM_MIN_ADDRESS (CORE_ADDR)0x400000
370
371 static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);
372
373 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
374
375 /* The list of available "set mips " and "show mips " commands.  */
376
377 static struct cmd_list_element *setmipscmdlist = NULL;
378 static struct cmd_list_element *showmipscmdlist = NULL;
379
380 /* Integer registers 0 thru 31 are handled explicitly by
381    mips_register_name().  Processor specific registers 32 and above
382    are listed in the following tables.  */
383
384 enum
385 { NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
386
387 /* Generic MIPS.  */
388
389 static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
390   "sr", "lo", "hi", "bad", "cause", "pc",
391   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
392   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
393   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
394   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
395   "fsr", "fir", "" /*"fp" */ , "",
396   "", "", "", "", "", "", "", "",
397   "", "", "", "", "", "", "", "",
398 };
399
400 /* Names of IDT R3041 registers.  */
401
402 static const char *mips_r3041_reg_names[] = {
403   "sr", "lo", "hi", "bad", "cause", "pc",
404   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
405   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
406   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
407   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
408   "fsr", "fir", "", /*"fp" */ "",
409   "", "", "bus", "ccfg", "", "", "", "",
410   "", "", "port", "cmp", "", "", "epc", "prid",
411 };
412
413 /* Names of tx39 registers.  */
414
415 static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
416   "sr", "lo", "hi", "bad", "cause", "pc",
417   "", "", "", "", "", "", "", "",
418   "", "", "", "", "", "", "", "",
419   "", "", "", "", "", "", "", "",
420   "", "", "", "", "", "", "", "",
421   "", "", "", "",
422   "", "", "", "", "", "", "", "",
423   "", "", "config", "cache", "debug", "depc", "epc", ""
424 };
425
426 /* Names of IRIX registers.  */
427 static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
428   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
429   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
430   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
431   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
432   "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
433 };
434
435
436 /* Return the name of the register corresponding to REGNO.  */
437 static const char *
438 mips_register_name (struct gdbarch *gdbarch, int regno)
439 {
440   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
441   /* GPR names for all ABIs other than n32/n64.  */
442   static char *mips_gpr_names[] = {
443     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
444     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
445     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
446     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
447   };
448
449   /* GPR names for n32 and n64 ABIs.  */
450   static char *mips_n32_n64_gpr_names[] = {
451     "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
452     "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
453     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
454     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
455   };
456
457   enum mips_abi abi = mips_abi (gdbarch);
458
459   /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers, 
460      but then don't make the raw register names visible.  This (upper)
461      range of user visible register numbers are the pseudo-registers.
462
463      This approach was adopted accommodate the following scenario:
464      It is possible to debug a 64-bit device using a 32-bit
465      programming model.  In such instances, the raw registers are
466      configured to be 64-bits wide, while the pseudo registers are
467      configured to be 32-bits wide.  The registers that the user
468      sees - the pseudo registers - match the users expectations
469      given the programming model being used.  */
470   int rawnum = regno % gdbarch_num_regs (gdbarch);
471   if (regno < gdbarch_num_regs (gdbarch))
472     return "";
473
474   /* The MIPS integer registers are always mapped from 0 to 31.  The
475      names of the registers (which reflects the conventions regarding
476      register use) vary depending on the ABI.  */
477   if (0 <= rawnum && rawnum < 32)
478     {
479       if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
480         return mips_n32_n64_gpr_names[rawnum];
481       else
482         return mips_gpr_names[rawnum];
483     }
484   else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
485     return tdesc_register_name (gdbarch, rawnum);
486   else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
487     {
488       gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
489       return tdep->mips_processor_reg_names[rawnum - 32];
490     }
491   else
492     internal_error (__FILE__, __LINE__,
493                     _("mips_register_name: bad register number %d"), rawnum);
494 }
495
496 /* Return the groups that a MIPS register can be categorised into.  */
497
498 static int
499 mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
500                           struct reggroup *reggroup)
501 {
502   int vector_p;
503   int float_p;
504   int raw_p;
505   int rawnum = regnum % gdbarch_num_regs (gdbarch);
506   int pseudo = regnum / gdbarch_num_regs (gdbarch);
507   if (reggroup == all_reggroup)
508     return pseudo;
509   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
510   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
511   /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
512      (gdbarch), as not all architectures are multi-arch.  */
513   raw_p = rawnum < gdbarch_num_regs (gdbarch);
514   if (gdbarch_register_name (gdbarch, regnum) == NULL
515       || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
516     return 0;
517   if (reggroup == float_reggroup)
518     return float_p && pseudo;
519   if (reggroup == vector_reggroup)
520     return vector_p && pseudo;
521   if (reggroup == general_reggroup)
522     return (!vector_p && !float_p) && pseudo;
523   /* Save the pseudo registers.  Need to make certain that any code
524      extracting register values from a saved register cache also uses
525      pseudo registers.  */
526   if (reggroup == save_reggroup)
527     return raw_p && pseudo;
528   /* Restore the same pseudo register.  */
529   if (reggroup == restore_reggroup)
530     return raw_p && pseudo;
531   return 0;
532 }
533
534 /* Return the groups that a MIPS register can be categorised into.
535    This version is only used if we have a target description which
536    describes real registers (and their groups).  */
537
538 static int
539 mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
540                                 struct reggroup *reggroup)
541 {
542   int rawnum = regnum % gdbarch_num_regs (gdbarch);
543   int pseudo = regnum / gdbarch_num_regs (gdbarch);
544   int ret;
545
546   /* Only save, restore, and display the pseudo registers.  Need to
547      make certain that any code extracting register values from a
548      saved register cache also uses pseudo registers.
549
550      Note: saving and restoring the pseudo registers is slightly
551      strange; if we have 64 bits, we should save and restore all
552      64 bits.  But this is hard and has little benefit.  */
553   if (!pseudo)
554     return 0;
555
556   ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
557   if (ret != -1)
558     return ret;
559
560   return mips_register_reggroup_p (gdbarch, regnum, reggroup);
561 }
562
563 /* Map the symbol table registers which live in the range [1 *
564    gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
565    registers.  Take care of alignment and size problems.  */
566
567 static enum register_status
568 mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
569                            int cookednum, gdb_byte *buf)
570 {
571   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
572   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
573               && cookednum < 2 * gdbarch_num_regs (gdbarch));
574   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
575     return regcache_raw_read (regcache, rawnum, buf);
576   else if (register_size (gdbarch, rawnum) >
577            register_size (gdbarch, cookednum))
578     {
579       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
580         return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
581       else
582         {
583           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
584           LONGEST regval;
585           enum register_status status;
586
587           status = regcache_raw_read_signed (regcache, rawnum, &regval);
588           if (status == REG_VALID)
589             store_signed_integer (buf, 4, byte_order, regval);
590           return status;
591         }
592     }
593   else
594     internal_error (__FILE__, __LINE__, _("bad register size"));
595 }
596
597 static void
598 mips_pseudo_register_write (struct gdbarch *gdbarch,
599                             struct regcache *regcache, int cookednum,
600                             const gdb_byte *buf)
601 {
602   int rawnum = cookednum % gdbarch_num_regs (gdbarch);
603   gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
604               && cookednum < 2 * gdbarch_num_regs (gdbarch));
605   if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
606     regcache_raw_write (regcache, rawnum, buf);
607   else if (register_size (gdbarch, rawnum) >
608            register_size (gdbarch, cookednum))
609     {
610       if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
611         regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
612       else
613         {
614           /* Sign extend the shortened version of the register prior
615              to placing it in the raw register.  This is required for
616              some mips64 parts in order to avoid unpredictable behavior.  */
617           enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
618           LONGEST regval = extract_signed_integer (buf, 4, byte_order);
619           regcache_raw_write_signed (regcache, rawnum, regval);
620         }
621     }
622   else
623     internal_error (__FILE__, __LINE__, _("bad register size"));
624 }
625
626 static int
627 mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
628                                  struct agent_expr *ax, int reg)
629 {
630   int rawnum = reg % gdbarch_num_regs (gdbarch);
631   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
632               && reg < 2 * gdbarch_num_regs (gdbarch));
633
634   ax_reg_mask (ax, rawnum);
635
636   return 0;
637 }
638
639 static int
640 mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
641                                     struct agent_expr *ax, int reg)
642 {
643   int rawnum = reg % gdbarch_num_regs (gdbarch);
644   gdb_assert (reg >= gdbarch_num_regs (gdbarch)
645               && reg < 2 * gdbarch_num_regs (gdbarch));
646   if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
647     {
648       ax_reg (ax, rawnum);
649
650       if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
651         {
652           if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
653               || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
654             {
655               ax_const_l (ax, 32);
656               ax_simple (ax, aop_lsh);
657             }
658           ax_const_l (ax, 32);
659           ax_simple (ax, aop_rsh_signed);
660         }
661     }
662   else
663     internal_error (__FILE__, __LINE__, _("bad register size"));
664
665   return 0;
666 }
667
668 /* Table to translate MIPS16 register field to actual register number.  */
669 static int mips16_to_32_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
670
671 /* Heuristic_proc_start may hunt through the text section for a long
672    time across a 2400 baud serial line.  Allows the user to limit this
673    search.  */
674
675 static unsigned int heuristic_fence_post = 0;
676
677 /* Number of bytes of storage in the actual machine representation for
678    register N.  NOTE: This defines the pseudo register type so need to
679    rebuild the architecture vector.  */
680
681 static int mips64_transfers_32bit_regs_p = 0;
682
683 static void
684 set_mips64_transfers_32bit_regs (char *args, int from_tty,
685                                  struct cmd_list_element *c)
686 {
687   struct gdbarch_info info;
688   gdbarch_info_init (&info);
689   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
690      instead of relying on globals.  Doing that would let generic code
691      handle the search for this specific architecture.  */
692   if (!gdbarch_update_p (info))
693     {
694       mips64_transfers_32bit_regs_p = 0;
695       error (_("32-bit compatibility mode not supported"));
696     }
697 }
698
699 /* Convert to/from a register and the corresponding memory value.  */
700
701 /* This predicate tests for the case of an 8 byte floating point
702    value that is being transferred to or from a pair of floating point
703    registers each of which are (or are considered to be) only 4 bytes
704    wide.  */
705 static int
706 mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
707                                     struct type *type)
708 {
709   return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
710           && register_size (gdbarch, regnum) == 4
711           && (regnum % gdbarch_num_regs (gdbarch))
712                 >= mips_regnum (gdbarch)->fp0
713           && (regnum % gdbarch_num_regs (gdbarch))
714                 < mips_regnum (gdbarch)->fp0 + 32
715           && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
716 }
717
718 /* This predicate tests for the case of a value of less than 8
719    bytes in width that is being transfered to or from an 8 byte
720    general purpose register.  */
721 static int
722 mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
723                                     struct type *type)
724 {
725   int num_regs = gdbarch_num_regs (gdbarch);
726
727   return (register_size (gdbarch, regnum) == 8
728           && regnum % num_regs > 0 && regnum % num_regs < 32
729           && TYPE_LENGTH (type) < 8);
730 }
731
732 static int
733 mips_convert_register_p (struct gdbarch *gdbarch,
734                          int regnum, struct type *type)
735 {
736   return mips_convert_register_float_case_p (gdbarch, regnum, type)
737       || mips_convert_register_gpreg_case_p (gdbarch, regnum, type);
738 }
739
740 static void
741 mips_register_to_value (struct frame_info *frame, int regnum,
742                         struct type *type, gdb_byte *to)
743 {
744   struct gdbarch *gdbarch = get_frame_arch (frame);
745
746   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
747     {
748       get_frame_register (frame, regnum + 0, to + 4);
749       get_frame_register (frame, regnum + 1, to + 0);
750     }
751   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
752     {
753       int len = TYPE_LENGTH (type);
754       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
755         get_frame_register_bytes (frame, regnum, 8 - len, len, to);
756       else
757         get_frame_register_bytes (frame, regnum, 0, len, to);
758     }
759   else
760     {
761       internal_error (__FILE__, __LINE__,
762                       _("mips_register_to_value: unrecognized case"));
763     }
764 }
765
766 static void
767 mips_value_to_register (struct frame_info *frame, int regnum,
768                         struct type *type, const gdb_byte *from)
769 {
770   struct gdbarch *gdbarch = get_frame_arch (frame);
771
772   if (mips_convert_register_float_case_p (gdbarch, regnum, type))
773     {
774       put_frame_register (frame, regnum + 0, from + 4);
775       put_frame_register (frame, regnum + 1, from + 0);
776     }
777   else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
778     {
779       gdb_byte fill[8];
780       int len = TYPE_LENGTH (type);
781       
782       /* Sign extend values, irrespective of type, that are stored to 
783          a 64-bit general purpose register.  (32-bit unsigned values
784          are stored as signed quantities within a 64-bit register.
785          When performing an operation, in compiled code, that combines
786          a 32-bit unsigned value with a signed 64-bit value, a type
787          conversion is first performed that zeroes out the high 32 bits.)  */
788       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
789         {
790           if (from[0] & 0x80)
791             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
792           else
793             store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
794           put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
795           put_frame_register_bytes (frame, regnum, 8 - len, len, from);
796         }
797       else
798         {
799           if (from[len-1] & 0x80)
800             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
801           else
802             store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
803           put_frame_register_bytes (frame, regnum, 0, len, from);
804           put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
805         }
806     }
807   else
808     {
809       internal_error (__FILE__, __LINE__,
810                       _("mips_value_to_register: unrecognized case"));
811     }
812 }
813
814 /* Return the GDB type object for the "standard" data type of data in
815    register REG.  */
816
817 static struct type *
818 mips_register_type (struct gdbarch *gdbarch, int regnum)
819 {
820   gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
821   if ((regnum % gdbarch_num_regs (gdbarch)) >= mips_regnum (gdbarch)->fp0
822       && (regnum % gdbarch_num_regs (gdbarch))
823          < mips_regnum (gdbarch)->fp0 + 32)
824     {
825       /* The floating-point registers raw, or cooked, always match
826          mips_isa_regsize(), and also map 1:1, byte for byte.  */
827       if (mips_isa_regsize (gdbarch) == 4)
828         return builtin_type (gdbarch)->builtin_float;
829       else
830         return builtin_type (gdbarch)->builtin_double;
831     }
832   else if (regnum < gdbarch_num_regs (gdbarch))
833     {
834       /* The raw or ISA registers.  These are all sized according to
835          the ISA regsize.  */
836       if (mips_isa_regsize (gdbarch) == 4)
837         return builtin_type (gdbarch)->builtin_int32;
838       else
839         return builtin_type (gdbarch)->builtin_int64;
840     }
841   else
842     {
843       /* The cooked or ABI registers.  These are sized according to
844          the ABI (with a few complications).  */
845       if (regnum >= (gdbarch_num_regs (gdbarch)
846                      + mips_regnum (gdbarch)->fp_control_status)
847           && regnum <= gdbarch_num_regs (gdbarch) + MIPS_LAST_EMBED_REGNUM)
848         /* The pseudo/cooked view of the embedded registers is always
849            32-bit.  The raw view is handled below.  */
850         return builtin_type (gdbarch)->builtin_int32;
851       else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
852         /* The target, while possibly using a 64-bit register buffer,
853            is only transfering 32-bits of each integer register.
854            Reflect this in the cooked/pseudo (ABI) register value.  */
855         return builtin_type (gdbarch)->builtin_int32;
856       else if (mips_abi_regsize (gdbarch) == 4)
857         /* The ABI is restricted to 32-bit registers (the ISA could be
858            32- or 64-bit).  */
859         return builtin_type (gdbarch)->builtin_int32;
860       else
861         /* 64-bit ABI.  */
862         return builtin_type (gdbarch)->builtin_int64;
863     }
864 }
865
866 /* Return the GDB type for the pseudo register REGNUM, which is the
867    ABI-level view.  This function is only called if there is a target
868    description which includes registers, so we know precisely the
869    types of hardware registers.  */
870
871 static struct type *
872 mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
873 {
874   const int num_regs = gdbarch_num_regs (gdbarch);
875   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
876   int rawnum = regnum % num_regs;
877   struct type *rawtype;
878
879   gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);
880
881   /* Absent registers are still absent.  */
882   rawtype = gdbarch_register_type (gdbarch, rawnum);
883   if (TYPE_LENGTH (rawtype) == 0)
884     return rawtype;
885
886   if (rawnum >= MIPS_EMBED_FP0_REGNUM && rawnum < MIPS_EMBED_FP0_REGNUM + 32)
887     /* Present the floating point registers however the hardware did;
888        do not try to convert between FPU layouts.  */
889     return rawtype;
890
891   if (rawnum >= MIPS_EMBED_FP0_REGNUM + 32 && rawnum <= MIPS_LAST_EMBED_REGNUM)
892     {
893       /* The pseudo/cooked view of embedded registers is always
894          32-bit, even if the target transfers 64-bit values for them.
895          New targets relying on XML descriptions should only transfer
896          the necessary 32 bits, but older versions of GDB expected 64,
897          so allow the target to provide 64 bits without interfering
898          with the displayed type.  */
899       return builtin_type (gdbarch)->builtin_int32;
900     }
901
902   /* Use pointer types for registers if we can.  For n32 we can not,
903      since we do not have a 64-bit pointer type.  */
904   if (mips_abi_regsize (gdbarch)
905       == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
906     {
907       if (rawnum == MIPS_SP_REGNUM || rawnum == MIPS_EMBED_BADVADDR_REGNUM)
908         return builtin_type (gdbarch)->builtin_data_ptr;
909       else if (rawnum == MIPS_EMBED_PC_REGNUM)
910         return builtin_type (gdbarch)->builtin_func_ptr;
911     }
912
913   if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
914       && rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_EMBED_PC_REGNUM)
915     return builtin_type (gdbarch)->builtin_int32;
916
917   /* For all other registers, pass through the hardware type.  */
918   return rawtype;
919 }
920
921 /* Should the upper word of 64-bit addresses be zeroed?  */
922 enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
923
924 static int
925 mips_mask_address_p (struct gdbarch_tdep *tdep)
926 {
927   switch (mask_address_var)
928     {
929     case AUTO_BOOLEAN_TRUE:
930       return 1;
931     case AUTO_BOOLEAN_FALSE:
932       return 0;
933       break;
934     case AUTO_BOOLEAN_AUTO:
935       return tdep->default_mask_address_p;
936     default:
937       internal_error (__FILE__, __LINE__,
938                       _("mips_mask_address_p: bad switch"));
939       return -1;
940     }
941 }
942
943 static void
944 show_mask_address (struct ui_file *file, int from_tty,
945                    struct cmd_list_element *c, const char *value)
946 {
947   struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
948
949   deprecated_show_value_hack (file, from_tty, c, value);
950   switch (mask_address_var)
951     {
952     case AUTO_BOOLEAN_TRUE:
953       printf_filtered ("The 32 bit mips address mask is enabled\n");
954       break;
955     case AUTO_BOOLEAN_FALSE:
956       printf_filtered ("The 32 bit mips address mask is disabled\n");
957       break;
958     case AUTO_BOOLEAN_AUTO:
959       printf_filtered
960         ("The 32 bit address mask is set automatically.  Currently %s\n",
961          mips_mask_address_p (tdep) ? "enabled" : "disabled");
962       break;
963     default:
964       internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
965       break;
966     }
967 }
968
969 /* Tell if the program counter value in MEMADDR is in a MIPS16 function.  */
970
971 int
972 mips_pc_is_mips16 (CORE_ADDR memaddr)
973 {
974   struct minimal_symbol *sym;
975
976   /* If bit 0 of the address is set, assume this is a MIPS16 address.  */
977   if (is_mips16_addr (memaddr))
978     return 1;
979
980   /* A flag indicating that this is a MIPS16 function is stored by elfread.c in
981      the high bit of the info field.  Use this to decide if the function is
982      MIPS16 or normal MIPS.  */
983   sym = lookup_minimal_symbol_by_pc (memaddr);
984   if (sym)
985     return msymbol_is_special (sym);
986   else
987     return 0;
988 }
989
990 /* MIPS believes that the PC has a sign extended value.  Perhaps the
991    all registers should be sign extended for simplicity?  */
992
993 static CORE_ADDR
994 mips_read_pc (struct regcache *regcache)
995 {
996   ULONGEST pc;
997   int regnum = mips_regnum (get_regcache_arch (regcache))->pc;
998   regcache_cooked_read_signed (regcache, regnum, &pc);
999   if (is_mips16_addr (pc))
1000     pc = unmake_mips16_addr (pc);
1001   return pc;
1002 }
1003
1004 static CORE_ADDR
1005 mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1006 {
1007   ULONGEST pc;
1008
1009   pc = frame_unwind_register_signed
1010          (next_frame, gdbarch_num_regs (gdbarch) + mips_regnum (gdbarch)->pc);
1011   if (is_mips16_addr (pc))
1012     pc = unmake_mips16_addr (pc);
1013   return pc;
1014 }
1015
1016 static CORE_ADDR
1017 mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1018 {
1019   return frame_unwind_register_signed
1020            (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
1021 }
1022
1023 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
1024    dummy frame.  The frame ID's base needs to match the TOS value
1025    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1026    breakpoint.  */
1027
1028 static struct frame_id
1029 mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1030 {
1031   return frame_id_build
1032            (get_frame_register_signed (this_frame,
1033                                        gdbarch_num_regs (gdbarch)
1034                                        + MIPS_SP_REGNUM),
1035             get_frame_pc (this_frame));
1036 }
1037
1038 static void
1039 mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
1040 {
1041   int regnum = mips_regnum (get_regcache_arch (regcache))->pc;
1042   if (mips_pc_is_mips16 (pc))
1043     regcache_cooked_write_unsigned (regcache, regnum, make_mips16_addr (pc));
1044   else
1045     regcache_cooked_write_unsigned (regcache, regnum, pc);
1046 }
1047
1048 /* Fetch and return instruction from the specified location.  If the PC
1049    is odd, assume it's a MIPS16 instruction; otherwise MIPS32.  */
1050
1051 static ULONGEST
1052 mips_fetch_instruction (struct gdbarch *gdbarch, CORE_ADDR addr)
1053 {
1054   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1055   gdb_byte buf[MIPS_INSN32_SIZE];
1056   int instlen;
1057   int status;
1058
1059   if (mips_pc_is_mips16 (addr))
1060     {
1061       instlen = MIPS_INSN16_SIZE;
1062       addr = unmake_mips16_addr (addr);
1063     }
1064   else
1065     instlen = MIPS_INSN32_SIZE;
1066   status = target_read_memory (addr, buf, instlen);
1067   if (status)
1068     memory_error (status, addr);
1069   return extract_unsigned_integer (buf, instlen, byte_order);
1070 }
1071
1072 /* These are the fields of 32 bit mips instructions.  */
1073 #define mips32_op(x) (x >> 26)
1074 #define itype_op(x) (x >> 26)
1075 #define itype_rs(x) ((x >> 21) & 0x1f)
1076 #define itype_rt(x) ((x >> 16) & 0x1f)
1077 #define itype_immediate(x) (x & 0xffff)
1078
1079 #define jtype_op(x) (x >> 26)
1080 #define jtype_target(x) (x & 0x03ffffff)
1081
1082 #define rtype_op(x) (x >> 26)
1083 #define rtype_rs(x) ((x >> 21) & 0x1f)
1084 #define rtype_rt(x) ((x >> 16) & 0x1f)
1085 #define rtype_rd(x) ((x >> 11) & 0x1f)
1086 #define rtype_shamt(x) ((x >> 6) & 0x1f)
1087 #define rtype_funct(x) (x & 0x3f)
1088
1089 static LONGEST
1090 mips32_relative_offset (ULONGEST inst)
1091 {
1092   return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
1093 }
1094
1095 /* Determine where to set a single step breakpoint while considering
1096    branch prediction.  */
1097 static CORE_ADDR
1098 mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
1099 {
1100   struct gdbarch *gdbarch = get_frame_arch (frame);
1101   unsigned long inst;
1102   int op;
1103   inst = mips_fetch_instruction (gdbarch, pc);
1104   if ((inst & 0xe0000000) != 0)         /* Not a special, jump or branch
1105                                            instruction.  */
1106     {
1107       if (itype_op (inst) >> 2 == 5)
1108         /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
1109         {
1110           op = (itype_op (inst) & 0x03);
1111           switch (op)
1112             {
1113             case 0:             /* BEQL */
1114               goto equal_branch;
1115             case 1:             /* BNEL */
1116               goto neq_branch;
1117             case 2:             /* BLEZL */
1118               goto less_branch;
1119             case 3:             /* BGTZL */
1120               goto greater_branch;
1121             default:
1122               pc += 4;
1123             }
1124         }
1125       else if (itype_op (inst) == 17 && itype_rs (inst) == 8)
1126         /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
1127         {
1128           int tf = itype_rt (inst) & 0x01;
1129           int cnum = itype_rt (inst) >> 2;
1130           int fcrcs =
1131             get_frame_register_signed (frame,
1132                                        mips_regnum (get_frame_arch (frame))->
1133                                                 fp_control_status);
1134           int cond = ((fcrcs >> 24) & 0x0e) | ((fcrcs >> 23) & 0x01);
1135
1136           if (((cond >> cnum) & 0x01) == tf)
1137             pc += mips32_relative_offset (inst) + 4;
1138           else
1139             pc += 8;
1140         }
1141       else
1142         pc += 4;                /* Not a branch, next instruction is easy.  */
1143     }
1144   else
1145     {                           /* This gets way messy.  */
1146
1147       /* Further subdivide into SPECIAL, REGIMM and other.  */
1148       switch (op = itype_op (inst) & 0x07)      /* Extract bits 28,27,26.  */
1149         {
1150         case 0:         /* SPECIAL */
1151           op = rtype_funct (inst);
1152           switch (op)
1153             {
1154             case 8:             /* JR */
1155             case 9:             /* JALR */
1156               /* Set PC to that address.  */
1157               pc = get_frame_register_signed (frame, rtype_rs (inst));
1158               break;
1159             case 12:            /* SYSCALL */
1160               {
1161                 struct gdbarch_tdep *tdep;
1162
1163                 tdep = gdbarch_tdep (get_frame_arch (frame));
1164                 if (tdep->syscall_next_pc != NULL)
1165                   pc = tdep->syscall_next_pc (frame);
1166                 else
1167                   pc += 4;
1168               }
1169               break;
1170             default:
1171               pc += 4;
1172             }
1173
1174           break;                /* end SPECIAL */
1175         case 1:                 /* REGIMM */
1176           {
1177             op = itype_rt (inst);       /* branch condition */
1178             switch (op)
1179               {
1180               case 0:           /* BLTZ */
1181               case 2:           /* BLTZL */
1182               case 16:          /* BLTZAL */
1183               case 18:          /* BLTZALL */
1184               less_branch:
1185                 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
1186                   pc += mips32_relative_offset (inst) + 4;
1187                 else
1188                   pc += 8;      /* after the delay slot */
1189                 break;
1190               case 1:           /* BGEZ */
1191               case 3:           /* BGEZL */
1192               case 17:          /* BGEZAL */
1193               case 19:          /* BGEZALL */
1194                 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
1195                   pc += mips32_relative_offset (inst) + 4;
1196                 else
1197                   pc += 8;      /* after the delay slot */
1198                 break;
1199                 /* All of the other instructions in the REGIMM category */
1200               default:
1201                 pc += 4;
1202               }
1203           }
1204           break;                /* end REGIMM */
1205         case 2:         /* J */
1206         case 3:         /* JAL */
1207           {
1208             unsigned long reg;
1209             reg = jtype_target (inst) << 2;
1210             /* Upper four bits get never changed...  */
1211             pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
1212           }
1213           break;
1214           /* FIXME case JALX : */
1215           {
1216             unsigned long reg;
1217             reg = jtype_target (inst) << 2;
1218             pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + 1;  /* yes, +1 */
1219             /* Add 1 to indicate 16 bit mode - Invert ISA mode */
1220           }
1221           break;                /* The new PC will be alternate mode */
1222         case 4:         /* BEQ, BEQL */
1223         equal_branch:
1224           if (get_frame_register_signed (frame, itype_rs (inst)) ==
1225               get_frame_register_signed (frame, itype_rt (inst)))
1226             pc += mips32_relative_offset (inst) + 4;
1227           else
1228             pc += 8;
1229           break;
1230         case 5:         /* BNE, BNEL */
1231         neq_branch:
1232           if (get_frame_register_signed (frame, itype_rs (inst)) !=
1233               get_frame_register_signed (frame, itype_rt (inst)))
1234             pc += mips32_relative_offset (inst) + 4;
1235           else
1236             pc += 8;
1237           break;
1238         case 6:         /* BLEZ, BLEZL */
1239           if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
1240             pc += mips32_relative_offset (inst) + 4;
1241           else
1242             pc += 8;
1243           break;
1244         case 7:
1245         default:
1246         greater_branch: /* BGTZ, BGTZL */
1247           if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
1248             pc += mips32_relative_offset (inst) + 4;
1249           else
1250             pc += 8;
1251           break;
1252         }                       /* switch */
1253     }                           /* else */
1254   return pc;
1255 }                               /* mips32_next_pc */
1256
1257 /* Decoding the next place to set a breakpoint is irregular for the
1258    mips 16 variant, but fortunately, there fewer instructions.  We have
1259    to cope ith extensions for 16 bit instructions and a pair of actual
1260    32 bit instructions.  We dont want to set a single step instruction
1261    on the extend instruction either.  */
1262
1263 /* Lots of mips16 instruction formats */
1264 /* Predicting jumps requires itype,ritype,i8type
1265    and their extensions      extItype,extritype,extI8type.  */
1266 enum mips16_inst_fmts
1267 {
1268   itype,                        /* 0  immediate 5,10 */
1269   ritype,                       /* 1   5,3,8 */
1270   rrtype,                       /* 2   5,3,3,5 */
1271   rritype,                      /* 3   5,3,3,5 */
1272   rrrtype,                      /* 4   5,3,3,3,2 */
1273   rriatype,                     /* 5   5,3,3,1,4 */
1274   shifttype,                    /* 6   5,3,3,3,2 */
1275   i8type,                       /* 7   5,3,8 */
1276   i8movtype,                    /* 8   5,3,3,5 */
1277   i8mov32rtype,                 /* 9   5,3,5,3 */
1278   i64type,                      /* 10  5,3,8 */
1279   ri64type,                     /* 11  5,3,3,5 */
1280   jalxtype,                     /* 12  5,1,5,5,16 - a 32 bit instruction */
1281   exiItype,                     /* 13  5,6,5,5,1,1,1,1,1,1,5 */
1282   extRitype,                    /* 14  5,6,5,5,3,1,1,1,5 */
1283   extRRItype,                   /* 15  5,5,5,5,3,3,5 */
1284   extRRIAtype,                  /* 16  5,7,4,5,3,3,1,4 */
1285   EXTshifttype,                 /* 17  5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
1286   extI8type,                    /* 18  5,6,5,5,3,1,1,1,5 */
1287   extI64type,                   /* 19  5,6,5,5,3,1,1,1,5 */
1288   extRi64type,                  /* 20  5,6,5,5,3,3,5 */
1289   extshift64type                /* 21  5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
1290 };
1291 /* I am heaping all the fields of the formats into one structure and
1292    then, only the fields which are involved in instruction extension.  */
1293 struct upk_mips16
1294 {
1295   CORE_ADDR offset;
1296   unsigned int regx;            /* Function in i8 type.  */
1297   unsigned int regy;
1298 };
1299
1300
1301 /* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
1302    for the bits which make up the immediate extension.  */
1303
1304 static CORE_ADDR
1305 extended_offset (unsigned int extension)
1306 {
1307   CORE_ADDR value;
1308   value = (extension >> 21) & 0x3f;     /* * extract 15:11 */
1309   value = value << 6;
1310   value |= (extension >> 16) & 0x1f;    /* extract 10:5 */
1311   value = value << 5;
1312   value |= extension & 0x01f;   /* extract 4:0 */
1313   return value;
1314 }
1315
1316 /* Only call this function if you know that this is an extendable
1317    instruction.  It won't malfunction, but why make excess remote memory
1318    references?  If the immediate operands get sign extended or something,
1319    do it after the extension is performed.  */
1320 /* FIXME: Every one of these cases needs to worry about sign extension
1321    when the offset is to be used in relative addressing.  */
1322
1323 static unsigned int
1324 fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
1325 {
1326   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1327   gdb_byte buf[8];
1328   pc &= 0xfffffffe;             /* Clear the low order bit.  */
1329   target_read_memory (pc, buf, 2);
1330   return extract_unsigned_integer (buf, 2, byte_order);
1331 }
1332
1333 static void
1334 unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
1335                unsigned int extension,
1336                unsigned int inst,
1337                enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
1338 {
1339   CORE_ADDR offset;
1340   int regx;
1341   int regy;
1342   switch (insn_format)
1343     {
1344     case itype:
1345       {
1346         CORE_ADDR value;
1347         if (extension)
1348           {
1349             value = extended_offset (extension);
1350             value = value << 11;        /* rom for the original value */
1351             value |= inst & 0x7ff;      /* eleven bits from instruction */
1352           }
1353         else
1354           {
1355             value = inst & 0x7ff;
1356             /* FIXME : Consider sign extension.  */
1357           }
1358         offset = value;
1359         regx = -1;
1360         regy = -1;
1361       }
1362       break;
1363     case ritype:
1364     case i8type:
1365       {                         /* A register identifier and an offset.  */
1366         /* Most of the fields are the same as I type but the
1367            immediate value is of a different length.  */
1368         CORE_ADDR value;
1369         if (extension)
1370           {
1371             value = extended_offset (extension);
1372             value = value << 8;         /* from the original instruction */
1373             value |= inst & 0xff;       /* eleven bits from instruction */
1374             regx = (extension >> 8) & 0x07;     /* or i8 funct */
1375             if (value & 0x4000)         /* Test the sign bit, bit 26.  */
1376               {
1377                 value &= ~0x3fff;       /* Remove the sign bit.  */
1378                 value = -value;
1379               }
1380           }
1381         else
1382           {
1383             value = inst & 0xff;        /* 8 bits */
1384             regx = (inst >> 8) & 0x07;  /* or i8 funct */
1385             /* FIXME: Do sign extension, this format needs it.  */
1386             if (value & 0x80)   /* THIS CONFUSES ME.  */
1387               {
1388                 value &= 0xef;  /* Remove the sign bit.  */
1389                 value = -value;
1390               }
1391           }
1392         offset = value;
1393         regy = -1;
1394         break;
1395       }
1396     case jalxtype:
1397       {
1398         unsigned long value;
1399         unsigned int nexthalf;
1400         value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
1401         value = value << 16;
1402         nexthalf = mips_fetch_instruction (gdbarch, pc + 2);  /* low bit
1403                                                                  still set.  */
1404         value |= nexthalf;
1405         offset = value;
1406         regx = -1;
1407         regy = -1;
1408         break;
1409       }
1410     default:
1411       internal_error (__FILE__, __LINE__, _("bad switch"));
1412     }
1413   upk->offset = offset;
1414   upk->regx = regx;
1415   upk->regy = regy;
1416 }
1417
1418
1419 static CORE_ADDR
1420 add_offset_16 (CORE_ADDR pc, int offset)
1421 {
1422   return ((offset << 2) | ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)));
1423 }
1424
1425 static CORE_ADDR
1426 extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
1427                          unsigned int extension, unsigned int insn)
1428 {
1429   struct gdbarch *gdbarch = get_frame_arch (frame);
1430   int op = (insn >> 11);
1431   switch (op)
1432     {
1433     case 2:                     /* Branch */
1434       {
1435         CORE_ADDR offset;
1436         struct upk_mips16 upk;
1437         unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
1438         offset = upk.offset;
1439         if (offset & 0x800)
1440           {
1441             offset &= 0xeff;
1442             offset = -offset;
1443           }
1444         pc += (offset << 1) + 2;
1445         break;
1446       }
1447     case 3:                     /* JAL , JALX - Watch out, these are 32 bit
1448                                    instructions.  */
1449       {
1450         struct upk_mips16 upk;
1451         unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
1452         pc = add_offset_16 (pc, upk.offset);
1453         if ((insn >> 10) & 0x01)        /* Exchange mode */
1454           pc = pc & ~0x01;      /* Clear low bit, indicate 32 bit mode.  */
1455         else
1456           pc |= 0x01;
1457         break;
1458       }
1459     case 4:                     /* beqz */
1460       {
1461         struct upk_mips16 upk;
1462         int reg;
1463         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
1464         reg = get_frame_register_signed (frame, upk.regx);
1465         if (reg == 0)
1466           pc += (upk.offset << 1) + 2;
1467         else
1468           pc += 2;
1469         break;
1470       }
1471     case 5:                     /* bnez */
1472       {
1473         struct upk_mips16 upk;
1474         int reg;
1475         unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
1476         reg = get_frame_register_signed (frame, upk.regx);
1477         if (reg != 0)
1478           pc += (upk.offset << 1) + 2;
1479         else
1480           pc += 2;
1481         break;
1482       }
1483     case 12:                    /* I8 Formats btez btnez */
1484       {
1485         struct upk_mips16 upk;
1486         int reg;
1487         unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
1488         /* upk.regx contains the opcode */
1489         reg = get_frame_register_signed (frame, 24);  /* Test register is 24 */
1490         if (((upk.regx == 0) && (reg == 0))     /* BTEZ */
1491             || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
1492           /* pc = add_offset_16(pc,upk.offset) ; */
1493           pc += (upk.offset << 1) + 2;
1494         else
1495           pc += 2;
1496         break;
1497       }
1498     case 29:                    /* RR Formats JR, JALR, JALR-RA */
1499       {
1500         struct upk_mips16 upk;
1501         /* upk.fmt = rrtype; */
1502         op = insn & 0x1f;
1503         if (op == 0)
1504           {
1505             int reg;
1506             upk.regx = (insn >> 8) & 0x07;
1507             upk.regy = (insn >> 5) & 0x07;
1508             switch (upk.regy)
1509               {
1510               case 0:
1511                 reg = upk.regx;
1512                 break;
1513               case 1:
1514                 reg = 31;
1515                 break;          /* Function return instruction.  */
1516               case 2:
1517                 reg = upk.regx;
1518                 break;
1519               default:
1520                 reg = 31;
1521                 break;          /* BOGUS Guess */
1522               }
1523             pc = get_frame_register_signed (frame, reg);
1524           }
1525         else
1526           pc += 2;
1527         break;
1528       }
1529     case 30:
1530       /* This is an instruction extension.  Fetch the real instruction
1531          (which follows the extension) and decode things based on
1532          that.  */
1533       {
1534         pc += 2;
1535         pc = extended_mips16_next_pc (frame, pc, insn,
1536                                       fetch_mips_16 (gdbarch, pc));
1537         break;
1538       }
1539     default:
1540       {
1541         pc += 2;
1542         break;
1543       }
1544     }
1545   return pc;
1546 }
1547
1548 static CORE_ADDR
1549 mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
1550 {
1551   struct gdbarch *gdbarch = get_frame_arch (frame);
1552   unsigned int insn = fetch_mips_16 (gdbarch, pc);
1553   return extended_mips16_next_pc (frame, pc, 0, insn);
1554 }
1555
1556 /* The mips_next_pc function supports single_step when the remote
1557    target monitor or stub is not developed enough to do a single_step.
1558    It works by decoding the current instruction and predicting where a
1559    branch will go.  This isnt hard because all the data is available.
1560    The MIPS32 and MIPS16 variants are quite different.  */
1561 static CORE_ADDR
1562 mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
1563 {
1564   if (is_mips16_addr (pc))
1565     return mips16_next_pc (frame, pc);
1566   else
1567     return mips32_next_pc (frame, pc);
1568 }
1569
1570 struct mips_frame_cache
1571 {
1572   CORE_ADDR base;
1573   struct trad_frame_saved_reg *saved_regs;
1574 };
1575
1576 /* Set a register's saved stack address in temp_saved_regs.  If an
1577    address has already been set for this register, do nothing; this
1578    way we will only recognize the first save of a given register in a
1579    function prologue.
1580
1581    For simplicity, save the address in both [0 .. gdbarch_num_regs) and
1582    [gdbarch_num_regs .. 2*gdbarch_num_regs).
1583    Strictly speaking, only the second range is used as it is only second
1584    range (the ABI instead of ISA registers) that comes into play when finding
1585    saved registers in a frame.  */
1586
1587 static void
1588 set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
1589                 int regnum, CORE_ADDR offset)
1590 {
1591   if (this_cache != NULL
1592       && this_cache->saved_regs[regnum].addr == -1)
1593     {
1594       this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
1595         = offset;
1596       this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
1597         = offset;
1598     }
1599 }
1600
1601
1602 /* Fetch the immediate value from a MIPS16 instruction.
1603    If the previous instruction was an EXTEND, use it to extend
1604    the upper bits of the immediate value.  This is a helper function
1605    for mips16_scan_prologue.  */
1606
1607 static int
1608 mips16_get_imm (unsigned short prev_inst,       /* previous instruction */
1609                 unsigned short inst,    /* current instruction */
1610                 int nbits,      /* number of bits in imm field */
1611                 int scale,      /* scale factor to be applied to imm */
1612                 int is_signed)  /* is the imm field signed?  */
1613 {
1614   int offset;
1615
1616   if ((prev_inst & 0xf800) == 0xf000)   /* prev instruction was EXTEND? */
1617     {
1618       offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
1619       if (offset & 0x8000)      /* check for negative extend */
1620         offset = 0 - (0x10000 - (offset & 0xffff));
1621       return offset | (inst & 0x1f);
1622     }
1623   else
1624     {
1625       int max_imm = 1 << nbits;
1626       int mask = max_imm - 1;
1627       int sign_bit = max_imm >> 1;
1628
1629       offset = inst & mask;
1630       if (is_signed && (offset & sign_bit))
1631         offset = 0 - (max_imm - offset);
1632       return offset * scale;
1633     }
1634 }
1635
1636
1637 /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
1638    the associated FRAME_CACHE if not null.
1639    Return the address of the first instruction past the prologue.  */
1640
1641 static CORE_ADDR
1642 mips16_scan_prologue (struct gdbarch *gdbarch,
1643                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
1644                       struct frame_info *this_frame,
1645                       struct mips_frame_cache *this_cache)
1646 {
1647   CORE_ADDR cur_pc;
1648   CORE_ADDR frame_addr = 0;     /* Value of $r17, used as frame pointer.  */
1649   CORE_ADDR sp;
1650   long frame_offset = 0;        /* Size of stack frame.  */
1651   long frame_adjust = 0;        /* Offset of FP from SP.  */
1652   int frame_reg = MIPS_SP_REGNUM;
1653   unsigned short prev_inst = 0; /* saved copy of previous instruction.  */
1654   unsigned inst = 0;            /* current instruction */
1655   unsigned entry_inst = 0;      /* the entry instruction */
1656   unsigned save_inst = 0;       /* the save instruction */
1657   int reg, offset;
1658
1659   int extend_bytes = 0;
1660   int prev_extend_bytes;
1661   CORE_ADDR end_prologue_addr = 0;
1662
1663   /* Can be called when there's no process, and hence when there's no
1664      THIS_FRAME.  */
1665   if (this_frame != NULL)
1666     sp = get_frame_register_signed (this_frame,
1667                                     gdbarch_num_regs (gdbarch)
1668                                     + MIPS_SP_REGNUM);
1669   else
1670     sp = 0;
1671
1672   if (limit_pc > start_pc + 200)
1673     limit_pc = start_pc + 200;
1674
1675   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
1676     {
1677       /* Save the previous instruction.  If it's an EXTEND, we'll extract
1678          the immediate offset extension from it in mips16_get_imm.  */
1679       prev_inst = inst;
1680
1681       /* Fetch and decode the instruction.  */
1682       inst = (unsigned short) mips_fetch_instruction (gdbarch, cur_pc);
1683
1684       /* Normally we ignore extend instructions.  However, if it is
1685          not followed by a valid prologue instruction, then this
1686          instruction is not part of the prologue either.  We must
1687          remember in this case to adjust the end_prologue_addr back
1688          over the extend.  */
1689       if ((inst & 0xf800) == 0xf000)    /* extend */
1690         {
1691           extend_bytes = MIPS_INSN16_SIZE;
1692           continue;
1693         }
1694
1695       prev_extend_bytes = extend_bytes;
1696       extend_bytes = 0;
1697
1698       if ((inst & 0xff00) == 0x6300     /* addiu sp */
1699           || (inst & 0xff00) == 0xfb00) /* daddiu sp */
1700         {
1701           offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
1702           if (offset < 0)       /* Negative stack adjustment?  */
1703             frame_offset -= offset;
1704           else
1705             /* Exit loop if a positive stack adjustment is found, which
1706                usually means that the stack cleanup code in the function
1707                epilogue is reached.  */
1708             break;
1709         }
1710       else if ((inst & 0xf800) == 0xd000)       /* sw reg,n($sp) */
1711         {
1712           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1713           reg = mips16_to_32_reg[(inst & 0x700) >> 8];
1714           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1715         }
1716       else if ((inst & 0xff00) == 0xf900)       /* sd reg,n($sp) */
1717         {
1718           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1719           reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1720           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1721         }
1722       else if ((inst & 0xff00) == 0x6200)       /* sw $ra,n($sp) */
1723         {
1724           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1725           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
1726         }
1727       else if ((inst & 0xff00) == 0xfa00)       /* sd $ra,n($sp) */
1728         {
1729           offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
1730           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
1731         }
1732       else if (inst == 0x673d)  /* move $s1, $sp */
1733         {
1734           frame_addr = sp;
1735           frame_reg = 17;
1736         }
1737       else if ((inst & 0xff00) == 0x0100)       /* addiu $s1,sp,n */
1738         {
1739           offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1740           frame_addr = sp + offset;
1741           frame_reg = 17;
1742           frame_adjust = offset;
1743         }
1744       else if ((inst & 0xFF00) == 0xd900)       /* sw reg,offset($s1) */
1745         {
1746           offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
1747           reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1748           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
1749         }
1750       else if ((inst & 0xFF00) == 0x7900)       /* sd reg,offset($s1) */
1751         {
1752           offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1753           reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1754           set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
1755         }
1756       else if ((inst & 0xf81f) == 0xe809
1757                && (inst & 0x700) != 0x700)      /* entry */
1758         entry_inst = inst;      /* Save for later processing.  */
1759       else if ((inst & 0xff80) == 0x6480)       /* save */
1760         {
1761           save_inst = inst;     /* Save for later processing.  */
1762           if (prev_extend_bytes)                /* extend */
1763             save_inst |= prev_inst << 16;
1764         }
1765       else if ((inst & 0xf800) == 0x1800)       /* jal(x) */
1766         cur_pc += MIPS_INSN16_SIZE;     /* 32-bit instruction */
1767       else if ((inst & 0xff1c) == 0x6704)       /* move reg,$a0-$a3 */
1768         {
1769           /* This instruction is part of the prologue, but we don't
1770              need to do anything special to handle it.  */
1771         }
1772       else
1773         {
1774           /* This instruction is not an instruction typically found
1775              in a prologue, so we must have reached the end of the
1776              prologue.  */
1777           if (end_prologue_addr == 0)
1778             end_prologue_addr = cur_pc - prev_extend_bytes;
1779         }
1780     }
1781
1782   /* The entry instruction is typically the first instruction in a function,
1783      and it stores registers at offsets relative to the value of the old SP
1784      (before the prologue).  But the value of the sp parameter to this
1785      function is the new SP (after the prologue has been executed).  So we
1786      can't calculate those offsets until we've seen the entire prologue,
1787      and can calculate what the old SP must have been.  */
1788   if (entry_inst != 0)
1789     {
1790       int areg_count = (entry_inst >> 8) & 7;
1791       int sreg_count = (entry_inst >> 6) & 3;
1792
1793       /* The entry instruction always subtracts 32 from the SP.  */
1794       frame_offset += 32;
1795
1796       /* Now we can calculate what the SP must have been at the
1797          start of the function prologue.  */
1798       sp += frame_offset;
1799
1800       /* Check if a0-a3 were saved in the caller's argument save area.  */
1801       for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
1802         {
1803           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1804           offset += mips_abi_regsize (gdbarch);
1805         }
1806
1807       /* Check if the ra register was pushed on the stack.  */
1808       offset = -4;
1809       if (entry_inst & 0x20)
1810         {
1811           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
1812           offset -= mips_abi_regsize (gdbarch);
1813         }
1814
1815       /* Check if the s0 and s1 registers were pushed on the stack.  */
1816       for (reg = 16; reg < sreg_count + 16; reg++)
1817         {
1818           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1819           offset -= mips_abi_regsize (gdbarch);
1820         }
1821     }
1822
1823   /* The SAVE instruction is similar to ENTRY, except that defined by the
1824      MIPS16e ASE of the MIPS Architecture.  Unlike with ENTRY though, the
1825      size of the frame is specified as an immediate field of instruction
1826      and an extended variation exists which lets additional registers and
1827      frame space to be specified.  The instruction always treats registers
1828      as 32-bit so its usefulness for 64-bit ABIs is questionable.  */
1829   if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
1830     {
1831       static int args_table[16] = {
1832         0, 0, 0, 0, 1, 1, 1, 1,
1833         2, 2, 2, 0, 3, 3, 4, -1,
1834       };
1835       static int astatic_table[16] = {
1836         0, 1, 2, 3, 0, 1, 2, 3,
1837         0, 1, 2, 4, 0, 1, 0, -1,
1838       };
1839       int aregs = (save_inst >> 16) & 0xf;
1840       int xsregs = (save_inst >> 24) & 0x7;
1841       int args = args_table[aregs];
1842       int astatic = astatic_table[aregs];
1843       long frame_size;
1844
1845       if (args < 0)
1846         {
1847           warning (_("Invalid number of argument registers encoded in SAVE."));
1848           args = 0;
1849         }
1850       if (astatic < 0)
1851         {
1852           warning (_("Invalid number of static registers encoded in SAVE."));
1853           astatic = 0;
1854         }
1855
1856       /* For standard SAVE the frame size of 0 means 128.  */
1857       frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
1858       if (frame_size == 0 && (save_inst >> 16) == 0)
1859         frame_size = 16;
1860       frame_size *= 8;
1861       frame_offset += frame_size;
1862
1863       /* Now we can calculate what the SP must have been at the
1864          start of the function prologue.  */
1865       sp += frame_offset;
1866
1867       /* Check if A0-A3 were saved in the caller's argument save area.  */
1868       for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
1869         {
1870           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1871           offset += mips_abi_regsize (gdbarch);
1872         }
1873
1874       offset = -4;
1875
1876       /* Check if the RA register was pushed on the stack.  */
1877       if (save_inst & 0x40)
1878         {
1879           set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
1880           offset -= mips_abi_regsize (gdbarch);
1881         }
1882
1883       /* Check if the S8 register was pushed on the stack.  */
1884       if (xsregs > 6)
1885         {
1886           set_reg_offset (gdbarch, this_cache, 30, sp + offset);
1887           offset -= mips_abi_regsize (gdbarch);
1888           xsregs--;
1889         }
1890       /* Check if S2-S7 were pushed on the stack.  */
1891       for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
1892         {
1893           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1894           offset -= mips_abi_regsize (gdbarch);
1895         }
1896
1897       /* Check if the S1 register was pushed on the stack.  */
1898       if (save_inst & 0x10)
1899         {
1900           set_reg_offset (gdbarch, this_cache, 17, sp + offset);
1901           offset -= mips_abi_regsize (gdbarch);
1902         }
1903       /* Check if the S0 register was pushed on the stack.  */
1904       if (save_inst & 0x20)
1905         {
1906           set_reg_offset (gdbarch, this_cache, 16, sp + offset);
1907           offset -= mips_abi_regsize (gdbarch);
1908         }
1909
1910       /* Check if A0-A3 were pushed on the stack.  */
1911       for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
1912         {
1913           set_reg_offset (gdbarch, this_cache, reg, sp + offset);
1914           offset -= mips_abi_regsize (gdbarch);
1915         }
1916     }
1917
1918   if (this_cache != NULL)
1919     {
1920       this_cache->base =
1921         (get_frame_register_signed (this_frame,
1922                                     gdbarch_num_regs (gdbarch) + frame_reg)
1923          + frame_offset - frame_adjust);
1924       /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
1925          be able to get rid of the assignment below, evetually.  But it's
1926          still needed for now.  */
1927       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
1928                              + mips_regnum (gdbarch)->pc]
1929         = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
1930     }
1931
1932   /* If we didn't reach the end of the prologue when scanning the function
1933      instructions, then set end_prologue_addr to the address of the
1934      instruction immediately after the last one we scanned.  */
1935   if (end_prologue_addr == 0)
1936     end_prologue_addr = cur_pc;
1937
1938   return end_prologue_addr;
1939 }
1940
1941 /* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
1942    Procedures that use the 32-bit instruction set are handled by the
1943    mips_insn32 unwinder.  */
1944
1945 static struct mips_frame_cache *
1946 mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
1947 {
1948   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1949   struct mips_frame_cache *cache;
1950
1951   if ((*this_cache) != NULL)
1952     return (*this_cache);
1953   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
1954   (*this_cache) = cache;
1955   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1956
1957   /* Analyze the function prologue.  */
1958   {
1959     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
1960     CORE_ADDR start_addr;
1961
1962     find_pc_partial_function (pc, NULL, &start_addr, NULL);
1963     if (start_addr == 0)
1964       start_addr = heuristic_proc_start (gdbarch, pc);
1965     /* We can't analyze the prologue if we couldn't find the begining
1966        of the function.  */
1967     if (start_addr == 0)
1968       return cache;
1969
1970     mips16_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
1971   }
1972   
1973   /* gdbarch_sp_regnum contains the value and not the address.  */
1974   trad_frame_set_value (cache->saved_regs,
1975                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
1976                         cache->base);
1977
1978   return (*this_cache);
1979 }
1980
1981 static void
1982 mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
1983                            struct frame_id *this_id)
1984 {
1985   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
1986                                                            this_cache);
1987   /* This marks the outermost frame.  */
1988   if (info->base == 0)
1989     return;
1990   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
1991 }
1992
1993 static struct value *
1994 mips_insn16_frame_prev_register (struct frame_info *this_frame,
1995                                  void **this_cache, int regnum)
1996 {
1997   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
1998                                                            this_cache);
1999   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2000 }
2001
2002 static int
2003 mips_insn16_frame_sniffer (const struct frame_unwind *self,
2004                            struct frame_info *this_frame, void **this_cache)
2005 {
2006   CORE_ADDR pc = get_frame_pc (this_frame);
2007   if (mips_pc_is_mips16 (pc))
2008     return 1;
2009   return 0;
2010 }
2011
2012 static const struct frame_unwind mips_insn16_frame_unwind =
2013 {
2014   NORMAL_FRAME,
2015   mips_insn16_frame_this_id,
2016   mips_insn16_frame_prev_register,
2017   NULL,
2018   mips_insn16_frame_sniffer
2019 };
2020
2021 static CORE_ADDR
2022 mips_insn16_frame_base_address (struct frame_info *this_frame,
2023                                 void **this_cache)
2024 {
2025   struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2026                                                            this_cache);
2027   return info->base;
2028 }
2029
2030 static const struct frame_base mips_insn16_frame_base =
2031 {
2032   &mips_insn16_frame_unwind,
2033   mips_insn16_frame_base_address,
2034   mips_insn16_frame_base_address,
2035   mips_insn16_frame_base_address
2036 };
2037
2038 static const struct frame_base *
2039 mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
2040 {
2041   CORE_ADDR pc = get_frame_pc (this_frame);
2042   if (mips_pc_is_mips16 (pc))
2043     return &mips_insn16_frame_base;
2044   else
2045     return NULL;
2046 }
2047
2048 /* Mark all the registers as unset in the saved_regs array
2049    of THIS_CACHE.  Do nothing if THIS_CACHE is null.  */
2050
2051 static void
2052 reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
2053 {
2054   if (this_cache == NULL || this_cache->saved_regs == NULL)
2055     return;
2056
2057   {
2058     const int num_regs = gdbarch_num_regs (gdbarch);
2059     int i;
2060
2061     for (i = 0; i < num_regs; i++)
2062       {
2063         this_cache->saved_regs[i].addr = -1;
2064       }
2065   }
2066 }
2067
2068 /* Analyze the function prologue from START_PC to LIMIT_PC.  Builds
2069    the associated FRAME_CACHE if not null.  
2070    Return the address of the first instruction past the prologue.  */
2071
2072 static CORE_ADDR
2073 mips32_scan_prologue (struct gdbarch *gdbarch,
2074                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
2075                       struct frame_info *this_frame,
2076                       struct mips_frame_cache *this_cache)
2077 {
2078   CORE_ADDR cur_pc;
2079   CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
2080                                frame-pointer.  */
2081   CORE_ADDR sp;
2082   long frame_offset;
2083   int  frame_reg = MIPS_SP_REGNUM;
2084
2085   CORE_ADDR end_prologue_addr = 0;
2086   int seen_sp_adjust = 0;
2087   int load_immediate_bytes = 0;
2088   int in_delay_slot = 0;
2089   int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);
2090
2091   /* Can be called when there's no process, and hence when there's no
2092      THIS_FRAME.  */
2093   if (this_frame != NULL)
2094     sp = get_frame_register_signed (this_frame,
2095                                     gdbarch_num_regs (gdbarch)
2096                                     + MIPS_SP_REGNUM);
2097   else
2098     sp = 0;
2099
2100   if (limit_pc > start_pc + 200)
2101     limit_pc = start_pc + 200;
2102
2103 restart:
2104
2105   frame_offset = 0;
2106   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
2107     {
2108       unsigned long inst, high_word, low_word;
2109       int reg;
2110
2111       /* Fetch the instruction.  */
2112       inst = (unsigned long) mips_fetch_instruction (gdbarch, cur_pc);
2113
2114       /* Save some code by pre-extracting some useful fields.  */
2115       high_word = (inst >> 16) & 0xffff;
2116       low_word = inst & 0xffff;
2117       reg = high_word & 0x1f;
2118
2119       if (high_word == 0x27bd           /* addiu $sp,$sp,-i */
2120           || high_word == 0x23bd        /* addi $sp,$sp,-i */
2121           || high_word == 0x67bd)       /* daddiu $sp,$sp,-i */
2122         {
2123           if (low_word & 0x8000)        /* Negative stack adjustment?  */
2124             frame_offset += 0x10000 - low_word;
2125           else
2126             /* Exit loop if a positive stack adjustment is found, which
2127                usually means that the stack cleanup code in the function
2128                epilogue is reached.  */
2129             break;
2130           seen_sp_adjust = 1;
2131         }
2132       else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
2133                && !regsize_is_64_bits)
2134         {
2135           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
2136         }
2137       else if (((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
2138                && regsize_is_64_bits)
2139         {
2140           /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra.  */
2141           set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
2142         }
2143       else if (high_word == 0x27be)     /* addiu $30,$sp,size */
2144         {
2145           /* Old gcc frame, r30 is virtual frame pointer.  */
2146           if ((long) low_word != frame_offset)
2147             frame_addr = sp + low_word;
2148           else if (this_frame && frame_reg == MIPS_SP_REGNUM)
2149             {
2150               unsigned alloca_adjust;
2151
2152               frame_reg = 30;
2153               frame_addr = get_frame_register_signed
2154                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
2155
2156               alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
2157               if (alloca_adjust > 0)
2158                 {
2159                   /* FP > SP + frame_size.  This may be because of
2160                      an alloca or somethings similar.  Fix sp to
2161                      "pre-alloca" value, and try again.  */
2162                   sp += alloca_adjust;
2163                   /* Need to reset the status of all registers.  Otherwise,
2164                      we will hit a guard that prevents the new address
2165                      for each register to be recomputed during the second
2166                      pass.  */
2167                   reset_saved_regs (gdbarch, this_cache);
2168                   goto restart;
2169                 }
2170             }
2171         }
2172       /* move $30,$sp.  With different versions of gas this will be either
2173          `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
2174          Accept any one of these.  */
2175       else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
2176         {
2177           /* New gcc frame, virtual frame pointer is at r30 + frame_size.  */
2178           if (this_frame && frame_reg == MIPS_SP_REGNUM)
2179             {
2180               unsigned alloca_adjust;
2181
2182               frame_reg = 30;
2183               frame_addr = get_frame_register_signed
2184                 (this_frame, gdbarch_num_regs (gdbarch) + 30);
2185
2186               alloca_adjust = (unsigned) (frame_addr - sp);
2187               if (alloca_adjust > 0)
2188                 {
2189                   /* FP > SP + frame_size.  This may be because of
2190                      an alloca or somethings similar.  Fix sp to
2191                      "pre-alloca" value, and try again.  */
2192                   sp = frame_addr;
2193                   /* Need to reset the status of all registers.  Otherwise,
2194                      we will hit a guard that prevents the new address
2195                      for each register to be recomputed during the second
2196                      pass.  */
2197                   reset_saved_regs (gdbarch, this_cache);
2198                   goto restart;
2199                 }
2200             }
2201         }
2202       else if ((high_word & 0xFFE0) == 0xafc0   /* sw reg,offset($30) */
2203                && !regsize_is_64_bits)
2204         {
2205           set_reg_offset (gdbarch, this_cache, reg, frame_addr + low_word);
2206         }
2207       else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
2208                || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
2209                || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
2210                || high_word == 0x3c1c /* lui $gp,n */
2211                || high_word == 0x279c /* addiu $gp,$gp,n */
2212                || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
2213                || inst == 0x033ce021 /* addu $gp,$t9,$gp */
2214               )
2215        {
2216          /* These instructions are part of the prologue, but we don't
2217             need to do anything special to handle them.  */
2218        }
2219       /* The instructions below load $at or $t0 with an immediate
2220          value in preparation for a stack adjustment via
2221          subu $sp,$sp,[$at,$t0].  These instructions could also
2222          initialize a local variable, so we accept them only before
2223          a stack adjustment instruction was seen.  */
2224       else if (!seen_sp_adjust
2225                && (high_word == 0x3c01 /* lui $at,n */
2226                    || high_word == 0x3c08 /* lui $t0,n */
2227                    || high_word == 0x3421 /* ori $at,$at,n */
2228                    || high_word == 0x3508 /* ori $t0,$t0,n */
2229                    || high_word == 0x3401 /* ori $at,$zero,n */
2230                    || high_word == 0x3408 /* ori $t0,$zero,n */
2231                   ))
2232        {
2233           load_immediate_bytes += MIPS_INSN32_SIZE;             /* FIXME!  */
2234        }
2235       else
2236        {
2237          /* This instruction is not an instruction typically found
2238             in a prologue, so we must have reached the end of the
2239             prologue.  */
2240          /* FIXME: brobecker/2004-10-10: Can't we just break out of this
2241             loop now?  Why would we need to continue scanning the function
2242             instructions?  */
2243          if (end_prologue_addr == 0)
2244            end_prologue_addr = cur_pc;
2245
2246          /* Check for branches and jumps.  For now, only jump to
2247             register are caught (i.e. returns).  */
2248          if ((itype_op (inst) & 0x07) == 0 && rtype_funct (inst) == 8)
2249            in_delay_slot = 1;
2250        }
2251
2252       /* If the previous instruction was a jump, we must have reached
2253          the end of the prologue by now.  Stop scanning so that we do
2254          not go past the function return.  */
2255       if (in_delay_slot)
2256         break;
2257     }
2258
2259   if (this_cache != NULL)
2260     {
2261       this_cache->base = 
2262         (get_frame_register_signed (this_frame,
2263                                     gdbarch_num_regs (gdbarch) + frame_reg)
2264          + frame_offset);
2265       /* FIXME: brobecker/2004-09-15: We should be able to get rid of
2266          this assignment below, eventually.  But it's still needed
2267          for now.  */
2268       this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2269                              + mips_regnum (gdbarch)->pc]
2270         = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2271                                  + MIPS_RA_REGNUM];
2272     }
2273
2274   /* If we didn't reach the end of the prologue when scanning the function
2275      instructions, then set end_prologue_addr to the address of the
2276      instruction immediately after the last one we scanned.  */
2277   /* brobecker/2004-10-10: I don't think this would ever happen, but
2278      we may as well be careful and do our best if we have a null
2279      end_prologue_addr.  */
2280   if (end_prologue_addr == 0)
2281     end_prologue_addr = cur_pc;
2282      
2283   /* In a frameless function, we might have incorrectly
2284      skipped some load immediate instructions.  Undo the skipping
2285      if the load immediate was not followed by a stack adjustment.  */
2286   if (load_immediate_bytes && !seen_sp_adjust)
2287     end_prologue_addr -= load_immediate_bytes;
2288
2289   return end_prologue_addr;
2290 }
2291
2292 /* Heuristic unwinder for procedures using 32-bit instructions (covers
2293    both 32-bit and 64-bit MIPS ISAs).  Procedures using 16-bit
2294    instructions (a.k.a. MIPS16) are handled by the mips_insn16
2295    unwinder.  */
2296
2297 static struct mips_frame_cache *
2298 mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
2299 {
2300   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2301   struct mips_frame_cache *cache;
2302
2303   if ((*this_cache) != NULL)
2304     return (*this_cache);
2305
2306   cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
2307   (*this_cache) = cache;
2308   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2309
2310   /* Analyze the function prologue.  */
2311   {
2312     const CORE_ADDR pc = get_frame_address_in_block (this_frame);
2313     CORE_ADDR start_addr;
2314
2315     find_pc_partial_function (pc, NULL, &start_addr, NULL);
2316     if (start_addr == 0)
2317       start_addr = heuristic_proc_start (gdbarch, pc);
2318     /* We can't analyze the prologue if we couldn't find the begining
2319        of the function.  */
2320     if (start_addr == 0)
2321       return cache;
2322
2323     mips32_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
2324   }
2325   
2326   /* gdbarch_sp_regnum contains the value and not the address.  */
2327   trad_frame_set_value (cache->saved_regs,
2328                         gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
2329                         cache->base);
2330
2331   return (*this_cache);
2332 }
2333
2334 static void
2335 mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
2336                            struct frame_id *this_id)
2337 {
2338   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
2339                                                            this_cache);
2340   /* This marks the outermost frame.  */
2341   if (info->base == 0)
2342     return;
2343   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
2344 }
2345
2346 static struct value *
2347 mips_insn32_frame_prev_register (struct frame_info *this_frame,
2348                                  void **this_cache, int regnum)
2349 {
2350   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
2351                                                            this_cache);
2352   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2353 }
2354
2355 static int
2356 mips_insn32_frame_sniffer (const struct frame_unwind *self,
2357                            struct frame_info *this_frame, void **this_cache)
2358 {
2359   CORE_ADDR pc = get_frame_pc (this_frame);
2360   if (! mips_pc_is_mips16 (pc))
2361     return 1;
2362   return 0;
2363 }
2364
2365 static const struct frame_unwind mips_insn32_frame_unwind =
2366 {
2367   NORMAL_FRAME,
2368   mips_insn32_frame_this_id,
2369   mips_insn32_frame_prev_register,
2370   NULL,
2371   mips_insn32_frame_sniffer
2372 };
2373
2374 static CORE_ADDR
2375 mips_insn32_frame_base_address (struct frame_info *this_frame,
2376                                 void **this_cache)
2377 {
2378   struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
2379                                                            this_cache);
2380   return info->base;
2381 }
2382
2383 static const struct frame_base mips_insn32_frame_base =
2384 {
2385   &mips_insn32_frame_unwind,
2386   mips_insn32_frame_base_address,
2387   mips_insn32_frame_base_address,
2388   mips_insn32_frame_base_address
2389 };
2390
2391 static const struct frame_base *
2392 mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
2393 {
2394   CORE_ADDR pc = get_frame_pc (this_frame);
2395   if (! mips_pc_is_mips16 (pc))
2396     return &mips_insn32_frame_base;
2397   else
2398     return NULL;
2399 }
2400
2401 static struct trad_frame_cache *
2402 mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
2403 {
2404   CORE_ADDR pc;
2405   CORE_ADDR start_addr;
2406   CORE_ADDR stack_addr;
2407   struct trad_frame_cache *this_trad_cache;
2408   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2409   int num_regs = gdbarch_num_regs (gdbarch);
2410
2411   if ((*this_cache) != NULL)
2412     return (*this_cache);
2413   this_trad_cache = trad_frame_cache_zalloc (this_frame);
2414   (*this_cache) = this_trad_cache;
2415
2416   /* The return address is in the link register.  */
2417   trad_frame_set_reg_realreg (this_trad_cache,
2418                               gdbarch_pc_regnum (gdbarch),
2419                               num_regs + MIPS_RA_REGNUM);
2420
2421   /* Frame ID, since it's a frameless / stackless function, no stack
2422      space is allocated and SP on entry is the current SP.  */
2423   pc = get_frame_pc (this_frame);
2424   find_pc_partial_function (pc, NULL, &start_addr, NULL);
2425   stack_addr = get_frame_register_signed (this_frame,
2426                                           num_regs + MIPS_SP_REGNUM);
2427   trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
2428
2429   /* Assume that the frame's base is the same as the
2430      stack-pointer.  */
2431   trad_frame_set_this_base (this_trad_cache, stack_addr);
2432
2433   return this_trad_cache;
2434 }
2435
2436 static void
2437 mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
2438                          struct frame_id *this_id)
2439 {
2440   struct trad_frame_cache *this_trad_cache
2441     = mips_stub_frame_cache (this_frame, this_cache);
2442   trad_frame_get_id (this_trad_cache, this_id);
2443 }
2444
2445 static struct value *
2446 mips_stub_frame_prev_register (struct frame_info *this_frame,
2447                                void **this_cache, int regnum)
2448 {
2449   struct trad_frame_cache *this_trad_cache
2450     = mips_stub_frame_cache (this_frame, this_cache);
2451   return trad_frame_get_register (this_trad_cache, this_frame, regnum);
2452 }
2453
2454 static int
2455 mips_stub_frame_sniffer (const struct frame_unwind *self,
2456                          struct frame_info *this_frame, void **this_cache)
2457 {
2458   gdb_byte dummy[4];
2459   struct obj_section *s;
2460   CORE_ADDR pc = get_frame_address_in_block (this_frame);
2461   struct minimal_symbol *msym;
2462
2463   /* Use the stub unwinder for unreadable code.  */
2464   if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2465     return 1;
2466
2467   if (in_plt_section (pc, NULL))
2468     return 1;
2469
2470   /* Binutils for MIPS puts lazy resolution stubs into .MIPS.stubs.  */
2471   s = find_pc_section (pc);
2472
2473   if (s != NULL
2474       && strcmp (bfd_get_section_name (s->objfile->obfd, s->the_bfd_section),
2475                  ".MIPS.stubs") == 0)
2476     return 1;
2477
2478   /* Calling a PIC function from a non-PIC function passes through a
2479      stub.  The stub for foo is named ".pic.foo".  */
2480   msym = lookup_minimal_symbol_by_pc (pc);
2481   if (msym != NULL
2482       && SYMBOL_LINKAGE_NAME (msym) != NULL
2483       && strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) == 0)
2484     return 1;
2485
2486   return 0;
2487 }
2488
2489 static const struct frame_unwind mips_stub_frame_unwind =
2490 {
2491   NORMAL_FRAME,
2492   mips_stub_frame_this_id,
2493   mips_stub_frame_prev_register,
2494   NULL,
2495   mips_stub_frame_sniffer
2496 };
2497
2498 static CORE_ADDR
2499 mips_stub_frame_base_address (struct frame_info *this_frame,
2500                               void **this_cache)
2501 {
2502   struct trad_frame_cache *this_trad_cache
2503     = mips_stub_frame_cache (this_frame, this_cache);
2504   return trad_frame_get_this_base (this_trad_cache);
2505 }
2506
2507 static const struct frame_base mips_stub_frame_base =
2508 {
2509   &mips_stub_frame_unwind,
2510   mips_stub_frame_base_address,
2511   mips_stub_frame_base_address,
2512   mips_stub_frame_base_address
2513 };
2514
2515 static const struct frame_base *
2516 mips_stub_frame_base_sniffer (struct frame_info *this_frame)
2517 {
2518   if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
2519     return &mips_stub_frame_base;
2520   else
2521     return NULL;
2522 }
2523
2524 /* mips_addr_bits_remove - remove useless address bits  */
2525
2526 static CORE_ADDR
2527 mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
2528 {
2529   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2530
2531   if (is_mips16_addr (addr))
2532     addr = unmake_mips16_addr (addr);
2533
2534   if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
2535     /* This hack is a work-around for existing boards using PMON, the
2536        simulator, and any other 64-bit targets that doesn't have true
2537        64-bit addressing.  On these targets, the upper 32 bits of
2538        addresses are ignored by the hardware.  Thus, the PC or SP are
2539        likely to have been sign extended to all 1s by instruction
2540        sequences that load 32-bit addresses.  For example, a typical
2541        piece of code that loads an address is this:
2542
2543        lui $r2, <upper 16 bits>
2544        ori $r2, <lower 16 bits>
2545
2546        But the lui sign-extends the value such that the upper 32 bits
2547        may be all 1s.  The workaround is simply to mask off these
2548        bits.  In the future, gcc may be changed to support true 64-bit
2549        addressing, and this masking will have to be disabled.  */
2550     return addr &= 0xffffffffUL;
2551   else
2552     return addr;
2553 }
2554
2555 /* Instructions used during single-stepping of atomic sequences.  */
2556 #define LL_OPCODE 0x30
2557 #define LLD_OPCODE 0x34
2558 #define SC_OPCODE 0x38
2559 #define SCD_OPCODE 0x3c
2560
2561 /* Checks for an atomic sequence of instructions beginning with a LL/LLD
2562    instruction and ending with a SC/SCD instruction.  If such a sequence
2563    is found, attempt to step through it.  A breakpoint is placed at the end of 
2564    the sequence.  */
2565
2566 static int
2567 deal_with_atomic_sequence (struct gdbarch *gdbarch,
2568                            struct address_space *aspace, CORE_ADDR pc)
2569 {
2570   CORE_ADDR breaks[2] = {-1, -1};
2571   CORE_ADDR loc = pc;
2572   CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination.  */
2573   unsigned long insn;
2574   int insn_count;
2575   int index;
2576   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */  
2577   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
2578
2579   if (pc & 0x01)
2580     return 0;
2581
2582   insn = mips_fetch_instruction (gdbarch, loc);
2583   /* Assume all atomic sequences start with a ll/lld instruction.  */
2584   if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
2585     return 0;
2586
2587   /* Assume that no atomic sequence is longer than "atomic_sequence_length" 
2588      instructions.  */
2589   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
2590     {
2591       int is_branch = 0;
2592       loc += MIPS_INSN32_SIZE;
2593       insn = mips_fetch_instruction (gdbarch, loc);
2594
2595       /* Assume that there is at most one branch in the atomic
2596          sequence.  If a branch is found, put a breakpoint in its
2597          destination address.  */
2598       switch (itype_op (insn))
2599         {
2600         case 0: /* SPECIAL */
2601           if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
2602             return 0; /* fallback to the standard single-step code.  */
2603           break;
2604         case 1: /* REGIMM */
2605           is_branch = ((itype_rt (insn) & 0xc0) == 0); /* B{LT,GE}Z* */
2606           break;
2607         case 2: /* J */
2608         case 3: /* JAL */
2609           return 0; /* fallback to the standard single-step code.  */
2610         case 4: /* BEQ */
2611         case 5: /* BNE */
2612         case 6: /* BLEZ */
2613         case 7: /* BGTZ */
2614         case 20: /* BEQL */
2615         case 21: /* BNEL */
2616         case 22: /* BLEZL */
2617         case 23: /* BGTTL */
2618           is_branch = 1;
2619           break;
2620         case 17: /* COP1 */
2621         case 18: /* COP2 */
2622         case 19: /* COP3 */
2623           is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
2624           break;
2625         }
2626       if (is_branch)
2627         {
2628           branch_bp = loc + mips32_relative_offset (insn) + 4;
2629           if (last_breakpoint >= 1)
2630             return 0; /* More than one branch found, fallback to the
2631                          standard single-step code.  */
2632           breaks[1] = branch_bp;
2633           last_breakpoint++;
2634         }
2635
2636       if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
2637         break;
2638     }
2639
2640   /* Assume that the atomic sequence ends with a sc/scd instruction.  */
2641   if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
2642     return 0;
2643
2644   loc += MIPS_INSN32_SIZE;
2645
2646   /* Insert a breakpoint right after the end of the atomic sequence.  */
2647   breaks[0] = loc;
2648
2649   /* Check for duplicated breakpoints.  Check also for a breakpoint
2650      placed (branch instruction's destination) in the atomic sequence.  */
2651   if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
2652     last_breakpoint = 0;
2653
2654   /* Effectively inserts the breakpoints.  */
2655   for (index = 0; index <= last_breakpoint; index++)
2656     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
2657
2658   return 1;
2659 }
2660
2661 /* mips_software_single_step() is called just before we want to resume
2662    the inferior, if we want to single-step it but there is no hardware
2663    or kernel single-step support (MIPS on GNU/Linux for example).  We find
2664    the target of the coming instruction and breakpoint it.  */
2665
2666 int
2667 mips_software_single_step (struct frame_info *frame)
2668 {
2669   struct gdbarch *gdbarch = get_frame_arch (frame);
2670   struct address_space *aspace = get_frame_address_space (frame);
2671   CORE_ADDR pc, next_pc;
2672
2673   pc = get_frame_pc (frame);
2674   if (deal_with_atomic_sequence (gdbarch, aspace, pc))
2675     return 1;
2676
2677   next_pc = mips_next_pc (frame, pc);
2678
2679   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
2680   return 1;
2681 }
2682
2683 /* Test whether the PC points to the return instruction at the
2684    end of a function.  */
2685
2686 static int
2687 mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
2688 {
2689   if (mips_pc_is_mips16 (pc))
2690     /* This mips16 case isn't necessarily reliable.  Sometimes the compiler
2691        generates a "jr $ra"; other times it generates code to load
2692        the return address from the stack to an accessible register (such
2693        as $a3), then a "jr" using that register.  This second case
2694        is almost impossible to distinguish from an indirect jump
2695        used for switch statements, so we don't even try.  */
2696     return mips_fetch_instruction (gdbarch, pc) == 0xe820;      /* jr $ra */
2697   else
2698     return mips_fetch_instruction (gdbarch, pc) == 0x3e00008;   /* jr $ra */
2699 }
2700
2701
2702 /* This fencepost looks highly suspicious to me.  Removing it also
2703    seems suspicious as it could affect remote debugging across serial
2704    lines.  */
2705
2706 static CORE_ADDR
2707 heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
2708 {
2709   CORE_ADDR start_pc;
2710   CORE_ADDR fence;
2711   int instlen;
2712   int seen_adjsp = 0;
2713   struct inferior *inf;
2714
2715   pc = gdbarch_addr_bits_remove (gdbarch, pc);
2716   start_pc = pc;
2717   fence = start_pc - heuristic_fence_post;
2718   if (start_pc == 0)
2719     return 0;
2720
2721   if (heuristic_fence_post == UINT_MAX || fence < VM_MIN_ADDRESS)
2722     fence = VM_MIN_ADDRESS;
2723
2724   instlen = mips_pc_is_mips16 (pc) ? MIPS_INSN16_SIZE : MIPS_INSN32_SIZE;
2725
2726   inf = current_inferior ();
2727
2728   /* Search back for previous return.  */
2729   for (start_pc -= instlen;; start_pc -= instlen)
2730     if (start_pc < fence)
2731       {
2732         /* It's not clear to me why we reach this point when
2733            stop_soon, but with this test, at least we
2734            don't print out warnings for every child forked (eg, on
2735            decstation).  22apr93 rich@cygnus.com.  */
2736         if (inf->control.stop_soon == NO_STOP_QUIETLY)
2737           {
2738             static int blurb_printed = 0;
2739
2740             warning (_("GDB can't find the start of the function at %s."),
2741                      paddress (gdbarch, pc));
2742
2743             if (!blurb_printed)
2744               {
2745                 /* This actually happens frequently in embedded
2746                    development, when you first connect to a board
2747                    and your stack pointer and pc are nowhere in
2748                    particular.  This message needs to give people
2749                    in that situation enough information to
2750                    determine that it's no big deal.  */
2751                 printf_filtered ("\n\
2752     GDB is unable to find the start of the function at %s\n\
2753 and thus can't determine the size of that function's stack frame.\n\
2754 This means that GDB may be unable to access that stack frame, or\n\
2755 the frames below it.\n\
2756     This problem is most likely caused by an invalid program counter or\n\
2757 stack pointer.\n\
2758     However, if you think GDB should simply search farther back\n\
2759 from %s for code which looks like the beginning of a\n\
2760 function, you can increase the range of the search using the `set\n\
2761 heuristic-fence-post' command.\n",
2762                         paddress (gdbarch, pc), paddress (gdbarch, pc));
2763                 blurb_printed = 1;
2764               }
2765           }
2766
2767         return 0;
2768       }
2769     else if (mips_pc_is_mips16 (start_pc))
2770       {
2771         unsigned short inst;
2772
2773         /* On MIPS16, any one of the following is likely to be the
2774            start of a function:
2775            extend save
2776            save
2777            entry
2778            addiu sp,-n
2779            daddiu sp,-n
2780            extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'.  */
2781         inst = mips_fetch_instruction (gdbarch, start_pc);
2782         if ((inst & 0xff80) == 0x6480)          /* save */
2783           {
2784             if (start_pc - instlen >= fence)
2785               {
2786                 inst = mips_fetch_instruction (gdbarch, start_pc - instlen);
2787                 if ((inst & 0xf800) == 0xf000)  /* extend */
2788                   start_pc -= instlen;
2789               }
2790             break;
2791           }
2792         else if (((inst & 0xf81f) == 0xe809
2793                   && (inst & 0x700) != 0x700)   /* entry */
2794                  || (inst & 0xff80) == 0x6380   /* addiu sp,-n */
2795                  || (inst & 0xff80) == 0xfb80   /* daddiu sp,-n */
2796                  || ((inst & 0xf810) == 0xf010 && seen_adjsp))  /* extend -n */
2797           break;
2798         else if ((inst & 0xff00) == 0x6300      /* addiu sp */
2799                  || (inst & 0xff00) == 0xfb00)  /* daddiu sp */
2800           seen_adjsp = 1;
2801         else
2802           seen_adjsp = 0;
2803       }
2804     else if (mips_about_to_return (gdbarch, start_pc))
2805       {
2806         /* Skip return and its delay slot.  */
2807         start_pc += 2 * MIPS_INSN32_SIZE;
2808         break;
2809       }
2810
2811   return start_pc;
2812 }
2813
2814 struct mips_objfile_private
2815 {
2816   bfd_size_type size;
2817   char *contents;
2818 };
2819
2820 /* According to the current ABI, should the type be passed in a
2821    floating-point register (assuming that there is space)?  When there
2822    is no FPU, FP are not even considered as possible candidates for
2823    FP registers and, consequently this returns false - forces FP
2824    arguments into integer registers.  */
2825
2826 static int
2827 fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
2828                    struct type *arg_type)
2829 {
2830   return ((typecode == TYPE_CODE_FLT
2831            || (MIPS_EABI (gdbarch)
2832                && (typecode == TYPE_CODE_STRUCT
2833                    || typecode == TYPE_CODE_UNION)
2834                && TYPE_NFIELDS (arg_type) == 1
2835                && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0))) 
2836                == TYPE_CODE_FLT))
2837           && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
2838 }
2839
2840 /* On o32, argument passing in GPRs depends on the alignment of the type being
2841    passed.  Return 1 if this type must be aligned to a doubleword boundary.  */
2842
2843 static int
2844 mips_type_needs_double_align (struct type *type)
2845 {
2846   enum type_code typecode = TYPE_CODE (type);
2847
2848   if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
2849     return 1;
2850   else if (typecode == TYPE_CODE_STRUCT)
2851     {
2852       if (TYPE_NFIELDS (type) < 1)
2853         return 0;
2854       return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
2855     }
2856   else if (typecode == TYPE_CODE_UNION)
2857     {
2858       int i, n;
2859
2860       n = TYPE_NFIELDS (type);
2861       for (i = 0; i < n; i++)
2862         if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
2863           return 1;
2864       return 0;
2865     }
2866   return 0;
2867 }
2868
2869 /* Adjust the address downward (direction of stack growth) so that it
2870    is correctly aligned for a new stack frame.  */
2871 static CORE_ADDR
2872 mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2873 {
2874   return align_down (addr, 16);
2875 }
2876
2877 static CORE_ADDR
2878 mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2879                            struct regcache *regcache, CORE_ADDR bp_addr,
2880                            int nargs, struct value **args, CORE_ADDR sp,
2881                            int struct_return, CORE_ADDR struct_addr)
2882 {
2883   int argreg;
2884   int float_argreg;
2885   int argnum;
2886   int len = 0;
2887   int stack_offset = 0;
2888   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2889   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2890   CORE_ADDR func_addr = find_function_addr (function, NULL);
2891   int regsize = mips_abi_regsize (gdbarch);
2892
2893   /* For shared libraries, "t9" needs to point at the function
2894      address.  */
2895   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
2896
2897   /* Set the return address register to point to the entry point of
2898      the program, where a breakpoint lies in wait.  */
2899   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
2900
2901   /* First ensure that the stack and structure return address (if any)
2902      are properly aligned.  The stack has to be at least 64-bit
2903      aligned even on 32-bit machines, because doubles must be 64-bit
2904      aligned.  For n32 and n64, stack frames need to be 128-bit
2905      aligned, so we round to this widest known alignment.  */
2906
2907   sp = align_down (sp, 16);
2908   struct_addr = align_down (struct_addr, 16);
2909
2910   /* Now make space on the stack for the args.  We allocate more
2911      than necessary for EABI, because the first few arguments are
2912      passed in registers, but that's OK.  */
2913   for (argnum = 0; argnum < nargs; argnum++)
2914     len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
2915   sp -= align_up (len, 16);
2916
2917   if (mips_debug)
2918     fprintf_unfiltered (gdb_stdlog,
2919                         "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
2920                         paddress (gdbarch, sp), (long) align_up (len, 16));
2921
2922   /* Initialize the integer and float register pointers.  */
2923   argreg = MIPS_A0_REGNUM;
2924   float_argreg = mips_fpa0_regnum (gdbarch);
2925
2926   /* The struct_return pointer occupies the first parameter-passing reg.  */
2927   if (struct_return)
2928     {
2929       if (mips_debug)
2930         fprintf_unfiltered (gdb_stdlog,
2931                             "mips_eabi_push_dummy_call: "
2932                             "struct_return reg=%d %s\n",
2933                             argreg, paddress (gdbarch, struct_addr));
2934       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
2935     }
2936
2937   /* Now load as many as possible of the first arguments into
2938      registers, and push the rest onto the stack.  Loop thru args
2939      from first to last.  */
2940   for (argnum = 0; argnum < nargs; argnum++)
2941     {
2942       const gdb_byte *val;
2943       gdb_byte valbuf[MAX_REGISTER_SIZE];
2944       struct value *arg = args[argnum];
2945       struct type *arg_type = check_typedef (value_type (arg));
2946       int len = TYPE_LENGTH (arg_type);
2947       enum type_code typecode = TYPE_CODE (arg_type);
2948
2949       if (mips_debug)
2950         fprintf_unfiltered (gdb_stdlog,
2951                             "mips_eabi_push_dummy_call: %d len=%d type=%d",
2952                             argnum + 1, len, (int) typecode);
2953
2954       /* Function pointer arguments to mips16 code need to be made into
2955          mips16 pointers.  */
2956       if (typecode == TYPE_CODE_PTR
2957           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
2958         {
2959           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
2960                                                    len, byte_order);
2961           if (mips_pc_is_mips16 (addr))
2962             {
2963               store_signed_integer (valbuf, len, byte_order, 
2964                                     make_mips16_addr (addr));
2965               val = valbuf;
2966             }
2967           else
2968             val = value_contents (arg);
2969         }
2970       /* The EABI passes structures that do not fit in a register by
2971          reference.  */
2972       else if (len > regsize
2973           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
2974         {
2975           store_unsigned_integer (valbuf, regsize, byte_order,
2976                                   value_address (arg));
2977           typecode = TYPE_CODE_PTR;
2978           len = regsize;
2979           val = valbuf;
2980           if (mips_debug)
2981             fprintf_unfiltered (gdb_stdlog, " push");
2982         }
2983       else
2984         val = value_contents (arg);
2985
2986       /* 32-bit ABIs always start floating point arguments in an
2987          even-numbered floating point register.  Round the FP register
2988          up before the check to see if there are any FP registers
2989          left.  Non MIPS_EABI targets also pass the FP in the integer
2990          registers so also round up normal registers.  */
2991       if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
2992         {
2993           if ((float_argreg & 1))
2994             float_argreg++;
2995         }
2996
2997       /* Floating point arguments passed in registers have to be
2998          treated specially.  On 32-bit architectures, doubles
2999          are passed in register pairs; the even register gets
3000          the low word, and the odd register gets the high word.
3001          On non-EABI processors, the first two floating point arguments are
3002          also copied to general registers, because MIPS16 functions
3003          don't use float registers for arguments.  This duplication of
3004          arguments in general registers can't hurt non-MIPS16 functions
3005          because those registers are normally skipped.  */
3006       /* MIPS_EABI squeezes a struct that contains a single floating
3007          point value into an FP register instead of pushing it onto the
3008          stack.  */
3009       if (fp_register_arg_p (gdbarch, typecode, arg_type)
3010           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
3011         {
3012           /* EABI32 will pass doubles in consecutive registers, even on
3013              64-bit cores.  At one time, we used to check the size of
3014              `float_argreg' to determine whether or not to pass doubles
3015              in consecutive registers, but this is not sufficient for
3016              making the ABI determination.  */
3017           if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
3018             {
3019               int low_offset = gdbarch_byte_order (gdbarch)
3020                                == BFD_ENDIAN_BIG ? 4 : 0;
3021               long regval;
3022
3023               /* Write the low word of the double to the even register(s).  */
3024               regval = extract_signed_integer (val + low_offset,
3025                                                4, byte_order);
3026               if (mips_debug)
3027                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3028                                     float_argreg, phex (regval, 4));
3029               regcache_cooked_write_signed (regcache, float_argreg++, regval);
3030
3031               /* Write the high word of the double to the odd register(s).  */
3032               regval = extract_signed_integer (val + 4 - low_offset,
3033                                                4, byte_order);
3034               if (mips_debug)
3035                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3036                                     float_argreg, phex (regval, 4));
3037               regcache_cooked_write_signed (regcache, float_argreg++, regval);
3038             }
3039           else
3040             {
3041               /* This is a floating point value that fits entirely
3042                  in a single register.  */
3043               /* On 32 bit ABI's the float_argreg is further adjusted
3044                  above to ensure that it is even register aligned.  */
3045               LONGEST regval = extract_signed_integer (val, len, byte_order);
3046               if (mips_debug)
3047                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3048                                     float_argreg, phex (regval, len));
3049               regcache_cooked_write_signed (regcache, float_argreg++, regval);
3050             }
3051         }
3052       else
3053         {
3054           /* Copy the argument to general registers or the stack in
3055              register-sized pieces.  Large arguments are split between
3056              registers and stack.  */
3057           /* Note: structs whose size is not a multiple of regsize
3058              are treated specially: Irix cc passes
3059              them in registers where gcc sometimes puts them on the
3060              stack.  For maximum compatibility, we will put them in
3061              both places.  */
3062           int odd_sized_struct = (len > regsize && len % regsize != 0);
3063
3064           /* Note: Floating-point values that didn't fit into an FP
3065              register are only written to memory.  */
3066           while (len > 0)
3067             {
3068               /* Remember if the argument was written to the stack.  */
3069               int stack_used_p = 0;
3070               int partial_len = (len < regsize ? len : regsize);
3071
3072               if (mips_debug)
3073                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3074                                     partial_len);
3075
3076               /* Write this portion of the argument to the stack.  */
3077               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
3078                   || odd_sized_struct
3079                   || fp_register_arg_p (gdbarch, typecode, arg_type))
3080                 {
3081                   /* Should shorter than int integer values be
3082                      promoted to int before being stored?  */
3083                   int longword_offset = 0;
3084                   CORE_ADDR addr;
3085                   stack_used_p = 1;
3086                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
3087                     {
3088                       if (regsize == 8
3089                           && (typecode == TYPE_CODE_INT
3090                               || typecode == TYPE_CODE_PTR
3091                               || typecode == TYPE_CODE_FLT) && len <= 4)
3092                         longword_offset = regsize - len;
3093                       else if ((typecode == TYPE_CODE_STRUCT
3094                                 || typecode == TYPE_CODE_UNION)
3095                                && TYPE_LENGTH (arg_type) < regsize)
3096                         longword_offset = regsize - len;
3097                     }
3098
3099                   if (mips_debug)
3100                     {
3101                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
3102                                           paddress (gdbarch, stack_offset));
3103                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
3104                                           paddress (gdbarch, longword_offset));
3105                     }
3106
3107                   addr = sp + stack_offset + longword_offset;
3108
3109                   if (mips_debug)
3110                     {
3111                       int i;
3112                       fprintf_unfiltered (gdb_stdlog, " @%s ",
3113                                           paddress (gdbarch, addr));
3114                       for (i = 0; i < partial_len; i++)
3115                         {
3116                           fprintf_unfiltered (gdb_stdlog, "%02x",
3117                                               val[i] & 0xff);
3118                         }
3119                     }
3120                   write_memory (addr, val, partial_len);
3121                 }
3122
3123               /* Note!!! This is NOT an else clause.  Odd sized
3124                  structs may go thru BOTH paths.  Floating point
3125                  arguments will not.  */
3126               /* Write this portion of the argument to a general
3127                  purpose register.  */
3128               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
3129                   && !fp_register_arg_p (gdbarch, typecode, arg_type))
3130                 {
3131                   LONGEST regval =
3132                     extract_signed_integer (val, partial_len, byte_order);
3133
3134                   if (mips_debug)
3135                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3136                                       argreg,
3137                                       phex (regval, regsize));
3138                   regcache_cooked_write_signed (regcache, argreg, regval);
3139                   argreg++;
3140                 }
3141
3142               len -= partial_len;
3143               val += partial_len;
3144
3145               /* Compute the offset into the stack at which we will
3146                  copy the next parameter.
3147
3148                  In the new EABI (and the NABI32), the stack_offset
3149                  only needs to be adjusted when it has been used.  */
3150
3151               if (stack_used_p)
3152                 stack_offset += align_up (partial_len, regsize);
3153             }
3154         }
3155       if (mips_debug)
3156         fprintf_unfiltered (gdb_stdlog, "\n");
3157     }
3158
3159   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
3160
3161   /* Return adjusted stack pointer.  */
3162   return sp;
3163 }
3164
3165 /* Determine the return value convention being used.  */
3166
3167 static enum return_value_convention
3168 mips_eabi_return_value (struct gdbarch *gdbarch, struct type *func_type,
3169                         struct type *type, struct regcache *regcache,
3170                         gdb_byte *readbuf, const gdb_byte *writebuf)
3171 {
3172   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3173   int fp_return_type = 0;
3174   int offset, regnum, xfer;
3175
3176   if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
3177     return RETURN_VALUE_STRUCT_CONVENTION;
3178
3179   /* Floating point type?  */
3180   if (tdep->mips_fpu_type != MIPS_FPU_NONE)
3181     {
3182       if (TYPE_CODE (type) == TYPE_CODE_FLT)
3183         fp_return_type = 1;
3184       /* Structs with a single field of float type 
3185          are returned in a floating point register.  */
3186       if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
3187            || TYPE_CODE (type) == TYPE_CODE_UNION)
3188           && TYPE_NFIELDS (type) == 1)
3189         {
3190           struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);
3191
3192           if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
3193             fp_return_type = 1;
3194         }
3195     }
3196
3197   if (fp_return_type)      
3198     {
3199       /* A floating-point value belongs in the least significant part
3200          of FP0/FP1.  */
3201       if (mips_debug)
3202         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
3203       regnum = mips_regnum (gdbarch)->fp0;
3204     }
3205   else 
3206     {
3207       /* An integer value goes in V0/V1.  */
3208       if (mips_debug)
3209         fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
3210       regnum = MIPS_V0_REGNUM;
3211     }
3212   for (offset = 0;
3213        offset < TYPE_LENGTH (type);
3214        offset += mips_abi_regsize (gdbarch), regnum++)
3215     {
3216       xfer = mips_abi_regsize (gdbarch);
3217       if (offset + xfer > TYPE_LENGTH (type))
3218         xfer = TYPE_LENGTH (type) - offset;
3219       mips_xfer_register (gdbarch, regcache,
3220                           gdbarch_num_regs (gdbarch) + regnum, xfer,
3221                           gdbarch_byte_order (gdbarch), readbuf, writebuf,
3222                           offset);
3223     }
3224
3225   return RETURN_VALUE_REGISTER_CONVENTION;
3226 }
3227
3228
3229 /* N32/N64 ABI stuff.  */
3230
3231 /* Search for a naturally aligned double at OFFSET inside a struct
3232    ARG_TYPE.  The N32 / N64 ABIs pass these in floating point
3233    registers.  */
3234
3235 static int
3236 mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
3237                             int offset)
3238 {
3239   int i;
3240
3241   if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
3242     return 0;
3243
3244   if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
3245     return 0;
3246
3247   if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
3248     return 0;
3249
3250   for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
3251     {
3252       int pos;
3253       struct type *field_type;
3254
3255       /* We're only looking at normal fields.  */
3256       if (field_is_static (&TYPE_FIELD (arg_type, i))
3257           || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
3258         continue;
3259
3260       /* If we have gone past the offset, there is no double to pass.  */
3261       pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
3262       if (pos > offset)
3263         return 0;
3264
3265       field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));
3266
3267       /* If this field is entirely before the requested offset, go
3268          on to the next one.  */
3269       if (pos + TYPE_LENGTH (field_type) <= offset)
3270         continue;
3271
3272       /* If this is our special aligned double, we can stop.  */
3273       if (TYPE_CODE (field_type) == TYPE_CODE_FLT
3274           && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
3275         return 1;
3276
3277       /* This field starts at or before the requested offset, and
3278          overlaps it.  If it is a structure, recurse inwards.  */
3279       return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
3280     }
3281
3282   return 0;
3283 }
3284
3285 static CORE_ADDR
3286 mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3287                              struct regcache *regcache, CORE_ADDR bp_addr,
3288                              int nargs, struct value **args, CORE_ADDR sp,
3289                              int struct_return, CORE_ADDR struct_addr)
3290 {
3291   int argreg;
3292   int float_argreg;
3293   int argnum;
3294   int len = 0;
3295   int stack_offset = 0;
3296   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3297   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3298   CORE_ADDR func_addr = find_function_addr (function, NULL);
3299
3300   /* For shared libraries, "t9" needs to point at the function
3301      address.  */
3302   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
3303
3304   /* Set the return address register to point to the entry point of
3305      the program, where a breakpoint lies in wait.  */
3306   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
3307
3308   /* First ensure that the stack and structure return address (if any)
3309      are properly aligned.  The stack has to be at least 64-bit
3310      aligned even on 32-bit machines, because doubles must be 64-bit
3311      aligned.  For n32 and n64, stack frames need to be 128-bit
3312      aligned, so we round to this widest known alignment.  */
3313
3314   sp = align_down (sp, 16);
3315   struct_addr = align_down (struct_addr, 16);
3316
3317   /* Now make space on the stack for the args.  */
3318   for (argnum = 0; argnum < nargs; argnum++)
3319     len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
3320   sp -= align_up (len, 16);
3321
3322   if (mips_debug)
3323     fprintf_unfiltered (gdb_stdlog,
3324                         "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
3325                         paddress (gdbarch, sp), (long) align_up (len, 16));
3326
3327   /* Initialize the integer and float register pointers.  */
3328   argreg = MIPS_A0_REGNUM;
3329   float_argreg = mips_fpa0_regnum (gdbarch);
3330
3331   /* The struct_return pointer occupies the first parameter-passing reg.  */
3332   if (struct_return)
3333     {
3334       if (mips_debug)
3335         fprintf_unfiltered (gdb_stdlog,
3336                             "mips_n32n64_push_dummy_call: "
3337                             "struct_return reg=%d %s\n",
3338                             argreg, paddress (gdbarch, struct_addr));
3339       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
3340     }
3341
3342   /* Now load as many as possible of the first arguments into
3343      registers, and push the rest onto the stack.  Loop thru args
3344      from first to last.  */
3345   for (argnum = 0; argnum < nargs; argnum++)
3346     {
3347       const gdb_byte *val;
3348       struct value *arg = args[argnum];
3349       struct type *arg_type = check_typedef (value_type (arg));
3350       int len = TYPE_LENGTH (arg_type);
3351       enum type_code typecode = TYPE_CODE (arg_type);
3352
3353       if (mips_debug)
3354         fprintf_unfiltered (gdb_stdlog,
3355                             "mips_n32n64_push_dummy_call: %d len=%d type=%d",
3356                             argnum + 1, len, (int) typecode);
3357
3358       val = value_contents (arg);
3359
3360       /* A 128-bit long double value requires an even-odd pair of
3361          floating-point registers.  */
3362       if (len == 16
3363           && fp_register_arg_p (gdbarch, typecode, arg_type)
3364           && (float_argreg & 1))
3365         {
3366           float_argreg++;
3367           argreg++;
3368         }
3369
3370       if (fp_register_arg_p (gdbarch, typecode, arg_type)
3371           && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
3372         {
3373           /* This is a floating point value that fits entirely
3374              in a single register or a pair of registers.  */
3375           int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
3376           LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
3377           if (mips_debug)
3378             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3379                                 float_argreg, phex (regval, reglen));
3380           regcache_cooked_write_unsigned (regcache, float_argreg, regval);
3381
3382           if (mips_debug)
3383             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3384                                 argreg, phex (regval, reglen));
3385           regcache_cooked_write_unsigned (regcache, argreg, regval);
3386           float_argreg++;
3387           argreg++;
3388           if (len == 16)
3389             {
3390               regval = extract_unsigned_integer (val + reglen,
3391                                                  reglen, byte_order);
3392               if (mips_debug)
3393                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3394                                     float_argreg, phex (regval, reglen));
3395               regcache_cooked_write_unsigned (regcache, float_argreg, regval);
3396
3397               if (mips_debug)
3398                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3399                                     argreg, phex (regval, reglen));
3400               regcache_cooked_write_unsigned (regcache, argreg, regval);
3401               float_argreg++;
3402               argreg++;
3403             }
3404         }
3405       else
3406         {
3407           /* Copy the argument to general registers or the stack in
3408              register-sized pieces.  Large arguments are split between
3409              registers and stack.  */
3410           /* For N32/N64, structs, unions, or other composite types are
3411              treated as a sequence of doublewords, and are passed in integer
3412              or floating point registers as though they were simple scalar
3413              parameters to the extent that they fit, with any excess on the
3414              stack packed according to the normal memory layout of the
3415              object.
3416              The caller does not reserve space for the register arguments;
3417              the callee is responsible for reserving it if required.  */
3418           /* Note: Floating-point values that didn't fit into an FP
3419              register are only written to memory.  */
3420           while (len > 0)
3421             {
3422               /* Remember if the argument was written to the stack.  */
3423               int stack_used_p = 0;
3424               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
3425
3426               if (mips_debug)
3427                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3428                                     partial_len);
3429
3430               if (fp_register_arg_p (gdbarch, typecode, arg_type))
3431                 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));
3432
3433               /* Write this portion of the argument to the stack.  */
3434               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
3435                 {
3436                   /* Should shorter than int integer values be
3437                      promoted to int before being stored?  */
3438                   int longword_offset = 0;
3439                   CORE_ADDR addr;
3440                   stack_used_p = 1;
3441                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
3442                     {
3443                       if ((typecode == TYPE_CODE_INT
3444                            || typecode == TYPE_CODE_PTR)
3445                           && len <= 4)
3446                         longword_offset = MIPS64_REGSIZE - len;
3447                     }
3448
3449                   if (mips_debug)
3450                     {
3451                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
3452                                           paddress (gdbarch, stack_offset));
3453                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
3454                                           paddress (gdbarch, longword_offset));
3455                     }
3456
3457                   addr = sp + stack_offset + longword_offset;
3458
3459                   if (mips_debug)
3460                     {
3461                       int i;
3462                       fprintf_unfiltered (gdb_stdlog, " @%s ",
3463                                           paddress (gdbarch, addr));
3464                       for (i = 0; i < partial_len; i++)
3465                         {
3466                           fprintf_unfiltered (gdb_stdlog, "%02x",
3467                                               val[i] & 0xff);
3468                         }
3469                     }
3470                   write_memory (addr, val, partial_len);
3471                 }
3472
3473               /* Note!!! This is NOT an else clause.  Odd sized
3474                  structs may go thru BOTH paths.  */
3475               /* Write this portion of the argument to a general
3476                  purpose register.  */
3477               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
3478                 {
3479                   LONGEST regval;
3480
3481                   /* Sign extend pointers, 32-bit integers and signed
3482                      16-bit and 8-bit integers; everything else is taken
3483                      as is.  */
3484
3485                   if ((partial_len == 4
3486                        && (typecode == TYPE_CODE_PTR
3487                            || typecode == TYPE_CODE_INT))
3488                       || (partial_len < 4
3489                           && typecode == TYPE_CODE_INT
3490                           && !TYPE_UNSIGNED (arg_type)))
3491                     regval = extract_signed_integer (val, partial_len,
3492                                                      byte_order);
3493                   else
3494                     regval = extract_unsigned_integer (val, partial_len,
3495                                                        byte_order);
3496
3497                   /* A non-floating-point argument being passed in a
3498                      general register.  If a struct or union, and if
3499                      the remaining length is smaller than the register
3500                      size, we have to adjust the register value on
3501                      big endian targets.
3502
3503                      It does not seem to be necessary to do the
3504                      same for integral types.  */
3505
3506                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
3507                       && partial_len < MIPS64_REGSIZE
3508                       && (typecode == TYPE_CODE_STRUCT
3509                           || typecode == TYPE_CODE_UNION))
3510                     regval <<= ((MIPS64_REGSIZE - partial_len)
3511                                 * TARGET_CHAR_BIT);
3512
3513                   if (mips_debug)
3514                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3515                                       argreg,
3516                                       phex (regval, MIPS64_REGSIZE));
3517                   regcache_cooked_write_unsigned (regcache, argreg, regval);
3518
3519                   if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
3520                                                   TYPE_LENGTH (arg_type) - len))
3521                     {
3522                       if (mips_debug)
3523                         fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
3524                                           float_argreg,
3525                                           phex (regval, MIPS64_REGSIZE));
3526                       regcache_cooked_write_unsigned (regcache, float_argreg,
3527                                                       regval);
3528                     }
3529
3530                   float_argreg++;
3531                   argreg++;
3532                 }
3533
3534               len -= partial_len;
3535               val += partial_len;
3536
3537               /* Compute the offset into the stack at which we will
3538                  copy the next parameter.
3539
3540                  In N32 (N64?), the stack_offset only needs to be
3541                  adjusted when it has been used.  */
3542
3543               if (stack_used_p)
3544                 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
3545             }
3546         }
3547       if (mips_debug)
3548         fprintf_unfiltered (gdb_stdlog, "\n");
3549     }
3550
3551   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
3552
3553   /* Return adjusted stack pointer.  */
3554   return sp;
3555 }
3556
3557 static enum return_value_convention
3558 mips_n32n64_return_value (struct gdbarch *gdbarch, struct type *func_type,
3559                           struct type *type, struct regcache *regcache,
3560                           gdb_byte *readbuf, const gdb_byte *writebuf)
3561 {
3562   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3563
3564   /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004
3565
3566      Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
3567      if needed), as appropriate for the type.  Composite results (struct,
3568      union, or array) are returned in $2/$f0 and $3/$f2 according to the
3569      following rules:
3570
3571      * A struct with only one or two floating point fields is returned in $f0
3572      (and $f2 if necessary).  This is a generalization of the Fortran COMPLEX
3573      case.
3574
3575      * Any other composite results of at most 128 bits are returned in
3576      $2 (first 64 bits) and $3 (remainder, if necessary).
3577
3578      * Larger composite results are handled by converting the function to a
3579      procedure with an implicit first parameter, which is a pointer to an area
3580      reserved by the caller to receive the result.  [The o32-bit ABI requires
3581      that all composite results be handled by conversion to implicit first
3582      parameters.  The MIPS/SGI Fortran implementation has always made a
3583      specific exception to return COMPLEX results in the floating point
3584      registers.]  */
3585
3586   if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
3587     return RETURN_VALUE_STRUCT_CONVENTION;
3588   else if (TYPE_CODE (type) == TYPE_CODE_FLT
3589            && TYPE_LENGTH (type) == 16
3590            && tdep->mips_fpu_type != MIPS_FPU_NONE)
3591     {
3592       /* A 128-bit floating-point value fills both $f0 and $f2.  The
3593          two registers are used in the same as memory order, so the
3594          eight bytes with the lower memory address are in $f0.  */
3595       if (mips_debug)
3596         fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
3597       mips_xfer_register (gdbarch, regcache,
3598                           gdbarch_num_regs (gdbarch)
3599                           + mips_regnum (gdbarch)->fp0,
3600                           8, gdbarch_byte_order (gdbarch),
3601                           readbuf, writebuf, 0);
3602       mips_xfer_register (gdbarch, regcache,
3603                           gdbarch_num_regs (gdbarch)
3604                           + mips_regnum (gdbarch)->fp0 + 2,
3605                           8, gdbarch_byte_order (gdbarch),
3606                           readbuf ? readbuf + 8 : readbuf,
3607                           writebuf ? writebuf + 8 : writebuf, 0);
3608       return RETURN_VALUE_REGISTER_CONVENTION;
3609     }
3610   else if (TYPE_CODE (type) == TYPE_CODE_FLT
3611            && tdep->mips_fpu_type != MIPS_FPU_NONE)
3612     {
3613       /* A single or double floating-point value that fits in FP0.  */
3614       if (mips_debug)
3615         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
3616       mips_xfer_register (gdbarch, regcache,
3617                           gdbarch_num_regs (gdbarch)
3618                           + mips_regnum (gdbarch)->fp0,
3619                           TYPE_LENGTH (type),
3620                           gdbarch_byte_order (gdbarch),
3621                           readbuf, writebuf, 0);
3622       return RETURN_VALUE_REGISTER_CONVENTION;
3623     }
3624   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3625            && TYPE_NFIELDS (type) <= 2
3626            && TYPE_NFIELDS (type) >= 1
3627            && ((TYPE_NFIELDS (type) == 1
3628                 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
3629                     == TYPE_CODE_FLT))
3630                || (TYPE_NFIELDS (type) == 2
3631                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
3632                        == TYPE_CODE_FLT)
3633                    && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
3634                        == TYPE_CODE_FLT))))
3635     {
3636       /* A struct that contains one or two floats.  Each value is part
3637          in the least significant part of their floating point
3638          register (or GPR, for soft float).  */
3639       int regnum;
3640       int field;
3641       for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
3642                                 ? mips_regnum (gdbarch)->fp0
3643                                 : MIPS_V0_REGNUM);
3644            field < TYPE_NFIELDS (type); field++, regnum += 2)
3645         {
3646           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
3647                         / TARGET_CHAR_BIT);
3648           if (mips_debug)
3649             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
3650                                 offset);
3651           if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
3652             {
3653               /* A 16-byte long double field goes in two consecutive
3654                  registers.  */
3655               mips_xfer_register (gdbarch, regcache,
3656                                   gdbarch_num_regs (gdbarch) + regnum,
3657                                   8,
3658                                   gdbarch_byte_order (gdbarch),
3659                                   readbuf, writebuf, offset);
3660               mips_xfer_register (gdbarch, regcache,
3661                                   gdbarch_num_regs (gdbarch) + regnum + 1,
3662                                   8,
3663                                   gdbarch_byte_order (gdbarch),
3664                                   readbuf, writebuf, offset + 8);
3665             }
3666           else
3667             mips_xfer_register (gdbarch, regcache,
3668                                 gdbarch_num_regs (gdbarch) + regnum,
3669                                 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
3670                                 gdbarch_byte_order (gdbarch),
3671                                 readbuf, writebuf, offset);
3672         }
3673       return RETURN_VALUE_REGISTER_CONVENTION;
3674     }
3675   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3676            || TYPE_CODE (type) == TYPE_CODE_UNION
3677            || TYPE_CODE (type) == TYPE_CODE_ARRAY)
3678     {
3679       /* A composite type.  Extract the left justified value,
3680          regardless of the byte order.  I.e. DO NOT USE
3681          mips_xfer_lower.  */
3682       int offset;
3683       int regnum;
3684       for (offset = 0, regnum = MIPS_V0_REGNUM;
3685            offset < TYPE_LENGTH (type);
3686            offset += register_size (gdbarch, regnum), regnum++)
3687         {
3688           int xfer = register_size (gdbarch, regnum);
3689           if (offset + xfer > TYPE_LENGTH (type))
3690             xfer = TYPE_LENGTH (type) - offset;
3691           if (mips_debug)
3692             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
3693                                 offset, xfer, regnum);
3694           mips_xfer_register (gdbarch, regcache,
3695                               gdbarch_num_regs (gdbarch) + regnum,
3696                               xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
3697                               offset);
3698         }
3699       return RETURN_VALUE_REGISTER_CONVENTION;
3700     }
3701   else
3702     {
3703       /* A scalar extract each part but least-significant-byte
3704          justified.  */
3705       int offset;
3706       int regnum;
3707       for (offset = 0, regnum = MIPS_V0_REGNUM;
3708            offset < TYPE_LENGTH (type);
3709            offset += register_size (gdbarch, regnum), regnum++)
3710         {
3711           int xfer = register_size (gdbarch, regnum);
3712           if (offset + xfer > TYPE_LENGTH (type))
3713             xfer = TYPE_LENGTH (type) - offset;
3714           if (mips_debug)
3715             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
3716                                 offset, xfer, regnum);
3717           mips_xfer_register (gdbarch, regcache,
3718                               gdbarch_num_regs (gdbarch) + regnum,
3719                               xfer, gdbarch_byte_order (gdbarch),
3720                               readbuf, writebuf, offset);
3721         }
3722       return RETURN_VALUE_REGISTER_CONVENTION;
3723     }
3724 }
3725
3726 /* O32 ABI stuff.  */
3727
3728 static CORE_ADDR
3729 mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3730                           struct regcache *regcache, CORE_ADDR bp_addr,
3731                           int nargs, struct value **args, CORE_ADDR sp,
3732                           int struct_return, CORE_ADDR struct_addr)
3733 {
3734   int argreg;
3735   int float_argreg;
3736   int argnum;
3737   int len = 0;
3738   int stack_offset = 0;
3739   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3740   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3741   CORE_ADDR func_addr = find_function_addr (function, NULL);
3742
3743   /* For shared libraries, "t9" needs to point at the function
3744      address.  */
3745   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
3746
3747   /* Set the return address register to point to the entry point of
3748      the program, where a breakpoint lies in wait.  */
3749   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
3750
3751   /* First ensure that the stack and structure return address (if any)
3752      are properly aligned.  The stack has to be at least 64-bit
3753      aligned even on 32-bit machines, because doubles must be 64-bit
3754      aligned.  For n32 and n64, stack frames need to be 128-bit
3755      aligned, so we round to this widest known alignment.  */
3756
3757   sp = align_down (sp, 16);
3758   struct_addr = align_down (struct_addr, 16);
3759
3760   /* Now make space on the stack for the args.  */
3761   for (argnum = 0; argnum < nargs; argnum++)
3762     {
3763       struct type *arg_type = check_typedef (value_type (args[argnum]));
3764       int arglen = TYPE_LENGTH (arg_type);
3765
3766       /* Align to double-word if necessary.  */
3767       if (mips_type_needs_double_align (arg_type))
3768         len = align_up (len, MIPS32_REGSIZE * 2);
3769       /* Allocate space on the stack.  */
3770       len += align_up (arglen, MIPS32_REGSIZE);
3771     }
3772   sp -= align_up (len, 16);
3773
3774   if (mips_debug)
3775     fprintf_unfiltered (gdb_stdlog,
3776                         "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
3777                         paddress (gdbarch, sp), (long) align_up (len, 16));
3778
3779   /* Initialize the integer and float register pointers.  */
3780   argreg = MIPS_A0_REGNUM;
3781   float_argreg = mips_fpa0_regnum (gdbarch);
3782
3783   /* The struct_return pointer occupies the first parameter-passing reg.  */
3784   if (struct_return)
3785     {
3786       if (mips_debug)
3787         fprintf_unfiltered (gdb_stdlog,
3788                             "mips_o32_push_dummy_call: "
3789                             "struct_return reg=%d %s\n",
3790                             argreg, paddress (gdbarch, struct_addr));
3791       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
3792       stack_offset += MIPS32_REGSIZE;
3793     }
3794
3795   /* Now load as many as possible of the first arguments into
3796      registers, and push the rest onto the stack.  Loop thru args
3797      from first to last.  */
3798   for (argnum = 0; argnum < nargs; argnum++)
3799     {
3800       const gdb_byte *val;
3801       struct value *arg = args[argnum];
3802       struct type *arg_type = check_typedef (value_type (arg));
3803       int len = TYPE_LENGTH (arg_type);
3804       enum type_code typecode = TYPE_CODE (arg_type);
3805
3806       if (mips_debug)
3807         fprintf_unfiltered (gdb_stdlog,
3808                             "mips_o32_push_dummy_call: %d len=%d type=%d",
3809                             argnum + 1, len, (int) typecode);
3810
3811       val = value_contents (arg);
3812
3813       /* 32-bit ABIs always start floating point arguments in an
3814          even-numbered floating point register.  Round the FP register
3815          up before the check to see if there are any FP registers
3816          left.  O32/O64 targets also pass the FP in the integer
3817          registers so also round up normal registers.  */
3818       if (fp_register_arg_p (gdbarch, typecode, arg_type))
3819         {
3820           if ((float_argreg & 1))
3821             float_argreg++;
3822         }
3823
3824       /* Floating point arguments passed in registers have to be
3825          treated specially.  On 32-bit architectures, doubles
3826          are passed in register pairs; the even register gets
3827          the low word, and the odd register gets the high word.
3828          On O32/O64, the first two floating point arguments are
3829          also copied to general registers, because MIPS16 functions
3830          don't use float registers for arguments.  This duplication of
3831          arguments in general registers can't hurt non-MIPS16 functions
3832          because those registers are normally skipped.  */
3833
3834       if (fp_register_arg_p (gdbarch, typecode, arg_type)
3835           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
3836         {
3837           if (register_size (gdbarch, float_argreg) < 8 && len == 8)
3838             {
3839               int low_offset = gdbarch_byte_order (gdbarch)
3840                                == BFD_ENDIAN_BIG ? 4 : 0;
3841               unsigned long regval;
3842
3843               /* Write the low word of the double to the even register(s).  */
3844               regval = extract_unsigned_integer (val + low_offset,
3845                                                  4, byte_order);
3846               if (mips_debug)
3847                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3848                                     float_argreg, phex (regval, 4));
3849               regcache_cooked_write_unsigned (regcache,
3850                                               float_argreg++, regval);
3851               if (mips_debug)
3852                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3853                                     argreg, phex (regval, 4));
3854               regcache_cooked_write_unsigned (regcache, argreg++, regval);
3855
3856               /* Write the high word of the double to the odd register(s).  */
3857               regval = extract_unsigned_integer (val + 4 - low_offset,
3858                                                  4, byte_order);
3859               if (mips_debug)
3860                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3861                                     float_argreg, phex (regval, 4));
3862               regcache_cooked_write_unsigned (regcache,
3863                                               float_argreg++, regval);
3864
3865               if (mips_debug)
3866                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3867                                     argreg, phex (regval, 4));
3868               regcache_cooked_write_unsigned (regcache, argreg++, regval);
3869             }
3870           else
3871             {
3872               /* This is a floating point value that fits entirely
3873                  in a single register.  */
3874               /* On 32 bit ABI's the float_argreg is further adjusted
3875                  above to ensure that it is even register aligned.  */
3876               LONGEST regval = extract_unsigned_integer (val, len, byte_order);
3877               if (mips_debug)
3878                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3879                                     float_argreg, phex (regval, len));
3880               regcache_cooked_write_unsigned (regcache,
3881                                               float_argreg++, regval);
3882               /* Although two FP registers are reserved for each
3883                  argument, only one corresponding integer register is
3884                  reserved.  */
3885               if (mips_debug)
3886                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3887                                     argreg, phex (regval, len));
3888               regcache_cooked_write_unsigned (regcache, argreg++, regval);
3889             }
3890           /* Reserve space for the FP register.  */
3891           stack_offset += align_up (len, MIPS32_REGSIZE);
3892         }
3893       else
3894         {
3895           /* Copy the argument to general registers or the stack in
3896              register-sized pieces.  Large arguments are split between
3897              registers and stack.  */
3898           /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
3899              are treated specially: Irix cc passes
3900              them in registers where gcc sometimes puts them on the
3901              stack.  For maximum compatibility, we will put them in
3902              both places.  */
3903           int odd_sized_struct = (len > MIPS32_REGSIZE
3904                                   && len % MIPS32_REGSIZE != 0);
3905           /* Structures should be aligned to eight bytes (even arg registers)
3906              on MIPS_ABI_O32, if their first member has double precision.  */
3907           if (mips_type_needs_double_align (arg_type))
3908             {
3909               if ((argreg & 1))
3910                 {
3911                   argreg++;
3912                   stack_offset += MIPS32_REGSIZE;
3913                 }
3914             }
3915           while (len > 0)
3916             {
3917               /* Remember if the argument was written to the stack.  */
3918               int stack_used_p = 0;
3919               int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);
3920
3921               if (mips_debug)
3922                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3923                                     partial_len);
3924
3925               /* Write this portion of the argument to the stack.  */
3926               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
3927                   || odd_sized_struct)
3928                 {
3929                   /* Should shorter than int integer values be
3930                      promoted to int before being stored?  */
3931                   int longword_offset = 0;
3932                   CORE_ADDR addr;
3933                   stack_used_p = 1;
3934
3935                   if (mips_debug)
3936                     {
3937                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
3938                                           paddress (gdbarch, stack_offset));
3939                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
3940                                           paddress (gdbarch, longword_offset));
3941                     }
3942
3943                   addr = sp + stack_offset + longword_offset;
3944
3945                   if (mips_debug)
3946                     {
3947                       int i;
3948                       fprintf_unfiltered (gdb_stdlog, " @%s ",
3949                                           paddress (gdbarch, addr));
3950                       for (i = 0; i < partial_len; i++)
3951                         {
3952                           fprintf_unfiltered (gdb_stdlog, "%02x",
3953                                               val[i] & 0xff);
3954                         }
3955                     }
3956                   write_memory (addr, val, partial_len);
3957                 }
3958
3959               /* Note!!! This is NOT an else clause.  Odd sized
3960                  structs may go thru BOTH paths.  */
3961               /* Write this portion of the argument to a general
3962                  purpose register.  */
3963               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
3964                 {
3965                   LONGEST regval = extract_signed_integer (val, partial_len,
3966                                                            byte_order);
3967                   /* Value may need to be sign extended, because
3968                      mips_isa_regsize() != mips_abi_regsize().  */
3969
3970                   /* A non-floating-point argument being passed in a
3971                      general register.  If a struct or union, and if
3972                      the remaining length is smaller than the register
3973                      size, we have to adjust the register value on
3974                      big endian targets.
3975
3976                      It does not seem to be necessary to do the
3977                      same for integral types.
3978
3979                      Also don't do this adjustment on O64 binaries.
3980
3981                      cagney/2001-07-23: gdb/179: Also, GCC, when
3982                      outputting LE O32 with sizeof (struct) <
3983                      mips_abi_regsize(), generates a left shift
3984                      as part of storing the argument in a register
3985                      (the left shift isn't generated when
3986                      sizeof (struct) >= mips_abi_regsize()).  Since
3987                      it is quite possible that this is GCC
3988                      contradicting the LE/O32 ABI, GDB has not been
3989                      adjusted to accommodate this.  Either someone
3990                      needs to demonstrate that the LE/O32 ABI
3991                      specifies such a left shift OR this new ABI gets
3992                      identified as such and GDB gets tweaked
3993                      accordingly.  */
3994
3995                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
3996                       && partial_len < MIPS32_REGSIZE
3997                       && (typecode == TYPE_CODE_STRUCT
3998                           || typecode == TYPE_CODE_UNION))
3999                     regval <<= ((MIPS32_REGSIZE - partial_len)
4000                                 * TARGET_CHAR_BIT);
4001
4002                   if (mips_debug)
4003                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4004                                       argreg,
4005                                       phex (regval, MIPS32_REGSIZE));
4006                   regcache_cooked_write_unsigned (regcache, argreg, regval);
4007                   argreg++;
4008
4009                   /* Prevent subsequent floating point arguments from
4010                      being passed in floating point registers.  */
4011                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
4012                 }
4013
4014               len -= partial_len;
4015               val += partial_len;
4016
4017               /* Compute the offset into the stack at which we will
4018                  copy the next parameter.
4019
4020                  In older ABIs, the caller reserved space for
4021                  registers that contained arguments.  This was loosely
4022                  refered to as their "home".  Consequently, space is
4023                  always allocated.  */
4024
4025               stack_offset += align_up (partial_len, MIPS32_REGSIZE);
4026             }
4027         }
4028       if (mips_debug)
4029         fprintf_unfiltered (gdb_stdlog, "\n");
4030     }
4031
4032   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
4033
4034   /* Return adjusted stack pointer.  */
4035   return sp;
4036 }
4037
4038 static enum return_value_convention
4039 mips_o32_return_value (struct gdbarch *gdbarch, struct type *func_type,
4040                        struct type *type, struct regcache *regcache,
4041                        gdb_byte *readbuf, const gdb_byte *writebuf)
4042 {
4043   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4044
4045   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4046       || TYPE_CODE (type) == TYPE_CODE_UNION
4047       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
4048     return RETURN_VALUE_STRUCT_CONVENTION;
4049   else if (TYPE_CODE (type) == TYPE_CODE_FLT
4050            && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
4051     {
4052       /* A single-precision floating-point value.  It fits in the
4053          least significant part of FP0.  */
4054       if (mips_debug)
4055         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4056       mips_xfer_register (gdbarch, regcache,
4057                           gdbarch_num_regs (gdbarch)
4058                             + mips_regnum (gdbarch)->fp0,
4059                           TYPE_LENGTH (type),
4060                           gdbarch_byte_order (gdbarch),
4061                           readbuf, writebuf, 0);
4062       return RETURN_VALUE_REGISTER_CONVENTION;
4063     }
4064   else if (TYPE_CODE (type) == TYPE_CODE_FLT
4065            && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
4066     {
4067       /* A double-precision floating-point value.  The most
4068          significant part goes in FP1, and the least significant in
4069          FP0.  */
4070       if (mips_debug)
4071         fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
4072       switch (gdbarch_byte_order (gdbarch))
4073         {
4074         case BFD_ENDIAN_LITTLE:
4075           mips_xfer_register (gdbarch, regcache,
4076                               gdbarch_num_regs (gdbarch)
4077                                 + mips_regnum (gdbarch)->fp0 +
4078                               0, 4, gdbarch_byte_order (gdbarch),
4079                               readbuf, writebuf, 0);
4080           mips_xfer_register (gdbarch, regcache,
4081                               gdbarch_num_regs (gdbarch)
4082                                 + mips_regnum (gdbarch)->fp0 + 1,
4083                               4, gdbarch_byte_order (gdbarch),
4084                               readbuf, writebuf, 4);
4085           break;
4086         case BFD_ENDIAN_BIG:
4087           mips_xfer_register (gdbarch, regcache,
4088                               gdbarch_num_regs (gdbarch)
4089                                 + mips_regnum (gdbarch)->fp0 + 1,
4090                               4, gdbarch_byte_order (gdbarch),
4091                               readbuf, writebuf, 0);
4092           mips_xfer_register (gdbarch, regcache,
4093                               gdbarch_num_regs (gdbarch)
4094                                 + mips_regnum (gdbarch)->fp0 + 0,
4095                               4, gdbarch_byte_order (gdbarch),
4096                               readbuf, writebuf, 4);
4097           break;
4098         default:
4099           internal_error (__FILE__, __LINE__, _("bad switch"));
4100         }
4101       return RETURN_VALUE_REGISTER_CONVENTION;
4102     }
4103 #if 0
4104   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4105            && TYPE_NFIELDS (type) <= 2
4106            && TYPE_NFIELDS (type) >= 1
4107            && ((TYPE_NFIELDS (type) == 1
4108                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4109                     == TYPE_CODE_FLT))
4110                || (TYPE_NFIELDS (type) == 2
4111                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4112                        == TYPE_CODE_FLT)
4113                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
4114                        == TYPE_CODE_FLT)))
4115            && tdep->mips_fpu_type != MIPS_FPU_NONE)
4116     {
4117       /* A struct that contains one or two floats.  Each value is part
4118          in the least significant part of their floating point
4119          register..  */
4120       gdb_byte reg[MAX_REGISTER_SIZE];
4121       int regnum;
4122       int field;
4123       for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
4124            field < TYPE_NFIELDS (type); field++, regnum += 2)
4125         {
4126           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
4127                         / TARGET_CHAR_BIT);
4128           if (mips_debug)
4129             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
4130                                 offset);
4131           mips_xfer_register (gdbarch, regcache,
4132                               gdbarch_num_regs (gdbarch) + regnum,
4133                               TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
4134                               gdbarch_byte_order (gdbarch),
4135                               readbuf, writebuf, offset);
4136         }
4137       return RETURN_VALUE_REGISTER_CONVENTION;
4138     }
4139 #endif
4140 #if 0
4141   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4142            || TYPE_CODE (type) == TYPE_CODE_UNION)
4143     {
4144       /* A structure or union.  Extract the left justified value,
4145          regardless of the byte order.  I.e. DO NOT USE
4146          mips_xfer_lower.  */
4147       int offset;
4148       int regnum;
4149       for (offset = 0, regnum = MIPS_V0_REGNUM;
4150            offset < TYPE_LENGTH (type);
4151            offset += register_size (gdbarch, regnum), regnum++)
4152         {
4153           int xfer = register_size (gdbarch, regnum);
4154           if (offset + xfer > TYPE_LENGTH (type))
4155             xfer = TYPE_LENGTH (type) - offset;
4156           if (mips_debug)
4157             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
4158                                 offset, xfer, regnum);
4159           mips_xfer_register (gdbarch, regcache,
4160                               gdbarch_num_regs (gdbarch) + regnum, xfer,
4161                               BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
4162         }
4163       return RETURN_VALUE_REGISTER_CONVENTION;
4164     }
4165 #endif
4166   else
4167     {
4168       /* A scalar extract each part but least-significant-byte
4169          justified.  o32 thinks registers are 4 byte, regardless of
4170          the ISA.  */
4171       int offset;
4172       int regnum;
4173       for (offset = 0, regnum = MIPS_V0_REGNUM;
4174            offset < TYPE_LENGTH (type);
4175            offset += MIPS32_REGSIZE, regnum++)
4176         {
4177           int xfer = MIPS32_REGSIZE;
4178           if (offset + xfer > TYPE_LENGTH (type))
4179             xfer = TYPE_LENGTH (type) - offset;
4180           if (mips_debug)
4181             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4182                                 offset, xfer, regnum);
4183           mips_xfer_register (gdbarch, regcache,
4184                               gdbarch_num_regs (gdbarch) + regnum, xfer,
4185                               gdbarch_byte_order (gdbarch),
4186                               readbuf, writebuf, offset);
4187         }
4188       return RETURN_VALUE_REGISTER_CONVENTION;
4189     }
4190 }
4191
4192 /* O64 ABI.  This is a hacked up kind of 64-bit version of the o32
4193    ABI.  */
4194
4195 static CORE_ADDR
4196 mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4197                           struct regcache *regcache, CORE_ADDR bp_addr,
4198                           int nargs,
4199                           struct value **args, CORE_ADDR sp,
4200                           int struct_return, CORE_ADDR struct_addr)
4201 {
4202   int argreg;
4203   int float_argreg;
4204   int argnum;
4205   int len = 0;
4206   int stack_offset = 0;
4207   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4208   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4209   CORE_ADDR func_addr = find_function_addr (function, NULL);
4210
4211   /* For shared libraries, "t9" needs to point at the function
4212      address.  */
4213   regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
4214
4215   /* Set the return address register to point to the entry point of
4216      the program, where a breakpoint lies in wait.  */
4217   regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
4218
4219   /* First ensure that the stack and structure return address (if any)
4220      are properly aligned.  The stack has to be at least 64-bit
4221      aligned even on 32-bit machines, because doubles must be 64-bit
4222      aligned.  For n32 and n64, stack frames need to be 128-bit
4223      aligned, so we round to this widest known alignment.  */
4224
4225   sp = align_down (sp, 16);
4226   struct_addr = align_down (struct_addr, 16);
4227
4228   /* Now make space on the stack for the args.  */
4229   for (argnum = 0; argnum < nargs; argnum++)
4230     {
4231       struct type *arg_type = check_typedef (value_type (args[argnum]));
4232       int arglen = TYPE_LENGTH (arg_type);
4233
4234       /* Allocate space on the stack.  */
4235       len += align_up (arglen, MIPS64_REGSIZE);
4236     }
4237   sp -= align_up (len, 16);
4238
4239   if (mips_debug)
4240     fprintf_unfiltered (gdb_stdlog,
4241                         "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
4242                         paddress (gdbarch, sp), (long) align_up (len, 16));
4243
4244   /* Initialize the integer and float register pointers.  */
4245   argreg = MIPS_A0_REGNUM;
4246   float_argreg = mips_fpa0_regnum (gdbarch);
4247
4248   /* The struct_return pointer occupies the first parameter-passing reg.  */
4249   if (struct_return)
4250     {
4251       if (mips_debug)
4252         fprintf_unfiltered (gdb_stdlog,
4253                             "mips_o64_push_dummy_call: "
4254                             "struct_return reg=%d %s\n",
4255                             argreg, paddress (gdbarch, struct_addr));
4256       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
4257       stack_offset += MIPS64_REGSIZE;
4258     }
4259
4260   /* Now load as many as possible of the first arguments into
4261      registers, and push the rest onto the stack.  Loop thru args
4262      from first to last.  */
4263   for (argnum = 0; argnum < nargs; argnum++)
4264     {
4265       const gdb_byte *val;
4266       gdb_byte valbuf[MAX_REGISTER_SIZE];
4267       struct value *arg = args[argnum];
4268       struct type *arg_type = check_typedef (value_type (arg));
4269       int len = TYPE_LENGTH (arg_type);
4270       enum type_code typecode = TYPE_CODE (arg_type);
4271
4272       if (mips_debug)
4273         fprintf_unfiltered (gdb_stdlog,
4274                             "mips_o64_push_dummy_call: %d len=%d type=%d",
4275                             argnum + 1, len, (int) typecode);
4276
4277       val = value_contents (arg);
4278
4279       /* Function pointer arguments to mips16 code need to be made into
4280          mips16 pointers.  */
4281       if (typecode == TYPE_CODE_PTR
4282           && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
4283         {
4284           CORE_ADDR addr = extract_signed_integer (value_contents (arg),
4285                                                    len, byte_order);
4286           if (mips_pc_is_mips16 (addr))
4287             {
4288               store_signed_integer (valbuf, len, byte_order, 
4289                                     make_mips16_addr (addr));
4290               val = valbuf;
4291             }
4292         }
4293
4294       /* Floating point arguments passed in registers have to be
4295          treated specially.  On 32-bit architectures, doubles
4296          are passed in register pairs; the even register gets
4297          the low word, and the odd register gets the high word.
4298          On O32/O64, the first two floating point arguments are
4299          also copied to general registers, because MIPS16 functions
4300          don't use float registers for arguments.  This duplication of
4301          arguments in general registers can't hurt non-MIPS16 functions
4302          because those registers are normally skipped.  */
4303
4304       if (fp_register_arg_p (gdbarch, typecode, arg_type)
4305           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
4306         {
4307           LONGEST regval = extract_unsigned_integer (val, len, byte_order);
4308           if (mips_debug)
4309             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4310                                 float_argreg, phex (regval, len));
4311           regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
4312           if (mips_debug)
4313             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4314                                 argreg, phex (regval, len));
4315           regcache_cooked_write_unsigned (regcache, argreg, regval);
4316           argreg++;
4317           /* Reserve space for the FP register.  */
4318           stack_offset += align_up (len, MIPS64_REGSIZE);
4319         }
4320       else
4321         {
4322           /* Copy the argument to general registers or the stack in
4323              register-sized pieces.  Large arguments are split between
4324              registers and stack.  */
4325           /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
4326              are treated specially: Irix cc passes them in registers
4327              where gcc sometimes puts them on the stack.  For maximum
4328              compatibility, we will put them in both places.  */
4329           int odd_sized_struct = (len > MIPS64_REGSIZE
4330                                   && len % MIPS64_REGSIZE != 0);
4331           while (len > 0)
4332             {
4333               /* Remember if the argument was written to the stack.  */
4334               int stack_used_p = 0;
4335               int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
4336
4337               if (mips_debug)
4338                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4339                                     partial_len);
4340
4341               /* Write this portion of the argument to the stack.  */
4342               if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
4343                   || odd_sized_struct)
4344                 {
4345                   /* Should shorter than int integer values be
4346                      promoted to int before being stored?  */
4347                   int longword_offset = 0;
4348                   CORE_ADDR addr;
4349                   stack_used_p = 1;
4350                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4351                     {
4352                       if ((typecode == TYPE_CODE_INT
4353                            || typecode == TYPE_CODE_PTR
4354                            || typecode == TYPE_CODE_FLT)
4355                           && len <= 4)
4356                         longword_offset = MIPS64_REGSIZE - len;
4357                     }
4358
4359                   if (mips_debug)
4360                     {
4361                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4362                                           paddress (gdbarch, stack_offset));
4363                       fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4364                                           paddress (gdbarch, longword_offset));
4365                     }
4366
4367                   addr = sp + stack_offset + longword_offset;
4368
4369                   if (mips_debug)
4370                     {
4371                       int i;
4372                       fprintf_unfiltered (gdb_stdlog, " @%s ",
4373                                           paddress (gdbarch, addr));
4374                       for (i = 0; i < partial_len; i++)
4375                         {
4376                           fprintf_unfiltered (gdb_stdlog, "%02x",
4377                                               val[i] & 0xff);
4378                         }
4379                     }
4380                   write_memory (addr, val, partial_len);
4381                 }
4382
4383               /* Note!!! This is NOT an else clause.  Odd sized
4384                  structs may go thru BOTH paths.  */
4385               /* Write this portion of the argument to a general
4386                  purpose register.  */
4387               if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
4388                 {
4389                   LONGEST regval = extract_signed_integer (val, partial_len,
4390                                                            byte_order);
4391                   /* Value may need to be sign extended, because
4392                      mips_isa_regsize() != mips_abi_regsize().  */
4393
4394                   /* A non-floating-point argument being passed in a
4395                      general register.  If a struct or union, and if
4396                      the remaining length is smaller than the register
4397                      size, we have to adjust the register value on
4398                      big endian targets.
4399
4400                      It does not seem to be necessary to do the
4401                      same for integral types.  */
4402
4403                   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
4404                       && partial_len < MIPS64_REGSIZE
4405                       && (typecode == TYPE_CODE_STRUCT
4406                           || typecode == TYPE_CODE_UNION))
4407                     regval <<= ((MIPS64_REGSIZE - partial_len)
4408                                 * TARGET_CHAR_BIT);
4409
4410                   if (mips_debug)
4411                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4412                                       argreg,
4413                                       phex (regval, MIPS64_REGSIZE));
4414                   regcache_cooked_write_unsigned (regcache, argreg, regval);
4415                   argreg++;
4416
4417                   /* Prevent subsequent floating point arguments from
4418                      being passed in floating point registers.  */
4419                   float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
4420                 }
4421
4422               len -= partial_len;
4423               val += partial_len;
4424
4425               /* Compute the offset into the stack at which we will
4426                  copy the next parameter.
4427
4428                  In older ABIs, the caller reserved space for
4429                  registers that contained arguments.  This was loosely
4430                  refered to as their "home".  Consequently, space is
4431                  always allocated.  */
4432
4433               stack_offset += align_up (partial_len, MIPS64_REGSIZE);
4434             }
4435         }
4436       if (mips_debug)
4437         fprintf_unfiltered (gdb_stdlog, "\n");
4438     }
4439
4440   regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
4441
4442   /* Return adjusted stack pointer.  */
4443   return sp;
4444 }
4445
4446 static enum return_value_convention
4447 mips_o64_return_value (struct gdbarch *gdbarch, struct type *func_type,
4448                        struct type *type, struct regcache *regcache,
4449                        gdb_byte *readbuf, const gdb_byte *writebuf)
4450 {
4451   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4452
4453   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4454       || TYPE_CODE (type) == TYPE_CODE_UNION
4455       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
4456     return RETURN_VALUE_STRUCT_CONVENTION;
4457   else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
4458     {
4459       /* A floating-point value.  It fits in the least significant
4460          part of FP0.  */
4461       if (mips_debug)
4462         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4463       mips_xfer_register (gdbarch, regcache,
4464                           gdbarch_num_regs (gdbarch)
4465                             + mips_regnum (gdbarch)->fp0,
4466                           TYPE_LENGTH (type),
4467                           gdbarch_byte_order (gdbarch),
4468                           readbuf, writebuf, 0);
4469       return RETURN_VALUE_REGISTER_CONVENTION;
4470     }
4471   else
4472     {
4473       /* A scalar extract each part but least-significant-byte
4474          justified.  */
4475       int offset;
4476       int regnum;
4477       for (offset = 0, regnum = MIPS_V0_REGNUM;
4478            offset < TYPE_LENGTH (type);
4479            offset += MIPS64_REGSIZE, regnum++)
4480         {
4481           int xfer = MIPS64_REGSIZE;
4482           if (offset + xfer > TYPE_LENGTH (type))
4483             xfer = TYPE_LENGTH (type) - offset;
4484           if (mips_debug)
4485             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4486                                 offset, xfer, regnum);
4487           mips_xfer_register (gdbarch, regcache,
4488                               gdbarch_num_regs (gdbarch) + regnum,
4489                               xfer, gdbarch_byte_order (gdbarch),
4490                               readbuf, writebuf, offset);
4491         }
4492       return RETURN_VALUE_REGISTER_CONVENTION;
4493     }
4494 }
4495
4496 /* Floating point register management.
4497
4498    Background: MIPS1 & 2 fp registers are 32 bits wide.  To support
4499    64bit operations, these early MIPS cpus treat fp register pairs
4500    (f0,f1) as a single register (d0).  Later MIPS cpu's have 64 bit fp
4501    registers and offer a compatibility mode that emulates the MIPS2 fp
4502    model.  When operating in MIPS2 fp compat mode, later cpu's split
4503    double precision floats into two 32-bit chunks and store them in
4504    consecutive fp regs.  To display 64-bit floats stored in this
4505    fashion, we have to combine 32 bits from f0 and 32 bits from f1.
4506    Throw in user-configurable endianness and you have a real mess.
4507
4508    The way this works is:
4509      - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
4510        double-precision value will be split across two logical registers.
4511        The lower-numbered logical register will hold the low-order bits,
4512        regardless of the processor's endianness.
4513      - If we are on a 64-bit processor, and we are looking for a
4514        single-precision value, it will be in the low ordered bits
4515        of a 64-bit GPR (after mfc1, for example) or a 64-bit register
4516        save slot in memory.
4517      - If we are in 64-bit mode, everything is straightforward.
4518
4519    Note that this code only deals with "live" registers at the top of the
4520    stack.  We will attempt to deal with saved registers later, when
4521    the raw/cooked register interface is in place.  (We need a general
4522    interface that can deal with dynamic saved register sizes -- fp
4523    regs could be 32 bits wide in one frame and 64 on the frame above
4524    and below).  */
4525
4526 /* Copy a 32-bit single-precision value from the current frame
4527    into rare_buffer.  */
4528
4529 static void
4530 mips_read_fp_register_single (struct frame_info *frame, int regno,
4531                               gdb_byte *rare_buffer)
4532 {
4533   struct gdbarch *gdbarch = get_frame_arch (frame);
4534   int raw_size = register_size (gdbarch, regno);
4535   gdb_byte *raw_buffer = alloca (raw_size);
4536
4537   if (!frame_register_read (frame, regno, raw_buffer))
4538     error (_("can't read register %d (%s)"),
4539            regno, gdbarch_register_name (gdbarch, regno));
4540   if (raw_size == 8)
4541     {
4542       /* We have a 64-bit value for this register.  Find the low-order
4543          32 bits.  */
4544       int offset;
4545
4546       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4547         offset = 4;
4548       else
4549         offset = 0;
4550
4551       memcpy (rare_buffer, raw_buffer + offset, 4);
4552     }
4553   else
4554     {
4555       memcpy (rare_buffer, raw_buffer, 4);
4556     }
4557 }
4558
4559 /* Copy a 64-bit double-precision value from the current frame into
4560    rare_buffer.  This may include getting half of it from the next
4561    register.  */
4562
4563 static void
4564 mips_read_fp_register_double (struct frame_info *frame, int regno,
4565                               gdb_byte *rare_buffer)
4566 {
4567   struct gdbarch *gdbarch = get_frame_arch (frame);
4568   int raw_size = register_size (gdbarch, regno);
4569
4570   if (raw_size == 8 && !mips2_fp_compat (frame))
4571     {
4572       /* We have a 64-bit value for this register, and we should use
4573          all 64 bits.  */
4574       if (!frame_register_read (frame, regno, rare_buffer))
4575         error (_("can't read register %d (%s)"),
4576                regno, gdbarch_register_name (gdbarch, regno));
4577     }
4578   else
4579     {
4580       int rawnum = regno % gdbarch_num_regs (gdbarch);
4581
4582       if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
4583         internal_error (__FILE__, __LINE__,
4584                         _("mips_read_fp_register_double: bad access to "
4585                         "odd-numbered FP register"));
4586
4587       /* mips_read_fp_register_single will find the correct 32 bits from
4588          each register.  */
4589       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4590         {
4591           mips_read_fp_register_single (frame, regno, rare_buffer + 4);
4592           mips_read_fp_register_single (frame, regno + 1, rare_buffer);
4593         }
4594       else
4595         {
4596           mips_read_fp_register_single (frame, regno, rare_buffer);
4597           mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
4598         }
4599     }
4600 }
4601
4602 static void
4603 mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
4604                         int regnum)
4605 {                               /* Do values for FP (float) regs.  */
4606   struct gdbarch *gdbarch = get_frame_arch (frame);
4607   gdb_byte *raw_buffer;
4608   double doub, flt1;    /* Doubles extracted from raw hex data.  */
4609   int inv1, inv2;
4610
4611   raw_buffer = alloca (2 * register_size (gdbarch,
4612                                           mips_regnum (gdbarch)->fp0));
4613
4614   fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
4615   fprintf_filtered (file, "%*s",
4616                     4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
4617                     "");
4618
4619   if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
4620     {
4621       struct value_print_options opts;
4622
4623       /* 4-byte registers: Print hex and floating.  Also print even
4624          numbered registers as doubles.  */
4625       mips_read_fp_register_single (frame, regnum, raw_buffer);
4626       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
4627                             raw_buffer, &inv1);
4628
4629       get_formatted_print_options (&opts, 'x');
4630       print_scalar_formatted (raw_buffer,
4631                               builtin_type (gdbarch)->builtin_uint32,
4632                               &opts, 'w', file);
4633
4634       fprintf_filtered (file, " flt: ");
4635       if (inv1)
4636         fprintf_filtered (file, " <invalid float> ");
4637       else
4638         fprintf_filtered (file, "%-17.9g", flt1);
4639
4640       if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
4641         {
4642           mips_read_fp_register_double (frame, regnum, raw_buffer);
4643           doub = unpack_double (builtin_type (gdbarch)->builtin_double,
4644                                 raw_buffer, &inv2);
4645
4646           fprintf_filtered (file, " dbl: ");
4647           if (inv2)
4648             fprintf_filtered (file, "<invalid double>");
4649           else
4650             fprintf_filtered (file, "%-24.17g", doub);
4651         }
4652     }
4653   else
4654     {
4655       struct value_print_options opts;
4656
4657       /* Eight byte registers: print each one as hex, float and double.  */
4658       mips_read_fp_register_single (frame, regnum, raw_buffer);
4659       flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
4660                             raw_buffer, &inv1);
4661
4662       mips_read_fp_register_double (frame, regnum, raw_buffer);
4663       doub = unpack_double (builtin_type (gdbarch)->builtin_double,
4664                             raw_buffer, &inv2);
4665
4666       get_formatted_print_options (&opts, 'x');
4667       print_scalar_formatted (raw_buffer,
4668                               builtin_type (gdbarch)->builtin_uint64,
4669                               &opts, 'g', file);
4670
4671       fprintf_filtered (file, " flt: ");
4672       if (inv1)
4673         fprintf_filtered (file, "<invalid float>");
4674       else
4675         fprintf_filtered (file, "%-17.9g", flt1);
4676
4677       fprintf_filtered (file, " dbl: ");
4678       if (inv2)
4679         fprintf_filtered (file, "<invalid double>");
4680       else
4681         fprintf_filtered (file, "%-24.17g", doub);
4682     }
4683 }
4684
4685 static void
4686 mips_print_register (struct ui_file *file, struct frame_info *frame,
4687                      int regnum)
4688 {
4689   struct gdbarch *gdbarch = get_frame_arch (frame);
4690   int offset;
4691   struct value_print_options opts;
4692   struct value *val;
4693
4694   if (TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT)
4695     {
4696       mips_print_fp_register (file, frame, regnum);
4697       return;
4698     }
4699
4700   val = get_frame_register_value (frame, regnum);
4701   if (value_optimized_out (val))
4702     {
4703       fprintf_filtered (file, "%s: [Invalid]",
4704                         gdbarch_register_name (gdbarch, regnum));
4705       return;
4706     }
4707
4708   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
4709
4710   /* The problem with printing numeric register names (r26, etc.) is that
4711      the user can't use them on input.  Probably the best solution is to
4712      fix it so that either the numeric or the funky (a2, etc.) names
4713      are accepted on input.  */
4714   if (regnum < MIPS_NUMREGS)
4715     fprintf_filtered (file, "(r%d): ", regnum);
4716   else
4717     fprintf_filtered (file, ": ");
4718
4719   get_formatted_print_options (&opts, 'x');
4720   val_print_scalar_formatted (value_type (val),
4721                               value_contents_for_printing (val),
4722                               value_embedded_offset (val),
4723                               val,
4724                               &opts, 0, file);
4725 }
4726
4727 /* Replacement for generic do_registers_info.
4728    Print regs in pretty columns.  */
4729
4730 static int
4731 print_fp_register_row (struct ui_file *file, struct frame_info *frame,
4732                        int regnum)
4733 {
4734   fprintf_filtered (file, " ");
4735   mips_print_fp_register (file, frame, regnum);
4736   fprintf_filtered (file, "\n");
4737   return regnum + 1;
4738 }
4739
4740
4741 /* Print a row's worth of GP (int) registers, with name labels above.  */
4742
4743 static int
4744 print_gp_register_row (struct ui_file *file, struct frame_info *frame,
4745                        int start_regnum)
4746 {
4747   struct gdbarch *gdbarch = get_frame_arch (frame);
4748   /* Do values for GP (int) regs.  */
4749   gdb_byte raw_buffer[MAX_REGISTER_SIZE];
4750   int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8);    /* display cols
4751                                                                per row.  */
4752   int col, byte;
4753   int regnum;
4754
4755   /* For GP registers, we print a separate row of names above the vals.  */
4756   for (col = 0, regnum = start_regnum;
4757        col < ncols && regnum < gdbarch_num_regs (gdbarch)
4758                                + gdbarch_num_pseudo_regs (gdbarch);
4759        regnum++)
4760     {
4761       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
4762         continue;               /* unused register */
4763       if (TYPE_CODE (register_type (gdbarch, regnum)) ==
4764           TYPE_CODE_FLT)
4765         break;                  /* End the row: reached FP register.  */
4766       /* Large registers are handled separately.  */
4767       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
4768         {
4769           if (col > 0)
4770             break;              /* End the row before this register.  */
4771
4772           /* Print this register on a row by itself.  */
4773           mips_print_register (file, frame, regnum);
4774           fprintf_filtered (file, "\n");
4775           return regnum + 1;
4776         }
4777       if (col == 0)
4778         fprintf_filtered (file, "     ");
4779       fprintf_filtered (file,
4780                         mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
4781                         gdbarch_register_name (gdbarch, regnum));
4782       col++;
4783     }
4784
4785   if (col == 0)
4786     return regnum;
4787
4788   /* Print the R0 to R31 names.  */
4789   if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
4790     fprintf_filtered (file, "\n R%-4d",
4791                       start_regnum % gdbarch_num_regs (gdbarch));
4792   else
4793     fprintf_filtered (file, "\n      ");
4794
4795   /* Now print the values in hex, 4 or 8 to the row.  */
4796   for (col = 0, regnum = start_regnum;
4797        col < ncols && regnum < gdbarch_num_regs (gdbarch)
4798                                + gdbarch_num_pseudo_regs (gdbarch);
4799        regnum++)
4800     {
4801       if (*gdbarch_register_name (gdbarch, regnum) == '\0')
4802         continue;               /* unused register */
4803       if (TYPE_CODE (register_type (gdbarch, regnum)) ==
4804           TYPE_CODE_FLT)
4805         break;                  /* End row: reached FP register.  */
4806       if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
4807         break;                  /* End row: large register.  */
4808
4809       /* OK: get the data in raw format.  */
4810       if (!frame_register_read (frame, regnum, raw_buffer))
4811         error (_("can't read register %d (%s)"),
4812                regnum, gdbarch_register_name (gdbarch, regnum));
4813       /* pad small registers */
4814       for (byte = 0;
4815            byte < (mips_abi_regsize (gdbarch)
4816                    - register_size (gdbarch, regnum)); byte++)
4817         printf_filtered ("  ");
4818       /* Now print the register value in hex, endian order.  */
4819       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4820         for (byte =
4821              register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
4822              byte < register_size (gdbarch, regnum); byte++)
4823           fprintf_filtered (file, "%02x", raw_buffer[byte]);
4824       else
4825         for (byte = register_size (gdbarch, regnum) - 1;
4826              byte >= 0; byte--)
4827           fprintf_filtered (file, "%02x", raw_buffer[byte]);
4828       fprintf_filtered (file, " ");
4829       col++;
4830     }
4831   if (col > 0)                  /* ie. if we actually printed anything...  */
4832     fprintf_filtered (file, "\n");
4833
4834   return regnum;
4835 }
4836
4837 /* MIPS_DO_REGISTERS_INFO(): called by "info register" command.  */
4838
4839 static void
4840 mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
4841                            struct frame_info *frame, int regnum, int all)
4842 {
4843   if (regnum != -1)             /* Do one specified register.  */
4844     {
4845       gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
4846       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
4847         error (_("Not a valid register for the current processor type"));
4848
4849       mips_print_register (file, frame, regnum);
4850       fprintf_filtered (file, "\n");
4851     }
4852   else
4853     /* Do all (or most) registers.  */
4854     {
4855       regnum = gdbarch_num_regs (gdbarch);
4856       while (regnum < gdbarch_num_regs (gdbarch)
4857                       + gdbarch_num_pseudo_regs (gdbarch))
4858         {
4859           if (TYPE_CODE (register_type (gdbarch, regnum)) ==
4860               TYPE_CODE_FLT)
4861             {
4862               if (all)          /* True for "INFO ALL-REGISTERS" command.  */
4863                 regnum = print_fp_register_row (file, frame, regnum);
4864               else
4865                 regnum += MIPS_NUMREGS; /* Skip floating point regs.  */
4866             }
4867           else
4868             regnum = print_gp_register_row (file, frame, regnum);
4869         }
4870     }
4871 }
4872
4873 /* Is this a branch with a delay slot?  */
4874
4875 static int
4876 is_delayed (unsigned long insn)
4877 {
4878   int i;
4879   for (i = 0; i < NUMOPCODES; ++i)
4880     if (mips_opcodes[i].pinfo != INSN_MACRO
4881         && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
4882       break;
4883   return (i < NUMOPCODES
4884           && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
4885                                        | INSN_COND_BRANCH_DELAY
4886                                        | INSN_COND_BRANCH_LIKELY)));
4887 }
4888
4889 static int
4890 mips_single_step_through_delay (struct gdbarch *gdbarch,
4891                                 struct frame_info *frame)
4892 {
4893   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4894   CORE_ADDR pc = get_frame_pc (frame);
4895   gdb_byte buf[MIPS_INSN32_SIZE];
4896
4897   /* There is no branch delay slot on MIPS16.  */
4898   if (mips_pc_is_mips16 (pc))
4899     return 0;
4900
4901   if (!breakpoint_here_p (get_frame_address_space (frame), pc + 4))
4902     return 0;
4903
4904   if (!safe_frame_unwind_memory (frame, pc, buf, sizeof buf))
4905     /* If error reading memory, guess that it is not a delayed
4906        branch.  */
4907     return 0;
4908   return is_delayed (extract_unsigned_integer (buf, sizeof buf, byte_order));
4909 }
4910
4911 /* To skip prologues, I use this predicate.  Returns either PC itself
4912    if the code at PC does not look like a function prologue; otherwise
4913    returns an address that (if we're lucky) follows the prologue.  If
4914    LENIENT, then we must skip everything which is involved in setting
4915    up the frame (it's OK to skip more, just so long as we don't skip
4916    anything which might clobber the registers which are being saved.
4917    We must skip more in the case where part of the prologue is in the
4918    delay slot of a non-prologue instruction).  */
4919
4920 static CORE_ADDR
4921 mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
4922 {
4923   CORE_ADDR limit_pc;
4924   CORE_ADDR func_addr;
4925
4926   /* See if we can determine the end of the prologue via the symbol table.
4927      If so, then return either PC, or the PC after the prologue, whichever
4928      is greater.  */
4929   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
4930     {
4931       CORE_ADDR post_prologue_pc
4932         = skip_prologue_using_sal (gdbarch, func_addr);
4933       if (post_prologue_pc != 0)
4934         return max (pc, post_prologue_pc);
4935     }
4936
4937   /* Can't determine prologue from the symbol table, need to examine
4938      instructions.  */
4939
4940   /* Find an upper limit on the function prologue using the debug
4941      information.  If the debug information could not be used to provide
4942      that bound, then use an arbitrary large number as the upper bound.  */
4943   limit_pc = skip_prologue_using_sal (gdbarch, pc);
4944   if (limit_pc == 0)
4945     limit_pc = pc + 100;          /* Magic.  */
4946
4947   if (mips_pc_is_mips16 (pc))
4948     return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
4949   else
4950     return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
4951 }
4952
4953 /* Check whether the PC is in a function epilogue (32-bit version).
4954    This is a helper function for mips_in_function_epilogue_p.  */
4955 static int
4956 mips32_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4957 {
4958   CORE_ADDR func_addr = 0, func_end = 0;
4959
4960   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
4961     {
4962       /* The MIPS epilogue is max. 12 bytes long.  */
4963       CORE_ADDR addr = func_end - 12;
4964
4965       if (addr < func_addr + 4)
4966         addr = func_addr + 4;
4967       if (pc < addr)
4968         return 0;
4969
4970       for (; pc < func_end; pc += MIPS_INSN32_SIZE)
4971         {
4972           unsigned long high_word;
4973           unsigned long inst;
4974
4975           inst = mips_fetch_instruction (gdbarch, pc);
4976           high_word = (inst >> 16) & 0xffff;
4977
4978           if (high_word != 0x27bd       /* addiu $sp,$sp,offset */
4979               && high_word != 0x67bd    /* daddiu $sp,$sp,offset */
4980               && inst != 0x03e00008     /* jr $ra */
4981               && inst != 0x00000000)    /* nop */
4982             return 0;
4983         }
4984
4985       return 1;
4986     }
4987
4988   return 0;
4989 }
4990
4991 /* Check whether the PC is in a function epilogue (16-bit version).
4992    This is a helper function for mips_in_function_epilogue_p.  */
4993 static int
4994 mips16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
4995 {
4996   CORE_ADDR func_addr = 0, func_end = 0;
4997
4998   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
4999     {
5000       /* The MIPS epilogue is max. 12 bytes long.  */
5001       CORE_ADDR addr = func_end - 12;
5002
5003       if (addr < func_addr + 4)
5004         addr = func_addr + 4;
5005       if (pc < addr)
5006         return 0;
5007
5008       for (; pc < func_end; pc += MIPS_INSN16_SIZE)
5009         {
5010           unsigned short inst;
5011
5012           inst = mips_fetch_instruction (gdbarch, pc);
5013
5014           if ((inst & 0xf800) == 0xf000)        /* extend */
5015             continue;
5016
5017           if (inst != 0x6300            /* addiu $sp,offset */
5018               && inst != 0xfb00         /* daddiu $sp,$sp,offset */
5019               && inst != 0xe820         /* jr $ra */
5020               && inst != 0xe8a0         /* jrc $ra */
5021               && inst != 0x6500)        /* nop */
5022             return 0;
5023         }
5024
5025       return 1;
5026     }
5027
5028   return 0;
5029 }
5030
5031 /* The epilogue is defined here as the area at the end of a function,
5032    after an instruction which destroys the function's stack frame.  */
5033 static int
5034 mips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
5035 {
5036   if (mips_pc_is_mips16 (pc))
5037     return mips16_in_function_epilogue_p (gdbarch, pc);
5038   else
5039     return mips32_in_function_epilogue_p (gdbarch, pc);
5040 }
5041
5042 /* Root of all "set mips "/"show mips " commands.  This will eventually be
5043    used for all MIPS-specific commands.  */
5044
5045 static void
5046 show_mips_command (char *args, int from_tty)
5047 {
5048   help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
5049 }
5050
5051 static void
5052 set_mips_command (char *args, int from_tty)
5053 {
5054   printf_unfiltered
5055     ("\"set mips\" must be followed by an appropriate subcommand.\n");
5056   help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
5057 }
5058
5059 /* Commands to show/set the MIPS FPU type.  */
5060
5061 static void
5062 show_mipsfpu_command (char *args, int from_tty)
5063 {
5064   char *fpu;
5065
5066   if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_mips)
5067     {
5068       printf_unfiltered
5069         ("The MIPS floating-point coprocessor is unknown "
5070          "because the current architecture is not MIPS.\n");
5071       return;
5072     }
5073
5074   switch (MIPS_FPU_TYPE (target_gdbarch))
5075     {
5076     case MIPS_FPU_SINGLE:
5077       fpu = "single-precision";
5078       break;
5079     case MIPS_FPU_DOUBLE:
5080       fpu = "double-precision";
5081       break;
5082     case MIPS_FPU_NONE:
5083       fpu = "absent (none)";
5084       break;
5085     default:
5086       internal_error (__FILE__, __LINE__, _("bad switch"));
5087     }
5088   if (mips_fpu_type_auto)
5089     printf_unfiltered ("The MIPS floating-point coprocessor "
5090                        "is set automatically (currently %s)\n",
5091                        fpu);
5092   else
5093     printf_unfiltered
5094       ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
5095 }
5096
5097
5098 static void
5099 set_mipsfpu_command (char *args, int from_tty)
5100 {
5101   printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
5102                      "\"single\",\"none\" or \"auto\".\n");
5103   show_mipsfpu_command (args, from_tty);
5104 }
5105
5106 static void
5107 set_mipsfpu_single_command (char *args, int from_tty)
5108 {
5109   struct gdbarch_info info;
5110   gdbarch_info_init (&info);
5111   mips_fpu_type = MIPS_FPU_SINGLE;
5112   mips_fpu_type_auto = 0;
5113   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5114      instead of relying on globals.  Doing that would let generic code
5115      handle the search for this specific architecture.  */
5116   if (!gdbarch_update_p (info))
5117     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
5118 }
5119
5120 static void
5121 set_mipsfpu_double_command (char *args, int from_tty)
5122 {
5123   struct gdbarch_info info;
5124   gdbarch_info_init (&info);
5125   mips_fpu_type = MIPS_FPU_DOUBLE;
5126   mips_fpu_type_auto = 0;
5127   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5128      instead of relying on globals.  Doing that would let generic code
5129      handle the search for this specific architecture.  */
5130   if (!gdbarch_update_p (info))
5131     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
5132 }
5133
5134 static void
5135 set_mipsfpu_none_command (char *args, int from_tty)
5136 {
5137   struct gdbarch_info info;
5138   gdbarch_info_init (&info);
5139   mips_fpu_type = MIPS_FPU_NONE;
5140   mips_fpu_type_auto = 0;
5141   /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5142      instead of relying on globals.  Doing that would let generic code
5143      handle the search for this specific architecture.  */
5144   if (!gdbarch_update_p (info))
5145     internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
5146 }
5147
5148 static void
5149 set_mipsfpu_auto_command (char *args, int from_tty)
5150 {
5151   mips_fpu_type_auto = 1;
5152 }
5153
5154 /* Attempt to identify the particular processor model by reading the
5155    processor id.  NOTE: cagney/2003-11-15: Firstly it isn't clear that
5156    the relevant processor still exists (it dates back to '94) and
5157    secondly this is not the way to do this.  The processor type should
5158    be set by forcing an architecture change.  */
5159
5160 void
5161 deprecated_mips_set_processor_regs_hack (void)
5162 {
5163   struct regcache *regcache = get_current_regcache ();
5164   struct gdbarch *gdbarch = get_regcache_arch (regcache);
5165   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5166   ULONGEST prid;
5167
5168   regcache_cooked_read_unsigned (regcache, MIPS_PRID_REGNUM, &prid);
5169   if ((prid & ~0xf) == 0x700)
5170     tdep->mips_processor_reg_names = mips_r3041_reg_names;
5171 }
5172
5173 /* Just like reinit_frame_cache, but with the right arguments to be
5174    callable as an sfunc.  */
5175
5176 static void
5177 reinit_frame_cache_sfunc (char *args, int from_tty,
5178                           struct cmd_list_element *c)
5179 {
5180   reinit_frame_cache ();
5181 }
5182
5183 static int
5184 gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
5185 {
5186   /* FIXME: cagney/2003-06-26: Is this even necessary?  The
5187      disassembler needs to be able to locally determine the ISA, and
5188      not rely on GDB.  Otherwize the stand-alone 'objdump -d' will not
5189      work.  */
5190   if (mips_pc_is_mips16 (memaddr))
5191     info->mach = bfd_mach_mips16;
5192
5193   /* Round down the instruction address to the appropriate boundary.  */
5194   memaddr &= (info->mach == bfd_mach_mips16 ? ~1 : ~3);
5195
5196   /* Set the disassembler options.  */
5197   if (!info->disassembler_options)
5198     /* This string is not recognized explicitly by the disassembler,
5199        but it tells the disassembler to not try to guess the ABI from
5200        the bfd elf headers, such that, if the user overrides the ABI
5201        of a program linked as NewABI, the disassembly will follow the
5202        register naming conventions specified by the user.  */
5203     info->disassembler_options = "gpr-names=32";
5204
5205   /* Call the appropriate disassembler based on the target endian-ness.  */
5206   if (info->endian == BFD_ENDIAN_BIG)
5207     return print_insn_big_mips (memaddr, info);
5208   else
5209     return print_insn_little_mips (memaddr, info);
5210 }
5211
5212 static int
5213 gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
5214 {
5215   /* Set up the disassembler info, so that we get the right
5216      register names from libopcodes.  */
5217   info->disassembler_options = "gpr-names=n32";
5218   info->flavour = bfd_target_elf_flavour;
5219
5220   return gdb_print_insn_mips (memaddr, info);
5221 }
5222
5223 static int
5224 gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
5225 {
5226   /* Set up the disassembler info, so that we get the right
5227      register names from libopcodes.  */
5228   info->disassembler_options = "gpr-names=64";
5229   info->flavour = bfd_target_elf_flavour;
5230
5231   return gdb_print_insn_mips (memaddr, info);
5232 }
5233
5234 /* This function implements gdbarch_breakpoint_from_pc.  It uses the
5235    program counter value to determine whether a 16- or 32-bit breakpoint
5236    should be used.  It returns a pointer to a string of bytes that encode a
5237    breakpoint instruction, stores the length of the string to *lenptr, and
5238    adjusts pc (if necessary) to point to the actual memory location where
5239    the breakpoint should be inserted.  */
5240
5241 static const gdb_byte *
5242 mips_breakpoint_from_pc (struct gdbarch *gdbarch,
5243                          CORE_ADDR *pcptr, int *lenptr)
5244 {
5245   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
5246     {
5247       if (mips_pc_is_mips16 (*pcptr))
5248         {
5249           static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
5250           *pcptr = unmake_mips16_addr (*pcptr);
5251           *lenptr = sizeof (mips16_big_breakpoint);
5252           return mips16_big_breakpoint;
5253         }
5254       else
5255         {
5256           /* The IDT board uses an unusual breakpoint value, and
5257              sometimes gets confused when it sees the usual MIPS
5258              breakpoint instruction.  */
5259           static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
5260           static gdb_byte pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
5261           static gdb_byte idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
5262           /* Likewise, IRIX appears to expect a different breakpoint,
5263              although this is not apparent until you try to use pthreads.  */
5264           static gdb_byte irix_big_breakpoint[] = { 0, 0, 0, 0xd };
5265
5266           *lenptr = sizeof (big_breakpoint);
5267
5268           if (strcmp (target_shortname, "mips") == 0)
5269             return idt_big_breakpoint;
5270           else if (strcmp (target_shortname, "ddb") == 0
5271                    || strcmp (target_shortname, "pmon") == 0
5272                    || strcmp (target_shortname, "lsi") == 0)
5273             return pmon_big_breakpoint;
5274           else if (gdbarch_osabi (gdbarch) == GDB_OSABI_IRIX)
5275             return irix_big_breakpoint;
5276           else
5277             return big_breakpoint;
5278         }
5279     }
5280   else
5281     {
5282       if (mips_pc_is_mips16 (*pcptr))
5283         {
5284           static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
5285           *pcptr = unmake_mips16_addr (*pcptr);
5286           *lenptr = sizeof (mips16_little_breakpoint);
5287           return mips16_little_breakpoint;
5288         }
5289       else
5290         {
5291           static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
5292           static gdb_byte pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
5293           static gdb_byte idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };
5294
5295           *lenptr = sizeof (little_breakpoint);
5296
5297           if (strcmp (target_shortname, "mips") == 0)
5298             return idt_little_breakpoint;
5299           else if (strcmp (target_shortname, "ddb") == 0
5300                    || strcmp (target_shortname, "pmon") == 0
5301                    || strcmp (target_shortname, "lsi") == 0)
5302             return pmon_little_breakpoint;
5303           else
5304             return little_breakpoint;
5305         }
5306     }
5307 }
5308
5309 /* If PC is in a mips16 call or return stub, return the address of the target
5310    PC, which is either the callee or the caller.  There are several
5311    cases which must be handled:
5312
5313    * If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
5314    target PC is in $31 ($ra).
5315    * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
5316    and the target PC is in $2.
5317    * If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
5318    before the jal instruction, this is effectively a call stub
5319    and the target PC is in $2.  Otherwise this is effectively
5320    a return stub and the target PC is in $18.
5321
5322    See the source code for the stubs in gcc/config/mips/mips16.S for
5323    gory details.  */
5324
5325 static CORE_ADDR
5326 mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
5327 {
5328   struct gdbarch *gdbarch = get_frame_arch (frame);
5329   char *name;
5330   CORE_ADDR start_addr;
5331
5332   /* Find the starting address and name of the function containing the PC.  */
5333   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
5334     return 0;
5335
5336   /* If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
5337      target PC is in $31 ($ra).  */
5338   if (strcmp (name, "__mips16_ret_sf") == 0
5339       || strcmp (name, "__mips16_ret_df") == 0)
5340     return get_frame_register_signed (frame, MIPS_RA_REGNUM);
5341
5342   if (strncmp (name, "__mips16_call_stub_", 19) == 0)
5343     {
5344       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
5345          and the target PC is in $2.  */
5346       if (name[19] >= '0' && name[19] <= '9')
5347         return get_frame_register_signed (frame, 2);
5348
5349       /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
5350          before the jal instruction, this is effectively a call stub
5351          and the target PC is in $2.  Otherwise this is effectively
5352          a return stub and the target PC is in $18.  */
5353       else if (name[19] == 's' || name[19] == 'd')
5354         {
5355           if (pc == start_addr)
5356             {
5357               /* Check if the target of the stub is a compiler-generated
5358                  stub.  Such a stub for a function bar might have a name
5359                  like __fn_stub_bar, and might look like this:
5360                  mfc1    $4,$f13
5361                  mfc1    $5,$f12
5362                  mfc1    $6,$f15
5363                  mfc1    $7,$f14
5364                  la      $1,bar   (becomes a lui/addiu pair)
5365                  jr      $1
5366                  So scan down to the lui/addi and extract the target
5367                  address from those two instructions.  */
5368
5369               CORE_ADDR target_pc = get_frame_register_signed (frame, 2);
5370               ULONGEST inst;
5371               int i;
5372
5373               /* See if the name of the target function is  __fn_stub_*.  */
5374               if (find_pc_partial_function (target_pc, &name, NULL, NULL) ==
5375                   0)
5376                 return target_pc;
5377               if (strncmp (name, "__fn_stub_", 10) != 0
5378                   && strcmp (name, "etext") != 0
5379                   && strcmp (name, "_etext") != 0)
5380                 return target_pc;
5381
5382               /* Scan through this _fn_stub_ code for the lui/addiu pair.
5383                  The limit on the search is arbitrarily set to 20
5384                  instructions.  FIXME.  */
5385               for (i = 0, pc = 0; i < 20; i++, target_pc += MIPS_INSN32_SIZE)
5386                 {
5387                   inst = mips_fetch_instruction (gdbarch, target_pc);
5388                   if ((inst & 0xffff0000) == 0x3c010000)        /* lui $at */
5389                     pc = (inst << 16) & 0xffff0000;             /* high word */
5390                   else if ((inst & 0xffff0000) == 0x24210000)   /* addiu $at */
5391                     return pc | (inst & 0xffff);                /* low word */
5392                 }
5393
5394               /* Couldn't find the lui/addui pair, so return stub address.  */
5395               return target_pc;
5396             }
5397           else
5398             /* This is the 'return' part of a call stub.  The return
5399                address is in $r18.  */
5400             return get_frame_register_signed (frame, 18);
5401         }
5402     }
5403   return 0;                     /* not a stub */
5404 }
5405
5406 /* If the current PC is the start of a non-PIC-to-PIC stub, return the
5407    PC of the stub target.  The stub just loads $t9 and jumps to it,
5408    so that $t9 has the correct value at function entry.  */
5409
5410 static CORE_ADDR
5411 mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
5412 {
5413   struct gdbarch *gdbarch = get_frame_arch (frame);
5414   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5415   struct minimal_symbol *msym;
5416   int i;
5417   gdb_byte stub_code[16];
5418   int32_t stub_words[4];
5419
5420   /* The stub for foo is named ".pic.foo", and is either two
5421      instructions inserted before foo or a three instruction sequence
5422      which jumps to foo.  */
5423   msym = lookup_minimal_symbol_by_pc (pc);
5424   if (msym == NULL
5425       || SYMBOL_VALUE_ADDRESS (msym) != pc
5426       || SYMBOL_LINKAGE_NAME (msym) == NULL
5427       || strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) != 0)
5428     return 0;
5429
5430   /* A two-instruction header.  */
5431   if (MSYMBOL_SIZE (msym) == 8)
5432     return pc + 8;
5433
5434   /* A three-instruction (plus delay slot) trampoline.  */
5435   if (MSYMBOL_SIZE (msym) == 16)
5436     {
5437       if (target_read_memory (pc, stub_code, 16) != 0)
5438         return 0;
5439       for (i = 0; i < 4; i++)
5440         stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
5441                                                   4, byte_order);
5442
5443       /* A stub contains these instructions:
5444          lui    t9, %hi(target)
5445          j      target
5446           addiu t9, t9, %lo(target)
5447          nop
5448
5449          This works even for N64, since stubs are only generated with
5450          -msym32.  */
5451       if ((stub_words[0] & 0xffff0000U) == 0x3c190000
5452           && (stub_words[1] & 0xfc000000U) == 0x08000000
5453           && (stub_words[2] & 0xffff0000U) == 0x27390000
5454           && stub_words[3] == 0x00000000)
5455         return (((stub_words[0] & 0x0000ffff) << 16)
5456                 + (stub_words[2] & 0x0000ffff));
5457     }
5458
5459   /* Not a recognized stub.  */
5460   return 0;
5461 }
5462
5463 static CORE_ADDR
5464 mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
5465 {
5466   CORE_ADDR target_pc;
5467
5468   target_pc = mips_skip_mips16_trampoline_code (frame, pc);
5469   if (target_pc)
5470     return target_pc;
5471
5472   target_pc = find_solib_trampoline_target (frame, pc);
5473   if (target_pc)
5474     return target_pc;
5475
5476   target_pc = mips_skip_pic_trampoline_code (frame, pc);
5477   if (target_pc)
5478     return target_pc;
5479
5480   return 0;
5481 }
5482
5483 /* Convert a dbx stab register number (from `r' declaration) to a GDB
5484    [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
5485
5486 static int
5487 mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
5488 {
5489   int regnum;
5490   if (num >= 0 && num < 32)
5491     regnum = num;
5492   else if (num >= 38 && num < 70)
5493     regnum = num + mips_regnum (gdbarch)->fp0 - 38;
5494   else if (num == 70)
5495     regnum = mips_regnum (gdbarch)->hi;
5496   else if (num == 71)
5497     regnum = mips_regnum (gdbarch)->lo;
5498   else
5499     /* This will hopefully (eventually) provoke a warning.  Should
5500        we be calling complaint() here?  */
5501     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
5502   return gdbarch_num_regs (gdbarch) + regnum;
5503 }
5504
5505
5506 /* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
5507    gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM.  */
5508
5509 static int
5510 mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
5511 {
5512   int regnum;
5513   if (num >= 0 && num < 32)
5514     regnum = num;
5515   else if (num >= 32 && num < 64)
5516     regnum = num + mips_regnum (gdbarch)->fp0 - 32;
5517   else if (num == 64)
5518     regnum = mips_regnum (gdbarch)->hi;
5519   else if (num == 65)
5520     regnum = mips_regnum (gdbarch)->lo;
5521   else
5522     /* This will hopefully (eventually) provoke a warning.  Should we
5523        be calling complaint() here?  */
5524     return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
5525   return gdbarch_num_regs (gdbarch) + regnum;
5526 }
5527
5528 static int
5529 mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
5530 {
5531   /* Only makes sense to supply raw registers.  */
5532   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
5533   /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
5534      decide if it is valid.  Should instead define a standard sim/gdb
5535      register numbering scheme.  */
5536   if (gdbarch_register_name (gdbarch,
5537                              gdbarch_num_regs (gdbarch) + regnum) != NULL
5538       && gdbarch_register_name (gdbarch,
5539                                 gdbarch_num_regs (gdbarch)
5540                                 + regnum)[0] != '\0')
5541     return regnum;
5542   else
5543     return LEGACY_SIM_REGNO_IGNORE;
5544 }
5545
5546
5547 /* Convert an integer into an address.  Extracting the value signed
5548    guarantees a correctly sign extended address.  */
5549
5550 static CORE_ADDR
5551 mips_integer_to_address (struct gdbarch *gdbarch,
5552                          struct type *type, const gdb_byte *buf)
5553 {
5554   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5555   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
5556 }
5557
5558 /* Dummy virtual frame pointer method.  This is no more or less accurate
5559    than most other architectures; we just need to be explicit about it,
5560    because the pseudo-register gdbarch_sp_regnum will otherwise lead to
5561    an assertion failure.  */
5562
5563 static void
5564 mips_virtual_frame_pointer (struct gdbarch *gdbarch, 
5565                             CORE_ADDR pc, int *reg, LONGEST *offset)
5566 {
5567   *reg = MIPS_SP_REGNUM;
5568   *offset = 0;
5569 }
5570
5571 static void
5572 mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
5573 {
5574   enum mips_abi *abip = (enum mips_abi *) obj;
5575   const char *name = bfd_get_section_name (abfd, sect);
5576
5577   if (*abip != MIPS_ABI_UNKNOWN)
5578     return;
5579
5580   if (strncmp (name, ".mdebug.", 8) != 0)
5581     return;
5582
5583   if (strcmp (name, ".mdebug.abi32") == 0)
5584     *abip = MIPS_ABI_O32;
5585   else if (strcmp (name, ".mdebug.abiN32") == 0)
5586     *abip = MIPS_ABI_N32;
5587   else if (strcmp (name, ".mdebug.abi64") == 0)
5588     *abip = MIPS_ABI_N64;
5589   else if (strcmp (name, ".mdebug.abiO64") == 0)
5590     *abip = MIPS_ABI_O64;
5591   else if (strcmp (name, ".mdebug.eabi32") == 0)
5592     *abip = MIPS_ABI_EABI32;
5593   else if (strcmp (name, ".mdebug.eabi64") == 0)
5594     *abip = MIPS_ABI_EABI64;
5595   else
5596     warning (_("unsupported ABI %s."), name + 8);
5597 }
5598
5599 static void
5600 mips_find_long_section (bfd *abfd, asection *sect, void *obj)
5601 {
5602   int *lbp = (int *) obj;
5603   const char *name = bfd_get_section_name (abfd, sect);
5604
5605   if (strncmp (name, ".gcc_compiled_long32", 20) == 0)
5606     *lbp = 32;
5607   else if (strncmp (name, ".gcc_compiled_long64", 20) == 0)
5608     *lbp = 64;
5609   else if (strncmp (name, ".gcc_compiled_long", 18) == 0)
5610     warning (_("unrecognized .gcc_compiled_longXX"));
5611 }
5612
5613 static enum mips_abi
5614 global_mips_abi (void)
5615 {
5616   int i;
5617
5618   for (i = 0; mips_abi_strings[i] != NULL; i++)
5619     if (mips_abi_strings[i] == mips_abi_string)
5620       return (enum mips_abi) i;
5621
5622   internal_error (__FILE__, __LINE__, _("unknown ABI string"));
5623 }
5624
5625 static void
5626 mips_register_g_packet_guesses (struct gdbarch *gdbarch)
5627 {
5628   /* If the size matches the set of 32-bit or 64-bit integer registers,
5629      assume that's what we've got.  */
5630   register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
5631   register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);
5632
5633   /* If the size matches the full set of registers GDB traditionally
5634      knows about, including floating point, for either 32-bit or
5635      64-bit, assume that's what we've got.  */
5636   register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
5637   register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);
5638
5639   /* Otherwise we don't have a useful guess.  */
5640 }
5641
5642 static struct value *
5643 value_of_mips_user_reg (struct frame_info *frame, const void *baton)
5644 {
5645   const int *reg_p = baton;
5646   return value_of_register (*reg_p, frame);
5647 }
5648
5649 static struct gdbarch *
5650 mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
5651 {
5652   struct gdbarch *gdbarch;
5653   struct gdbarch_tdep *tdep;
5654   int elf_flags;
5655   enum mips_abi mips_abi, found_abi, wanted_abi;
5656   int i, num_regs;
5657   enum mips_fpu_type fpu_type;
5658   struct tdesc_arch_data *tdesc_data = NULL;
5659   int elf_fpu_type = 0;
5660
5661   /* Check any target description for validity.  */
5662   if (tdesc_has_registers (info.target_desc))
5663     {
5664       static const char *const mips_gprs[] = {
5665         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
5666         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
5667         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
5668         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
5669       };
5670       static const char *const mips_fprs[] = {
5671         "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
5672         "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
5673         "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
5674         "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
5675       };
5676
5677       const struct tdesc_feature *feature;
5678       int valid_p;
5679
5680       feature = tdesc_find_feature (info.target_desc,
5681                                     "org.gnu.gdb.mips.cpu");
5682       if (feature == NULL)
5683         return NULL;
5684
5685       tdesc_data = tdesc_data_alloc ();
5686
5687       valid_p = 1;
5688       for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
5689         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
5690                                             mips_gprs[i]);
5691
5692
5693       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5694                                           MIPS_EMBED_LO_REGNUM, "lo");
5695       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5696                                           MIPS_EMBED_HI_REGNUM, "hi");
5697       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5698                                           MIPS_EMBED_PC_REGNUM, "pc");
5699
5700       if (!valid_p)
5701         {
5702           tdesc_data_cleanup (tdesc_data);
5703           return NULL;
5704         }
5705
5706       feature = tdesc_find_feature (info.target_desc,
5707                                     "org.gnu.gdb.mips.cp0");
5708       if (feature == NULL)
5709         {
5710           tdesc_data_cleanup (tdesc_data);
5711           return NULL;
5712         }
5713
5714       valid_p = 1;
5715       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5716                                           MIPS_EMBED_BADVADDR_REGNUM,
5717                                           "badvaddr");
5718       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5719                                           MIPS_PS_REGNUM, "status");
5720       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5721                                           MIPS_EMBED_CAUSE_REGNUM, "cause");
5722
5723       if (!valid_p)
5724         {
5725           tdesc_data_cleanup (tdesc_data);
5726           return NULL;
5727         }
5728
5729       /* FIXME drow/2007-05-17: The FPU should be optional.  The MIPS
5730          backend is not prepared for that, though.  */
5731       feature = tdesc_find_feature (info.target_desc,
5732                                     "org.gnu.gdb.mips.fpu");
5733       if (feature == NULL)
5734         {
5735           tdesc_data_cleanup (tdesc_data);
5736           return NULL;
5737         }
5738
5739       valid_p = 1;
5740       for (i = 0; i < 32; i++)
5741         valid_p &= tdesc_numbered_register (feature, tdesc_data,
5742                                             i + MIPS_EMBED_FP0_REGNUM,
5743                                             mips_fprs[i]);
5744
5745       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5746                                           MIPS_EMBED_FP0_REGNUM + 32, "fcsr");
5747       valid_p &= tdesc_numbered_register (feature, tdesc_data,
5748                                           MIPS_EMBED_FP0_REGNUM + 33, "fir");
5749
5750       if (!valid_p)
5751         {
5752           tdesc_data_cleanup (tdesc_data);
5753           return NULL;
5754         }
5755
5756       /* It would be nice to detect an attempt to use a 64-bit ABI
5757          when only 32-bit registers are provided.  */
5758     }
5759
5760   /* First of all, extract the elf_flags, if available.  */
5761   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
5762     elf_flags = elf_elfheader (info.abfd)->e_flags;
5763   else if (arches != NULL)
5764     elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
5765   else
5766     elf_flags = 0;
5767   if (gdbarch_debug)
5768     fprintf_unfiltered (gdb_stdlog,
5769                         "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
5770
5771   /* Check ELF_FLAGS to see if it specifies the ABI being used.  */
5772   switch ((elf_flags & EF_MIPS_ABI))
5773     {
5774     case E_MIPS_ABI_O32:
5775       found_abi = MIPS_ABI_O32;
5776       break;
5777     case E_MIPS_ABI_O64:
5778       found_abi = MIPS_ABI_O64;
5779       break;
5780     case E_MIPS_ABI_EABI32:
5781       found_abi = MIPS_ABI_EABI32;
5782       break;
5783     case E_MIPS_ABI_EABI64:
5784       found_abi = MIPS_ABI_EABI64;
5785       break;
5786     default:
5787       if ((elf_flags & EF_MIPS_ABI2))
5788         found_abi = MIPS_ABI_N32;
5789       else
5790         found_abi = MIPS_ABI_UNKNOWN;
5791       break;
5792     }
5793
5794   /* GCC creates a pseudo-section whose name describes the ABI.  */
5795   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
5796     bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
5797
5798   /* If we have no useful BFD information, use the ABI from the last
5799      MIPS architecture (if there is one).  */
5800   if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
5801     found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
5802
5803   /* Try the architecture for any hint of the correct ABI.  */
5804   if (found_abi == MIPS_ABI_UNKNOWN
5805       && info.bfd_arch_info != NULL
5806       && info.bfd_arch_info->arch == bfd_arch_mips)
5807     {
5808       switch (info.bfd_arch_info->mach)
5809         {
5810         case bfd_mach_mips3900:
5811           found_abi = MIPS_ABI_EABI32;
5812           break;
5813         case bfd_mach_mips4100:
5814         case bfd_mach_mips5000:
5815           found_abi = MIPS_ABI_EABI64;
5816           break;
5817         case bfd_mach_mips8000:
5818         case bfd_mach_mips10000:
5819           /* On Irix, ELF64 executables use the N64 ABI.  The
5820              pseudo-sections which describe the ABI aren't present
5821              on IRIX.  (Even for executables created by gcc.)  */
5822           if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
5823               && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5824             found_abi = MIPS_ABI_N64;
5825           else
5826             found_abi = MIPS_ABI_N32;
5827           break;
5828         }
5829     }
5830
5831   /* Default 64-bit objects to N64 instead of O32.  */
5832   if (found_abi == MIPS_ABI_UNKNOWN
5833       && info.abfd != NULL
5834       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
5835       && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5836     found_abi = MIPS_ABI_N64;
5837
5838   if (gdbarch_debug)
5839     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
5840                         found_abi);
5841
5842   /* What has the user specified from the command line?  */
5843   wanted_abi = global_mips_abi ();
5844   if (gdbarch_debug)
5845     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
5846                         wanted_abi);
5847
5848   /* Now that we have found what the ABI for this binary would be,
5849      check whether the user is overriding it.  */
5850   if (wanted_abi != MIPS_ABI_UNKNOWN)
5851     mips_abi = wanted_abi;
5852   else if (found_abi != MIPS_ABI_UNKNOWN)
5853     mips_abi = found_abi;
5854   else
5855     mips_abi = MIPS_ABI_O32;
5856   if (gdbarch_debug)
5857     fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
5858                         mips_abi);
5859
5860   /* Also used when doing an architecture lookup.  */
5861   if (gdbarch_debug)
5862     fprintf_unfiltered (gdb_stdlog,
5863                         "mips_gdbarch_init: "
5864                         "mips64_transfers_32bit_regs_p = %d\n",
5865                         mips64_transfers_32bit_regs_p);
5866
5867   /* Determine the MIPS FPU type.  */
5868 #ifdef HAVE_ELF
5869   if (info.abfd
5870       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
5871     elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
5872                                              Tag_GNU_MIPS_ABI_FP);
5873 #endif /* HAVE_ELF */
5874
5875   if (!mips_fpu_type_auto)
5876     fpu_type = mips_fpu_type;
5877   else if (elf_fpu_type != 0)
5878     {
5879       switch (elf_fpu_type)
5880         {
5881         case 1:
5882           fpu_type = MIPS_FPU_DOUBLE;
5883           break;
5884         case 2:
5885           fpu_type = MIPS_FPU_SINGLE;
5886           break;
5887         case 3:
5888         default:
5889           /* Soft float or unknown.  */
5890           fpu_type = MIPS_FPU_NONE;
5891           break;
5892         }
5893     }
5894   else if (info.bfd_arch_info != NULL
5895            && info.bfd_arch_info->arch == bfd_arch_mips)
5896     switch (info.bfd_arch_info->mach)
5897       {
5898       case bfd_mach_mips3900:
5899       case bfd_mach_mips4100:
5900       case bfd_mach_mips4111:
5901       case bfd_mach_mips4120:
5902         fpu_type = MIPS_FPU_NONE;
5903         break;
5904       case bfd_mach_mips4650:
5905         fpu_type = MIPS_FPU_SINGLE;
5906         break;
5907       default:
5908         fpu_type = MIPS_FPU_DOUBLE;
5909         break;
5910       }
5911   else if (arches != NULL)
5912     fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
5913   else
5914     fpu_type = MIPS_FPU_DOUBLE;
5915   if (gdbarch_debug)
5916     fprintf_unfiltered (gdb_stdlog,
5917                         "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
5918
5919   /* Check for blatant incompatibilities.  */
5920
5921   /* If we have only 32-bit registers, then we can't debug a 64-bit
5922      ABI.  */
5923   if (info.target_desc
5924       && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
5925       && mips_abi != MIPS_ABI_EABI32
5926       && mips_abi != MIPS_ABI_O32)
5927     {
5928       if (tdesc_data != NULL)
5929         tdesc_data_cleanup (tdesc_data);
5930       return NULL;
5931     }
5932
5933   /* Try to find a pre-existing architecture.  */
5934   for (arches = gdbarch_list_lookup_by_info (arches, &info);
5935        arches != NULL;
5936        arches = gdbarch_list_lookup_by_info (arches->next, &info))
5937     {
5938       /* MIPS needs to be pedantic about which ABI the object is
5939          using.  */
5940       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
5941         continue;
5942       if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
5943         continue;
5944       /* Need to be pedantic about which register virtual size is
5945          used.  */
5946       if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
5947           != mips64_transfers_32bit_regs_p)
5948         continue;
5949       /* Be pedantic about which FPU is selected.  */
5950       if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
5951         continue;
5952
5953       if (tdesc_data != NULL)
5954         tdesc_data_cleanup (tdesc_data);
5955       return arches->gdbarch;
5956     }
5957
5958   /* Need a new architecture.  Fill in a target specific vector.  */
5959   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
5960   gdbarch = gdbarch_alloc (&info, tdep);
5961   tdep->elf_flags = elf_flags;
5962   tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
5963   tdep->found_abi = found_abi;
5964   tdep->mips_abi = mips_abi;
5965   tdep->mips_fpu_type = fpu_type;
5966   tdep->register_size_valid_p = 0;
5967   tdep->register_size = 0;
5968   tdep->gregset = NULL;
5969   tdep->gregset64 = NULL;
5970   tdep->fpregset = NULL;
5971   tdep->fpregset64 = NULL;
5972
5973   if (info.target_desc)
5974     {
5975       /* Some useful properties can be inferred from the target.  */
5976       if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
5977         {
5978           tdep->register_size_valid_p = 1;
5979           tdep->register_size = 4;
5980         }
5981       else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
5982         {
5983           tdep->register_size_valid_p = 1;
5984           tdep->register_size = 8;
5985         }
5986     }
5987
5988   /* Initially set everything according to the default ABI/ISA.  */
5989   set_gdbarch_short_bit (gdbarch, 16);
5990   set_gdbarch_int_bit (gdbarch, 32);
5991   set_gdbarch_float_bit (gdbarch, 32);
5992   set_gdbarch_double_bit (gdbarch, 64);
5993   set_gdbarch_long_double_bit (gdbarch, 64);
5994   set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
5995   set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
5996   set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
5997
5998   set_gdbarch_ax_pseudo_register_collect (gdbarch,
5999                                           mips_ax_pseudo_register_collect);
6000   set_gdbarch_ax_pseudo_register_push_stack
6001       (gdbarch, mips_ax_pseudo_register_push_stack);
6002
6003   set_gdbarch_elf_make_msymbol_special (gdbarch,
6004                                         mips_elf_make_msymbol_special);
6005
6006   /* Fill in the OS dependant register numbers and names.  */
6007   {
6008     const char **reg_names;
6009     struct mips_regnum *regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch,
6010                                                          struct mips_regnum);
6011     if (tdesc_has_registers (info.target_desc))
6012       {
6013         regnum->lo = MIPS_EMBED_LO_REGNUM;
6014         regnum->hi = MIPS_EMBED_HI_REGNUM;
6015         regnum->badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
6016         regnum->cause = MIPS_EMBED_CAUSE_REGNUM;
6017         regnum->pc = MIPS_EMBED_PC_REGNUM;
6018         regnum->fp0 = MIPS_EMBED_FP0_REGNUM;
6019         regnum->fp_control_status = 70;
6020         regnum->fp_implementation_revision = 71;
6021         num_regs = MIPS_LAST_EMBED_REGNUM + 1;
6022         reg_names = NULL;
6023       }
6024     else if (info.osabi == GDB_OSABI_IRIX)
6025       {
6026         regnum->fp0 = 32;
6027         regnum->pc = 64;
6028         regnum->cause = 65;
6029         regnum->badvaddr = 66;
6030         regnum->hi = 67;
6031         regnum->lo = 68;
6032         regnum->fp_control_status = 69;
6033         regnum->fp_implementation_revision = 70;
6034         num_regs = 71;
6035         reg_names = mips_irix_reg_names;
6036       }
6037     else
6038       {
6039         regnum->lo = MIPS_EMBED_LO_REGNUM;
6040         regnum->hi = MIPS_EMBED_HI_REGNUM;
6041         regnum->badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
6042         regnum->cause = MIPS_EMBED_CAUSE_REGNUM;
6043         regnum->pc = MIPS_EMBED_PC_REGNUM;
6044         regnum->fp0 = MIPS_EMBED_FP0_REGNUM;
6045         regnum->fp_control_status = 70;
6046         regnum->fp_implementation_revision = 71;
6047         num_regs = 90;
6048         if (info.bfd_arch_info != NULL
6049             && info.bfd_arch_info->mach == bfd_mach_mips3900)
6050           reg_names = mips_tx39_reg_names;
6051         else
6052           reg_names = mips_generic_reg_names;
6053       }
6054     /* FIXME: cagney/2003-11-15: For MIPS, hasn't gdbarch_pc_regnum been
6055        replaced by gdbarch_read_pc?  */
6056     set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
6057     set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
6058     set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
6059     set_gdbarch_num_regs (gdbarch, num_regs);
6060     set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
6061     set_gdbarch_register_name (gdbarch, mips_register_name);
6062     set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
6063     tdep->mips_processor_reg_names = reg_names;
6064     tdep->regnum = regnum;
6065   }
6066
6067   switch (mips_abi)
6068     {
6069     case MIPS_ABI_O32:
6070       set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
6071       set_gdbarch_return_value (gdbarch, mips_o32_return_value);
6072       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
6073       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
6074       tdep->default_mask_address_p = 0;
6075       set_gdbarch_long_bit (gdbarch, 32);
6076       set_gdbarch_ptr_bit (gdbarch, 32);
6077       set_gdbarch_long_long_bit (gdbarch, 64);
6078       break;
6079     case MIPS_ABI_O64:
6080       set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
6081       set_gdbarch_return_value (gdbarch, mips_o64_return_value);
6082       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
6083       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
6084       tdep->default_mask_address_p = 0;
6085       set_gdbarch_long_bit (gdbarch, 32);
6086       set_gdbarch_ptr_bit (gdbarch, 32);
6087       set_gdbarch_long_long_bit (gdbarch, 64);
6088       break;
6089     case MIPS_ABI_EABI32:
6090       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
6091       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
6092       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
6093       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
6094       tdep->default_mask_address_p = 0;
6095       set_gdbarch_long_bit (gdbarch, 32);
6096       set_gdbarch_ptr_bit (gdbarch, 32);
6097       set_gdbarch_long_long_bit (gdbarch, 64);
6098       break;
6099     case MIPS_ABI_EABI64:
6100       set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
6101       set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
6102       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
6103       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
6104       tdep->default_mask_address_p = 0;
6105       set_gdbarch_long_bit (gdbarch, 64);
6106       set_gdbarch_ptr_bit (gdbarch, 64);
6107       set_gdbarch_long_long_bit (gdbarch, 64);
6108       break;
6109     case MIPS_ABI_N32:
6110       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
6111       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
6112       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
6113       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
6114       tdep->default_mask_address_p = 0;
6115       set_gdbarch_long_bit (gdbarch, 32);
6116       set_gdbarch_ptr_bit (gdbarch, 32);
6117       set_gdbarch_long_long_bit (gdbarch, 64);
6118       set_gdbarch_long_double_bit (gdbarch, 128);
6119       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
6120       break;
6121     case MIPS_ABI_N64:
6122       set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
6123       set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
6124       tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
6125       tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
6126       tdep->default_mask_address_p = 0;
6127       set_gdbarch_long_bit (gdbarch, 64);
6128       set_gdbarch_ptr_bit (gdbarch, 64);
6129       set_gdbarch_long_long_bit (gdbarch, 64);
6130       set_gdbarch_long_double_bit (gdbarch, 128);
6131       set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
6132       break;
6133     default:
6134       internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
6135     }
6136
6137   /* GCC creates a pseudo-section whose name specifies the size of
6138      longs, since -mlong32 or -mlong64 may be used independent of
6139      other options.  How those options affect pointer sizes is ABI and
6140      architecture dependent, so use them to override the default sizes
6141      set by the ABI.  This table shows the relationship between ABI,
6142      -mlongXX, and size of pointers:
6143
6144      ABI                -mlongXX        ptr bits
6145      ---                --------        --------
6146      o32                32              32
6147      o32                64              32
6148      n32                32              32
6149      n32                64              64
6150      o64                32              32
6151      o64                64              64
6152      n64                32              32
6153      n64                64              64
6154      eabi32             32              32
6155      eabi32             64              32
6156      eabi64             32              32
6157      eabi64             64              64
6158
6159     Note that for o32 and eabi32, pointers are always 32 bits
6160     regardless of any -mlongXX option.  For all others, pointers and
6161     longs are the same, as set by -mlongXX or set by defaults.  */
6162
6163   if (info.abfd != NULL)
6164     {
6165       int long_bit = 0;
6166
6167       bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
6168       if (long_bit)
6169         {
6170           set_gdbarch_long_bit (gdbarch, long_bit);
6171           switch (mips_abi)
6172             {
6173             case MIPS_ABI_O32:
6174             case MIPS_ABI_EABI32:
6175               break;
6176             case MIPS_ABI_N32:
6177             case MIPS_ABI_O64:
6178             case MIPS_ABI_N64:
6179             case MIPS_ABI_EABI64:
6180               set_gdbarch_ptr_bit (gdbarch, long_bit);
6181               break;
6182             default:
6183               internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
6184             }
6185         }
6186     }
6187
6188   /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
6189      that could indicate -gp32 BUT gas/config/tc-mips.c contains the
6190      comment:
6191
6192      ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
6193      flag in object files because to do so would make it impossible to
6194      link with libraries compiled without "-gp32".  This is
6195      unnecessarily restrictive.
6196
6197      We could solve this problem by adding "-gp32" multilibs to gcc,
6198      but to set this flag before gcc is built with such multilibs will
6199      break too many systems.''
6200
6201      But even more unhelpfully, the default linker output target for
6202      mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
6203      for 64-bit programs - you need to change the ABI to change this,
6204      and not all gcc targets support that currently.  Therefore using
6205      this flag to detect 32-bit mode would do the wrong thing given
6206      the current gcc - it would make GDB treat these 64-bit programs
6207      as 32-bit programs by default.  */
6208
6209   set_gdbarch_read_pc (gdbarch, mips_read_pc);
6210   set_gdbarch_write_pc (gdbarch, mips_write_pc);
6211
6212   /* Add/remove bits from an address.  The MIPS needs be careful to
6213      ensure that all 32 bit addresses are sign extended to 64 bits.  */
6214   set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
6215
6216   /* Unwind the frame.  */
6217   set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
6218   set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
6219   set_gdbarch_dummy_id (gdbarch, mips_dummy_id);
6220
6221   /* Map debug register numbers onto internal register numbers.  */
6222   set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
6223   set_gdbarch_ecoff_reg_to_regnum (gdbarch,
6224                                    mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6225   set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
6226                                     mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6227   set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
6228
6229   /* MIPS version of CALL_DUMMY.  */
6230
6231   /* NOTE: cagney/2003-08-05: Eventually call dummy location will be
6232      replaced by a command, and all targets will default to on stack
6233      (regardless of the stack's execute status).  */
6234   set_gdbarch_call_dummy_location (gdbarch, AT_SYMBOL);
6235   set_gdbarch_frame_align (gdbarch, mips_frame_align);
6236
6237   set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
6238   set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
6239   set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
6240
6241   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
6242   set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
6243
6244   set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
6245
6246   set_gdbarch_in_function_epilogue_p (gdbarch, mips_in_function_epilogue_p);
6247
6248   set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
6249   set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
6250   set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
6251
6252   set_gdbarch_register_type (gdbarch, mips_register_type);
6253
6254   set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
6255
6256   if (mips_abi == MIPS_ABI_N32)
6257     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
6258   else if (mips_abi == MIPS_ABI_N64)
6259     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
6260   else
6261     set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
6262
6263   /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
6264      HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
6265      need to all be folded into the target vector.  Since they are
6266      being used as guards for target_stopped_by_watchpoint, why not have
6267      target_stopped_by_watchpoint return the type of watchpoint that the code
6268      is sitting on?  */
6269   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
6270
6271   set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
6272
6273   set_gdbarch_single_step_through_delay (gdbarch,
6274                                          mips_single_step_through_delay);
6275
6276   /* Virtual tables.  */
6277   set_gdbarch_vbit_in_delta (gdbarch, 1);
6278
6279   mips_register_g_packet_guesses (gdbarch);
6280
6281   /* Hook in OS ABI-specific overrides, if they have been registered.  */
6282   info.tdep_info = (void *) tdesc_data;
6283   gdbarch_init_osabi (info, gdbarch);
6284
6285   /* Unwind the frame.  */
6286   dwarf2_append_unwinders (gdbarch);
6287   frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
6288   frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
6289   frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
6290   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
6291   frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
6292   frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
6293   frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
6294
6295   if (tdesc_data)
6296     {
6297       set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
6298       tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
6299
6300       /* Override the normal target description methods to handle our
6301          dual real and pseudo registers.  */
6302       set_gdbarch_register_name (gdbarch, mips_register_name);
6303       set_gdbarch_register_reggroup_p (gdbarch,
6304                                        mips_tdesc_register_reggroup_p);
6305
6306       num_regs = gdbarch_num_regs (gdbarch);
6307       set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
6308       set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
6309       set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
6310     }
6311
6312   /* Add ABI-specific aliases for the registers.  */
6313   if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
6314     for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
6315       user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
6316                     value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
6317   else
6318     for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
6319       user_reg_add (gdbarch, mips_o32_aliases[i].name,
6320                     value_of_mips_user_reg, &mips_o32_aliases[i].regnum);
6321
6322   /* Add some other standard aliases.  */
6323   for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
6324     user_reg_add (gdbarch, mips_register_aliases[i].name,
6325                   value_of_mips_user_reg, &mips_register_aliases[i].regnum);
6326
6327   for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
6328     user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
6329                   value_of_mips_user_reg, 
6330                   &mips_numeric_register_aliases[i].regnum);
6331
6332   return gdbarch;
6333 }
6334
6335 static void
6336 mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
6337 {
6338   struct gdbarch_info info;
6339
6340   /* Force the architecture to update, and (if it's a MIPS architecture)
6341      mips_gdbarch_init will take care of the rest.  */
6342   gdbarch_info_init (&info);
6343   gdbarch_update_p (info);
6344 }
6345
6346 /* Print out which MIPS ABI is in use.  */
6347
6348 static void
6349 show_mips_abi (struct ui_file *file,
6350                int from_tty,
6351                struct cmd_list_element *ignored_cmd,
6352                const char *ignored_value)
6353 {
6354   if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_mips)
6355     fprintf_filtered
6356       (file, 
6357        "The MIPS ABI is unknown because the current architecture "
6358        "is not MIPS.\n");
6359   else
6360     {
6361       enum mips_abi global_abi = global_mips_abi ();
6362       enum mips_abi actual_abi = mips_abi (target_gdbarch);
6363       const char *actual_abi_str = mips_abi_strings[actual_abi];
6364
6365       if (global_abi == MIPS_ABI_UNKNOWN)
6366         fprintf_filtered
6367           (file, 
6368            "The MIPS ABI is set automatically (currently \"%s\").\n",
6369            actual_abi_str);
6370       else if (global_abi == actual_abi)
6371         fprintf_filtered
6372           (file,
6373            "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
6374            actual_abi_str);
6375       else
6376         {
6377           /* Probably shouldn't happen...  */
6378           fprintf_filtered (file,
6379                             "The (auto detected) MIPS ABI \"%s\" is in use "
6380                             "even though the user setting was \"%s\".\n",
6381              actual_abi_str, mips_abi_strings[global_abi]);
6382         }
6383     }
6384 }
6385
6386 static void
6387 mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
6388 {
6389   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6390   if (tdep != NULL)
6391     {
6392       int ef_mips_arch;
6393       int ef_mips_32bitmode;
6394       /* Determine the ISA.  */
6395       switch (tdep->elf_flags & EF_MIPS_ARCH)
6396         {
6397         case E_MIPS_ARCH_1:
6398           ef_mips_arch = 1;
6399           break;
6400         case E_MIPS_ARCH_2:
6401           ef_mips_arch = 2;
6402           break;
6403         case E_MIPS_ARCH_3:
6404           ef_mips_arch = 3;
6405           break;
6406         case E_MIPS_ARCH_4:
6407           ef_mips_arch = 4;
6408           break;
6409         default:
6410           ef_mips_arch = 0;
6411           break;
6412         }
6413       /* Determine the size of a pointer.  */
6414       ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
6415       fprintf_unfiltered (file,
6416                           "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
6417                           tdep->elf_flags);
6418       fprintf_unfiltered (file,
6419                           "mips_dump_tdep: ef_mips_32bitmode = %d\n",
6420                           ef_mips_32bitmode);
6421       fprintf_unfiltered (file,
6422                           "mips_dump_tdep: ef_mips_arch = %d\n",
6423                           ef_mips_arch);
6424       fprintf_unfiltered (file,
6425                           "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
6426                           tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
6427       fprintf_unfiltered (file,
6428                           "mips_dump_tdep: "
6429                           "mips_mask_address_p() %d (default %d)\n",
6430                           mips_mask_address_p (tdep),
6431                           tdep->default_mask_address_p);
6432     }
6433   fprintf_unfiltered (file,
6434                       "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
6435                       MIPS_DEFAULT_FPU_TYPE,
6436                       (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
6437                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
6438                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
6439                        : "???"));
6440   fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
6441                       MIPS_EABI (gdbarch));
6442   fprintf_unfiltered (file,
6443                       "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
6444                       MIPS_FPU_TYPE (gdbarch),
6445                       (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
6446                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
6447                        : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
6448                        : "???"));
6449 }
6450
6451 extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
6452
6453 void
6454 _initialize_mips_tdep (void)
6455 {
6456   static struct cmd_list_element *mipsfpulist = NULL;
6457   struct cmd_list_element *c;
6458
6459   mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
6460   if (MIPS_ABI_LAST + 1
6461       != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
6462     internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
6463
6464   gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
6465
6466   mips_pdr_data = register_objfile_data ();
6467
6468   /* Create feature sets with the appropriate properties.  The values
6469      are not important.  */
6470   mips_tdesc_gp32 = allocate_target_description ();
6471   set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");
6472
6473   mips_tdesc_gp64 = allocate_target_description ();
6474   set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");
6475
6476   /* Add root prefix command for all "set mips"/"show mips" commands.  */
6477   add_prefix_cmd ("mips", no_class, set_mips_command,
6478                   _("Various MIPS specific commands."),
6479                   &setmipscmdlist, "set mips ", 0, &setlist);
6480
6481   add_prefix_cmd ("mips", no_class, show_mips_command,
6482                   _("Various MIPS specific commands."),
6483                   &showmipscmdlist, "show mips ", 0, &showlist);
6484
6485   /* Allow the user to override the ABI.  */
6486   add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
6487                         &mips_abi_string, _("\
6488 Set the MIPS ABI used by this program."), _("\
6489 Show the MIPS ABI used by this program."), _("\
6490 This option can be set to one of:\n\
6491   auto  - the default ABI associated with the current binary\n\
6492   o32\n\
6493   o64\n\
6494   n32\n\
6495   n64\n\
6496   eabi32\n\
6497   eabi64"),
6498                         mips_abi_update,
6499                         show_mips_abi,
6500                         &setmipscmdlist, &showmipscmdlist);
6501
6502   /* Let the user turn off floating point and set the fence post for
6503      heuristic_proc_start.  */
6504
6505   add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
6506                   _("Set use of MIPS floating-point coprocessor."),
6507                   &mipsfpulist, "set mipsfpu ", 0, &setlist);
6508   add_cmd ("single", class_support, set_mipsfpu_single_command,
6509            _("Select single-precision MIPS floating-point coprocessor."),
6510            &mipsfpulist);
6511   add_cmd ("double", class_support, set_mipsfpu_double_command,
6512            _("Select double-precision MIPS floating-point coprocessor."),
6513            &mipsfpulist);
6514   add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
6515   add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
6516   add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
6517   add_cmd ("none", class_support, set_mipsfpu_none_command,
6518            _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
6519   add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
6520   add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
6521   add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
6522   add_cmd ("auto", class_support, set_mipsfpu_auto_command,
6523            _("Select MIPS floating-point coprocessor automatically."),
6524            &mipsfpulist);
6525   add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
6526            _("Show current use of MIPS floating-point coprocessor target."),
6527            &showlist);
6528
6529   /* We really would like to have both "0" and "unlimited" work, but
6530      command.c doesn't deal with that.  So make it a var_zinteger
6531      because the user can always use "999999" or some such for unlimited.  */
6532   add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
6533                             &heuristic_fence_post, _("\
6534 Set the distance searched for the start of a function."), _("\
6535 Show the distance searched for the start of a function."), _("\
6536 If you are debugging a stripped executable, GDB needs to search through the\n\
6537 program for the start of a function.  This command sets the distance of the\n\
6538 search.  The only need to set it is when debugging a stripped executable."),
6539                             reinit_frame_cache_sfunc,
6540                             NULL, /* FIXME: i18n: The distance searched for
6541                                      the start of a function is %s.  */
6542                             &setlist, &showlist);
6543
6544   /* Allow the user to control whether the upper bits of 64-bit
6545      addresses should be zeroed.  */
6546   add_setshow_auto_boolean_cmd ("mask-address", no_class,
6547                                 &mask_address_var, _("\
6548 Set zeroing of upper 32 bits of 64-bit addresses."), _("\
6549 Show zeroing of upper 32 bits of 64-bit addresses."), _("\
6550 Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
6551 allow GDB to determine the correct value."),
6552                                 NULL, show_mask_address,
6553                                 &setmipscmdlist, &showmipscmdlist);
6554
6555   /* Allow the user to control the size of 32 bit registers within the
6556      raw remote packet.  */
6557   add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
6558                            &mips64_transfers_32bit_regs_p, _("\
6559 Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
6560                            _("\
6561 Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
6562                            _("\
6563 Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
6564 that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
6565 64 bits for others.  Use \"off\" to disable compatibility mode"),
6566                            set_mips64_transfers_32bit_regs,
6567                            NULL, /* FIXME: i18n: Compatibility with 64-bit
6568                                     MIPS target that transfers 32-bit
6569                                     quantities is %s.  */
6570                            &setlist, &showlist);
6571
6572   /* Debug this files internals.  */
6573   add_setshow_zinteger_cmd ("mips", class_maintenance,
6574                             &mips_debug, _("\
6575 Set mips debugging."), _("\
6576 Show mips debugging."), _("\
6577 When non-zero, mips specific debugging is enabled."),
6578                             NULL,
6579                             NULL, /* FIXME: i18n: Mips debugging is
6580                                      currently %s.  */
6581                             &setdebuglist, &showdebuglist);
6582 }