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