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