Automatic date update in version.in
[binutils-gdb.git] / gdb / sparc-tdep.c
blob5f1d9efabea098e9234020a510de45962c45fdd3
1 /* Target-dependent code for SPARC.
3 Copyright (C) 2003-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "dis-asm.h"
23 #include "dwarf2.h"
24 #include "dwarf2/frame.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbcore.h"
29 #include "gdbtypes.h"
30 #include "inferior.h"
31 #include "symtab.h"
32 #include "objfiles.h"
33 #include "osabi.h"
34 #include "regcache.h"
35 #include "target.h"
36 #include "target-descriptions.h"
37 #include "value.h"
39 #include "sparc-tdep.h"
40 #include "sparc-ravenscar-thread.h"
41 #include <algorithm>
43 struct regset;
45 /* This file implements the SPARC 32-bit ABI as defined by the section
46 "Low-Level System Information" of the SPARC Compliance Definition
47 (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC. The SCD
48 lists changes with respect to the original 32-bit psABI as defined
49 in the "System V ABI, SPARC Processor Supplement".
51 Note that if we talk about SunOS, we mean SunOS 4.x, which was
52 BSD-based, which is sometimes (retroactively?) referred to as
53 Solaris 1.x. If we talk about Solaris we mean Solaris 2.x and
54 above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
55 suffering from severe version number inflation). Solaris 2.x is
56 also known as SunOS 5.x, since that's what uname(1) says. Solaris
57 2.x is SVR4-based. */
59 /* Please use the sparc32_-prefix for 32-bit specific code, the
60 sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
61 code that can handle both. The 64-bit specific code lives in
62 sparc64-tdep.c; don't add any here. */
64 /* The stack pointer is offset from the stack frame by a BIAS of 2047
65 (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
66 hosts, so undefine it first. */
67 #undef BIAS
68 #define BIAS 2047
70 /* Macros to extract fields from SPARC instructions. */
71 #define X_OP(i) (((i) >> 30) & 0x3)
72 #define X_RD(i) (((i) >> 25) & 0x1f)
73 #define X_A(i) (((i) >> 29) & 1)
74 #define X_COND(i) (((i) >> 25) & 0xf)
75 #define X_OP2(i) (((i) >> 22) & 0x7)
76 #define X_IMM22(i) ((i) & 0x3fffff)
77 #define X_OP3(i) (((i) >> 19) & 0x3f)
78 #define X_RS1(i) (((i) >> 14) & 0x1f)
79 #define X_RS2(i) ((i) & 0x1f)
80 #define X_I(i) (((i) >> 13) & 1)
81 /* Sign extension macros. */
82 #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
83 #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
84 #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
85 #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
86 /* Macros to identify some instructions. */
87 /* RETURN (RETT in V8) */
88 #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
90 /* Fetch the instruction at PC. Instructions are always big-endian
91 even if the processor operates in little-endian mode. */
93 unsigned long
94 sparc_fetch_instruction (CORE_ADDR pc)
96 gdb_byte buf[4];
97 unsigned long insn;
98 int i;
100 /* If we can't read the instruction at PC, return zero. */
101 if (target_read_memory (pc, buf, sizeof (buf)))
102 return 0;
104 insn = 0;
105 for (i = 0; i < sizeof (buf); i++)
106 insn = (insn << 8) | buf[i];
107 return insn;
111 /* Return non-zero if the instruction corresponding to PC is an "unimp"
112 instruction. */
114 static int
115 sparc_is_unimp_insn (CORE_ADDR pc)
117 const unsigned long insn = sparc_fetch_instruction (pc);
119 return ((insn & 0xc1c00000) == 0);
122 /* Return non-zero if the instruction corresponding to PC is an
123 "annulled" branch, i.e. the annul bit is set. */
126 sparc_is_annulled_branch_insn (CORE_ADDR pc)
128 /* The branch instructions featuring an annul bit can be identified
129 by the following bit patterns:
131 OP=0
132 OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
133 OP2=2: Branch on Integer Condition Codes (Bcc).
134 OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
135 OP2=6: Branch on FP Condition Codes (FBcc).
136 OP2=3 && Bit28=0:
137 Branch on Integer Register with Prediction (BPr).
139 This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
140 coprocessor branch instructions (Op2=7). */
142 const unsigned long insn = sparc_fetch_instruction (pc);
143 const unsigned op2 = X_OP2 (insn);
145 if ((X_OP (insn) == 0)
146 && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
147 || ((op2 == 3) && ((insn & 0x10000000) == 0))))
148 return X_A (insn);
149 else
150 return 0;
153 /* OpenBSD/sparc includes StackGhost, which according to the author's
154 website http://stackghost.cerias.purdue.edu "... transparently and
155 automatically protects applications' stack frames; more
156 specifically, it guards the return pointers. The protection
157 mechanisms require no application source or binary modification and
158 imposes only a negligible performance penalty."
160 The same website provides the following description of how
161 StackGhost works:
163 "StackGhost interfaces with the kernel trap handler that would
164 normally write out registers to the stack and the handler that
165 would read them back in. By XORing a cookie into the
166 return-address saved in the user stack when it is actually written
167 to the stack, and then XOR it out when the return-address is pulled
168 from the stack, StackGhost can cause attacker corrupted return
169 pointers to behave in a manner the attacker cannot predict.
170 StackGhost can also use several unused bits in the return pointer
171 to detect a smashed return pointer and abort the process."
173 For GDB this means that whenever we're reading %i7 from a stack
174 frame's window save area, we'll have to XOR the cookie.
176 More information on StackGuard can be found on in:
178 Mike Frantzen and Mike Shuey. "StackGhost: Hardware Facilitated
179 Stack Protection." 2001. Published in USENIX Security Symposium
180 '01. */
182 /* Fetch StackGhost Per-Process XOR cookie. */
184 ULONGEST
185 sparc_fetch_wcookie (struct gdbarch *gdbarch)
187 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
188 struct target_ops *ops = current_inferior ()->top_target ();
189 gdb_byte buf[8];
190 int len;
192 len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
193 if (len == -1)
194 return 0;
196 /* We should have either an 32-bit or an 64-bit cookie. */
197 gdb_assert (len == 4 || len == 8);
199 return extract_unsigned_integer (buf, len, byte_order);
203 /* The functions on this page are intended to be used to classify
204 function arguments. */
206 /* Check whether TYPE is "Integral or Pointer". */
208 static int
209 sparc_integral_or_pointer_p (const struct type *type)
211 int len = type->length ();
213 switch (type->code ())
215 case TYPE_CODE_INT:
216 case TYPE_CODE_BOOL:
217 case TYPE_CODE_CHAR:
218 case TYPE_CODE_ENUM:
219 case TYPE_CODE_RANGE:
220 /* We have byte, half-word, word and extended-word/doubleword
221 integral types. The doubleword is an extension to the
222 original 32-bit ABI by the SCD 2.4.x. */
223 return (len == 1 || len == 2 || len == 4 || len == 8);
224 case TYPE_CODE_PTR:
225 case TYPE_CODE_REF:
226 case TYPE_CODE_RVALUE_REF:
227 /* Allow either 32-bit or 64-bit pointers. */
228 return (len == 4 || len == 8);
229 default:
230 break;
233 return 0;
236 /* Check whether TYPE is "Floating". */
238 static int
239 sparc_floating_p (const struct type *type)
241 switch (type->code ())
243 case TYPE_CODE_FLT:
245 int len = type->length ();
246 return (len == 4 || len == 8 || len == 16);
248 default:
249 break;
252 return 0;
255 /* Check whether TYPE is "Complex Floating". */
257 static int
258 sparc_complex_floating_p (const struct type *type)
260 switch (type->code ())
262 case TYPE_CODE_COMPLEX:
264 int len = type->length ();
265 return (len == 8 || len == 16 || len == 32);
267 default:
268 break;
271 return 0;
274 /* Check whether TYPE is "Structure or Union".
276 In terms of Ada subprogram calls, arrays are treated the same as
277 struct and union types. So this function also returns non-zero
278 for array types. */
280 static int
281 sparc_structure_or_union_p (const struct type *type)
283 switch (type->code ())
285 case TYPE_CODE_STRUCT:
286 case TYPE_CODE_UNION:
287 case TYPE_CODE_ARRAY:
288 return 1;
289 default:
290 break;
293 return 0;
296 /* Return true if TYPE is returned by memory, false if returned by
297 register. */
299 static bool
300 sparc_structure_return_p (const struct type *type)
302 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
304 /* Float vectors are always returned by memory. */
305 if (sparc_floating_p (check_typedef (type->target_type ())))
306 return true;
307 /* Integer vectors are returned by memory if the vector size
308 is greater than 8 bytes long. */
309 return (type->length () > 8);
312 if (sparc_floating_p (type))
314 /* Floating point types are passed by register for size 4 and
315 8 bytes, and by memory for size 16 bytes. */
316 return (type->length () == 16);
319 /* Other than that, only aggregates of all sizes get returned by
320 memory. */
321 return sparc_structure_or_union_p (type);
324 /* Return true if arguments of the given TYPE are passed by
325 memory; false if returned by register. */
327 static bool
328 sparc_arg_by_memory_p (const struct type *type)
330 if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
332 /* Float vectors are always passed by memory. */
333 if (sparc_floating_p (check_typedef (type->target_type ())))
334 return true;
335 /* Integer vectors are passed by memory if the vector size
336 is greater than 8 bytes long. */
337 return (type->length () > 8);
340 /* Floats are passed by register for size 4 and 8 bytes, and by memory
341 for size 16 bytes. */
342 if (sparc_floating_p (type))
343 return (type->length () == 16);
345 /* Complex floats and aggregates of all sizes are passed by memory. */
346 if (sparc_complex_floating_p (type) || sparc_structure_or_union_p (type))
347 return true;
349 /* Everything else gets passed by register. */
350 return false;
353 /* Register information. */
354 #define SPARC32_FPU_REGISTERS \
355 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
356 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
357 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
358 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
359 #define SPARC32_CP0_REGISTERS \
360 "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
362 static const char * const sparc_core_register_names[] = {
363 SPARC_CORE_REGISTERS
365 static const char * const sparc32_fpu_register_names[] = {
366 SPARC32_FPU_REGISTERS
368 static const char * const sparc32_cp0_register_names[] = {
369 SPARC32_CP0_REGISTERS
372 static const char * const sparc32_register_names[] =
374 SPARC_CORE_REGISTERS,
375 SPARC32_FPU_REGISTERS,
376 SPARC32_CP0_REGISTERS
379 /* Total number of registers. */
380 #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
382 /* We provide the aliases %d0..%d30 for the floating registers as
383 "psuedo" registers. */
385 static const char * const sparc32_pseudo_register_names[] =
387 "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
388 "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
391 /* Total number of pseudo registers. */
392 #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
394 /* Return the name of pseudo register REGNUM. */
396 static const char *
397 sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
399 regnum -= gdbarch_num_regs (gdbarch);
401 if (regnum < SPARC32_NUM_PSEUDO_REGS)
402 return sparc32_pseudo_register_names[regnum];
404 internal_error (__FILE__, __LINE__,
405 _("sparc32_pseudo_register_name: bad register number %d"),
406 regnum);
409 /* Return the name of register REGNUM. */
411 static const char *
412 sparc32_register_name (struct gdbarch *gdbarch, int regnum)
414 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
415 return tdesc_register_name (gdbarch, regnum);
417 if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
418 return sparc32_register_names[regnum];
420 return sparc32_pseudo_register_name (gdbarch, regnum);
423 /* Construct types for ISA-specific registers. */
425 static struct type *
426 sparc_psr_type (struct gdbarch *gdbarch)
428 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
430 if (!tdep->sparc_psr_type)
432 struct type *type;
434 type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 32);
435 append_flags_type_flag (type, 5, "ET");
436 append_flags_type_flag (type, 6, "PS");
437 append_flags_type_flag (type, 7, "S");
438 append_flags_type_flag (type, 12, "EF");
439 append_flags_type_flag (type, 13, "EC");
441 tdep->sparc_psr_type = type;
444 return tdep->sparc_psr_type;
447 static struct type *
448 sparc_fsr_type (struct gdbarch *gdbarch)
450 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
452 if (!tdep->sparc_fsr_type)
454 struct type *type;
456 type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 32);
457 append_flags_type_flag (type, 0, "NXA");
458 append_flags_type_flag (type, 1, "DZA");
459 append_flags_type_flag (type, 2, "UFA");
460 append_flags_type_flag (type, 3, "OFA");
461 append_flags_type_flag (type, 4, "NVA");
462 append_flags_type_flag (type, 5, "NXC");
463 append_flags_type_flag (type, 6, "DZC");
464 append_flags_type_flag (type, 7, "UFC");
465 append_flags_type_flag (type, 8, "OFC");
466 append_flags_type_flag (type, 9, "NVC");
467 append_flags_type_flag (type, 22, "NS");
468 append_flags_type_flag (type, 23, "NXM");
469 append_flags_type_flag (type, 24, "DZM");
470 append_flags_type_flag (type, 25, "UFM");
471 append_flags_type_flag (type, 26, "OFM");
472 append_flags_type_flag (type, 27, "NVM");
474 tdep->sparc_fsr_type = type;
477 return tdep->sparc_fsr_type;
480 /* Return the GDB type object for the "standard" data type of data in
481 pseudo register REGNUM. */
483 static struct type *
484 sparc32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
486 regnum -= gdbarch_num_regs (gdbarch);
488 if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
489 return builtin_type (gdbarch)->builtin_double;
491 internal_error (__FILE__, __LINE__,
492 _("sparc32_pseudo_register_type: bad register number %d"),
493 regnum);
496 /* Return the GDB type object for the "standard" data type of data in
497 register REGNUM. */
499 static struct type *
500 sparc32_register_type (struct gdbarch *gdbarch, int regnum)
502 if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
503 return tdesc_register_type (gdbarch, regnum);
505 if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
506 return builtin_type (gdbarch)->builtin_float;
508 if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
509 return builtin_type (gdbarch)->builtin_data_ptr;
511 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
512 return builtin_type (gdbarch)->builtin_func_ptr;
514 if (regnum == SPARC32_PSR_REGNUM)
515 return sparc_psr_type (gdbarch);
517 if (regnum == SPARC32_FSR_REGNUM)
518 return sparc_fsr_type (gdbarch);
520 if (regnum >= gdbarch_num_regs (gdbarch))
521 return sparc32_pseudo_register_type (gdbarch, regnum);
523 return builtin_type (gdbarch)->builtin_int32;
526 static enum register_status
527 sparc32_pseudo_register_read (struct gdbarch *gdbarch,
528 readable_regcache *regcache,
529 int regnum, gdb_byte *buf)
531 enum register_status status;
533 regnum -= gdbarch_num_regs (gdbarch);
534 gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
536 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
537 status = regcache->raw_read (regnum, buf);
538 if (status == REG_VALID)
539 status = regcache->raw_read (regnum + 1, buf + 4);
540 return status;
543 static void
544 sparc32_pseudo_register_write (struct gdbarch *gdbarch,
545 struct regcache *regcache,
546 int regnum, const gdb_byte *buf)
548 regnum -= gdbarch_num_regs (gdbarch);
549 gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
551 regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
552 regcache->raw_write (regnum, buf);
553 regcache->raw_write (regnum + 1, buf + 4);
556 /* Implement the stack_frame_destroyed_p gdbarch method. */
559 sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
561 /* This function must return true if we are one instruction after an
562 instruction that destroyed the stack frame of the current
563 function. The SPARC instructions used to restore the callers
564 stack frame are RESTORE and RETURN/RETT.
566 Of these RETURN/RETT is a branch instruction and thus we return
567 true if we are in its delay slot.
569 RESTORE is almost always found in the delay slot of a branch
570 instruction that transfers control to the caller, such as JMPL.
571 Thus the next instruction is in the caller frame and we don't
572 need to do anything about it. */
574 unsigned int insn = sparc_fetch_instruction (pc - 4);
576 return X_RETTURN (insn);
580 static CORE_ADDR
581 sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
583 /* The ABI requires double-word alignment. */
584 return address & ~0x7;
587 static CORE_ADDR
588 sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
589 CORE_ADDR funcaddr,
590 struct value **args, int nargs,
591 struct type *value_type,
592 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
593 struct regcache *regcache)
595 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
597 *bp_addr = sp - 4;
598 *real_pc = funcaddr;
600 if (using_struct_return (gdbarch, NULL, value_type))
602 gdb_byte buf[4];
604 /* This is an UNIMP instruction. */
605 store_unsigned_integer (buf, 4, byte_order,
606 value_type->length () & 0x1fff);
607 write_memory (sp - 8, buf, 4);
608 return sp - 8;
611 return sp - 4;
614 static CORE_ADDR
615 sparc32_store_arguments (struct regcache *regcache, int nargs,
616 struct value **args, CORE_ADDR sp,
617 function_call_return_method return_method,
618 CORE_ADDR struct_addr)
620 struct gdbarch *gdbarch = regcache->arch ();
621 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
622 /* Number of words in the "parameter array". */
623 int num_elements = 0;
624 int element = 0;
625 int i;
627 for (i = 0; i < nargs; i++)
629 struct type *type = value_type (args[i]);
630 int len = type->length ();
632 if (sparc_arg_by_memory_p (type))
634 /* Structure, Union and Quad-Precision Arguments. */
635 sp -= len;
637 /* Use doubleword alignment for these values. That's always
638 correct, and wasting a few bytes shouldn't be a problem. */
639 sp &= ~0x7;
641 write_memory (sp, value_contents (args[i]).data (), len);
642 args[i] = value_from_pointer (lookup_pointer_type (type), sp);
643 num_elements++;
645 else if (sparc_floating_p (type))
647 /* Floating arguments. */
648 gdb_assert (len == 4 || len == 8);
649 num_elements += (len / 4);
651 else
653 /* Arguments passed via the General Purpose Registers. */
654 num_elements += ((len + 3) / 4);
658 /* Always allocate at least six words. */
659 sp -= std::max (6, num_elements) * 4;
661 /* The psABI says that "Software convention requires space for the
662 struct/union return value pointer, even if the word is unused." */
663 sp -= 4;
665 /* The psABI says that "Although software convention and the
666 operating system require every stack frame to be doubleword
667 aligned." */
668 sp &= ~0x7;
670 for (i = 0; i < nargs; i++)
672 const bfd_byte *valbuf = value_contents (args[i]).data ();
673 struct type *type = value_type (args[i]);
674 int len = type->length ();
675 gdb_byte buf[4];
677 if (len < 4)
679 memset (buf, 0, 4 - len);
680 memcpy (buf + 4 - len, valbuf, len);
681 valbuf = buf;
682 len = 4;
685 gdb_assert (len == 4 || len == 8);
687 if (element < 6)
689 int regnum = SPARC_O0_REGNUM + element;
691 regcache->cooked_write (regnum, valbuf);
692 if (len > 4 && element < 5)
693 regcache->cooked_write (regnum + 1, valbuf + 4);
696 /* Always store the argument in memory. */
697 write_memory (sp + 4 + element * 4, valbuf, len);
698 element += len / 4;
701 gdb_assert (element == num_elements);
703 if (return_method == return_method_struct)
705 gdb_byte buf[4];
707 store_unsigned_integer (buf, 4, byte_order, struct_addr);
708 write_memory (sp, buf, 4);
711 return sp;
714 static CORE_ADDR
715 sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
716 struct regcache *regcache, CORE_ADDR bp_addr,
717 int nargs, struct value **args, CORE_ADDR sp,
718 function_call_return_method return_method,
719 CORE_ADDR struct_addr)
721 CORE_ADDR call_pc = (return_method == return_method_struct
722 ? (bp_addr - 12) : (bp_addr - 8));
724 /* Set return address. */
725 regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
727 /* Set up function arguments. */
728 sp = sparc32_store_arguments (regcache, nargs, args, sp, return_method,
729 struct_addr);
731 /* Allocate the 16-word window save area. */
732 sp -= 16 * 4;
734 /* Stack should be doubleword aligned at this point. */
735 gdb_assert (sp % 8 == 0);
737 /* Finally, update the stack pointer. */
738 regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
740 return sp;
744 /* Use the program counter to determine the contents and size of a
745 breakpoint instruction. Return a pointer to a string of bytes that
746 encode a breakpoint instruction, store the length of the string in
747 *LEN and optionally adjust *PC to point to the correct memory
748 location for inserting the breakpoint. */
749 constexpr gdb_byte sparc_break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
751 typedef BP_MANIPULATION (sparc_break_insn) sparc_breakpoint;
754 /* Allocate and initialize a frame cache. */
756 static struct sparc_frame_cache *
757 sparc_alloc_frame_cache (void)
759 struct sparc_frame_cache *cache;
761 cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
763 /* Base address. */
764 cache->base = 0;
765 cache->pc = 0;
767 /* Frameless until proven otherwise. */
768 cache->frameless_p = 1;
769 cache->frame_offset = 0;
770 cache->saved_regs_mask = 0;
771 cache->copied_regs_mask = 0;
772 cache->struct_return_p = 0;
774 return cache;
777 /* GCC generates several well-known sequences of instructions at the begining
778 of each function prologue when compiling with -fstack-check. If one of
779 such sequences starts at START_PC, then return the address of the
780 instruction immediately past this sequence. Otherwise, return START_PC. */
782 static CORE_ADDR
783 sparc_skip_stack_check (const CORE_ADDR start_pc)
785 CORE_ADDR pc = start_pc;
786 unsigned long insn;
787 int probing_loop = 0;
789 /* With GCC, all stack checking sequences begin with the same two
790 instructions, plus an optional one in the case of a probing loop:
792 sethi <some immediate>, %g1
793 sub %sp, %g1, %g1
797 sethi <some immediate>, %g1
798 sethi <some immediate>, %g4
799 sub %sp, %g1, %g1
803 sethi <some immediate>, %g1
804 sub %sp, %g1, %g1
805 sethi <some immediate>, %g4
807 If the optional instruction is found (setting g4), assume that a
808 probing loop will follow. */
810 /* sethi <some immediate>, %g1 */
811 insn = sparc_fetch_instruction (pc);
812 pc = pc + 4;
813 if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
814 return start_pc;
816 /* optional: sethi <some immediate>, %g4 */
817 insn = sparc_fetch_instruction (pc);
818 pc = pc + 4;
819 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
821 probing_loop = 1;
822 insn = sparc_fetch_instruction (pc);
823 pc = pc + 4;
826 /* sub %sp, %g1, %g1 */
827 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
828 && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
829 return start_pc;
831 insn = sparc_fetch_instruction (pc);
832 pc = pc + 4;
834 /* optional: sethi <some immediate>, %g4 */
835 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
837 probing_loop = 1;
838 insn = sparc_fetch_instruction (pc);
839 pc = pc + 4;
842 /* First possible sequence:
843 [first two instructions above]
844 clr [%g1 - some immediate] */
846 /* clr [%g1 - some immediate] */
847 if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
848 && X_RS1 (insn) == 1 && X_RD (insn) == 0)
850 /* Valid stack-check sequence, return the new PC. */
851 return pc;
854 /* Second possible sequence: A small number of probes.
855 [first two instructions above]
856 clr [%g1]
857 add %g1, -<some immediate>, %g1
858 clr [%g1]
859 [repeat the two instructions above any (small) number of times]
860 clr [%g1 - some immediate] */
862 /* clr [%g1] */
863 else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
864 && X_RS1 (insn) == 1 && X_RD (insn) == 0)
866 while (1)
868 /* add %g1, -<some immediate>, %g1 */
869 insn = sparc_fetch_instruction (pc);
870 pc = pc + 4;
871 if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
872 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
873 break;
875 /* clr [%g1] */
876 insn = sparc_fetch_instruction (pc);
877 pc = pc + 4;
878 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
879 && X_RD (insn) == 0 && X_RS1 (insn) == 1))
880 return start_pc;
883 /* clr [%g1 - some immediate] */
884 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
885 && X_RS1 (insn) == 1 && X_RD (insn) == 0))
886 return start_pc;
888 /* We found a valid stack-check sequence, return the new PC. */
889 return pc;
892 /* Third sequence: A probing loop.
893 [first three instructions above]
894 sub %g1, %g4, %g4
895 cmp %g1, %g4
896 be <disp>
897 add %g1, -<some immediate>, %g1
898 ba <disp>
899 clr [%g1]
901 And an optional last probe for the remainder:
903 clr [%g4 - some immediate] */
905 if (probing_loop)
907 /* sub %g1, %g4, %g4 */
908 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
909 && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
910 return start_pc;
912 /* cmp %g1, %g4 */
913 insn = sparc_fetch_instruction (pc);
914 pc = pc + 4;
915 if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
916 && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
917 return start_pc;
919 /* be <disp> */
920 insn = sparc_fetch_instruction (pc);
921 pc = pc + 4;
922 if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
923 return start_pc;
925 /* add %g1, -<some immediate>, %g1 */
926 insn = sparc_fetch_instruction (pc);
927 pc = pc + 4;
928 if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
929 && X_RS1 (insn) == 1 && X_RD (insn) == 1))
930 return start_pc;
932 /* ba <disp> */
933 insn = sparc_fetch_instruction (pc);
934 pc = pc + 4;
935 if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
936 return start_pc;
938 /* clr [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
939 insn = sparc_fetch_instruction (pc);
940 pc = pc + 4;
941 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
942 && X_RD (insn) == 0 && X_RS1 (insn) == 1
943 && (!X_I(insn) || X_SIMM13 (insn) == 0)))
944 return start_pc;
946 /* We found a valid stack-check sequence, return the new PC. */
948 /* optional: clr [%g4 - some immediate] */
949 insn = sparc_fetch_instruction (pc);
950 pc = pc + 4;
951 if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
952 && X_RS1 (insn) == 4 && X_RD (insn) == 0))
953 return pc - 4;
954 else
955 return pc;
958 /* No stack check code in our prologue, return the start_pc. */
959 return start_pc;
962 /* Record the effect of a SAVE instruction on CACHE. */
964 void
965 sparc_record_save_insn (struct sparc_frame_cache *cache)
967 /* The frame is set up. */
968 cache->frameless_p = 0;
970 /* The frame pointer contains the CFA. */
971 cache->frame_offset = 0;
973 /* The `local' and `in' registers are all saved. */
974 cache->saved_regs_mask = 0xffff;
976 /* The `out' registers are all renamed. */
977 cache->copied_regs_mask = 0xff;
980 /* Do a full analysis of the prologue at PC and update CACHE accordingly.
981 Bail out early if CURRENT_PC is reached. Return the address where
982 the analysis stopped.
984 We handle both the traditional register window model and the single
985 register window (aka flat) model. */
987 CORE_ADDR
988 sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
989 CORE_ADDR current_pc, struct sparc_frame_cache *cache)
991 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
992 unsigned long insn;
993 int offset = 0;
994 int dest = -1;
996 pc = sparc_skip_stack_check (pc);
998 if (current_pc <= pc)
999 return current_pc;
1001 /* We have to handle to "Procedure Linkage Table" (PLT) special. On
1002 SPARC the linker usually defines a symbol (typically
1003 _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
1004 This symbol makes us end up here with PC pointing at the start of
1005 the PLT and CURRENT_PC probably pointing at a PLT entry. If we
1006 would do our normal prologue analysis, we would probably conclude
1007 that we've got a frame when in reality we don't, since the
1008 dynamic linker patches up the first PLT with some code that
1009 starts with a SAVE instruction. Patch up PC such that it points
1010 at the start of our PLT entry. */
1011 if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
1012 pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
1014 insn = sparc_fetch_instruction (pc);
1016 /* Recognize store insns and record their sources. */
1017 while (X_OP (insn) == 3
1018 && (X_OP3 (insn) == 0x4 /* stw */
1019 || X_OP3 (insn) == 0x7 /* std */
1020 || X_OP3 (insn) == 0xe) /* stx */
1021 && X_RS1 (insn) == SPARC_SP_REGNUM)
1023 int regnum = X_RD (insn);
1025 /* Recognize stores into the corresponding stack slots. */
1026 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1027 && ((X_I (insn)
1028 && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
1029 ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
1030 : (regnum - SPARC_L0_REGNUM) * 4))
1031 || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
1033 cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
1034 if (X_OP3 (insn) == 0x7)
1035 cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
1038 offset += 4;
1040 insn = sparc_fetch_instruction (pc + offset);
1043 /* Recognize a SETHI insn and record its destination. */
1044 if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
1046 dest = X_RD (insn);
1047 offset += 4;
1049 insn = sparc_fetch_instruction (pc + offset);
1052 /* Allow for an arithmetic operation on DEST or %g1. */
1053 if (X_OP (insn) == 2 && X_I (insn)
1054 && (X_RD (insn) == 1 || X_RD (insn) == dest))
1056 offset += 4;
1058 insn = sparc_fetch_instruction (pc + offset);
1061 /* Check for the SAVE instruction that sets up the frame. */
1062 if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
1064 sparc_record_save_insn (cache);
1065 offset += 4;
1066 return pc + offset;
1069 /* Check for an arithmetic operation on %sp. */
1070 if (X_OP (insn) == 2
1071 && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1072 && X_RS1 (insn) == SPARC_SP_REGNUM
1073 && X_RD (insn) == SPARC_SP_REGNUM)
1075 if (X_I (insn))
1077 cache->frame_offset = X_SIMM13 (insn);
1078 if (X_OP3 (insn) == 0)
1079 cache->frame_offset = -cache->frame_offset;
1081 offset += 4;
1083 insn = sparc_fetch_instruction (pc + offset);
1085 /* Check for an arithmetic operation that sets up the frame. */
1086 if (X_OP (insn) == 2
1087 && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
1088 && X_RS1 (insn) == SPARC_SP_REGNUM
1089 && X_RD (insn) == SPARC_FP_REGNUM)
1091 cache->frameless_p = 0;
1092 cache->frame_offset = 0;
1093 /* We could check that the amount subtracted to %sp above is the
1094 same as the one added here, but this seems superfluous. */
1095 cache->copied_regs_mask |= 0x40;
1096 offset += 4;
1098 insn = sparc_fetch_instruction (pc + offset);
1101 /* Check for a move (or) operation that copies the return register. */
1102 if (X_OP (insn) == 2
1103 && X_OP3 (insn) == 0x2
1104 && !X_I (insn)
1105 && X_RS1 (insn) == SPARC_G0_REGNUM
1106 && X_RS2 (insn) == SPARC_O7_REGNUM
1107 && X_RD (insn) == SPARC_I7_REGNUM)
1109 cache->copied_regs_mask |= 0x80;
1110 offset += 4;
1113 return pc + offset;
1116 return pc;
1119 /* Return PC of first real instruction of the function starting at
1120 START_PC. */
1122 static CORE_ADDR
1123 sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
1125 CORE_ADDR func_addr;
1126 struct sparc_frame_cache cache;
1128 /* This is the preferred method, find the end of the prologue by
1129 using the debugging information. */
1131 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
1133 CORE_ADDR post_prologue_pc
1134 = skip_prologue_using_sal (gdbarch, func_addr);
1136 if (post_prologue_pc != 0)
1137 return std::max (start_pc, post_prologue_pc);
1140 start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
1142 /* The psABI says that "Although the first 6 words of arguments
1143 reside in registers, the standard stack frame reserves space for
1144 them.". It also suggests that a function may use that space to
1145 "write incoming arguments 0 to 5" into that space, and that's
1146 indeed what GCC seems to be doing. In that case GCC will
1147 generate debug information that points to the stack slots instead
1148 of the registers, so we should consider the instructions that
1149 write out these incoming arguments onto the stack. */
1151 while (1)
1153 unsigned long insn = sparc_fetch_instruction (start_pc);
1155 /* Recognize instructions that store incoming arguments into the
1156 corresponding stack slots. */
1157 if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
1158 && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
1160 int regnum = X_RD (insn);
1162 /* Case of arguments still in %o[0..5]. */
1163 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
1164 && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
1165 && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
1167 start_pc += 4;
1168 continue;
1171 /* Case of arguments copied into %i[0..5]. */
1172 if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
1173 && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
1174 && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
1176 start_pc += 4;
1177 continue;
1181 break;
1184 return start_pc;
1187 /* Normal frames. */
1189 struct sparc_frame_cache *
1190 sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
1192 struct sparc_frame_cache *cache;
1194 if (*this_cache)
1195 return (struct sparc_frame_cache *) *this_cache;
1197 cache = sparc_alloc_frame_cache ();
1198 *this_cache = cache;
1200 cache->pc = get_frame_func (this_frame);
1201 if (cache->pc != 0)
1202 sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
1203 get_frame_pc (this_frame), cache);
1205 if (cache->frameless_p)
1207 /* This function is frameless, so %fp (%i6) holds the frame
1208 pointer for our calling frame. Use %sp (%o6) as this frame's
1209 base address. */
1210 cache->base =
1211 get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1213 else
1215 /* For normal frames, %fp (%i6) holds the frame pointer, the
1216 base address for the current stack frame. */
1217 cache->base =
1218 get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
1221 cache->base += cache->frame_offset;
1223 if (cache->base & 1)
1224 cache->base += BIAS;
1226 return cache;
1229 static int
1230 sparc32_struct_return_from_sym (struct symbol *sym)
1232 struct type *type = check_typedef (sym->type ());
1233 enum type_code code = type->code ();
1235 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
1237 type = check_typedef (type->target_type ());
1238 if (sparc_structure_or_union_p (type)
1239 || (sparc_floating_p (type) && type->length () == 16))
1240 return 1;
1243 return 0;
1246 struct sparc_frame_cache *
1247 sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
1249 struct sparc_frame_cache *cache;
1250 struct symbol *sym;
1252 if (*this_cache)
1253 return (struct sparc_frame_cache *) *this_cache;
1255 cache = sparc_frame_cache (this_frame, this_cache);
1257 sym = find_pc_function (cache->pc);
1258 if (sym)
1260 cache->struct_return_p = sparc32_struct_return_from_sym (sym);
1262 else
1264 /* There is no debugging information for this function to
1265 help us determine whether this function returns a struct
1266 or not. So we rely on another heuristic which is to check
1267 the instruction at the return address and see if this is
1268 an "unimp" instruction. If it is, then it is a struct-return
1269 function. */
1270 CORE_ADDR pc;
1271 int regnum =
1272 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1274 pc = get_frame_register_unsigned (this_frame, regnum) + 8;
1275 if (sparc_is_unimp_insn (pc))
1276 cache->struct_return_p = 1;
1279 return cache;
1282 static void
1283 sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
1284 struct frame_id *this_id)
1286 struct sparc_frame_cache *cache =
1287 sparc32_frame_cache (this_frame, this_cache);
1289 /* This marks the outermost frame. */
1290 if (cache->base == 0)
1291 return;
1293 (*this_id) = frame_id_build (cache->base, cache->pc);
1296 static struct value *
1297 sparc32_frame_prev_register (struct frame_info *this_frame,
1298 void **this_cache, int regnum)
1300 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1301 struct sparc_frame_cache *cache =
1302 sparc32_frame_cache (this_frame, this_cache);
1304 if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
1306 CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
1308 /* If this functions has a Structure, Union or Quad-Precision
1309 return value, we have to skip the UNIMP instruction that encodes
1310 the size of the structure. */
1311 if (cache->struct_return_p)
1312 pc += 4;
1314 regnum =
1315 (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
1316 pc += get_frame_register_unsigned (this_frame, regnum) + 8;
1317 return frame_unwind_got_constant (this_frame, regnum, pc);
1320 /* Handle StackGhost. */
1322 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1324 if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
1326 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1327 ULONGEST i7;
1329 /* Read the value in from memory. */
1330 i7 = get_frame_memory_unsigned (this_frame, addr, 4);
1331 return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
1335 /* The previous frame's `local' and `in' registers may have been saved
1336 in the register save area. */
1337 if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
1338 && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
1340 CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
1342 return frame_unwind_got_memory (this_frame, regnum, addr);
1345 /* The previous frame's `out' registers may be accessible as the current
1346 frame's `in' registers. */
1347 if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
1348 && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
1349 regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
1351 return frame_unwind_got_register (this_frame, regnum, regnum);
1354 static const struct frame_unwind sparc32_frame_unwind =
1356 "sparc32 prologue",
1357 NORMAL_FRAME,
1358 default_frame_unwind_stop_reason,
1359 sparc32_frame_this_id,
1360 sparc32_frame_prev_register,
1361 NULL,
1362 default_frame_sniffer
1366 static CORE_ADDR
1367 sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
1369 struct sparc_frame_cache *cache =
1370 sparc32_frame_cache (this_frame, this_cache);
1372 return cache->base;
1375 static const struct frame_base sparc32_frame_base =
1377 &sparc32_frame_unwind,
1378 sparc32_frame_base_address,
1379 sparc32_frame_base_address,
1380 sparc32_frame_base_address
1383 static struct frame_id
1384 sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1386 CORE_ADDR sp;
1388 sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
1389 if (sp & 1)
1390 sp += BIAS;
1391 return frame_id_build (sp, get_frame_pc (this_frame));
1395 /* Extract a function return value of TYPE from REGCACHE, and copy
1396 that into VALBUF. */
1398 static void
1399 sparc32_extract_return_value (struct type *type, struct regcache *regcache,
1400 gdb_byte *valbuf)
1402 int len = type->length ();
1403 gdb_byte buf[32];
1405 gdb_assert (!sparc_structure_return_p (type));
1407 if (sparc_floating_p (type) || sparc_complex_floating_p (type)
1408 || type->code () == TYPE_CODE_ARRAY)
1410 /* Floating return values. */
1411 regcache->cooked_read (SPARC_F0_REGNUM, buf);
1412 if (len > 4)
1413 regcache->cooked_read (SPARC_F1_REGNUM, buf + 4);
1414 if (len > 8)
1416 regcache->cooked_read (SPARC_F2_REGNUM, buf + 8);
1417 regcache->cooked_read (SPARC_F3_REGNUM, buf + 12);
1419 if (len > 16)
1421 regcache->cooked_read (SPARC_F4_REGNUM, buf + 16);
1422 regcache->cooked_read (SPARC_F5_REGNUM, buf + 20);
1423 regcache->cooked_read (SPARC_F6_REGNUM, buf + 24);
1424 regcache->cooked_read (SPARC_F7_REGNUM, buf + 28);
1426 memcpy (valbuf, buf, len);
1428 else
1430 /* Integral and pointer return values. */
1431 gdb_assert (sparc_integral_or_pointer_p (type));
1433 regcache->cooked_read (SPARC_O0_REGNUM, buf);
1434 if (len > 4)
1436 regcache->cooked_read (SPARC_O1_REGNUM, buf + 4);
1437 gdb_assert (len == 8);
1438 memcpy (valbuf, buf, 8);
1440 else
1442 /* Just stripping off any unused bytes should preserve the
1443 signed-ness just fine. */
1444 memcpy (valbuf, buf + 4 - len, len);
1449 /* Store the function return value of type TYPE from VALBUF into
1450 REGCACHE. */
1452 static void
1453 sparc32_store_return_value (struct type *type, struct regcache *regcache,
1454 const gdb_byte *valbuf)
1456 int len = type->length ();
1457 gdb_byte buf[32];
1459 gdb_assert (!sparc_structure_return_p (type));
1461 if (sparc_floating_p (type) || sparc_complex_floating_p (type))
1463 /* Floating return values. */
1464 memcpy (buf, valbuf, len);
1465 regcache->cooked_write (SPARC_F0_REGNUM, buf);
1466 if (len > 4)
1467 regcache->cooked_write (SPARC_F1_REGNUM, buf + 4);
1468 if (len > 8)
1470 regcache->cooked_write (SPARC_F2_REGNUM, buf + 8);
1471 regcache->cooked_write (SPARC_F3_REGNUM, buf + 12);
1473 if (len > 16)
1475 regcache->cooked_write (SPARC_F4_REGNUM, buf + 16);
1476 regcache->cooked_write (SPARC_F5_REGNUM, buf + 20);
1477 regcache->cooked_write (SPARC_F6_REGNUM, buf + 24);
1478 regcache->cooked_write (SPARC_F7_REGNUM, buf + 28);
1481 else
1483 /* Integral and pointer return values. */
1484 gdb_assert (sparc_integral_or_pointer_p (type));
1486 if (len > 4)
1488 gdb_assert (len == 8);
1489 memcpy (buf, valbuf, 8);
1490 regcache->cooked_write (SPARC_O1_REGNUM, buf + 4);
1492 else
1494 /* ??? Do we need to do any sign-extension here? */
1495 memcpy (buf + 4 - len, valbuf, len);
1497 regcache->cooked_write (SPARC_O0_REGNUM, buf);
1501 static enum return_value_convention
1502 sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
1503 struct type *type, struct regcache *regcache,
1504 gdb_byte *readbuf, const gdb_byte *writebuf)
1506 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1508 /* The psABI says that "...every stack frame reserves the word at
1509 %fp+64. If a function returns a structure, union, or
1510 quad-precision value, this word should hold the address of the
1511 object into which the return value should be copied." This
1512 guarantees that we can always find the return value, not just
1513 before the function returns. */
1515 if (sparc_structure_return_p (type))
1517 ULONGEST sp;
1518 CORE_ADDR addr;
1520 if (readbuf)
1522 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1523 addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1524 read_memory (addr, readbuf, type->length ());
1526 if (writebuf)
1528 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
1529 addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
1530 write_memory (addr, writebuf, type->length ());
1533 return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
1536 if (readbuf)
1537 sparc32_extract_return_value (type, regcache, readbuf);
1538 if (writebuf)
1539 sparc32_store_return_value (type, regcache, writebuf);
1541 return RETURN_VALUE_REGISTER_CONVENTION;
1544 static int
1545 sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
1547 return (sparc_structure_or_union_p (type)
1548 || (sparc_floating_p (type) && type->length () == 16)
1549 || sparc_complex_floating_p (type));
1552 static int
1553 sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
1555 CORE_ADDR pc = get_frame_address_in_block (this_frame);
1556 struct symbol *sym = find_pc_function (pc);
1558 if (sym)
1559 return sparc32_struct_return_from_sym (sym);
1560 return 0;
1563 static void
1564 sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1565 struct dwarf2_frame_state_reg *reg,
1566 struct frame_info *this_frame)
1568 int off;
1570 switch (regnum)
1572 case SPARC_G0_REGNUM:
1573 /* Since %g0 is always zero, there is no point in saving it, and
1574 people will be inclined omit it from the CFI. Make sure we
1575 don't warn about that. */
1576 reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1577 break;
1578 case SPARC_SP_REGNUM:
1579 reg->how = DWARF2_FRAME_REG_CFA;
1580 break;
1581 case SPARC32_PC_REGNUM:
1582 case SPARC32_NPC_REGNUM:
1583 reg->how = DWARF2_FRAME_REG_RA_OFFSET;
1584 off = 8;
1585 if (sparc32_dwarf2_struct_return_p (this_frame))
1586 off += 4;
1587 if (regnum == SPARC32_NPC_REGNUM)
1588 off += 4;
1589 reg->loc.offset = off;
1590 break;
1594 /* Implement the execute_dwarf_cfa_vendor_op method. */
1596 static bool
1597 sparc_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
1598 struct dwarf2_frame_state *fs)
1600 /* Only DW_CFA_GNU_window_save is expected on SPARC. */
1601 if (op != DW_CFA_GNU_window_save)
1602 return false;
1604 uint64_t reg;
1605 int size = register_size (gdbarch, 0);
1607 fs->regs.alloc_regs (32);
1608 for (reg = 8; reg < 16; reg++)
1610 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
1611 fs->regs.reg[reg].loc.reg = reg + 16;
1613 for (reg = 16; reg < 32; reg++)
1615 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
1616 fs->regs.reg[reg].loc.offset = (reg - 16) * size;
1619 return true;
1623 /* The SPARC Architecture doesn't have hardware single-step support,
1624 and most operating systems don't implement it either, so we provide
1625 software single-step mechanism. */
1627 static CORE_ADDR
1628 sparc_analyze_control_transfer (struct regcache *regcache,
1629 CORE_ADDR pc, CORE_ADDR *npc)
1631 unsigned long insn = sparc_fetch_instruction (pc);
1632 int conditional_p = X_COND (insn) & 0x7;
1633 int branch_p = 0, fused_p = 0;
1634 long offset = 0; /* Must be signed for sign-extend. */
1636 if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
1638 if ((insn & 0x10000000) == 0)
1640 /* Branch on Integer Register with Prediction (BPr). */
1641 branch_p = 1;
1642 conditional_p = 1;
1644 else
1646 /* Compare and Branch */
1647 branch_p = 1;
1648 fused_p = 1;
1649 offset = 4 * X_DISP10 (insn);
1652 else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
1654 /* Branch on Floating-Point Condition Codes (FBfcc). */
1655 branch_p = 1;
1656 offset = 4 * X_DISP22 (insn);
1658 else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
1660 /* Branch on Floating-Point Condition Codes with Prediction
1661 (FBPfcc). */
1662 branch_p = 1;
1663 offset = 4 * X_DISP19 (insn);
1665 else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
1667 /* Branch on Integer Condition Codes (Bicc). */
1668 branch_p = 1;
1669 offset = 4 * X_DISP22 (insn);
1671 else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
1673 /* Branch on Integer Condition Codes with Prediction (BPcc). */
1674 branch_p = 1;
1675 offset = 4 * X_DISP19 (insn);
1677 else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
1679 struct frame_info *frame = get_current_frame ();
1681 /* Trap instruction (TRAP). */
1682 gdbarch *arch = regcache->arch ();
1683 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1684 return tdep->step_trap (frame, insn);
1687 /* FIXME: Handle DONE and RETRY instructions. */
1689 if (branch_p)
1691 if (fused_p)
1693 /* Fused compare-and-branch instructions are non-delayed,
1694 and do not have an annulling capability. So we need to
1695 always set a breakpoint on both the NPC and the branch
1696 target address. */
1697 gdb_assert (offset != 0);
1698 return pc + offset;
1700 else if (conditional_p)
1702 /* For conditional branches, return nPC + 4 iff the annul
1703 bit is 1. */
1704 return (X_A (insn) ? *npc + 4 : 0);
1706 else
1708 /* For unconditional branches, return the target if its
1709 specified condition is "always" and return nPC + 4 if the
1710 condition is "never". If the annul bit is 1, set *NPC to
1711 zero. */
1712 if (X_COND (insn) == 0x0)
1713 pc = *npc, offset = 4;
1714 if (X_A (insn))
1715 *npc = 0;
1717 return pc + offset;
1721 return 0;
1724 static CORE_ADDR
1725 sparc_step_trap (struct frame_info *frame, unsigned long insn)
1727 return 0;
1730 static std::vector<CORE_ADDR>
1731 sparc_software_single_step (struct regcache *regcache)
1733 struct gdbarch *arch = regcache->arch ();
1734 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1735 CORE_ADDR npc, nnpc;
1737 CORE_ADDR pc, orig_npc;
1738 std::vector<CORE_ADDR> next_pcs;
1740 pc = regcache_raw_get_unsigned (regcache, tdep->pc_regnum);
1741 orig_npc = npc = regcache_raw_get_unsigned (regcache, tdep->npc_regnum);
1743 /* Analyze the instruction at PC. */
1744 nnpc = sparc_analyze_control_transfer (regcache, pc, &npc);
1745 if (npc != 0)
1746 next_pcs.push_back (npc);
1748 if (nnpc != 0)
1749 next_pcs.push_back (nnpc);
1751 /* Assert that we have set at least one breakpoint, and that
1752 they're not set at the same spot - unless we're going
1753 from here straight to NULL, i.e. a call or jump to 0. */
1754 gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
1755 gdb_assert (nnpc != npc || orig_npc == 0);
1757 return next_pcs;
1760 static void
1761 sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
1763 gdbarch *arch = regcache->arch ();
1764 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (arch);
1766 regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
1767 regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
1771 /* Iterate over core file register note sections. */
1773 static void
1774 sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
1775 iterate_over_regset_sections_cb *cb,
1776 void *cb_data,
1777 const struct regcache *regcache)
1779 sparc_gdbarch_tdep *tdep = gdbarch_tdep<sparc_gdbarch_tdep> (gdbarch);
1781 cb (".reg", tdep->sizeof_gregset, tdep->sizeof_gregset, tdep->gregset, NULL,
1782 cb_data);
1783 cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, tdep->fpregset,
1784 NULL, cb_data);
1788 static int
1789 validate_tdesc_registers (const struct target_desc *tdesc,
1790 struct tdesc_arch_data *tdesc_data,
1791 const char *feature_name,
1792 const char * const register_names[],
1793 unsigned int registers_num,
1794 unsigned int reg_start)
1796 int valid_p = 1;
1797 const struct tdesc_feature *feature;
1799 feature = tdesc_find_feature (tdesc, feature_name);
1800 if (feature == NULL)
1801 return 0;
1803 for (unsigned int i = 0; i < registers_num; i++)
1804 valid_p &= tdesc_numbered_register (feature, tdesc_data,
1805 reg_start + i,
1806 register_names[i]);
1808 return valid_p;
1811 static struct gdbarch *
1812 sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1814 const struct target_desc *tdesc = info.target_desc;
1815 struct gdbarch *gdbarch;
1816 int valid_p = 1;
1818 /* If there is already a candidate, use it. */
1819 arches = gdbarch_list_lookup_by_info (arches, &info);
1820 if (arches != NULL)
1821 return arches->gdbarch;
1823 /* Allocate space for the new architecture. */
1824 sparc_gdbarch_tdep *tdep = new sparc_gdbarch_tdep;
1825 gdbarch = gdbarch_alloc (&info, tdep);
1827 tdep->pc_regnum = SPARC32_PC_REGNUM;
1828 tdep->npc_regnum = SPARC32_NPC_REGNUM;
1829 tdep->step_trap = sparc_step_trap;
1830 tdep->fpu_register_names = sparc32_fpu_register_names;
1831 tdep->fpu_registers_num = ARRAY_SIZE (sparc32_fpu_register_names);
1832 tdep->cp0_register_names = sparc32_cp0_register_names;
1833 tdep->cp0_registers_num = ARRAY_SIZE (sparc32_cp0_register_names);
1835 set_gdbarch_long_double_bit (gdbarch, 128);
1836 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
1838 set_gdbarch_wchar_bit (gdbarch, 16);
1839 set_gdbarch_wchar_signed (gdbarch, 1);
1841 set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
1842 set_gdbarch_register_name (gdbarch, sparc32_register_name);
1843 set_gdbarch_register_type (gdbarch, sparc32_register_type);
1844 set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
1845 set_tdesc_pseudo_register_name (gdbarch, sparc32_pseudo_register_name);
1846 set_tdesc_pseudo_register_type (gdbarch, sparc32_pseudo_register_type);
1847 set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
1848 set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
1850 /* Register numbers of various important registers. */
1851 set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
1852 set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
1853 set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
1855 /* Call dummy code. */
1856 set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
1857 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1858 set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
1859 set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
1861 set_gdbarch_return_value (gdbarch, sparc32_return_value);
1862 set_gdbarch_stabs_argument_has_addr
1863 (gdbarch, sparc32_stabs_argument_has_addr);
1865 set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
1867 /* Stack grows downward. */
1868 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1870 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1871 sparc_breakpoint::kind_from_pc);
1872 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1873 sparc_breakpoint::bp_from_kind);
1875 set_gdbarch_frame_args_skip (gdbarch, 8);
1877 set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
1878 set_gdbarch_write_pc (gdbarch, sparc_write_pc);
1880 set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
1882 frame_base_set_default (gdbarch, &sparc32_frame_base);
1884 /* Hook in the DWARF CFI frame unwinder. */
1885 dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
1886 /* Register DWARF vendor CFI handler. */
1887 set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
1888 sparc_execute_dwarf_cfa_vendor_op);
1889 /* FIXME: kettenis/20050423: Don't enable the unwinder until the
1890 StackGhost issues have been resolved. */
1892 /* Hook in ABI-specific overrides, if they have been registered. */
1893 gdbarch_init_osabi (info, gdbarch);
1895 frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
1897 if (tdesc_has_registers (tdesc))
1899 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
1901 /* Validate that the descriptor provides the mandatory registers
1902 and allocate their numbers. */
1903 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1904 "org.gnu.gdb.sparc.cpu",
1905 sparc_core_register_names,
1906 ARRAY_SIZE (sparc_core_register_names),
1907 SPARC_G0_REGNUM);
1908 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1909 "org.gnu.gdb.sparc.fpu",
1910 tdep->fpu_register_names,
1911 tdep->fpu_registers_num,
1912 SPARC_F0_REGNUM);
1913 valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
1914 "org.gnu.gdb.sparc.cp0",
1915 tdep->cp0_register_names,
1916 tdep->cp0_registers_num,
1917 SPARC_F0_REGNUM
1918 + tdep->fpu_registers_num);
1919 if (!valid_p)
1920 return NULL;
1922 /* Target description may have changed. */
1923 info.tdesc_data = tdesc_data.get ();
1924 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1927 /* If we have register sets, enable the generic core file support. */
1928 if (tdep->gregset)
1929 set_gdbarch_iterate_over_regset_sections
1930 (gdbarch, sparc_iterate_over_regset_sections);
1932 register_sparc_ravenscar_ops (gdbarch);
1934 return gdbarch;
1937 /* Helper functions for dealing with register windows. */
1939 void
1940 sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
1942 struct gdbarch *gdbarch = regcache->arch ();
1943 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1944 int offset = 0;
1945 gdb_byte buf[8];
1946 int i;
1948 /* This function calls functions that depend on the global current thread. */
1949 gdb_assert (regcache->ptid () == inferior_ptid);
1951 if (sp & 1)
1953 /* Registers are 64-bit. */
1954 sp += BIAS;
1956 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1958 if (regnum == i || regnum == -1)
1960 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
1962 /* Handle StackGhost. */
1963 if (i == SPARC_I7_REGNUM)
1965 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
1966 ULONGEST i7;
1968 i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
1969 store_unsigned_integer (buf + offset, 8, byte_order,
1970 i7 ^ wcookie);
1973 regcache->raw_supply (i, buf);
1977 else
1979 /* Registers are 32-bit. Toss any sign-extension of the stack
1980 pointer. */
1981 sp &= 0xffffffffUL;
1983 /* Clear out the top half of the temporary buffer, and put the
1984 register value in the bottom half if we're in 64-bit mode. */
1985 if (gdbarch_ptr_bit (regcache->arch ()) == 64)
1987 memset (buf, 0, 4);
1988 offset = 4;
1991 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
1993 if (regnum == i || regnum == -1)
1995 target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
1996 buf + offset, 4);
1998 /* Handle StackGhost. */
1999 if (i == SPARC_I7_REGNUM)
2001 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2002 ULONGEST i7;
2004 i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
2005 store_unsigned_integer (buf + offset, 4, byte_order,
2006 i7 ^ wcookie);
2009 regcache->raw_supply (i, buf);
2015 void
2016 sparc_collect_rwindow (const struct regcache *regcache,
2017 CORE_ADDR sp, int regnum)
2019 struct gdbarch *gdbarch = regcache->arch ();
2020 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2021 int offset = 0;
2022 gdb_byte buf[8];
2023 int i;
2025 /* This function calls functions that depend on the global current thread. */
2026 gdb_assert (regcache->ptid () == inferior_ptid);
2028 if (sp & 1)
2030 /* Registers are 64-bit. */
2031 sp += BIAS;
2033 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2035 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
2037 regcache->raw_collect (i, buf);
2039 /* Handle StackGhost. */
2040 if (i == SPARC_I7_REGNUM)
2042 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2043 ULONGEST i7;
2045 i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
2046 store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
2049 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
2053 else
2055 /* Registers are 32-bit. Toss any sign-extension of the stack
2056 pointer. */
2057 sp &= 0xffffffffUL;
2059 /* Only use the bottom half if we're in 64-bit mode. */
2060 if (gdbarch_ptr_bit (regcache->arch ()) == 64)
2061 offset = 4;
2063 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2065 if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
2067 regcache->raw_collect (i, buf);
2069 /* Handle StackGhost. */
2070 if (i == SPARC_I7_REGNUM)
2072 ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
2073 ULONGEST i7;
2075 i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
2076 store_unsigned_integer (buf + offset, 4, byte_order,
2077 i7 ^ wcookie);
2080 target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
2081 buf + offset, 4);
2087 /* Helper functions for dealing with register sets. */
2089 void
2090 sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
2091 struct regcache *regcache,
2092 int regnum, const void *gregs)
2094 const gdb_byte *regs = (const gdb_byte *) gregs;
2095 gdb_byte zero[4] = { 0 };
2096 int i;
2098 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2099 regcache->raw_supply (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
2101 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2102 regcache->raw_supply (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
2104 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2105 regcache->raw_supply (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
2107 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2108 regcache->raw_supply (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
2110 if (regnum == SPARC_G0_REGNUM || regnum == -1)
2111 regcache->raw_supply (SPARC_G0_REGNUM, &zero);
2113 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2115 int offset = gregmap->r_g1_offset;
2117 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2119 if (regnum == i || regnum == -1)
2120 regcache->raw_supply (i, regs + offset);
2121 offset += 4;
2125 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2127 /* Not all of the register set variants include Locals and
2128 Inputs. For those that don't, we read them off the stack. */
2129 if (gregmap->r_l0_offset == -1)
2131 ULONGEST sp;
2133 regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
2134 sparc_supply_rwindow (regcache, sp, regnum);
2136 else
2138 int offset = gregmap->r_l0_offset;
2140 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2142 if (regnum == i || regnum == -1)
2143 regcache->raw_supply (i, regs + offset);
2144 offset += 4;
2150 void
2151 sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
2152 const struct regcache *regcache,
2153 int regnum, void *gregs)
2155 gdb_byte *regs = (gdb_byte *) gregs;
2156 int i;
2158 if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
2159 regcache->raw_collect (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
2161 if (regnum == SPARC32_PC_REGNUM || regnum == -1)
2162 regcache->raw_collect (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
2164 if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
2165 regcache->raw_collect (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
2167 if (regnum == SPARC32_Y_REGNUM || regnum == -1)
2168 regcache->raw_collect (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
2170 if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
2172 int offset = gregmap->r_g1_offset;
2174 /* %g0 is always zero. */
2175 for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
2177 if (regnum == i || regnum == -1)
2178 regcache->raw_collect (i, regs + offset);
2179 offset += 4;
2183 if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
2185 /* Not all of the register set variants include Locals and
2186 Inputs. For those that don't, we read them off the stack. */
2187 if (gregmap->r_l0_offset != -1)
2189 int offset = gregmap->r_l0_offset;
2191 for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
2193 if (regnum == i || regnum == -1)
2194 regcache->raw_collect (i, regs + offset);
2195 offset += 4;
2201 void
2202 sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
2203 struct regcache *regcache,
2204 int regnum, const void *fpregs)
2206 const gdb_byte *regs = (const gdb_byte *) fpregs;
2207 int i;
2209 for (i = 0; i < 32; i++)
2211 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2212 regcache->raw_supply (SPARC_F0_REGNUM + i,
2213 regs + fpregmap->r_f0_offset + (i * 4));
2216 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2217 regcache->raw_supply (SPARC32_FSR_REGNUM, regs + fpregmap->r_fsr_offset);
2220 void
2221 sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
2222 const struct regcache *regcache,
2223 int regnum, void *fpregs)
2225 gdb_byte *regs = (gdb_byte *) fpregs;
2226 int i;
2228 for (i = 0; i < 32; i++)
2230 if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
2231 regcache->raw_collect (SPARC_F0_REGNUM + i,
2232 regs + fpregmap->r_f0_offset + (i * 4));
2235 if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
2236 regcache->raw_collect (SPARC32_FSR_REGNUM,
2237 regs + fpregmap->r_fsr_offset);
2241 /* SunOS 4. */
2243 /* From <machine/reg.h>. */
2244 const struct sparc_gregmap sparc32_sunos4_gregmap =
2246 0 * 4, /* %psr */
2247 1 * 4, /* %pc */
2248 2 * 4, /* %npc */
2249 3 * 4, /* %y */
2250 -1, /* %wim */
2251 -1, /* %tbr */
2252 4 * 4, /* %g1 */
2253 -1 /* %l0 */
2256 const struct sparc_fpregmap sparc32_sunos4_fpregmap =
2258 0 * 4, /* %f0 */
2259 33 * 4, /* %fsr */
2262 const struct sparc_fpregmap sparc32_bsd_fpregmap =
2264 0 * 4, /* %f0 */
2265 32 * 4, /* %fsr */
2268 void _initialize_sparc_tdep ();
2269 void
2270 _initialize_sparc_tdep ()
2272 gdbarch_register (bfd_arch_sparc, sparc32_gdbarch_init);