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