* Makefile.am: Remove references to em68klynx.o and esparclynx.o.
[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,
79dd2d24 4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software
16e109ca 5 Foundation, Inc.
bf64bfd6 6
c906108c
SS
7 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
8 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
9
c5aa993b 10 This file is part of GDB.
c906108c 11
c5aa993b
JM
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
c906108c 16
c5aa993b
JM
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
c906108c 21
c5aa993b
JM
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. */
c906108c
SS
26
27#include "defs.h"
28#include "gdb_string.h"
5e2e9765 29#include "gdb_assert.h"
c906108c
SS
30#include "frame.h"
31#include "inferior.h"
32#include "symtab.h"
33#include "value.h"
34#include "gdbcmd.h"
35#include "language.h"
36#include "gdbcore.h"
37#include "symfile.h"
38#include "objfiles.h"
39#include "gdbtypes.h"
40#include "target.h"
28d069e6 41#include "arch-utils.h"
4e052eda 42#include "regcache.h"
70f80edf 43#include "osabi.h"
d1973055 44#include "mips-tdep.h"
fe898f56 45#include "block.h"
a4b8ebc8 46#include "reggroups.h"
c906108c 47#include "opcode/mips.h"
c2d11a7d
JM
48#include "elf/mips.h"
49#include "elf-bfd.h"
2475bac3 50#include "symcat.h"
a4b8ebc8 51#include "sim-regno.h"
a89aa300 52#include "dis-asm.h"
edfae063
AC
53#include "frame-unwind.h"
54#include "frame-base.h"
55#include "trad-frame.h"
7d9b040b 56#include "infcall.h"
fed7ba43 57#include "floatformat.h"
c906108c 58
8d5f9dcb
DJ
59static const struct objfile_data *mips_pdr_data;
60
5bbcb741 61static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
e0f7ec59 62
24e05951 63/* A useful bit in the CP0 status register (MIPS_PS_REGNUM). */
dd824b04
DJ
64/* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip. */
65#define ST0_FR (1 << 26)
66
b0069a17
AC
67/* The sizes of floating point registers. */
68
69enum
70{
71 MIPS_FPU_SINGLE_REGSIZE = 4,
72 MIPS_FPU_DOUBLE_REGSIZE = 8
73};
74
0dadbba0 75
2e4ebe70
DJ
76static const char *mips_abi_string;
77
78static const char *mips_abi_strings[] = {
79 "auto",
80 "n32",
81 "o32",
28d169de 82 "n64",
2e4ebe70
DJ
83 "o64",
84 "eabi32",
85 "eabi64",
86 NULL
87};
88
d929b26f
AC
89/* Various MIPS ISA options (related to stack analysis) can be
90 overridden dynamically. Establish an enum/array for managing
91 them. */
92
53904c9e
AC
93static const char size_auto[] = "auto";
94static const char size_32[] = "32";
95static const char size_64[] = "64";
d929b26f 96
53904c9e 97static const char *size_enums[] = {
d929b26f
AC
98 size_auto,
99 size_32,
100 size_64,
a5ea2558
AC
101 0
102};
103
7a292a7a 104/* Some MIPS boards don't support floating point while others only
ceae6e75 105 support single-precision floating-point operations. */
c906108c
SS
106
107enum mips_fpu_type
6d82d43b
AC
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
6d82d43b
AC
124{
125 /* from the elf header */
126 int elf_flags;
127
128 /* mips options */
129 enum mips_abi mips_abi;
130 enum mips_abi found_abi;
131 enum mips_fpu_type mips_fpu_type;
132 int mips_last_arg_regnum;
133 int mips_last_fp_arg_regnum;
6d82d43b
AC
134 int default_mask_address_p;
135 /* Is the target using 64-bit raw integer registers but only
136 storing a left-aligned 32-bit value in each? */
137 int mips64_transfers_32bit_regs_p;
138 /* Indexes for various registers. IRIX and embedded have
139 different values. This contains the "public" fields. Don't
140 add any that do not need to be public. */
141 const struct mips_regnum *regnum;
142 /* Register names table for the current register set. */
143 const char **mips_processor_reg_names;
144};
c2d11a7d 145
fed7ba43
JB
146static int
147n32n64_floatformat_always_valid (const struct floatformat *fmt,
148 const char *from)
149{
150 return 1;
151}
152
153/* FIXME: brobecker/2004-08-08: Long Double values are 128 bit long.
154 They are implemented as a pair of 64bit doubles where the high
155 part holds the result of the operation rounded to double, and
156 the low double holds the difference between the exact result and
157 the rounded result. So "high" + "low" contains the result with
158 added precision. Unfortunately, the floatformat structure used
159 by GDB is not powerful enough to describe this format. As a temporary
160 measure, we define a 128bit floatformat that only uses the high part.
161 We lose a bit of precision but that's probably the best we can do
162 for now with the current infrastructure. */
163
164static const struct floatformat floatformat_n32n64_long_double_big =
165{
166 floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
167 floatformat_intbit_no,
168 "floatformat_ieee_double_big",
169 n32n64_floatformat_always_valid
170};
171
56cea623
AC
172const struct mips_regnum *
173mips_regnum (struct gdbarch *gdbarch)
174{
175 return gdbarch_tdep (gdbarch)->regnum;
176}
177
178static int
179mips_fpa0_regnum (struct gdbarch *gdbarch)
180{
181 return mips_regnum (gdbarch)->fp0 + 12;
182}
183
0dadbba0 184#define MIPS_EABI (gdbarch_tdep (current_gdbarch)->mips_abi == MIPS_ABI_EABI32 \
216a600b 185 || gdbarch_tdep (current_gdbarch)->mips_abi == MIPS_ABI_EABI64)
c2d11a7d 186
c2d11a7d 187#define MIPS_LAST_FP_ARG_REGNUM (gdbarch_tdep (current_gdbarch)->mips_last_fp_arg_regnum)
c2d11a7d 188
c2d11a7d 189#define MIPS_LAST_ARG_REGNUM (gdbarch_tdep (current_gdbarch)->mips_last_arg_regnum)
c2d11a7d 190
c2d11a7d 191#define MIPS_FPU_TYPE (gdbarch_tdep (current_gdbarch)->mips_fpu_type)
c2d11a7d 192
95404a3e
AC
193/* MIPS16 function addresses are odd (bit 0 is set). Here are some
194 functions to test, set, or clear bit 0 of addresses. */
195
196static CORE_ADDR
197is_mips16_addr (CORE_ADDR addr)
198{
199 return ((addr) & 1);
200}
201
95404a3e
AC
202static CORE_ADDR
203unmake_mips16_addr (CORE_ADDR addr)
204{
205 return ((addr) & ~1);
206}
207
22540ece
AC
208/* Return the contents of register REGNUM as a signed integer. */
209
210static LONGEST
211read_signed_register (int regnum)
212{
1d93fe1a
AC
213 LONGEST val;
214 regcache_cooked_read_signed (current_regcache, regnum, &val);
215 return val;
22540ece
AC
216}
217
218static LONGEST
219read_signed_register_pid (int regnum, ptid_t ptid)
220{
221 ptid_t save_ptid;
222 LONGEST retval;
223
224 if (ptid_equal (ptid, inferior_ptid))
225 return read_signed_register (regnum);
226
227 save_ptid = inferior_ptid;
228
229 inferior_ptid = ptid;
230
231 retval = read_signed_register (regnum);
232
233 inferior_ptid = save_ptid;
234
235 return retval;
236}
237
d1973055
KB
238/* Return the MIPS ABI associated with GDBARCH. */
239enum mips_abi
240mips_abi (struct gdbarch *gdbarch)
241{
242 return gdbarch_tdep (gdbarch)->mips_abi;
243}
244
4246e332 245int
1b13c4f6 246mips_isa_regsize (struct gdbarch *gdbarch)
4246e332
AC
247{
248 return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
249 / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
250}
251
480d3dd2
AC
252/* Return the currently configured (or set) saved register size. */
253
1b13c4f6 254static const char *mips_abi_regsize_string = size_auto;
480d3dd2 255
e6bc2e8a 256unsigned int
13326b4e 257mips_abi_regsize (struct gdbarch *gdbarch)
d929b26f 258{
1b13c4f6 259 if (mips_abi_regsize_string == size_auto)
13326b4e
AC
260 switch (mips_abi (gdbarch))
261 {
262 case MIPS_ABI_EABI32:
263 case MIPS_ABI_O32:
264 return 4;
265 case MIPS_ABI_N32:
266 case MIPS_ABI_N64:
267 case MIPS_ABI_O64:
268 case MIPS_ABI_EABI64:
269 return 8;
270 case MIPS_ABI_UNKNOWN:
271 case MIPS_ABI_LAST:
272 default:
e2e0b3e5 273 internal_error (__FILE__, __LINE__, _("bad switch"));
13326b4e 274 }
1b13c4f6 275 else if (mips_abi_regsize_string == size_64)
d929b26f 276 return 8;
1b13c4f6 277 else /* if (mips_abi_regsize_string == size_32) */
d929b26f
AC
278 return 4;
279}
280
71b8ef93 281/* Functions for setting and testing a bit in a minimal symbol that
5a89d8aa 282 marks it as 16-bit function. The MSB of the minimal symbol's
f594e5e9 283 "info" field is used for this purpose.
5a89d8aa
MS
284
285 ELF_MAKE_MSYMBOL_SPECIAL tests whether an ELF symbol is "special",
286 i.e. refers to a 16-bit function, and sets a "special" bit in a
287 minimal symbol to mark it as a 16-bit function
288
f594e5e9 289 MSYMBOL_IS_SPECIAL tests the "special" bit in a minimal symbol */
5a89d8aa 290
5a89d8aa 291static void
6d82d43b
AC
292mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
293{
294 if (((elf_symbol_type *) (sym))->internal_elf_sym.st_other == STO_MIPS16)
295 {
296 MSYMBOL_INFO (msym) = (char *)
297 (((long) MSYMBOL_INFO (msym)) | 0x80000000);
298 SYMBOL_VALUE_ADDRESS (msym) |= 1;
299 }
5a89d8aa
MS
300}
301
71b8ef93
MS
302static int
303msymbol_is_special (struct minimal_symbol *msym)
304{
305 return (((long) MSYMBOL_INFO (msym) & 0x80000000) != 0);
306}
307
88658117
AC
308/* XFER a value from the big/little/left end of the register.
309 Depending on the size of the value it might occupy the entire
310 register or just part of it. Make an allowance for this, aligning
311 things accordingly. */
312
313static void
314mips_xfer_register (struct regcache *regcache, int reg_num, int length,
6d82d43b
AC
315 enum bfd_endian endian, bfd_byte * in,
316 const bfd_byte * out, int buf_offset)
88658117 317{
88658117 318 int reg_offset = 0;
a4b8ebc8 319 gdb_assert (reg_num >= NUM_REGS);
cb1d2653
AC
320 /* Need to transfer the left or right part of the register, based on
321 the targets byte order. */
88658117
AC
322 switch (endian)
323 {
324 case BFD_ENDIAN_BIG:
719ec221 325 reg_offset = register_size (current_gdbarch, reg_num) - length;
88658117
AC
326 break;
327 case BFD_ENDIAN_LITTLE:
328 reg_offset = 0;
329 break;
6d82d43b 330 case BFD_ENDIAN_UNKNOWN: /* Indicates no alignment. */
88658117
AC
331 reg_offset = 0;
332 break;
333 default:
e2e0b3e5 334 internal_error (__FILE__, __LINE__, _("bad switch"));
88658117
AC
335 }
336 if (mips_debug)
cb1d2653
AC
337 fprintf_unfiltered (gdb_stderr,
338 "xfer $%d, reg offset %d, buf offset %d, length %d, ",
339 reg_num, reg_offset, buf_offset, length);
88658117
AC
340 if (mips_debug && out != NULL)
341 {
342 int i;
cb1d2653 343 fprintf_unfiltered (gdb_stdlog, "out ");
88658117 344 for (i = 0; i < length; i++)
cb1d2653 345 fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
88658117
AC
346 }
347 if (in != NULL)
6d82d43b
AC
348 regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
349 in + buf_offset);
88658117 350 if (out != NULL)
6d82d43b
AC
351 regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
352 out + buf_offset);
88658117
AC
353 if (mips_debug && in != NULL)
354 {
355 int i;
cb1d2653 356 fprintf_unfiltered (gdb_stdlog, "in ");
88658117 357 for (i = 0; i < length; i++)
cb1d2653 358 fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
88658117
AC
359 }
360 if (mips_debug)
361 fprintf_unfiltered (gdb_stdlog, "\n");
362}
363
dd824b04
DJ
364/* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
365 compatiblity mode. A return value of 1 means that we have
366 physical 64-bit registers, but should treat them as 32-bit registers. */
367
368static int
369mips2_fp_compat (void)
370{
371 /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
372 meaningful. */
6d82d43b
AC
373 if (register_size (current_gdbarch, mips_regnum (current_gdbarch)->fp0) ==
374 4)
dd824b04
DJ
375 return 0;
376
377#if 0
378 /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
379 in all the places we deal with FP registers. PR gdb/413. */
380 /* Otherwise check the FR bit in the status register - it controls
381 the FP compatiblity mode. If it is clear we are in compatibility
382 mode. */
24e05951 383 if ((read_register (MIPS_PS_REGNUM) & ST0_FR) == 0)
dd824b04
DJ
384 return 1;
385#endif
361d1df0 386
dd824b04
DJ
387 return 0;
388}
389
d929b26f 390/* The amount of space reserved on the stack for registers. This is
1b13c4f6 391 different to MIPS_ABI_REGSIZE as it determines the alignment of
d929b26f
AC
392 data allocated after the registers have run out. */
393
53904c9e 394static const char *mips_stack_argsize_string = size_auto;
d929b26f
AC
395
396static unsigned int
13326b4e 397mips_stack_argsize (struct gdbarch *gdbarch)
d929b26f
AC
398{
399 if (mips_stack_argsize_string == size_auto)
13326b4e 400 return mips_abi_regsize (gdbarch);
d929b26f
AC
401 else if (mips_stack_argsize_string == size_64)
402 return 8;
6d82d43b 403 else /* if (mips_stack_argsize_string == size_32) */
d929b26f
AC
404 return 4;
405}
406
7a292a7a 407#define VM_MIN_ADDRESS (CORE_ADDR)0x400000
c906108c 408
a14ed312 409static CORE_ADDR heuristic_proc_start (CORE_ADDR);
c906108c 410
a14ed312 411static CORE_ADDR read_next_frame_reg (struct frame_info *, int);
c906108c 412
a14ed312 413static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
c906108c 414
67b2c998
DJ
415static struct type *mips_float_register_type (void);
416static struct type *mips_double_register_type (void);
417
acdb74a0
AC
418/* The list of available "set mips " and "show mips " commands */
419
420static struct cmd_list_element *setmipscmdlist = NULL;
421static struct cmd_list_element *showmipscmdlist = NULL;
422
5e2e9765
KB
423/* Integer registers 0 thru 31 are handled explicitly by
424 mips_register_name(). Processor specific registers 32 and above
691c0433
AC
425 are listed in the followign tables. */
426
6d82d43b
AC
427enum
428{ NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
691c0433
AC
429
430/* Generic MIPS. */
431
432static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
433 "sr", "lo", "hi", "bad", "cause", "pc",
434 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
435 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
436 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
437 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
438 "fsr", "fir", "" /*"fp" */ , "",
439 "", "", "", "", "", "", "", "",
440 "", "", "", "", "", "", "", "",
691c0433
AC
441};
442
443/* Names of IDT R3041 registers. */
444
445static const char *mips_r3041_reg_names[] = {
6d82d43b
AC
446 "sr", "lo", "hi", "bad", "cause", "pc",
447 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
448 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
449 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
450 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
451 "fsr", "fir", "", /*"fp" */ "",
452 "", "", "bus", "ccfg", "", "", "", "",
453 "", "", "port", "cmp", "", "", "epc", "prid",
691c0433
AC
454};
455
456/* Names of tx39 registers. */
457
458static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
459 "sr", "lo", "hi", "bad", "cause", "pc",
460 "", "", "", "", "", "", "", "",
461 "", "", "", "", "", "", "", "",
462 "", "", "", "", "", "", "", "",
463 "", "", "", "", "", "", "", "",
464 "", "", "", "",
465 "", "", "", "", "", "", "", "",
466 "", "", "config", "cache", "debug", "depc", "epc", ""
691c0433
AC
467};
468
469/* Names of IRIX registers. */
470static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
471 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
472 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
473 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
474 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
475 "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
691c0433
AC
476};
477
cce74817 478
5e2e9765 479/* Return the name of the register corresponding to REGNO. */
5a89d8aa 480static const char *
5e2e9765 481mips_register_name (int regno)
cce74817 482{
691c0433 483 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
5e2e9765
KB
484 /* GPR names for all ABIs other than n32/n64. */
485 static char *mips_gpr_names[] = {
6d82d43b
AC
486 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
487 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
488 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
489 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
5e2e9765
KB
490 };
491
492 /* GPR names for n32 and n64 ABIs. */
493 static char *mips_n32_n64_gpr_names[] = {
6d82d43b
AC
494 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
495 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
496 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
497 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
5e2e9765
KB
498 };
499
500 enum mips_abi abi = mips_abi (current_gdbarch);
501
a4b8ebc8
AC
502 /* Map [NUM_REGS .. 2*NUM_REGS) onto the raw registers, but then
503 don't make the raw register names visible. */
504 int rawnum = regno % NUM_REGS;
505 if (regno < NUM_REGS)
506 return "";
507
5e2e9765
KB
508 /* The MIPS integer registers are always mapped from 0 to 31. The
509 names of the registers (which reflects the conventions regarding
510 register use) vary depending on the ABI. */
a4b8ebc8 511 if (0 <= rawnum && rawnum < 32)
5e2e9765
KB
512 {
513 if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
a4b8ebc8 514 return mips_n32_n64_gpr_names[rawnum];
5e2e9765 515 else
a4b8ebc8 516 return mips_gpr_names[rawnum];
5e2e9765 517 }
a4b8ebc8 518 else if (32 <= rawnum && rawnum < NUM_REGS)
691c0433
AC
519 {
520 gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
521 return tdep->mips_processor_reg_names[rawnum - 32];
522 }
5e2e9765
KB
523 else
524 internal_error (__FILE__, __LINE__,
e2e0b3e5 525 _("mips_register_name: bad register number %d"), rawnum);
cce74817 526}
5e2e9765 527
a4b8ebc8 528/* Return the groups that a MIPS register can be categorised into. */
c5aa993b 529
a4b8ebc8
AC
530static int
531mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
532 struct reggroup *reggroup)
533{
534 int vector_p;
535 int float_p;
536 int raw_p;
537 int rawnum = regnum % NUM_REGS;
538 int pseudo = regnum / NUM_REGS;
539 if (reggroup == all_reggroup)
540 return pseudo;
541 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
542 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
543 /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
544 (gdbarch), as not all architectures are multi-arch. */
545 raw_p = rawnum < NUM_REGS;
6d82d43b 546 if (REGISTER_NAME (regnum) == NULL || REGISTER_NAME (regnum)[0] == '\0')
a4b8ebc8
AC
547 return 0;
548 if (reggroup == float_reggroup)
549 return float_p && pseudo;
550 if (reggroup == vector_reggroup)
551 return vector_p && pseudo;
552 if (reggroup == general_reggroup)
553 return (!vector_p && !float_p) && pseudo;
554 /* Save the pseudo registers. Need to make certain that any code
555 extracting register values from a saved register cache also uses
556 pseudo registers. */
557 if (reggroup == save_reggroup)
558 return raw_p && pseudo;
559 /* Restore the same pseudo register. */
560 if (reggroup == restore_reggroup)
561 return raw_p && pseudo;
6d82d43b 562 return 0;
a4b8ebc8
AC
563}
564
565/* Map the symbol table registers which live in the range [1 *
566 NUM_REGS .. 2 * NUM_REGS) back onto the corresponding raw
47ebcfbe 567 registers. Take care of alignment and size problems. */
c5aa993b 568
a4b8ebc8
AC
569static void
570mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
571 int cookednum, void *buf)
572{
47ebcfbe 573 int rawnum = cookednum % NUM_REGS;
a4b8ebc8 574 gdb_assert (cookednum >= NUM_REGS && cookednum < 2 * NUM_REGS);
47ebcfbe 575 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
de38af99 576 regcache_raw_read (regcache, rawnum, buf);
6d82d43b
AC
577 else if (register_size (gdbarch, rawnum) >
578 register_size (gdbarch, cookednum))
47ebcfbe
AC
579 {
580 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
581 || TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
582 regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
583 else
584 regcache_raw_read_part (regcache, rawnum, 4, 4, buf);
585 }
586 else
e2e0b3e5 587 internal_error (__FILE__, __LINE__, _("bad register size"));
a4b8ebc8
AC
588}
589
590static void
6d82d43b
AC
591mips_pseudo_register_write (struct gdbarch *gdbarch,
592 struct regcache *regcache, int cookednum,
593 const void *buf)
a4b8ebc8 594{
47ebcfbe 595 int rawnum = cookednum % NUM_REGS;
a4b8ebc8 596 gdb_assert (cookednum >= NUM_REGS && cookednum < 2 * NUM_REGS);
47ebcfbe 597 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
de38af99 598 regcache_raw_write (regcache, rawnum, buf);
6d82d43b
AC
599 else if (register_size (gdbarch, rawnum) >
600 register_size (gdbarch, cookednum))
47ebcfbe
AC
601 {
602 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
603 || TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
604 regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
605 else
606 regcache_raw_write_part (regcache, rawnum, 4, 4, buf);
607 }
608 else
e2e0b3e5 609 internal_error (__FILE__, __LINE__, _("bad register size"));
a4b8ebc8 610}
c5aa993b 611
c906108c 612/* Table to translate MIPS16 register field to actual register number. */
6d82d43b 613static int mips16_to_32_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
c906108c
SS
614
615/* Heuristic_proc_start may hunt through the text section for a long
616 time across a 2400 baud serial line. Allows the user to limit this
617 search. */
618
619static unsigned int heuristic_fence_post = 0;
620
46cd78fb 621/* Number of bytes of storage in the actual machine representation for
719ec221
AC
622 register N. NOTE: This defines the pseudo register type so need to
623 rebuild the architecture vector. */
43e526b9
JM
624
625static int mips64_transfers_32bit_regs_p = 0;
626
719ec221
AC
627static void
628set_mips64_transfers_32bit_regs (char *args, int from_tty,
629 struct cmd_list_element *c)
43e526b9 630{
719ec221
AC
631 struct gdbarch_info info;
632 gdbarch_info_init (&info);
633 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
634 instead of relying on globals. Doing that would let generic code
635 handle the search for this specific architecture. */
636 if (!gdbarch_update_p (info))
a4b8ebc8 637 {
719ec221 638 mips64_transfers_32bit_regs_p = 0;
8a3fe4f8 639 error (_("32-bit compatibility mode not supported"));
a4b8ebc8 640 }
a4b8ebc8
AC
641}
642
47ebcfbe 643/* Convert to/from a register and the corresponding memory value. */
43e526b9 644
ff2e87ac
AC
645static int
646mips_convert_register_p (int regnum, struct type *type)
647{
648 return (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
719ec221 649 && register_size (current_gdbarch, regnum) == 4
87783b8b
AC
650 && (regnum % NUM_REGS) >= mips_regnum (current_gdbarch)->fp0
651 && (regnum % NUM_REGS) < mips_regnum (current_gdbarch)->fp0 + 32
6d82d43b 652 && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
ff2e87ac
AC
653}
654
42c466d7 655static void
ff2e87ac
AC
656mips_register_to_value (struct frame_info *frame, int regnum,
657 struct type *type, void *to)
102182a9 658{
7f5f525d
AC
659 get_frame_register (frame, regnum + 0, (char *) to + 4);
660 get_frame_register (frame, regnum + 1, (char *) to + 0);
102182a9
MS
661}
662
42c466d7 663static void
ff2e87ac
AC
664mips_value_to_register (struct frame_info *frame, int regnum,
665 struct type *type, const void *from)
102182a9 666{
ff2e87ac
AC
667 put_frame_register (frame, regnum + 0, (const char *) from + 4);
668 put_frame_register (frame, regnum + 1, (const char *) from + 0);
102182a9
MS
669}
670
a4b8ebc8
AC
671/* Return the GDB type object for the "standard" data type of data in
672 register REG. */
78fde5f8
KB
673
674static struct type *
a4b8ebc8
AC
675mips_register_type (struct gdbarch *gdbarch, int regnum)
676{
5ef80fb0 677 gdb_assert (regnum >= 0 && regnum < 2 * NUM_REGS);
56cea623
AC
678 if ((regnum % NUM_REGS) >= mips_regnum (current_gdbarch)->fp0
679 && (regnum % NUM_REGS) < mips_regnum (current_gdbarch)->fp0 + 32)
a6425924 680 {
5ef80fb0 681 /* The floating-point registers raw, or cooked, always match
1b13c4f6 682 mips_isa_regsize(), and also map 1:1, byte for byte. */
5ef80fb0
AC
683 switch (gdbarch_byte_order (gdbarch))
684 {
685 case BFD_ENDIAN_BIG:
1b13c4f6 686 if (mips_isa_regsize (gdbarch) == 4)
5ef80fb0
AC
687 return builtin_type_ieee_single_big;
688 else
689 return builtin_type_ieee_double_big;
690 case BFD_ENDIAN_LITTLE:
1b13c4f6 691 if (mips_isa_regsize (gdbarch) == 4)
5ef80fb0
AC
692 return builtin_type_ieee_single_little;
693 else
694 return builtin_type_ieee_double_little;
695 case BFD_ENDIAN_UNKNOWN:
696 default:
e2e0b3e5 697 internal_error (__FILE__, __LINE__, _("bad switch"));
5ef80fb0 698 }
a6425924 699 }
d5ac5a39
AC
700 else if (regnum < NUM_REGS)
701 {
702 /* The raw or ISA registers. These are all sized according to
703 the ISA regsize. */
704 if (mips_isa_regsize (gdbarch) == 4)
705 return builtin_type_int32;
706 else
707 return builtin_type_int64;
708 }
78fde5f8 709 else
d5ac5a39
AC
710 {
711 /* The cooked or ABI registers. These are sized according to
712 the ABI (with a few complications). */
713 if (regnum >= (NUM_REGS
714 + mips_regnum (current_gdbarch)->fp_control_status)
607fc93c 715 && regnum <= NUM_REGS + MIPS_LAST_EMBED_REGNUM)
d5ac5a39
AC
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;
719 else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
720 /* The target, while possibly using a 64-bit register buffer,
721 is only transfering 32-bits of each integer register.
722 Reflect this in the cooked/pseudo (ABI) register value. */
723 return builtin_type_int32;
724 else if (mips_abi_regsize (gdbarch) == 4)
725 /* The ABI is restricted to 32-bit registers (the ISA could be
726 32- or 64-bit). */
727 return builtin_type_int32;
728 else
729 /* 64-bit ABI. */
730 return builtin_type_int64;
731 }
78fde5f8
KB
732}
733
bcb0cc15
MS
734/* TARGET_READ_SP -- Remove useless bits from the stack pointer. */
735
736static CORE_ADDR
737mips_read_sp (void)
738{
f10683bb 739 return read_signed_register (MIPS_SP_REGNUM);
bcb0cc15
MS
740}
741
c906108c 742/* Should the upper word of 64-bit addresses be zeroed? */
7f19b9a2 743enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
4014092b
AC
744
745static int
480d3dd2 746mips_mask_address_p (struct gdbarch_tdep *tdep)
4014092b
AC
747{
748 switch (mask_address_var)
749 {
7f19b9a2 750 case AUTO_BOOLEAN_TRUE:
4014092b 751 return 1;
7f19b9a2 752 case AUTO_BOOLEAN_FALSE:
4014092b
AC
753 return 0;
754 break;
7f19b9a2 755 case AUTO_BOOLEAN_AUTO:
480d3dd2 756 return tdep->default_mask_address_p;
4014092b 757 default:
e2e0b3e5 758 internal_error (__FILE__, __LINE__, _("mips_mask_address_p: bad switch"));
4014092b 759 return -1;
361d1df0 760 }
4014092b
AC
761}
762
763static void
08546159
AC
764show_mask_address (struct ui_file *file, int from_tty,
765 struct cmd_list_element *c, const char *value)
4014092b 766{
480d3dd2 767 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
08546159
AC
768
769 deprecated_show_value_hack (file, from_tty, c, value);
4014092b
AC
770 switch (mask_address_var)
771 {
7f19b9a2 772 case AUTO_BOOLEAN_TRUE:
4014092b
AC
773 printf_filtered ("The 32 bit mips address mask is enabled\n");
774 break;
7f19b9a2 775 case AUTO_BOOLEAN_FALSE:
4014092b
AC
776 printf_filtered ("The 32 bit mips address mask is disabled\n");
777 break;
7f19b9a2 778 case AUTO_BOOLEAN_AUTO:
6d82d43b
AC
779 printf_filtered
780 ("The 32 bit address mask is set automatically. Currently %s\n",
781 mips_mask_address_p (tdep) ? "enabled" : "disabled");
4014092b
AC
782 break;
783 default:
e2e0b3e5 784 internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
4014092b 785 break;
361d1df0 786 }
4014092b 787}
c906108c 788
c906108c
SS
789/* Tell if the program counter value in MEMADDR is in a MIPS16 function. */
790
0fe7e7c8
AC
791int
792mips_pc_is_mips16 (CORE_ADDR memaddr)
c906108c
SS
793{
794 struct minimal_symbol *sym;
795
796 /* If bit 0 of the address is set, assume this is a MIPS16 address. */
95404a3e 797 if (is_mips16_addr (memaddr))
c906108c
SS
798 return 1;
799
800 /* A flag indicating that this is a MIPS16 function is stored by elfread.c in
801 the high bit of the info field. Use this to decide if the function is
802 MIPS16 or normal MIPS. */
803 sym = lookup_minimal_symbol_by_pc (memaddr);
804 if (sym)
71b8ef93 805 return msymbol_is_special (sym);
c906108c
SS
806 else
807 return 0;
808}
809
b2fa5097 810/* MIPS believes that the PC has a sign extended value. Perhaps the
6c997a34
AC
811 all registers should be sign extended for simplicity? */
812
813static CORE_ADDR
39f77062 814mips_read_pc (ptid_t ptid)
6c997a34 815{
b6cb9035
AC
816 return read_signed_register_pid (mips_regnum (current_gdbarch)->pc, ptid);
817}
818
58dfe9ff
AC
819static CORE_ADDR
820mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
821{
edfae063
AC
822 return frame_unwind_register_signed (next_frame,
823 NUM_REGS + mips_regnum (gdbarch)->pc);
824}
825
826/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
827 dummy frame. The frame ID's base needs to match the TOS value
828 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
829 breakpoint. */
830
831static struct frame_id
832mips_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
833{
f10683bb 834 return frame_id_build (frame_unwind_register_signed (next_frame, NUM_REGS + MIPS_SP_REGNUM),
edfae063 835 frame_pc_unwind (next_frame));
58dfe9ff
AC
836}
837
b6cb9035
AC
838static void
839mips_write_pc (CORE_ADDR pc, ptid_t ptid)
840{
841 write_register_pid (mips_regnum (current_gdbarch)->pc, pc, ptid);
6c997a34 842}
c906108c 843
c906108c
SS
844/* Fetch and return instruction from the specified location. If the PC
845 is odd, assume it's a MIPS16 instruction; otherwise MIPS32. */
846
d37cca3d 847static ULONGEST
acdb74a0 848mips_fetch_instruction (CORE_ADDR addr)
c906108c 849{
95ac2dcf 850 char buf[MIPS_INSN32_SIZE];
c906108c
SS
851 int instlen;
852 int status;
853
0fe7e7c8 854 if (mips_pc_is_mips16 (addr))
c906108c 855 {
95ac2dcf 856 instlen = MIPS_INSN16_SIZE;
95404a3e 857 addr = unmake_mips16_addr (addr);
c906108c
SS
858 }
859 else
95ac2dcf 860 instlen = MIPS_INSN32_SIZE;
1f602b35 861 status = deprecated_read_memory_nobpt (addr, buf, instlen);
c906108c
SS
862 if (status)
863 memory_error (status, addr);
864 return extract_unsigned_integer (buf, instlen);
865}
866
c906108c 867/* These the fields of 32 bit mips instructions */
e135b889
DJ
868#define mips32_op(x) (x >> 26)
869#define itype_op(x) (x >> 26)
870#define itype_rs(x) ((x >> 21) & 0x1f)
c906108c 871#define itype_rt(x) ((x >> 16) & 0x1f)
e135b889 872#define itype_immediate(x) (x & 0xffff)
c906108c 873
e135b889
DJ
874#define jtype_op(x) (x >> 26)
875#define jtype_target(x) (x & 0x03ffffff)
c906108c 876
e135b889
DJ
877#define rtype_op(x) (x >> 26)
878#define rtype_rs(x) ((x >> 21) & 0x1f)
879#define rtype_rt(x) ((x >> 16) & 0x1f)
880#define rtype_rd(x) ((x >> 11) & 0x1f)
881#define rtype_shamt(x) ((x >> 6) & 0x1f)
882#define rtype_funct(x) (x & 0x3f)
c906108c 883
06987e64
MK
884static LONGEST
885mips32_relative_offset (ULONGEST inst)
c5aa993b 886{
06987e64 887 return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
c906108c
SS
888}
889
890/* Determine whate to set a single step breakpoint while considering
891 branch prediction */
5a89d8aa 892static CORE_ADDR
c5aa993b
JM
893mips32_next_pc (CORE_ADDR pc)
894{
895 unsigned long inst;
896 int op;
897 inst = mips_fetch_instruction (pc);
e135b889 898 if ((inst & 0xe0000000) != 0) /* Not a special, jump or branch instruction */
c5aa993b 899 {
e135b889 900 if (itype_op (inst) >> 2 == 5)
6d82d43b 901 /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
c5aa993b 902 {
e135b889 903 op = (itype_op (inst) & 0x03);
c906108c
SS
904 switch (op)
905 {
e135b889
DJ
906 case 0: /* BEQL */
907 goto equal_branch;
908 case 1: /* BNEL */
909 goto neq_branch;
910 case 2: /* BLEZL */
911 goto less_branch;
912 case 3: /* BGTZ */
913 goto greater_branch;
c5aa993b
JM
914 default:
915 pc += 4;
c906108c
SS
916 }
917 }
e135b889 918 else if (itype_op (inst) == 17 && itype_rs (inst) == 8)
6d82d43b 919 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
e135b889
DJ
920 {
921 int tf = itype_rt (inst) & 0x01;
922 int cnum = itype_rt (inst) >> 2;
6d82d43b
AC
923 int fcrcs =
924 read_signed_register (mips_regnum (current_gdbarch)->
925 fp_control_status);
e135b889
DJ
926 int cond = ((fcrcs >> 24) & 0x0e) | ((fcrcs >> 23) & 0x01);
927
928 if (((cond >> cnum) & 0x01) == tf)
929 pc += mips32_relative_offset (inst) + 4;
930 else
931 pc += 8;
932 }
c5aa993b
JM
933 else
934 pc += 4; /* Not a branch, next instruction is easy */
c906108c
SS
935 }
936 else
c5aa993b
JM
937 { /* This gets way messy */
938
c906108c 939 /* Further subdivide into SPECIAL, REGIMM and other */
e135b889 940 switch (op = itype_op (inst) & 0x07) /* extract bits 28,27,26 */
c906108c 941 {
c5aa993b
JM
942 case 0: /* SPECIAL */
943 op = rtype_funct (inst);
944 switch (op)
945 {
946 case 8: /* JR */
947 case 9: /* JALR */
6c997a34
AC
948 /* Set PC to that address */
949 pc = read_signed_register (rtype_rs (inst));
c5aa993b
JM
950 break;
951 default:
952 pc += 4;
953 }
954
6d82d43b 955 break; /* end SPECIAL */
c5aa993b 956 case 1: /* REGIMM */
c906108c 957 {
e135b889
DJ
958 op = itype_rt (inst); /* branch condition */
959 switch (op)
c906108c 960 {
c5aa993b 961 case 0: /* BLTZ */
e135b889
DJ
962 case 2: /* BLTZL */
963 case 16: /* BLTZAL */
c5aa993b 964 case 18: /* BLTZALL */
c906108c 965 less_branch:
6c997a34 966 if (read_signed_register (itype_rs (inst)) < 0)
c5aa993b
JM
967 pc += mips32_relative_offset (inst) + 4;
968 else
969 pc += 8; /* after the delay slot */
970 break;
e135b889 971 case 1: /* BGEZ */
c5aa993b
JM
972 case 3: /* BGEZL */
973 case 17: /* BGEZAL */
974 case 19: /* BGEZALL */
6c997a34 975 if (read_signed_register (itype_rs (inst)) >= 0)
c5aa993b
JM
976 pc += mips32_relative_offset (inst) + 4;
977 else
978 pc += 8; /* after the delay slot */
979 break;
e135b889 980 /* All of the other instructions in the REGIMM category */
c5aa993b
JM
981 default:
982 pc += 4;
c906108c
SS
983 }
984 }
6d82d43b 985 break; /* end REGIMM */
c5aa993b
JM
986 case 2: /* J */
987 case 3: /* JAL */
988 {
989 unsigned long reg;
990 reg = jtype_target (inst) << 2;
e135b889 991 /* Upper four bits get never changed... */
c5aa993b 992 pc = reg + ((pc + 4) & 0xf0000000);
c906108c 993 }
c5aa993b
JM
994 break;
995 /* FIXME case JALX : */
996 {
997 unsigned long reg;
998 reg = jtype_target (inst) << 2;
999 pc = reg + ((pc + 4) & 0xf0000000) + 1; /* yes, +1 */
c906108c
SS
1000 /* Add 1 to indicate 16 bit mode - Invert ISA mode */
1001 }
c5aa993b 1002 break; /* The new PC will be alternate mode */
e135b889 1003 case 4: /* BEQ, BEQL */
c5aa993b 1004 equal_branch:
6c997a34
AC
1005 if (read_signed_register (itype_rs (inst)) ==
1006 read_signed_register (itype_rt (inst)))
c5aa993b
JM
1007 pc += mips32_relative_offset (inst) + 4;
1008 else
1009 pc += 8;
1010 break;
e135b889 1011 case 5: /* BNE, BNEL */
c5aa993b 1012 neq_branch:
6c997a34 1013 if (read_signed_register (itype_rs (inst)) !=
e135b889 1014 read_signed_register (itype_rt (inst)))
c5aa993b
JM
1015 pc += mips32_relative_offset (inst) + 4;
1016 else
1017 pc += 8;
1018 break;
e135b889 1019 case 6: /* BLEZ, BLEZL */
1fd8cd20 1020 if (read_signed_register (itype_rs (inst)) <= 0)
c5aa993b
JM
1021 pc += mips32_relative_offset (inst) + 4;
1022 else
1023 pc += 8;
1024 break;
1025 case 7:
e135b889
DJ
1026 default:
1027 greater_branch: /* BGTZ, BGTZL */
1fd8cd20 1028 if (read_signed_register (itype_rs (inst)) > 0)
c5aa993b
JM
1029 pc += mips32_relative_offset (inst) + 4;
1030 else
1031 pc += 8;
1032 break;
c5aa993b
JM
1033 } /* switch */
1034 } /* else */
1035 return pc;
1036} /* mips32_next_pc */
c906108c
SS
1037
1038/* Decoding the next place to set a breakpoint is irregular for the
e26cc349 1039 mips 16 variant, but fortunately, there fewer instructions. We have to cope
c906108c
SS
1040 ith extensions for 16 bit instructions and a pair of actual 32 bit instructions.
1041 We dont want to set a single step instruction on the extend instruction
1042 either.
c5aa993b 1043 */
c906108c
SS
1044
1045/* Lots of mips16 instruction formats */
1046/* Predicting jumps requires itype,ritype,i8type
1047 and their extensions extItype,extritype,extI8type
c5aa993b 1048 */
c906108c
SS
1049enum mips16_inst_fmts
1050{
c5aa993b
JM
1051 itype, /* 0 immediate 5,10 */
1052 ritype, /* 1 5,3,8 */
1053 rrtype, /* 2 5,3,3,5 */
1054 rritype, /* 3 5,3,3,5 */
1055 rrrtype, /* 4 5,3,3,3,2 */
1056 rriatype, /* 5 5,3,3,1,4 */
1057 shifttype, /* 6 5,3,3,3,2 */
1058 i8type, /* 7 5,3,8 */
1059 i8movtype, /* 8 5,3,3,5 */
1060 i8mov32rtype, /* 9 5,3,5,3 */
1061 i64type, /* 10 5,3,8 */
1062 ri64type, /* 11 5,3,3,5 */
1063 jalxtype, /* 12 5,1,5,5,16 - a 32 bit instruction */
1064 exiItype, /* 13 5,6,5,5,1,1,1,1,1,1,5 */
1065 extRitype, /* 14 5,6,5,5,3,1,1,1,5 */
1066 extRRItype, /* 15 5,5,5,5,3,3,5 */
1067 extRRIAtype, /* 16 5,7,4,5,3,3,1,4 */
1068 EXTshifttype, /* 17 5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
1069 extI8type, /* 18 5,6,5,5,3,1,1,1,5 */
1070 extI64type, /* 19 5,6,5,5,3,1,1,1,5 */
1071 extRi64type, /* 20 5,6,5,5,3,3,5 */
1072 extshift64type /* 21 5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
1073};
12f02c2a
AC
1074/* I am heaping all the fields of the formats into one structure and
1075 then, only the fields which are involved in instruction extension */
c906108c 1076struct upk_mips16
6d82d43b
AC
1077{
1078 CORE_ADDR offset;
1079 unsigned int regx; /* Function in i8 type */
1080 unsigned int regy;
1081};
c906108c
SS
1082
1083
12f02c2a
AC
1084/* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
1085 for the bits which make up the immediatate extension. */
c906108c 1086
12f02c2a
AC
1087static CORE_ADDR
1088extended_offset (unsigned int extension)
c906108c 1089{
12f02c2a 1090 CORE_ADDR value;
c5aa993b
JM
1091 value = (extension >> 21) & 0x3f; /* * extract 15:11 */
1092 value = value << 6;
1093 value |= (extension >> 16) & 0x1f; /* extrace 10:5 */
1094 value = value << 5;
1095 value |= extension & 0x01f; /* extract 4:0 */
1096 return value;
c906108c
SS
1097}
1098
1099/* Only call this function if you know that this is an extendable
1100 instruction, It wont malfunction, but why make excess remote memory references?
1101 If the immediate operands get sign extended or somthing, do it after
1102 the extension is performed.
c5aa993b 1103 */
c906108c
SS
1104/* FIXME: Every one of these cases needs to worry about sign extension
1105 when the offset is to be used in relative addressing */
1106
1107
12f02c2a 1108static unsigned int
c5aa993b 1109fetch_mips_16 (CORE_ADDR pc)
c906108c 1110{
c5aa993b
JM
1111 char buf[8];
1112 pc &= 0xfffffffe; /* clear the low order bit */
1113 target_read_memory (pc, buf, 2);
1114 return extract_unsigned_integer (buf, 2);
c906108c
SS
1115}
1116
1117static void
c5aa993b 1118unpack_mips16 (CORE_ADDR pc,
12f02c2a
AC
1119 unsigned int extension,
1120 unsigned int inst,
6d82d43b 1121 enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
c906108c 1122{
12f02c2a
AC
1123 CORE_ADDR offset;
1124 int regx;
1125 int regy;
1126 switch (insn_format)
c906108c 1127 {
c5aa993b 1128 case itype:
c906108c 1129 {
12f02c2a
AC
1130 CORE_ADDR value;
1131 if (extension)
c5aa993b
JM
1132 {
1133 value = extended_offset (extension);
1134 value = value << 11; /* rom for the original value */
6d82d43b 1135 value |= inst & 0x7ff; /* eleven bits from instruction */
c906108c
SS
1136 }
1137 else
c5aa993b 1138 {
12f02c2a 1139 value = inst & 0x7ff;
c5aa993b 1140 /* FIXME : Consider sign extension */
c906108c 1141 }
12f02c2a
AC
1142 offset = value;
1143 regx = -1;
1144 regy = -1;
c906108c 1145 }
c5aa993b
JM
1146 break;
1147 case ritype:
1148 case i8type:
1149 { /* A register identifier and an offset */
c906108c
SS
1150 /* Most of the fields are the same as I type but the
1151 immediate value is of a different length */
12f02c2a
AC
1152 CORE_ADDR value;
1153 if (extension)
c906108c 1154 {
c5aa993b
JM
1155 value = extended_offset (extension);
1156 value = value << 8; /* from the original instruction */
12f02c2a
AC
1157 value |= inst & 0xff; /* eleven bits from instruction */
1158 regx = (extension >> 8) & 0x07; /* or i8 funct */
c5aa993b
JM
1159 if (value & 0x4000) /* test the sign bit , bit 26 */
1160 {
1161 value &= ~0x3fff; /* remove the sign bit */
1162 value = -value;
c906108c
SS
1163 }
1164 }
c5aa993b
JM
1165 else
1166 {
12f02c2a
AC
1167 value = inst & 0xff; /* 8 bits */
1168 regx = (inst >> 8) & 0x07; /* or i8 funct */
c5aa993b
JM
1169 /* FIXME: Do sign extension , this format needs it */
1170 if (value & 0x80) /* THIS CONFUSES ME */
1171 {
1172 value &= 0xef; /* remove the sign bit */
1173 value = -value;
1174 }
c5aa993b 1175 }
12f02c2a
AC
1176 offset = value;
1177 regy = -1;
c5aa993b 1178 break;
c906108c 1179 }
c5aa993b 1180 case jalxtype:
c906108c 1181 {
c5aa993b 1182 unsigned long value;
12f02c2a
AC
1183 unsigned int nexthalf;
1184 value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
c5aa993b
JM
1185 value = value << 16;
1186 nexthalf = mips_fetch_instruction (pc + 2); /* low bit still set */
1187 value |= nexthalf;
12f02c2a
AC
1188 offset = value;
1189 regx = -1;
1190 regy = -1;
c5aa993b 1191 break;
c906108c
SS
1192 }
1193 default:
e2e0b3e5 1194 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c 1195 }
12f02c2a
AC
1196 upk->offset = offset;
1197 upk->regx = regx;
1198 upk->regy = regy;
c906108c
SS
1199}
1200
1201
c5aa993b
JM
1202static CORE_ADDR
1203add_offset_16 (CORE_ADDR pc, int offset)
c906108c 1204{
c5aa993b 1205 return ((offset << 2) | ((pc + 2) & (0xf0000000)));
c906108c
SS
1206}
1207
12f02c2a
AC
1208static CORE_ADDR
1209extended_mips16_next_pc (CORE_ADDR pc,
6d82d43b 1210 unsigned int extension, unsigned int insn)
c906108c 1211{
12f02c2a
AC
1212 int op = (insn >> 11);
1213 switch (op)
c906108c 1214 {
6d82d43b 1215 case 2: /* Branch */
12f02c2a
AC
1216 {
1217 CORE_ADDR offset;
1218 struct upk_mips16 upk;
1219 unpack_mips16 (pc, extension, insn, itype, &upk);
1220 offset = upk.offset;
1221 if (offset & 0x800)
1222 {
1223 offset &= 0xeff;
1224 offset = -offset;
1225 }
1226 pc += (offset << 1) + 2;
1227 break;
1228 }
6d82d43b 1229 case 3: /* JAL , JALX - Watch out, these are 32 bit instruction */
12f02c2a
AC
1230 {
1231 struct upk_mips16 upk;
1232 unpack_mips16 (pc, extension, insn, jalxtype, &upk);
1233 pc = add_offset_16 (pc, upk.offset);
1234 if ((insn >> 10) & 0x01) /* Exchange mode */
1235 pc = pc & ~0x01; /* Clear low bit, indicate 32 bit mode */
1236 else
1237 pc |= 0x01;
1238 break;
1239 }
6d82d43b 1240 case 4: /* beqz */
12f02c2a
AC
1241 {
1242 struct upk_mips16 upk;
1243 int reg;
1244 unpack_mips16 (pc, extension, insn, ritype, &upk);
1245 reg = read_signed_register (upk.regx);
1246 if (reg == 0)
1247 pc += (upk.offset << 1) + 2;
1248 else
1249 pc += 2;
1250 break;
1251 }
6d82d43b 1252 case 5: /* bnez */
12f02c2a
AC
1253 {
1254 struct upk_mips16 upk;
1255 int reg;
1256 unpack_mips16 (pc, extension, insn, ritype, &upk);
1257 reg = read_signed_register (upk.regx);
1258 if (reg != 0)
1259 pc += (upk.offset << 1) + 2;
1260 else
1261 pc += 2;
1262 break;
1263 }
6d82d43b 1264 case 12: /* I8 Formats btez btnez */
12f02c2a
AC
1265 {
1266 struct upk_mips16 upk;
1267 int reg;
1268 unpack_mips16 (pc, extension, insn, i8type, &upk);
1269 /* upk.regx contains the opcode */
1270 reg = read_signed_register (24); /* Test register is 24 */
1271 if (((upk.regx == 0) && (reg == 0)) /* BTEZ */
1272 || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
1273 /* pc = add_offset_16(pc,upk.offset) ; */
1274 pc += (upk.offset << 1) + 2;
1275 else
1276 pc += 2;
1277 break;
1278 }
6d82d43b 1279 case 29: /* RR Formats JR, JALR, JALR-RA */
12f02c2a
AC
1280 {
1281 struct upk_mips16 upk;
1282 /* upk.fmt = rrtype; */
1283 op = insn & 0x1f;
1284 if (op == 0)
c5aa993b 1285 {
12f02c2a
AC
1286 int reg;
1287 upk.regx = (insn >> 8) & 0x07;
1288 upk.regy = (insn >> 5) & 0x07;
1289 switch (upk.regy)
c5aa993b 1290 {
12f02c2a
AC
1291 case 0:
1292 reg = upk.regx;
1293 break;
1294 case 1:
1295 reg = 31;
6d82d43b 1296 break; /* Function return instruction */
12f02c2a
AC
1297 case 2:
1298 reg = upk.regx;
1299 break;
1300 default:
1301 reg = 31;
6d82d43b 1302 break; /* BOGUS Guess */
c906108c 1303 }
12f02c2a 1304 pc = read_signed_register (reg);
c906108c 1305 }
12f02c2a 1306 else
c5aa993b 1307 pc += 2;
12f02c2a
AC
1308 break;
1309 }
1310 case 30:
1311 /* This is an instruction extension. Fetch the real instruction
1312 (which follows the extension) and decode things based on
1313 that. */
1314 {
1315 pc += 2;
1316 pc = extended_mips16_next_pc (pc, insn, fetch_mips_16 (pc));
1317 break;
1318 }
1319 default:
1320 {
1321 pc += 2;
1322 break;
1323 }
c906108c 1324 }
c5aa993b 1325 return pc;
12f02c2a 1326}
c906108c 1327
5a89d8aa 1328static CORE_ADDR
12f02c2a
AC
1329mips16_next_pc (CORE_ADDR pc)
1330{
1331 unsigned int insn = fetch_mips_16 (pc);
1332 return extended_mips16_next_pc (pc, 0, insn);
1333}
1334
1335/* The mips_next_pc function supports single_step when the remote
7e73cedf 1336 target monitor or stub is not developed enough to do a single_step.
12f02c2a
AC
1337 It works by decoding the current instruction and predicting where a
1338 branch will go. This isnt hard because all the data is available.
1339 The MIPS32 and MIPS16 variants are quite different */
c5aa993b
JM
1340CORE_ADDR
1341mips_next_pc (CORE_ADDR pc)
c906108c 1342{
c5aa993b
JM
1343 if (pc & 0x01)
1344 return mips16_next_pc (pc);
1345 else
1346 return mips32_next_pc (pc);
12f02c2a 1347}
c906108c 1348
edfae063
AC
1349struct mips_frame_cache
1350{
1351 CORE_ADDR base;
1352 struct trad_frame_saved_reg *saved_regs;
1353};
1354
29639122
JB
1355/* Set a register's saved stack address in temp_saved_regs. If an
1356 address has already been set for this register, do nothing; this
1357 way we will only recognize the first save of a given register in a
1358 function prologue.
eec63939 1359
29639122
JB
1360 For simplicity, save the address in both [0 .. NUM_REGS) and
1361 [NUM_REGS .. 2*NUM_REGS). Strictly speaking, only the second range
1362 is used as it is only second range (the ABI instead of ISA
1363 registers) that comes into play when finding saved registers in a
1364 frame. */
eec63939
AC
1365
1366static void
29639122
JB
1367set_reg_offset (struct mips_frame_cache *this_cache, int regnum,
1368 CORE_ADDR offset)
eec63939 1369{
29639122
JB
1370 if (this_cache != NULL
1371 && this_cache->saved_regs[regnum].addr == -1)
1372 {
1373 this_cache->saved_regs[regnum + 0 * NUM_REGS].addr = offset;
1374 this_cache->saved_regs[regnum + 1 * NUM_REGS].addr = offset;
1375 }
eec63939
AC
1376}
1377
eec63939 1378
29639122
JB
1379/* Fetch the immediate value from a MIPS16 instruction.
1380 If the previous instruction was an EXTEND, use it to extend
1381 the upper bits of the immediate value. This is a helper function
1382 for mips16_scan_prologue. */
eec63939 1383
29639122
JB
1384static int
1385mips16_get_imm (unsigned short prev_inst, /* previous instruction */
1386 unsigned short inst, /* current instruction */
1387 int nbits, /* number of bits in imm field */
1388 int scale, /* scale factor to be applied to imm */
1389 int is_signed) /* is the imm field signed? */
eec63939 1390{
29639122 1391 int offset;
eec63939 1392
29639122
JB
1393 if ((prev_inst & 0xf800) == 0xf000) /* prev instruction was EXTEND? */
1394 {
1395 offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
1396 if (offset & 0x8000) /* check for negative extend */
1397 offset = 0 - (0x10000 - (offset & 0xffff));
1398 return offset | (inst & 0x1f);
1399 }
eec63939 1400 else
29639122
JB
1401 {
1402 int max_imm = 1 << nbits;
1403 int mask = max_imm - 1;
1404 int sign_bit = max_imm >> 1;
45c9dd44 1405
29639122
JB
1406 offset = inst & mask;
1407 if (is_signed && (offset & sign_bit))
1408 offset = 0 - (max_imm - offset);
1409 return offset * scale;
1410 }
1411}
eec63939 1412
65596487 1413
29639122
JB
1414/* Analyze the function prologue from START_PC to LIMIT_PC. Builds
1415 the associated FRAME_CACHE if not null.
1416 Return the address of the first instruction past the prologue. */
eec63939 1417
29639122
JB
1418static CORE_ADDR
1419mips16_scan_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
1420 struct frame_info *next_frame,
1421 struct mips_frame_cache *this_cache)
1422{
1423 CORE_ADDR cur_pc;
1424 CORE_ADDR frame_addr = 0; /* Value of $r17, used as frame pointer */
1425 CORE_ADDR sp;
1426 long frame_offset = 0; /* Size of stack frame. */
1427 long frame_adjust = 0; /* Offset of FP from SP. */
1428 int frame_reg = MIPS_SP_REGNUM;
1429 unsigned short prev_inst = 0; /* saved copy of previous instruction */
1430 unsigned inst = 0; /* current instruction */
1431 unsigned entry_inst = 0; /* the entry instruction */
1432 int reg, offset;
a343eb3c 1433
29639122
JB
1434 int extend_bytes = 0;
1435 int prev_extend_bytes;
1436 CORE_ADDR end_prologue_addr = 0;
a343eb3c 1437
29639122
JB
1438 /* Can be called when there's no process, and hence when there's no
1439 NEXT_FRAME. */
1440 if (next_frame != NULL)
1441 sp = read_next_frame_reg (next_frame, NUM_REGS + MIPS_SP_REGNUM);
1442 else
1443 sp = 0;
eec63939 1444
29639122
JB
1445 if (limit_pc > start_pc + 200)
1446 limit_pc = start_pc + 200;
eec63939 1447
95ac2dcf 1448 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
29639122
JB
1449 {
1450 /* Save the previous instruction. If it's an EXTEND, we'll extract
1451 the immediate offset extension from it in mips16_get_imm. */
1452 prev_inst = inst;
eec63939 1453
29639122
JB
1454 /* Fetch and decode the instruction. */
1455 inst = (unsigned short) mips_fetch_instruction (cur_pc);
eec63939 1456
29639122
JB
1457 /* Normally we ignore extend instructions. However, if it is
1458 not followed by a valid prologue instruction, then this
1459 instruction is not part of the prologue either. We must
1460 remember in this case to adjust the end_prologue_addr back
1461 over the extend. */
1462 if ((inst & 0xf800) == 0xf000) /* extend */
1463 {
95ac2dcf 1464 extend_bytes = MIPS_INSN16_SIZE;
29639122
JB
1465 continue;
1466 }
eec63939 1467
29639122
JB
1468 prev_extend_bytes = extend_bytes;
1469 extend_bytes = 0;
eec63939 1470
29639122
JB
1471 if ((inst & 0xff00) == 0x6300 /* addiu sp */
1472 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
1473 {
1474 offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
1475 if (offset < 0) /* negative stack adjustment? */
1476 frame_offset -= offset;
1477 else
1478 /* Exit loop if a positive stack adjustment is found, which
1479 usually means that the stack cleanup code in the function
1480 epilogue is reached. */
1481 break;
1482 }
1483 else if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
1484 {
1485 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1486 reg = mips16_to_32_reg[(inst & 0x700) >> 8];
1487 set_reg_offset (this_cache, reg, sp + offset);
1488 }
1489 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
1490 {
1491 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1492 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1493 set_reg_offset (this_cache, reg, sp + offset);
1494 }
1495 else if ((inst & 0xff00) == 0x6200) /* sw $ra,n($sp) */
1496 {
1497 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
4c7d22cb 1498 set_reg_offset (this_cache, MIPS_RA_REGNUM, sp + offset);
29639122
JB
1499 }
1500 else if ((inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
1501 {
1502 offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
4c7d22cb 1503 set_reg_offset (this_cache, MIPS_RA_REGNUM, sp + offset);
29639122
JB
1504 }
1505 else if (inst == 0x673d) /* move $s1, $sp */
1506 {
1507 frame_addr = sp;
1508 frame_reg = 17;
1509 }
1510 else if ((inst & 0xff00) == 0x0100) /* addiu $s1,sp,n */
1511 {
1512 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1513 frame_addr = sp + offset;
1514 frame_reg = 17;
1515 frame_adjust = offset;
1516 }
1517 else if ((inst & 0xFF00) == 0xd900) /* sw reg,offset($s1) */
1518 {
1519 offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
1520 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1521 set_reg_offset (this_cache, reg, frame_addr + offset);
1522 }
1523 else if ((inst & 0xFF00) == 0x7900) /* sd reg,offset($s1) */
1524 {
1525 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1526 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
1527 set_reg_offset (this_cache, reg, frame_addr + offset);
1528 }
1529 else if ((inst & 0xf81f) == 0xe809
1530 && (inst & 0x700) != 0x700) /* entry */
1531 entry_inst = inst; /* save for later processing */
1532 else if ((inst & 0xf800) == 0x1800) /* jal(x) */
95ac2dcf 1533 cur_pc += MIPS_INSN16_SIZE; /* 32-bit instruction */
29639122
JB
1534 else if ((inst & 0xff1c) == 0x6704) /* move reg,$a0-$a3 */
1535 {
1536 /* This instruction is part of the prologue, but we don't
1537 need to do anything special to handle it. */
1538 }
1539 else
1540 {
1541 /* This instruction is not an instruction typically found
1542 in a prologue, so we must have reached the end of the
1543 prologue. */
1544 if (end_prologue_addr == 0)
1545 end_prologue_addr = cur_pc - prev_extend_bytes;
1546 }
1547 }
eec63939 1548
29639122
JB
1549 /* The entry instruction is typically the first instruction in a function,
1550 and it stores registers at offsets relative to the value of the old SP
1551 (before the prologue). But the value of the sp parameter to this
1552 function is the new SP (after the prologue has been executed). So we
1553 can't calculate those offsets until we've seen the entire prologue,
1554 and can calculate what the old SP must have been. */
1555 if (entry_inst != 0)
1556 {
1557 int areg_count = (entry_inst >> 8) & 7;
1558 int sreg_count = (entry_inst >> 6) & 3;
eec63939 1559
29639122
JB
1560 /* The entry instruction always subtracts 32 from the SP. */
1561 frame_offset += 32;
1562
1563 /* Now we can calculate what the SP must have been at the
1564 start of the function prologue. */
1565 sp += frame_offset;
1566
1567 /* Check if a0-a3 were saved in the caller's argument save area. */
1568 for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
1569 {
1570 set_reg_offset (this_cache, reg, sp + offset);
1571 offset += mips_abi_regsize (current_gdbarch);
1572 }
1573
1574 /* Check if the ra register was pushed on the stack. */
1575 offset = -4;
1576 if (entry_inst & 0x20)
1577 {
4c7d22cb 1578 set_reg_offset (this_cache, MIPS_RA_REGNUM, sp + offset);
29639122
JB
1579 offset -= mips_abi_regsize (current_gdbarch);
1580 }
1581
1582 /* Check if the s0 and s1 registers were pushed on the stack. */
1583 for (reg = 16; reg < sreg_count + 16; reg++)
1584 {
1585 set_reg_offset (this_cache, reg, sp + offset);
1586 offset -= mips_abi_regsize (current_gdbarch);
1587 }
1588 }
1589
1590 if (this_cache != NULL)
1591 {
1592 this_cache->base =
1593 (frame_unwind_register_signed (next_frame, NUM_REGS + frame_reg)
1594 + frame_offset - frame_adjust);
1595 /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
1596 be able to get rid of the assignment below, evetually. But it's
1597 still needed for now. */
1598 this_cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->pc]
4c7d22cb 1599 = this_cache->saved_regs[NUM_REGS + MIPS_RA_REGNUM];
29639122
JB
1600 }
1601
1602 /* If we didn't reach the end of the prologue when scanning the function
1603 instructions, then set end_prologue_addr to the address of the
1604 instruction immediately after the last one we scanned. */
1605 if (end_prologue_addr == 0)
1606 end_prologue_addr = cur_pc;
1607
1608 return end_prologue_addr;
eec63939
AC
1609}
1610
29639122
JB
1611/* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
1612 Procedures that use the 32-bit instruction set are handled by the
1613 mips_insn32 unwinder. */
1614
1615static struct mips_frame_cache *
1616mips_insn16_frame_cache (struct frame_info *next_frame, void **this_cache)
eec63939 1617{
29639122 1618 struct mips_frame_cache *cache;
eec63939
AC
1619
1620 if ((*this_cache) != NULL)
1621 return (*this_cache);
29639122
JB
1622 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
1623 (*this_cache) = cache;
1624 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
eec63939 1625
29639122
JB
1626 /* Analyze the function prologue. */
1627 {
1628 const CORE_ADDR pc = frame_pc_unwind (next_frame);
1629 CORE_ADDR start_addr;
eec63939 1630
29639122
JB
1631 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1632 if (start_addr == 0)
1633 start_addr = heuristic_proc_start (pc);
1634 /* We can't analyze the prologue if we couldn't find the begining
1635 of the function. */
1636 if (start_addr == 0)
1637 return cache;
eec63939 1638
29639122
JB
1639 mips16_scan_prologue (start_addr, pc, next_frame, *this_cache);
1640 }
1641
1642 /* SP_REGNUM, contains the value and not the address. */
1643 trad_frame_set_value (cache->saved_regs, NUM_REGS + MIPS_SP_REGNUM, cache->base);
eec63939 1644
29639122 1645 return (*this_cache);
eec63939
AC
1646}
1647
1648static void
29639122
JB
1649mips_insn16_frame_this_id (struct frame_info *next_frame, void **this_cache,
1650 struct frame_id *this_id)
eec63939 1651{
29639122
JB
1652 struct mips_frame_cache *info = mips_insn16_frame_cache (next_frame,
1653 this_cache);
1654 (*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
eec63939
AC
1655}
1656
1657static void
29639122 1658mips_insn16_frame_prev_register (struct frame_info *next_frame,
eec63939
AC
1659 void **this_cache,
1660 int regnum, int *optimizedp,
1661 enum lval_type *lvalp, CORE_ADDR *addrp,
1662 int *realnump, void *valuep)
1663{
29639122
JB
1664 struct mips_frame_cache *info = mips_insn16_frame_cache (next_frame,
1665 this_cache);
1666 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
1667 optimizedp, lvalp, addrp, realnump, valuep);
eec63939
AC
1668}
1669
29639122 1670static const struct frame_unwind mips_insn16_frame_unwind =
eec63939
AC
1671{
1672 NORMAL_FRAME,
29639122
JB
1673 mips_insn16_frame_this_id,
1674 mips_insn16_frame_prev_register
eec63939
AC
1675};
1676
1677static const struct frame_unwind *
29639122 1678mips_insn16_frame_sniffer (struct frame_info *next_frame)
eec63939
AC
1679{
1680 CORE_ADDR pc = frame_pc_unwind (next_frame);
0fe7e7c8 1681 if (mips_pc_is_mips16 (pc))
29639122
JB
1682 return &mips_insn16_frame_unwind;
1683 return NULL;
eec63939
AC
1684}
1685
1686static CORE_ADDR
29639122
JB
1687mips_insn16_frame_base_address (struct frame_info *next_frame,
1688 void **this_cache)
eec63939 1689{
29639122
JB
1690 struct mips_frame_cache *info = mips_insn16_frame_cache (next_frame,
1691 this_cache);
1692 return info->base;
eec63939
AC
1693}
1694
29639122 1695static const struct frame_base mips_insn16_frame_base =
eec63939 1696{
29639122
JB
1697 &mips_insn16_frame_unwind,
1698 mips_insn16_frame_base_address,
1699 mips_insn16_frame_base_address,
1700 mips_insn16_frame_base_address
eec63939
AC
1701};
1702
1703static const struct frame_base *
29639122 1704mips_insn16_frame_base_sniffer (struct frame_info *next_frame)
eec63939 1705{
29639122
JB
1706 if (mips_insn16_frame_sniffer (next_frame) != NULL)
1707 return &mips_insn16_frame_base;
eec63939
AC
1708 else
1709 return NULL;
edfae063
AC
1710}
1711
29639122
JB
1712/* Mark all the registers as unset in the saved_regs array
1713 of THIS_CACHE. Do nothing if THIS_CACHE is null. */
1714
1715void
1716reset_saved_regs (struct mips_frame_cache *this_cache)
c906108c 1717{
29639122
JB
1718 if (this_cache == NULL || this_cache->saved_regs == NULL)
1719 return;
1720
1721 {
1722 const int num_regs = NUM_REGS;
1723 int i;
64159455 1724
29639122
JB
1725 for (i = 0; i < num_regs; i++)
1726 {
1727 this_cache->saved_regs[i].addr = -1;
1728 }
1729 }
c906108c
SS
1730}
1731
29639122
JB
1732/* Analyze the function prologue from START_PC to LIMIT_PC. Builds
1733 the associated FRAME_CACHE if not null.
1734 Return the address of the first instruction past the prologue. */
c906108c 1735
875e1767 1736static CORE_ADDR
29639122
JB
1737mips32_scan_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
1738 struct frame_info *next_frame,
1739 struct mips_frame_cache *this_cache)
c906108c 1740{
29639122
JB
1741 CORE_ADDR cur_pc;
1742 CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for frame-pointer */
1743 CORE_ADDR sp;
1744 long frame_offset;
1745 int frame_reg = MIPS_SP_REGNUM;
8fa9cfa1 1746
29639122
JB
1747 CORE_ADDR end_prologue_addr = 0;
1748 int seen_sp_adjust = 0;
1749 int load_immediate_bytes = 0;
8fa9cfa1 1750
29639122
JB
1751 /* Can be called when there's no process, and hence when there's no
1752 NEXT_FRAME. */
1753 if (next_frame != NULL)
1754 sp = read_next_frame_reg (next_frame, NUM_REGS + MIPS_SP_REGNUM);
8fa9cfa1 1755 else
29639122 1756 sp = 0;
9022177c 1757
29639122
JB
1758 if (limit_pc > start_pc + 200)
1759 limit_pc = start_pc + 200;
9022177c 1760
29639122 1761restart:
9022177c 1762
29639122 1763 frame_offset = 0;
95ac2dcf 1764 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
9022177c 1765 {
29639122
JB
1766 unsigned long inst, high_word, low_word;
1767 int reg;
9022177c 1768
29639122
JB
1769 /* Fetch the instruction. */
1770 inst = (unsigned long) mips_fetch_instruction (cur_pc);
9022177c 1771
29639122
JB
1772 /* Save some code by pre-extracting some useful fields. */
1773 high_word = (inst >> 16) & 0xffff;
1774 low_word = inst & 0xffff;
1775 reg = high_word & 0x1f;
fe29b929 1776
29639122
JB
1777 if (high_word == 0x27bd /* addiu $sp,$sp,-i */
1778 || high_word == 0x23bd /* addi $sp,$sp,-i */
1779 || high_word == 0x67bd) /* daddiu $sp,$sp,-i */
1780 {
1781 if (low_word & 0x8000) /* negative stack adjustment? */
1782 frame_offset += 0x10000 - low_word;
1783 else
1784 /* Exit loop if a positive stack adjustment is found, which
1785 usually means that the stack cleanup code in the function
1786 epilogue is reached. */
1787 break;
1788 seen_sp_adjust = 1;
1789 }
1790 else if ((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
1791 {
1792 set_reg_offset (this_cache, reg, sp + low_word);
1793 }
1794 else if ((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
1795 {
1796 /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra. */
1797 set_reg_offset (this_cache, reg, sp + low_word);
1798 }
1799 else if (high_word == 0x27be) /* addiu $30,$sp,size */
1800 {
1801 /* Old gcc frame, r30 is virtual frame pointer. */
1802 if ((long) low_word != frame_offset)
1803 frame_addr = sp + low_word;
1804 else if (frame_reg == MIPS_SP_REGNUM)
1805 {
1806 unsigned alloca_adjust;
a4b8ebc8 1807
29639122
JB
1808 frame_reg = 30;
1809 frame_addr = read_next_frame_reg (next_frame, NUM_REGS + 30);
1810 alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
1811 if (alloca_adjust > 0)
1812 {
1813 /* FP > SP + frame_size. This may be because of
1814 an alloca or somethings similar. Fix sp to
1815 "pre-alloca" value, and try again. */
1816 sp += alloca_adjust;
1817 /* Need to reset the status of all registers. Otherwise,
1818 we will hit a guard that prevents the new address
1819 for each register to be recomputed during the second
1820 pass. */
1821 reset_saved_regs (this_cache);
1822 goto restart;
1823 }
1824 }
1825 }
1826 /* move $30,$sp. With different versions of gas this will be either
1827 `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
1828 Accept any one of these. */
1829 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
1830 {
1831 /* New gcc frame, virtual frame pointer is at r30 + frame_size. */
1832 if (frame_reg == MIPS_SP_REGNUM)
1833 {
1834 unsigned alloca_adjust;
c906108c 1835
29639122
JB
1836 frame_reg = 30;
1837 frame_addr = read_next_frame_reg (next_frame, NUM_REGS + 30);
1838 alloca_adjust = (unsigned) (frame_addr - sp);
1839 if (alloca_adjust > 0)
1840 {
1841 /* FP > SP + frame_size. This may be because of
1842 an alloca or somethings similar. Fix sp to
1843 "pre-alloca" value, and try again. */
1844 sp = frame_addr;
1845 /* Need to reset the status of all registers. Otherwise,
1846 we will hit a guard that prevents the new address
1847 for each register to be recomputed during the second
1848 pass. */
1849 reset_saved_regs (this_cache);
1850 goto restart;
1851 }
1852 }
1853 }
1854 else if ((high_word & 0xFFE0) == 0xafc0) /* sw reg,offset($30) */
1855 {
1856 set_reg_offset (this_cache, reg, frame_addr + low_word);
1857 }
1858 else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
1859 || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
1860 || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
1861 || high_word == 0x3c1c /* lui $gp,n */
1862 || high_word == 0x279c /* addiu $gp,$gp,n */
1863 || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
1864 || inst == 0x033ce021 /* addu $gp,$t9,$gp */
1865 )
1866 {
1867 /* These instructions are part of the prologue, but we don't
1868 need to do anything special to handle them. */
1869 }
1870 /* The instructions below load $at or $t0 with an immediate
1871 value in preparation for a stack adjustment via
1872 subu $sp,$sp,[$at,$t0]. These instructions could also
1873 initialize a local variable, so we accept them only before
1874 a stack adjustment instruction was seen. */
1875 else if (!seen_sp_adjust
1876 && (high_word == 0x3c01 /* lui $at,n */
1877 || high_word == 0x3c08 /* lui $t0,n */
1878 || high_word == 0x3421 /* ori $at,$at,n */
1879 || high_word == 0x3508 /* ori $t0,$t0,n */
1880 || high_word == 0x3401 /* ori $at,$zero,n */
1881 || high_word == 0x3408 /* ori $t0,$zero,n */
1882 ))
1883 {
95ac2dcf 1884 load_immediate_bytes += MIPS_INSN32_SIZE; /* FIXME! */
29639122
JB
1885 }
1886 else
1887 {
1888 /* This instruction is not an instruction typically found
1889 in a prologue, so we must have reached the end of the
1890 prologue. */
1891 /* FIXME: brobecker/2004-10-10: Can't we just break out of this
1892 loop now? Why would we need to continue scanning the function
1893 instructions? */
1894 if (end_prologue_addr == 0)
1895 end_prologue_addr = cur_pc;
1896 }
a4b8ebc8 1897 }
c906108c 1898
29639122
JB
1899 if (this_cache != NULL)
1900 {
1901 this_cache->base =
1902 (frame_unwind_register_signed (next_frame, NUM_REGS + frame_reg)
1903 + frame_offset);
1904 /* FIXME: brobecker/2004-09-15: We should be able to get rid of
1905 this assignment below, eventually. But it's still needed
1906 for now. */
1907 this_cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->pc]
4c7d22cb 1908 = this_cache->saved_regs[NUM_REGS + MIPS_RA_REGNUM];
29639122 1909 }
c906108c 1910
29639122
JB
1911 /* If we didn't reach the end of the prologue when scanning the function
1912 instructions, then set end_prologue_addr to the address of the
1913 instruction immediately after the last one we scanned. */
1914 /* brobecker/2004-10-10: I don't think this would ever happen, but
1915 we may as well be careful and do our best if we have a null
1916 end_prologue_addr. */
1917 if (end_prologue_addr == 0)
1918 end_prologue_addr = cur_pc;
1919
1920 /* In a frameless function, we might have incorrectly
1921 skipped some load immediate instructions. Undo the skipping
1922 if the load immediate was not followed by a stack adjustment. */
1923 if (load_immediate_bytes && !seen_sp_adjust)
1924 end_prologue_addr -= load_immediate_bytes;
c906108c 1925
29639122 1926 return end_prologue_addr;
c906108c
SS
1927}
1928
29639122
JB
1929/* Heuristic unwinder for procedures using 32-bit instructions (covers
1930 both 32-bit and 64-bit MIPS ISAs). Procedures using 16-bit
1931 instructions (a.k.a. MIPS16) are handled by the mips_insn16
1932 unwinder. */
c906108c 1933
29639122
JB
1934static struct mips_frame_cache *
1935mips_insn32_frame_cache (struct frame_info *next_frame, void **this_cache)
c906108c 1936{
29639122 1937 struct mips_frame_cache *cache;
c906108c 1938
29639122
JB
1939 if ((*this_cache) != NULL)
1940 return (*this_cache);
c5aa993b 1941
29639122
JB
1942 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
1943 (*this_cache) = cache;
1944 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
c5aa993b 1945
29639122
JB
1946 /* Analyze the function prologue. */
1947 {
1948 const CORE_ADDR pc = frame_pc_unwind (next_frame);
1949 CORE_ADDR start_addr;
c906108c 1950
29639122
JB
1951 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1952 if (start_addr == 0)
1953 start_addr = heuristic_proc_start (pc);
1954 /* We can't analyze the prologue if we couldn't find the begining
1955 of the function. */
1956 if (start_addr == 0)
1957 return cache;
c5aa993b 1958
29639122
JB
1959 mips32_scan_prologue (start_addr, pc, next_frame, *this_cache);
1960 }
1961
1962 /* SP_REGNUM, contains the value and not the address. */
1963 trad_frame_set_value (cache->saved_regs, NUM_REGS + MIPS_SP_REGNUM, cache->base);
c5aa993b 1964
29639122 1965 return (*this_cache);
c906108c
SS
1966}
1967
29639122
JB
1968static void
1969mips_insn32_frame_this_id (struct frame_info *next_frame, void **this_cache,
1970 struct frame_id *this_id)
c906108c 1971{
29639122
JB
1972 struct mips_frame_cache *info = mips_insn32_frame_cache (next_frame,
1973 this_cache);
1974 (*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
1975}
c906108c 1976
29639122
JB
1977static void
1978mips_insn32_frame_prev_register (struct frame_info *next_frame,
1979 void **this_cache,
1980 int regnum, int *optimizedp,
1981 enum lval_type *lvalp, CORE_ADDR *addrp,
1982 int *realnump, void *valuep)
1983{
1984 struct mips_frame_cache *info = mips_insn32_frame_cache (next_frame,
1985 this_cache);
1986 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
1987 optimizedp, lvalp, addrp, realnump, valuep);
c906108c
SS
1988}
1989
29639122
JB
1990static const struct frame_unwind mips_insn32_frame_unwind =
1991{
1992 NORMAL_FRAME,
1993 mips_insn32_frame_this_id,
1994 mips_insn32_frame_prev_register
1995};
c906108c 1996
29639122
JB
1997static const struct frame_unwind *
1998mips_insn32_frame_sniffer (struct frame_info *next_frame)
1999{
2000 CORE_ADDR pc = frame_pc_unwind (next_frame);
0fe7e7c8 2001 if (! mips_pc_is_mips16 (pc))
29639122
JB
2002 return &mips_insn32_frame_unwind;
2003 return NULL;
2004}
c906108c 2005
1c645fec 2006static CORE_ADDR
29639122
JB
2007mips_insn32_frame_base_address (struct frame_info *next_frame,
2008 void **this_cache)
c906108c 2009{
29639122
JB
2010 struct mips_frame_cache *info = mips_insn32_frame_cache (next_frame,
2011 this_cache);
2012 return info->base;
2013}
c906108c 2014
29639122
JB
2015static const struct frame_base mips_insn32_frame_base =
2016{
2017 &mips_insn32_frame_unwind,
2018 mips_insn32_frame_base_address,
2019 mips_insn32_frame_base_address,
2020 mips_insn32_frame_base_address
2021};
1c645fec 2022
29639122
JB
2023static const struct frame_base *
2024mips_insn32_frame_base_sniffer (struct frame_info *next_frame)
2025{
2026 if (mips_insn32_frame_sniffer (next_frame) != NULL)
2027 return &mips_insn32_frame_base;
a65bbe44 2028 else
29639122
JB
2029 return NULL;
2030}
a65bbe44 2031
29639122
JB
2032static struct trad_frame_cache *
2033mips_stub_frame_cache (struct frame_info *next_frame, void **this_cache)
2034{
2035 CORE_ADDR pc;
2036 CORE_ADDR start_addr;
2037 CORE_ADDR stack_addr;
2038 struct trad_frame_cache *this_trad_cache;
c906108c 2039
29639122
JB
2040 if ((*this_cache) != NULL)
2041 return (*this_cache);
2042 this_trad_cache = trad_frame_cache_zalloc (next_frame);
2043 (*this_cache) = this_trad_cache;
1c645fec 2044
29639122 2045 /* The return address is in the link register. */
4c7d22cb 2046 trad_frame_set_reg_realreg (this_trad_cache, PC_REGNUM, MIPS_RA_REGNUM);
1c645fec 2047
29639122
JB
2048 /* Frame ID, since it's a frameless / stackless function, no stack
2049 space is allocated and SP on entry is the current SP. */
2050 pc = frame_pc_unwind (next_frame);
2051 find_pc_partial_function (pc, NULL, &start_addr, NULL);
4c7d22cb 2052 stack_addr = frame_unwind_register_signed (next_frame, MIPS_SP_REGNUM);
29639122 2053 trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
1c645fec 2054
29639122
JB
2055 /* Assume that the frame's base is the same as the
2056 stack-pointer. */
2057 trad_frame_set_this_base (this_trad_cache, stack_addr);
c906108c 2058
29639122
JB
2059 return this_trad_cache;
2060}
c906108c 2061
29639122
JB
2062static void
2063mips_stub_frame_this_id (struct frame_info *next_frame, void **this_cache,
2064 struct frame_id *this_id)
2065{
2066 struct trad_frame_cache *this_trad_cache
2067 = mips_stub_frame_cache (next_frame, this_cache);
2068 trad_frame_get_id (this_trad_cache, this_id);
2069}
c906108c 2070
29639122
JB
2071static void
2072mips_stub_frame_prev_register (struct frame_info *next_frame,
2073 void **this_cache,
2074 int regnum, int *optimizedp,
2075 enum lval_type *lvalp, CORE_ADDR *addrp,
2076 int *realnump, void *valuep)
2077{
2078 struct trad_frame_cache *this_trad_cache
2079 = mips_stub_frame_cache (next_frame, this_cache);
2080 trad_frame_get_register (this_trad_cache, next_frame, regnum, optimizedp,
2081 lvalp, addrp, realnump, valuep);
2082}
c906108c 2083
29639122
JB
2084static const struct frame_unwind mips_stub_frame_unwind =
2085{
2086 NORMAL_FRAME,
2087 mips_stub_frame_this_id,
2088 mips_stub_frame_prev_register
2089};
c906108c 2090
29639122
JB
2091static const struct frame_unwind *
2092mips_stub_frame_sniffer (struct frame_info *next_frame)
2093{
2094 CORE_ADDR pc = frame_pc_unwind (next_frame);
2095 if (in_plt_section (pc, NULL))
2096 return &mips_stub_frame_unwind;
2097 else
2098 return NULL;
2099}
c906108c 2100
29639122
JB
2101static CORE_ADDR
2102mips_stub_frame_base_address (struct frame_info *next_frame,
2103 void **this_cache)
2104{
2105 struct trad_frame_cache *this_trad_cache
2106 = mips_stub_frame_cache (next_frame, this_cache);
2107 return trad_frame_get_this_base (this_trad_cache);
2108}
0fce0821 2109
29639122
JB
2110static const struct frame_base mips_stub_frame_base =
2111{
2112 &mips_stub_frame_unwind,
2113 mips_stub_frame_base_address,
2114 mips_stub_frame_base_address,
2115 mips_stub_frame_base_address
2116};
2117
2118static const struct frame_base *
2119mips_stub_frame_base_sniffer (struct frame_info *next_frame)
2120{
2121 if (mips_stub_frame_sniffer (next_frame) != NULL)
2122 return &mips_stub_frame_base;
2123 else
2124 return NULL;
2125}
2126
2127static CORE_ADDR
2128read_next_frame_reg (struct frame_info *fi, int regno)
2129{
2130 /* Always a pseudo. */
2131 gdb_assert (regno >= NUM_REGS);
2132 if (fi == NULL)
0fce0821 2133 {
29639122
JB
2134 LONGEST val;
2135 regcache_cooked_read_signed (current_regcache, regno, &val);
2136 return val;
0fce0821 2137 }
29639122
JB
2138 else
2139 return frame_unwind_register_signed (fi, regno);
1c645fec 2140
c906108c
SS
2141}
2142
29639122 2143/* mips_addr_bits_remove - remove useless address bits */
65596487 2144
29639122
JB
2145static CORE_ADDR
2146mips_addr_bits_remove (CORE_ADDR addr)
65596487 2147{
29639122
JB
2148 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2149 if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
2150 /* This hack is a work-around for existing boards using PMON, the
2151 simulator, and any other 64-bit targets that doesn't have true
2152 64-bit addressing. On these targets, the upper 32 bits of
2153 addresses are ignored by the hardware. Thus, the PC or SP are
2154 likely to have been sign extended to all 1s by instruction
2155 sequences that load 32-bit addresses. For example, a typical
2156 piece of code that loads an address is this:
65596487 2157
29639122
JB
2158 lui $r2, <upper 16 bits>
2159 ori $r2, <lower 16 bits>
65596487 2160
29639122
JB
2161 But the lui sign-extends the value such that the upper 32 bits
2162 may be all 1s. The workaround is simply to mask off these
2163 bits. In the future, gcc may be changed to support true 64-bit
2164 addressing, and this masking will have to be disabled. */
2165 return addr &= 0xffffffffUL;
2166 else
2167 return addr;
65596487
JB
2168}
2169
29639122
JB
2170/* mips_software_single_step() is called just before we want to resume
2171 the inferior, if we want to single-step it but there is no hardware
2172 or kernel single-step support (MIPS on GNU/Linux for example). We find
2173 the target of the coming instruction and breakpoint it.
1e05a7bf 2174
29639122
JB
2175 single_step is also called just after the inferior stops. If we had
2176 set up a simulated single-step, we undo our damage. */
2177
2178void
2179mips_software_single_step (enum target_signal sig, int insert_breakpoints_p)
c906108c 2180{
29639122
JB
2181 static CORE_ADDR next_pc;
2182 typedef char binsn_quantum[BREAKPOINT_MAX];
2183 static binsn_quantum break_mem;
2184 CORE_ADDR pc;
65596487 2185
29639122
JB
2186 if (insert_breakpoints_p)
2187 {
2188 pc = read_register (mips_regnum (current_gdbarch)->pc);
2189 next_pc = mips_next_pc (pc);
98b4dd94 2190
29639122
JB
2191 target_insert_breakpoint (next_pc, break_mem);
2192 }
a65bbe44 2193 else
29639122
JB
2194 target_remove_breakpoint (next_pc, break_mem);
2195}
a65bbe44 2196
29639122
JB
2197/* Test whether the PC points to the return instruction at the
2198 end of a function. */
65596487 2199
29639122
JB
2200static int
2201mips_about_to_return (CORE_ADDR pc)
2202{
0fe7e7c8 2203 if (mips_pc_is_mips16 (pc))
29639122
JB
2204 /* This mips16 case isn't necessarily reliable. Sometimes the compiler
2205 generates a "jr $ra"; other times it generates code to load
2206 the return address from the stack to an accessible register (such
2207 as $a3), then a "jr" using that register. This second case
2208 is almost impossible to distinguish from an indirect jump
2209 used for switch statements, so we don't even try. */
2210 return mips_fetch_instruction (pc) == 0xe820; /* jr $ra */
2211 else
2212 return mips_fetch_instruction (pc) == 0x3e00008; /* jr $ra */
2213}
c906108c 2214
c906108c 2215
29639122
JB
2216/* This fencepost looks highly suspicious to me. Removing it also
2217 seems suspicious as it could affect remote debugging across serial
2218 lines. */
c906108c 2219
29639122
JB
2220static CORE_ADDR
2221heuristic_proc_start (CORE_ADDR pc)
2222{
2223 CORE_ADDR start_pc;
2224 CORE_ADDR fence;
2225 int instlen;
2226 int seen_adjsp = 0;
65596487 2227
29639122
JB
2228 pc = ADDR_BITS_REMOVE (pc);
2229 start_pc = pc;
2230 fence = start_pc - heuristic_fence_post;
2231 if (start_pc == 0)
2232 return 0;
65596487 2233
29639122
JB
2234 if (heuristic_fence_post == UINT_MAX || fence < VM_MIN_ADDRESS)
2235 fence = VM_MIN_ADDRESS;
65596487 2236
95ac2dcf 2237 instlen = mips_pc_is_mips16 (pc) ? MIPS_INSN16_SIZE : MIPS_INSN32_SIZE;
98b4dd94 2238
29639122
JB
2239 /* search back for previous return */
2240 for (start_pc -= instlen;; start_pc -= instlen)
2241 if (start_pc < fence)
2242 {
2243 /* It's not clear to me why we reach this point when
2244 stop_soon, but with this test, at least we
2245 don't print out warnings for every child forked (eg, on
2246 decstation). 22apr93 rich@cygnus.com. */
2247 if (stop_soon == NO_STOP_QUIETLY)
2248 {
2249 static int blurb_printed = 0;
98b4dd94 2250
8a3fe4f8 2251 warning (_("GDB can't find the start of the function at 0x%s."),
29639122
JB
2252 paddr_nz (pc));
2253
2254 if (!blurb_printed)
2255 {
2256 /* This actually happens frequently in embedded
2257 development, when you first connect to a board
2258 and your stack pointer and pc are nowhere in
2259 particular. This message needs to give people
2260 in that situation enough information to
2261 determine that it's no big deal. */
2262 printf_filtered ("\n\
2263 GDB is unable to find the start of the function at 0x%s\n\
2264and thus can't determine the size of that function's stack frame.\n\
2265This means that GDB may be unable to access that stack frame, or\n\
2266the frames below it.\n\
2267 This problem is most likely caused by an invalid program counter or\n\
2268stack pointer.\n\
2269 However, if you think GDB should simply search farther back\n\
2270from 0x%s for code which looks like the beginning of a\n\
2271function, you can increase the range of the search using the `set\n\
2272heuristic-fence-post' command.\n", paddr_nz (pc), paddr_nz (pc));
2273 blurb_printed = 1;
2274 }
2275 }
2276
2277 return 0;
2278 }
0fe7e7c8 2279 else if (mips_pc_is_mips16 (start_pc))
29639122
JB
2280 {
2281 unsigned short inst;
2282
2283 /* On MIPS16, any one of the following is likely to be the
2284 start of a function:
2285 entry
2286 addiu sp,-n
2287 daddiu sp,-n
2288 extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n' */
2289 inst = mips_fetch_instruction (start_pc);
2290 if (((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
2291 || (inst & 0xff80) == 0x6380 /* addiu sp,-n */
2292 || (inst & 0xff80) == 0xfb80 /* daddiu sp,-n */
2293 || ((inst & 0xf810) == 0xf010 && seen_adjsp)) /* extend -n */
2294 break;
2295 else if ((inst & 0xff00) == 0x6300 /* addiu sp */
2296 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
2297 seen_adjsp = 1;
2298 else
2299 seen_adjsp = 0;
2300 }
2301 else if (mips_about_to_return (start_pc))
2302 {
4c7d22cb 2303 /* Skip return and its delay slot. */
95ac2dcf 2304 start_pc += 2 * MIPS_INSN32_SIZE;
29639122
JB
2305 break;
2306 }
2307
2308 return start_pc;
c906108c
SS
2309}
2310
6c0d6680
DJ
2311struct mips_objfile_private
2312{
2313 bfd_size_type size;
2314 char *contents;
2315};
2316
f09ded24
AC
2317/* According to the current ABI, should the type be passed in a
2318 floating-point register (assuming that there is space)? When there
2319 is no FPU, FP are not even considered as possibile candidates for
2320 FP registers and, consequently this returns false - forces FP
2321 arguments into integer registers. */
2322
2323static int
2324fp_register_arg_p (enum type_code typecode, struct type *arg_type)
2325{
2326 return ((typecode == TYPE_CODE_FLT
2327 || (MIPS_EABI
6d82d43b
AC
2328 && (typecode == TYPE_CODE_STRUCT
2329 || typecode == TYPE_CODE_UNION)
f09ded24
AC
2330 && TYPE_NFIELDS (arg_type) == 1
2331 && TYPE_CODE (TYPE_FIELD_TYPE (arg_type, 0)) == TYPE_CODE_FLT))
c86b5b38 2332 && MIPS_FPU_TYPE != MIPS_FPU_NONE);
f09ded24
AC
2333}
2334
49e790b0
DJ
2335/* On o32, argument passing in GPRs depends on the alignment of the type being
2336 passed. Return 1 if this type must be aligned to a doubleword boundary. */
2337
2338static int
2339mips_type_needs_double_align (struct type *type)
2340{
2341 enum type_code typecode = TYPE_CODE (type);
361d1df0 2342
49e790b0
DJ
2343 if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
2344 return 1;
2345 else if (typecode == TYPE_CODE_STRUCT)
2346 {
2347 if (TYPE_NFIELDS (type) < 1)
2348 return 0;
2349 return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
2350 }
2351 else if (typecode == TYPE_CODE_UNION)
2352 {
361d1df0 2353 int i, n;
49e790b0
DJ
2354
2355 n = TYPE_NFIELDS (type);
2356 for (i = 0; i < n; i++)
2357 if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
2358 return 1;
2359 return 0;
2360 }
2361 return 0;
2362}
2363
dc604539
AC
2364/* Adjust the address downward (direction of stack growth) so that it
2365 is correctly aligned for a new stack frame. */
2366static CORE_ADDR
2367mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2368{
5b03f266 2369 return align_down (addr, 16);
dc604539
AC
2370}
2371
f7ab6ec6 2372static CORE_ADDR
7d9b040b 2373mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
2374 struct regcache *regcache, CORE_ADDR bp_addr,
2375 int nargs, struct value **args, CORE_ADDR sp,
2376 int struct_return, CORE_ADDR struct_addr)
c906108c
SS
2377{
2378 int argreg;
2379 int float_argreg;
2380 int argnum;
2381 int len = 0;
2382 int stack_offset = 0;
480d3dd2 2383 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7d9b040b 2384 CORE_ADDR func_addr = find_function_addr (function, NULL);
c906108c 2385
25ab4790
AC
2386 /* For shared libraries, "t9" needs to point at the function
2387 address. */
4c7d22cb 2388 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
2389
2390 /* Set the return address register to point to the entry point of
2391 the program, where a breakpoint lies in wait. */
4c7d22cb 2392 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 2393
c906108c 2394 /* First ensure that the stack and structure return address (if any)
cb3d25d1
MS
2395 are properly aligned. The stack has to be at least 64-bit
2396 aligned even on 32-bit machines, because doubles must be 64-bit
2397 aligned. For n32 and n64, stack frames need to be 128-bit
2398 aligned, so we round to this widest known alignment. */
2399
5b03f266
AC
2400 sp = align_down (sp, 16);
2401 struct_addr = align_down (struct_addr, 16);
c5aa993b 2402
46e0f506 2403 /* Now make space on the stack for the args. We allocate more
c906108c 2404 than necessary for EABI, because the first few arguments are
46e0f506 2405 passed in registers, but that's OK. */
c906108c 2406 for (argnum = 0; argnum < nargs; argnum++)
4991999e 2407 len += align_up (TYPE_LENGTH (value_type (args[argnum])),
13326b4e 2408 mips_stack_argsize (gdbarch));
5b03f266 2409 sp -= align_up (len, 16);
c906108c 2410
9ace0497 2411 if (mips_debug)
6d82d43b 2412 fprintf_unfiltered (gdb_stdlog,
5b03f266
AC
2413 "mips_eabi_push_dummy_call: sp=0x%s allocated %ld\n",
2414 paddr_nz (sp), (long) align_up (len, 16));
9ace0497 2415
c906108c 2416 /* Initialize the integer and float register pointers. */
4c7d22cb 2417 argreg = MIPS_A0_REGNUM;
56cea623 2418 float_argreg = mips_fpa0_regnum (current_gdbarch);
c906108c 2419
46e0f506 2420 /* The struct_return pointer occupies the first parameter-passing reg. */
c906108c 2421 if (struct_return)
9ace0497
AC
2422 {
2423 if (mips_debug)
2424 fprintf_unfiltered (gdb_stdlog,
25ab4790 2425 "mips_eabi_push_dummy_call: struct_return reg=%d 0x%s\n",
cb3d25d1 2426 argreg, paddr_nz (struct_addr));
9ace0497
AC
2427 write_register (argreg++, struct_addr);
2428 }
c906108c
SS
2429
2430 /* Now load as many as possible of the first arguments into
2431 registers, and push the rest onto the stack. Loop thru args
2432 from first to last. */
2433 for (argnum = 0; argnum < nargs; argnum++)
2434 {
2435 char *val;
d9d9c31f 2436 char valbuf[MAX_REGISTER_SIZE];
ea7c478f 2437 struct value *arg = args[argnum];
4991999e 2438 struct type *arg_type = check_typedef (value_type (arg));
c906108c
SS
2439 int len = TYPE_LENGTH (arg_type);
2440 enum type_code typecode = TYPE_CODE (arg_type);
2441
9ace0497
AC
2442 if (mips_debug)
2443 fprintf_unfiltered (gdb_stdlog,
25ab4790 2444 "mips_eabi_push_dummy_call: %d len=%d type=%d",
acdb74a0 2445 argnum + 1, len, (int) typecode);
9ace0497 2446
c906108c 2447 /* The EABI passes structures that do not fit in a register by
46e0f506 2448 reference. */
13326b4e 2449 if (len > mips_abi_regsize (gdbarch)
9ace0497 2450 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
c906108c 2451 {
13326b4e 2452 store_unsigned_integer (valbuf, mips_abi_regsize (gdbarch),
480d3dd2 2453 VALUE_ADDRESS (arg));
c906108c 2454 typecode = TYPE_CODE_PTR;
13326b4e 2455 len = mips_abi_regsize (gdbarch);
c906108c 2456 val = valbuf;
9ace0497
AC
2457 if (mips_debug)
2458 fprintf_unfiltered (gdb_stdlog, " push");
c906108c
SS
2459 }
2460 else
0fd88904 2461 val = (char *) value_contents (arg);
c906108c
SS
2462
2463 /* 32-bit ABIs always start floating point arguments in an
acdb74a0
AC
2464 even-numbered floating point register. Round the FP register
2465 up before the check to see if there are any FP registers
46e0f506
MS
2466 left. Non MIPS_EABI targets also pass the FP in the integer
2467 registers so also round up normal registers. */
ceae6e75
AC
2468 if (mips_abi_regsize (gdbarch) < 8
2469 && fp_register_arg_p (typecode, arg_type))
acdb74a0
AC
2470 {
2471 if ((float_argreg & 1))
2472 float_argreg++;
2473 }
c906108c
SS
2474
2475 /* Floating point arguments passed in registers have to be
2476 treated specially. On 32-bit architectures, doubles
c5aa993b
JM
2477 are passed in register pairs; the even register gets
2478 the low word, and the odd register gets the high word.
2479 On non-EABI processors, the first two floating point arguments are
2480 also copied to general registers, because MIPS16 functions
2481 don't use float registers for arguments. This duplication of
2482 arguments in general registers can't hurt non-MIPS16 functions
2483 because those registers are normally skipped. */
1012bd0e
EZ
2484 /* MIPS_EABI squeezes a struct that contains a single floating
2485 point value into an FP register instead of pushing it onto the
46e0f506 2486 stack. */
f09ded24
AC
2487 if (fp_register_arg_p (typecode, arg_type)
2488 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
c906108c 2489 {
ceae6e75 2490 if (mips_abi_regsize (gdbarch) < 8 && len == 8)
c906108c 2491 {
d7449b42 2492 int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
c906108c
SS
2493 unsigned long regval;
2494
2495 /* Write the low word of the double to the even register(s). */
c5aa993b 2496 regval = extract_unsigned_integer (val + low_offset, 4);
9ace0497 2497 if (mips_debug)
acdb74a0 2498 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 2499 float_argreg, phex (regval, 4));
c906108c 2500 write_register (float_argreg++, regval);
c906108c
SS
2501
2502 /* Write the high word of the double to the odd register(s). */
c5aa993b 2503 regval = extract_unsigned_integer (val + 4 - low_offset, 4);
9ace0497 2504 if (mips_debug)
acdb74a0 2505 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 2506 float_argreg, phex (regval, 4));
c906108c 2507 write_register (float_argreg++, regval);
c906108c
SS
2508 }
2509 else
2510 {
2511 /* This is a floating point value that fits entirely
2512 in a single register. */
53a5351d 2513 /* On 32 bit ABI's the float_argreg is further adjusted
6d82d43b 2514 above to ensure that it is even register aligned. */
9ace0497
AC
2515 LONGEST regval = extract_unsigned_integer (val, len);
2516 if (mips_debug)
acdb74a0 2517 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 2518 float_argreg, phex (regval, len));
c906108c 2519 write_register (float_argreg++, regval);
c906108c
SS
2520 }
2521 }
2522 else
2523 {
2524 /* Copy the argument to general registers or the stack in
2525 register-sized pieces. Large arguments are split between
2526 registers and stack. */
4246e332 2527 /* Note: structs whose size is not a multiple of
d5ac5a39
AC
2528 mips_abi_regsize() are treated specially: Irix cc passes
2529 them in registers where gcc sometimes puts them on the
2530 stack. For maximum compatibility, we will put them in
2531 both places. */
13326b4e
AC
2532 int odd_sized_struct = ((len > mips_abi_regsize (gdbarch))
2533 && (len % mips_abi_regsize (gdbarch) != 0));
46e0f506 2534
f09ded24 2535 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 2536 register are only written to memory. */
c906108c
SS
2537 while (len > 0)
2538 {
ebafbe83 2539 /* Remember if the argument was written to the stack. */
566f0f7a 2540 int stack_used_p = 0;
13326b4e
AC
2541 int partial_len = (len < mips_abi_regsize (gdbarch)
2542 ? len : mips_abi_regsize (gdbarch));
c906108c 2543
acdb74a0
AC
2544 if (mips_debug)
2545 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
2546 partial_len);
2547
566f0f7a 2548 /* Write this portion of the argument to the stack. */
f09ded24
AC
2549 if (argreg > MIPS_LAST_ARG_REGNUM
2550 || odd_sized_struct
2551 || fp_register_arg_p (typecode, arg_type))
c906108c 2552 {
c906108c
SS
2553 /* Should shorter than int integer values be
2554 promoted to int before being stored? */
c906108c 2555 int longword_offset = 0;
9ace0497 2556 CORE_ADDR addr;
566f0f7a 2557 stack_used_p = 1;
d7449b42 2558 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
7a292a7a 2559 {
13326b4e 2560 if (mips_stack_argsize (gdbarch) == 8
480d3dd2
AC
2561 && (typecode == TYPE_CODE_INT
2562 || typecode == TYPE_CODE_PTR
6d82d43b 2563 || typecode == TYPE_CODE_FLT) && len <= 4)
13326b4e 2564 longword_offset = mips_stack_argsize (gdbarch) - len;
480d3dd2
AC
2565 else if ((typecode == TYPE_CODE_STRUCT
2566 || typecode == TYPE_CODE_UNION)
2567 && (TYPE_LENGTH (arg_type)
13326b4e
AC
2568 < mips_stack_argsize (gdbarch)))
2569 longword_offset = mips_stack_argsize (gdbarch) - len;
7a292a7a 2570 }
c5aa993b 2571
9ace0497
AC
2572 if (mips_debug)
2573 {
cb3d25d1
MS
2574 fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
2575 paddr_nz (stack_offset));
2576 fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
2577 paddr_nz (longword_offset));
9ace0497 2578 }
361d1df0 2579
9ace0497
AC
2580 addr = sp + stack_offset + longword_offset;
2581
2582 if (mips_debug)
2583 {
2584 int i;
6d82d43b 2585 fprintf_unfiltered (gdb_stdlog, " @0x%s ",
cb3d25d1 2586 paddr_nz (addr));
9ace0497
AC
2587 for (i = 0; i < partial_len; i++)
2588 {
6d82d43b 2589 fprintf_unfiltered (gdb_stdlog, "%02x",
cb3d25d1 2590 val[i] & 0xff);
9ace0497
AC
2591 }
2592 }
2593 write_memory (addr, val, partial_len);
c906108c
SS
2594 }
2595
f09ded24
AC
2596 /* Note!!! This is NOT an else clause. Odd sized
2597 structs may go thru BOTH paths. Floating point
46e0f506 2598 arguments will not. */
566f0f7a 2599 /* Write this portion of the argument to a general
6d82d43b 2600 purpose register. */
f09ded24
AC
2601 if (argreg <= MIPS_LAST_ARG_REGNUM
2602 && !fp_register_arg_p (typecode, arg_type))
c906108c 2603 {
6d82d43b
AC
2604 LONGEST regval =
2605 extract_unsigned_integer (val, partial_len);
c906108c 2606
9ace0497 2607 if (mips_debug)
acdb74a0 2608 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
9ace0497 2609 argreg,
6d82d43b 2610 phex (regval,
13326b4e 2611 mips_abi_regsize (gdbarch)));
c906108c
SS
2612 write_register (argreg, regval);
2613 argreg++;
c906108c 2614 }
c5aa993b 2615
c906108c
SS
2616 len -= partial_len;
2617 val += partial_len;
2618
566f0f7a 2619 /* Compute the the offset into the stack at which we
6d82d43b 2620 will copy the next parameter.
566f0f7a 2621
566f0f7a 2622 In the new EABI (and the NABI32), the stack_offset
46e0f506 2623 only needs to be adjusted when it has been used. */
c906108c 2624
46e0f506 2625 if (stack_used_p)
480d3dd2 2626 stack_offset += align_up (partial_len,
13326b4e 2627 mips_stack_argsize (gdbarch));
c906108c
SS
2628 }
2629 }
9ace0497
AC
2630 if (mips_debug)
2631 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
2632 }
2633
f10683bb 2634 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 2635
0f71a2f6
JM
2636 /* Return adjusted stack pointer. */
2637 return sp;
2638}
2639
9c8fdbfa 2640/* Determin the return value convention being used. */
6d82d43b 2641
9c8fdbfa
AC
2642static enum return_value_convention
2643mips_eabi_return_value (struct gdbarch *gdbarch,
2644 struct type *type, struct regcache *regcache,
2645 void *readbuf, const void *writebuf)
6d82d43b 2646{
9c8fdbfa
AC
2647 if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
2648 return RETURN_VALUE_STRUCT_CONVENTION;
2649 if (readbuf)
2650 memset (readbuf, 0, TYPE_LENGTH (type));
2651 return RETURN_VALUE_REGISTER_CONVENTION;
6d82d43b
AC
2652}
2653
6d82d43b
AC
2654
2655/* N32/N64 ABI stuff. */
ebafbe83 2656
f7ab6ec6 2657static CORE_ADDR
7d9b040b 2658mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
2659 struct regcache *regcache, CORE_ADDR bp_addr,
2660 int nargs, struct value **args, CORE_ADDR sp,
2661 int struct_return, CORE_ADDR struct_addr)
cb3d25d1
MS
2662{
2663 int argreg;
2664 int float_argreg;
2665 int argnum;
2666 int len = 0;
2667 int stack_offset = 0;
480d3dd2 2668 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7d9b040b 2669 CORE_ADDR func_addr = find_function_addr (function, NULL);
cb3d25d1 2670
25ab4790
AC
2671 /* For shared libraries, "t9" needs to point at the function
2672 address. */
4c7d22cb 2673 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
2674
2675 /* Set the return address register to point to the entry point of
2676 the program, where a breakpoint lies in wait. */
4c7d22cb 2677 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 2678
cb3d25d1
MS
2679 /* First ensure that the stack and structure return address (if any)
2680 are properly aligned. The stack has to be at least 64-bit
2681 aligned even on 32-bit machines, because doubles must be 64-bit
2682 aligned. For n32 and n64, stack frames need to be 128-bit
2683 aligned, so we round to this widest known alignment. */
2684
5b03f266
AC
2685 sp = align_down (sp, 16);
2686 struct_addr = align_down (struct_addr, 16);
cb3d25d1
MS
2687
2688 /* Now make space on the stack for the args. */
2689 for (argnum = 0; argnum < nargs; argnum++)
4991999e 2690 len += align_up (TYPE_LENGTH (value_type (args[argnum])),
13326b4e 2691 mips_stack_argsize (gdbarch));
5b03f266 2692 sp -= align_up (len, 16);
cb3d25d1
MS
2693
2694 if (mips_debug)
6d82d43b 2695 fprintf_unfiltered (gdb_stdlog,
5b03f266
AC
2696 "mips_n32n64_push_dummy_call: sp=0x%s allocated %ld\n",
2697 paddr_nz (sp), (long) align_up (len, 16));
cb3d25d1
MS
2698
2699 /* Initialize the integer and float register pointers. */
4c7d22cb 2700 argreg = MIPS_A0_REGNUM;
56cea623 2701 float_argreg = mips_fpa0_regnum (current_gdbarch);
cb3d25d1 2702
46e0f506 2703 /* The struct_return pointer occupies the first parameter-passing reg. */
cb3d25d1
MS
2704 if (struct_return)
2705 {
2706 if (mips_debug)
2707 fprintf_unfiltered (gdb_stdlog,
25ab4790 2708 "mips_n32n64_push_dummy_call: struct_return reg=%d 0x%s\n",
cb3d25d1
MS
2709 argreg, paddr_nz (struct_addr));
2710 write_register (argreg++, struct_addr);
2711 }
2712
2713 /* Now load as many as possible of the first arguments into
2714 registers, and push the rest onto the stack. Loop thru args
2715 from first to last. */
2716 for (argnum = 0; argnum < nargs; argnum++)
2717 {
2718 char *val;
cb3d25d1 2719 struct value *arg = args[argnum];
4991999e 2720 struct type *arg_type = check_typedef (value_type (arg));
cb3d25d1
MS
2721 int len = TYPE_LENGTH (arg_type);
2722 enum type_code typecode = TYPE_CODE (arg_type);
2723
2724 if (mips_debug)
2725 fprintf_unfiltered (gdb_stdlog,
25ab4790 2726 "mips_n32n64_push_dummy_call: %d len=%d type=%d",
cb3d25d1
MS
2727 argnum + 1, len, (int) typecode);
2728
0fd88904 2729 val = (char *) value_contents (arg);
cb3d25d1
MS
2730
2731 if (fp_register_arg_p (typecode, arg_type)
2732 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
2733 {
2734 /* This is a floating point value that fits entirely
2735 in a single register. */
2736 /* On 32 bit ABI's the float_argreg is further adjusted
2737 above to ensure that it is even register aligned. */
2738 LONGEST regval = extract_unsigned_integer (val, len);
2739 if (mips_debug)
2740 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
2741 float_argreg, phex (regval, len));
2742 write_register (float_argreg++, regval);
2743
2744 if (mips_debug)
2745 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
2746 argreg, phex (regval, len));
2747 write_register (argreg, regval);
2748 argreg += 1;
2749 }
2750 else
2751 {
2752 /* Copy the argument to general registers or the stack in
2753 register-sized pieces. Large arguments are split between
2754 registers and stack. */
4246e332 2755 /* Note: structs whose size is not a multiple of
d5ac5a39
AC
2756 mips_abi_regsize() are treated specially: Irix cc passes
2757 them in registers where gcc sometimes puts them on the
2758 stack. For maximum compatibility, we will put them in
2759 both places. */
13326b4e
AC
2760 int odd_sized_struct = ((len > mips_abi_regsize (gdbarch))
2761 && (len % mips_abi_regsize (gdbarch) != 0));
cb3d25d1 2762 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 2763 register are only written to memory. */
cb3d25d1
MS
2764 while (len > 0)
2765 {
2766 /* Rememer if the argument was written to the stack. */
2767 int stack_used_p = 0;
13326b4e
AC
2768 int partial_len = (len < mips_abi_regsize (gdbarch)
2769 ? len : mips_abi_regsize (gdbarch));
cb3d25d1
MS
2770
2771 if (mips_debug)
2772 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
2773 partial_len);
2774
2775 /* Write this portion of the argument to the stack. */
2776 if (argreg > MIPS_LAST_ARG_REGNUM
2777 || odd_sized_struct
2778 || fp_register_arg_p (typecode, arg_type))
2779 {
2780 /* Should shorter than int integer values be
2781 promoted to int before being stored? */
2782 int longword_offset = 0;
2783 CORE_ADDR addr;
2784 stack_used_p = 1;
2785 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
2786 {
13326b4e 2787 if (mips_stack_argsize (gdbarch) == 8
480d3dd2
AC
2788 && (typecode == TYPE_CODE_INT
2789 || typecode == TYPE_CODE_PTR
6d82d43b 2790 || typecode == TYPE_CODE_FLT) && len <= 4)
13326b4e 2791 longword_offset = mips_stack_argsize (gdbarch) - len;
cb3d25d1
MS
2792 }
2793
2794 if (mips_debug)
2795 {
2796 fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
2797 paddr_nz (stack_offset));
2798 fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
2799 paddr_nz (longword_offset));
2800 }
2801
2802 addr = sp + stack_offset + longword_offset;
2803
2804 if (mips_debug)
2805 {
2806 int i;
6d82d43b 2807 fprintf_unfiltered (gdb_stdlog, " @0x%s ",
cb3d25d1
MS
2808 paddr_nz (addr));
2809 for (i = 0; i < partial_len; i++)
2810 {
6d82d43b 2811 fprintf_unfiltered (gdb_stdlog, "%02x",
cb3d25d1
MS
2812 val[i] & 0xff);
2813 }
2814 }
2815 write_memory (addr, val, partial_len);
2816 }
2817
2818 /* Note!!! This is NOT an else clause. Odd sized
2819 structs may go thru BOTH paths. Floating point
2820 arguments will not. */
2821 /* Write this portion of the argument to a general
6d82d43b 2822 purpose register. */
cb3d25d1
MS
2823 if (argreg <= MIPS_LAST_ARG_REGNUM
2824 && !fp_register_arg_p (typecode, arg_type))
2825 {
6d82d43b
AC
2826 LONGEST regval =
2827 extract_unsigned_integer (val, partial_len);
cb3d25d1
MS
2828
2829 /* A non-floating-point argument being passed in a
2830 general register. If a struct or union, and if
2831 the remaining length is smaller than the register
2832 size, we have to adjust the register value on
2833 big endian targets.
2834
2835 It does not seem to be necessary to do the
2836 same for integral types.
2837
2838 cagney/2001-07-23: gdb/179: Also, GCC, when
2839 outputting LE O32 with sizeof (struct) <
1b13c4f6 2840 mips_abi_regsize(), generates a left shift as
cb3d25d1
MS
2841 part of storing the argument in a register a
2842 register (the left shift isn't generated when
1b13c4f6 2843 sizeof (struct) >= mips_abi_regsize()). Since
480d3dd2
AC
2844 it is quite possible that this is GCC
2845 contradicting the LE/O32 ABI, GDB has not been
2846 adjusted to accommodate this. Either someone
2847 needs to demonstrate that the LE/O32 ABI
2848 specifies such a left shift OR this new ABI gets
2849 identified as such and GDB gets tweaked
2850 accordingly. */
cb3d25d1
MS
2851
2852 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
13326b4e 2853 && partial_len < mips_abi_regsize (gdbarch)
cb3d25d1
MS
2854 && (typecode == TYPE_CODE_STRUCT ||
2855 typecode == TYPE_CODE_UNION))
13326b4e 2856 regval <<= ((mips_abi_regsize (gdbarch) - partial_len) *
cb3d25d1
MS
2857 TARGET_CHAR_BIT);
2858
2859 if (mips_debug)
2860 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
2861 argreg,
6d82d43b 2862 phex (regval,
13326b4e 2863 mips_abi_regsize (gdbarch)));
cb3d25d1
MS
2864 write_register (argreg, regval);
2865 argreg++;
2866 }
2867
2868 len -= partial_len;
2869 val += partial_len;
2870
2871 /* Compute the the offset into the stack at which we
6d82d43b 2872 will copy the next parameter.
cb3d25d1
MS
2873
2874 In N32 (N64?), the stack_offset only needs to be
2875 adjusted when it has been used. */
2876
2877 if (stack_used_p)
480d3dd2 2878 stack_offset += align_up (partial_len,
13326b4e 2879 mips_stack_argsize (gdbarch));
cb3d25d1
MS
2880 }
2881 }
2882 if (mips_debug)
2883 fprintf_unfiltered (gdb_stdlog, "\n");
2884 }
2885
f10683bb 2886 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 2887
cb3d25d1
MS
2888 /* Return adjusted stack pointer. */
2889 return sp;
2890}
2891
6d82d43b
AC
2892static enum return_value_convention
2893mips_n32n64_return_value (struct gdbarch *gdbarch,
2894 struct type *type, struct regcache *regcache,
2895 void *readbuf, const void *writebuf)
ebafbe83 2896{
6d82d43b
AC
2897 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2898 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2899 || TYPE_CODE (type) == TYPE_CODE_UNION
2900 || TYPE_CODE (type) == TYPE_CODE_ARRAY
13326b4e 2901 || TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
6d82d43b
AC
2902 return RETURN_VALUE_STRUCT_CONVENTION;
2903 else if (TYPE_CODE (type) == TYPE_CODE_FLT
2904 && tdep->mips_fpu_type != MIPS_FPU_NONE)
2905 {
2906 /* A floating-point value belongs in the least significant part
2907 of FP0. */
2908 if (mips_debug)
2909 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
2910 mips_xfer_register (regcache,
2911 NUM_REGS + mips_regnum (current_gdbarch)->fp0,
2912 TYPE_LENGTH (type),
2913 TARGET_BYTE_ORDER, readbuf, writebuf, 0);
2914 return RETURN_VALUE_REGISTER_CONVENTION;
2915 }
2916 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2917 && TYPE_NFIELDS (type) <= 2
2918 && TYPE_NFIELDS (type) >= 1
2919 && ((TYPE_NFIELDS (type) == 1
2920 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
2921 == TYPE_CODE_FLT))
2922 || (TYPE_NFIELDS (type) == 2
2923 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
2924 == TYPE_CODE_FLT)
2925 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
2926 == TYPE_CODE_FLT)))
2927 && tdep->mips_fpu_type != MIPS_FPU_NONE)
2928 {
2929 /* A struct that contains one or two floats. Each value is part
2930 in the least significant part of their floating point
2931 register.. */
6d82d43b
AC
2932 int regnum;
2933 int field;
2934 for (field = 0, regnum = mips_regnum (current_gdbarch)->fp0;
2935 field < TYPE_NFIELDS (type); field++, regnum += 2)
2936 {
2937 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
2938 / TARGET_CHAR_BIT);
2939 if (mips_debug)
2940 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
2941 offset);
2942 mips_xfer_register (regcache, NUM_REGS + regnum,
2943 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
2944 TARGET_BYTE_ORDER, readbuf, writebuf, offset);
2945 }
2946 return RETURN_VALUE_REGISTER_CONVENTION;
2947 }
2948 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2949 || TYPE_CODE (type) == TYPE_CODE_UNION)
2950 {
2951 /* A structure or union. Extract the left justified value,
2952 regardless of the byte order. I.e. DO NOT USE
2953 mips_xfer_lower. */
2954 int offset;
2955 int regnum;
4c7d22cb 2956 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b
AC
2957 offset < TYPE_LENGTH (type);
2958 offset += register_size (current_gdbarch, regnum), regnum++)
2959 {
2960 int xfer = register_size (current_gdbarch, regnum);
2961 if (offset + xfer > TYPE_LENGTH (type))
2962 xfer = TYPE_LENGTH (type) - offset;
2963 if (mips_debug)
2964 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
2965 offset, xfer, regnum);
2966 mips_xfer_register (regcache, NUM_REGS + regnum, xfer,
2967 BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
2968 }
2969 return RETURN_VALUE_REGISTER_CONVENTION;
2970 }
2971 else
2972 {
2973 /* A scalar extract each part but least-significant-byte
2974 justified. */
2975 int offset;
2976 int regnum;
4c7d22cb 2977 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b
AC
2978 offset < TYPE_LENGTH (type);
2979 offset += register_size (current_gdbarch, regnum), regnum++)
2980 {
2981 int xfer = register_size (current_gdbarch, regnum);
6d82d43b
AC
2982 if (offset + xfer > TYPE_LENGTH (type))
2983 xfer = TYPE_LENGTH (type) - offset;
2984 if (mips_debug)
2985 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
2986 offset, xfer, regnum);
2987 mips_xfer_register (regcache, NUM_REGS + regnum, xfer,
2988 TARGET_BYTE_ORDER, readbuf, writebuf, offset);
2989 }
2990 return RETURN_VALUE_REGISTER_CONVENTION;
2991 }
2992}
2993
2994/* O32 ABI stuff. */
2995
2996static CORE_ADDR
7d9b040b 2997mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
2998 struct regcache *regcache, CORE_ADDR bp_addr,
2999 int nargs, struct value **args, CORE_ADDR sp,
3000 int struct_return, CORE_ADDR struct_addr)
3001{
3002 int argreg;
3003 int float_argreg;
3004 int argnum;
3005 int len = 0;
3006 int stack_offset = 0;
3007 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7d9b040b 3008 CORE_ADDR func_addr = find_function_addr (function, NULL);
6d82d43b
AC
3009
3010 /* For shared libraries, "t9" needs to point at the function
3011 address. */
4c7d22cb 3012 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
6d82d43b
AC
3013
3014 /* Set the return address register to point to the entry point of
3015 the program, where a breakpoint lies in wait. */
4c7d22cb 3016 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
6d82d43b
AC
3017
3018 /* First ensure that the stack and structure return address (if any)
3019 are properly aligned. The stack has to be at least 64-bit
3020 aligned even on 32-bit machines, because doubles must be 64-bit
ebafbe83
MS
3021 aligned. For n32 and n64, stack frames need to be 128-bit
3022 aligned, so we round to this widest known alignment. */
3023
5b03f266
AC
3024 sp = align_down (sp, 16);
3025 struct_addr = align_down (struct_addr, 16);
ebafbe83
MS
3026
3027 /* Now make space on the stack for the args. */
3028 for (argnum = 0; argnum < nargs; argnum++)
4991999e 3029 len += align_up (TYPE_LENGTH (value_type (args[argnum])),
13326b4e 3030 mips_stack_argsize (gdbarch));
5b03f266 3031 sp -= align_up (len, 16);
ebafbe83
MS
3032
3033 if (mips_debug)
6d82d43b 3034 fprintf_unfiltered (gdb_stdlog,
5b03f266
AC
3035 "mips_o32_push_dummy_call: sp=0x%s allocated %ld\n",
3036 paddr_nz (sp), (long) align_up (len, 16));
ebafbe83
MS
3037
3038 /* Initialize the integer and float register pointers. */
4c7d22cb 3039 argreg = MIPS_A0_REGNUM;
56cea623 3040 float_argreg = mips_fpa0_regnum (current_gdbarch);
ebafbe83 3041
bcb0cc15 3042 /* The struct_return pointer occupies the first parameter-passing reg. */
ebafbe83
MS
3043 if (struct_return)
3044 {
3045 if (mips_debug)
3046 fprintf_unfiltered (gdb_stdlog,
25ab4790 3047 "mips_o32_push_dummy_call: struct_return reg=%d 0x%s\n",
ebafbe83
MS
3048 argreg, paddr_nz (struct_addr));
3049 write_register (argreg++, struct_addr);
13326b4e 3050 stack_offset += mips_stack_argsize (gdbarch);
ebafbe83
MS
3051 }
3052
3053 /* Now load as many as possible of the first arguments into
3054 registers, and push the rest onto the stack. Loop thru args
3055 from first to last. */
3056 for (argnum = 0; argnum < nargs; argnum++)
3057 {
3058 char *val;
ebafbe83 3059 struct value *arg = args[argnum];
4991999e 3060 struct type *arg_type = check_typedef (value_type (arg));
ebafbe83
MS
3061 int len = TYPE_LENGTH (arg_type);
3062 enum type_code typecode = TYPE_CODE (arg_type);
3063
3064 if (mips_debug)
3065 fprintf_unfiltered (gdb_stdlog,
25ab4790 3066 "mips_o32_push_dummy_call: %d len=%d type=%d",
46cac009
AC
3067 argnum + 1, len, (int) typecode);
3068
0fd88904 3069 val = (char *) value_contents (arg);
46cac009
AC
3070
3071 /* 32-bit ABIs always start floating point arguments in an
3072 even-numbered floating point register. Round the FP register
3073 up before the check to see if there are any FP registers
3074 left. O32/O64 targets also pass the FP in the integer
3075 registers so also round up normal registers. */
ceae6e75
AC
3076 if (mips_abi_regsize (gdbarch) < 8
3077 && fp_register_arg_p (typecode, arg_type))
46cac009
AC
3078 {
3079 if ((float_argreg & 1))
3080 float_argreg++;
3081 }
3082
3083 /* Floating point arguments passed in registers have to be
3084 treated specially. On 32-bit architectures, doubles
3085 are passed in register pairs; the even register gets
3086 the low word, and the odd register gets the high word.
3087 On O32/O64, the first two floating point arguments are
3088 also copied to general registers, because MIPS16 functions
3089 don't use float registers for arguments. This duplication of
3090 arguments in general registers can't hurt non-MIPS16 functions
3091 because those registers are normally skipped. */
3092
3093 if (fp_register_arg_p (typecode, arg_type)
3094 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
3095 {
ceae6e75 3096 if (mips_abi_regsize (gdbarch) < 8 && len == 8)
46cac009
AC
3097 {
3098 int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
3099 unsigned long regval;
3100
3101 /* Write the low word of the double to the even register(s). */
3102 regval = extract_unsigned_integer (val + low_offset, 4);
3103 if (mips_debug)
3104 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3105 float_argreg, phex (regval, 4));
3106 write_register (float_argreg++, regval);
3107 if (mips_debug)
3108 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3109 argreg, phex (regval, 4));
3110 write_register (argreg++, regval);
3111
3112 /* Write the high word of the double to the odd register(s). */
3113 regval = extract_unsigned_integer (val + 4 - low_offset, 4);
3114 if (mips_debug)
3115 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3116 float_argreg, phex (regval, 4));
3117 write_register (float_argreg++, regval);
3118
3119 if (mips_debug)
3120 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3121 argreg, phex (regval, 4));
3122 write_register (argreg++, regval);
3123 }
3124 else
3125 {
3126 /* This is a floating point value that fits entirely
3127 in a single register. */
3128 /* On 32 bit ABI's the float_argreg is further adjusted
6d82d43b 3129 above to ensure that it is even register aligned. */
46cac009
AC
3130 LONGEST regval = extract_unsigned_integer (val, len);
3131 if (mips_debug)
3132 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3133 float_argreg, phex (regval, len));
3134 write_register (float_argreg++, regval);
3135 /* CAGNEY: 32 bit MIPS ABI's always reserve two FP
6d82d43b
AC
3136 registers for each argument. The below is (my
3137 guess) to ensure that the corresponding integer
3138 register has reserved the same space. */
46cac009
AC
3139 if (mips_debug)
3140 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3141 argreg, phex (regval, len));
3142 write_register (argreg, regval);
ceae6e75 3143 argreg += (mips_abi_regsize (gdbarch) == 8) ? 1 : 2;
46cac009
AC
3144 }
3145 /* Reserve space for the FP register. */
13326b4e 3146 stack_offset += align_up (len, mips_stack_argsize (gdbarch));
46cac009
AC
3147 }
3148 else
3149 {
3150 /* Copy the argument to general registers or the stack in
3151 register-sized pieces. Large arguments are split between
3152 registers and stack. */
4246e332 3153 /* Note: structs whose size is not a multiple of
d5ac5a39
AC
3154 mips_abi_regsize() are treated specially: Irix cc passes
3155 them in registers where gcc sometimes puts them on the
3156 stack. For maximum compatibility, we will put them in
3157 both places. */
13326b4e
AC
3158 int odd_sized_struct = ((len > mips_abi_regsize (gdbarch))
3159 && (len % mips_abi_regsize (gdbarch) != 0));
46cac009
AC
3160 /* Structures should be aligned to eight bytes (even arg registers)
3161 on MIPS_ABI_O32, if their first member has double precision. */
13326b4e 3162 if (mips_abi_regsize (gdbarch) < 8
46cac009
AC
3163 && mips_type_needs_double_align (arg_type))
3164 {
3165 if ((argreg & 1))
6d82d43b 3166 argreg++;
46cac009
AC
3167 }
3168 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 3169 register are only written to memory. */
46cac009
AC
3170 while (len > 0)
3171 {
3172 /* Remember if the argument was written to the stack. */
3173 int stack_used_p = 0;
13326b4e
AC
3174 int partial_len = (len < mips_abi_regsize (gdbarch)
3175 ? len : mips_abi_regsize (gdbarch));
46cac009
AC
3176
3177 if (mips_debug)
3178 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3179 partial_len);
3180
3181 /* Write this portion of the argument to the stack. */
3182 if (argreg > MIPS_LAST_ARG_REGNUM
3183 || odd_sized_struct
3184 || fp_register_arg_p (typecode, arg_type))
3185 {
3186 /* Should shorter than int integer values be
3187 promoted to int before being stored? */
3188 int longword_offset = 0;
3189 CORE_ADDR addr;
3190 stack_used_p = 1;
3191 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3192 {
13326b4e 3193 if (mips_stack_argsize (gdbarch) == 8
480d3dd2
AC
3194 && (typecode == TYPE_CODE_INT
3195 || typecode == TYPE_CODE_PTR
6d82d43b 3196 || typecode == TYPE_CODE_FLT) && len <= 4)
13326b4e 3197 longword_offset = mips_stack_argsize (gdbarch) - len;
46cac009
AC
3198 }
3199
3200 if (mips_debug)
3201 {
3202 fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
3203 paddr_nz (stack_offset));
3204 fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
3205 paddr_nz (longword_offset));
3206 }
3207
3208 addr = sp + stack_offset + longword_offset;
3209
3210 if (mips_debug)
3211 {
3212 int i;
6d82d43b 3213 fprintf_unfiltered (gdb_stdlog, " @0x%s ",
46cac009
AC
3214 paddr_nz (addr));
3215 for (i = 0; i < partial_len; i++)
3216 {
6d82d43b 3217 fprintf_unfiltered (gdb_stdlog, "%02x",
46cac009
AC
3218 val[i] & 0xff);
3219 }
3220 }
3221 write_memory (addr, val, partial_len);
3222 }
3223
3224 /* Note!!! This is NOT an else clause. Odd sized
3225 structs may go thru BOTH paths. Floating point
3226 arguments will not. */
3227 /* Write this portion of the argument to a general
6d82d43b 3228 purpose register. */
46cac009
AC
3229 if (argreg <= MIPS_LAST_ARG_REGNUM
3230 && !fp_register_arg_p (typecode, arg_type))
3231 {
3232 LONGEST regval = extract_signed_integer (val, partial_len);
4246e332 3233 /* Value may need to be sign extended, because
1b13c4f6 3234 mips_isa_regsize() != mips_abi_regsize(). */
46cac009
AC
3235
3236 /* A non-floating-point argument being passed in a
3237 general register. If a struct or union, and if
3238 the remaining length is smaller than the register
3239 size, we have to adjust the register value on
3240 big endian targets.
3241
3242 It does not seem to be necessary to do the
3243 same for integral types.
3244
3245 Also don't do this adjustment on O64 binaries.
3246
3247 cagney/2001-07-23: gdb/179: Also, GCC, when
3248 outputting LE O32 with sizeof (struct) <
1b13c4f6 3249 mips_abi_regsize(), generates a left shift as
46cac009
AC
3250 part of storing the argument in a register a
3251 register (the left shift isn't generated when
1b13c4f6 3252 sizeof (struct) >= mips_abi_regsize()). Since
480d3dd2
AC
3253 it is quite possible that this is GCC
3254 contradicting the LE/O32 ABI, GDB has not been
3255 adjusted to accommodate this. Either someone
3256 needs to demonstrate that the LE/O32 ABI
3257 specifies such a left shift OR this new ABI gets
3258 identified as such and GDB gets tweaked
3259 accordingly. */
3260
13326b4e 3261 if (mips_abi_regsize (gdbarch) < 8
46cac009 3262 && TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
13326b4e 3263 && partial_len < mips_abi_regsize (gdbarch)
46cac009
AC
3264 && (typecode == TYPE_CODE_STRUCT ||
3265 typecode == TYPE_CODE_UNION))
13326b4e 3266 regval <<= ((mips_abi_regsize (gdbarch) - partial_len) *
46cac009
AC
3267 TARGET_CHAR_BIT);
3268
3269 if (mips_debug)
3270 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3271 argreg,
6d82d43b 3272 phex (regval,
13326b4e 3273 mips_abi_regsize (gdbarch)));
46cac009
AC
3274 write_register (argreg, regval);
3275 argreg++;
3276
3277 /* Prevent subsequent floating point arguments from
3278 being passed in floating point registers. */
3279 float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
3280 }
3281
3282 len -= partial_len;
3283 val += partial_len;
3284
3285 /* Compute the the offset into the stack at which we
6d82d43b 3286 will copy the next parameter.
46cac009 3287
6d82d43b
AC
3288 In older ABIs, the caller reserved space for
3289 registers that contained arguments. This was loosely
3290 refered to as their "home". Consequently, space is
3291 always allocated. */
46cac009 3292
480d3dd2 3293 stack_offset += align_up (partial_len,
13326b4e 3294 mips_stack_argsize (gdbarch));
46cac009
AC
3295 }
3296 }
3297 if (mips_debug)
3298 fprintf_unfiltered (gdb_stdlog, "\n");
3299 }
3300
f10683bb 3301 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 3302
46cac009
AC
3303 /* Return adjusted stack pointer. */
3304 return sp;
3305}
3306
6d82d43b
AC
3307static enum return_value_convention
3308mips_o32_return_value (struct gdbarch *gdbarch, struct type *type,
3309 struct regcache *regcache,
3310 void *readbuf, const void *writebuf)
3311{
3312 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
3313
3314 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3315 || TYPE_CODE (type) == TYPE_CODE_UNION
3316 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
3317 return RETURN_VALUE_STRUCT_CONVENTION;
3318 else if (TYPE_CODE (type) == TYPE_CODE_FLT
3319 && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
3320 {
3321 /* A single-precision floating-point value. It fits in the
3322 least significant part of FP0. */
3323 if (mips_debug)
3324 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
3325 mips_xfer_register (regcache,
3326 NUM_REGS + mips_regnum (current_gdbarch)->fp0,
3327 TYPE_LENGTH (type),
3328 TARGET_BYTE_ORDER, readbuf, writebuf, 0);
3329 return RETURN_VALUE_REGISTER_CONVENTION;
3330 }
3331 else if (TYPE_CODE (type) == TYPE_CODE_FLT
3332 && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
3333 {
3334 /* A double-precision floating-point value. The most
3335 significant part goes in FP1, and the least significant in
3336 FP0. */
3337 if (mips_debug)
3338 fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
3339 switch (TARGET_BYTE_ORDER)
3340 {
3341 case BFD_ENDIAN_LITTLE:
3342 mips_xfer_register (regcache,
3343 NUM_REGS + mips_regnum (current_gdbarch)->fp0 +
3344 0, 4, TARGET_BYTE_ORDER, readbuf, writebuf, 0);
3345 mips_xfer_register (regcache,
3346 NUM_REGS + mips_regnum (current_gdbarch)->fp0 +
3347 1, 4, TARGET_BYTE_ORDER, readbuf, writebuf, 4);
3348 break;
3349 case BFD_ENDIAN_BIG:
3350 mips_xfer_register (regcache,
3351 NUM_REGS + mips_regnum (current_gdbarch)->fp0 +
3352 1, 4, TARGET_BYTE_ORDER, readbuf, writebuf, 0);
3353 mips_xfer_register (regcache,
3354 NUM_REGS + mips_regnum (current_gdbarch)->fp0 +
3355 0, 4, TARGET_BYTE_ORDER, readbuf, writebuf, 4);
3356 break;
3357 default:
e2e0b3e5 3358 internal_error (__FILE__, __LINE__, _("bad switch"));
6d82d43b
AC
3359 }
3360 return RETURN_VALUE_REGISTER_CONVENTION;
3361 }
3362#if 0
3363 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3364 && TYPE_NFIELDS (type) <= 2
3365 && TYPE_NFIELDS (type) >= 1
3366 && ((TYPE_NFIELDS (type) == 1
3367 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
3368 == TYPE_CODE_FLT))
3369 || (TYPE_NFIELDS (type) == 2
3370 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
3371 == TYPE_CODE_FLT)
3372 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
3373 == TYPE_CODE_FLT)))
3374 && tdep->mips_fpu_type != MIPS_FPU_NONE)
3375 {
3376 /* A struct that contains one or two floats. Each value is part
3377 in the least significant part of their floating point
3378 register.. */
3379 bfd_byte reg[MAX_REGISTER_SIZE];
3380 int regnum;
3381 int field;
3382 for (field = 0, regnum = mips_regnum (current_gdbarch)->fp0;
3383 field < TYPE_NFIELDS (type); field++, regnum += 2)
3384 {
3385 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
3386 / TARGET_CHAR_BIT);
3387 if (mips_debug)
3388 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
3389 offset);
3390 mips_xfer_register (regcache, NUM_REGS + regnum,
3391 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
3392 TARGET_BYTE_ORDER, readbuf, writebuf, offset);
3393 }
3394 return RETURN_VALUE_REGISTER_CONVENTION;
3395 }
3396#endif
3397#if 0
3398 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3399 || TYPE_CODE (type) == TYPE_CODE_UNION)
3400 {
3401 /* A structure or union. Extract the left justified value,
3402 regardless of the byte order. I.e. DO NOT USE
3403 mips_xfer_lower. */
3404 int offset;
3405 int regnum;
4c7d22cb 3406 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b
AC
3407 offset < TYPE_LENGTH (type);
3408 offset += register_size (current_gdbarch, regnum), regnum++)
3409 {
3410 int xfer = register_size (current_gdbarch, regnum);
3411 if (offset + xfer > TYPE_LENGTH (type))
3412 xfer = TYPE_LENGTH (type) - offset;
3413 if (mips_debug)
3414 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
3415 offset, xfer, regnum);
3416 mips_xfer_register (regcache, NUM_REGS + regnum, xfer,
3417 BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
3418 }
3419 return RETURN_VALUE_REGISTER_CONVENTION;
3420 }
3421#endif
3422 else
3423 {
3424 /* A scalar extract each part but least-significant-byte
3425 justified. o32 thinks registers are 4 byte, regardless of
3426 the ISA. mips_stack_argsize controls this. */
3427 int offset;
3428 int regnum;
4c7d22cb 3429 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b 3430 offset < TYPE_LENGTH (type);
13326b4e 3431 offset += mips_stack_argsize (gdbarch), regnum++)
6d82d43b 3432 {
13326b4e 3433 int xfer = mips_stack_argsize (gdbarch);
6d82d43b
AC
3434 if (offset + xfer > TYPE_LENGTH (type))
3435 xfer = TYPE_LENGTH (type) - offset;
3436 if (mips_debug)
3437 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
3438 offset, xfer, regnum);
3439 mips_xfer_register (regcache, NUM_REGS + regnum, xfer,
3440 TARGET_BYTE_ORDER, readbuf, writebuf, offset);
3441 }
3442 return RETURN_VALUE_REGISTER_CONVENTION;
3443 }
3444}
3445
3446/* O64 ABI. This is a hacked up kind of 64-bit version of the o32
3447 ABI. */
46cac009
AC
3448
3449static CORE_ADDR
7d9b040b 3450mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
3451 struct regcache *regcache, CORE_ADDR bp_addr,
3452 int nargs,
3453 struct value **args, CORE_ADDR sp,
3454 int struct_return, CORE_ADDR struct_addr)
46cac009
AC
3455{
3456 int argreg;
3457 int float_argreg;
3458 int argnum;
3459 int len = 0;
3460 int stack_offset = 0;
480d3dd2 3461 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7d9b040b 3462 CORE_ADDR func_addr = find_function_addr (function, NULL);
46cac009 3463
25ab4790
AC
3464 /* For shared libraries, "t9" needs to point at the function
3465 address. */
4c7d22cb 3466 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
3467
3468 /* Set the return address register to point to the entry point of
3469 the program, where a breakpoint lies in wait. */
4c7d22cb 3470 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 3471
46cac009
AC
3472 /* First ensure that the stack and structure return address (if any)
3473 are properly aligned. The stack has to be at least 64-bit
3474 aligned even on 32-bit machines, because doubles must be 64-bit
3475 aligned. For n32 and n64, stack frames need to be 128-bit
3476 aligned, so we round to this widest known alignment. */
3477
5b03f266
AC
3478 sp = align_down (sp, 16);
3479 struct_addr = align_down (struct_addr, 16);
46cac009
AC
3480
3481 /* Now make space on the stack for the args. */
3482 for (argnum = 0; argnum < nargs; argnum++)
4991999e 3483 len += align_up (TYPE_LENGTH (value_type (args[argnum])),
13326b4e 3484 mips_stack_argsize (gdbarch));
5b03f266 3485 sp -= align_up (len, 16);
46cac009
AC
3486
3487 if (mips_debug)
6d82d43b 3488 fprintf_unfiltered (gdb_stdlog,
5b03f266
AC
3489 "mips_o64_push_dummy_call: sp=0x%s allocated %ld\n",
3490 paddr_nz (sp), (long) align_up (len, 16));
46cac009
AC
3491
3492 /* Initialize the integer and float register pointers. */
4c7d22cb 3493 argreg = MIPS_A0_REGNUM;
56cea623 3494 float_argreg = mips_fpa0_regnum (current_gdbarch);
46cac009
AC
3495
3496 /* The struct_return pointer occupies the first parameter-passing reg. */
3497 if (struct_return)
3498 {
3499 if (mips_debug)
3500 fprintf_unfiltered (gdb_stdlog,
25ab4790 3501 "mips_o64_push_dummy_call: struct_return reg=%d 0x%s\n",
46cac009
AC
3502 argreg, paddr_nz (struct_addr));
3503 write_register (argreg++, struct_addr);
13326b4e 3504 stack_offset += mips_stack_argsize (gdbarch);
46cac009
AC
3505 }
3506
3507 /* Now load as many as possible of the first arguments into
3508 registers, and push the rest onto the stack. Loop thru args
3509 from first to last. */
3510 for (argnum = 0; argnum < nargs; argnum++)
3511 {
3512 char *val;
46cac009 3513 struct value *arg = args[argnum];
4991999e 3514 struct type *arg_type = check_typedef (value_type (arg));
46cac009
AC
3515 int len = TYPE_LENGTH (arg_type);
3516 enum type_code typecode = TYPE_CODE (arg_type);
3517
3518 if (mips_debug)
3519 fprintf_unfiltered (gdb_stdlog,
25ab4790 3520 "mips_o64_push_dummy_call: %d len=%d type=%d",
ebafbe83
MS
3521 argnum + 1, len, (int) typecode);
3522
0fd88904 3523 val = (char *) value_contents (arg);
ebafbe83
MS
3524
3525 /* 32-bit ABIs always start floating point arguments in an
3526 even-numbered floating point register. Round the FP register
3527 up before the check to see if there are any FP registers
3528 left. O32/O64 targets also pass the FP in the integer
3529 registers so also round up normal registers. */
ceae6e75
AC
3530 if (mips_abi_regsize (gdbarch) < 8
3531 && fp_register_arg_p (typecode, arg_type))
ebafbe83
MS
3532 {
3533 if ((float_argreg & 1))
3534 float_argreg++;
3535 }
3536
3537 /* Floating point arguments passed in registers have to be
3538 treated specially. On 32-bit architectures, doubles
3539 are passed in register pairs; the even register gets
3540 the low word, and the odd register gets the high word.
3541 On O32/O64, the first two floating point arguments are
3542 also copied to general registers, because MIPS16 functions
3543 don't use float registers for arguments. This duplication of
3544 arguments in general registers can't hurt non-MIPS16 functions
3545 because those registers are normally skipped. */
3546
3547 if (fp_register_arg_p (typecode, arg_type)
3548 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM)
3549 {
ceae6e75 3550 if (mips_abi_regsize (gdbarch) < 8 && len == 8)
ebafbe83
MS
3551 {
3552 int low_offset = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? 4 : 0;
3553 unsigned long regval;
3554
3555 /* Write the low word of the double to the even register(s). */
3556 regval = extract_unsigned_integer (val + low_offset, 4);
3557 if (mips_debug)
3558 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3559 float_argreg, phex (regval, 4));
3560 write_register (float_argreg++, regval);
3561 if (mips_debug)
3562 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3563 argreg, phex (regval, 4));
3564 write_register (argreg++, regval);
3565
3566 /* Write the high word of the double to the odd register(s). */
3567 regval = extract_unsigned_integer (val + 4 - low_offset, 4);
3568 if (mips_debug)
3569 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3570 float_argreg, phex (regval, 4));
3571 write_register (float_argreg++, regval);
3572
3573 if (mips_debug)
3574 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3575 argreg, phex (regval, 4));
3576 write_register (argreg++, regval);
3577 }
3578 else
3579 {
3580 /* This is a floating point value that fits entirely
3581 in a single register. */
3582 /* On 32 bit ABI's the float_argreg is further adjusted
6d82d43b 3583 above to ensure that it is even register aligned. */
ebafbe83
MS
3584 LONGEST regval = extract_unsigned_integer (val, len);
3585 if (mips_debug)
3586 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3587 float_argreg, phex (regval, len));
3588 write_register (float_argreg++, regval);
3589 /* CAGNEY: 32 bit MIPS ABI's always reserve two FP
6d82d43b
AC
3590 registers for each argument. The below is (my
3591 guess) to ensure that the corresponding integer
3592 register has reserved the same space. */
ebafbe83
MS
3593 if (mips_debug)
3594 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3595 argreg, phex (regval, len));
3596 write_register (argreg, regval);
ceae6e75 3597 argreg += (mips_abi_regsize (gdbarch) == 8) ? 1 : 2;
ebafbe83
MS
3598 }
3599 /* Reserve space for the FP register. */
13326b4e 3600 stack_offset += align_up (len, mips_stack_argsize (gdbarch));
ebafbe83
MS
3601 }
3602 else
3603 {
3604 /* Copy the argument to general registers or the stack in
3605 register-sized pieces. Large arguments are split between
3606 registers and stack. */
4246e332 3607 /* Note: structs whose size is not a multiple of
d5ac5a39
AC
3608 mips_abi_regsize() are treated specially: Irix cc passes
3609 them in registers where gcc sometimes puts them on the
3610 stack. For maximum compatibility, we will put them in
3611 both places. */
13326b4e
AC
3612 int odd_sized_struct = ((len > mips_abi_regsize (gdbarch))
3613 && (len % mips_abi_regsize (gdbarch) != 0));
ebafbe83
MS
3614 /* Structures should be aligned to eight bytes (even arg registers)
3615 on MIPS_ABI_O32, if their first member has double precision. */
13326b4e 3616 if (mips_abi_regsize (gdbarch) < 8
ebafbe83
MS
3617 && mips_type_needs_double_align (arg_type))
3618 {
3619 if ((argreg & 1))
6d82d43b 3620 argreg++;
ebafbe83
MS
3621 }
3622 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 3623 register are only written to memory. */
ebafbe83
MS
3624 while (len > 0)
3625 {
3626 /* Remember if the argument was written to the stack. */
3627 int stack_used_p = 0;
13326b4e
AC
3628 int partial_len = (len < mips_abi_regsize (gdbarch)
3629 ? len : mips_abi_regsize (gdbarch));
ebafbe83
MS
3630
3631 if (mips_debug)
3632 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3633 partial_len);
3634
3635 /* Write this portion of the argument to the stack. */
3636 if (argreg > MIPS_LAST_ARG_REGNUM
3637 || odd_sized_struct
3638 || fp_register_arg_p (typecode, arg_type))
3639 {
3640 /* Should shorter than int integer values be
3641 promoted to int before being stored? */
3642 int longword_offset = 0;
3643 CORE_ADDR addr;
3644 stack_used_p = 1;
3645 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3646 {
13326b4e 3647 if (mips_stack_argsize (gdbarch) == 8
480d3dd2
AC
3648 && (typecode == TYPE_CODE_INT
3649 || typecode == TYPE_CODE_PTR
6d82d43b 3650 || typecode == TYPE_CODE_FLT) && len <= 4)
13326b4e 3651 longword_offset = mips_stack_argsize (gdbarch) - len;
ebafbe83
MS
3652 }
3653
3654 if (mips_debug)
3655 {
3656 fprintf_unfiltered (gdb_stdlog, " - stack_offset=0x%s",
3657 paddr_nz (stack_offset));
3658 fprintf_unfiltered (gdb_stdlog, " longword_offset=0x%s",
3659 paddr_nz (longword_offset));
3660 }
3661
3662 addr = sp + stack_offset + longword_offset;
3663
3664 if (mips_debug)
3665 {
3666 int i;
6d82d43b 3667 fprintf_unfiltered (gdb_stdlog, " @0x%s ",
ebafbe83
MS
3668 paddr_nz (addr));
3669 for (i = 0; i < partial_len; i++)
3670 {
6d82d43b 3671 fprintf_unfiltered (gdb_stdlog, "%02x",
ebafbe83
MS
3672 val[i] & 0xff);
3673 }
3674 }
3675 write_memory (addr, val, partial_len);
3676 }
3677
3678 /* Note!!! This is NOT an else clause. Odd sized
3679 structs may go thru BOTH paths. Floating point
3680 arguments will not. */
3681 /* Write this portion of the argument to a general
6d82d43b 3682 purpose register. */
ebafbe83
MS
3683 if (argreg <= MIPS_LAST_ARG_REGNUM
3684 && !fp_register_arg_p (typecode, arg_type))
3685 {
3686 LONGEST regval = extract_signed_integer (val, partial_len);
4246e332 3687 /* Value may need to be sign extended, because
1b13c4f6 3688 mips_isa_regsize() != mips_abi_regsize(). */
ebafbe83
MS
3689
3690 /* A non-floating-point argument being passed in a
3691 general register. If a struct or union, and if
3692 the remaining length is smaller than the register
3693 size, we have to adjust the register value on
3694 big endian targets.
3695
3696 It does not seem to be necessary to do the
3697 same for integral types.
3698
3699 Also don't do this adjustment on O64 binaries.
3700
3701 cagney/2001-07-23: gdb/179: Also, GCC, when
3702 outputting LE O32 with sizeof (struct) <
1b13c4f6 3703 mips_abi_regsize(), generates a left shift as
ebafbe83
MS
3704 part of storing the argument in a register a
3705 register (the left shift isn't generated when
1b13c4f6 3706 sizeof (struct) >= mips_abi_regsize()). Since
480d3dd2
AC
3707 it is quite possible that this is GCC
3708 contradicting the LE/O32 ABI, GDB has not been
3709 adjusted to accommodate this. Either someone
3710 needs to demonstrate that the LE/O32 ABI
3711 specifies such a left shift OR this new ABI gets
3712 identified as such and GDB gets tweaked
3713 accordingly. */
3714
13326b4e 3715 if (mips_abi_regsize (gdbarch) < 8
ebafbe83 3716 && TARGET_BYTE_ORDER == BFD_ENDIAN_BIG
13326b4e 3717 && partial_len < mips_abi_regsize (gdbarch)
ebafbe83
MS
3718 && (typecode == TYPE_CODE_STRUCT ||
3719 typecode == TYPE_CODE_UNION))
13326b4e 3720 regval <<= ((mips_abi_regsize (gdbarch) - partial_len) *
ebafbe83
MS
3721 TARGET_CHAR_BIT);
3722
3723 if (mips_debug)
3724 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3725 argreg,
6d82d43b 3726 phex (regval,
13326b4e 3727 mips_abi_regsize (gdbarch)));
ebafbe83
MS
3728 write_register (argreg, regval);
3729 argreg++;
3730
3731 /* Prevent subsequent floating point arguments from
3732 being passed in floating point registers. */
3733 float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
3734 }
3735
3736 len -= partial_len;
3737 val += partial_len;
3738
3739 /* Compute the the offset into the stack at which we
6d82d43b 3740 will copy the next parameter.
ebafbe83 3741
6d82d43b
AC
3742 In older ABIs, the caller reserved space for
3743 registers that contained arguments. This was loosely
3744 refered to as their "home". Consequently, space is
3745 always allocated. */
ebafbe83 3746
480d3dd2 3747 stack_offset += align_up (partial_len,
13326b4e 3748 mips_stack_argsize (gdbarch));
ebafbe83
MS
3749 }
3750 }
3751 if (mips_debug)
3752 fprintf_unfiltered (gdb_stdlog, "\n");
3753 }
3754
f10683bb 3755 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 3756
ebafbe83
MS
3757 /* Return adjusted stack pointer. */
3758 return sp;
3759}
3760
9c8fdbfa
AC
3761static enum return_value_convention
3762mips_o64_return_value (struct gdbarch *gdbarch,
3763 struct type *type, struct regcache *regcache,
3764 void *readbuf, const void *writebuf)
6d82d43b 3765{
9c8fdbfa 3766 return RETURN_VALUE_STRUCT_CONVENTION;
6d82d43b
AC
3767}
3768
dd824b04
DJ
3769/* Floating point register management.
3770
3771 Background: MIPS1 & 2 fp registers are 32 bits wide. To support
3772 64bit operations, these early MIPS cpus treat fp register pairs
3773 (f0,f1) as a single register (d0). Later MIPS cpu's have 64 bit fp
3774 registers and offer a compatibility mode that emulates the MIPS2 fp
3775 model. When operating in MIPS2 fp compat mode, later cpu's split
3776 double precision floats into two 32-bit chunks and store them in
3777 consecutive fp regs. To display 64-bit floats stored in this
3778 fashion, we have to combine 32 bits from f0 and 32 bits from f1.
3779 Throw in user-configurable endianness and you have a real mess.
3780
3781 The way this works is:
3782 - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
3783 double-precision value will be split across two logical registers.
3784 The lower-numbered logical register will hold the low-order bits,
3785 regardless of the processor's endianness.
3786 - If we are on a 64-bit processor, and we are looking for a
3787 single-precision value, it will be in the low ordered bits
3788 of a 64-bit GPR (after mfc1, for example) or a 64-bit register
3789 save slot in memory.
3790 - If we are in 64-bit mode, everything is straightforward.
3791
3792 Note that this code only deals with "live" registers at the top of the
3793 stack. We will attempt to deal with saved registers later, when
3794 the raw/cooked register interface is in place. (We need a general
3795 interface that can deal with dynamic saved register sizes -- fp
3796 regs could be 32 bits wide in one frame and 64 on the frame above
3797 and below). */
3798
67b2c998
DJ
3799static struct type *
3800mips_float_register_type (void)
3801{
361d1df0 3802 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
67b2c998
DJ
3803 return builtin_type_ieee_single_big;
3804 else
3805 return builtin_type_ieee_single_little;
3806}
3807
3808static struct type *
3809mips_double_register_type (void)
3810{
361d1df0 3811 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
67b2c998
DJ
3812 return builtin_type_ieee_double_big;
3813 else
3814 return builtin_type_ieee_double_little;
3815}
3816
dd824b04
DJ
3817/* Copy a 32-bit single-precision value from the current frame
3818 into rare_buffer. */
3819
3820static void
e11c53d2
AC
3821mips_read_fp_register_single (struct frame_info *frame, int regno,
3822 char *rare_buffer)
dd824b04 3823{
719ec221 3824 int raw_size = register_size (current_gdbarch, regno);
dd824b04
DJ
3825 char *raw_buffer = alloca (raw_size);
3826
e11c53d2 3827 if (!frame_register_read (frame, regno, raw_buffer))
8a3fe4f8 3828 error (_("can't read register %d (%s)"), regno, REGISTER_NAME (regno));
dd824b04
DJ
3829 if (raw_size == 8)
3830 {
3831 /* We have a 64-bit value for this register. Find the low-order
6d82d43b 3832 32 bits. */
dd824b04
DJ
3833 int offset;
3834
3835 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3836 offset = 4;
3837 else
3838 offset = 0;
3839
3840 memcpy (rare_buffer, raw_buffer + offset, 4);
3841 }
3842 else
3843 {
3844 memcpy (rare_buffer, raw_buffer, 4);
3845 }
3846}
3847
3848/* Copy a 64-bit double-precision value from the current frame into
3849 rare_buffer. This may include getting half of it from the next
3850 register. */
3851
3852static void
e11c53d2
AC
3853mips_read_fp_register_double (struct frame_info *frame, int regno,
3854 char *rare_buffer)
dd824b04 3855{
719ec221 3856 int raw_size = register_size (current_gdbarch, regno);
dd824b04
DJ
3857
3858 if (raw_size == 8 && !mips2_fp_compat ())
3859 {
3860 /* We have a 64-bit value for this register, and we should use
6d82d43b 3861 all 64 bits. */
e11c53d2 3862 if (!frame_register_read (frame, regno, rare_buffer))
8a3fe4f8 3863 error (_("can't read register %d (%s)"), regno, REGISTER_NAME (regno));
dd824b04
DJ
3864 }
3865 else
3866 {
56cea623 3867 if ((regno - mips_regnum (current_gdbarch)->fp0) & 1)
dd824b04 3868 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
3869 _("mips_read_fp_register_double: bad access to "
3870 "odd-numbered FP register"));
dd824b04
DJ
3871
3872 /* mips_read_fp_register_single will find the correct 32 bits from
6d82d43b 3873 each register. */
dd824b04
DJ
3874 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
3875 {
e11c53d2
AC
3876 mips_read_fp_register_single (frame, regno, rare_buffer + 4);
3877 mips_read_fp_register_single (frame, regno + 1, rare_buffer);
dd824b04 3878 }
361d1df0 3879 else
dd824b04 3880 {
e11c53d2
AC
3881 mips_read_fp_register_single (frame, regno, rare_buffer);
3882 mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
dd824b04
DJ
3883 }
3884 }
3885}
3886
c906108c 3887static void
e11c53d2
AC
3888mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
3889 int regnum)
c5aa993b 3890{ /* do values for FP (float) regs */
dd824b04 3891 char *raw_buffer;
3903d437
AC
3892 double doub, flt1; /* doubles extracted from raw hex data */
3893 int inv1, inv2;
c5aa993b 3894
6d82d43b
AC
3895 raw_buffer =
3896 (char *) alloca (2 *
3897 register_size (current_gdbarch,
3898 mips_regnum (current_gdbarch)->fp0));
c906108c 3899
e11c53d2
AC
3900 fprintf_filtered (file, "%s:", REGISTER_NAME (regnum));
3901 fprintf_filtered (file, "%*s", 4 - (int) strlen (REGISTER_NAME (regnum)),
3902 "");
f0ef6b29 3903
719ec221 3904 if (register_size (current_gdbarch, regnum) == 4 || mips2_fp_compat ())
c906108c 3905 {
f0ef6b29
KB
3906 /* 4-byte registers: Print hex and floating. Also print even
3907 numbered registers as doubles. */
e11c53d2 3908 mips_read_fp_register_single (frame, regnum, raw_buffer);
67b2c998 3909 flt1 = unpack_double (mips_float_register_type (), raw_buffer, &inv1);
c5aa993b 3910
6d82d43b
AC
3911 print_scalar_formatted (raw_buffer, builtin_type_uint32, 'x', 'w',
3912 file);
dd824b04 3913
e11c53d2 3914 fprintf_filtered (file, " flt: ");
1adad886 3915 if (inv1)
e11c53d2 3916 fprintf_filtered (file, " <invalid float> ");
1adad886 3917 else
e11c53d2 3918 fprintf_filtered (file, "%-17.9g", flt1);
1adad886 3919
f0ef6b29
KB
3920 if (regnum % 2 == 0)
3921 {
e11c53d2 3922 mips_read_fp_register_double (frame, regnum, raw_buffer);
f0ef6b29 3923 doub = unpack_double (mips_double_register_type (), raw_buffer,
6d82d43b 3924 &inv2);
1adad886 3925
e11c53d2 3926 fprintf_filtered (file, " dbl: ");
f0ef6b29 3927 if (inv2)
e11c53d2 3928 fprintf_filtered (file, "<invalid double>");
f0ef6b29 3929 else
e11c53d2 3930 fprintf_filtered (file, "%-24.17g", doub);
f0ef6b29 3931 }
c906108c
SS
3932 }
3933 else
dd824b04 3934 {
f0ef6b29 3935 /* Eight byte registers: print each one as hex, float and double. */
e11c53d2 3936 mips_read_fp_register_single (frame, regnum, raw_buffer);
2f38ef89 3937 flt1 = unpack_double (mips_float_register_type (), raw_buffer, &inv1);
c906108c 3938
e11c53d2 3939 mips_read_fp_register_double (frame, regnum, raw_buffer);
f0ef6b29
KB
3940 doub = unpack_double (mips_double_register_type (), raw_buffer, &inv2);
3941
361d1df0 3942
6d82d43b
AC
3943 print_scalar_formatted (raw_buffer, builtin_type_uint64, 'x', 'g',
3944 file);
f0ef6b29 3945
e11c53d2 3946 fprintf_filtered (file, " flt: ");
1adad886 3947 if (inv1)
e11c53d2 3948 fprintf_filtered (file, "<invalid float>");
1adad886 3949 else
e11c53d2 3950 fprintf_filtered (file, "%-17.9g", flt1);
1adad886 3951
e11c53d2 3952 fprintf_filtered (file, " dbl: ");
f0ef6b29 3953 if (inv2)
e11c53d2 3954 fprintf_filtered (file, "<invalid double>");
1adad886 3955 else
e11c53d2 3956 fprintf_filtered (file, "%-24.17g", doub);
f0ef6b29
KB
3957 }
3958}
3959
3960static void
e11c53d2
AC
3961mips_print_register (struct ui_file *file, struct frame_info *frame,
3962 int regnum, int all)
f0ef6b29 3963{
a4b8ebc8 3964 struct gdbarch *gdbarch = get_frame_arch (frame);
d9d9c31f 3965 char raw_buffer[MAX_REGISTER_SIZE];
f0ef6b29 3966 int offset;
1adad886 3967
a4b8ebc8 3968 if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) == TYPE_CODE_FLT)
f0ef6b29 3969 {
e11c53d2 3970 mips_print_fp_register (file, frame, regnum);
f0ef6b29
KB
3971 return;
3972 }
3973
3974 /* Get the data in raw format. */
e11c53d2 3975 if (!frame_register_read (frame, regnum, raw_buffer))
f0ef6b29 3976 {
e11c53d2 3977 fprintf_filtered (file, "%s: [Invalid]", REGISTER_NAME (regnum));
f0ef6b29 3978 return;
c906108c 3979 }
f0ef6b29 3980
e11c53d2 3981 fputs_filtered (REGISTER_NAME (regnum), file);
f0ef6b29
KB
3982
3983 /* The problem with printing numeric register names (r26, etc.) is that
3984 the user can't use them on input. Probably the best solution is to
3985 fix it so that either the numeric or the funky (a2, etc.) names
3986 are accepted on input. */
3987 if (regnum < MIPS_NUMREGS)
e11c53d2 3988 fprintf_filtered (file, "(r%d): ", regnum);
f0ef6b29 3989 else
e11c53d2 3990 fprintf_filtered (file, ": ");
f0ef6b29
KB
3991
3992 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
6d82d43b
AC
3993 offset =
3994 register_size (current_gdbarch,
3995 regnum) - register_size (current_gdbarch, regnum);
f0ef6b29
KB
3996 else
3997 offset = 0;
3998
6d82d43b
AC
3999 print_scalar_formatted (raw_buffer + offset,
4000 gdbarch_register_type (gdbarch, regnum), 'x', 0,
4001 file);
c906108c
SS
4002}
4003
f0ef6b29
KB
4004/* Replacement for generic do_registers_info.
4005 Print regs in pretty columns. */
4006
4007static int
e11c53d2
AC
4008print_fp_register_row (struct ui_file *file, struct frame_info *frame,
4009 int regnum)
f0ef6b29 4010{
e11c53d2
AC
4011 fprintf_filtered (file, " ");
4012 mips_print_fp_register (file, frame, regnum);
4013 fprintf_filtered (file, "\n");
f0ef6b29
KB
4014 return regnum + 1;
4015}
4016
4017
c906108c
SS
4018/* Print a row's worth of GP (int) registers, with name labels above */
4019
4020static int
e11c53d2 4021print_gp_register_row (struct ui_file *file, struct frame_info *frame,
a4b8ebc8 4022 int start_regnum)
c906108c 4023{
a4b8ebc8 4024 struct gdbarch *gdbarch = get_frame_arch (frame);
c906108c 4025 /* do values for GP (int) regs */
d9d9c31f 4026 char raw_buffer[MAX_REGISTER_SIZE];
d5ac5a39 4027 int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8); /* display cols per row */
c906108c 4028 int col, byte;
a4b8ebc8 4029 int regnum;
c906108c
SS
4030
4031 /* For GP registers, we print a separate row of names above the vals */
e11c53d2 4032 fprintf_filtered (file, " ");
a4b8ebc8 4033 for (col = 0, regnum = start_regnum;
6d82d43b 4034 col < ncols && regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
c906108c
SS
4035 {
4036 if (*REGISTER_NAME (regnum) == '\0')
c5aa993b 4037 continue; /* unused register */
6d82d43b
AC
4038 if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) ==
4039 TYPE_CODE_FLT)
c5aa993b 4040 break; /* end the row: reached FP register */
6d82d43b 4041 fprintf_filtered (file,
d5ac5a39 4042 mips_abi_regsize (current_gdbarch) == 8 ? "%17s" : "%9s",
e11c53d2 4043 REGISTER_NAME (regnum));
c906108c
SS
4044 col++;
4045 }
a4b8ebc8 4046 /* print the R0 to R31 names */
20e6603c
AC
4047 if ((start_regnum % NUM_REGS) < MIPS_NUMREGS)
4048 fprintf_filtered (file, "\n R%-4d", start_regnum % NUM_REGS);
4049 else
4050 fprintf_filtered (file, "\n ");
c906108c 4051
c906108c 4052 /* now print the values in hex, 4 or 8 to the row */
a4b8ebc8 4053 for (col = 0, regnum = start_regnum;
6d82d43b 4054 col < ncols && regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
c906108c
SS
4055 {
4056 if (*REGISTER_NAME (regnum) == '\0')
c5aa993b 4057 continue; /* unused register */
6d82d43b
AC
4058 if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) ==
4059 TYPE_CODE_FLT)
c5aa993b 4060 break; /* end row: reached FP register */
c906108c 4061 /* OK: get the data in raw format. */
e11c53d2 4062 if (!frame_register_read (frame, regnum, raw_buffer))
8a3fe4f8 4063 error (_("can't read register %d (%s)"), regnum, REGISTER_NAME (regnum));
c906108c 4064 /* pad small registers */
4246e332 4065 for (byte = 0;
d5ac5a39 4066 byte < (mips_abi_regsize (current_gdbarch)
6d82d43b 4067 - register_size (current_gdbarch, regnum)); byte++)
c906108c
SS
4068 printf_filtered (" ");
4069 /* Now print the register value in hex, endian order. */
d7449b42 4070 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
6d82d43b
AC
4071 for (byte =
4072 register_size (current_gdbarch,
4073 regnum) - register_size (current_gdbarch, regnum);
4074 byte < register_size (current_gdbarch, regnum); byte++)
e11c53d2 4075 fprintf_filtered (file, "%02x", (unsigned char) raw_buffer[byte]);
c906108c 4076 else
c73e8f27 4077 for (byte = register_size (current_gdbarch, regnum) - 1;
6d82d43b 4078 byte >= 0; byte--)
e11c53d2
AC
4079 fprintf_filtered (file, "%02x", (unsigned char) raw_buffer[byte]);
4080 fprintf_filtered (file, " ");
c906108c
SS
4081 col++;
4082 }
c5aa993b 4083 if (col > 0) /* ie. if we actually printed anything... */
e11c53d2 4084 fprintf_filtered (file, "\n");
c906108c
SS
4085
4086 return regnum;
4087}
4088
4089/* MIPS_DO_REGISTERS_INFO(): called by "info register" command */
4090
bf1f5b4c 4091static void
e11c53d2
AC
4092mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
4093 struct frame_info *frame, int regnum, int all)
c906108c 4094{
c5aa993b 4095 if (regnum != -1) /* do one specified register */
c906108c 4096 {
a4b8ebc8 4097 gdb_assert (regnum >= NUM_REGS);
c906108c 4098 if (*(REGISTER_NAME (regnum)) == '\0')
8a3fe4f8 4099 error (_("Not a valid register for the current processor type"));
c906108c 4100
e11c53d2
AC
4101 mips_print_register (file, frame, regnum, 0);
4102 fprintf_filtered (file, "\n");
c906108c 4103 }
c5aa993b
JM
4104 else
4105 /* do all (or most) registers */
c906108c 4106 {
a4b8ebc8
AC
4107 regnum = NUM_REGS;
4108 while (regnum < NUM_REGS + NUM_PSEUDO_REGS)
c906108c 4109 {
6d82d43b
AC
4110 if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) ==
4111 TYPE_CODE_FLT)
e11c53d2
AC
4112 {
4113 if (all) /* true for "INFO ALL-REGISTERS" command */
4114 regnum = print_fp_register_row (file, frame, regnum);
4115 else
4116 regnum += MIPS_NUMREGS; /* skip floating point regs */
4117 }
c906108c 4118 else
e11c53d2 4119 regnum = print_gp_register_row (file, frame, regnum);
c906108c
SS
4120 }
4121 }
4122}
4123
c906108c
SS
4124/* Is this a branch with a delay slot? */
4125
c906108c 4126static int
acdb74a0 4127is_delayed (unsigned long insn)
c906108c
SS
4128{
4129 int i;
4130 for (i = 0; i < NUMOPCODES; ++i)
4131 if (mips_opcodes[i].pinfo != INSN_MACRO
4132 && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
4133 break;
4134 return (i < NUMOPCODES
4135 && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
4136 | INSN_COND_BRANCH_DELAY
4137 | INSN_COND_BRANCH_LIKELY)));
4138}
4139
4140int
3352ef37
AC
4141mips_single_step_through_delay (struct gdbarch *gdbarch,
4142 struct frame_info *frame)
c906108c 4143{
3352ef37 4144 CORE_ADDR pc = get_frame_pc (frame);
95ac2dcf 4145 char buf[MIPS_INSN32_SIZE];
c906108c
SS
4146
4147 /* There is no branch delay slot on MIPS16. */
0fe7e7c8 4148 if (mips_pc_is_mips16 (pc))
c906108c
SS
4149 return 0;
4150
3352ef37
AC
4151 if (!safe_frame_unwind_memory (frame, pc, buf, sizeof buf))
4152 /* If error reading memory, guess that it is not a delayed
4153 branch. */
c906108c 4154 return 0;
4c7d22cb 4155 return is_delayed (extract_unsigned_integer (buf, sizeof buf));
c906108c
SS
4156}
4157
6d82d43b
AC
4158/* To skip prologues, I use this predicate. Returns either PC itself
4159 if the code at PC does not look like a function prologue; otherwise
4160 returns an address that (if we're lucky) follows the prologue. If
4161 LENIENT, then we must skip everything which is involved in setting
4162 up the frame (it's OK to skip more, just so long as we don't skip
4163 anything which might clobber the registers which are being saved.
4164 We must skip more in the case where part of the prologue is in the
4165 delay slot of a non-prologue instruction). */
4166
4167static CORE_ADDR
4168mips_skip_prologue (CORE_ADDR pc)
4169{
8b622e6a
AC
4170 CORE_ADDR limit_pc;
4171 CORE_ADDR func_addr;
4172
6d82d43b
AC
4173 /* See if we can determine the end of the prologue via the symbol table.
4174 If so, then return either PC, or the PC after the prologue, whichever
4175 is greater. */
8b622e6a
AC
4176 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
4177 {
4178 CORE_ADDR post_prologue_pc = skip_prologue_using_sal (func_addr);
4179 if (post_prologue_pc != 0)
4180 return max (pc, post_prologue_pc);
4181 }
6d82d43b
AC
4182
4183 /* Can't determine prologue from the symbol table, need to examine
4184 instructions. */
4185
98b4dd94
JB
4186 /* Find an upper limit on the function prologue using the debug
4187 information. If the debug information could not be used to provide
4188 that bound, then use an arbitrary large number as the upper bound. */
4189 limit_pc = skip_prologue_using_sal (pc);
4190 if (limit_pc == 0)
4191 limit_pc = pc + 100; /* Magic. */
4192
0fe7e7c8 4193 if (mips_pc_is_mips16 (pc))
a65bbe44 4194 return mips16_scan_prologue (pc, limit_pc, NULL, NULL);
6d82d43b 4195 else
a65bbe44 4196 return mips32_scan_prologue (pc, limit_pc, NULL, NULL);
88658117
AC
4197}
4198
a5ea2558
AC
4199/* Root of all "set mips "/"show mips " commands. This will eventually be
4200 used for all MIPS-specific commands. */
4201
a5ea2558 4202static void
acdb74a0 4203show_mips_command (char *args, int from_tty)
a5ea2558
AC
4204{
4205 help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
4206}
4207
a5ea2558 4208static void
acdb74a0 4209set_mips_command (char *args, int from_tty)
a5ea2558 4210{
6d82d43b
AC
4211 printf_unfiltered
4212 ("\"set mips\" must be followed by an appropriate subcommand.\n");
a5ea2558
AC
4213 help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
4214}
4215
c906108c
SS
4216/* Commands to show/set the MIPS FPU type. */
4217
c906108c 4218static void
acdb74a0 4219show_mipsfpu_command (char *args, int from_tty)
c906108c 4220{
c906108c
SS
4221 char *fpu;
4222 switch (MIPS_FPU_TYPE)
4223 {
4224 case MIPS_FPU_SINGLE:
4225 fpu = "single-precision";
4226 break;
4227 case MIPS_FPU_DOUBLE:
4228 fpu = "double-precision";
4229 break;
4230 case MIPS_FPU_NONE:
4231 fpu = "absent (none)";
4232 break;
93d56215 4233 default:
e2e0b3e5 4234 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c
SS
4235 }
4236 if (mips_fpu_type_auto)
6d82d43b
AC
4237 printf_unfiltered
4238 ("The MIPS floating-point coprocessor is set automatically (currently %s)\n",
4239 fpu);
c906108c 4240 else
6d82d43b
AC
4241 printf_unfiltered
4242 ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
c906108c
SS
4243}
4244
4245
c906108c 4246static void
acdb74a0 4247set_mipsfpu_command (char *args, int from_tty)
c906108c 4248{
6d82d43b
AC
4249 printf_unfiltered
4250 ("\"set mipsfpu\" must be followed by \"double\", \"single\",\"none\" or \"auto\".\n");
c906108c
SS
4251 show_mipsfpu_command (args, from_tty);
4252}
4253
c906108c 4254static void
acdb74a0 4255set_mipsfpu_single_command (char *args, int from_tty)
c906108c 4256{
8d5838b5
AC
4257 struct gdbarch_info info;
4258 gdbarch_info_init (&info);
c906108c
SS
4259 mips_fpu_type = MIPS_FPU_SINGLE;
4260 mips_fpu_type_auto = 0;
8d5838b5
AC
4261 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
4262 instead of relying on globals. Doing that would let generic code
4263 handle the search for this specific architecture. */
4264 if (!gdbarch_update_p (info))
e2e0b3e5 4265 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
4266}
4267
c906108c 4268static void
acdb74a0 4269set_mipsfpu_double_command (char *args, int from_tty)
c906108c 4270{
8d5838b5
AC
4271 struct gdbarch_info info;
4272 gdbarch_info_init (&info);
c906108c
SS
4273 mips_fpu_type = MIPS_FPU_DOUBLE;
4274 mips_fpu_type_auto = 0;
8d5838b5
AC
4275 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
4276 instead of relying on globals. Doing that would let generic code
4277 handle the search for this specific architecture. */
4278 if (!gdbarch_update_p (info))
e2e0b3e5 4279 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
4280}
4281
c906108c 4282static void
acdb74a0 4283set_mipsfpu_none_command (char *args, int from_tty)
c906108c 4284{
8d5838b5
AC
4285 struct gdbarch_info info;
4286 gdbarch_info_init (&info);
c906108c
SS
4287 mips_fpu_type = MIPS_FPU_NONE;
4288 mips_fpu_type_auto = 0;
8d5838b5
AC
4289 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
4290 instead of relying on globals. Doing that would let generic code
4291 handle the search for this specific architecture. */
4292 if (!gdbarch_update_p (info))
e2e0b3e5 4293 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
4294}
4295
c906108c 4296static void
acdb74a0 4297set_mipsfpu_auto_command (char *args, int from_tty)
c906108c
SS
4298{
4299 mips_fpu_type_auto = 1;
4300}
4301
c906108c 4302/* Attempt to identify the particular processor model by reading the
691c0433
AC
4303 processor id. NOTE: cagney/2003-11-15: Firstly it isn't clear that
4304 the relevant processor still exists (it dates back to '94) and
4305 secondly this is not the way to do this. The processor type should
4306 be set by forcing an architecture change. */
c906108c 4307
691c0433
AC
4308void
4309deprecated_mips_set_processor_regs_hack (void)
c906108c 4310{
691c0433 4311 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
c906108c
SS
4312 CORE_ADDR prid;
4313
a5c9623c 4314 prid = read_register (MIPS_PRID_REGNUM);
c906108c
SS
4315
4316 if ((prid & ~0xf) == 0x700)
691c0433 4317 tdep->mips_processor_reg_names = mips_r3041_reg_names;
c906108c
SS
4318}
4319
4320/* Just like reinit_frame_cache, but with the right arguments to be
4321 callable as an sfunc. */
4322
4323static void
acdb74a0
AC
4324reinit_frame_cache_sfunc (char *args, int from_tty,
4325 struct cmd_list_element *c)
c906108c
SS
4326{
4327 reinit_frame_cache ();
4328}
4329
a89aa300
AC
4330static int
4331gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
c906108c 4332{
e5ab0dce 4333 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
c906108c 4334
d31431ed
AC
4335 /* FIXME: cagney/2003-06-26: Is this even necessary? The
4336 disassembler needs to be able to locally determine the ISA, and
4337 not rely on GDB. Otherwize the stand-alone 'objdump -d' will not
4338 work. */
ec4045ea
AC
4339 if (mips_pc_is_mips16 (memaddr))
4340 info->mach = bfd_mach_mips16;
c906108c
SS
4341
4342 /* Round down the instruction address to the appropriate boundary. */
65c11066 4343 memaddr &= (info->mach == bfd_mach_mips16 ? ~1 : ~3);
c5aa993b 4344
e5ab0dce 4345 /* Set the disassembler options. */
6d82d43b 4346 if (tdep->mips_abi == MIPS_ABI_N32 || tdep->mips_abi == MIPS_ABI_N64)
e5ab0dce
AC
4347 {
4348 /* Set up the disassembler info, so that we get the right
6d82d43b 4349 register names from libopcodes. */
e5ab0dce
AC
4350 if (tdep->mips_abi == MIPS_ABI_N32)
4351 info->disassembler_options = "gpr-names=n32";
4352 else
4353 info->disassembler_options = "gpr-names=64";
4354 info->flavour = bfd_target_elf_flavour;
4355 }
4356 else
4357 /* This string is not recognized explicitly by the disassembler,
4358 but it tells the disassembler to not try to guess the ABI from
4359 the bfd elf headers, such that, if the user overrides the ABI
4360 of a program linked as NewABI, the disassembly will follow the
4361 register naming conventions specified by the user. */
4362 info->disassembler_options = "gpr-names=32";
4363
c906108c 4364 /* Call the appropriate disassembler based on the target endian-ness. */
d7449b42 4365 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c
SS
4366 return print_insn_big_mips (memaddr, info);
4367 else
4368 return print_insn_little_mips (memaddr, info);
4369}
4370
c906108c
SS
4371/* This function implements the BREAKPOINT_FROM_PC macro. It uses the program
4372 counter value to determine whether a 16- or 32-bit breakpoint should be
4373 used. It returns a pointer to a string of bytes that encode a breakpoint
4374 instruction, stores the length of the string to *lenptr, and adjusts pc
4375 (if necessary) to point to the actual memory location where the
4376 breakpoint should be inserted. */
4377
f7ab6ec6 4378static const unsigned char *
6d82d43b 4379mips_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
c906108c 4380{
d7449b42 4381 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
c906108c 4382 {
0fe7e7c8 4383 if (mips_pc_is_mips16 (*pcptr))
c906108c 4384 {
6d82d43b 4385 static unsigned char mips16_big_breakpoint[] = { 0xe8, 0xa5 };
95404a3e 4386 *pcptr = unmake_mips16_addr (*pcptr);
c5aa993b 4387 *lenptr = sizeof (mips16_big_breakpoint);
c906108c
SS
4388 return mips16_big_breakpoint;
4389 }
4390 else
4391 {
aaab4dba
AC
4392 /* The IDT board uses an unusual breakpoint value, and
4393 sometimes gets confused when it sees the usual MIPS
4394 breakpoint instruction. */
6d82d43b
AC
4395 static unsigned char big_breakpoint[] = { 0, 0x5, 0, 0xd };
4396 static unsigned char pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
4397 static unsigned char idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
c906108c 4398
c5aa993b 4399 *lenptr = sizeof (big_breakpoint);
c906108c
SS
4400
4401 if (strcmp (target_shortname, "mips") == 0)
4402 return idt_big_breakpoint;
4403 else if (strcmp (target_shortname, "ddb") == 0
4404 || strcmp (target_shortname, "pmon") == 0
4405 || strcmp (target_shortname, "lsi") == 0)
4406 return pmon_big_breakpoint;
4407 else
4408 return big_breakpoint;
4409 }
4410 }
4411 else
4412 {
0fe7e7c8 4413 if (mips_pc_is_mips16 (*pcptr))
c906108c 4414 {
6d82d43b 4415 static unsigned char mips16_little_breakpoint[] = { 0xa5, 0xe8 };
95404a3e 4416 *pcptr = unmake_mips16_addr (*pcptr);
c5aa993b 4417 *lenptr = sizeof (mips16_little_breakpoint);
c906108c
SS
4418 return mips16_little_breakpoint;
4419 }
4420 else
4421 {
6d82d43b
AC
4422 static unsigned char little_breakpoint[] = { 0xd, 0, 0x5, 0 };
4423 static unsigned char pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
4424 static unsigned char idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };
c906108c 4425
c5aa993b 4426 *lenptr = sizeof (little_breakpoint);
c906108c
SS
4427
4428 if (strcmp (target_shortname, "mips") == 0)
4429 return idt_little_breakpoint;
4430 else if (strcmp (target_shortname, "ddb") == 0
4431 || strcmp (target_shortname, "pmon") == 0
4432 || strcmp (target_shortname, "lsi") == 0)
4433 return pmon_little_breakpoint;
4434 else
4435 return little_breakpoint;
4436 }
4437 }
4438}
4439
4440/* If PC is in a mips16 call or return stub, return the address of the target
4441 PC, which is either the callee or the caller. There are several
4442 cases which must be handled:
4443
4444 * If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
c5aa993b 4445 target PC is in $31 ($ra).
c906108c 4446 * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
c5aa993b 4447 and the target PC is in $2.
c906108c 4448 * If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
c5aa993b
JM
4449 before the jal instruction, this is effectively a call stub
4450 and the the target PC is in $2. Otherwise this is effectively
4451 a return stub and the target PC is in $18.
c906108c
SS
4452
4453 See the source code for the stubs in gcc/config/mips/mips16.S for
e7d6a6d2 4454 gory details. */
c906108c 4455
757a7cc6 4456static CORE_ADDR
e7d6a6d2 4457mips_skip_trampoline_code (CORE_ADDR pc)
c906108c
SS
4458{
4459 char *name;
4460 CORE_ADDR start_addr;
4461
4462 /* Find the starting address and name of the function containing the PC. */
4463 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
4464 return 0;
4465
4466 /* If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
4467 target PC is in $31 ($ra). */
4468 if (strcmp (name, "__mips16_ret_sf") == 0
4469 || strcmp (name, "__mips16_ret_df") == 0)
4c7d22cb 4470 return read_signed_register (MIPS_RA_REGNUM);
c906108c
SS
4471
4472 if (strncmp (name, "__mips16_call_stub_", 19) == 0)
4473 {
4474 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
4475 and the target PC is in $2. */
4476 if (name[19] >= '0' && name[19] <= '9')
6c997a34 4477 return read_signed_register (2);
c906108c
SS
4478
4479 /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
c5aa993b
JM
4480 before the jal instruction, this is effectively a call stub
4481 and the the target PC is in $2. Otherwise this is effectively
4482 a return stub and the target PC is in $18. */
c906108c
SS
4483 else if (name[19] == 's' || name[19] == 'd')
4484 {
4485 if (pc == start_addr)
4486 {
4487 /* Check if the target of the stub is a compiler-generated
c5aa993b
JM
4488 stub. Such a stub for a function bar might have a name
4489 like __fn_stub_bar, and might look like this:
4490 mfc1 $4,$f13
4491 mfc1 $5,$f12
4492 mfc1 $6,$f15
4493 mfc1 $7,$f14
4494 la $1,bar (becomes a lui/addiu pair)
4495 jr $1
4496 So scan down to the lui/addi and extract the target
4497 address from those two instructions. */
c906108c 4498
6c997a34 4499 CORE_ADDR target_pc = read_signed_register (2);
d37cca3d 4500 ULONGEST inst;
c906108c
SS
4501 int i;
4502
4503 /* See if the name of the target function is __fn_stub_*. */
6d82d43b
AC
4504 if (find_pc_partial_function (target_pc, &name, NULL, NULL) ==
4505 0)
c906108c
SS
4506 return target_pc;
4507 if (strncmp (name, "__fn_stub_", 10) != 0
4508 && strcmp (name, "etext") != 0
4509 && strcmp (name, "_etext") != 0)
4510 return target_pc;
4511
4512 /* Scan through this _fn_stub_ code for the lui/addiu pair.
c5aa993b
JM
4513 The limit on the search is arbitrarily set to 20
4514 instructions. FIXME. */
95ac2dcf 4515 for (i = 0, pc = 0; i < 20; i++, target_pc += MIPS_INSN32_SIZE)
c906108c 4516 {
c5aa993b
JM
4517 inst = mips_fetch_instruction (target_pc);
4518 if ((inst & 0xffff0000) == 0x3c010000) /* lui $at */
4519 pc = (inst << 16) & 0xffff0000; /* high word */
4520 else if ((inst & 0xffff0000) == 0x24210000) /* addiu $at */
4521 return pc | (inst & 0xffff); /* low word */
c906108c
SS
4522 }
4523
4524 /* Couldn't find the lui/addui pair, so return stub address. */
4525 return target_pc;
4526 }
4527 else
4528 /* This is the 'return' part of a call stub. The return
4529 address is in $r18. */
6c997a34 4530 return read_signed_register (18);
c906108c
SS
4531 }
4532 }
c5aa993b 4533 return 0; /* not a stub */
c906108c
SS
4534}
4535
a4b8ebc8
AC
4536/* Convert a dbx stab register number (from `r' declaration) to a GDB
4537 [1 * NUM_REGS .. 2 * NUM_REGS) REGNUM. */
88c72b7d
AC
4538
4539static int
4540mips_stab_reg_to_regnum (int num)
4541{
a4b8ebc8 4542 int regnum;
2f38ef89 4543 if (num >= 0 && num < 32)
a4b8ebc8 4544 regnum = num;
2f38ef89 4545 else if (num >= 38 && num < 70)
56cea623 4546 regnum = num + mips_regnum (current_gdbarch)->fp0 - 38;
040b99fd 4547 else if (num == 70)
56cea623 4548 regnum = mips_regnum (current_gdbarch)->hi;
040b99fd 4549 else if (num == 71)
56cea623 4550 regnum = mips_regnum (current_gdbarch)->lo;
2f38ef89 4551 else
a4b8ebc8
AC
4552 /* This will hopefully (eventually) provoke a warning. Should
4553 we be calling complaint() here? */
4554 return NUM_REGS + NUM_PSEUDO_REGS;
4555 return NUM_REGS + regnum;
88c72b7d
AC
4556}
4557
2f38ef89 4558
a4b8ebc8
AC
4559/* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
4560 NUM_REGS .. 2 * NUM_REGS) REGNUM. */
88c72b7d
AC
4561
4562static int
2f38ef89 4563mips_dwarf_dwarf2_ecoff_reg_to_regnum (int num)
88c72b7d 4564{
a4b8ebc8 4565 int regnum;
2f38ef89 4566 if (num >= 0 && num < 32)
a4b8ebc8 4567 regnum = num;
2f38ef89 4568 else if (num >= 32 && num < 64)
56cea623 4569 regnum = num + mips_regnum (current_gdbarch)->fp0 - 32;
040b99fd 4570 else if (num == 64)
56cea623 4571 regnum = mips_regnum (current_gdbarch)->hi;
040b99fd 4572 else if (num == 65)
56cea623 4573 regnum = mips_regnum (current_gdbarch)->lo;
2f38ef89 4574 else
a4b8ebc8
AC
4575 /* This will hopefully (eventually) provoke a warning. Should we
4576 be calling complaint() here? */
4577 return NUM_REGS + NUM_PSEUDO_REGS;
4578 return NUM_REGS + regnum;
4579}
4580
4581static int
4582mips_register_sim_regno (int regnum)
4583{
4584 /* Only makes sense to supply raw registers. */
4585 gdb_assert (regnum >= 0 && regnum < NUM_REGS);
4586 /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
4587 decide if it is valid. Should instead define a standard sim/gdb
4588 register numbering scheme. */
4589 if (REGISTER_NAME (NUM_REGS + regnum) != NULL
4590 && REGISTER_NAME (NUM_REGS + regnum)[0] != '\0')
4591 return regnum;
4592 else
6d82d43b 4593 return LEGACY_SIM_REGNO_IGNORE;
88c72b7d
AC
4594}
4595
2f38ef89 4596
fc0c74b1
AC
4597/* Convert an integer into an address. By first converting the value
4598 into a pointer and then extracting it signed, the address is
4599 guarenteed to be correctly sign extended. */
4600
4601static CORE_ADDR
79dd2d24
AC
4602mips_integer_to_address (struct gdbarch *gdbarch,
4603 struct type *type, const bfd_byte *buf)
fc0c74b1
AC
4604{
4605 char *tmp = alloca (TYPE_LENGTH (builtin_type_void_data_ptr));
4606 LONGEST val = unpack_long (type, buf);
4607 store_signed_integer (tmp, TYPE_LENGTH (builtin_type_void_data_ptr), val);
4608 return extract_signed_integer (tmp,
4609 TYPE_LENGTH (builtin_type_void_data_ptr));
4610}
4611
caaa3122
DJ
4612static void
4613mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
4614{
4615 enum mips_abi *abip = (enum mips_abi *) obj;
4616 const char *name = bfd_get_section_name (abfd, sect);
4617
4618 if (*abip != MIPS_ABI_UNKNOWN)
4619 return;
4620
4621 if (strncmp (name, ".mdebug.", 8) != 0)
4622 return;
4623
4624 if (strcmp (name, ".mdebug.abi32") == 0)
4625 *abip = MIPS_ABI_O32;
4626 else if (strcmp (name, ".mdebug.abiN32") == 0)
4627 *abip = MIPS_ABI_N32;
62a49b2c 4628 else if (strcmp (name, ".mdebug.abi64") == 0)
e3bddbfa 4629 *abip = MIPS_ABI_N64;
caaa3122
DJ
4630 else if (strcmp (name, ".mdebug.abiO64") == 0)
4631 *abip = MIPS_ABI_O64;
4632 else if (strcmp (name, ".mdebug.eabi32") == 0)
4633 *abip = MIPS_ABI_EABI32;
4634 else if (strcmp (name, ".mdebug.eabi64") == 0)
4635 *abip = MIPS_ABI_EABI64;
4636 else
8a3fe4f8 4637 warning (_("unsupported ABI %s."), name + 8);
caaa3122
DJ
4638}
4639
2e4ebe70
DJ
4640static enum mips_abi
4641global_mips_abi (void)
4642{
4643 int i;
4644
4645 for (i = 0; mips_abi_strings[i] != NULL; i++)
4646 if (mips_abi_strings[i] == mips_abi_string)
4647 return (enum mips_abi) i;
4648
e2e0b3e5 4649 internal_error (__FILE__, __LINE__, _("unknown ABI string"));
2e4ebe70
DJ
4650}
4651
c2d11a7d 4652static struct gdbarch *
6d82d43b 4653mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
c2d11a7d 4654{
c2d11a7d
JM
4655 struct gdbarch *gdbarch;
4656 struct gdbarch_tdep *tdep;
4657 int elf_flags;
2e4ebe70 4658 enum mips_abi mips_abi, found_abi, wanted_abi;
a4b8ebc8 4659 int num_regs;
8d5838b5 4660 enum mips_fpu_type fpu_type;
c2d11a7d 4661
ec03c1ac
AC
4662 /* First of all, extract the elf_flags, if available. */
4663 if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
4664 elf_flags = elf_elfheader (info.abfd)->e_flags;
6214a8a1
AC
4665 else if (arches != NULL)
4666 elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
ec03c1ac
AC
4667 else
4668 elf_flags = 0;
4669 if (gdbarch_debug)
4670 fprintf_unfiltered (gdb_stdlog,
6d82d43b 4671 "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
c2d11a7d 4672
102182a9 4673 /* Check ELF_FLAGS to see if it specifies the ABI being used. */
0dadbba0
AC
4674 switch ((elf_flags & EF_MIPS_ABI))
4675 {
4676 case E_MIPS_ABI_O32:
ec03c1ac 4677 found_abi = MIPS_ABI_O32;
0dadbba0
AC
4678 break;
4679 case E_MIPS_ABI_O64:
ec03c1ac 4680 found_abi = MIPS_ABI_O64;
0dadbba0
AC
4681 break;
4682 case E_MIPS_ABI_EABI32:
ec03c1ac 4683 found_abi = MIPS_ABI_EABI32;
0dadbba0
AC
4684 break;
4685 case E_MIPS_ABI_EABI64:
ec03c1ac 4686 found_abi = MIPS_ABI_EABI64;
0dadbba0
AC
4687 break;
4688 default:
acdb74a0 4689 if ((elf_flags & EF_MIPS_ABI2))
ec03c1ac 4690 found_abi = MIPS_ABI_N32;
acdb74a0 4691 else
ec03c1ac 4692 found_abi = MIPS_ABI_UNKNOWN;
0dadbba0
AC
4693 break;
4694 }
acdb74a0 4695
caaa3122 4696 /* GCC creates a pseudo-section whose name describes the ABI. */
ec03c1ac
AC
4697 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
4698 bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
caaa3122 4699
dc305454 4700 /* If we have no useful BFD information, use the ABI from the last
ec03c1ac
AC
4701 MIPS architecture (if there is one). */
4702 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
4703 found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
2e4ebe70 4704
32a6503c 4705 /* Try the architecture for any hint of the correct ABI. */
ec03c1ac 4706 if (found_abi == MIPS_ABI_UNKNOWN
bf64bfd6
AC
4707 && info.bfd_arch_info != NULL
4708 && info.bfd_arch_info->arch == bfd_arch_mips)
4709 {
4710 switch (info.bfd_arch_info->mach)
4711 {
4712 case bfd_mach_mips3900:
ec03c1ac 4713 found_abi = MIPS_ABI_EABI32;
bf64bfd6
AC
4714 break;
4715 case bfd_mach_mips4100:
4716 case bfd_mach_mips5000:
ec03c1ac 4717 found_abi = MIPS_ABI_EABI64;
bf64bfd6 4718 break;
1d06468c
EZ
4719 case bfd_mach_mips8000:
4720 case bfd_mach_mips10000:
32a6503c
KB
4721 /* On Irix, ELF64 executables use the N64 ABI. The
4722 pseudo-sections which describe the ABI aren't present
4723 on IRIX. (Even for executables created by gcc.) */
28d169de
KB
4724 if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
4725 && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
ec03c1ac 4726 found_abi = MIPS_ABI_N64;
28d169de 4727 else
ec03c1ac 4728 found_abi = MIPS_ABI_N32;
1d06468c 4729 break;
bf64bfd6
AC
4730 }
4731 }
2e4ebe70 4732
ec03c1ac
AC
4733 if (gdbarch_debug)
4734 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
4735 found_abi);
4736
4737 /* What has the user specified from the command line? */
4738 wanted_abi = global_mips_abi ();
4739 if (gdbarch_debug)
4740 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
4741 wanted_abi);
2e4ebe70
DJ
4742
4743 /* Now that we have found what the ABI for this binary would be,
4744 check whether the user is overriding it. */
2e4ebe70
DJ
4745 if (wanted_abi != MIPS_ABI_UNKNOWN)
4746 mips_abi = wanted_abi;
ec03c1ac
AC
4747 else if (found_abi != MIPS_ABI_UNKNOWN)
4748 mips_abi = found_abi;
4749 else
4750 mips_abi = MIPS_ABI_O32;
4751 if (gdbarch_debug)
4752 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
4753 mips_abi);
2e4ebe70 4754
ec03c1ac 4755 /* Also used when doing an architecture lookup. */
4b9b3959 4756 if (gdbarch_debug)
ec03c1ac
AC
4757 fprintf_unfiltered (gdb_stdlog,
4758 "mips_gdbarch_init: mips64_transfers_32bit_regs_p = %d\n",
4759 mips64_transfers_32bit_regs_p);
0dadbba0 4760
8d5838b5
AC
4761 /* Determine the MIPS FPU type. */
4762 if (!mips_fpu_type_auto)
4763 fpu_type = mips_fpu_type;
4764 else if (info.bfd_arch_info != NULL
4765 && info.bfd_arch_info->arch == bfd_arch_mips)
4766 switch (info.bfd_arch_info->mach)
4767 {
4768 case bfd_mach_mips3900:
4769 case bfd_mach_mips4100:
4770 case bfd_mach_mips4111:
a9d61c86 4771 case bfd_mach_mips4120:
8d5838b5
AC
4772 fpu_type = MIPS_FPU_NONE;
4773 break;
4774 case bfd_mach_mips4650:
4775 fpu_type = MIPS_FPU_SINGLE;
4776 break;
4777 default:
4778 fpu_type = MIPS_FPU_DOUBLE;
4779 break;
4780 }
4781 else if (arches != NULL)
4782 fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
4783 else
4784 fpu_type = MIPS_FPU_DOUBLE;
4785 if (gdbarch_debug)
4786 fprintf_unfiltered (gdb_stdlog,
6d82d43b 4787 "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
8d5838b5 4788
c2d11a7d
JM
4789 /* try to find a pre-existing architecture */
4790 for (arches = gdbarch_list_lookup_by_info (arches, &info);
4791 arches != NULL;
4792 arches = gdbarch_list_lookup_by_info (arches->next, &info))
4793 {
4794 /* MIPS needs to be pedantic about which ABI the object is
102182a9 4795 using. */
9103eae0 4796 if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
c2d11a7d 4797 continue;
9103eae0 4798 if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
0dadbba0 4799 continue;
719ec221
AC
4800 /* Need to be pedantic about which register virtual size is
4801 used. */
4802 if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
4803 != mips64_transfers_32bit_regs_p)
4804 continue;
8d5838b5
AC
4805 /* Be pedantic about which FPU is selected. */
4806 if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
4807 continue;
4be87837 4808 return arches->gdbarch;
c2d11a7d
JM
4809 }
4810
102182a9 4811 /* Need a new architecture. Fill in a target specific vector. */
c2d11a7d
JM
4812 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
4813 gdbarch = gdbarch_alloc (&info, tdep);
4814 tdep->elf_flags = elf_flags;
719ec221 4815 tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
ec03c1ac
AC
4816 tdep->found_abi = found_abi;
4817 tdep->mips_abi = mips_abi;
8d5838b5 4818 tdep->mips_fpu_type = fpu_type;
c2d11a7d 4819
102182a9 4820 /* Initially set everything according to the default ABI/ISA. */
c2d11a7d
JM
4821 set_gdbarch_short_bit (gdbarch, 16);
4822 set_gdbarch_int_bit (gdbarch, 32);
4823 set_gdbarch_float_bit (gdbarch, 32);
4824 set_gdbarch_double_bit (gdbarch, 64);
4825 set_gdbarch_long_double_bit (gdbarch, 64);
a4b8ebc8
AC
4826 set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
4827 set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
4828 set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
1d06468c 4829
6d82d43b 4830 set_gdbarch_elf_make_msymbol_special (gdbarch,
f7ab6ec6
MS
4831 mips_elf_make_msymbol_special);
4832
16e109ca 4833 /* Fill in the OS dependant register numbers and names. */
56cea623 4834 {
16e109ca 4835 const char **reg_names;
56cea623
AC
4836 struct mips_regnum *regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch,
4837 struct mips_regnum);
56cea623
AC
4838 if (info.osabi == GDB_OSABI_IRIX)
4839 {
4840 regnum->fp0 = 32;
4841 regnum->pc = 64;
4842 regnum->cause = 65;
4843 regnum->badvaddr = 66;
4844 regnum->hi = 67;
4845 regnum->lo = 68;
4846 regnum->fp_control_status = 69;
4847 regnum->fp_implementation_revision = 70;
4848 num_regs = 71;
16e109ca 4849 reg_names = mips_irix_reg_names;
56cea623
AC
4850 }
4851 else
4852 {
4853 regnum->lo = MIPS_EMBED_LO_REGNUM;
4854 regnum->hi = MIPS_EMBED_HI_REGNUM;
4855 regnum->badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
4856 regnum->cause = MIPS_EMBED_CAUSE_REGNUM;
4857 regnum->pc = MIPS_EMBED_PC_REGNUM;
4858 regnum->fp0 = MIPS_EMBED_FP0_REGNUM;
4859 regnum->fp_control_status = 70;
4860 regnum->fp_implementation_revision = 71;
4861 num_regs = 90;
16e109ca
AC
4862 if (info.bfd_arch_info != NULL
4863 && info.bfd_arch_info->mach == bfd_mach_mips3900)
4864 reg_names = mips_tx39_reg_names;
4865 else
4866 reg_names = mips_generic_reg_names;
56cea623
AC
4867 }
4868 /* FIXME: cagney/2003-11-15: For MIPS, hasn't PC_REGNUM been
4869 replaced by read_pc? */
f10683bb
MH
4870 set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
4871 set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
56cea623
AC
4872 set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
4873 set_gdbarch_num_regs (gdbarch, num_regs);
4874 set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
16e109ca
AC
4875 set_gdbarch_register_name (gdbarch, mips_register_name);
4876 tdep->mips_processor_reg_names = reg_names;
4877 tdep->regnum = regnum;
56cea623 4878 }
fe29b929 4879
0dadbba0 4880 switch (mips_abi)
c2d11a7d 4881 {
0dadbba0 4882 case MIPS_ABI_O32:
25ab4790 4883 set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
29dfb2ac 4884 set_gdbarch_return_value (gdbarch, mips_o32_return_value);
4c7d22cb 4885 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
56cea623 4886 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
4014092b 4887 tdep->default_mask_address_p = 0;
c2d11a7d
JM
4888 set_gdbarch_long_bit (gdbarch, 32);
4889 set_gdbarch_ptr_bit (gdbarch, 32);
4890 set_gdbarch_long_long_bit (gdbarch, 64);
4891 break;
0dadbba0 4892 case MIPS_ABI_O64:
25ab4790 4893 set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
9c8fdbfa 4894 set_gdbarch_return_value (gdbarch, mips_o64_return_value);
4c7d22cb 4895 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
56cea623 4896 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
361d1df0 4897 tdep->default_mask_address_p = 0;
c2d11a7d
JM
4898 set_gdbarch_long_bit (gdbarch, 32);
4899 set_gdbarch_ptr_bit (gdbarch, 32);
4900 set_gdbarch_long_long_bit (gdbarch, 64);
4901 break;
0dadbba0 4902 case MIPS_ABI_EABI32:
25ab4790 4903 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
9c8fdbfa 4904 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
4c7d22cb 4905 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 4906 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 4907 tdep->default_mask_address_p = 0;
c2d11a7d
JM
4908 set_gdbarch_long_bit (gdbarch, 32);
4909 set_gdbarch_ptr_bit (gdbarch, 32);
4910 set_gdbarch_long_long_bit (gdbarch, 64);
4911 break;
0dadbba0 4912 case MIPS_ABI_EABI64:
25ab4790 4913 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
9c8fdbfa 4914 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
4c7d22cb 4915 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 4916 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 4917 tdep->default_mask_address_p = 0;
c2d11a7d
JM
4918 set_gdbarch_long_bit (gdbarch, 64);
4919 set_gdbarch_ptr_bit (gdbarch, 64);
4920 set_gdbarch_long_long_bit (gdbarch, 64);
4921 break;
0dadbba0 4922 case MIPS_ABI_N32:
25ab4790 4923 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
29dfb2ac 4924 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
4c7d22cb 4925 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 4926 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 4927 tdep->default_mask_address_p = 0;
0dadbba0
AC
4928 set_gdbarch_long_bit (gdbarch, 32);
4929 set_gdbarch_ptr_bit (gdbarch, 32);
4930 set_gdbarch_long_long_bit (gdbarch, 64);
fed7ba43
JB
4931 set_gdbarch_long_double_bit (gdbarch, 128);
4932 set_gdbarch_long_double_format (gdbarch,
4933 &floatformat_n32n64_long_double_big);
28d169de
KB
4934 break;
4935 case MIPS_ABI_N64:
25ab4790 4936 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
29dfb2ac 4937 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
4c7d22cb 4938 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 4939 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
28d169de
KB
4940 tdep->default_mask_address_p = 0;
4941 set_gdbarch_long_bit (gdbarch, 64);
4942 set_gdbarch_ptr_bit (gdbarch, 64);
4943 set_gdbarch_long_long_bit (gdbarch, 64);
fed7ba43
JB
4944 set_gdbarch_long_double_bit (gdbarch, 128);
4945 set_gdbarch_long_double_format (gdbarch,
4946 &floatformat_n32n64_long_double_big);
0dadbba0 4947 break;
c2d11a7d 4948 default:
e2e0b3e5 4949 internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
c2d11a7d
JM
4950 }
4951
a5ea2558
AC
4952 /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
4953 that could indicate -gp32 BUT gas/config/tc-mips.c contains the
4954 comment:
4955
4956 ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
4957 flag in object files because to do so would make it impossible to
102182a9 4958 link with libraries compiled without "-gp32". This is
a5ea2558 4959 unnecessarily restrictive.
361d1df0 4960
a5ea2558
AC
4961 We could solve this problem by adding "-gp32" multilibs to gcc,
4962 but to set this flag before gcc is built with such multilibs will
4963 break too many systems.''
4964
4965 But even more unhelpfully, the default linker output target for
4966 mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
4967 for 64-bit programs - you need to change the ABI to change this,
102182a9 4968 and not all gcc targets support that currently. Therefore using
a5ea2558
AC
4969 this flag to detect 32-bit mode would do the wrong thing given
4970 the current gcc - it would make GDB treat these 64-bit programs
102182a9 4971 as 32-bit programs by default. */
a5ea2558 4972
6c997a34 4973 set_gdbarch_read_pc (gdbarch, mips_read_pc);
b6cb9035 4974 set_gdbarch_write_pc (gdbarch, mips_write_pc);
bcb0cc15 4975 set_gdbarch_read_sp (gdbarch, mips_read_sp);
c2d11a7d 4976
102182a9
MS
4977 /* Add/remove bits from an address. The MIPS needs be careful to
4978 ensure that all 32 bit addresses are sign extended to 64 bits. */
875e1767
AC
4979 set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
4980
58dfe9ff
AC
4981 /* Unwind the frame. */
4982 set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
edfae063 4983 set_gdbarch_unwind_dummy_id (gdbarch, mips_unwind_dummy_id);
10312cc4 4984
102182a9 4985 /* Map debug register numbers onto internal register numbers. */
88c72b7d 4986 set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
6d82d43b
AC
4987 set_gdbarch_ecoff_reg_to_regnum (gdbarch,
4988 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
4989 set_gdbarch_dwarf_reg_to_regnum (gdbarch,
4990 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
4991 set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
4992 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
a4b8ebc8 4993 set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
88c72b7d 4994
c2d11a7d
JM
4995 /* MIPS version of CALL_DUMMY */
4996
9710e734
AC
4997 /* NOTE: cagney/2003-08-05: Eventually call dummy location will be
4998 replaced by a command, and all targets will default to on stack
4999 (regardless of the stack's execute status). */
5000 set_gdbarch_call_dummy_location (gdbarch, AT_SYMBOL);
dc604539 5001 set_gdbarch_frame_align (gdbarch, mips_frame_align);
d05285fa 5002
87783b8b
AC
5003 set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
5004 set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
5005 set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
5006
f7b9e9fc
AC
5007 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
5008 set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
f7b9e9fc
AC
5009
5010 set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
f7b9e9fc 5011
fc0c74b1
AC
5012 set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
5013 set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
5014 set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
70f80edf 5015
a4b8ebc8 5016 set_gdbarch_register_type (gdbarch, mips_register_type);
78fde5f8 5017
e11c53d2 5018 set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
bf1f5b4c 5019
e5ab0dce
AC
5020 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
5021
3a3bc038
AC
5022 /* FIXME: cagney/2003-08-29: The macros HAVE_STEPPABLE_WATCHPOINT,
5023 HAVE_NONSTEPPABLE_WATCHPOINT, and HAVE_CONTINUABLE_WATCHPOINT
5024 need to all be folded into the target vector. Since they are
5025 being used as guards for STOPPED_BY_WATCHPOINT, why not have
5026 STOPPED_BY_WATCHPOINT return the type of watchpoint that the code
5027 is sitting on? */
5028 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
5029
e7d6a6d2 5030 set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
757a7cc6 5031
3352ef37
AC
5032 set_gdbarch_single_step_through_delay (gdbarch, mips_single_step_through_delay);
5033
6de918a6
DJ
5034 /* Hook in OS ABI-specific overrides, if they have been registered. */
5035 gdbarch_init_osabi (info, gdbarch);
757a7cc6 5036
5792a79b 5037 /* Unwind the frame. */
eec63939 5038 frame_unwind_append_sniffer (gdbarch, mips_stub_frame_sniffer);
45c9dd44
AC
5039 frame_unwind_append_sniffer (gdbarch, mips_insn16_frame_sniffer);
5040 frame_unwind_append_sniffer (gdbarch, mips_insn32_frame_sniffer);
eec63939 5041 frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
45c9dd44
AC
5042 frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
5043 frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
5792a79b 5044
4b9b3959
AC
5045 return gdbarch;
5046}
5047
2e4ebe70 5048static void
6d82d43b 5049mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
2e4ebe70
DJ
5050{
5051 struct gdbarch_info info;
5052
5053 /* Force the architecture to update, and (if it's a MIPS architecture)
5054 mips_gdbarch_init will take care of the rest. */
5055 gdbarch_info_init (&info);
5056 gdbarch_update_p (info);
5057}
5058
ad188201
KB
5059/* Print out which MIPS ABI is in use. */
5060
5061static void
5062show_mips_abi (char *ignore_args, int from_tty)
5063{
5064 if (gdbarch_bfd_arch_info (current_gdbarch)->arch != bfd_arch_mips)
6d82d43b
AC
5065 printf_filtered
5066 ("The MIPS ABI is unknown because the current architecture is not MIPS.\n");
ad188201
KB
5067 else
5068 {
5069 enum mips_abi global_abi = global_mips_abi ();
5070 enum mips_abi actual_abi = mips_abi (current_gdbarch);
5071 const char *actual_abi_str = mips_abi_strings[actual_abi];
5072
5073 if (global_abi == MIPS_ABI_UNKNOWN)
6d82d43b
AC
5074 printf_filtered
5075 ("The MIPS ABI is set automatically (currently \"%s\").\n",
5076 actual_abi_str);
ad188201 5077 else if (global_abi == actual_abi)
6d82d43b
AC
5078 printf_filtered
5079 ("The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
5080 actual_abi_str);
ad188201
KB
5081 else
5082 {
5083 /* Probably shouldn't happen... */
6d82d43b
AC
5084 printf_filtered
5085 ("The (auto detected) MIPS ABI \"%s\" is in use even though the user setting was \"%s\".\n",
5086 actual_abi_str, mips_abi_strings[global_abi]);
ad188201
KB
5087 }
5088 }
5089}
5090
4b9b3959
AC
5091static void
5092mips_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
5093{
5094 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
5095 if (tdep != NULL)
c2d11a7d 5096 {
acdb74a0
AC
5097 int ef_mips_arch;
5098 int ef_mips_32bitmode;
5099 /* determine the ISA */
5100 switch (tdep->elf_flags & EF_MIPS_ARCH)
5101 {
5102 case E_MIPS_ARCH_1:
5103 ef_mips_arch = 1;
5104 break;
5105 case E_MIPS_ARCH_2:
5106 ef_mips_arch = 2;
5107 break;
5108 case E_MIPS_ARCH_3:
5109 ef_mips_arch = 3;
5110 break;
5111 case E_MIPS_ARCH_4:
93d56215 5112 ef_mips_arch = 4;
acdb74a0
AC
5113 break;
5114 default:
93d56215 5115 ef_mips_arch = 0;
acdb74a0
AC
5116 break;
5117 }
5118 /* determine the size of a pointer */
5119 ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
4b9b3959
AC
5120 fprintf_unfiltered (file,
5121 "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
0dadbba0 5122 tdep->elf_flags);
4b9b3959 5123 fprintf_unfiltered (file,
acdb74a0
AC
5124 "mips_dump_tdep: ef_mips_32bitmode = %d\n",
5125 ef_mips_32bitmode);
5126 fprintf_unfiltered (file,
5127 "mips_dump_tdep: ef_mips_arch = %d\n",
5128 ef_mips_arch);
5129 fprintf_unfiltered (file,
5130 "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
6d82d43b 5131 tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
4014092b
AC
5132 fprintf_unfiltered (file,
5133 "mips_dump_tdep: mips_mask_address_p() %d (default %d)\n",
480d3dd2 5134 mips_mask_address_p (tdep),
4014092b 5135 tdep->default_mask_address_p);
c2d11a7d 5136 }
4b9b3959
AC
5137 fprintf_unfiltered (file,
5138 "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
5139 MIPS_DEFAULT_FPU_TYPE,
5140 (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
5141 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
5142 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
5143 : "???"));
6d82d43b 5144 fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n", MIPS_EABI);
4b9b3959
AC
5145 fprintf_unfiltered (file,
5146 "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
5147 MIPS_FPU_TYPE,
5148 (MIPS_FPU_TYPE == MIPS_FPU_NONE ? "none"
5149 : MIPS_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
5150 : MIPS_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
5151 : "???"));
4b9b3959 5152 fprintf_unfiltered (file,
480d3dd2 5153 "mips_dump_tdep: mips_stack_argsize() = %d\n",
13326b4e 5154 mips_stack_argsize (current_gdbarch));
c2d11a7d
JM
5155}
5156
6d82d43b 5157extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
a78f21af 5158
c906108c 5159void
acdb74a0 5160_initialize_mips_tdep (void)
c906108c
SS
5161{
5162 static struct cmd_list_element *mipsfpulist = NULL;
5163 struct cmd_list_element *c;
5164
6d82d43b 5165 mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
2e4ebe70
DJ
5166 if (MIPS_ABI_LAST + 1
5167 != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
e2e0b3e5 5168 internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
2e4ebe70 5169
4b9b3959 5170 gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
c906108c 5171
8d5f9dcb
DJ
5172 mips_pdr_data = register_objfile_data ();
5173
a5ea2558
AC
5174 /* Add root prefix command for all "set mips"/"show mips" commands */
5175 add_prefix_cmd ("mips", no_class, set_mips_command,
1bedd215 5176 _("Various MIPS specific commands."),
a5ea2558
AC
5177 &setmipscmdlist, "set mips ", 0, &setlist);
5178
5179 add_prefix_cmd ("mips", no_class, show_mips_command,
1bedd215 5180 _("Various MIPS specific commands."),
a5ea2558
AC
5181 &showmipscmdlist, "show mips ", 0, &showlist);
5182
5183 /* Allow the user to override the saved register size. */
1b295c3d 5184 add_setshow_enum_cmd ("saved-gpreg-size", class_obscure,
7915a72c
AC
5185 size_enums, &mips_abi_regsize_string, _("\
5186Set size of general purpose registers saved on the stack."), _("\
5187Show size of general purpose registers saved on the stack."), _("\
a5ea2558
AC
5188This option can be set to one of:\n\
5189 32 - Force GDB to treat saved GP registers as 32-bit\n\
5190 64 - Force GDB to treat saved GP registers as 64-bit\n\
5191 auto - Allow GDB to use the target's default setting or autodetect the\n\
7915a72c
AC
5192 saved GP register size from information contained in the\n\
5193 executable (default)."),
2c5b56ce 5194 NULL,
7915a72c 5195 NULL, /* FIXME: i18n: Size of general purpose registers saved on the stack is %s. */
2c5b56ce 5196 &setmipscmdlist, &showmipscmdlist);
a5ea2558 5197
d929b26f 5198 /* Allow the user to override the argument stack size. */
1b295c3d 5199 add_setshow_enum_cmd ("stack-arg-size", class_obscure,
7915a72c
AC
5200 size_enums, &mips_stack_argsize_string, _("\
5201Set the amount of stack space reserved for each argument."), _("\
5202Show the amount of stack space reserved for each argument."), _("\
d929b26f
AC
5203This option can be set to one of:\n\
5204 32 - Force GDB to allocate 32-bit chunks per argument\n\
5205 64 - Force GDB to allocate 64-bit chunks per argument\n\
5206 auto - Allow GDB to determine the correct setting from the current\n\
7915a72c 5207 target and executable (default)"),
2c5b56ce 5208 NULL,
7915a72c 5209 NULL, /* FIXME: i18n: The amount of stack space reserved for each argument is %s. */
2c5b56ce 5210 &setmipscmdlist, &showmipscmdlist);
d929b26f 5211
2e4ebe70 5212 /* Allow the user to override the ABI. */
7ab04401
AC
5213 add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
5214 &mips_abi_string, _("\
5215Set the MIPS ABI used by this program."), _("\
5216Show the MIPS ABI used by this program."), _("\
5217This option can be set to one of:\n\
5218 auto - the default ABI associated with the current binary\n\
5219 o32\n\
5220 o64\n\
5221 n32\n\
5222 n64\n\
5223 eabi32\n\
5224 eabi64"),
5225 mips_abi_update,
5226 show_mips_abi,
5227 &setmipscmdlist, &showmipscmdlist);
2e4ebe70 5228
c906108c
SS
5229 /* Let the user turn off floating point and set the fence post for
5230 heuristic_proc_start. */
5231
5232 add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
1bedd215 5233 _("Set use of MIPS floating-point coprocessor."),
c906108c
SS
5234 &mipsfpulist, "set mipsfpu ", 0, &setlist);
5235 add_cmd ("single", class_support, set_mipsfpu_single_command,
1a966eab 5236 _("Select single-precision MIPS floating-point coprocessor."),
c906108c
SS
5237 &mipsfpulist);
5238 add_cmd ("double", class_support, set_mipsfpu_double_command,
1a966eab 5239 _("Select double-precision MIPS floating-point coprocessor."),
c906108c
SS
5240 &mipsfpulist);
5241 add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
5242 add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
5243 add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
5244 add_cmd ("none", class_support, set_mipsfpu_none_command,
1a966eab 5245 _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
c906108c
SS
5246 add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
5247 add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
5248 add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
5249 add_cmd ("auto", class_support, set_mipsfpu_auto_command,
1a966eab 5250 _("Select MIPS floating-point coprocessor automatically."),
c906108c
SS
5251 &mipsfpulist);
5252 add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
1a966eab 5253 _("Show current use of MIPS floating-point coprocessor target."),
c906108c
SS
5254 &showlist);
5255
c906108c
SS
5256 /* We really would like to have both "0" and "unlimited" work, but
5257 command.c doesn't deal with that. So make it a var_zinteger
5258 because the user can always use "999999" or some such for unlimited. */
6bcadd06 5259 add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
7915a72c
AC
5260 &heuristic_fence_post, _("\
5261Set the distance searched for the start of a function."), _("\
5262Show the distance searched for the start of a function."), _("\
c906108c
SS
5263If you are debugging a stripped executable, GDB needs to search through the\n\
5264program for the start of a function. This command sets the distance of the\n\
7915a72c 5265search. The only need to set it is when debugging a stripped executable."),
2c5b56ce 5266 reinit_frame_cache_sfunc,
7915a72c 5267 NULL, /* FIXME: i18n: The distance searched for the start of a function is %s. */
6bcadd06 5268 &setlist, &showlist);
c906108c
SS
5269
5270 /* Allow the user to control whether the upper bits of 64-bit
5271 addresses should be zeroed. */
7915a72c
AC
5272 add_setshow_auto_boolean_cmd ("mask-address", no_class,
5273 &mask_address_var, _("\
5274Set zeroing of upper 32 bits of 64-bit addresses."), _("\
5275Show zeroing of upper 32 bits of 64-bit addresses."), _("\
e9e68a56 5276Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to \n\
7915a72c 5277allow GDB to determine the correct value."),
08546159
AC
5278 NULL, show_mask_address,
5279 &setmipscmdlist, &showmipscmdlist);
43e526b9
JM
5280
5281 /* Allow the user to control the size of 32 bit registers within the
5282 raw remote packet. */
b3f42336 5283 add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
7915a72c
AC
5284 &mips64_transfers_32bit_regs_p, _("\
5285Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
5286 _("\
5287Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
5288 _("\
719ec221
AC
5289Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
5290that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
7915a72c 529164 bits for others. Use \"off\" to disable compatibility mode"),
2c5b56ce 5292 set_mips64_transfers_32bit_regs,
7915a72c 5293 NULL, /* FIXME: i18n: Compatibility with 64-bit MIPS target that transfers 32-bit quantities is %s. */
7915a72c 5294 &setlist, &showlist);
9ace0497
AC
5295
5296 /* Debug this files internals. */
6bcadd06 5297 add_setshow_zinteger_cmd ("mips", class_maintenance,
7915a72c
AC
5298 &mips_debug, _("\
5299Set mips debugging."), _("\
5300Show mips debugging."), _("\
5301When non-zero, mips specific debugging is enabled."),
2c5b56ce 5302 NULL,
7915a72c 5303 NULL, /* FIXME: i18n: Mips debugging is currently %s. */
6bcadd06 5304 &setdebuglist, &showdebuglist);
c906108c 5305}
This page took 0.989534 seconds and 4 git commands to generate.