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