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