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