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