2012-05-09 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / mips-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
bf64bfd6 2
0b302171 3 Copyright (C) 1988-2012 Free Software Foundation, Inc.
bf64bfd6 4
c906108c
SS
5 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
6 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
7
c5aa993b 8 This file is part of GDB.
c906108c 9
c5aa993b
JM
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
c5aa993b 13 (at your option) any later version.
c906108c 14
c5aa993b
JM
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
c906108c 19
c5aa993b 20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
22
23#include "defs.h"
24#include "gdb_string.h"
5e2e9765 25#include "gdb_assert.h"
c906108c
SS
26#include "frame.h"
27#include "inferior.h"
28#include "symtab.h"
29#include "value.h"
30#include "gdbcmd.h"
31#include "language.h"
32#include "gdbcore.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "gdbtypes.h"
36#include "target.h"
28d069e6 37#include "arch-utils.h"
4e052eda 38#include "regcache.h"
70f80edf 39#include "osabi.h"
d1973055 40#include "mips-tdep.h"
fe898f56 41#include "block.h"
a4b8ebc8 42#include "reggroups.h"
c906108c 43#include "opcode/mips.h"
c2d11a7d
JM
44#include "elf/mips.h"
45#include "elf-bfd.h"
2475bac3 46#include "symcat.h"
a4b8ebc8 47#include "sim-regno.h"
a89aa300 48#include "dis-asm.h"
edfae063
AC
49#include "frame-unwind.h"
50#include "frame-base.h"
51#include "trad-frame.h"
7d9b040b 52#include "infcall.h"
fed7ba43 53#include "floatformat.h"
29709017
DJ
54#include "remote.h"
55#include "target-descriptions.h"
2bd0c3d7 56#include "dwarf2-frame.h"
f8b73d13 57#include "user-regs.h"
79a45b7d 58#include "valprint.h"
175ff332 59#include "ax.h"
c906108c 60
8d5f9dcb
DJ
61static const struct objfile_data *mips_pdr_data;
62
5bbcb741 63static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
e0f7ec59 64
24e05951 65/* A useful bit in the CP0 status register (MIPS_PS_REGNUM). */
dd824b04
DJ
66/* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip. */
67#define ST0_FR (1 << 26)
68
b0069a17
AC
69/* The sizes of floating point registers. */
70
71enum
72{
73 MIPS_FPU_SINGLE_REGSIZE = 4,
74 MIPS_FPU_DOUBLE_REGSIZE = 8
75};
76
1a69e1e4
DJ
77enum
78{
79 MIPS32_REGSIZE = 4,
80 MIPS64_REGSIZE = 8
81};
0dadbba0 82
2e4ebe70
DJ
83static const char *mips_abi_string;
84
40478521 85static const char *const mips_abi_strings[] = {
2e4ebe70
DJ
86 "auto",
87 "n32",
88 "o32",
28d169de 89 "n64",
2e4ebe70
DJ
90 "o64",
91 "eabi32",
92 "eabi64",
93 NULL
94};
95
f8b73d13
DJ
96/* The standard register names, and all the valid aliases for them. */
97struct register_alias
98{
99 const char *name;
100 int regnum;
101};
102
103/* Aliases for o32 and most other ABIs. */
104const struct register_alias mips_o32_aliases[] = {
105 { "ta0", 12 },
106 { "ta1", 13 },
107 { "ta2", 14 },
108 { "ta3", 15 }
109};
110
111/* Aliases for n32 and n64. */
112const struct register_alias mips_n32_n64_aliases[] = {
113 { "ta0", 8 },
114 { "ta1", 9 },
115 { "ta2", 10 },
116 { "ta3", 11 }
117};
118
119/* Aliases for ABI-independent registers. */
120const struct register_alias mips_register_aliases[] = {
121 /* The architecture manuals specify these ABI-independent names for
122 the GPRs. */
123#define R(n) { "r" #n, n }
124 R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
125 R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
126 R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
127 R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
128#undef R
129
130 /* k0 and k1 are sometimes called these instead (for "kernel
131 temp"). */
132 { "kt0", 26 },
133 { "kt1", 27 },
134
135 /* This is the traditional GDB name for the CP0 status register. */
136 { "sr", MIPS_PS_REGNUM },
137
138 /* This is the traditional GDB name for the CP0 BadVAddr register. */
139 { "bad", MIPS_EMBED_BADVADDR_REGNUM },
140
141 /* This is the traditional GDB name for the FCSR. */
142 { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
143};
144
865093a3
AR
145const struct register_alias mips_numeric_register_aliases[] = {
146#define R(n) { #n, n }
147 R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
148 R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
149 R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
150 R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
151#undef R
152};
153
c906108c
SS
154#ifndef MIPS_DEFAULT_FPU_TYPE
155#define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
156#endif
157static int mips_fpu_type_auto = 1;
158static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
7a292a7a 159
9ace0497 160static int mips_debug = 0;
7a292a7a 161
29709017
DJ
162/* Properties (for struct target_desc) describing the g/G packet
163 layout. */
164#define PROPERTY_GP32 "internal: transfers-32bit-registers"
165#define PROPERTY_GP64 "internal: transfers-64bit-registers"
166
4eb0ad19
DJ
167struct target_desc *mips_tdesc_gp32;
168struct target_desc *mips_tdesc_gp64;
169
56cea623
AC
170const struct mips_regnum *
171mips_regnum (struct gdbarch *gdbarch)
172{
173 return gdbarch_tdep (gdbarch)->regnum;
174}
175
176static int
177mips_fpa0_regnum (struct gdbarch *gdbarch)
178{
179 return mips_regnum (gdbarch)->fp0 + 12;
180}
181
004159a2
MR
182/* Return 1 if REGNUM refers to a floating-point general register, raw
183 or cooked. Otherwise return 0. */
184
185static int
186mips_float_register_p (struct gdbarch *gdbarch, int regnum)
187{
188 int rawnum = regnum % gdbarch_num_regs (gdbarch);
189
190 return (rawnum >= mips_regnum (gdbarch)->fp0
191 && rawnum < mips_regnum (gdbarch)->fp0 + 32);
192}
193
74ed0bb4
MD
194#define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
195 == MIPS_ABI_EABI32 \
196 || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)
c2d11a7d 197
025bb325
MS
198#define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
199 (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)
c2d11a7d 200
025bb325
MS
201#define MIPS_LAST_ARG_REGNUM(gdbarch) \
202 (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)
c2d11a7d 203
74ed0bb4 204#define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)
c2d11a7d 205
d1973055
KB
206/* Return the MIPS ABI associated with GDBARCH. */
207enum mips_abi
208mips_abi (struct gdbarch *gdbarch)
209{
210 return gdbarch_tdep (gdbarch)->mips_abi;
211}
212
4246e332 213int
1b13c4f6 214mips_isa_regsize (struct gdbarch *gdbarch)
4246e332 215{
29709017
DJ
216 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
217
218 /* If we know how big the registers are, use that size. */
219 if (tdep->register_size_valid_p)
220 return tdep->register_size;
221
222 /* Fall back to the previous behavior. */
4246e332
AC
223 return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
224 / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
225}
226
025bb325 227/* Return the currently configured (or set) saved register size. */
480d3dd2 228
e6bc2e8a 229unsigned int
13326b4e 230mips_abi_regsize (struct gdbarch *gdbarch)
d929b26f 231{
1a69e1e4
DJ
232 switch (mips_abi (gdbarch))
233 {
234 case MIPS_ABI_EABI32:
235 case MIPS_ABI_O32:
236 return 4;
237 case MIPS_ABI_N32:
238 case MIPS_ABI_N64:
239 case MIPS_ABI_O64:
240 case MIPS_ABI_EABI64:
241 return 8;
242 case MIPS_ABI_UNKNOWN:
243 case MIPS_ABI_LAST:
244 default:
245 internal_error (__FILE__, __LINE__, _("bad switch"));
246 }
d929b26f
AC
247}
248
742c84f6
MR
249/* MIPS16 function addresses are odd (bit 0 is set). Here are some
250 functions to test, set, or clear bit 0 of addresses. */
251
252static CORE_ADDR
253is_mips16_addr (CORE_ADDR addr)
254{
255 return ((addr) & 1);
256}
257
258static CORE_ADDR
259unmake_mips16_addr (CORE_ADDR addr)
260{
261 return ((addr) & ~(CORE_ADDR) 1);
262}
263
264static CORE_ADDR
265make_mips16_addr (CORE_ADDR addr)
266{
267 return ((addr) | (CORE_ADDR) 1);
268}
269
71b8ef93 270/* Functions for setting and testing a bit in a minimal symbol that
5a89d8aa 271 marks it as 16-bit function. The MSB of the minimal symbol's
f594e5e9 272 "info" field is used for this purpose.
5a89d8aa 273
95f1da47 274 gdbarch_elf_make_msymbol_special tests whether an ELF symbol is "special",
5a89d8aa
MS
275 i.e. refers to a 16-bit function, and sets a "special" bit in a
276 minimal symbol to mark it as a 16-bit function
277
f594e5e9 278 MSYMBOL_IS_SPECIAL tests the "special" bit in a minimal symbol */
5a89d8aa 279
5a89d8aa 280static void
6d82d43b
AC
281mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
282{
7f0e6aae
MR
283 if (ELF_ST_IS_MIPS16 (((elf_symbol_type *)
284 (sym))->internal_elf_sym.st_other))
6d82d43b 285 {
b887350f 286 MSYMBOL_TARGET_FLAG_1 (msym) = 1;
6d82d43b 287 }
5a89d8aa
MS
288}
289
71b8ef93
MS
290static int
291msymbol_is_special (struct minimal_symbol *msym)
292{
b887350f 293 return MSYMBOL_TARGET_FLAG_1 (msym);
71b8ef93
MS
294}
295
88658117
AC
296/* XFER a value from the big/little/left end of the register.
297 Depending on the size of the value it might occupy the entire
298 register or just part of it. Make an allowance for this, aligning
299 things accordingly. */
300
301static void
ba32f989
DJ
302mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
303 int reg_num, int length,
870cd05e
MK
304 enum bfd_endian endian, gdb_byte *in,
305 const gdb_byte *out, int buf_offset)
88658117 306{
88658117 307 int reg_offset = 0;
72a155b4
UW
308
309 gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
cb1d2653
AC
310 /* Need to transfer the left or right part of the register, based on
311 the targets byte order. */
88658117
AC
312 switch (endian)
313 {
314 case BFD_ENDIAN_BIG:
72a155b4 315 reg_offset = register_size (gdbarch, reg_num) - length;
88658117
AC
316 break;
317 case BFD_ENDIAN_LITTLE:
318 reg_offset = 0;
319 break;
6d82d43b 320 case BFD_ENDIAN_UNKNOWN: /* Indicates no alignment. */
88658117
AC
321 reg_offset = 0;
322 break;
323 default:
e2e0b3e5 324 internal_error (__FILE__, __LINE__, _("bad switch"));
88658117
AC
325 }
326 if (mips_debug)
cb1d2653
AC
327 fprintf_unfiltered (gdb_stderr,
328 "xfer $%d, reg offset %d, buf offset %d, length %d, ",
329 reg_num, reg_offset, buf_offset, length);
88658117
AC
330 if (mips_debug && out != NULL)
331 {
332 int i;
cb1d2653 333 fprintf_unfiltered (gdb_stdlog, "out ");
88658117 334 for (i = 0; i < length; i++)
cb1d2653 335 fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
88658117
AC
336 }
337 if (in != NULL)
6d82d43b
AC
338 regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
339 in + buf_offset);
88658117 340 if (out != NULL)
6d82d43b
AC
341 regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
342 out + buf_offset);
88658117
AC
343 if (mips_debug && in != NULL)
344 {
345 int i;
cb1d2653 346 fprintf_unfiltered (gdb_stdlog, "in ");
88658117 347 for (i = 0; i < length; i++)
cb1d2653 348 fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
88658117
AC
349 }
350 if (mips_debug)
351 fprintf_unfiltered (gdb_stdlog, "\n");
352}
353
dd824b04
DJ
354/* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
355 compatiblity mode. A return value of 1 means that we have
356 physical 64-bit registers, but should treat them as 32-bit registers. */
357
358static int
9c9acae0 359mips2_fp_compat (struct frame_info *frame)
dd824b04 360{
72a155b4 361 struct gdbarch *gdbarch = get_frame_arch (frame);
dd824b04
DJ
362 /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
363 meaningful. */
72a155b4 364 if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
dd824b04
DJ
365 return 0;
366
367#if 0
368 /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
369 in all the places we deal with FP registers. PR gdb/413. */
370 /* Otherwise check the FR bit in the status register - it controls
371 the FP compatiblity mode. If it is clear we are in compatibility
372 mode. */
9c9acae0 373 if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
dd824b04
DJ
374 return 1;
375#endif
361d1df0 376
dd824b04
DJ
377 return 0;
378}
379
7a292a7a 380#define VM_MIN_ADDRESS (CORE_ADDR)0x400000
c906108c 381
74ed0bb4 382static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);
c906108c 383
a14ed312 384static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
c906108c 385
025bb325 386/* The list of available "set mips " and "show mips " commands. */
acdb74a0
AC
387
388static struct cmd_list_element *setmipscmdlist = NULL;
389static struct cmd_list_element *showmipscmdlist = NULL;
390
5e2e9765
KB
391/* Integer registers 0 thru 31 are handled explicitly by
392 mips_register_name(). Processor specific registers 32 and above
8a9fc081 393 are listed in the following tables. */
691c0433 394
6d82d43b
AC
395enum
396{ NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
691c0433
AC
397
398/* Generic MIPS. */
399
400static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
401 "sr", "lo", "hi", "bad", "cause", "pc",
402 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
403 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
404 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
405 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
1faeff08 406 "fsr", "fir",
691c0433
AC
407};
408
409/* Names of IDT R3041 registers. */
410
411static const char *mips_r3041_reg_names[] = {
6d82d43b
AC
412 "sr", "lo", "hi", "bad", "cause", "pc",
413 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
414 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
415 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
416 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
417 "fsr", "fir", "", /*"fp" */ "",
418 "", "", "bus", "ccfg", "", "", "", "",
419 "", "", "port", "cmp", "", "", "epc", "prid",
691c0433
AC
420};
421
422/* Names of tx39 registers. */
423
424static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
425 "sr", "lo", "hi", "bad", "cause", "pc",
426 "", "", "", "", "", "", "", "",
427 "", "", "", "", "", "", "", "",
428 "", "", "", "", "", "", "", "",
429 "", "", "", "", "", "", "", "",
430 "", "", "", "",
431 "", "", "", "", "", "", "", "",
1faeff08 432 "", "", "config", "cache", "debug", "depc", "epc",
691c0433
AC
433};
434
435/* Names of IRIX registers. */
436static const char *mips_irix_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
6d82d43b
AC
437 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
438 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
439 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
440 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
441 "pc", "cause", "bad", "hi", "lo", "fsr", "fir"
691c0433
AC
442};
443
44099a67 444/* Names of registers with Linux kernels. */
1faeff08
MR
445static const char *mips_linux_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
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"
452};
453
cce74817 454
5e2e9765 455/* Return the name of the register corresponding to REGNO. */
5a89d8aa 456static const char *
d93859e2 457mips_register_name (struct gdbarch *gdbarch, int regno)
cce74817 458{
d93859e2 459 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5e2e9765
KB
460 /* GPR names for all ABIs other than n32/n64. */
461 static char *mips_gpr_names[] = {
6d82d43b
AC
462 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
463 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
464 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
465 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
5e2e9765
KB
466 };
467
468 /* GPR names for n32 and n64 ABIs. */
469 static char *mips_n32_n64_gpr_names[] = {
6d82d43b
AC
470 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
471 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
472 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
473 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
5e2e9765
KB
474 };
475
d93859e2 476 enum mips_abi abi = mips_abi (gdbarch);
5e2e9765 477
f57d151a 478 /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers,
6229fbea
HZ
479 but then don't make the raw register names visible. This (upper)
480 range of user visible register numbers are the pseudo-registers.
481
482 This approach was adopted accommodate the following scenario:
483 It is possible to debug a 64-bit device using a 32-bit
484 programming model. In such instances, the raw registers are
485 configured to be 64-bits wide, while the pseudo registers are
486 configured to be 32-bits wide. The registers that the user
487 sees - the pseudo registers - match the users expectations
488 given the programming model being used. */
d93859e2
UW
489 int rawnum = regno % gdbarch_num_regs (gdbarch);
490 if (regno < gdbarch_num_regs (gdbarch))
a4b8ebc8
AC
491 return "";
492
5e2e9765
KB
493 /* The MIPS integer registers are always mapped from 0 to 31. The
494 names of the registers (which reflects the conventions regarding
495 register use) vary depending on the ABI. */
a4b8ebc8 496 if (0 <= rawnum && rawnum < 32)
5e2e9765
KB
497 {
498 if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
a4b8ebc8 499 return mips_n32_n64_gpr_names[rawnum];
5e2e9765 500 else
a4b8ebc8 501 return mips_gpr_names[rawnum];
5e2e9765 502 }
d93859e2
UW
503 else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
504 return tdesc_register_name (gdbarch, rawnum);
505 else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
691c0433
AC
506 {
507 gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
1faeff08
MR
508 if (tdep->mips_processor_reg_names[rawnum - 32])
509 return tdep->mips_processor_reg_names[rawnum - 32];
510 return "";
691c0433 511 }
5e2e9765
KB
512 else
513 internal_error (__FILE__, __LINE__,
e2e0b3e5 514 _("mips_register_name: bad register number %d"), rawnum);
cce74817 515}
5e2e9765 516
a4b8ebc8 517/* Return the groups that a MIPS register can be categorised into. */
c5aa993b 518
a4b8ebc8
AC
519static int
520mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
521 struct reggroup *reggroup)
522{
523 int vector_p;
524 int float_p;
525 int raw_p;
72a155b4
UW
526 int rawnum = regnum % gdbarch_num_regs (gdbarch);
527 int pseudo = regnum / gdbarch_num_regs (gdbarch);
a4b8ebc8
AC
528 if (reggroup == all_reggroup)
529 return pseudo;
530 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
531 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
532 /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
533 (gdbarch), as not all architectures are multi-arch. */
72a155b4
UW
534 raw_p = rawnum < gdbarch_num_regs (gdbarch);
535 if (gdbarch_register_name (gdbarch, regnum) == NULL
536 || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
a4b8ebc8
AC
537 return 0;
538 if (reggroup == float_reggroup)
539 return float_p && pseudo;
540 if (reggroup == vector_reggroup)
541 return vector_p && pseudo;
542 if (reggroup == general_reggroup)
543 return (!vector_p && !float_p) && pseudo;
544 /* Save the pseudo registers. Need to make certain that any code
545 extracting register values from a saved register cache also uses
546 pseudo registers. */
547 if (reggroup == save_reggroup)
548 return raw_p && pseudo;
549 /* Restore the same pseudo register. */
550 if (reggroup == restore_reggroup)
551 return raw_p && pseudo;
6d82d43b 552 return 0;
a4b8ebc8
AC
553}
554
f8b73d13
DJ
555/* Return the groups that a MIPS register can be categorised into.
556 This version is only used if we have a target description which
557 describes real registers (and their groups). */
558
559static int
560mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
561 struct reggroup *reggroup)
562{
563 int rawnum = regnum % gdbarch_num_regs (gdbarch);
564 int pseudo = regnum / gdbarch_num_regs (gdbarch);
565 int ret;
566
567 /* Only save, restore, and display the pseudo registers. Need to
568 make certain that any code extracting register values from a
569 saved register cache also uses pseudo registers.
570
571 Note: saving and restoring the pseudo registers is slightly
572 strange; if we have 64 bits, we should save and restore all
573 64 bits. But this is hard and has little benefit. */
574 if (!pseudo)
575 return 0;
576
577 ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
578 if (ret != -1)
579 return ret;
580
581 return mips_register_reggroup_p (gdbarch, regnum, reggroup);
582}
583
a4b8ebc8 584/* Map the symbol table registers which live in the range [1 *
f57d151a 585 gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
47ebcfbe 586 registers. Take care of alignment and size problems. */
c5aa993b 587
05d1431c 588static enum register_status
a4b8ebc8 589mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
47a35522 590 int cookednum, gdb_byte *buf)
a4b8ebc8 591{
72a155b4
UW
592 int rawnum = cookednum % gdbarch_num_regs (gdbarch);
593 gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
594 && cookednum < 2 * gdbarch_num_regs (gdbarch));
47ebcfbe 595 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
05d1431c 596 return regcache_raw_read (regcache, rawnum, buf);
6d82d43b
AC
597 else if (register_size (gdbarch, rawnum) >
598 register_size (gdbarch, cookednum))
47ebcfbe 599 {
8bdf35dc 600 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
05d1431c 601 return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
47ebcfbe 602 else
8bdf35dc
KB
603 {
604 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
605 LONGEST regval;
05d1431c
PA
606 enum register_status status;
607
608 status = regcache_raw_read_signed (regcache, rawnum, &regval);
609 if (status == REG_VALID)
610 store_signed_integer (buf, 4, byte_order, regval);
611 return status;
8bdf35dc 612 }
47ebcfbe
AC
613 }
614 else
e2e0b3e5 615 internal_error (__FILE__, __LINE__, _("bad register size"));
a4b8ebc8
AC
616}
617
618static void
6d82d43b
AC
619mips_pseudo_register_write (struct gdbarch *gdbarch,
620 struct regcache *regcache, int cookednum,
47a35522 621 const gdb_byte *buf)
a4b8ebc8 622{
72a155b4
UW
623 int rawnum = cookednum % gdbarch_num_regs (gdbarch);
624 gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
625 && cookednum < 2 * gdbarch_num_regs (gdbarch));
47ebcfbe 626 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
de38af99 627 regcache_raw_write (regcache, rawnum, buf);
6d82d43b
AC
628 else if (register_size (gdbarch, rawnum) >
629 register_size (gdbarch, cookednum))
47ebcfbe 630 {
8bdf35dc 631 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
47ebcfbe
AC
632 regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
633 else
8bdf35dc
KB
634 {
635 /* Sign extend the shortened version of the register prior
636 to placing it in the raw register. This is required for
637 some mips64 parts in order to avoid unpredictable behavior. */
638 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
639 LONGEST regval = extract_signed_integer (buf, 4, byte_order);
640 regcache_raw_write_signed (regcache, rawnum, regval);
641 }
47ebcfbe
AC
642 }
643 else
e2e0b3e5 644 internal_error (__FILE__, __LINE__, _("bad register size"));
a4b8ebc8 645}
c5aa993b 646
175ff332
HZ
647static int
648mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
649 struct agent_expr *ax, int reg)
650{
651 int rawnum = reg % gdbarch_num_regs (gdbarch);
652 gdb_assert (reg >= gdbarch_num_regs (gdbarch)
653 && reg < 2 * gdbarch_num_regs (gdbarch));
654
655 ax_reg_mask (ax, rawnum);
656
657 return 0;
658}
659
660static int
661mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
662 struct agent_expr *ax, int reg)
663{
664 int rawnum = reg % gdbarch_num_regs (gdbarch);
665 gdb_assert (reg >= gdbarch_num_regs (gdbarch)
666 && reg < 2 * gdbarch_num_regs (gdbarch));
667 if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
668 {
669 ax_reg (ax, rawnum);
670
671 if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
672 {
673 if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
674 || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
675 {
676 ax_const_l (ax, 32);
677 ax_simple (ax, aop_lsh);
678 }
679 ax_const_l (ax, 32);
680 ax_simple (ax, aop_rsh_signed);
681 }
682 }
683 else
684 internal_error (__FILE__, __LINE__, _("bad register size"));
685
686 return 0;
687}
688
c906108c 689/* Table to translate MIPS16 register field to actual register number. */
6d82d43b 690static int mips16_to_32_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
c906108c
SS
691
692/* Heuristic_proc_start may hunt through the text section for a long
693 time across a 2400 baud serial line. Allows the user to limit this
694 search. */
695
696static unsigned int heuristic_fence_post = 0;
697
46cd78fb 698/* Number of bytes of storage in the actual machine representation for
719ec221
AC
699 register N. NOTE: This defines the pseudo register type so need to
700 rebuild the architecture vector. */
43e526b9
JM
701
702static int mips64_transfers_32bit_regs_p = 0;
703
719ec221
AC
704static void
705set_mips64_transfers_32bit_regs (char *args, int from_tty,
706 struct cmd_list_element *c)
43e526b9 707{
719ec221
AC
708 struct gdbarch_info info;
709 gdbarch_info_init (&info);
710 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
711 instead of relying on globals. Doing that would let generic code
712 handle the search for this specific architecture. */
713 if (!gdbarch_update_p (info))
a4b8ebc8 714 {
719ec221 715 mips64_transfers_32bit_regs_p = 0;
8a3fe4f8 716 error (_("32-bit compatibility mode not supported"));
a4b8ebc8 717 }
a4b8ebc8
AC
718}
719
47ebcfbe 720/* Convert to/from a register and the corresponding memory value. */
43e526b9 721
ee51a8c7
KB
722/* This predicate tests for the case of an 8 byte floating point
723 value that is being transferred to or from a pair of floating point
724 registers each of which are (or are considered to be) only 4 bytes
725 wide. */
ff2e87ac 726static int
ee51a8c7
KB
727mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
728 struct type *type)
ff2e87ac 729{
0abe36f5
MD
730 return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
731 && register_size (gdbarch, regnum) == 4
004159a2 732 && mips_float_register_p (gdbarch, regnum)
6d82d43b 733 && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
ff2e87ac
AC
734}
735
ee51a8c7
KB
736/* This predicate tests for the case of a value of less than 8
737 bytes in width that is being transfered to or from an 8 byte
738 general purpose register. */
739static int
740mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
741 struct type *type)
742{
743 int num_regs = gdbarch_num_regs (gdbarch);
744
745 return (register_size (gdbarch, regnum) == 8
746 && regnum % num_regs > 0 && regnum % num_regs < 32
747 && TYPE_LENGTH (type) < 8);
748}
749
750static int
025bb325
MS
751mips_convert_register_p (struct gdbarch *gdbarch,
752 int regnum, struct type *type)
ee51a8c7
KB
753{
754 return mips_convert_register_float_case_p (gdbarch, regnum, type)
755 || mips_convert_register_gpreg_case_p (gdbarch, regnum, type);
756}
757
8dccd430 758static int
ff2e87ac 759mips_register_to_value (struct frame_info *frame, int regnum,
8dccd430
PA
760 struct type *type, gdb_byte *to,
761 int *optimizedp, int *unavailablep)
102182a9 762{
ee51a8c7
KB
763 struct gdbarch *gdbarch = get_frame_arch (frame);
764
765 if (mips_convert_register_float_case_p (gdbarch, regnum, type))
766 {
767 get_frame_register (frame, regnum + 0, to + 4);
768 get_frame_register (frame, regnum + 1, to + 0);
8dccd430
PA
769
770 if (!get_frame_register_bytes (frame, regnum + 0, 0, 4, to + 4,
771 optimizedp, unavailablep))
772 return 0;
773
774 if (!get_frame_register_bytes (frame, regnum + 1, 0, 4, to + 0,
775 optimizedp, unavailablep))
776 return 0;
777 *optimizedp = *unavailablep = 0;
778 return 1;
ee51a8c7
KB
779 }
780 else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
781 {
782 int len = TYPE_LENGTH (type);
8dccd430
PA
783 CORE_ADDR offset;
784
785 offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 - len : 0;
786 if (!get_frame_register_bytes (frame, regnum, offset, len, to,
787 optimizedp, unavailablep))
788 return 0;
789
790 *optimizedp = *unavailablep = 0;
791 return 1;
ee51a8c7
KB
792 }
793 else
794 {
795 internal_error (__FILE__, __LINE__,
796 _("mips_register_to_value: unrecognized case"));
797 }
102182a9
MS
798}
799
42c466d7 800static void
ff2e87ac 801mips_value_to_register (struct frame_info *frame, int regnum,
47a35522 802 struct type *type, const gdb_byte *from)
102182a9 803{
ee51a8c7
KB
804 struct gdbarch *gdbarch = get_frame_arch (frame);
805
806 if (mips_convert_register_float_case_p (gdbarch, regnum, type))
807 {
808 put_frame_register (frame, regnum + 0, from + 4);
809 put_frame_register (frame, regnum + 1, from + 0);
810 }
811 else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
812 {
813 gdb_byte fill[8];
814 int len = TYPE_LENGTH (type);
815
816 /* Sign extend values, irrespective of type, that are stored to
817 a 64-bit general purpose register. (32-bit unsigned values
818 are stored as signed quantities within a 64-bit register.
819 When performing an operation, in compiled code, that combines
820 a 32-bit unsigned value with a signed 64-bit value, a type
821 conversion is first performed that zeroes out the high 32 bits.) */
822 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
823 {
824 if (from[0] & 0x80)
825 store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
826 else
827 store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
828 put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
829 put_frame_register_bytes (frame, regnum, 8 - len, len, from);
830 }
831 else
832 {
833 if (from[len-1] & 0x80)
834 store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
835 else
836 store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
837 put_frame_register_bytes (frame, regnum, 0, len, from);
838 put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
839 }
840 }
841 else
842 {
843 internal_error (__FILE__, __LINE__,
844 _("mips_value_to_register: unrecognized case"));
845 }
102182a9
MS
846}
847
a4b8ebc8
AC
848/* Return the GDB type object for the "standard" data type of data in
849 register REG. */
78fde5f8
KB
850
851static struct type *
a4b8ebc8
AC
852mips_register_type (struct gdbarch *gdbarch, int regnum)
853{
72a155b4 854 gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
004159a2 855 if (mips_float_register_p (gdbarch, regnum))
a6425924 856 {
5ef80fb0 857 /* The floating-point registers raw, or cooked, always match
1b13c4f6 858 mips_isa_regsize(), and also map 1:1, byte for byte. */
8da61cc4 859 if (mips_isa_regsize (gdbarch) == 4)
27067745 860 return builtin_type (gdbarch)->builtin_float;
8da61cc4 861 else
27067745 862 return builtin_type (gdbarch)->builtin_double;
a6425924 863 }
72a155b4 864 else if (regnum < gdbarch_num_regs (gdbarch))
d5ac5a39
AC
865 {
866 /* The raw or ISA registers. These are all sized according to
867 the ISA regsize. */
868 if (mips_isa_regsize (gdbarch) == 4)
df4df182 869 return builtin_type (gdbarch)->builtin_int32;
d5ac5a39 870 else
df4df182 871 return builtin_type (gdbarch)->builtin_int64;
d5ac5a39 872 }
78fde5f8 873 else
d5ac5a39 874 {
1faeff08
MR
875 int rawnum = regnum - gdbarch_num_regs (gdbarch);
876
d5ac5a39
AC
877 /* The cooked or ABI registers. These are sized according to
878 the ABI (with a few complications). */
1faeff08
MR
879 if (rawnum == mips_regnum (gdbarch)->fp_control_status
880 || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
881 return builtin_type (gdbarch)->builtin_int32;
882 else if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
883 && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
884 && rawnum >= MIPS_FIRST_EMBED_REGNUM
885 && rawnum <= MIPS_LAST_EMBED_REGNUM)
d5ac5a39
AC
886 /* The pseudo/cooked view of the embedded registers is always
887 32-bit. The raw view is handled below. */
df4df182 888 return builtin_type (gdbarch)->builtin_int32;
d5ac5a39
AC
889 else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
890 /* The target, while possibly using a 64-bit register buffer,
891 is only transfering 32-bits of each integer register.
892 Reflect this in the cooked/pseudo (ABI) register value. */
df4df182 893 return builtin_type (gdbarch)->builtin_int32;
d5ac5a39
AC
894 else if (mips_abi_regsize (gdbarch) == 4)
895 /* The ABI is restricted to 32-bit registers (the ISA could be
896 32- or 64-bit). */
df4df182 897 return builtin_type (gdbarch)->builtin_int32;
d5ac5a39
AC
898 else
899 /* 64-bit ABI. */
df4df182 900 return builtin_type (gdbarch)->builtin_int64;
d5ac5a39 901 }
78fde5f8
KB
902}
903
f8b73d13
DJ
904/* Return the GDB type for the pseudo register REGNUM, which is the
905 ABI-level view. This function is only called if there is a target
906 description which includes registers, so we know precisely the
907 types of hardware registers. */
908
909static struct type *
910mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
911{
912 const int num_regs = gdbarch_num_regs (gdbarch);
913 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
914 int rawnum = regnum % num_regs;
915 struct type *rawtype;
916
917 gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);
918
919 /* Absent registers are still absent. */
920 rawtype = gdbarch_register_type (gdbarch, rawnum);
921 if (TYPE_LENGTH (rawtype) == 0)
922 return rawtype;
923
1faeff08
MR
924 if (rawnum >= mips_regnum (gdbarch)->fp0
925 && rawnum < mips_regnum (gdbarch)->fp0 + 32)
f8b73d13
DJ
926 /* Present the floating point registers however the hardware did;
927 do not try to convert between FPU layouts. */
928 return rawtype;
929
f8b73d13
DJ
930 /* Use pointer types for registers if we can. For n32 we can not,
931 since we do not have a 64-bit pointer type. */
0dfff4cb
UW
932 if (mips_abi_regsize (gdbarch)
933 == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
f8b73d13 934 {
1faeff08
MR
935 if (rawnum == MIPS_SP_REGNUM
936 || rawnum == mips_regnum (gdbarch)->badvaddr)
0dfff4cb 937 return builtin_type (gdbarch)->builtin_data_ptr;
1faeff08 938 else if (rawnum == mips_regnum (gdbarch)->pc)
0dfff4cb 939 return builtin_type (gdbarch)->builtin_func_ptr;
f8b73d13
DJ
940 }
941
942 if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
1faeff08
MR
943 && ((rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_PS_REGNUM)
944 || rawnum == mips_regnum (gdbarch)->lo
945 || rawnum == mips_regnum (gdbarch)->hi
946 || rawnum == mips_regnum (gdbarch)->badvaddr
947 || rawnum == mips_regnum (gdbarch)->cause
948 || rawnum == mips_regnum (gdbarch)->pc
949 || (mips_regnum (gdbarch)->dspacc != -1
950 && rawnum >= mips_regnum (gdbarch)->dspacc
951 && rawnum < mips_regnum (gdbarch)->dspacc + 6)))
df4df182 952 return builtin_type (gdbarch)->builtin_int32;
f8b73d13 953
1faeff08
MR
954 if (gdbarch_osabi (gdbarch) != GDB_OSABI_IRIX
955 && gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
956 && rawnum >= MIPS_EMBED_FP0_REGNUM + 32
957 && rawnum <= MIPS_LAST_EMBED_REGNUM)
958 {
959 /* The pseudo/cooked view of embedded registers is always
960 32-bit, even if the target transfers 64-bit values for them.
961 New targets relying on XML descriptions should only transfer
962 the necessary 32 bits, but older versions of GDB expected 64,
963 so allow the target to provide 64 bits without interfering
964 with the displayed type. */
965 return builtin_type (gdbarch)->builtin_int32;
966 }
967
f8b73d13
DJ
968 /* For all other registers, pass through the hardware type. */
969 return rawtype;
970}
bcb0cc15 971
025bb325 972/* Should the upper word of 64-bit addresses be zeroed? */
7f19b9a2 973enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
4014092b
AC
974
975static int
480d3dd2 976mips_mask_address_p (struct gdbarch_tdep *tdep)
4014092b
AC
977{
978 switch (mask_address_var)
979 {
7f19b9a2 980 case AUTO_BOOLEAN_TRUE:
4014092b 981 return 1;
7f19b9a2 982 case AUTO_BOOLEAN_FALSE:
4014092b
AC
983 return 0;
984 break;
7f19b9a2 985 case AUTO_BOOLEAN_AUTO:
480d3dd2 986 return tdep->default_mask_address_p;
4014092b 987 default:
025bb325
MS
988 internal_error (__FILE__, __LINE__,
989 _("mips_mask_address_p: bad switch"));
4014092b 990 return -1;
361d1df0 991 }
4014092b
AC
992}
993
994static void
08546159
AC
995show_mask_address (struct ui_file *file, int from_tty,
996 struct cmd_list_element *c, const char *value)
4014092b 997{
1cf3db46 998 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch);
08546159
AC
999
1000 deprecated_show_value_hack (file, from_tty, c, value);
4014092b
AC
1001 switch (mask_address_var)
1002 {
7f19b9a2 1003 case AUTO_BOOLEAN_TRUE:
4014092b
AC
1004 printf_filtered ("The 32 bit mips address mask is enabled\n");
1005 break;
7f19b9a2 1006 case AUTO_BOOLEAN_FALSE:
4014092b
AC
1007 printf_filtered ("The 32 bit mips address mask is disabled\n");
1008 break;
7f19b9a2 1009 case AUTO_BOOLEAN_AUTO:
6d82d43b
AC
1010 printf_filtered
1011 ("The 32 bit address mask is set automatically. Currently %s\n",
1012 mips_mask_address_p (tdep) ? "enabled" : "disabled");
4014092b
AC
1013 break;
1014 default:
e2e0b3e5 1015 internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
4014092b 1016 break;
361d1df0 1017 }
4014092b 1018}
c906108c 1019
c906108c
SS
1020/* Tell if the program counter value in MEMADDR is in a MIPS16 function. */
1021
0fe7e7c8
AC
1022int
1023mips_pc_is_mips16 (CORE_ADDR memaddr)
c906108c
SS
1024{
1025 struct minimal_symbol *sym;
1026
91912e4d
MR
1027 /* A flag indicating that this is a MIPS16 function is stored by
1028 elfread.c in the high bit of the info field. Use this to decide
1029 if the function is MIPS16 or normal MIPS. Otherwise if bit 0 of
1030 the address is set, assume this is a MIPS16 address. */
c906108c
SS
1031 sym = lookup_minimal_symbol_by_pc (memaddr);
1032 if (sym)
71b8ef93 1033 return msymbol_is_special (sym);
c906108c 1034 else
91912e4d 1035 return is_mips16_addr (memaddr);
c906108c
SS
1036}
1037
14132e89
MR
1038/* Various MIPS16 thunk (aka stub or trampoline) names. */
1039
1040static const char mips_str_mips16_call_stub[] = "__mips16_call_stub_";
1041static const char mips_str_mips16_ret_stub[] = "__mips16_ret_";
1042static const char mips_str_call_fp_stub[] = "__call_stub_fp_";
1043static const char mips_str_call_stub[] = "__call_stub_";
1044static const char mips_str_fn_stub[] = "__fn_stub_";
1045
1046/* This is used as a PIC thunk prefix. */
1047
1048static const char mips_str_pic[] = ".pic.";
1049
1050/* Return non-zero if the PC is inside a call thunk (aka stub or
1051 trampoline) that should be treated as a temporary frame. */
1052
1053static int
1054mips_in_frame_stub (CORE_ADDR pc)
1055{
1056 CORE_ADDR start_addr;
1057 const char *name;
1058
1059 /* Find the starting address of the function containing the PC. */
1060 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
1061 return 0;
1062
1063 /* If the PC is in __mips16_call_stub_*, this is a call/return stub. */
1064 if (strncmp (name, mips_str_mips16_call_stub,
1065 strlen (mips_str_mips16_call_stub)) == 0)
1066 return 1;
1067 /* If the PC is in __call_stub_*, this is a call/return or a call stub. */
1068 if (strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
1069 return 1;
1070 /* If the PC is in __fn_stub_*, this is a call stub. */
1071 if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0)
1072 return 1;
1073
1074 return 0; /* Not a stub. */
1075}
1076
b2fa5097 1077/* MIPS believes that the PC has a sign extended value. Perhaps the
025bb325 1078 all registers should be sign extended for simplicity? */
6c997a34
AC
1079
1080static CORE_ADDR
61a1198a 1081mips_read_pc (struct regcache *regcache)
6c997a34 1082{
61a1198a
UW
1083 ULONGEST pc;
1084 int regnum = mips_regnum (get_regcache_arch (regcache))->pc;
1085 regcache_cooked_read_signed (regcache, regnum, &pc);
930bd0e0
KB
1086 if (is_mips16_addr (pc))
1087 pc = unmake_mips16_addr (pc);
61a1198a 1088 return pc;
b6cb9035
AC
1089}
1090
58dfe9ff
AC
1091static CORE_ADDR
1092mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1093{
14132e89 1094 CORE_ADDR pc;
930bd0e0
KB
1095
1096 pc = frame_unwind_register_signed
1097 (next_frame, gdbarch_num_regs (gdbarch) + mips_regnum (gdbarch)->pc);
1098 if (is_mips16_addr (pc))
1099 pc = unmake_mips16_addr (pc);
14132e89
MR
1100 /* macro/2012-04-20: This hack skips over MIPS16 call thunks as
1101 intermediate frames. In this case we can get the caller's address
1102 from $ra, or if $ra contains an address within a thunk as well, then
1103 it must be in the return path of __mips16_call_stub_{s,d}{f,c}_{0..10}
1104 and thus the caller's address is in $s2. */
1105 if (frame_relative_level (next_frame) >= 0 && mips_in_frame_stub (pc))
1106 {
1107 pc = frame_unwind_register_signed
1108 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
1109 if (is_mips16_addr (pc))
1110 pc = unmake_mips16_addr (pc);
1111 if (mips_in_frame_stub (pc))
1112 {
1113 pc = frame_unwind_register_signed
1114 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
1115 if (is_mips16_addr (pc))
1116 pc = unmake_mips16_addr (pc);
1117 }
1118 }
930bd0e0 1119 return pc;
edfae063
AC
1120}
1121
30244cd8
UW
1122static CORE_ADDR
1123mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1124{
72a155b4
UW
1125 return frame_unwind_register_signed
1126 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
30244cd8
UW
1127}
1128
b8a22b94 1129/* Assuming THIS_FRAME is a dummy, return the frame ID of that
edfae063
AC
1130 dummy frame. The frame ID's base needs to match the TOS value
1131 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1132 breakpoint. */
1133
1134static struct frame_id
b8a22b94 1135mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
edfae063 1136{
f57d151a 1137 return frame_id_build
b8a22b94
DJ
1138 (get_frame_register_signed (this_frame,
1139 gdbarch_num_regs (gdbarch)
1140 + MIPS_SP_REGNUM),
1141 get_frame_pc (this_frame));
58dfe9ff
AC
1142}
1143
b6cb9035 1144static void
61a1198a 1145mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
b6cb9035 1146{
61a1198a 1147 int regnum = mips_regnum (get_regcache_arch (regcache))->pc;
930bd0e0
KB
1148 if (mips_pc_is_mips16 (pc))
1149 regcache_cooked_write_unsigned (regcache, regnum, make_mips16_addr (pc));
1150 else
1151 regcache_cooked_write_unsigned (regcache, regnum, pc);
6c997a34 1152}
c906108c 1153
c906108c
SS
1154/* Fetch and return instruction from the specified location. If the PC
1155 is odd, assume it's a MIPS16 instruction; otherwise MIPS32. */
1156
d37cca3d 1157static ULONGEST
e17a4113 1158mips_fetch_instruction (struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 1159{
e17a4113 1160 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
47a35522 1161 gdb_byte buf[MIPS_INSN32_SIZE];
c906108c
SS
1162 int instlen;
1163 int status;
1164
0fe7e7c8 1165 if (mips_pc_is_mips16 (addr))
c906108c 1166 {
95ac2dcf 1167 instlen = MIPS_INSN16_SIZE;
95404a3e 1168 addr = unmake_mips16_addr (addr);
c906108c
SS
1169 }
1170 else
95ac2dcf 1171 instlen = MIPS_INSN32_SIZE;
8defab1a 1172 status = target_read_memory (addr, buf, instlen);
c906108c
SS
1173 if (status)
1174 memory_error (status, addr);
e17a4113 1175 return extract_unsigned_integer (buf, instlen, byte_order);
c906108c
SS
1176}
1177
025bb325 1178/* These are the fields of 32 bit mips instructions. */
e135b889
DJ
1179#define mips32_op(x) (x >> 26)
1180#define itype_op(x) (x >> 26)
1181#define itype_rs(x) ((x >> 21) & 0x1f)
c906108c 1182#define itype_rt(x) ((x >> 16) & 0x1f)
e135b889 1183#define itype_immediate(x) (x & 0xffff)
c906108c 1184
e135b889
DJ
1185#define jtype_op(x) (x >> 26)
1186#define jtype_target(x) (x & 0x03ffffff)
c906108c 1187
e135b889
DJ
1188#define rtype_op(x) (x >> 26)
1189#define rtype_rs(x) ((x >> 21) & 0x1f)
1190#define rtype_rt(x) ((x >> 16) & 0x1f)
1191#define rtype_rd(x) ((x >> 11) & 0x1f)
1192#define rtype_shamt(x) ((x >> 6) & 0x1f)
1193#define rtype_funct(x) (x & 0x3f)
c906108c 1194
06987e64
MK
1195static LONGEST
1196mips32_relative_offset (ULONGEST inst)
c5aa993b 1197{
06987e64 1198 return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
c906108c
SS
1199}
1200
a385295e
MR
1201/* Determine the address of the next instruction executed after the INST
1202 floating condition branch instruction at PC. COUNT specifies the
1203 number of the floating condition bits tested by the branch. */
1204
1205static CORE_ADDR
1206mips32_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
1207 ULONGEST inst, CORE_ADDR pc, int count)
1208{
1209 int fcsr = mips_regnum (gdbarch)->fp_control_status;
1210 int cnum = (itype_rt (inst) >> 2) & (count - 1);
1211 int tf = itype_rt (inst) & 1;
1212 int mask = (1 << count) - 1;
1213 ULONGEST fcs;
1214 int cond;
1215
1216 if (fcsr == -1)
1217 /* No way to handle; it'll most likely trap anyway. */
1218 return pc;
1219
1220 fcs = get_frame_register_unsigned (frame, fcsr);
1221 cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
1222
1223 if (((cond >> cnum) & mask) != mask * !tf)
1224 pc += mips32_relative_offset (inst);
1225 else
1226 pc += 4;
1227
1228 return pc;
1229}
1230
f49e4e6d
MS
1231/* Determine where to set a single step breakpoint while considering
1232 branch prediction. */
5a89d8aa 1233static CORE_ADDR
0b1b3e42 1234mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
c5aa993b 1235{
e17a4113 1236 struct gdbarch *gdbarch = get_frame_arch (frame);
c5aa993b
JM
1237 unsigned long inst;
1238 int op;
e17a4113 1239 inst = mips_fetch_instruction (gdbarch, pc);
025bb325
MS
1240 if ((inst & 0xe0000000) != 0) /* Not a special, jump or branch
1241 instruction. */
c5aa993b 1242 {
e135b889 1243 if (itype_op (inst) >> 2 == 5)
6d82d43b 1244 /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
c5aa993b 1245 {
e135b889 1246 op = (itype_op (inst) & 0x03);
c906108c
SS
1247 switch (op)
1248 {
e135b889
DJ
1249 case 0: /* BEQL */
1250 goto equal_branch;
1251 case 1: /* BNEL */
1252 goto neq_branch;
1253 case 2: /* BLEZL */
1254 goto less_branch;
313628cc 1255 case 3: /* BGTZL */
e135b889 1256 goto greater_branch;
c5aa993b
JM
1257 default:
1258 pc += 4;
c906108c
SS
1259 }
1260 }
e135b889 1261 else if (itype_op (inst) == 17 && itype_rs (inst) == 8)
6d82d43b 1262 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
a385295e
MR
1263 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 1);
1264 else if (itype_op (inst) == 17 && itype_rs (inst) == 9
1265 && (itype_rt (inst) & 2) == 0)
1266 /* BC1ANY2F, BC1ANY2T: 010001 01001 xxx0x */
1267 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 2);
1268 else if (itype_op (inst) == 17 && itype_rs (inst) == 10
1269 && (itype_rt (inst) & 2) == 0)
1270 /* BC1ANY4F, BC1ANY4T: 010001 01010 xxx0x */
1271 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 4);
9e8da49c
MR
1272 else if (itype_op (inst) == 29)
1273 /* JALX: 011101 */
1274 /* The new PC will be alternate mode. */
1275 {
1276 unsigned long reg;
1277
1278 reg = jtype_target (inst) << 2;
1279 /* Add 1 to indicate 16-bit mode -- invert ISA mode. */
1280 pc = ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + reg + 1;
1281 }
c5aa993b 1282 else
025bb325 1283 pc += 4; /* Not a branch, next instruction is easy. */
c906108c
SS
1284 }
1285 else
025bb325 1286 { /* This gets way messy. */
c5aa993b 1287
025bb325
MS
1288 /* Further subdivide into SPECIAL, REGIMM and other. */
1289 switch (op = itype_op (inst) & 0x07) /* Extract bits 28,27,26. */
c906108c 1290 {
c5aa993b
JM
1291 case 0: /* SPECIAL */
1292 op = rtype_funct (inst);
1293 switch (op)
1294 {
1295 case 8: /* JR */
1296 case 9: /* JALR */
025bb325 1297 /* Set PC to that address. */
0b1b3e42 1298 pc = get_frame_register_signed (frame, rtype_rs (inst));
c5aa993b 1299 break;
e38d4e1a
DJ
1300 case 12: /* SYSCALL */
1301 {
1302 struct gdbarch_tdep *tdep;
1303
1304 tdep = gdbarch_tdep (get_frame_arch (frame));
1305 if (tdep->syscall_next_pc != NULL)
1306 pc = tdep->syscall_next_pc (frame);
1307 else
1308 pc += 4;
1309 }
1310 break;
c5aa993b
JM
1311 default:
1312 pc += 4;
1313 }
1314
6d82d43b 1315 break; /* end SPECIAL */
025bb325 1316 case 1: /* REGIMM */
c906108c 1317 {
e135b889
DJ
1318 op = itype_rt (inst); /* branch condition */
1319 switch (op)
c906108c 1320 {
c5aa993b 1321 case 0: /* BLTZ */
e135b889
DJ
1322 case 2: /* BLTZL */
1323 case 16: /* BLTZAL */
c5aa993b 1324 case 18: /* BLTZALL */
c906108c 1325 less_branch:
0b1b3e42 1326 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
c5aa993b
JM
1327 pc += mips32_relative_offset (inst) + 4;
1328 else
1329 pc += 8; /* after the delay slot */
1330 break;
e135b889 1331 case 1: /* BGEZ */
c5aa993b
JM
1332 case 3: /* BGEZL */
1333 case 17: /* BGEZAL */
1334 case 19: /* BGEZALL */
0b1b3e42 1335 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
c5aa993b
JM
1336 pc += mips32_relative_offset (inst) + 4;
1337 else
1338 pc += 8; /* after the delay slot */
1339 break;
a385295e
MR
1340 case 0x1c: /* BPOSGE32 */
1341 case 0x1e: /* BPOSGE64 */
1342 pc += 4;
1343 if (itype_rs (inst) == 0)
1344 {
1345 unsigned int pos = (op & 2) ? 64 : 32;
1346 int dspctl = mips_regnum (gdbarch)->dspctl;
1347
1348 if (dspctl == -1)
1349 /* No way to handle; it'll most likely trap anyway. */
1350 break;
1351
1352 if ((get_frame_register_unsigned (frame,
1353 dspctl) & 0x7f) >= pos)
1354 pc += mips32_relative_offset (inst);
1355 else
1356 pc += 4;
1357 }
1358 break;
e135b889 1359 /* All of the other instructions in the REGIMM category */
c5aa993b
JM
1360 default:
1361 pc += 4;
c906108c
SS
1362 }
1363 }
6d82d43b 1364 break; /* end REGIMM */
c5aa993b
JM
1365 case 2: /* J */
1366 case 3: /* JAL */
1367 {
1368 unsigned long reg;
1369 reg = jtype_target (inst) << 2;
025bb325 1370 /* Upper four bits get never changed... */
5b652102 1371 pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
c906108c 1372 }
c5aa993b 1373 break;
e135b889 1374 case 4: /* BEQ, BEQL */
c5aa993b 1375 equal_branch:
0b1b3e42
UW
1376 if (get_frame_register_signed (frame, itype_rs (inst)) ==
1377 get_frame_register_signed (frame, itype_rt (inst)))
c5aa993b
JM
1378 pc += mips32_relative_offset (inst) + 4;
1379 else
1380 pc += 8;
1381 break;
e135b889 1382 case 5: /* BNE, BNEL */
c5aa993b 1383 neq_branch:
0b1b3e42
UW
1384 if (get_frame_register_signed (frame, itype_rs (inst)) !=
1385 get_frame_register_signed (frame, itype_rt (inst)))
c5aa993b
JM
1386 pc += mips32_relative_offset (inst) + 4;
1387 else
1388 pc += 8;
1389 break;
e135b889 1390 case 6: /* BLEZ, BLEZL */
0b1b3e42 1391 if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
c5aa993b
JM
1392 pc += mips32_relative_offset (inst) + 4;
1393 else
1394 pc += 8;
1395 break;
1396 case 7:
e135b889
DJ
1397 default:
1398 greater_branch: /* BGTZ, BGTZL */
0b1b3e42 1399 if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
c5aa993b
JM
1400 pc += mips32_relative_offset (inst) + 4;
1401 else
1402 pc += 8;
1403 break;
c5aa993b
JM
1404 } /* switch */
1405 } /* else */
1406 return pc;
1407} /* mips32_next_pc */
c906108c
SS
1408
1409/* Decoding the next place to set a breakpoint is irregular for the
025bb325
MS
1410 mips 16 variant, but fortunately, there fewer instructions. We have
1411 to cope ith extensions for 16 bit instructions and a pair of actual
1412 32 bit instructions. We dont want to set a single step instruction
1413 on the extend instruction either. */
c906108c
SS
1414
1415/* Lots of mips16 instruction formats */
1416/* Predicting jumps requires itype,ritype,i8type
025bb325 1417 and their extensions extItype,extritype,extI8type. */
c906108c
SS
1418enum mips16_inst_fmts
1419{
c5aa993b
JM
1420 itype, /* 0 immediate 5,10 */
1421 ritype, /* 1 5,3,8 */
1422 rrtype, /* 2 5,3,3,5 */
1423 rritype, /* 3 5,3,3,5 */
1424 rrrtype, /* 4 5,3,3,3,2 */
1425 rriatype, /* 5 5,3,3,1,4 */
1426 shifttype, /* 6 5,3,3,3,2 */
1427 i8type, /* 7 5,3,8 */
1428 i8movtype, /* 8 5,3,3,5 */
1429 i8mov32rtype, /* 9 5,3,5,3 */
1430 i64type, /* 10 5,3,8 */
1431 ri64type, /* 11 5,3,3,5 */
1432 jalxtype, /* 12 5,1,5,5,16 - a 32 bit instruction */
1433 exiItype, /* 13 5,6,5,5,1,1,1,1,1,1,5 */
1434 extRitype, /* 14 5,6,5,5,3,1,1,1,5 */
1435 extRRItype, /* 15 5,5,5,5,3,3,5 */
1436 extRRIAtype, /* 16 5,7,4,5,3,3,1,4 */
1437 EXTshifttype, /* 17 5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
1438 extI8type, /* 18 5,6,5,5,3,1,1,1,5 */
1439 extI64type, /* 19 5,6,5,5,3,1,1,1,5 */
1440 extRi64type, /* 20 5,6,5,5,3,3,5 */
1441 extshift64type /* 21 5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
1442};
12f02c2a 1443/* I am heaping all the fields of the formats into one structure and
025bb325 1444 then, only the fields which are involved in instruction extension. */
c906108c 1445struct upk_mips16
6d82d43b
AC
1446{
1447 CORE_ADDR offset;
025bb325 1448 unsigned int regx; /* Function in i8 type. */
6d82d43b
AC
1449 unsigned int regy;
1450};
c906108c
SS
1451
1452
12f02c2a 1453/* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
c68cf8ad 1454 for the bits which make up the immediate extension. */
c906108c 1455
12f02c2a
AC
1456static CORE_ADDR
1457extended_offset (unsigned int extension)
c906108c 1458{
12f02c2a 1459 CORE_ADDR value;
130854df 1460
4c2051c6 1461 value = (extension >> 16) & 0x1f; /* Extract 15:11. */
c5aa993b 1462 value = value << 6;
4c2051c6 1463 value |= (extension >> 21) & 0x3f; /* Extract 10:5. */
c5aa993b 1464 value = value << 5;
130854df
MR
1465 value |= extension & 0x1f; /* Extract 4:0. */
1466
c5aa993b 1467 return value;
c906108c
SS
1468}
1469
1470/* Only call this function if you know that this is an extendable
bcf1ea1e
MR
1471 instruction. It won't malfunction, but why make excess remote memory
1472 references? If the immediate operands get sign extended or something,
1473 do it after the extension is performed. */
c906108c 1474/* FIXME: Every one of these cases needs to worry about sign extension
bcf1ea1e 1475 when the offset is to be used in relative addressing. */
c906108c 1476
12f02c2a 1477static unsigned int
e17a4113 1478fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
c906108c 1479{
e17a4113 1480 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
47a35522 1481 gdb_byte buf[8];
025bb325 1482 pc &= 0xfffffffe; /* Clear the low order bit. */
c5aa993b 1483 target_read_memory (pc, buf, 2);
e17a4113 1484 return extract_unsigned_integer (buf, 2, byte_order);
c906108c
SS
1485}
1486
1487static void
e17a4113 1488unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
12f02c2a
AC
1489 unsigned int extension,
1490 unsigned int inst,
6d82d43b 1491 enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
c906108c 1492{
12f02c2a
AC
1493 CORE_ADDR offset;
1494 int regx;
1495 int regy;
1496 switch (insn_format)
c906108c 1497 {
c5aa993b 1498 case itype:
c906108c 1499 {
12f02c2a
AC
1500 CORE_ADDR value;
1501 if (extension)
c5aa993b 1502 {
4c2051c6
MR
1503 value = extended_offset ((extension << 16) | inst);
1504 value = (value ^ 0x8000) - 0x8000; /* Sign-extend. */
c906108c
SS
1505 }
1506 else
c5aa993b 1507 {
12f02c2a 1508 value = inst & 0x7ff;
4c2051c6 1509 value = (value ^ 0x400) - 0x400; /* Sign-extend. */
c906108c 1510 }
12f02c2a
AC
1511 offset = value;
1512 regx = -1;
1513 regy = -1;
c906108c 1514 }
c5aa993b
JM
1515 break;
1516 case ritype:
1517 case i8type:
025bb325 1518 { /* A register identifier and an offset. */
c906108c 1519 /* Most of the fields are the same as I type but the
025bb325 1520 immediate value is of a different length. */
12f02c2a
AC
1521 CORE_ADDR value;
1522 if (extension)
c906108c 1523 {
4c2051c6
MR
1524 value = extended_offset ((extension << 16) | inst);
1525 value = (value ^ 0x8000) - 0x8000; /* Sign-extend. */
c906108c 1526 }
c5aa993b
JM
1527 else
1528 {
4c2051c6
MR
1529 value = inst & 0xff; /* 8 bits */
1530 value = (value ^ 0x80) - 0x80; /* Sign-extend. */
c5aa993b 1531 }
12f02c2a 1532 offset = value;
4c2051c6 1533 regx = (inst >> 8) & 0x07; /* i8 funct */
12f02c2a 1534 regy = -1;
c5aa993b 1535 break;
c906108c 1536 }
c5aa993b 1537 case jalxtype:
c906108c 1538 {
c5aa993b 1539 unsigned long value;
12f02c2a
AC
1540 unsigned int nexthalf;
1541 value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
c5aa993b 1542 value = value << 16;
025bb325
MS
1543 nexthalf = mips_fetch_instruction (gdbarch, pc + 2); /* low bit
1544 still set. */
c5aa993b 1545 value |= nexthalf;
12f02c2a
AC
1546 offset = value;
1547 regx = -1;
1548 regy = -1;
c5aa993b 1549 break;
c906108c
SS
1550 }
1551 default:
e2e0b3e5 1552 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c 1553 }
12f02c2a
AC
1554 upk->offset = offset;
1555 upk->regx = regx;
1556 upk->regy = regy;
c906108c
SS
1557}
1558
1559
c5aa993b
JM
1560static CORE_ADDR
1561add_offset_16 (CORE_ADDR pc, int offset)
c906108c 1562{
5b652102 1563 return ((offset << 2) | ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)));
c906108c
SS
1564}
1565
12f02c2a 1566static CORE_ADDR
0b1b3e42 1567extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
6d82d43b 1568 unsigned int extension, unsigned int insn)
c906108c 1569{
e17a4113 1570 struct gdbarch *gdbarch = get_frame_arch (frame);
12f02c2a
AC
1571 int op = (insn >> 11);
1572 switch (op)
c906108c 1573 {
6d82d43b 1574 case 2: /* Branch */
12f02c2a
AC
1575 {
1576 CORE_ADDR offset;
1577 struct upk_mips16 upk;
e17a4113 1578 unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
4c2051c6 1579 pc += (upk.offset << 1) + 2;
12f02c2a
AC
1580 break;
1581 }
025bb325
MS
1582 case 3: /* JAL , JALX - Watch out, these are 32 bit
1583 instructions. */
12f02c2a
AC
1584 {
1585 struct upk_mips16 upk;
e17a4113 1586 unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
12f02c2a
AC
1587 pc = add_offset_16 (pc, upk.offset);
1588 if ((insn >> 10) & 0x01) /* Exchange mode */
025bb325 1589 pc = pc & ~0x01; /* Clear low bit, indicate 32 bit mode. */
12f02c2a
AC
1590 else
1591 pc |= 0x01;
1592 break;
1593 }
6d82d43b 1594 case 4: /* beqz */
12f02c2a
AC
1595 {
1596 struct upk_mips16 upk;
1597 int reg;
e17a4113 1598 unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
4c2051c6 1599 reg = get_frame_register_signed (frame, mips16_to_32_reg[upk.regx]);
12f02c2a
AC
1600 if (reg == 0)
1601 pc += (upk.offset << 1) + 2;
1602 else
1603 pc += 2;
1604 break;
1605 }
6d82d43b 1606 case 5: /* bnez */
12f02c2a
AC
1607 {
1608 struct upk_mips16 upk;
1609 int reg;
e17a4113 1610 unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
4c2051c6 1611 reg = get_frame_register_signed (frame, mips16_to_32_reg[upk.regx]);
12f02c2a
AC
1612 if (reg != 0)
1613 pc += (upk.offset << 1) + 2;
1614 else
1615 pc += 2;
1616 break;
1617 }
6d82d43b 1618 case 12: /* I8 Formats btez btnez */
12f02c2a
AC
1619 {
1620 struct upk_mips16 upk;
1621 int reg;
e17a4113 1622 unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
12f02c2a 1623 /* upk.regx contains the opcode */
0b1b3e42 1624 reg = get_frame_register_signed (frame, 24); /* Test register is 24 */
12f02c2a
AC
1625 if (((upk.regx == 0) && (reg == 0)) /* BTEZ */
1626 || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
1627 /* pc = add_offset_16(pc,upk.offset) ; */
1628 pc += (upk.offset << 1) + 2;
1629 else
1630 pc += 2;
1631 break;
1632 }
6d82d43b 1633 case 29: /* RR Formats JR, JALR, JALR-RA */
12f02c2a
AC
1634 {
1635 struct upk_mips16 upk;
1636 /* upk.fmt = rrtype; */
1637 op = insn & 0x1f;
1638 if (op == 0)
c5aa993b 1639 {
12f02c2a
AC
1640 int reg;
1641 upk.regx = (insn >> 8) & 0x07;
1642 upk.regy = (insn >> 5) & 0x07;
4c2051c6
MR
1643 if ((upk.regy & 1) == 0)
1644 reg = mips16_to_32_reg[upk.regx];
1645 else
1646 reg = 31; /* Function return instruction. */
0b1b3e42 1647 pc = get_frame_register_signed (frame, reg);
c906108c 1648 }
12f02c2a 1649 else
c5aa993b 1650 pc += 2;
12f02c2a
AC
1651 break;
1652 }
1653 case 30:
1654 /* This is an instruction extension. Fetch the real instruction
1655 (which follows the extension) and decode things based on
025bb325 1656 that. */
12f02c2a
AC
1657 {
1658 pc += 2;
e17a4113
UW
1659 pc = extended_mips16_next_pc (frame, pc, insn,
1660 fetch_mips_16 (gdbarch, pc));
12f02c2a
AC
1661 break;
1662 }
1663 default:
1664 {
1665 pc += 2;
1666 break;
1667 }
c906108c 1668 }
c5aa993b 1669 return pc;
12f02c2a 1670}
c906108c 1671
5a89d8aa 1672static CORE_ADDR
0b1b3e42 1673mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
12f02c2a 1674{
e17a4113
UW
1675 struct gdbarch *gdbarch = get_frame_arch (frame);
1676 unsigned int insn = fetch_mips_16 (gdbarch, pc);
0b1b3e42 1677 return extended_mips16_next_pc (frame, pc, 0, insn);
12f02c2a
AC
1678}
1679
1680/* The mips_next_pc function supports single_step when the remote
7e73cedf 1681 target monitor or stub is not developed enough to do a single_step.
12f02c2a 1682 It works by decoding the current instruction and predicting where a
025bb325 1683 branch will go. This isnt hard because all the data is available.
ce1f96de 1684 The MIPS32 and MIPS16 variants are quite different. */
ad527d2e 1685static CORE_ADDR
0b1b3e42 1686mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
c906108c 1687{
91912e4d 1688 if (mips_pc_is_mips16 (pc))
0b1b3e42 1689 return mips16_next_pc (frame, pc);
c5aa993b 1690 else
0b1b3e42 1691 return mips32_next_pc (frame, pc);
12f02c2a 1692}
c906108c 1693
edfae063
AC
1694struct mips_frame_cache
1695{
1696 CORE_ADDR base;
1697 struct trad_frame_saved_reg *saved_regs;
1698};
1699
29639122
JB
1700/* Set a register's saved stack address in temp_saved_regs. If an
1701 address has already been set for this register, do nothing; this
1702 way we will only recognize the first save of a given register in a
1703 function prologue.
eec63939 1704
f57d151a
UW
1705 For simplicity, save the address in both [0 .. gdbarch_num_regs) and
1706 [gdbarch_num_regs .. 2*gdbarch_num_regs).
1707 Strictly speaking, only the second range is used as it is only second
1708 range (the ABI instead of ISA registers) that comes into play when finding
1709 saved registers in a frame. */
eec63939
AC
1710
1711static void
74ed0bb4
MD
1712set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
1713 int regnum, CORE_ADDR offset)
eec63939 1714{
29639122
JB
1715 if (this_cache != NULL
1716 && this_cache->saved_regs[regnum].addr == -1)
1717 {
74ed0bb4
MD
1718 this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
1719 = offset;
1720 this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
1721 = offset;
29639122 1722 }
eec63939
AC
1723}
1724
eec63939 1725
29639122
JB
1726/* Fetch the immediate value from a MIPS16 instruction.
1727 If the previous instruction was an EXTEND, use it to extend
1728 the upper bits of the immediate value. This is a helper function
1729 for mips16_scan_prologue. */
eec63939 1730
29639122
JB
1731static int
1732mips16_get_imm (unsigned short prev_inst, /* previous instruction */
1733 unsigned short inst, /* current instruction */
1734 int nbits, /* number of bits in imm field */
1735 int scale, /* scale factor to be applied to imm */
025bb325 1736 int is_signed) /* is the imm field signed? */
eec63939 1737{
29639122 1738 int offset;
eec63939 1739
29639122
JB
1740 if ((prev_inst & 0xf800) == 0xf000) /* prev instruction was EXTEND? */
1741 {
1742 offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
1743 if (offset & 0x8000) /* check for negative extend */
1744 offset = 0 - (0x10000 - (offset & 0xffff));
1745 return offset | (inst & 0x1f);
1746 }
eec63939 1747 else
29639122
JB
1748 {
1749 int max_imm = 1 << nbits;
1750 int mask = max_imm - 1;
1751 int sign_bit = max_imm >> 1;
45c9dd44 1752
29639122
JB
1753 offset = inst & mask;
1754 if (is_signed && (offset & sign_bit))
1755 offset = 0 - (max_imm - offset);
1756 return offset * scale;
1757 }
1758}
eec63939 1759
65596487 1760
29639122
JB
1761/* Analyze the function prologue from START_PC to LIMIT_PC. Builds
1762 the associated FRAME_CACHE if not null.
1763 Return the address of the first instruction past the prologue. */
eec63939 1764
29639122 1765static CORE_ADDR
e17a4113
UW
1766mips16_scan_prologue (struct gdbarch *gdbarch,
1767 CORE_ADDR start_pc, CORE_ADDR limit_pc,
b8a22b94 1768 struct frame_info *this_frame,
29639122
JB
1769 struct mips_frame_cache *this_cache)
1770{
1771 CORE_ADDR cur_pc;
025bb325 1772 CORE_ADDR frame_addr = 0; /* Value of $r17, used as frame pointer. */
29639122
JB
1773 CORE_ADDR sp;
1774 long frame_offset = 0; /* Size of stack frame. */
1775 long frame_adjust = 0; /* Offset of FP from SP. */
1776 int frame_reg = MIPS_SP_REGNUM;
025bb325 1777 unsigned short prev_inst = 0; /* saved copy of previous instruction. */
29639122
JB
1778 unsigned inst = 0; /* current instruction */
1779 unsigned entry_inst = 0; /* the entry instruction */
2207132d 1780 unsigned save_inst = 0; /* the save instruction */
29639122 1781 int reg, offset;
a343eb3c 1782
29639122
JB
1783 int extend_bytes = 0;
1784 int prev_extend_bytes;
1785 CORE_ADDR end_prologue_addr = 0;
a343eb3c 1786
29639122 1787 /* Can be called when there's no process, and hence when there's no
b8a22b94
DJ
1788 THIS_FRAME. */
1789 if (this_frame != NULL)
1790 sp = get_frame_register_signed (this_frame,
1791 gdbarch_num_regs (gdbarch)
1792 + MIPS_SP_REGNUM);
29639122
JB
1793 else
1794 sp = 0;
eec63939 1795
29639122
JB
1796 if (limit_pc > start_pc + 200)
1797 limit_pc = start_pc + 200;
eec63939 1798
95ac2dcf 1799 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
29639122
JB
1800 {
1801 /* Save the previous instruction. If it's an EXTEND, we'll extract
1802 the immediate offset extension from it in mips16_get_imm. */
1803 prev_inst = inst;
eec63939 1804
025bb325 1805 /* Fetch and decode the instruction. */
e17a4113 1806 inst = (unsigned short) mips_fetch_instruction (gdbarch, cur_pc);
eec63939 1807
29639122
JB
1808 /* Normally we ignore extend instructions. However, if it is
1809 not followed by a valid prologue instruction, then this
1810 instruction is not part of the prologue either. We must
1811 remember in this case to adjust the end_prologue_addr back
1812 over the extend. */
1813 if ((inst & 0xf800) == 0xf000) /* extend */
1814 {
95ac2dcf 1815 extend_bytes = MIPS_INSN16_SIZE;
29639122
JB
1816 continue;
1817 }
eec63939 1818
29639122
JB
1819 prev_extend_bytes = extend_bytes;
1820 extend_bytes = 0;
eec63939 1821
29639122
JB
1822 if ((inst & 0xff00) == 0x6300 /* addiu sp */
1823 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
1824 {
1825 offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
025bb325 1826 if (offset < 0) /* Negative stack adjustment? */
29639122
JB
1827 frame_offset -= offset;
1828 else
1829 /* Exit loop if a positive stack adjustment is found, which
1830 usually means that the stack cleanup code in the function
1831 epilogue is reached. */
1832 break;
1833 }
1834 else if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
1835 {
1836 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1837 reg = mips16_to_32_reg[(inst & 0x700) >> 8];
74ed0bb4 1838 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
29639122
JB
1839 }
1840 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
1841 {
1842 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1843 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
74ed0bb4 1844 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
29639122
JB
1845 }
1846 else if ((inst & 0xff00) == 0x6200) /* sw $ra,n($sp) */
1847 {
1848 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
74ed0bb4 1849 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
29639122
JB
1850 }
1851 else if ((inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
1852 {
1853 offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
74ed0bb4 1854 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
29639122
JB
1855 }
1856 else if (inst == 0x673d) /* move $s1, $sp */
1857 {
1858 frame_addr = sp;
1859 frame_reg = 17;
1860 }
1861 else if ((inst & 0xff00) == 0x0100) /* addiu $s1,sp,n */
1862 {
1863 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1864 frame_addr = sp + offset;
1865 frame_reg = 17;
1866 frame_adjust = offset;
1867 }
1868 else if ((inst & 0xFF00) == 0xd900) /* sw reg,offset($s1) */
1869 {
1870 offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
1871 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
74ed0bb4 1872 set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
29639122
JB
1873 }
1874 else if ((inst & 0xFF00) == 0x7900) /* sd reg,offset($s1) */
1875 {
1876 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1877 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
74ed0bb4 1878 set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
29639122
JB
1879 }
1880 else if ((inst & 0xf81f) == 0xe809
1881 && (inst & 0x700) != 0x700) /* entry */
025bb325 1882 entry_inst = inst; /* Save for later processing. */
2207132d
MR
1883 else if ((inst & 0xff80) == 0x6480) /* save */
1884 {
025bb325 1885 save_inst = inst; /* Save for later processing. */
2207132d
MR
1886 if (prev_extend_bytes) /* extend */
1887 save_inst |= prev_inst << 16;
1888 }
29639122 1889 else if ((inst & 0xf800) == 0x1800) /* jal(x) */
95ac2dcf 1890 cur_pc += MIPS_INSN16_SIZE; /* 32-bit instruction */
29639122
JB
1891 else if ((inst & 0xff1c) == 0x6704) /* move reg,$a0-$a3 */
1892 {
1893 /* This instruction is part of the prologue, but we don't
1894 need to do anything special to handle it. */
1895 }
1896 else
1897 {
1898 /* This instruction is not an instruction typically found
1899 in a prologue, so we must have reached the end of the
1900 prologue. */
1901 if (end_prologue_addr == 0)
1902 end_prologue_addr = cur_pc - prev_extend_bytes;
1903 }
1904 }
eec63939 1905
29639122
JB
1906 /* The entry instruction is typically the first instruction in a function,
1907 and it stores registers at offsets relative to the value of the old SP
1908 (before the prologue). But the value of the sp parameter to this
1909 function is the new SP (after the prologue has been executed). So we
1910 can't calculate those offsets until we've seen the entire prologue,
025bb325 1911 and can calculate what the old SP must have been. */
29639122
JB
1912 if (entry_inst != 0)
1913 {
1914 int areg_count = (entry_inst >> 8) & 7;
1915 int sreg_count = (entry_inst >> 6) & 3;
eec63939 1916
29639122
JB
1917 /* The entry instruction always subtracts 32 from the SP. */
1918 frame_offset += 32;
1919
1920 /* Now we can calculate what the SP must have been at the
1921 start of the function prologue. */
1922 sp += frame_offset;
1923
1924 /* Check if a0-a3 were saved in the caller's argument save area. */
1925 for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
1926 {
74ed0bb4 1927 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
72a155b4 1928 offset += mips_abi_regsize (gdbarch);
29639122
JB
1929 }
1930
1931 /* Check if the ra register was pushed on the stack. */
1932 offset = -4;
1933 if (entry_inst & 0x20)
1934 {
74ed0bb4 1935 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
72a155b4 1936 offset -= mips_abi_regsize (gdbarch);
29639122
JB
1937 }
1938
1939 /* Check if the s0 and s1 registers were pushed on the stack. */
1940 for (reg = 16; reg < sreg_count + 16; reg++)
1941 {
74ed0bb4 1942 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
72a155b4 1943 offset -= mips_abi_regsize (gdbarch);
29639122
JB
1944 }
1945 }
1946
2207132d
MR
1947 /* The SAVE instruction is similar to ENTRY, except that defined by the
1948 MIPS16e ASE of the MIPS Architecture. Unlike with ENTRY though, the
1949 size of the frame is specified as an immediate field of instruction
1950 and an extended variation exists which lets additional registers and
1951 frame space to be specified. The instruction always treats registers
1952 as 32-bit so its usefulness for 64-bit ABIs is questionable. */
1953 if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
1954 {
1955 static int args_table[16] = {
1956 0, 0, 0, 0, 1, 1, 1, 1,
1957 2, 2, 2, 0, 3, 3, 4, -1,
1958 };
1959 static int astatic_table[16] = {
1960 0, 1, 2, 3, 0, 1, 2, 3,
1961 0, 1, 2, 4, 0, 1, 0, -1,
1962 };
1963 int aregs = (save_inst >> 16) & 0xf;
1964 int xsregs = (save_inst >> 24) & 0x7;
1965 int args = args_table[aregs];
1966 int astatic = astatic_table[aregs];
1967 long frame_size;
1968
1969 if (args < 0)
1970 {
1971 warning (_("Invalid number of argument registers encoded in SAVE."));
1972 args = 0;
1973 }
1974 if (astatic < 0)
1975 {
1976 warning (_("Invalid number of static registers encoded in SAVE."));
1977 astatic = 0;
1978 }
1979
1980 /* For standard SAVE the frame size of 0 means 128. */
1981 frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
1982 if (frame_size == 0 && (save_inst >> 16) == 0)
1983 frame_size = 16;
1984 frame_size *= 8;
1985 frame_offset += frame_size;
1986
1987 /* Now we can calculate what the SP must have been at the
1988 start of the function prologue. */
1989 sp += frame_offset;
1990
1991 /* Check if A0-A3 were saved in the caller's argument save area. */
1992 for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
1993 {
74ed0bb4 1994 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2207132d
MR
1995 offset += mips_abi_regsize (gdbarch);
1996 }
1997
1998 offset = -4;
1999
2000 /* Check if the RA register was pushed on the stack. */
2001 if (save_inst & 0x40)
2002 {
74ed0bb4 2003 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2207132d
MR
2004 offset -= mips_abi_regsize (gdbarch);
2005 }
2006
2007 /* Check if the S8 register was pushed on the stack. */
2008 if (xsregs > 6)
2009 {
74ed0bb4 2010 set_reg_offset (gdbarch, this_cache, 30, sp + offset);
2207132d
MR
2011 offset -= mips_abi_regsize (gdbarch);
2012 xsregs--;
2013 }
2014 /* Check if S2-S7 were pushed on the stack. */
2015 for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
2016 {
74ed0bb4 2017 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2207132d
MR
2018 offset -= mips_abi_regsize (gdbarch);
2019 }
2020
2021 /* Check if the S1 register was pushed on the stack. */
2022 if (save_inst & 0x10)
2023 {
74ed0bb4 2024 set_reg_offset (gdbarch, this_cache, 17, sp + offset);
2207132d
MR
2025 offset -= mips_abi_regsize (gdbarch);
2026 }
2027 /* Check if the S0 register was pushed on the stack. */
2028 if (save_inst & 0x20)
2029 {
74ed0bb4 2030 set_reg_offset (gdbarch, this_cache, 16, sp + offset);
2207132d
MR
2031 offset -= mips_abi_regsize (gdbarch);
2032 }
2033
2034 /* Check if A0-A3 were pushed on the stack. */
2035 for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
2036 {
74ed0bb4 2037 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2207132d
MR
2038 offset -= mips_abi_regsize (gdbarch);
2039 }
2040 }
2041
29639122
JB
2042 if (this_cache != NULL)
2043 {
2044 this_cache->base =
b8a22b94
DJ
2045 (get_frame_register_signed (this_frame,
2046 gdbarch_num_regs (gdbarch) + frame_reg)
29639122
JB
2047 + frame_offset - frame_adjust);
2048 /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
025bb325 2049 be able to get rid of the assignment below, evetually. But it's
29639122 2050 still needed for now. */
72a155b4
UW
2051 this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2052 + mips_regnum (gdbarch)->pc]
2053 = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
29639122
JB
2054 }
2055
2056 /* If we didn't reach the end of the prologue when scanning the function
2057 instructions, then set end_prologue_addr to the address of the
2058 instruction immediately after the last one we scanned. */
2059 if (end_prologue_addr == 0)
2060 end_prologue_addr = cur_pc;
2061
2062 return end_prologue_addr;
eec63939
AC
2063}
2064
29639122
JB
2065/* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
2066 Procedures that use the 32-bit instruction set are handled by the
2067 mips_insn32 unwinder. */
2068
2069static struct mips_frame_cache *
b8a22b94 2070mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
eec63939 2071{
e17a4113 2072 struct gdbarch *gdbarch = get_frame_arch (this_frame);
29639122 2073 struct mips_frame_cache *cache;
eec63939
AC
2074
2075 if ((*this_cache) != NULL)
2076 return (*this_cache);
29639122
JB
2077 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
2078 (*this_cache) = cache;
b8a22b94 2079 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
eec63939 2080
29639122
JB
2081 /* Analyze the function prologue. */
2082 {
b8a22b94 2083 const CORE_ADDR pc = get_frame_address_in_block (this_frame);
29639122 2084 CORE_ADDR start_addr;
eec63939 2085
29639122
JB
2086 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2087 if (start_addr == 0)
e17a4113 2088 start_addr = heuristic_proc_start (gdbarch, pc);
29639122
JB
2089 /* We can't analyze the prologue if we couldn't find the begining
2090 of the function. */
2091 if (start_addr == 0)
2092 return cache;
eec63939 2093
e17a4113 2094 mips16_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
29639122
JB
2095 }
2096
3e8c568d 2097 /* gdbarch_sp_regnum contains the value and not the address. */
72a155b4 2098 trad_frame_set_value (cache->saved_regs,
e17a4113 2099 gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
72a155b4 2100 cache->base);
eec63939 2101
29639122 2102 return (*this_cache);
eec63939
AC
2103}
2104
2105static void
b8a22b94 2106mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
29639122 2107 struct frame_id *this_id)
eec63939 2108{
b8a22b94 2109 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
29639122 2110 this_cache);
21327321
DJ
2111 /* This marks the outermost frame. */
2112 if (info->base == 0)
2113 return;
b8a22b94 2114 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
eec63939
AC
2115}
2116
b8a22b94
DJ
2117static struct value *
2118mips_insn16_frame_prev_register (struct frame_info *this_frame,
2119 void **this_cache, int regnum)
eec63939 2120{
b8a22b94 2121 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
29639122 2122 this_cache);
b8a22b94
DJ
2123 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2124}
2125
2126static int
2127mips_insn16_frame_sniffer (const struct frame_unwind *self,
2128 struct frame_info *this_frame, void **this_cache)
2129{
2130 CORE_ADDR pc = get_frame_pc (this_frame);
2131 if (mips_pc_is_mips16 (pc))
2132 return 1;
2133 return 0;
eec63939
AC
2134}
2135
29639122 2136static const struct frame_unwind mips_insn16_frame_unwind =
eec63939
AC
2137{
2138 NORMAL_FRAME,
8fbca658 2139 default_frame_unwind_stop_reason,
29639122 2140 mips_insn16_frame_this_id,
b8a22b94
DJ
2141 mips_insn16_frame_prev_register,
2142 NULL,
2143 mips_insn16_frame_sniffer
eec63939
AC
2144};
2145
eec63939 2146static CORE_ADDR
b8a22b94 2147mips_insn16_frame_base_address (struct frame_info *this_frame,
29639122 2148 void **this_cache)
eec63939 2149{
b8a22b94 2150 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
29639122
JB
2151 this_cache);
2152 return info->base;
eec63939
AC
2153}
2154
29639122 2155static const struct frame_base mips_insn16_frame_base =
eec63939 2156{
29639122
JB
2157 &mips_insn16_frame_unwind,
2158 mips_insn16_frame_base_address,
2159 mips_insn16_frame_base_address,
2160 mips_insn16_frame_base_address
eec63939
AC
2161};
2162
2163static const struct frame_base *
b8a22b94 2164mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
eec63939 2165{
b8a22b94
DJ
2166 CORE_ADDR pc = get_frame_pc (this_frame);
2167 if (mips_pc_is_mips16 (pc))
29639122 2168 return &mips_insn16_frame_base;
eec63939
AC
2169 else
2170 return NULL;
edfae063
AC
2171}
2172
29639122
JB
2173/* Mark all the registers as unset in the saved_regs array
2174 of THIS_CACHE. Do nothing if THIS_CACHE is null. */
2175
74ed0bb4
MD
2176static void
2177reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
c906108c 2178{
29639122
JB
2179 if (this_cache == NULL || this_cache->saved_regs == NULL)
2180 return;
2181
2182 {
74ed0bb4 2183 const int num_regs = gdbarch_num_regs (gdbarch);
29639122 2184 int i;
64159455 2185
29639122
JB
2186 for (i = 0; i < num_regs; i++)
2187 {
2188 this_cache->saved_regs[i].addr = -1;
2189 }
2190 }
c906108c
SS
2191}
2192
025bb325 2193/* Analyze the function prologue from START_PC to LIMIT_PC. Builds
29639122
JB
2194 the associated FRAME_CACHE if not null.
2195 Return the address of the first instruction past the prologue. */
c906108c 2196
875e1767 2197static CORE_ADDR
e17a4113
UW
2198mips32_scan_prologue (struct gdbarch *gdbarch,
2199 CORE_ADDR start_pc, CORE_ADDR limit_pc,
b8a22b94 2200 struct frame_info *this_frame,
29639122 2201 struct mips_frame_cache *this_cache)
c906108c 2202{
29639122 2203 CORE_ADDR cur_pc;
025bb325
MS
2204 CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
2205 frame-pointer. */
29639122
JB
2206 CORE_ADDR sp;
2207 long frame_offset;
2208 int frame_reg = MIPS_SP_REGNUM;
8fa9cfa1 2209
29639122
JB
2210 CORE_ADDR end_prologue_addr = 0;
2211 int seen_sp_adjust = 0;
2212 int load_immediate_bytes = 0;
db5f024e 2213 int in_delay_slot = 0;
7d1e6fb8 2214 int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);
8fa9cfa1 2215
29639122 2216 /* Can be called when there's no process, and hence when there's no
b8a22b94
DJ
2217 THIS_FRAME. */
2218 if (this_frame != NULL)
2219 sp = get_frame_register_signed (this_frame,
2220 gdbarch_num_regs (gdbarch)
2221 + MIPS_SP_REGNUM);
8fa9cfa1 2222 else
29639122 2223 sp = 0;
9022177c 2224
29639122
JB
2225 if (limit_pc > start_pc + 200)
2226 limit_pc = start_pc + 200;
9022177c 2227
29639122 2228restart:
9022177c 2229
29639122 2230 frame_offset = 0;
95ac2dcf 2231 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
9022177c 2232 {
29639122
JB
2233 unsigned long inst, high_word, low_word;
2234 int reg;
9022177c 2235
025bb325 2236 /* Fetch the instruction. */
e17a4113 2237 inst = (unsigned long) mips_fetch_instruction (gdbarch, cur_pc);
9022177c 2238
29639122
JB
2239 /* Save some code by pre-extracting some useful fields. */
2240 high_word = (inst >> 16) & 0xffff;
2241 low_word = inst & 0xffff;
2242 reg = high_word & 0x1f;
fe29b929 2243
025bb325 2244 if (high_word == 0x27bd /* addiu $sp,$sp,-i */
29639122
JB
2245 || high_word == 0x23bd /* addi $sp,$sp,-i */
2246 || high_word == 0x67bd) /* daddiu $sp,$sp,-i */
2247 {
025bb325 2248 if (low_word & 0x8000) /* Negative stack adjustment? */
29639122
JB
2249 frame_offset += 0x10000 - low_word;
2250 else
2251 /* Exit loop if a positive stack adjustment is found, which
2252 usually means that the stack cleanup code in the function
2253 epilogue is reached. */
2254 break;
2255 seen_sp_adjust = 1;
2256 }
7d1e6fb8
KB
2257 else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
2258 && !regsize_is_64_bits)
29639122 2259 {
74ed0bb4 2260 set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
29639122 2261 }
7d1e6fb8
KB
2262 else if (((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
2263 && regsize_is_64_bits)
29639122
JB
2264 {
2265 /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra. */
74ed0bb4 2266 set_reg_offset (gdbarch, this_cache, reg, sp + low_word);
29639122
JB
2267 }
2268 else if (high_word == 0x27be) /* addiu $30,$sp,size */
2269 {
2270 /* Old gcc frame, r30 is virtual frame pointer. */
2271 if ((long) low_word != frame_offset)
2272 frame_addr = sp + low_word;
b8a22b94 2273 else if (this_frame && frame_reg == MIPS_SP_REGNUM)
29639122
JB
2274 {
2275 unsigned alloca_adjust;
a4b8ebc8 2276
29639122 2277 frame_reg = 30;
b8a22b94
DJ
2278 frame_addr = get_frame_register_signed
2279 (this_frame, gdbarch_num_regs (gdbarch) + 30);
d2ca4222 2280
29639122
JB
2281 alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
2282 if (alloca_adjust > 0)
2283 {
025bb325 2284 /* FP > SP + frame_size. This may be because of
29639122
JB
2285 an alloca or somethings similar. Fix sp to
2286 "pre-alloca" value, and try again. */
2287 sp += alloca_adjust;
2288 /* Need to reset the status of all registers. Otherwise,
2289 we will hit a guard that prevents the new address
2290 for each register to be recomputed during the second
2291 pass. */
74ed0bb4 2292 reset_saved_regs (gdbarch, this_cache);
29639122
JB
2293 goto restart;
2294 }
2295 }
2296 }
2297 /* move $30,$sp. With different versions of gas this will be either
2298 `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
2299 Accept any one of these. */
2300 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
2301 {
2302 /* New gcc frame, virtual frame pointer is at r30 + frame_size. */
b8a22b94 2303 if (this_frame && frame_reg == MIPS_SP_REGNUM)
29639122
JB
2304 {
2305 unsigned alloca_adjust;
c906108c 2306
29639122 2307 frame_reg = 30;
b8a22b94
DJ
2308 frame_addr = get_frame_register_signed
2309 (this_frame, gdbarch_num_regs (gdbarch) + 30);
d2ca4222 2310
29639122
JB
2311 alloca_adjust = (unsigned) (frame_addr - sp);
2312 if (alloca_adjust > 0)
2313 {
025bb325 2314 /* FP > SP + frame_size. This may be because of
29639122
JB
2315 an alloca or somethings similar. Fix sp to
2316 "pre-alloca" value, and try again. */
2317 sp = frame_addr;
2318 /* Need to reset the status of all registers. Otherwise,
2319 we will hit a guard that prevents the new address
2320 for each register to be recomputed during the second
2321 pass. */
74ed0bb4 2322 reset_saved_regs (gdbarch, this_cache);
29639122
JB
2323 goto restart;
2324 }
2325 }
2326 }
7d1e6fb8
KB
2327 else if ((high_word & 0xFFE0) == 0xafc0 /* sw reg,offset($30) */
2328 && !regsize_is_64_bits)
29639122 2329 {
74ed0bb4 2330 set_reg_offset (gdbarch, this_cache, reg, frame_addr + low_word);
29639122
JB
2331 }
2332 else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
2333 || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
2334 || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
2335 || high_word == 0x3c1c /* lui $gp,n */
2336 || high_word == 0x279c /* addiu $gp,$gp,n */
2337 || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
2338 || inst == 0x033ce021 /* addu $gp,$t9,$gp */
2339 )
19080931
MR
2340 {
2341 /* These instructions are part of the prologue, but we don't
2342 need to do anything special to handle them. */
2343 }
29639122
JB
2344 /* The instructions below load $at or $t0 with an immediate
2345 value in preparation for a stack adjustment via
025bb325 2346 subu $sp,$sp,[$at,$t0]. These instructions could also
29639122
JB
2347 initialize a local variable, so we accept them only before
2348 a stack adjustment instruction was seen. */
2349 else if (!seen_sp_adjust
19080931
MR
2350 && (high_word == 0x3c01 /* lui $at,n */
2351 || high_word == 0x3c08 /* lui $t0,n */
2352 || high_word == 0x3421 /* ori $at,$at,n */
2353 || high_word == 0x3508 /* ori $t0,$t0,n */
2354 || high_word == 0x3401 /* ori $at,$zero,n */
2355 || high_word == 0x3408 /* ori $t0,$zero,n */
2356 ))
2357 {
2358 if (end_prologue_addr == 0)
2359 load_immediate_bytes += MIPS_INSN32_SIZE; /* FIXME! */
2360 }
29639122 2361 else
19080931
MR
2362 {
2363 /* This instruction is not an instruction typically found
2364 in a prologue, so we must have reached the end of the
2365 prologue. */
2366 /* FIXME: brobecker/2004-10-10: Can't we just break out of this
2367 loop now? Why would we need to continue scanning the function
2368 instructions? */
2369 if (end_prologue_addr == 0)
2370 end_prologue_addr = cur_pc;
2371
2372 /* Check for branches and jumps. For now, only jump to
2373 register are caught (i.e. returns). */
2374 if ((itype_op (inst) & 0x07) == 0 && rtype_funct (inst) == 8)
2375 in_delay_slot = 1;
2376 }
db5f024e
DJ
2377
2378 /* If the previous instruction was a jump, we must have reached
2379 the end of the prologue by now. Stop scanning so that we do
2380 not go past the function return. */
2381 if (in_delay_slot)
2382 break;
a4b8ebc8 2383 }
c906108c 2384
29639122
JB
2385 if (this_cache != NULL)
2386 {
2387 this_cache->base =
b8a22b94
DJ
2388 (get_frame_register_signed (this_frame,
2389 gdbarch_num_regs (gdbarch) + frame_reg)
29639122
JB
2390 + frame_offset);
2391 /* FIXME: brobecker/2004-09-15: We should be able to get rid of
2392 this assignment below, eventually. But it's still needed
2393 for now. */
72a155b4
UW
2394 this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2395 + mips_regnum (gdbarch)->pc]
2396 = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
f57d151a 2397 + MIPS_RA_REGNUM];
29639122 2398 }
c906108c 2399
29639122
JB
2400 /* If we didn't reach the end of the prologue when scanning the function
2401 instructions, then set end_prologue_addr to the address of the
2402 instruction immediately after the last one we scanned. */
2403 /* brobecker/2004-10-10: I don't think this would ever happen, but
2404 we may as well be careful and do our best if we have a null
2405 end_prologue_addr. */
2406 if (end_prologue_addr == 0)
2407 end_prologue_addr = cur_pc;
2408
2409 /* In a frameless function, we might have incorrectly
025bb325 2410 skipped some load immediate instructions. Undo the skipping
29639122
JB
2411 if the load immediate was not followed by a stack adjustment. */
2412 if (load_immediate_bytes && !seen_sp_adjust)
2413 end_prologue_addr -= load_immediate_bytes;
c906108c 2414
29639122 2415 return end_prologue_addr;
c906108c
SS
2416}
2417
29639122
JB
2418/* Heuristic unwinder for procedures using 32-bit instructions (covers
2419 both 32-bit and 64-bit MIPS ISAs). Procedures using 16-bit
2420 instructions (a.k.a. MIPS16) are handled by the mips_insn16
2421 unwinder. */
c906108c 2422
29639122 2423static struct mips_frame_cache *
b8a22b94 2424mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
c906108c 2425{
e17a4113 2426 struct gdbarch *gdbarch = get_frame_arch (this_frame);
29639122 2427 struct mips_frame_cache *cache;
c906108c 2428
29639122
JB
2429 if ((*this_cache) != NULL)
2430 return (*this_cache);
c5aa993b 2431
29639122
JB
2432 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
2433 (*this_cache) = cache;
b8a22b94 2434 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
c5aa993b 2435
29639122
JB
2436 /* Analyze the function prologue. */
2437 {
b8a22b94 2438 const CORE_ADDR pc = get_frame_address_in_block (this_frame);
29639122 2439 CORE_ADDR start_addr;
c906108c 2440
29639122
JB
2441 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2442 if (start_addr == 0)
e17a4113 2443 start_addr = heuristic_proc_start (gdbarch, pc);
29639122
JB
2444 /* We can't analyze the prologue if we couldn't find the begining
2445 of the function. */
2446 if (start_addr == 0)
2447 return cache;
c5aa993b 2448
e17a4113 2449 mips32_scan_prologue (gdbarch, start_addr, pc, this_frame, *this_cache);
29639122
JB
2450 }
2451
3e8c568d 2452 /* gdbarch_sp_regnum contains the value and not the address. */
f57d151a 2453 trad_frame_set_value (cache->saved_regs,
e17a4113 2454 gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
f57d151a 2455 cache->base);
c5aa993b 2456
29639122 2457 return (*this_cache);
c906108c
SS
2458}
2459
29639122 2460static void
b8a22b94 2461mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
29639122 2462 struct frame_id *this_id)
c906108c 2463{
b8a22b94 2464 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
29639122 2465 this_cache);
21327321
DJ
2466 /* This marks the outermost frame. */
2467 if (info->base == 0)
2468 return;
b8a22b94 2469 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
29639122 2470}
c906108c 2471
b8a22b94
DJ
2472static struct value *
2473mips_insn32_frame_prev_register (struct frame_info *this_frame,
2474 void **this_cache, int regnum)
29639122 2475{
b8a22b94 2476 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
29639122 2477 this_cache);
b8a22b94
DJ
2478 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2479}
2480
2481static int
2482mips_insn32_frame_sniffer (const struct frame_unwind *self,
2483 struct frame_info *this_frame, void **this_cache)
2484{
2485 CORE_ADDR pc = get_frame_pc (this_frame);
2486 if (! mips_pc_is_mips16 (pc))
2487 return 1;
2488 return 0;
c906108c
SS
2489}
2490
29639122
JB
2491static const struct frame_unwind mips_insn32_frame_unwind =
2492{
2493 NORMAL_FRAME,
8fbca658 2494 default_frame_unwind_stop_reason,
29639122 2495 mips_insn32_frame_this_id,
b8a22b94
DJ
2496 mips_insn32_frame_prev_register,
2497 NULL,
2498 mips_insn32_frame_sniffer
29639122 2499};
c906108c 2500
1c645fec 2501static CORE_ADDR
b8a22b94 2502mips_insn32_frame_base_address (struct frame_info *this_frame,
29639122 2503 void **this_cache)
c906108c 2504{
b8a22b94 2505 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
29639122
JB
2506 this_cache);
2507 return info->base;
2508}
c906108c 2509
29639122
JB
2510static const struct frame_base mips_insn32_frame_base =
2511{
2512 &mips_insn32_frame_unwind,
2513 mips_insn32_frame_base_address,
2514 mips_insn32_frame_base_address,
2515 mips_insn32_frame_base_address
2516};
1c645fec 2517
29639122 2518static const struct frame_base *
b8a22b94 2519mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
29639122 2520{
b8a22b94
DJ
2521 CORE_ADDR pc = get_frame_pc (this_frame);
2522 if (! mips_pc_is_mips16 (pc))
29639122 2523 return &mips_insn32_frame_base;
a65bbe44 2524 else
29639122
JB
2525 return NULL;
2526}
a65bbe44 2527
29639122 2528static struct trad_frame_cache *
b8a22b94 2529mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
29639122
JB
2530{
2531 CORE_ADDR pc;
2532 CORE_ADDR start_addr;
2533 CORE_ADDR stack_addr;
2534 struct trad_frame_cache *this_trad_cache;
b8a22b94
DJ
2535 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2536 int num_regs = gdbarch_num_regs (gdbarch);
c906108c 2537
29639122
JB
2538 if ((*this_cache) != NULL)
2539 return (*this_cache);
b8a22b94 2540 this_trad_cache = trad_frame_cache_zalloc (this_frame);
29639122 2541 (*this_cache) = this_trad_cache;
1c645fec 2542
29639122 2543 /* The return address is in the link register. */
3e8c568d 2544 trad_frame_set_reg_realreg (this_trad_cache,
72a155b4 2545 gdbarch_pc_regnum (gdbarch),
b8a22b94 2546 num_regs + MIPS_RA_REGNUM);
1c645fec 2547
29639122
JB
2548 /* Frame ID, since it's a frameless / stackless function, no stack
2549 space is allocated and SP on entry is the current SP. */
b8a22b94 2550 pc = get_frame_pc (this_frame);
29639122 2551 find_pc_partial_function (pc, NULL, &start_addr, NULL);
b8a22b94
DJ
2552 stack_addr = get_frame_register_signed (this_frame,
2553 num_regs + MIPS_SP_REGNUM);
aa6c981f 2554 trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
1c645fec 2555
29639122
JB
2556 /* Assume that the frame's base is the same as the
2557 stack-pointer. */
2558 trad_frame_set_this_base (this_trad_cache, stack_addr);
c906108c 2559
29639122
JB
2560 return this_trad_cache;
2561}
c906108c 2562
29639122 2563static void
b8a22b94 2564mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
29639122
JB
2565 struct frame_id *this_id)
2566{
2567 struct trad_frame_cache *this_trad_cache
b8a22b94 2568 = mips_stub_frame_cache (this_frame, this_cache);
29639122
JB
2569 trad_frame_get_id (this_trad_cache, this_id);
2570}
c906108c 2571
b8a22b94
DJ
2572static struct value *
2573mips_stub_frame_prev_register (struct frame_info *this_frame,
2574 void **this_cache, int regnum)
29639122
JB
2575{
2576 struct trad_frame_cache *this_trad_cache
b8a22b94
DJ
2577 = mips_stub_frame_cache (this_frame, this_cache);
2578 return trad_frame_get_register (this_trad_cache, this_frame, regnum);
29639122 2579}
c906108c 2580
b8a22b94
DJ
2581static int
2582mips_stub_frame_sniffer (const struct frame_unwind *self,
2583 struct frame_info *this_frame, void **this_cache)
29639122 2584{
aa6c981f 2585 gdb_byte dummy[4];
979b38e0 2586 struct obj_section *s;
b8a22b94 2587 CORE_ADDR pc = get_frame_address_in_block (this_frame);
db5f024e 2588 struct minimal_symbol *msym;
979b38e0 2589
aa6c981f 2590 /* Use the stub unwinder for unreadable code. */
b8a22b94
DJ
2591 if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
2592 return 1;
aa6c981f 2593
29639122 2594 if (in_plt_section (pc, NULL))
b8a22b94 2595 return 1;
979b38e0
DJ
2596
2597 /* Binutils for MIPS puts lazy resolution stubs into .MIPS.stubs. */
2598 s = find_pc_section (pc);
2599
2600 if (s != NULL
2601 && strcmp (bfd_get_section_name (s->objfile->obfd, s->the_bfd_section),
2602 ".MIPS.stubs") == 0)
b8a22b94 2603 return 1;
979b38e0 2604
db5f024e
DJ
2605 /* Calling a PIC function from a non-PIC function passes through a
2606 stub. The stub for foo is named ".pic.foo". */
2607 msym = lookup_minimal_symbol_by_pc (pc);
2608 if (msym != NULL
2609 && SYMBOL_LINKAGE_NAME (msym) != NULL
2610 && strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) == 0)
2611 return 1;
2612
b8a22b94 2613 return 0;
29639122 2614}
c906108c 2615
b8a22b94
DJ
2616static const struct frame_unwind mips_stub_frame_unwind =
2617{
2618 NORMAL_FRAME,
8fbca658 2619 default_frame_unwind_stop_reason,
b8a22b94
DJ
2620 mips_stub_frame_this_id,
2621 mips_stub_frame_prev_register,
2622 NULL,
2623 mips_stub_frame_sniffer
2624};
2625
29639122 2626static CORE_ADDR
b8a22b94 2627mips_stub_frame_base_address (struct frame_info *this_frame,
29639122
JB
2628 void **this_cache)
2629{
2630 struct trad_frame_cache *this_trad_cache
b8a22b94 2631 = mips_stub_frame_cache (this_frame, this_cache);
29639122
JB
2632 return trad_frame_get_this_base (this_trad_cache);
2633}
0fce0821 2634
29639122
JB
2635static const struct frame_base mips_stub_frame_base =
2636{
2637 &mips_stub_frame_unwind,
2638 mips_stub_frame_base_address,
2639 mips_stub_frame_base_address,
2640 mips_stub_frame_base_address
2641};
2642
2643static const struct frame_base *
b8a22b94 2644mips_stub_frame_base_sniffer (struct frame_info *this_frame)
29639122 2645{
b8a22b94 2646 if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
29639122
JB
2647 return &mips_stub_frame_base;
2648 else
2649 return NULL;
2650}
2651
29639122 2652/* mips_addr_bits_remove - remove useless address bits */
65596487 2653
29639122 2654static CORE_ADDR
24568a2c 2655mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
65596487 2656{
24568a2c 2657 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
930bd0e0
KB
2658
2659 if (is_mips16_addr (addr))
2660 addr = unmake_mips16_addr (addr);
2661
29639122
JB
2662 if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
2663 /* This hack is a work-around for existing boards using PMON, the
2664 simulator, and any other 64-bit targets that doesn't have true
2665 64-bit addressing. On these targets, the upper 32 bits of
2666 addresses are ignored by the hardware. Thus, the PC or SP are
2667 likely to have been sign extended to all 1s by instruction
2668 sequences that load 32-bit addresses. For example, a typical
2669 piece of code that loads an address is this:
65596487 2670
29639122
JB
2671 lui $r2, <upper 16 bits>
2672 ori $r2, <lower 16 bits>
65596487 2673
29639122
JB
2674 But the lui sign-extends the value such that the upper 32 bits
2675 may be all 1s. The workaround is simply to mask off these
2676 bits. In the future, gcc may be changed to support true 64-bit
2677 addressing, and this masking will have to be disabled. */
2678 return addr &= 0xffffffffUL;
2679 else
2680 return addr;
65596487
JB
2681}
2682
3d5f6d12
DJ
2683/* Instructions used during single-stepping of atomic sequences. */
2684#define LL_OPCODE 0x30
2685#define LLD_OPCODE 0x34
2686#define SC_OPCODE 0x38
2687#define SCD_OPCODE 0x3c
2688
2689/* Checks for an atomic sequence of instructions beginning with a LL/LLD
2690 instruction and ending with a SC/SCD instruction. If such a sequence
2691 is found, attempt to step through it. A breakpoint is placed at the end of
2692 the sequence. */
2693
2694static int
6c95b8df
PA
2695deal_with_atomic_sequence (struct gdbarch *gdbarch,
2696 struct address_space *aspace, CORE_ADDR pc)
3d5f6d12
DJ
2697{
2698 CORE_ADDR breaks[2] = {-1, -1};
2699 CORE_ADDR loc = pc;
2700 CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination. */
2701 unsigned long insn;
2702 int insn_count;
2703 int index;
2704 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
2705 const int atomic_sequence_length = 16; /* Instruction sequence length. */
2706
2707 if (pc & 0x01)
2708 return 0;
2709
e17a4113 2710 insn = mips_fetch_instruction (gdbarch, loc);
3d5f6d12
DJ
2711 /* Assume all atomic sequences start with a ll/lld instruction. */
2712 if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
2713 return 0;
2714
2715 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
2716 instructions. */
2717 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
2718 {
2719 int is_branch = 0;
2720 loc += MIPS_INSN32_SIZE;
e17a4113 2721 insn = mips_fetch_instruction (gdbarch, loc);
3d5f6d12
DJ
2722
2723 /* Assume that there is at most one branch in the atomic
2724 sequence. If a branch is found, put a breakpoint in its
2725 destination address. */
2726 switch (itype_op (insn))
2727 {
2728 case 0: /* SPECIAL */
2729 if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
025bb325 2730 return 0; /* fallback to the standard single-step code. */
3d5f6d12
DJ
2731 break;
2732 case 1: /* REGIMM */
a385295e
MR
2733 is_branch = ((itype_rt (insn) & 0xc) == 0 /* B{LT,GE}Z* */
2734 || ((itype_rt (insn) & 0x1e) == 0
2735 && itype_rs (insn) == 0)); /* BPOSGE* */
3d5f6d12
DJ
2736 break;
2737 case 2: /* J */
2738 case 3: /* JAL */
025bb325 2739 return 0; /* fallback to the standard single-step code. */
3d5f6d12
DJ
2740 case 4: /* BEQ */
2741 case 5: /* BNE */
2742 case 6: /* BLEZ */
2743 case 7: /* BGTZ */
2744 case 20: /* BEQL */
2745 case 21: /* BNEL */
2746 case 22: /* BLEZL */
2747 case 23: /* BGTTL */
2748 is_branch = 1;
2749 break;
2750 case 17: /* COP1 */
a385295e
MR
2751 is_branch = ((itype_rs (insn) == 9 || itype_rs (insn) == 10)
2752 && (itype_rt (insn) & 0x2) == 0);
2753 if (is_branch) /* BC1ANY2F, BC1ANY2T, BC1ANY4F, BC1ANY4T */
2754 break;
2755 /* Fall through. */
3d5f6d12
DJ
2756 case 18: /* COP2 */
2757 case 19: /* COP3 */
2758 is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
2759 break;
2760 }
2761 if (is_branch)
2762 {
2763 branch_bp = loc + mips32_relative_offset (insn) + 4;
2764 if (last_breakpoint >= 1)
2765 return 0; /* More than one branch found, fallback to the
2766 standard single-step code. */
2767 breaks[1] = branch_bp;
2768 last_breakpoint++;
2769 }
2770
2771 if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
2772 break;
2773 }
2774
2775 /* Assume that the atomic sequence ends with a sc/scd instruction. */
2776 if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
2777 return 0;
2778
2779 loc += MIPS_INSN32_SIZE;
2780
2781 /* Insert a breakpoint right after the end of the atomic sequence. */
2782 breaks[0] = loc;
2783
2784 /* Check for duplicated breakpoints. Check also for a breakpoint
025bb325 2785 placed (branch instruction's destination) in the atomic sequence. */
3d5f6d12
DJ
2786 if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
2787 last_breakpoint = 0;
2788
2789 /* Effectively inserts the breakpoints. */
2790 for (index = 0; index <= last_breakpoint; index++)
6c95b8df 2791 insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
3d5f6d12
DJ
2792
2793 return 1;
2794}
2795
29639122
JB
2796/* mips_software_single_step() is called just before we want to resume
2797 the inferior, if we want to single-step it but there is no hardware
2798 or kernel single-step support (MIPS on GNU/Linux for example). We find
e0cd558a 2799 the target of the coming instruction and breakpoint it. */
29639122 2800
e6590a1b 2801int
0b1b3e42 2802mips_software_single_step (struct frame_info *frame)
c906108c 2803{
a6d9a66e 2804 struct gdbarch *gdbarch = get_frame_arch (frame);
6c95b8df 2805 struct address_space *aspace = get_frame_address_space (frame);
8181d85f 2806 CORE_ADDR pc, next_pc;
65596487 2807
0b1b3e42 2808 pc = get_frame_pc (frame);
6c95b8df 2809 if (deal_with_atomic_sequence (gdbarch, aspace, pc))
3d5f6d12
DJ
2810 return 1;
2811
0b1b3e42 2812 next_pc = mips_next_pc (frame, pc);
e6590a1b 2813
6c95b8df 2814 insert_single_step_breakpoint (gdbarch, aspace, next_pc);
e6590a1b 2815 return 1;
29639122 2816}
a65bbe44 2817
29639122 2818/* Test whether the PC points to the return instruction at the
025bb325 2819 end of a function. */
65596487 2820
29639122 2821static int
e17a4113 2822mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
29639122 2823{
6321c22a
MR
2824 ULONGEST insn;
2825 ULONGEST hint;
2826
2827 /* This used to check for MIPS16, but this piece of code is never
2828 called for MIPS16 functions. */
2829 gdb_assert (!mips_pc_is_mips16 (pc));
2830
2831 insn = mips_fetch_instruction (gdbarch, pc);
2832 hint = 0x7c0;
2833 return (insn & ~hint) == 0x3e00008; /* jr(.hb) $ra */
29639122 2834}
c906108c 2835
c906108c 2836
29639122
JB
2837/* This fencepost looks highly suspicious to me. Removing it also
2838 seems suspicious as it could affect remote debugging across serial
2839 lines. */
c906108c 2840
29639122 2841static CORE_ADDR
74ed0bb4 2842heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
29639122
JB
2843{
2844 CORE_ADDR start_pc;
2845 CORE_ADDR fence;
2846 int instlen;
2847 int seen_adjsp = 0;
d6b48e9c 2848 struct inferior *inf;
65596487 2849
74ed0bb4 2850 pc = gdbarch_addr_bits_remove (gdbarch, pc);
29639122
JB
2851 start_pc = pc;
2852 fence = start_pc - heuristic_fence_post;
2853 if (start_pc == 0)
2854 return 0;
65596487 2855
29639122
JB
2856 if (heuristic_fence_post == UINT_MAX || fence < VM_MIN_ADDRESS)
2857 fence = VM_MIN_ADDRESS;
65596487 2858
95ac2dcf 2859 instlen = mips_pc_is_mips16 (pc) ? MIPS_INSN16_SIZE : MIPS_INSN32_SIZE;
98b4dd94 2860
d6b48e9c
PA
2861 inf = current_inferior ();
2862
025bb325 2863 /* Search back for previous return. */
29639122
JB
2864 for (start_pc -= instlen;; start_pc -= instlen)
2865 if (start_pc < fence)
2866 {
2867 /* It's not clear to me why we reach this point when
2868 stop_soon, but with this test, at least we
2869 don't print out warnings for every child forked (eg, on
2870 decstation). 22apr93 rich@cygnus.com. */
16c381f0 2871 if (inf->control.stop_soon == NO_STOP_QUIETLY)
29639122
JB
2872 {
2873 static int blurb_printed = 0;
98b4dd94 2874
5af949e3
UW
2875 warning (_("GDB can't find the start of the function at %s."),
2876 paddress (gdbarch, pc));
29639122
JB
2877
2878 if (!blurb_printed)
2879 {
2880 /* This actually happens frequently in embedded
2881 development, when you first connect to a board
2882 and your stack pointer and pc are nowhere in
2883 particular. This message needs to give people
2884 in that situation enough information to
2885 determine that it's no big deal. */
2886 printf_filtered ("\n\
5af949e3 2887 GDB is unable to find the start of the function at %s\n\
29639122
JB
2888and thus can't determine the size of that function's stack frame.\n\
2889This means that GDB may be unable to access that stack frame, or\n\
2890the frames below it.\n\
2891 This problem is most likely caused by an invalid program counter or\n\
2892stack pointer.\n\
2893 However, if you think GDB should simply search farther back\n\
5af949e3 2894from %s for code which looks like the beginning of a\n\
29639122 2895function, you can increase the range of the search using the `set\n\
5af949e3
UW
2896heuristic-fence-post' command.\n",
2897 paddress (gdbarch, pc), paddress (gdbarch, pc));
29639122
JB
2898 blurb_printed = 1;
2899 }
2900 }
2901
2902 return 0;
2903 }
0fe7e7c8 2904 else if (mips_pc_is_mips16 (start_pc))
29639122
JB
2905 {
2906 unsigned short inst;
2907
2908 /* On MIPS16, any one of the following is likely to be the
2909 start of a function:
193774b3
MR
2910 extend save
2911 save
29639122
JB
2912 entry
2913 addiu sp,-n
2914 daddiu sp,-n
025bb325 2915 extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'. */
e17a4113 2916 inst = mips_fetch_instruction (gdbarch, start_pc);
193774b3
MR
2917 if ((inst & 0xff80) == 0x6480) /* save */
2918 {
2919 if (start_pc - instlen >= fence)
2920 {
e17a4113 2921 inst = mips_fetch_instruction (gdbarch, start_pc - instlen);
193774b3
MR
2922 if ((inst & 0xf800) == 0xf000) /* extend */
2923 start_pc -= instlen;
2924 }
2925 break;
2926 }
2927 else if (((inst & 0xf81f) == 0xe809
2928 && (inst & 0x700) != 0x700) /* entry */
2929 || (inst & 0xff80) == 0x6380 /* addiu sp,-n */
2930 || (inst & 0xff80) == 0xfb80 /* daddiu sp,-n */
2931 || ((inst & 0xf810) == 0xf010 && seen_adjsp)) /* extend -n */
29639122
JB
2932 break;
2933 else if ((inst & 0xff00) == 0x6300 /* addiu sp */
2934 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
2935 seen_adjsp = 1;
2936 else
2937 seen_adjsp = 0;
2938 }
e17a4113 2939 else if (mips_about_to_return (gdbarch, start_pc))
29639122 2940 {
4c7d22cb 2941 /* Skip return and its delay slot. */
95ac2dcf 2942 start_pc += 2 * MIPS_INSN32_SIZE;
29639122
JB
2943 break;
2944 }
2945
2946 return start_pc;
c906108c
SS
2947}
2948
6c0d6680
DJ
2949struct mips_objfile_private
2950{
2951 bfd_size_type size;
2952 char *contents;
2953};
2954
f09ded24
AC
2955/* According to the current ABI, should the type be passed in a
2956 floating-point register (assuming that there is space)? When there
a1f5b845 2957 is no FPU, FP are not even considered as possible candidates for
f09ded24 2958 FP registers and, consequently this returns false - forces FP
025bb325 2959 arguments into integer registers. */
f09ded24
AC
2960
2961static int
74ed0bb4
MD
2962fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
2963 struct type *arg_type)
f09ded24
AC
2964{
2965 return ((typecode == TYPE_CODE_FLT
74ed0bb4 2966 || (MIPS_EABI (gdbarch)
6d82d43b
AC
2967 && (typecode == TYPE_CODE_STRUCT
2968 || typecode == TYPE_CODE_UNION)
f09ded24 2969 && TYPE_NFIELDS (arg_type) == 1
b2d6f210
MS
2970 && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0)))
2971 == TYPE_CODE_FLT))
74ed0bb4 2972 && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
f09ded24
AC
2973}
2974
49e790b0 2975/* On o32, argument passing in GPRs depends on the alignment of the type being
025bb325 2976 passed. Return 1 if this type must be aligned to a doubleword boundary. */
49e790b0
DJ
2977
2978static int
2979mips_type_needs_double_align (struct type *type)
2980{
2981 enum type_code typecode = TYPE_CODE (type);
361d1df0 2982
49e790b0
DJ
2983 if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
2984 return 1;
2985 else if (typecode == TYPE_CODE_STRUCT)
2986 {
2987 if (TYPE_NFIELDS (type) < 1)
2988 return 0;
2989 return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
2990 }
2991 else if (typecode == TYPE_CODE_UNION)
2992 {
361d1df0 2993 int i, n;
49e790b0
DJ
2994
2995 n = TYPE_NFIELDS (type);
2996 for (i = 0; i < n; i++)
2997 if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
2998 return 1;
2999 return 0;
3000 }
3001 return 0;
3002}
3003
dc604539
AC
3004/* Adjust the address downward (direction of stack growth) so that it
3005 is correctly aligned for a new stack frame. */
3006static CORE_ADDR
3007mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
3008{
5b03f266 3009 return align_down (addr, 16);
dc604539
AC
3010}
3011
f7ab6ec6 3012static CORE_ADDR
7d9b040b 3013mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
3014 struct regcache *regcache, CORE_ADDR bp_addr,
3015 int nargs, struct value **args, CORE_ADDR sp,
3016 int struct_return, CORE_ADDR struct_addr)
c906108c
SS
3017{
3018 int argreg;
3019 int float_argreg;
3020 int argnum;
3021 int len = 0;
3022 int stack_offset = 0;
480d3dd2 3023 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 3024 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7d9b040b 3025 CORE_ADDR func_addr = find_function_addr (function, NULL);
1a69e1e4 3026 int regsize = mips_abi_regsize (gdbarch);
c906108c 3027
25ab4790
AC
3028 /* For shared libraries, "t9" needs to point at the function
3029 address. */
4c7d22cb 3030 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
3031
3032 /* Set the return address register to point to the entry point of
3033 the program, where a breakpoint lies in wait. */
4c7d22cb 3034 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 3035
c906108c 3036 /* First ensure that the stack and structure return address (if any)
cb3d25d1
MS
3037 are properly aligned. The stack has to be at least 64-bit
3038 aligned even on 32-bit machines, because doubles must be 64-bit
3039 aligned. For n32 and n64, stack frames need to be 128-bit
3040 aligned, so we round to this widest known alignment. */
3041
5b03f266
AC
3042 sp = align_down (sp, 16);
3043 struct_addr = align_down (struct_addr, 16);
c5aa993b 3044
46e0f506 3045 /* Now make space on the stack for the args. We allocate more
c906108c 3046 than necessary for EABI, because the first few arguments are
46e0f506 3047 passed in registers, but that's OK. */
c906108c 3048 for (argnum = 0; argnum < nargs; argnum++)
1a69e1e4 3049 len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
5b03f266 3050 sp -= align_up (len, 16);
c906108c 3051
9ace0497 3052 if (mips_debug)
6d82d43b 3053 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
3054 "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
3055 paddress (gdbarch, sp), (long) align_up (len, 16));
9ace0497 3056
c906108c 3057 /* Initialize the integer and float register pointers. */
4c7d22cb 3058 argreg = MIPS_A0_REGNUM;
72a155b4 3059 float_argreg = mips_fpa0_regnum (gdbarch);
c906108c 3060
46e0f506 3061 /* The struct_return pointer occupies the first parameter-passing reg. */
c906108c 3062 if (struct_return)
9ace0497
AC
3063 {
3064 if (mips_debug)
3065 fprintf_unfiltered (gdb_stdlog,
025bb325
MS
3066 "mips_eabi_push_dummy_call: "
3067 "struct_return reg=%d %s\n",
5af949e3 3068 argreg, paddress (gdbarch, struct_addr));
9c9acae0 3069 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
9ace0497 3070 }
c906108c
SS
3071
3072 /* Now load as many as possible of the first arguments into
3073 registers, and push the rest onto the stack. Loop thru args
3074 from first to last. */
3075 for (argnum = 0; argnum < nargs; argnum++)
3076 {
47a35522
MK
3077 const gdb_byte *val;
3078 gdb_byte valbuf[MAX_REGISTER_SIZE];
ea7c478f 3079 struct value *arg = args[argnum];
4991999e 3080 struct type *arg_type = check_typedef (value_type (arg));
c906108c
SS
3081 int len = TYPE_LENGTH (arg_type);
3082 enum type_code typecode = TYPE_CODE (arg_type);
3083
9ace0497
AC
3084 if (mips_debug)
3085 fprintf_unfiltered (gdb_stdlog,
25ab4790 3086 "mips_eabi_push_dummy_call: %d len=%d type=%d",
acdb74a0 3087 argnum + 1, len, (int) typecode);
9ace0497 3088
930bd0e0
KB
3089 /* Function pointer arguments to mips16 code need to be made into
3090 mips16 pointers. */
3091 if (typecode == TYPE_CODE_PTR
3092 && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
3093 {
3094 CORE_ADDR addr = extract_signed_integer (value_contents (arg),
3095 len, byte_order);
3096 if (mips_pc_is_mips16 (addr))
3097 {
3098 store_signed_integer (valbuf, len, byte_order,
3099 make_mips16_addr (addr));
3100 val = valbuf;
3101 }
3102 else
3103 val = value_contents (arg);
3104 }
c906108c 3105 /* The EABI passes structures that do not fit in a register by
46e0f506 3106 reference. */
930bd0e0 3107 else if (len > regsize
9ace0497 3108 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
c906108c 3109 {
e17a4113
UW
3110 store_unsigned_integer (valbuf, regsize, byte_order,
3111 value_address (arg));
c906108c 3112 typecode = TYPE_CODE_PTR;
1a69e1e4 3113 len = regsize;
c906108c 3114 val = valbuf;
9ace0497
AC
3115 if (mips_debug)
3116 fprintf_unfiltered (gdb_stdlog, " push");
c906108c
SS
3117 }
3118 else
47a35522 3119 val = value_contents (arg);
c906108c
SS
3120
3121 /* 32-bit ABIs always start floating point arguments in an
acdb74a0
AC
3122 even-numbered floating point register. Round the FP register
3123 up before the check to see if there are any FP registers
46e0f506
MS
3124 left. Non MIPS_EABI targets also pass the FP in the integer
3125 registers so also round up normal registers. */
74ed0bb4 3126 if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
acdb74a0
AC
3127 {
3128 if ((float_argreg & 1))
3129 float_argreg++;
3130 }
c906108c
SS
3131
3132 /* Floating point arguments passed in registers have to be
3133 treated specially. On 32-bit architectures, doubles
c5aa993b
JM
3134 are passed in register pairs; the even register gets
3135 the low word, and the odd register gets the high word.
3136 On non-EABI processors, the first two floating point arguments are
3137 also copied to general registers, because MIPS16 functions
3138 don't use float registers for arguments. This duplication of
3139 arguments in general registers can't hurt non-MIPS16 functions
3140 because those registers are normally skipped. */
1012bd0e
EZ
3141 /* MIPS_EABI squeezes a struct that contains a single floating
3142 point value into an FP register instead of pushing it onto the
46e0f506 3143 stack. */
74ed0bb4
MD
3144 if (fp_register_arg_p (gdbarch, typecode, arg_type)
3145 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
c906108c 3146 {
6da397e0
KB
3147 /* EABI32 will pass doubles in consecutive registers, even on
3148 64-bit cores. At one time, we used to check the size of
3149 `float_argreg' to determine whether or not to pass doubles
3150 in consecutive registers, but this is not sufficient for
3151 making the ABI determination. */
3152 if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
c906108c 3153 {
72a155b4 3154 int low_offset = gdbarch_byte_order (gdbarch)
4c6b5505 3155 == BFD_ENDIAN_BIG ? 4 : 0;
a8852dc5 3156 long regval;
c906108c
SS
3157
3158 /* Write the low word of the double to the even register(s). */
a8852dc5
KB
3159 regval = extract_signed_integer (val + low_offset,
3160 4, byte_order);
9ace0497 3161 if (mips_debug)
acdb74a0 3162 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 3163 float_argreg, phex (regval, 4));
a8852dc5 3164 regcache_cooked_write_signed (regcache, float_argreg++, regval);
c906108c
SS
3165
3166 /* Write the high word of the double to the odd register(s). */
a8852dc5
KB
3167 regval = extract_signed_integer (val + 4 - low_offset,
3168 4, byte_order);
9ace0497 3169 if (mips_debug)
acdb74a0 3170 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 3171 float_argreg, phex (regval, 4));
a8852dc5 3172 regcache_cooked_write_signed (regcache, float_argreg++, regval);
c906108c
SS
3173 }
3174 else
3175 {
3176 /* This is a floating point value that fits entirely
3177 in a single register. */
53a5351d 3178 /* On 32 bit ABI's the float_argreg is further adjusted
6d82d43b 3179 above to ensure that it is even register aligned. */
a8852dc5 3180 LONGEST regval = extract_signed_integer (val, len, byte_order);
9ace0497 3181 if (mips_debug)
acdb74a0 3182 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
9ace0497 3183 float_argreg, phex (regval, len));
a8852dc5 3184 regcache_cooked_write_signed (regcache, float_argreg++, regval);
c906108c
SS
3185 }
3186 }
3187 else
3188 {
3189 /* Copy the argument to general registers or the stack in
3190 register-sized pieces. Large arguments are split between
3191 registers and stack. */
1a69e1e4
DJ
3192 /* Note: structs whose size is not a multiple of regsize
3193 are treated specially: Irix cc passes
d5ac5a39
AC
3194 them in registers where gcc sometimes puts them on the
3195 stack. For maximum compatibility, we will put them in
3196 both places. */
1a69e1e4 3197 int odd_sized_struct = (len > regsize && len % regsize != 0);
46e0f506 3198
f09ded24 3199 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 3200 register are only written to memory. */
c906108c
SS
3201 while (len > 0)
3202 {
ebafbe83 3203 /* Remember if the argument was written to the stack. */
566f0f7a 3204 int stack_used_p = 0;
1a69e1e4 3205 int partial_len = (len < regsize ? len : regsize);
c906108c 3206
acdb74a0
AC
3207 if (mips_debug)
3208 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3209 partial_len);
3210
566f0f7a 3211 /* Write this portion of the argument to the stack. */
74ed0bb4 3212 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
f09ded24 3213 || odd_sized_struct
74ed0bb4 3214 || fp_register_arg_p (gdbarch, typecode, arg_type))
c906108c 3215 {
c906108c 3216 /* Should shorter than int integer values be
025bb325 3217 promoted to int before being stored? */
c906108c 3218 int longword_offset = 0;
9ace0497 3219 CORE_ADDR addr;
566f0f7a 3220 stack_used_p = 1;
72a155b4 3221 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
7a292a7a 3222 {
1a69e1e4 3223 if (regsize == 8
480d3dd2
AC
3224 && (typecode == TYPE_CODE_INT
3225 || typecode == TYPE_CODE_PTR
6d82d43b 3226 || typecode == TYPE_CODE_FLT) && len <= 4)
1a69e1e4 3227 longword_offset = regsize - len;
480d3dd2
AC
3228 else if ((typecode == TYPE_CODE_STRUCT
3229 || typecode == TYPE_CODE_UNION)
1a69e1e4
DJ
3230 && TYPE_LENGTH (arg_type) < regsize)
3231 longword_offset = regsize - len;
7a292a7a 3232 }
c5aa993b 3233
9ace0497
AC
3234 if (mips_debug)
3235 {
5af949e3
UW
3236 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
3237 paddress (gdbarch, stack_offset));
3238 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
3239 paddress (gdbarch, longword_offset));
9ace0497 3240 }
361d1df0 3241
9ace0497
AC
3242 addr = sp + stack_offset + longword_offset;
3243
3244 if (mips_debug)
3245 {
3246 int i;
5af949e3
UW
3247 fprintf_unfiltered (gdb_stdlog, " @%s ",
3248 paddress (gdbarch, addr));
9ace0497
AC
3249 for (i = 0; i < partial_len; i++)
3250 {
6d82d43b 3251 fprintf_unfiltered (gdb_stdlog, "%02x",
cb3d25d1 3252 val[i] & 0xff);
9ace0497
AC
3253 }
3254 }
3255 write_memory (addr, val, partial_len);
c906108c
SS
3256 }
3257
f09ded24
AC
3258 /* Note!!! This is NOT an else clause. Odd sized
3259 structs may go thru BOTH paths. Floating point
46e0f506 3260 arguments will not. */
566f0f7a 3261 /* Write this portion of the argument to a general
6d82d43b 3262 purpose register. */
74ed0bb4
MD
3263 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
3264 && !fp_register_arg_p (gdbarch, typecode, arg_type))
c906108c 3265 {
6d82d43b 3266 LONGEST regval =
a8852dc5 3267 extract_signed_integer (val, partial_len, byte_order);
c906108c 3268
9ace0497 3269 if (mips_debug)
acdb74a0 3270 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
9ace0497 3271 argreg,
1a69e1e4 3272 phex (regval, regsize));
a8852dc5 3273 regcache_cooked_write_signed (regcache, argreg, regval);
c906108c 3274 argreg++;
c906108c 3275 }
c5aa993b 3276
c906108c
SS
3277 len -= partial_len;
3278 val += partial_len;
3279
b021a221
MS
3280 /* Compute the offset into the stack at which we will
3281 copy the next parameter.
566f0f7a 3282
566f0f7a 3283 In the new EABI (and the NABI32), the stack_offset
46e0f506 3284 only needs to be adjusted when it has been used. */
c906108c 3285
46e0f506 3286 if (stack_used_p)
1a69e1e4 3287 stack_offset += align_up (partial_len, regsize);
c906108c
SS
3288 }
3289 }
9ace0497
AC
3290 if (mips_debug)
3291 fprintf_unfiltered (gdb_stdlog, "\n");
c906108c
SS
3292 }
3293
f10683bb 3294 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 3295
0f71a2f6
JM
3296 /* Return adjusted stack pointer. */
3297 return sp;
3298}
3299
a1f5b845 3300/* Determine the return value convention being used. */
6d82d43b 3301
9c8fdbfa 3302static enum return_value_convention
c055b101 3303mips_eabi_return_value (struct gdbarch *gdbarch, struct type *func_type,
9c8fdbfa 3304 struct type *type, struct regcache *regcache,
47a35522 3305 gdb_byte *readbuf, const gdb_byte *writebuf)
6d82d43b 3306{
609ba780
JM
3307 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3308 int fp_return_type = 0;
3309 int offset, regnum, xfer;
3310
9c8fdbfa
AC
3311 if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
3312 return RETURN_VALUE_STRUCT_CONVENTION;
609ba780
JM
3313
3314 /* Floating point type? */
3315 if (tdep->mips_fpu_type != MIPS_FPU_NONE)
3316 {
3317 if (TYPE_CODE (type) == TYPE_CODE_FLT)
3318 fp_return_type = 1;
3319 /* Structs with a single field of float type
3320 are returned in a floating point register. */
3321 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
3322 || TYPE_CODE (type) == TYPE_CODE_UNION)
3323 && TYPE_NFIELDS (type) == 1)
3324 {
3325 struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);
3326
3327 if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
3328 fp_return_type = 1;
3329 }
3330 }
3331
3332 if (fp_return_type)
3333 {
3334 /* A floating-point value belongs in the least significant part
3335 of FP0/FP1. */
3336 if (mips_debug)
3337 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
3338 regnum = mips_regnum (gdbarch)->fp0;
3339 }
3340 else
3341 {
3342 /* An integer value goes in V0/V1. */
3343 if (mips_debug)
3344 fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
3345 regnum = MIPS_V0_REGNUM;
3346 }
3347 for (offset = 0;
3348 offset < TYPE_LENGTH (type);
3349 offset += mips_abi_regsize (gdbarch), regnum++)
3350 {
3351 xfer = mips_abi_regsize (gdbarch);
3352 if (offset + xfer > TYPE_LENGTH (type))
3353 xfer = TYPE_LENGTH (type) - offset;
3354 mips_xfer_register (gdbarch, regcache,
3355 gdbarch_num_regs (gdbarch) + regnum, xfer,
3356 gdbarch_byte_order (gdbarch), readbuf, writebuf,
3357 offset);
3358 }
3359
9c8fdbfa 3360 return RETURN_VALUE_REGISTER_CONVENTION;
6d82d43b
AC
3361}
3362
6d82d43b
AC
3363
3364/* N32/N64 ABI stuff. */
ebafbe83 3365
8d26208a
DJ
3366/* Search for a naturally aligned double at OFFSET inside a struct
3367 ARG_TYPE. The N32 / N64 ABIs pass these in floating point
3368 registers. */
3369
3370static int
74ed0bb4
MD
3371mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
3372 int offset)
8d26208a
DJ
3373{
3374 int i;
3375
3376 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
3377 return 0;
3378
74ed0bb4 3379 if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
8d26208a
DJ
3380 return 0;
3381
3382 if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
3383 return 0;
3384
3385 for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
3386 {
3387 int pos;
3388 struct type *field_type;
3389
3390 /* We're only looking at normal fields. */
5bc60cfb 3391 if (field_is_static (&TYPE_FIELD (arg_type, i))
8d26208a
DJ
3392 || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
3393 continue;
3394
3395 /* If we have gone past the offset, there is no double to pass. */
3396 pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
3397 if (pos > offset)
3398 return 0;
3399
3400 field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));
3401
3402 /* If this field is entirely before the requested offset, go
3403 on to the next one. */
3404 if (pos + TYPE_LENGTH (field_type) <= offset)
3405 continue;
3406
3407 /* If this is our special aligned double, we can stop. */
3408 if (TYPE_CODE (field_type) == TYPE_CODE_FLT
3409 && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
3410 return 1;
3411
3412 /* This field starts at or before the requested offset, and
3413 overlaps it. If it is a structure, recurse inwards. */
74ed0bb4 3414 return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
8d26208a
DJ
3415 }
3416
3417 return 0;
3418}
3419
f7ab6ec6 3420static CORE_ADDR
7d9b040b 3421mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
3422 struct regcache *regcache, CORE_ADDR bp_addr,
3423 int nargs, struct value **args, CORE_ADDR sp,
3424 int struct_return, CORE_ADDR struct_addr)
cb3d25d1
MS
3425{
3426 int argreg;
3427 int float_argreg;
3428 int argnum;
3429 int len = 0;
3430 int stack_offset = 0;
480d3dd2 3431 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 3432 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7d9b040b 3433 CORE_ADDR func_addr = find_function_addr (function, NULL);
cb3d25d1 3434
25ab4790
AC
3435 /* For shared libraries, "t9" needs to point at the function
3436 address. */
4c7d22cb 3437 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
3438
3439 /* Set the return address register to point to the entry point of
3440 the program, where a breakpoint lies in wait. */
4c7d22cb 3441 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 3442
cb3d25d1
MS
3443 /* First ensure that the stack and structure return address (if any)
3444 are properly aligned. The stack has to be at least 64-bit
3445 aligned even on 32-bit machines, because doubles must be 64-bit
3446 aligned. For n32 and n64, stack frames need to be 128-bit
3447 aligned, so we round to this widest known alignment. */
3448
5b03f266
AC
3449 sp = align_down (sp, 16);
3450 struct_addr = align_down (struct_addr, 16);
cb3d25d1
MS
3451
3452 /* Now make space on the stack for the args. */
3453 for (argnum = 0; argnum < nargs; argnum++)
1a69e1e4 3454 len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
5b03f266 3455 sp -= align_up (len, 16);
cb3d25d1
MS
3456
3457 if (mips_debug)
6d82d43b 3458 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
3459 "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
3460 paddress (gdbarch, sp), (long) align_up (len, 16));
cb3d25d1
MS
3461
3462 /* Initialize the integer and float register pointers. */
4c7d22cb 3463 argreg = MIPS_A0_REGNUM;
72a155b4 3464 float_argreg = mips_fpa0_regnum (gdbarch);
cb3d25d1 3465
46e0f506 3466 /* The struct_return pointer occupies the first parameter-passing reg. */
cb3d25d1
MS
3467 if (struct_return)
3468 {
3469 if (mips_debug)
3470 fprintf_unfiltered (gdb_stdlog,
025bb325
MS
3471 "mips_n32n64_push_dummy_call: "
3472 "struct_return reg=%d %s\n",
5af949e3 3473 argreg, paddress (gdbarch, struct_addr));
9c9acae0 3474 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
cb3d25d1
MS
3475 }
3476
3477 /* Now load as many as possible of the first arguments into
3478 registers, and push the rest onto the stack. Loop thru args
3479 from first to last. */
3480 for (argnum = 0; argnum < nargs; argnum++)
3481 {
47a35522 3482 const gdb_byte *val;
cb3d25d1 3483 struct value *arg = args[argnum];
4991999e 3484 struct type *arg_type = check_typedef (value_type (arg));
cb3d25d1
MS
3485 int len = TYPE_LENGTH (arg_type);
3486 enum type_code typecode = TYPE_CODE (arg_type);
3487
3488 if (mips_debug)
3489 fprintf_unfiltered (gdb_stdlog,
25ab4790 3490 "mips_n32n64_push_dummy_call: %d len=%d type=%d",
cb3d25d1
MS
3491 argnum + 1, len, (int) typecode);
3492
47a35522 3493 val = value_contents (arg);
cb3d25d1 3494
5b68030f
JM
3495 /* A 128-bit long double value requires an even-odd pair of
3496 floating-point registers. */
3497 if (len == 16
3498 && fp_register_arg_p (gdbarch, typecode, arg_type)
3499 && (float_argreg & 1))
3500 {
3501 float_argreg++;
3502 argreg++;
3503 }
3504
74ed0bb4
MD
3505 if (fp_register_arg_p (gdbarch, typecode, arg_type)
3506 && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
cb3d25d1
MS
3507 {
3508 /* This is a floating point value that fits entirely
5b68030f
JM
3509 in a single register or a pair of registers. */
3510 int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
e17a4113 3511 LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
cb3d25d1
MS
3512 if (mips_debug)
3513 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5b68030f 3514 float_argreg, phex (regval, reglen));
8d26208a 3515 regcache_cooked_write_unsigned (regcache, float_argreg, regval);
cb3d25d1
MS
3516
3517 if (mips_debug)
3518 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5b68030f 3519 argreg, phex (regval, reglen));
9c9acae0 3520 regcache_cooked_write_unsigned (regcache, argreg, regval);
8d26208a
DJ
3521 float_argreg++;
3522 argreg++;
5b68030f
JM
3523 if (len == 16)
3524 {
e17a4113
UW
3525 regval = extract_unsigned_integer (val + reglen,
3526 reglen, byte_order);
5b68030f
JM
3527 if (mips_debug)
3528 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3529 float_argreg, phex (regval, reglen));
3530 regcache_cooked_write_unsigned (regcache, float_argreg, regval);
3531
3532 if (mips_debug)
3533 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3534 argreg, phex (regval, reglen));
3535 regcache_cooked_write_unsigned (regcache, argreg, regval);
3536 float_argreg++;
3537 argreg++;
3538 }
cb3d25d1
MS
3539 }
3540 else
3541 {
3542 /* Copy the argument to general registers or the stack in
3543 register-sized pieces. Large arguments are split between
3544 registers and stack. */
ab2e1992
MR
3545 /* For N32/N64, structs, unions, or other composite types are
3546 treated as a sequence of doublewords, and are passed in integer
3547 or floating point registers as though they were simple scalar
3548 parameters to the extent that they fit, with any excess on the
3549 stack packed according to the normal memory layout of the
3550 object.
3551 The caller does not reserve space for the register arguments;
3552 the callee is responsible for reserving it if required. */
cb3d25d1 3553 /* Note: Floating-point values that didn't fit into an FP
6d82d43b 3554 register are only written to memory. */
cb3d25d1
MS
3555 while (len > 0)
3556 {
ad018eee 3557 /* Remember if the argument was written to the stack. */
cb3d25d1 3558 int stack_used_p = 0;
1a69e1e4 3559 int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
cb3d25d1
MS
3560
3561 if (mips_debug)
3562 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
3563 partial_len);
3564
74ed0bb4
MD
3565 if (fp_register_arg_p (gdbarch, typecode, arg_type))
3566 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));
8d26208a 3567
cb3d25d1 3568 /* Write this portion of the argument to the stack. */
74ed0bb4 3569 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
cb3d25d1
MS
3570 {
3571 /* Should shorter than int integer values be
025bb325 3572 promoted to int before being stored? */
cb3d25d1
MS
3573 int longword_offset = 0;
3574 CORE_ADDR addr;
3575 stack_used_p = 1;
72a155b4 3576 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
cb3d25d1 3577 {
1a69e1e4 3578 if ((typecode == TYPE_CODE_INT
5b68030f 3579 || typecode == TYPE_CODE_PTR)
1a69e1e4
DJ
3580 && len <= 4)
3581 longword_offset = MIPS64_REGSIZE - len;
cb3d25d1
MS
3582 }
3583
3584 if (mips_debug)
3585 {
5af949e3
UW
3586 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
3587 paddress (gdbarch, stack_offset));
3588 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
3589 paddress (gdbarch, longword_offset));
cb3d25d1
MS
3590 }
3591
3592 addr = sp + stack_offset + longword_offset;
3593
3594 if (mips_debug)
3595 {
3596 int i;
5af949e3
UW
3597 fprintf_unfiltered (gdb_stdlog, " @%s ",
3598 paddress (gdbarch, addr));
cb3d25d1
MS
3599 for (i = 0; i < partial_len; i++)
3600 {
6d82d43b 3601 fprintf_unfiltered (gdb_stdlog, "%02x",
cb3d25d1
MS
3602 val[i] & 0xff);
3603 }
3604 }
3605 write_memory (addr, val, partial_len);
3606 }
3607
3608 /* Note!!! This is NOT an else clause. Odd sized
8d26208a 3609 structs may go thru BOTH paths. */
cb3d25d1 3610 /* Write this portion of the argument to a general
6d82d43b 3611 purpose register. */
74ed0bb4 3612 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
cb3d25d1 3613 {
5863b5d5
MR
3614 LONGEST regval;
3615
3616 /* Sign extend pointers, 32-bit integers and signed
3617 16-bit and 8-bit integers; everything else is taken
3618 as is. */
3619
3620 if ((partial_len == 4
3621 && (typecode == TYPE_CODE_PTR
3622 || typecode == TYPE_CODE_INT))
3623 || (partial_len < 4
3624 && typecode == TYPE_CODE_INT
3625 && !TYPE_UNSIGNED (arg_type)))
e17a4113
UW
3626 regval = extract_signed_integer (val, partial_len,
3627 byte_order);
5863b5d5 3628 else
e17a4113
UW
3629 regval = extract_unsigned_integer (val, partial_len,
3630 byte_order);
cb3d25d1
MS
3631
3632 /* A non-floating-point argument being passed in a
3633 general register. If a struct or union, and if
3634 the remaining length is smaller than the register
3635 size, we have to adjust the register value on
3636 big endian targets.
3637
3638 It does not seem to be necessary to do the
1a69e1e4 3639 same for integral types. */
cb3d25d1 3640
72a155b4 3641 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
1a69e1e4 3642 && partial_len < MIPS64_REGSIZE
06f9a1af
MR
3643 && (typecode == TYPE_CODE_STRUCT
3644 || typecode == TYPE_CODE_UNION))
1a69e1e4 3645 regval <<= ((MIPS64_REGSIZE - partial_len)
9ecf7166 3646 * TARGET_CHAR_BIT);
cb3d25d1
MS
3647
3648 if (mips_debug)
3649 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
3650 argreg,
1a69e1e4 3651 phex (regval, MIPS64_REGSIZE));
9c9acae0 3652 regcache_cooked_write_unsigned (regcache, argreg, regval);
8d26208a 3653
74ed0bb4 3654 if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
8d26208a
DJ
3655 TYPE_LENGTH (arg_type) - len))
3656 {
3657 if (mips_debug)
3658 fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
3659 float_argreg,
3660 phex (regval, MIPS64_REGSIZE));
3661 regcache_cooked_write_unsigned (regcache, float_argreg,
3662 regval);
3663 }
3664
3665 float_argreg++;
cb3d25d1
MS
3666 argreg++;
3667 }
3668
3669 len -= partial_len;
3670 val += partial_len;
3671
b021a221
MS
3672 /* Compute the offset into the stack at which we will
3673 copy the next parameter.
cb3d25d1
MS
3674
3675 In N32 (N64?), the stack_offset only needs to be
3676 adjusted when it has been used. */
3677
3678 if (stack_used_p)
1a69e1e4 3679 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
cb3d25d1
MS
3680 }
3681 }
3682 if (mips_debug)
3683 fprintf_unfiltered (gdb_stdlog, "\n");
3684 }
3685
f10683bb 3686 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 3687
cb3d25d1
MS
3688 /* Return adjusted stack pointer. */
3689 return sp;
3690}
3691
6d82d43b 3692static enum return_value_convention
c055b101 3693mips_n32n64_return_value (struct gdbarch *gdbarch, struct type *func_type,
6d82d43b 3694 struct type *type, struct regcache *regcache,
47a35522 3695 gdb_byte *readbuf, const gdb_byte *writebuf)
ebafbe83 3696{
72a155b4 3697 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
b18bb924
MR
3698
3699 /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004
3700
3701 Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
3702 if needed), as appropriate for the type. Composite results (struct,
3703 union, or array) are returned in $2/$f0 and $3/$f2 according to the
3704 following rules:
3705
3706 * A struct with only one or two floating point fields is returned in $f0
3707 (and $f2 if necessary). This is a generalization of the Fortran COMPLEX
3708 case.
3709
f08877ba 3710 * Any other composite results of at most 128 bits are returned in
b18bb924
MR
3711 $2 (first 64 bits) and $3 (remainder, if necessary).
3712
3713 * Larger composite results are handled by converting the function to a
3714 procedure with an implicit first parameter, which is a pointer to an area
3715 reserved by the caller to receive the result. [The o32-bit ABI requires
3716 that all composite results be handled by conversion to implicit first
3717 parameters. The MIPS/SGI Fortran implementation has always made a
3718 specific exception to return COMPLEX results in the floating point
3719 registers.] */
3720
f08877ba 3721 if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
6d82d43b 3722 return RETURN_VALUE_STRUCT_CONVENTION;
d05f6826
DJ
3723 else if (TYPE_CODE (type) == TYPE_CODE_FLT
3724 && TYPE_LENGTH (type) == 16
3725 && tdep->mips_fpu_type != MIPS_FPU_NONE)
3726 {
3727 /* A 128-bit floating-point value fills both $f0 and $f2. The
3728 two registers are used in the same as memory order, so the
3729 eight bytes with the lower memory address are in $f0. */
3730 if (mips_debug)
3731 fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
ba32f989 3732 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
3733 (gdbarch_num_regs (gdbarch)
3734 + mips_regnum (gdbarch)->fp0),
72a155b4 3735 8, gdbarch_byte_order (gdbarch),
4c6b5505 3736 readbuf, writebuf, 0);
ba32f989 3737 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
3738 (gdbarch_num_regs (gdbarch)
3739 + mips_regnum (gdbarch)->fp0 + 2),
72a155b4 3740 8, gdbarch_byte_order (gdbarch),
4c6b5505 3741 readbuf ? readbuf + 8 : readbuf,
d05f6826
DJ
3742 writebuf ? writebuf + 8 : writebuf, 0);
3743 return RETURN_VALUE_REGISTER_CONVENTION;
3744 }
6d82d43b
AC
3745 else if (TYPE_CODE (type) == TYPE_CODE_FLT
3746 && tdep->mips_fpu_type != MIPS_FPU_NONE)
3747 {
59aa1faa 3748 /* A single or double floating-point value that fits in FP0. */
6d82d43b
AC
3749 if (mips_debug)
3750 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
ba32f989 3751 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
3752 (gdbarch_num_regs (gdbarch)
3753 + mips_regnum (gdbarch)->fp0),
6d82d43b 3754 TYPE_LENGTH (type),
72a155b4 3755 gdbarch_byte_order (gdbarch),
4c6b5505 3756 readbuf, writebuf, 0);
6d82d43b
AC
3757 return RETURN_VALUE_REGISTER_CONVENTION;
3758 }
3759 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3760 && TYPE_NFIELDS (type) <= 2
3761 && TYPE_NFIELDS (type) >= 1
3762 && ((TYPE_NFIELDS (type) == 1
b18bb924 3763 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
6d82d43b
AC
3764 == TYPE_CODE_FLT))
3765 || (TYPE_NFIELDS (type) == 2
b18bb924 3766 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
6d82d43b 3767 == TYPE_CODE_FLT)
b18bb924 3768 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
5b68030f 3769 == TYPE_CODE_FLT))))
6d82d43b
AC
3770 {
3771 /* A struct that contains one or two floats. Each value is part
3772 in the least significant part of their floating point
5b68030f 3773 register (or GPR, for soft float). */
6d82d43b
AC
3774 int regnum;
3775 int field;
5b68030f
JM
3776 for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
3777 ? mips_regnum (gdbarch)->fp0
3778 : MIPS_V0_REGNUM);
6d82d43b
AC
3779 field < TYPE_NFIELDS (type); field++, regnum += 2)
3780 {
3781 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
3782 / TARGET_CHAR_BIT);
3783 if (mips_debug)
3784 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
3785 offset);
5b68030f
JM
3786 if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
3787 {
3788 /* A 16-byte long double field goes in two consecutive
3789 registers. */
3790 mips_xfer_register (gdbarch, regcache,
3791 gdbarch_num_regs (gdbarch) + regnum,
3792 8,
3793 gdbarch_byte_order (gdbarch),
3794 readbuf, writebuf, offset);
3795 mips_xfer_register (gdbarch, regcache,
3796 gdbarch_num_regs (gdbarch) + regnum + 1,
3797 8,
3798 gdbarch_byte_order (gdbarch),
3799 readbuf, writebuf, offset + 8);
3800 }
3801 else
3802 mips_xfer_register (gdbarch, regcache,
3803 gdbarch_num_regs (gdbarch) + regnum,
3804 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
3805 gdbarch_byte_order (gdbarch),
3806 readbuf, writebuf, offset);
6d82d43b
AC
3807 }
3808 return RETURN_VALUE_REGISTER_CONVENTION;
3809 }
3810 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
f08877ba
JB
3811 || TYPE_CODE (type) == TYPE_CODE_UNION
3812 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
6d82d43b 3813 {
f08877ba 3814 /* A composite type. Extract the left justified value,
6d82d43b
AC
3815 regardless of the byte order. I.e. DO NOT USE
3816 mips_xfer_lower. */
3817 int offset;
3818 int regnum;
4c7d22cb 3819 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b 3820 offset < TYPE_LENGTH (type);
72a155b4 3821 offset += register_size (gdbarch, regnum), regnum++)
6d82d43b 3822 {
72a155b4 3823 int xfer = register_size (gdbarch, regnum);
6d82d43b
AC
3824 if (offset + xfer > TYPE_LENGTH (type))
3825 xfer = TYPE_LENGTH (type) - offset;
3826 if (mips_debug)
3827 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
3828 offset, xfer, regnum);
ba32f989
DJ
3829 mips_xfer_register (gdbarch, regcache,
3830 gdbarch_num_regs (gdbarch) + regnum,
72a155b4
UW
3831 xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
3832 offset);
6d82d43b
AC
3833 }
3834 return RETURN_VALUE_REGISTER_CONVENTION;
3835 }
3836 else
3837 {
3838 /* A scalar extract each part but least-significant-byte
3839 justified. */
3840 int offset;
3841 int regnum;
4c7d22cb 3842 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b 3843 offset < TYPE_LENGTH (type);
72a155b4 3844 offset += register_size (gdbarch, regnum), regnum++)
6d82d43b 3845 {
72a155b4 3846 int xfer = register_size (gdbarch, regnum);
6d82d43b
AC
3847 if (offset + xfer > TYPE_LENGTH (type))
3848 xfer = TYPE_LENGTH (type) - offset;
3849 if (mips_debug)
3850 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
3851 offset, xfer, regnum);
ba32f989
DJ
3852 mips_xfer_register (gdbarch, regcache,
3853 gdbarch_num_regs (gdbarch) + regnum,
72a155b4 3854 xfer, gdbarch_byte_order (gdbarch),
4c6b5505 3855 readbuf, writebuf, offset);
6d82d43b
AC
3856 }
3857 return RETURN_VALUE_REGISTER_CONVENTION;
3858 }
3859}
3860
3861/* O32 ABI stuff. */
3862
3863static CORE_ADDR
7d9b040b 3864mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
3865 struct regcache *regcache, CORE_ADDR bp_addr,
3866 int nargs, struct value **args, CORE_ADDR sp,
3867 int struct_return, CORE_ADDR struct_addr)
3868{
3869 int argreg;
3870 int float_argreg;
3871 int argnum;
3872 int len = 0;
3873 int stack_offset = 0;
3874 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 3875 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7d9b040b 3876 CORE_ADDR func_addr = find_function_addr (function, NULL);
6d82d43b
AC
3877
3878 /* For shared libraries, "t9" needs to point at the function
3879 address. */
4c7d22cb 3880 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
6d82d43b
AC
3881
3882 /* Set the return address register to point to the entry point of
3883 the program, where a breakpoint lies in wait. */
4c7d22cb 3884 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
6d82d43b
AC
3885
3886 /* First ensure that the stack and structure return address (if any)
3887 are properly aligned. The stack has to be at least 64-bit
3888 aligned even on 32-bit machines, because doubles must be 64-bit
ebafbe83
MS
3889 aligned. For n32 and n64, stack frames need to be 128-bit
3890 aligned, so we round to this widest known alignment. */
3891
5b03f266
AC
3892 sp = align_down (sp, 16);
3893 struct_addr = align_down (struct_addr, 16);
ebafbe83
MS
3894
3895 /* Now make space on the stack for the args. */
3896 for (argnum = 0; argnum < nargs; argnum++)
968b5391
MR
3897 {
3898 struct type *arg_type = check_typedef (value_type (args[argnum]));
3899 int arglen = TYPE_LENGTH (arg_type);
3900
3901 /* Align to double-word if necessary. */
2afd3f0a 3902 if (mips_type_needs_double_align (arg_type))
1a69e1e4 3903 len = align_up (len, MIPS32_REGSIZE * 2);
968b5391 3904 /* Allocate space on the stack. */
1a69e1e4 3905 len += align_up (arglen, MIPS32_REGSIZE);
968b5391 3906 }
5b03f266 3907 sp -= align_up (len, 16);
ebafbe83
MS
3908
3909 if (mips_debug)
6d82d43b 3910 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
3911 "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
3912 paddress (gdbarch, sp), (long) align_up (len, 16));
ebafbe83
MS
3913
3914 /* Initialize the integer and float register pointers. */
4c7d22cb 3915 argreg = MIPS_A0_REGNUM;
72a155b4 3916 float_argreg = mips_fpa0_regnum (gdbarch);
ebafbe83 3917
bcb0cc15 3918 /* The struct_return pointer occupies the first parameter-passing reg. */
ebafbe83
MS
3919 if (struct_return)
3920 {
3921 if (mips_debug)
3922 fprintf_unfiltered (gdb_stdlog,
025bb325
MS
3923 "mips_o32_push_dummy_call: "
3924 "struct_return reg=%d %s\n",
5af949e3 3925 argreg, paddress (gdbarch, struct_addr));
9c9acae0 3926 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
1a69e1e4 3927 stack_offset += MIPS32_REGSIZE;
ebafbe83
MS
3928 }
3929
3930 /* Now load as many as possible of the first arguments into
3931 registers, and push the rest onto the stack. Loop thru args
3932 from first to last. */
3933 for (argnum = 0; argnum < nargs; argnum++)
3934 {
47a35522 3935 const gdb_byte *val;
ebafbe83 3936 struct value *arg = args[argnum];
4991999e 3937 struct type *arg_type = check_typedef (value_type (arg));
ebafbe83
MS
3938 int len = TYPE_LENGTH (arg_type);
3939 enum type_code typecode = TYPE_CODE (arg_type);
3940
3941 if (mips_debug)
3942 fprintf_unfiltered (gdb_stdlog,
25ab4790 3943 "mips_o32_push_dummy_call: %d len=%d type=%d",
46cac009
AC
3944 argnum + 1, len, (int) typecode);
3945
47a35522 3946 val = value_contents (arg);
46cac009
AC
3947
3948 /* 32-bit ABIs always start floating point arguments in an
3949 even-numbered floating point register. Round the FP register
3950 up before the check to see if there are any FP registers
3951 left. O32/O64 targets also pass the FP in the integer
3952 registers so also round up normal registers. */
74ed0bb4 3953 if (fp_register_arg_p (gdbarch, typecode, arg_type))
46cac009
AC
3954 {
3955 if ((float_argreg & 1))
3956 float_argreg++;
3957 }
3958
3959 /* Floating point arguments passed in registers have to be
3960 treated specially. On 32-bit architectures, doubles
3961 are passed in register pairs; the even register gets
3962 the low word, and the odd register gets the high word.
3963 On O32/O64, the first two floating point arguments are
3964 also copied to general registers, because MIPS16 functions
3965 don't use float registers for arguments. This duplication of
3966 arguments in general registers can't hurt non-MIPS16 functions
3967 because those registers are normally skipped. */
3968
74ed0bb4
MD
3969 if (fp_register_arg_p (gdbarch, typecode, arg_type)
3970 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
46cac009 3971 {
8b07f6d8 3972 if (register_size (gdbarch, float_argreg) < 8 && len == 8)
46cac009 3973 {
72a155b4 3974 int low_offset = gdbarch_byte_order (gdbarch)
4c6b5505 3975 == BFD_ENDIAN_BIG ? 4 : 0;
46cac009
AC
3976 unsigned long regval;
3977
3978 /* Write the low word of the double to the even register(s). */
e17a4113
UW
3979 regval = extract_unsigned_integer (val + low_offset,
3980 4, byte_order);
46cac009
AC
3981 if (mips_debug)
3982 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3983 float_argreg, phex (regval, 4));
025bb325
MS
3984 regcache_cooked_write_unsigned (regcache,
3985 float_argreg++, regval);
46cac009
AC
3986 if (mips_debug)
3987 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
3988 argreg, phex (regval, 4));
9c9acae0 3989 regcache_cooked_write_unsigned (regcache, argreg++, regval);
46cac009
AC
3990
3991 /* Write the high word of the double to the odd register(s). */
e17a4113
UW
3992 regval = extract_unsigned_integer (val + 4 - low_offset,
3993 4, byte_order);
46cac009
AC
3994 if (mips_debug)
3995 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
3996 float_argreg, phex (regval, 4));
025bb325
MS
3997 regcache_cooked_write_unsigned (regcache,
3998 float_argreg++, regval);
46cac009
AC
3999
4000 if (mips_debug)
4001 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4002 argreg, phex (regval, 4));
9c9acae0 4003 regcache_cooked_write_unsigned (regcache, argreg++, regval);
46cac009
AC
4004 }
4005 else
4006 {
4007 /* This is a floating point value that fits entirely
4008 in a single register. */
4009 /* On 32 bit ABI's the float_argreg is further adjusted
6d82d43b 4010 above to ensure that it is even register aligned. */
e17a4113 4011 LONGEST regval = extract_unsigned_integer (val, len, byte_order);
46cac009
AC
4012 if (mips_debug)
4013 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4014 float_argreg, phex (regval, len));
025bb325
MS
4015 regcache_cooked_write_unsigned (regcache,
4016 float_argreg++, regval);
5b68030f
JM
4017 /* Although two FP registers are reserved for each
4018 argument, only one corresponding integer register is
4019 reserved. */
46cac009
AC
4020 if (mips_debug)
4021 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4022 argreg, phex (regval, len));
5b68030f 4023 regcache_cooked_write_unsigned (regcache, argreg++, regval);
46cac009
AC
4024 }
4025 /* Reserve space for the FP register. */
1a69e1e4 4026 stack_offset += align_up (len, MIPS32_REGSIZE);
46cac009
AC
4027 }
4028 else
4029 {
4030 /* Copy the argument to general registers or the stack in
4031 register-sized pieces. Large arguments are split between
4032 registers and stack. */
1a69e1e4
DJ
4033 /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
4034 are treated specially: Irix cc passes
d5ac5a39
AC
4035 them in registers where gcc sometimes puts them on the
4036 stack. For maximum compatibility, we will put them in
4037 both places. */
1a69e1e4
DJ
4038 int odd_sized_struct = (len > MIPS32_REGSIZE
4039 && len % MIPS32_REGSIZE != 0);
46cac009
AC
4040 /* Structures should be aligned to eight bytes (even arg registers)
4041 on MIPS_ABI_O32, if their first member has double precision. */
2afd3f0a 4042 if (mips_type_needs_double_align (arg_type))
46cac009
AC
4043 {
4044 if ((argreg & 1))
968b5391
MR
4045 {
4046 argreg++;
1a69e1e4 4047 stack_offset += MIPS32_REGSIZE;
968b5391 4048 }
46cac009 4049 }
46cac009
AC
4050 while (len > 0)
4051 {
4052 /* Remember if the argument was written to the stack. */
4053 int stack_used_p = 0;
1a69e1e4 4054 int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);
46cac009
AC
4055
4056 if (mips_debug)
4057 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4058 partial_len);
4059
4060 /* Write this portion of the argument to the stack. */
74ed0bb4 4061 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
968b5391 4062 || odd_sized_struct)
46cac009
AC
4063 {
4064 /* Should shorter than int integer values be
025bb325 4065 promoted to int before being stored? */
46cac009
AC
4066 int longword_offset = 0;
4067 CORE_ADDR addr;
4068 stack_used_p = 1;
46cac009
AC
4069
4070 if (mips_debug)
4071 {
5af949e3
UW
4072 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4073 paddress (gdbarch, stack_offset));
4074 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4075 paddress (gdbarch, longword_offset));
46cac009
AC
4076 }
4077
4078 addr = sp + stack_offset + longword_offset;
4079
4080 if (mips_debug)
4081 {
4082 int i;
5af949e3
UW
4083 fprintf_unfiltered (gdb_stdlog, " @%s ",
4084 paddress (gdbarch, addr));
46cac009
AC
4085 for (i = 0; i < partial_len; i++)
4086 {
6d82d43b 4087 fprintf_unfiltered (gdb_stdlog, "%02x",
46cac009
AC
4088 val[i] & 0xff);
4089 }
4090 }
4091 write_memory (addr, val, partial_len);
4092 }
4093
4094 /* Note!!! This is NOT an else clause. Odd sized
968b5391 4095 structs may go thru BOTH paths. */
46cac009 4096 /* Write this portion of the argument to a general
6d82d43b 4097 purpose register. */
74ed0bb4 4098 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
46cac009 4099 {
e17a4113
UW
4100 LONGEST regval = extract_signed_integer (val, partial_len,
4101 byte_order);
4246e332 4102 /* Value may need to be sign extended, because
1b13c4f6 4103 mips_isa_regsize() != mips_abi_regsize(). */
46cac009
AC
4104
4105 /* A non-floating-point argument being passed in a
4106 general register. If a struct or union, and if
4107 the remaining length is smaller than the register
4108 size, we have to adjust the register value on
4109 big endian targets.
4110
4111 It does not seem to be necessary to do the
4112 same for integral types.
4113
4114 Also don't do this adjustment on O64 binaries.
4115
4116 cagney/2001-07-23: gdb/179: Also, GCC, when
4117 outputting LE O32 with sizeof (struct) <
e914cb17
MR
4118 mips_abi_regsize(), generates a left shift
4119 as part of storing the argument in a register
4120 (the left shift isn't generated when
1b13c4f6 4121 sizeof (struct) >= mips_abi_regsize()). Since
480d3dd2
AC
4122 it is quite possible that this is GCC
4123 contradicting the LE/O32 ABI, GDB has not been
4124 adjusted to accommodate this. Either someone
4125 needs to demonstrate that the LE/O32 ABI
4126 specifies such a left shift OR this new ABI gets
4127 identified as such and GDB gets tweaked
4128 accordingly. */
4129
72a155b4 4130 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
1a69e1e4 4131 && partial_len < MIPS32_REGSIZE
06f9a1af
MR
4132 && (typecode == TYPE_CODE_STRUCT
4133 || typecode == TYPE_CODE_UNION))
1a69e1e4 4134 regval <<= ((MIPS32_REGSIZE - partial_len)
9ecf7166 4135 * TARGET_CHAR_BIT);
46cac009
AC
4136
4137 if (mips_debug)
4138 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4139 argreg,
1a69e1e4 4140 phex (regval, MIPS32_REGSIZE));
9c9acae0 4141 regcache_cooked_write_unsigned (regcache, argreg, regval);
46cac009
AC
4142 argreg++;
4143
4144 /* Prevent subsequent floating point arguments from
4145 being passed in floating point registers. */
74ed0bb4 4146 float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
46cac009
AC
4147 }
4148
4149 len -= partial_len;
4150 val += partial_len;
4151
b021a221
MS
4152 /* Compute the offset into the stack at which we will
4153 copy the next parameter.
46cac009 4154
6d82d43b
AC
4155 In older ABIs, the caller reserved space for
4156 registers that contained arguments. This was loosely
4157 refered to as their "home". Consequently, space is
4158 always allocated. */
46cac009 4159
1a69e1e4 4160 stack_offset += align_up (partial_len, MIPS32_REGSIZE);
46cac009
AC
4161 }
4162 }
4163 if (mips_debug)
4164 fprintf_unfiltered (gdb_stdlog, "\n");
4165 }
4166
f10683bb 4167 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 4168
46cac009
AC
4169 /* Return adjusted stack pointer. */
4170 return sp;
4171}
4172
6d82d43b 4173static enum return_value_convention
c055b101
CV
4174mips_o32_return_value (struct gdbarch *gdbarch, struct type *func_type,
4175 struct type *type, struct regcache *regcache,
47a35522 4176 gdb_byte *readbuf, const gdb_byte *writebuf)
6d82d43b 4177{
72a155b4 4178 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6d82d43b
AC
4179
4180 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4181 || TYPE_CODE (type) == TYPE_CODE_UNION
4182 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
4183 return RETURN_VALUE_STRUCT_CONVENTION;
4184 else if (TYPE_CODE (type) == TYPE_CODE_FLT
4185 && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
4186 {
4187 /* A single-precision floating-point value. It fits in the
4188 least significant part of FP0. */
4189 if (mips_debug)
4190 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
ba32f989 4191 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4192 (gdbarch_num_regs (gdbarch)
4193 + mips_regnum (gdbarch)->fp0),
6d82d43b 4194 TYPE_LENGTH (type),
72a155b4 4195 gdbarch_byte_order (gdbarch),
4c6b5505 4196 readbuf, writebuf, 0);
6d82d43b
AC
4197 return RETURN_VALUE_REGISTER_CONVENTION;
4198 }
4199 else if (TYPE_CODE (type) == TYPE_CODE_FLT
4200 && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
4201 {
4202 /* A double-precision floating-point value. The most
4203 significant part goes in FP1, and the least significant in
4204 FP0. */
4205 if (mips_debug)
4206 fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
72a155b4 4207 switch (gdbarch_byte_order (gdbarch))
6d82d43b
AC
4208 {
4209 case BFD_ENDIAN_LITTLE:
ba32f989 4210 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4211 (gdbarch_num_regs (gdbarch)
4212 + mips_regnum (gdbarch)->fp0 + 0),
4213 4, gdbarch_byte_order (gdbarch),
4c6b5505 4214 readbuf, writebuf, 0);
ba32f989 4215 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4216 (gdbarch_num_regs (gdbarch)
4217 + mips_regnum (gdbarch)->fp0 + 1),
72a155b4 4218 4, gdbarch_byte_order (gdbarch),
4c6b5505 4219 readbuf, writebuf, 4);
6d82d43b
AC
4220 break;
4221 case BFD_ENDIAN_BIG:
ba32f989 4222 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4223 (gdbarch_num_regs (gdbarch)
4224 + mips_regnum (gdbarch)->fp0 + 1),
72a155b4 4225 4, gdbarch_byte_order (gdbarch),
4c6b5505 4226 readbuf, writebuf, 0);
ba32f989 4227 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4228 (gdbarch_num_regs (gdbarch)
4229 + mips_regnum (gdbarch)->fp0 + 0),
72a155b4 4230 4, gdbarch_byte_order (gdbarch),
4c6b5505 4231 readbuf, writebuf, 4);
6d82d43b
AC
4232 break;
4233 default:
e2e0b3e5 4234 internal_error (__FILE__, __LINE__, _("bad switch"));
6d82d43b
AC
4235 }
4236 return RETURN_VALUE_REGISTER_CONVENTION;
4237 }
4238#if 0
4239 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4240 && TYPE_NFIELDS (type) <= 2
4241 && TYPE_NFIELDS (type) >= 1
4242 && ((TYPE_NFIELDS (type) == 1
4243 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4244 == TYPE_CODE_FLT))
4245 || (TYPE_NFIELDS (type) == 2
4246 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
4247 == TYPE_CODE_FLT)
4248 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
4249 == TYPE_CODE_FLT)))
4250 && tdep->mips_fpu_type != MIPS_FPU_NONE)
4251 {
4252 /* A struct that contains one or two floats. Each value is part
4253 in the least significant part of their floating point
4254 register.. */
870cd05e 4255 gdb_byte reg[MAX_REGISTER_SIZE];
6d82d43b
AC
4256 int regnum;
4257 int field;
72a155b4 4258 for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
6d82d43b
AC
4259 field < TYPE_NFIELDS (type); field++, regnum += 2)
4260 {
4261 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
4262 / TARGET_CHAR_BIT);
4263 if (mips_debug)
4264 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
4265 offset);
ba32f989
DJ
4266 mips_xfer_register (gdbarch, regcache,
4267 gdbarch_num_regs (gdbarch) + regnum,
6d82d43b 4268 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
72a155b4 4269 gdbarch_byte_order (gdbarch),
4c6b5505 4270 readbuf, writebuf, offset);
6d82d43b
AC
4271 }
4272 return RETURN_VALUE_REGISTER_CONVENTION;
4273 }
4274#endif
4275#if 0
4276 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4277 || TYPE_CODE (type) == TYPE_CODE_UNION)
4278 {
4279 /* A structure or union. Extract the left justified value,
4280 regardless of the byte order. I.e. DO NOT USE
4281 mips_xfer_lower. */
4282 int offset;
4283 int regnum;
4c7d22cb 4284 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b 4285 offset < TYPE_LENGTH (type);
72a155b4 4286 offset += register_size (gdbarch, regnum), regnum++)
6d82d43b 4287 {
72a155b4 4288 int xfer = register_size (gdbarch, regnum);
6d82d43b
AC
4289 if (offset + xfer > TYPE_LENGTH (type))
4290 xfer = TYPE_LENGTH (type) - offset;
4291 if (mips_debug)
4292 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
4293 offset, xfer, regnum);
ba32f989
DJ
4294 mips_xfer_register (gdbarch, regcache,
4295 gdbarch_num_regs (gdbarch) + regnum, xfer,
6d82d43b
AC
4296 BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
4297 }
4298 return RETURN_VALUE_REGISTER_CONVENTION;
4299 }
4300#endif
4301 else
4302 {
4303 /* A scalar extract each part but least-significant-byte
4304 justified. o32 thinks registers are 4 byte, regardless of
1a69e1e4 4305 the ISA. */
6d82d43b
AC
4306 int offset;
4307 int regnum;
4c7d22cb 4308 for (offset = 0, regnum = MIPS_V0_REGNUM;
6d82d43b 4309 offset < TYPE_LENGTH (type);
1a69e1e4 4310 offset += MIPS32_REGSIZE, regnum++)
6d82d43b 4311 {
1a69e1e4 4312 int xfer = MIPS32_REGSIZE;
6d82d43b
AC
4313 if (offset + xfer > TYPE_LENGTH (type))
4314 xfer = TYPE_LENGTH (type) - offset;
4315 if (mips_debug)
4316 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4317 offset, xfer, regnum);
ba32f989
DJ
4318 mips_xfer_register (gdbarch, regcache,
4319 gdbarch_num_regs (gdbarch) + regnum, xfer,
72a155b4 4320 gdbarch_byte_order (gdbarch),
4c6b5505 4321 readbuf, writebuf, offset);
6d82d43b
AC
4322 }
4323 return RETURN_VALUE_REGISTER_CONVENTION;
4324 }
4325}
4326
4327/* O64 ABI. This is a hacked up kind of 64-bit version of the o32
4328 ABI. */
46cac009
AC
4329
4330static CORE_ADDR
7d9b040b 4331mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6d82d43b
AC
4332 struct regcache *regcache, CORE_ADDR bp_addr,
4333 int nargs,
4334 struct value **args, CORE_ADDR sp,
4335 int struct_return, CORE_ADDR struct_addr)
46cac009
AC
4336{
4337 int argreg;
4338 int float_argreg;
4339 int argnum;
4340 int len = 0;
4341 int stack_offset = 0;
480d3dd2 4342 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 4343 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7d9b040b 4344 CORE_ADDR func_addr = find_function_addr (function, NULL);
46cac009 4345
25ab4790
AC
4346 /* For shared libraries, "t9" needs to point at the function
4347 address. */
4c7d22cb 4348 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
25ab4790
AC
4349
4350 /* Set the return address register to point to the entry point of
4351 the program, where a breakpoint lies in wait. */
4c7d22cb 4352 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
25ab4790 4353
46cac009
AC
4354 /* First ensure that the stack and structure return address (if any)
4355 are properly aligned. The stack has to be at least 64-bit
4356 aligned even on 32-bit machines, because doubles must be 64-bit
4357 aligned. For n32 and n64, stack frames need to be 128-bit
4358 aligned, so we round to this widest known alignment. */
4359
5b03f266
AC
4360 sp = align_down (sp, 16);
4361 struct_addr = align_down (struct_addr, 16);
46cac009
AC
4362
4363 /* Now make space on the stack for the args. */
4364 for (argnum = 0; argnum < nargs; argnum++)
968b5391
MR
4365 {
4366 struct type *arg_type = check_typedef (value_type (args[argnum]));
4367 int arglen = TYPE_LENGTH (arg_type);
4368
968b5391 4369 /* Allocate space on the stack. */
1a69e1e4 4370 len += align_up (arglen, MIPS64_REGSIZE);
968b5391 4371 }
5b03f266 4372 sp -= align_up (len, 16);
46cac009
AC
4373
4374 if (mips_debug)
6d82d43b 4375 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
4376 "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
4377 paddress (gdbarch, sp), (long) align_up (len, 16));
46cac009
AC
4378
4379 /* Initialize the integer and float register pointers. */
4c7d22cb 4380 argreg = MIPS_A0_REGNUM;
72a155b4 4381 float_argreg = mips_fpa0_regnum (gdbarch);
46cac009
AC
4382
4383 /* The struct_return pointer occupies the first parameter-passing reg. */
4384 if (struct_return)
4385 {
4386 if (mips_debug)
4387 fprintf_unfiltered (gdb_stdlog,
025bb325
MS
4388 "mips_o64_push_dummy_call: "
4389 "struct_return reg=%d %s\n",
5af949e3 4390 argreg, paddress (gdbarch, struct_addr));
9c9acae0 4391 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
1a69e1e4 4392 stack_offset += MIPS64_REGSIZE;
46cac009
AC
4393 }
4394
4395 /* Now load as many as possible of the first arguments into
4396 registers, and push the rest onto the stack. Loop thru args
4397 from first to last. */
4398 for (argnum = 0; argnum < nargs; argnum++)
4399 {
47a35522 4400 const gdb_byte *val;
930bd0e0 4401 gdb_byte valbuf[MAX_REGISTER_SIZE];
46cac009 4402 struct value *arg = args[argnum];
4991999e 4403 struct type *arg_type = check_typedef (value_type (arg));
46cac009
AC
4404 int len = TYPE_LENGTH (arg_type);
4405 enum type_code typecode = TYPE_CODE (arg_type);
4406
4407 if (mips_debug)
4408 fprintf_unfiltered (gdb_stdlog,
25ab4790 4409 "mips_o64_push_dummy_call: %d len=%d type=%d",
ebafbe83
MS
4410 argnum + 1, len, (int) typecode);
4411
47a35522 4412 val = value_contents (arg);
ebafbe83 4413
930bd0e0
KB
4414 /* Function pointer arguments to mips16 code need to be made into
4415 mips16 pointers. */
4416 if (typecode == TYPE_CODE_PTR
4417 && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
4418 {
4419 CORE_ADDR addr = extract_signed_integer (value_contents (arg),
4420 len, byte_order);
4421 if (mips_pc_is_mips16 (addr))
4422 {
4423 store_signed_integer (valbuf, len, byte_order,
4424 make_mips16_addr (addr));
4425 val = valbuf;
4426 }
4427 }
4428
ebafbe83
MS
4429 /* Floating point arguments passed in registers have to be
4430 treated specially. On 32-bit architectures, doubles
4431 are passed in register pairs; the even register gets
4432 the low word, and the odd register gets the high word.
4433 On O32/O64, the first two floating point arguments are
4434 also copied to general registers, because MIPS16 functions
4435 don't use float registers for arguments. This duplication of
4436 arguments in general registers can't hurt non-MIPS16 functions
4437 because those registers are normally skipped. */
4438
74ed0bb4
MD
4439 if (fp_register_arg_p (gdbarch, typecode, arg_type)
4440 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
ebafbe83 4441 {
e17a4113 4442 LONGEST regval = extract_unsigned_integer (val, len, byte_order);
2afd3f0a
MR
4443 if (mips_debug)
4444 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4445 float_argreg, phex (regval, len));
9c9acae0 4446 regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
2afd3f0a
MR
4447 if (mips_debug)
4448 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4449 argreg, phex (regval, len));
9c9acae0 4450 regcache_cooked_write_unsigned (regcache, argreg, regval);
2afd3f0a 4451 argreg++;
ebafbe83 4452 /* Reserve space for the FP register. */
1a69e1e4 4453 stack_offset += align_up (len, MIPS64_REGSIZE);
ebafbe83
MS
4454 }
4455 else
4456 {
4457 /* Copy the argument to general registers or the stack in
4458 register-sized pieces. Large arguments are split between
4459 registers and stack. */
1a69e1e4 4460 /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
436aafc4
MR
4461 are treated specially: Irix cc passes them in registers
4462 where gcc sometimes puts them on the stack. For maximum
4463 compatibility, we will put them in both places. */
1a69e1e4
DJ
4464 int odd_sized_struct = (len > MIPS64_REGSIZE
4465 && len % MIPS64_REGSIZE != 0);
ebafbe83
MS
4466 while (len > 0)
4467 {
4468 /* Remember if the argument was written to the stack. */
4469 int stack_used_p = 0;
1a69e1e4 4470 int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
ebafbe83
MS
4471
4472 if (mips_debug)
4473 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4474 partial_len);
4475
4476 /* Write this portion of the argument to the stack. */
74ed0bb4 4477 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
968b5391 4478 || odd_sized_struct)
ebafbe83
MS
4479 {
4480 /* Should shorter than int integer values be
025bb325 4481 promoted to int before being stored? */
ebafbe83
MS
4482 int longword_offset = 0;
4483 CORE_ADDR addr;
4484 stack_used_p = 1;
72a155b4 4485 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
ebafbe83 4486 {
1a69e1e4
DJ
4487 if ((typecode == TYPE_CODE_INT
4488 || typecode == TYPE_CODE_PTR
4489 || typecode == TYPE_CODE_FLT)
4490 && len <= 4)
4491 longword_offset = MIPS64_REGSIZE - len;
ebafbe83
MS
4492 }
4493
4494 if (mips_debug)
4495 {
5af949e3
UW
4496 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4497 paddress (gdbarch, stack_offset));
4498 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4499 paddress (gdbarch, longword_offset));
ebafbe83
MS
4500 }
4501
4502 addr = sp + stack_offset + longword_offset;
4503
4504 if (mips_debug)
4505 {
4506 int i;
5af949e3
UW
4507 fprintf_unfiltered (gdb_stdlog, " @%s ",
4508 paddress (gdbarch, addr));
ebafbe83
MS
4509 for (i = 0; i < partial_len; i++)
4510 {
6d82d43b 4511 fprintf_unfiltered (gdb_stdlog, "%02x",
ebafbe83
MS
4512 val[i] & 0xff);
4513 }
4514 }
4515 write_memory (addr, val, partial_len);
4516 }
4517
4518 /* Note!!! This is NOT an else clause. Odd sized
968b5391 4519 structs may go thru BOTH paths. */
ebafbe83 4520 /* Write this portion of the argument to a general
6d82d43b 4521 purpose register. */
74ed0bb4 4522 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
ebafbe83 4523 {
e17a4113
UW
4524 LONGEST regval = extract_signed_integer (val, partial_len,
4525 byte_order);
4246e332 4526 /* Value may need to be sign extended, because
1b13c4f6 4527 mips_isa_regsize() != mips_abi_regsize(). */
ebafbe83
MS
4528
4529 /* A non-floating-point argument being passed in a
4530 general register. If a struct or union, and if
4531 the remaining length is smaller than the register
4532 size, we have to adjust the register value on
4533 big endian targets.
4534
4535 It does not seem to be necessary to do the
025bb325 4536 same for integral types. */
480d3dd2 4537
72a155b4 4538 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
1a69e1e4 4539 && partial_len < MIPS64_REGSIZE
06f9a1af
MR
4540 && (typecode == TYPE_CODE_STRUCT
4541 || typecode == TYPE_CODE_UNION))
1a69e1e4 4542 regval <<= ((MIPS64_REGSIZE - partial_len)
9ecf7166 4543 * TARGET_CHAR_BIT);
ebafbe83
MS
4544
4545 if (mips_debug)
4546 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4547 argreg,
1a69e1e4 4548 phex (regval, MIPS64_REGSIZE));
9c9acae0 4549 regcache_cooked_write_unsigned (regcache, argreg, regval);
ebafbe83
MS
4550 argreg++;
4551
4552 /* Prevent subsequent floating point arguments from
4553 being passed in floating point registers. */
74ed0bb4 4554 float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
ebafbe83
MS
4555 }
4556
4557 len -= partial_len;
4558 val += partial_len;
4559
b021a221
MS
4560 /* Compute the offset into the stack at which we will
4561 copy the next parameter.
ebafbe83 4562
6d82d43b
AC
4563 In older ABIs, the caller reserved space for
4564 registers that contained arguments. This was loosely
4565 refered to as their "home". Consequently, space is
4566 always allocated. */
ebafbe83 4567
1a69e1e4 4568 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
ebafbe83
MS
4569 }
4570 }
4571 if (mips_debug)
4572 fprintf_unfiltered (gdb_stdlog, "\n");
4573 }
4574
f10683bb 4575 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
310e9b6a 4576
ebafbe83
MS
4577 /* Return adjusted stack pointer. */
4578 return sp;
4579}
4580
9c8fdbfa 4581static enum return_value_convention
c055b101 4582mips_o64_return_value (struct gdbarch *gdbarch, struct type *func_type,
9c8fdbfa 4583 struct type *type, struct regcache *regcache,
47a35522 4584 gdb_byte *readbuf, const gdb_byte *writebuf)
6d82d43b 4585{
72a155b4 4586 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
7a076fd2
FF
4587
4588 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
4589 || TYPE_CODE (type) == TYPE_CODE_UNION
4590 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
4591 return RETURN_VALUE_STRUCT_CONVENTION;
74ed0bb4 4592 else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
7a076fd2
FF
4593 {
4594 /* A floating-point value. It fits in the least significant
4595 part of FP0. */
4596 if (mips_debug)
4597 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
ba32f989 4598 mips_xfer_register (gdbarch, regcache,
dca9aa3a
MR
4599 (gdbarch_num_regs (gdbarch)
4600 + mips_regnum (gdbarch)->fp0),
7a076fd2 4601 TYPE_LENGTH (type),
72a155b4 4602 gdbarch_byte_order (gdbarch),
4c6b5505 4603 readbuf, writebuf, 0);
7a076fd2
FF
4604 return RETURN_VALUE_REGISTER_CONVENTION;
4605 }
4606 else
4607 {
4608 /* A scalar extract each part but least-significant-byte
025bb325 4609 justified. */
7a076fd2
FF
4610 int offset;
4611 int regnum;
4612 for (offset = 0, regnum = MIPS_V0_REGNUM;
4613 offset < TYPE_LENGTH (type);
1a69e1e4 4614 offset += MIPS64_REGSIZE, regnum++)
7a076fd2 4615 {
1a69e1e4 4616 int xfer = MIPS64_REGSIZE;
7a076fd2
FF
4617 if (offset + xfer > TYPE_LENGTH (type))
4618 xfer = TYPE_LENGTH (type) - offset;
4619 if (mips_debug)
4620 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
4621 offset, xfer, regnum);
ba32f989
DJ
4622 mips_xfer_register (gdbarch, regcache,
4623 gdbarch_num_regs (gdbarch) + regnum,
72a155b4 4624 xfer, gdbarch_byte_order (gdbarch),
4c6b5505 4625 readbuf, writebuf, offset);
7a076fd2
FF
4626 }
4627 return RETURN_VALUE_REGISTER_CONVENTION;
4628 }
6d82d43b
AC
4629}
4630
dd824b04
DJ
4631/* Floating point register management.
4632
4633 Background: MIPS1 & 2 fp registers are 32 bits wide. To support
4634 64bit operations, these early MIPS cpus treat fp register pairs
4635 (f0,f1) as a single register (d0). Later MIPS cpu's have 64 bit fp
4636 registers and offer a compatibility mode that emulates the MIPS2 fp
4637 model. When operating in MIPS2 fp compat mode, later cpu's split
4638 double precision floats into two 32-bit chunks and store them in
4639 consecutive fp regs. To display 64-bit floats stored in this
4640 fashion, we have to combine 32 bits from f0 and 32 bits from f1.
4641 Throw in user-configurable endianness and you have a real mess.
4642
4643 The way this works is:
4644 - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
4645 double-precision value will be split across two logical registers.
4646 The lower-numbered logical register will hold the low-order bits,
4647 regardless of the processor's endianness.
4648 - If we are on a 64-bit processor, and we are looking for a
4649 single-precision value, it will be in the low ordered bits
4650 of a 64-bit GPR (after mfc1, for example) or a 64-bit register
4651 save slot in memory.
4652 - If we are in 64-bit mode, everything is straightforward.
4653
4654 Note that this code only deals with "live" registers at the top of the
4655 stack. We will attempt to deal with saved registers later, when
025bb325 4656 the raw/cooked register interface is in place. (We need a general
dd824b04
DJ
4657 interface that can deal with dynamic saved register sizes -- fp
4658 regs could be 32 bits wide in one frame and 64 on the frame above
4659 and below). */
4660
4661/* Copy a 32-bit single-precision value from the current frame
4662 into rare_buffer. */
4663
4664static void
e11c53d2 4665mips_read_fp_register_single (struct frame_info *frame, int regno,
47a35522 4666 gdb_byte *rare_buffer)
dd824b04 4667{
72a155b4
UW
4668 struct gdbarch *gdbarch = get_frame_arch (frame);
4669 int raw_size = register_size (gdbarch, regno);
47a35522 4670 gdb_byte *raw_buffer = alloca (raw_size);
dd824b04 4671
e11c53d2 4672 if (!frame_register_read (frame, regno, raw_buffer))
c9f4d572 4673 error (_("can't read register %d (%s)"),
72a155b4 4674 regno, gdbarch_register_name (gdbarch, regno));
dd824b04
DJ
4675 if (raw_size == 8)
4676 {
4677 /* We have a 64-bit value for this register. Find the low-order
6d82d43b 4678 32 bits. */
dd824b04
DJ
4679 int offset;
4680
72a155b4 4681 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
dd824b04
DJ
4682 offset = 4;
4683 else
4684 offset = 0;
4685
4686 memcpy (rare_buffer, raw_buffer + offset, 4);
4687 }
4688 else
4689 {
4690 memcpy (rare_buffer, raw_buffer, 4);
4691 }
4692}
4693
4694/* Copy a 64-bit double-precision value from the current frame into
4695 rare_buffer. This may include getting half of it from the next
4696 register. */
4697
4698static void
e11c53d2 4699mips_read_fp_register_double (struct frame_info *frame, int regno,
47a35522 4700 gdb_byte *rare_buffer)
dd824b04 4701{
72a155b4
UW
4702 struct gdbarch *gdbarch = get_frame_arch (frame);
4703 int raw_size = register_size (gdbarch, regno);
dd824b04 4704
9c9acae0 4705 if (raw_size == 8 && !mips2_fp_compat (frame))
dd824b04
DJ
4706 {
4707 /* We have a 64-bit value for this register, and we should use
6d82d43b 4708 all 64 bits. */
e11c53d2 4709 if (!frame_register_read (frame, regno, rare_buffer))
c9f4d572 4710 error (_("can't read register %d (%s)"),
72a155b4 4711 regno, gdbarch_register_name (gdbarch, regno));
dd824b04
DJ
4712 }
4713 else
4714 {
72a155b4 4715 int rawnum = regno % gdbarch_num_regs (gdbarch);
82e91389 4716
72a155b4 4717 if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
dd824b04 4718 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
4719 _("mips_read_fp_register_double: bad access to "
4720 "odd-numbered FP register"));
dd824b04
DJ
4721
4722 /* mips_read_fp_register_single will find the correct 32 bits from
6d82d43b 4723 each register. */
72a155b4 4724 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
dd824b04 4725 {
e11c53d2
AC
4726 mips_read_fp_register_single (frame, regno, rare_buffer + 4);
4727 mips_read_fp_register_single (frame, regno + 1, rare_buffer);
dd824b04 4728 }
361d1df0 4729 else
dd824b04 4730 {
e11c53d2
AC
4731 mips_read_fp_register_single (frame, regno, rare_buffer);
4732 mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
dd824b04
DJ
4733 }
4734 }
4735}
4736
c906108c 4737static void
e11c53d2
AC
4738mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
4739 int regnum)
025bb325 4740{ /* Do values for FP (float) regs. */
72a155b4 4741 struct gdbarch *gdbarch = get_frame_arch (frame);
47a35522 4742 gdb_byte *raw_buffer;
025bb325 4743 double doub, flt1; /* Doubles extracted from raw hex data. */
3903d437 4744 int inv1, inv2;
c5aa993b 4745
025bb325
MS
4746 raw_buffer = alloca (2 * register_size (gdbarch,
4747 mips_regnum (gdbarch)->fp0));
c906108c 4748
72a155b4 4749 fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
c9f4d572 4750 fprintf_filtered (file, "%*s",
72a155b4 4751 4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
e11c53d2 4752 "");
f0ef6b29 4753
72a155b4 4754 if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
c906108c 4755 {
79a45b7d
TT
4756 struct value_print_options opts;
4757
f0ef6b29
KB
4758 /* 4-byte registers: Print hex and floating. Also print even
4759 numbered registers as doubles. */
e11c53d2 4760 mips_read_fp_register_single (frame, regnum, raw_buffer);
025bb325
MS
4761 flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
4762 raw_buffer, &inv1);
c5aa993b 4763
79a45b7d 4764 get_formatted_print_options (&opts, 'x');
df4df182
UW
4765 print_scalar_formatted (raw_buffer,
4766 builtin_type (gdbarch)->builtin_uint32,
4767 &opts, 'w', file);
dd824b04 4768
e11c53d2 4769 fprintf_filtered (file, " flt: ");
1adad886 4770 if (inv1)
e11c53d2 4771 fprintf_filtered (file, " <invalid float> ");
1adad886 4772 else
e11c53d2 4773 fprintf_filtered (file, "%-17.9g", flt1);
1adad886 4774
72a155b4 4775 if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
f0ef6b29 4776 {
e11c53d2 4777 mips_read_fp_register_double (frame, regnum, raw_buffer);
27067745
UW
4778 doub = unpack_double (builtin_type (gdbarch)->builtin_double,
4779 raw_buffer, &inv2);
1adad886 4780
e11c53d2 4781 fprintf_filtered (file, " dbl: ");
f0ef6b29 4782 if (inv2)
e11c53d2 4783 fprintf_filtered (file, "<invalid double>");
f0ef6b29 4784 else
e11c53d2 4785 fprintf_filtered (file, "%-24.17g", doub);
f0ef6b29 4786 }
c906108c
SS
4787 }
4788 else
dd824b04 4789 {
79a45b7d
TT
4790 struct value_print_options opts;
4791
f0ef6b29 4792 /* Eight byte registers: print each one as hex, float and double. */
e11c53d2 4793 mips_read_fp_register_single (frame, regnum, raw_buffer);
27067745
UW
4794 flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
4795 raw_buffer, &inv1);
c906108c 4796
e11c53d2 4797 mips_read_fp_register_double (frame, regnum, raw_buffer);
27067745
UW
4798 doub = unpack_double (builtin_type (gdbarch)->builtin_double,
4799 raw_buffer, &inv2);
f0ef6b29 4800
79a45b7d 4801 get_formatted_print_options (&opts, 'x');
df4df182
UW
4802 print_scalar_formatted (raw_buffer,
4803 builtin_type (gdbarch)->builtin_uint64,
4804 &opts, 'g', file);
f0ef6b29 4805
e11c53d2 4806 fprintf_filtered (file, " flt: ");
1adad886 4807 if (inv1)
e11c53d2 4808 fprintf_filtered (file, "<invalid float>");
1adad886 4809 else
e11c53d2 4810 fprintf_filtered (file, "%-17.9g", flt1);
1adad886 4811
e11c53d2 4812 fprintf_filtered (file, " dbl: ");
f0ef6b29 4813 if (inv2)
e11c53d2 4814 fprintf_filtered (file, "<invalid double>");
1adad886 4815 else
e11c53d2 4816 fprintf_filtered (file, "%-24.17g", doub);
f0ef6b29
KB
4817 }
4818}
4819
4820static void
e11c53d2 4821mips_print_register (struct ui_file *file, struct frame_info *frame,
0cc93a06 4822 int regnum)
f0ef6b29 4823{
a4b8ebc8 4824 struct gdbarch *gdbarch = get_frame_arch (frame);
f0ef6b29 4825 int offset;
79a45b7d 4826 struct value_print_options opts;
de15c4ab 4827 struct value *val;
1adad886 4828
004159a2 4829 if (mips_float_register_p (gdbarch, regnum))
f0ef6b29 4830 {
e11c53d2 4831 mips_print_fp_register (file, frame, regnum);
f0ef6b29
KB
4832 return;
4833 }
4834
de15c4ab
PA
4835 val = get_frame_register_value (frame, regnum);
4836 if (value_optimized_out (val))
f0ef6b29 4837 {
c9f4d572 4838 fprintf_filtered (file, "%s: [Invalid]",
72a155b4 4839 gdbarch_register_name (gdbarch, regnum));
f0ef6b29 4840 return;
c906108c 4841 }
f0ef6b29 4842
72a155b4 4843 fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
f0ef6b29
KB
4844
4845 /* The problem with printing numeric register names (r26, etc.) is that
4846 the user can't use them on input. Probably the best solution is to
4847 fix it so that either the numeric or the funky (a2, etc.) names
4848 are accepted on input. */
4849 if (regnum < MIPS_NUMREGS)
e11c53d2 4850 fprintf_filtered (file, "(r%d): ", regnum);
f0ef6b29 4851 else
e11c53d2 4852 fprintf_filtered (file, ": ");
f0ef6b29 4853
79a45b7d 4854 get_formatted_print_options (&opts, 'x');
de15c4ab
PA
4855 val_print_scalar_formatted (value_type (val),
4856 value_contents_for_printing (val),
4857 value_embedded_offset (val),
4858 val,
4859 &opts, 0, file);
c906108c
SS
4860}
4861
f0ef6b29
KB
4862/* Replacement for generic do_registers_info.
4863 Print regs in pretty columns. */
4864
4865static int
e11c53d2
AC
4866print_fp_register_row (struct ui_file *file, struct frame_info *frame,
4867 int regnum)
f0ef6b29 4868{
e11c53d2
AC
4869 fprintf_filtered (file, " ");
4870 mips_print_fp_register (file, frame, regnum);
4871 fprintf_filtered (file, "\n");
f0ef6b29
KB
4872 return regnum + 1;
4873}
4874
4875
025bb325 4876/* Print a row's worth of GP (int) registers, with name labels above. */
c906108c
SS
4877
4878static int
e11c53d2 4879print_gp_register_row (struct ui_file *file, struct frame_info *frame,
a4b8ebc8 4880 int start_regnum)
c906108c 4881{
a4b8ebc8 4882 struct gdbarch *gdbarch = get_frame_arch (frame);
025bb325 4883 /* Do values for GP (int) regs. */
47a35522 4884 gdb_byte raw_buffer[MAX_REGISTER_SIZE];
025bb325
MS
4885 int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8); /* display cols
4886 per row. */
c906108c 4887 int col, byte;
a4b8ebc8 4888 int regnum;
c906108c 4889
025bb325 4890 /* For GP registers, we print a separate row of names above the vals. */
a4b8ebc8 4891 for (col = 0, regnum = start_regnum;
72a155b4
UW
4892 col < ncols && regnum < gdbarch_num_regs (gdbarch)
4893 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 4894 regnum++)
c906108c 4895 {
72a155b4 4896 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
c5aa993b 4897 continue; /* unused register */
004159a2 4898 if (mips_float_register_p (gdbarch, regnum))
025bb325 4899 break; /* End the row: reached FP register. */
0cc93a06 4900 /* Large registers are handled separately. */
72a155b4 4901 if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
0cc93a06
DJ
4902 {
4903 if (col > 0)
4904 break; /* End the row before this register. */
4905
4906 /* Print this register on a row by itself. */
4907 mips_print_register (file, frame, regnum);
4908 fprintf_filtered (file, "\n");
4909 return regnum + 1;
4910 }
d05f6826
DJ
4911 if (col == 0)
4912 fprintf_filtered (file, " ");
6d82d43b 4913 fprintf_filtered (file,
72a155b4
UW
4914 mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
4915 gdbarch_register_name (gdbarch, regnum));
c906108c
SS
4916 col++;
4917 }
d05f6826
DJ
4918
4919 if (col == 0)
4920 return regnum;
4921
025bb325 4922 /* Print the R0 to R31 names. */
72a155b4 4923 if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
f57d151a 4924 fprintf_filtered (file, "\n R%-4d",
72a155b4 4925 start_regnum % gdbarch_num_regs (gdbarch));
20e6603c
AC
4926 else
4927 fprintf_filtered (file, "\n ");
c906108c 4928
025bb325 4929 /* Now print the values in hex, 4 or 8 to the row. */
a4b8ebc8 4930 for (col = 0, regnum = start_regnum;
72a155b4
UW
4931 col < ncols && regnum < gdbarch_num_regs (gdbarch)
4932 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 4933 regnum++)
c906108c 4934 {
72a155b4 4935 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
c5aa993b 4936 continue; /* unused register */
004159a2 4937 if (mips_float_register_p (gdbarch, regnum))
025bb325 4938 break; /* End row: reached FP register. */
72a155b4 4939 if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
0cc93a06
DJ
4940 break; /* End row: large register. */
4941
c906108c 4942 /* OK: get the data in raw format. */
e11c53d2 4943 if (!frame_register_read (frame, regnum, raw_buffer))
c9f4d572 4944 error (_("can't read register %d (%s)"),
72a155b4 4945 regnum, gdbarch_register_name (gdbarch, regnum));
c906108c 4946 /* pad small registers */
4246e332 4947 for (byte = 0;
72a155b4
UW
4948 byte < (mips_abi_regsize (gdbarch)
4949 - register_size (gdbarch, regnum)); byte++)
c906108c 4950 printf_filtered (" ");
025bb325 4951 /* Now print the register value in hex, endian order. */
72a155b4 4952 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6d82d43b 4953 for (byte =
72a155b4
UW
4954 register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
4955 byte < register_size (gdbarch, regnum); byte++)
47a35522 4956 fprintf_filtered (file, "%02x", raw_buffer[byte]);
c906108c 4957 else
72a155b4 4958 for (byte = register_size (gdbarch, regnum) - 1;
6d82d43b 4959 byte >= 0; byte--)
47a35522 4960 fprintf_filtered (file, "%02x", raw_buffer[byte]);
e11c53d2 4961 fprintf_filtered (file, " ");
c906108c
SS
4962 col++;
4963 }
025bb325 4964 if (col > 0) /* ie. if we actually printed anything... */
e11c53d2 4965 fprintf_filtered (file, "\n");
c906108c
SS
4966
4967 return regnum;
4968}
4969
025bb325 4970/* MIPS_DO_REGISTERS_INFO(): called by "info register" command. */
c906108c 4971
bf1f5b4c 4972static void
e11c53d2
AC
4973mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
4974 struct frame_info *frame, int regnum, int all)
c906108c 4975{
025bb325 4976 if (regnum != -1) /* Do one specified register. */
c906108c 4977 {
72a155b4
UW
4978 gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
4979 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
8a3fe4f8 4980 error (_("Not a valid register for the current processor type"));
c906108c 4981
0cc93a06 4982 mips_print_register (file, frame, regnum);
e11c53d2 4983 fprintf_filtered (file, "\n");
c906108c 4984 }
c5aa993b 4985 else
025bb325 4986 /* Do all (or most) registers. */
c906108c 4987 {
72a155b4
UW
4988 regnum = gdbarch_num_regs (gdbarch);
4989 while (regnum < gdbarch_num_regs (gdbarch)
4990 + gdbarch_num_pseudo_regs (gdbarch))
c906108c 4991 {
004159a2 4992 if (mips_float_register_p (gdbarch, regnum))
e11c53d2 4993 {
025bb325 4994 if (all) /* True for "INFO ALL-REGISTERS" command. */
e11c53d2
AC
4995 regnum = print_fp_register_row (file, frame, regnum);
4996 else
025bb325 4997 regnum += MIPS_NUMREGS; /* Skip floating point regs. */
e11c53d2 4998 }
c906108c 4999 else
e11c53d2 5000 regnum = print_gp_register_row (file, frame, regnum);
c906108c
SS
5001 }
5002 }
5003}
5004
c906108c
SS
5005/* Is this a branch with a delay slot? */
5006
c906108c 5007static int
acdb74a0 5008is_delayed (unsigned long insn)
c906108c
SS
5009{
5010 int i;
5011 for (i = 0; i < NUMOPCODES; ++i)
5012 if (mips_opcodes[i].pinfo != INSN_MACRO
5013 && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
5014 break;
5015 return (i < NUMOPCODES
5016 && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
5017 | INSN_COND_BRANCH_DELAY
5018 | INSN_COND_BRANCH_LIKELY)));
5019}
5020
63807e1d 5021static int
3352ef37
AC
5022mips_single_step_through_delay (struct gdbarch *gdbarch,
5023 struct frame_info *frame)
c906108c 5024{
e17a4113 5025 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3352ef37 5026 CORE_ADDR pc = get_frame_pc (frame);
47a35522 5027 gdb_byte buf[MIPS_INSN32_SIZE];
c906108c
SS
5028
5029 /* There is no branch delay slot on MIPS16. */
0fe7e7c8 5030 if (mips_pc_is_mips16 (pc))
c906108c
SS
5031 return 0;
5032
6c95b8df 5033 if (!breakpoint_here_p (get_frame_address_space (frame), pc + 4))
06648491
MK
5034 return 0;
5035
3352ef37
AC
5036 if (!safe_frame_unwind_memory (frame, pc, buf, sizeof buf))
5037 /* If error reading memory, guess that it is not a delayed
5038 branch. */
c906108c 5039 return 0;
e17a4113 5040 return is_delayed (extract_unsigned_integer (buf, sizeof buf, byte_order));
c906108c
SS
5041}
5042
6d82d43b
AC
5043/* To skip prologues, I use this predicate. Returns either PC itself
5044 if the code at PC does not look like a function prologue; otherwise
5045 returns an address that (if we're lucky) follows the prologue. If
5046 LENIENT, then we must skip everything which is involved in setting
5047 up the frame (it's OK to skip more, just so long as we don't skip
5048 anything which might clobber the registers which are being saved.
5049 We must skip more in the case where part of the prologue is in the
5050 delay slot of a non-prologue instruction). */
5051
5052static CORE_ADDR
6093d2eb 5053mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
6d82d43b 5054{
8b622e6a
AC
5055 CORE_ADDR limit_pc;
5056 CORE_ADDR func_addr;
5057
6d82d43b
AC
5058 /* See if we can determine the end of the prologue via the symbol table.
5059 If so, then return either PC, or the PC after the prologue, whichever
5060 is greater. */
8b622e6a
AC
5061 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
5062 {
d80b854b
UW
5063 CORE_ADDR post_prologue_pc
5064 = skip_prologue_using_sal (gdbarch, func_addr);
8b622e6a
AC
5065 if (post_prologue_pc != 0)
5066 return max (pc, post_prologue_pc);
5067 }
6d82d43b
AC
5068
5069 /* Can't determine prologue from the symbol table, need to examine
5070 instructions. */
5071
98b4dd94
JB
5072 /* Find an upper limit on the function prologue using the debug
5073 information. If the debug information could not be used to provide
5074 that bound, then use an arbitrary large number as the upper bound. */
d80b854b 5075 limit_pc = skip_prologue_using_sal (gdbarch, pc);
98b4dd94
JB
5076 if (limit_pc == 0)
5077 limit_pc = pc + 100; /* Magic. */
5078
0fe7e7c8 5079 if (mips_pc_is_mips16 (pc))
e17a4113 5080 return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6d82d43b 5081 else
e17a4113 5082 return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
88658117
AC
5083}
5084
97ab0fdd
MR
5085/* Check whether the PC is in a function epilogue (32-bit version).
5086 This is a helper function for mips_in_function_epilogue_p. */
5087static int
e17a4113 5088mips32_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
97ab0fdd
MR
5089{
5090 CORE_ADDR func_addr = 0, func_end = 0;
5091
5092 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
5093 {
5094 /* The MIPS epilogue is max. 12 bytes long. */
5095 CORE_ADDR addr = func_end - 12;
5096
5097 if (addr < func_addr + 4)
5098 addr = func_addr + 4;
5099 if (pc < addr)
5100 return 0;
5101
5102 for (; pc < func_end; pc += MIPS_INSN32_SIZE)
5103 {
5104 unsigned long high_word;
5105 unsigned long inst;
5106
e17a4113 5107 inst = mips_fetch_instruction (gdbarch, pc);
97ab0fdd
MR
5108 high_word = (inst >> 16) & 0xffff;
5109
5110 if (high_word != 0x27bd /* addiu $sp,$sp,offset */
5111 && high_word != 0x67bd /* daddiu $sp,$sp,offset */
5112 && inst != 0x03e00008 /* jr $ra */
5113 && inst != 0x00000000) /* nop */
5114 return 0;
5115 }
5116
5117 return 1;
5118 }
5119
5120 return 0;
5121}
5122
5123/* Check whether the PC is in a function epilogue (16-bit version).
5124 This is a helper function for mips_in_function_epilogue_p. */
5125static int
e17a4113 5126mips16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
97ab0fdd
MR
5127{
5128 CORE_ADDR func_addr = 0, func_end = 0;
5129
5130 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
5131 {
5132 /* The MIPS epilogue is max. 12 bytes long. */
5133 CORE_ADDR addr = func_end - 12;
5134
5135 if (addr < func_addr + 4)
5136 addr = func_addr + 4;
5137 if (pc < addr)
5138 return 0;
5139
5140 for (; pc < func_end; pc += MIPS_INSN16_SIZE)
5141 {
5142 unsigned short inst;
5143
e17a4113 5144 inst = mips_fetch_instruction (gdbarch, pc);
97ab0fdd
MR
5145
5146 if ((inst & 0xf800) == 0xf000) /* extend */
5147 continue;
5148
5149 if (inst != 0x6300 /* addiu $sp,offset */
5150 && inst != 0xfb00 /* daddiu $sp,$sp,offset */
5151 && inst != 0xe820 /* jr $ra */
5152 && inst != 0xe8a0 /* jrc $ra */
5153 && inst != 0x6500) /* nop */
5154 return 0;
5155 }
5156
5157 return 1;
5158 }
5159
5160 return 0;
5161}
5162
5163/* The epilogue is defined here as the area at the end of a function,
5164 after an instruction which destroys the function's stack frame. */
5165static int
5166mips_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
5167{
5168 if (mips_pc_is_mips16 (pc))
e17a4113 5169 return mips16_in_function_epilogue_p (gdbarch, pc);
97ab0fdd 5170 else
e17a4113 5171 return mips32_in_function_epilogue_p (gdbarch, pc);
97ab0fdd
MR
5172}
5173
025bb325 5174/* Root of all "set mips "/"show mips " commands. This will eventually be
a5ea2558
AC
5175 used for all MIPS-specific commands. */
5176
a5ea2558 5177static void
acdb74a0 5178show_mips_command (char *args, int from_tty)
a5ea2558
AC
5179{
5180 help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
5181}
5182
a5ea2558 5183static void
acdb74a0 5184set_mips_command (char *args, int from_tty)
a5ea2558 5185{
6d82d43b
AC
5186 printf_unfiltered
5187 ("\"set mips\" must be followed by an appropriate subcommand.\n");
a5ea2558
AC
5188 help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
5189}
5190
c906108c
SS
5191/* Commands to show/set the MIPS FPU type. */
5192
c906108c 5193static void
acdb74a0 5194show_mipsfpu_command (char *args, int from_tty)
c906108c 5195{
c906108c 5196 char *fpu;
6ca0852e 5197
1cf3db46 5198 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_mips)
6ca0852e
UW
5199 {
5200 printf_unfiltered
5201 ("The MIPS floating-point coprocessor is unknown "
5202 "because the current architecture is not MIPS.\n");
5203 return;
5204 }
5205
1cf3db46 5206 switch (MIPS_FPU_TYPE (target_gdbarch))
c906108c
SS
5207 {
5208 case MIPS_FPU_SINGLE:
5209 fpu = "single-precision";
5210 break;
5211 case MIPS_FPU_DOUBLE:
5212 fpu = "double-precision";
5213 break;
5214 case MIPS_FPU_NONE:
5215 fpu = "absent (none)";
5216 break;
93d56215 5217 default:
e2e0b3e5 5218 internal_error (__FILE__, __LINE__, _("bad switch"));
c906108c
SS
5219 }
5220 if (mips_fpu_type_auto)
025bb325
MS
5221 printf_unfiltered ("The MIPS floating-point coprocessor "
5222 "is set automatically (currently %s)\n",
5223 fpu);
c906108c 5224 else
6d82d43b
AC
5225 printf_unfiltered
5226 ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
c906108c
SS
5227}
5228
5229
c906108c 5230static void
acdb74a0 5231set_mipsfpu_command (char *args, int from_tty)
c906108c 5232{
025bb325
MS
5233 printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
5234 "\"single\",\"none\" or \"auto\".\n");
c906108c
SS
5235 show_mipsfpu_command (args, from_tty);
5236}
5237
c906108c 5238static void
acdb74a0 5239set_mipsfpu_single_command (char *args, int from_tty)
c906108c 5240{
8d5838b5
AC
5241 struct gdbarch_info info;
5242 gdbarch_info_init (&info);
c906108c
SS
5243 mips_fpu_type = MIPS_FPU_SINGLE;
5244 mips_fpu_type_auto = 0;
8d5838b5
AC
5245 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5246 instead of relying on globals. Doing that would let generic code
5247 handle the search for this specific architecture. */
5248 if (!gdbarch_update_p (info))
e2e0b3e5 5249 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
5250}
5251
c906108c 5252static void
acdb74a0 5253set_mipsfpu_double_command (char *args, int from_tty)
c906108c 5254{
8d5838b5
AC
5255 struct gdbarch_info info;
5256 gdbarch_info_init (&info);
c906108c
SS
5257 mips_fpu_type = MIPS_FPU_DOUBLE;
5258 mips_fpu_type_auto = 0;
8d5838b5
AC
5259 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5260 instead of relying on globals. Doing that would let generic code
5261 handle the search for this specific architecture. */
5262 if (!gdbarch_update_p (info))
e2e0b3e5 5263 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
5264}
5265
c906108c 5266static void
acdb74a0 5267set_mipsfpu_none_command (char *args, int from_tty)
c906108c 5268{
8d5838b5
AC
5269 struct gdbarch_info info;
5270 gdbarch_info_init (&info);
c906108c
SS
5271 mips_fpu_type = MIPS_FPU_NONE;
5272 mips_fpu_type_auto = 0;
8d5838b5
AC
5273 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
5274 instead of relying on globals. Doing that would let generic code
5275 handle the search for this specific architecture. */
5276 if (!gdbarch_update_p (info))
e2e0b3e5 5277 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
c906108c
SS
5278}
5279
c906108c 5280static void
acdb74a0 5281set_mipsfpu_auto_command (char *args, int from_tty)
c906108c
SS
5282{
5283 mips_fpu_type_auto = 1;
5284}
5285
c906108c 5286/* Attempt to identify the particular processor model by reading the
691c0433
AC
5287 processor id. NOTE: cagney/2003-11-15: Firstly it isn't clear that
5288 the relevant processor still exists (it dates back to '94) and
5289 secondly this is not the way to do this. The processor type should
5290 be set by forcing an architecture change. */
c906108c 5291
691c0433
AC
5292void
5293deprecated_mips_set_processor_regs_hack (void)
c906108c 5294{
bb486190
UW
5295 struct regcache *regcache = get_current_regcache ();
5296 struct gdbarch *gdbarch = get_regcache_arch (regcache);
5297 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
a9614958 5298 ULONGEST prid;
c906108c 5299
bb486190 5300 regcache_cooked_read_unsigned (regcache, MIPS_PRID_REGNUM, &prid);
c906108c 5301 if ((prid & ~0xf) == 0x700)
691c0433 5302 tdep->mips_processor_reg_names = mips_r3041_reg_names;
c906108c
SS
5303}
5304
5305/* Just like reinit_frame_cache, but with the right arguments to be
5306 callable as an sfunc. */
5307
5308static void
acdb74a0
AC
5309reinit_frame_cache_sfunc (char *args, int from_tty,
5310 struct cmd_list_element *c)
c906108c
SS
5311{
5312 reinit_frame_cache ();
5313}
5314
a89aa300
AC
5315static int
5316gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
c906108c 5317{
d31431ed
AC
5318 /* FIXME: cagney/2003-06-26: Is this even necessary? The
5319 disassembler needs to be able to locally determine the ISA, and
5320 not rely on GDB. Otherwize the stand-alone 'objdump -d' will not
5321 work. */
ec4045ea
AC
5322 if (mips_pc_is_mips16 (memaddr))
5323 info->mach = bfd_mach_mips16;
c906108c
SS
5324
5325 /* Round down the instruction address to the appropriate boundary. */
65c11066 5326 memaddr &= (info->mach == bfd_mach_mips16 ? ~1 : ~3);
c5aa993b 5327
e5ab0dce 5328 /* Set the disassembler options. */
9dae60cc 5329 if (!info->disassembler_options)
e5ab0dce
AC
5330 /* This string is not recognized explicitly by the disassembler,
5331 but it tells the disassembler to not try to guess the ABI from
5332 the bfd elf headers, such that, if the user overrides the ABI
5333 of a program linked as NewABI, the disassembly will follow the
5334 register naming conventions specified by the user. */
5335 info->disassembler_options = "gpr-names=32";
5336
c906108c 5337 /* Call the appropriate disassembler based on the target endian-ness. */
40887e1a 5338 if (info->endian == BFD_ENDIAN_BIG)
c906108c
SS
5339 return print_insn_big_mips (memaddr, info);
5340 else
5341 return print_insn_little_mips (memaddr, info);
5342}
5343
9dae60cc
UW
5344static int
5345gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
5346{
5347 /* Set up the disassembler info, so that we get the right
5348 register names from libopcodes. */
5349 info->disassembler_options = "gpr-names=n32";
5350 info->flavour = bfd_target_elf_flavour;
5351
5352 return gdb_print_insn_mips (memaddr, info);
5353}
5354
5355static int
5356gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
5357{
5358 /* Set up the disassembler info, so that we get the right
5359 register names from libopcodes. */
5360 info->disassembler_options = "gpr-names=64";
5361 info->flavour = bfd_target_elf_flavour;
5362
5363 return gdb_print_insn_mips (memaddr, info);
5364}
5365
025bb325
MS
5366/* This function implements gdbarch_breakpoint_from_pc. It uses the
5367 program counter value to determine whether a 16- or 32-bit breakpoint
5368 should be used. It returns a pointer to a string of bytes that encode a
5369 breakpoint instruction, stores the length of the string to *lenptr, and
5370 adjusts pc (if necessary) to point to the actual memory location where
5371 the breakpoint should be inserted. */
c906108c 5372
47a35522 5373static const gdb_byte *
025bb325
MS
5374mips_breakpoint_from_pc (struct gdbarch *gdbarch,
5375 CORE_ADDR *pcptr, int *lenptr)
c906108c 5376{
67d57894 5377 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c 5378 {
0fe7e7c8 5379 if (mips_pc_is_mips16 (*pcptr))
c906108c 5380 {
47a35522 5381 static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
95404a3e 5382 *pcptr = unmake_mips16_addr (*pcptr);
c5aa993b 5383 *lenptr = sizeof (mips16_big_breakpoint);
c906108c
SS
5384 return mips16_big_breakpoint;
5385 }
5386 else
5387 {
aaab4dba
AC
5388 /* The IDT board uses an unusual breakpoint value, and
5389 sometimes gets confused when it sees the usual MIPS
5390 breakpoint instruction. */
47a35522
MK
5391 static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
5392 static gdb_byte pmon_big_breakpoint[] = { 0, 0, 0, 0xd };
5393 static gdb_byte idt_big_breakpoint[] = { 0, 0, 0x0a, 0xd };
f2ec0ecf 5394 /* Likewise, IRIX appears to expect a different breakpoint,
025bb325 5395 although this is not apparent until you try to use pthreads. */
f2ec0ecf 5396 static gdb_byte irix_big_breakpoint[] = { 0, 0, 0, 0xd };
c906108c 5397
c5aa993b 5398 *lenptr = sizeof (big_breakpoint);
c906108c
SS
5399
5400 if (strcmp (target_shortname, "mips") == 0)
5401 return idt_big_breakpoint;
5402 else if (strcmp (target_shortname, "ddb") == 0
5403 || strcmp (target_shortname, "pmon") == 0
5404 || strcmp (target_shortname, "lsi") == 0)
5405 return pmon_big_breakpoint;
f2ec0ecf
JB
5406 else if (gdbarch_osabi (gdbarch) == GDB_OSABI_IRIX)
5407 return irix_big_breakpoint;
c906108c
SS
5408 else
5409 return big_breakpoint;
5410 }
5411 }
5412 else
5413 {
0fe7e7c8 5414 if (mips_pc_is_mips16 (*pcptr))
c906108c 5415 {
47a35522 5416 static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
95404a3e 5417 *pcptr = unmake_mips16_addr (*pcptr);
c5aa993b 5418 *lenptr = sizeof (mips16_little_breakpoint);
c906108c
SS
5419 return mips16_little_breakpoint;
5420 }
5421 else
5422 {
47a35522
MK
5423 static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
5424 static gdb_byte pmon_little_breakpoint[] = { 0xd, 0, 0, 0 };
5425 static gdb_byte idt_little_breakpoint[] = { 0xd, 0x0a, 0, 0 };
c906108c 5426
c5aa993b 5427 *lenptr = sizeof (little_breakpoint);
c906108c
SS
5428
5429 if (strcmp (target_shortname, "mips") == 0)
5430 return idt_little_breakpoint;
5431 else if (strcmp (target_shortname, "ddb") == 0
5432 || strcmp (target_shortname, "pmon") == 0
5433 || strcmp (target_shortname, "lsi") == 0)
5434 return pmon_little_breakpoint;
5435 else
5436 return little_breakpoint;
5437 }
5438 }
5439}
5440
c8cef75f
MR
5441/* Return non-zero if the ADDR instruction has a branch delay slot
5442 (i.e. it is a jump or branch instruction). This function is based
5443 on mips32_next_pc. */
5444
5445static int
5446mips32_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr)
5447{
5448 gdb_byte buf[MIPS_INSN32_SIZE];
5449 unsigned long inst;
5450 int status;
5451 int op;
a385295e
MR
5452 int rs;
5453 int rt;
c8cef75f
MR
5454
5455 status = target_read_memory (addr, buf, MIPS_INSN32_SIZE);
5456 if (status)
5457 return 0;
5458
5459 inst = mips_fetch_instruction (gdbarch, addr);
5460 op = itype_op (inst);
5461 if ((inst & 0xe0000000) != 0)
a385295e
MR
5462 {
5463 rs = itype_rs (inst);
5464 rt = itype_rt (inst);
5465 return (op >> 2 == 5 /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
5466 || op == 29 /* JALX: bits 011101 */
5467 || (op == 17
5468 && (rs == 8
c8cef75f 5469 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
a385295e
MR
5470 || (rs == 9 && (rt & 0x2) == 0)
5471 /* BC1ANY2F, BC1ANY2T: bits 010001 01001 */
5472 || (rs == 10 && (rt & 0x2) == 0))));
5473 /* BC1ANY4F, BC1ANY4T: bits 010001 01010 */
5474 }
c8cef75f
MR
5475 else
5476 switch (op & 0x07) /* extract bits 28,27,26 */
5477 {
5478 case 0: /* SPECIAL */
5479 op = rtype_funct (inst);
5480 return (op == 8 /* JR */
5481 || op == 9); /* JALR */
5482 break; /* end SPECIAL */
5483 case 1: /* REGIMM */
a385295e
MR
5484 rs = itype_rs (inst);
5485 rt = itype_rt (inst); /* branch condition */
5486 return ((rt & 0xc) == 0
c8cef75f
MR
5487 /* BLTZ, BLTZL, BGEZ, BGEZL: bits 000xx */
5488 /* BLTZAL, BLTZALL, BGEZAL, BGEZALL: 100xx */
a385295e
MR
5489 || ((rt & 0x1e) == 0x1c && rs == 0));
5490 /* BPOSGE32, BPOSGE64: bits 1110x */
c8cef75f
MR
5491 break; /* end REGIMM */
5492 default: /* J, JAL, BEQ, BNE, BLEZ, BGTZ */
5493 return 1;
5494 break;
5495 }
5496}
5497
5498/* Return non-zero if the ADDR instruction, which must be a 32-bit
5499 instruction if MUSTBE32 is set or can be any instruction otherwise,
5500 has a branch delay slot (i.e. it is a non-compact jump instruction). */
5501
5502static int
5503mips16_instruction_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr,
5504 int mustbe32)
5505{
5506 gdb_byte buf[MIPS_INSN16_SIZE];
5507 unsigned short inst;
5508 int status;
5509
5510 status = target_read_memory (addr, buf, MIPS_INSN16_SIZE);
5511 if (status)
5512 return 0;
5513
5514 inst = mips_fetch_instruction (gdbarch, addr);
5515 if (!mustbe32)
5516 return (inst & 0xf89f) == 0xe800; /* JR/JALR (16-bit instruction) */
5517 return (inst & 0xf800) == 0x1800; /* JAL/JALX (32-bit instruction) */
5518}
5519
5520/* Calculate the starting address of the MIPS memory segment BPADDR is in.
5521 This assumes KSSEG exists. */
5522
5523static CORE_ADDR
5524mips_segment_boundary (CORE_ADDR bpaddr)
5525{
5526 CORE_ADDR mask = CORE_ADDR_MAX;
5527 int segsize;
5528
5529 if (sizeof (CORE_ADDR) == 8)
5530 /* Get the topmost two bits of bpaddr in a 32-bit safe manner (avoid
5531 a compiler warning produced where CORE_ADDR is a 32-bit type even
5532 though in that case this is dead code). */
5533 switch (bpaddr >> ((sizeof (CORE_ADDR) << 3) - 2) & 3)
5534 {
5535 case 3:
5536 if (bpaddr == (bfd_signed_vma) (int32_t) bpaddr)
5537 segsize = 29; /* 32-bit compatibility segment */
5538 else
5539 segsize = 62; /* xkseg */
5540 break;
5541 case 2: /* xkphys */
5542 segsize = 59;
5543 break;
5544 default: /* xksseg (1), xkuseg/kuseg (0) */
5545 segsize = 62;
5546 break;
5547 }
5548 else if (bpaddr & 0x80000000) /* kernel segment */
5549 segsize = 29;
5550 else
5551 segsize = 31; /* user segment */
5552 mask <<= segsize;
5553 return bpaddr & mask;
5554}
5555
5556/* Move the breakpoint at BPADDR out of any branch delay slot by shifting
5557 it backwards if necessary. Return the address of the new location. */
5558
5559static CORE_ADDR
5560mips_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
5561{
5562 CORE_ADDR prev_addr, next_addr;
5563 CORE_ADDR boundary;
5564 CORE_ADDR func_addr;
5565
5566 /* If a breakpoint is set on the instruction in a branch delay slot,
5567 GDB gets confused. When the breakpoint is hit, the PC isn't on
5568 the instruction in the branch delay slot, the PC will point to
5569 the branch instruction. Since the PC doesn't match any known
5570 breakpoints, GDB reports a trap exception.
5571
5572 There are two possible fixes for this problem.
5573
5574 1) When the breakpoint gets hit, see if the BD bit is set in the
5575 Cause register (which indicates the last exception occurred in a
5576 branch delay slot). If the BD bit is set, fix the PC to point to
5577 the instruction in the branch delay slot.
5578
5579 2) When the user sets the breakpoint, don't allow him to set the
5580 breakpoint on the instruction in the branch delay slot. Instead
5581 move the breakpoint to the branch instruction (which will have
5582 the same result).
5583
5584 The problem with the first solution is that if the user then
5585 single-steps the processor, the branch instruction will get
5586 skipped (since GDB thinks the PC is on the instruction in the
5587 branch delay slot).
5588
5589 So, we'll use the second solution. To do this we need to know if
5590 the instruction we're trying to set the breakpoint on is in the
5591 branch delay slot. */
5592
5593 boundary = mips_segment_boundary (bpaddr);
5594
5595 /* Make sure we don't scan back before the beginning of the current
5596 function, since we may fetch constant data or insns that look like
5597 a jump. Of course we might do that anyway if the compiler has
5598 moved constants inline. :-( */
5599 if (find_pc_partial_function (bpaddr, NULL, &func_addr, NULL)
5600 && func_addr > boundary && func_addr <= bpaddr)
5601 boundary = func_addr;
5602
5603 if (!mips_pc_is_mips16 (bpaddr))
5604 {
5605 if (bpaddr == boundary)
5606 return bpaddr;
5607
5608 /* If the previous instruction has a branch delay slot, we have
5609 to move the breakpoint to the branch instruction. */
5610 prev_addr = bpaddr - 4;
5611 if (mips32_instruction_has_delay_slot (gdbarch, prev_addr))
5612 bpaddr = prev_addr;
5613 }
5614 else
5615 {
5616 struct minimal_symbol *sym;
5617 CORE_ADDR addr, jmpaddr;
5618 int i;
5619
5620 boundary = unmake_mips16_addr (boundary);
5621
5622 /* The only MIPS16 instructions with delay slots are JAL, JALX,
5623 JALR and JR. An absolute JAL/JALX is always 4 bytes long,
5624 so try for that first, then try the 2 byte JALR/JR.
5625 FIXME: We have to assume that bpaddr is not the second half
5626 of an extended instruction. */
5627
5628 jmpaddr = 0;
5629 addr = bpaddr;
5630 for (i = 1; i < 4; i++)
5631 {
5632 if (unmake_mips16_addr (addr) == boundary)
5633 break;
5634 addr -= 2;
5635 if (i == 1 && mips16_instruction_has_delay_slot (gdbarch, addr, 0))
5636 /* Looks like a JR/JALR at [target-1], but it could be
5637 the second word of a previous JAL/JALX, so record it
5638 and check back one more. */
5639 jmpaddr = addr;
5640 else if (i > 1
5641 && mips16_instruction_has_delay_slot (gdbarch, addr, 1))
5642 {
5643 if (i == 2)
5644 /* Looks like a JAL/JALX at [target-2], but it could also
5645 be the second word of a previous JAL/JALX, record it,
5646 and check back one more. */
5647 jmpaddr = addr;
5648 else
5649 /* Looks like a JAL/JALX at [target-3], so any previously
5650 recorded JAL/JALX or JR/JALR must be wrong, because:
5651
5652 >-3: JAL
5653 -2: JAL-ext (can't be JAL/JALX)
5654 -1: bdslot (can't be JR/JALR)
5655 0: target insn
5656
5657 Of course it could be another JAL-ext which looks
5658 like a JAL, but in that case we'd have broken out
5659 of this loop at [target-2]:
5660
5661 -4: JAL
5662 >-3: JAL-ext
5663 -2: bdslot (can't be jmp)
5664 -1: JR/JALR
5665 0: target insn */
5666 jmpaddr = 0;
5667 }
5668 else
5669 {
5670 /* Not a jump instruction: if we're at [target-1] this
5671 could be the second word of a JAL/JALX, so continue;
5672 otherwise we're done. */
5673 if (i > 1)
5674 break;
5675 }
5676 }
5677
5678 if (jmpaddr)
5679 bpaddr = jmpaddr;
5680 }
5681
5682 return bpaddr;
5683}
5684
14132e89
MR
5685/* Return non-zero if SUFFIX is one of the numeric suffixes used for MIPS16
5686 call stubs, one of 1, 2, 5, 6, 9, 10, or, if ZERO is non-zero, also 0. */
5687
5688static int
5689mips_is_stub_suffix (const char *suffix, int zero)
5690{
5691 switch (suffix[0])
5692 {
5693 case '0':
5694 return zero && suffix[1] == '\0';
5695 case '1':
5696 return suffix[1] == '\0' || (suffix[1] == '0' && suffix[2] == '\0');
5697 case '2':
5698 case '5':
5699 case '6':
5700 case '9':
5701 return suffix[1] == '\0';
5702 default:
5703 return 0;
5704 }
5705}
5706
5707/* Return non-zero if MODE is one of the mode infixes used for MIPS16
5708 call stubs, one of sf, df, sc, or dc. */
5709
5710static int
5711mips_is_stub_mode (const char *mode)
5712{
5713 return ((mode[0] == 's' || mode[0] == 'd')
5714 && (mode[1] == 'f' || mode[1] == 'c'));
5715}
5716
5717/* Code at PC is a compiler-generated stub. Such a stub for a function
5718 bar might have a name like __fn_stub_bar, and might look like this:
5719
5720 mfc1 $4, $f13
5721 mfc1 $5, $f12
5722 mfc1 $6, $f15
5723 mfc1 $7, $f14
5724
5725 followed by (or interspersed with):
5726
5727 j bar
5728
5729 or:
5730
5731 lui $25, %hi(bar)
5732 addiu $25, $25, %lo(bar)
5733 jr $25
5734
5735 ($1 may be used in old code; for robustness we accept any register)
5736 or, in PIC code:
5737
5738 lui $28, %hi(_gp_disp)
5739 addiu $28, $28, %lo(_gp_disp)
5740 addu $28, $28, $25
5741 lw $25, %got(bar)
5742 addiu $25, $25, %lo(bar)
5743 jr $25
5744
5745 In the case of a __call_stub_bar stub, the sequence to set up
5746 arguments might look like this:
5747
5748 mtc1 $4, $f13
5749 mtc1 $5, $f12
5750 mtc1 $6, $f15
5751 mtc1 $7, $f14
5752
5753 followed by (or interspersed with) one of the jump sequences above.
5754
5755 In the case of a __call_stub_fp_bar stub, JAL or JALR is used instead
5756 of J or JR, respectively, followed by:
5757
5758 mfc1 $2, $f0
5759 mfc1 $3, $f1
5760 jr $18
5761
5762 We are at the beginning of the stub here, and scan down and extract
5763 the target address from the jump immediate instruction or, if a jump
5764 register instruction is used, from the register referred. Return
5765 the value of PC calculated or 0 if inconclusive.
5766
5767 The limit on the search is arbitrarily set to 20 instructions. FIXME. */
5768
5769static CORE_ADDR
5770mips_get_mips16_fn_stub_pc (struct frame_info *frame, CORE_ADDR pc)
5771{
5772 struct gdbarch *gdbarch = get_frame_arch (frame);
5773 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5774 int addrreg = MIPS_ZERO_REGNUM;
5775 CORE_ADDR start_pc = pc;
5776 CORE_ADDR target_pc = 0;
5777 CORE_ADDR addr = 0;
5778 CORE_ADDR gp = 0;
5779 int status = 0;
5780 int i;
5781
5782 for (i = 0;
5783 status == 0 && target_pc == 0 && i < 20;
5784 i++, pc += MIPS_INSN32_SIZE)
5785 {
5786 ULONGEST inst = mips_fetch_instruction (gdbarch, pc);
5787 CORE_ADDR imm;
5788 int rt;
5789 int rs;
5790 int rd;
5791
5792 switch (itype_op (inst))
5793 {
5794 case 0: /* SPECIAL */
5795 switch (rtype_funct (inst))
5796 {
5797 case 8: /* JR */
5798 case 9: /* JALR */
5799 rs = rtype_rs (inst);
5800 if (rs == MIPS_GP_REGNUM)
5801 target_pc = gp; /* Hmm... */
5802 else if (rs == addrreg)
5803 target_pc = addr;
5804 break;
5805
5806 case 0x21: /* ADDU */
5807 rt = rtype_rt (inst);
5808 rs = rtype_rs (inst);
5809 rd = rtype_rd (inst);
5810 if (rd == MIPS_GP_REGNUM
5811 && ((rs == MIPS_GP_REGNUM && rt == MIPS_T9_REGNUM)
5812 || (rs == MIPS_T9_REGNUM && rt == MIPS_GP_REGNUM)))
5813 gp += start_pc;
5814 break;
5815 }
5816 break;
5817
5818 case 2: /* J */
5819 case 3: /* JAL */
5820 target_pc = jtype_target (inst) << 2;
5821 target_pc += ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
5822 break;
5823
5824 case 9: /* ADDIU */
5825 rt = itype_rt (inst);
5826 rs = itype_rs (inst);
5827 if (rt == rs)
5828 {
5829 imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
5830 if (rt == MIPS_GP_REGNUM)
5831 gp += imm;
5832 else if (rt == addrreg)
5833 addr += imm;
5834 }
5835 break;
5836
5837 case 0xf: /* LUI */
5838 rt = itype_rt (inst);
5839 imm = ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 16;
5840 if (rt == MIPS_GP_REGNUM)
5841 gp = imm;
5842 else if (rt != MIPS_ZERO_REGNUM)
5843 {
5844 addrreg = rt;
5845 addr = imm;
5846 }
5847 break;
5848
5849 case 0x23: /* LW */
5850 rt = itype_rt (inst);
5851 rs = itype_rs (inst);
5852 imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
5853 if (gp != 0 && rs == MIPS_GP_REGNUM)
5854 {
5855 gdb_byte buf[4];
5856
5857 memset (buf, 0, sizeof (buf));
5858 status = target_read_memory (gp + imm, buf, sizeof (buf));
5859 addrreg = rt;
5860 addr = extract_signed_integer (buf, sizeof (buf), byte_order);
5861 }
5862 break;
5863 }
5864 }
5865
5866 return target_pc;
5867}
5868
5869/* If PC is in a MIPS16 call or return stub, return the address of the
5870 target PC, which is either the callee or the caller. There are several
c906108c
SS
5871 cases which must be handled:
5872
14132e89
MR
5873 * If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
5874 and the target PC is in $31 ($ra).
c906108c 5875 * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
14132e89
MR
5876 and the target PC is in $2.
5877 * If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
5878 i.e. before the JALR instruction, this is effectively a call stub
5879 and the target PC is in $2. Otherwise this is effectively
5880 a return stub and the target PC is in $18.
5881 * If the PC is at the start of __call_stub_fp_*, i.e. before the
5882 JAL or JALR instruction, this is effectively a call stub and the
5883 target PC is buried in the instruction stream. Otherwise this
5884 is effectively a return stub and the target PC is in $18.
5885 * If the PC is in __call_stub_* or in __fn_stub_*, this is a call
5886 stub and the target PC is buried in the instruction stream.
5887
5888 See the source code for the stubs in gcc/config/mips/mips16.S, or the
5889 stub builder in gcc/config/mips/mips.c (mips16_build_call_stub) for the
e7d6a6d2 5890 gory details. */
c906108c 5891
757a7cc6 5892static CORE_ADDR
db5f024e 5893mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
c906108c 5894{
e17a4113 5895 struct gdbarch *gdbarch = get_frame_arch (frame);
c906108c 5896 CORE_ADDR start_addr;
14132e89
MR
5897 const char *name;
5898 size_t prefixlen;
c906108c
SS
5899
5900 /* Find the starting address and name of the function containing the PC. */
5901 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
5902 return 0;
5903
14132e89
MR
5904 /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
5905 and the target PC is in $31 ($ra). */
5906 prefixlen = strlen (mips_str_mips16_ret_stub);
5907 if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
5908 && mips_is_stub_mode (name + prefixlen)
5909 && name[prefixlen + 2] == '\0')
5910 return get_frame_register_signed
5911 (frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
5912
5913 /* If the PC is in __mips16_call_stub_*, this is one of the call
5914 call/return stubs. */
5915 prefixlen = strlen (mips_str_mips16_call_stub);
5916 if (strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0)
c906108c
SS
5917 {
5918 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
5919 and the target PC is in $2. */
14132e89
MR
5920 if (mips_is_stub_suffix (name + prefixlen, 0))
5921 return get_frame_register_signed
5922 (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
c906108c 5923
14132e89
MR
5924 /* If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
5925 i.e. before the JALR instruction, this is effectively a call stub
b021a221 5926 and the target PC is in $2. Otherwise this is effectively
c5aa993b 5927 a return stub and the target PC is in $18. */
14132e89
MR
5928 else if (mips_is_stub_mode (name + prefixlen)
5929 && name[prefixlen + 2] == '_'
5930 && mips_is_stub_suffix (name + prefixlen + 3, 0))
c906108c
SS
5931 {
5932 if (pc == start_addr)
14132e89
MR
5933 /* This is the 'call' part of a call stub. The return
5934 address is in $2. */
5935 return get_frame_register_signed
5936 (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
c906108c
SS
5937 else
5938 /* This is the 'return' part of a call stub. The return
14132e89
MR
5939 address is in $18. */
5940 return get_frame_register_signed
5941 (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
c906108c 5942 }
14132e89
MR
5943 else
5944 return 0; /* Not a stub. */
5945 }
5946
5947 /* If the PC is in __call_stub_* or __fn_stub*, this is one of the
5948 compiler-generated call or call/return stubs. */
5949 if (strncmp (name, mips_str_fn_stub, strlen (mips_str_fn_stub)) == 0
5950 || strncmp (name, mips_str_call_stub, strlen (mips_str_call_stub)) == 0)
5951 {
5952 if (pc == start_addr)
5953 /* This is the 'call' part of a call stub. Call this helper
5954 to scan through this code for interesting instructions
5955 and determine the final PC. */
5956 return mips_get_mips16_fn_stub_pc (frame, pc);
5957 else
5958 /* This is the 'return' part of a call stub. The return address
5959 is in $18. */
5960 return get_frame_register_signed
5961 (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
c906108c 5962 }
14132e89
MR
5963
5964 return 0; /* Not a stub. */
5965}
5966
5967/* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
5968 This implements the IN_SOLIB_RETURN_TRAMPOLINE macro. */
5969
5970static int
5971mips_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc, const char *name)
5972{
5973 CORE_ADDR start_addr;
5974 size_t prefixlen;
5975
5976 /* Find the starting address of the function containing the PC. */
5977 if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
5978 return 0;
5979
5980 /* If the PC is in __mips16_call_stub_{s,d}{f,c}_{0..10} but not at
5981 the start, i.e. after the JALR instruction, this is effectively
5982 a return stub. */
5983 prefixlen = strlen (mips_str_mips16_call_stub);
5984 if (pc != start_addr
5985 && strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0
5986 && mips_is_stub_mode (name + prefixlen)
5987 && name[prefixlen + 2] == '_'
5988 && mips_is_stub_suffix (name + prefixlen + 3, 1))
5989 return 1;
5990
5991 /* If the PC is in __call_stub_fp_* but not at the start, i.e. after
5992 the JAL or JALR instruction, this is effectively a return stub. */
5993 prefixlen = strlen (mips_str_call_fp_stub);
5994 if (pc != start_addr
5995 && strncmp (name, mips_str_call_fp_stub, prefixlen) == 0)
5996 return 1;
5997
5998 /* Consume the .pic. prefix of any PIC stub, this function must return
5999 true when the PC is in a PIC stub of a __mips16_ret_{d,s}{f,c} stub
6000 or the call stub path will trigger in handle_inferior_event causing
6001 it to go astray. */
6002 prefixlen = strlen (mips_str_pic);
6003 if (strncmp (name, mips_str_pic, prefixlen) == 0)
6004 name += prefixlen;
6005
6006 /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub. */
6007 prefixlen = strlen (mips_str_mips16_ret_stub);
6008 if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
6009 && mips_is_stub_mode (name + prefixlen)
6010 && name[prefixlen + 2] == '\0')
6011 return 1;
6012
6013 return 0; /* Not a stub. */
c906108c
SS
6014}
6015
db5f024e
DJ
6016/* If the current PC is the start of a non-PIC-to-PIC stub, return the
6017 PC of the stub target. The stub just loads $t9 and jumps to it,
6018 so that $t9 has the correct value at function entry. */
6019
6020static CORE_ADDR
6021mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
6022{
e17a4113
UW
6023 struct gdbarch *gdbarch = get_frame_arch (frame);
6024 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
db5f024e
DJ
6025 struct minimal_symbol *msym;
6026 int i;
6027 gdb_byte stub_code[16];
6028 int32_t stub_words[4];
6029
6030 /* The stub for foo is named ".pic.foo", and is either two
6031 instructions inserted before foo or a three instruction sequence
6032 which jumps to foo. */
6033 msym = lookup_minimal_symbol_by_pc (pc);
6034 if (msym == NULL
6035 || SYMBOL_VALUE_ADDRESS (msym) != pc
6036 || SYMBOL_LINKAGE_NAME (msym) == NULL
6037 || strncmp (SYMBOL_LINKAGE_NAME (msym), ".pic.", 5) != 0)
6038 return 0;
6039
6040 /* A two-instruction header. */
6041 if (MSYMBOL_SIZE (msym) == 8)
6042 return pc + 8;
6043
6044 /* A three-instruction (plus delay slot) trampoline. */
6045 if (MSYMBOL_SIZE (msym) == 16)
6046 {
6047 if (target_read_memory (pc, stub_code, 16) != 0)
6048 return 0;
6049 for (i = 0; i < 4; i++)
e17a4113
UW
6050 stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
6051 4, byte_order);
db5f024e
DJ
6052
6053 /* A stub contains these instructions:
6054 lui t9, %hi(target)
6055 j target
6056 addiu t9, t9, %lo(target)
6057 nop
6058
6059 This works even for N64, since stubs are only generated with
6060 -msym32. */
6061 if ((stub_words[0] & 0xffff0000U) == 0x3c190000
6062 && (stub_words[1] & 0xfc000000U) == 0x08000000
6063 && (stub_words[2] & 0xffff0000U) == 0x27390000
6064 && stub_words[3] == 0x00000000)
34b192ce
MR
6065 return ((((stub_words[0] & 0x0000ffff) << 16)
6066 + (stub_words[2] & 0x0000ffff)) ^ 0x8000) - 0x8000;
db5f024e
DJ
6067 }
6068
6069 /* Not a recognized stub. */
6070 return 0;
6071}
6072
6073static CORE_ADDR
6074mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
6075{
14132e89 6076 CORE_ADDR requested_pc = pc;
db5f024e 6077 CORE_ADDR target_pc;
14132e89
MR
6078 CORE_ADDR new_pc;
6079
6080 do
6081 {
6082 target_pc = pc;
db5f024e 6083
14132e89
MR
6084 new_pc = mips_skip_mips16_trampoline_code (frame, pc);
6085 if (new_pc)
6086 {
6087 pc = new_pc;
6088 if (is_mips16_addr (pc))
6089 pc = unmake_mips16_addr (pc);
6090 }
db5f024e 6091
14132e89
MR
6092 new_pc = find_solib_trampoline_target (frame, pc);
6093 if (new_pc)
6094 {
6095 pc = new_pc;
6096 if (is_mips16_addr (pc))
6097 pc = unmake_mips16_addr (pc);
6098 }
db5f024e 6099
14132e89
MR
6100 new_pc = mips_skip_pic_trampoline_code (frame, pc);
6101 if (new_pc)
6102 {
6103 pc = new_pc;
6104 if (is_mips16_addr (pc))
6105 pc = unmake_mips16_addr (pc);
6106 }
6107 }
6108 while (pc != target_pc);
db5f024e 6109
14132e89 6110 return pc != requested_pc ? pc : 0;
db5f024e
DJ
6111}
6112
a4b8ebc8 6113/* Convert a dbx stab register number (from `r' declaration) to a GDB
f57d151a 6114 [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM. */
88c72b7d
AC
6115
6116static int
d3f73121 6117mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
88c72b7d 6118{
a4b8ebc8 6119 int regnum;
2f38ef89 6120 if (num >= 0 && num < 32)
a4b8ebc8 6121 regnum = num;
2f38ef89 6122 else if (num >= 38 && num < 70)
d3f73121 6123 regnum = num + mips_regnum (gdbarch)->fp0 - 38;
040b99fd 6124 else if (num == 70)
d3f73121 6125 regnum = mips_regnum (gdbarch)->hi;
040b99fd 6126 else if (num == 71)
d3f73121 6127 regnum = mips_regnum (gdbarch)->lo;
1faeff08
MR
6128 else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 72 && num < 78)
6129 regnum = num + mips_regnum (gdbarch)->dspacc - 72;
2f38ef89 6130 else
a4b8ebc8
AC
6131 /* This will hopefully (eventually) provoke a warning. Should
6132 we be calling complaint() here? */
d3f73121
MD
6133 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
6134 return gdbarch_num_regs (gdbarch) + regnum;
88c72b7d
AC
6135}
6136
2f38ef89 6137
a4b8ebc8 6138/* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
f57d151a 6139 gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM. */
88c72b7d
AC
6140
6141static int
d3f73121 6142mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
88c72b7d 6143{
a4b8ebc8 6144 int regnum;
2f38ef89 6145 if (num >= 0 && num < 32)
a4b8ebc8 6146 regnum = num;
2f38ef89 6147 else if (num >= 32 && num < 64)
d3f73121 6148 regnum = num + mips_regnum (gdbarch)->fp0 - 32;
040b99fd 6149 else if (num == 64)
d3f73121 6150 regnum = mips_regnum (gdbarch)->hi;
040b99fd 6151 else if (num == 65)
d3f73121 6152 regnum = mips_regnum (gdbarch)->lo;
1faeff08
MR
6153 else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 66 && num < 72)
6154 regnum = num + mips_regnum (gdbarch)->dspacc - 66;
2f38ef89 6155 else
a4b8ebc8
AC
6156 /* This will hopefully (eventually) provoke a warning. Should we
6157 be calling complaint() here? */
d3f73121
MD
6158 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
6159 return gdbarch_num_regs (gdbarch) + regnum;
a4b8ebc8
AC
6160}
6161
6162static int
e7faf938 6163mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
a4b8ebc8
AC
6164{
6165 /* Only makes sense to supply raw registers. */
e7faf938 6166 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
a4b8ebc8
AC
6167 /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
6168 decide if it is valid. Should instead define a standard sim/gdb
6169 register numbering scheme. */
e7faf938
MD
6170 if (gdbarch_register_name (gdbarch,
6171 gdbarch_num_regs (gdbarch) + regnum) != NULL
6172 && gdbarch_register_name (gdbarch,
025bb325
MS
6173 gdbarch_num_regs (gdbarch)
6174 + regnum)[0] != '\0')
a4b8ebc8
AC
6175 return regnum;
6176 else
6d82d43b 6177 return LEGACY_SIM_REGNO_IGNORE;
88c72b7d
AC
6178}
6179
2f38ef89 6180
4844f454
CV
6181/* Convert an integer into an address. Extracting the value signed
6182 guarantees a correctly sign extended address. */
fc0c74b1
AC
6183
6184static CORE_ADDR
79dd2d24 6185mips_integer_to_address (struct gdbarch *gdbarch,
870cd05e 6186 struct type *type, const gdb_byte *buf)
fc0c74b1 6187{
e17a4113
UW
6188 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6189 return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
fc0c74b1
AC
6190}
6191
82e91389
DJ
6192/* Dummy virtual frame pointer method. This is no more or less accurate
6193 than most other architectures; we just need to be explicit about it,
6194 because the pseudo-register gdbarch_sp_regnum will otherwise lead to
6195 an assertion failure. */
6196
6197static void
a54fba4c
MD
6198mips_virtual_frame_pointer (struct gdbarch *gdbarch,
6199 CORE_ADDR pc, int *reg, LONGEST *offset)
82e91389
DJ
6200{
6201 *reg = MIPS_SP_REGNUM;
6202 *offset = 0;
6203}
6204
caaa3122
DJ
6205static void
6206mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
6207{
6208 enum mips_abi *abip = (enum mips_abi *) obj;
6209 const char *name = bfd_get_section_name (abfd, sect);
6210
6211 if (*abip != MIPS_ABI_UNKNOWN)
6212 return;
6213
6214 if (strncmp (name, ".mdebug.", 8) != 0)
6215 return;
6216
6217 if (strcmp (name, ".mdebug.abi32") == 0)
6218 *abip = MIPS_ABI_O32;
6219 else if (strcmp (name, ".mdebug.abiN32") == 0)
6220 *abip = MIPS_ABI_N32;
62a49b2c 6221 else if (strcmp (name, ".mdebug.abi64") == 0)
e3bddbfa 6222 *abip = MIPS_ABI_N64;
caaa3122
DJ
6223 else if (strcmp (name, ".mdebug.abiO64") == 0)
6224 *abip = MIPS_ABI_O64;
6225 else if (strcmp (name, ".mdebug.eabi32") == 0)
6226 *abip = MIPS_ABI_EABI32;
6227 else if (strcmp (name, ".mdebug.eabi64") == 0)
6228 *abip = MIPS_ABI_EABI64;
6229 else
8a3fe4f8 6230 warning (_("unsupported ABI %s."), name + 8);
caaa3122
DJ
6231}
6232
22e47e37
FF
6233static void
6234mips_find_long_section (bfd *abfd, asection *sect, void *obj)
6235{
6236 int *lbp = (int *) obj;
6237 const char *name = bfd_get_section_name (abfd, sect);
6238
6239 if (strncmp (name, ".gcc_compiled_long32", 20) == 0)
6240 *lbp = 32;
6241 else if (strncmp (name, ".gcc_compiled_long64", 20) == 0)
6242 *lbp = 64;
6243 else if (strncmp (name, ".gcc_compiled_long", 18) == 0)
6244 warning (_("unrecognized .gcc_compiled_longXX"));
6245}
6246
2e4ebe70
DJ
6247static enum mips_abi
6248global_mips_abi (void)
6249{
6250 int i;
6251
6252 for (i = 0; mips_abi_strings[i] != NULL; i++)
6253 if (mips_abi_strings[i] == mips_abi_string)
6254 return (enum mips_abi) i;
6255
e2e0b3e5 6256 internal_error (__FILE__, __LINE__, _("unknown ABI string"));
2e4ebe70
DJ
6257}
6258
29709017
DJ
6259static void
6260mips_register_g_packet_guesses (struct gdbarch *gdbarch)
6261{
29709017
DJ
6262 /* If the size matches the set of 32-bit or 64-bit integer registers,
6263 assume that's what we've got. */
4eb0ad19
DJ
6264 register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
6265 register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);
29709017
DJ
6266
6267 /* If the size matches the full set of registers GDB traditionally
6268 knows about, including floating point, for either 32-bit or
6269 64-bit, assume that's what we've got. */
4eb0ad19
DJ
6270 register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
6271 register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);
29709017
DJ
6272
6273 /* Otherwise we don't have a useful guess. */
6274}
6275
f8b73d13
DJ
6276static struct value *
6277value_of_mips_user_reg (struct frame_info *frame, const void *baton)
6278{
6279 const int *reg_p = baton;
6280 return value_of_register (*reg_p, frame);
6281}
6282
c2d11a7d 6283static struct gdbarch *
6d82d43b 6284mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
c2d11a7d 6285{
c2d11a7d
JM
6286 struct gdbarch *gdbarch;
6287 struct gdbarch_tdep *tdep;
6288 int elf_flags;
2e4ebe70 6289 enum mips_abi mips_abi, found_abi, wanted_abi;
f8b73d13 6290 int i, num_regs;
8d5838b5 6291 enum mips_fpu_type fpu_type;
f8b73d13 6292 struct tdesc_arch_data *tdesc_data = NULL;
609ca2b9 6293 int elf_fpu_type = 0;
1faeff08
MR
6294 const char **reg_names;
6295 struct mips_regnum mips_regnum, *regnum;
6296 int dspacc;
6297 int dspctl;
6298
6299 /* Fill in the OS dependent register numbers and names. */
6300 if (info.osabi == GDB_OSABI_IRIX)
6301 {
6302 mips_regnum.fp0 = 32;
6303 mips_regnum.pc = 64;
6304 mips_regnum.cause = 65;
6305 mips_regnum.badvaddr = 66;
6306 mips_regnum.hi = 67;
6307 mips_regnum.lo = 68;
6308 mips_regnum.fp_control_status = 69;
6309 mips_regnum.fp_implementation_revision = 70;
6310 mips_regnum.dspacc = dspacc = -1;
6311 mips_regnum.dspctl = dspctl = -1;
6312 num_regs = 71;
6313 reg_names = mips_irix_reg_names;
6314 }
6315 else if (info.osabi == GDB_OSABI_LINUX)
6316 {
6317 mips_regnum.fp0 = 38;
6318 mips_regnum.pc = 37;
6319 mips_regnum.cause = 36;
6320 mips_regnum.badvaddr = 35;
6321 mips_regnum.hi = 34;
6322 mips_regnum.lo = 33;
6323 mips_regnum.fp_control_status = 70;
6324 mips_regnum.fp_implementation_revision = 71;
6325 mips_regnum.dspacc = -1;
6326 mips_regnum.dspctl = -1;
6327 dspacc = 72;
6328 dspctl = 78;
6329 num_regs = 79;
6330 reg_names = mips_linux_reg_names;
6331 }
6332 else
6333 {
6334 mips_regnum.lo = MIPS_EMBED_LO_REGNUM;
6335 mips_regnum.hi = MIPS_EMBED_HI_REGNUM;
6336 mips_regnum.badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
6337 mips_regnum.cause = MIPS_EMBED_CAUSE_REGNUM;
6338 mips_regnum.pc = MIPS_EMBED_PC_REGNUM;
6339 mips_regnum.fp0 = MIPS_EMBED_FP0_REGNUM;
6340 mips_regnum.fp_control_status = 70;
6341 mips_regnum.fp_implementation_revision = 71;
6342 mips_regnum.dspacc = dspacc = -1;
6343 mips_regnum.dspctl = dspctl = -1;
6344 num_regs = MIPS_LAST_EMBED_REGNUM + 1;
6345 if (info.bfd_arch_info != NULL
6346 && info.bfd_arch_info->mach == bfd_mach_mips3900)
6347 reg_names = mips_tx39_reg_names;
6348 else
6349 reg_names = mips_generic_reg_names;
6350 }
f8b73d13
DJ
6351
6352 /* Check any target description for validity. */
6353 if (tdesc_has_registers (info.target_desc))
6354 {
6355 static const char *const mips_gprs[] = {
6356 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
6357 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
6358 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
6359 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
6360 };
6361 static const char *const mips_fprs[] = {
6362 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
6363 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
6364 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
6365 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
6366 };
6367
6368 const struct tdesc_feature *feature;
6369 int valid_p;
6370
6371 feature = tdesc_find_feature (info.target_desc,
6372 "org.gnu.gdb.mips.cpu");
6373 if (feature == NULL)
6374 return NULL;
6375
6376 tdesc_data = tdesc_data_alloc ();
6377
6378 valid_p = 1;
6379 for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
6380 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
6381 mips_gprs[i]);
6382
6383
6384 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6385 mips_regnum.lo, "lo");
f8b73d13 6386 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6387 mips_regnum.hi, "hi");
f8b73d13 6388 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6389 mips_regnum.pc, "pc");
f8b73d13
DJ
6390
6391 if (!valid_p)
6392 {
6393 tdesc_data_cleanup (tdesc_data);
6394 return NULL;
6395 }
6396
6397 feature = tdesc_find_feature (info.target_desc,
6398 "org.gnu.gdb.mips.cp0");
6399 if (feature == NULL)
6400 {
6401 tdesc_data_cleanup (tdesc_data);
6402 return NULL;
6403 }
6404
6405 valid_p = 1;
6406 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6407 mips_regnum.badvaddr, "badvaddr");
f8b73d13
DJ
6408 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6409 MIPS_PS_REGNUM, "status");
6410 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6411 mips_regnum.cause, "cause");
f8b73d13
DJ
6412
6413 if (!valid_p)
6414 {
6415 tdesc_data_cleanup (tdesc_data);
6416 return NULL;
6417 }
6418
6419 /* FIXME drow/2007-05-17: The FPU should be optional. The MIPS
6420 backend is not prepared for that, though. */
6421 feature = tdesc_find_feature (info.target_desc,
6422 "org.gnu.gdb.mips.fpu");
6423 if (feature == NULL)
6424 {
6425 tdesc_data_cleanup (tdesc_data);
6426 return NULL;
6427 }
6428
6429 valid_p = 1;
6430 for (i = 0; i < 32; i++)
6431 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08 6432 i + mips_regnum.fp0, mips_fprs[i]);
f8b73d13
DJ
6433
6434 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1faeff08
MR
6435 mips_regnum.fp_control_status,
6436 "fcsr");
6437 valid_p
6438 &= tdesc_numbered_register (feature, tdesc_data,
6439 mips_regnum.fp_implementation_revision,
6440 "fir");
f8b73d13
DJ
6441
6442 if (!valid_p)
6443 {
6444 tdesc_data_cleanup (tdesc_data);
6445 return NULL;
6446 }
6447
1faeff08
MR
6448 if (dspacc >= 0)
6449 {
6450 feature = tdesc_find_feature (info.target_desc,
6451 "org.gnu.gdb.mips.dsp");
6452 /* The DSP registers are optional; it's OK if they are absent. */
6453 if (feature != NULL)
6454 {
6455 i = 0;
6456 valid_p = 1;
6457 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6458 dspacc + i++, "hi1");
6459 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6460 dspacc + i++, "lo1");
6461 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6462 dspacc + i++, "hi2");
6463 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6464 dspacc + i++, "lo2");
6465 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6466 dspacc + i++, "hi3");
6467 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6468 dspacc + i++, "lo3");
6469
6470 valid_p &= tdesc_numbered_register (feature, tdesc_data,
6471 dspctl, "dspctl");
6472
6473 if (!valid_p)
6474 {
6475 tdesc_data_cleanup (tdesc_data);
6476 return NULL;
6477 }
6478
6479 mips_regnum.dspacc = dspacc;
6480 mips_regnum.dspctl = dspctl;
6481 }
6482 }
6483
f8b73d13
DJ
6484 /* It would be nice to detect an attempt to use a 64-bit ABI
6485 when only 32-bit registers are provided. */
1faeff08 6486 reg_names = NULL;
f8b73d13 6487 }
c2d11a7d 6488
ec03c1ac
AC
6489 /* First of all, extract the elf_flags, if available. */
6490 if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
6491 elf_flags = elf_elfheader (info.abfd)->e_flags;
6214a8a1
AC
6492 else if (arches != NULL)
6493 elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
ec03c1ac
AC
6494 else
6495 elf_flags = 0;
6496 if (gdbarch_debug)
6497 fprintf_unfiltered (gdb_stdlog,
6d82d43b 6498 "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
c2d11a7d 6499
102182a9 6500 /* Check ELF_FLAGS to see if it specifies the ABI being used. */
0dadbba0
AC
6501 switch ((elf_flags & EF_MIPS_ABI))
6502 {
6503 case E_MIPS_ABI_O32:
ec03c1ac 6504 found_abi = MIPS_ABI_O32;
0dadbba0
AC
6505 break;
6506 case E_MIPS_ABI_O64:
ec03c1ac 6507 found_abi = MIPS_ABI_O64;
0dadbba0
AC
6508 break;
6509 case E_MIPS_ABI_EABI32:
ec03c1ac 6510 found_abi = MIPS_ABI_EABI32;
0dadbba0
AC
6511 break;
6512 case E_MIPS_ABI_EABI64:
ec03c1ac 6513 found_abi = MIPS_ABI_EABI64;
0dadbba0
AC
6514 break;
6515 default:
acdb74a0 6516 if ((elf_flags & EF_MIPS_ABI2))
ec03c1ac 6517 found_abi = MIPS_ABI_N32;
acdb74a0 6518 else
ec03c1ac 6519 found_abi = MIPS_ABI_UNKNOWN;
0dadbba0
AC
6520 break;
6521 }
acdb74a0 6522
caaa3122 6523 /* GCC creates a pseudo-section whose name describes the ABI. */
ec03c1ac
AC
6524 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
6525 bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
caaa3122 6526
dc305454 6527 /* If we have no useful BFD information, use the ABI from the last
ec03c1ac
AC
6528 MIPS architecture (if there is one). */
6529 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
6530 found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
2e4ebe70 6531
32a6503c 6532 /* Try the architecture for any hint of the correct ABI. */
ec03c1ac 6533 if (found_abi == MIPS_ABI_UNKNOWN
bf64bfd6
AC
6534 && info.bfd_arch_info != NULL
6535 && info.bfd_arch_info->arch == bfd_arch_mips)
6536 {
6537 switch (info.bfd_arch_info->mach)
6538 {
6539 case bfd_mach_mips3900:
ec03c1ac 6540 found_abi = MIPS_ABI_EABI32;
bf64bfd6
AC
6541 break;
6542 case bfd_mach_mips4100:
6543 case bfd_mach_mips5000:
ec03c1ac 6544 found_abi = MIPS_ABI_EABI64;
bf64bfd6 6545 break;
1d06468c
EZ
6546 case bfd_mach_mips8000:
6547 case bfd_mach_mips10000:
32a6503c
KB
6548 /* On Irix, ELF64 executables use the N64 ABI. The
6549 pseudo-sections which describe the ABI aren't present
6550 on IRIX. (Even for executables created by gcc.) */
28d169de
KB
6551 if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
6552 && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
ec03c1ac 6553 found_abi = MIPS_ABI_N64;
28d169de 6554 else
ec03c1ac 6555 found_abi = MIPS_ABI_N32;
1d06468c 6556 break;
bf64bfd6
AC
6557 }
6558 }
2e4ebe70 6559
26c53e50
DJ
6560 /* Default 64-bit objects to N64 instead of O32. */
6561 if (found_abi == MIPS_ABI_UNKNOWN
6562 && info.abfd != NULL
6563 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
6564 && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6565 found_abi = MIPS_ABI_N64;
6566
ec03c1ac
AC
6567 if (gdbarch_debug)
6568 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
6569 found_abi);
6570
6571 /* What has the user specified from the command line? */
6572 wanted_abi = global_mips_abi ();
6573 if (gdbarch_debug)
6574 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
6575 wanted_abi);
2e4ebe70
DJ
6576
6577 /* Now that we have found what the ABI for this binary would be,
6578 check whether the user is overriding it. */
2e4ebe70
DJ
6579 if (wanted_abi != MIPS_ABI_UNKNOWN)
6580 mips_abi = wanted_abi;
ec03c1ac
AC
6581 else if (found_abi != MIPS_ABI_UNKNOWN)
6582 mips_abi = found_abi;
6583 else
6584 mips_abi = MIPS_ABI_O32;
6585 if (gdbarch_debug)
6586 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
6587 mips_abi);
2e4ebe70 6588
ec03c1ac 6589 /* Also used when doing an architecture lookup. */
4b9b3959 6590 if (gdbarch_debug)
ec03c1ac 6591 fprintf_unfiltered (gdb_stdlog,
025bb325
MS
6592 "mips_gdbarch_init: "
6593 "mips64_transfers_32bit_regs_p = %d\n",
ec03c1ac 6594 mips64_transfers_32bit_regs_p);
0dadbba0 6595
8d5838b5 6596 /* Determine the MIPS FPU type. */
609ca2b9
DJ
6597#ifdef HAVE_ELF
6598 if (info.abfd
6599 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
6600 elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
6601 Tag_GNU_MIPS_ABI_FP);
6602#endif /* HAVE_ELF */
6603
8d5838b5
AC
6604 if (!mips_fpu_type_auto)
6605 fpu_type = mips_fpu_type;
609ca2b9
DJ
6606 else if (elf_fpu_type != 0)
6607 {
6608 switch (elf_fpu_type)
6609 {
6610 case 1:
6611 fpu_type = MIPS_FPU_DOUBLE;
6612 break;
6613 case 2:
6614 fpu_type = MIPS_FPU_SINGLE;
6615 break;
6616 case 3:
6617 default:
6618 /* Soft float or unknown. */
6619 fpu_type = MIPS_FPU_NONE;
6620 break;
6621 }
6622 }
8d5838b5
AC
6623 else if (info.bfd_arch_info != NULL
6624 && info.bfd_arch_info->arch == bfd_arch_mips)
6625 switch (info.bfd_arch_info->mach)
6626 {
6627 case bfd_mach_mips3900:
6628 case bfd_mach_mips4100:
6629 case bfd_mach_mips4111:
a9d61c86 6630 case bfd_mach_mips4120:
8d5838b5
AC
6631 fpu_type = MIPS_FPU_NONE;
6632 break;
6633 case bfd_mach_mips4650:
6634 fpu_type = MIPS_FPU_SINGLE;
6635 break;
6636 default:
6637 fpu_type = MIPS_FPU_DOUBLE;
6638 break;
6639 }
6640 else if (arches != NULL)
6641 fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
6642 else
6643 fpu_type = MIPS_FPU_DOUBLE;
6644 if (gdbarch_debug)
6645 fprintf_unfiltered (gdb_stdlog,
6d82d43b 6646 "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
8d5838b5 6647
29709017
DJ
6648 /* Check for blatant incompatibilities. */
6649
6650 /* If we have only 32-bit registers, then we can't debug a 64-bit
6651 ABI. */
6652 if (info.target_desc
6653 && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
6654 && mips_abi != MIPS_ABI_EABI32
6655 && mips_abi != MIPS_ABI_O32)
f8b73d13
DJ
6656 {
6657 if (tdesc_data != NULL)
6658 tdesc_data_cleanup (tdesc_data);
6659 return NULL;
6660 }
29709017 6661
025bb325 6662 /* Try to find a pre-existing architecture. */
c2d11a7d
JM
6663 for (arches = gdbarch_list_lookup_by_info (arches, &info);
6664 arches != NULL;
6665 arches = gdbarch_list_lookup_by_info (arches->next, &info))
6666 {
6667 /* MIPS needs to be pedantic about which ABI the object is
102182a9 6668 using. */
9103eae0 6669 if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
c2d11a7d 6670 continue;
9103eae0 6671 if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
0dadbba0 6672 continue;
719ec221
AC
6673 /* Need to be pedantic about which register virtual size is
6674 used. */
6675 if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
6676 != mips64_transfers_32bit_regs_p)
6677 continue;
8d5838b5
AC
6678 /* Be pedantic about which FPU is selected. */
6679 if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
6680 continue;
f8b73d13
DJ
6681
6682 if (tdesc_data != NULL)
6683 tdesc_data_cleanup (tdesc_data);
4be87837 6684 return arches->gdbarch;
c2d11a7d
JM
6685 }
6686
102182a9 6687 /* Need a new architecture. Fill in a target specific vector. */
c2d11a7d
JM
6688 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
6689 gdbarch = gdbarch_alloc (&info, tdep);
6690 tdep->elf_flags = elf_flags;
719ec221 6691 tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
ec03c1ac
AC
6692 tdep->found_abi = found_abi;
6693 tdep->mips_abi = mips_abi;
8d5838b5 6694 tdep->mips_fpu_type = fpu_type;
29709017
DJ
6695 tdep->register_size_valid_p = 0;
6696 tdep->register_size = 0;
50e8a0d5
HZ
6697 tdep->gregset = NULL;
6698 tdep->gregset64 = NULL;
6699 tdep->fpregset = NULL;
6700 tdep->fpregset64 = NULL;
29709017
DJ
6701
6702 if (info.target_desc)
6703 {
6704 /* Some useful properties can be inferred from the target. */
6705 if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
6706 {
6707 tdep->register_size_valid_p = 1;
6708 tdep->register_size = 4;
6709 }
6710 else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
6711 {
6712 tdep->register_size_valid_p = 1;
6713 tdep->register_size = 8;
6714 }
6715 }
c2d11a7d 6716
102182a9 6717 /* Initially set everything according to the default ABI/ISA. */
c2d11a7d
JM
6718 set_gdbarch_short_bit (gdbarch, 16);
6719 set_gdbarch_int_bit (gdbarch, 32);
6720 set_gdbarch_float_bit (gdbarch, 32);
6721 set_gdbarch_double_bit (gdbarch, 64);
6722 set_gdbarch_long_double_bit (gdbarch, 64);
a4b8ebc8
AC
6723 set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
6724 set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
6725 set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
1d06468c 6726
175ff332
HZ
6727 set_gdbarch_ax_pseudo_register_collect (gdbarch,
6728 mips_ax_pseudo_register_collect);
6729 set_gdbarch_ax_pseudo_register_push_stack
6730 (gdbarch, mips_ax_pseudo_register_push_stack);
6731
6d82d43b 6732 set_gdbarch_elf_make_msymbol_special (gdbarch,
f7ab6ec6
MS
6733 mips_elf_make_msymbol_special);
6734
1faeff08
MR
6735 regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct mips_regnum);
6736 *regnum = mips_regnum;
1faeff08
MR
6737 set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
6738 set_gdbarch_num_regs (gdbarch, num_regs);
6739 set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
6740 set_gdbarch_register_name (gdbarch, mips_register_name);
6741 set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
6742 tdep->mips_processor_reg_names = reg_names;
6743 tdep->regnum = regnum;
fe29b929 6744
0dadbba0 6745 switch (mips_abi)
c2d11a7d 6746 {
0dadbba0 6747 case MIPS_ABI_O32:
25ab4790 6748 set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
29dfb2ac 6749 set_gdbarch_return_value (gdbarch, mips_o32_return_value);
4c7d22cb 6750 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
56cea623 6751 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
4014092b 6752 tdep->default_mask_address_p = 0;
c2d11a7d
JM
6753 set_gdbarch_long_bit (gdbarch, 32);
6754 set_gdbarch_ptr_bit (gdbarch, 32);
6755 set_gdbarch_long_long_bit (gdbarch, 64);
6756 break;
0dadbba0 6757 case MIPS_ABI_O64:
25ab4790 6758 set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
9c8fdbfa 6759 set_gdbarch_return_value (gdbarch, mips_o64_return_value);
4c7d22cb 6760 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
56cea623 6761 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
361d1df0 6762 tdep->default_mask_address_p = 0;
c2d11a7d
JM
6763 set_gdbarch_long_bit (gdbarch, 32);
6764 set_gdbarch_ptr_bit (gdbarch, 32);
6765 set_gdbarch_long_long_bit (gdbarch, 64);
6766 break;
0dadbba0 6767 case MIPS_ABI_EABI32:
25ab4790 6768 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
9c8fdbfa 6769 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
4c7d22cb 6770 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 6771 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 6772 tdep->default_mask_address_p = 0;
c2d11a7d
JM
6773 set_gdbarch_long_bit (gdbarch, 32);
6774 set_gdbarch_ptr_bit (gdbarch, 32);
6775 set_gdbarch_long_long_bit (gdbarch, 64);
6776 break;
0dadbba0 6777 case MIPS_ABI_EABI64:
25ab4790 6778 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
9c8fdbfa 6779 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
4c7d22cb 6780 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 6781 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 6782 tdep->default_mask_address_p = 0;
c2d11a7d
JM
6783 set_gdbarch_long_bit (gdbarch, 64);
6784 set_gdbarch_ptr_bit (gdbarch, 64);
6785 set_gdbarch_long_long_bit (gdbarch, 64);
6786 break;
0dadbba0 6787 case MIPS_ABI_N32:
25ab4790 6788 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
29dfb2ac 6789 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
4c7d22cb 6790 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 6791 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
4014092b 6792 tdep->default_mask_address_p = 0;
0dadbba0
AC
6793 set_gdbarch_long_bit (gdbarch, 32);
6794 set_gdbarch_ptr_bit (gdbarch, 32);
6795 set_gdbarch_long_long_bit (gdbarch, 64);
fed7ba43 6796 set_gdbarch_long_double_bit (gdbarch, 128);
b14d30e1 6797 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
28d169de
KB
6798 break;
6799 case MIPS_ABI_N64:
25ab4790 6800 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
29dfb2ac 6801 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
4c7d22cb 6802 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
56cea623 6803 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
28d169de
KB
6804 tdep->default_mask_address_p = 0;
6805 set_gdbarch_long_bit (gdbarch, 64);
6806 set_gdbarch_ptr_bit (gdbarch, 64);
6807 set_gdbarch_long_long_bit (gdbarch, 64);
fed7ba43 6808 set_gdbarch_long_double_bit (gdbarch, 128);
b14d30e1 6809 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
0dadbba0 6810 break;
c2d11a7d 6811 default:
e2e0b3e5 6812 internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
c2d11a7d
JM
6813 }
6814
22e47e37
FF
6815 /* GCC creates a pseudo-section whose name specifies the size of
6816 longs, since -mlong32 or -mlong64 may be used independent of
6817 other options. How those options affect pointer sizes is ABI and
6818 architecture dependent, so use them to override the default sizes
6819 set by the ABI. This table shows the relationship between ABI,
6820 -mlongXX, and size of pointers:
6821
6822 ABI -mlongXX ptr bits
6823 --- -------- --------
6824 o32 32 32
6825 o32 64 32
6826 n32 32 32
6827 n32 64 64
6828 o64 32 32
6829 o64 64 64
6830 n64 32 32
6831 n64 64 64
6832 eabi32 32 32
6833 eabi32 64 32
6834 eabi64 32 32
6835 eabi64 64 64
6836
6837 Note that for o32 and eabi32, pointers are always 32 bits
6838 regardless of any -mlongXX option. For all others, pointers and
025bb325 6839 longs are the same, as set by -mlongXX or set by defaults. */
22e47e37
FF
6840
6841 if (info.abfd != NULL)
6842 {
6843 int long_bit = 0;
6844
6845 bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
6846 if (long_bit)
6847 {
6848 set_gdbarch_long_bit (gdbarch, long_bit);
6849 switch (mips_abi)
6850 {
6851 case MIPS_ABI_O32:
6852 case MIPS_ABI_EABI32:
6853 break;
6854 case MIPS_ABI_N32:
6855 case MIPS_ABI_O64:
6856 case MIPS_ABI_N64:
6857 case MIPS_ABI_EABI64:
6858 set_gdbarch_ptr_bit (gdbarch, long_bit);
6859 break;
6860 default:
6861 internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
6862 }
6863 }
6864 }
6865
a5ea2558
AC
6866 /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
6867 that could indicate -gp32 BUT gas/config/tc-mips.c contains the
6868 comment:
6869
6870 ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
6871 flag in object files because to do so would make it impossible to
102182a9 6872 link with libraries compiled without "-gp32". This is
a5ea2558 6873 unnecessarily restrictive.
361d1df0 6874
a5ea2558
AC
6875 We could solve this problem by adding "-gp32" multilibs to gcc,
6876 but to set this flag before gcc is built with such multilibs will
6877 break too many systems.''
6878
6879 But even more unhelpfully, the default linker output target for
6880 mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
6881 for 64-bit programs - you need to change the ABI to change this,
102182a9 6882 and not all gcc targets support that currently. Therefore using
a5ea2558
AC
6883 this flag to detect 32-bit mode would do the wrong thing given
6884 the current gcc - it would make GDB treat these 64-bit programs
102182a9 6885 as 32-bit programs by default. */
a5ea2558 6886
6c997a34 6887 set_gdbarch_read_pc (gdbarch, mips_read_pc);
b6cb9035 6888 set_gdbarch_write_pc (gdbarch, mips_write_pc);
c2d11a7d 6889
102182a9
MS
6890 /* Add/remove bits from an address. The MIPS needs be careful to
6891 ensure that all 32 bit addresses are sign extended to 64 bits. */
875e1767
AC
6892 set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
6893
58dfe9ff
AC
6894 /* Unwind the frame. */
6895 set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
30244cd8 6896 set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
b8a22b94 6897 set_gdbarch_dummy_id (gdbarch, mips_dummy_id);
10312cc4 6898
102182a9 6899 /* Map debug register numbers onto internal register numbers. */
88c72b7d 6900 set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
6d82d43b
AC
6901 set_gdbarch_ecoff_reg_to_regnum (gdbarch,
6902 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
6d82d43b
AC
6903 set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
6904 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
a4b8ebc8 6905 set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
88c72b7d 6906
025bb325 6907 /* MIPS version of CALL_DUMMY. */
c2d11a7d 6908
9710e734
AC
6909 /* NOTE: cagney/2003-08-05: Eventually call dummy location will be
6910 replaced by a command, and all targets will default to on stack
6911 (regardless of the stack's execute status). */
6912 set_gdbarch_call_dummy_location (gdbarch, AT_SYMBOL);
dc604539 6913 set_gdbarch_frame_align (gdbarch, mips_frame_align);
d05285fa 6914
87783b8b
AC
6915 set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
6916 set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
6917 set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
6918
f7b9e9fc
AC
6919 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
6920 set_gdbarch_breakpoint_from_pc (gdbarch, mips_breakpoint_from_pc);
c8cef75f
MR
6921 set_gdbarch_adjust_breakpoint_address (gdbarch,
6922 mips_adjust_breakpoint_address);
f7b9e9fc
AC
6923
6924 set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
f7b9e9fc 6925
97ab0fdd
MR
6926 set_gdbarch_in_function_epilogue_p (gdbarch, mips_in_function_epilogue_p);
6927
fc0c74b1
AC
6928 set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
6929 set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
6930 set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
70f80edf 6931
a4b8ebc8 6932 set_gdbarch_register_type (gdbarch, mips_register_type);
78fde5f8 6933
e11c53d2 6934 set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
bf1f5b4c 6935
9dae60cc
UW
6936 if (mips_abi == MIPS_ABI_N32)
6937 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
6938 else if (mips_abi == MIPS_ABI_N64)
6939 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
6940 else
6941 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
e5ab0dce 6942
d92524f1
PM
6943 /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
6944 HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
3a3bc038 6945 need to all be folded into the target vector. Since they are
d92524f1
PM
6946 being used as guards for target_stopped_by_watchpoint, why not have
6947 target_stopped_by_watchpoint return the type of watchpoint that the code
3a3bc038
AC
6948 is sitting on? */
6949 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
6950
e7d6a6d2 6951 set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
757a7cc6 6952
14132e89
MR
6953 /* NOTE drow/2012-04-25: We overload the core solib trampoline code
6954 to support MIPS16. This is a bad thing. Make sure not to do it
6955 if we have an OS ABI that actually supports shared libraries, since
6956 shared library support is more important. If we have an OS someday
6957 that supports both shared libraries and MIPS16, we'll have to find
6958 a better place for these.
6959 macro/2012-04-25: But that applies to return trampolines only and
6960 currently no MIPS OS ABI uses shared libraries that have them. */
6961 set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);
6962
025bb325
MS
6963 set_gdbarch_single_step_through_delay (gdbarch,
6964 mips_single_step_through_delay);
3352ef37 6965
0d5de010
DJ
6966 /* Virtual tables. */
6967 set_gdbarch_vbit_in_delta (gdbarch, 1);
6968
29709017
DJ
6969 mips_register_g_packet_guesses (gdbarch);
6970
6de918a6 6971 /* Hook in OS ABI-specific overrides, if they have been registered. */
822b6570 6972 info.tdep_info = (void *) tdesc_data;
6de918a6 6973 gdbarch_init_osabi (info, gdbarch);
757a7cc6 6974
9aac7884
MR
6975 /* The hook may have adjusted num_regs, fetch the final value and
6976 set pc_regnum and sp_regnum now that it has been fixed. */
6977 /* FIXME: cagney/2003-11-15: For MIPS, hasn't gdbarch_pc_regnum been
6978 replaced by gdbarch_read_pc? */
6979 num_regs = gdbarch_num_regs (gdbarch);
6980 set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
6981 set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
6982
5792a79b 6983 /* Unwind the frame. */
b8a22b94
DJ
6984 dwarf2_append_unwinders (gdbarch);
6985 frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
6986 frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
6987 frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
2bd0c3d7 6988 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
eec63939 6989 frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
45c9dd44
AC
6990 frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
6991 frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
5792a79b 6992
f8b73d13
DJ
6993 if (tdesc_data)
6994 {
6995 set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
7cc46491 6996 tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
f8b73d13
DJ
6997
6998 /* Override the normal target description methods to handle our
6999 dual real and pseudo registers. */
7000 set_gdbarch_register_name (gdbarch, mips_register_name);
025bb325
MS
7001 set_gdbarch_register_reggroup_p (gdbarch,
7002 mips_tdesc_register_reggroup_p);
f8b73d13
DJ
7003
7004 num_regs = gdbarch_num_regs (gdbarch);
7005 set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
7006 set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
7007 set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
7008 }
7009
7010 /* Add ABI-specific aliases for the registers. */
7011 if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
7012 for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
7013 user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
7014 value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
7015 else
7016 for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
7017 user_reg_add (gdbarch, mips_o32_aliases[i].name,
7018 value_of_mips_user_reg, &mips_o32_aliases[i].regnum);
7019
7020 /* Add some other standard aliases. */
7021 for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
7022 user_reg_add (gdbarch, mips_register_aliases[i].name,
7023 value_of_mips_user_reg, &mips_register_aliases[i].regnum);
7024
865093a3
AR
7025 for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
7026 user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
7027 value_of_mips_user_reg,
7028 &mips_numeric_register_aliases[i].regnum);
7029
4b9b3959
AC
7030 return gdbarch;
7031}
7032
2e4ebe70 7033static void
6d82d43b 7034mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
2e4ebe70
DJ
7035{
7036 struct gdbarch_info info;
7037
7038 /* Force the architecture to update, and (if it's a MIPS architecture)
7039 mips_gdbarch_init will take care of the rest. */
7040 gdbarch_info_init (&info);
7041 gdbarch_update_p (info);
7042}
7043
ad188201
KB
7044/* Print out which MIPS ABI is in use. */
7045
7046static void
1f8ca57c
JB
7047show_mips_abi (struct ui_file *file,
7048 int from_tty,
7049 struct cmd_list_element *ignored_cmd,
7050 const char *ignored_value)
ad188201 7051{
1cf3db46 7052 if (gdbarch_bfd_arch_info (target_gdbarch)->arch != bfd_arch_mips)
1f8ca57c
JB
7053 fprintf_filtered
7054 (file,
7055 "The MIPS ABI is unknown because the current architecture "
7056 "is not MIPS.\n");
ad188201
KB
7057 else
7058 {
7059 enum mips_abi global_abi = global_mips_abi ();
1cf3db46 7060 enum mips_abi actual_abi = mips_abi (target_gdbarch);
ad188201
KB
7061 const char *actual_abi_str = mips_abi_strings[actual_abi];
7062
7063 if (global_abi == MIPS_ABI_UNKNOWN)
1f8ca57c
JB
7064 fprintf_filtered
7065 (file,
7066 "The MIPS ABI is set automatically (currently \"%s\").\n",
6d82d43b 7067 actual_abi_str);
ad188201 7068 else if (global_abi == actual_abi)
1f8ca57c
JB
7069 fprintf_filtered
7070 (file,
7071 "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
6d82d43b 7072 actual_abi_str);
ad188201
KB
7073 else
7074 {
7075 /* Probably shouldn't happen... */
025bb325
MS
7076 fprintf_filtered (file,
7077 "The (auto detected) MIPS ABI \"%s\" is in use "
7078 "even though the user setting was \"%s\".\n",
6d82d43b 7079 actual_abi_str, mips_abi_strings[global_abi]);
ad188201
KB
7080 }
7081 }
7082}
7083
4b9b3959 7084static void
72a155b4 7085mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
4b9b3959 7086{
72a155b4 7087 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4b9b3959 7088 if (tdep != NULL)
c2d11a7d 7089 {
acdb74a0
AC
7090 int ef_mips_arch;
7091 int ef_mips_32bitmode;
f49e4e6d 7092 /* Determine the ISA. */
acdb74a0
AC
7093 switch (tdep->elf_flags & EF_MIPS_ARCH)
7094 {
7095 case E_MIPS_ARCH_1:
7096 ef_mips_arch = 1;
7097 break;
7098 case E_MIPS_ARCH_2:
7099 ef_mips_arch = 2;
7100 break;
7101 case E_MIPS_ARCH_3:
7102 ef_mips_arch = 3;
7103 break;
7104 case E_MIPS_ARCH_4:
93d56215 7105 ef_mips_arch = 4;
acdb74a0
AC
7106 break;
7107 default:
93d56215 7108 ef_mips_arch = 0;
acdb74a0
AC
7109 break;
7110 }
f49e4e6d 7111 /* Determine the size of a pointer. */
acdb74a0 7112 ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
4b9b3959
AC
7113 fprintf_unfiltered (file,
7114 "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
0dadbba0 7115 tdep->elf_flags);
4b9b3959 7116 fprintf_unfiltered (file,
acdb74a0
AC
7117 "mips_dump_tdep: ef_mips_32bitmode = %d\n",
7118 ef_mips_32bitmode);
7119 fprintf_unfiltered (file,
7120 "mips_dump_tdep: ef_mips_arch = %d\n",
7121 ef_mips_arch);
7122 fprintf_unfiltered (file,
7123 "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
6d82d43b 7124 tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
4014092b 7125 fprintf_unfiltered (file,
025bb325
MS
7126 "mips_dump_tdep: "
7127 "mips_mask_address_p() %d (default %d)\n",
480d3dd2 7128 mips_mask_address_p (tdep),
4014092b 7129 tdep->default_mask_address_p);
c2d11a7d 7130 }
4b9b3959
AC
7131 fprintf_unfiltered (file,
7132 "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
7133 MIPS_DEFAULT_FPU_TYPE,
7134 (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
7135 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
7136 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
7137 : "???"));
74ed0bb4
MD
7138 fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
7139 MIPS_EABI (gdbarch));
4b9b3959
AC
7140 fprintf_unfiltered (file,
7141 "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
74ed0bb4
MD
7142 MIPS_FPU_TYPE (gdbarch),
7143 (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
7144 : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
7145 : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
4b9b3959 7146 : "???"));
c2d11a7d
JM
7147}
7148
025bb325 7149extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
a78f21af 7150
c906108c 7151void
acdb74a0 7152_initialize_mips_tdep (void)
c906108c
SS
7153{
7154 static struct cmd_list_element *mipsfpulist = NULL;
7155 struct cmd_list_element *c;
7156
6d82d43b 7157 mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
2e4ebe70
DJ
7158 if (MIPS_ABI_LAST + 1
7159 != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
e2e0b3e5 7160 internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
2e4ebe70 7161
4b9b3959 7162 gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
c906108c 7163
8d5f9dcb
DJ
7164 mips_pdr_data = register_objfile_data ();
7165
4eb0ad19
DJ
7166 /* Create feature sets with the appropriate properties. The values
7167 are not important. */
7168 mips_tdesc_gp32 = allocate_target_description ();
7169 set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");
7170
7171 mips_tdesc_gp64 = allocate_target_description ();
7172 set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");
7173
025bb325 7174 /* Add root prefix command for all "set mips"/"show mips" commands. */
a5ea2558 7175 add_prefix_cmd ("mips", no_class, set_mips_command,
1bedd215 7176 _("Various MIPS specific commands."),
a5ea2558
AC
7177 &setmipscmdlist, "set mips ", 0, &setlist);
7178
7179 add_prefix_cmd ("mips", no_class, show_mips_command,
1bedd215 7180 _("Various MIPS specific commands."),
a5ea2558
AC
7181 &showmipscmdlist, "show mips ", 0, &showlist);
7182
025bb325 7183 /* Allow the user to override the ABI. */
7ab04401
AC
7184 add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
7185 &mips_abi_string, _("\
7186Set the MIPS ABI used by this program."), _("\
7187Show the MIPS ABI used by this program."), _("\
7188This option can be set to one of:\n\
7189 auto - the default ABI associated with the current binary\n\
7190 o32\n\
7191 o64\n\
7192 n32\n\
7193 n64\n\
7194 eabi32\n\
7195 eabi64"),
7196 mips_abi_update,
7197 show_mips_abi,
7198 &setmipscmdlist, &showmipscmdlist);
2e4ebe70 7199
c906108c
SS
7200 /* Let the user turn off floating point and set the fence post for
7201 heuristic_proc_start. */
7202
7203 add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
1bedd215 7204 _("Set use of MIPS floating-point coprocessor."),
c906108c
SS
7205 &mipsfpulist, "set mipsfpu ", 0, &setlist);
7206 add_cmd ("single", class_support, set_mipsfpu_single_command,
1a966eab 7207 _("Select single-precision MIPS floating-point coprocessor."),
c906108c
SS
7208 &mipsfpulist);
7209 add_cmd ("double", class_support, set_mipsfpu_double_command,
1a966eab 7210 _("Select double-precision MIPS floating-point coprocessor."),
c906108c
SS
7211 &mipsfpulist);
7212 add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
7213 add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
7214 add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
7215 add_cmd ("none", class_support, set_mipsfpu_none_command,
1a966eab 7216 _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
c906108c
SS
7217 add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
7218 add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
7219 add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
7220 add_cmd ("auto", class_support, set_mipsfpu_auto_command,
1a966eab 7221 _("Select MIPS floating-point coprocessor automatically."),
c906108c
SS
7222 &mipsfpulist);
7223 add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
1a966eab 7224 _("Show current use of MIPS floating-point coprocessor target."),
c906108c
SS
7225 &showlist);
7226
c906108c
SS
7227 /* We really would like to have both "0" and "unlimited" work, but
7228 command.c doesn't deal with that. So make it a var_zinteger
7229 because the user can always use "999999" or some such for unlimited. */
6bcadd06 7230 add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
7915a72c
AC
7231 &heuristic_fence_post, _("\
7232Set the distance searched for the start of a function."), _("\
7233Show the distance searched for the start of a function."), _("\
c906108c
SS
7234If you are debugging a stripped executable, GDB needs to search through the\n\
7235program for the start of a function. This command sets the distance of the\n\
7915a72c 7236search. The only need to set it is when debugging a stripped executable."),
2c5b56ce 7237 reinit_frame_cache_sfunc,
025bb325
MS
7238 NULL, /* FIXME: i18n: The distance searched for
7239 the start of a function is %s. */
6bcadd06 7240 &setlist, &showlist);
c906108c
SS
7241
7242 /* Allow the user to control whether the upper bits of 64-bit
7243 addresses should be zeroed. */
7915a72c
AC
7244 add_setshow_auto_boolean_cmd ("mask-address", no_class,
7245 &mask_address_var, _("\
7246Set zeroing of upper 32 bits of 64-bit addresses."), _("\
7247Show zeroing of upper 32 bits of 64-bit addresses."), _("\
cce7e648 7248Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
7915a72c 7249allow GDB to determine the correct value."),
08546159
AC
7250 NULL, show_mask_address,
7251 &setmipscmdlist, &showmipscmdlist);
43e526b9
JM
7252
7253 /* Allow the user to control the size of 32 bit registers within the
7254 raw remote packet. */
b3f42336 7255 add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
7915a72c
AC
7256 &mips64_transfers_32bit_regs_p, _("\
7257Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
7258 _("\
7259Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
7260 _("\
719ec221
AC
7261Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
7262that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
7915a72c 726364 bits for others. Use \"off\" to disable compatibility mode"),
2c5b56ce 7264 set_mips64_transfers_32bit_regs,
025bb325
MS
7265 NULL, /* FIXME: i18n: Compatibility with 64-bit
7266 MIPS target that transfers 32-bit
7267 quantities is %s. */
7915a72c 7268 &setlist, &showlist);
9ace0497 7269
025bb325 7270 /* Debug this files internals. */
6bcadd06 7271 add_setshow_zinteger_cmd ("mips", class_maintenance,
7915a72c
AC
7272 &mips_debug, _("\
7273Set mips debugging."), _("\
7274Show mips debugging."), _("\
7275When non-zero, mips specific debugging is enabled."),
2c5b56ce 7276 NULL,
025bb325
MS
7277 NULL, /* FIXME: i18n: Mips debugging is
7278 currently %s. */
6bcadd06 7279 &setdebuglist, &showdebuglist);
c906108c 7280}
This page took 2.151824 seconds and 4 git commands to generate.