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