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