OSDN Git Service

67f516b87fe62077f6b31b84cc714d66cf5a5b0f
[pf3gnuchains/pf3gnuchains3x.git] / gdb / mips-tdep.c
1 /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
2
3    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4    1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
7    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
8
9    This file is part of GDB.
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 59 Temple Place - Suite 330,
24    Boston, MA 02111-1307, USA.  */
25
26 #include "defs.h"
27 #include "gdb_string.h"
28 #include "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
627    the ``FCRCS_REGNUM <= reg && reg <= LAST_EMBED_REGNUM'' clause
628    though.  In some cases 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 ADDR_BITS_REMOVE (read_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         ? 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_quietly, but with this test, at least we
1822            don't print out warnings for every child forked (eg, on
1823            decstation).  22apr93 rich@cygnus.com.  */
1824         if (!stop_soon_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 ADDR_BITS_REMOVE (read_next_frame_reg (frame, 
2446                                                 PROC_FRAME_REG (proc_desc)) +
2447                            PROC_FRAME_OFFSET (proc_desc) - 
2448                            PROC_FRAME_ADJUST (proc_desc));
2449 }
2450
2451 static mips_extra_func_info_t cached_proc_desc;
2452
2453 static CORE_ADDR
2454 mips_frame_chain (struct frame_info *frame)
2455 {
2456   mips_extra_func_info_t proc_desc;
2457   CORE_ADDR tmp;
2458   CORE_ADDR saved_pc = DEPRECATED_FRAME_SAVED_PC (frame);
2459
2460   if (saved_pc == 0 || inside_entry_file (saved_pc))
2461     return 0;
2462
2463   /* Check if the PC is inside a call stub.  If it is, fetch the
2464      PC of the caller of that stub.  */
2465   if ((tmp = SKIP_TRAMPOLINE_CODE (saved_pc)) != 0)
2466     saved_pc = tmp;
2467
2468   if (DEPRECATED_PC_IN_CALL_DUMMY (saved_pc, 0, 0))
2469     {
2470       /* A dummy frame, uses SP not FP.  Get the old SP value.  If all
2471          is well, frame->frame the bottom of the current frame will
2472          contain that value.  */
2473       return get_frame_base (frame);
2474     }
2475
2476   /* Look up the procedure descriptor for this PC.  */
2477   proc_desc = find_proc_desc (saved_pc, frame, 1);
2478   if (!proc_desc)
2479     return 0;
2480
2481   cached_proc_desc = proc_desc;
2482
2483   /* If no frame pointer and frame size is zero, we must be at end
2484      of stack (or otherwise hosed).  If we don't check frame size,
2485      we loop forever if we see a zero size frame.  */
2486   if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
2487       && PROC_FRAME_OFFSET (proc_desc) == 0
2488       /* The previous frame from a sigtramp frame might be frameless
2489          and have frame size zero.  */
2490       && !(get_frame_type (frame) == SIGTRAMP_FRAME)
2491       /* For a generic dummy frame, let get_frame_pointer() unwind a
2492          register value saved as part of the dummy frame call.  */
2493       && !(DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame), 0, 0)))
2494     return 0;
2495   else
2496     return get_frame_pointer (frame, proc_desc);
2497 }
2498
2499 static void
2500 mips_init_extra_frame_info (int fromleaf, struct frame_info *fci)
2501 {
2502   int regnum;
2503   mips_extra_func_info_t proc_desc;
2504
2505   if (get_frame_type (fci) == DUMMY_FRAME)
2506     return;
2507
2508   /* Use proc_desc calculated in frame_chain.  When there is no
2509      next frame, i.e, get_next_frame (fci) == NULL, we call
2510      find_proc_desc () to calculate it, passing an explicit
2511      NULL as the frame parameter.  */
2512   proc_desc =
2513     get_next_frame (fci)
2514     ? cached_proc_desc
2515     : find_proc_desc (get_frame_pc (fci),
2516                       NULL /* i.e, get_next_frame (fci) */,
2517                       1);
2518
2519   frame_extra_info_zalloc (fci, sizeof (struct frame_extra_info));
2520
2521   deprecated_set_frame_saved_regs_hack (fci, NULL);
2522   get_frame_extra_info (fci)->proc_desc =
2523     proc_desc == &temp_proc_desc ? 0 : proc_desc;
2524   if (proc_desc)
2525     {
2526       /* Fixup frame-pointer - only needed for top frame */
2527       /* This may not be quite right, if proc has a real frame register.
2528          Get the value of the frame relative sp, procedure might have been
2529          interrupted by a signal at it's very start.  */
2530       if (get_frame_pc (fci) == PROC_LOW_ADDR (proc_desc)
2531           && !PROC_DESC_IS_DUMMY (proc_desc))
2532         deprecated_update_frame_base_hack (fci, read_next_frame_reg (get_next_frame (fci), SP_REGNUM));
2533       else if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fci), 0, 0))
2534         /* Do not ``fix'' fci->frame.  It will have the value of the
2535            generic dummy frame's top-of-stack (since the draft
2536            fci->frame is obtained by returning the unwound stack
2537            pointer) and that is what we want.  That way the fci->frame
2538            value will match the top-of-stack value that was saved as
2539            part of the dummy frames data.  */
2540         /* Do nothing.  */;
2541       else
2542         deprecated_update_frame_base_hack (fci, get_frame_pointer (get_next_frame (fci), proc_desc));
2543
2544       if (proc_desc == &temp_proc_desc)
2545         {
2546           char *name;
2547
2548           /* Do not set the saved registers for a sigtramp frame,
2549              mips_find_saved_registers will do that for us.  We can't
2550              use (get_frame_type (fci) == SIGTRAMP_FRAME), it is not
2551              yet set.  */
2552           /* FIXME: cagney/2002-11-18: This problem will go away once
2553              frame.c:get_prev_frame() is modified to set the frame's
2554              type before calling functions like this.  */
2555           find_pc_partial_function (get_frame_pc (fci), &name,
2556                                     (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
2557           if (!PC_IN_SIGTRAMP (get_frame_pc (fci), name))
2558             {
2559               frame_saved_regs_zalloc (fci);
2560               memcpy (get_frame_saved_regs (fci), temp_saved_regs, SIZEOF_FRAME_SAVED_REGS);
2561               get_frame_saved_regs (fci)[PC_REGNUM]
2562                 = get_frame_saved_regs (fci)[RA_REGNUM];
2563               /* Set value of previous frame's stack pointer.  Remember that
2564                  saved_regs[SP_REGNUM] is special in that it contains the
2565                  value of the stack pointer register.  The other saved_regs
2566                  values are addresses (in the inferior) at which a given
2567                  register's value may be found.  */
2568               get_frame_saved_regs (fci)[SP_REGNUM] = get_frame_base (fci);
2569             }
2570         }
2571
2572       /* hack: if argument regs are saved, guess these contain args */
2573       /* assume we can't tell how many args for now */
2574       get_frame_extra_info (fci)->num_args = -1;
2575       for (regnum = MIPS_LAST_ARG_REGNUM; regnum >= A0_REGNUM; regnum--)
2576         {
2577           if (PROC_REG_MASK (proc_desc) & (1 << regnum))
2578             {
2579               get_frame_extra_info (fci)->num_args = regnum - A0_REGNUM + 1;
2580               break;
2581             }
2582         }
2583     }
2584 }
2585
2586 /* MIPS stack frames are almost impenetrable.  When execution stops,
2587    we basically have to look at symbol information for the function
2588    that we stopped in, which tells us *which* register (if any) is
2589    the base of the frame pointer, and what offset from that register
2590    the frame itself is at.
2591
2592    This presents a problem when trying to examine a stack in memory
2593    (that isn't executing at the moment), using the "frame" command.  We
2594    don't have a PC, nor do we have any registers except SP.
2595
2596    This routine takes two arguments, SP and PC, and tries to make the
2597    cached frames look as if these two arguments defined a frame on the
2598    cache.  This allows the rest of info frame to extract the important
2599    arguments without difficulty.  */
2600
2601 struct frame_info *
2602 setup_arbitrary_frame (int argc, CORE_ADDR *argv)
2603 {
2604   if (argc != 2)
2605     error ("MIPS frame specifications require two arguments: sp and pc");
2606
2607   return create_new_frame (argv[0], argv[1]);
2608 }
2609
2610 /* According to the current ABI, should the type be passed in a
2611    floating-point register (assuming that there is space)?  When there
2612    is no FPU, FP are not even considered as possibile candidates for
2613    FP registers and, consequently this returns false - forces FP
2614    arguments into integer registers. */
2615
2616 static int
2617 fp_register_arg_p (enum type_code typecode, struct type *arg_type)
2618 {
2619   return ((typecode == TYPE_CODE_FLT
2620            || (MIPS_EABI
2621                && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
2622                && TYPE_NFIELDS (arg_type) == 1
2623                && TYPE_CODE (TYPE_FIELD_TYPE (arg_type, 0)) == TYPE_CODE_FLT))
2624           && MIPS_FPU_TYPE != MIPS_FPU_NONE);
2625 }
2626
2627 /* On o32, argument passing in GPRs depends on the alignment of the type being
2628    passed.  Return 1 if this type must be aligned to a doubleword boundary. */
2629
2630 static int
2631 mips_type_needs_double_align (struct type *type)
2632 {
2633   enum type_code typecode = TYPE_CODE (type);
2634
2635   if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
2636     return 1;
2637   else if (typecode == TYPE_CODE_STRUCT)
2638     {
2639       if (TYPE_NFIELDS (type) < 1)
2640         return 0;
2641       return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
2642     }
2643   else if (typecode == TYPE_CODE_UNION)
2644     {
2645       int i, n;
2646
2647       n = TYPE_NFIELDS (type);
2648       for (i = 0; i < n; i++)
2649         if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
2650           return 1;
2651       return 0;
2652     }
2653   return 0;
2654 }
2655
2656 /* Macros to round N up or down to the next A boundary; 
2657    A must be a power of two.  */
2658
2659 #define ROUND_DOWN(n,a) ((n) & ~((a)-1))
2660 #define ROUND_UP(n,a) (((n)+(a)-1) & ~((a)-1))
2661
2662 /* Adjust the address downward (direction of stack growth) so that it
2663    is correctly aligned for a new stack frame.  */
2664 static CORE_ADDR
2665 mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2666 {
2667   return ROUND_DOWN (addr, 16);
2668 }
2669
2670 static CORE_ADDR
2671 mips_eabi_push_arguments (int nargs,
2672                           struct value **args,
2673                           CORE_ADDR sp,
2674                           int struct_return,
2675                           CORE_ADDR struct_addr)
2676 {
2677   int argreg;
2678   int float_argreg;
2679   int argnum;
2680   int len = 0;
2681   int stack_offset = 0;
2682
2683   /* First ensure that the stack and structure return address (if any)
2684      are properly aligned.  The stack has to be at least 64-bit
2685      aligned even on 32-bit machines, because doubles must be 64-bit
2686      aligned.  For n32 and n64, stack frames need to be 128-bit
2687      aligned, so we round to this widest known alignment.  */
2688
2689   sp = ROUND_DOWN (sp, 16);
2690   struct_addr = ROUND_DOWN (struct_addr, 16);
2691
2692   /* Now make space on the stack for the args.  We allocate more
2693      than necessary for EABI, because the first few arguments are
2694      passed in registers, but that's OK.  */
2695   for (argnum = 0; argnum < nargs; argnum++)
2696     len += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 
2697                      MIPS_STACK_ARGSIZE);
2698   sp -= ROUND_UP (len, 16);
2699
2700   if (mips_debug)
2701     fprintf_unfiltered (gdb_stdlog, 
2702                         "mips_eabi_push_arguments: sp=0x%s allocated %d\n",
2703                         paddr_nz (sp), ROUND_UP (len, 16));
2704
2705   /* Initialize the integer and float register pointers.  */
2706   argreg = A0_REGNUM;
2707   float_argreg = FPA0_REGNUM;
2708
2709   /* The struct_return pointer occupies the first parameter-passing reg.  */
2710   if (struct_return)
2711     {
2712       if (mips_debug)
2713         fprintf_unfiltered (gdb_stdlog,
2714                             "mips_eabi_push_arguments: struct_return reg=%d 0x%s\n",
2715                             argreg, paddr_nz (struct_addr));
2716       write_register (argreg++, struct_addr);
2717     }
2718
2719   /* Now load as many as possible of the first arguments into
2720      registers, and push the rest onto the stack.  Loop thru args
2721      from first to last.  */
2722   for (argnum = 0; argnum < nargs; argnum++)
2723     {
2724       char *val;
2725       char *valbuf = alloca (MAX_REGISTER_RAW_SIZE);
2726       struct value *arg = args[argnum];
2727       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
2728       int len = TYPE_LENGTH (arg_type);
2729       enum type_code typecode = TYPE_CODE (arg_type);
2730
2731       if (mips_debug)
2732         fprintf_unfiltered (gdb_stdlog,
2733                             "mips_eabi_push_arguments: %d len=%d type=%d",
2734                             argnum + 1, len, (int) typecode);
2735
2736       /* The EABI passes structures that do not fit in a register by
2737          reference.  */
2738       if (len > MIPS_SAVED_REGSIZE
2739           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
2740         {
2741           store_address (valbuf, MIPS_SAVED_REGSIZE, VALUE_ADDRESS (arg));
2742           typecode = TYPE_CODE_PTR;
2743           len = MIPS_SAVED_REGSIZE;
2744           val = valbuf;
2745           if (mips_debug)
2746             fprintf_unfiltered (gdb_stdlog, " push");
2747         }
2748       else
2749         val = (char *) VALUE_CONTENTS (arg);
2750
2751       /* 32-bit ABIs always start floating point arguments in an
2752          even-numbered floating point register.  Round the FP register
2753          up before the check to see if there are any FP registers
2754          left.  Non MIPS_EABI targets also pass the FP in the integer
2755          registers so also round up normal registers.  */
2756       if (!FP_REGISTER_DOUBLE
2757           && fp_register_arg_p (typecode, arg_type))
2758         {
2759           if ((float_argreg & 1))
2760             float_argreg++;
2761         }
2762
2763       /* Floating point arguments passed in registers have to be
2764          treated specially.  On 32-bit architectures, doubles
2765          are passed in register pairs; the even register gets
2766          the low word, and the odd register gets the high word.
2767          On non-EABI processors, the first two floating point arguments are
2768          also copied to general registers, because MIPS16 functions
2769          don't use float registers for arguments.  This duplication of
2770          arguments in general registers can't hurt non-MIPS16 functions
2771          because those registers are normally skipped.  */
2772       /* MIPS_EABI squeezes a struct that contains a single floating
2773          point value into an FP register instead of pushing it onto the
2774          stack.  */
2775       if (fp_register_arg_p (typecode, arg_type)
2776           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
2777         {
2778           if (!FP_REGISTER_DOUBLE && len == 8)
2779             {
2780               int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
2781               unsigned long regval;
2782
2783               /* Write the low word of the double to the even register(s).  */
2784               regval = extract_unsigned_integer (val + low_offset, 4);
2785               if (mips_debug)
2786                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
2787                                     float_argreg, phex (regval, 4));
2788               write_register (float_argreg++, regval);
2789
2790               /* Write the high word of the double to the odd register(s).  */
2791               regval = extract_unsigned_integer (val + 4 - low_offset, 4);
2792               if (mips_debug)
2793                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
2794                                     float_argreg, phex (regval, 4));
2795               write_register (float_argreg++, regval);
2796             }
2797           else
2798             {
2799               /* This is a floating point value that fits entirely
2800                  in a single register.  */
2801               /* On 32 bit ABI's the float_argreg is further adjusted
2802                  above to ensure that it is even register aligned.  */
2803               LONGEST regval = extract_unsigned_integer (val, len);
2804               if (mips_debug)
2805                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
2806                                     float_argreg, phex (regval, len));
2807               write_register (float_argreg++, regval);
2808             }
2809         }
2810       else
2811         {
2812           /* Copy the argument to general registers or the stack in
2813              register-sized pieces.  Large arguments are split between
2814              registers and stack.  */
2815           /* Note: structs whose size is not a multiple of MIPS_REGSIZE
2816              are treated specially: Irix cc passes them in registers
2817              where gcc sometimes puts them on the stack.  For maximum
2818              compatibility, we will put them in both places.  */
2819           int odd_sized_struct = ((len > MIPS_SAVED_REGSIZE) &&
2820                                   (len % MIPS_SAVED_REGSIZE != 0));
2821
2822           /* Note: Floating-point values that didn't fit into an FP
2823              register are only written to memory.  */
2824           while (len > 0)
2825             {
2826               /* Remember if the argument was written to the stack.  */
2827               int stack_used_p = 0;
2828               int partial_len = 
2829                 len < MIPS_SAVED_REGSIZE ? len : MIPS_SAVED_REGSIZE;
2830
2831               if (mips_debug)
2832                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
2833                                     partial_len);
2834
2835               /* Write this portion of the argument to the stack.  */
2836               if (argreg > MIPS_LAST_ARG_REGNUM
2837                   || odd_sized_struct
2838                   || fp_register_arg_p (typecode, arg_type))
2839                 {
2840                   /* Should shorter than int integer values be
2841                      promoted to int before being stored? */
2842                   int longword_offset = 0;
2843                   CORE_ADDR addr;
2844                   stack_used_p = 1;
2845                   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
2846                     {
2847                       if (MIPS_STACK_ARGSIZE == 8 &&
2848                           (typecode == TYPE_CODE_INT ||
2849                            typecode == TYPE_CODE_PTR ||
2850                            typecode == TYPE_CODE_FLT) && len <= 4)
2851                         longword_offset = MIPS_STACK_ARGSIZE - len;
2852                       else if ((typecode == TYPE_CODE_STRUCT ||
2853                                 typecode == TYPE_CODE_UNION) &&
2854                                TYPE_LENGTH (arg_type) < MIPS_STACK_ARGSIZE)
2855                         longword_offset = MIPS_STACK_ARGSIZE - len;
2856                     }
2857
2858                   if (mips_debug)
2859                     {
2860                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
2861                                           paddr_nz (stack_offset));
2862                       fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
2863                                           paddr_nz (longword_offset));
2864                     }
2865
2866                   addr = sp + stack_offset + longword_offset;
2867
2868                   if (mips_debug)
2869                     {
2870                       int i;
2871                       fprintf_unfiltered (gdb_stdlog, " @0x%s ", 
2872                                           paddr_nz (addr));
2873                       for (i = 0; i < partial_len; i++)
2874                         {
2875                           fprintf_unfiltered (gdb_stdlog, "%02x", 
2876                                               val[i] & 0xff);
2877                         }
2878                     }
2879                   write_memory (addr, val, partial_len);
2880                 }
2881
2882               /* Note!!! This is NOT an else clause.  Odd sized
2883                  structs may go thru BOTH paths.  Floating point
2884                  arguments will not.  */
2885               /* Write this portion of the argument to a general
2886                  purpose register.  */
2887               if (argreg <= MIPS_LAST_ARG_REGNUM
2888                   && !fp_register_arg_p (typecode, arg_type))
2889                 {
2890                   LONGEST regval = extract_unsigned_integer (val, partial_len);
2891
2892                   if (mips_debug)
2893                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
2894                                       argreg,
2895                                       phex (regval, MIPS_SAVED_REGSIZE));
2896                   write_register (argreg, regval);
2897                   argreg++;
2898                 }
2899
2900               len -= partial_len;
2901               val += partial_len;
2902
2903               /* Compute the the offset into the stack at which we
2904                  will copy the next parameter.
2905
2906                  In the new EABI (and the NABI32), the stack_offset
2907                  only needs to be adjusted when it has been used.  */
2908
2909               if (stack_used_p)
2910                 stack_offset += ROUND_UP (partial_len, MIPS_STACK_ARGSIZE);
2911             }
2912         }
2913       if (mips_debug)
2914         fprintf_unfiltered (gdb_stdlog, "\n");
2915     }
2916
2917   /* Return adjusted stack pointer.  */
2918   return sp;
2919 }
2920
2921 /* N32/N64 version of push_arguments.  */
2922
2923 static CORE_ADDR
2924 mips_n32n64_push_arguments (int nargs,
2925                             struct value **args,
2926                             CORE_ADDR sp,
2927                             int struct_return,
2928                             CORE_ADDR struct_addr)
2929 {
2930   int argreg;
2931   int float_argreg;
2932   int argnum;
2933   int len = 0;
2934   int stack_offset = 0;
2935
2936   /* First ensure that the stack and structure return address (if any)
2937      are properly aligned.  The stack has to be at least 64-bit
2938      aligned even on 32-bit machines, because doubles must be 64-bit
2939      aligned.  For n32 and n64, stack frames need to be 128-bit
2940      aligned, so we round to this widest known alignment.  */
2941
2942   sp = ROUND_DOWN (sp, 16);
2943   struct_addr = ROUND_DOWN (struct_addr, 16);
2944
2945   /* Now make space on the stack for the args.  */
2946   for (argnum = 0; argnum < nargs; argnum++)
2947     len += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 
2948                      MIPS_STACK_ARGSIZE);
2949   sp -= ROUND_UP (len, 16);
2950
2951   if (mips_debug)
2952     fprintf_unfiltered (gdb_stdlog, 
2953                         "mips_n32n64_push_arguments: sp=0x%s allocated %d\n",
2954                         paddr_nz (sp), ROUND_UP (len, 16));
2955
2956   /* Initialize the integer and float register pointers.  */
2957   argreg = A0_REGNUM;
2958   float_argreg = FPA0_REGNUM;
2959
2960   /* The struct_return pointer occupies the first parameter-passing reg.  */
2961   if (struct_return)
2962     {
2963       if (mips_debug)
2964         fprintf_unfiltered (gdb_stdlog,
2965                             "mips_n32n64_push_arguments: struct_return reg=%d 0x%s\n",
2966                             argreg, paddr_nz (struct_addr));
2967       write_register (argreg++, struct_addr);
2968     }
2969
2970   /* Now load as many as possible of the first arguments into
2971      registers, and push the rest onto the stack.  Loop thru args
2972      from first to last.  */
2973   for (argnum = 0; argnum < nargs; argnum++)
2974     {
2975       char *val;
2976       char *valbuf = alloca (MAX_REGISTER_RAW_SIZE);
2977       struct value *arg = args[argnum];
2978       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
2979       int len = TYPE_LENGTH (arg_type);
2980       enum type_code typecode = TYPE_CODE (arg_type);
2981
2982       if (mips_debug)
2983         fprintf_unfiltered (gdb_stdlog,
2984                             "mips_n32n64_push_arguments: %d len=%d type=%d",
2985                             argnum + 1, len, (int) typecode);
2986
2987       val = (char *) VALUE_CONTENTS (arg);
2988
2989       if (fp_register_arg_p (typecode, arg_type)
2990           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
2991         {
2992           /* This is a floating point value that fits entirely
2993              in a single register.  */
2994           /* On 32 bit ABI's the float_argreg is further adjusted
2995              above to ensure that it is even register aligned.  */
2996           LONGEST regval = extract_unsigned_integer (val, len);
2997           if (mips_debug)
2998             fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
2999                                 float_argreg, phex (regval, len));
3000           write_register (float_argreg++, regval);
3001
3002           if (mips_debug)
3003             fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3004                                 argreg, phex (regval, len));
3005           write_register (argreg, regval);
3006           argreg += 1;
3007         }
3008       else
3009         {
3010           /* Copy the argument to general registers or the stack in
3011              register-sized pieces.  Large arguments are split between
3012              registers and stack.  */
3013           /* Note: structs whose size is not a multiple of MIPS_REGSIZE
3014              are treated specially: Irix cc passes them in registers
3015              where gcc sometimes puts them on the stack.  For maximum
3016              compatibility, we will put them in both places.  */
3017           int odd_sized_struct = ((len > MIPS_SAVED_REGSIZE) &&
3018                                   (len % MIPS_SAVED_REGSIZE != 0));
3019           /* Note: Floating-point values that didn't fit into an FP
3020              register are only written to memory.  */
3021           while (len > 0)
3022             {
3023               /* Rememer if the argument was written to the stack.  */
3024               int stack_used_p = 0;
3025               int partial_len = len < MIPS_SAVED_REGSIZE ? 
3026                 len : MIPS_SAVED_REGSIZE;
3027
3028               if (mips_debug)
3029                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3030                                     partial_len);
3031
3032               /* Write this portion of the argument to the stack.  */
3033               if (argreg > MIPS_LAST_ARG_REGNUM
3034                   || odd_sized_struct
3035                   || fp_register_arg_p (typecode, arg_type))
3036                 {
3037                   /* Should shorter than int integer values be
3038                      promoted to int before being stored? */
3039                   int longword_offset = 0;
3040                   CORE_ADDR addr;
3041                   stack_used_p = 1;
3042                   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3043                     {
3044                       if (MIPS_STACK_ARGSIZE == 8 &&
3045                           (typecode == TYPE_CODE_INT ||
3046                            typecode == TYPE_CODE_PTR ||
3047                            typecode == TYPE_CODE_FLT) && len <= 4)
3048                         longword_offset = MIPS_STACK_ARGSIZE - len;
3049                     }
3050
3051                   if (mips_debug)
3052                     {
3053                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
3054                                           paddr_nz (stack_offset));
3055                       fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
3056                                           paddr_nz (longword_offset));
3057                     }
3058
3059                   addr = sp + stack_offset + longword_offset;
3060
3061                   if (mips_debug)
3062                     {
3063                       int i;
3064                       fprintf_unfiltered (gdb_stdlog, " @0x%s ", 
3065                                           paddr_nz (addr));
3066                       for (i = 0; i < partial_len; i++)
3067                         {
3068                           fprintf_unfiltered (gdb_stdlog, "%02x", 
3069                                               val[i] & 0xff);
3070                         }
3071                     }
3072                   write_memory (addr, val, partial_len);
3073                 }
3074
3075               /* Note!!! This is NOT an else clause.  Odd sized
3076                  structs may go thru BOTH paths.  Floating point
3077                  arguments will not.  */
3078               /* Write this portion of the argument to a general
3079                  purpose register.  */
3080               if (argreg <= MIPS_LAST_ARG_REGNUM
3081                   && !fp_register_arg_p (typecode, arg_type))
3082                 {
3083                   LONGEST regval = extract_unsigned_integer (val, partial_len);
3084
3085                   /* A non-floating-point argument being passed in a
3086                      general register.  If a struct or union, and if
3087                      the remaining length is smaller than the register
3088                      size, we have to adjust the register value on
3089                      big endian targets.
3090
3091                      It does not seem to be necessary to do the
3092                      same for integral types.
3093
3094                      cagney/2001-07-23: gdb/179: Also, GCC, when
3095                      outputting LE O32 with sizeof (struct) <
3096                      MIPS_SAVED_REGSIZE, generates a left shift as
3097                      part of storing the argument in a register a
3098                      register (the left shift isn't generated when
3099                      sizeof (struct) >= MIPS_SAVED_REGSIZE).  Since it
3100                      is quite possible that this is GCC contradicting
3101                      the LE/O32 ABI, GDB has not been adjusted to
3102                      accommodate this.  Either someone needs to
3103                      demonstrate that the LE/O32 ABI specifies such a
3104                      left shift OR this new ABI gets identified as
3105                      such and GDB gets tweaked accordingly.  */
3106
3107                   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
3108                       && partial_len < MIPS_SAVED_REGSIZE
3109                       && (typecode == TYPE_CODE_STRUCT ||
3110                           typecode == TYPE_CODE_UNION))
3111                     regval <<= ((MIPS_SAVED_REGSIZE - partial_len) *
3112                                 TARGET_CHAR_BIT);
3113
3114                   if (mips_debug)
3115                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3116                                       argreg,
3117                                       phex (regval, MIPS_SAVED_REGSIZE));
3118                   write_register (argreg, regval);
3119                   argreg++;
3120                 }
3121
3122               len -= partial_len;
3123               val += partial_len;
3124
3125               /* Compute the the offset into the stack at which we
3126                  will copy the next parameter.
3127
3128                  In N32 (N64?), the stack_offset only needs to be
3129                  adjusted when it has been used.  */
3130
3131               if (stack_used_p)
3132                 stack_offset += ROUND_UP (partial_len, MIPS_STACK_ARGSIZE);
3133             }
3134         }
3135       if (mips_debug)
3136         fprintf_unfiltered (gdb_stdlog, "\n");
3137     }
3138
3139   /* Return adjusted stack pointer.  */
3140   return sp;
3141 }
3142
3143 /* O32 version of push_arguments.  */
3144
3145 static CORE_ADDR
3146 mips_o32_push_arguments (int nargs,
3147                          struct value **args,
3148                          CORE_ADDR sp,
3149                          int struct_return,
3150                          CORE_ADDR struct_addr)
3151 {
3152   int argreg;
3153   int float_argreg;
3154   int argnum;
3155   int len = 0;
3156   int stack_offset = 0;
3157
3158   /* First ensure that the stack and structure return address (if any)
3159      are properly aligned.  The stack has to be at least 64-bit
3160      aligned even on 32-bit machines, because doubles must be 64-bit
3161      aligned.  For n32 and n64, stack frames need to be 128-bit
3162      aligned, so we round to this widest known alignment.  */
3163
3164   sp = ROUND_DOWN (sp, 16);
3165   struct_addr = ROUND_DOWN (struct_addr, 16);
3166
3167   /* Now make space on the stack for the args.  */
3168   for (argnum = 0; argnum < nargs; argnum++)
3169     len += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 
3170                      MIPS_STACK_ARGSIZE);
3171   sp -= ROUND_UP (len, 16);
3172
3173   if (mips_debug)
3174     fprintf_unfiltered (gdb_stdlog, 
3175                         "mips_o32_push_arguments: sp=0x%s allocated %d\n",
3176                         paddr_nz (sp), ROUND_UP (len, 16));
3177
3178   /* Initialize the integer and float register pointers.  */
3179   argreg = A0_REGNUM;
3180   float_argreg = FPA0_REGNUM;
3181
3182   /* The struct_return pointer occupies the first parameter-passing reg.  */
3183   if (struct_return)
3184     {
3185       if (mips_debug)
3186         fprintf_unfiltered (gdb_stdlog,
3187                             "mips_o32_push_arguments: struct_return reg=%d 0x%s\n",
3188                             argreg, paddr_nz (struct_addr));
3189       write_register (argreg++, struct_addr);
3190       stack_offset += MIPS_STACK_ARGSIZE;
3191     }
3192
3193   /* Now load as many as possible of the first arguments into
3194      registers, and push the rest onto the stack.  Loop thru args
3195      from first to last.  */
3196   for (argnum = 0; argnum < nargs; argnum++)
3197     {
3198       char *val;
3199       char *valbuf = alloca (MAX_REGISTER_RAW_SIZE);
3200       struct value *arg = args[argnum];
3201       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
3202       int len = TYPE_LENGTH (arg_type);
3203       enum type_code typecode = TYPE_CODE (arg_type);
3204
3205       if (mips_debug)
3206         fprintf_unfiltered (gdb_stdlog,
3207                             "mips_o32_push_arguments: %d len=%d type=%d",
3208                             argnum + 1, len, (int) typecode);
3209
3210       val = (char *) VALUE_CONTENTS (arg);
3211
3212       /* 32-bit ABIs always start floating point arguments in an
3213          even-numbered floating point register.  Round the FP register
3214          up before the check to see if there are any FP registers
3215          left.  O32/O64 targets also pass the FP in the integer
3216          registers so also round up normal registers.  */
3217       if (!FP_REGISTER_DOUBLE
3218           && fp_register_arg_p (typecode, arg_type))
3219         {
3220           if ((float_argreg & 1))
3221             float_argreg++;
3222         }
3223
3224       /* Floating point arguments passed in registers have to be
3225          treated specially.  On 32-bit architectures, doubles
3226          are passed in register pairs; the even register gets
3227          the low word, and the odd register gets the high word.
3228          On O32/O64, the first two floating point arguments are
3229          also copied to general registers, because MIPS16 functions
3230          don't use float registers for arguments.  This duplication of
3231          arguments in general registers can't hurt non-MIPS16 functions
3232          because those registers are normally skipped.  */
3233
3234       if (fp_register_arg_p (typecode, arg_type)
3235           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
3236         {
3237           if (!FP_REGISTER_DOUBLE && len == 8)
3238             {
3239               int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
3240               unsigned long regval;
3241
3242               /* Write the low word of the double to the even register(s).  */
3243               regval = extract_unsigned_integer (val + low_offset, 4);
3244               if (mips_debug)
3245                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3246                                     float_argreg, phex (regval, 4));
3247               write_register (float_argreg++, regval);
3248               if (mips_debug)
3249                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3250                                     argreg, phex (regval, 4));
3251               write_register (argreg++, regval);
3252
3253               /* Write the high word of the double to the odd register(s).  */
3254               regval = extract_unsigned_integer (val + 4 - low_offset, 4);
3255               if (mips_debug)
3256                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3257                                     float_argreg, phex (regval, 4));
3258               write_register (float_argreg++, regval);
3259
3260               if (mips_debug)
3261                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3262                                     argreg, phex (regval, 4));
3263               write_register (argreg++, regval);
3264             }
3265           else
3266             {
3267               /* This is a floating point value that fits entirely
3268                  in a single register.  */
3269               /* On 32 bit ABI's the float_argreg is further adjusted
3270                  above to ensure that it is even register aligned.  */
3271               LONGEST regval = extract_unsigned_integer (val, len);
3272               if (mips_debug)
3273                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3274                                     float_argreg, phex (regval, len));
3275               write_register (float_argreg++, regval);
3276               /* CAGNEY: 32 bit MIPS ABI's always reserve two FP
3277                  registers for each argument.  The below is (my
3278                  guess) to ensure that the corresponding integer
3279                  register has reserved the same space.  */
3280               if (mips_debug)
3281                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3282                                     argreg, phex (regval, len));
3283               write_register (argreg, regval);
3284               argreg += FP_REGISTER_DOUBLE ? 1 : 2;
3285             }
3286           /* Reserve space for the FP register.  */
3287           stack_offset += ROUND_UP (len, MIPS_STACK_ARGSIZE);
3288         }
3289       else
3290         {
3291           /* Copy the argument to general registers or the stack in
3292              register-sized pieces.  Large arguments are split between
3293              registers and stack.  */
3294           /* Note: structs whose size is not a multiple of MIPS_REGSIZE
3295              are treated specially: Irix cc passes them in registers
3296              where gcc sometimes puts them on the stack.  For maximum
3297              compatibility, we will put them in both places.  */
3298           int odd_sized_struct = ((len > MIPS_SAVED_REGSIZE) &&
3299                                   (len % MIPS_SAVED_REGSIZE != 0));
3300           /* Structures should be aligned to eight bytes (even arg registers)
3301              on MIPS_ABI_O32, if their first member has double precision.  */
3302           if (MIPS_SAVED_REGSIZE < 8
3303               && mips_type_needs_double_align (arg_type))
3304             {
3305               if ((argreg & 1))
3306                 argreg++;
3307             }
3308           /* Note: Floating-point values that didn't fit into an FP
3309              register are only written to memory.  */
3310           while (len > 0)
3311             {
3312               /* Remember if the argument was written to the stack.  */
3313               int stack_used_p = 0;
3314               int partial_len = 
3315                 len < MIPS_SAVED_REGSIZE ? len : MIPS_SAVED_REGSIZE;
3316
3317               if (mips_debug)
3318                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3319                                     partial_len);
3320
3321               /* Write this portion of the argument to the stack.  */
3322               if (argreg > MIPS_LAST_ARG_REGNUM
3323                   || odd_sized_struct
3324                   || fp_register_arg_p (typecode, arg_type))
3325                 {
3326                   /* Should shorter than int integer values be
3327                      promoted to int before being stored? */
3328                   int longword_offset = 0;
3329                   CORE_ADDR addr;
3330                   stack_used_p = 1;
3331                   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3332                     {
3333                       if (MIPS_STACK_ARGSIZE == 8 &&
3334                           (typecode == TYPE_CODE_INT ||
3335                            typecode == TYPE_CODE_PTR ||
3336                            typecode == TYPE_CODE_FLT) && len <= 4)
3337                         longword_offset = MIPS_STACK_ARGSIZE - len;
3338                     }
3339
3340                   if (mips_debug)
3341                     {
3342                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
3343                                           paddr_nz (stack_offset));
3344                       fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
3345                                           paddr_nz (longword_offset));
3346                     }
3347
3348                   addr = sp + stack_offset + longword_offset;
3349
3350                   if (mips_debug)
3351                     {
3352                       int i;
3353                       fprintf_unfiltered (gdb_stdlog, " @0x%s ", 
3354                                           paddr_nz (addr));
3355                       for (i = 0; i < partial_len; i++)
3356                         {
3357                           fprintf_unfiltered (gdb_stdlog, "%02x", 
3358                                               val[i] & 0xff);
3359                         }
3360                     }
3361                   write_memory (addr, val, partial_len);
3362                 }
3363
3364               /* Note!!! This is NOT an else clause.  Odd sized
3365                  structs may go thru BOTH paths.  Floating point
3366                  arguments will not.  */
3367               /* Write this portion of the argument to a general
3368                  purpose register.  */
3369               if (argreg <= MIPS_LAST_ARG_REGNUM
3370                   && !fp_register_arg_p (typecode, arg_type))
3371                 {
3372                   LONGEST regval = extract_signed_integer (val, partial_len);
3373                   /* Value may need to be sign extended, because 
3374                      MIPS_REGSIZE != MIPS_SAVED_REGSIZE.  */
3375
3376                   /* A non-floating-point argument being passed in a
3377                      general register.  If a struct or union, and if
3378                      the remaining length is smaller than the register
3379                      size, we have to adjust the register value on
3380                      big endian targets.
3381
3382                      It does not seem to be necessary to do the
3383                      same for integral types.
3384
3385                      Also don't do this adjustment on O64 binaries.
3386
3387                      cagney/2001-07-23: gdb/179: Also, GCC, when
3388                      outputting LE O32 with sizeof (struct) <
3389                      MIPS_SAVED_REGSIZE, generates a left shift as
3390                      part of storing the argument in a register a
3391                      register (the left shift isn't generated when
3392                      sizeof (struct) >= MIPS_SAVED_REGSIZE).  Since it
3393                      is quite possible that this is GCC contradicting
3394                      the LE/O32 ABI, GDB has not been adjusted to
3395                      accommodate this.  Either someone needs to
3396                      demonstrate that the LE/O32 ABI specifies such a
3397                      left shift OR this new ABI gets identified as
3398                      such and GDB gets tweaked accordingly.  */
3399
3400                   if (MIPS_SAVED_REGSIZE < 8
3401                       && TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
3402                       && partial_len < MIPS_SAVED_REGSIZE
3403                       && (typecode == TYPE_CODE_STRUCT ||
3404                           typecode == TYPE_CODE_UNION))
3405                     regval <<= ((MIPS_SAVED_REGSIZE - partial_len) *
3406                                 TARGET_CHAR_BIT);
3407
3408                   if (mips_debug)
3409                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3410                                       argreg,
3411                                       phex (regval, MIPS_SAVED_REGSIZE));
3412                   write_register (argreg, regval);
3413                   argreg++;
3414
3415                   /* Prevent subsequent floating point arguments from
3416                      being passed in floating point registers.  */
3417                   float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
3418                 }
3419
3420               len -= partial_len;
3421               val += partial_len;
3422
3423               /* Compute the the offset into the stack at which we
3424                  will copy the next parameter.
3425
3426                  In older ABIs, the caller reserved space for
3427                  registers that contained arguments.  This was loosely
3428                  refered to as their "home".  Consequently, space is
3429                  always allocated.  */
3430
3431               stack_offset += ROUND_UP (partial_len, MIPS_STACK_ARGSIZE);
3432             }
3433         }
3434       if (mips_debug)
3435         fprintf_unfiltered (gdb_stdlog, "\n");
3436     }
3437
3438   /* Return adjusted stack pointer.  */
3439   return sp;
3440 }
3441
3442 /* O64 version of push_arguments.  */
3443
3444 static CORE_ADDR
3445 mips_o64_push_arguments (int nargs,
3446                          struct value **args,
3447                          CORE_ADDR sp,
3448                          int struct_return,
3449                          CORE_ADDR struct_addr)
3450 {
3451   int argreg;
3452   int float_argreg;
3453   int argnum;
3454   int len = 0;
3455   int stack_offset = 0;
3456
3457   /* First ensure that the stack and structure return address (if any)
3458      are properly aligned.  The stack has to be at least 64-bit
3459      aligned even on 32-bit machines, because doubles must be 64-bit
3460      aligned.  For n32 and n64, stack frames need to be 128-bit
3461      aligned, so we round to this widest known alignment.  */
3462
3463   sp = ROUND_DOWN (sp, 16);
3464   struct_addr = ROUND_DOWN (struct_addr, 16);
3465
3466   /* Now make space on the stack for the args.  */
3467   for (argnum = 0; argnum < nargs; argnum++)
3468     len += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 
3469                      MIPS_STACK_ARGSIZE);
3470   sp -= ROUND_UP (len, 16);
3471
3472   if (mips_debug)
3473     fprintf_unfiltered (gdb_stdlog, 
3474                         "mips_o64_push_arguments: sp=0x%s allocated %d\n",
3475                         paddr_nz (sp), ROUND_UP (len, 16));
3476
3477   /* Initialize the integer and float register pointers.  */
3478   argreg = A0_REGNUM;
3479   float_argreg = FPA0_REGNUM;
3480
3481   /* The struct_return pointer occupies the first parameter-passing reg.  */
3482   if (struct_return)
3483     {
3484       if (mips_debug)
3485         fprintf_unfiltered (gdb_stdlog,
3486                             "mips_o64_push_arguments: struct_return reg=%d 0x%s\n",
3487                             argreg, paddr_nz (struct_addr));
3488       write_register (argreg++, struct_addr);
3489       stack_offset += MIPS_STACK_ARGSIZE;
3490     }
3491
3492   /* Now load as many as possible of the first arguments into
3493      registers, and push the rest onto the stack.  Loop thru args
3494      from first to last.  */
3495   for (argnum = 0; argnum < nargs; argnum++)
3496     {
3497       char *val;
3498       char *valbuf = alloca (MAX_REGISTER_RAW_SIZE);
3499       struct value *arg = args[argnum];
3500       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
3501       int len = TYPE_LENGTH (arg_type);
3502       enum type_code typecode = TYPE_CODE (arg_type);
3503
3504       if (mips_debug)
3505         fprintf_unfiltered (gdb_stdlog,
3506                             "mips_o64_push_arguments: %d len=%d type=%d",
3507                             argnum + 1, len, (int) typecode);
3508
3509       val = (char *) VALUE_CONTENTS (arg);
3510
3511       /* 32-bit ABIs always start floating point arguments in an
3512          even-numbered floating point register.  Round the FP register
3513          up before the check to see if there are any FP registers
3514          left.  O32/O64 targets also pass the FP in the integer
3515          registers so also round up normal registers.  */
3516       if (!FP_REGISTER_DOUBLE
3517           && fp_register_arg_p (typecode, arg_type))
3518         {
3519           if ((float_argreg & 1))
3520             float_argreg++;
3521         }
3522
3523       /* Floating point arguments passed in registers have to be
3524          treated specially.  On 32-bit architectures, doubles
3525          are passed in register pairs; the even register gets
3526          the low word, and the odd register gets the high word.
3527          On O32/O64, the first two floating point arguments are
3528          also copied to general registers, because MIPS16 functions
3529          don't use float registers for arguments.  This duplication of
3530          arguments in general registers can't hurt non-MIPS16 functions
3531          because those registers are normally skipped.  */
3532
3533       if (fp_register_arg_p (typecode, arg_type)
3534           && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
3535         {
3536           if (!FP_REGISTER_DOUBLE && len == 8)
3537             {
3538               int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
3539               unsigned long regval;
3540
3541               /* Write the low word of the double to the even register(s).  */
3542               regval = extract_unsigned_integer (val + low_offset, 4);
3543               if (mips_debug)
3544                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3545                                     float_argreg, phex (regval, 4));
3546               write_register (float_argreg++, regval);
3547               if (mips_debug)
3548                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3549                                     argreg, phex (regval, 4));
3550               write_register (argreg++, regval);
3551
3552               /* Write the high word of the double to the odd register(s).  */
3553               regval = extract_unsigned_integer (val + 4 - low_offset, 4);
3554               if (mips_debug)
3555                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3556                                     float_argreg, phex (regval, 4));
3557               write_register (float_argreg++, regval);
3558
3559               if (mips_debug)
3560                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3561                                     argreg, phex (regval, 4));
3562               write_register (argreg++, regval);
3563             }
3564           else
3565             {
3566               /* This is a floating point value that fits entirely
3567                  in a single register.  */
3568               /* On 32 bit ABI's the float_argreg is further adjusted
3569                  above to ensure that it is even register aligned.  */
3570               LONGEST regval = extract_unsigned_integer (val, len);
3571               if (mips_debug)
3572                 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3573                                     float_argreg, phex (regval, len));
3574               write_register (float_argreg++, regval);
3575               /* CAGNEY: 32 bit MIPS ABI's always reserve two FP
3576                  registers for each argument.  The below is (my
3577                  guess) to ensure that the corresponding integer
3578                  register has reserved the same space.  */
3579               if (mips_debug)
3580                 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3581                                     argreg, phex (regval, len));
3582               write_register (argreg, regval);
3583               argreg += FP_REGISTER_DOUBLE ? 1 : 2;
3584             }
3585           /* Reserve space for the FP register.  */
3586           stack_offset += ROUND_UP (len, MIPS_STACK_ARGSIZE);
3587         }
3588       else
3589         {
3590           /* Copy the argument to general registers or the stack in
3591              register-sized pieces.  Large arguments are split between
3592              registers and stack.  */
3593           /* Note: structs whose size is not a multiple of MIPS_REGSIZE
3594              are treated specially: Irix cc passes them in registers
3595              where gcc sometimes puts them on the stack.  For maximum
3596              compatibility, we will put them in both places.  */
3597           int odd_sized_struct = ((len > MIPS_SAVED_REGSIZE) &&
3598                                   (len % MIPS_SAVED_REGSIZE != 0));
3599           /* Structures should be aligned to eight bytes (even arg registers)
3600              on MIPS_ABI_O32, if their first member has double precision.  */
3601           if (MIPS_SAVED_REGSIZE < 8
3602               && mips_type_needs_double_align (arg_type))
3603             {
3604               if ((argreg & 1))
3605                 argreg++;
3606             }
3607           /* Note: Floating-point values that didn't fit into an FP
3608              register are only written to memory.  */
3609           while (len > 0)
3610             {
3611               /* Remember if the argument was written to the stack.  */
3612               int stack_used_p = 0;
3613               int partial_len = 
3614                 len < MIPS_SAVED_REGSIZE ? len : MIPS_SAVED_REGSIZE;
3615
3616               if (mips_debug)
3617                 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3618                                     partial_len);
3619
3620               /* Write this portion of the argument to the stack.  */
3621               if (argreg > MIPS_LAST_ARG_REGNUM
3622                   || odd_sized_struct
3623                   || fp_register_arg_p (typecode, arg_type))
3624                 {
3625                   /* Should shorter than int integer values be
3626                      promoted to int before being stored? */
3627                   int longword_offset = 0;
3628                   CORE_ADDR addr;
3629                   stack_used_p = 1;
3630                   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3631                     {
3632                       if (MIPS_STACK_ARGSIZE == 8 &&
3633                           (typecode == TYPE_CODE_INT ||
3634                            typecode == TYPE_CODE_PTR ||
3635                            typecode == TYPE_CODE_FLT) && len <= 4)
3636                         longword_offset = MIPS_STACK_ARGSIZE - len;
3637                     }
3638
3639                   if (mips_debug)
3640                     {
3641                       fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
3642                                           paddr_nz (stack_offset));
3643                       fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
3644                                           paddr_nz (longword_offset));
3645                     }
3646
3647                   addr = sp + stack_offset + longword_offset;
3648
3649                   if (mips_debug)
3650                     {
3651                       int i;
3652                       fprintf_unfiltered (gdb_stdlog, " @0x%s ", 
3653                                           paddr_nz (addr));
3654                       for (i = 0; i < partial_len; i++)
3655                         {
3656                           fprintf_unfiltered (gdb_stdlog, "%02x", 
3657                                               val[i] & 0xff);
3658                         }
3659                     }
3660                   write_memory (addr, val, partial_len);
3661                 }
3662
3663               /* Note!!! This is NOT an else clause.  Odd sized
3664                  structs may go thru BOTH paths.  Floating point
3665                  arguments will not.  */
3666               /* Write this portion of the argument to a general
3667                  purpose register.  */
3668               if (argreg <= MIPS_LAST_ARG_REGNUM
3669                   && !fp_register_arg_p (typecode, arg_type))
3670                 {
3671                   LONGEST regval = extract_signed_integer (val, partial_len);
3672                   /* Value may need to be sign extended, because 
3673                      MIPS_REGSIZE != MIPS_SAVED_REGSIZE.  */
3674
3675                   /* A non-floating-point argument being passed in a
3676                      general register.  If a struct or union, and if
3677                      the remaining length is smaller than the register
3678                      size, we have to adjust the register value on
3679                      big endian targets.
3680
3681                      It does not seem to be necessary to do the
3682                      same for integral types.
3683
3684                      Also don't do this adjustment on O64 binaries.
3685
3686                      cagney/2001-07-23: gdb/179: Also, GCC, when
3687                      outputting LE O32 with sizeof (struct) <
3688                      MIPS_SAVED_REGSIZE, generates a left shift as
3689                      part of storing the argument in a register a
3690                      register (the left shift isn't generated when
3691                      sizeof (struct) >= MIPS_SAVED_REGSIZE).  Since it
3692                      is quite possible that this is GCC contradicting
3693                      the LE/O32 ABI, GDB has not been adjusted to
3694                      accommodate this.  Either someone needs to
3695                      demonstrate that the LE/O32 ABI specifies such a
3696                      left shift OR this new ABI gets identified as
3697                      such and GDB gets tweaked accordingly.  */
3698
3699                   if (MIPS_SAVED_REGSIZE < 8
3700                       && TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
3701                       && partial_len < MIPS_SAVED_REGSIZE
3702                       && (typecode == TYPE_CODE_STRUCT ||
3703                           typecode == TYPE_CODE_UNION))
3704                     regval <<= ((MIPS_SAVED_REGSIZE - partial_len) *
3705                                 TARGET_CHAR_BIT);
3706
3707                   if (mips_debug)
3708                     fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3709                                       argreg,
3710                                       phex (regval, MIPS_SAVED_REGSIZE));
3711                   write_register (argreg, regval);
3712                   argreg++;
3713
3714                   /* Prevent subsequent floating point arguments from
3715                      being passed in floating point registers.  */
3716                   float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
3717                 }
3718
3719               len -= partial_len;
3720               val += partial_len;
3721
3722               /* Compute the the offset into the stack at which we
3723                  will copy the next parameter.
3724
3725                  In older ABIs, the caller reserved space for
3726                  registers that contained arguments.  This was loosely
3727                  refered to as their "home".  Consequently, space is
3728                  always allocated.  */
3729
3730               stack_offset += ROUND_UP (partial_len, MIPS_STACK_ARGSIZE);
3731             }
3732         }
3733       if (mips_debug)
3734         fprintf_unfiltered (gdb_stdlog, "\n");
3735     }
3736
3737   /* Return adjusted stack pointer.  */
3738   return sp;
3739 }
3740
3741 static CORE_ADDR
3742 mips_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
3743 {
3744   /* Set the return address register to point to the entry
3745      point of the program, where a breakpoint lies in wait.  */
3746   write_register (RA_REGNUM, CALL_DUMMY_ADDRESS ());
3747   return sp;
3748 }
3749
3750 static void
3751 mips_push_register (CORE_ADDR * sp, int regno)
3752 {
3753   char *buffer = alloca (MAX_REGISTER_RAW_SIZE);
3754   int regsize;
3755   int offset;
3756   if (MIPS_SAVED_REGSIZE < REGISTER_RAW_SIZE (regno))
3757     {
3758       regsize = MIPS_SAVED_REGSIZE;
3759       offset = (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
3760                 ? REGISTER_RAW_SIZE (regno) - MIPS_SAVED_REGSIZE
3761                 : 0);
3762     }
3763   else
3764     {
3765       regsize = REGISTER_RAW_SIZE (regno);
3766       offset = 0;
3767     }
3768   *sp -= regsize;
3769   deprecated_read_register_gen (regno, buffer);
3770   write_memory (*sp, buffer + offset, regsize);
3771 }
3772
3773 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<(MIPS_NUMREGS-1). */
3774 #define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
3775
3776 static void
3777 mips_push_dummy_frame (void)
3778 {
3779   int ireg;
3780   struct linked_proc_info *link = (struct linked_proc_info *)
3781   xmalloc (sizeof (struct linked_proc_info));
3782   mips_extra_func_info_t proc_desc = &link->info;
3783   CORE_ADDR sp = ADDR_BITS_REMOVE (read_signed_register (SP_REGNUM));
3784   CORE_ADDR old_sp = sp;
3785   link->next = linked_proc_desc_table;
3786   linked_proc_desc_table = link;
3787
3788 /* FIXME!   are these correct ? */
3789 #define PUSH_FP_REGNUM 16       /* must be a register preserved across calls */
3790 #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<(MIPS_NUMREGS-1))
3791 #define FLOAT_REG_SAVE_MASK MASK(0,19)
3792 #define FLOAT_SINGLE_REG_SAVE_MASK \
3793   ((1<<18)|(1<<16)|(1<<14)|(1<<12)|(1<<10)|(1<<8)|(1<<6)|(1<<4)|(1<<2)|(1<<0))
3794   /*
3795    * The registers we must save are all those not preserved across
3796    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
3797    * In addition, we must save the PC, PUSH_FP_REGNUM, MMLO/-HI
3798    * and FP Control/Status registers.
3799    *
3800    *
3801    * Dummy frame layout:
3802    *  (high memory)
3803    *    Saved PC
3804    *    Saved MMHI, MMLO, FPC_CSR
3805    *    Saved R31
3806    *    Saved R28
3807    *    ...
3808    *    Saved R1
3809    *    Saved D18 (i.e. F19, F18)
3810    *    ...
3811    *    Saved D0 (i.e. F1, F0)
3812    *    Argument build area and stack arguments written via mips_push_arguments
3813    *  (low memory)
3814    */
3815
3816   /* Save special registers (PC, MMHI, MMLO, FPC_CSR) */
3817   PROC_FRAME_REG (proc_desc) = PUSH_FP_REGNUM;
3818   PROC_FRAME_OFFSET (proc_desc) = 0;
3819   PROC_FRAME_ADJUST (proc_desc) = 0;
3820   mips_push_register (&sp, PC_REGNUM);
3821   mips_push_register (&sp, HI_REGNUM);
3822   mips_push_register (&sp, LO_REGNUM);
3823   mips_push_register (&sp, MIPS_FPU_TYPE == MIPS_FPU_NONE ? 0 : FCRCS_REGNUM);
3824
3825   /* Save general CPU registers */
3826   PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
3827   /* PROC_REG_OFFSET is the offset of the first saved register from FP.  */
3828   PROC_REG_OFFSET (proc_desc) = sp - old_sp - MIPS_SAVED_REGSIZE;
3829   for (ireg = 32; --ireg >= 0;)
3830     if (PROC_REG_MASK (proc_desc) & (1 << ireg))
3831       mips_push_register (&sp, ireg);
3832
3833   /* Save floating point registers starting with high order word */
3834   PROC_FREG_MASK (proc_desc) =
3835     MIPS_FPU_TYPE == MIPS_FPU_DOUBLE ? FLOAT_REG_SAVE_MASK
3836     : MIPS_FPU_TYPE == MIPS_FPU_SINGLE ? FLOAT_SINGLE_REG_SAVE_MASK : 0;
3837   /* PROC_FREG_OFFSET is the offset of the first saved *double* register
3838      from FP.  */
3839   PROC_FREG_OFFSET (proc_desc) = sp - old_sp - 8;
3840   for (ireg = 32; --ireg >= 0;)
3841     if (PROC_FREG_MASK (proc_desc) & (1 << ireg))
3842       mips_push_register (&sp, ireg + FP0_REGNUM);
3843
3844   /* Update the frame pointer for the call dummy and the stack pointer.
3845      Set the procedure's starting and ending addresses to point to the
3846      call dummy address at the entry point.  */
3847   write_register (PUSH_FP_REGNUM, old_sp);
3848   write_register (SP_REGNUM, sp);
3849   PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
3850   PROC_HIGH_ADDR (proc_desc) = CALL_DUMMY_ADDRESS () + 4;
3851   SET_PROC_DESC_IS_DUMMY (proc_desc);
3852   PROC_PC_REG (proc_desc) = RA_REGNUM;
3853 }
3854
3855 static void
3856 mips_pop_frame (void)
3857 {
3858   register int regnum;
3859   struct frame_info *frame = get_current_frame ();
3860   CORE_ADDR new_sp = get_frame_base (frame);
3861   mips_extra_func_info_t proc_desc = get_frame_extra_info (frame)->proc_desc;
3862
3863   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame), 0, 0))
3864     {
3865       generic_pop_dummy_frame ();
3866       flush_cached_frames ();
3867       return;
3868     }
3869
3870   write_register (PC_REGNUM, DEPRECATED_FRAME_SAVED_PC (frame));
3871   if (get_frame_saved_regs (frame) == NULL)
3872     DEPRECATED_FRAME_INIT_SAVED_REGS (frame);
3873   for (regnum = 0; regnum < NUM_REGS; regnum++)
3874     if (regnum != SP_REGNUM && regnum != PC_REGNUM
3875         && get_frame_saved_regs (frame)[regnum])
3876       {
3877         /* Floating point registers must not be sign extended, 
3878            in case MIPS_SAVED_REGSIZE = 4 but sizeof (FP0_REGNUM) == 8.  */
3879
3880         if (FP0_REGNUM <= regnum && regnum < FP0_REGNUM + 32)
3881           write_register (regnum,
3882                           read_memory_unsigned_integer (get_frame_saved_regs (frame)[regnum],
3883                                                         MIPS_SAVED_REGSIZE));
3884         else
3885           write_register (regnum,
3886                           read_memory_integer (get_frame_saved_regs (frame)[regnum],
3887                                                MIPS_SAVED_REGSIZE));
3888       }
3889
3890   write_register (SP_REGNUM, new_sp);
3891   flush_cached_frames ();
3892
3893   if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
3894     {
3895       struct linked_proc_info *pi_ptr, *prev_ptr;
3896
3897       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
3898            pi_ptr != NULL;
3899            prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
3900         {
3901           if (&pi_ptr->info == proc_desc)
3902             break;
3903         }
3904
3905       if (pi_ptr == NULL)
3906         error ("Can't locate dummy extra frame info\n");
3907
3908       if (prev_ptr != NULL)
3909         prev_ptr->next = pi_ptr->next;
3910       else
3911         linked_proc_desc_table = pi_ptr->next;
3912
3913       xfree (pi_ptr);
3914
3915       write_register (HI_REGNUM,
3916                       read_memory_integer (new_sp - 2 * MIPS_SAVED_REGSIZE,
3917                                            MIPS_SAVED_REGSIZE));
3918       write_register (LO_REGNUM,
3919                       read_memory_integer (new_sp - 3 * MIPS_SAVED_REGSIZE,
3920                                            MIPS_SAVED_REGSIZE));
3921       if (MIPS_FPU_TYPE != MIPS_FPU_NONE)
3922         write_register (FCRCS_REGNUM,
3923                         read_memory_integer (new_sp - 4 * MIPS_SAVED_REGSIZE,
3924                                              MIPS_SAVED_REGSIZE));
3925     }
3926 }
3927
3928 static void
3929 mips_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs, 
3930                      struct value **args, struct type *type, int gcc_p)
3931 {
3932   write_register(T9_REGNUM, fun);
3933 }
3934
3935 /* Floating point register management.
3936
3937    Background: MIPS1 & 2 fp registers are 32 bits wide.  To support
3938    64bit operations, these early MIPS cpus treat fp register pairs
3939    (f0,f1) as a single register (d0).  Later MIPS cpu's have 64 bit fp
3940    registers and offer a compatibility mode that emulates the MIPS2 fp
3941    model.  When operating in MIPS2 fp compat mode, later cpu's split
3942    double precision floats into two 32-bit chunks and store them in
3943    consecutive fp regs.  To display 64-bit floats stored in this
3944    fashion, we have to combine 32 bits from f0 and 32 bits from f1.
3945    Throw in user-configurable endianness and you have a real mess.
3946
3947    The way this works is:
3948      - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
3949        double-precision value will be split across two logical registers.
3950        The lower-numbered logical register will hold the low-order bits,
3951        regardless of the processor's endianness.
3952      - If we are on a 64-bit processor, and we are looking for a
3953        single-precision value, it will be in the low ordered bits
3954        of a 64-bit GPR (after mfc1, for example) or a 64-bit register
3955        save slot in memory.
3956      - If we are in 64-bit mode, everything is straightforward.
3957
3958    Note that this code only deals with "live" registers at the top of the
3959    stack.  We will attempt to deal with saved registers later, when
3960    the raw/cooked register interface is in place. (We need a general
3961    interface that can deal with dynamic saved register sizes -- fp
3962    regs could be 32 bits wide in one frame and 64 on the frame above
3963    and below).  */
3964
3965 static struct type *
3966 mips_float_register_type (void)
3967 {
3968   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3969     return builtin_type_ieee_single_big;
3970   else
3971     return builtin_type_ieee_single_little;
3972 }
3973
3974 static struct type *
3975 mips_double_register_type (void)
3976 {
3977   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3978     return builtin_type_ieee_double_big;
3979   else
3980     return builtin_type_ieee_double_little;
3981 }
3982
3983 /* Copy a 32-bit single-precision value from the current frame
3984    into rare_buffer.  */
3985
3986 static void
3987 mips_read_fp_register_single (int regno, char *rare_buffer)
3988 {
3989   int raw_size = REGISTER_RAW_SIZE (regno);
3990   char *raw_buffer = alloca (raw_size);
3991
3992   if (!frame_register_read (deprecated_selected_frame, regno, raw_buffer))
3993     error ("can't read register %d (%s)", regno, REGISTER_NAME (regno));
3994   if (raw_size == 8)
3995     {
3996       /* We have a 64-bit value for this register.  Find the low-order
3997          32 bits.  */
3998       int offset;
3999
4000       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4001         offset = 4;
4002       else
4003         offset = 0;
4004
4005       memcpy (rare_buffer, raw_buffer + offset, 4);
4006     }
4007   else
4008     {
4009       memcpy (rare_buffer, raw_buffer, 4);
4010     }
4011 }
4012
4013 /* Copy a 64-bit double-precision value from the current frame into
4014    rare_buffer.  This may include getting half of it from the next
4015    register.  */
4016
4017 static void
4018 mips_read_fp_register_double (int regno, char *rare_buffer)
4019 {
4020   int raw_size = REGISTER_RAW_SIZE (regno);
4021
4022   if (raw_size == 8 && !mips2_fp_compat ())
4023     {
4024       /* We have a 64-bit value for this register, and we should use
4025          all 64 bits.  */
4026       if (!frame_register_read (deprecated_selected_frame, regno, rare_buffer))
4027         error ("can't read register %d (%s)", regno, REGISTER_NAME (regno));
4028     }
4029   else
4030     {
4031       if ((regno - FP0_REGNUM) & 1)
4032         internal_error (__FILE__, __LINE__,
4033                         "mips_read_fp_register_double: bad access to "
4034                         "odd-numbered FP register");
4035
4036       /* mips_read_fp_register_single will find the correct 32 bits from
4037          each register.  */
4038       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4039         {
4040           mips_read_fp_register_single (regno, rare_buffer + 4);
4041           mips_read_fp_register_single (regno + 1, rare_buffer);
4042         }
4043       else
4044         {
4045           mips_read_fp_register_single (regno, rare_buffer);
4046           mips_read_fp_register_single (regno + 1, rare_buffer + 4);
4047         }
4048     }
4049 }
4050
4051 static void
4052 mips_print_register (int regnum, int all)
4053 {
4054   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
4055
4056   /* Get the data in raw format.  */
4057   if (!frame_register_read (deprecated_selected_frame, regnum, raw_buffer))
4058     {
4059       printf_filtered ("%s: [Invalid]", REGISTER_NAME (regnum));
4060       return;
4061     }
4062
4063   /* If we have a actual 32-bit floating point register (or we are in
4064      32-bit compatibility mode), and the register is even-numbered,
4065      also print it as a double (spanning two registers).  */
4066   if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
4067       && (REGISTER_RAW_SIZE (regnum) == 4
4068           || mips2_fp_compat ())
4069       && !((regnum - FP0_REGNUM) & 1))
4070     {
4071       char *dbuffer = alloca (2 * MAX_REGISTER_RAW_SIZE);
4072
4073       mips_read_fp_register_double (regnum, dbuffer);
4074
4075       printf_filtered ("(d%d: ", regnum - FP0_REGNUM);
4076       val_print (mips_double_register_type (), dbuffer, 0, 0,
4077                  gdb_stdout, 0, 1, 0, Val_pretty_default);
4078       printf_filtered ("); ");
4079     }
4080   fputs_filtered (REGISTER_NAME (regnum), gdb_stdout);
4081
4082   /* The problem with printing numeric register names (r26, etc.) is that
4083      the user can't use them on input.  Probably the best solution is to
4084      fix it so that either the numeric or the funky (a2, etc.) names
4085      are accepted on input.  */
4086   if (regnum < MIPS_NUMREGS)
4087     printf_filtered ("(r%d): ", regnum);
4088   else
4089     printf_filtered (": ");
4090
4091   /* If virtual format is floating, print it that way.  */
4092   if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
4093     if (REGISTER_RAW_SIZE (regnum) == 8 && !mips2_fp_compat ())
4094       {
4095         /* We have a meaningful 64-bit value in this register.  Show
4096            it as a 32-bit float and a 64-bit double.  */
4097         int offset = 4 * (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG);
4098
4099         printf_filtered (" (float) ");
4100         val_print (mips_float_register_type (), raw_buffer + offset, 0, 0,
4101                    gdb_stdout, 0, 1, 0, Val_pretty_default);
4102         printf_filtered (", (double) ");
4103         val_print (mips_double_register_type (), raw_buffer, 0, 0,
4104                    gdb_stdout, 0, 1, 0, Val_pretty_default);
4105       }
4106     else
4107       val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0, 0,
4108                  gdb_stdout, 0, 1, 0, Val_pretty_default);
4109   /* Else print as integer in hex.  */
4110   else
4111     {
4112       int offset;
4113
4114       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4115         offset = REGISTER_RAW_SIZE (regnum) - REGISTER_VIRTUAL_SIZE (regnum);
4116       else
4117         offset = 0;
4118
4119       print_scalar_formatted (raw_buffer + offset,
4120                               REGISTER_VIRTUAL_TYPE (regnum),
4121                               'x', 0, gdb_stdout);
4122     }
4123 }
4124
4125 /* Replacement for generic do_registers_info.
4126    Print regs in pretty columns.  */
4127
4128 static int
4129 do_fp_register_row (int regnum)
4130 {                               /* do values for FP (float) regs */
4131   char *raw_buffer;
4132   double doub, flt1, flt2;      /* doubles extracted from raw hex data */
4133   int inv1, inv2, inv3;
4134
4135   raw_buffer = (char *) alloca (2 * REGISTER_RAW_SIZE (FP0_REGNUM));
4136
4137   if (REGISTER_RAW_SIZE (regnum) == 4 || mips2_fp_compat ())
4138     {
4139       /* 4-byte registers: we can fit two registers per row.  */
4140       /* Also print every pair of 4-byte regs as an 8-byte double.  */
4141       mips_read_fp_register_single (regnum, raw_buffer);
4142       flt1 = unpack_double (mips_float_register_type (), raw_buffer, &inv1);
4143
4144       mips_read_fp_register_single (regnum + 1, raw_buffer);
4145       flt2 = unpack_double (mips_float_register_type (), raw_buffer, &inv2);
4146
4147       mips_read_fp_register_double (regnum, raw_buffer);
4148       doub = unpack_double (mips_double_register_type (), raw_buffer, &inv3);
4149
4150       printf_filtered (" %-5s", REGISTER_NAME (regnum));
4151       if (inv1)
4152         printf_filtered (": <invalid float>");
4153       else
4154         printf_filtered ("%-17.9g", flt1);
4155
4156       printf_filtered (" %-5s", REGISTER_NAME (regnum + 1));
4157       if (inv2)
4158         printf_filtered (": <invalid float>");
4159       else
4160         printf_filtered ("%-17.9g", flt2);
4161
4162       printf_filtered (" dbl: ");
4163       if (inv3)
4164         printf_filtered ("<invalid double>");
4165       else
4166         printf_filtered ("%-24.17g", doub);
4167       printf_filtered ("\n");
4168
4169       /* may want to do hex display here (future enhancement) */
4170       regnum += 2;
4171     }
4172   else
4173     {
4174       /* Eight byte registers: print each one as float AND as double.  */
4175       mips_read_fp_register_single (regnum, raw_buffer);
4176       flt1 = unpack_double (mips_float_register_type (), raw_buffer, &inv1);
4177
4178       mips_read_fp_register_double (regnum, raw_buffer);
4179       doub = unpack_double (mips_double_register_type (), raw_buffer, &inv3);
4180
4181       printf_filtered (" %-5s: ", REGISTER_NAME (regnum));
4182       if (inv1)
4183         printf_filtered ("<invalid float>");
4184       else
4185         printf_filtered ("flt: %-17.9g", flt1);
4186
4187       printf_filtered (" dbl: ");
4188       if (inv3)
4189         printf_filtered ("<invalid double>");
4190       else
4191         printf_filtered ("%-24.17g", doub);
4192
4193       printf_filtered ("\n");
4194       /* may want to do hex display here (future enhancement) */
4195       regnum++;
4196     }
4197   return regnum;
4198 }
4199
4200 /* Print a row's worth of GP (int) registers, with name labels above */
4201
4202 static int
4203 do_gp_register_row (int regnum)
4204 {
4205   /* do values for GP (int) regs */
4206   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
4207   int ncols = (MIPS_REGSIZE == 8 ? 4 : 8);      /* display cols per row */
4208   int col, byte;
4209   int start_regnum = regnum;
4210   int numregs = NUM_REGS;
4211
4212
4213   /* For GP registers, we print a separate row of names above the vals */
4214   printf_filtered ("     ");
4215   for (col = 0; col < ncols && regnum < numregs; regnum++)
4216     {
4217       if (*REGISTER_NAME (regnum) == '\0')
4218         continue;               /* unused register */
4219       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
4220         break;                  /* end the row: reached FP register */
4221       printf_filtered (MIPS_REGSIZE == 8 ? "%17s" : "%9s",
4222                        REGISTER_NAME (regnum));
4223       col++;
4224     }
4225   printf_filtered (start_regnum < MIPS_NUMREGS ? "\n R%-4d" : "\n      ",
4226                    start_regnum);       /* print the R0 to R31 names */
4227
4228   regnum = start_regnum;        /* go back to start of row */
4229   /* now print the values in hex, 4 or 8 to the row */
4230   for (col = 0; col < ncols && regnum < numregs; regnum++)
4231     {
4232       if (*REGISTER_NAME (regnum) == '\0')
4233         continue;               /* unused register */
4234       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
4235         break;                  /* end row: reached FP register */
4236       /* OK: get the data in raw format.  */
4237       if (!frame_register_read (deprecated_selected_frame, regnum, raw_buffer))
4238         error ("can't read register %d (%s)", regnum, REGISTER_NAME (regnum));
4239       /* pad small registers */
4240       for (byte = 0; byte < (MIPS_REGSIZE - REGISTER_VIRTUAL_SIZE (regnum)); byte++)
4241         printf_filtered ("  ");
4242       /* Now print the register value in hex, endian order. */
4243       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
4244         for (byte = REGISTER_RAW_SIZE (regnum) - REGISTER_VIRTUAL_SIZE (regnum);
4245              byte < REGISTER_RAW_SIZE (regnum);
4246              byte++)
4247           printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
4248       else
4249         for (byte = REGISTER_VIRTUAL_SIZE (regnum) - 1;
4250              byte >= 0;
4251              byte--)
4252           printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
4253       printf_filtered (" ");
4254       col++;
4255     }
4256   if (col > 0)                  /* ie. if we actually printed anything... */
4257     printf_filtered ("\n");
4258
4259   return regnum;
4260 }
4261
4262 /* MIPS_DO_REGISTERS_INFO(): called by "info register" command */
4263
4264 static void
4265 mips_do_registers_info (int regnum, int fpregs)
4266 {
4267   if (regnum != -1)             /* do one specified register */
4268     {
4269       if (*(REGISTER_NAME (regnum)) == '\0')
4270         error ("Not a valid register for the current processor type");
4271
4272       mips_print_register (regnum, 0);
4273       printf_filtered ("\n");
4274     }
4275   else
4276     /* do all (or most) registers */
4277     {
4278       regnum = 0;
4279       while (regnum < NUM_REGS)
4280         {
4281           if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
4282             if (fpregs)         /* true for "INFO ALL-REGISTERS" command */
4283               regnum = do_fp_register_row (regnum);     /* FP regs */
4284             else
4285               regnum += MIPS_NUMREGS;   /* skip floating point regs */
4286           else
4287             regnum = do_gp_register_row (regnum);       /* GP (int) regs */
4288         }
4289     }
4290 }
4291
4292 /* Is this a branch with a delay slot?  */
4293
4294 static int is_delayed (unsigned long);
4295
4296 static int
4297 is_delayed (unsigned long insn)
4298 {
4299   int i;
4300   for (i = 0; i < NUMOPCODES; ++i)
4301     if (mips_opcodes[i].pinfo != INSN_MACRO
4302         && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
4303       break;
4304   return (i < NUMOPCODES
4305           && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
4306                                        | INSN_COND_BRANCH_DELAY
4307                                        | INSN_COND_BRANCH_LIKELY)));
4308 }
4309
4310 int
4311 mips_step_skips_delay (CORE_ADDR pc)
4312 {
4313   char buf[MIPS_INSTLEN];
4314
4315   /* There is no branch delay slot on MIPS16.  */
4316   if (pc_is_mips16 (pc))
4317     return 0;
4318
4319   if (target_read_memory (pc, buf, MIPS_INSTLEN) != 0)
4320     /* If error reading memory, guess that it is not a delayed branch.  */
4321     return 0;
4322   return is_delayed ((unsigned long) extract_unsigned_integer (buf, MIPS_INSTLEN));
4323 }
4324
4325
4326 /* Skip the PC past function prologue instructions (32-bit version).
4327    This is a helper function for mips_skip_prologue.  */
4328
4329 static CORE_ADDR
4330 mips32_skip_prologue (CORE_ADDR pc)
4331 {
4332   t_inst inst;
4333   CORE_ADDR end_pc;
4334   int seen_sp_adjust = 0;
4335   int load_immediate_bytes = 0;
4336
4337   /* Skip the typical prologue instructions. These are the stack adjustment
4338      instruction and the instructions that save registers on the stack
4339      or in the gcc frame.  */
4340   for (end_pc = pc + 100; pc < end_pc; pc += MIPS_INSTLEN)
4341     {
4342       unsigned long high_word;
4343
4344       inst = mips_fetch_instruction (pc);
4345       high_word = (inst >> 16) & 0xffff;
4346
4347       if (high_word == 0x27bd   /* addiu $sp,$sp,offset */
4348           || high_word == 0x67bd)       /* daddiu $sp,$sp,offset */
4349         seen_sp_adjust = 1;
4350       else if (inst == 0x03a1e823 ||    /* subu $sp,$sp,$at */
4351                inst == 0x03a8e823)      /* subu $sp,$sp,$t0 */
4352         seen_sp_adjust = 1;
4353       else if (((inst & 0xFFE00000) == 0xAFA00000       /* sw reg,n($sp) */
4354                 || (inst & 0xFFE00000) == 0xFFA00000)   /* sd reg,n($sp) */
4355                && (inst & 0x001F0000))  /* reg != $zero */
4356         continue;
4357
4358       else if ((inst & 0xFFE00000) == 0xE7A00000)       /* swc1 freg,n($sp) */
4359         continue;
4360       else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
4361         /* sx reg,n($s8) */
4362         continue;               /* reg != $zero */
4363
4364       /* move $s8,$sp.  With different versions of gas this will be either
4365          `addu $s8,$sp,$zero' or `or $s8,$sp,$zero' or `daddu s8,sp,$0'.
4366          Accept any one of these.  */
4367       else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
4368         continue;
4369
4370       else if ((inst & 0xFF9F07FF) == 0x00800021)       /* move reg,$a0-$a3 */
4371         continue;
4372       else if (high_word == 0x3c1c)     /* lui $gp,n */
4373         continue;
4374       else if (high_word == 0x279c)     /* addiu $gp,$gp,n */
4375         continue;
4376       else if (inst == 0x0399e021       /* addu $gp,$gp,$t9 */
4377                || inst == 0x033ce021)   /* addu $gp,$t9,$gp */
4378         continue;
4379       /* The following instructions load $at or $t0 with an immediate
4380          value in preparation for a stack adjustment via
4381          subu $sp,$sp,[$at,$t0]. These instructions could also initialize
4382          a local variable, so we accept them only before a stack adjustment
4383          instruction was seen.  */
4384       else if (!seen_sp_adjust)
4385         {
4386           if (high_word == 0x3c01 ||    /* lui $at,n */
4387               high_word == 0x3c08)      /* lui $t0,n */
4388             {
4389               load_immediate_bytes += MIPS_INSTLEN;     /* FIXME!! */
4390               continue;
4391             }
4392           else if (high_word == 0x3421 ||       /* ori $at,$at,n */
4393                    high_word == 0x3508 ||       /* ori $t0,$t0,n */
4394                    high_word == 0x3401 ||       /* ori $at,$zero,n */
4395                    high_word == 0x3408)         /* ori $t0,$zero,n */
4396             {
4397               load_immediate_bytes += MIPS_INSTLEN;     /* FIXME!! */
4398               continue;
4399             }
4400           else
4401             break;
4402         }
4403       else
4404         break;
4405     }
4406
4407   /* In a frameless function, we might have incorrectly
4408      skipped some load immediate instructions. Undo the skipping
4409      if the load immediate was not followed by a stack adjustment.  */
4410   if (load_immediate_bytes && !seen_sp_adjust)
4411     pc -= load_immediate_bytes;
4412   return pc;
4413 }
4414
4415 /* Skip the PC past function prologue instructions (16-bit version).
4416    This is a helper function for mips_skip_prologue.  */
4417
4418 static CORE_ADDR
4419 mips16_skip_prologue (CORE_ADDR pc)
4420 {
4421   CORE_ADDR end_pc;
4422   int extend_bytes = 0;
4423   int prev_extend_bytes;
4424
4425   /* Table of instructions likely to be found in a function prologue.  */
4426   static struct
4427     {
4428       unsigned short inst;
4429       unsigned short mask;
4430     }
4431   table[] =
4432   {
4433     {
4434       0x6300, 0xff00
4435     }
4436     ,                           /* addiu $sp,offset */
4437     {
4438       0xfb00, 0xff00
4439     }
4440     ,                           /* daddiu $sp,offset */
4441     {
4442       0xd000, 0xf800
4443     }
4444     ,                           /* sw reg,n($sp) */
4445     {
4446       0xf900, 0xff00
4447     }
4448     ,                           /* sd reg,n($sp) */
4449     {
4450       0x6200, 0xff00
4451     }
4452     ,                           /* sw $ra,n($sp) */
4453     {
4454       0xfa00, 0xff00
4455     }
4456     ,                           /* sd $ra,n($sp) */
4457     {
4458       0x673d, 0xffff
4459     }
4460     ,                           /* move $s1,sp */
4461     {
4462       0xd980, 0xff80
4463     }
4464     ,                           /* sw $a0-$a3,n($s1) */
4465     {
4466       0x6704, 0xff1c
4467     }
4468     ,                           /* move reg,$a0-$a3 */
4469     {
4470       0xe809, 0xf81f
4471     }
4472     ,                           /* entry pseudo-op */
4473     {
4474       0x0100, 0xff00
4475     }
4476     ,                           /* addiu $s1,$sp,n */
4477     {
4478       0, 0
4479     }                           /* end of table marker */
4480   };
4481
4482   /* Skip the typical prologue instructions. These are the stack adjustment
4483      instruction and the instructions that save registers on the stack
4484      or in the gcc frame.  */
4485   for (end_pc = pc + 100; pc < end_pc; pc += MIPS16_INSTLEN)
4486     {
4487       unsigned short inst;
4488       int i;
4489
4490       inst = mips_fetch_instruction (pc);
4491
4492       /* Normally we ignore an extend instruction.  However, if it is
4493          not followed by a valid prologue instruction, we must adjust
4494          the pc back over the extend so that it won't be considered
4495          part of the prologue.  */
4496       if ((inst & 0xf800) == 0xf000)    /* extend */
4497         {
4498           extend_bytes = MIPS16_INSTLEN;
4499           continue;
4500         }
4501       prev_extend_bytes = extend_bytes;
4502       extend_bytes = 0;
4503
4504       /* Check for other valid prologue instructions besides extend.  */
4505       for (i = 0; table[i].mask != 0; i++)
4506         if ((inst & table[i].mask) == table[i].inst)    /* found, get out */
4507           break;
4508       if (table[i].mask != 0)   /* it was in table? */
4509         continue;               /* ignore it */
4510       else
4511         /* non-prologue */
4512         {
4513           /* Return the current pc, adjusted backwards by 2 if
4514              the previous instruction was an extend.  */
4515           return pc - prev_extend_bytes;
4516         }
4517     }
4518   return pc;
4519 }
4520
4521 /* To skip prologues, I use this predicate.  Returns either PC itself
4522    if the code at PC does not look like a function prologue; otherwise
4523    returns an address that (if we're lucky) follows the prologue.  If
4524    LENIENT, then we must skip everything which is involved in setting
4525    up the frame (it's OK to skip more, just so long as we don't skip
4526    anything which might clobber the registers which are being saved.
4527    We must skip more in the case where part of the prologue is in the
4528    delay slot of a non-prologue instruction).  */
4529
4530 static CORE_ADDR
4531 mips_skip_prologue (CORE_ADDR pc)
4532 {
4533   /* See if we can determine the end of the prologue via the symbol table.
4534      If so, then return either PC, or the PC after the prologue, whichever
4535      is greater.  */
4536
4537   CORE_ADDR post_prologue_pc = after_prologue (pc, NULL);
4538
4539   if (post_prologue_pc != 0)
4540     return max (pc, post_prologue_pc);
4541
4542   /* Can't determine prologue from the symbol table, need to examine
4543      instructions.  */
4544
4545   if (pc_is_mips16 (pc))
4546     return mips16_skip_prologue (pc);
4547   else
4548     return mips32_skip_prologue (pc);
4549 }
4550
4551 /* Determine how a return value is stored within the MIPS register
4552    file, given the return type `valtype'. */
4553
4554 struct return_value_word
4555 {
4556   int len;
4557   int reg;
4558   int reg_offset;
4559   int buf_offset;
4560 };
4561
4562 static void
4563 return_value_location (struct type *valtype,
4564                        struct return_value_word *hi,
4565                        struct return_value_word *lo)
4566 {
4567   int len = TYPE_LENGTH (valtype);
4568
4569   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
4570       && ((MIPS_FPU_TYPE == MIPS_FPU_DOUBLE && (len == 4 || len == 8))
4571           || (MIPS_FPU_TYPE == MIPS_FPU_SINGLE && len == 4)))
4572     {
4573       if (!FP_REGISTER_DOUBLE && len == 8)
4574         {
4575           /* We need to break a 64bit float in two 32 bit halves and
4576              spread them across a floating-point register pair. */
4577           lo->buf_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
4578           hi->buf_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 0 : 4;
4579           lo->reg_offset = ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
4580                              && REGISTER_RAW_SIZE (FP0_REGNUM) == 8)
4581                             ? 4 : 0);
4582           hi->reg_offset = lo->reg_offset;
4583           lo->reg = FP0_REGNUM + 0;
4584           hi->reg = FP0_REGNUM + 1;
4585           lo->len = 4;
4586           hi->len = 4;
4587         }
4588       else
4589         {
4590           /* The floating point value fits in a single floating-point
4591              register. */
4592           lo->reg_offset = ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
4593                              && REGISTER_RAW_SIZE (FP0_REGNUM) == 8
4594                              && len == 4)
4595                             ? 4 : 0);
4596           lo->reg = FP0_REGNUM;
4597           lo->len = len;
4598           lo->buf_offset = 0;
4599           hi->len = 0;
4600           hi->reg_offset = 0;
4601           hi->buf_offset = 0;
4602           hi->reg = 0;
4603         }
4604     }
4605   else
4606     {
4607       /* Locate a result possibly spread across two registers. */
4608       int regnum = 2;
4609       lo->reg = regnum + 0;
4610       hi->reg = regnum + 1;
4611       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
4612           && len < MIPS_SAVED_REGSIZE)
4613         {
4614           /* "un-left-justify" the value in the low register */
4615           lo->reg_offset = MIPS_SAVED_REGSIZE - len;
4616           lo->len = len;
4617           hi->reg_offset = 0;
4618           hi->len = 0;
4619         }
4620       else if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
4621                && len > MIPS_SAVED_REGSIZE      /* odd-size structs */
4622                && len < MIPS_SAVED_REGSIZE * 2
4623                && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
4624                    TYPE_CODE (valtype) == TYPE_CODE_UNION))
4625         {
4626           /* "un-left-justify" the value spread across two registers. */
4627           lo->reg_offset = 2 * MIPS_SAVED_REGSIZE - len;
4628           lo->len = MIPS_SAVED_REGSIZE - lo->reg_offset;
4629           hi->reg_offset = 0;
4630           hi->len = len - lo->len;
4631         }
4632       else
4633         {
4634           /* Only perform a partial copy of the second register. */
4635           lo->reg_offset = 0;
4636           hi->reg_offset = 0;
4637           if (len > MIPS_SAVED_REGSIZE)
4638             {
4639               lo->len = MIPS_SAVED_REGSIZE;
4640               hi->len = len - MIPS_SAVED_REGSIZE;
4641             }
4642           else
4643             {
4644               lo->len = len;
4645               hi->len = 0;
4646             }
4647         }
4648       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
4649           && REGISTER_RAW_SIZE (regnum) == 8
4650           && MIPS_SAVED_REGSIZE == 4)
4651         {
4652           /* Account for the fact that only the least-signficant part
4653              of the register is being used */
4654           lo->reg_offset += 4;
4655           hi->reg_offset += 4;
4656         }
4657       lo->buf_offset = 0;
4658       hi->buf_offset = lo->len;
4659     }
4660 }
4661
4662 /* Given a return value in `regbuf' with a type `valtype', extract and
4663    copy its value into `valbuf'. */
4664
4665 static void
4666 mips_eabi_extract_return_value (struct type *valtype,
4667                                 char regbuf[REGISTER_BYTES],
4668                                 char *valbuf)
4669 {
4670   struct return_value_word lo;
4671   struct return_value_word hi;
4672   return_value_location (valtype, &hi, &lo);
4673
4674   memcpy (valbuf + lo.buf_offset,
4675           regbuf + REGISTER_BYTE (lo.reg) + lo.reg_offset,
4676           lo.len);
4677
4678   if (hi.len > 0)
4679     memcpy (valbuf + hi.buf_offset,
4680             regbuf + REGISTER_BYTE (hi.reg) + hi.reg_offset,
4681             hi.len);
4682 }
4683
4684 static void
4685 mips_o64_extract_return_value (struct type *valtype,
4686                                char regbuf[REGISTER_BYTES],
4687                                char *valbuf)
4688 {
4689   struct return_value_word lo;
4690   struct return_value_word hi;
4691   return_value_location (valtype, &hi, &lo);
4692
4693   memcpy (valbuf + lo.buf_offset,
4694           regbuf + REGISTER_BYTE (lo.reg) + lo.reg_offset,
4695           lo.len);
4696
4697   if (hi.len > 0)
4698     memcpy (valbuf + hi.buf_offset,
4699             regbuf + REGISTER_BYTE (hi.reg) + hi.reg_offset,
4700             hi.len);
4701 }
4702
4703 /* Given a return value in `valbuf' with a type `valtype', write it's
4704    value into the appropriate register. */
4705
4706 static void
4707 mips_eabi_store_return_value (struct type *valtype, char *valbuf)
4708 {
4709   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
4710   struct return_value_word lo;
4711   struct return_value_word hi;
4712   return_value_location (valtype, &hi, &lo);
4713
4714   memset (raw_buffer, 0, sizeof (raw_buffer));
4715   memcpy (raw_buffer + lo.reg_offset, valbuf + lo.buf_offset, lo.len);
4716   deprecated_write_register_bytes (REGISTER_BYTE (lo.reg), raw_buffer,
4717                                    REGISTER_RAW_SIZE (lo.reg));
4718
4719   if (hi.len > 0)
4720     {
4721       memset (raw_buffer, 0, sizeof (raw_buffer));
4722       memcpy (raw_buffer + hi.reg_offset, valbuf + hi.buf_offset, hi.len);
4723       deprecated_write_register_bytes (REGISTER_BYTE (hi.reg), raw_buffer,
4724                                        REGISTER_RAW_SIZE (hi.reg));
4725     }
4726 }
4727
4728 static void
4729 mips_o64_store_return_value (struct type *valtype, char *valbuf)
4730 {
4731   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
4732   struct return_value_word lo;
4733   struct return_value_word hi;
4734   return_value_location (valtype, &hi, &lo);
4735
4736   memset (raw_buffer, 0, sizeof (raw_buffer));
4737   memcpy (raw_buffer + lo.reg_offset, valbuf + lo.buf_offset, lo.len);
4738   deprecated_write_register_bytes (REGISTER_BYTE (lo.reg), raw_buffer,
4739                                    REGISTER_RAW_SIZE (lo.reg));
4740
4741   if (hi.len > 0)
4742     {
4743       memset (raw_buffer, 0, sizeof (raw_buffer));
4744       memcpy (raw_buffer + hi.reg_offset, valbuf + hi.buf_offset, hi.len);
4745       deprecated_write_register_bytes (REGISTER_BYTE (hi.reg), raw_buffer,
4746                                        REGISTER_RAW_SIZE (hi.reg));
4747     }
4748 }
4749
4750 /* O32 ABI stuff.  */
4751
4752 static void
4753 mips_o32_xfer_return_value (struct type *type,
4754                             struct regcache *regcache,
4755                             bfd_byte *in, const bfd_byte *out)
4756 {
4757   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
4758   if (TYPE_CODE (type) == TYPE_CODE_FLT
4759       && TYPE_LENGTH (type) == 4
4760       && tdep->mips_fpu_type != MIPS_FPU_NONE)
4761     {
4762       /* A single-precision floating-point value.  It fits in the
4763          least significant part of FP0.  */
4764       if (mips_debug)
4765         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4766       mips_xfer_register (regcache, FP0_REGNUM, TYPE_LENGTH (type),
4767                           TARGET_BYTE_ORDER, in, out, 0);
4768     }
4769   else if (TYPE_CODE (type) == TYPE_CODE_FLT
4770            && TYPE_LENGTH (type) == 8
4771            && tdep->mips_fpu_type != MIPS_FPU_NONE)
4772     {
4773       /* A double-precision floating-point value.  It fits in the
4774          least significant part of FP0/FP1 but with byte ordering
4775          based on the target (???).  */
4776       if (mips_debug)
4777         fprintf_unfiltered (gdb_stderr, "Return float in $fp0/$fp1\n");
4778       switch (TARGET_BYTE_ORDER)
4779         {
4780         case BFD_ENDIAN_LITTLE:
4781           mips_xfer_register (regcache, FP0_REGNUM + 0, 4,
4782                               TARGET_BYTE_ORDER, in, out, 0);
4783           mips_xfer_register (regcache, FP0_REGNUM + 1, 4,
4784                               TARGET_BYTE_ORDER, in, out, 4);
4785           break;
4786         case BFD_ENDIAN_BIG:
4787           mips_xfer_register (regcache, FP0_REGNUM + 1, 4,
4788                               TARGET_BYTE_ORDER, in, out, 0);
4789           mips_xfer_register (regcache, FP0_REGNUM + 0, 4,
4790                               TARGET_BYTE_ORDER, in, out, 4);
4791           break;
4792         default:
4793           internal_error (__FILE__, __LINE__, "bad switch");
4794         }
4795     }
4796 #if 0
4797   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4798            && TYPE_NFIELDS (type) <= 2
4799            && TYPE_NFIELDS (type) >= 1
4800            && ((TYPE_NFIELDS (type) == 1
4801                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4802                     == TYPE_CODE_FLT))
4803                || (TYPE_NFIELDS (type) == 2
4804                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4805                        == TYPE_CODE_FLT)
4806                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
4807                        == TYPE_CODE_FLT)))
4808            && tdep->mips_fpu_type != MIPS_FPU_NONE)
4809     {
4810       /* A struct that contains one or two floats.  Each value is part
4811          in the least significant part of their floating point
4812          register..  */
4813       bfd_byte *reg = alloca (MAX_REGISTER_RAW_SIZE);
4814       int regnum;
4815       int field;
4816       for (field = 0, regnum = FP0_REGNUM;
4817            field < TYPE_NFIELDS (type);
4818            field++, regnum += 2)
4819         {
4820           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
4821                         / TARGET_CHAR_BIT);
4822           if (mips_debug)
4823             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n", offset);
4824           mips_xfer_register (regcache, regnum, TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
4825                               TARGET_BYTE_ORDER, in, out, offset);
4826         }
4827     }
4828 #endif
4829 #if 0
4830   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4831            || TYPE_CODE (type) == TYPE_CODE_UNION)
4832     {
4833       /* A structure or union.  Extract the left justified value,
4834          regardless of the byte order.  I.e. DO NOT USE
4835          mips_xfer_lower.  */
4836       int offset;
4837       int regnum;
4838       for (offset = 0, regnum = V0_REGNUM;
4839            offset < TYPE_LENGTH (type);
4840            offset += REGISTER_RAW_SIZE (regnum), regnum++)
4841         {
4842           int xfer = REGISTER_RAW_SIZE (regnum);
4843           if (offset + xfer > TYPE_LENGTH (type))
4844             xfer = TYPE_LENGTH (type) - offset;
4845           if (mips_debug)
4846             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
4847                                 offset, xfer, regnum);
4848           mips_xfer_register (regcache, regnum, xfer, BFD_ENDIAN_UNKNOWN,
4849                               in, out, offset);
4850         }
4851     }
4852 #endif
4853   else
4854     {
4855       /* A scalar extract each part but least-significant-byte
4856          justified.  o32 thinks registers are 4 byte, regardless of
4857          the ISA.  mips_stack_argsize controls this.  */
4858       int offset;
4859       int regnum;
4860       for (offset = 0, regnum = V0_REGNUM;
4861            offset < TYPE_LENGTH (type);
4862            offset += mips_stack_argsize (), regnum++)
4863         {
4864           int xfer = mips_stack_argsize ();
4865           int pos = 0;
4866           if (offset + xfer > TYPE_LENGTH (type))
4867             xfer = TYPE_LENGTH (type) - offset;
4868           if (mips_debug)
4869             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4870                                 offset, xfer, regnum);
4871           mips_xfer_register (regcache, regnum, xfer, TARGET_BYTE_ORDER,
4872                               in, out, offset);
4873         }
4874     }
4875 }
4876
4877 static void
4878 mips_o32_extract_return_value (struct type *type,
4879                                struct regcache *regcache,
4880                                void *valbuf)
4881 {
4882   mips_o32_xfer_return_value (type, regcache, valbuf, NULL); 
4883 }
4884
4885 static void
4886 mips_o32_store_return_value (struct type *type, char *valbuf)
4887 {
4888   mips_o32_xfer_return_value (type, current_regcache, NULL, valbuf); 
4889 }
4890
4891 /* N32/N44 ABI stuff.  */
4892
4893 static void
4894 mips_n32n64_xfer_return_value (struct type *type,
4895                                struct regcache *regcache,
4896                                bfd_byte *in, const bfd_byte *out)
4897 {
4898   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
4899   if (TYPE_CODE (type) == TYPE_CODE_FLT
4900       && tdep->mips_fpu_type != MIPS_FPU_NONE)
4901     {
4902       /* A floating-point value belongs in the least significant part
4903          of FP0.  */
4904       if (mips_debug)
4905         fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4906       mips_xfer_register (regcache, FP0_REGNUM, TYPE_LENGTH (type),
4907                           TARGET_BYTE_ORDER, in, out, 0);
4908     }
4909   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4910            && TYPE_NFIELDS (type) <= 2
4911            && TYPE_NFIELDS (type) >= 1
4912            && ((TYPE_NFIELDS (type) == 1
4913                 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4914                     == TYPE_CODE_FLT))
4915                || (TYPE_NFIELDS (type) == 2
4916                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4917                        == TYPE_CODE_FLT)
4918                    && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
4919                        == TYPE_CODE_FLT)))
4920            && tdep->mips_fpu_type != MIPS_FPU_NONE)
4921     {
4922       /* A struct that contains one or two floats.  Each value is part
4923          in the least significant part of their floating point
4924          register..  */
4925       bfd_byte *reg = alloca (MAX_REGISTER_RAW_SIZE);
4926       int regnum;
4927       int field;
4928       for (field = 0, regnum = FP0_REGNUM;
4929            field < TYPE_NFIELDS (type);
4930            field++, regnum += 2)
4931         {
4932           int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
4933                         / TARGET_CHAR_BIT);
4934           if (mips_debug)
4935             fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n", offset);
4936           mips_xfer_register (regcache, regnum, TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
4937                               TARGET_BYTE_ORDER, in, out, offset);
4938         }
4939     }
4940   else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4941            || TYPE_CODE (type) == TYPE_CODE_UNION)
4942     {
4943       /* A structure or union.  Extract the left justified value,
4944          regardless of the byte order.  I.e. DO NOT USE
4945          mips_xfer_lower.  */
4946       int offset;
4947       int regnum;
4948       for (offset = 0, regnum = V0_REGNUM;
4949            offset < TYPE_LENGTH (type);
4950            offset += REGISTER_RAW_SIZE (regnum), regnum++)
4951         {
4952           int xfer = REGISTER_RAW_SIZE (regnum);
4953           if (offset + xfer > TYPE_LENGTH (type))
4954             xfer = TYPE_LENGTH (type) - offset;
4955           if (mips_debug)
4956             fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
4957                                 offset, xfer, regnum);
4958           mips_xfer_register (regcache, regnum, xfer, BFD_ENDIAN_UNKNOWN,
4959                               in, out, offset);
4960         }
4961     }
4962   else
4963     {
4964       /* A scalar extract each part but least-significant-byte
4965          justified.  */
4966       int offset;
4967       int regnum;
4968       for (offset = 0, regnum = V0_REGNUM;
4969            offset < TYPE_LENGTH (type);
4970            offset += REGISTER_RAW_SIZE (regnum), regnum++)
4971         {
4972           int xfer = REGISTER_RAW_SIZE (regnum);
4973           int pos = 0;
4974           if (offset + xfer > TYPE_LENGTH (type))
4975             xfer = TYPE_LENGTH (type) - offset;
4976           if (mips_debug)
4977             fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4978                                 offset, xfer, regnum);
4979           mips_xfer_register (regcache, regnum, xfer, TARGET_BYTE_ORDER,
4980                               in, out, offset);
4981         }
4982     }
4983 }
4984
4985 static void
4986 mips_n32n64_extract_return_value (struct type *type,
4987                                   struct regcache *regcache,
4988                                   void *valbuf)
4989 {
4990   mips_n32n64_xfer_return_value (type, regcache, valbuf, NULL);
4991 }
4992
4993 static void
4994 mips_n32n64_store_return_value (struct type *type, char *valbuf)
4995 {
4996   mips_n32n64_xfer_return_value (type, current_regcache, NULL, valbuf);
4997 }
4998
4999 static void
5000 mips_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
5001 {
5002   /* Nothing to do -- push_arguments does all the work.  */
5003 }
5004
5005 static CORE_ADDR
5006 mips_extract_struct_value_address (struct regcache *regcache)
5007 {
5008   /* FIXME: This will only work at random.  The caller passes the
5009      struct_return address in V0, but it is not preserved.  It may
5010      still be there, or this may be a random value.  */
5011   LONGEST val;
5012
5013   regcache_cooked_read_signed (regcache, V0_REGNUM, &val);
5014   return val;
5015 }
5016
5017 /* Exported procedure: Is PC in the signal trampoline code */
5018
5019 static int
5020 mips_pc_in_sigtramp (CORE_ADDR pc, char *ignore)
5021 {
5022   if (sigtramp_address == 0)
5023     fixup_sigtramp ();
5024   return (pc >= sigtramp_address && pc < sigtramp_end);
5025 }
5026
5027 /* Root of all "set mips "/"show mips " commands. This will eventually be
5028    used for all MIPS-specific commands.  */
5029
5030 static void
5031 show_mips_command (char *args, int from_tty)
5032 {
5033   help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
5034 }
5035
5036 static void
5037 set_mips_command (char *args, int from_tty)
5038 {
5039   printf_unfiltered ("\"set mips\" must be followed by an appropriate subcommand.\n");
5040   help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
5041 }
5042
5043 /* Commands to show/set the MIPS FPU type.  */
5044
5045 static void
5046 show_mipsfpu_command (char *args, int from_tty)
5047 {
5048   char *fpu;
5049   switch (MIPS_FPU_TYPE)
5050     {
5051     case MIPS_FPU_SINGLE:
5052       fpu = "single-precision";
5053       break;
5054     case MIPS_FPU_DOUBLE:
5055       fpu = "double-precision";
5056       break;
5057     case MIPS_FPU_NONE:
5058       fpu = "absent (none)";
5059       break;
5060     default:
5061       internal_error (__FILE__, __LINE__, "bad switch");
5062     }
5063   if (mips_fpu_type_auto)
5064     printf_unfiltered ("The MIPS floating-point coprocessor is set automatically (currently %s)\n",
5065                        fpu);
5066   else
5067     printf_unfiltered ("The MIPS floating-point coprocessor is assumed to be %s\n",
5068                        fpu);
5069 }
5070
5071
5072 static void
5073 set_mipsfpu_command (char *args, int from_tty)
5074 {
5075   printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", \"single\",\"none\" or \"auto\".\n");
5076   show_mipsfpu_command (args, from_tty);
5077 }
5078
5079 static void
5080 set_mipsfpu_single_command (char *args, int from_tty)
5081 {
5082   mips_fpu_type = MIPS_FPU_SINGLE;
5083   mips_fpu_type_auto = 0;
5084   gdbarch_tdep (current_gdbarch)->mips_fpu_type = MIPS_FPU_SINGLE;
5085 }
5086
5087 static void
5088 set_mipsfpu_double_command (char *args, int from_tty)
5089 {
5090   mips_fpu_type = MIPS_FPU_DOUBLE;
5091   mips_fpu_type_auto = 0;
5092   gdbarch_tdep (current_gdbarch)->mips_fpu_type = MIPS_FPU_DOUBLE;
5093 }
5094
5095 static void
5096 set_mipsfpu_none_command (char *args, int from_tty)
5097 {
5098   mips_fpu_type = MIPS_FPU_NONE;
5099   mips_fpu_type_auto = 0;
5100   gdbarch_tdep (current_gdbarch)->mips_fpu_type = MIPS_FPU_NONE;
5101 }
5102
5103 static void
5104 set_mipsfpu_auto_command (char *args, int from_tty)
5105 {
5106   mips_fpu_type_auto = 1;
5107 }
5108
5109 /* Command to set the processor type.  */
5110
5111 void
5112 mips_set_processor_type_command (char *args, int from_tty)
5113 {
5114   int i;
5115
5116   if (tmp_mips_processor_type == NULL || *tmp_mips_processor_type == '\0')
5117     {
5118       printf_unfiltered ("The known MIPS processor types are as follows:\n\n");
5119       for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
5120         printf_unfiltered ("%s\n", mips_processor_type_table[i].name);
5121
5122       /* Restore the value.  */
5123       tmp_mips_processor_type = xstrdup (mips_processor_type);
5124
5125       return;
5126     }
5127
5128   if (!mips_set_processor_type (tmp_mips_processor_type))
5129     {
5130       error ("Unknown processor type `%s'.", tmp_mips_processor_type);
5131       /* Restore its value.  */
5132       tmp_mips_processor_type = xstrdup (mips_processor_type);
5133     }
5134 }
5135
5136 static void
5137 mips_show_processor_type_command (char *args, int from_tty)
5138 {
5139 }
5140
5141 /* Modify the actual processor type. */
5142
5143 static int
5144 mips_set_processor_type (char *str)
5145 {
5146   int i;
5147
5148   if (str == NULL)
5149     return 0;
5150
5151   for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
5152     {
5153       if (strcasecmp (str, mips_processor_type_table[i].name) == 0)
5154         {
5155           mips_processor_type = str;
5156           mips_processor_reg_names = mips_processor_type_table[i].regnames;
5157           return 1;
5158           /* FIXME tweak fpu flag too */
5159         }
5160     }
5161
5162   return 0;
5163 }
5164
5165 /* Attempt to identify the particular processor model by reading the
5166    processor id.  */
5167
5168 char *
5169 mips_read_processor_type (void)
5170 {
5171   CORE_ADDR prid;
5172
5173   prid = read_register (PRID_REGNUM);
5174
5175   if ((prid & ~0xf) == 0x700)
5176     return savestring ("r3041", strlen ("r3041"));
5177
5178   return NULL;
5179 }
5180
5181 /* Just like reinit_frame_cache, but with the right arguments to be
5182    callable as an sfunc.  */
5183
5184 static void
5185 reinit_frame_cache_sfunc (char *args, int from_tty,
5186                           struct cmd_list_element *c)
5187 {
5188   reinit_frame_cache ();
5189 }
5190
5191 int
5192 gdb_print_insn_mips (bfd_vma memaddr, disassemble_info *info)
5193 {
5194   mips_extra_func_info_t proc_desc;
5195
5196   /* Search for the function containing this address.  Set the low bit
5197      of the address when searching, in case we were given an even address
5198      that is the start of a 16-bit function.  If we didn't do this,
5199      the search would fail because the symbol table says the function
5200      starts at an odd address, i.e. 1 byte past the given address.  */
5201   memaddr = ADDR_BITS_REMOVE (memaddr);
5202   proc_desc = non_heuristic_proc_desc (MAKE_MIPS16_ADDR (memaddr), NULL);
5203
5204   /* Make an attempt to determine if this is a 16-bit function.  If
5205      the procedure descriptor exists and the address therein is odd,
5206      it's definitely a 16-bit function.  Otherwise, we have to just
5207      guess that if the address passed in is odd, it's 16-bits.  */
5208   if (proc_desc)
5209     info->mach = pc_is_mips16 (PROC_LOW_ADDR (proc_desc)) ?
5210       bfd_mach_mips16 : TM_PRINT_INSN_MACH;
5211   else
5212     info->mach = pc_is_mips16 (memaddr) ?
5213       bfd_mach_mips16 : TM_PRINT_INSN_MACH;
5214
5215   /* Round down the instruction address to the appropriate boundary.  */
5216   memaddr &= (info->mach == bfd_mach_mips16 ? ~1 : ~3);
5217
5218   /* Call the appropriate disassembler based on the target endian-ness.  */
5219   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
5220     return print_insn_big_mips (memaddr, info);
5221   else
5222     return print_insn_little_mips (memaddr, info);
5223 }
5224
5225 /* Old-style breakpoint macros.
5226    The IDT board uses an unusual breakpoint value, and sometimes gets
5227    confused when it sees the usual MIPS breakpoint instruction.  */
5228
5229 #define BIG_BREAKPOINT {0, 0x5, 0, 0xd}
5230 #define LITTLE_BREAKPOINT {0xd, 0, 0x5, 0}
5231 #define PMON_BIG_BREAKPOINT {0, 0, 0, 0xd}
5232 #define PMON_LITTLE_BREAKPOINT {0xd, 0, 0, 0}
5233 #define IDT_BIG_BREAKPOINT {0, 0, 0x0a, 0xd}
5234 #define IDT_LITTLE_BREAKPOINT {0xd, 0x0a, 0, 0}
5235 #define MIPS16_BIG_BREAKPOINT {0xe8, 0xa5}
5236 #define MIPS16_LITTLE_BREAKPOINT {0xa5, 0xe8}
5237
5238 /* This function implements the BREAKPOINT_FROM_PC macro.  It uses the program
5239    counter value to determine whether a 16- or 32-bit breakpoint should be
5240    used.  It returns a pointer to a string of bytes that encode a breakpoint
5241    instruction, stores the length of the string to *lenptr, and adjusts pc
5242    (if necessary) to point to the actual memory location where the
5243    breakpoint should be inserted.  */
5244
5245 static const unsigned char *
5246 mips_breakpoint_from_pc (CORE_ADDR * pcptr, int *lenptr)
5247 {
5248   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
5249     {
5250       if (pc_is_mips16 (*pcptr))
5251         {
5252           static unsigned char mips16_big_breakpoint[] =
5253             MIPS16_BIG_BREAKPOINT;
5254           *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
5255           *lenptr = sizeof (mips16_big_breakpoint);
5256           return mips16_big_breakpoint;
5257         }
5258       else
5259         {
5260           static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
5261           static unsigned char pmon_big_breakpoint[] = PMON_BIG_BREAKPOINT;
5262           static unsigned char idt_big_breakpoint[] = IDT_BIG_BREAKPOINT;
5263
5264           *lenptr = sizeof (big_breakpoint);
5265
5266           if (strcmp (target_shortname, "mips") == 0)
5267             return idt_big_breakpoint;
5268           else if (strcmp (target_shortname, "ddb") == 0
5269                    || strcmp (target_shortname, "pmon") == 0
5270                    || strcmp (target_shortname, "lsi") == 0)
5271             return pmon_big_breakpoint;
5272           else
5273             return big_breakpoint;
5274         }
5275     }
5276   else
5277     {
5278       if (pc_is_mips16 (*pcptr))
5279         {
5280           static unsigned char mips16_little_breakpoint[] =
5281             MIPS16_LITTLE_BREAKPOINT;
5282           *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
5283           *lenptr = sizeof (mips16_little_breakpoint);
5284           return mips16_little_breakpoint;
5285         }
5286       else
5287         {
5288           static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
5289           static unsigned char pmon_little_breakpoint[] =
5290             PMON_LITTLE_BREAKPOINT;
5291           static unsigned char idt_little_breakpoint[] =
5292             IDT_LITTLE_BREAKPOINT;
5293
5294           *lenptr = sizeof (little_breakpoint);
5295
5296           if (strcmp (target_shortname, "mips") == 0)
5297             return idt_little_breakpoint;
5298           else if (strcmp (target_shortname, "ddb") == 0
5299                    || strcmp (target_shortname, "pmon") == 0
5300                    || strcmp (target_shortname, "lsi") == 0)
5301             return pmon_little_breakpoint;
5302           else
5303             return little_breakpoint;
5304         }
5305     }
5306 }
5307
5308 /* If PC is in a mips16 call or return stub, return the address of the target
5309    PC, which is either the callee or the caller.  There are several
5310    cases which must be handled:
5311
5312    * If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
5313    target PC is in $31 ($ra).
5314    * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
5315    and the target PC is in $2.
5316    * If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
5317    before the jal instruction, this is effectively a call stub
5318    and the the target PC is in $2.  Otherwise this is effectively
5319    a return stub and the target PC is in $18.
5320
5321    See the source code for the stubs in gcc/config/mips/mips16.S for
5322    gory details.
5323
5324    This function implements the SKIP_TRAMPOLINE_CODE macro.
5325  */
5326
5327 static CORE_ADDR
5328 mips_skip_stub (CORE_ADDR pc)
5329 {
5330   char *name;
5331   CORE_ADDR start_addr;
5332
5333   /* Find the starting address and name of the function containing the PC.  */
5334   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
5335     return 0;
5336
5337   /* If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
5338      target PC is in $31 ($ra).  */
5339   if (strcmp (name, "__mips16_ret_sf") == 0
5340       || strcmp (name, "__mips16_ret_df") == 0)
5341     return read_signed_register (RA_REGNUM);
5342
5343   if (strncmp (name, "__mips16_call_stub_", 19) == 0)
5344     {
5345       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
5346          and the target PC is in $2.  */
5347       if (name[19] >= '0' && name[19] <= '9')
5348         return read_signed_register (2);
5349
5350       /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
5351          before the jal instruction, this is effectively a call stub
5352          and the the target PC is in $2.  Otherwise this is effectively
5353          a return stub and the target PC is in $18.  */
5354       else if (name[19] == 's' || name[19] == 'd')
5355         {
5356           if (pc == start_addr)
5357             {
5358               /* Check if the target of the stub is a compiler-generated
5359                  stub.  Such a stub for a function bar might have a name
5360                  like __fn_stub_bar, and might look like this:
5361                  mfc1    $4,$f13
5362                  mfc1    $5,$f12
5363                  mfc1    $6,$f15
5364                  mfc1    $7,$f14
5365                  la      $1,bar   (becomes a lui/addiu pair)
5366                  jr      $1
5367                  So scan down to the lui/addi and extract the target
5368                  address from those two instructions.  */
5369
5370               CORE_ADDR target_pc = read_signed_register (2);
5371               t_inst inst;
5372               int i;
5373
5374               /* See if the name of the target function is  __fn_stub_*.  */
5375               if (find_pc_partial_function (target_pc, &name, NULL, NULL) == 0)
5376                 return target_pc;
5377               if (strncmp (name, "__fn_stub_", 10) != 0
5378                   && strcmp (name, "etext") != 0
5379                   && strcmp (name, "_etext") != 0)
5380                 return target_pc;
5381
5382               /* Scan through this _fn_stub_ code for the lui/addiu pair.
5383                  The limit on the search is arbitrarily set to 20
5384                  instructions.  FIXME.  */
5385               for (i = 0, pc = 0; i < 20; i++, target_pc += MIPS_INSTLEN)
5386                 {
5387                   inst = mips_fetch_instruction (target_pc);
5388                   if ((inst & 0xffff0000) == 0x3c010000)        /* lui $at */
5389                     pc = (inst << 16) & 0xffff0000;     /* high word */
5390                   else if ((inst & 0xffff0000) == 0x24210000)   /* addiu $at */
5391                     return pc | (inst & 0xffff);        /* low word */
5392                 }
5393
5394               /* Couldn't find the lui/addui pair, so return stub address.  */
5395               return target_pc;
5396             }
5397           else
5398             /* This is the 'return' part of a call stub.  The return
5399                address is in $r18.  */
5400             return read_signed_register (18);
5401         }
5402     }
5403   return 0;                     /* not a stub */
5404 }
5405
5406
5407 /* Return non-zero if the PC is inside a call thunk (aka stub or trampoline).
5408    This implements the IN_SOLIB_CALL_TRAMPOLINE macro.  */
5409
5410 static int
5411 mips_in_call_stub (CORE_ADDR pc, char *name)
5412 {
5413   CORE_ADDR start_addr;
5414
5415   /* Find the starting address of the function containing the PC.  If the
5416      caller didn't give us a name, look it up at the same time.  */
5417   if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
5418     return 0;
5419
5420   if (strncmp (name, "__mips16_call_stub_", 19) == 0)
5421     {
5422       /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub.  */
5423       if (name[19] >= '0' && name[19] <= '9')
5424         return 1;
5425       /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
5426          before the jal instruction, this is effectively a call stub.  */
5427       else if (name[19] == 's' || name[19] == 'd')
5428         return pc == start_addr;
5429     }
5430
5431   return 0;                     /* not a stub */
5432 }
5433
5434
5435 /* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
5436    This implements the IN_SOLIB_RETURN_TRAMPOLINE macro.  */
5437
5438 static int
5439 mips_in_return_stub (CORE_ADDR pc, char *name)
5440 {
5441   CORE_ADDR start_addr;
5442
5443   /* Find the starting address of the function containing the PC.  */
5444   if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
5445     return 0;
5446
5447   /* If the PC is in __mips16_ret_{d,s}f, this is a return stub.  */
5448   if (strcmp (name, "__mips16_ret_sf") == 0
5449       || strcmp (name, "__mips16_ret_df") == 0)
5450     return 1;
5451
5452   /* If the PC is in __mips16_call_stub_{s,d}f_{0..10} but not at the start,
5453      i.e. after the jal instruction, this is effectively a return stub.  */
5454   if (strncmp (name, "__mips16_call_stub_", 19) == 0
5455       && (name[19] == 's' || name[19] == 'd')
5456       && pc != start_addr)
5457     return 1;
5458
5459   return 0;                     /* not a stub */
5460 }
5461
5462
5463 /* Return non-zero if the PC is in a library helper function that should
5464    be ignored.  This implements the IGNORE_HELPER_CALL macro.  */
5465
5466 int
5467 mips_ignore_helper (CORE_ADDR pc)
5468 {
5469   char *name;
5470
5471   /* Find the starting address and name of the function containing the PC.  */
5472   if (find_pc_partial_function (pc, &name, NULL, NULL) == 0)
5473     return 0;
5474
5475   /* If the PC is in __mips16_ret_{d,s}f, this is a library helper function
5476      that we want to ignore.  */
5477   return (strcmp (name, "__mips16_ret_sf") == 0
5478           || strcmp (name, "__mips16_ret_df") == 0);
5479 }
5480
5481
5482 /* Return a location where we can set a breakpoint that will be hit
5483    when an inferior function call returns.  This is normally the
5484    program's entry point.  Executables that don't have an entry
5485    point (e.g. programs in ROM) should define a symbol __CALL_DUMMY_ADDRESS
5486    whose address is the location where the breakpoint should be placed.  */
5487
5488 static CORE_ADDR
5489 mips_call_dummy_address (void)
5490 {
5491   struct minimal_symbol *sym;
5492
5493   sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
5494   if (sym)
5495     return SYMBOL_VALUE_ADDRESS (sym);
5496   else
5497     return entry_point_address ();
5498 }
5499
5500
5501 /* When debugging a 64 MIPS target running a 32 bit ABI, the size of
5502    the register stored on the stack (32) is different to its real raw
5503    size (64).  The below ensures that registers are fetched from the
5504    stack using their ABI size and then stored into the RAW_BUFFER
5505    using their raw size.
5506
5507    The alternative to adding this function would be to add an ABI
5508    macro - REGISTER_STACK_SIZE(). */
5509
5510 static void
5511 mips_get_saved_register (char *raw_buffer,
5512                          int *optimizedp,
5513                          CORE_ADDR *addrp,
5514                          struct frame_info *frame,
5515                          int regnum,
5516                          enum lval_type *lvalp)
5517 {
5518   CORE_ADDR addrx;
5519   enum lval_type lvalx;
5520   int optimizedx;
5521
5522   if (!target_has_registers)
5523     error ("No registers.");
5524
5525   /* Make certain that all needed parameters are present.  */
5526   if (addrp == NULL)
5527     addrp = &addrx;
5528   if (lvalp == NULL)
5529     lvalp = &lvalx;
5530   if (optimizedp == NULL)
5531     optimizedp = &optimizedx;
5532   generic_unwind_get_saved_register (raw_buffer, optimizedp, addrp, frame,
5533                                      regnum, lvalp);
5534   /* FIXME: cagney/2002-09-13: This is just so bad.  The MIPS should
5535      have a pseudo register range that correspons to the ABI's, rather
5536      than the ISA's, view of registers.  These registers would then
5537      implicitly describe their size and hence could be used without
5538      the below munging.  */
5539   if ((*lvalp) == lval_memory)
5540     {
5541       if (raw_buffer != NULL)
5542         {
5543           if (regnum < 32)
5544             {
5545               /* Only MIPS_SAVED_REGSIZE bytes of GP registers are
5546                  saved. */
5547               LONGEST val = read_memory_integer ((*addrp), MIPS_SAVED_REGSIZE);
5548               store_address (raw_buffer, REGISTER_RAW_SIZE (regnum), val);
5549             }
5550         }
5551     }
5552 }
5553
5554 /* Immediately after a function call, return the saved pc.
5555    Can't always go through the frames for this because on some machines
5556    the new frame is not set up until the new function executes
5557    some instructions.  */
5558
5559 static CORE_ADDR
5560 mips_saved_pc_after_call (struct frame_info *frame)
5561 {
5562   return read_signed_register (RA_REGNUM);
5563 }
5564
5565
5566 /* Convert a dbx stab register number (from `r' declaration) to a gdb
5567    REGNUM */
5568
5569 static int
5570 mips_stab_reg_to_regnum (int num)
5571 {
5572   if (num >= 0 && num < 32)
5573     return num;
5574   else if (num >= 38 && num < 70)
5575     return num + FP0_REGNUM - 38;
5576   else
5577     {
5578       /* This will hopefully (eventually) provoke a warning.  Should
5579          we be calling complaint() here?  */
5580       return NUM_REGS + NUM_PSEUDO_REGS;
5581     }
5582 }
5583
5584
5585 /* Convert a dwarf, dwarf2, or ecoff register number to a gdb REGNUM */
5586
5587 static int
5588 mips_dwarf_dwarf2_ecoff_reg_to_regnum (int num)
5589 {
5590   if (num >= 0 && num < 32)
5591     return num;
5592   else if (num >= 32 && num < 64)
5593     return num + FP0_REGNUM - 32;
5594   else
5595     {
5596       /* This will hopefully (eventually) provoke a warning.  Should
5597          we be calling complaint() here?  */
5598       return NUM_REGS + NUM_PSEUDO_REGS;
5599     }
5600 }
5601
5602
5603 /* Convert an integer into an address.  By first converting the value
5604    into a pointer and then extracting it signed, the address is
5605    guarenteed to be correctly sign extended.  */
5606
5607 static CORE_ADDR
5608 mips_integer_to_address (struct type *type, void *buf)
5609 {
5610   char *tmp = alloca (TYPE_LENGTH (builtin_type_void_data_ptr));
5611   LONGEST val = unpack_long (type, buf);
5612   store_signed_integer (tmp, TYPE_LENGTH (builtin_type_void_data_ptr), val);
5613   return extract_signed_integer (tmp,
5614                                  TYPE_LENGTH (builtin_type_void_data_ptr));
5615 }
5616
5617 static void
5618 mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
5619 {
5620   enum mips_abi *abip = (enum mips_abi *) obj;
5621   const char *name = bfd_get_section_name (abfd, sect);
5622
5623   if (*abip != MIPS_ABI_UNKNOWN)
5624     return;
5625
5626   if (strncmp (name, ".mdebug.", 8) != 0)
5627     return;
5628
5629   if (strcmp (name, ".mdebug.abi32") == 0)
5630     *abip = MIPS_ABI_O32;
5631   else if (strcmp (name, ".mdebug.abiN32") == 0)
5632     *abip = MIPS_ABI_N32;
5633   else if (strcmp (name, ".mdebug.abi64") == 0)
5634     *abip = MIPS_ABI_N64;
5635   else if (strcmp (name, ".mdebug.abiO64") == 0)
5636     *abip = MIPS_ABI_O64;
5637   else if (strcmp (name, ".mdebug.eabi32") == 0)
5638     *abip = MIPS_ABI_EABI32;
5639   else if (strcmp (name, ".mdebug.eabi64") == 0)
5640     *abip = MIPS_ABI_EABI64;
5641   else
5642     warning ("unsupported ABI %s.", name + 8);
5643 }
5644
5645 static enum mips_abi
5646 global_mips_abi (void)
5647 {
5648   int i;
5649
5650   for (i = 0; mips_abi_strings[i] != NULL; i++)
5651     if (mips_abi_strings[i] == mips_abi_string)
5652       return (enum mips_abi) i;
5653
5654   internal_error (__FILE__, __LINE__,
5655                   "unknown ABI string");
5656 }
5657
5658 static struct gdbarch *
5659 mips_gdbarch_init (struct gdbarch_info info,
5660                    struct gdbarch_list *arches)
5661 {
5662   static LONGEST mips_call_dummy_words[] =
5663   {0};
5664   struct gdbarch *gdbarch;
5665   struct gdbarch_tdep *tdep;
5666   int elf_flags;
5667   enum mips_abi mips_abi, found_abi, wanted_abi;
5668
5669   /* Reset the disassembly info, in case it was set to something
5670      non-default.  */
5671   tm_print_insn_info.flavour = bfd_target_unknown_flavour;
5672   tm_print_insn_info.arch = bfd_arch_unknown;
5673   tm_print_insn_info.mach = 0;
5674
5675   elf_flags = 0;
5676
5677   if (info.abfd)
5678     {
5679       /* First of all, extract the elf_flags, if available.  */
5680       if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
5681         elf_flags = elf_elfheader (info.abfd)->e_flags;
5682     }
5683
5684   /* Check ELF_FLAGS to see if it specifies the ABI being used.  */
5685   switch ((elf_flags & EF_MIPS_ABI))
5686     {
5687     case E_MIPS_ABI_O32:
5688       mips_abi = MIPS_ABI_O32;
5689       break;
5690     case E_MIPS_ABI_O64:
5691       mips_abi = MIPS_ABI_O64;
5692       break;
5693     case E_MIPS_ABI_EABI32:
5694       mips_abi = MIPS_ABI_EABI32;
5695       break;
5696     case E_MIPS_ABI_EABI64:
5697       mips_abi = MIPS_ABI_EABI64;
5698       break;
5699     default:
5700       if ((elf_flags & EF_MIPS_ABI2))
5701         mips_abi = MIPS_ABI_N32;
5702       else
5703         mips_abi = MIPS_ABI_UNKNOWN;
5704       break;
5705     }
5706
5707   /* GCC creates a pseudo-section whose name describes the ABI.  */
5708   if (mips_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
5709     bfd_map_over_sections (info.abfd, mips_find_abi_section, &mips_abi);
5710
5711   /* If we have no bfd, then mips_abi will still be MIPS_ABI_UNKNOWN.
5712      Use the ABI from the last architecture if there is one.  */
5713   if (info.abfd == NULL && arches != NULL)
5714     mips_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
5715
5716   /* Try the architecture for any hint of the correct ABI.  */
5717   if (mips_abi == MIPS_ABI_UNKNOWN
5718       && info.bfd_arch_info != NULL
5719       && info.bfd_arch_info->arch == bfd_arch_mips)
5720     {
5721       switch (info.bfd_arch_info->mach)
5722         {
5723         case bfd_mach_mips3900:
5724           mips_abi = MIPS_ABI_EABI32;
5725           break;
5726         case bfd_mach_mips4100:
5727         case bfd_mach_mips5000:
5728           mips_abi = MIPS_ABI_EABI64;
5729           break;
5730         case bfd_mach_mips8000:
5731         case bfd_mach_mips10000:
5732           /* On Irix, ELF64 executables use the N64 ABI.  The
5733              pseudo-sections which describe the ABI aren't present
5734              on IRIX.  (Even for executables created by gcc.)  */
5735           if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
5736               && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5737             mips_abi = MIPS_ABI_N64;
5738           else
5739             mips_abi = MIPS_ABI_N32;
5740           break;
5741         }
5742     }
5743
5744   if (mips_abi == MIPS_ABI_UNKNOWN)
5745     mips_abi = MIPS_ABI_O32;
5746
5747   /* Now that we have found what the ABI for this binary would be,
5748      check whether the user is overriding it.  */
5749   found_abi = mips_abi;
5750   wanted_abi = global_mips_abi ();
5751   if (wanted_abi != MIPS_ABI_UNKNOWN)
5752     mips_abi = wanted_abi;
5753
5754   if (gdbarch_debug)
5755     {
5756       fprintf_unfiltered (gdb_stdlog,
5757                           "mips_gdbarch_init: elf_flags = 0x%08x\n",
5758                           elf_flags);
5759       fprintf_unfiltered (gdb_stdlog,
5760                           "mips_gdbarch_init: mips_abi = %d\n",
5761                           mips_abi);
5762       fprintf_unfiltered (gdb_stdlog,
5763                           "mips_gdbarch_init: found_mips_abi = %d\n",
5764                           found_abi);
5765     }
5766
5767   /* try to find a pre-existing architecture */
5768   for (arches = gdbarch_list_lookup_by_info (arches, &info);
5769        arches != NULL;
5770        arches = gdbarch_list_lookup_by_info (arches->next, &info))
5771     {
5772       /* MIPS needs to be pedantic about which ABI the object is
5773          using.  */
5774       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
5775         continue;
5776       if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
5777         continue;
5778       return arches->gdbarch;
5779     }
5780
5781   /* Need a new architecture.  Fill in a target specific vector.  */
5782   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
5783   gdbarch = gdbarch_alloc (&info, tdep);
5784   tdep->elf_flags = elf_flags;
5785
5786   /* Initially set everything according to the default ABI/ISA.  */
5787   set_gdbarch_short_bit (gdbarch, 16);
5788   set_gdbarch_int_bit (gdbarch, 32);
5789   set_gdbarch_float_bit (gdbarch, 32);
5790   set_gdbarch_double_bit (gdbarch, 64);
5791   set_gdbarch_long_double_bit (gdbarch, 64);
5792   set_gdbarch_register_raw_size (gdbarch, mips_register_raw_size);
5793   set_gdbarch_deprecated_max_register_raw_size (gdbarch, 8);
5794   set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 8);
5795   tdep->found_abi = found_abi;
5796   tdep->mips_abi = mips_abi;
5797
5798   set_gdbarch_elf_make_msymbol_special (gdbarch, 
5799                                         mips_elf_make_msymbol_special);
5800
5801   if (info.osabi == GDB_OSABI_IRIX)
5802     set_gdbarch_num_regs (gdbarch, 71);
5803   else
5804     set_gdbarch_num_regs (gdbarch, 90);
5805
5806   switch (mips_abi)
5807     {
5808     case MIPS_ABI_O32:
5809       set_gdbarch_deprecated_push_arguments (gdbarch, mips_o32_push_arguments);
5810       set_gdbarch_deprecated_store_return_value (gdbarch, mips_o32_store_return_value);
5811       set_gdbarch_extract_return_value (gdbarch, mips_o32_extract_return_value);
5812       tdep->mips_default_saved_regsize = 4;
5813       tdep->mips_default_stack_argsize = 4;
5814       tdep->mips_fp_register_double = 0;
5815       tdep->mips_last_arg_regnum = A0_REGNUM + 4 - 1;
5816       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 4 - 1;
5817       tdep->gdb_target_is_mips64 = 0;
5818       tdep->default_mask_address_p = 0;
5819       set_gdbarch_long_bit (gdbarch, 32);
5820       set_gdbarch_ptr_bit (gdbarch, 32);
5821       set_gdbarch_long_long_bit (gdbarch, 64);
5822       set_gdbarch_reg_struct_has_addr (gdbarch, 
5823                                        mips_o32_reg_struct_has_addr);
5824       set_gdbarch_use_struct_convention (gdbarch, 
5825                                          mips_o32_use_struct_convention);
5826       break;
5827     case MIPS_ABI_O64:
5828       set_gdbarch_deprecated_push_arguments (gdbarch, mips_o64_push_arguments);
5829       set_gdbarch_deprecated_store_return_value (gdbarch, mips_o64_store_return_value);
5830       set_gdbarch_deprecated_extract_return_value (gdbarch, mips_o64_extract_return_value);
5831       tdep->mips_default_saved_regsize = 8;
5832       tdep->mips_default_stack_argsize = 8;
5833       tdep->mips_fp_register_double = 1;
5834       tdep->mips_last_arg_regnum = A0_REGNUM + 4 - 1;
5835       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 4 - 1;
5836       tdep->gdb_target_is_mips64 = 1;
5837       tdep->default_mask_address_p = 0;
5838       set_gdbarch_long_bit (gdbarch, 32);
5839       set_gdbarch_ptr_bit (gdbarch, 32);
5840       set_gdbarch_long_long_bit (gdbarch, 64);
5841       set_gdbarch_reg_struct_has_addr (gdbarch, 
5842                                        mips_o32_reg_struct_has_addr);
5843       set_gdbarch_use_struct_convention (gdbarch, 
5844                                          mips_o32_use_struct_convention);
5845       break;
5846     case MIPS_ABI_EABI32:
5847       set_gdbarch_deprecated_push_arguments (gdbarch, mips_eabi_push_arguments);
5848       set_gdbarch_deprecated_store_return_value (gdbarch, mips_eabi_store_return_value);
5849       set_gdbarch_deprecated_extract_return_value (gdbarch, mips_eabi_extract_return_value);
5850       tdep->mips_default_saved_regsize = 4;
5851       tdep->mips_default_stack_argsize = 4;
5852       tdep->mips_fp_register_double = 0;
5853       tdep->mips_last_arg_regnum = A0_REGNUM + 8 - 1;
5854       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 8 - 1;
5855       tdep->gdb_target_is_mips64 = 0;
5856       tdep->default_mask_address_p = 0;
5857       set_gdbarch_long_bit (gdbarch, 32);
5858       set_gdbarch_ptr_bit (gdbarch, 32);
5859       set_gdbarch_long_long_bit (gdbarch, 64);
5860       set_gdbarch_reg_struct_has_addr (gdbarch, 
5861                                        mips_eabi_reg_struct_has_addr);
5862       set_gdbarch_use_struct_convention (gdbarch, 
5863                                          mips_eabi_use_struct_convention);
5864       break;
5865     case MIPS_ABI_EABI64:
5866       set_gdbarch_deprecated_push_arguments (gdbarch, mips_eabi_push_arguments);
5867       set_gdbarch_deprecated_store_return_value (gdbarch, mips_eabi_store_return_value);
5868       set_gdbarch_deprecated_extract_return_value (gdbarch, mips_eabi_extract_return_value);
5869       tdep->mips_default_saved_regsize = 8;
5870       tdep->mips_default_stack_argsize = 8;
5871       tdep->mips_fp_register_double = 1;
5872       tdep->mips_last_arg_regnum = A0_REGNUM + 8 - 1;
5873       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 8 - 1;
5874       tdep->gdb_target_is_mips64 = 1;
5875       tdep->default_mask_address_p = 0;
5876       set_gdbarch_long_bit (gdbarch, 64);
5877       set_gdbarch_ptr_bit (gdbarch, 64);
5878       set_gdbarch_long_long_bit (gdbarch, 64);
5879       set_gdbarch_reg_struct_has_addr (gdbarch, 
5880                                        mips_eabi_reg_struct_has_addr);
5881       set_gdbarch_use_struct_convention (gdbarch, 
5882                                          mips_eabi_use_struct_convention);
5883       break;
5884     case MIPS_ABI_N32:
5885       set_gdbarch_deprecated_push_arguments (gdbarch, mips_n32n64_push_arguments);
5886       set_gdbarch_deprecated_store_return_value (gdbarch, mips_n32n64_store_return_value);
5887       set_gdbarch_extract_return_value (gdbarch, mips_n32n64_extract_return_value);
5888       tdep->mips_default_saved_regsize = 8;
5889       tdep->mips_default_stack_argsize = 8;
5890       tdep->mips_fp_register_double = 1;
5891       tdep->mips_last_arg_regnum = A0_REGNUM + 8 - 1;
5892       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 8 - 1;
5893       tdep->gdb_target_is_mips64 = 1;
5894       tdep->default_mask_address_p = 0;
5895       set_gdbarch_long_bit (gdbarch, 32);
5896       set_gdbarch_ptr_bit (gdbarch, 32);
5897       set_gdbarch_long_long_bit (gdbarch, 64);
5898
5899       /* Set up the disassembler info, so that we get the right
5900          register names from libopcodes.  */
5901       tm_print_insn_info.flavour = bfd_target_elf_flavour;
5902       tm_print_insn_info.arch = bfd_arch_mips;
5903       if (info.bfd_arch_info != NULL
5904           && info.bfd_arch_info->arch == bfd_arch_mips
5905           && info.bfd_arch_info->mach)
5906         tm_print_insn_info.mach = info.bfd_arch_info->mach;
5907       else
5908         tm_print_insn_info.mach = bfd_mach_mips8000;
5909
5910       set_gdbarch_use_struct_convention (gdbarch, 
5911                                          mips_n32n64_use_struct_convention);
5912       set_gdbarch_reg_struct_has_addr (gdbarch, 
5913                                        mips_n32n64_reg_struct_has_addr);
5914       break;
5915     case MIPS_ABI_N64:
5916       set_gdbarch_deprecated_push_arguments (gdbarch, mips_n32n64_push_arguments);
5917       set_gdbarch_deprecated_store_return_value (gdbarch, mips_n32n64_store_return_value);
5918       set_gdbarch_extract_return_value (gdbarch, mips_n32n64_extract_return_value);
5919       tdep->mips_default_saved_regsize = 8;
5920       tdep->mips_default_stack_argsize = 8;
5921       tdep->mips_fp_register_double = 1;
5922       tdep->mips_last_arg_regnum = A0_REGNUM + 8 - 1;
5923       tdep->mips_last_fp_arg_regnum = FPA0_REGNUM + 8 - 1;
5924       tdep->gdb_target_is_mips64 = 1;
5925       tdep->default_mask_address_p = 0;
5926       set_gdbarch_long_bit (gdbarch, 64);
5927       set_gdbarch_ptr_bit (gdbarch, 64);
5928       set_gdbarch_long_long_bit (gdbarch, 64);
5929
5930       /* Set up the disassembler info, so that we get the right
5931          register names from libopcodes.  */
5932       tm_print_insn_info.flavour = bfd_target_elf_flavour;
5933       tm_print_insn_info.arch = bfd_arch_mips;
5934       if (info.bfd_arch_info != NULL
5935           && info.bfd_arch_info->arch == bfd_arch_mips
5936           && info.bfd_arch_info->mach)
5937         tm_print_insn_info.mach = info.bfd_arch_info->mach;
5938       else
5939         tm_print_insn_info.mach = bfd_mach_mips8000;
5940
5941       set_gdbarch_use_struct_convention (gdbarch, 
5942                                          mips_n32n64_use_struct_convention);
5943       set_gdbarch_reg_struct_has_addr (gdbarch, 
5944                                        mips_n32n64_reg_struct_has_addr);
5945       break;
5946     default:
5947       internal_error (__FILE__, __LINE__,
5948                       "unknown ABI in switch");
5949     }
5950
5951   /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
5952      that could indicate -gp32 BUT gas/config/tc-mips.c contains the
5953      comment:
5954
5955      ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
5956      flag in object files because to do so would make it impossible to
5957      link with libraries compiled without "-gp32".  This is
5958      unnecessarily restrictive.
5959
5960      We could solve this problem by adding "-gp32" multilibs to gcc,
5961      but to set this flag before gcc is built with such multilibs will
5962      break too many systems.''
5963
5964      But even more unhelpfully, the default linker output target for
5965      mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
5966      for 64-bit programs - you need to change the ABI to change this,
5967      and not all gcc targets support that currently.  Therefore using
5968      this flag to detect 32-bit mode would do the wrong thing given
5969      the current gcc - it would make GDB treat these 64-bit programs
5970      as 32-bit programs by default.  */
5971
5972   /* enable/disable the MIPS FPU */
5973   if (!mips_fpu_type_auto)
5974     tdep->mips_fpu_type = mips_fpu_type;
5975   else if (info.bfd_arch_info != NULL
5976            && info.bfd_arch_info->arch == bfd_arch_mips)
5977     switch (info.bfd_arch_info->mach)
5978       {
5979       case bfd_mach_mips3900:
5980       case bfd_mach_mips4100:
5981       case bfd_mach_mips4111:
5982         tdep->mips_fpu_type = MIPS_FPU_NONE;
5983         break;
5984       case bfd_mach_mips4650:
5985         tdep->mips_fpu_type = MIPS_FPU_SINGLE;
5986         break;
5987       default:
5988         tdep->mips_fpu_type = MIPS_FPU_DOUBLE;
5989         break;
5990       }
5991   else
5992     tdep->mips_fpu_type = MIPS_FPU_DOUBLE;
5993
5994   /* MIPS version of register names.  NOTE: At present the MIPS
5995      register name management is part way between the old -
5996      #undef/#define REGISTER_NAMES and the new REGISTER_NAME(nr).
5997      Further work on it is required.  */
5998   /* NOTE: many targets (esp. embedded) do not go thru the
5999      gdbarch_register_name vector at all, instead bypassing it
6000      by defining REGISTER_NAMES.  */
6001   set_gdbarch_register_name (gdbarch, mips_register_name);
6002   set_gdbarch_read_pc (gdbarch, mips_read_pc);
6003   set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
6004   set_gdbarch_read_fp (gdbarch, mips_read_sp); /* Draft FRAME base.  */
6005   set_gdbarch_read_sp (gdbarch, mips_read_sp);
6006   set_gdbarch_deprecated_dummy_write_sp (gdbarch, generic_target_write_sp);
6007
6008   /* Add/remove bits from an address.  The MIPS needs be careful to
6009      ensure that all 32 bit addresses are sign extended to 64 bits.  */
6010   set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
6011
6012   /* There's a mess in stack frame creation.  See comments in
6013      blockframe.c near reference to DEPRECATED_INIT_FRAME_PC_FIRST.  */
6014   set_gdbarch_deprecated_init_frame_pc_first (gdbarch, mips_init_frame_pc_first);
6015   set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_noop);
6016
6017   /* Map debug register numbers onto internal register numbers.  */
6018   set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
6019   set_gdbarch_ecoff_reg_to_regnum (gdbarch, mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6020   set_gdbarch_dwarf_reg_to_regnum (gdbarch, mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6021   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6022
6023   /* Initialize a frame */
6024   set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, mips_frame_init_saved_regs);
6025   set_gdbarch_deprecated_init_extra_frame_info (gdbarch, mips_init_extra_frame_info);
6026
6027   /* MIPS version of CALL_DUMMY */
6028
6029   set_gdbarch_call_dummy_p (gdbarch, 1);
6030   set_gdbarch_call_dummy_address (gdbarch, mips_call_dummy_address);
6031   set_gdbarch_deprecated_push_return_address (gdbarch, mips_push_return_address);
6032   set_gdbarch_deprecated_pop_frame (gdbarch, mips_pop_frame);
6033   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
6034   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
6035   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
6036   set_gdbarch_call_dummy_length (gdbarch, 0);
6037   set_gdbarch_fix_call_dummy (gdbarch, mips_fix_call_dummy);
6038   set_gdbarch_call_dummy_words (gdbarch, mips_call_dummy_words);
6039   set_gdbarch_sizeof_call_dummy_words (gdbarch, sizeof (mips_call_dummy_words));
6040   set_gdbarch_deprecated_push_return_address (gdbarch, mips_push_return_address);
6041   set_gdbarch_frame_align (gdbarch, mips_frame_align);
6042   set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
6043   set_gdbarch_register_convertible (gdbarch, mips_register_convertible);
6044   set_gdbarch_register_convert_to_virtual (gdbarch, 
6045                                            mips_register_convert_to_virtual);
6046   set_gdbarch_register_convert_to_raw (gdbarch, 
6047                                        mips_register_convert_to_raw);
6048
6049   set_gdbarch_deprecated_frame_chain (gdbarch, mips_frame_chain);
6050   set_gdbarch_frameless_function_invocation (gdbarch, 
6051                                              generic_frameless_function_invocation_not);
6052   set_gdbarch_deprecated_frame_saved_pc (gdbarch, mips_frame_saved_pc);
6053   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
6054   set_gdbarch_frame_args_skip (gdbarch, 0);
6055
6056   set_gdbarch_deprecated_get_saved_register (gdbarch, mips_get_saved_register);
6057
6058   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
6059   set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
6060   set_gdbarch_decr_pc_after_break (gdbarch, 0);
6061
6062   set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
6063   set_gdbarch_saved_pc_after_call (gdbarch, mips_saved_pc_after_call);
6064
6065   set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
6066   set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
6067   set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
6068
6069   set_gdbarch_function_start_offset (gdbarch, 0);
6070
6071   /* There are MIPS targets which do not yet use this since they still
6072      define REGISTER_VIRTUAL_TYPE.  */
6073   set_gdbarch_register_virtual_type (gdbarch, mips_register_virtual_type);
6074   set_gdbarch_register_virtual_size (gdbarch, generic_register_size);
6075
6076   set_gdbarch_deprecated_do_registers_info (gdbarch, mips_do_registers_info);
6077   set_gdbarch_pc_in_sigtramp (gdbarch, mips_pc_in_sigtramp);
6078
6079   /* Hook in OS ABI-specific overrides, if they have been registered.  */
6080   gdbarch_init_osabi (info, gdbarch);
6081
6082   set_gdbarch_deprecated_store_struct_return (gdbarch, mips_store_struct_return);
6083   set_gdbarch_extract_struct_value_address (gdbarch, 
6084                                             mips_extract_struct_value_address);
6085   
6086   set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_stub);
6087
6088   set_gdbarch_in_solib_call_trampoline (gdbarch, mips_in_call_stub);
6089   set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);
6090
6091   return gdbarch;
6092 }
6093
6094 static void
6095 mips_abi_update (char *ignore_args, int from_tty, 
6096                  struct cmd_list_element *c)
6097 {
6098   struct gdbarch_info info;
6099
6100   /* Force the architecture to update, and (if it's a MIPS architecture)
6101      mips_gdbarch_init will take care of the rest.  */
6102   gdbarch_info_init (&info);
6103   gdbarch_update_p (info);
6104 }
6105
6106 /* Print out which MIPS ABI is in use.  */
6107
6108 static void
6109 show_mips_abi (char *ignore_args, int from_tty)
6110 {
6111   if (gdbarch_bfd_arch_info (current_gdbarch)->arch != bfd_arch_mips)
6112     printf_filtered (
6113       "The MIPS ABI is unknown because the current architecture is not MIPS.\n");
6114   else
6115     {
6116       enum mips_abi global_abi = global_mips_abi ();
6117       enum mips_abi actual_abi = mips_abi (current_gdbarch);
6118       const char *actual_abi_str = mips_abi_strings[actual_abi];
6119
6120       if (global_abi == MIPS_ABI_UNKNOWN)
6121         printf_filtered ("The MIPS ABI is set automatically (currently \"%s\").\n",
6122                          actual_abi_str);
6123       else if (global_abi == actual_abi)
6124         printf_filtered (
6125           "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
6126           actual_abi_str);
6127       else
6128         {
6129           /* Probably shouldn't happen...  */
6130           printf_filtered (
6131             "The (auto detected) MIPS ABI \"%s\" is in use even though the user setting was \"%s\".\n",
6132             actual_abi_str,
6133             mips_abi_strings[global_abi]);
6134         }
6135     }
6136 }
6137
6138 static void
6139 mips_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
6140 {
6141   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
6142   if (tdep != NULL)
6143     {
6144       int ef_mips_arch;
6145       int ef_mips_32bitmode;
6146       /* determine the ISA */
6147       switch (tdep->elf_flags & EF_MIPS_ARCH)
6148         {
6149         case E_MIPS_ARCH_1:
6150           ef_mips_arch = 1;
6151           break;
6152         case E_MIPS_ARCH_2:
6153           ef_mips_arch = 2;
6154           break;
6155         case E_MIPS_ARCH_3:
6156           ef_mips_arch = 3;
6157           break;
6158         case E_MIPS_ARCH_4:
6159           ef_mips_arch = 4;
6160           break;
6161         default:
6162           ef_mips_arch = 0;
6163           break;
6164         }
6165       /* determine the size of a pointer */
6166       ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
6167       fprintf_unfiltered (file,
6168                           "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
6169                           tdep->elf_flags);
6170       fprintf_unfiltered (file,
6171                           "mips_dump_tdep: ef_mips_32bitmode = %d\n",
6172                           ef_mips_32bitmode);
6173       fprintf_unfiltered (file,
6174                           "mips_dump_tdep: ef_mips_arch = %d\n",
6175                           ef_mips_arch);
6176       fprintf_unfiltered (file,
6177                           "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
6178                           tdep->mips_abi,
6179                           mips_abi_strings[tdep->mips_abi]);
6180       fprintf_unfiltered (file,
6181                           "mips_dump_tdep: mips_mask_address_p() %d (default %d)\n",
6182                           mips_mask_address_p (),
6183                           tdep->default_mask_address_p);
6184     }
6185   fprintf_unfiltered (file,
6186                       "mips_dump_tdep: FP_REGISTER_DOUBLE = %d\n",
6187                       FP_REGISTER_DOUBLE);
6188   fprintf_unfiltered (file,
6189                       "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
6190                       MIPS_DEFAULT_FPU_TYPE,
6191                       (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
6192                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
6193                        : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
6194                        : "???"));
6195   fprintf_unfiltered (file,
6196                       "mips_dump_tdep: MIPS_EABI = %d\n",
6197                       MIPS_EABI);
6198   fprintf_unfiltered (file,
6199                       "mips_dump_tdep: MIPS_LAST_FP_ARG_REGNUM = %d (%d regs)\n",
6200                       MIPS_LAST_FP_ARG_REGNUM,
6201                       MIPS_LAST_FP_ARG_REGNUM - FPA0_REGNUM + 1);
6202   fprintf_unfiltered (file,
6203                       "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
6204                       MIPS_FPU_TYPE,
6205                       (MIPS_FPU_TYPE == MIPS_FPU_NONE ? "none"
6206                        : MIPS_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
6207                        : MIPS_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
6208                        : "???"));
6209   fprintf_unfiltered (file,
6210                       "mips_dump_tdep: MIPS_DEFAULT_SAVED_REGSIZE = %d\n",
6211                       MIPS_DEFAULT_SAVED_REGSIZE);
6212   fprintf_unfiltered (file,
6213                       "mips_dump_tdep: FP_REGISTER_DOUBLE = %d\n",
6214                       FP_REGISTER_DOUBLE);
6215   fprintf_unfiltered (file,
6216                       "mips_dump_tdep: MIPS_DEFAULT_STACK_ARGSIZE = %d\n",
6217                       MIPS_DEFAULT_STACK_ARGSIZE);
6218   fprintf_unfiltered (file,
6219                       "mips_dump_tdep: MIPS_STACK_ARGSIZE = %d\n",
6220                       MIPS_STACK_ARGSIZE);
6221   fprintf_unfiltered (file,
6222                       "mips_dump_tdep: MIPS_REGSIZE = %d\n",
6223                       MIPS_REGSIZE);
6224   fprintf_unfiltered (file,
6225                       "mips_dump_tdep: A0_REGNUM = %d\n",
6226                       A0_REGNUM);
6227   fprintf_unfiltered (file,
6228                       "mips_dump_tdep: ADDR_BITS_REMOVE # %s\n",
6229                       XSTRING (ADDR_BITS_REMOVE(ADDR)));
6230   fprintf_unfiltered (file,
6231                       "mips_dump_tdep: ATTACH_DETACH # %s\n",
6232                       XSTRING (ATTACH_DETACH));
6233   fprintf_unfiltered (file,
6234                       "mips_dump_tdep: BADVADDR_REGNUM = %d\n",
6235                       BADVADDR_REGNUM);
6236   fprintf_unfiltered (file,
6237                       "mips_dump_tdep: BIG_BREAKPOINT = delete?\n");
6238   fprintf_unfiltered (file,
6239                       "mips_dump_tdep: CAUSE_REGNUM = %d\n",
6240                       CAUSE_REGNUM);
6241   fprintf_unfiltered (file,
6242                       "mips_dump_tdep: DWARF_REG_TO_REGNUM # %s\n",
6243                       XSTRING (DWARF_REG_TO_REGNUM (REGNUM)));
6244   fprintf_unfiltered (file,
6245                       "mips_dump_tdep: ECOFF_REG_TO_REGNUM # %s\n",
6246                       XSTRING (ECOFF_REG_TO_REGNUM (REGNUM)));
6247   fprintf_unfiltered (file,
6248                       "mips_dump_tdep: FCRCS_REGNUM = %d\n",
6249                       FCRCS_REGNUM);
6250   fprintf_unfiltered (file,
6251                       "mips_dump_tdep: FCRIR_REGNUM = %d\n",
6252                       FCRIR_REGNUM);
6253   fprintf_unfiltered (file,
6254                       "mips_dump_tdep: FIRST_EMBED_REGNUM = %d\n",
6255                       FIRST_EMBED_REGNUM);
6256   fprintf_unfiltered (file,
6257                       "mips_dump_tdep: FPA0_REGNUM = %d\n",
6258                       FPA0_REGNUM);
6259   fprintf_unfiltered (file,
6260                       "mips_dump_tdep: GDB_TARGET_IS_MIPS64 = %d\n",
6261                       GDB_TARGET_IS_MIPS64);
6262   fprintf_unfiltered (file,
6263                       "mips_dump_tdep: GEN_REG_SAVE_MASK = %d\n",
6264                       GEN_REG_SAVE_MASK);
6265   fprintf_unfiltered (file,
6266                       "mips_dump_tdep: HAVE_NONSTEPPABLE_WATCHPOINT # %s\n",
6267                       XSTRING (HAVE_NONSTEPPABLE_WATCHPOINT));
6268   fprintf_unfiltered (file,
6269                       "mips_dump_tdep:  HI_REGNUM = %d\n",
6270                       HI_REGNUM);
6271   fprintf_unfiltered (file,
6272                       "mips_dump_tdep: IDT_BIG_BREAKPOINT = delete?\n");
6273   fprintf_unfiltered (file,
6274                       "mips_dump_tdep: IDT_LITTLE_BREAKPOINT = delete?\n");
6275   fprintf_unfiltered (file,
6276                       "mips_dump_tdep: IGNORE_HELPER_CALL # %s\n",
6277                       XSTRING (IGNORE_HELPER_CALL (PC)));
6278   fprintf_unfiltered (file,
6279                       "mips_dump_tdep: IN_SOLIB_CALL_TRAMPOLINE # %s\n",
6280                       XSTRING (IN_SOLIB_CALL_TRAMPOLINE (PC, NAME)));
6281   fprintf_unfiltered (file,
6282                       "mips_dump_tdep: IN_SOLIB_RETURN_TRAMPOLINE # %s\n",
6283                       XSTRING (IN_SOLIB_RETURN_TRAMPOLINE (PC, NAME)));
6284   fprintf_unfiltered (file,
6285                       "mips_dump_tdep: IS_MIPS16_ADDR = FIXME!\n");
6286   fprintf_unfiltered (file,
6287                       "mips_dump_tdep: LAST_EMBED_REGNUM = %d\n",
6288                       LAST_EMBED_REGNUM);
6289   fprintf_unfiltered (file,
6290                       "mips_dump_tdep: LITTLE_BREAKPOINT = delete?\n");
6291   fprintf_unfiltered (file,
6292                       "mips_dump_tdep: LO_REGNUM = %d\n",
6293                       LO_REGNUM);
6294 #ifdef MACHINE_CPROC_FP_OFFSET
6295   fprintf_unfiltered (file,
6296                       "mips_dump_tdep: MACHINE_CPROC_FP_OFFSET = %d\n",
6297                       MACHINE_CPROC_FP_OFFSET);
6298 #endif
6299 #ifdef MACHINE_CPROC_PC_OFFSET
6300   fprintf_unfiltered (file,
6301                       "mips_dump_tdep: MACHINE_CPROC_PC_OFFSET = %d\n",
6302                       MACHINE_CPROC_PC_OFFSET);
6303 #endif
6304 #ifdef MACHINE_CPROC_SP_OFFSET
6305   fprintf_unfiltered (file,
6306                       "mips_dump_tdep: MACHINE_CPROC_SP_OFFSET = %d\n",
6307                       MACHINE_CPROC_SP_OFFSET);
6308 #endif
6309   fprintf_unfiltered (file,
6310                       "mips_dump_tdep: MAKE_MIPS16_ADDR = FIXME!\n");
6311   fprintf_unfiltered (file,
6312                       "mips_dump_tdep: MIPS16_BIG_BREAKPOINT = delete?\n");
6313   fprintf_unfiltered (file,
6314                       "mips_dump_tdep: MIPS16_INSTLEN = %d\n",
6315                       MIPS16_INSTLEN);
6316   fprintf_unfiltered (file,
6317                       "mips_dump_tdep: MIPS16_LITTLE_BREAKPOINT = delete?\n");
6318   fprintf_unfiltered (file,
6319                       "mips_dump_tdep: MIPS_DEFAULT_ABI = FIXME!\n");
6320   fprintf_unfiltered (file,
6321                       "mips_dump_tdep: MIPS_EFI_SYMBOL_NAME = multi-arch!!\n");
6322   fprintf_unfiltered (file,
6323                       "mips_dump_tdep: MIPS_INSTLEN = %d\n",
6324                       MIPS_INSTLEN);
6325   fprintf_unfiltered (file,
6326                       "mips_dump_tdep: MIPS_LAST_ARG_REGNUM = %d (%d regs)\n",
6327                       MIPS_LAST_ARG_REGNUM,
6328                       MIPS_LAST_ARG_REGNUM - A0_REGNUM + 1);
6329   fprintf_unfiltered (file,
6330                       "mips_dump_tdep: MIPS_NUMREGS = %d\n",
6331                       MIPS_NUMREGS);
6332   fprintf_unfiltered (file,
6333                       "mips_dump_tdep: MIPS_REGISTER_NAMES = delete?\n");
6334   fprintf_unfiltered (file,
6335                       "mips_dump_tdep: MIPS_SAVED_REGSIZE = %d\n",
6336                       MIPS_SAVED_REGSIZE);
6337   fprintf_unfiltered (file,
6338                       "mips_dump_tdep: OP_LDFPR = used?\n");
6339   fprintf_unfiltered (file,
6340                       "mips_dump_tdep: OP_LDGPR = used?\n");
6341   fprintf_unfiltered (file,
6342                       "mips_dump_tdep: PMON_BIG_BREAKPOINT = delete?\n");
6343   fprintf_unfiltered (file,
6344                       "mips_dump_tdep: PMON_LITTLE_BREAKPOINT = delete?\n");
6345   fprintf_unfiltered (file,
6346                       "mips_dump_tdep: PRID_REGNUM = %d\n",
6347                       PRID_REGNUM);
6348   fprintf_unfiltered (file,
6349                       "mips_dump_tdep: PRINT_EXTRA_FRAME_INFO # %s\n",
6350                       XSTRING (PRINT_EXTRA_FRAME_INFO (FRAME)));
6351   fprintf_unfiltered (file,
6352                       "mips_dump_tdep: PROC_DESC_IS_DUMMY = function?\n");
6353   fprintf_unfiltered (file,
6354                       "mips_dump_tdep: PROC_FRAME_ADJUST = function?\n");
6355   fprintf_unfiltered (file,
6356                       "mips_dump_tdep: PROC_FRAME_OFFSET = function?\n");
6357   fprintf_unfiltered (file,
6358                       "mips_dump_tdep: PROC_FRAME_REG = function?\n");
6359   fprintf_unfiltered (file,
6360                       "mips_dump_tdep: PROC_FREG_MASK = function?\n");
6361   fprintf_unfiltered (file,
6362                       "mips_dump_tdep: PROC_FREG_OFFSET = function?\n");
6363   fprintf_unfiltered (file,
6364                       "mips_dump_tdep: PROC_HIGH_ADDR = function?\n");
6365   fprintf_unfiltered (file,
6366                       "mips_dump_tdep: PROC_LOW_ADDR = function?\n");
6367   fprintf_unfiltered (file,
6368                       "mips_dump_tdep: PROC_PC_REG = function?\n");
6369   fprintf_unfiltered (file,
6370                       "mips_dump_tdep: PROC_REG_MASK = function?\n");
6371   fprintf_unfiltered (file,
6372                       "mips_dump_tdep: PROC_REG_OFFSET = function?\n");
6373   fprintf_unfiltered (file,
6374                       "mips_dump_tdep: PROC_SYMBOL = function?\n");
6375   fprintf_unfiltered (file,
6376                       "mips_dump_tdep: PS_REGNUM = %d\n",
6377                       PS_REGNUM);
6378   fprintf_unfiltered (file,
6379                       "mips_dump_tdep: PUSH_FP_REGNUM = %d\n",
6380                       PUSH_FP_REGNUM);
6381   fprintf_unfiltered (file,
6382                       "mips_dump_tdep: RA_REGNUM = %d\n",
6383                       RA_REGNUM);
6384   fprintf_unfiltered (file,
6385                       "mips_dump_tdep: REGISTER_CONVERT_FROM_TYPE # %s\n",
6386                       XSTRING (REGISTER_CONVERT_FROM_TYPE (REGNUM, VALTYPE, RAW_BUFFER)));
6387   fprintf_unfiltered (file,
6388                       "mips_dump_tdep: REGISTER_CONVERT_TO_TYPE # %s\n",
6389                       XSTRING (REGISTER_CONVERT_TO_TYPE (REGNUM, VALTYPE, RAW_BUFFER)));
6390   fprintf_unfiltered (file,
6391                       "mips_dump_tdep: REGISTER_NAMES = delete?\n");
6392   fprintf_unfiltered (file,
6393                       "mips_dump_tdep: ROUND_DOWN = function?\n");
6394   fprintf_unfiltered (file,
6395                       "mips_dump_tdep: ROUND_UP = function?\n");
6396 #ifdef SAVED_BYTES
6397   fprintf_unfiltered (file,
6398                       "mips_dump_tdep: SAVED_BYTES = %d\n",
6399                       SAVED_BYTES);
6400 #endif
6401 #ifdef SAVED_FP
6402   fprintf_unfiltered (file,
6403                       "mips_dump_tdep: SAVED_FP = %d\n",
6404                       SAVED_FP);
6405 #endif
6406 #ifdef SAVED_PC
6407   fprintf_unfiltered (file,
6408                       "mips_dump_tdep: SAVED_PC = %d\n",
6409                       SAVED_PC);
6410 #endif
6411   fprintf_unfiltered (file,
6412                       "mips_dump_tdep: SETUP_ARBITRARY_FRAME # %s\n",
6413                       XSTRING (SETUP_ARBITRARY_FRAME (NUMARGS, ARGS)));
6414   fprintf_unfiltered (file,
6415                       "mips_dump_tdep: SET_PROC_DESC_IS_DUMMY = function?\n");
6416   fprintf_unfiltered (file,
6417                       "mips_dump_tdep: SIGFRAME_BASE = %d\n",
6418                       SIGFRAME_BASE);
6419   fprintf_unfiltered (file,
6420                       "mips_dump_tdep: SIGFRAME_FPREGSAVE_OFF = %d\n",
6421                       SIGFRAME_FPREGSAVE_OFF);
6422   fprintf_unfiltered (file,
6423                       "mips_dump_tdep: SIGFRAME_PC_OFF = %d\n",
6424                       SIGFRAME_PC_OFF);
6425   fprintf_unfiltered (file,
6426                       "mips_dump_tdep: SIGFRAME_REGSAVE_OFF = %d\n",
6427                       SIGFRAME_REGSAVE_OFF);
6428   fprintf_unfiltered (file,
6429                       "mips_dump_tdep: SIGFRAME_REG_SIZE = %d\n",
6430                       SIGFRAME_REG_SIZE);
6431   fprintf_unfiltered (file,
6432                       "mips_dump_tdep: SKIP_TRAMPOLINE_CODE # %s\n",
6433                       XSTRING (SKIP_TRAMPOLINE_CODE (PC)));
6434   fprintf_unfiltered (file,
6435                       "mips_dump_tdep: SOFTWARE_SINGLE_STEP # %s\n",
6436                       XSTRING (SOFTWARE_SINGLE_STEP (SIG, BP_P)));
6437   fprintf_unfiltered (file,
6438                       "mips_dump_tdep: SOFTWARE_SINGLE_STEP_P () = %d\n",
6439                       SOFTWARE_SINGLE_STEP_P ());
6440   fprintf_unfiltered (file,
6441                       "mips_dump_tdep: STAB_REG_TO_REGNUM # %s\n",
6442                       XSTRING (STAB_REG_TO_REGNUM (REGNUM)));
6443 #ifdef STACK_END_ADDR
6444   fprintf_unfiltered (file,
6445                       "mips_dump_tdep: STACK_END_ADDR = %d\n",
6446                       STACK_END_ADDR);
6447 #endif
6448   fprintf_unfiltered (file,
6449                       "mips_dump_tdep: STEP_SKIPS_DELAY # %s\n",
6450                       XSTRING (STEP_SKIPS_DELAY (PC)));
6451   fprintf_unfiltered (file,
6452                       "mips_dump_tdep: STEP_SKIPS_DELAY_P = %d\n",
6453                       STEP_SKIPS_DELAY_P);
6454   fprintf_unfiltered (file,
6455                       "mips_dump_tdep: STOPPED_BY_WATCHPOINT # %s\n",
6456                       XSTRING (STOPPED_BY_WATCHPOINT (WS)));
6457   fprintf_unfiltered (file,
6458                       "mips_dump_tdep: T9_REGNUM = %d\n",
6459                       T9_REGNUM);
6460   fprintf_unfiltered (file,
6461                       "mips_dump_tdep: TABULAR_REGISTER_OUTPUT = used?\n");
6462   fprintf_unfiltered (file,
6463                       "mips_dump_tdep: TARGET_CAN_USE_HARDWARE_WATCHPOINT # %s\n",
6464                       XSTRING (TARGET_CAN_USE_HARDWARE_WATCHPOINT (TYPE,CNT,OTHERTYPE)));
6465   fprintf_unfiltered (file,
6466                       "mips_dump_tdep: TARGET_HAS_HARDWARE_WATCHPOINTS # %s\n",
6467                       XSTRING (TARGET_HAS_HARDWARE_WATCHPOINTS));
6468   fprintf_unfiltered (file,
6469                       "mips_dump_tdep: TARGET_MIPS = used?\n");
6470   fprintf_unfiltered (file,
6471                       "mips_dump_tdep: TM_PRINT_INSN_MACH # %s\n",
6472                       XSTRING (TM_PRINT_INSN_MACH));
6473 #ifdef TRACE_CLEAR
6474   fprintf_unfiltered (file,
6475                       "mips_dump_tdep: TRACE_CLEAR # %s\n",
6476                       XSTRING (TRACE_CLEAR (THREAD, STATE)));
6477 #endif
6478 #ifdef TRACE_FLAVOR
6479   fprintf_unfiltered (file,
6480                       "mips_dump_tdep: TRACE_FLAVOR = %d\n",
6481                       TRACE_FLAVOR);
6482 #endif
6483 #ifdef TRACE_FLAVOR_SIZE
6484   fprintf_unfiltered (file,
6485                       "mips_dump_tdep: TRACE_FLAVOR_SIZE = %d\n",
6486                       TRACE_FLAVOR_SIZE);
6487 #endif
6488 #ifdef TRACE_SET
6489   fprintf_unfiltered (file,
6490                       "mips_dump_tdep: TRACE_SET # %s\n",
6491                       XSTRING (TRACE_SET (X,STATE)));
6492 #endif
6493   fprintf_unfiltered (file,
6494                       "mips_dump_tdep: UNMAKE_MIPS16_ADDR = function?\n");
6495 #ifdef UNUSED_REGNUM
6496   fprintf_unfiltered (file,
6497                       "mips_dump_tdep: UNUSED_REGNUM = %d\n",
6498                       UNUSED_REGNUM);
6499 #endif
6500   fprintf_unfiltered (file,
6501                       "mips_dump_tdep: V0_REGNUM = %d\n",
6502                       V0_REGNUM);
6503   fprintf_unfiltered (file,
6504                       "mips_dump_tdep: VM_MIN_ADDRESS = %ld\n",
6505                       (long) VM_MIN_ADDRESS);
6506 #ifdef VX_NUM_REGS
6507   fprintf_unfiltered (file,
6508                       "mips_dump_tdep: VX_NUM_REGS = %d (used?)\n",
6509                       VX_NUM_REGS);
6510 #endif
6511   fprintf_unfiltered (file,
6512                       "mips_dump_tdep: ZERO_REGNUM = %d\n",
6513                       ZERO_REGNUM);
6514   fprintf_unfiltered (file,
6515                       "mips_dump_tdep: _PROC_MAGIC_ = %d\n",
6516                       _PROC_MAGIC_);
6517 }
6518
6519 void
6520 _initialize_mips_tdep (void)
6521 {
6522   static struct cmd_list_element *mipsfpulist = NULL;
6523   struct cmd_list_element *c;
6524
6525   mips_abi_string = mips_abi_strings [MIPS_ABI_UNKNOWN];
6526   if (MIPS_ABI_LAST + 1
6527       != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
6528     internal_error (__FILE__, __LINE__, "mips_abi_strings out of sync");
6529
6530   gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
6531   if (!tm_print_insn)           /* Someone may have already set it */
6532     tm_print_insn = gdb_print_insn_mips;
6533
6534   /* Add root prefix command for all "set mips"/"show mips" commands */
6535   add_prefix_cmd ("mips", no_class, set_mips_command,
6536                   "Various MIPS specific commands.",
6537                   &setmipscmdlist, "set mips ", 0, &setlist);
6538
6539   add_prefix_cmd ("mips", no_class, show_mips_command,
6540                   "Various MIPS specific commands.",
6541                   &showmipscmdlist, "show mips ", 0, &showlist);
6542
6543   /* Allow the user to override the saved register size. */
6544   add_show_from_set (add_set_enum_cmd ("saved-gpreg-size",
6545                                        class_obscure,
6546                                        size_enums,
6547                                        &mips_saved_regsize_string, "\
6548 Set size of general purpose registers saved on the stack.\n\
6549 This option can be set to one of:\n\
6550   32    - Force GDB to treat saved GP registers as 32-bit\n\
6551   64    - Force GDB to treat saved GP registers as 64-bit\n\
6552   auto  - Allow GDB to use the target's default setting or autodetect the\n\
6553           saved GP register size from information contained in the executable.\n\
6554           (default: auto)",
6555                                        &setmipscmdlist),
6556                      &showmipscmdlist);
6557
6558   /* Allow the user to override the argument stack size. */
6559   add_show_from_set (add_set_enum_cmd ("stack-arg-size",
6560                                        class_obscure,
6561                                        size_enums,
6562                                        &mips_stack_argsize_string, "\
6563 Set the amount of stack space reserved for each argument.\n\
6564 This option can be set to one of:\n\
6565   32    - Force GDB to allocate 32-bit chunks per argument\n\
6566   64    - Force GDB to allocate 64-bit chunks per argument\n\
6567   auto  - Allow GDB to determine the correct setting from the current\n\
6568           target and executable (default)",
6569                                        &setmipscmdlist),
6570                      &showmipscmdlist);
6571
6572   /* Allow the user to override the ABI. */
6573   c = add_set_enum_cmd
6574     ("abi", class_obscure, mips_abi_strings, &mips_abi_string,
6575      "Set the ABI used by this program.\n"
6576      "This option can be set to one of:\n"
6577      "  auto  - the default ABI associated with the current binary\n"
6578      "  o32\n"
6579      "  o64\n"
6580      "  n32\n"
6581      "  n64\n"
6582      "  eabi32\n"
6583      "  eabi64",
6584      &setmipscmdlist);
6585   set_cmd_sfunc (c, mips_abi_update);
6586   add_cmd ("abi", class_obscure, show_mips_abi,
6587            "Show ABI in use by MIPS target", &showmipscmdlist);
6588
6589   /* Let the user turn off floating point and set the fence post for
6590      heuristic_proc_start.  */
6591
6592   add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
6593                   "Set use of MIPS floating-point coprocessor.",
6594                   &mipsfpulist, "set mipsfpu ", 0, &setlist);
6595   add_cmd ("single", class_support, set_mipsfpu_single_command,
6596            "Select single-precision MIPS floating-point coprocessor.",
6597            &mipsfpulist);
6598   add_cmd ("double", class_support, set_mipsfpu_double_command,
6599            "Select double-precision MIPS floating-point coprocessor.",
6600            &mipsfpulist);
6601   add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
6602   add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
6603   add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
6604   add_cmd ("none", class_support, set_mipsfpu_none_command,
6605            "Select no MIPS floating-point coprocessor.",
6606            &mipsfpulist);
6607   add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
6608   add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
6609   add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
6610   add_cmd ("auto", class_support, set_mipsfpu_auto_command,
6611            "Select MIPS floating-point coprocessor automatically.",
6612            &mipsfpulist);
6613   add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
6614            "Show current use of MIPS floating-point coprocessor target.",
6615            &showlist);
6616
6617   /* We really would like to have both "0" and "unlimited" work, but
6618      command.c doesn't deal with that.  So make it a var_zinteger
6619      because the user can always use "999999" or some such for unlimited.  */
6620   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
6621                    (char *) &heuristic_fence_post,
6622                    "\
6623 Set the distance searched for the start of a function.\n\
6624 If you are debugging a stripped executable, GDB needs to search through the\n\
6625 program for the start of a function.  This command sets the distance of the\n\
6626 search.  The only need to set it is when debugging a stripped executable.",
6627                    &setlist);
6628   /* We need to throw away the frame cache when we set this, since it
6629      might change our ability to get backtraces.  */
6630   set_cmd_sfunc (c, reinit_frame_cache_sfunc);
6631   add_show_from_set (c, &showlist);
6632
6633   /* Allow the user to control whether the upper bits of 64-bit
6634      addresses should be zeroed.  */
6635   add_setshow_auto_boolean_cmd ("mask-address", no_class, &mask_address_var, "\
6636 Set zeroing of upper 32 bits of 64-bit addresses.\n\
6637 Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to \n\
6638 allow GDB to determine the correct value.\n", "\
6639 Show zeroing of upper 32 bits of 64-bit addresses.",
6640                                 NULL, show_mask_address,
6641                                 &setmipscmdlist, &showmipscmdlist);
6642
6643   /* Allow the user to control the size of 32 bit registers within the
6644      raw remote packet.  */
6645   add_show_from_set (add_set_cmd ("remote-mips64-transfers-32bit-regs",
6646                                   class_obscure,
6647                                   var_boolean,
6648                                   (char *)&mips64_transfers_32bit_regs_p, "\
6649 Set compatibility with MIPS targets that transfers 32 and 64 bit quantities.\n\
6650 Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
6651 that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
6652 64 bits for others.  Use \"off\" to disable compatibility mode",
6653                                   &setlist),
6654                      &showlist);
6655
6656   /* Debug this files internals. */
6657   add_show_from_set (add_set_cmd ("mips", class_maintenance, var_zinteger,
6658                                   &mips_debug, "Set mips debugging.\n\
6659 When non-zero, mips specific debugging is enabled.", &setdebuglist),
6660                      &showdebuglist);
6661 }