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