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