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