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