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