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