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