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