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