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