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