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