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