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