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