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