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