GDBARCH_BREAKPOINT_MANIPULATION and SET_GDBARCH_BREAKPOINT_MANIPULATION
[deliverable/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-2016 Free Software Foundation, Inc.
4
5 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
6 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "symtab.h"
27 #include "value.h"
28 #include "gdbcmd.h"
29 #include "language.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32 #include "objfiles.h"
33 #include "gdbtypes.h"
34 #include "target.h"
35 #include "arch-utils.h"
36 #include "regcache.h"
37 #include "osabi.h"
38 #include "mips-tdep.h"
39 #include "block.h"
40 #include "reggroups.h"
41 #include "opcode/mips.h"
42 #include "elf/mips.h"
43 #include "elf-bfd.h"
44 #include "symcat.h"
45 #include "sim-regno.h"
46 #include "dis-asm.h"
47 #include "frame-unwind.h"
48 #include "frame-base.h"
49 #include "trad-frame.h"
50 #include "infcall.h"
51 #include "floatformat.h"
52 #include "remote.h"
53 #include "target-descriptions.h"
54 #include "dwarf2-frame.h"
55 #include "user-regs.h"
56 #include "valprint.h"
57 #include "ax.h"
58 #include <algorithm>
59
60 static const struct objfile_data *mips_pdr_data;
61
62 static struct type *mips_register_type (struct gdbarch *gdbarch, int regnum);
63
64 static int mips32_instruction_has_delay_slot (struct gdbarch *gdbarch,
65 ULONGEST inst);
66 static int micromips_instruction_has_delay_slot (ULONGEST insn, int mustbe32);
67 static int mips16_instruction_has_delay_slot (unsigned short inst,
68 int mustbe32);
69
70 static int mips32_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
71 CORE_ADDR addr);
72 static int micromips_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
73 CORE_ADDR addr, int mustbe32);
74 static int mips16_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
75 CORE_ADDR addr, int mustbe32);
76
77 static void mips_print_float_info (struct gdbarch *, struct ui_file *,
78 struct frame_info *, const char *);
79
80 /* A useful bit in the CP0 status register (MIPS_PS_REGNUM). */
81 /* This bit is set if we are emulating 32-bit FPRs on a 64-bit chip. */
82 #define ST0_FR (1 << 26)
83
84 /* The sizes of floating point registers. */
85
86 enum
87 {
88 MIPS_FPU_SINGLE_REGSIZE = 4,
89 MIPS_FPU_DOUBLE_REGSIZE = 8
90 };
91
92 enum
93 {
94 MIPS32_REGSIZE = 4,
95 MIPS64_REGSIZE = 8
96 };
97
98 static const char *mips_abi_string;
99
100 static const char *const mips_abi_strings[] = {
101 "auto",
102 "n32",
103 "o32",
104 "n64",
105 "o64",
106 "eabi32",
107 "eabi64",
108 NULL
109 };
110
111 /* For backwards compatibility we default to MIPS16. This flag is
112 overridden as soon as unambiguous ELF file flags tell us the
113 compressed ISA encoding used. */
114 static const char mips_compression_mips16[] = "mips16";
115 static const char mips_compression_micromips[] = "micromips";
116 static const char *const mips_compression_strings[] =
117 {
118 mips_compression_mips16,
119 mips_compression_micromips,
120 NULL
121 };
122
123 static const char *mips_compression_string = mips_compression_mips16;
124
125 /* The standard register names, and all the valid aliases for them. */
126 struct register_alias
127 {
128 const char *name;
129 int regnum;
130 };
131
132 /* Aliases for o32 and most other ABIs. */
133 const struct register_alias mips_o32_aliases[] = {
134 { "ta0", 12 },
135 { "ta1", 13 },
136 { "ta2", 14 },
137 { "ta3", 15 }
138 };
139
140 /* Aliases for n32 and n64. */
141 const struct register_alias mips_n32_n64_aliases[] = {
142 { "ta0", 8 },
143 { "ta1", 9 },
144 { "ta2", 10 },
145 { "ta3", 11 }
146 };
147
148 /* Aliases for ABI-independent registers. */
149 const struct register_alias mips_register_aliases[] = {
150 /* The architecture manuals specify these ABI-independent names for
151 the GPRs. */
152 #define R(n) { "r" #n, n }
153 R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
154 R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
155 R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
156 R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
157 #undef R
158
159 /* k0 and k1 are sometimes called these instead (for "kernel
160 temp"). */
161 { "kt0", 26 },
162 { "kt1", 27 },
163
164 /* This is the traditional GDB name for the CP0 status register. */
165 { "sr", MIPS_PS_REGNUM },
166
167 /* This is the traditional GDB name for the CP0 BadVAddr register. */
168 { "bad", MIPS_EMBED_BADVADDR_REGNUM },
169
170 /* This is the traditional GDB name for the FCSR. */
171 { "fsr", MIPS_EMBED_FP0_REGNUM + 32 }
172 };
173
174 const struct register_alias mips_numeric_register_aliases[] = {
175 #define R(n) { #n, n }
176 R(0), R(1), R(2), R(3), R(4), R(5), R(6), R(7),
177 R(8), R(9), R(10), R(11), R(12), R(13), R(14), R(15),
178 R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
179 R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
180 #undef R
181 };
182
183 #ifndef MIPS_DEFAULT_FPU_TYPE
184 #define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
185 #endif
186 static int mips_fpu_type_auto = 1;
187 static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
188
189 static unsigned int mips_debug = 0;
190
191 /* Properties (for struct target_desc) describing the g/G packet
192 layout. */
193 #define PROPERTY_GP32 "internal: transfers-32bit-registers"
194 #define PROPERTY_GP64 "internal: transfers-64bit-registers"
195
196 struct target_desc *mips_tdesc_gp32;
197 struct target_desc *mips_tdesc_gp64;
198
199 const struct mips_regnum *
200 mips_regnum (struct gdbarch *gdbarch)
201 {
202 return gdbarch_tdep (gdbarch)->regnum;
203 }
204
205 static int
206 mips_fpa0_regnum (struct gdbarch *gdbarch)
207 {
208 return mips_regnum (gdbarch)->fp0 + 12;
209 }
210
211 /* Return 1 if REGNUM refers to a floating-point general register, raw
212 or cooked. Otherwise return 0. */
213
214 static int
215 mips_float_register_p (struct gdbarch *gdbarch, int regnum)
216 {
217 int rawnum = regnum % gdbarch_num_regs (gdbarch);
218
219 return (rawnum >= mips_regnum (gdbarch)->fp0
220 && rawnum < mips_regnum (gdbarch)->fp0 + 32);
221 }
222
223 #define MIPS_EABI(gdbarch) (gdbarch_tdep (gdbarch)->mips_abi \
224 == MIPS_ABI_EABI32 \
225 || gdbarch_tdep (gdbarch)->mips_abi == MIPS_ABI_EABI64)
226
227 #define MIPS_LAST_FP_ARG_REGNUM(gdbarch) \
228 (gdbarch_tdep (gdbarch)->mips_last_fp_arg_regnum)
229
230 #define MIPS_LAST_ARG_REGNUM(gdbarch) \
231 (gdbarch_tdep (gdbarch)->mips_last_arg_regnum)
232
233 #define MIPS_FPU_TYPE(gdbarch) (gdbarch_tdep (gdbarch)->mips_fpu_type)
234
235 /* Return the MIPS ABI associated with GDBARCH. */
236 enum mips_abi
237 mips_abi (struct gdbarch *gdbarch)
238 {
239 return gdbarch_tdep (gdbarch)->mips_abi;
240 }
241
242 int
243 mips_isa_regsize (struct gdbarch *gdbarch)
244 {
245 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
246
247 /* If we know how big the registers are, use that size. */
248 if (tdep->register_size_valid_p)
249 return tdep->register_size;
250
251 /* Fall back to the previous behavior. */
252 return (gdbarch_bfd_arch_info (gdbarch)->bits_per_word
253 / gdbarch_bfd_arch_info (gdbarch)->bits_per_byte);
254 }
255
256 /* Return the currently configured (or set) saved register size. */
257
258 unsigned int
259 mips_abi_regsize (struct gdbarch *gdbarch)
260 {
261 switch (mips_abi (gdbarch))
262 {
263 case MIPS_ABI_EABI32:
264 case MIPS_ABI_O32:
265 return 4;
266 case MIPS_ABI_N32:
267 case MIPS_ABI_N64:
268 case MIPS_ABI_O64:
269 case MIPS_ABI_EABI64:
270 return 8;
271 case MIPS_ABI_UNKNOWN:
272 case MIPS_ABI_LAST:
273 default:
274 internal_error (__FILE__, __LINE__, _("bad switch"));
275 }
276 }
277
278 /* MIPS16/microMIPS function addresses are odd (bit 0 is set). Here
279 are some functions to handle addresses associated with compressed
280 code including but not limited to testing, setting, or clearing
281 bit 0 of such addresses. */
282
283 /* Return one iff compressed code is the MIPS16 instruction set. */
284
285 static int
286 is_mips16_isa (struct gdbarch *gdbarch)
287 {
288 return gdbarch_tdep (gdbarch)->mips_isa == ISA_MIPS16;
289 }
290
291 /* Return one iff compressed code is the microMIPS instruction set. */
292
293 static int
294 is_micromips_isa (struct gdbarch *gdbarch)
295 {
296 return gdbarch_tdep (gdbarch)->mips_isa == ISA_MICROMIPS;
297 }
298
299 /* Return one iff ADDR denotes compressed code. */
300
301 static int
302 is_compact_addr (CORE_ADDR addr)
303 {
304 return ((addr) & 1);
305 }
306
307 /* Return one iff ADDR denotes standard ISA code. */
308
309 static int
310 is_mips_addr (CORE_ADDR addr)
311 {
312 return !is_compact_addr (addr);
313 }
314
315 /* Return one iff ADDR denotes MIPS16 code. */
316
317 static int
318 is_mips16_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
319 {
320 return is_compact_addr (addr) && is_mips16_isa (gdbarch);
321 }
322
323 /* Return one iff ADDR denotes microMIPS code. */
324
325 static int
326 is_micromips_addr (struct gdbarch *gdbarch, CORE_ADDR addr)
327 {
328 return is_compact_addr (addr) && is_micromips_isa (gdbarch);
329 }
330
331 /* Strip the ISA (compression) bit off from ADDR. */
332
333 static CORE_ADDR
334 unmake_compact_addr (CORE_ADDR addr)
335 {
336 return ((addr) & ~(CORE_ADDR) 1);
337 }
338
339 /* Add the ISA (compression) bit to ADDR. */
340
341 static CORE_ADDR
342 make_compact_addr (CORE_ADDR addr)
343 {
344 return ((addr) | (CORE_ADDR) 1);
345 }
346
347 /* Extern version of unmake_compact_addr; we use a separate function
348 so that unmake_compact_addr can be inlined throughout this file. */
349
350 CORE_ADDR
351 mips_unmake_compact_addr (CORE_ADDR addr)
352 {
353 return unmake_compact_addr (addr);
354 }
355
356 /* Functions for setting and testing a bit in a minimal symbol that
357 marks it as MIPS16 or microMIPS function. The MSB of the minimal
358 symbol's "info" field is used for this purpose.
359
360 gdbarch_elf_make_msymbol_special tests whether an ELF symbol is
361 "special", i.e. refers to a MIPS16 or microMIPS function, and sets
362 one of the "special" bits in a minimal symbol to mark it accordingly.
363 The test checks an ELF-private flag that is valid for true function
364 symbols only; for synthetic symbols such as for PLT stubs that have
365 no ELF-private part at all the MIPS BFD backend arranges for this
366 information to be carried in the asymbol's udata field instead.
367
368 msymbol_is_mips16 and msymbol_is_micromips test the "special" bit
369 in a minimal symbol. */
370
371 static void
372 mips_elf_make_msymbol_special (asymbol * sym, struct minimal_symbol *msym)
373 {
374 elf_symbol_type *elfsym = (elf_symbol_type *) sym;
375 unsigned char st_other;
376
377 if ((sym->flags & BSF_SYNTHETIC) == 0)
378 st_other = elfsym->internal_elf_sym.st_other;
379 else if ((sym->flags & BSF_FUNCTION) != 0)
380 st_other = sym->udata.i;
381 else
382 return;
383
384 if (ELF_ST_IS_MICROMIPS (st_other))
385 {
386 MSYMBOL_TARGET_FLAG_MICROMIPS (msym) = 1;
387 SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) | 1);
388 }
389 else if (ELF_ST_IS_MIPS16 (st_other))
390 {
391 MSYMBOL_TARGET_FLAG_MIPS16 (msym) = 1;
392 SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) | 1);
393 }
394 }
395
396 /* Return one iff MSYM refers to standard ISA code. */
397
398 static int
399 msymbol_is_mips (struct minimal_symbol *msym)
400 {
401 return !(MSYMBOL_TARGET_FLAG_MIPS16 (msym)
402 | MSYMBOL_TARGET_FLAG_MICROMIPS (msym));
403 }
404
405 /* Return one iff MSYM refers to MIPS16 code. */
406
407 static int
408 msymbol_is_mips16 (struct minimal_symbol *msym)
409 {
410 return MSYMBOL_TARGET_FLAG_MIPS16 (msym);
411 }
412
413 /* Return one iff MSYM refers to microMIPS code. */
414
415 static int
416 msymbol_is_micromips (struct minimal_symbol *msym)
417 {
418 return MSYMBOL_TARGET_FLAG_MICROMIPS (msym);
419 }
420
421 /* Set the ISA bit in the main symbol too, complementing the corresponding
422 minimal symbol setting and reflecting the run-time value of the symbol.
423 The need for comes from the ISA bit having been cleared as code in
424 `_bfd_mips_elf_symbol_processing' separated it into the ELF symbol's
425 `st_other' STO_MIPS16 or STO_MICROMIPS annotation, making the values
426 of symbols referring to compressed code different in GDB to the values
427 used by actual code. That in turn makes them evaluate incorrectly in
428 expressions, producing results different to what the same expressions
429 yield when compiled into the program being debugged. */
430
431 static void
432 mips_make_symbol_special (struct symbol *sym, struct objfile *objfile)
433 {
434 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
435 {
436 /* We are in symbol reading so it is OK to cast away constness. */
437 struct block *block = (struct block *) SYMBOL_BLOCK_VALUE (sym);
438 CORE_ADDR compact_block_start;
439 struct bound_minimal_symbol msym;
440
441 compact_block_start = BLOCK_START (block) | 1;
442 msym = lookup_minimal_symbol_by_pc (compact_block_start);
443 if (msym.minsym && !msymbol_is_mips (msym.minsym))
444 {
445 BLOCK_START (block) = compact_block_start;
446 }
447 }
448 }
449
450 /* XFER a value from the big/little/left end of the register.
451 Depending on the size of the value it might occupy the entire
452 register or just part of it. Make an allowance for this, aligning
453 things accordingly. */
454
455 static void
456 mips_xfer_register (struct gdbarch *gdbarch, struct regcache *regcache,
457 int reg_num, int length,
458 enum bfd_endian endian, gdb_byte *in,
459 const gdb_byte *out, int buf_offset)
460 {
461 int reg_offset = 0;
462
463 gdb_assert (reg_num >= gdbarch_num_regs (gdbarch));
464 /* Need to transfer the left or right part of the register, based on
465 the targets byte order. */
466 switch (endian)
467 {
468 case BFD_ENDIAN_BIG:
469 reg_offset = register_size (gdbarch, reg_num) - length;
470 break;
471 case BFD_ENDIAN_LITTLE:
472 reg_offset = 0;
473 break;
474 case BFD_ENDIAN_UNKNOWN: /* Indicates no alignment. */
475 reg_offset = 0;
476 break;
477 default:
478 internal_error (__FILE__, __LINE__, _("bad switch"));
479 }
480 if (mips_debug)
481 fprintf_unfiltered (gdb_stderr,
482 "xfer $%d, reg offset %d, buf offset %d, length %d, ",
483 reg_num, reg_offset, buf_offset, length);
484 if (mips_debug && out != NULL)
485 {
486 int i;
487 fprintf_unfiltered (gdb_stdlog, "out ");
488 for (i = 0; i < length; i++)
489 fprintf_unfiltered (gdb_stdlog, "%02x", out[buf_offset + i]);
490 }
491 if (in != NULL)
492 regcache_cooked_read_part (regcache, reg_num, reg_offset, length,
493 in + buf_offset);
494 if (out != NULL)
495 regcache_cooked_write_part (regcache, reg_num, reg_offset, length,
496 out + buf_offset);
497 if (mips_debug && in != NULL)
498 {
499 int i;
500 fprintf_unfiltered (gdb_stdlog, "in ");
501 for (i = 0; i < length; i++)
502 fprintf_unfiltered (gdb_stdlog, "%02x", in[buf_offset + i]);
503 }
504 if (mips_debug)
505 fprintf_unfiltered (gdb_stdlog, "\n");
506 }
507
508 /* Determine if a MIPS3 or later cpu is operating in MIPS{1,2} FPU
509 compatiblity mode. A return value of 1 means that we have
510 physical 64-bit registers, but should treat them as 32-bit registers. */
511
512 static int
513 mips2_fp_compat (struct frame_info *frame)
514 {
515 struct gdbarch *gdbarch = get_frame_arch (frame);
516 /* MIPS1 and MIPS2 have only 32 bit FPRs, and the FR bit is not
517 meaningful. */
518 if (register_size (gdbarch, mips_regnum (gdbarch)->fp0) == 4)
519 return 0;
520
521 #if 0
522 /* FIXME drow 2002-03-10: This is disabled until we can do it consistently,
523 in all the places we deal with FP registers. PR gdb/413. */
524 /* Otherwise check the FR bit in the status register - it controls
525 the FP compatiblity mode. If it is clear we are in compatibility
526 mode. */
527 if ((get_frame_register_unsigned (frame, MIPS_PS_REGNUM) & ST0_FR) == 0)
528 return 1;
529 #endif
530
531 return 0;
532 }
533
534 #define VM_MIN_ADDRESS (CORE_ADDR)0x400000
535
536 static CORE_ADDR heuristic_proc_start (struct gdbarch *, CORE_ADDR);
537
538 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
539
540 /* The list of available "set mips " and "show mips " commands. */
541
542 static struct cmd_list_element *setmipscmdlist = NULL;
543 static struct cmd_list_element *showmipscmdlist = NULL;
544
545 /* Integer registers 0 thru 31 are handled explicitly by
546 mips_register_name(). Processor specific registers 32 and above
547 are listed in the following tables. */
548
549 enum
550 { NUM_MIPS_PROCESSOR_REGS = (90 - 32) };
551
552 /* Generic MIPS. */
553
554 static const char *mips_generic_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
555 "sr", "lo", "hi", "bad", "cause", "pc",
556 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
557 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
558 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
559 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
560 "fsr", "fir",
561 };
562
563 /* Names of tx39 registers. */
564
565 static const char *mips_tx39_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
566 "sr", "lo", "hi", "bad", "cause", "pc",
567 "", "", "", "", "", "", "", "",
568 "", "", "", "", "", "", "", "",
569 "", "", "", "", "", "", "", "",
570 "", "", "", "", "", "", "", "",
571 "", "", "", "",
572 "", "", "", "", "", "", "", "",
573 "", "", "config", "cache", "debug", "depc", "epc",
574 };
575
576 /* Names of registers with Linux kernels. */
577 static const char *mips_linux_reg_names[NUM_MIPS_PROCESSOR_REGS] = {
578 "sr", "lo", "hi", "bad", "cause", "pc",
579 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
580 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
581 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
582 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
583 "fsr", "fir"
584 };
585
586
587 /* Return the name of the register corresponding to REGNO. */
588 static const char *
589 mips_register_name (struct gdbarch *gdbarch, int regno)
590 {
591 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
592 /* GPR names for all ABIs other than n32/n64. */
593 static char *mips_gpr_names[] = {
594 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
595 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
596 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
597 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
598 };
599
600 /* GPR names for n32 and n64 ABIs. */
601 static char *mips_n32_n64_gpr_names[] = {
602 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
603 "a4", "a5", "a6", "a7", "t0", "t1", "t2", "t3",
604 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
605 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra"
606 };
607
608 enum mips_abi abi = mips_abi (gdbarch);
609
610 /* Map [gdbarch_num_regs .. 2*gdbarch_num_regs) onto the raw registers,
611 but then don't make the raw register names visible. This (upper)
612 range of user visible register numbers are the pseudo-registers.
613
614 This approach was adopted accommodate the following scenario:
615 It is possible to debug a 64-bit device using a 32-bit
616 programming model. In such instances, the raw registers are
617 configured to be 64-bits wide, while the pseudo registers are
618 configured to be 32-bits wide. The registers that the user
619 sees - the pseudo registers - match the users expectations
620 given the programming model being used. */
621 int rawnum = regno % gdbarch_num_regs (gdbarch);
622 if (regno < gdbarch_num_regs (gdbarch))
623 return "";
624
625 /* The MIPS integer registers are always mapped from 0 to 31. The
626 names of the registers (which reflects the conventions regarding
627 register use) vary depending on the ABI. */
628 if (0 <= rawnum && rawnum < 32)
629 {
630 if (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
631 return mips_n32_n64_gpr_names[rawnum];
632 else
633 return mips_gpr_names[rawnum];
634 }
635 else if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
636 return tdesc_register_name (gdbarch, rawnum);
637 else if (32 <= rawnum && rawnum < gdbarch_num_regs (gdbarch))
638 {
639 gdb_assert (rawnum - 32 < NUM_MIPS_PROCESSOR_REGS);
640 if (tdep->mips_processor_reg_names[rawnum - 32])
641 return tdep->mips_processor_reg_names[rawnum - 32];
642 return "";
643 }
644 else
645 internal_error (__FILE__, __LINE__,
646 _("mips_register_name: bad register number %d"), rawnum);
647 }
648
649 /* Return the groups that a MIPS register can be categorised into. */
650
651 static int
652 mips_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
653 struct reggroup *reggroup)
654 {
655 int vector_p;
656 int float_p;
657 int raw_p;
658 int rawnum = regnum % gdbarch_num_regs (gdbarch);
659 int pseudo = regnum / gdbarch_num_regs (gdbarch);
660 if (reggroup == all_reggroup)
661 return pseudo;
662 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
663 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
664 /* FIXME: cagney/2003-04-13: Can't yet use gdbarch_num_regs
665 (gdbarch), as not all architectures are multi-arch. */
666 raw_p = rawnum < gdbarch_num_regs (gdbarch);
667 if (gdbarch_register_name (gdbarch, regnum) == NULL
668 || gdbarch_register_name (gdbarch, regnum)[0] == '\0')
669 return 0;
670 if (reggroup == float_reggroup)
671 return float_p && pseudo;
672 if (reggroup == vector_reggroup)
673 return vector_p && pseudo;
674 if (reggroup == general_reggroup)
675 return (!vector_p && !float_p) && pseudo;
676 /* Save the pseudo registers. Need to make certain that any code
677 extracting register values from a saved register cache also uses
678 pseudo registers. */
679 if (reggroup == save_reggroup)
680 return raw_p && pseudo;
681 /* Restore the same pseudo register. */
682 if (reggroup == restore_reggroup)
683 return raw_p && pseudo;
684 return 0;
685 }
686
687 /* Return the groups that a MIPS register can be categorised into.
688 This version is only used if we have a target description which
689 describes real registers (and their groups). */
690
691 static int
692 mips_tdesc_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
693 struct reggroup *reggroup)
694 {
695 int rawnum = regnum % gdbarch_num_regs (gdbarch);
696 int pseudo = regnum / gdbarch_num_regs (gdbarch);
697 int ret;
698
699 /* Only save, restore, and display the pseudo registers. Need to
700 make certain that any code extracting register values from a
701 saved register cache also uses pseudo registers.
702
703 Note: saving and restoring the pseudo registers is slightly
704 strange; if we have 64 bits, we should save and restore all
705 64 bits. But this is hard and has little benefit. */
706 if (!pseudo)
707 return 0;
708
709 ret = tdesc_register_in_reggroup_p (gdbarch, rawnum, reggroup);
710 if (ret != -1)
711 return ret;
712
713 return mips_register_reggroup_p (gdbarch, regnum, reggroup);
714 }
715
716 /* Map the symbol table registers which live in the range [1 *
717 gdbarch_num_regs .. 2 * gdbarch_num_regs) back onto the corresponding raw
718 registers. Take care of alignment and size problems. */
719
720 static enum register_status
721 mips_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
722 int cookednum, gdb_byte *buf)
723 {
724 int rawnum = cookednum % gdbarch_num_regs (gdbarch);
725 gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
726 && cookednum < 2 * gdbarch_num_regs (gdbarch));
727 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
728 return regcache_raw_read (regcache, rawnum, buf);
729 else if (register_size (gdbarch, rawnum) >
730 register_size (gdbarch, cookednum))
731 {
732 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
733 return regcache_raw_read_part (regcache, rawnum, 0, 4, buf);
734 else
735 {
736 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
737 LONGEST regval;
738 enum register_status status;
739
740 status = regcache_raw_read_signed (regcache, rawnum, &regval);
741 if (status == REG_VALID)
742 store_signed_integer (buf, 4, byte_order, regval);
743 return status;
744 }
745 }
746 else
747 internal_error (__FILE__, __LINE__, _("bad register size"));
748 }
749
750 static void
751 mips_pseudo_register_write (struct gdbarch *gdbarch,
752 struct regcache *regcache, int cookednum,
753 const gdb_byte *buf)
754 {
755 int rawnum = cookednum % gdbarch_num_regs (gdbarch);
756 gdb_assert (cookednum >= gdbarch_num_regs (gdbarch)
757 && cookednum < 2 * gdbarch_num_regs (gdbarch));
758 if (register_size (gdbarch, rawnum) == register_size (gdbarch, cookednum))
759 regcache_raw_write (regcache, rawnum, buf);
760 else if (register_size (gdbarch, rawnum) >
761 register_size (gdbarch, cookednum))
762 {
763 if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
764 regcache_raw_write_part (regcache, rawnum, 0, 4, buf);
765 else
766 {
767 /* Sign extend the shortened version of the register prior
768 to placing it in the raw register. This is required for
769 some mips64 parts in order to avoid unpredictable behavior. */
770 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
771 LONGEST regval = extract_signed_integer (buf, 4, byte_order);
772 regcache_raw_write_signed (regcache, rawnum, regval);
773 }
774 }
775 else
776 internal_error (__FILE__, __LINE__, _("bad register size"));
777 }
778
779 static int
780 mips_ax_pseudo_register_collect (struct gdbarch *gdbarch,
781 struct agent_expr *ax, int reg)
782 {
783 int rawnum = reg % gdbarch_num_regs (gdbarch);
784 gdb_assert (reg >= gdbarch_num_regs (gdbarch)
785 && reg < 2 * gdbarch_num_regs (gdbarch));
786
787 ax_reg_mask (ax, rawnum);
788
789 return 0;
790 }
791
792 static int
793 mips_ax_pseudo_register_push_stack (struct gdbarch *gdbarch,
794 struct agent_expr *ax, int reg)
795 {
796 int rawnum = reg % gdbarch_num_regs (gdbarch);
797 gdb_assert (reg >= gdbarch_num_regs (gdbarch)
798 && reg < 2 * gdbarch_num_regs (gdbarch));
799 if (register_size (gdbarch, rawnum) >= register_size (gdbarch, reg))
800 {
801 ax_reg (ax, rawnum);
802
803 if (register_size (gdbarch, rawnum) > register_size (gdbarch, reg))
804 {
805 if (!gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p
806 || gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
807 {
808 ax_const_l (ax, 32);
809 ax_simple (ax, aop_lsh);
810 }
811 ax_const_l (ax, 32);
812 ax_simple (ax, aop_rsh_signed);
813 }
814 }
815 else
816 internal_error (__FILE__, __LINE__, _("bad register size"));
817
818 return 0;
819 }
820
821 /* Table to translate 3-bit register field to actual register number. */
822 static const signed char mips_reg3_to_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
823
824 /* Heuristic_proc_start may hunt through the text section for a long
825 time across a 2400 baud serial line. Allows the user to limit this
826 search. */
827
828 static int heuristic_fence_post = 0;
829
830 /* Number of bytes of storage in the actual machine representation for
831 register N. NOTE: This defines the pseudo register type so need to
832 rebuild the architecture vector. */
833
834 static int mips64_transfers_32bit_regs_p = 0;
835
836 static void
837 set_mips64_transfers_32bit_regs (char *args, int from_tty,
838 struct cmd_list_element *c)
839 {
840 struct gdbarch_info info;
841 gdbarch_info_init (&info);
842 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
843 instead of relying on globals. Doing that would let generic code
844 handle the search for this specific architecture. */
845 if (!gdbarch_update_p (info))
846 {
847 mips64_transfers_32bit_regs_p = 0;
848 error (_("32-bit compatibility mode not supported"));
849 }
850 }
851
852 /* Convert to/from a register and the corresponding memory value. */
853
854 /* This predicate tests for the case of an 8 byte floating point
855 value that is being transferred to or from a pair of floating point
856 registers each of which are (or are considered to be) only 4 bytes
857 wide. */
858 static int
859 mips_convert_register_float_case_p (struct gdbarch *gdbarch, int regnum,
860 struct type *type)
861 {
862 return (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
863 && register_size (gdbarch, regnum) == 4
864 && mips_float_register_p (gdbarch, regnum)
865 && TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8);
866 }
867
868 /* This predicate tests for the case of a value of less than 8
869 bytes in width that is being transfered to or from an 8 byte
870 general purpose register. */
871 static int
872 mips_convert_register_gpreg_case_p (struct gdbarch *gdbarch, int regnum,
873 struct type *type)
874 {
875 int num_regs = gdbarch_num_regs (gdbarch);
876
877 return (register_size (gdbarch, regnum) == 8
878 && regnum % num_regs > 0 && regnum % num_regs < 32
879 && TYPE_LENGTH (type) < 8);
880 }
881
882 static int
883 mips_convert_register_p (struct gdbarch *gdbarch,
884 int regnum, struct type *type)
885 {
886 return (mips_convert_register_float_case_p (gdbarch, regnum, type)
887 || mips_convert_register_gpreg_case_p (gdbarch, regnum, type));
888 }
889
890 static int
891 mips_register_to_value (struct frame_info *frame, int regnum,
892 struct type *type, gdb_byte *to,
893 int *optimizedp, int *unavailablep)
894 {
895 struct gdbarch *gdbarch = get_frame_arch (frame);
896
897 if (mips_convert_register_float_case_p (gdbarch, regnum, type))
898 {
899 get_frame_register (frame, regnum + 0, to + 4);
900 get_frame_register (frame, regnum + 1, to + 0);
901
902 if (!get_frame_register_bytes (frame, regnum + 0, 0, 4, to + 4,
903 optimizedp, unavailablep))
904 return 0;
905
906 if (!get_frame_register_bytes (frame, regnum + 1, 0, 4, to + 0,
907 optimizedp, unavailablep))
908 return 0;
909 *optimizedp = *unavailablep = 0;
910 return 1;
911 }
912 else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
913 {
914 int len = TYPE_LENGTH (type);
915 CORE_ADDR offset;
916
917 offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 - len : 0;
918 if (!get_frame_register_bytes (frame, regnum, offset, len, to,
919 optimizedp, unavailablep))
920 return 0;
921
922 *optimizedp = *unavailablep = 0;
923 return 1;
924 }
925 else
926 {
927 internal_error (__FILE__, __LINE__,
928 _("mips_register_to_value: unrecognized case"));
929 }
930 }
931
932 static void
933 mips_value_to_register (struct frame_info *frame, int regnum,
934 struct type *type, const gdb_byte *from)
935 {
936 struct gdbarch *gdbarch = get_frame_arch (frame);
937
938 if (mips_convert_register_float_case_p (gdbarch, regnum, type))
939 {
940 put_frame_register (frame, regnum + 0, from + 4);
941 put_frame_register (frame, regnum + 1, from + 0);
942 }
943 else if (mips_convert_register_gpreg_case_p (gdbarch, regnum, type))
944 {
945 gdb_byte fill[8];
946 int len = TYPE_LENGTH (type);
947
948 /* Sign extend values, irrespective of type, that are stored to
949 a 64-bit general purpose register. (32-bit unsigned values
950 are stored as signed quantities within a 64-bit register.
951 When performing an operation, in compiled code, that combines
952 a 32-bit unsigned value with a signed 64-bit value, a type
953 conversion is first performed that zeroes out the high 32 bits.) */
954 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
955 {
956 if (from[0] & 0x80)
957 store_signed_integer (fill, 8, BFD_ENDIAN_BIG, -1);
958 else
959 store_signed_integer (fill, 8, BFD_ENDIAN_BIG, 0);
960 put_frame_register_bytes (frame, regnum, 0, 8 - len, fill);
961 put_frame_register_bytes (frame, regnum, 8 - len, len, from);
962 }
963 else
964 {
965 if (from[len-1] & 0x80)
966 store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, -1);
967 else
968 store_signed_integer (fill, 8, BFD_ENDIAN_LITTLE, 0);
969 put_frame_register_bytes (frame, regnum, 0, len, from);
970 put_frame_register_bytes (frame, regnum, len, 8 - len, fill);
971 }
972 }
973 else
974 {
975 internal_error (__FILE__, __LINE__,
976 _("mips_value_to_register: unrecognized case"));
977 }
978 }
979
980 /* Return the GDB type object for the "standard" data type of data in
981 register REG. */
982
983 static struct type *
984 mips_register_type (struct gdbarch *gdbarch, int regnum)
985 {
986 gdb_assert (regnum >= 0 && regnum < 2 * gdbarch_num_regs (gdbarch));
987 if (mips_float_register_p (gdbarch, regnum))
988 {
989 /* The floating-point registers raw, or cooked, always match
990 mips_isa_regsize(), and also map 1:1, byte for byte. */
991 if (mips_isa_regsize (gdbarch) == 4)
992 return builtin_type (gdbarch)->builtin_float;
993 else
994 return builtin_type (gdbarch)->builtin_double;
995 }
996 else if (regnum < gdbarch_num_regs (gdbarch))
997 {
998 /* The raw or ISA registers. These are all sized according to
999 the ISA regsize. */
1000 if (mips_isa_regsize (gdbarch) == 4)
1001 return builtin_type (gdbarch)->builtin_int32;
1002 else
1003 return builtin_type (gdbarch)->builtin_int64;
1004 }
1005 else
1006 {
1007 int rawnum = regnum - gdbarch_num_regs (gdbarch);
1008
1009 /* The cooked or ABI registers. These are sized according to
1010 the ABI (with a few complications). */
1011 if (rawnum == mips_regnum (gdbarch)->fp_control_status
1012 || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
1013 return builtin_type (gdbarch)->builtin_int32;
1014 else if (gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
1015 && rawnum >= MIPS_FIRST_EMBED_REGNUM
1016 && rawnum <= MIPS_LAST_EMBED_REGNUM)
1017 /* The pseudo/cooked view of the embedded registers is always
1018 32-bit. The raw view is handled below. */
1019 return builtin_type (gdbarch)->builtin_int32;
1020 else if (gdbarch_tdep (gdbarch)->mips64_transfers_32bit_regs_p)
1021 /* The target, while possibly using a 64-bit register buffer,
1022 is only transfering 32-bits of each integer register.
1023 Reflect this in the cooked/pseudo (ABI) register value. */
1024 return builtin_type (gdbarch)->builtin_int32;
1025 else if (mips_abi_regsize (gdbarch) == 4)
1026 /* The ABI is restricted to 32-bit registers (the ISA could be
1027 32- or 64-bit). */
1028 return builtin_type (gdbarch)->builtin_int32;
1029 else
1030 /* 64-bit ABI. */
1031 return builtin_type (gdbarch)->builtin_int64;
1032 }
1033 }
1034
1035 /* Return the GDB type for the pseudo register REGNUM, which is the
1036 ABI-level view. This function is only called if there is a target
1037 description which includes registers, so we know precisely the
1038 types of hardware registers. */
1039
1040 static struct type *
1041 mips_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
1042 {
1043 const int num_regs = gdbarch_num_regs (gdbarch);
1044 int rawnum = regnum % num_regs;
1045 struct type *rawtype;
1046
1047 gdb_assert (regnum >= num_regs && regnum < 2 * num_regs);
1048
1049 /* Absent registers are still absent. */
1050 rawtype = gdbarch_register_type (gdbarch, rawnum);
1051 if (TYPE_LENGTH (rawtype) == 0)
1052 return rawtype;
1053
1054 /* Present the floating point registers however the hardware did;
1055 do not try to convert between FPU layouts. */
1056 if (mips_float_register_p (gdbarch, rawnum))
1057 return rawtype;
1058
1059 /* Floating-point control registers are always 32-bit even though for
1060 backwards compatibility reasons 64-bit targets will transfer them
1061 as 64-bit quantities even if using XML descriptions. */
1062 if (rawnum == mips_regnum (gdbarch)->fp_control_status
1063 || rawnum == mips_regnum (gdbarch)->fp_implementation_revision)
1064 return builtin_type (gdbarch)->builtin_int32;
1065
1066 /* Use pointer types for registers if we can. For n32 we can not,
1067 since we do not have a 64-bit pointer type. */
1068 if (mips_abi_regsize (gdbarch)
1069 == TYPE_LENGTH (builtin_type (gdbarch)->builtin_data_ptr))
1070 {
1071 if (rawnum == MIPS_SP_REGNUM
1072 || rawnum == mips_regnum (gdbarch)->badvaddr)
1073 return builtin_type (gdbarch)->builtin_data_ptr;
1074 else if (rawnum == mips_regnum (gdbarch)->pc)
1075 return builtin_type (gdbarch)->builtin_func_ptr;
1076 }
1077
1078 if (mips_abi_regsize (gdbarch) == 4 && TYPE_LENGTH (rawtype) == 8
1079 && ((rawnum >= MIPS_ZERO_REGNUM && rawnum <= MIPS_PS_REGNUM)
1080 || rawnum == mips_regnum (gdbarch)->lo
1081 || rawnum == mips_regnum (gdbarch)->hi
1082 || rawnum == mips_regnum (gdbarch)->badvaddr
1083 || rawnum == mips_regnum (gdbarch)->cause
1084 || rawnum == mips_regnum (gdbarch)->pc
1085 || (mips_regnum (gdbarch)->dspacc != -1
1086 && rawnum >= mips_regnum (gdbarch)->dspacc
1087 && rawnum < mips_regnum (gdbarch)->dspacc + 6)))
1088 return builtin_type (gdbarch)->builtin_int32;
1089
1090 /* The pseudo/cooked view of embedded registers is always
1091 32-bit, even if the target transfers 64-bit values for them.
1092 New targets relying on XML descriptions should only transfer
1093 the necessary 32 bits, but older versions of GDB expected 64,
1094 so allow the target to provide 64 bits without interfering
1095 with the displayed type. */
1096 if (gdbarch_osabi (gdbarch) != GDB_OSABI_LINUX
1097 && rawnum >= MIPS_FIRST_EMBED_REGNUM
1098 && rawnum <= MIPS_LAST_EMBED_REGNUM)
1099 return builtin_type (gdbarch)->builtin_int32;
1100
1101 /* For all other registers, pass through the hardware type. */
1102 return rawtype;
1103 }
1104
1105 /* Should the upper word of 64-bit addresses be zeroed? */
1106 enum auto_boolean mask_address_var = AUTO_BOOLEAN_AUTO;
1107
1108 static int
1109 mips_mask_address_p (struct gdbarch_tdep *tdep)
1110 {
1111 switch (mask_address_var)
1112 {
1113 case AUTO_BOOLEAN_TRUE:
1114 return 1;
1115 case AUTO_BOOLEAN_FALSE:
1116 return 0;
1117 break;
1118 case AUTO_BOOLEAN_AUTO:
1119 return tdep->default_mask_address_p;
1120 default:
1121 internal_error (__FILE__, __LINE__,
1122 _("mips_mask_address_p: bad switch"));
1123 return -1;
1124 }
1125 }
1126
1127 static void
1128 show_mask_address (struct ui_file *file, int from_tty,
1129 struct cmd_list_element *c, const char *value)
1130 {
1131 struct gdbarch_tdep *tdep = gdbarch_tdep (target_gdbarch ());
1132
1133 deprecated_show_value_hack (file, from_tty, c, value);
1134 switch (mask_address_var)
1135 {
1136 case AUTO_BOOLEAN_TRUE:
1137 printf_filtered ("The 32 bit mips address mask is enabled\n");
1138 break;
1139 case AUTO_BOOLEAN_FALSE:
1140 printf_filtered ("The 32 bit mips address mask is disabled\n");
1141 break;
1142 case AUTO_BOOLEAN_AUTO:
1143 printf_filtered
1144 ("The 32 bit address mask is set automatically. Currently %s\n",
1145 mips_mask_address_p (tdep) ? "enabled" : "disabled");
1146 break;
1147 default:
1148 internal_error (__FILE__, __LINE__, _("show_mask_address: bad switch"));
1149 break;
1150 }
1151 }
1152
1153 /* Tell if the program counter value in MEMADDR is in a standard ISA
1154 function. */
1155
1156 int
1157 mips_pc_is_mips (CORE_ADDR memaddr)
1158 {
1159 struct bound_minimal_symbol sym;
1160
1161 /* Flags indicating that this is a MIPS16 or microMIPS function is
1162 stored by elfread.c in the high bit of the info field. Use this
1163 to decide if the function is standard MIPS. Otherwise if bit 0
1164 of the address is clear, then this is a standard MIPS function. */
1165 sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
1166 if (sym.minsym)
1167 return msymbol_is_mips (sym.minsym);
1168 else
1169 return is_mips_addr (memaddr);
1170 }
1171
1172 /* Tell if the program counter value in MEMADDR is in a MIPS16 function. */
1173
1174 int
1175 mips_pc_is_mips16 (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1176 {
1177 struct bound_minimal_symbol sym;
1178
1179 /* A flag indicating that this is a MIPS16 function is stored by
1180 elfread.c in the high bit of the info field. Use this to decide
1181 if the function is MIPS16. Otherwise if bit 0 of the address is
1182 set, then ELF file flags will tell if this is a MIPS16 function. */
1183 sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
1184 if (sym.minsym)
1185 return msymbol_is_mips16 (sym.minsym);
1186 else
1187 return is_mips16_addr (gdbarch, memaddr);
1188 }
1189
1190 /* Tell if the program counter value in MEMADDR is in a microMIPS function. */
1191
1192 int
1193 mips_pc_is_micromips (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1194 {
1195 struct bound_minimal_symbol sym;
1196
1197 /* A flag indicating that this is a microMIPS function is stored by
1198 elfread.c in the high bit of the info field. Use this to decide
1199 if the function is microMIPS. Otherwise if bit 0 of the address
1200 is set, then ELF file flags will tell if this is a microMIPS
1201 function. */
1202 sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
1203 if (sym.minsym)
1204 return msymbol_is_micromips (sym.minsym);
1205 else
1206 return is_micromips_addr (gdbarch, memaddr);
1207 }
1208
1209 /* Tell the ISA type of the function the program counter value in MEMADDR
1210 is in. */
1211
1212 static enum mips_isa
1213 mips_pc_isa (struct gdbarch *gdbarch, CORE_ADDR memaddr)
1214 {
1215 struct bound_minimal_symbol sym;
1216
1217 /* A flag indicating that this is a MIPS16 or a microMIPS function
1218 is stored by elfread.c in the high bit of the info field. Use
1219 this to decide if the function is MIPS16 or microMIPS or normal
1220 MIPS. Otherwise if bit 0 of the address is set, then ELF file
1221 flags will tell if this is a MIPS16 or a microMIPS function. */
1222 sym = lookup_minimal_symbol_by_pc (make_compact_addr (memaddr));
1223 if (sym.minsym)
1224 {
1225 if (msymbol_is_micromips (sym.minsym))
1226 return ISA_MICROMIPS;
1227 else if (msymbol_is_mips16 (sym.minsym))
1228 return ISA_MIPS16;
1229 else
1230 return ISA_MIPS;
1231 }
1232 else
1233 {
1234 if (is_mips_addr (memaddr))
1235 return ISA_MIPS;
1236 else if (is_micromips_addr (gdbarch, memaddr))
1237 return ISA_MICROMIPS;
1238 else
1239 return ISA_MIPS16;
1240 }
1241 }
1242
1243 /* Set the ISA bit correctly in the PC, used by DWARF-2 machinery.
1244 The need for comes from the ISA bit having been cleared, making
1245 addresses in FDE, range records, etc. referring to compressed code
1246 different to those in line information, the symbol table and finally
1247 the PC register. That in turn confuses many operations. */
1248
1249 static CORE_ADDR
1250 mips_adjust_dwarf2_addr (CORE_ADDR pc)
1251 {
1252 pc = unmake_compact_addr (pc);
1253 return mips_pc_is_mips (pc) ? pc : make_compact_addr (pc);
1254 }
1255
1256 /* Recalculate the line record requested so that the resulting PC has
1257 the ISA bit set correctly, used by DWARF-2 machinery. The need for
1258 this adjustment comes from some records associated with compressed
1259 code having the ISA bit cleared, most notably at function prologue
1260 ends. The ISA bit is in this context retrieved from the minimal
1261 symbol covering the address requested, which in turn has been
1262 constructed from the binary's symbol table rather than DWARF-2
1263 information. The correct setting of the ISA bit is required for
1264 breakpoint addresses to correctly match against the stop PC.
1265
1266 As line entries can specify relative address adjustments we need to
1267 keep track of the absolute value of the last line address recorded
1268 in line information, so that we can calculate the actual address to
1269 apply the ISA bit adjustment to. We use PC for this tracking and
1270 keep the original address there.
1271
1272 As such relative address adjustments can be odd within compressed
1273 code we need to keep track of the last line address with the ISA
1274 bit adjustment applied too, as the original address may or may not
1275 have had the ISA bit set. We use ADJ_PC for this tracking and keep
1276 the adjusted address there.
1277
1278 For relative address adjustments we then use these variables to
1279 calculate the address intended by line information, which will be
1280 PC-relative, and return an updated adjustment carrying ISA bit
1281 information, which will be ADJ_PC-relative. For absolute address
1282 adjustments we just return the same address that we store in ADJ_PC
1283 too.
1284
1285 As the first line entry can be relative to an implied address value
1286 of 0 we need to have the initial address set up that we store in PC
1287 and ADJ_PC. This is arranged with a call from `dwarf_decode_lines_1'
1288 that sets PC to 0 and ADJ_PC accordingly, usually 0 as well. */
1289
1290 static CORE_ADDR
1291 mips_adjust_dwarf2_line (CORE_ADDR addr, int rel)
1292 {
1293 static CORE_ADDR adj_pc;
1294 static CORE_ADDR pc;
1295 CORE_ADDR isa_pc;
1296
1297 pc = rel ? pc + addr : addr;
1298 isa_pc = mips_adjust_dwarf2_addr (pc);
1299 addr = rel ? isa_pc - adj_pc : isa_pc;
1300 adj_pc = isa_pc;
1301 return addr;
1302 }
1303
1304 /* Various MIPS16 thunk (aka stub or trampoline) names. */
1305
1306 static const char mips_str_mips16_call_stub[] = "__mips16_call_stub_";
1307 static const char mips_str_mips16_ret_stub[] = "__mips16_ret_";
1308 static const char mips_str_call_fp_stub[] = "__call_stub_fp_";
1309 static const char mips_str_call_stub[] = "__call_stub_";
1310 static const char mips_str_fn_stub[] = "__fn_stub_";
1311
1312 /* This is used as a PIC thunk prefix. */
1313
1314 static const char mips_str_pic[] = ".pic.";
1315
1316 /* Return non-zero if the PC is inside a call thunk (aka stub or
1317 trampoline) that should be treated as a temporary frame. */
1318
1319 static int
1320 mips_in_frame_stub (CORE_ADDR pc)
1321 {
1322 CORE_ADDR start_addr;
1323 const char *name;
1324
1325 /* Find the starting address of the function containing the PC. */
1326 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
1327 return 0;
1328
1329 /* If the PC is in __mips16_call_stub_*, this is a call/return stub. */
1330 if (startswith (name, mips_str_mips16_call_stub))
1331 return 1;
1332 /* If the PC is in __call_stub_*, this is a call/return or a call stub. */
1333 if (startswith (name, mips_str_call_stub))
1334 return 1;
1335 /* If the PC is in __fn_stub_*, this is a call stub. */
1336 if (startswith (name, mips_str_fn_stub))
1337 return 1;
1338
1339 return 0; /* Not a stub. */
1340 }
1341
1342 /* MIPS believes that the PC has a sign extended value. Perhaps the
1343 all registers should be sign extended for simplicity? */
1344
1345 static CORE_ADDR
1346 mips_read_pc (struct regcache *regcache)
1347 {
1348 int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
1349 LONGEST pc;
1350
1351 regcache_cooked_read_signed (regcache, regnum, &pc);
1352 return pc;
1353 }
1354
1355 static CORE_ADDR
1356 mips_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1357 {
1358 CORE_ADDR pc;
1359
1360 pc = frame_unwind_register_signed (next_frame, gdbarch_pc_regnum (gdbarch));
1361 /* macro/2012-04-20: This hack skips over MIPS16 call thunks as
1362 intermediate frames. In this case we can get the caller's address
1363 from $ra, or if $ra contains an address within a thunk as well, then
1364 it must be in the return path of __mips16_call_stub_{s,d}{f,c}_{0..10}
1365 and thus the caller's address is in $s2. */
1366 if (frame_relative_level (next_frame) >= 0 && mips_in_frame_stub (pc))
1367 {
1368 pc = frame_unwind_register_signed
1369 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
1370 if (mips_in_frame_stub (pc))
1371 pc = frame_unwind_register_signed
1372 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
1373 }
1374 return pc;
1375 }
1376
1377 static CORE_ADDR
1378 mips_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1379 {
1380 return frame_unwind_register_signed
1381 (next_frame, gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM);
1382 }
1383
1384 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
1385 dummy frame. The frame ID's base needs to match the TOS value
1386 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1387 breakpoint. */
1388
1389 static struct frame_id
1390 mips_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1391 {
1392 return frame_id_build
1393 (get_frame_register_signed (this_frame,
1394 gdbarch_num_regs (gdbarch)
1395 + MIPS_SP_REGNUM),
1396 get_frame_pc (this_frame));
1397 }
1398
1399 /* Implement the "write_pc" gdbarch method. */
1400
1401 void
1402 mips_write_pc (struct regcache *regcache, CORE_ADDR pc)
1403 {
1404 int regnum = gdbarch_pc_regnum (get_regcache_arch (regcache));
1405
1406 regcache_cooked_write_unsigned (regcache, regnum, pc);
1407 }
1408
1409 /* Fetch and return instruction from the specified location. Handle
1410 MIPS16/microMIPS as appropriate. */
1411
1412 static ULONGEST
1413 mips_fetch_instruction (struct gdbarch *gdbarch,
1414 enum mips_isa isa, CORE_ADDR addr, int *errp)
1415 {
1416 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1417 gdb_byte buf[MIPS_INSN32_SIZE];
1418 int instlen;
1419 int err;
1420
1421 switch (isa)
1422 {
1423 case ISA_MICROMIPS:
1424 case ISA_MIPS16:
1425 instlen = MIPS_INSN16_SIZE;
1426 addr = unmake_compact_addr (addr);
1427 break;
1428 case ISA_MIPS:
1429 instlen = MIPS_INSN32_SIZE;
1430 break;
1431 default:
1432 internal_error (__FILE__, __LINE__, _("invalid ISA"));
1433 break;
1434 }
1435 err = target_read_memory (addr, buf, instlen);
1436 if (errp != NULL)
1437 *errp = err;
1438 if (err != 0)
1439 {
1440 if (errp == NULL)
1441 memory_error (TARGET_XFER_E_IO, addr);
1442 return 0;
1443 }
1444 return extract_unsigned_integer (buf, instlen, byte_order);
1445 }
1446
1447 /* These are the fields of 32 bit mips instructions. */
1448 #define mips32_op(x) (x >> 26)
1449 #define itype_op(x) (x >> 26)
1450 #define itype_rs(x) ((x >> 21) & 0x1f)
1451 #define itype_rt(x) ((x >> 16) & 0x1f)
1452 #define itype_immediate(x) (x & 0xffff)
1453
1454 #define jtype_op(x) (x >> 26)
1455 #define jtype_target(x) (x & 0x03ffffff)
1456
1457 #define rtype_op(x) (x >> 26)
1458 #define rtype_rs(x) ((x >> 21) & 0x1f)
1459 #define rtype_rt(x) ((x >> 16) & 0x1f)
1460 #define rtype_rd(x) ((x >> 11) & 0x1f)
1461 #define rtype_shamt(x) ((x >> 6) & 0x1f)
1462 #define rtype_funct(x) (x & 0x3f)
1463
1464 /* MicroMIPS instruction fields. */
1465 #define micromips_op(x) ((x) >> 10)
1466
1467 /* 16-bit/32-bit-high-part instruction formats, B and S refer to the lowest
1468 bit and the size respectively of the field extracted. */
1469 #define b0s4_imm(x) ((x) & 0xf)
1470 #define b0s5_imm(x) ((x) & 0x1f)
1471 #define b0s5_reg(x) ((x) & 0x1f)
1472 #define b0s7_imm(x) ((x) & 0x7f)
1473 #define b0s10_imm(x) ((x) & 0x3ff)
1474 #define b1s4_imm(x) (((x) >> 1) & 0xf)
1475 #define b1s9_imm(x) (((x) >> 1) & 0x1ff)
1476 #define b2s3_cc(x) (((x) >> 2) & 0x7)
1477 #define b4s2_regl(x) (((x) >> 4) & 0x3)
1478 #define b5s5_op(x) (((x) >> 5) & 0x1f)
1479 #define b5s5_reg(x) (((x) >> 5) & 0x1f)
1480 #define b6s4_op(x) (((x) >> 6) & 0xf)
1481 #define b7s3_reg(x) (((x) >> 7) & 0x7)
1482
1483 /* 32-bit instruction formats, B and S refer to the lowest bit and the size
1484 respectively of the field extracted. */
1485 #define b0s6_op(x) ((x) & 0x3f)
1486 #define b0s11_op(x) ((x) & 0x7ff)
1487 #define b0s12_imm(x) ((x) & 0xfff)
1488 #define b0s16_imm(x) ((x) & 0xffff)
1489 #define b0s26_imm(x) ((x) & 0x3ffffff)
1490 #define b6s10_ext(x) (((x) >> 6) & 0x3ff)
1491 #define b11s5_reg(x) (((x) >> 11) & 0x1f)
1492 #define b12s4_op(x) (((x) >> 12) & 0xf)
1493
1494 /* Return the size in bytes of the instruction INSN encoded in the ISA
1495 instruction set. */
1496
1497 static int
1498 mips_insn_size (enum mips_isa isa, ULONGEST insn)
1499 {
1500 switch (isa)
1501 {
1502 case ISA_MICROMIPS:
1503 if ((micromips_op (insn) & 0x4) == 0x4
1504 || (micromips_op (insn) & 0x7) == 0x0)
1505 return 2 * MIPS_INSN16_SIZE;
1506 else
1507 return MIPS_INSN16_SIZE;
1508 case ISA_MIPS16:
1509 if ((insn & 0xf800) == 0xf000)
1510 return 2 * MIPS_INSN16_SIZE;
1511 else
1512 return MIPS_INSN16_SIZE;
1513 case ISA_MIPS:
1514 return MIPS_INSN32_SIZE;
1515 }
1516 internal_error (__FILE__, __LINE__, _("invalid ISA"));
1517 }
1518
1519 static LONGEST
1520 mips32_relative_offset (ULONGEST inst)
1521 {
1522 return ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 2;
1523 }
1524
1525 /* Determine the address of the next instruction executed after the INST
1526 floating condition branch instruction at PC. COUNT specifies the
1527 number of the floating condition bits tested by the branch. */
1528
1529 static CORE_ADDR
1530 mips32_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
1531 ULONGEST inst, CORE_ADDR pc, int count)
1532 {
1533 int fcsr = mips_regnum (gdbarch)->fp_control_status;
1534 int cnum = (itype_rt (inst) >> 2) & (count - 1);
1535 int tf = itype_rt (inst) & 1;
1536 int mask = (1 << count) - 1;
1537 ULONGEST fcs;
1538 int cond;
1539
1540 if (fcsr == -1)
1541 /* No way to handle; it'll most likely trap anyway. */
1542 return pc;
1543
1544 fcs = get_frame_register_unsigned (frame, fcsr);
1545 cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
1546
1547 if (((cond >> cnum) & mask) != mask * !tf)
1548 pc += mips32_relative_offset (inst);
1549 else
1550 pc += 4;
1551
1552 return pc;
1553 }
1554
1555 /* Return nonzero if the gdbarch is an Octeon series. */
1556
1557 static int
1558 is_octeon (struct gdbarch *gdbarch)
1559 {
1560 const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
1561
1562 return (info->mach == bfd_mach_mips_octeon
1563 || info->mach == bfd_mach_mips_octeonp
1564 || info->mach == bfd_mach_mips_octeon2);
1565 }
1566
1567 /* Return true if the OP represents the Octeon's BBIT instruction. */
1568
1569 static int
1570 is_octeon_bbit_op (int op, struct gdbarch *gdbarch)
1571 {
1572 if (!is_octeon (gdbarch))
1573 return 0;
1574 /* BBIT0 is encoded as LWC2: 110 010. */
1575 /* BBIT032 is encoded as LDC2: 110 110. */
1576 /* BBIT1 is encoded as SWC2: 111 010. */
1577 /* BBIT132 is encoded as SDC2: 111 110. */
1578 if (op == 50 || op == 54 || op == 58 || op == 62)
1579 return 1;
1580 return 0;
1581 }
1582
1583
1584 /* Determine where to set a single step breakpoint while considering
1585 branch prediction. */
1586
1587 static CORE_ADDR
1588 mips32_next_pc (struct frame_info *frame, CORE_ADDR pc)
1589 {
1590 struct gdbarch *gdbarch = get_frame_arch (frame);
1591 unsigned long inst;
1592 int op;
1593 inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
1594 op = itype_op (inst);
1595 if ((inst & 0xe0000000) != 0) /* Not a special, jump or branch
1596 instruction. */
1597 {
1598 if (op >> 2 == 5)
1599 /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
1600 {
1601 switch (op & 0x03)
1602 {
1603 case 0: /* BEQL */
1604 goto equal_branch;
1605 case 1: /* BNEL */
1606 goto neq_branch;
1607 case 2: /* BLEZL */
1608 goto less_branch;
1609 case 3: /* BGTZL */
1610 goto greater_branch;
1611 default:
1612 pc += 4;
1613 }
1614 }
1615 else if (op == 17 && itype_rs (inst) == 8)
1616 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
1617 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 1);
1618 else if (op == 17 && itype_rs (inst) == 9
1619 && (itype_rt (inst) & 2) == 0)
1620 /* BC1ANY2F, BC1ANY2T: 010001 01001 xxx0x */
1621 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 2);
1622 else if (op == 17 && itype_rs (inst) == 10
1623 && (itype_rt (inst) & 2) == 0)
1624 /* BC1ANY4F, BC1ANY4T: 010001 01010 xxx0x */
1625 pc = mips32_bc1_pc (gdbarch, frame, inst, pc + 4, 4);
1626 else if (op == 29)
1627 /* JALX: 011101 */
1628 /* The new PC will be alternate mode. */
1629 {
1630 unsigned long reg;
1631
1632 reg = jtype_target (inst) << 2;
1633 /* Add 1 to indicate 16-bit mode -- invert ISA mode. */
1634 pc = ((pc + 4) & ~(CORE_ADDR) 0x0fffffff) + reg + 1;
1635 }
1636 else if (is_octeon_bbit_op (op, gdbarch))
1637 {
1638 int bit, branch_if;
1639
1640 branch_if = op == 58 || op == 62;
1641 bit = itype_rt (inst);
1642
1643 /* Take into account the *32 instructions. */
1644 if (op == 54 || op == 62)
1645 bit += 32;
1646
1647 if (((get_frame_register_signed (frame,
1648 itype_rs (inst)) >> bit) & 1)
1649 == branch_if)
1650 pc += mips32_relative_offset (inst) + 4;
1651 else
1652 pc += 8; /* After the delay slot. */
1653 }
1654
1655 else
1656 pc += 4; /* Not a branch, next instruction is easy. */
1657 }
1658 else
1659 { /* This gets way messy. */
1660
1661 /* Further subdivide into SPECIAL, REGIMM and other. */
1662 switch (op & 0x07) /* Extract bits 28,27,26. */
1663 {
1664 case 0: /* SPECIAL */
1665 op = rtype_funct (inst);
1666 switch (op)
1667 {
1668 case 8: /* JR */
1669 case 9: /* JALR */
1670 /* Set PC to that address. */
1671 pc = get_frame_register_signed (frame, rtype_rs (inst));
1672 break;
1673 case 12: /* SYSCALL */
1674 {
1675 struct gdbarch_tdep *tdep;
1676
1677 tdep = gdbarch_tdep (get_frame_arch (frame));
1678 if (tdep->syscall_next_pc != NULL)
1679 pc = tdep->syscall_next_pc (frame);
1680 else
1681 pc += 4;
1682 }
1683 break;
1684 default:
1685 pc += 4;
1686 }
1687
1688 break; /* end SPECIAL */
1689 case 1: /* REGIMM */
1690 {
1691 op = itype_rt (inst); /* branch condition */
1692 switch (op)
1693 {
1694 case 0: /* BLTZ */
1695 case 2: /* BLTZL */
1696 case 16: /* BLTZAL */
1697 case 18: /* BLTZALL */
1698 less_branch:
1699 if (get_frame_register_signed (frame, itype_rs (inst)) < 0)
1700 pc += mips32_relative_offset (inst) + 4;
1701 else
1702 pc += 8; /* after the delay slot */
1703 break;
1704 case 1: /* BGEZ */
1705 case 3: /* BGEZL */
1706 case 17: /* BGEZAL */
1707 case 19: /* BGEZALL */
1708 if (get_frame_register_signed (frame, itype_rs (inst)) >= 0)
1709 pc += mips32_relative_offset (inst) + 4;
1710 else
1711 pc += 8; /* after the delay slot */
1712 break;
1713 case 0x1c: /* BPOSGE32 */
1714 case 0x1e: /* BPOSGE64 */
1715 pc += 4;
1716 if (itype_rs (inst) == 0)
1717 {
1718 unsigned int pos = (op & 2) ? 64 : 32;
1719 int dspctl = mips_regnum (gdbarch)->dspctl;
1720
1721 if (dspctl == -1)
1722 /* No way to handle; it'll most likely trap anyway. */
1723 break;
1724
1725 if ((get_frame_register_unsigned (frame,
1726 dspctl) & 0x7f) >= pos)
1727 pc += mips32_relative_offset (inst);
1728 else
1729 pc += 4;
1730 }
1731 break;
1732 /* All of the other instructions in the REGIMM category */
1733 default:
1734 pc += 4;
1735 }
1736 }
1737 break; /* end REGIMM */
1738 case 2: /* J */
1739 case 3: /* JAL */
1740 {
1741 unsigned long reg;
1742 reg = jtype_target (inst) << 2;
1743 /* Upper four bits get never changed... */
1744 pc = reg + ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
1745 }
1746 break;
1747 case 4: /* BEQ, BEQL */
1748 equal_branch:
1749 if (get_frame_register_signed (frame, itype_rs (inst)) ==
1750 get_frame_register_signed (frame, itype_rt (inst)))
1751 pc += mips32_relative_offset (inst) + 4;
1752 else
1753 pc += 8;
1754 break;
1755 case 5: /* BNE, BNEL */
1756 neq_branch:
1757 if (get_frame_register_signed (frame, itype_rs (inst)) !=
1758 get_frame_register_signed (frame, itype_rt (inst)))
1759 pc += mips32_relative_offset (inst) + 4;
1760 else
1761 pc += 8;
1762 break;
1763 case 6: /* BLEZ, BLEZL */
1764 if (get_frame_register_signed (frame, itype_rs (inst)) <= 0)
1765 pc += mips32_relative_offset (inst) + 4;
1766 else
1767 pc += 8;
1768 break;
1769 case 7:
1770 default:
1771 greater_branch: /* BGTZ, BGTZL */
1772 if (get_frame_register_signed (frame, itype_rs (inst)) > 0)
1773 pc += mips32_relative_offset (inst) + 4;
1774 else
1775 pc += 8;
1776 break;
1777 } /* switch */
1778 } /* else */
1779 return pc;
1780 } /* mips32_next_pc */
1781
1782 /* Extract the 7-bit signed immediate offset from the microMIPS instruction
1783 INSN. */
1784
1785 static LONGEST
1786 micromips_relative_offset7 (ULONGEST insn)
1787 {
1788 return ((b0s7_imm (insn) ^ 0x40) - 0x40) << 1;
1789 }
1790
1791 /* Extract the 10-bit signed immediate offset from the microMIPS instruction
1792 INSN. */
1793
1794 static LONGEST
1795 micromips_relative_offset10 (ULONGEST insn)
1796 {
1797 return ((b0s10_imm (insn) ^ 0x200) - 0x200) << 1;
1798 }
1799
1800 /* Extract the 16-bit signed immediate offset from the microMIPS instruction
1801 INSN. */
1802
1803 static LONGEST
1804 micromips_relative_offset16 (ULONGEST insn)
1805 {
1806 return ((b0s16_imm (insn) ^ 0x8000) - 0x8000) << 1;
1807 }
1808
1809 /* Return the size in bytes of the microMIPS instruction at the address PC. */
1810
1811 static int
1812 micromips_pc_insn_size (struct gdbarch *gdbarch, CORE_ADDR pc)
1813 {
1814 ULONGEST insn;
1815
1816 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1817 return mips_insn_size (ISA_MICROMIPS, insn);
1818 }
1819
1820 /* Calculate the address of the next microMIPS instruction to execute
1821 after the INSN coprocessor 1 conditional branch instruction at the
1822 address PC. COUNT denotes the number of coprocessor condition bits
1823 examined by the branch. */
1824
1825 static CORE_ADDR
1826 micromips_bc1_pc (struct gdbarch *gdbarch, struct frame_info *frame,
1827 ULONGEST insn, CORE_ADDR pc, int count)
1828 {
1829 int fcsr = mips_regnum (gdbarch)->fp_control_status;
1830 int cnum = b2s3_cc (insn >> 16) & (count - 1);
1831 int tf = b5s5_op (insn >> 16) & 1;
1832 int mask = (1 << count) - 1;
1833 ULONGEST fcs;
1834 int cond;
1835
1836 if (fcsr == -1)
1837 /* No way to handle; it'll most likely trap anyway. */
1838 return pc;
1839
1840 fcs = get_frame_register_unsigned (frame, fcsr);
1841 cond = ((fcs >> 24) & 0xfe) | ((fcs >> 23) & 0x01);
1842
1843 if (((cond >> cnum) & mask) != mask * !tf)
1844 pc += micromips_relative_offset16 (insn);
1845 else
1846 pc += micromips_pc_insn_size (gdbarch, pc);
1847
1848 return pc;
1849 }
1850
1851 /* Calculate the address of the next microMIPS instruction to execute
1852 after the instruction at the address PC. */
1853
1854 static CORE_ADDR
1855 micromips_next_pc (struct frame_info *frame, CORE_ADDR pc)
1856 {
1857 struct gdbarch *gdbarch = get_frame_arch (frame);
1858 ULONGEST insn;
1859
1860 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1861 pc += MIPS_INSN16_SIZE;
1862 switch (mips_insn_size (ISA_MICROMIPS, insn))
1863 {
1864 /* 32-bit instructions. */
1865 case 2 * MIPS_INSN16_SIZE:
1866 insn <<= 16;
1867 insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
1868 pc += MIPS_INSN16_SIZE;
1869 switch (micromips_op (insn >> 16))
1870 {
1871 case 0x00: /* POOL32A: bits 000000 */
1872 if (b0s6_op (insn) == 0x3c
1873 /* POOL32Axf: bits 000000 ... 111100 */
1874 && (b6s10_ext (insn) & 0x2bf) == 0x3c)
1875 /* JALR, JALR.HB: 000000 000x111100 111100 */
1876 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
1877 pc = get_frame_register_signed (frame, b0s5_reg (insn >> 16));
1878 break;
1879
1880 case 0x10: /* POOL32I: bits 010000 */
1881 switch (b5s5_op (insn >> 16))
1882 {
1883 case 0x00: /* BLTZ: bits 010000 00000 */
1884 case 0x01: /* BLTZAL: bits 010000 00001 */
1885 case 0x11: /* BLTZALS: bits 010000 10001 */
1886 if (get_frame_register_signed (frame,
1887 b0s5_reg (insn >> 16)) < 0)
1888 pc += micromips_relative_offset16 (insn);
1889 else
1890 pc += micromips_pc_insn_size (gdbarch, pc);
1891 break;
1892
1893 case 0x02: /* BGEZ: bits 010000 00010 */
1894 case 0x03: /* BGEZAL: bits 010000 00011 */
1895 case 0x13: /* BGEZALS: bits 010000 10011 */
1896 if (get_frame_register_signed (frame,
1897 b0s5_reg (insn >> 16)) >= 0)
1898 pc += micromips_relative_offset16 (insn);
1899 else
1900 pc += micromips_pc_insn_size (gdbarch, pc);
1901 break;
1902
1903 case 0x04: /* BLEZ: bits 010000 00100 */
1904 if (get_frame_register_signed (frame,
1905 b0s5_reg (insn >> 16)) <= 0)
1906 pc += micromips_relative_offset16 (insn);
1907 else
1908 pc += micromips_pc_insn_size (gdbarch, pc);
1909 break;
1910
1911 case 0x05: /* BNEZC: bits 010000 00101 */
1912 if (get_frame_register_signed (frame,
1913 b0s5_reg (insn >> 16)) != 0)
1914 pc += micromips_relative_offset16 (insn);
1915 break;
1916
1917 case 0x06: /* BGTZ: bits 010000 00110 */
1918 if (get_frame_register_signed (frame,
1919 b0s5_reg (insn >> 16)) > 0)
1920 pc += micromips_relative_offset16 (insn);
1921 else
1922 pc += micromips_pc_insn_size (gdbarch, pc);
1923 break;
1924
1925 case 0x07: /* BEQZC: bits 010000 00111 */
1926 if (get_frame_register_signed (frame,
1927 b0s5_reg (insn >> 16)) == 0)
1928 pc += micromips_relative_offset16 (insn);
1929 break;
1930
1931 case 0x14: /* BC2F: bits 010000 10100 xxx00 */
1932 case 0x15: /* BC2T: bits 010000 10101 xxx00 */
1933 if (((insn >> 16) & 0x3) == 0x0)
1934 /* BC2F, BC2T: don't know how to handle these. */
1935 break;
1936 break;
1937
1938 case 0x1a: /* BPOSGE64: bits 010000 11010 */
1939 case 0x1b: /* BPOSGE32: bits 010000 11011 */
1940 {
1941 unsigned int pos = (b5s5_op (insn >> 16) & 1) ? 32 : 64;
1942 int dspctl = mips_regnum (gdbarch)->dspctl;
1943
1944 if (dspctl == -1)
1945 /* No way to handle; it'll most likely trap anyway. */
1946 break;
1947
1948 if ((get_frame_register_unsigned (frame,
1949 dspctl) & 0x7f) >= pos)
1950 pc += micromips_relative_offset16 (insn);
1951 else
1952 pc += micromips_pc_insn_size (gdbarch, pc);
1953 }
1954 break;
1955
1956 case 0x1c: /* BC1F: bits 010000 11100 xxx00 */
1957 /* BC1ANY2F: bits 010000 11100 xxx01 */
1958 case 0x1d: /* BC1T: bits 010000 11101 xxx00 */
1959 /* BC1ANY2T: bits 010000 11101 xxx01 */
1960 if (((insn >> 16) & 0x2) == 0x0)
1961 pc = micromips_bc1_pc (gdbarch, frame, insn, pc,
1962 ((insn >> 16) & 0x1) + 1);
1963 break;
1964
1965 case 0x1e: /* BC1ANY4F: bits 010000 11110 xxx01 */
1966 case 0x1f: /* BC1ANY4T: bits 010000 11111 xxx01 */
1967 if (((insn >> 16) & 0x3) == 0x1)
1968 pc = micromips_bc1_pc (gdbarch, frame, insn, pc, 4);
1969 break;
1970 }
1971 break;
1972
1973 case 0x1d: /* JALS: bits 011101 */
1974 case 0x35: /* J: bits 110101 */
1975 case 0x3d: /* JAL: bits 111101 */
1976 pc = ((pc | 0x7fffffe) ^ 0x7fffffe) | (b0s26_imm (insn) << 1);
1977 break;
1978
1979 case 0x25: /* BEQ: bits 100101 */
1980 if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
1981 == get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
1982 pc += micromips_relative_offset16 (insn);
1983 else
1984 pc += micromips_pc_insn_size (gdbarch, pc);
1985 break;
1986
1987 case 0x2d: /* BNE: bits 101101 */
1988 if (get_frame_register_signed (frame, b0s5_reg (insn >> 16))
1989 != get_frame_register_signed (frame, b5s5_reg (insn >> 16)))
1990 pc += micromips_relative_offset16 (insn);
1991 else
1992 pc += micromips_pc_insn_size (gdbarch, pc);
1993 break;
1994
1995 case 0x3c: /* JALX: bits 111100 */
1996 pc = ((pc | 0xfffffff) ^ 0xfffffff) | (b0s26_imm (insn) << 2);
1997 break;
1998 }
1999 break;
2000
2001 /* 16-bit instructions. */
2002 case MIPS_INSN16_SIZE:
2003 switch (micromips_op (insn))
2004 {
2005 case 0x11: /* POOL16C: bits 010001 */
2006 if ((b5s5_op (insn) & 0x1c) == 0xc)
2007 /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
2008 pc = get_frame_register_signed (frame, b0s5_reg (insn));
2009 else if (b5s5_op (insn) == 0x18)
2010 /* JRADDIUSP: bits 010001 11000 */
2011 pc = get_frame_register_signed (frame, MIPS_RA_REGNUM);
2012 break;
2013
2014 case 0x23: /* BEQZ16: bits 100011 */
2015 {
2016 int rs = mips_reg3_to_reg[b7s3_reg (insn)];
2017
2018 if (get_frame_register_signed (frame, rs) == 0)
2019 pc += micromips_relative_offset7 (insn);
2020 else
2021 pc += micromips_pc_insn_size (gdbarch, pc);
2022 }
2023 break;
2024
2025 case 0x2b: /* BNEZ16: bits 101011 */
2026 {
2027 int rs = mips_reg3_to_reg[b7s3_reg (insn)];
2028
2029 if (get_frame_register_signed (frame, rs) != 0)
2030 pc += micromips_relative_offset7 (insn);
2031 else
2032 pc += micromips_pc_insn_size (gdbarch, pc);
2033 }
2034 break;
2035
2036 case 0x33: /* B16: bits 110011 */
2037 pc += micromips_relative_offset10 (insn);
2038 break;
2039 }
2040 break;
2041 }
2042
2043 return pc;
2044 }
2045
2046 /* Decoding the next place to set a breakpoint is irregular for the
2047 mips 16 variant, but fortunately, there fewer instructions. We have
2048 to cope ith extensions for 16 bit instructions and a pair of actual
2049 32 bit instructions. We dont want to set a single step instruction
2050 on the extend instruction either. */
2051
2052 /* Lots of mips16 instruction formats */
2053 /* Predicting jumps requires itype,ritype,i8type
2054 and their extensions extItype,extritype,extI8type. */
2055 enum mips16_inst_fmts
2056 {
2057 itype, /* 0 immediate 5,10 */
2058 ritype, /* 1 5,3,8 */
2059 rrtype, /* 2 5,3,3,5 */
2060 rritype, /* 3 5,3,3,5 */
2061 rrrtype, /* 4 5,3,3,3,2 */
2062 rriatype, /* 5 5,3,3,1,4 */
2063 shifttype, /* 6 5,3,3,3,2 */
2064 i8type, /* 7 5,3,8 */
2065 i8movtype, /* 8 5,3,3,5 */
2066 i8mov32rtype, /* 9 5,3,5,3 */
2067 i64type, /* 10 5,3,8 */
2068 ri64type, /* 11 5,3,3,5 */
2069 jalxtype, /* 12 5,1,5,5,16 - a 32 bit instruction */
2070 exiItype, /* 13 5,6,5,5,1,1,1,1,1,1,5 */
2071 extRitype, /* 14 5,6,5,5,3,1,1,1,5 */
2072 extRRItype, /* 15 5,5,5,5,3,3,5 */
2073 extRRIAtype, /* 16 5,7,4,5,3,3,1,4 */
2074 EXTshifttype, /* 17 5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
2075 extI8type, /* 18 5,6,5,5,3,1,1,1,5 */
2076 extI64type, /* 19 5,6,5,5,3,1,1,1,5 */
2077 extRi64type, /* 20 5,6,5,5,3,3,5 */
2078 extshift64type /* 21 5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
2079 };
2080 /* I am heaping all the fields of the formats into one structure and
2081 then, only the fields which are involved in instruction extension. */
2082 struct upk_mips16
2083 {
2084 CORE_ADDR offset;
2085 unsigned int regx; /* Function in i8 type. */
2086 unsigned int regy;
2087 };
2088
2089
2090 /* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same format
2091 for the bits which make up the immediate extension. */
2092
2093 static CORE_ADDR
2094 extended_offset (unsigned int extension)
2095 {
2096 CORE_ADDR value;
2097
2098 value = (extension >> 16) & 0x1f; /* Extract 15:11. */
2099 value = value << 6;
2100 value |= (extension >> 21) & 0x3f; /* Extract 10:5. */
2101 value = value << 5;
2102 value |= extension & 0x1f; /* Extract 4:0. */
2103
2104 return value;
2105 }
2106
2107 /* Only call this function if you know that this is an extendable
2108 instruction. It won't malfunction, but why make excess remote memory
2109 references? If the immediate operands get sign extended or something,
2110 do it after the extension is performed. */
2111 /* FIXME: Every one of these cases needs to worry about sign extension
2112 when the offset is to be used in relative addressing. */
2113
2114 static unsigned int
2115 fetch_mips_16 (struct gdbarch *gdbarch, CORE_ADDR pc)
2116 {
2117 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2118 gdb_byte buf[8];
2119
2120 pc = unmake_compact_addr (pc); /* Clear the low order bit. */
2121 target_read_memory (pc, buf, 2);
2122 return extract_unsigned_integer (buf, 2, byte_order);
2123 }
2124
2125 static void
2126 unpack_mips16 (struct gdbarch *gdbarch, CORE_ADDR pc,
2127 unsigned int extension,
2128 unsigned int inst,
2129 enum mips16_inst_fmts insn_format, struct upk_mips16 *upk)
2130 {
2131 CORE_ADDR offset;
2132 int regx;
2133 int regy;
2134 switch (insn_format)
2135 {
2136 case itype:
2137 {
2138 CORE_ADDR value;
2139 if (extension)
2140 {
2141 value = extended_offset ((extension << 16) | inst);
2142 value = (value ^ 0x8000) - 0x8000; /* Sign-extend. */
2143 }
2144 else
2145 {
2146 value = inst & 0x7ff;
2147 value = (value ^ 0x400) - 0x400; /* Sign-extend. */
2148 }
2149 offset = value;
2150 regx = -1;
2151 regy = -1;
2152 }
2153 break;
2154 case ritype:
2155 case i8type:
2156 { /* A register identifier and an offset. */
2157 /* Most of the fields are the same as I type but the
2158 immediate value is of a different length. */
2159 CORE_ADDR value;
2160 if (extension)
2161 {
2162 value = extended_offset ((extension << 16) | inst);
2163 value = (value ^ 0x8000) - 0x8000; /* Sign-extend. */
2164 }
2165 else
2166 {
2167 value = inst & 0xff; /* 8 bits */
2168 value = (value ^ 0x80) - 0x80; /* Sign-extend. */
2169 }
2170 offset = value;
2171 regx = (inst >> 8) & 0x07; /* i8 funct */
2172 regy = -1;
2173 break;
2174 }
2175 case jalxtype:
2176 {
2177 unsigned long value;
2178 unsigned int nexthalf;
2179 value = ((inst & 0x1f) << 5) | ((inst >> 5) & 0x1f);
2180 value = value << 16;
2181 nexthalf = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc + 2, NULL);
2182 /* Low bit still set. */
2183 value |= nexthalf;
2184 offset = value;
2185 regx = -1;
2186 regy = -1;
2187 break;
2188 }
2189 default:
2190 internal_error (__FILE__, __LINE__, _("bad switch"));
2191 }
2192 upk->offset = offset;
2193 upk->regx = regx;
2194 upk->regy = regy;
2195 }
2196
2197
2198 /* Calculate the destination of a branch whose 16-bit opcode word is at PC,
2199 and having a signed 16-bit OFFSET. */
2200
2201 static CORE_ADDR
2202 add_offset_16 (CORE_ADDR pc, int offset)
2203 {
2204 return pc + (offset << 1) + 2;
2205 }
2206
2207 static CORE_ADDR
2208 extended_mips16_next_pc (struct frame_info *frame, CORE_ADDR pc,
2209 unsigned int extension, unsigned int insn)
2210 {
2211 struct gdbarch *gdbarch = get_frame_arch (frame);
2212 int op = (insn >> 11);
2213 switch (op)
2214 {
2215 case 2: /* Branch */
2216 {
2217 struct upk_mips16 upk;
2218 unpack_mips16 (gdbarch, pc, extension, insn, itype, &upk);
2219 pc = add_offset_16 (pc, upk.offset);
2220 break;
2221 }
2222 case 3: /* JAL , JALX - Watch out, these are 32 bit
2223 instructions. */
2224 {
2225 struct upk_mips16 upk;
2226 unpack_mips16 (gdbarch, pc, extension, insn, jalxtype, &upk);
2227 pc = ((pc + 2) & (~(CORE_ADDR) 0x0fffffff)) | (upk.offset << 2);
2228 if ((insn >> 10) & 0x01) /* Exchange mode */
2229 pc = pc & ~0x01; /* Clear low bit, indicate 32 bit mode. */
2230 else
2231 pc |= 0x01;
2232 break;
2233 }
2234 case 4: /* beqz */
2235 {
2236 struct upk_mips16 upk;
2237 int reg;
2238 unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
2239 reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
2240 if (reg == 0)
2241 pc = add_offset_16 (pc, upk.offset);
2242 else
2243 pc += 2;
2244 break;
2245 }
2246 case 5: /* bnez */
2247 {
2248 struct upk_mips16 upk;
2249 int reg;
2250 unpack_mips16 (gdbarch, pc, extension, insn, ritype, &upk);
2251 reg = get_frame_register_signed (frame, mips_reg3_to_reg[upk.regx]);
2252 if (reg != 0)
2253 pc = add_offset_16 (pc, upk.offset);
2254 else
2255 pc += 2;
2256 break;
2257 }
2258 case 12: /* I8 Formats btez btnez */
2259 {
2260 struct upk_mips16 upk;
2261 int reg;
2262 unpack_mips16 (gdbarch, pc, extension, insn, i8type, &upk);
2263 /* upk.regx contains the opcode */
2264 reg = get_frame_register_signed (frame, 24); /* Test register is 24 */
2265 if (((upk.regx == 0) && (reg == 0)) /* BTEZ */
2266 || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
2267 pc = add_offset_16 (pc, upk.offset);
2268 else
2269 pc += 2;
2270 break;
2271 }
2272 case 29: /* RR Formats JR, JALR, JALR-RA */
2273 {
2274 struct upk_mips16 upk;
2275 /* upk.fmt = rrtype; */
2276 op = insn & 0x1f;
2277 if (op == 0)
2278 {
2279 int reg;
2280 upk.regx = (insn >> 8) & 0x07;
2281 upk.regy = (insn >> 5) & 0x07;
2282 if ((upk.regy & 1) == 0)
2283 reg = mips_reg3_to_reg[upk.regx];
2284 else
2285 reg = 31; /* Function return instruction. */
2286 pc = get_frame_register_signed (frame, reg);
2287 }
2288 else
2289 pc += 2;
2290 break;
2291 }
2292 case 30:
2293 /* This is an instruction extension. Fetch the real instruction
2294 (which follows the extension) and decode things based on
2295 that. */
2296 {
2297 pc += 2;
2298 pc = extended_mips16_next_pc (frame, pc, insn,
2299 fetch_mips_16 (gdbarch, pc));
2300 break;
2301 }
2302 default:
2303 {
2304 pc += 2;
2305 break;
2306 }
2307 }
2308 return pc;
2309 }
2310
2311 static CORE_ADDR
2312 mips16_next_pc (struct frame_info *frame, CORE_ADDR pc)
2313 {
2314 struct gdbarch *gdbarch = get_frame_arch (frame);
2315 unsigned int insn = fetch_mips_16 (gdbarch, pc);
2316 return extended_mips16_next_pc (frame, pc, 0, insn);
2317 }
2318
2319 /* The mips_next_pc function supports single_step when the remote
2320 target monitor or stub is not developed enough to do a single_step.
2321 It works by decoding the current instruction and predicting where a
2322 branch will go. This isn't hard because all the data is available.
2323 The MIPS32, MIPS16 and microMIPS variants are quite different. */
2324 static CORE_ADDR
2325 mips_next_pc (struct frame_info *frame, CORE_ADDR pc)
2326 {
2327 struct gdbarch *gdbarch = get_frame_arch (frame);
2328
2329 if (mips_pc_is_mips16 (gdbarch, pc))
2330 return mips16_next_pc (frame, pc);
2331 else if (mips_pc_is_micromips (gdbarch, pc))
2332 return micromips_next_pc (frame, pc);
2333 else
2334 return mips32_next_pc (frame, pc);
2335 }
2336
2337 /* Return non-zero if the MIPS16 instruction INSN is a compact branch
2338 or jump. */
2339
2340 static int
2341 mips16_instruction_is_compact_branch (unsigned short insn)
2342 {
2343 switch (insn & 0xf800)
2344 {
2345 case 0xe800:
2346 return (insn & 0x009f) == 0x80; /* JALRC/JRC */
2347 case 0x6000:
2348 return (insn & 0x0600) == 0; /* BTNEZ/BTEQZ */
2349 case 0x2800: /* BNEZ */
2350 case 0x2000: /* BEQZ */
2351 case 0x1000: /* B */
2352 return 1;
2353 default:
2354 return 0;
2355 }
2356 }
2357
2358 /* Return non-zero if the microMIPS instruction INSN is a compact branch
2359 or jump. */
2360
2361 static int
2362 micromips_instruction_is_compact_branch (unsigned short insn)
2363 {
2364 switch (micromips_op (insn))
2365 {
2366 case 0x11: /* POOL16C: bits 010001 */
2367 return (b5s5_op (insn) == 0x18
2368 /* JRADDIUSP: bits 010001 11000 */
2369 || b5s5_op (insn) == 0xd);
2370 /* JRC: bits 010011 01101 */
2371 case 0x10: /* POOL32I: bits 010000 */
2372 return (b5s5_op (insn) & 0x1d) == 0x5;
2373 /* BEQZC/BNEZC: bits 010000 001x1 */
2374 default:
2375 return 0;
2376 }
2377 }
2378
2379 struct mips_frame_cache
2380 {
2381 CORE_ADDR base;
2382 struct trad_frame_saved_reg *saved_regs;
2383 };
2384
2385 /* Set a register's saved stack address in temp_saved_regs. If an
2386 address has already been set for this register, do nothing; this
2387 way we will only recognize the first save of a given register in a
2388 function prologue.
2389
2390 For simplicity, save the address in both [0 .. gdbarch_num_regs) and
2391 [gdbarch_num_regs .. 2*gdbarch_num_regs).
2392 Strictly speaking, only the second range is used as it is only second
2393 range (the ABI instead of ISA registers) that comes into play when finding
2394 saved registers in a frame. */
2395
2396 static void
2397 set_reg_offset (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache,
2398 int regnum, CORE_ADDR offset)
2399 {
2400 if (this_cache != NULL
2401 && this_cache->saved_regs[regnum].addr == -1)
2402 {
2403 this_cache->saved_regs[regnum + 0 * gdbarch_num_regs (gdbarch)].addr
2404 = offset;
2405 this_cache->saved_regs[regnum + 1 * gdbarch_num_regs (gdbarch)].addr
2406 = offset;
2407 }
2408 }
2409
2410
2411 /* Fetch the immediate value from a MIPS16 instruction.
2412 If the previous instruction was an EXTEND, use it to extend
2413 the upper bits of the immediate value. This is a helper function
2414 for mips16_scan_prologue. */
2415
2416 static int
2417 mips16_get_imm (unsigned short prev_inst, /* previous instruction */
2418 unsigned short inst, /* current instruction */
2419 int nbits, /* number of bits in imm field */
2420 int scale, /* scale factor to be applied to imm */
2421 int is_signed) /* is the imm field signed? */
2422 {
2423 int offset;
2424
2425 if ((prev_inst & 0xf800) == 0xf000) /* prev instruction was EXTEND? */
2426 {
2427 offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
2428 if (offset & 0x8000) /* check for negative extend */
2429 offset = 0 - (0x10000 - (offset & 0xffff));
2430 return offset | (inst & 0x1f);
2431 }
2432 else
2433 {
2434 int max_imm = 1 << nbits;
2435 int mask = max_imm - 1;
2436 int sign_bit = max_imm >> 1;
2437
2438 offset = inst & mask;
2439 if (is_signed && (offset & sign_bit))
2440 offset = 0 - (max_imm - offset);
2441 return offset * scale;
2442 }
2443 }
2444
2445
2446 /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
2447 the associated FRAME_CACHE if not null.
2448 Return the address of the first instruction past the prologue. */
2449
2450 static CORE_ADDR
2451 mips16_scan_prologue (struct gdbarch *gdbarch,
2452 CORE_ADDR start_pc, CORE_ADDR limit_pc,
2453 struct frame_info *this_frame,
2454 struct mips_frame_cache *this_cache)
2455 {
2456 int prev_non_prologue_insn = 0;
2457 int this_non_prologue_insn;
2458 int non_prologue_insns = 0;
2459 CORE_ADDR prev_pc;
2460 CORE_ADDR cur_pc;
2461 CORE_ADDR frame_addr = 0; /* Value of $r17, used as frame pointer. */
2462 CORE_ADDR sp;
2463 long frame_offset = 0; /* Size of stack frame. */
2464 long frame_adjust = 0; /* Offset of FP from SP. */
2465 int frame_reg = MIPS_SP_REGNUM;
2466 unsigned short prev_inst = 0; /* saved copy of previous instruction. */
2467 unsigned inst = 0; /* current instruction */
2468 unsigned entry_inst = 0; /* the entry instruction */
2469 unsigned save_inst = 0; /* the save instruction */
2470 int prev_delay_slot = 0;
2471 int in_delay_slot;
2472 int reg, offset;
2473
2474 int extend_bytes = 0;
2475 int prev_extend_bytes = 0;
2476 CORE_ADDR end_prologue_addr;
2477
2478 /* Can be called when there's no process, and hence when there's no
2479 THIS_FRAME. */
2480 if (this_frame != NULL)
2481 sp = get_frame_register_signed (this_frame,
2482 gdbarch_num_regs (gdbarch)
2483 + MIPS_SP_REGNUM);
2484 else
2485 sp = 0;
2486
2487 if (limit_pc > start_pc + 200)
2488 limit_pc = start_pc + 200;
2489 prev_pc = start_pc;
2490
2491 /* Permit at most one non-prologue non-control-transfer instruction
2492 in the middle which may have been reordered by the compiler for
2493 optimisation. */
2494 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN16_SIZE)
2495 {
2496 this_non_prologue_insn = 0;
2497 in_delay_slot = 0;
2498
2499 /* Save the previous instruction. If it's an EXTEND, we'll extract
2500 the immediate offset extension from it in mips16_get_imm. */
2501 prev_inst = inst;
2502
2503 /* Fetch and decode the instruction. */
2504 inst = (unsigned short) mips_fetch_instruction (gdbarch, ISA_MIPS16,
2505 cur_pc, NULL);
2506
2507 /* Normally we ignore extend instructions. However, if it is
2508 not followed by a valid prologue instruction, then this
2509 instruction is not part of the prologue either. We must
2510 remember in this case to adjust the end_prologue_addr back
2511 over the extend. */
2512 if ((inst & 0xf800) == 0xf000) /* extend */
2513 {
2514 extend_bytes = MIPS_INSN16_SIZE;
2515 continue;
2516 }
2517
2518 prev_extend_bytes = extend_bytes;
2519 extend_bytes = 0;
2520
2521 if ((inst & 0xff00) == 0x6300 /* addiu sp */
2522 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
2523 {
2524 offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
2525 if (offset < 0) /* Negative stack adjustment? */
2526 frame_offset -= offset;
2527 else
2528 /* Exit loop if a positive stack adjustment is found, which
2529 usually means that the stack cleanup code in the function
2530 epilogue is reached. */
2531 break;
2532 }
2533 else if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
2534 {
2535 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2536 reg = mips_reg3_to_reg[(inst & 0x700) >> 8];
2537 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2538 }
2539 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
2540 {
2541 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
2542 reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2543 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2544 }
2545 else if ((inst & 0xff00) == 0x6200) /* sw $ra,n($sp) */
2546 {
2547 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2548 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2549 }
2550 else if ((inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
2551 {
2552 offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
2553 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2554 }
2555 else if (inst == 0x673d) /* move $s1, $sp */
2556 {
2557 frame_addr = sp;
2558 frame_reg = 17;
2559 }
2560 else if ((inst & 0xff00) == 0x0100) /* addiu $s1,sp,n */
2561 {
2562 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
2563 frame_addr = sp + offset;
2564 frame_reg = 17;
2565 frame_adjust = offset;
2566 }
2567 else if ((inst & 0xFF00) == 0xd900) /* sw reg,offset($s1) */
2568 {
2569 offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
2570 reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2571 set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
2572 }
2573 else if ((inst & 0xFF00) == 0x7900) /* sd reg,offset($s1) */
2574 {
2575 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
2576 reg = mips_reg3_to_reg[(inst & 0xe0) >> 5];
2577 set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
2578 }
2579 else if ((inst & 0xf81f) == 0xe809
2580 && (inst & 0x700) != 0x700) /* entry */
2581 entry_inst = inst; /* Save for later processing. */
2582 else if ((inst & 0xff80) == 0x6480) /* save */
2583 {
2584 save_inst = inst; /* Save for later processing. */
2585 if (prev_extend_bytes) /* extend */
2586 save_inst |= prev_inst << 16;
2587 }
2588 else if ((inst & 0xff1c) == 0x6704) /* move reg,$a0-$a3 */
2589 {
2590 /* This instruction is part of the prologue, but we don't
2591 need to do anything special to handle it. */
2592 }
2593 else if (mips16_instruction_has_delay_slot (inst, 0))
2594 /* JAL/JALR/JALX/JR */
2595 {
2596 /* The instruction in the delay slot can be a part
2597 of the prologue, so move forward once more. */
2598 in_delay_slot = 1;
2599 if (mips16_instruction_has_delay_slot (inst, 1))
2600 /* JAL/JALX */
2601 {
2602 prev_extend_bytes = MIPS_INSN16_SIZE;
2603 cur_pc += MIPS_INSN16_SIZE; /* 32-bit instruction */
2604 }
2605 }
2606 else
2607 {
2608 this_non_prologue_insn = 1;
2609 }
2610
2611 non_prologue_insns += this_non_prologue_insn;
2612
2613 /* A jump or branch, or enough non-prologue insns seen? If so,
2614 then we must have reached the end of the prologue by now. */
2615 if (prev_delay_slot || non_prologue_insns > 1
2616 || mips16_instruction_is_compact_branch (inst))
2617 break;
2618
2619 prev_non_prologue_insn = this_non_prologue_insn;
2620 prev_delay_slot = in_delay_slot;
2621 prev_pc = cur_pc - prev_extend_bytes;
2622 }
2623
2624 /* The entry instruction is typically the first instruction in a function,
2625 and it stores registers at offsets relative to the value of the old SP
2626 (before the prologue). But the value of the sp parameter to this
2627 function is the new SP (after the prologue has been executed). So we
2628 can't calculate those offsets until we've seen the entire prologue,
2629 and can calculate what the old SP must have been. */
2630 if (entry_inst != 0)
2631 {
2632 int areg_count = (entry_inst >> 8) & 7;
2633 int sreg_count = (entry_inst >> 6) & 3;
2634
2635 /* The entry instruction always subtracts 32 from the SP. */
2636 frame_offset += 32;
2637
2638 /* Now we can calculate what the SP must have been at the
2639 start of the function prologue. */
2640 sp += frame_offset;
2641
2642 /* Check if a0-a3 were saved in the caller's argument save area. */
2643 for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
2644 {
2645 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2646 offset += mips_abi_regsize (gdbarch);
2647 }
2648
2649 /* Check if the ra register was pushed on the stack. */
2650 offset = -4;
2651 if (entry_inst & 0x20)
2652 {
2653 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2654 offset -= mips_abi_regsize (gdbarch);
2655 }
2656
2657 /* Check if the s0 and s1 registers were pushed on the stack. */
2658 for (reg = 16; reg < sreg_count + 16; reg++)
2659 {
2660 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2661 offset -= mips_abi_regsize (gdbarch);
2662 }
2663 }
2664
2665 /* The SAVE instruction is similar to ENTRY, except that defined by the
2666 MIPS16e ASE of the MIPS Architecture. Unlike with ENTRY though, the
2667 size of the frame is specified as an immediate field of instruction
2668 and an extended variation exists which lets additional registers and
2669 frame space to be specified. The instruction always treats registers
2670 as 32-bit so its usefulness for 64-bit ABIs is questionable. */
2671 if (save_inst != 0 && mips_abi_regsize (gdbarch) == 4)
2672 {
2673 static int args_table[16] = {
2674 0, 0, 0, 0, 1, 1, 1, 1,
2675 2, 2, 2, 0, 3, 3, 4, -1,
2676 };
2677 static int astatic_table[16] = {
2678 0, 1, 2, 3, 0, 1, 2, 3,
2679 0, 1, 2, 4, 0, 1, 0, -1,
2680 };
2681 int aregs = (save_inst >> 16) & 0xf;
2682 int xsregs = (save_inst >> 24) & 0x7;
2683 int args = args_table[aregs];
2684 int astatic = astatic_table[aregs];
2685 long frame_size;
2686
2687 if (args < 0)
2688 {
2689 warning (_("Invalid number of argument registers encoded in SAVE."));
2690 args = 0;
2691 }
2692 if (astatic < 0)
2693 {
2694 warning (_("Invalid number of static registers encoded in SAVE."));
2695 astatic = 0;
2696 }
2697
2698 /* For standard SAVE the frame size of 0 means 128. */
2699 frame_size = ((save_inst >> 16) & 0xf0) | (save_inst & 0xf);
2700 if (frame_size == 0 && (save_inst >> 16) == 0)
2701 frame_size = 16;
2702 frame_size *= 8;
2703 frame_offset += frame_size;
2704
2705 /* Now we can calculate what the SP must have been at the
2706 start of the function prologue. */
2707 sp += frame_offset;
2708
2709 /* Check if A0-A3 were saved in the caller's argument save area. */
2710 for (reg = MIPS_A0_REGNUM, offset = 0; reg < args + 4; reg++)
2711 {
2712 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2713 offset += mips_abi_regsize (gdbarch);
2714 }
2715
2716 offset = -4;
2717
2718 /* Check if the RA register was pushed on the stack. */
2719 if (save_inst & 0x40)
2720 {
2721 set_reg_offset (gdbarch, this_cache, MIPS_RA_REGNUM, sp + offset);
2722 offset -= mips_abi_regsize (gdbarch);
2723 }
2724
2725 /* Check if the S8 register was pushed on the stack. */
2726 if (xsregs > 6)
2727 {
2728 set_reg_offset (gdbarch, this_cache, 30, sp + offset);
2729 offset -= mips_abi_regsize (gdbarch);
2730 xsregs--;
2731 }
2732 /* Check if S2-S7 were pushed on the stack. */
2733 for (reg = 18 + xsregs - 1; reg > 18 - 1; reg--)
2734 {
2735 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2736 offset -= mips_abi_regsize (gdbarch);
2737 }
2738
2739 /* Check if the S1 register was pushed on the stack. */
2740 if (save_inst & 0x10)
2741 {
2742 set_reg_offset (gdbarch, this_cache, 17, sp + offset);
2743 offset -= mips_abi_regsize (gdbarch);
2744 }
2745 /* Check if the S0 register was pushed on the stack. */
2746 if (save_inst & 0x20)
2747 {
2748 set_reg_offset (gdbarch, this_cache, 16, sp + offset);
2749 offset -= mips_abi_regsize (gdbarch);
2750 }
2751
2752 /* Check if A0-A3 were pushed on the stack. */
2753 for (reg = MIPS_A0_REGNUM + 3; reg > MIPS_A0_REGNUM + 3 - astatic; reg--)
2754 {
2755 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
2756 offset -= mips_abi_regsize (gdbarch);
2757 }
2758 }
2759
2760 if (this_cache != NULL)
2761 {
2762 this_cache->base =
2763 (get_frame_register_signed (this_frame,
2764 gdbarch_num_regs (gdbarch) + frame_reg)
2765 + frame_offset - frame_adjust);
2766 /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
2767 be able to get rid of the assignment below, evetually. But it's
2768 still needed for now. */
2769 this_cache->saved_regs[gdbarch_num_regs (gdbarch)
2770 + mips_regnum (gdbarch)->pc]
2771 = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
2772 }
2773
2774 /* Set end_prologue_addr to the address of the instruction immediately
2775 after the last one we scanned. Unless the last one looked like a
2776 non-prologue instruction (and we looked ahead), in which case use
2777 its address instead. */
2778 end_prologue_addr = (prev_non_prologue_insn || prev_delay_slot
2779 ? prev_pc : cur_pc - prev_extend_bytes);
2780
2781 return end_prologue_addr;
2782 }
2783
2784 /* Heuristic unwinder for 16-bit MIPS instruction set (aka MIPS16).
2785 Procedures that use the 32-bit instruction set are handled by the
2786 mips_insn32 unwinder. */
2787
2788 static struct mips_frame_cache *
2789 mips_insn16_frame_cache (struct frame_info *this_frame, void **this_cache)
2790 {
2791 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2792 struct mips_frame_cache *cache;
2793
2794 if ((*this_cache) != NULL)
2795 return (struct mips_frame_cache *) (*this_cache);
2796 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
2797 (*this_cache) = cache;
2798 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
2799
2800 /* Analyze the function prologue. */
2801 {
2802 const CORE_ADDR pc = get_frame_address_in_block (this_frame);
2803 CORE_ADDR start_addr;
2804
2805 find_pc_partial_function (pc, NULL, &start_addr, NULL);
2806 if (start_addr == 0)
2807 start_addr = heuristic_proc_start (gdbarch, pc);
2808 /* We can't analyze the prologue if we couldn't find the begining
2809 of the function. */
2810 if (start_addr == 0)
2811 return cache;
2812
2813 mips16_scan_prologue (gdbarch, start_addr, pc, this_frame,
2814 (struct mips_frame_cache *) *this_cache);
2815 }
2816
2817 /* gdbarch_sp_regnum contains the value and not the address. */
2818 trad_frame_set_value (cache->saved_regs,
2819 gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
2820 cache->base);
2821
2822 return (struct mips_frame_cache *) (*this_cache);
2823 }
2824
2825 static void
2826 mips_insn16_frame_this_id (struct frame_info *this_frame, void **this_cache,
2827 struct frame_id *this_id)
2828 {
2829 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2830 this_cache);
2831 /* This marks the outermost frame. */
2832 if (info->base == 0)
2833 return;
2834 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
2835 }
2836
2837 static struct value *
2838 mips_insn16_frame_prev_register (struct frame_info *this_frame,
2839 void **this_cache, int regnum)
2840 {
2841 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2842 this_cache);
2843 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
2844 }
2845
2846 static int
2847 mips_insn16_frame_sniffer (const struct frame_unwind *self,
2848 struct frame_info *this_frame, void **this_cache)
2849 {
2850 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2851 CORE_ADDR pc = get_frame_pc (this_frame);
2852 if (mips_pc_is_mips16 (gdbarch, pc))
2853 return 1;
2854 return 0;
2855 }
2856
2857 static const struct frame_unwind mips_insn16_frame_unwind =
2858 {
2859 NORMAL_FRAME,
2860 default_frame_unwind_stop_reason,
2861 mips_insn16_frame_this_id,
2862 mips_insn16_frame_prev_register,
2863 NULL,
2864 mips_insn16_frame_sniffer
2865 };
2866
2867 static CORE_ADDR
2868 mips_insn16_frame_base_address (struct frame_info *this_frame,
2869 void **this_cache)
2870 {
2871 struct mips_frame_cache *info = mips_insn16_frame_cache (this_frame,
2872 this_cache);
2873 return info->base;
2874 }
2875
2876 static const struct frame_base mips_insn16_frame_base =
2877 {
2878 &mips_insn16_frame_unwind,
2879 mips_insn16_frame_base_address,
2880 mips_insn16_frame_base_address,
2881 mips_insn16_frame_base_address
2882 };
2883
2884 static const struct frame_base *
2885 mips_insn16_frame_base_sniffer (struct frame_info *this_frame)
2886 {
2887 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2888 CORE_ADDR pc = get_frame_pc (this_frame);
2889 if (mips_pc_is_mips16 (gdbarch, pc))
2890 return &mips_insn16_frame_base;
2891 else
2892 return NULL;
2893 }
2894
2895 /* Decode a 9-bit signed immediate argument of ADDIUSP -- -2 is mapped
2896 to -258, -1 -- to -257, 0 -- to 256, 1 -- to 257 and other values are
2897 interpreted directly, and then multiplied by 4. */
2898
2899 static int
2900 micromips_decode_imm9 (int imm)
2901 {
2902 imm = (imm ^ 0x100) - 0x100;
2903 if (imm > -3 && imm < 2)
2904 imm ^= 0x100;
2905 return imm << 2;
2906 }
2907
2908 /* Analyze the function prologue from START_PC to LIMIT_PC. Return
2909 the address of the first instruction past the prologue. */
2910
2911 static CORE_ADDR
2912 micromips_scan_prologue (struct gdbarch *gdbarch,
2913 CORE_ADDR start_pc, CORE_ADDR limit_pc,
2914 struct frame_info *this_frame,
2915 struct mips_frame_cache *this_cache)
2916 {
2917 CORE_ADDR end_prologue_addr;
2918 int prev_non_prologue_insn = 0;
2919 int frame_reg = MIPS_SP_REGNUM;
2920 int this_non_prologue_insn;
2921 int non_prologue_insns = 0;
2922 long frame_offset = 0; /* Size of stack frame. */
2923 long frame_adjust = 0; /* Offset of FP from SP. */
2924 int prev_delay_slot = 0;
2925 int in_delay_slot;
2926 CORE_ADDR prev_pc;
2927 CORE_ADDR cur_pc;
2928 ULONGEST insn; /* current instruction */
2929 CORE_ADDR sp;
2930 long offset;
2931 long sp_adj;
2932 long v1_off = 0; /* The assumption is LUI will replace it. */
2933 int reglist;
2934 int breg;
2935 int dreg;
2936 int sreg;
2937 int treg;
2938 int loc;
2939 int op;
2940 int s;
2941 int i;
2942
2943 /* Can be called when there's no process, and hence when there's no
2944 THIS_FRAME. */
2945 if (this_frame != NULL)
2946 sp = get_frame_register_signed (this_frame,
2947 gdbarch_num_regs (gdbarch)
2948 + MIPS_SP_REGNUM);
2949 else
2950 sp = 0;
2951
2952 if (limit_pc > start_pc + 200)
2953 limit_pc = start_pc + 200;
2954 prev_pc = start_pc;
2955
2956 /* Permit at most one non-prologue non-control-transfer instruction
2957 in the middle which may have been reordered by the compiler for
2958 optimisation. */
2959 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += loc)
2960 {
2961 this_non_prologue_insn = 0;
2962 in_delay_slot = 0;
2963 sp_adj = 0;
2964 loc = 0;
2965 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, cur_pc, NULL);
2966 loc += MIPS_INSN16_SIZE;
2967 switch (mips_insn_size (ISA_MICROMIPS, insn))
2968 {
2969 /* 32-bit instructions. */
2970 case 2 * MIPS_INSN16_SIZE:
2971 insn <<= 16;
2972 insn |= mips_fetch_instruction (gdbarch,
2973 ISA_MICROMIPS, cur_pc + loc, NULL);
2974 loc += MIPS_INSN16_SIZE;
2975 switch (micromips_op (insn >> 16))
2976 {
2977 /* Record $sp/$fp adjustment. */
2978 /* Discard (D)ADDU $gp,$jp used for PIC code. */
2979 case 0x0: /* POOL32A: bits 000000 */
2980 case 0x16: /* POOL32S: bits 010110 */
2981 op = b0s11_op (insn);
2982 sreg = b0s5_reg (insn >> 16);
2983 treg = b5s5_reg (insn >> 16);
2984 dreg = b11s5_reg (insn);
2985 if (op == 0x1d0
2986 /* SUBU: bits 000000 00111010000 */
2987 /* DSUBU: bits 010110 00111010000 */
2988 && dreg == MIPS_SP_REGNUM && sreg == MIPS_SP_REGNUM
2989 && treg == 3)
2990 /* (D)SUBU $sp, $v1 */
2991 sp_adj = v1_off;
2992 else if (op != 0x150
2993 /* ADDU: bits 000000 00101010000 */
2994 /* DADDU: bits 010110 00101010000 */
2995 || dreg != 28 || sreg != 28 || treg != MIPS_T9_REGNUM)
2996 this_non_prologue_insn = 1;
2997 break;
2998
2999 case 0x8: /* POOL32B: bits 001000 */
3000 op = b12s4_op (insn);
3001 breg = b0s5_reg (insn >> 16);
3002 reglist = sreg = b5s5_reg (insn >> 16);
3003 offset = (b0s12_imm (insn) ^ 0x800) - 0x800;
3004 if ((op == 0x9 || op == 0xc)
3005 /* SWP: bits 001000 1001 */
3006 /* SDP: bits 001000 1100 */
3007 && breg == MIPS_SP_REGNUM && sreg < MIPS_RA_REGNUM)
3008 /* S[DW]P reg,offset($sp) */
3009 {
3010 s = 4 << ((b12s4_op (insn) & 0x4) == 0x4);
3011 set_reg_offset (gdbarch, this_cache,
3012 sreg, sp + offset);
3013 set_reg_offset (gdbarch, this_cache,
3014 sreg + 1, sp + offset + s);
3015 }
3016 else if ((op == 0xd || op == 0xf)
3017 /* SWM: bits 001000 1101 */
3018 /* SDM: bits 001000 1111 */
3019 && breg == MIPS_SP_REGNUM
3020 /* SWM reglist,offset($sp) */
3021 && ((reglist >= 1 && reglist <= 9)
3022 || (reglist >= 16 && reglist <= 25)))
3023 {
3024 int sreglist = std::min(reglist & 0xf, 8);
3025
3026 s = 4 << ((b12s4_op (insn) & 0x2) == 0x2);
3027 for (i = 0; i < sreglist; i++)
3028 set_reg_offset (gdbarch, this_cache, 16 + i, sp + s * i);
3029 if ((reglist & 0xf) > 8)
3030 set_reg_offset (gdbarch, this_cache, 30, sp + s * i++);
3031 if ((reglist & 0x10) == 0x10)
3032 set_reg_offset (gdbarch, this_cache,
3033 MIPS_RA_REGNUM, sp + s * i++);
3034 }
3035 else
3036 this_non_prologue_insn = 1;
3037 break;
3038
3039 /* Record $sp/$fp adjustment. */
3040 /* Discard (D)ADDIU $gp used for PIC code. */
3041 case 0xc: /* ADDIU: bits 001100 */
3042 case 0x17: /* DADDIU: bits 010111 */
3043 sreg = b0s5_reg (insn >> 16);
3044 dreg = b5s5_reg (insn >> 16);
3045 offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
3046 if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM)
3047 /* (D)ADDIU $sp, imm */
3048 sp_adj = offset;
3049 else if (sreg == MIPS_SP_REGNUM && dreg == 30)
3050 /* (D)ADDIU $fp, $sp, imm */
3051 {
3052 frame_adjust = offset;
3053 frame_reg = 30;
3054 }
3055 else if (sreg != 28 || dreg != 28)
3056 /* (D)ADDIU $gp, imm */
3057 this_non_prologue_insn = 1;
3058 break;
3059
3060 /* LUI $v1 is used for larger $sp adjustments. */
3061 /* Discard LUI $gp used for PIC code. */
3062 case 0x10: /* POOL32I: bits 010000 */
3063 if (b5s5_op (insn >> 16) == 0xd
3064 /* LUI: bits 010000 001101 */
3065 && b0s5_reg (insn >> 16) == 3)
3066 /* LUI $v1, imm */
3067 v1_off = ((b0s16_imm (insn) << 16) ^ 0x80000000) - 0x80000000;
3068 else if (b5s5_op (insn >> 16) != 0xd
3069 /* LUI: bits 010000 001101 */
3070 || b0s5_reg (insn >> 16) != 28)
3071 /* LUI $gp, imm */
3072 this_non_prologue_insn = 1;
3073 break;
3074
3075 /* ORI $v1 is used for larger $sp adjustments. */
3076 case 0x14: /* ORI: bits 010100 */
3077 sreg = b0s5_reg (insn >> 16);
3078 dreg = b5s5_reg (insn >> 16);
3079 if (sreg == 3 && dreg == 3)
3080 /* ORI $v1, imm */
3081 v1_off |= b0s16_imm (insn);
3082 else
3083 this_non_prologue_insn = 1;
3084 break;
3085
3086 case 0x26: /* SWC1: bits 100110 */
3087 case 0x2e: /* SDC1: bits 101110 */
3088 breg = b0s5_reg (insn >> 16);
3089 if (breg != MIPS_SP_REGNUM)
3090 /* S[DW]C1 reg,offset($sp) */
3091 this_non_prologue_insn = 1;
3092 break;
3093
3094 case 0x36: /* SD: bits 110110 */
3095 case 0x3e: /* SW: bits 111110 */
3096 breg = b0s5_reg (insn >> 16);
3097 sreg = b5s5_reg (insn >> 16);
3098 offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
3099 if (breg == MIPS_SP_REGNUM)
3100 /* S[DW] reg,offset($sp) */
3101 set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
3102 else
3103 this_non_prologue_insn = 1;
3104 break;
3105
3106 default:
3107 /* The instruction in the delay slot can be a part
3108 of the prologue, so move forward once more. */
3109 if (micromips_instruction_has_delay_slot (insn, 0))
3110 in_delay_slot = 1;
3111 else
3112 this_non_prologue_insn = 1;
3113 break;
3114 }
3115 insn >>= 16;
3116 break;
3117
3118 /* 16-bit instructions. */
3119 case MIPS_INSN16_SIZE:
3120 switch (micromips_op (insn))
3121 {
3122 case 0x3: /* MOVE: bits 000011 */
3123 sreg = b0s5_reg (insn);
3124 dreg = b5s5_reg (insn);
3125 if (sreg == MIPS_SP_REGNUM && dreg == 30)
3126 /* MOVE $fp, $sp */
3127 frame_reg = 30;
3128 else if ((sreg & 0x1c) != 0x4)
3129 /* MOVE reg, $a0-$a3 */
3130 this_non_prologue_insn = 1;
3131 break;
3132
3133 case 0x11: /* POOL16C: bits 010001 */
3134 if (b6s4_op (insn) == 0x5)
3135 /* SWM: bits 010001 0101 */
3136 {
3137 offset = ((b0s4_imm (insn) << 2) ^ 0x20) - 0x20;
3138 reglist = b4s2_regl (insn);
3139 for (i = 0; i <= reglist; i++)
3140 set_reg_offset (gdbarch, this_cache, 16 + i, sp + 4 * i);
3141 set_reg_offset (gdbarch, this_cache,
3142 MIPS_RA_REGNUM, sp + 4 * i++);
3143 }
3144 else
3145 this_non_prologue_insn = 1;
3146 break;
3147
3148 case 0x13: /* POOL16D: bits 010011 */
3149 if ((insn & 0x1) == 0x1)
3150 /* ADDIUSP: bits 010011 1 */
3151 sp_adj = micromips_decode_imm9 (b1s9_imm (insn));
3152 else if (b5s5_reg (insn) == MIPS_SP_REGNUM)
3153 /* ADDIUS5: bits 010011 0 */
3154 /* ADDIUS5 $sp, imm */
3155 sp_adj = (b1s4_imm (insn) ^ 8) - 8;
3156 else
3157 this_non_prologue_insn = 1;
3158 break;
3159
3160 case 0x32: /* SWSP: bits 110010 */
3161 offset = b0s5_imm (insn) << 2;
3162 sreg = b5s5_reg (insn);
3163 set_reg_offset (gdbarch, this_cache, sreg, sp + offset);
3164 break;
3165
3166 default:
3167 /* The instruction in the delay slot can be a part
3168 of the prologue, so move forward once more. */
3169 if (micromips_instruction_has_delay_slot (insn << 16, 0))
3170 in_delay_slot = 1;
3171 else
3172 this_non_prologue_insn = 1;
3173 break;
3174 }
3175 break;
3176 }
3177 if (sp_adj < 0)
3178 frame_offset -= sp_adj;
3179
3180 non_prologue_insns += this_non_prologue_insn;
3181
3182 /* A jump or branch, enough non-prologue insns seen or positive
3183 stack adjustment? If so, then we must have reached the end
3184 of the prologue by now. */
3185 if (prev_delay_slot || non_prologue_insns > 1 || sp_adj > 0
3186 || micromips_instruction_is_compact_branch (insn))
3187 break;
3188
3189 prev_non_prologue_insn = this_non_prologue_insn;
3190 prev_delay_slot = in_delay_slot;
3191 prev_pc = cur_pc;
3192 }
3193
3194 if (this_cache != NULL)
3195 {
3196 this_cache->base =
3197 (get_frame_register_signed (this_frame,
3198 gdbarch_num_regs (gdbarch) + frame_reg)
3199 + frame_offset - frame_adjust);
3200 /* FIXME: brobecker/2004-10-10: Just as in the mips32 case, we should
3201 be able to get rid of the assignment below, evetually. But it's
3202 still needed for now. */
3203 this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3204 + mips_regnum (gdbarch)->pc]
3205 = this_cache->saved_regs[gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM];
3206 }
3207
3208 /* Set end_prologue_addr to the address of the instruction immediately
3209 after the last one we scanned. Unless the last one looked like a
3210 non-prologue instruction (and we looked ahead), in which case use
3211 its address instead. */
3212 end_prologue_addr
3213 = prev_non_prologue_insn || prev_delay_slot ? prev_pc : cur_pc;
3214
3215 return end_prologue_addr;
3216 }
3217
3218 /* Heuristic unwinder for procedures using microMIPS instructions.
3219 Procedures that use the 32-bit instruction set are handled by the
3220 mips_insn32 unwinder. Likewise MIPS16 and the mips_insn16 unwinder. */
3221
3222 static struct mips_frame_cache *
3223 mips_micro_frame_cache (struct frame_info *this_frame, void **this_cache)
3224 {
3225 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3226 struct mips_frame_cache *cache;
3227
3228 if ((*this_cache) != NULL)
3229 return (struct mips_frame_cache *) (*this_cache);
3230
3231 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
3232 (*this_cache) = cache;
3233 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3234
3235 /* Analyze the function prologue. */
3236 {
3237 const CORE_ADDR pc = get_frame_address_in_block (this_frame);
3238 CORE_ADDR start_addr;
3239
3240 find_pc_partial_function (pc, NULL, &start_addr, NULL);
3241 if (start_addr == 0)
3242 start_addr = heuristic_proc_start (get_frame_arch (this_frame), pc);
3243 /* We can't analyze the prologue if we couldn't find the begining
3244 of the function. */
3245 if (start_addr == 0)
3246 return cache;
3247
3248 micromips_scan_prologue (gdbarch, start_addr, pc, this_frame,
3249 (struct mips_frame_cache *) *this_cache);
3250 }
3251
3252 /* gdbarch_sp_regnum contains the value and not the address. */
3253 trad_frame_set_value (cache->saved_regs,
3254 gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
3255 cache->base);
3256
3257 return (struct mips_frame_cache *) (*this_cache);
3258 }
3259
3260 static void
3261 mips_micro_frame_this_id (struct frame_info *this_frame, void **this_cache,
3262 struct frame_id *this_id)
3263 {
3264 struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3265 this_cache);
3266 /* This marks the outermost frame. */
3267 if (info->base == 0)
3268 return;
3269 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
3270 }
3271
3272 static struct value *
3273 mips_micro_frame_prev_register (struct frame_info *this_frame,
3274 void **this_cache, int regnum)
3275 {
3276 struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3277 this_cache);
3278 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3279 }
3280
3281 static int
3282 mips_micro_frame_sniffer (const struct frame_unwind *self,
3283 struct frame_info *this_frame, void **this_cache)
3284 {
3285 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3286 CORE_ADDR pc = get_frame_pc (this_frame);
3287
3288 if (mips_pc_is_micromips (gdbarch, pc))
3289 return 1;
3290 return 0;
3291 }
3292
3293 static const struct frame_unwind mips_micro_frame_unwind =
3294 {
3295 NORMAL_FRAME,
3296 default_frame_unwind_stop_reason,
3297 mips_micro_frame_this_id,
3298 mips_micro_frame_prev_register,
3299 NULL,
3300 mips_micro_frame_sniffer
3301 };
3302
3303 static CORE_ADDR
3304 mips_micro_frame_base_address (struct frame_info *this_frame,
3305 void **this_cache)
3306 {
3307 struct mips_frame_cache *info = mips_micro_frame_cache (this_frame,
3308 this_cache);
3309 return info->base;
3310 }
3311
3312 static const struct frame_base mips_micro_frame_base =
3313 {
3314 &mips_micro_frame_unwind,
3315 mips_micro_frame_base_address,
3316 mips_micro_frame_base_address,
3317 mips_micro_frame_base_address
3318 };
3319
3320 static const struct frame_base *
3321 mips_micro_frame_base_sniffer (struct frame_info *this_frame)
3322 {
3323 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3324 CORE_ADDR pc = get_frame_pc (this_frame);
3325
3326 if (mips_pc_is_micromips (gdbarch, pc))
3327 return &mips_micro_frame_base;
3328 else
3329 return NULL;
3330 }
3331
3332 /* Mark all the registers as unset in the saved_regs array
3333 of THIS_CACHE. Do nothing if THIS_CACHE is null. */
3334
3335 static void
3336 reset_saved_regs (struct gdbarch *gdbarch, struct mips_frame_cache *this_cache)
3337 {
3338 if (this_cache == NULL || this_cache->saved_regs == NULL)
3339 return;
3340
3341 {
3342 const int num_regs = gdbarch_num_regs (gdbarch);
3343 int i;
3344
3345 for (i = 0; i < num_regs; i++)
3346 {
3347 this_cache->saved_regs[i].addr = -1;
3348 }
3349 }
3350 }
3351
3352 /* Analyze the function prologue from START_PC to LIMIT_PC. Builds
3353 the associated FRAME_CACHE if not null.
3354 Return the address of the first instruction past the prologue. */
3355
3356 static CORE_ADDR
3357 mips32_scan_prologue (struct gdbarch *gdbarch,
3358 CORE_ADDR start_pc, CORE_ADDR limit_pc,
3359 struct frame_info *this_frame,
3360 struct mips_frame_cache *this_cache)
3361 {
3362 int prev_non_prologue_insn;
3363 int this_non_prologue_insn;
3364 int non_prologue_insns;
3365 CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for
3366 frame-pointer. */
3367 int prev_delay_slot;
3368 CORE_ADDR prev_pc;
3369 CORE_ADDR cur_pc;
3370 CORE_ADDR sp;
3371 long frame_offset;
3372 int frame_reg = MIPS_SP_REGNUM;
3373
3374 CORE_ADDR end_prologue_addr;
3375 int seen_sp_adjust = 0;
3376 int load_immediate_bytes = 0;
3377 int in_delay_slot;
3378 int regsize_is_64_bits = (mips_abi_regsize (gdbarch) == 8);
3379
3380 /* Can be called when there's no process, and hence when there's no
3381 THIS_FRAME. */
3382 if (this_frame != NULL)
3383 sp = get_frame_register_signed (this_frame,
3384 gdbarch_num_regs (gdbarch)
3385 + MIPS_SP_REGNUM);
3386 else
3387 sp = 0;
3388
3389 if (limit_pc > start_pc + 200)
3390 limit_pc = start_pc + 200;
3391
3392 restart:
3393 prev_non_prologue_insn = 0;
3394 non_prologue_insns = 0;
3395 prev_delay_slot = 0;
3396 prev_pc = start_pc;
3397
3398 /* Permit at most one non-prologue non-control-transfer instruction
3399 in the middle which may have been reordered by the compiler for
3400 optimisation. */
3401 frame_offset = 0;
3402 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSN32_SIZE)
3403 {
3404 unsigned long inst, high_word;
3405 long offset;
3406 int reg;
3407
3408 this_non_prologue_insn = 0;
3409 in_delay_slot = 0;
3410
3411 /* Fetch the instruction. */
3412 inst = (unsigned long) mips_fetch_instruction (gdbarch, ISA_MIPS,
3413 cur_pc, NULL);
3414
3415 /* Save some code by pre-extracting some useful fields. */
3416 high_word = (inst >> 16) & 0xffff;
3417 offset = ((inst & 0xffff) ^ 0x8000) - 0x8000;
3418 reg = high_word & 0x1f;
3419
3420 if (high_word == 0x27bd /* addiu $sp,$sp,-i */
3421 || high_word == 0x23bd /* addi $sp,$sp,-i */
3422 || high_word == 0x67bd) /* daddiu $sp,$sp,-i */
3423 {
3424 if (offset < 0) /* Negative stack adjustment? */
3425 frame_offset -= offset;
3426 else
3427 /* Exit loop if a positive stack adjustment is found, which
3428 usually means that the stack cleanup code in the function
3429 epilogue is reached. */
3430 break;
3431 seen_sp_adjust = 1;
3432 }
3433 else if (((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
3434 && !regsize_is_64_bits)
3435 {
3436 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
3437 }
3438 else if (((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
3439 && regsize_is_64_bits)
3440 {
3441 /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra. */
3442 set_reg_offset (gdbarch, this_cache, reg, sp + offset);
3443 }
3444 else if (high_word == 0x27be) /* addiu $30,$sp,size */
3445 {
3446 /* Old gcc frame, r30 is virtual frame pointer. */
3447 if (offset != frame_offset)
3448 frame_addr = sp + offset;
3449 else if (this_frame && frame_reg == MIPS_SP_REGNUM)
3450 {
3451 unsigned alloca_adjust;
3452
3453 frame_reg = 30;
3454 frame_addr = get_frame_register_signed
3455 (this_frame, gdbarch_num_regs (gdbarch) + 30);
3456 frame_offset = 0;
3457
3458 alloca_adjust = (unsigned) (frame_addr - (sp + offset));
3459 if (alloca_adjust > 0)
3460 {
3461 /* FP > SP + frame_size. This may be because of
3462 an alloca or somethings similar. Fix sp to
3463 "pre-alloca" value, and try again. */
3464 sp += alloca_adjust;
3465 /* Need to reset the status of all registers. Otherwise,
3466 we will hit a guard that prevents the new address
3467 for each register to be recomputed during the second
3468 pass. */
3469 reset_saved_regs (gdbarch, this_cache);
3470 goto restart;
3471 }
3472 }
3473 }
3474 /* move $30,$sp. With different versions of gas this will be either
3475 `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
3476 Accept any one of these. */
3477 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
3478 {
3479 /* New gcc frame, virtual frame pointer is at r30 + frame_size. */
3480 if (this_frame && frame_reg == MIPS_SP_REGNUM)
3481 {
3482 unsigned alloca_adjust;
3483
3484 frame_reg = 30;
3485 frame_addr = get_frame_register_signed
3486 (this_frame, gdbarch_num_regs (gdbarch) + 30);
3487
3488 alloca_adjust = (unsigned) (frame_addr - sp);
3489 if (alloca_adjust > 0)
3490 {
3491 /* FP > SP + frame_size. This may be because of
3492 an alloca or somethings similar. Fix sp to
3493 "pre-alloca" value, and try again. */
3494 sp = frame_addr;
3495 /* Need to reset the status of all registers. Otherwise,
3496 we will hit a guard that prevents the new address
3497 for each register to be recomputed during the second
3498 pass. */
3499 reset_saved_regs (gdbarch, this_cache);
3500 goto restart;
3501 }
3502 }
3503 }
3504 else if ((high_word & 0xFFE0) == 0xafc0 /* sw reg,offset($30) */
3505 && !regsize_is_64_bits)
3506 {
3507 set_reg_offset (gdbarch, this_cache, reg, frame_addr + offset);
3508 }
3509 else if ((high_word & 0xFFE0) == 0xE7A0 /* swc1 freg,n($sp) */
3510 || (high_word & 0xF3E0) == 0xA3C0 /* sx reg,n($s8) */
3511 || (inst & 0xFF9F07FF) == 0x00800021 /* move reg,$a0-$a3 */
3512 || high_word == 0x3c1c /* lui $gp,n */
3513 || high_word == 0x279c /* addiu $gp,$gp,n */
3514 || inst == 0x0399e021 /* addu $gp,$gp,$t9 */
3515 || inst == 0x033ce021 /* addu $gp,$t9,$gp */
3516 )
3517 {
3518 /* These instructions are part of the prologue, but we don't
3519 need to do anything special to handle them. */
3520 }
3521 /* The instructions below load $at or $t0 with an immediate
3522 value in preparation for a stack adjustment via
3523 subu $sp,$sp,[$at,$t0]. These instructions could also
3524 initialize a local variable, so we accept them only before
3525 a stack adjustment instruction was seen. */
3526 else if (!seen_sp_adjust
3527 && !prev_delay_slot
3528 && (high_word == 0x3c01 /* lui $at,n */
3529 || high_word == 0x3c08 /* lui $t0,n */
3530 || high_word == 0x3421 /* ori $at,$at,n */
3531 || high_word == 0x3508 /* ori $t0,$t0,n */
3532 || high_word == 0x3401 /* ori $at,$zero,n */
3533 || high_word == 0x3408 /* ori $t0,$zero,n */
3534 ))
3535 {
3536 load_immediate_bytes += MIPS_INSN32_SIZE; /* FIXME! */
3537 }
3538 /* Check for branches and jumps. The instruction in the delay
3539 slot can be a part of the prologue, so move forward once more. */
3540 else if (mips32_instruction_has_delay_slot (gdbarch, inst))
3541 {
3542 in_delay_slot = 1;
3543 }
3544 /* This instruction is not an instruction typically found
3545 in a prologue, so we must have reached the end of the
3546 prologue. */
3547 else
3548 {
3549 this_non_prologue_insn = 1;
3550 }
3551
3552 non_prologue_insns += this_non_prologue_insn;
3553
3554 /* A jump or branch, or enough non-prologue insns seen? If so,
3555 then we must have reached the end of the prologue by now. */
3556 if (prev_delay_slot || non_prologue_insns > 1)
3557 break;
3558
3559 prev_non_prologue_insn = this_non_prologue_insn;
3560 prev_delay_slot = in_delay_slot;
3561 prev_pc = cur_pc;
3562 }
3563
3564 if (this_cache != NULL)
3565 {
3566 this_cache->base =
3567 (get_frame_register_signed (this_frame,
3568 gdbarch_num_regs (gdbarch) + frame_reg)
3569 + frame_offset);
3570 /* FIXME: brobecker/2004-09-15: We should be able to get rid of
3571 this assignment below, eventually. But it's still needed
3572 for now. */
3573 this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3574 + mips_regnum (gdbarch)->pc]
3575 = this_cache->saved_regs[gdbarch_num_regs (gdbarch)
3576 + MIPS_RA_REGNUM];
3577 }
3578
3579 /* Set end_prologue_addr to the address of the instruction immediately
3580 after the last one we scanned. Unless the last one looked like a
3581 non-prologue instruction (and we looked ahead), in which case use
3582 its address instead. */
3583 end_prologue_addr
3584 = prev_non_prologue_insn || prev_delay_slot ? prev_pc : cur_pc;
3585
3586 /* In a frameless function, we might have incorrectly
3587 skipped some load immediate instructions. Undo the skipping
3588 if the load immediate was not followed by a stack adjustment. */
3589 if (load_immediate_bytes && !seen_sp_adjust)
3590 end_prologue_addr -= load_immediate_bytes;
3591
3592 return end_prologue_addr;
3593 }
3594
3595 /* Heuristic unwinder for procedures using 32-bit instructions (covers
3596 both 32-bit and 64-bit MIPS ISAs). Procedures using 16-bit
3597 instructions (a.k.a. MIPS16) are handled by the mips_insn16
3598 unwinder. Likewise microMIPS and the mips_micro unwinder. */
3599
3600 static struct mips_frame_cache *
3601 mips_insn32_frame_cache (struct frame_info *this_frame, void **this_cache)
3602 {
3603 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3604 struct mips_frame_cache *cache;
3605
3606 if ((*this_cache) != NULL)
3607 return (struct mips_frame_cache *) (*this_cache);
3608
3609 cache = FRAME_OBSTACK_ZALLOC (struct mips_frame_cache);
3610 (*this_cache) = cache;
3611 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3612
3613 /* Analyze the function prologue. */
3614 {
3615 const CORE_ADDR pc = get_frame_address_in_block (this_frame);
3616 CORE_ADDR start_addr;
3617
3618 find_pc_partial_function (pc, NULL, &start_addr, NULL);
3619 if (start_addr == 0)
3620 start_addr = heuristic_proc_start (gdbarch, pc);
3621 /* We can't analyze the prologue if we couldn't find the begining
3622 of the function. */
3623 if (start_addr == 0)
3624 return cache;
3625
3626 mips32_scan_prologue (gdbarch, start_addr, pc, this_frame,
3627 (struct mips_frame_cache *) *this_cache);
3628 }
3629
3630 /* gdbarch_sp_regnum contains the value and not the address. */
3631 trad_frame_set_value (cache->saved_regs,
3632 gdbarch_num_regs (gdbarch) + MIPS_SP_REGNUM,
3633 cache->base);
3634
3635 return (struct mips_frame_cache *) (*this_cache);
3636 }
3637
3638 static void
3639 mips_insn32_frame_this_id (struct frame_info *this_frame, void **this_cache,
3640 struct frame_id *this_id)
3641 {
3642 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3643 this_cache);
3644 /* This marks the outermost frame. */
3645 if (info->base == 0)
3646 return;
3647 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
3648 }
3649
3650 static struct value *
3651 mips_insn32_frame_prev_register (struct frame_info *this_frame,
3652 void **this_cache, int regnum)
3653 {
3654 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3655 this_cache);
3656 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3657 }
3658
3659 static int
3660 mips_insn32_frame_sniffer (const struct frame_unwind *self,
3661 struct frame_info *this_frame, void **this_cache)
3662 {
3663 CORE_ADDR pc = get_frame_pc (this_frame);
3664 if (mips_pc_is_mips (pc))
3665 return 1;
3666 return 0;
3667 }
3668
3669 static const struct frame_unwind mips_insn32_frame_unwind =
3670 {
3671 NORMAL_FRAME,
3672 default_frame_unwind_stop_reason,
3673 mips_insn32_frame_this_id,
3674 mips_insn32_frame_prev_register,
3675 NULL,
3676 mips_insn32_frame_sniffer
3677 };
3678
3679 static CORE_ADDR
3680 mips_insn32_frame_base_address (struct frame_info *this_frame,
3681 void **this_cache)
3682 {
3683 struct mips_frame_cache *info = mips_insn32_frame_cache (this_frame,
3684 this_cache);
3685 return info->base;
3686 }
3687
3688 static const struct frame_base mips_insn32_frame_base =
3689 {
3690 &mips_insn32_frame_unwind,
3691 mips_insn32_frame_base_address,
3692 mips_insn32_frame_base_address,
3693 mips_insn32_frame_base_address
3694 };
3695
3696 static const struct frame_base *
3697 mips_insn32_frame_base_sniffer (struct frame_info *this_frame)
3698 {
3699 CORE_ADDR pc = get_frame_pc (this_frame);
3700 if (mips_pc_is_mips (pc))
3701 return &mips_insn32_frame_base;
3702 else
3703 return NULL;
3704 }
3705
3706 static struct trad_frame_cache *
3707 mips_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
3708 {
3709 CORE_ADDR pc;
3710 CORE_ADDR start_addr;
3711 CORE_ADDR stack_addr;
3712 struct trad_frame_cache *this_trad_cache;
3713 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3714 int num_regs = gdbarch_num_regs (gdbarch);
3715
3716 if ((*this_cache) != NULL)
3717 return (struct trad_frame_cache *) (*this_cache);
3718 this_trad_cache = trad_frame_cache_zalloc (this_frame);
3719 (*this_cache) = this_trad_cache;
3720
3721 /* The return address is in the link register. */
3722 trad_frame_set_reg_realreg (this_trad_cache,
3723 gdbarch_pc_regnum (gdbarch),
3724 num_regs + MIPS_RA_REGNUM);
3725
3726 /* Frame ID, since it's a frameless / stackless function, no stack
3727 space is allocated and SP on entry is the current SP. */
3728 pc = get_frame_pc (this_frame);
3729 find_pc_partial_function (pc, NULL, &start_addr, NULL);
3730 stack_addr = get_frame_register_signed (this_frame,
3731 num_regs + MIPS_SP_REGNUM);
3732 trad_frame_set_id (this_trad_cache, frame_id_build (stack_addr, start_addr));
3733
3734 /* Assume that the frame's base is the same as the
3735 stack-pointer. */
3736 trad_frame_set_this_base (this_trad_cache, stack_addr);
3737
3738 return this_trad_cache;
3739 }
3740
3741 static void
3742 mips_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
3743 struct frame_id *this_id)
3744 {
3745 struct trad_frame_cache *this_trad_cache
3746 = mips_stub_frame_cache (this_frame, this_cache);
3747 trad_frame_get_id (this_trad_cache, this_id);
3748 }
3749
3750 static struct value *
3751 mips_stub_frame_prev_register (struct frame_info *this_frame,
3752 void **this_cache, int regnum)
3753 {
3754 struct trad_frame_cache *this_trad_cache
3755 = mips_stub_frame_cache (this_frame, this_cache);
3756 return trad_frame_get_register (this_trad_cache, this_frame, regnum);
3757 }
3758
3759 static int
3760 mips_stub_frame_sniffer (const struct frame_unwind *self,
3761 struct frame_info *this_frame, void **this_cache)
3762 {
3763 gdb_byte dummy[4];
3764 struct obj_section *s;
3765 CORE_ADDR pc = get_frame_address_in_block (this_frame);
3766 struct bound_minimal_symbol msym;
3767
3768 /* Use the stub unwinder for unreadable code. */
3769 if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
3770 return 1;
3771
3772 if (in_plt_section (pc) || in_mips_stubs_section (pc))
3773 return 1;
3774
3775 /* Calling a PIC function from a non-PIC function passes through a
3776 stub. The stub for foo is named ".pic.foo". */
3777 msym = lookup_minimal_symbol_by_pc (pc);
3778 if (msym.minsym != NULL
3779 && MSYMBOL_LINKAGE_NAME (msym.minsym) != NULL
3780 && startswith (MSYMBOL_LINKAGE_NAME (msym.minsym), ".pic."))
3781 return 1;
3782
3783 return 0;
3784 }
3785
3786 static const struct frame_unwind mips_stub_frame_unwind =
3787 {
3788 NORMAL_FRAME,
3789 default_frame_unwind_stop_reason,
3790 mips_stub_frame_this_id,
3791 mips_stub_frame_prev_register,
3792 NULL,
3793 mips_stub_frame_sniffer
3794 };
3795
3796 static CORE_ADDR
3797 mips_stub_frame_base_address (struct frame_info *this_frame,
3798 void **this_cache)
3799 {
3800 struct trad_frame_cache *this_trad_cache
3801 = mips_stub_frame_cache (this_frame, this_cache);
3802 return trad_frame_get_this_base (this_trad_cache);
3803 }
3804
3805 static const struct frame_base mips_stub_frame_base =
3806 {
3807 &mips_stub_frame_unwind,
3808 mips_stub_frame_base_address,
3809 mips_stub_frame_base_address,
3810 mips_stub_frame_base_address
3811 };
3812
3813 static const struct frame_base *
3814 mips_stub_frame_base_sniffer (struct frame_info *this_frame)
3815 {
3816 if (mips_stub_frame_sniffer (&mips_stub_frame_unwind, this_frame, NULL))
3817 return &mips_stub_frame_base;
3818 else
3819 return NULL;
3820 }
3821
3822 /* mips_addr_bits_remove - remove useless address bits */
3823
3824 static CORE_ADDR
3825 mips_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
3826 {
3827 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3828
3829 if (mips_mask_address_p (tdep) && (((ULONGEST) addr) >> 32 == 0xffffffffUL))
3830 /* This hack is a work-around for existing boards using PMON, the
3831 simulator, and any other 64-bit targets that doesn't have true
3832 64-bit addressing. On these targets, the upper 32 bits of
3833 addresses are ignored by the hardware. Thus, the PC or SP are
3834 likely to have been sign extended to all 1s by instruction
3835 sequences that load 32-bit addresses. For example, a typical
3836 piece of code that loads an address is this:
3837
3838 lui $r2, <upper 16 bits>
3839 ori $r2, <lower 16 bits>
3840
3841 But the lui sign-extends the value such that the upper 32 bits
3842 may be all 1s. The workaround is simply to mask off these
3843 bits. In the future, gcc may be changed to support true 64-bit
3844 addressing, and this masking will have to be disabled. */
3845 return addr &= 0xffffffffUL;
3846 else
3847 return addr;
3848 }
3849
3850
3851 /* Checks for an atomic sequence of instructions beginning with a LL/LLD
3852 instruction and ending with a SC/SCD instruction. If such a sequence
3853 is found, attempt to step through it. A breakpoint is placed at the end of
3854 the sequence. */
3855
3856 /* Instructions used during single-stepping of atomic sequences, standard
3857 ISA version. */
3858 #define LL_OPCODE 0x30
3859 #define LLD_OPCODE 0x34
3860 #define SC_OPCODE 0x38
3861 #define SCD_OPCODE 0x3c
3862
3863 static int
3864 mips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
3865 struct address_space *aspace, CORE_ADDR pc)
3866 {
3867 CORE_ADDR breaks[2] = {-1, -1};
3868 CORE_ADDR loc = pc;
3869 CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination. */
3870 ULONGEST insn;
3871 int insn_count;
3872 int index;
3873 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
3874 const int atomic_sequence_length = 16; /* Instruction sequence length. */
3875
3876 insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
3877 /* Assume all atomic sequences start with a ll/lld instruction. */
3878 if (itype_op (insn) != LL_OPCODE && itype_op (insn) != LLD_OPCODE)
3879 return 0;
3880
3881 /* Assume that no atomic sequence is longer than "atomic_sequence_length"
3882 instructions. */
3883 for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
3884 {
3885 int is_branch = 0;
3886 loc += MIPS_INSN32_SIZE;
3887 insn = mips_fetch_instruction (gdbarch, ISA_MIPS, loc, NULL);
3888
3889 /* Assume that there is at most one branch in the atomic
3890 sequence. If a branch is found, put a breakpoint in its
3891 destination address. */
3892 switch (itype_op (insn))
3893 {
3894 case 0: /* SPECIAL */
3895 if (rtype_funct (insn) >> 1 == 4) /* JR, JALR */
3896 return 0; /* fallback to the standard single-step code. */
3897 break;
3898 case 1: /* REGIMM */
3899 is_branch = ((itype_rt (insn) & 0xc) == 0 /* B{LT,GE}Z* */
3900 || ((itype_rt (insn) & 0x1e) == 0
3901 && itype_rs (insn) == 0)); /* BPOSGE* */
3902 break;
3903 case 2: /* J */
3904 case 3: /* JAL */
3905 return 0; /* fallback to the standard single-step code. */
3906 case 4: /* BEQ */
3907 case 5: /* BNE */
3908 case 6: /* BLEZ */
3909 case 7: /* BGTZ */
3910 case 20: /* BEQL */
3911 case 21: /* BNEL */
3912 case 22: /* BLEZL */
3913 case 23: /* BGTTL */
3914 is_branch = 1;
3915 break;
3916 case 17: /* COP1 */
3917 is_branch = ((itype_rs (insn) == 9 || itype_rs (insn) == 10)
3918 && (itype_rt (insn) & 0x2) == 0);
3919 if (is_branch) /* BC1ANY2F, BC1ANY2T, BC1ANY4F, BC1ANY4T */
3920 break;
3921 /* Fall through. */
3922 case 18: /* COP2 */
3923 case 19: /* COP3 */
3924 is_branch = (itype_rs (insn) == 8); /* BCzF, BCzFL, BCzT, BCzTL */
3925 break;
3926 }
3927 if (is_branch)
3928 {
3929 branch_bp = loc + mips32_relative_offset (insn) + 4;
3930 if (last_breakpoint >= 1)
3931 return 0; /* More than one branch found, fallback to the
3932 standard single-step code. */
3933 breaks[1] = branch_bp;
3934 last_breakpoint++;
3935 }
3936
3937 if (itype_op (insn) == SC_OPCODE || itype_op (insn) == SCD_OPCODE)
3938 break;
3939 }
3940
3941 /* Assume that the atomic sequence ends with a sc/scd instruction. */
3942 if (itype_op (insn) != SC_OPCODE && itype_op (insn) != SCD_OPCODE)
3943 return 0;
3944
3945 loc += MIPS_INSN32_SIZE;
3946
3947 /* Insert a breakpoint right after the end of the atomic sequence. */
3948 breaks[0] = loc;
3949
3950 /* Check for duplicated breakpoints. Check also for a breakpoint
3951 placed (branch instruction's destination) in the atomic sequence. */
3952 if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
3953 last_breakpoint = 0;
3954
3955 /* Effectively inserts the breakpoints. */
3956 for (index = 0; index <= last_breakpoint; index++)
3957 insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
3958
3959 return 1;
3960 }
3961
3962 static int
3963 micromips_deal_with_atomic_sequence (struct gdbarch *gdbarch,
3964 struct address_space *aspace,
3965 CORE_ADDR pc)
3966 {
3967 const int atomic_sequence_length = 16; /* Instruction sequence length. */
3968 int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed). */
3969 CORE_ADDR breaks[2] = {-1, -1};
3970 CORE_ADDR branch_bp = 0; /* Breakpoint at branch instruction's
3971 destination. */
3972 CORE_ADDR loc = pc;
3973 int sc_found = 0;
3974 ULONGEST insn;
3975 int insn_count;
3976 int index;
3977
3978 /* Assume all atomic sequences start with a ll/lld instruction. */
3979 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3980 if (micromips_op (insn) != 0x18) /* POOL32C: bits 011000 */
3981 return 0;
3982 loc += MIPS_INSN16_SIZE;
3983 insn <<= 16;
3984 insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3985 if ((b12s4_op (insn) & 0xb) != 0x3) /* LL, LLD: bits 011000 0x11 */
3986 return 0;
3987 loc += MIPS_INSN16_SIZE;
3988
3989 /* Assume all atomic sequences end with an sc/scd instruction. Assume
3990 that no atomic sequence is longer than "atomic_sequence_length"
3991 instructions. */
3992 for (insn_count = 0;
3993 !sc_found && insn_count < atomic_sequence_length;
3994 ++insn_count)
3995 {
3996 int is_branch = 0;
3997
3998 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, loc, NULL);
3999 loc += MIPS_INSN16_SIZE;
4000
4001 /* Assume that there is at most one conditional branch in the
4002 atomic sequence. If a branch is found, put a breakpoint in
4003 its destination address. */
4004 switch (mips_insn_size (ISA_MICROMIPS, insn))
4005 {
4006 /* 32-bit instructions. */
4007 case 2 * MIPS_INSN16_SIZE:
4008 switch (micromips_op (insn))
4009 {
4010 case 0x10: /* POOL32I: bits 010000 */
4011 if ((b5s5_op (insn) & 0x18) != 0x0
4012 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
4013 /* BLEZ, BNEZC, BGTZ, BEQZC: 010000 001xx */
4014 && (b5s5_op (insn) & 0x1d) != 0x11
4015 /* BLTZALS, BGEZALS: bits 010000 100x1 */
4016 && ((b5s5_op (insn) & 0x1e) != 0x14
4017 || (insn & 0x3) != 0x0)
4018 /* BC2F, BC2T: bits 010000 1010x xxx00 */
4019 && (b5s5_op (insn) & 0x1e) != 0x1a
4020 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
4021 && ((b5s5_op (insn) & 0x1e) != 0x1c
4022 || (insn & 0x3) != 0x0)
4023 /* BC1F, BC1T: bits 010000 1110x xxx00 */
4024 && ((b5s5_op (insn) & 0x1c) != 0x1c
4025 || (insn & 0x3) != 0x1))
4026 /* BC1ANY*: bits 010000 111xx xxx01 */
4027 break;
4028 /* Fall through. */
4029
4030 case 0x25: /* BEQ: bits 100101 */
4031 case 0x2d: /* BNE: bits 101101 */
4032 insn <<= 16;
4033 insn |= mips_fetch_instruction (gdbarch,
4034 ISA_MICROMIPS, loc, NULL);
4035 branch_bp = (loc + MIPS_INSN16_SIZE
4036 + micromips_relative_offset16 (insn));
4037 is_branch = 1;
4038 break;
4039
4040 case 0x00: /* POOL32A: bits 000000 */
4041 insn <<= 16;
4042 insn |= mips_fetch_instruction (gdbarch,
4043 ISA_MICROMIPS, loc, NULL);
4044 if (b0s6_op (insn) != 0x3c
4045 /* POOL32Axf: bits 000000 ... 111100 */
4046 || (b6s10_ext (insn) & 0x2bf) != 0x3c)
4047 /* JALR, JALR.HB: 000000 000x111100 111100 */
4048 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
4049 break;
4050 /* Fall through. */
4051
4052 case 0x1d: /* JALS: bits 011101 */
4053 case 0x35: /* J: bits 110101 */
4054 case 0x3d: /* JAL: bits 111101 */
4055 case 0x3c: /* JALX: bits 111100 */
4056 return 0; /* Fall back to the standard single-step code. */
4057
4058 case 0x18: /* POOL32C: bits 011000 */
4059 if ((b12s4_op (insn) & 0xb) == 0xb)
4060 /* SC, SCD: bits 011000 1x11 */
4061 sc_found = 1;
4062 break;
4063 }
4064 loc += MIPS_INSN16_SIZE;
4065 break;
4066
4067 /* 16-bit instructions. */
4068 case MIPS_INSN16_SIZE:
4069 switch (micromips_op (insn))
4070 {
4071 case 0x23: /* BEQZ16: bits 100011 */
4072 case 0x2b: /* BNEZ16: bits 101011 */
4073 branch_bp = loc + micromips_relative_offset7 (insn);
4074 is_branch = 1;
4075 break;
4076
4077 case 0x11: /* POOL16C: bits 010001 */
4078 if ((b5s5_op (insn) & 0x1c) != 0xc
4079 /* JR16, JRC, JALR16, JALRS16: 010001 011xx */
4080 && b5s5_op (insn) != 0x18)
4081 /* JRADDIUSP: bits 010001 11000 */
4082 break;
4083 return 0; /* Fall back to the standard single-step code. */
4084
4085 case 0x33: /* B16: bits 110011 */
4086 return 0; /* Fall back to the standard single-step code. */
4087 }
4088 break;
4089 }
4090 if (is_branch)
4091 {
4092 if (last_breakpoint >= 1)
4093 return 0; /* More than one branch found, fallback to the
4094 standard single-step code. */
4095 breaks[1] = branch_bp;
4096 last_breakpoint++;
4097 }
4098 }
4099 if (!sc_found)
4100 return 0;
4101
4102 /* Insert a breakpoint right after the end of the atomic sequence. */
4103 breaks[0] = loc;
4104
4105 /* Check for duplicated breakpoints. Check also for a breakpoint
4106 placed (branch instruction's destination) in the atomic sequence */
4107 if (last_breakpoint && pc <= breaks[1] && breaks[1] <= breaks[0])
4108 last_breakpoint = 0;
4109
4110 /* Effectively inserts the breakpoints. */
4111 for (index = 0; index <= last_breakpoint; index++)
4112 insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
4113
4114 return 1;
4115 }
4116
4117 static int
4118 deal_with_atomic_sequence (struct gdbarch *gdbarch,
4119 struct address_space *aspace, CORE_ADDR pc)
4120 {
4121 if (mips_pc_is_mips (pc))
4122 return mips_deal_with_atomic_sequence (gdbarch, aspace, pc);
4123 else if (mips_pc_is_micromips (gdbarch, pc))
4124 return micromips_deal_with_atomic_sequence (gdbarch, aspace, pc);
4125 else
4126 return 0;
4127 }
4128
4129 /* mips_software_single_step() is called just before we want to resume
4130 the inferior, if we want to single-step it but there is no hardware
4131 or kernel single-step support (MIPS on GNU/Linux for example). We find
4132 the target of the coming instruction and breakpoint it. */
4133
4134 int
4135 mips_software_single_step (struct frame_info *frame)
4136 {
4137 struct gdbarch *gdbarch = get_frame_arch (frame);
4138 struct address_space *aspace = get_frame_address_space (frame);
4139 CORE_ADDR pc, next_pc;
4140
4141 pc = get_frame_pc (frame);
4142 if (deal_with_atomic_sequence (gdbarch, aspace, pc))
4143 return 1;
4144
4145 next_pc = mips_next_pc (frame, pc);
4146
4147 insert_single_step_breakpoint (gdbarch, aspace, next_pc);
4148 return 1;
4149 }
4150
4151 /* Test whether the PC points to the return instruction at the
4152 end of a function. */
4153
4154 static int
4155 mips_about_to_return (struct gdbarch *gdbarch, CORE_ADDR pc)
4156 {
4157 ULONGEST insn;
4158 ULONGEST hint;
4159
4160 /* This used to check for MIPS16, but this piece of code is never
4161 called for MIPS16 functions. And likewise microMIPS ones. */
4162 gdb_assert (mips_pc_is_mips (pc));
4163
4164 insn = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
4165 hint = 0x7c0;
4166 return (insn & ~hint) == 0x3e00008; /* jr(.hb) $ra */
4167 }
4168
4169
4170 /* This fencepost looks highly suspicious to me. Removing it also
4171 seems suspicious as it could affect remote debugging across serial
4172 lines. */
4173
4174 static CORE_ADDR
4175 heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
4176 {
4177 CORE_ADDR start_pc;
4178 CORE_ADDR fence;
4179 int instlen;
4180 int seen_adjsp = 0;
4181 struct inferior *inf;
4182
4183 pc = gdbarch_addr_bits_remove (gdbarch, pc);
4184 start_pc = pc;
4185 fence = start_pc - heuristic_fence_post;
4186 if (start_pc == 0)
4187 return 0;
4188
4189 if (heuristic_fence_post == -1 || fence < VM_MIN_ADDRESS)
4190 fence = VM_MIN_ADDRESS;
4191
4192 instlen = mips_pc_is_mips (pc) ? MIPS_INSN32_SIZE : MIPS_INSN16_SIZE;
4193
4194 inf = current_inferior ();
4195
4196 /* Search back for previous return. */
4197 for (start_pc -= instlen;; start_pc -= instlen)
4198 if (start_pc < fence)
4199 {
4200 /* It's not clear to me why we reach this point when
4201 stop_soon, but with this test, at least we
4202 don't print out warnings for every child forked (eg, on
4203 decstation). 22apr93 rich@cygnus.com. */
4204 if (inf->control.stop_soon == NO_STOP_QUIETLY)
4205 {
4206 static int blurb_printed = 0;
4207
4208 warning (_("GDB can't find the start of the function at %s."),
4209 paddress (gdbarch, pc));
4210
4211 if (!blurb_printed)
4212 {
4213 /* This actually happens frequently in embedded
4214 development, when you first connect to a board
4215 and your stack pointer and pc are nowhere in
4216 particular. This message needs to give people
4217 in that situation enough information to
4218 determine that it's no big deal. */
4219 printf_filtered ("\n\
4220 GDB is unable to find the start of the function at %s\n\
4221 and thus can't determine the size of that function's stack frame.\n\
4222 This means that GDB may be unable to access that stack frame, or\n\
4223 the frames below it.\n\
4224 This problem is most likely caused by an invalid program counter or\n\
4225 stack pointer.\n\
4226 However, if you think GDB should simply search farther back\n\
4227 from %s for code which looks like the beginning of a\n\
4228 function, you can increase the range of the search using the `set\n\
4229 heuristic-fence-post' command.\n",
4230 paddress (gdbarch, pc), paddress (gdbarch, pc));
4231 blurb_printed = 1;
4232 }
4233 }
4234
4235 return 0;
4236 }
4237 else if (mips_pc_is_mips16 (gdbarch, start_pc))
4238 {
4239 unsigned short inst;
4240
4241 /* On MIPS16, any one of the following is likely to be the
4242 start of a function:
4243 extend save
4244 save
4245 entry
4246 addiu sp,-n
4247 daddiu sp,-n
4248 extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n'. */
4249 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, start_pc, NULL);
4250 if ((inst & 0xff80) == 0x6480) /* save */
4251 {
4252 if (start_pc - instlen >= fence)
4253 {
4254 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16,
4255 start_pc - instlen, NULL);
4256 if ((inst & 0xf800) == 0xf000) /* extend */
4257 start_pc -= instlen;
4258 }
4259 break;
4260 }
4261 else if (((inst & 0xf81f) == 0xe809
4262 && (inst & 0x700) != 0x700) /* entry */
4263 || (inst & 0xff80) == 0x6380 /* addiu sp,-n */
4264 || (inst & 0xff80) == 0xfb80 /* daddiu sp,-n */
4265 || ((inst & 0xf810) == 0xf010 && seen_adjsp)) /* extend -n */
4266 break;
4267 else if ((inst & 0xff00) == 0x6300 /* addiu sp */
4268 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
4269 seen_adjsp = 1;
4270 else
4271 seen_adjsp = 0;
4272 }
4273 else if (mips_pc_is_micromips (gdbarch, start_pc))
4274 {
4275 ULONGEST insn;
4276 int stop = 0;
4277 long offset;
4278 int dreg;
4279 int sreg;
4280
4281 /* On microMIPS, any one of the following is likely to be the
4282 start of a function:
4283 ADDIUSP -imm
4284 (D)ADDIU $sp, -imm
4285 LUI $gp, imm */
4286 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
4287 switch (micromips_op (insn))
4288 {
4289 case 0xc: /* ADDIU: bits 001100 */
4290 case 0x17: /* DADDIU: bits 010111 */
4291 sreg = b0s5_reg (insn);
4292 dreg = b5s5_reg (insn);
4293 insn <<= 16;
4294 insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS,
4295 pc + MIPS_INSN16_SIZE, NULL);
4296 offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
4297 if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
4298 /* (D)ADDIU $sp, imm */
4299 && offset < 0)
4300 stop = 1;
4301 break;
4302
4303 case 0x10: /* POOL32I: bits 010000 */
4304 if (b5s5_op (insn) == 0xd
4305 /* LUI: bits 010000 001101 */
4306 && b0s5_reg (insn >> 16) == 28)
4307 /* LUI $gp, imm */
4308 stop = 1;
4309 break;
4310
4311 case 0x13: /* POOL16D: bits 010011 */
4312 if ((insn & 0x1) == 0x1)
4313 /* ADDIUSP: bits 010011 1 */
4314 {
4315 offset = micromips_decode_imm9 (b1s9_imm (insn));
4316 if (offset < 0)
4317 /* ADDIUSP -imm */
4318 stop = 1;
4319 }
4320 else
4321 /* ADDIUS5: bits 010011 0 */
4322 {
4323 dreg = b5s5_reg (insn);
4324 offset = (b1s4_imm (insn) ^ 8) - 8;
4325 if (dreg == MIPS_SP_REGNUM && offset < 0)
4326 /* ADDIUS5 $sp, -imm */
4327 stop = 1;
4328 }
4329 break;
4330 }
4331 if (stop)
4332 break;
4333 }
4334 else if (mips_about_to_return (gdbarch, start_pc))
4335 {
4336 /* Skip return and its delay slot. */
4337 start_pc += 2 * MIPS_INSN32_SIZE;
4338 break;
4339 }
4340
4341 return start_pc;
4342 }
4343
4344 struct mips_objfile_private
4345 {
4346 bfd_size_type size;
4347 char *contents;
4348 };
4349
4350 /* According to the current ABI, should the type be passed in a
4351 floating-point register (assuming that there is space)? When there
4352 is no FPU, FP are not even considered as possible candidates for
4353 FP registers and, consequently this returns false - forces FP
4354 arguments into integer registers. */
4355
4356 static int
4357 fp_register_arg_p (struct gdbarch *gdbarch, enum type_code typecode,
4358 struct type *arg_type)
4359 {
4360 return ((typecode == TYPE_CODE_FLT
4361 || (MIPS_EABI (gdbarch)
4362 && (typecode == TYPE_CODE_STRUCT
4363 || typecode == TYPE_CODE_UNION)
4364 && TYPE_NFIELDS (arg_type) == 1
4365 && TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (arg_type, 0)))
4366 == TYPE_CODE_FLT))
4367 && MIPS_FPU_TYPE(gdbarch) != MIPS_FPU_NONE);
4368 }
4369
4370 /* On o32, argument passing in GPRs depends on the alignment of the type being
4371 passed. Return 1 if this type must be aligned to a doubleword boundary. */
4372
4373 static int
4374 mips_type_needs_double_align (struct type *type)
4375 {
4376 enum type_code typecode = TYPE_CODE (type);
4377
4378 if (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
4379 return 1;
4380 else if (typecode == TYPE_CODE_STRUCT)
4381 {
4382 if (TYPE_NFIELDS (type) < 1)
4383 return 0;
4384 return mips_type_needs_double_align (TYPE_FIELD_TYPE (type, 0));
4385 }
4386 else if (typecode == TYPE_CODE_UNION)
4387 {
4388 int i, n;
4389
4390 n = TYPE_NFIELDS (type);
4391 for (i = 0; i < n; i++)
4392 if (mips_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
4393 return 1;
4394 return 0;
4395 }
4396 return 0;
4397 }
4398
4399 /* Adjust the address downward (direction of stack growth) so that it
4400 is correctly aligned for a new stack frame. */
4401 static CORE_ADDR
4402 mips_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
4403 {
4404 return align_down (addr, 16);
4405 }
4406
4407 /* Implement the "push_dummy_code" gdbarch method. */
4408
4409 static CORE_ADDR
4410 mips_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
4411 CORE_ADDR funaddr, struct value **args,
4412 int nargs, struct type *value_type,
4413 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
4414 struct regcache *regcache)
4415 {
4416 static gdb_byte nop_insn[] = { 0, 0, 0, 0 };
4417 CORE_ADDR nop_addr;
4418 CORE_ADDR bp_slot;
4419
4420 /* Reserve enough room on the stack for our breakpoint instruction. */
4421 bp_slot = sp - sizeof (nop_insn);
4422
4423 /* Return to microMIPS mode if calling microMIPS code to avoid
4424 triggering an address error exception on processors that only
4425 support microMIPS execution. */
4426 *bp_addr = (mips_pc_is_micromips (gdbarch, funaddr)
4427 ? make_compact_addr (bp_slot) : bp_slot);
4428
4429 /* The breakpoint layer automatically adjusts the address of
4430 breakpoints inserted in a branch delay slot. With enough
4431 bad luck, the 4 bytes located just before our breakpoint
4432 instruction could look like a branch instruction, and thus
4433 trigger the adjustement, and break the function call entirely.
4434 So, we reserve those 4 bytes and write a nop instruction
4435 to prevent that from happening. */
4436 nop_addr = bp_slot - sizeof (nop_insn);
4437 write_memory (nop_addr, nop_insn, sizeof (nop_insn));
4438 sp = mips_frame_align (gdbarch, nop_addr);
4439
4440 /* Inferior resumes at the function entry point. */
4441 *real_pc = funaddr;
4442
4443 return sp;
4444 }
4445
4446 static CORE_ADDR
4447 mips_eabi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4448 struct regcache *regcache, CORE_ADDR bp_addr,
4449 int nargs, struct value **args, CORE_ADDR sp,
4450 int struct_return, CORE_ADDR struct_addr)
4451 {
4452 int argreg;
4453 int float_argreg;
4454 int argnum;
4455 int len = 0;
4456 int stack_offset = 0;
4457 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4458 CORE_ADDR func_addr = find_function_addr (function, NULL);
4459 int regsize = mips_abi_regsize (gdbarch);
4460
4461 /* For shared libraries, "t9" needs to point at the function
4462 address. */
4463 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
4464
4465 /* Set the return address register to point to the entry point of
4466 the program, where a breakpoint lies in wait. */
4467 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
4468
4469 /* First ensure that the stack and structure return address (if any)
4470 are properly aligned. The stack has to be at least 64-bit
4471 aligned even on 32-bit machines, because doubles must be 64-bit
4472 aligned. For n32 and n64, stack frames need to be 128-bit
4473 aligned, so we round to this widest known alignment. */
4474
4475 sp = align_down (sp, 16);
4476 struct_addr = align_down (struct_addr, 16);
4477
4478 /* Now make space on the stack for the args. We allocate more
4479 than necessary for EABI, because the first few arguments are
4480 passed in registers, but that's OK. */
4481 for (argnum = 0; argnum < nargs; argnum++)
4482 len += align_up (TYPE_LENGTH (value_type (args[argnum])), regsize);
4483 sp -= align_up (len, 16);
4484
4485 if (mips_debug)
4486 fprintf_unfiltered (gdb_stdlog,
4487 "mips_eabi_push_dummy_call: sp=%s allocated %ld\n",
4488 paddress (gdbarch, sp), (long) align_up (len, 16));
4489
4490 /* Initialize the integer and float register pointers. */
4491 argreg = MIPS_A0_REGNUM;
4492 float_argreg = mips_fpa0_regnum (gdbarch);
4493
4494 /* The struct_return pointer occupies the first parameter-passing reg. */
4495 if (struct_return)
4496 {
4497 if (mips_debug)
4498 fprintf_unfiltered (gdb_stdlog,
4499 "mips_eabi_push_dummy_call: "
4500 "struct_return reg=%d %s\n",
4501 argreg, paddress (gdbarch, struct_addr));
4502 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
4503 }
4504
4505 /* Now load as many as possible of the first arguments into
4506 registers, and push the rest onto the stack. Loop thru args
4507 from first to last. */
4508 for (argnum = 0; argnum < nargs; argnum++)
4509 {
4510 const gdb_byte *val;
4511 gdb_byte valbuf[MAX_REGISTER_SIZE];
4512 struct value *arg = args[argnum];
4513 struct type *arg_type = check_typedef (value_type (arg));
4514 int len = TYPE_LENGTH (arg_type);
4515 enum type_code typecode = TYPE_CODE (arg_type);
4516
4517 if (mips_debug)
4518 fprintf_unfiltered (gdb_stdlog,
4519 "mips_eabi_push_dummy_call: %d len=%d type=%d",
4520 argnum + 1, len, (int) typecode);
4521
4522 /* The EABI passes structures that do not fit in a register by
4523 reference. */
4524 if (len > regsize
4525 && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
4526 {
4527 store_unsigned_integer (valbuf, regsize, byte_order,
4528 value_address (arg));
4529 typecode = TYPE_CODE_PTR;
4530 len = regsize;
4531 val = valbuf;
4532 if (mips_debug)
4533 fprintf_unfiltered (gdb_stdlog, " push");
4534 }
4535 else
4536 val = value_contents (arg);
4537
4538 /* 32-bit ABIs always start floating point arguments in an
4539 even-numbered floating point register. Round the FP register
4540 up before the check to see if there are any FP registers
4541 left. Non MIPS_EABI targets also pass the FP in the integer
4542 registers so also round up normal registers. */
4543 if (regsize < 8 && fp_register_arg_p (gdbarch, typecode, arg_type))
4544 {
4545 if ((float_argreg & 1))
4546 float_argreg++;
4547 }
4548
4549 /* Floating point arguments passed in registers have to be
4550 treated specially. On 32-bit architectures, doubles
4551 are passed in register pairs; the even register gets
4552 the low word, and the odd register gets the high word.
4553 On non-EABI processors, the first two floating point arguments are
4554 also copied to general registers, because MIPS16 functions
4555 don't use float registers for arguments. This duplication of
4556 arguments in general registers can't hurt non-MIPS16 functions
4557 because those registers are normally skipped. */
4558 /* MIPS_EABI squeezes a struct that contains a single floating
4559 point value into an FP register instead of pushing it onto the
4560 stack. */
4561 if (fp_register_arg_p (gdbarch, typecode, arg_type)
4562 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
4563 {
4564 /* EABI32 will pass doubles in consecutive registers, even on
4565 64-bit cores. At one time, we used to check the size of
4566 `float_argreg' to determine whether or not to pass doubles
4567 in consecutive registers, but this is not sufficient for
4568 making the ABI determination. */
4569 if (len == 8 && mips_abi (gdbarch) == MIPS_ABI_EABI32)
4570 {
4571 int low_offset = gdbarch_byte_order (gdbarch)
4572 == BFD_ENDIAN_BIG ? 4 : 0;
4573 long regval;
4574
4575 /* Write the low word of the double to the even register(s). */
4576 regval = extract_signed_integer (val + low_offset,
4577 4, byte_order);
4578 if (mips_debug)
4579 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4580 float_argreg, phex (regval, 4));
4581 regcache_cooked_write_signed (regcache, float_argreg++, regval);
4582
4583 /* Write the high word of the double to the odd register(s). */
4584 regval = extract_signed_integer (val + 4 - low_offset,
4585 4, byte_order);
4586 if (mips_debug)
4587 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4588 float_argreg, phex (regval, 4));
4589 regcache_cooked_write_signed (regcache, float_argreg++, regval);
4590 }
4591 else
4592 {
4593 /* This is a floating point value that fits entirely
4594 in a single register. */
4595 /* On 32 bit ABI's the float_argreg is further adjusted
4596 above to ensure that it is even register aligned. */
4597 LONGEST regval = extract_signed_integer (val, len, byte_order);
4598 if (mips_debug)
4599 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4600 float_argreg, phex (regval, len));
4601 regcache_cooked_write_signed (regcache, float_argreg++, regval);
4602 }
4603 }
4604 else
4605 {
4606 /* Copy the argument to general registers or the stack in
4607 register-sized pieces. Large arguments are split between
4608 registers and stack. */
4609 /* Note: structs whose size is not a multiple of regsize
4610 are treated specially: Irix cc passes
4611 them in registers where gcc sometimes puts them on the
4612 stack. For maximum compatibility, we will put them in
4613 both places. */
4614 int odd_sized_struct = (len > regsize && len % regsize != 0);
4615
4616 /* Note: Floating-point values that didn't fit into an FP
4617 register are only written to memory. */
4618 while (len > 0)
4619 {
4620 /* Remember if the argument was written to the stack. */
4621 int stack_used_p = 0;
4622 int partial_len = (len < regsize ? len : regsize);
4623
4624 if (mips_debug)
4625 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4626 partial_len);
4627
4628 /* Write this portion of the argument to the stack. */
4629 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
4630 || odd_sized_struct
4631 || fp_register_arg_p (gdbarch, typecode, arg_type))
4632 {
4633 /* Should shorter than int integer values be
4634 promoted to int before being stored? */
4635 int longword_offset = 0;
4636 CORE_ADDR addr;
4637 stack_used_p = 1;
4638 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4639 {
4640 if (regsize == 8
4641 && (typecode == TYPE_CODE_INT
4642 || typecode == TYPE_CODE_PTR
4643 || typecode == TYPE_CODE_FLT) && len <= 4)
4644 longword_offset = regsize - len;
4645 else if ((typecode == TYPE_CODE_STRUCT
4646 || typecode == TYPE_CODE_UNION)
4647 && TYPE_LENGTH (arg_type) < regsize)
4648 longword_offset = regsize - len;
4649 }
4650
4651 if (mips_debug)
4652 {
4653 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
4654 paddress (gdbarch, stack_offset));
4655 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
4656 paddress (gdbarch, longword_offset));
4657 }
4658
4659 addr = sp + stack_offset + longword_offset;
4660
4661 if (mips_debug)
4662 {
4663 int i;
4664 fprintf_unfiltered (gdb_stdlog, " @%s ",
4665 paddress (gdbarch, addr));
4666 for (i = 0; i < partial_len; i++)
4667 {
4668 fprintf_unfiltered (gdb_stdlog, "%02x",
4669 val[i] & 0xff);
4670 }
4671 }
4672 write_memory (addr, val, partial_len);
4673 }
4674
4675 /* Note!!! This is NOT an else clause. Odd sized
4676 structs may go thru BOTH paths. Floating point
4677 arguments will not. */
4678 /* Write this portion of the argument to a general
4679 purpose register. */
4680 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch)
4681 && !fp_register_arg_p (gdbarch, typecode, arg_type))
4682 {
4683 LONGEST regval =
4684 extract_signed_integer (val, partial_len, byte_order);
4685
4686 if (mips_debug)
4687 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
4688 argreg,
4689 phex (regval, regsize));
4690 regcache_cooked_write_signed (regcache, argreg, regval);
4691 argreg++;
4692 }
4693
4694 len -= partial_len;
4695 val += partial_len;
4696
4697 /* Compute the offset into the stack at which we will
4698 copy the next parameter.
4699
4700 In the new EABI (and the NABI32), the stack_offset
4701 only needs to be adjusted when it has been used. */
4702
4703 if (stack_used_p)
4704 stack_offset += align_up (partial_len, regsize);
4705 }
4706 }
4707 if (mips_debug)
4708 fprintf_unfiltered (gdb_stdlog, "\n");
4709 }
4710
4711 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
4712
4713 /* Return adjusted stack pointer. */
4714 return sp;
4715 }
4716
4717 /* Determine the return value convention being used. */
4718
4719 static enum return_value_convention
4720 mips_eabi_return_value (struct gdbarch *gdbarch, struct value *function,
4721 struct type *type, struct regcache *regcache,
4722 gdb_byte *readbuf, const gdb_byte *writebuf)
4723 {
4724 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4725 int fp_return_type = 0;
4726 int offset, regnum, xfer;
4727
4728 if (TYPE_LENGTH (type) > 2 * mips_abi_regsize (gdbarch))
4729 return RETURN_VALUE_STRUCT_CONVENTION;
4730
4731 /* Floating point type? */
4732 if (tdep->mips_fpu_type != MIPS_FPU_NONE)
4733 {
4734 if (TYPE_CODE (type) == TYPE_CODE_FLT)
4735 fp_return_type = 1;
4736 /* Structs with a single field of float type
4737 are returned in a floating point register. */
4738 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
4739 || TYPE_CODE (type) == TYPE_CODE_UNION)
4740 && TYPE_NFIELDS (type) == 1)
4741 {
4742 struct type *fieldtype = TYPE_FIELD_TYPE (type, 0);
4743
4744 if (TYPE_CODE (check_typedef (fieldtype)) == TYPE_CODE_FLT)
4745 fp_return_type = 1;
4746 }
4747 }
4748
4749 if (fp_return_type)
4750 {
4751 /* A floating-point value belongs in the least significant part
4752 of FP0/FP1. */
4753 if (mips_debug)
4754 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
4755 regnum = mips_regnum (gdbarch)->fp0;
4756 }
4757 else
4758 {
4759 /* An integer value goes in V0/V1. */
4760 if (mips_debug)
4761 fprintf_unfiltered (gdb_stderr, "Return scalar in $v0\n");
4762 regnum = MIPS_V0_REGNUM;
4763 }
4764 for (offset = 0;
4765 offset < TYPE_LENGTH (type);
4766 offset += mips_abi_regsize (gdbarch), regnum++)
4767 {
4768 xfer = mips_abi_regsize (gdbarch);
4769 if (offset + xfer > TYPE_LENGTH (type))
4770 xfer = TYPE_LENGTH (type) - offset;
4771 mips_xfer_register (gdbarch, regcache,
4772 gdbarch_num_regs (gdbarch) + regnum, xfer,
4773 gdbarch_byte_order (gdbarch), readbuf, writebuf,
4774 offset);
4775 }
4776
4777 return RETURN_VALUE_REGISTER_CONVENTION;
4778 }
4779
4780
4781 /* N32/N64 ABI stuff. */
4782
4783 /* Search for a naturally aligned double at OFFSET inside a struct
4784 ARG_TYPE. The N32 / N64 ABIs pass these in floating point
4785 registers. */
4786
4787 static int
4788 mips_n32n64_fp_arg_chunk_p (struct gdbarch *gdbarch, struct type *arg_type,
4789 int offset)
4790 {
4791 int i;
4792
4793 if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
4794 return 0;
4795
4796 if (MIPS_FPU_TYPE (gdbarch) != MIPS_FPU_DOUBLE)
4797 return 0;
4798
4799 if (TYPE_LENGTH (arg_type) < offset + MIPS64_REGSIZE)
4800 return 0;
4801
4802 for (i = 0; i < TYPE_NFIELDS (arg_type); i++)
4803 {
4804 int pos;
4805 struct type *field_type;
4806
4807 /* We're only looking at normal fields. */
4808 if (field_is_static (&TYPE_FIELD (arg_type, i))
4809 || (TYPE_FIELD_BITPOS (arg_type, i) % 8) != 0)
4810 continue;
4811
4812 /* If we have gone past the offset, there is no double to pass. */
4813 pos = TYPE_FIELD_BITPOS (arg_type, i) / 8;
4814 if (pos > offset)
4815 return 0;
4816
4817 field_type = check_typedef (TYPE_FIELD_TYPE (arg_type, i));
4818
4819 /* If this field is entirely before the requested offset, go
4820 on to the next one. */
4821 if (pos + TYPE_LENGTH (field_type) <= offset)
4822 continue;
4823
4824 /* If this is our special aligned double, we can stop. */
4825 if (TYPE_CODE (field_type) == TYPE_CODE_FLT
4826 && TYPE_LENGTH (field_type) == MIPS64_REGSIZE)
4827 return 1;
4828
4829 /* This field starts at or before the requested offset, and
4830 overlaps it. If it is a structure, recurse inwards. */
4831 return mips_n32n64_fp_arg_chunk_p (gdbarch, field_type, offset - pos);
4832 }
4833
4834 return 0;
4835 }
4836
4837 static CORE_ADDR
4838 mips_n32n64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
4839 struct regcache *regcache, CORE_ADDR bp_addr,
4840 int nargs, struct value **args, CORE_ADDR sp,
4841 int struct_return, CORE_ADDR struct_addr)
4842 {
4843 int argreg;
4844 int float_argreg;
4845 int argnum;
4846 int len = 0;
4847 int stack_offset = 0;
4848 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4849 CORE_ADDR func_addr = find_function_addr (function, NULL);
4850
4851 /* For shared libraries, "t9" needs to point at the function
4852 address. */
4853 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
4854
4855 /* Set the return address register to point to the entry point of
4856 the program, where a breakpoint lies in wait. */
4857 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
4858
4859 /* First ensure that the stack and structure return address (if any)
4860 are properly aligned. The stack has to be at least 64-bit
4861 aligned even on 32-bit machines, because doubles must be 64-bit
4862 aligned. For n32 and n64, stack frames need to be 128-bit
4863 aligned, so we round to this widest known alignment. */
4864
4865 sp = align_down (sp, 16);
4866 struct_addr = align_down (struct_addr, 16);
4867
4868 /* Now make space on the stack for the args. */
4869 for (argnum = 0; argnum < nargs; argnum++)
4870 len += align_up (TYPE_LENGTH (value_type (args[argnum])), MIPS64_REGSIZE);
4871 sp -= align_up (len, 16);
4872
4873 if (mips_debug)
4874 fprintf_unfiltered (gdb_stdlog,
4875 "mips_n32n64_push_dummy_call: sp=%s allocated %ld\n",
4876 paddress (gdbarch, sp), (long) align_up (len, 16));
4877
4878 /* Initialize the integer and float register pointers. */
4879 argreg = MIPS_A0_REGNUM;
4880 float_argreg = mips_fpa0_regnum (gdbarch);
4881
4882 /* The struct_return pointer occupies the first parameter-passing reg. */
4883 if (struct_return)
4884 {
4885 if (mips_debug)
4886 fprintf_unfiltered (gdb_stdlog,
4887 "mips_n32n64_push_dummy_call: "
4888 "struct_return reg=%d %s\n",
4889 argreg, paddress (gdbarch, struct_addr));
4890 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
4891 }
4892
4893 /* Now load as many as possible of the first arguments into
4894 registers, and push the rest onto the stack. Loop thru args
4895 from first to last. */
4896 for (argnum = 0; argnum < nargs; argnum++)
4897 {
4898 const gdb_byte *val;
4899 struct value *arg = args[argnum];
4900 struct type *arg_type = check_typedef (value_type (arg));
4901 int len = TYPE_LENGTH (arg_type);
4902 enum type_code typecode = TYPE_CODE (arg_type);
4903
4904 if (mips_debug)
4905 fprintf_unfiltered (gdb_stdlog,
4906 "mips_n32n64_push_dummy_call: %d len=%d type=%d",
4907 argnum + 1, len, (int) typecode);
4908
4909 val = value_contents (arg);
4910
4911 /* A 128-bit long double value requires an even-odd pair of
4912 floating-point registers. */
4913 if (len == 16
4914 && fp_register_arg_p (gdbarch, typecode, arg_type)
4915 && (float_argreg & 1))
4916 {
4917 float_argreg++;
4918 argreg++;
4919 }
4920
4921 if (fp_register_arg_p (gdbarch, typecode, arg_type)
4922 && argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
4923 {
4924 /* This is a floating point value that fits entirely
4925 in a single register or a pair of registers. */
4926 int reglen = (len <= MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
4927 LONGEST regval = extract_unsigned_integer (val, reglen, byte_order);
4928 if (mips_debug)
4929 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4930 float_argreg, phex (regval, reglen));
4931 regcache_cooked_write_unsigned (regcache, float_argreg, regval);
4932
4933 if (mips_debug)
4934 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4935 argreg, phex (regval, reglen));
4936 regcache_cooked_write_unsigned (regcache, argreg, regval);
4937 float_argreg++;
4938 argreg++;
4939 if (len == 16)
4940 {
4941 regval = extract_unsigned_integer (val + reglen,
4942 reglen, byte_order);
4943 if (mips_debug)
4944 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
4945 float_argreg, phex (regval, reglen));
4946 regcache_cooked_write_unsigned (regcache, float_argreg, regval);
4947
4948 if (mips_debug)
4949 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
4950 argreg, phex (regval, reglen));
4951 regcache_cooked_write_unsigned (regcache, argreg, regval);
4952 float_argreg++;
4953 argreg++;
4954 }
4955 }
4956 else
4957 {
4958 /* Copy the argument to general registers or the stack in
4959 register-sized pieces. Large arguments are split between
4960 registers and stack. */
4961 /* For N32/N64, structs, unions, or other composite types are
4962 treated as a sequence of doublewords, and are passed in integer
4963 or floating point registers as though they were simple scalar
4964 parameters to the extent that they fit, with any excess on the
4965 stack packed according to the normal memory layout of the
4966 object.
4967 The caller does not reserve space for the register arguments;
4968 the callee is responsible for reserving it if required. */
4969 /* Note: Floating-point values that didn't fit into an FP
4970 register are only written to memory. */
4971 while (len > 0)
4972 {
4973 /* Remember if the argument was written to the stack. */
4974 int stack_used_p = 0;
4975 int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
4976
4977 if (mips_debug)
4978 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
4979 partial_len);
4980
4981 if (fp_register_arg_p (gdbarch, typecode, arg_type))
4982 gdb_assert (argreg > MIPS_LAST_ARG_REGNUM (gdbarch));
4983
4984 /* Write this portion of the argument to the stack. */
4985 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch))
4986 {
4987 /* Should shorter than int integer values be
4988 promoted to int before being stored? */
4989 int longword_offset = 0;
4990 CORE_ADDR addr;
4991 stack_used_p = 1;
4992 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
4993 {
4994 if ((typecode == TYPE_CODE_INT
4995 || typecode == TYPE_CODE_PTR)
4996 && len <= 4)
4997 longword_offset = MIPS64_REGSIZE - len;
4998 }
4999
5000 if (mips_debug)
5001 {
5002 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
5003 paddress (gdbarch, stack_offset));
5004 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
5005 paddress (gdbarch, longword_offset));
5006 }
5007
5008 addr = sp + stack_offset + longword_offset;
5009
5010 if (mips_debug)
5011 {
5012 int i;
5013 fprintf_unfiltered (gdb_stdlog, " @%s ",
5014 paddress (gdbarch, addr));
5015 for (i = 0; i < partial_len; i++)
5016 {
5017 fprintf_unfiltered (gdb_stdlog, "%02x",
5018 val[i] & 0xff);
5019 }
5020 }
5021 write_memory (addr, val, partial_len);
5022 }
5023
5024 /* Note!!! This is NOT an else clause. Odd sized
5025 structs may go thru BOTH paths. */
5026 /* Write this portion of the argument to a general
5027 purpose register. */
5028 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
5029 {
5030 LONGEST regval;
5031
5032 /* Sign extend pointers, 32-bit integers and signed
5033 16-bit and 8-bit integers; everything else is taken
5034 as is. */
5035
5036 if ((partial_len == 4
5037 && (typecode == TYPE_CODE_PTR
5038 || typecode == TYPE_CODE_INT))
5039 || (partial_len < 4
5040 && typecode == TYPE_CODE_INT
5041 && !TYPE_UNSIGNED (arg_type)))
5042 regval = extract_signed_integer (val, partial_len,
5043 byte_order);
5044 else
5045 regval = extract_unsigned_integer (val, partial_len,
5046 byte_order);
5047
5048 /* A non-floating-point argument being passed in a
5049 general register. If a struct or union, and if
5050 the remaining length is smaller than the register
5051 size, we have to adjust the register value on
5052 big endian targets.
5053
5054 It does not seem to be necessary to do the
5055 same for integral types. */
5056
5057 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
5058 && partial_len < MIPS64_REGSIZE
5059 && (typecode == TYPE_CODE_STRUCT
5060 || typecode == TYPE_CODE_UNION))
5061 regval <<= ((MIPS64_REGSIZE - partial_len)
5062 * TARGET_CHAR_BIT);
5063
5064 if (mips_debug)
5065 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
5066 argreg,
5067 phex (regval, MIPS64_REGSIZE));
5068 regcache_cooked_write_unsigned (regcache, argreg, regval);
5069
5070 if (mips_n32n64_fp_arg_chunk_p (gdbarch, arg_type,
5071 TYPE_LENGTH (arg_type) - len))
5072 {
5073 if (mips_debug)
5074 fprintf_filtered (gdb_stdlog, " - fpreg=%d val=%s",
5075 float_argreg,
5076 phex (regval, MIPS64_REGSIZE));
5077 regcache_cooked_write_unsigned (regcache, float_argreg,
5078 regval);
5079 }
5080
5081 float_argreg++;
5082 argreg++;
5083 }
5084
5085 len -= partial_len;
5086 val += partial_len;
5087
5088 /* Compute the offset into the stack at which we will
5089 copy the next parameter.
5090
5091 In N32 (N64?), the stack_offset only needs to be
5092 adjusted when it has been used. */
5093
5094 if (stack_used_p)
5095 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
5096 }
5097 }
5098 if (mips_debug)
5099 fprintf_unfiltered (gdb_stdlog, "\n");
5100 }
5101
5102 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
5103
5104 /* Return adjusted stack pointer. */
5105 return sp;
5106 }
5107
5108 static enum return_value_convention
5109 mips_n32n64_return_value (struct gdbarch *gdbarch, struct value *function,
5110 struct type *type, struct regcache *regcache,
5111 gdb_byte *readbuf, const gdb_byte *writebuf)
5112 {
5113 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5114
5115 /* From MIPSpro N32 ABI Handbook, Document Number: 007-2816-004
5116
5117 Function results are returned in $2 (and $3 if needed), or $f0 (and $f2
5118 if needed), as appropriate for the type. Composite results (struct,
5119 union, or array) are returned in $2/$f0 and $3/$f2 according to the
5120 following rules:
5121
5122 * A struct with only one or two floating point fields is returned in $f0
5123 (and $f2 if necessary). This is a generalization of the Fortran COMPLEX
5124 case.
5125
5126 * Any other composite results of at most 128 bits are returned in
5127 $2 (first 64 bits) and $3 (remainder, if necessary).
5128
5129 * Larger composite results are handled by converting the function to a
5130 procedure with an implicit first parameter, which is a pointer to an area
5131 reserved by the caller to receive the result. [The o32-bit ABI requires
5132 that all composite results be handled by conversion to implicit first
5133 parameters. The MIPS/SGI Fortran implementation has always made a
5134 specific exception to return COMPLEX results in the floating point
5135 registers.] */
5136
5137 if (TYPE_LENGTH (type) > 2 * MIPS64_REGSIZE)
5138 return RETURN_VALUE_STRUCT_CONVENTION;
5139 else if (TYPE_CODE (type) == TYPE_CODE_FLT
5140 && TYPE_LENGTH (type) == 16
5141 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5142 {
5143 /* A 128-bit floating-point value fills both $f0 and $f2. The
5144 two registers are used in the same as memory order, so the
5145 eight bytes with the lower memory address are in $f0. */
5146 if (mips_debug)
5147 fprintf_unfiltered (gdb_stderr, "Return float in $f0 and $f2\n");
5148 mips_xfer_register (gdbarch, regcache,
5149 (gdbarch_num_regs (gdbarch)
5150 + mips_regnum (gdbarch)->fp0),
5151 8, gdbarch_byte_order (gdbarch),
5152 readbuf, writebuf, 0);
5153 mips_xfer_register (gdbarch, regcache,
5154 (gdbarch_num_regs (gdbarch)
5155 + mips_regnum (gdbarch)->fp0 + 2),
5156 8, gdbarch_byte_order (gdbarch),
5157 readbuf ? readbuf + 8 : readbuf,
5158 writebuf ? writebuf + 8 : writebuf, 0);
5159 return RETURN_VALUE_REGISTER_CONVENTION;
5160 }
5161 else if (TYPE_CODE (type) == TYPE_CODE_FLT
5162 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5163 {
5164 /* A single or double floating-point value that fits in FP0. */
5165 if (mips_debug)
5166 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
5167 mips_xfer_register (gdbarch, regcache,
5168 (gdbarch_num_regs (gdbarch)
5169 + mips_regnum (gdbarch)->fp0),
5170 TYPE_LENGTH (type),
5171 gdbarch_byte_order (gdbarch),
5172 readbuf, writebuf, 0);
5173 return RETURN_VALUE_REGISTER_CONVENTION;
5174 }
5175 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5176 && TYPE_NFIELDS (type) <= 2
5177 && TYPE_NFIELDS (type) >= 1
5178 && ((TYPE_NFIELDS (type) == 1
5179 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
5180 == TYPE_CODE_FLT))
5181 || (TYPE_NFIELDS (type) == 2
5182 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 0)))
5183 == TYPE_CODE_FLT)
5184 && (TYPE_CODE (check_typedef (TYPE_FIELD_TYPE (type, 1)))
5185 == TYPE_CODE_FLT))))
5186 {
5187 /* A struct that contains one or two floats. Each value is part
5188 in the least significant part of their floating point
5189 register (or GPR, for soft float). */
5190 int regnum;
5191 int field;
5192 for (field = 0, regnum = (tdep->mips_fpu_type != MIPS_FPU_NONE
5193 ? mips_regnum (gdbarch)->fp0
5194 : MIPS_V0_REGNUM);
5195 field < TYPE_NFIELDS (type); field++, regnum += 2)
5196 {
5197 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
5198 / TARGET_CHAR_BIT);
5199 if (mips_debug)
5200 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
5201 offset);
5202 if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)) == 16)
5203 {
5204 /* A 16-byte long double field goes in two consecutive
5205 registers. */
5206 mips_xfer_register (gdbarch, regcache,
5207 gdbarch_num_regs (gdbarch) + regnum,
5208 8,
5209 gdbarch_byte_order (gdbarch),
5210 readbuf, writebuf, offset);
5211 mips_xfer_register (gdbarch, regcache,
5212 gdbarch_num_regs (gdbarch) + regnum + 1,
5213 8,
5214 gdbarch_byte_order (gdbarch),
5215 readbuf, writebuf, offset + 8);
5216 }
5217 else
5218 mips_xfer_register (gdbarch, regcache,
5219 gdbarch_num_regs (gdbarch) + regnum,
5220 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
5221 gdbarch_byte_order (gdbarch),
5222 readbuf, writebuf, offset);
5223 }
5224 return RETURN_VALUE_REGISTER_CONVENTION;
5225 }
5226 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5227 || TYPE_CODE (type) == TYPE_CODE_UNION
5228 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
5229 {
5230 /* A composite type. Extract the left justified value,
5231 regardless of the byte order. I.e. DO NOT USE
5232 mips_xfer_lower. */
5233 int offset;
5234 int regnum;
5235 for (offset = 0, regnum = MIPS_V0_REGNUM;
5236 offset < TYPE_LENGTH (type);
5237 offset += register_size (gdbarch, regnum), regnum++)
5238 {
5239 int xfer = register_size (gdbarch, regnum);
5240 if (offset + xfer > TYPE_LENGTH (type))
5241 xfer = TYPE_LENGTH (type) - offset;
5242 if (mips_debug)
5243 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
5244 offset, xfer, regnum);
5245 mips_xfer_register (gdbarch, regcache,
5246 gdbarch_num_regs (gdbarch) + regnum,
5247 xfer, BFD_ENDIAN_UNKNOWN, readbuf, writebuf,
5248 offset);
5249 }
5250 return RETURN_VALUE_REGISTER_CONVENTION;
5251 }
5252 else
5253 {
5254 /* A scalar extract each part but least-significant-byte
5255 justified. */
5256 int offset;
5257 int regnum;
5258 for (offset = 0, regnum = MIPS_V0_REGNUM;
5259 offset < TYPE_LENGTH (type);
5260 offset += register_size (gdbarch, regnum), regnum++)
5261 {
5262 int xfer = register_size (gdbarch, regnum);
5263 if (offset + xfer > TYPE_LENGTH (type))
5264 xfer = TYPE_LENGTH (type) - offset;
5265 if (mips_debug)
5266 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
5267 offset, xfer, regnum);
5268 mips_xfer_register (gdbarch, regcache,
5269 gdbarch_num_regs (gdbarch) + regnum,
5270 xfer, gdbarch_byte_order (gdbarch),
5271 readbuf, writebuf, offset);
5272 }
5273 return RETURN_VALUE_REGISTER_CONVENTION;
5274 }
5275 }
5276
5277 /* Which registers to use for passing floating-point values between
5278 function calls, one of floating-point, general and both kinds of
5279 registers. O32 and O64 use different register kinds for standard
5280 MIPS and MIPS16 code; to make the handling of cases where we may
5281 not know what kind of code is being used (e.g. no debug information)
5282 easier we sometimes use both kinds. */
5283
5284 enum mips_fval_reg
5285 {
5286 mips_fval_fpr,
5287 mips_fval_gpr,
5288 mips_fval_both
5289 };
5290
5291 /* O32 ABI stuff. */
5292
5293 static CORE_ADDR
5294 mips_o32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
5295 struct regcache *regcache, CORE_ADDR bp_addr,
5296 int nargs, struct value **args, CORE_ADDR sp,
5297 int struct_return, CORE_ADDR struct_addr)
5298 {
5299 int argreg;
5300 int float_argreg;
5301 int argnum;
5302 int len = 0;
5303 int stack_offset = 0;
5304 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5305 CORE_ADDR func_addr = find_function_addr (function, NULL);
5306
5307 /* For shared libraries, "t9" needs to point at the function
5308 address. */
5309 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
5310
5311 /* Set the return address register to point to the entry point of
5312 the program, where a breakpoint lies in wait. */
5313 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
5314
5315 /* First ensure that the stack and structure return address (if any)
5316 are properly aligned. The stack has to be at least 64-bit
5317 aligned even on 32-bit machines, because doubles must be 64-bit
5318 aligned. For n32 and n64, stack frames need to be 128-bit
5319 aligned, so we round to this widest known alignment. */
5320
5321 sp = align_down (sp, 16);
5322 struct_addr = align_down (struct_addr, 16);
5323
5324 /* Now make space on the stack for the args. */
5325 for (argnum = 0; argnum < nargs; argnum++)
5326 {
5327 struct type *arg_type = check_typedef (value_type (args[argnum]));
5328
5329 /* Align to double-word if necessary. */
5330 if (mips_type_needs_double_align (arg_type))
5331 len = align_up (len, MIPS32_REGSIZE * 2);
5332 /* Allocate space on the stack. */
5333 len += align_up (TYPE_LENGTH (arg_type), MIPS32_REGSIZE);
5334 }
5335 sp -= align_up (len, 16);
5336
5337 if (mips_debug)
5338 fprintf_unfiltered (gdb_stdlog,
5339 "mips_o32_push_dummy_call: sp=%s allocated %ld\n",
5340 paddress (gdbarch, sp), (long) align_up (len, 16));
5341
5342 /* Initialize the integer and float register pointers. */
5343 argreg = MIPS_A0_REGNUM;
5344 float_argreg = mips_fpa0_regnum (gdbarch);
5345
5346 /* The struct_return pointer occupies the first parameter-passing reg. */
5347 if (struct_return)
5348 {
5349 if (mips_debug)
5350 fprintf_unfiltered (gdb_stdlog,
5351 "mips_o32_push_dummy_call: "
5352 "struct_return reg=%d %s\n",
5353 argreg, paddress (gdbarch, struct_addr));
5354 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
5355 stack_offset += MIPS32_REGSIZE;
5356 }
5357
5358 /* Now load as many as possible of the first arguments into
5359 registers, and push the rest onto the stack. Loop thru args
5360 from first to last. */
5361 for (argnum = 0; argnum < nargs; argnum++)
5362 {
5363 const gdb_byte *val;
5364 struct value *arg = args[argnum];
5365 struct type *arg_type = check_typedef (value_type (arg));
5366 int len = TYPE_LENGTH (arg_type);
5367 enum type_code typecode = TYPE_CODE (arg_type);
5368
5369 if (mips_debug)
5370 fprintf_unfiltered (gdb_stdlog,
5371 "mips_o32_push_dummy_call: %d len=%d type=%d",
5372 argnum + 1, len, (int) typecode);
5373
5374 val = value_contents (arg);
5375
5376 /* 32-bit ABIs always start floating point arguments in an
5377 even-numbered floating point register. Round the FP register
5378 up before the check to see if there are any FP registers
5379 left. O32 targets also pass the FP in the integer registers
5380 so also round up normal registers. */
5381 if (fp_register_arg_p (gdbarch, typecode, arg_type))
5382 {
5383 if ((float_argreg & 1))
5384 float_argreg++;
5385 }
5386
5387 /* Floating point arguments passed in registers have to be
5388 treated specially. On 32-bit architectures, doubles are
5389 passed in register pairs; the even FP register gets the
5390 low word, and the odd FP register gets the high word.
5391 On O32, the first two floating point arguments are also
5392 copied to general registers, following their memory order,
5393 because MIPS16 functions don't use float registers for
5394 arguments. This duplication of arguments in general
5395 registers can't hurt non-MIPS16 functions, because those
5396 registers are normally skipped. */
5397
5398 if (fp_register_arg_p (gdbarch, typecode, arg_type)
5399 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
5400 {
5401 if (register_size (gdbarch, float_argreg) < 8 && len == 8)
5402 {
5403 int freg_offset = gdbarch_byte_order (gdbarch)
5404 == BFD_ENDIAN_BIG ? 1 : 0;
5405 unsigned long regval;
5406
5407 /* First word. */
5408 regval = extract_unsigned_integer (val, 4, byte_order);
5409 if (mips_debug)
5410 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5411 float_argreg + freg_offset,
5412 phex (regval, 4));
5413 regcache_cooked_write_unsigned (regcache,
5414 float_argreg++ + freg_offset,
5415 regval);
5416 if (mips_debug)
5417 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5418 argreg, phex (regval, 4));
5419 regcache_cooked_write_unsigned (regcache, argreg++, regval);
5420
5421 /* Second word. */
5422 regval = extract_unsigned_integer (val + 4, 4, byte_order);
5423 if (mips_debug)
5424 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5425 float_argreg - freg_offset,
5426 phex (regval, 4));
5427 regcache_cooked_write_unsigned (regcache,
5428 float_argreg++ - freg_offset,
5429 regval);
5430 if (mips_debug)
5431 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5432 argreg, phex (regval, 4));
5433 regcache_cooked_write_unsigned (regcache, argreg++, regval);
5434 }
5435 else
5436 {
5437 /* This is a floating point value that fits entirely
5438 in a single register. */
5439 /* On 32 bit ABI's the float_argreg is further adjusted
5440 above to ensure that it is even register aligned. */
5441 LONGEST regval = extract_unsigned_integer (val, len, byte_order);
5442 if (mips_debug)
5443 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5444 float_argreg, phex (regval, len));
5445 regcache_cooked_write_unsigned (regcache,
5446 float_argreg++, regval);
5447 /* Although two FP registers are reserved for each
5448 argument, only one corresponding integer register is
5449 reserved. */
5450 if (mips_debug)
5451 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5452 argreg, phex (regval, len));
5453 regcache_cooked_write_unsigned (regcache, argreg++, regval);
5454 }
5455 /* Reserve space for the FP register. */
5456 stack_offset += align_up (len, MIPS32_REGSIZE);
5457 }
5458 else
5459 {
5460 /* Copy the argument to general registers or the stack in
5461 register-sized pieces. Large arguments are split between
5462 registers and stack. */
5463 /* Note: structs whose size is not a multiple of MIPS32_REGSIZE
5464 are treated specially: Irix cc passes
5465 them in registers where gcc sometimes puts them on the
5466 stack. For maximum compatibility, we will put them in
5467 both places. */
5468 int odd_sized_struct = (len > MIPS32_REGSIZE
5469 && len % MIPS32_REGSIZE != 0);
5470 /* Structures should be aligned to eight bytes (even arg registers)
5471 on MIPS_ABI_O32, if their first member has double precision. */
5472 if (mips_type_needs_double_align (arg_type))
5473 {
5474 if ((argreg & 1))
5475 {
5476 argreg++;
5477 stack_offset += MIPS32_REGSIZE;
5478 }
5479 }
5480 while (len > 0)
5481 {
5482 int partial_len = (len < MIPS32_REGSIZE ? len : MIPS32_REGSIZE);
5483
5484 if (mips_debug)
5485 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
5486 partial_len);
5487
5488 /* Write this portion of the argument to the stack. */
5489 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
5490 || odd_sized_struct)
5491 {
5492 /* Should shorter than int integer values be
5493 promoted to int before being stored? */
5494 int longword_offset = 0;
5495 CORE_ADDR addr;
5496
5497 if (mips_debug)
5498 {
5499 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
5500 paddress (gdbarch, stack_offset));
5501 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
5502 paddress (gdbarch, longword_offset));
5503 }
5504
5505 addr = sp + stack_offset + longword_offset;
5506
5507 if (mips_debug)
5508 {
5509 int i;
5510 fprintf_unfiltered (gdb_stdlog, " @%s ",
5511 paddress (gdbarch, addr));
5512 for (i = 0; i < partial_len; i++)
5513 {
5514 fprintf_unfiltered (gdb_stdlog, "%02x",
5515 val[i] & 0xff);
5516 }
5517 }
5518 write_memory (addr, val, partial_len);
5519 }
5520
5521 /* Note!!! This is NOT an else clause. Odd sized
5522 structs may go thru BOTH paths. */
5523 /* Write this portion of the argument to a general
5524 purpose register. */
5525 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
5526 {
5527 LONGEST regval = extract_signed_integer (val, partial_len,
5528 byte_order);
5529 /* Value may need to be sign extended, because
5530 mips_isa_regsize() != mips_abi_regsize(). */
5531
5532 /* A non-floating-point argument being passed in a
5533 general register. If a struct or union, and if
5534 the remaining length is smaller than the register
5535 size, we have to adjust the register value on
5536 big endian targets.
5537
5538 It does not seem to be necessary to do the
5539 same for integral types.
5540
5541 Also don't do this adjustment on O64 binaries.
5542
5543 cagney/2001-07-23: gdb/179: Also, GCC, when
5544 outputting LE O32 with sizeof (struct) <
5545 mips_abi_regsize(), generates a left shift
5546 as part of storing the argument in a register
5547 (the left shift isn't generated when
5548 sizeof (struct) >= mips_abi_regsize()). Since
5549 it is quite possible that this is GCC
5550 contradicting the LE/O32 ABI, GDB has not been
5551 adjusted to accommodate this. Either someone
5552 needs to demonstrate that the LE/O32 ABI
5553 specifies such a left shift OR this new ABI gets
5554 identified as such and GDB gets tweaked
5555 accordingly. */
5556
5557 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
5558 && partial_len < MIPS32_REGSIZE
5559 && (typecode == TYPE_CODE_STRUCT
5560 || typecode == TYPE_CODE_UNION))
5561 regval <<= ((MIPS32_REGSIZE - partial_len)
5562 * TARGET_CHAR_BIT);
5563
5564 if (mips_debug)
5565 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
5566 argreg,
5567 phex (regval, MIPS32_REGSIZE));
5568 regcache_cooked_write_unsigned (regcache, argreg, regval);
5569 argreg++;
5570
5571 /* Prevent subsequent floating point arguments from
5572 being passed in floating point registers. */
5573 float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
5574 }
5575
5576 len -= partial_len;
5577 val += partial_len;
5578
5579 /* Compute the offset into the stack at which we will
5580 copy the next parameter.
5581
5582 In older ABIs, the caller reserved space for
5583 registers that contained arguments. This was loosely
5584 refered to as their "home". Consequently, space is
5585 always allocated. */
5586
5587 stack_offset += align_up (partial_len, MIPS32_REGSIZE);
5588 }
5589 }
5590 if (mips_debug)
5591 fprintf_unfiltered (gdb_stdlog, "\n");
5592 }
5593
5594 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
5595
5596 /* Return adjusted stack pointer. */
5597 return sp;
5598 }
5599
5600 static enum return_value_convention
5601 mips_o32_return_value (struct gdbarch *gdbarch, struct value *function,
5602 struct type *type, struct regcache *regcache,
5603 gdb_byte *readbuf, const gdb_byte *writebuf)
5604 {
5605 CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
5606 int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
5607 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5608 enum mips_fval_reg fval_reg;
5609
5610 fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
5611 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5612 || TYPE_CODE (type) == TYPE_CODE_UNION
5613 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
5614 return RETURN_VALUE_STRUCT_CONVENTION;
5615 else if (TYPE_CODE (type) == TYPE_CODE_FLT
5616 && TYPE_LENGTH (type) == 4 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5617 {
5618 /* A single-precision floating-point value. If reading in or copying,
5619 then we get it from/put it to FP0 for standard MIPS code or GPR2
5620 for MIPS16 code. If writing out only, then we put it to both FP0
5621 and GPR2. We do not support reading in with no function known, if
5622 this safety check ever triggers, then we'll have to try harder. */
5623 gdb_assert (function || !readbuf);
5624 if (mips_debug)
5625 switch (fval_reg)
5626 {
5627 case mips_fval_fpr:
5628 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
5629 break;
5630 case mips_fval_gpr:
5631 fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
5632 break;
5633 case mips_fval_both:
5634 fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
5635 break;
5636 }
5637 if (fval_reg != mips_fval_gpr)
5638 mips_xfer_register (gdbarch, regcache,
5639 (gdbarch_num_regs (gdbarch)
5640 + mips_regnum (gdbarch)->fp0),
5641 TYPE_LENGTH (type),
5642 gdbarch_byte_order (gdbarch),
5643 readbuf, writebuf, 0);
5644 if (fval_reg != mips_fval_fpr)
5645 mips_xfer_register (gdbarch, regcache,
5646 gdbarch_num_regs (gdbarch) + 2,
5647 TYPE_LENGTH (type),
5648 gdbarch_byte_order (gdbarch),
5649 readbuf, writebuf, 0);
5650 return RETURN_VALUE_REGISTER_CONVENTION;
5651 }
5652 else if (TYPE_CODE (type) == TYPE_CODE_FLT
5653 && TYPE_LENGTH (type) == 8 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5654 {
5655 /* A double-precision floating-point value. If reading in or copying,
5656 then we get it from/put it to FP1 and FP0 for standard MIPS code or
5657 GPR2 and GPR3 for MIPS16 code. If writing out only, then we put it
5658 to both FP1/FP0 and GPR2/GPR3. We do not support reading in with
5659 no function known, if this safety check ever triggers, then we'll
5660 have to try harder. */
5661 gdb_assert (function || !readbuf);
5662 if (mips_debug)
5663 switch (fval_reg)
5664 {
5665 case mips_fval_fpr:
5666 fprintf_unfiltered (gdb_stderr, "Return float in $fp1/$fp0\n");
5667 break;
5668 case mips_fval_gpr:
5669 fprintf_unfiltered (gdb_stderr, "Return float in $2/$3\n");
5670 break;
5671 case mips_fval_both:
5672 fprintf_unfiltered (gdb_stderr,
5673 "Return float in $fp1/$fp0 and $2/$3\n");
5674 break;
5675 }
5676 if (fval_reg != mips_fval_gpr)
5677 {
5678 /* The most significant part goes in FP1, and the least significant
5679 in FP0. */
5680 switch (gdbarch_byte_order (gdbarch))
5681 {
5682 case BFD_ENDIAN_LITTLE:
5683 mips_xfer_register (gdbarch, regcache,
5684 (gdbarch_num_regs (gdbarch)
5685 + mips_regnum (gdbarch)->fp0 + 0),
5686 4, gdbarch_byte_order (gdbarch),
5687 readbuf, writebuf, 0);
5688 mips_xfer_register (gdbarch, regcache,
5689 (gdbarch_num_regs (gdbarch)
5690 + mips_regnum (gdbarch)->fp0 + 1),
5691 4, gdbarch_byte_order (gdbarch),
5692 readbuf, writebuf, 4);
5693 break;
5694 case BFD_ENDIAN_BIG:
5695 mips_xfer_register (gdbarch, regcache,
5696 (gdbarch_num_regs (gdbarch)
5697 + mips_regnum (gdbarch)->fp0 + 1),
5698 4, gdbarch_byte_order (gdbarch),
5699 readbuf, writebuf, 0);
5700 mips_xfer_register (gdbarch, regcache,
5701 (gdbarch_num_regs (gdbarch)
5702 + mips_regnum (gdbarch)->fp0 + 0),
5703 4, gdbarch_byte_order (gdbarch),
5704 readbuf, writebuf, 4);
5705 break;
5706 default:
5707 internal_error (__FILE__, __LINE__, _("bad switch"));
5708 }
5709 }
5710 if (fval_reg != mips_fval_fpr)
5711 {
5712 /* The two 32-bit parts are always placed in GPR2 and GPR3
5713 following these registers' memory order. */
5714 mips_xfer_register (gdbarch, regcache,
5715 gdbarch_num_regs (gdbarch) + 2,
5716 4, gdbarch_byte_order (gdbarch),
5717 readbuf, writebuf, 0);
5718 mips_xfer_register (gdbarch, regcache,
5719 gdbarch_num_regs (gdbarch) + 3,
5720 4, gdbarch_byte_order (gdbarch),
5721 readbuf, writebuf, 4);
5722 }
5723 return RETURN_VALUE_REGISTER_CONVENTION;
5724 }
5725 #if 0
5726 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5727 && TYPE_NFIELDS (type) <= 2
5728 && TYPE_NFIELDS (type) >= 1
5729 && ((TYPE_NFIELDS (type) == 1
5730 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
5731 == TYPE_CODE_FLT))
5732 || (TYPE_NFIELDS (type) == 2
5733 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 0))
5734 == TYPE_CODE_FLT)
5735 && (TYPE_CODE (TYPE_FIELD_TYPE (type, 1))
5736 == TYPE_CODE_FLT)))
5737 && tdep->mips_fpu_type != MIPS_FPU_NONE)
5738 {
5739 /* A struct that contains one or two floats. Each value is part
5740 in the least significant part of their floating point
5741 register.. */
5742 gdb_byte reg[MAX_REGISTER_SIZE];
5743 int regnum;
5744 int field;
5745 for (field = 0, regnum = mips_regnum (gdbarch)->fp0;
5746 field < TYPE_NFIELDS (type); field++, regnum += 2)
5747 {
5748 int offset = (FIELD_BITPOS (TYPE_FIELDS (type)[field])
5749 / TARGET_CHAR_BIT);
5750 if (mips_debug)
5751 fprintf_unfiltered (gdb_stderr, "Return float struct+%d\n",
5752 offset);
5753 mips_xfer_register (gdbarch, regcache,
5754 gdbarch_num_regs (gdbarch) + regnum,
5755 TYPE_LENGTH (TYPE_FIELD_TYPE (type, field)),
5756 gdbarch_byte_order (gdbarch),
5757 readbuf, writebuf, offset);
5758 }
5759 return RETURN_VALUE_REGISTER_CONVENTION;
5760 }
5761 #endif
5762 #if 0
5763 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT
5764 || TYPE_CODE (type) == TYPE_CODE_UNION)
5765 {
5766 /* A structure or union. Extract the left justified value,
5767 regardless of the byte order. I.e. DO NOT USE
5768 mips_xfer_lower. */
5769 int offset;
5770 int regnum;
5771 for (offset = 0, regnum = MIPS_V0_REGNUM;
5772 offset < TYPE_LENGTH (type);
5773 offset += register_size (gdbarch, regnum), regnum++)
5774 {
5775 int xfer = register_size (gdbarch, regnum);
5776 if (offset + xfer > TYPE_LENGTH (type))
5777 xfer = TYPE_LENGTH (type) - offset;
5778 if (mips_debug)
5779 fprintf_unfiltered (gdb_stderr, "Return struct+%d:%d in $%d\n",
5780 offset, xfer, regnum);
5781 mips_xfer_register (gdbarch, regcache,
5782 gdbarch_num_regs (gdbarch) + regnum, xfer,
5783 BFD_ENDIAN_UNKNOWN, readbuf, writebuf, offset);
5784 }
5785 return RETURN_VALUE_REGISTER_CONVENTION;
5786 }
5787 #endif
5788 else
5789 {
5790 /* A scalar extract each part but least-significant-byte
5791 justified. o32 thinks registers are 4 byte, regardless of
5792 the ISA. */
5793 int offset;
5794 int regnum;
5795 for (offset = 0, regnum = MIPS_V0_REGNUM;
5796 offset < TYPE_LENGTH (type);
5797 offset += MIPS32_REGSIZE, regnum++)
5798 {
5799 int xfer = MIPS32_REGSIZE;
5800 if (offset + xfer > TYPE_LENGTH (type))
5801 xfer = TYPE_LENGTH (type) - offset;
5802 if (mips_debug)
5803 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
5804 offset, xfer, regnum);
5805 mips_xfer_register (gdbarch, regcache,
5806 gdbarch_num_regs (gdbarch) + regnum, xfer,
5807 gdbarch_byte_order (gdbarch),
5808 readbuf, writebuf, offset);
5809 }
5810 return RETURN_VALUE_REGISTER_CONVENTION;
5811 }
5812 }
5813
5814 /* O64 ABI. This is a hacked up kind of 64-bit version of the o32
5815 ABI. */
5816
5817 static CORE_ADDR
5818 mips_o64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
5819 struct regcache *regcache, CORE_ADDR bp_addr,
5820 int nargs,
5821 struct value **args, CORE_ADDR sp,
5822 int struct_return, CORE_ADDR struct_addr)
5823 {
5824 int argreg;
5825 int float_argreg;
5826 int argnum;
5827 int len = 0;
5828 int stack_offset = 0;
5829 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5830 CORE_ADDR func_addr = find_function_addr (function, NULL);
5831
5832 /* For shared libraries, "t9" needs to point at the function
5833 address. */
5834 regcache_cooked_write_signed (regcache, MIPS_T9_REGNUM, func_addr);
5835
5836 /* Set the return address register to point to the entry point of
5837 the program, where a breakpoint lies in wait. */
5838 regcache_cooked_write_signed (regcache, MIPS_RA_REGNUM, bp_addr);
5839
5840 /* First ensure that the stack and structure return address (if any)
5841 are properly aligned. The stack has to be at least 64-bit
5842 aligned even on 32-bit machines, because doubles must be 64-bit
5843 aligned. For n32 and n64, stack frames need to be 128-bit
5844 aligned, so we round to this widest known alignment. */
5845
5846 sp = align_down (sp, 16);
5847 struct_addr = align_down (struct_addr, 16);
5848
5849 /* Now make space on the stack for the args. */
5850 for (argnum = 0; argnum < nargs; argnum++)
5851 {
5852 struct type *arg_type = check_typedef (value_type (args[argnum]));
5853
5854 /* Allocate space on the stack. */
5855 len += align_up (TYPE_LENGTH (arg_type), MIPS64_REGSIZE);
5856 }
5857 sp -= align_up (len, 16);
5858
5859 if (mips_debug)
5860 fprintf_unfiltered (gdb_stdlog,
5861 "mips_o64_push_dummy_call: sp=%s allocated %ld\n",
5862 paddress (gdbarch, sp), (long) align_up (len, 16));
5863
5864 /* Initialize the integer and float register pointers. */
5865 argreg = MIPS_A0_REGNUM;
5866 float_argreg = mips_fpa0_regnum (gdbarch);
5867
5868 /* The struct_return pointer occupies the first parameter-passing reg. */
5869 if (struct_return)
5870 {
5871 if (mips_debug)
5872 fprintf_unfiltered (gdb_stdlog,
5873 "mips_o64_push_dummy_call: "
5874 "struct_return reg=%d %s\n",
5875 argreg, paddress (gdbarch, struct_addr));
5876 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
5877 stack_offset += MIPS64_REGSIZE;
5878 }
5879
5880 /* Now load as many as possible of the first arguments into
5881 registers, and push the rest onto the stack. Loop thru args
5882 from first to last. */
5883 for (argnum = 0; argnum < nargs; argnum++)
5884 {
5885 const gdb_byte *val;
5886 struct value *arg = args[argnum];
5887 struct type *arg_type = check_typedef (value_type (arg));
5888 int len = TYPE_LENGTH (arg_type);
5889 enum type_code typecode = TYPE_CODE (arg_type);
5890
5891 if (mips_debug)
5892 fprintf_unfiltered (gdb_stdlog,
5893 "mips_o64_push_dummy_call: %d len=%d type=%d",
5894 argnum + 1, len, (int) typecode);
5895
5896 val = value_contents (arg);
5897
5898 /* Floating point arguments passed in registers have to be
5899 treated specially. On 32-bit architectures, doubles are
5900 passed in register pairs; the even FP register gets the
5901 low word, and the odd FP register gets the high word.
5902 On O64, the first two floating point arguments are also
5903 copied to general registers, because MIPS16 functions
5904 don't use float registers for arguments. This duplication
5905 of arguments in general registers can't hurt non-MIPS16
5906 functions because those registers are normally skipped. */
5907
5908 if (fp_register_arg_p (gdbarch, typecode, arg_type)
5909 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM (gdbarch))
5910 {
5911 LONGEST regval = extract_unsigned_integer (val, len, byte_order);
5912 if (mips_debug)
5913 fprintf_unfiltered (gdb_stdlog, " - fpreg=%d val=%s",
5914 float_argreg, phex (regval, len));
5915 regcache_cooked_write_unsigned (regcache, float_argreg++, regval);
5916 if (mips_debug)
5917 fprintf_unfiltered (gdb_stdlog, " - reg=%d val=%s",
5918 argreg, phex (regval, len));
5919 regcache_cooked_write_unsigned (regcache, argreg, regval);
5920 argreg++;
5921 /* Reserve space for the FP register. */
5922 stack_offset += align_up (len, MIPS64_REGSIZE);
5923 }
5924 else
5925 {
5926 /* Copy the argument to general registers or the stack in
5927 register-sized pieces. Large arguments are split between
5928 registers and stack. */
5929 /* Note: structs whose size is not a multiple of MIPS64_REGSIZE
5930 are treated specially: Irix cc passes them in registers
5931 where gcc sometimes puts them on the stack. For maximum
5932 compatibility, we will put them in both places. */
5933 int odd_sized_struct = (len > MIPS64_REGSIZE
5934 && len % MIPS64_REGSIZE != 0);
5935 while (len > 0)
5936 {
5937 int partial_len = (len < MIPS64_REGSIZE ? len : MIPS64_REGSIZE);
5938
5939 if (mips_debug)
5940 fprintf_unfiltered (gdb_stdlog, " -- partial=%d",
5941 partial_len);
5942
5943 /* Write this portion of the argument to the stack. */
5944 if (argreg > MIPS_LAST_ARG_REGNUM (gdbarch)
5945 || odd_sized_struct)
5946 {
5947 /* Should shorter than int integer values be
5948 promoted to int before being stored? */
5949 int longword_offset = 0;
5950 CORE_ADDR addr;
5951 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
5952 {
5953 if ((typecode == TYPE_CODE_INT
5954 || typecode == TYPE_CODE_PTR
5955 || typecode == TYPE_CODE_FLT)
5956 && len <= 4)
5957 longword_offset = MIPS64_REGSIZE - len;
5958 }
5959
5960 if (mips_debug)
5961 {
5962 fprintf_unfiltered (gdb_stdlog, " - stack_offset=%s",
5963 paddress (gdbarch, stack_offset));
5964 fprintf_unfiltered (gdb_stdlog, " longword_offset=%s",
5965 paddress (gdbarch, longword_offset));
5966 }
5967
5968 addr = sp + stack_offset + longword_offset;
5969
5970 if (mips_debug)
5971 {
5972 int i;
5973 fprintf_unfiltered (gdb_stdlog, " @%s ",
5974 paddress (gdbarch, addr));
5975 for (i = 0; i < partial_len; i++)
5976 {
5977 fprintf_unfiltered (gdb_stdlog, "%02x",
5978 val[i] & 0xff);
5979 }
5980 }
5981 write_memory (addr, val, partial_len);
5982 }
5983
5984 /* Note!!! This is NOT an else clause. Odd sized
5985 structs may go thru BOTH paths. */
5986 /* Write this portion of the argument to a general
5987 purpose register. */
5988 if (argreg <= MIPS_LAST_ARG_REGNUM (gdbarch))
5989 {
5990 LONGEST regval = extract_signed_integer (val, partial_len,
5991 byte_order);
5992 /* Value may need to be sign extended, because
5993 mips_isa_regsize() != mips_abi_regsize(). */
5994
5995 /* A non-floating-point argument being passed in a
5996 general register. If a struct or union, and if
5997 the remaining length is smaller than the register
5998 size, we have to adjust the register value on
5999 big endian targets.
6000
6001 It does not seem to be necessary to do the
6002 same for integral types. */
6003
6004 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
6005 && partial_len < MIPS64_REGSIZE
6006 && (typecode == TYPE_CODE_STRUCT
6007 || typecode == TYPE_CODE_UNION))
6008 regval <<= ((MIPS64_REGSIZE - partial_len)
6009 * TARGET_CHAR_BIT);
6010
6011 if (mips_debug)
6012 fprintf_filtered (gdb_stdlog, " - reg=%d val=%s",
6013 argreg,
6014 phex (regval, MIPS64_REGSIZE));
6015 regcache_cooked_write_unsigned (regcache, argreg, regval);
6016 argreg++;
6017
6018 /* Prevent subsequent floating point arguments from
6019 being passed in floating point registers. */
6020 float_argreg = MIPS_LAST_FP_ARG_REGNUM (gdbarch) + 1;
6021 }
6022
6023 len -= partial_len;
6024 val += partial_len;
6025
6026 /* Compute the offset into the stack at which we will
6027 copy the next parameter.
6028
6029 In older ABIs, the caller reserved space for
6030 registers that contained arguments. This was loosely
6031 refered to as their "home". Consequently, space is
6032 always allocated. */
6033
6034 stack_offset += align_up (partial_len, MIPS64_REGSIZE);
6035 }
6036 }
6037 if (mips_debug)
6038 fprintf_unfiltered (gdb_stdlog, "\n");
6039 }
6040
6041 regcache_cooked_write_signed (regcache, MIPS_SP_REGNUM, sp);
6042
6043 /* Return adjusted stack pointer. */
6044 return sp;
6045 }
6046
6047 static enum return_value_convention
6048 mips_o64_return_value (struct gdbarch *gdbarch, struct value *function,
6049 struct type *type, struct regcache *regcache,
6050 gdb_byte *readbuf, const gdb_byte *writebuf)
6051 {
6052 CORE_ADDR func_addr = function ? find_function_addr (function, NULL) : 0;
6053 int mips16 = mips_pc_is_mips16 (gdbarch, func_addr);
6054 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6055 enum mips_fval_reg fval_reg;
6056
6057 fval_reg = readbuf ? mips16 ? mips_fval_gpr : mips_fval_fpr : mips_fval_both;
6058 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
6059 || TYPE_CODE (type) == TYPE_CODE_UNION
6060 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
6061 return RETURN_VALUE_STRUCT_CONVENTION;
6062 else if (fp_register_arg_p (gdbarch, TYPE_CODE (type), type))
6063 {
6064 /* A floating-point value. If reading in or copying, then we get it
6065 from/put it to FP0 for standard MIPS code or GPR2 for MIPS16 code.
6066 If writing out only, then we put it to both FP0 and GPR2. We do
6067 not support reading in with no function known, if this safety
6068 check ever triggers, then we'll have to try harder. */
6069 gdb_assert (function || !readbuf);
6070 if (mips_debug)
6071 switch (fval_reg)
6072 {
6073 case mips_fval_fpr:
6074 fprintf_unfiltered (gdb_stderr, "Return float in $fp0\n");
6075 break;
6076 case mips_fval_gpr:
6077 fprintf_unfiltered (gdb_stderr, "Return float in $2\n");
6078 break;
6079 case mips_fval_both:
6080 fprintf_unfiltered (gdb_stderr, "Return float in $fp0 and $2\n");
6081 break;
6082 }
6083 if (fval_reg != mips_fval_gpr)
6084 mips_xfer_register (gdbarch, regcache,
6085 (gdbarch_num_regs (gdbarch)
6086 + mips_regnum (gdbarch)->fp0),
6087 TYPE_LENGTH (type),
6088 gdbarch_byte_order (gdbarch),
6089 readbuf, writebuf, 0);
6090 if (fval_reg != mips_fval_fpr)
6091 mips_xfer_register (gdbarch, regcache,
6092 gdbarch_num_regs (gdbarch) + 2,
6093 TYPE_LENGTH (type),
6094 gdbarch_byte_order (gdbarch),
6095 readbuf, writebuf, 0);
6096 return RETURN_VALUE_REGISTER_CONVENTION;
6097 }
6098 else
6099 {
6100 /* A scalar extract each part but least-significant-byte
6101 justified. */
6102 int offset;
6103 int regnum;
6104 for (offset = 0, regnum = MIPS_V0_REGNUM;
6105 offset < TYPE_LENGTH (type);
6106 offset += MIPS64_REGSIZE, regnum++)
6107 {
6108 int xfer = MIPS64_REGSIZE;
6109 if (offset + xfer > TYPE_LENGTH (type))
6110 xfer = TYPE_LENGTH (type) - offset;
6111 if (mips_debug)
6112 fprintf_unfiltered (gdb_stderr, "Return scalar+%d:%d in $%d\n",
6113 offset, xfer, regnum);
6114 mips_xfer_register (gdbarch, regcache,
6115 gdbarch_num_regs (gdbarch) + regnum,
6116 xfer, gdbarch_byte_order (gdbarch),
6117 readbuf, writebuf, offset);
6118 }
6119 return RETURN_VALUE_REGISTER_CONVENTION;
6120 }
6121 }
6122
6123 /* Floating point register management.
6124
6125 Background: MIPS1 & 2 fp registers are 32 bits wide. To support
6126 64bit operations, these early MIPS cpus treat fp register pairs
6127 (f0,f1) as a single register (d0). Later MIPS cpu's have 64 bit fp
6128 registers and offer a compatibility mode that emulates the MIPS2 fp
6129 model. When operating in MIPS2 fp compat mode, later cpu's split
6130 double precision floats into two 32-bit chunks and store them in
6131 consecutive fp regs. To display 64-bit floats stored in this
6132 fashion, we have to combine 32 bits from f0 and 32 bits from f1.
6133 Throw in user-configurable endianness and you have a real mess.
6134
6135 The way this works is:
6136 - If we are in 32-bit mode or on a 32-bit processor, then a 64-bit
6137 double-precision value will be split across two logical registers.
6138 The lower-numbered logical register will hold the low-order bits,
6139 regardless of the processor's endianness.
6140 - If we are on a 64-bit processor, and we are looking for a
6141 single-precision value, it will be in the low ordered bits
6142 of a 64-bit GPR (after mfc1, for example) or a 64-bit register
6143 save slot in memory.
6144 - If we are in 64-bit mode, everything is straightforward.
6145
6146 Note that this code only deals with "live" registers at the top of the
6147 stack. We will attempt to deal with saved registers later, when
6148 the raw/cooked register interface is in place. (We need a general
6149 interface that can deal with dynamic saved register sizes -- fp
6150 regs could be 32 bits wide in one frame and 64 on the frame above
6151 and below). */
6152
6153 /* Copy a 32-bit single-precision value from the current frame
6154 into rare_buffer. */
6155
6156 static void
6157 mips_read_fp_register_single (struct frame_info *frame, int regno,
6158 gdb_byte *rare_buffer)
6159 {
6160 struct gdbarch *gdbarch = get_frame_arch (frame);
6161 int raw_size = register_size (gdbarch, regno);
6162 gdb_byte *raw_buffer = (gdb_byte *) alloca (raw_size);
6163
6164 if (!deprecated_frame_register_read (frame, regno, raw_buffer))
6165 error (_("can't read register %d (%s)"),
6166 regno, gdbarch_register_name (gdbarch, regno));
6167 if (raw_size == 8)
6168 {
6169 /* We have a 64-bit value for this register. Find the low-order
6170 32 bits. */
6171 int offset;
6172
6173 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6174 offset = 4;
6175 else
6176 offset = 0;
6177
6178 memcpy (rare_buffer, raw_buffer + offset, 4);
6179 }
6180 else
6181 {
6182 memcpy (rare_buffer, raw_buffer, 4);
6183 }
6184 }
6185
6186 /* Copy a 64-bit double-precision value from the current frame into
6187 rare_buffer. This may include getting half of it from the next
6188 register. */
6189
6190 static void
6191 mips_read_fp_register_double (struct frame_info *frame, int regno,
6192 gdb_byte *rare_buffer)
6193 {
6194 struct gdbarch *gdbarch = get_frame_arch (frame);
6195 int raw_size = register_size (gdbarch, regno);
6196
6197 if (raw_size == 8 && !mips2_fp_compat (frame))
6198 {
6199 /* We have a 64-bit value for this register, and we should use
6200 all 64 bits. */
6201 if (!deprecated_frame_register_read (frame, regno, rare_buffer))
6202 error (_("can't read register %d (%s)"),
6203 regno, gdbarch_register_name (gdbarch, regno));
6204 }
6205 else
6206 {
6207 int rawnum = regno % gdbarch_num_regs (gdbarch);
6208
6209 if ((rawnum - mips_regnum (gdbarch)->fp0) & 1)
6210 internal_error (__FILE__, __LINE__,
6211 _("mips_read_fp_register_double: bad access to "
6212 "odd-numbered FP register"));
6213
6214 /* mips_read_fp_register_single will find the correct 32 bits from
6215 each register. */
6216 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6217 {
6218 mips_read_fp_register_single (frame, regno, rare_buffer + 4);
6219 mips_read_fp_register_single (frame, regno + 1, rare_buffer);
6220 }
6221 else
6222 {
6223 mips_read_fp_register_single (frame, regno, rare_buffer);
6224 mips_read_fp_register_single (frame, regno + 1, rare_buffer + 4);
6225 }
6226 }
6227 }
6228
6229 static void
6230 mips_print_fp_register (struct ui_file *file, struct frame_info *frame,
6231 int regnum)
6232 { /* Do values for FP (float) regs. */
6233 struct gdbarch *gdbarch = get_frame_arch (frame);
6234 gdb_byte *raw_buffer;
6235 double doub, flt1; /* Doubles extracted from raw hex data. */
6236 int inv1, inv2;
6237
6238 raw_buffer
6239 = ((gdb_byte *)
6240 alloca (2 * register_size (gdbarch, mips_regnum (gdbarch)->fp0)));
6241
6242 fprintf_filtered (file, "%s:", gdbarch_register_name (gdbarch, regnum));
6243 fprintf_filtered (file, "%*s",
6244 4 - (int) strlen (gdbarch_register_name (gdbarch, regnum)),
6245 "");
6246
6247 if (register_size (gdbarch, regnum) == 4 || mips2_fp_compat (frame))
6248 {
6249 struct value_print_options opts;
6250
6251 /* 4-byte registers: Print hex and floating. Also print even
6252 numbered registers as doubles. */
6253 mips_read_fp_register_single (frame, regnum, raw_buffer);
6254 flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
6255 raw_buffer, &inv1);
6256
6257 get_formatted_print_options (&opts, 'x');
6258 print_scalar_formatted (raw_buffer,
6259 builtin_type (gdbarch)->builtin_uint32,
6260 &opts, 'w', file);
6261
6262 fprintf_filtered (file, " flt: ");
6263 if (inv1)
6264 fprintf_filtered (file, " <invalid float> ");
6265 else
6266 fprintf_filtered (file, "%-17.9g", flt1);
6267
6268 if ((regnum - gdbarch_num_regs (gdbarch)) % 2 == 0)
6269 {
6270 mips_read_fp_register_double (frame, regnum, raw_buffer);
6271 doub = unpack_double (builtin_type (gdbarch)->builtin_double,
6272 raw_buffer, &inv2);
6273
6274 fprintf_filtered (file, " dbl: ");
6275 if (inv2)
6276 fprintf_filtered (file, "<invalid double>");
6277 else
6278 fprintf_filtered (file, "%-24.17g", doub);
6279 }
6280 }
6281 else
6282 {
6283 struct value_print_options opts;
6284
6285 /* Eight byte registers: print each one as hex, float and double. */
6286 mips_read_fp_register_single (frame, regnum, raw_buffer);
6287 flt1 = unpack_double (builtin_type (gdbarch)->builtin_float,
6288 raw_buffer, &inv1);
6289
6290 mips_read_fp_register_double (frame, regnum, raw_buffer);
6291 doub = unpack_double (builtin_type (gdbarch)->builtin_double,
6292 raw_buffer, &inv2);
6293
6294 get_formatted_print_options (&opts, 'x');
6295 print_scalar_formatted (raw_buffer,
6296 builtin_type (gdbarch)->builtin_uint64,
6297 &opts, 'g', file);
6298
6299 fprintf_filtered (file, " flt: ");
6300 if (inv1)
6301 fprintf_filtered (file, "<invalid float>");
6302 else
6303 fprintf_filtered (file, "%-17.9g", flt1);
6304
6305 fprintf_filtered (file, " dbl: ");
6306 if (inv2)
6307 fprintf_filtered (file, "<invalid double>");
6308 else
6309 fprintf_filtered (file, "%-24.17g", doub);
6310 }
6311 }
6312
6313 static void
6314 mips_print_register (struct ui_file *file, struct frame_info *frame,
6315 int regnum)
6316 {
6317 struct gdbarch *gdbarch = get_frame_arch (frame);
6318 struct value_print_options opts;
6319 struct value *val;
6320
6321 if (mips_float_register_p (gdbarch, regnum))
6322 {
6323 mips_print_fp_register (file, frame, regnum);
6324 return;
6325 }
6326
6327 val = get_frame_register_value (frame, regnum);
6328
6329 fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
6330
6331 /* The problem with printing numeric register names (r26, etc.) is that
6332 the user can't use them on input. Probably the best solution is to
6333 fix it so that either the numeric or the funky (a2, etc.) names
6334 are accepted on input. */
6335 if (regnum < MIPS_NUMREGS)
6336 fprintf_filtered (file, "(r%d): ", regnum);
6337 else
6338 fprintf_filtered (file, ": ");
6339
6340 get_formatted_print_options (&opts, 'x');
6341 val_print_scalar_formatted (value_type (val),
6342 value_contents_for_printing (val),
6343 value_embedded_offset (val),
6344 val,
6345 &opts, 0, file);
6346 }
6347
6348 /* Print IEEE exception condition bits in FLAGS. */
6349
6350 static void
6351 print_fpu_flags (struct ui_file *file, int flags)
6352 {
6353 if (flags & (1 << 0))
6354 fputs_filtered (" inexact", file);
6355 if (flags & (1 << 1))
6356 fputs_filtered (" uflow", file);
6357 if (flags & (1 << 2))
6358 fputs_filtered (" oflow", file);
6359 if (flags & (1 << 3))
6360 fputs_filtered (" div0", file);
6361 if (flags & (1 << 4))
6362 fputs_filtered (" inval", file);
6363 if (flags & (1 << 5))
6364 fputs_filtered (" unimp", file);
6365 fputc_filtered ('\n', file);
6366 }
6367
6368 /* Print interesting information about the floating point processor
6369 (if present) or emulator. */
6370
6371 static void
6372 mips_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
6373 struct frame_info *frame, const char *args)
6374 {
6375 int fcsr = mips_regnum (gdbarch)->fp_control_status;
6376 enum mips_fpu_type type = MIPS_FPU_TYPE (gdbarch);
6377 ULONGEST fcs = 0;
6378 int i;
6379
6380 if (fcsr == -1 || !read_frame_register_unsigned (frame, fcsr, &fcs))
6381 type = MIPS_FPU_NONE;
6382
6383 fprintf_filtered (file, "fpu type: %s\n",
6384 type == MIPS_FPU_DOUBLE ? "double-precision"
6385 : type == MIPS_FPU_SINGLE ? "single-precision"
6386 : "none / unused");
6387
6388 if (type == MIPS_FPU_NONE)
6389 return;
6390
6391 fprintf_filtered (file, "reg size: %d bits\n",
6392 register_size (gdbarch, mips_regnum (gdbarch)->fp0) * 8);
6393
6394 fputs_filtered ("cond :", file);
6395 if (fcs & (1 << 23))
6396 fputs_filtered (" 0", file);
6397 for (i = 1; i <= 7; i++)
6398 if (fcs & (1 << (24 + i)))
6399 fprintf_filtered (file, " %d", i);
6400 fputc_filtered ('\n', file);
6401
6402 fputs_filtered ("cause :", file);
6403 print_fpu_flags (file, (fcs >> 12) & 0x3f);
6404 fputs ("mask :", stdout);
6405 print_fpu_flags (file, (fcs >> 7) & 0x1f);
6406 fputs ("flags :", stdout);
6407 print_fpu_flags (file, (fcs >> 2) & 0x1f);
6408
6409 fputs_filtered ("rounding: ", file);
6410 switch (fcs & 3)
6411 {
6412 case 0: fputs_filtered ("nearest\n", file); break;
6413 case 1: fputs_filtered ("zero\n", file); break;
6414 case 2: fputs_filtered ("+inf\n", file); break;
6415 case 3: fputs_filtered ("-inf\n", file); break;
6416 }
6417
6418 fputs_filtered ("flush :", file);
6419 if (fcs & (1 << 21))
6420 fputs_filtered (" nearest", file);
6421 if (fcs & (1 << 22))
6422 fputs_filtered (" override", file);
6423 if (fcs & (1 << 24))
6424 fputs_filtered (" zero", file);
6425 if ((fcs & (0xb << 21)) == 0)
6426 fputs_filtered (" no", file);
6427 fputc_filtered ('\n', file);
6428
6429 fprintf_filtered (file, "nan2008 : %s\n", fcs & (1 << 18) ? "yes" : "no");
6430 fprintf_filtered (file, "abs2008 : %s\n", fcs & (1 << 19) ? "yes" : "no");
6431 fputc_filtered ('\n', file);
6432
6433 default_print_float_info (gdbarch, file, frame, args);
6434 }
6435
6436 /* Replacement for generic do_registers_info.
6437 Print regs in pretty columns. */
6438
6439 static int
6440 print_fp_register_row (struct ui_file *file, struct frame_info *frame,
6441 int regnum)
6442 {
6443 fprintf_filtered (file, " ");
6444 mips_print_fp_register (file, frame, regnum);
6445 fprintf_filtered (file, "\n");
6446 return regnum + 1;
6447 }
6448
6449
6450 /* Print a row's worth of GP (int) registers, with name labels above. */
6451
6452 static int
6453 print_gp_register_row (struct ui_file *file, struct frame_info *frame,
6454 int start_regnum)
6455 {
6456 struct gdbarch *gdbarch = get_frame_arch (frame);
6457 /* Do values for GP (int) regs. */
6458 gdb_byte raw_buffer[MAX_REGISTER_SIZE];
6459 int ncols = (mips_abi_regsize (gdbarch) == 8 ? 4 : 8); /* display cols
6460 per row. */
6461 int col, byte;
6462 int regnum;
6463
6464 /* For GP registers, we print a separate row of names above the vals. */
6465 for (col = 0, regnum = start_regnum;
6466 col < ncols && regnum < gdbarch_num_regs (gdbarch)
6467 + gdbarch_num_pseudo_regs (gdbarch);
6468 regnum++)
6469 {
6470 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
6471 continue; /* unused register */
6472 if (mips_float_register_p (gdbarch, regnum))
6473 break; /* End the row: reached FP register. */
6474 /* Large registers are handled separately. */
6475 if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
6476 {
6477 if (col > 0)
6478 break; /* End the row before this register. */
6479
6480 /* Print this register on a row by itself. */
6481 mips_print_register (file, frame, regnum);
6482 fprintf_filtered (file, "\n");
6483 return regnum + 1;
6484 }
6485 if (col == 0)
6486 fprintf_filtered (file, " ");
6487 fprintf_filtered (file,
6488 mips_abi_regsize (gdbarch) == 8 ? "%17s" : "%9s",
6489 gdbarch_register_name (gdbarch, regnum));
6490 col++;
6491 }
6492
6493 if (col == 0)
6494 return regnum;
6495
6496 /* Print the R0 to R31 names. */
6497 if ((start_regnum % gdbarch_num_regs (gdbarch)) < MIPS_NUMREGS)
6498 fprintf_filtered (file, "\n R%-4d",
6499 start_regnum % gdbarch_num_regs (gdbarch));
6500 else
6501 fprintf_filtered (file, "\n ");
6502
6503 /* Now print the values in hex, 4 or 8 to the row. */
6504 for (col = 0, regnum = start_regnum;
6505 col < ncols && regnum < gdbarch_num_regs (gdbarch)
6506 + gdbarch_num_pseudo_regs (gdbarch);
6507 regnum++)
6508 {
6509 if (*gdbarch_register_name (gdbarch, regnum) == '\0')
6510 continue; /* unused register */
6511 if (mips_float_register_p (gdbarch, regnum))
6512 break; /* End row: reached FP register. */
6513 if (register_size (gdbarch, regnum) > mips_abi_regsize (gdbarch))
6514 break; /* End row: large register. */
6515
6516 /* OK: get the data in raw format. */
6517 if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
6518 error (_("can't read register %d (%s)"),
6519 regnum, gdbarch_register_name (gdbarch, regnum));
6520 /* pad small registers */
6521 for (byte = 0;
6522 byte < (mips_abi_regsize (gdbarch)
6523 - register_size (gdbarch, regnum)); byte++)
6524 printf_filtered (" ");
6525 /* Now print the register value in hex, endian order. */
6526 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
6527 for (byte =
6528 register_size (gdbarch, regnum) - register_size (gdbarch, regnum);
6529 byte < register_size (gdbarch, regnum); byte++)
6530 fprintf_filtered (file, "%02x", raw_buffer[byte]);
6531 else
6532 for (byte = register_size (gdbarch, regnum) - 1;
6533 byte >= 0; byte--)
6534 fprintf_filtered (file, "%02x", raw_buffer[byte]);
6535 fprintf_filtered (file, " ");
6536 col++;
6537 }
6538 if (col > 0) /* ie. if we actually printed anything... */
6539 fprintf_filtered (file, "\n");
6540
6541 return regnum;
6542 }
6543
6544 /* MIPS_DO_REGISTERS_INFO(): called by "info register" command. */
6545
6546 static void
6547 mips_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
6548 struct frame_info *frame, int regnum, int all)
6549 {
6550 if (regnum != -1) /* Do one specified register. */
6551 {
6552 gdb_assert (regnum >= gdbarch_num_regs (gdbarch));
6553 if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
6554 error (_("Not a valid register for the current processor type"));
6555
6556 mips_print_register (file, frame, regnum);
6557 fprintf_filtered (file, "\n");
6558 }
6559 else
6560 /* Do all (or most) registers. */
6561 {
6562 regnum = gdbarch_num_regs (gdbarch);
6563 while (regnum < gdbarch_num_regs (gdbarch)
6564 + gdbarch_num_pseudo_regs (gdbarch))
6565 {
6566 if (mips_float_register_p (gdbarch, regnum))
6567 {
6568 if (all) /* True for "INFO ALL-REGISTERS" command. */
6569 regnum = print_fp_register_row (file, frame, regnum);
6570 else
6571 regnum += MIPS_NUMREGS; /* Skip floating point regs. */
6572 }
6573 else
6574 regnum = print_gp_register_row (file, frame, regnum);
6575 }
6576 }
6577 }
6578
6579 static int
6580 mips_single_step_through_delay (struct gdbarch *gdbarch,
6581 struct frame_info *frame)
6582 {
6583 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6584 CORE_ADDR pc = get_frame_pc (frame);
6585 struct address_space *aspace;
6586 enum mips_isa isa;
6587 ULONGEST insn;
6588 int status;
6589 int size;
6590
6591 if ((mips_pc_is_mips (pc)
6592 && !mips32_insn_at_pc_has_delay_slot (gdbarch, pc))
6593 || (mips_pc_is_micromips (gdbarch, pc)
6594 && !micromips_insn_at_pc_has_delay_slot (gdbarch, pc, 0))
6595 || (mips_pc_is_mips16 (gdbarch, pc)
6596 && !mips16_insn_at_pc_has_delay_slot (gdbarch, pc, 0)))
6597 return 0;
6598
6599 isa = mips_pc_isa (gdbarch, pc);
6600 /* _has_delay_slot above will have validated the read. */
6601 insn = mips_fetch_instruction (gdbarch, isa, pc, NULL);
6602 size = mips_insn_size (isa, insn);
6603 aspace = get_frame_address_space (frame);
6604 return breakpoint_here_p (aspace, pc + size) != no_breakpoint_here;
6605 }
6606
6607 /* To skip prologues, I use this predicate. Returns either PC itself
6608 if the code at PC does not look like a function prologue; otherwise
6609 returns an address that (if we're lucky) follows the prologue. If
6610 LENIENT, then we must skip everything which is involved in setting
6611 up the frame (it's OK to skip more, just so long as we don't skip
6612 anything which might clobber the registers which are being saved.
6613 We must skip more in the case where part of the prologue is in the
6614 delay slot of a non-prologue instruction). */
6615
6616 static CORE_ADDR
6617 mips_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
6618 {
6619 CORE_ADDR limit_pc;
6620 CORE_ADDR func_addr;
6621
6622 /* See if we can determine the end of the prologue via the symbol table.
6623 If so, then return either PC, or the PC after the prologue, whichever
6624 is greater. */
6625 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
6626 {
6627 CORE_ADDR post_prologue_pc
6628 = skip_prologue_using_sal (gdbarch, func_addr);
6629 if (post_prologue_pc != 0)
6630 return std::max (pc, post_prologue_pc);
6631 }
6632
6633 /* Can't determine prologue from the symbol table, need to examine
6634 instructions. */
6635
6636 /* Find an upper limit on the function prologue using the debug
6637 information. If the debug information could not be used to provide
6638 that bound, then use an arbitrary large number as the upper bound. */
6639 limit_pc = skip_prologue_using_sal (gdbarch, pc);
6640 if (limit_pc == 0)
6641 limit_pc = pc + 100; /* Magic. */
6642
6643 if (mips_pc_is_mips16 (gdbarch, pc))
6644 return mips16_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6645 else if (mips_pc_is_micromips (gdbarch, pc))
6646 return micromips_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6647 else
6648 return mips32_scan_prologue (gdbarch, pc, limit_pc, NULL, NULL);
6649 }
6650
6651 /* Implement the stack_frame_destroyed_p gdbarch method (32-bit version).
6652 This is a helper function for mips_stack_frame_destroyed_p. */
6653
6654 static int
6655 mips32_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6656 {
6657 CORE_ADDR func_addr = 0, func_end = 0;
6658
6659 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6660 {
6661 /* The MIPS epilogue is max. 12 bytes long. */
6662 CORE_ADDR addr = func_end - 12;
6663
6664 if (addr < func_addr + 4)
6665 addr = func_addr + 4;
6666 if (pc < addr)
6667 return 0;
6668
6669 for (; pc < func_end; pc += MIPS_INSN32_SIZE)
6670 {
6671 unsigned long high_word;
6672 unsigned long inst;
6673
6674 inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
6675 high_word = (inst >> 16) & 0xffff;
6676
6677 if (high_word != 0x27bd /* addiu $sp,$sp,offset */
6678 && high_word != 0x67bd /* daddiu $sp,$sp,offset */
6679 && inst != 0x03e00008 /* jr $ra */
6680 && inst != 0x00000000) /* nop */
6681 return 0;
6682 }
6683
6684 return 1;
6685 }
6686
6687 return 0;
6688 }
6689
6690 /* Implement the stack_frame_destroyed_p gdbarch method (microMIPS version).
6691 This is a helper function for mips_stack_frame_destroyed_p. */
6692
6693 static int
6694 micromips_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6695 {
6696 CORE_ADDR func_addr = 0;
6697 CORE_ADDR func_end = 0;
6698 CORE_ADDR addr;
6699 ULONGEST insn;
6700 long offset;
6701 int dreg;
6702 int sreg;
6703 int loc;
6704
6705 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6706 return 0;
6707
6708 /* The microMIPS epilogue is max. 12 bytes long. */
6709 addr = func_end - 12;
6710
6711 if (addr < func_addr + 2)
6712 addr = func_addr + 2;
6713 if (pc < addr)
6714 return 0;
6715
6716 for (; pc < func_end; pc += loc)
6717 {
6718 loc = 0;
6719 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, NULL);
6720 loc += MIPS_INSN16_SIZE;
6721 switch (mips_insn_size (ISA_MICROMIPS, insn))
6722 {
6723 /* 32-bit instructions. */
6724 case 2 * MIPS_INSN16_SIZE:
6725 insn <<= 16;
6726 insn |= mips_fetch_instruction (gdbarch,
6727 ISA_MICROMIPS, pc + loc, NULL);
6728 loc += MIPS_INSN16_SIZE;
6729 switch (micromips_op (insn >> 16))
6730 {
6731 case 0xc: /* ADDIU: bits 001100 */
6732 case 0x17: /* DADDIU: bits 010111 */
6733 sreg = b0s5_reg (insn >> 16);
6734 dreg = b5s5_reg (insn >> 16);
6735 offset = (b0s16_imm (insn) ^ 0x8000) - 0x8000;
6736 if (sreg == MIPS_SP_REGNUM && dreg == MIPS_SP_REGNUM
6737 /* (D)ADDIU $sp, imm */
6738 && offset >= 0)
6739 break;
6740 return 0;
6741
6742 default:
6743 return 0;
6744 }
6745 break;
6746
6747 /* 16-bit instructions. */
6748 case MIPS_INSN16_SIZE:
6749 switch (micromips_op (insn))
6750 {
6751 case 0x3: /* MOVE: bits 000011 */
6752 sreg = b0s5_reg (insn);
6753 dreg = b5s5_reg (insn);
6754 if (sreg == 0 && dreg == 0)
6755 /* MOVE $zero, $zero aka NOP */
6756 break;
6757 return 0;
6758
6759 case 0x11: /* POOL16C: bits 010001 */
6760 if (b5s5_op (insn) == 0x18
6761 /* JRADDIUSP: bits 010011 11000 */
6762 || (b5s5_op (insn) == 0xd
6763 /* JRC: bits 010011 01101 */
6764 && b0s5_reg (insn) == MIPS_RA_REGNUM))
6765 /* JRC $ra */
6766 break;
6767 return 0;
6768
6769 case 0x13: /* POOL16D: bits 010011 */
6770 offset = micromips_decode_imm9 (b1s9_imm (insn));
6771 if ((insn & 0x1) == 0x1
6772 /* ADDIUSP: bits 010011 1 */
6773 && offset > 0)
6774 break;
6775 return 0;
6776
6777 default:
6778 return 0;
6779 }
6780 }
6781 }
6782
6783 return 1;
6784 }
6785
6786 /* Implement the stack_frame_destroyed_p gdbarch method (16-bit version).
6787 This is a helper function for mips_stack_frame_destroyed_p. */
6788
6789 static int
6790 mips16_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6791 {
6792 CORE_ADDR func_addr = 0, func_end = 0;
6793
6794 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
6795 {
6796 /* The MIPS epilogue is max. 12 bytes long. */
6797 CORE_ADDR addr = func_end - 12;
6798
6799 if (addr < func_addr + 4)
6800 addr = func_addr + 4;
6801 if (pc < addr)
6802 return 0;
6803
6804 for (; pc < func_end; pc += MIPS_INSN16_SIZE)
6805 {
6806 unsigned short inst;
6807
6808 inst = mips_fetch_instruction (gdbarch, ISA_MIPS16, pc, NULL);
6809
6810 if ((inst & 0xf800) == 0xf000) /* extend */
6811 continue;
6812
6813 if (inst != 0x6300 /* addiu $sp,offset */
6814 && inst != 0xfb00 /* daddiu $sp,$sp,offset */
6815 && inst != 0xe820 /* jr $ra */
6816 && inst != 0xe8a0 /* jrc $ra */
6817 && inst != 0x6500) /* nop */
6818 return 0;
6819 }
6820
6821 return 1;
6822 }
6823
6824 return 0;
6825 }
6826
6827 /* Implement the stack_frame_destroyed_p gdbarch method.
6828
6829 The epilogue is defined here as the area at the end of a function,
6830 after an instruction which destroys the function's stack frame. */
6831
6832 static int
6833 mips_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
6834 {
6835 if (mips_pc_is_mips16 (gdbarch, pc))
6836 return mips16_stack_frame_destroyed_p (gdbarch, pc);
6837 else if (mips_pc_is_micromips (gdbarch, pc))
6838 return micromips_stack_frame_destroyed_p (gdbarch, pc);
6839 else
6840 return mips32_stack_frame_destroyed_p (gdbarch, pc);
6841 }
6842
6843 /* Root of all "set mips "/"show mips " commands. This will eventually be
6844 used for all MIPS-specific commands. */
6845
6846 static void
6847 show_mips_command (char *args, int from_tty)
6848 {
6849 help_list (showmipscmdlist, "show mips ", all_commands, gdb_stdout);
6850 }
6851
6852 static void
6853 set_mips_command (char *args, int from_tty)
6854 {
6855 printf_unfiltered
6856 ("\"set mips\" must be followed by an appropriate subcommand.\n");
6857 help_list (setmipscmdlist, "set mips ", all_commands, gdb_stdout);
6858 }
6859
6860 /* Commands to show/set the MIPS FPU type. */
6861
6862 static void
6863 show_mipsfpu_command (char *args, int from_tty)
6864 {
6865 char *fpu;
6866
6867 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
6868 {
6869 printf_unfiltered
6870 ("The MIPS floating-point coprocessor is unknown "
6871 "because the current architecture is not MIPS.\n");
6872 return;
6873 }
6874
6875 switch (MIPS_FPU_TYPE (target_gdbarch ()))
6876 {
6877 case MIPS_FPU_SINGLE:
6878 fpu = "single-precision";
6879 break;
6880 case MIPS_FPU_DOUBLE:
6881 fpu = "double-precision";
6882 break;
6883 case MIPS_FPU_NONE:
6884 fpu = "absent (none)";
6885 break;
6886 default:
6887 internal_error (__FILE__, __LINE__, _("bad switch"));
6888 }
6889 if (mips_fpu_type_auto)
6890 printf_unfiltered ("The MIPS floating-point coprocessor "
6891 "is set automatically (currently %s)\n",
6892 fpu);
6893 else
6894 printf_unfiltered
6895 ("The MIPS floating-point coprocessor is assumed to be %s\n", fpu);
6896 }
6897
6898
6899 static void
6900 set_mipsfpu_command (char *args, int from_tty)
6901 {
6902 printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", "
6903 "\"single\",\"none\" or \"auto\".\n");
6904 show_mipsfpu_command (args, from_tty);
6905 }
6906
6907 static void
6908 set_mipsfpu_single_command (char *args, int from_tty)
6909 {
6910 struct gdbarch_info info;
6911 gdbarch_info_init (&info);
6912 mips_fpu_type = MIPS_FPU_SINGLE;
6913 mips_fpu_type_auto = 0;
6914 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6915 instead of relying on globals. Doing that would let generic code
6916 handle the search for this specific architecture. */
6917 if (!gdbarch_update_p (info))
6918 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6919 }
6920
6921 static void
6922 set_mipsfpu_double_command (char *args, int from_tty)
6923 {
6924 struct gdbarch_info info;
6925 gdbarch_info_init (&info);
6926 mips_fpu_type = MIPS_FPU_DOUBLE;
6927 mips_fpu_type_auto = 0;
6928 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6929 instead of relying on globals. Doing that would let generic code
6930 handle the search for this specific architecture. */
6931 if (!gdbarch_update_p (info))
6932 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6933 }
6934
6935 static void
6936 set_mipsfpu_none_command (char *args, int from_tty)
6937 {
6938 struct gdbarch_info info;
6939 gdbarch_info_init (&info);
6940 mips_fpu_type = MIPS_FPU_NONE;
6941 mips_fpu_type_auto = 0;
6942 /* FIXME: cagney/2003-11-15: Should be setting a field in "info"
6943 instead of relying on globals. Doing that would let generic code
6944 handle the search for this specific architecture. */
6945 if (!gdbarch_update_p (info))
6946 internal_error (__FILE__, __LINE__, _("set mipsfpu failed"));
6947 }
6948
6949 static void
6950 set_mipsfpu_auto_command (char *args, int from_tty)
6951 {
6952 mips_fpu_type_auto = 1;
6953 }
6954
6955 /* Just like reinit_frame_cache, but with the right arguments to be
6956 callable as an sfunc. */
6957
6958 static void
6959 reinit_frame_cache_sfunc (char *args, int from_tty,
6960 struct cmd_list_element *c)
6961 {
6962 reinit_frame_cache ();
6963 }
6964
6965 static int
6966 gdb_print_insn_mips (bfd_vma memaddr, struct disassemble_info *info)
6967 {
6968 struct gdbarch *gdbarch = (struct gdbarch *) info->application_data;
6969
6970 /* FIXME: cagney/2003-06-26: Is this even necessary? The
6971 disassembler needs to be able to locally determine the ISA, and
6972 not rely on GDB. Otherwize the stand-alone 'objdump -d' will not
6973 work. */
6974 if (mips_pc_is_mips16 (gdbarch, memaddr))
6975 info->mach = bfd_mach_mips16;
6976 else if (mips_pc_is_micromips (gdbarch, memaddr))
6977 info->mach = bfd_mach_mips_micromips;
6978
6979 /* Round down the instruction address to the appropriate boundary. */
6980 memaddr &= (info->mach == bfd_mach_mips16
6981 || info->mach == bfd_mach_mips_micromips) ? ~1 : ~3;
6982
6983 /* Set the disassembler options. */
6984 if (!info->disassembler_options)
6985 /* This string is not recognized explicitly by the disassembler,
6986 but it tells the disassembler to not try to guess the ABI from
6987 the bfd elf headers, such that, if the user overrides the ABI
6988 of a program linked as NewABI, the disassembly will follow the
6989 register naming conventions specified by the user. */
6990 info->disassembler_options = "gpr-names=32";
6991
6992 /* Call the appropriate disassembler based on the target endian-ness. */
6993 if (info->endian == BFD_ENDIAN_BIG)
6994 return print_insn_big_mips (memaddr, info);
6995 else
6996 return print_insn_little_mips (memaddr, info);
6997 }
6998
6999 static int
7000 gdb_print_insn_mips_n32 (bfd_vma memaddr, struct disassemble_info *info)
7001 {
7002 /* Set up the disassembler info, so that we get the right
7003 register names from libopcodes. */
7004 info->disassembler_options = "gpr-names=n32";
7005 info->flavour = bfd_target_elf_flavour;
7006
7007 return gdb_print_insn_mips (memaddr, info);
7008 }
7009
7010 static int
7011 gdb_print_insn_mips_n64 (bfd_vma memaddr, struct disassemble_info *info)
7012 {
7013 /* Set up the disassembler info, so that we get the right
7014 register names from libopcodes. */
7015 info->disassembler_options = "gpr-names=64";
7016 info->flavour = bfd_target_elf_flavour;
7017
7018 return gdb_print_insn_mips (memaddr, info);
7019 }
7020
7021 /* This function implements gdbarch_breakpoint_from_pc. It uses the
7022 program counter value to determine whether a 16- or 32-bit breakpoint
7023 should be used. It returns a pointer to a string of bytes that encode a
7024 breakpoint instruction, stores the length of the string to *lenptr, and
7025 adjusts pc (if necessary) to point to the actual memory location where
7026 the breakpoint should be inserted. */
7027
7028 static const gdb_byte *
7029 mips_breakpoint_from_pc (struct gdbarch *gdbarch,
7030 CORE_ADDR *pcptr, int *lenptr)
7031 {
7032 CORE_ADDR pc = *pcptr;
7033
7034 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
7035 {
7036 if (mips_pc_is_mips16 (gdbarch, pc))
7037 {
7038 static gdb_byte mips16_big_breakpoint[] = { 0xe8, 0xa5 };
7039 *pcptr = unmake_compact_addr (pc);
7040 *lenptr = sizeof (mips16_big_breakpoint);
7041 return mips16_big_breakpoint;
7042 }
7043 else if (mips_pc_is_micromips (gdbarch, pc))
7044 {
7045 static gdb_byte micromips16_big_breakpoint[] = { 0x46, 0x85 };
7046 static gdb_byte micromips32_big_breakpoint[] = { 0, 0x5, 0, 0x7 };
7047 ULONGEST insn;
7048 int err;
7049 int size;
7050
7051 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &err);
7052 size = err ? 2 : mips_insn_size (ISA_MICROMIPS, insn);
7053 *pcptr = unmake_compact_addr (pc);
7054 *lenptr = size;
7055 return (size == 2) ? micromips16_big_breakpoint
7056 : micromips32_big_breakpoint;
7057 }
7058 else
7059 {
7060 static gdb_byte big_breakpoint[] = { 0, 0x5, 0, 0xd };
7061
7062 *lenptr = sizeof (big_breakpoint);
7063 return big_breakpoint;
7064 }
7065 }
7066 else
7067 {
7068 if (mips_pc_is_mips16 (gdbarch, pc))
7069 {
7070 static gdb_byte mips16_little_breakpoint[] = { 0xa5, 0xe8 };
7071 *pcptr = unmake_compact_addr (pc);
7072 *lenptr = sizeof (mips16_little_breakpoint);
7073 return mips16_little_breakpoint;
7074 }
7075 else if (mips_pc_is_micromips (gdbarch, pc))
7076 {
7077 static gdb_byte micromips16_little_breakpoint[] = { 0x85, 0x46 };
7078 static gdb_byte micromips32_little_breakpoint[] = { 0x5, 0, 0x7, 0 };
7079 ULONGEST insn;
7080 int err;
7081 int size;
7082
7083 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &err);
7084 size = err ? 2 : mips_insn_size (ISA_MICROMIPS, insn);
7085 *pcptr = unmake_compact_addr (pc);
7086 *lenptr = size;
7087 return (size == 2) ? micromips16_little_breakpoint
7088 : micromips32_little_breakpoint;
7089 }
7090 else
7091 {
7092 static gdb_byte little_breakpoint[] = { 0xd, 0, 0x5, 0 };
7093
7094 *lenptr = sizeof (little_breakpoint);
7095 return little_breakpoint;
7096 }
7097 }
7098 }
7099
7100 /* Determine the remote breakpoint kind suitable for the PC. The following
7101 kinds are used:
7102
7103 * 2 -- 16-bit MIPS16 mode breakpoint,
7104
7105 * 3 -- 16-bit microMIPS mode breakpoint,
7106
7107 * 4 -- 32-bit standard MIPS mode breakpoint,
7108
7109 * 5 -- 32-bit microMIPS mode breakpoint. */
7110
7111 static void
7112 mips_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
7113 int *kindptr)
7114 {
7115 CORE_ADDR pc = *pcptr;
7116
7117 if (mips_pc_is_mips16 (gdbarch, pc))
7118 {
7119 *pcptr = unmake_compact_addr (pc);
7120 *kindptr = 2;
7121 }
7122 else if (mips_pc_is_micromips (gdbarch, pc))
7123 {
7124 ULONGEST insn;
7125 int status;
7126 int size;
7127
7128 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, pc, &status);
7129 size = status ? 2 : mips_insn_size (ISA_MICROMIPS, insn) == 2 ? 2 : 4;
7130 *pcptr = unmake_compact_addr (pc);
7131 *kindptr = size | 1;
7132 }
7133 else
7134 *kindptr = 4;
7135 }
7136
7137 /* Return non-zero if the standard MIPS instruction INST has a branch
7138 delay slot (i.e. it is a jump or branch instruction). This function
7139 is based on mips32_next_pc. */
7140
7141 static int
7142 mips32_instruction_has_delay_slot (struct gdbarch *gdbarch, ULONGEST inst)
7143 {
7144 int op;
7145 int rs;
7146 int rt;
7147
7148 op = itype_op (inst);
7149 if ((inst & 0xe0000000) != 0)
7150 {
7151 rs = itype_rs (inst);
7152 rt = itype_rt (inst);
7153 return (is_octeon_bbit_op (op, gdbarch)
7154 || op >> 2 == 5 /* BEQL, BNEL, BLEZL, BGTZL: bits 0101xx */
7155 || op == 29 /* JALX: bits 011101 */
7156 || (op == 17
7157 && (rs == 8
7158 /* BC1F, BC1FL, BC1T, BC1TL: 010001 01000 */
7159 || (rs == 9 && (rt & 0x2) == 0)
7160 /* BC1ANY2F, BC1ANY2T: bits 010001 01001 */
7161 || (rs == 10 && (rt & 0x2) == 0))));
7162 /* BC1ANY4F, BC1ANY4T: bits 010001 01010 */
7163 }
7164 else
7165 switch (op & 0x07) /* extract bits 28,27,26 */
7166 {
7167 case 0: /* SPECIAL */
7168 op = rtype_funct (inst);
7169 return (op == 8 /* JR */
7170 || op == 9); /* JALR */
7171 break; /* end SPECIAL */
7172 case 1: /* REGIMM */
7173 rs = itype_rs (inst);
7174 rt = itype_rt (inst); /* branch condition */
7175 return ((rt & 0xc) == 0
7176 /* BLTZ, BLTZL, BGEZ, BGEZL: bits 000xx */
7177 /* BLTZAL, BLTZALL, BGEZAL, BGEZALL: 100xx */
7178 || ((rt & 0x1e) == 0x1c && rs == 0));
7179 /* BPOSGE32, BPOSGE64: bits 1110x */
7180 break; /* end REGIMM */
7181 default: /* J, JAL, BEQ, BNE, BLEZ, BGTZ */
7182 return 1;
7183 break;
7184 }
7185 }
7186
7187 /* Return non-zero if a standard MIPS instruction at ADDR has a branch
7188 delay slot (i.e. it is a jump or branch instruction). */
7189
7190 static int
7191 mips32_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch, CORE_ADDR addr)
7192 {
7193 ULONGEST insn;
7194 int status;
7195
7196 insn = mips_fetch_instruction (gdbarch, ISA_MIPS, addr, &status);
7197 if (status)
7198 return 0;
7199
7200 return mips32_instruction_has_delay_slot (gdbarch, insn);
7201 }
7202
7203 /* Return non-zero if the microMIPS instruction INSN, comprising the
7204 16-bit major opcode word in the high 16 bits and any second word
7205 in the low 16 bits, has a branch delay slot (i.e. it is a non-compact
7206 jump or branch instruction). The instruction must be 32-bit if
7207 MUSTBE32 is set or can be any instruction otherwise. */
7208
7209 static int
7210 micromips_instruction_has_delay_slot (ULONGEST insn, int mustbe32)
7211 {
7212 ULONGEST major = insn >> 16;
7213
7214 switch (micromips_op (major))
7215 {
7216 /* 16-bit instructions. */
7217 case 0x33: /* B16: bits 110011 */
7218 case 0x2b: /* BNEZ16: bits 101011 */
7219 case 0x23: /* BEQZ16: bits 100011 */
7220 return !mustbe32;
7221 case 0x11: /* POOL16C: bits 010001 */
7222 return (!mustbe32
7223 && ((b5s5_op (major) == 0xc
7224 /* JR16: bits 010001 01100 */
7225 || (b5s5_op (major) & 0x1e) == 0xe)));
7226 /* JALR16, JALRS16: bits 010001 0111x */
7227 /* 32-bit instructions. */
7228 case 0x3d: /* JAL: bits 111101 */
7229 case 0x3c: /* JALX: bits 111100 */
7230 case 0x35: /* J: bits 110101 */
7231 case 0x2d: /* BNE: bits 101101 */
7232 case 0x25: /* BEQ: bits 100101 */
7233 case 0x1d: /* JALS: bits 011101 */
7234 return 1;
7235 case 0x10: /* POOL32I: bits 010000 */
7236 return ((b5s5_op (major) & 0x1c) == 0x0
7237 /* BLTZ, BLTZAL, BGEZ, BGEZAL: 010000 000xx */
7238 || (b5s5_op (major) & 0x1d) == 0x4
7239 /* BLEZ, BGTZ: bits 010000 001x0 */
7240 || (b5s5_op (major) & 0x1d) == 0x11
7241 /* BLTZALS, BGEZALS: bits 010000 100x1 */
7242 || ((b5s5_op (major) & 0x1e) == 0x14
7243 && (major & 0x3) == 0x0)
7244 /* BC2F, BC2T: bits 010000 1010x xxx00 */
7245 || (b5s5_op (major) & 0x1e) == 0x1a
7246 /* BPOSGE64, BPOSGE32: bits 010000 1101x */
7247 || ((b5s5_op (major) & 0x1e) == 0x1c
7248 && (major & 0x3) == 0x0)
7249 /* BC1F, BC1T: bits 010000 1110x xxx00 */
7250 || ((b5s5_op (major) & 0x1c) == 0x1c
7251 && (major & 0x3) == 0x1));
7252 /* BC1ANY*: bits 010000 111xx xxx01 */
7253 case 0x0: /* POOL32A: bits 000000 */
7254 return (b0s6_op (insn) == 0x3c
7255 /* POOL32Axf: bits 000000 ... 111100 */
7256 && (b6s10_ext (insn) & 0x2bf) == 0x3c);
7257 /* JALR, JALR.HB: 000000 000x111100 111100 */
7258 /* JALRS, JALRS.HB: 000000 010x111100 111100 */
7259 default:
7260 return 0;
7261 }
7262 }
7263
7264 /* Return non-zero if a microMIPS instruction at ADDR has a branch delay
7265 slot (i.e. it is a non-compact jump instruction). The instruction
7266 must be 32-bit if MUSTBE32 is set or can be any instruction otherwise. */
7267
7268 static int
7269 micromips_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
7270 CORE_ADDR addr, int mustbe32)
7271 {
7272 ULONGEST insn;
7273 int status;
7274 int size;
7275
7276 insn = mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
7277 if (status)
7278 return 0;
7279 size = mips_insn_size (ISA_MICROMIPS, insn);
7280 insn <<= 16;
7281 if (size == 2 * MIPS_INSN16_SIZE)
7282 {
7283 insn |= mips_fetch_instruction (gdbarch, ISA_MICROMIPS, addr, &status);
7284 if (status)
7285 return 0;
7286 }
7287
7288 return micromips_instruction_has_delay_slot (insn, mustbe32);
7289 }
7290
7291 /* Return non-zero if the MIPS16 instruction INST, which must be
7292 a 32-bit instruction if MUSTBE32 is set or can be any instruction
7293 otherwise, has a branch delay slot (i.e. it is a non-compact jump
7294 instruction). This function is based on mips16_next_pc. */
7295
7296 static int
7297 mips16_instruction_has_delay_slot (unsigned short inst, int mustbe32)
7298 {
7299 if ((inst & 0xf89f) == 0xe800) /* JR/JALR (16-bit instruction) */
7300 return !mustbe32;
7301 return (inst & 0xf800) == 0x1800; /* JAL/JALX (32-bit instruction) */
7302 }
7303
7304 /* Return non-zero if a MIPS16 instruction at ADDR has a branch delay
7305 slot (i.e. it is a non-compact jump instruction). The instruction
7306 must be 32-bit if MUSTBE32 is set or can be any instruction otherwise. */
7307
7308 static int
7309 mips16_insn_at_pc_has_delay_slot (struct gdbarch *gdbarch,
7310 CORE_ADDR addr, int mustbe32)
7311 {
7312 unsigned short insn;
7313 int status;
7314
7315 insn = mips_fetch_instruction (gdbarch, ISA_MIPS16, addr, &status);
7316 if (status)
7317 return 0;
7318
7319 return mips16_instruction_has_delay_slot (insn, mustbe32);
7320 }
7321
7322 /* Calculate the starting address of the MIPS memory segment BPADDR is in.
7323 This assumes KSSEG exists. */
7324
7325 static CORE_ADDR
7326 mips_segment_boundary (CORE_ADDR bpaddr)
7327 {
7328 CORE_ADDR mask = CORE_ADDR_MAX;
7329 int segsize;
7330
7331 if (sizeof (CORE_ADDR) == 8)
7332 /* Get the topmost two bits of bpaddr in a 32-bit safe manner (avoid
7333 a compiler warning produced where CORE_ADDR is a 32-bit type even
7334 though in that case this is dead code). */
7335 switch (bpaddr >> ((sizeof (CORE_ADDR) << 3) - 2) & 3)
7336 {
7337 case 3:
7338 if (bpaddr == (bfd_signed_vma) (int32_t) bpaddr)
7339 segsize = 29; /* 32-bit compatibility segment */
7340 else
7341 segsize = 62; /* xkseg */
7342 break;
7343 case 2: /* xkphys */
7344 segsize = 59;
7345 break;
7346 default: /* xksseg (1), xkuseg/kuseg (0) */
7347 segsize = 62;
7348 break;
7349 }
7350 else if (bpaddr & 0x80000000) /* kernel segment */
7351 segsize = 29;
7352 else
7353 segsize = 31; /* user segment */
7354 mask <<= segsize;
7355 return bpaddr & mask;
7356 }
7357
7358 /* Move the breakpoint at BPADDR out of any branch delay slot by shifting
7359 it backwards if necessary. Return the address of the new location. */
7360
7361 static CORE_ADDR
7362 mips_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
7363 {
7364 CORE_ADDR prev_addr;
7365 CORE_ADDR boundary;
7366 CORE_ADDR func_addr;
7367
7368 /* If a breakpoint is set on the instruction in a branch delay slot,
7369 GDB gets confused. When the breakpoint is hit, the PC isn't on
7370 the instruction in the branch delay slot, the PC will point to
7371 the branch instruction. Since the PC doesn't match any known
7372 breakpoints, GDB reports a trap exception.
7373
7374 There are two possible fixes for this problem.
7375
7376 1) When the breakpoint gets hit, see if the BD bit is set in the
7377 Cause register (which indicates the last exception occurred in a
7378 branch delay slot). If the BD bit is set, fix the PC to point to
7379 the instruction in the branch delay slot.
7380
7381 2) When the user sets the breakpoint, don't allow him to set the
7382 breakpoint on the instruction in the branch delay slot. Instead
7383 move the breakpoint to the branch instruction (which will have
7384 the same result).
7385
7386 The problem with the first solution is that if the user then
7387 single-steps the processor, the branch instruction will get
7388 skipped (since GDB thinks the PC is on the instruction in the
7389 branch delay slot).
7390
7391 So, we'll use the second solution. To do this we need to know if
7392 the instruction we're trying to set the breakpoint on is in the
7393 branch delay slot. */
7394
7395 boundary = mips_segment_boundary (bpaddr);
7396
7397 /* Make sure we don't scan back before the beginning of the current
7398 function, since we may fetch constant data or insns that look like
7399 a jump. Of course we might do that anyway if the compiler has
7400 moved constants inline. :-( */
7401 if (find_pc_partial_function (bpaddr, NULL, &func_addr, NULL)
7402 && func_addr > boundary && func_addr <= bpaddr)
7403 boundary = func_addr;
7404
7405 if (mips_pc_is_mips (bpaddr))
7406 {
7407 if (bpaddr == boundary)
7408 return bpaddr;
7409
7410 /* If the previous instruction has a branch delay slot, we have
7411 to move the breakpoint to the branch instruction. */
7412 prev_addr = bpaddr - 4;
7413 if (mips32_insn_at_pc_has_delay_slot (gdbarch, prev_addr))
7414 bpaddr = prev_addr;
7415 }
7416 else
7417 {
7418 int (*insn_at_pc_has_delay_slot) (struct gdbarch *, CORE_ADDR, int);
7419 CORE_ADDR addr, jmpaddr;
7420 int i;
7421
7422 boundary = unmake_compact_addr (boundary);
7423
7424 /* The only MIPS16 instructions with delay slots are JAL, JALX,
7425 JALR and JR. An absolute JAL/JALX is always 4 bytes long,
7426 so try for that first, then try the 2 byte JALR/JR.
7427 The microMIPS ASE has a whole range of jumps and branches
7428 with delay slots, some of which take 4 bytes and some take
7429 2 bytes, so the idea is the same.
7430 FIXME: We have to assume that bpaddr is not the second half
7431 of an extended instruction. */
7432 insn_at_pc_has_delay_slot = (mips_pc_is_micromips (gdbarch, bpaddr)
7433 ? micromips_insn_at_pc_has_delay_slot
7434 : mips16_insn_at_pc_has_delay_slot);
7435
7436 jmpaddr = 0;
7437 addr = bpaddr;
7438 for (i = 1; i < 4; i++)
7439 {
7440 if (unmake_compact_addr (addr) == boundary)
7441 break;
7442 addr -= MIPS_INSN16_SIZE;
7443 if (i == 1 && insn_at_pc_has_delay_slot (gdbarch, addr, 0))
7444 /* Looks like a JR/JALR at [target-1], but it could be
7445 the second word of a previous JAL/JALX, so record it
7446 and check back one more. */
7447 jmpaddr = addr;
7448 else if (i > 1 && insn_at_pc_has_delay_slot (gdbarch, addr, 1))
7449 {
7450 if (i == 2)
7451 /* Looks like a JAL/JALX at [target-2], but it could also
7452 be the second word of a previous JAL/JALX, record it,
7453 and check back one more. */
7454 jmpaddr = addr;
7455 else
7456 /* Looks like a JAL/JALX at [target-3], so any previously
7457 recorded JAL/JALX or JR/JALR must be wrong, because:
7458
7459 >-3: JAL
7460 -2: JAL-ext (can't be JAL/JALX)
7461 -1: bdslot (can't be JR/JALR)
7462 0: target insn
7463
7464 Of course it could be another JAL-ext which looks
7465 like a JAL, but in that case we'd have broken out
7466 of this loop at [target-2]:
7467
7468 -4: JAL
7469 >-3: JAL-ext
7470 -2: bdslot (can't be jmp)
7471 -1: JR/JALR
7472 0: target insn */
7473 jmpaddr = 0;
7474 }
7475 else
7476 {
7477 /* Not a jump instruction: if we're at [target-1] this
7478 could be the second word of a JAL/JALX, so continue;
7479 otherwise we're done. */
7480 if (i > 1)
7481 break;
7482 }
7483 }
7484
7485 if (jmpaddr)
7486 bpaddr = jmpaddr;
7487 }
7488
7489 return bpaddr;
7490 }
7491
7492 /* Return non-zero if SUFFIX is one of the numeric suffixes used for MIPS16
7493 call stubs, one of 1, 2, 5, 6, 9, 10, or, if ZERO is non-zero, also 0. */
7494
7495 static int
7496 mips_is_stub_suffix (const char *suffix, int zero)
7497 {
7498 switch (suffix[0])
7499 {
7500 case '0':
7501 return zero && suffix[1] == '\0';
7502 case '1':
7503 return suffix[1] == '\0' || (suffix[1] == '0' && suffix[2] == '\0');
7504 case '2':
7505 case '5':
7506 case '6':
7507 case '9':
7508 return suffix[1] == '\0';
7509 default:
7510 return 0;
7511 }
7512 }
7513
7514 /* Return non-zero if MODE is one of the mode infixes used for MIPS16
7515 call stubs, one of sf, df, sc, or dc. */
7516
7517 static int
7518 mips_is_stub_mode (const char *mode)
7519 {
7520 return ((mode[0] == 's' || mode[0] == 'd')
7521 && (mode[1] == 'f' || mode[1] == 'c'));
7522 }
7523
7524 /* Code at PC is a compiler-generated stub. Such a stub for a function
7525 bar might have a name like __fn_stub_bar, and might look like this:
7526
7527 mfc1 $4, $f13
7528 mfc1 $5, $f12
7529 mfc1 $6, $f15
7530 mfc1 $7, $f14
7531
7532 followed by (or interspersed with):
7533
7534 j bar
7535
7536 or:
7537
7538 lui $25, %hi(bar)
7539 addiu $25, $25, %lo(bar)
7540 jr $25
7541
7542 ($1 may be used in old code; for robustness we accept any register)
7543 or, in PIC code:
7544
7545 lui $28, %hi(_gp_disp)
7546 addiu $28, $28, %lo(_gp_disp)
7547 addu $28, $28, $25
7548 lw $25, %got(bar)
7549 addiu $25, $25, %lo(bar)
7550 jr $25
7551
7552 In the case of a __call_stub_bar stub, the sequence to set up
7553 arguments might look like this:
7554
7555 mtc1 $4, $f13
7556 mtc1 $5, $f12
7557 mtc1 $6, $f15
7558 mtc1 $7, $f14
7559
7560 followed by (or interspersed with) one of the jump sequences above.
7561
7562 In the case of a __call_stub_fp_bar stub, JAL or JALR is used instead
7563 of J or JR, respectively, followed by:
7564
7565 mfc1 $2, $f0
7566 mfc1 $3, $f1
7567 jr $18
7568
7569 We are at the beginning of the stub here, and scan down and extract
7570 the target address from the jump immediate instruction or, if a jump
7571 register instruction is used, from the register referred. Return
7572 the value of PC calculated or 0 if inconclusive.
7573
7574 The limit on the search is arbitrarily set to 20 instructions. FIXME. */
7575
7576 static CORE_ADDR
7577 mips_get_mips16_fn_stub_pc (struct frame_info *frame, CORE_ADDR pc)
7578 {
7579 struct gdbarch *gdbarch = get_frame_arch (frame);
7580 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7581 int addrreg = MIPS_ZERO_REGNUM;
7582 CORE_ADDR start_pc = pc;
7583 CORE_ADDR target_pc = 0;
7584 CORE_ADDR addr = 0;
7585 CORE_ADDR gp = 0;
7586 int status = 0;
7587 int i;
7588
7589 for (i = 0;
7590 status == 0 && target_pc == 0 && i < 20;
7591 i++, pc += MIPS_INSN32_SIZE)
7592 {
7593 ULONGEST inst = mips_fetch_instruction (gdbarch, ISA_MIPS, pc, NULL);
7594 CORE_ADDR imm;
7595 int rt;
7596 int rs;
7597 int rd;
7598
7599 switch (itype_op (inst))
7600 {
7601 case 0: /* SPECIAL */
7602 switch (rtype_funct (inst))
7603 {
7604 case 8: /* JR */
7605 case 9: /* JALR */
7606 rs = rtype_rs (inst);
7607 if (rs == MIPS_GP_REGNUM)
7608 target_pc = gp; /* Hmm... */
7609 else if (rs == addrreg)
7610 target_pc = addr;
7611 break;
7612
7613 case 0x21: /* ADDU */
7614 rt = rtype_rt (inst);
7615 rs = rtype_rs (inst);
7616 rd = rtype_rd (inst);
7617 if (rd == MIPS_GP_REGNUM
7618 && ((rs == MIPS_GP_REGNUM && rt == MIPS_T9_REGNUM)
7619 || (rs == MIPS_T9_REGNUM && rt == MIPS_GP_REGNUM)))
7620 gp += start_pc;
7621 break;
7622 }
7623 break;
7624
7625 case 2: /* J */
7626 case 3: /* JAL */
7627 target_pc = jtype_target (inst) << 2;
7628 target_pc += ((pc + 4) & ~(CORE_ADDR) 0x0fffffff);
7629 break;
7630
7631 case 9: /* ADDIU */
7632 rt = itype_rt (inst);
7633 rs = itype_rs (inst);
7634 if (rt == rs)
7635 {
7636 imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
7637 if (rt == MIPS_GP_REGNUM)
7638 gp += imm;
7639 else if (rt == addrreg)
7640 addr += imm;
7641 }
7642 break;
7643
7644 case 0xf: /* LUI */
7645 rt = itype_rt (inst);
7646 imm = ((itype_immediate (inst) ^ 0x8000) - 0x8000) << 16;
7647 if (rt == MIPS_GP_REGNUM)
7648 gp = imm;
7649 else if (rt != MIPS_ZERO_REGNUM)
7650 {
7651 addrreg = rt;
7652 addr = imm;
7653 }
7654 break;
7655
7656 case 0x23: /* LW */
7657 rt = itype_rt (inst);
7658 rs = itype_rs (inst);
7659 imm = (itype_immediate (inst) ^ 0x8000) - 0x8000;
7660 if (gp != 0 && rs == MIPS_GP_REGNUM)
7661 {
7662 gdb_byte buf[4];
7663
7664 memset (buf, 0, sizeof (buf));
7665 status = target_read_memory (gp + imm, buf, sizeof (buf));
7666 addrreg = rt;
7667 addr = extract_signed_integer (buf, sizeof (buf), byte_order);
7668 }
7669 break;
7670 }
7671 }
7672
7673 return target_pc;
7674 }
7675
7676 /* If PC is in a MIPS16 call or return stub, return the address of the
7677 target PC, which is either the callee or the caller. There are several
7678 cases which must be handled:
7679
7680 * If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
7681 and the target PC is in $31 ($ra).
7682 * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
7683 and the target PC is in $2.
7684 * If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
7685 i.e. before the JALR instruction, this is effectively a call stub
7686 and the target PC is in $2. Otherwise this is effectively
7687 a return stub and the target PC is in $18.
7688 * If the PC is at the start of __call_stub_fp_*, i.e. before the
7689 JAL or JALR instruction, this is effectively a call stub and the
7690 target PC is buried in the instruction stream. Otherwise this
7691 is effectively a return stub and the target PC is in $18.
7692 * If the PC is in __call_stub_* or in __fn_stub_*, this is a call
7693 stub and the target PC is buried in the instruction stream.
7694
7695 See the source code for the stubs in gcc/config/mips/mips16.S, or the
7696 stub builder in gcc/config/mips/mips.c (mips16_build_call_stub) for the
7697 gory details. */
7698
7699 static CORE_ADDR
7700 mips_skip_mips16_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7701 {
7702 struct gdbarch *gdbarch = get_frame_arch (frame);
7703 CORE_ADDR start_addr;
7704 const char *name;
7705 size_t prefixlen;
7706
7707 /* Find the starting address and name of the function containing the PC. */
7708 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
7709 return 0;
7710
7711 /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub
7712 and the target PC is in $31 ($ra). */
7713 prefixlen = strlen (mips_str_mips16_ret_stub);
7714 if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
7715 && mips_is_stub_mode (name + prefixlen)
7716 && name[prefixlen + 2] == '\0')
7717 return get_frame_register_signed
7718 (frame, gdbarch_num_regs (gdbarch) + MIPS_RA_REGNUM);
7719
7720 /* If the PC is in __mips16_call_stub_*, this is one of the call
7721 call/return stubs. */
7722 prefixlen = strlen (mips_str_mips16_call_stub);
7723 if (strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0)
7724 {
7725 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
7726 and the target PC is in $2. */
7727 if (mips_is_stub_suffix (name + prefixlen, 0))
7728 return get_frame_register_signed
7729 (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
7730
7731 /* If the PC at the start of __mips16_call_stub_{s,d}{f,c}_{0..10},
7732 i.e. before the JALR instruction, this is effectively a call stub
7733 and the target PC is in $2. Otherwise this is effectively
7734 a return stub and the target PC is in $18. */
7735 else if (mips_is_stub_mode (name + prefixlen)
7736 && name[prefixlen + 2] == '_'
7737 && mips_is_stub_suffix (name + prefixlen + 3, 0))
7738 {
7739 if (pc == start_addr)
7740 /* This is the 'call' part of a call stub. The return
7741 address is in $2. */
7742 return get_frame_register_signed
7743 (frame, gdbarch_num_regs (gdbarch) + MIPS_V0_REGNUM);
7744 else
7745 /* This is the 'return' part of a call stub. The return
7746 address is in $18. */
7747 return get_frame_register_signed
7748 (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
7749 }
7750 else
7751 return 0; /* Not a stub. */
7752 }
7753
7754 /* If the PC is in __call_stub_* or __fn_stub*, this is one of the
7755 compiler-generated call or call/return stubs. */
7756 if (startswith (name, mips_str_fn_stub)
7757 || startswith (name, mips_str_call_stub))
7758 {
7759 if (pc == start_addr)
7760 /* This is the 'call' part of a call stub. Call this helper
7761 to scan through this code for interesting instructions
7762 and determine the final PC. */
7763 return mips_get_mips16_fn_stub_pc (frame, pc);
7764 else
7765 /* This is the 'return' part of a call stub. The return address
7766 is in $18. */
7767 return get_frame_register_signed
7768 (frame, gdbarch_num_regs (gdbarch) + MIPS_S2_REGNUM);
7769 }
7770
7771 return 0; /* Not a stub. */
7772 }
7773
7774 /* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
7775 This implements the IN_SOLIB_RETURN_TRAMPOLINE macro. */
7776
7777 static int
7778 mips_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc, const char *name)
7779 {
7780 CORE_ADDR start_addr;
7781 size_t prefixlen;
7782
7783 /* Find the starting address of the function containing the PC. */
7784 if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
7785 return 0;
7786
7787 /* If the PC is in __mips16_call_stub_{s,d}{f,c}_{0..10} but not at
7788 the start, i.e. after the JALR instruction, this is effectively
7789 a return stub. */
7790 prefixlen = strlen (mips_str_mips16_call_stub);
7791 if (pc != start_addr
7792 && strncmp (name, mips_str_mips16_call_stub, prefixlen) == 0
7793 && mips_is_stub_mode (name + prefixlen)
7794 && name[prefixlen + 2] == '_'
7795 && mips_is_stub_suffix (name + prefixlen + 3, 1))
7796 return 1;
7797
7798 /* If the PC is in __call_stub_fp_* but not at the start, i.e. after
7799 the JAL or JALR instruction, this is effectively a return stub. */
7800 prefixlen = strlen (mips_str_call_fp_stub);
7801 if (pc != start_addr
7802 && strncmp (name, mips_str_call_fp_stub, prefixlen) == 0)
7803 return 1;
7804
7805 /* Consume the .pic. prefix of any PIC stub, this function must return
7806 true when the PC is in a PIC stub of a __mips16_ret_{d,s}{f,c} stub
7807 or the call stub path will trigger in handle_inferior_event causing
7808 it to go astray. */
7809 prefixlen = strlen (mips_str_pic);
7810 if (strncmp (name, mips_str_pic, prefixlen) == 0)
7811 name += prefixlen;
7812
7813 /* If the PC is in __mips16_ret_{d,s}{f,c}, this is a return stub. */
7814 prefixlen = strlen (mips_str_mips16_ret_stub);
7815 if (strncmp (name, mips_str_mips16_ret_stub, prefixlen) == 0
7816 && mips_is_stub_mode (name + prefixlen)
7817 && name[prefixlen + 2] == '\0')
7818 return 1;
7819
7820 return 0; /* Not a stub. */
7821 }
7822
7823 /* If the current PC is the start of a non-PIC-to-PIC stub, return the
7824 PC of the stub target. The stub just loads $t9 and jumps to it,
7825 so that $t9 has the correct value at function entry. */
7826
7827 static CORE_ADDR
7828 mips_skip_pic_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7829 {
7830 struct gdbarch *gdbarch = get_frame_arch (frame);
7831 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7832 struct bound_minimal_symbol msym;
7833 int i;
7834 gdb_byte stub_code[16];
7835 int32_t stub_words[4];
7836
7837 /* The stub for foo is named ".pic.foo", and is either two
7838 instructions inserted before foo or a three instruction sequence
7839 which jumps to foo. */
7840 msym = lookup_minimal_symbol_by_pc (pc);
7841 if (msym.minsym == NULL
7842 || BMSYMBOL_VALUE_ADDRESS (msym) != pc
7843 || MSYMBOL_LINKAGE_NAME (msym.minsym) == NULL
7844 || !startswith (MSYMBOL_LINKAGE_NAME (msym.minsym), ".pic."))
7845 return 0;
7846
7847 /* A two-instruction header. */
7848 if (MSYMBOL_SIZE (msym.minsym) == 8)
7849 return pc + 8;
7850
7851 /* A three-instruction (plus delay slot) trampoline. */
7852 if (MSYMBOL_SIZE (msym.minsym) == 16)
7853 {
7854 if (target_read_memory (pc, stub_code, 16) != 0)
7855 return 0;
7856 for (i = 0; i < 4; i++)
7857 stub_words[i] = extract_unsigned_integer (stub_code + i * 4,
7858 4, byte_order);
7859
7860 /* A stub contains these instructions:
7861 lui t9, %hi(target)
7862 j target
7863 addiu t9, t9, %lo(target)
7864 nop
7865
7866 This works even for N64, since stubs are only generated with
7867 -msym32. */
7868 if ((stub_words[0] & 0xffff0000U) == 0x3c190000
7869 && (stub_words[1] & 0xfc000000U) == 0x08000000
7870 && (stub_words[2] & 0xffff0000U) == 0x27390000
7871 && stub_words[3] == 0x00000000)
7872 return ((((stub_words[0] & 0x0000ffff) << 16)
7873 + (stub_words[2] & 0x0000ffff)) ^ 0x8000) - 0x8000;
7874 }
7875
7876 /* Not a recognized stub. */
7877 return 0;
7878 }
7879
7880 static CORE_ADDR
7881 mips_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
7882 {
7883 CORE_ADDR requested_pc = pc;
7884 CORE_ADDR target_pc;
7885 CORE_ADDR new_pc;
7886
7887 do
7888 {
7889 target_pc = pc;
7890
7891 new_pc = mips_skip_mips16_trampoline_code (frame, pc);
7892 if (new_pc)
7893 pc = new_pc;
7894
7895 new_pc = find_solib_trampoline_target (frame, pc);
7896 if (new_pc)
7897 pc = new_pc;
7898
7899 new_pc = mips_skip_pic_trampoline_code (frame, pc);
7900 if (new_pc)
7901 pc = new_pc;
7902 }
7903 while (pc != target_pc);
7904
7905 return pc != requested_pc ? pc : 0;
7906 }
7907
7908 /* Convert a dbx stab register number (from `r' declaration) to a GDB
7909 [1 * gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM. */
7910
7911 static int
7912 mips_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
7913 {
7914 int regnum;
7915 if (num >= 0 && num < 32)
7916 regnum = num;
7917 else if (num >= 38 && num < 70)
7918 regnum = num + mips_regnum (gdbarch)->fp0 - 38;
7919 else if (num == 70)
7920 regnum = mips_regnum (gdbarch)->hi;
7921 else if (num == 71)
7922 regnum = mips_regnum (gdbarch)->lo;
7923 else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 72 && num < 78)
7924 regnum = num + mips_regnum (gdbarch)->dspacc - 72;
7925 else
7926 return -1;
7927 return gdbarch_num_regs (gdbarch) + regnum;
7928 }
7929
7930
7931 /* Convert a dwarf, dwarf2, or ecoff register number to a GDB [1 *
7932 gdbarch_num_regs .. 2 * gdbarch_num_regs) REGNUM. */
7933
7934 static int
7935 mips_dwarf_dwarf2_ecoff_reg_to_regnum (struct gdbarch *gdbarch, int num)
7936 {
7937 int regnum;
7938 if (num >= 0 && num < 32)
7939 regnum = num;
7940 else if (num >= 32 && num < 64)
7941 regnum = num + mips_regnum (gdbarch)->fp0 - 32;
7942 else if (num == 64)
7943 regnum = mips_regnum (gdbarch)->hi;
7944 else if (num == 65)
7945 regnum = mips_regnum (gdbarch)->lo;
7946 else if (mips_regnum (gdbarch)->dspacc != -1 && num >= 66 && num < 72)
7947 regnum = num + mips_regnum (gdbarch)->dspacc - 66;
7948 else
7949 return -1;
7950 return gdbarch_num_regs (gdbarch) + regnum;
7951 }
7952
7953 static int
7954 mips_register_sim_regno (struct gdbarch *gdbarch, int regnum)
7955 {
7956 /* Only makes sense to supply raw registers. */
7957 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
7958 /* FIXME: cagney/2002-05-13: Need to look at the pseudo register to
7959 decide if it is valid. Should instead define a standard sim/gdb
7960 register numbering scheme. */
7961 if (gdbarch_register_name (gdbarch,
7962 gdbarch_num_regs (gdbarch) + regnum) != NULL
7963 && gdbarch_register_name (gdbarch,
7964 gdbarch_num_regs (gdbarch)
7965 + regnum)[0] != '\0')
7966 return regnum;
7967 else
7968 return LEGACY_SIM_REGNO_IGNORE;
7969 }
7970
7971
7972 /* Convert an integer into an address. Extracting the value signed
7973 guarantees a correctly sign extended address. */
7974
7975 static CORE_ADDR
7976 mips_integer_to_address (struct gdbarch *gdbarch,
7977 struct type *type, const gdb_byte *buf)
7978 {
7979 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
7980 return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
7981 }
7982
7983 /* Dummy virtual frame pointer method. This is no more or less accurate
7984 than most other architectures; we just need to be explicit about it,
7985 because the pseudo-register gdbarch_sp_regnum will otherwise lead to
7986 an assertion failure. */
7987
7988 static void
7989 mips_virtual_frame_pointer (struct gdbarch *gdbarch,
7990 CORE_ADDR pc, int *reg, LONGEST *offset)
7991 {
7992 *reg = MIPS_SP_REGNUM;
7993 *offset = 0;
7994 }
7995
7996 static void
7997 mips_find_abi_section (bfd *abfd, asection *sect, void *obj)
7998 {
7999 enum mips_abi *abip = (enum mips_abi *) obj;
8000 const char *name = bfd_get_section_name (abfd, sect);
8001
8002 if (*abip != MIPS_ABI_UNKNOWN)
8003 return;
8004
8005 if (!startswith (name, ".mdebug."))
8006 return;
8007
8008 if (strcmp (name, ".mdebug.abi32") == 0)
8009 *abip = MIPS_ABI_O32;
8010 else if (strcmp (name, ".mdebug.abiN32") == 0)
8011 *abip = MIPS_ABI_N32;
8012 else if (strcmp (name, ".mdebug.abi64") == 0)
8013 *abip = MIPS_ABI_N64;
8014 else if (strcmp (name, ".mdebug.abiO64") == 0)
8015 *abip = MIPS_ABI_O64;
8016 else if (strcmp (name, ".mdebug.eabi32") == 0)
8017 *abip = MIPS_ABI_EABI32;
8018 else if (strcmp (name, ".mdebug.eabi64") == 0)
8019 *abip = MIPS_ABI_EABI64;
8020 else
8021 warning (_("unsupported ABI %s."), name + 8);
8022 }
8023
8024 static void
8025 mips_find_long_section (bfd *abfd, asection *sect, void *obj)
8026 {
8027 int *lbp = (int *) obj;
8028 const char *name = bfd_get_section_name (abfd, sect);
8029
8030 if (startswith (name, ".gcc_compiled_long32"))
8031 *lbp = 32;
8032 else if (startswith (name, ".gcc_compiled_long64"))
8033 *lbp = 64;
8034 else if (startswith (name, ".gcc_compiled_long"))
8035 warning (_("unrecognized .gcc_compiled_longXX"));
8036 }
8037
8038 static enum mips_abi
8039 global_mips_abi (void)
8040 {
8041 int i;
8042
8043 for (i = 0; mips_abi_strings[i] != NULL; i++)
8044 if (mips_abi_strings[i] == mips_abi_string)
8045 return (enum mips_abi) i;
8046
8047 internal_error (__FILE__, __LINE__, _("unknown ABI string"));
8048 }
8049
8050 /* Return the default compressed instruction set, either of MIPS16
8051 or microMIPS, selected when none could have been determined from
8052 the ELF header of the binary being executed (or no binary has been
8053 selected. */
8054
8055 static enum mips_isa
8056 global_mips_compression (void)
8057 {
8058 int i;
8059
8060 for (i = 0; mips_compression_strings[i] != NULL; i++)
8061 if (mips_compression_strings[i] == mips_compression_string)
8062 return (enum mips_isa) i;
8063
8064 internal_error (__FILE__, __LINE__, _("unknown compressed ISA string"));
8065 }
8066
8067 static void
8068 mips_register_g_packet_guesses (struct gdbarch *gdbarch)
8069 {
8070 /* If the size matches the set of 32-bit or 64-bit integer registers,
8071 assume that's what we've got. */
8072 register_remote_g_packet_guess (gdbarch, 38 * 4, mips_tdesc_gp32);
8073 register_remote_g_packet_guess (gdbarch, 38 * 8, mips_tdesc_gp64);
8074
8075 /* If the size matches the full set of registers GDB traditionally
8076 knows about, including floating point, for either 32-bit or
8077 64-bit, assume that's what we've got. */
8078 register_remote_g_packet_guess (gdbarch, 90 * 4, mips_tdesc_gp32);
8079 register_remote_g_packet_guess (gdbarch, 90 * 8, mips_tdesc_gp64);
8080
8081 /* Otherwise we don't have a useful guess. */
8082 }
8083
8084 static struct value *
8085 value_of_mips_user_reg (struct frame_info *frame, const void *baton)
8086 {
8087 const int *reg_p = (const int *) baton;
8088 return value_of_register (*reg_p, frame);
8089 }
8090
8091 static struct gdbarch *
8092 mips_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
8093 {
8094 struct gdbarch *gdbarch;
8095 struct gdbarch_tdep *tdep;
8096 int elf_flags;
8097 enum mips_abi mips_abi, found_abi, wanted_abi;
8098 int i, num_regs;
8099 enum mips_fpu_type fpu_type;
8100 struct tdesc_arch_data *tdesc_data = NULL;
8101 int elf_fpu_type = Val_GNU_MIPS_ABI_FP_ANY;
8102 const char **reg_names;
8103 struct mips_regnum mips_regnum, *regnum;
8104 enum mips_isa mips_isa;
8105 int dspacc;
8106 int dspctl;
8107
8108 /* Fill in the OS dependent register numbers and names. */
8109 if (info.osabi == GDB_OSABI_LINUX)
8110 {
8111 mips_regnum.fp0 = 38;
8112 mips_regnum.pc = 37;
8113 mips_regnum.cause = 36;
8114 mips_regnum.badvaddr = 35;
8115 mips_regnum.hi = 34;
8116 mips_regnum.lo = 33;
8117 mips_regnum.fp_control_status = 70;
8118 mips_regnum.fp_implementation_revision = 71;
8119 mips_regnum.dspacc = -1;
8120 mips_regnum.dspctl = -1;
8121 dspacc = 72;
8122 dspctl = 78;
8123 num_regs = 90;
8124 reg_names = mips_linux_reg_names;
8125 }
8126 else
8127 {
8128 mips_regnum.lo = MIPS_EMBED_LO_REGNUM;
8129 mips_regnum.hi = MIPS_EMBED_HI_REGNUM;
8130 mips_regnum.badvaddr = MIPS_EMBED_BADVADDR_REGNUM;
8131 mips_regnum.cause = MIPS_EMBED_CAUSE_REGNUM;
8132 mips_regnum.pc = MIPS_EMBED_PC_REGNUM;
8133 mips_regnum.fp0 = MIPS_EMBED_FP0_REGNUM;
8134 mips_regnum.fp_control_status = 70;
8135 mips_regnum.fp_implementation_revision = 71;
8136 mips_regnum.dspacc = dspacc = -1;
8137 mips_regnum.dspctl = dspctl = -1;
8138 num_regs = MIPS_LAST_EMBED_REGNUM + 1;
8139 if (info.bfd_arch_info != NULL
8140 && info.bfd_arch_info->mach == bfd_mach_mips3900)
8141 reg_names = mips_tx39_reg_names;
8142 else
8143 reg_names = mips_generic_reg_names;
8144 }
8145
8146 /* Check any target description for validity. */
8147 if (tdesc_has_registers (info.target_desc))
8148 {
8149 static const char *const mips_gprs[] = {
8150 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
8151 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
8152 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
8153 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
8154 };
8155 static const char *const mips_fprs[] = {
8156 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
8157 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
8158 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
8159 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
8160 };
8161
8162 const struct tdesc_feature *feature;
8163 int valid_p;
8164
8165 feature = tdesc_find_feature (info.target_desc,
8166 "org.gnu.gdb.mips.cpu");
8167 if (feature == NULL)
8168 return NULL;
8169
8170 tdesc_data = tdesc_data_alloc ();
8171
8172 valid_p = 1;
8173 for (i = MIPS_ZERO_REGNUM; i <= MIPS_RA_REGNUM; i++)
8174 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
8175 mips_gprs[i]);
8176
8177
8178 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8179 mips_regnum.lo, "lo");
8180 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8181 mips_regnum.hi, "hi");
8182 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8183 mips_regnum.pc, "pc");
8184
8185 if (!valid_p)
8186 {
8187 tdesc_data_cleanup (tdesc_data);
8188 return NULL;
8189 }
8190
8191 feature = tdesc_find_feature (info.target_desc,
8192 "org.gnu.gdb.mips.cp0");
8193 if (feature == NULL)
8194 {
8195 tdesc_data_cleanup (tdesc_data);
8196 return NULL;
8197 }
8198
8199 valid_p = 1;
8200 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8201 mips_regnum.badvaddr, "badvaddr");
8202 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8203 MIPS_PS_REGNUM, "status");
8204 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8205 mips_regnum.cause, "cause");
8206
8207 if (!valid_p)
8208 {
8209 tdesc_data_cleanup (tdesc_data);
8210 return NULL;
8211 }
8212
8213 /* FIXME drow/2007-05-17: The FPU should be optional. The MIPS
8214 backend is not prepared for that, though. */
8215 feature = tdesc_find_feature (info.target_desc,
8216 "org.gnu.gdb.mips.fpu");
8217 if (feature == NULL)
8218 {
8219 tdesc_data_cleanup (tdesc_data);
8220 return NULL;
8221 }
8222
8223 valid_p = 1;
8224 for (i = 0; i < 32; i++)
8225 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8226 i + mips_regnum.fp0, mips_fprs[i]);
8227
8228 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8229 mips_regnum.fp_control_status,
8230 "fcsr");
8231 valid_p
8232 &= tdesc_numbered_register (feature, tdesc_data,
8233 mips_regnum.fp_implementation_revision,
8234 "fir");
8235
8236 if (!valid_p)
8237 {
8238 tdesc_data_cleanup (tdesc_data);
8239 return NULL;
8240 }
8241
8242 num_regs = mips_regnum.fp_implementation_revision + 1;
8243
8244 if (dspacc >= 0)
8245 {
8246 feature = tdesc_find_feature (info.target_desc,
8247 "org.gnu.gdb.mips.dsp");
8248 /* The DSP registers are optional; it's OK if they are absent. */
8249 if (feature != NULL)
8250 {
8251 i = 0;
8252 valid_p = 1;
8253 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8254 dspacc + i++, "hi1");
8255 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8256 dspacc + i++, "lo1");
8257 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8258 dspacc + i++, "hi2");
8259 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8260 dspacc + i++, "lo2");
8261 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8262 dspacc + i++, "hi3");
8263 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8264 dspacc + i++, "lo3");
8265
8266 valid_p &= tdesc_numbered_register (feature, tdesc_data,
8267 dspctl, "dspctl");
8268
8269 if (!valid_p)
8270 {
8271 tdesc_data_cleanup (tdesc_data);
8272 return NULL;
8273 }
8274
8275 mips_regnum.dspacc = dspacc;
8276 mips_regnum.dspctl = dspctl;
8277
8278 num_regs = mips_regnum.dspctl + 1;
8279 }
8280 }
8281
8282 /* It would be nice to detect an attempt to use a 64-bit ABI
8283 when only 32-bit registers are provided. */
8284 reg_names = NULL;
8285 }
8286
8287 /* First of all, extract the elf_flags, if available. */
8288 if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
8289 elf_flags = elf_elfheader (info.abfd)->e_flags;
8290 else if (arches != NULL)
8291 elf_flags = gdbarch_tdep (arches->gdbarch)->elf_flags;
8292 else
8293 elf_flags = 0;
8294 if (gdbarch_debug)
8295 fprintf_unfiltered (gdb_stdlog,
8296 "mips_gdbarch_init: elf_flags = 0x%08x\n", elf_flags);
8297
8298 /* Check ELF_FLAGS to see if it specifies the ABI being used. */
8299 switch ((elf_flags & EF_MIPS_ABI))
8300 {
8301 case E_MIPS_ABI_O32:
8302 found_abi = MIPS_ABI_O32;
8303 break;
8304 case E_MIPS_ABI_O64:
8305 found_abi = MIPS_ABI_O64;
8306 break;
8307 case E_MIPS_ABI_EABI32:
8308 found_abi = MIPS_ABI_EABI32;
8309 break;
8310 case E_MIPS_ABI_EABI64:
8311 found_abi = MIPS_ABI_EABI64;
8312 break;
8313 default:
8314 if ((elf_flags & EF_MIPS_ABI2))
8315 found_abi = MIPS_ABI_N32;
8316 else
8317 found_abi = MIPS_ABI_UNKNOWN;
8318 break;
8319 }
8320
8321 /* GCC creates a pseudo-section whose name describes the ABI. */
8322 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd != NULL)
8323 bfd_map_over_sections (info.abfd, mips_find_abi_section, &found_abi);
8324
8325 /* If we have no useful BFD information, use the ABI from the last
8326 MIPS architecture (if there is one). */
8327 if (found_abi == MIPS_ABI_UNKNOWN && info.abfd == NULL && arches != NULL)
8328 found_abi = gdbarch_tdep (arches->gdbarch)->found_abi;
8329
8330 /* Try the architecture for any hint of the correct ABI. */
8331 if (found_abi == MIPS_ABI_UNKNOWN
8332 && info.bfd_arch_info != NULL
8333 && info.bfd_arch_info->arch == bfd_arch_mips)
8334 {
8335 switch (info.bfd_arch_info->mach)
8336 {
8337 case bfd_mach_mips3900:
8338 found_abi = MIPS_ABI_EABI32;
8339 break;
8340 case bfd_mach_mips4100:
8341 case bfd_mach_mips5000:
8342 found_abi = MIPS_ABI_EABI64;
8343 break;
8344 case bfd_mach_mips8000:
8345 case bfd_mach_mips10000:
8346 /* On Irix, ELF64 executables use the N64 ABI. The
8347 pseudo-sections which describe the ABI aren't present
8348 on IRIX. (Even for executables created by gcc.) */
8349 if (info.abfd != NULL
8350 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
8351 && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
8352 found_abi = MIPS_ABI_N64;
8353 else
8354 found_abi = MIPS_ABI_N32;
8355 break;
8356 }
8357 }
8358
8359 /* Default 64-bit objects to N64 instead of O32. */
8360 if (found_abi == MIPS_ABI_UNKNOWN
8361 && info.abfd != NULL
8362 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour
8363 && elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
8364 found_abi = MIPS_ABI_N64;
8365
8366 if (gdbarch_debug)
8367 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: found_abi = %d\n",
8368 found_abi);
8369
8370 /* What has the user specified from the command line? */
8371 wanted_abi = global_mips_abi ();
8372 if (gdbarch_debug)
8373 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: wanted_abi = %d\n",
8374 wanted_abi);
8375
8376 /* Now that we have found what the ABI for this binary would be,
8377 check whether the user is overriding it. */
8378 if (wanted_abi != MIPS_ABI_UNKNOWN)
8379 mips_abi = wanted_abi;
8380 else if (found_abi != MIPS_ABI_UNKNOWN)
8381 mips_abi = found_abi;
8382 else
8383 mips_abi = MIPS_ABI_O32;
8384 if (gdbarch_debug)
8385 fprintf_unfiltered (gdb_stdlog, "mips_gdbarch_init: mips_abi = %d\n",
8386 mips_abi);
8387
8388 /* Determine the default compressed ISA. */
8389 if ((elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0
8390 && (elf_flags & EF_MIPS_ARCH_ASE_M16) == 0)
8391 mips_isa = ISA_MICROMIPS;
8392 else if ((elf_flags & EF_MIPS_ARCH_ASE_M16) != 0
8393 && (elf_flags & EF_MIPS_ARCH_ASE_MICROMIPS) == 0)
8394 mips_isa = ISA_MIPS16;
8395 else
8396 mips_isa = global_mips_compression ();
8397 mips_compression_string = mips_compression_strings[mips_isa];
8398
8399 /* Also used when doing an architecture lookup. */
8400 if (gdbarch_debug)
8401 fprintf_unfiltered (gdb_stdlog,
8402 "mips_gdbarch_init: "
8403 "mips64_transfers_32bit_regs_p = %d\n",
8404 mips64_transfers_32bit_regs_p);
8405
8406 /* Determine the MIPS FPU type. */
8407 #ifdef HAVE_ELF
8408 if (info.abfd
8409 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
8410 elf_fpu_type = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
8411 Tag_GNU_MIPS_ABI_FP);
8412 #endif /* HAVE_ELF */
8413
8414 if (!mips_fpu_type_auto)
8415 fpu_type = mips_fpu_type;
8416 else if (elf_fpu_type != Val_GNU_MIPS_ABI_FP_ANY)
8417 {
8418 switch (elf_fpu_type)
8419 {
8420 case Val_GNU_MIPS_ABI_FP_DOUBLE:
8421 fpu_type = MIPS_FPU_DOUBLE;
8422 break;
8423 case Val_GNU_MIPS_ABI_FP_SINGLE:
8424 fpu_type = MIPS_FPU_SINGLE;
8425 break;
8426 case Val_GNU_MIPS_ABI_FP_SOFT:
8427 default:
8428 /* Soft float or unknown. */
8429 fpu_type = MIPS_FPU_NONE;
8430 break;
8431 }
8432 }
8433 else if (info.bfd_arch_info != NULL
8434 && info.bfd_arch_info->arch == bfd_arch_mips)
8435 switch (info.bfd_arch_info->mach)
8436 {
8437 case bfd_mach_mips3900:
8438 case bfd_mach_mips4100:
8439 case bfd_mach_mips4111:
8440 case bfd_mach_mips4120:
8441 fpu_type = MIPS_FPU_NONE;
8442 break;
8443 case bfd_mach_mips4650:
8444 fpu_type = MIPS_FPU_SINGLE;
8445 break;
8446 default:
8447 fpu_type = MIPS_FPU_DOUBLE;
8448 break;
8449 }
8450 else if (arches != NULL)
8451 fpu_type = gdbarch_tdep (arches->gdbarch)->mips_fpu_type;
8452 else
8453 fpu_type = MIPS_FPU_DOUBLE;
8454 if (gdbarch_debug)
8455 fprintf_unfiltered (gdb_stdlog,
8456 "mips_gdbarch_init: fpu_type = %d\n", fpu_type);
8457
8458 /* Check for blatant incompatibilities. */
8459
8460 /* If we have only 32-bit registers, then we can't debug a 64-bit
8461 ABI. */
8462 if (info.target_desc
8463 && tdesc_property (info.target_desc, PROPERTY_GP32) != NULL
8464 && mips_abi != MIPS_ABI_EABI32
8465 && mips_abi != MIPS_ABI_O32)
8466 {
8467 if (tdesc_data != NULL)
8468 tdesc_data_cleanup (tdesc_data);
8469 return NULL;
8470 }
8471
8472 /* Try to find a pre-existing architecture. */
8473 for (arches = gdbarch_list_lookup_by_info (arches, &info);
8474 arches != NULL;
8475 arches = gdbarch_list_lookup_by_info (arches->next, &info))
8476 {
8477 /* MIPS needs to be pedantic about which ABI and the compressed
8478 ISA variation the object is using. */
8479 if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
8480 continue;
8481 if (gdbarch_tdep (arches->gdbarch)->mips_abi != mips_abi)
8482 continue;
8483 if (gdbarch_tdep (arches->gdbarch)->mips_isa != mips_isa)
8484 continue;
8485 /* Need to be pedantic about which register virtual size is
8486 used. */
8487 if (gdbarch_tdep (arches->gdbarch)->mips64_transfers_32bit_regs_p
8488 != mips64_transfers_32bit_regs_p)
8489 continue;
8490 /* Be pedantic about which FPU is selected. */
8491 if (gdbarch_tdep (arches->gdbarch)->mips_fpu_type != fpu_type)
8492 continue;
8493
8494 if (tdesc_data != NULL)
8495 tdesc_data_cleanup (tdesc_data);
8496 return arches->gdbarch;
8497 }
8498
8499 /* Need a new architecture. Fill in a target specific vector. */
8500 tdep = XNEW (struct gdbarch_tdep);
8501 gdbarch = gdbarch_alloc (&info, tdep);
8502 tdep->elf_flags = elf_flags;
8503 tdep->mips64_transfers_32bit_regs_p = mips64_transfers_32bit_regs_p;
8504 tdep->found_abi = found_abi;
8505 tdep->mips_abi = mips_abi;
8506 tdep->mips_isa = mips_isa;
8507 tdep->mips_fpu_type = fpu_type;
8508 tdep->register_size_valid_p = 0;
8509 tdep->register_size = 0;
8510
8511 if (info.target_desc)
8512 {
8513 /* Some useful properties can be inferred from the target. */
8514 if (tdesc_property (info.target_desc, PROPERTY_GP32) != NULL)
8515 {
8516 tdep->register_size_valid_p = 1;
8517 tdep->register_size = 4;
8518 }
8519 else if (tdesc_property (info.target_desc, PROPERTY_GP64) != NULL)
8520 {
8521 tdep->register_size_valid_p = 1;
8522 tdep->register_size = 8;
8523 }
8524 }
8525
8526 /* Initially set everything according to the default ABI/ISA. */
8527 set_gdbarch_short_bit (gdbarch, 16);
8528 set_gdbarch_int_bit (gdbarch, 32);
8529 set_gdbarch_float_bit (gdbarch, 32);
8530 set_gdbarch_double_bit (gdbarch, 64);
8531 set_gdbarch_long_double_bit (gdbarch, 64);
8532 set_gdbarch_register_reggroup_p (gdbarch, mips_register_reggroup_p);
8533 set_gdbarch_pseudo_register_read (gdbarch, mips_pseudo_register_read);
8534 set_gdbarch_pseudo_register_write (gdbarch, mips_pseudo_register_write);
8535
8536 set_gdbarch_ax_pseudo_register_collect (gdbarch,
8537 mips_ax_pseudo_register_collect);
8538 set_gdbarch_ax_pseudo_register_push_stack
8539 (gdbarch, mips_ax_pseudo_register_push_stack);
8540
8541 set_gdbarch_elf_make_msymbol_special (gdbarch,
8542 mips_elf_make_msymbol_special);
8543 set_gdbarch_make_symbol_special (gdbarch, mips_make_symbol_special);
8544 set_gdbarch_adjust_dwarf2_addr (gdbarch, mips_adjust_dwarf2_addr);
8545 set_gdbarch_adjust_dwarf2_line (gdbarch, mips_adjust_dwarf2_line);
8546
8547 regnum = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct mips_regnum);
8548 *regnum = mips_regnum;
8549 set_gdbarch_fp0_regnum (gdbarch, regnum->fp0);
8550 set_gdbarch_num_regs (gdbarch, num_regs);
8551 set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
8552 set_gdbarch_register_name (gdbarch, mips_register_name);
8553 set_gdbarch_virtual_frame_pointer (gdbarch, mips_virtual_frame_pointer);
8554 tdep->mips_processor_reg_names = reg_names;
8555 tdep->regnum = regnum;
8556
8557 switch (mips_abi)
8558 {
8559 case MIPS_ABI_O32:
8560 set_gdbarch_push_dummy_call (gdbarch, mips_o32_push_dummy_call);
8561 set_gdbarch_return_value (gdbarch, mips_o32_return_value);
8562 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
8563 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
8564 tdep->default_mask_address_p = 0;
8565 set_gdbarch_long_bit (gdbarch, 32);
8566 set_gdbarch_ptr_bit (gdbarch, 32);
8567 set_gdbarch_long_long_bit (gdbarch, 64);
8568 break;
8569 case MIPS_ABI_O64:
8570 set_gdbarch_push_dummy_call (gdbarch, mips_o64_push_dummy_call);
8571 set_gdbarch_return_value (gdbarch, mips_o64_return_value);
8572 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 4 - 1;
8573 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 4 - 1;
8574 tdep->default_mask_address_p = 0;
8575 set_gdbarch_long_bit (gdbarch, 32);
8576 set_gdbarch_ptr_bit (gdbarch, 32);
8577 set_gdbarch_long_long_bit (gdbarch, 64);
8578 break;
8579 case MIPS_ABI_EABI32:
8580 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
8581 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
8582 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8583 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8584 tdep->default_mask_address_p = 0;
8585 set_gdbarch_long_bit (gdbarch, 32);
8586 set_gdbarch_ptr_bit (gdbarch, 32);
8587 set_gdbarch_long_long_bit (gdbarch, 64);
8588 break;
8589 case MIPS_ABI_EABI64:
8590 set_gdbarch_push_dummy_call (gdbarch, mips_eabi_push_dummy_call);
8591 set_gdbarch_return_value (gdbarch, mips_eabi_return_value);
8592 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8593 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8594 tdep->default_mask_address_p = 0;
8595 set_gdbarch_long_bit (gdbarch, 64);
8596 set_gdbarch_ptr_bit (gdbarch, 64);
8597 set_gdbarch_long_long_bit (gdbarch, 64);
8598 break;
8599 case MIPS_ABI_N32:
8600 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
8601 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
8602 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8603 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8604 tdep->default_mask_address_p = 0;
8605 set_gdbarch_long_bit (gdbarch, 32);
8606 set_gdbarch_ptr_bit (gdbarch, 32);
8607 set_gdbarch_long_long_bit (gdbarch, 64);
8608 set_gdbarch_long_double_bit (gdbarch, 128);
8609 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
8610 break;
8611 case MIPS_ABI_N64:
8612 set_gdbarch_push_dummy_call (gdbarch, mips_n32n64_push_dummy_call);
8613 set_gdbarch_return_value (gdbarch, mips_n32n64_return_value);
8614 tdep->mips_last_arg_regnum = MIPS_A0_REGNUM + 8 - 1;
8615 tdep->mips_last_fp_arg_regnum = tdep->regnum->fp0 + 12 + 8 - 1;
8616 tdep->default_mask_address_p = 0;
8617 set_gdbarch_long_bit (gdbarch, 64);
8618 set_gdbarch_ptr_bit (gdbarch, 64);
8619 set_gdbarch_long_long_bit (gdbarch, 64);
8620 set_gdbarch_long_double_bit (gdbarch, 128);
8621 set_gdbarch_long_double_format (gdbarch, floatformats_ibm_long_double);
8622 break;
8623 default:
8624 internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
8625 }
8626
8627 /* GCC creates a pseudo-section whose name specifies the size of
8628 longs, since -mlong32 or -mlong64 may be used independent of
8629 other options. How those options affect pointer sizes is ABI and
8630 architecture dependent, so use them to override the default sizes
8631 set by the ABI. This table shows the relationship between ABI,
8632 -mlongXX, and size of pointers:
8633
8634 ABI -mlongXX ptr bits
8635 --- -------- --------
8636 o32 32 32
8637 o32 64 32
8638 n32 32 32
8639 n32 64 64
8640 o64 32 32
8641 o64 64 64
8642 n64 32 32
8643 n64 64 64
8644 eabi32 32 32
8645 eabi32 64 32
8646 eabi64 32 32
8647 eabi64 64 64
8648
8649 Note that for o32 and eabi32, pointers are always 32 bits
8650 regardless of any -mlongXX option. For all others, pointers and
8651 longs are the same, as set by -mlongXX or set by defaults. */
8652
8653 if (info.abfd != NULL)
8654 {
8655 int long_bit = 0;
8656
8657 bfd_map_over_sections (info.abfd, mips_find_long_section, &long_bit);
8658 if (long_bit)
8659 {
8660 set_gdbarch_long_bit (gdbarch, long_bit);
8661 switch (mips_abi)
8662 {
8663 case MIPS_ABI_O32:
8664 case MIPS_ABI_EABI32:
8665 break;
8666 case MIPS_ABI_N32:
8667 case MIPS_ABI_O64:
8668 case MIPS_ABI_N64:
8669 case MIPS_ABI_EABI64:
8670 set_gdbarch_ptr_bit (gdbarch, long_bit);
8671 break;
8672 default:
8673 internal_error (__FILE__, __LINE__, _("unknown ABI in switch"));
8674 }
8675 }
8676 }
8677
8678 /* FIXME: jlarmour/2000-04-07: There *is* a flag EF_MIPS_32BIT_MODE
8679 that could indicate -gp32 BUT gas/config/tc-mips.c contains the
8680 comment:
8681
8682 ``We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
8683 flag in object files because to do so would make it impossible to
8684 link with libraries compiled without "-gp32". This is
8685 unnecessarily restrictive.
8686
8687 We could solve this problem by adding "-gp32" multilibs to gcc,
8688 but to set this flag before gcc is built with such multilibs will
8689 break too many systems.''
8690
8691 But even more unhelpfully, the default linker output target for
8692 mips64-elf is elf32-bigmips, and has EF_MIPS_32BIT_MODE set, even
8693 for 64-bit programs - you need to change the ABI to change this,
8694 and not all gcc targets support that currently. Therefore using
8695 this flag to detect 32-bit mode would do the wrong thing given
8696 the current gcc - it would make GDB treat these 64-bit programs
8697 as 32-bit programs by default. */
8698
8699 set_gdbarch_read_pc (gdbarch, mips_read_pc);
8700 set_gdbarch_write_pc (gdbarch, mips_write_pc);
8701
8702 /* Add/remove bits from an address. The MIPS needs be careful to
8703 ensure that all 32 bit addresses are sign extended to 64 bits. */
8704 set_gdbarch_addr_bits_remove (gdbarch, mips_addr_bits_remove);
8705
8706 /* Unwind the frame. */
8707 set_gdbarch_unwind_pc (gdbarch, mips_unwind_pc);
8708 set_gdbarch_unwind_sp (gdbarch, mips_unwind_sp);
8709 set_gdbarch_dummy_id (gdbarch, mips_dummy_id);
8710
8711 /* Map debug register numbers onto internal register numbers. */
8712 set_gdbarch_stab_reg_to_regnum (gdbarch, mips_stab_reg_to_regnum);
8713 set_gdbarch_ecoff_reg_to_regnum (gdbarch,
8714 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
8715 set_gdbarch_dwarf2_reg_to_regnum (gdbarch,
8716 mips_dwarf_dwarf2_ecoff_reg_to_regnum);
8717 set_gdbarch_register_sim_regno (gdbarch, mips_register_sim_regno);
8718
8719 /* MIPS version of CALL_DUMMY. */
8720
8721 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
8722 set_gdbarch_push_dummy_code (gdbarch, mips_push_dummy_code);
8723 set_gdbarch_frame_align (gdbarch, mips_frame_align);
8724
8725 set_gdbarch_print_float_info (gdbarch, mips_print_float_info);
8726
8727 set_gdbarch_convert_register_p (gdbarch, mips_convert_register_p);
8728 set_gdbarch_register_to_value (gdbarch, mips_register_to_value);
8729 set_gdbarch_value_to_register (gdbarch, mips_value_to_register);
8730
8731 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
8732 SET_GDBARCH_BREAKPOINT_MANIPULATION (mips);
8733 set_gdbarch_remote_breakpoint_from_pc (gdbarch,
8734 mips_remote_breakpoint_from_pc);
8735 set_gdbarch_adjust_breakpoint_address (gdbarch,
8736 mips_adjust_breakpoint_address);
8737
8738 set_gdbarch_skip_prologue (gdbarch, mips_skip_prologue);
8739
8740 set_gdbarch_stack_frame_destroyed_p (gdbarch, mips_stack_frame_destroyed_p);
8741
8742 set_gdbarch_pointer_to_address (gdbarch, signed_pointer_to_address);
8743 set_gdbarch_address_to_pointer (gdbarch, address_to_signed_pointer);
8744 set_gdbarch_integer_to_address (gdbarch, mips_integer_to_address);
8745
8746 set_gdbarch_register_type (gdbarch, mips_register_type);
8747
8748 set_gdbarch_print_registers_info (gdbarch, mips_print_registers_info);
8749
8750 if (mips_abi == MIPS_ABI_N32)
8751 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n32);
8752 else if (mips_abi == MIPS_ABI_N64)
8753 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips_n64);
8754 else
8755 set_gdbarch_print_insn (gdbarch, gdb_print_insn_mips);
8756
8757 /* FIXME: cagney/2003-08-29: The macros target_have_steppable_watchpoint,
8758 HAVE_NONSTEPPABLE_WATCHPOINT, and target_have_continuable_watchpoint
8759 need to all be folded into the target vector. Since they are
8760 being used as guards for target_stopped_by_watchpoint, why not have
8761 target_stopped_by_watchpoint return the type of watchpoint that the code
8762 is sitting on? */
8763 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
8764
8765 set_gdbarch_skip_trampoline_code (gdbarch, mips_skip_trampoline_code);
8766
8767 /* NOTE drow/2012-04-25: We overload the core solib trampoline code
8768 to support MIPS16. This is a bad thing. Make sure not to do it
8769 if we have an OS ABI that actually supports shared libraries, since
8770 shared library support is more important. If we have an OS someday
8771 that supports both shared libraries and MIPS16, we'll have to find
8772 a better place for these.
8773 macro/2012-04-25: But that applies to return trampolines only and
8774 currently no MIPS OS ABI uses shared libraries that have them. */
8775 set_gdbarch_in_solib_return_trampoline (gdbarch, mips_in_return_stub);
8776
8777 set_gdbarch_single_step_through_delay (gdbarch,
8778 mips_single_step_through_delay);
8779
8780 /* Virtual tables. */
8781 set_gdbarch_vbit_in_delta (gdbarch, 1);
8782
8783 mips_register_g_packet_guesses (gdbarch);
8784
8785 /* Hook in OS ABI-specific overrides, if they have been registered. */
8786 info.tdep_info = tdesc_data;
8787 gdbarch_init_osabi (info, gdbarch);
8788
8789 /* The hook may have adjusted num_regs, fetch the final value and
8790 set pc_regnum and sp_regnum now that it has been fixed. */
8791 num_regs = gdbarch_num_regs (gdbarch);
8792 set_gdbarch_pc_regnum (gdbarch, regnum->pc + num_regs);
8793 set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
8794
8795 /* Unwind the frame. */
8796 dwarf2_append_unwinders (gdbarch);
8797 frame_unwind_append_unwinder (gdbarch, &mips_stub_frame_unwind);
8798 frame_unwind_append_unwinder (gdbarch, &mips_insn16_frame_unwind);
8799 frame_unwind_append_unwinder (gdbarch, &mips_micro_frame_unwind);
8800 frame_unwind_append_unwinder (gdbarch, &mips_insn32_frame_unwind);
8801 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
8802 frame_base_append_sniffer (gdbarch, mips_stub_frame_base_sniffer);
8803 frame_base_append_sniffer (gdbarch, mips_insn16_frame_base_sniffer);
8804 frame_base_append_sniffer (gdbarch, mips_micro_frame_base_sniffer);
8805 frame_base_append_sniffer (gdbarch, mips_insn32_frame_base_sniffer);
8806
8807 if (tdesc_data)
8808 {
8809 set_tdesc_pseudo_register_type (gdbarch, mips_pseudo_register_type);
8810 tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
8811
8812 /* Override the normal target description methods to handle our
8813 dual real and pseudo registers. */
8814 set_gdbarch_register_name (gdbarch, mips_register_name);
8815 set_gdbarch_register_reggroup_p (gdbarch,
8816 mips_tdesc_register_reggroup_p);
8817
8818 num_regs = gdbarch_num_regs (gdbarch);
8819 set_gdbarch_num_pseudo_regs (gdbarch, num_regs);
8820 set_gdbarch_pc_regnum (gdbarch, tdep->regnum->pc + num_regs);
8821 set_gdbarch_sp_regnum (gdbarch, MIPS_SP_REGNUM + num_regs);
8822 }
8823
8824 /* Add ABI-specific aliases for the registers. */
8825 if (mips_abi == MIPS_ABI_N32 || mips_abi == MIPS_ABI_N64)
8826 for (i = 0; i < ARRAY_SIZE (mips_n32_n64_aliases); i++)
8827 user_reg_add (gdbarch, mips_n32_n64_aliases[i].name,
8828 value_of_mips_user_reg, &mips_n32_n64_aliases[i].regnum);
8829 else
8830 for (i = 0; i < ARRAY_SIZE (mips_o32_aliases); i++)
8831 user_reg_add (gdbarch, mips_o32_aliases[i].name,
8832 value_of_mips_user_reg, &mips_o32_aliases[i].regnum);
8833
8834 /* Add some other standard aliases. */
8835 for (i = 0; i < ARRAY_SIZE (mips_register_aliases); i++)
8836 user_reg_add (gdbarch, mips_register_aliases[i].name,
8837 value_of_mips_user_reg, &mips_register_aliases[i].regnum);
8838
8839 for (i = 0; i < ARRAY_SIZE (mips_numeric_register_aliases); i++)
8840 user_reg_add (gdbarch, mips_numeric_register_aliases[i].name,
8841 value_of_mips_user_reg,
8842 &mips_numeric_register_aliases[i].regnum);
8843
8844 return gdbarch;
8845 }
8846
8847 static void
8848 mips_abi_update (char *ignore_args, int from_tty, struct cmd_list_element *c)
8849 {
8850 struct gdbarch_info info;
8851
8852 /* Force the architecture to update, and (if it's a MIPS architecture)
8853 mips_gdbarch_init will take care of the rest. */
8854 gdbarch_info_init (&info);
8855 gdbarch_update_p (info);
8856 }
8857
8858 /* Print out which MIPS ABI is in use. */
8859
8860 static void
8861 show_mips_abi (struct ui_file *file,
8862 int from_tty,
8863 struct cmd_list_element *ignored_cmd,
8864 const char *ignored_value)
8865 {
8866 if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_mips)
8867 fprintf_filtered
8868 (file,
8869 "The MIPS ABI is unknown because the current architecture "
8870 "is not MIPS.\n");
8871 else
8872 {
8873 enum mips_abi global_abi = global_mips_abi ();
8874 enum mips_abi actual_abi = mips_abi (target_gdbarch ());
8875 const char *actual_abi_str = mips_abi_strings[actual_abi];
8876
8877 if (global_abi == MIPS_ABI_UNKNOWN)
8878 fprintf_filtered
8879 (file,
8880 "The MIPS ABI is set automatically (currently \"%s\").\n",
8881 actual_abi_str);
8882 else if (global_abi == actual_abi)
8883 fprintf_filtered
8884 (file,
8885 "The MIPS ABI is assumed to be \"%s\" (due to user setting).\n",
8886 actual_abi_str);
8887 else
8888 {
8889 /* Probably shouldn't happen... */
8890 fprintf_filtered (file,
8891 "The (auto detected) MIPS ABI \"%s\" is in use "
8892 "even though the user setting was \"%s\".\n",
8893 actual_abi_str, mips_abi_strings[global_abi]);
8894 }
8895 }
8896 }
8897
8898 /* Print out which MIPS compressed ISA encoding is used. */
8899
8900 static void
8901 show_mips_compression (struct ui_file *file, int from_tty,
8902 struct cmd_list_element *c, const char *value)
8903 {
8904 fprintf_filtered (file, _("The compressed ISA encoding used is %s.\n"),
8905 value);
8906 }
8907
8908 static void
8909 mips_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
8910 {
8911 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
8912 if (tdep != NULL)
8913 {
8914 int ef_mips_arch;
8915 int ef_mips_32bitmode;
8916 /* Determine the ISA. */
8917 switch (tdep->elf_flags & EF_MIPS_ARCH)
8918 {
8919 case E_MIPS_ARCH_1:
8920 ef_mips_arch = 1;
8921 break;
8922 case E_MIPS_ARCH_2:
8923 ef_mips_arch = 2;
8924 break;
8925 case E_MIPS_ARCH_3:
8926 ef_mips_arch = 3;
8927 break;
8928 case E_MIPS_ARCH_4:
8929 ef_mips_arch = 4;
8930 break;
8931 default:
8932 ef_mips_arch = 0;
8933 break;
8934 }
8935 /* Determine the size of a pointer. */
8936 ef_mips_32bitmode = (tdep->elf_flags & EF_MIPS_32BITMODE);
8937 fprintf_unfiltered (file,
8938 "mips_dump_tdep: tdep->elf_flags = 0x%x\n",
8939 tdep->elf_flags);
8940 fprintf_unfiltered (file,
8941 "mips_dump_tdep: ef_mips_32bitmode = %d\n",
8942 ef_mips_32bitmode);
8943 fprintf_unfiltered (file,
8944 "mips_dump_tdep: ef_mips_arch = %d\n",
8945 ef_mips_arch);
8946 fprintf_unfiltered (file,
8947 "mips_dump_tdep: tdep->mips_abi = %d (%s)\n",
8948 tdep->mips_abi, mips_abi_strings[tdep->mips_abi]);
8949 fprintf_unfiltered (file,
8950 "mips_dump_tdep: "
8951 "mips_mask_address_p() %d (default %d)\n",
8952 mips_mask_address_p (tdep),
8953 tdep->default_mask_address_p);
8954 }
8955 fprintf_unfiltered (file,
8956 "mips_dump_tdep: MIPS_DEFAULT_FPU_TYPE = %d (%s)\n",
8957 MIPS_DEFAULT_FPU_TYPE,
8958 (MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_NONE ? "none"
8959 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_SINGLE ? "single"
8960 : MIPS_DEFAULT_FPU_TYPE == MIPS_FPU_DOUBLE ? "double"
8961 : "???"));
8962 fprintf_unfiltered (file, "mips_dump_tdep: MIPS_EABI = %d\n",
8963 MIPS_EABI (gdbarch));
8964 fprintf_unfiltered (file,
8965 "mips_dump_tdep: MIPS_FPU_TYPE = %d (%s)\n",
8966 MIPS_FPU_TYPE (gdbarch),
8967 (MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_NONE ? "none"
8968 : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_SINGLE ? "single"
8969 : MIPS_FPU_TYPE (gdbarch) == MIPS_FPU_DOUBLE ? "double"
8970 : "???"));
8971 }
8972
8973 extern initialize_file_ftype _initialize_mips_tdep; /* -Wmissing-prototypes */
8974
8975 void
8976 _initialize_mips_tdep (void)
8977 {
8978 static struct cmd_list_element *mipsfpulist = NULL;
8979 struct cmd_list_element *c;
8980
8981 mips_abi_string = mips_abi_strings[MIPS_ABI_UNKNOWN];
8982 if (MIPS_ABI_LAST + 1
8983 != sizeof (mips_abi_strings) / sizeof (mips_abi_strings[0]))
8984 internal_error (__FILE__, __LINE__, _("mips_abi_strings out of sync"));
8985
8986 gdbarch_register (bfd_arch_mips, mips_gdbarch_init, mips_dump_tdep);
8987
8988 mips_pdr_data = register_objfile_data ();
8989
8990 /* Create feature sets with the appropriate properties. The values
8991 are not important. */
8992 mips_tdesc_gp32 = allocate_target_description ();
8993 set_tdesc_property (mips_tdesc_gp32, PROPERTY_GP32, "");
8994
8995 mips_tdesc_gp64 = allocate_target_description ();
8996 set_tdesc_property (mips_tdesc_gp64, PROPERTY_GP64, "");
8997
8998 /* Add root prefix command for all "set mips"/"show mips" commands. */
8999 add_prefix_cmd ("mips", no_class, set_mips_command,
9000 _("Various MIPS specific commands."),
9001 &setmipscmdlist, "set mips ", 0, &setlist);
9002
9003 add_prefix_cmd ("mips", no_class, show_mips_command,
9004 _("Various MIPS specific commands."),
9005 &showmipscmdlist, "show mips ", 0, &showlist);
9006
9007 /* Allow the user to override the ABI. */
9008 add_setshow_enum_cmd ("abi", class_obscure, mips_abi_strings,
9009 &mips_abi_string, _("\
9010 Set the MIPS ABI used by this program."), _("\
9011 Show the MIPS ABI used by this program."), _("\
9012 This option can be set to one of:\n\
9013 auto - the default ABI associated with the current binary\n\
9014 o32\n\
9015 o64\n\
9016 n32\n\
9017 n64\n\
9018 eabi32\n\
9019 eabi64"),
9020 mips_abi_update,
9021 show_mips_abi,
9022 &setmipscmdlist, &showmipscmdlist);
9023
9024 /* Allow the user to set the ISA to assume for compressed code if ELF
9025 file flags don't tell or there is no program file selected. This
9026 setting is updated whenever unambiguous ELF file flags are interpreted,
9027 and carried over to subsequent sessions. */
9028 add_setshow_enum_cmd ("compression", class_obscure, mips_compression_strings,
9029 &mips_compression_string, _("\
9030 Set the compressed ISA encoding used by MIPS code."), _("\
9031 Show the compressed ISA encoding used by MIPS code."), _("\
9032 Select the compressed ISA encoding used in functions that have no symbol\n\
9033 information available. The encoding can be set to either of:\n\
9034 mips16\n\
9035 micromips\n\
9036 and is updated automatically from ELF file flags if available."),
9037 mips_abi_update,
9038 show_mips_compression,
9039 &setmipscmdlist, &showmipscmdlist);
9040
9041 /* Let the user turn off floating point and set the fence post for
9042 heuristic_proc_start. */
9043
9044 add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
9045 _("Set use of MIPS floating-point coprocessor."),
9046 &mipsfpulist, "set mipsfpu ", 0, &setlist);
9047 add_cmd ("single", class_support, set_mipsfpu_single_command,
9048 _("Select single-precision MIPS floating-point coprocessor."),
9049 &mipsfpulist);
9050 add_cmd ("double", class_support, set_mipsfpu_double_command,
9051 _("Select double-precision MIPS floating-point coprocessor."),
9052 &mipsfpulist);
9053 add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
9054 add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
9055 add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
9056 add_cmd ("none", class_support, set_mipsfpu_none_command,
9057 _("Select no MIPS floating-point coprocessor."), &mipsfpulist);
9058 add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
9059 add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
9060 add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
9061 add_cmd ("auto", class_support, set_mipsfpu_auto_command,
9062 _("Select MIPS floating-point coprocessor automatically."),
9063 &mipsfpulist);
9064 add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
9065 _("Show current use of MIPS floating-point coprocessor target."),
9066 &showlist);
9067
9068 /* We really would like to have both "0" and "unlimited" work, but
9069 command.c doesn't deal with that. So make it a var_zinteger
9070 because the user can always use "999999" or some such for unlimited. */
9071 add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
9072 &heuristic_fence_post, _("\
9073 Set the distance searched for the start of a function."), _("\
9074 Show the distance searched for the start of a function."), _("\
9075 If you are debugging a stripped executable, GDB needs to search through the\n\
9076 program for the start of a function. This command sets the distance of the\n\
9077 search. The only need to set it is when debugging a stripped executable."),
9078 reinit_frame_cache_sfunc,
9079 NULL, /* FIXME: i18n: The distance searched for
9080 the start of a function is %s. */
9081 &setlist, &showlist);
9082
9083 /* Allow the user to control whether the upper bits of 64-bit
9084 addresses should be zeroed. */
9085 add_setshow_auto_boolean_cmd ("mask-address", no_class,
9086 &mask_address_var, _("\
9087 Set zeroing of upper 32 bits of 64-bit addresses."), _("\
9088 Show zeroing of upper 32 bits of 64-bit addresses."), _("\
9089 Use \"on\" to enable the masking, \"off\" to disable it and \"auto\" to\n\
9090 allow GDB to determine the correct value."),
9091 NULL, show_mask_address,
9092 &setmipscmdlist, &showmipscmdlist);
9093
9094 /* Allow the user to control the size of 32 bit registers within the
9095 raw remote packet. */
9096 add_setshow_boolean_cmd ("remote-mips64-transfers-32bit-regs", class_obscure,
9097 &mips64_transfers_32bit_regs_p, _("\
9098 Set compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
9099 _("\
9100 Show compatibility with 64-bit MIPS target that transfers 32-bit quantities."),
9101 _("\
9102 Use \"on\" to enable backward compatibility with older MIPS 64 GDB+target\n\
9103 that would transfer 32 bits for some registers (e.g. SR, FSR) and\n\
9104 64 bits for others. Use \"off\" to disable compatibility mode"),
9105 set_mips64_transfers_32bit_regs,
9106 NULL, /* FIXME: i18n: Compatibility with 64-bit
9107 MIPS target that transfers 32-bit
9108 quantities is %s. */
9109 &setlist, &showlist);
9110
9111 /* Debug this files internals. */
9112 add_setshow_zuinteger_cmd ("mips", class_maintenance,
9113 &mips_debug, _("\
9114 Set mips debugging."), _("\
9115 Show mips debugging."), _("\
9116 When non-zero, mips specific debugging is enabled."),
9117 NULL,
9118 NULL, /* FIXME: i18n: Mips debugging is
9119 currently %s. */
9120 &setdebuglist, &showdebuglist);
9121 }
This page took 0.273358 seconds and 5 git commands to generate.