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