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