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