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