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