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