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