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